]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.c
lavfi: make formats API private on next bump.
[ffmpeg] / libavfilter / avfilter.c
1 /*
2  * filter layer
3  * Copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /* #define DEBUG */
23
24 #include "libavutil/pixdesc.h"
25 #include "libavutil/rational.h"
26 #include "libavutil/audioconvert.h"
27
28 #include "avfilter.h"
29 #include "formats.h"
30 #include "internal.h"
31
32 unsigned avfilter_version(void) {
33     return LIBAVFILTER_VERSION_INT;
34 }
35
36 const char *avfilter_configuration(void)
37 {
38     return LIBAV_CONFIGURATION;
39 }
40
41 const char *avfilter_license(void)
42 {
43 #define LICENSE_PREFIX "libavfilter license: "
44     return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
45 }
46
47 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
48                          AVFilterPad **pads, AVFilterLink ***links,
49                          AVFilterPad *newpad)
50 {
51     unsigned i;
52
53     idx = FFMIN(idx, *count);
54
55     *pads  = av_realloc(*pads,  sizeof(AVFilterPad)   * (*count + 1));
56     *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
57     memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad)   * (*count-idx));
58     memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
59     memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
60     (*links)[idx] = NULL;
61
62     (*count)++;
63     for (i = idx+1; i < *count; i++)
64         if (*links[i])
65             (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
66 }
67
68 int avfilter_link(AVFilterContext *src, unsigned srcpad,
69                   AVFilterContext *dst, unsigned dstpad)
70 {
71     AVFilterLink *link;
72
73     if (src->output_count <= srcpad || dst->input_count <= dstpad ||
74         src->outputs[srcpad]        || dst->inputs[dstpad])
75         return -1;
76
77     if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
78         av_log(src, AV_LOG_ERROR,
79                "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
80                src->name, srcpad, dst->name, dstpad);
81         return AVERROR(EINVAL);
82     }
83
84     src->outputs[srcpad] =
85     dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
86
87     link->src     = src;
88     link->dst     = dst;
89     link->srcpad  = &src->output_pads[srcpad];
90     link->dstpad  = &dst->input_pads[dstpad];
91     link->type    = src->output_pads[srcpad].type;
92     assert(PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
93     link->format  = -1;
94
95     return 0;
96 }
97
98 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
99                            unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
100 {
101     int ret;
102     unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
103
104     av_log(link->dst, AV_LOG_INFO, "auto-inserting filter '%s' "
105            "between the filter '%s' and the filter '%s'\n",
106            filt->name, link->src->name, link->dst->name);
107
108     link->dst->inputs[dstpad_idx] = NULL;
109     if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
110         /* failed to link output filter to new filter */
111         link->dst->inputs[dstpad_idx] = link;
112         return ret;
113     }
114
115     /* re-hookup the link to the new destination filter we inserted */
116     link->dst = filt;
117     link->dstpad = &filt->input_pads[filt_srcpad_idx];
118     filt->inputs[filt_srcpad_idx] = link;
119
120     /* if any information on supported media formats already exists on the
121      * link, we need to preserve that */
122     if (link->out_formats)
123         ff_formats_changeref(&link->out_formats,
124                                    &filt->outputs[filt_dstpad_idx]->out_formats);
125     if (link->out_samplerates)
126         ff_formats_changeref(&link->out_samplerates,
127                                    &filt->outputs[filt_dstpad_idx]->out_samplerates);
128     if (link->out_channel_layouts)
129         ff_channel_layouts_changeref(&link->out_channel_layouts,
130                                      &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
131
132     return 0;
133 }
134
135 int avfilter_config_links(AVFilterContext *filter)
136 {
137     int (*config_link)(AVFilterLink *);
138     unsigned i;
139     int ret;
140
141     for (i = 0; i < filter->input_count; i ++) {
142         AVFilterLink *link = filter->inputs[i];
143
144         if (!link) continue;
145
146         switch (link->init_state) {
147         case AVLINK_INIT:
148             continue;
149         case AVLINK_STARTINIT:
150             av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
151             return 0;
152         case AVLINK_UNINIT:
153             link->init_state = AVLINK_STARTINIT;
154
155             if ((ret = avfilter_config_links(link->src)) < 0)
156                 return ret;
157
158             if (!(config_link = link->srcpad->config_props)) {
159                 if (link->src->input_count != 1) {
160                     av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
161                                                     "with more than one input "
162                                                     "must set config_props() "
163                                                     "callbacks on all outputs\n");
164                     return AVERROR(EINVAL);
165                 }
166             } else if ((ret = config_link(link)) < 0)
167                 return ret;
168
169             if (link->time_base.num == 0 && link->time_base.den == 0)
170                 link->time_base = link->src && link->src->input_count ?
171                     link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
172
173             if (link->type == AVMEDIA_TYPE_VIDEO) {
174                 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
175                     link->sample_aspect_ratio = link->src->input_count ?
176                         link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
177
178                 if (link->src->input_count) {
179                     if (!link->w)
180                         link->w = link->src->inputs[0]->w;
181                     if (!link->h)
182                         link->h = link->src->inputs[0]->h;
183                 } else if (!link->w || !link->h) {
184                     av_log(link->src, AV_LOG_ERROR,
185                            "Video source filters must set their output link's "
186                            "width and height\n");
187                     return AVERROR(EINVAL);
188                 }
189             }
190
191             if ((config_link = link->dstpad->config_props))
192                 if ((ret = config_link(link)) < 0)
193                     return ret;
194
195             link->init_state = AVLINK_INIT;
196         }
197     }
198
199     return 0;
200 }
201
202 void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
203 {
204     if (link->type == AVMEDIA_TYPE_VIDEO) {
205         av_dlog(ctx,
206                 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
207                 link, link->w, link->h,
208                 av_pix_fmt_descriptors[link->format].name,
209                 link->src ? link->src->filter->name : "",
210                 link->dst ? link->dst->filter->name : "",
211                 end ? "\n" : "");
212     } else {
213         char buf[128];
214         av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
215
216         av_dlog(ctx,
217                 "link[%p r:%"PRId64" cl:%s fmt:%-16s %-16s->%-16s]%s",
218                 link, link->sample_rate, buf,
219                 av_get_sample_fmt_name(link->format),
220                 link->src ? link->src->filter->name : "",
221                 link->dst ? link->dst->filter->name : "",
222                 end ? "\n" : "");
223     }
224 }
225
226 int avfilter_request_frame(AVFilterLink *link)
227 {
228     FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
229
230     if (link->srcpad->request_frame)
231         return link->srcpad->request_frame(link);
232     else if (link->src->inputs[0])
233         return avfilter_request_frame(link->src->inputs[0]);
234     else return -1;
235 }
236
237 int avfilter_poll_frame(AVFilterLink *link)
238 {
239     int i, min = INT_MAX;
240
241     if (link->srcpad->poll_frame)
242         return link->srcpad->poll_frame(link);
243
244     for (i = 0; i < link->src->input_count; i++) {
245         int val;
246         if (!link->src->inputs[i])
247             return -1;
248         val = avfilter_poll_frame(link->src->inputs[i]);
249         min = FFMIN(min, val);
250     }
251
252     return min;
253 }
254
255 #define MAX_REGISTERED_AVFILTERS_NB 64
256
257 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
258
259 static int next_registered_avfilter_idx = 0;
260
261 AVFilter *avfilter_get_by_name(const char *name)
262 {
263     int i;
264
265     for (i = 0; registered_avfilters[i]; i++)
266         if (!strcmp(registered_avfilters[i]->name, name))
267             return registered_avfilters[i];
268
269     return NULL;
270 }
271
272 int avfilter_register(AVFilter *filter)
273 {
274     if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
275         return -1;
276
277     registered_avfilters[next_registered_avfilter_idx++] = filter;
278     return 0;
279 }
280
281 AVFilter **av_filter_next(AVFilter **filter)
282 {
283     return filter ? ++filter : &registered_avfilters[0];
284 }
285
286 void avfilter_uninit(void)
287 {
288     memset(registered_avfilters, 0, sizeof(registered_avfilters));
289     next_registered_avfilter_idx = 0;
290 }
291
292 static int pad_count(const AVFilterPad *pads)
293 {
294     int count;
295
296     for(count = 0; pads->name; count ++) pads ++;
297     return count;
298 }
299
300 static const char *filter_name(void *p)
301 {
302     AVFilterContext *filter = p;
303     return filter->filter->name;
304 }
305
306 static const AVClass avfilter_class = {
307     "AVFilter",
308     filter_name,
309     NULL,
310     LIBAVUTIL_VERSION_INT,
311 };
312
313 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
314 {
315     AVFilterContext *ret;
316     *filter_ctx = NULL;
317
318     if (!filter)
319         return AVERROR(EINVAL);
320
321     ret = av_mallocz(sizeof(AVFilterContext));
322     if (!ret)
323         return AVERROR(ENOMEM);
324
325     ret->av_class = &avfilter_class;
326     ret->filter   = filter;
327     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
328     if (filter->priv_size) {
329         ret->priv     = av_mallocz(filter->priv_size);
330         if (!ret->priv)
331             goto err;
332     }
333
334     ret->input_count  = pad_count(filter->inputs);
335     if (ret->input_count) {
336         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->input_count);
337         if (!ret->input_pads)
338             goto err;
339         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->input_count);
340         ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->input_count);
341         if (!ret->inputs)
342             goto err;
343     }
344
345     ret->output_count = pad_count(filter->outputs);
346     if (ret->output_count) {
347         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->output_count);
348         if (!ret->output_pads)
349             goto err;
350         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->output_count);
351         ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->output_count);
352         if (!ret->outputs)
353             goto err;
354     }
355
356     *filter_ctx = ret;
357     return 0;
358
359 err:
360     av_freep(&ret->inputs);
361     av_freep(&ret->input_pads);
362     ret->input_count = 0;
363     av_freep(&ret->outputs);
364     av_freep(&ret->output_pads);
365     ret->output_count = 0;
366     av_freep(&ret->priv);
367     av_free(ret);
368     return AVERROR(ENOMEM);
369 }
370
371 void avfilter_free(AVFilterContext *filter)
372 {
373     int i;
374     AVFilterLink *link;
375
376     if (filter->filter->uninit)
377         filter->filter->uninit(filter);
378
379     for (i = 0; i < filter->input_count; i++) {
380         if ((link = filter->inputs[i])) {
381             if (link->src)
382                 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
383             ff_formats_unref(&link->in_formats);
384             ff_formats_unref(&link->out_formats);
385             ff_formats_unref(&link->in_samplerates);
386             ff_formats_unref(&link->out_samplerates);
387             ff_channel_layouts_unref(&link->in_channel_layouts);
388             ff_channel_layouts_unref(&link->out_channel_layouts);
389         }
390         av_freep(&link);
391     }
392     for (i = 0; i < filter->output_count; i++) {
393         if ((link = filter->outputs[i])) {
394             if (link->dst)
395                 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
396             ff_formats_unref(&link->in_formats);
397             ff_formats_unref(&link->out_formats);
398             ff_formats_unref(&link->in_samplerates);
399             ff_formats_unref(&link->out_samplerates);
400             ff_channel_layouts_unref(&link->in_channel_layouts);
401             ff_channel_layouts_unref(&link->out_channel_layouts);
402         }
403         av_freep(&link);
404     }
405
406     av_freep(&filter->name);
407     av_freep(&filter->input_pads);
408     av_freep(&filter->output_pads);
409     av_freep(&filter->inputs);
410     av_freep(&filter->outputs);
411     av_freep(&filter->priv);
412     av_free(filter);
413 }
414
415 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
416 {
417     int ret=0;
418
419     if (filter->filter->init)
420         ret = filter->filter->init(filter, args, opaque);
421     return ret;
422 }
423
424 #if FF_API_DEFAULT_CONFIG_OUTPUT_LINK
425 int avfilter_default_config_output_link(AVFilterLink *link)
426 {
427     return 0;
428 }
429 #endif