OSDN Git Service

libspeex encoder wraper
[coroid/ffmpeg_saccubus.git] / libavfilter / defaults.c
1 /*
2  * Filter layer - default implementations
3  * Copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/audioconvert.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/samplefmt.h"
25 #include "avfilter.h"
26 #include "internal.h"
27
28 void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
29 {
30     av_free(ptr->data[0]);
31     av_free(ptr);
32 }
33
34 /* TODO: set the buffer's priv member to a context structure for the whole
35  * filter chain.  This will allow for a buffer pool instead of the constant
36  * alloc & free cycle currently implemented. */
37 AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
38 {
39     int linesize[4];
40     uint8_t *data[4];
41     int i;
42     AVFilterBufferRef *picref = NULL;
43     AVFilterPool *pool = link->pool;
44
45     if (pool) {
46         for (i = 0; i < POOL_SIZE; i++) {
47             picref = pool->pic[i];
48             if (picref && picref->buf->format == link->format && picref->buf->w == w && picref->buf->h == h) {
49                 AVFilterBuffer *pic = picref->buf;
50                 pool->pic[i] = NULL;
51                 pool->count--;
52                 picref->video->w = w;
53                 picref->video->h = h;
54                 picref->perms = perms | AV_PERM_READ;
55                 picref->format = link->format;
56                 pic->refcount = 1;
57                 memcpy(picref->data,     pic->data,     sizeof(picref->data));
58                 memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
59                 return picref;
60             }
61         }
62     } else
63         pool = link->pool = av_mallocz(sizeof(AVFilterPool));
64
65     // align: +2 is needed for swscaler, +16 to be SIMD-friendly
66     if ((i = av_image_alloc(data, linesize, w, h, link->format, 16)) < 0)
67         return NULL;
68
69     picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
70                                                        perms, w, h, link->format);
71     if (!picref) {
72         av_free(data[0]);
73         return NULL;
74     }
75     memset(data[0], 128, i);
76
77     picref->buf->priv = pool;
78     picref->buf->free = NULL;
79
80     return picref;
81 }
82
83 AVFilterBufferRef *avfilter_default_get_audio_buffer(AVFilterLink *link, int perms,
84                                                      enum AVSampleFormat sample_fmt, int nb_samples,
85                                                      int64_t channel_layout, int planar)
86 {
87     AVFilterBufferRef *samplesref = NULL;
88     int linesize[8];
89     uint8_t *data[8];
90     int nb_channels = av_get_channel_layout_nb_channels(channel_layout);
91
92     /* Calculate total buffer size, round to multiple of 16 to be SIMD friendly */
93     if (av_samples_alloc(data, linesize,
94                          nb_channels, nb_samples, sample_fmt,
95                          planar, 16) < 0)
96         return NULL;
97
98     samplesref =
99         avfilter_get_audio_buffer_ref_from_arrays(data, linesize, perms,
100                                                   nb_samples, sample_fmt,
101                                                   channel_layout, planar);
102     if (!samplesref) {
103         av_free(data[0]);
104         return NULL;
105     }
106
107     return samplesref;
108 }
109
110 void avfilter_default_start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
111 {
112     AVFilterLink *outlink = NULL;
113
114     if (inlink->dst->output_count)
115         outlink = inlink->dst->outputs[0];
116
117     if (outlink) {
118         outlink->out_buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
119         avfilter_copy_buffer_ref_props(outlink->out_buf, picref);
120         avfilter_start_frame(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
121     }
122 }
123
124 void avfilter_default_draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
125 {
126     AVFilterLink *outlink = NULL;
127
128     if (inlink->dst->output_count)
129         outlink = inlink->dst->outputs[0];
130
131     if (outlink)
132         avfilter_draw_slice(outlink, y, h, slice_dir);
133 }
134
135 void avfilter_default_end_frame(AVFilterLink *inlink)
136 {
137     AVFilterLink *outlink = NULL;
138
139     if (inlink->dst->output_count)
140         outlink = inlink->dst->outputs[0];
141
142     avfilter_unref_buffer(inlink->cur_buf);
143     inlink->cur_buf = NULL;
144
145     if (outlink) {
146         if (outlink->out_buf) {
147             avfilter_unref_buffer(outlink->out_buf);
148             outlink->out_buf = NULL;
149         }
150         avfilter_end_frame(outlink);
151     }
152 }
153
154 /* FIXME: samplesref is same as link->cur_buf. Need to consider removing the redundant parameter. */
155 void avfilter_default_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
156 {
157     AVFilterLink *outlink = NULL;
158
159     if (inlink->dst->output_count)
160         outlink = inlink->dst->outputs[0];
161
162     if (outlink) {
163         outlink->out_buf = avfilter_default_get_audio_buffer(inlink, AV_PERM_WRITE, samplesref->format,
164                                                              samplesref->audio->nb_samples,
165                                                              samplesref->audio->channel_layout,
166                                                              samplesref->audio->planar);
167         outlink->out_buf->pts                = samplesref->pts;
168         outlink->out_buf->audio->sample_rate = samplesref->audio->sample_rate;
169         avfilter_filter_samples(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
170         avfilter_unref_buffer(outlink->out_buf);
171         outlink->out_buf = NULL;
172     }
173     avfilter_unref_buffer(samplesref);
174     inlink->cur_buf = NULL;
175 }
176
177 static void set_common_formats(AVFilterContext *ctx, AVFilterFormats *fmts,
178                                enum AVMediaType type, int offin, int offout)
179 {
180     int i;
181     for (i = 0; i < ctx->input_count; i++)
182         if (ctx->inputs[i] && ctx->inputs[i]->type == type)
183             avfilter_formats_ref(fmts,
184                                  (AVFilterFormats **)((uint8_t *)ctx->inputs[i]+offout));
185
186     for (i = 0; i < ctx->output_count; i++)
187         if (ctx->outputs[i] && ctx->outputs[i]->type == type)
188             avfilter_formats_ref(fmts,
189                                  (AVFilterFormats **)((uint8_t *)ctx->outputs[i]+offin));
190
191     if (!fmts->refcount) {
192         av_free(fmts->formats);
193         av_free(fmts->refs);
194         av_free(fmts);
195     }
196 }
197
198 void avfilter_set_common_pixel_formats(AVFilterContext *ctx, AVFilterFormats *formats)
199 {
200     set_common_formats(ctx, formats, AVMEDIA_TYPE_VIDEO,
201                        offsetof(AVFilterLink, in_formats),
202                        offsetof(AVFilterLink, out_formats));
203 }
204
205 void avfilter_set_common_sample_formats(AVFilterContext *ctx, AVFilterFormats *formats)
206 {
207     set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
208                        offsetof(AVFilterLink, in_formats),
209                        offsetof(AVFilterLink, out_formats));
210 }
211
212 void avfilter_set_common_channel_layouts(AVFilterContext *ctx, AVFilterFormats *formats)
213 {
214     set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
215                        offsetof(AVFilterLink, in_chlayouts),
216                        offsetof(AVFilterLink, out_chlayouts));
217 }
218
219 void avfilter_set_common_packing_formats(AVFilterContext *ctx, AVFilterFormats *formats)
220 {
221     set_common_formats(ctx, formats, AVMEDIA_TYPE_AUDIO,
222                        offsetof(AVFilterLink, in_packing),
223                        offsetof(AVFilterLink, out_packing));
224 }
225
226 int avfilter_default_query_formats(AVFilterContext *ctx)
227 {
228     avfilter_set_common_pixel_formats(ctx, avfilter_all_formats(AVMEDIA_TYPE_VIDEO));
229     avfilter_set_common_sample_formats(ctx, avfilter_all_formats(AVMEDIA_TYPE_AUDIO));
230     avfilter_set_common_channel_layouts(ctx, avfilter_all_channel_layouts());
231     avfilter_set_common_packing_formats(ctx, avfilter_all_packing_formats());
232
233     return 0;
234 }
235
236 void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
237 {
238     avfilter_start_frame(link->dst->outputs[0], picref);
239 }
240
241 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
242 {
243     avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
244 }
245
246 void avfilter_null_end_frame(AVFilterLink *link)
247 {
248     avfilter_end_frame(link->dst->outputs[0]);
249 }
250
251 void avfilter_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
252 {
253     avfilter_filter_samples(link->dst->outputs[0], samplesref);
254 }
255
256 AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
257 {
258     return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
259 }
260
261 AVFilterBufferRef *avfilter_null_get_audio_buffer(AVFilterLink *link, int perms,
262                                                   enum AVSampleFormat sample_fmt, int size,
263                                                   int64_t channel_layout, int packed)
264 {
265     return avfilter_get_audio_buffer(link->dst->outputs[0], perms, sample_fmt,
266                                      size, channel_layout, packed);
267 }
268