]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.c
de059b7f15acf9a59f23e6e7c9b1c5372e69e7b4
[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 ff_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->nb_outputs <= srcpad || dst->nb_inputs <= 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->nb_inputs; 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->nb_inputs != 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                 av_log(link->src, AV_LOG_ERROR,
168                        "Failed to configure output pad on %s\n",
169                        link->src->name);
170                 return ret;
171             }
172
173             if (link->time_base.num == 0 && link->time_base.den == 0)
174                 link->time_base = link->src && link->src->nb_inputs ?
175                     link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
176
177             if (link->type == AVMEDIA_TYPE_VIDEO) {
178                 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
179                     link->sample_aspect_ratio = link->src->nb_inputs ?
180                         link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
181
182                 if (link->src->nb_inputs) {
183                     if (!link->w)
184                         link->w = link->src->inputs[0]->w;
185                     if (!link->h)
186                         link->h = link->src->inputs[0]->h;
187                 } else if (!link->w || !link->h) {
188                     av_log(link->src, AV_LOG_ERROR,
189                            "Video source filters must set their output link's "
190                            "width and height\n");
191                     return AVERROR(EINVAL);
192                 }
193             }
194
195             if ((config_link = link->dstpad->config_props))
196                 if ((ret = config_link(link)) < 0) {
197                     av_log(link->src, AV_LOG_ERROR,
198                            "Failed to configure input pad on %s\n",
199                            link->dst->name);
200                     return ret;
201                 }
202
203             link->init_state = AVLINK_INIT;
204         }
205     }
206
207     return 0;
208 }
209
210 void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
211 {
212     if (link->type == AVMEDIA_TYPE_VIDEO) {
213         av_dlog(ctx,
214                 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
215                 link, link->w, link->h,
216                 av_pix_fmt_descriptors[link->format].name,
217                 link->src ? link->src->filter->name : "",
218                 link->dst ? link->dst->filter->name : "",
219                 end ? "\n" : "");
220     } else {
221         char buf[128];
222         av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
223
224         av_dlog(ctx,
225                 "link[%p r:%"PRId64" cl:%s fmt:%-16s %-16s->%-16s]%s",
226                 link, link->sample_rate, buf,
227                 av_get_sample_fmt_name(link->format),
228                 link->src ? link->src->filter->name : "",
229                 link->dst ? link->dst->filter->name : "",
230                 end ? "\n" : "");
231     }
232 }
233
234 int ff_request_frame(AVFilterLink *link)
235 {
236     FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
237
238     if (link->srcpad->request_frame)
239         return link->srcpad->request_frame(link);
240     else if (link->src->inputs[0])
241         return ff_request_frame(link->src->inputs[0]);
242     else return -1;
243 }
244
245 int ff_poll_frame(AVFilterLink *link)
246 {
247     int i, min = INT_MAX;
248
249     if (link->srcpad->poll_frame)
250         return link->srcpad->poll_frame(link);
251
252     for (i = 0; i < link->src->nb_inputs; i++) {
253         int val;
254         if (!link->src->inputs[i])
255             return -1;
256         val = ff_poll_frame(link->src->inputs[i]);
257         min = FFMIN(min, val);
258     }
259
260     return min;
261 }
262
263 #define MAX_REGISTERED_AVFILTERS_NB 64
264
265 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
266
267 static int next_registered_avfilter_idx = 0;
268
269 AVFilter *avfilter_get_by_name(const char *name)
270 {
271     int i;
272
273     for (i = 0; registered_avfilters[i]; i++)
274         if (!strcmp(registered_avfilters[i]->name, name))
275             return registered_avfilters[i];
276
277     return NULL;
278 }
279
280 int avfilter_register(AVFilter *filter)
281 {
282     if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
283         return -1;
284
285     registered_avfilters[next_registered_avfilter_idx++] = filter;
286     return 0;
287 }
288
289 AVFilter **av_filter_next(AVFilter **filter)
290 {
291     return filter ? ++filter : &registered_avfilters[0];
292 }
293
294 void avfilter_uninit(void)
295 {
296     memset(registered_avfilters, 0, sizeof(registered_avfilters));
297     next_registered_avfilter_idx = 0;
298 }
299
300 static int pad_count(const AVFilterPad *pads)
301 {
302     int count;
303
304     for(count = 0; pads->name; count ++) pads ++;
305     return count;
306 }
307
308 static const char *filter_name(void *p)
309 {
310     AVFilterContext *filter = p;
311     return filter->filter->name;
312 }
313
314 static const AVClass avfilter_class = {
315     "AVFilter",
316     filter_name,
317     NULL,
318     LIBAVUTIL_VERSION_INT,
319 };
320
321 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
322 {
323     AVFilterContext *ret;
324     *filter_ctx = NULL;
325
326     if (!filter)
327         return AVERROR(EINVAL);
328
329     ret = av_mallocz(sizeof(AVFilterContext));
330     if (!ret)
331         return AVERROR(ENOMEM);
332
333     ret->av_class = &avfilter_class;
334     ret->filter   = filter;
335     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
336     if (filter->priv_size) {
337         ret->priv     = av_mallocz(filter->priv_size);
338         if (!ret->priv)
339             goto err;
340     }
341
342     ret->nb_inputs = pad_count(filter->inputs);
343     if (ret->nb_inputs ) {
344         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
345         if (!ret->input_pads)
346             goto err;
347         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
348         ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
349         if (!ret->inputs)
350             goto err;
351     }
352
353     ret->nb_outputs = pad_count(filter->outputs);
354     if (ret->nb_outputs) {
355         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
356         if (!ret->output_pads)
357             goto err;
358         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
359         ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
360         if (!ret->outputs)
361             goto err;
362     }
363 #if FF_API_FOO_COUNT
364     ret->output_count = ret->nb_outputs;
365     ret->input_count  = ret->nb_inputs;
366 #endif
367
368     *filter_ctx = ret;
369     return 0;
370
371 err:
372     av_freep(&ret->inputs);
373     av_freep(&ret->input_pads);
374     ret->nb_inputs = 0;
375     av_freep(&ret->outputs);
376     av_freep(&ret->output_pads);
377     ret->nb_outputs = 0;
378     av_freep(&ret->priv);
379     av_free(ret);
380     return AVERROR(ENOMEM);
381 }
382
383 void avfilter_free(AVFilterContext *filter)
384 {
385     int i;
386     AVFilterLink *link;
387
388     if (filter->filter->uninit)
389         filter->filter->uninit(filter);
390
391     for (i = 0; i < filter->nb_inputs; i++) {
392         if ((link = filter->inputs[i])) {
393             if (link->src)
394                 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
395             ff_formats_unref(&link->in_formats);
396             ff_formats_unref(&link->out_formats);
397             ff_formats_unref(&link->in_samplerates);
398             ff_formats_unref(&link->out_samplerates);
399             ff_channel_layouts_unref(&link->in_channel_layouts);
400             ff_channel_layouts_unref(&link->out_channel_layouts);
401         }
402         av_freep(&link);
403     }
404     for (i = 0; i < filter->nb_outputs; i++) {
405         if ((link = filter->outputs[i])) {
406             if (link->dst)
407                 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
408             ff_formats_unref(&link->in_formats);
409             ff_formats_unref(&link->out_formats);
410             ff_formats_unref(&link->in_samplerates);
411             ff_formats_unref(&link->out_samplerates);
412             ff_channel_layouts_unref(&link->in_channel_layouts);
413             ff_channel_layouts_unref(&link->out_channel_layouts);
414         }
415         av_freep(&link);
416     }
417
418     av_freep(&filter->name);
419     av_freep(&filter->input_pads);
420     av_freep(&filter->output_pads);
421     av_freep(&filter->inputs);
422     av_freep(&filter->outputs);
423     av_freep(&filter->priv);
424     av_free(filter);
425 }
426
427 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
428 {
429     int ret=0;
430
431     if (filter->filter->init)
432         ret = filter->filter->init(filter, args);
433     return ret;
434 }
435
436 const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx)
437 {
438     return pads[pad_idx].name;
439 }
440
441 enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
442 {
443     return pads[pad_idx].type;
444 }
445
446 #if FF_API_DEFAULT_CONFIG_OUTPUT_LINK
447 int avfilter_default_config_output_link(AVFilterLink *link)
448 {
449     return 0;
450 }
451 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
452                          AVFilterPad **pads, AVFilterLink ***links,
453                          AVFilterPad *newpad)
454 {
455     ff_insert_pad(idx, count, padidx_off, pads, links, newpad);
456 }
457 void avfilter_insert_inpad(AVFilterContext *f, unsigned index,
458                            AVFilterPad *p)
459 {
460     ff_insert_pad(index, &f->nb_inputs, offsetof(AVFilterLink, dstpad),
461                   &f->input_pads, &f->inputs, p);
462 #if FF_API_FOO_COUNT
463     f->input_count = f->nb_inputs;
464 #endif
465 }
466 void avfilter_insert_outpad(AVFilterContext *f, unsigned index,
467                             AVFilterPad *p)
468 {
469     ff_insert_pad(index, &f->nb_outputs, offsetof(AVFilterLink, srcpad),
470                   &f->output_pads, &f->outputs, p);
471 #if FF_API_FOO_COUNT
472     f->output_count = f->nb_outputs;
473 #endif
474 }
475 int avfilter_poll_frame(AVFilterLink *link)
476 {
477     return ff_poll_frame(link);
478 }
479 int avfilter_request_frame(AVFilterLink *link)
480 {
481     return ff_request_frame(link);
482 }
483 #endif