]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.c
lavfi: switch to an AVOptions-based system.
[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/avstring.h"
25 #include "libavutil/channel_layout.h"
26 #include "libavutil/common.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/rational.h"
31 #include "libavutil/samplefmt.h"
32
33 #include "audio.h"
34 #include "avfilter.h"
35 #include "formats.h"
36 #include "internal.h"
37 #include "video.h"
38
39 unsigned avfilter_version(void) {
40     return LIBAVFILTER_VERSION_INT;
41 }
42
43 const char *avfilter_configuration(void)
44 {
45     return LIBAV_CONFIGURATION;
46 }
47
48 const char *avfilter_license(void)
49 {
50 #define LICENSE_PREFIX "libavfilter license: "
51     return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
52 }
53
54 void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
55                    AVFilterPad **pads, AVFilterLink ***links,
56                    AVFilterPad *newpad)
57 {
58     unsigned i;
59
60     idx = FFMIN(idx, *count);
61
62     *pads  = av_realloc(*pads,  sizeof(AVFilterPad)   * (*count + 1));
63     *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
64     memmove(*pads +idx+1, *pads +idx, sizeof(AVFilterPad)   * (*count-idx));
65     memmove(*links+idx+1, *links+idx, sizeof(AVFilterLink*) * (*count-idx));
66     memcpy(*pads+idx, newpad, sizeof(AVFilterPad));
67     (*links)[idx] = NULL;
68
69     (*count)++;
70     for (i = idx+1; i < *count; i++)
71         if (*links[i])
72             (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
73 }
74
75 int avfilter_link(AVFilterContext *src, unsigned srcpad,
76                   AVFilterContext *dst, unsigned dstpad)
77 {
78     AVFilterLink *link;
79
80     if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
81         src->outputs[srcpad]      || dst->inputs[dstpad])
82         return -1;
83
84     if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
85         av_log(src, AV_LOG_ERROR,
86                "Media type mismatch between the '%s' filter output pad %d and the '%s' filter input pad %d\n",
87                src->name, srcpad, dst->name, dstpad);
88         return AVERROR(EINVAL);
89     }
90
91     src->outputs[srcpad] =
92     dst-> inputs[dstpad] = link = av_mallocz(sizeof(AVFilterLink));
93
94     link->src     = src;
95     link->dst     = dst;
96     link->srcpad  = &src->output_pads[srcpad];
97     link->dstpad  = &dst->input_pads[dstpad];
98     link->type    = src->output_pads[srcpad].type;
99     assert(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
100     link->format  = -1;
101
102     return 0;
103 }
104
105 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
106                            unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
107 {
108     int ret;
109     unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
110
111     av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
112            "between the filter '%s' and the filter '%s'\n",
113            filt->name, link->src->name, link->dst->name);
114
115     link->dst->inputs[dstpad_idx] = NULL;
116     if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
117         /* failed to link output filter to new filter */
118         link->dst->inputs[dstpad_idx] = link;
119         return ret;
120     }
121
122     /* re-hookup the link to the new destination filter we inserted */
123     link->dst = filt;
124     link->dstpad = &filt->input_pads[filt_srcpad_idx];
125     filt->inputs[filt_srcpad_idx] = link;
126
127     /* if any information on supported media formats already exists on the
128      * link, we need to preserve that */
129     if (link->out_formats)
130         ff_formats_changeref(&link->out_formats,
131                                    &filt->outputs[filt_dstpad_idx]->out_formats);
132     if (link->out_samplerates)
133         ff_formats_changeref(&link->out_samplerates,
134                                    &filt->outputs[filt_dstpad_idx]->out_samplerates);
135     if (link->out_channel_layouts)
136         ff_channel_layouts_changeref(&link->out_channel_layouts,
137                                      &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
138
139     return 0;
140 }
141
142 int avfilter_config_links(AVFilterContext *filter)
143 {
144     int (*config_link)(AVFilterLink *);
145     unsigned i;
146     int ret;
147
148     for (i = 0; i < filter->nb_inputs; i ++) {
149         AVFilterLink *link = filter->inputs[i];
150
151         if (!link) continue;
152
153         switch (link->init_state) {
154         case AVLINK_INIT:
155             continue;
156         case AVLINK_STARTINIT:
157             av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
158             return 0;
159         case AVLINK_UNINIT:
160             link->init_state = AVLINK_STARTINIT;
161
162             if ((ret = avfilter_config_links(link->src)) < 0)
163                 return ret;
164
165             if (!(config_link = link->srcpad->config_props)) {
166                 if (link->src->nb_inputs != 1) {
167                     av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
168                                                     "with more than one input "
169                                                     "must set config_props() "
170                                                     "callbacks on all outputs\n");
171                     return AVERROR(EINVAL);
172                 }
173             } else if ((ret = config_link(link)) < 0) {
174                 av_log(link->src, AV_LOG_ERROR,
175                        "Failed to configure output pad on %s\n",
176                        link->src->name);
177                 return ret;
178             }
179
180             if (link->time_base.num == 0 && link->time_base.den == 0)
181                 link->time_base = link->src && link->src->nb_inputs ?
182                     link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
183
184             if (link->type == AVMEDIA_TYPE_VIDEO) {
185                 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
186                     link->sample_aspect_ratio = link->src->nb_inputs ?
187                         link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
188
189                 if (link->src->nb_inputs) {
190                     if (!link->w)
191                         link->w = link->src->inputs[0]->w;
192                     if (!link->h)
193                         link->h = link->src->inputs[0]->h;
194                 } else if (!link->w || !link->h) {
195                     av_log(link->src, AV_LOG_ERROR,
196                            "Video source filters must set their output link's "
197                            "width and height\n");
198                     return AVERROR(EINVAL);
199                 }
200             }
201
202             if ((config_link = link->dstpad->config_props))
203                 if ((ret = config_link(link)) < 0) {
204                     av_log(link->src, AV_LOG_ERROR,
205                            "Failed to configure input pad on %s\n",
206                            link->dst->name);
207                     return ret;
208                 }
209
210             link->init_state = AVLINK_INIT;
211         }
212     }
213
214     return 0;
215 }
216
217 void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
218 {
219     if (link->type == AVMEDIA_TYPE_VIDEO) {
220         av_dlog(ctx,
221                 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
222                 link, link->w, link->h,
223                 av_get_pix_fmt_name(link->format),
224                 link->src ? link->src->filter->name : "",
225                 link->dst ? link->dst->filter->name : "",
226                 end ? "\n" : "");
227     } else {
228         char buf[128];
229         av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
230
231         av_dlog(ctx,
232                 "link[%p r:%d cl:%s fmt:%-16s %-16s->%-16s]%s",
233                 link, link->sample_rate, buf,
234                 av_get_sample_fmt_name(link->format),
235                 link->src ? link->src->filter->name : "",
236                 link->dst ? link->dst->filter->name : "",
237                 end ? "\n" : "");
238     }
239 }
240
241 int ff_request_frame(AVFilterLink *link)
242 {
243     FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
244
245     if (link->srcpad->request_frame)
246         return link->srcpad->request_frame(link);
247     else if (link->src->inputs[0])
248         return ff_request_frame(link->src->inputs[0]);
249     else return -1;
250 }
251
252 int ff_poll_frame(AVFilterLink *link)
253 {
254     int i, min = INT_MAX;
255
256     if (link->srcpad->poll_frame)
257         return link->srcpad->poll_frame(link);
258
259     for (i = 0; i < link->src->nb_inputs; i++) {
260         int val;
261         if (!link->src->inputs[i])
262             return -1;
263         val = ff_poll_frame(link->src->inputs[i]);
264         min = FFMIN(min, val);
265     }
266
267     return min;
268 }
269
270 #define MAX_REGISTERED_AVFILTERS_NB 64
271
272 static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
273
274 static int next_registered_avfilter_idx = 0;
275
276 AVFilter *avfilter_get_by_name(const char *name)
277 {
278     int i;
279
280     for (i = 0; registered_avfilters[i]; i++)
281         if (!strcmp(registered_avfilters[i]->name, name))
282             return registered_avfilters[i];
283
284     return NULL;
285 }
286
287 int avfilter_register(AVFilter *filter)
288 {
289     if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB)
290         return -1;
291
292     registered_avfilters[next_registered_avfilter_idx++] = filter;
293     return 0;
294 }
295
296 AVFilter **av_filter_next(AVFilter **filter)
297 {
298     return filter ? ++filter : &registered_avfilters[0];
299 }
300
301 void avfilter_uninit(void)
302 {
303     memset(registered_avfilters, 0, sizeof(registered_avfilters));
304     next_registered_avfilter_idx = 0;
305 }
306
307 static int pad_count(const AVFilterPad *pads)
308 {
309     int count;
310
311     if (!pads)
312         return 0;
313
314     for(count = 0; pads->name; count ++) pads ++;
315     return count;
316 }
317
318 static const char *filter_name(void *p)
319 {
320     AVFilterContext *filter = p;
321     return filter->filter->name;
322 }
323
324 static const AVClass avfilter_class = {
325     .class_name = "AVFilter",
326     .item_name  = filter_name,
327     .version    = LIBAVUTIL_VERSION_INT,
328 };
329
330 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
331 {
332     AVFilterContext *ret;
333     *filter_ctx = NULL;
334
335     if (!filter)
336         return AVERROR(EINVAL);
337
338     ret = av_mallocz(sizeof(AVFilterContext));
339     if (!ret)
340         return AVERROR(ENOMEM);
341
342     ret->av_class = &avfilter_class;
343     ret->filter   = filter;
344     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
345     if (filter->priv_size) {
346         ret->priv     = av_mallocz(filter->priv_size);
347         if (!ret->priv)
348             goto err;
349     }
350
351     if (filter->priv_class) {
352         *(const AVClass**)ret->priv = filter->priv_class;
353         av_opt_set_defaults(ret->priv);
354     }
355
356     ret->nb_inputs = pad_count(filter->inputs);
357     if (ret->nb_inputs ) {
358         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
359         if (!ret->input_pads)
360             goto err;
361         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
362         ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
363         if (!ret->inputs)
364             goto err;
365     }
366
367     ret->nb_outputs = pad_count(filter->outputs);
368     if (ret->nb_outputs) {
369         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
370         if (!ret->output_pads)
371             goto err;
372         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
373         ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
374         if (!ret->outputs)
375             goto err;
376     }
377 #if FF_API_FOO_COUNT
378     ret->output_count = ret->nb_outputs;
379     ret->input_count  = ret->nb_inputs;
380 #endif
381
382     *filter_ctx = ret;
383     return 0;
384
385 err:
386     av_freep(&ret->inputs);
387     av_freep(&ret->input_pads);
388     ret->nb_inputs = 0;
389     av_freep(&ret->outputs);
390     av_freep(&ret->output_pads);
391     ret->nb_outputs = 0;
392     av_freep(&ret->priv);
393     av_free(ret);
394     return AVERROR(ENOMEM);
395 }
396
397 void avfilter_free(AVFilterContext *filter)
398 {
399     int i;
400     AVFilterLink *link;
401
402     if (filter->filter->uninit)
403         filter->filter->uninit(filter);
404
405     for (i = 0; i < filter->nb_inputs; i++) {
406         if ((link = filter->inputs[i])) {
407             if (link->src)
408                 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
409             ff_formats_unref(&link->in_formats);
410             ff_formats_unref(&link->out_formats);
411             ff_formats_unref(&link->in_samplerates);
412             ff_formats_unref(&link->out_samplerates);
413             ff_channel_layouts_unref(&link->in_channel_layouts);
414             ff_channel_layouts_unref(&link->out_channel_layouts);
415         }
416         av_freep(&link);
417     }
418     for (i = 0; i < filter->nb_outputs; i++) {
419         if ((link = filter->outputs[i])) {
420             if (link->dst)
421                 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
422             ff_formats_unref(&link->in_formats);
423             ff_formats_unref(&link->out_formats);
424             ff_formats_unref(&link->in_samplerates);
425             ff_formats_unref(&link->out_samplerates);
426             ff_channel_layouts_unref(&link->in_channel_layouts);
427             ff_channel_layouts_unref(&link->out_channel_layouts);
428         }
429         av_freep(&link);
430     }
431
432     if (filter->filter->priv_class)
433         av_opt_free(filter->priv);
434
435     av_freep(&filter->name);
436     av_freep(&filter->input_pads);
437     av_freep(&filter->output_pads);
438     av_freep(&filter->inputs);
439     av_freep(&filter->outputs);
440     av_freep(&filter->priv);
441     av_free(filter);
442 }
443
444 /* process a list of value1:value2:..., each value corresponding
445  * to subsequent AVOption, in the order they are declared */
446 static int process_unnamed_options(AVFilterContext *ctx, AVDictionary **options,
447                                    const char *args)
448 {
449     const AVOption *o = NULL;
450     const char *p = args;
451     char *val;
452
453     while (*p) {
454         o = av_opt_next(ctx->priv, o);
455         if (!o) {
456             av_log(ctx, AV_LOG_ERROR, "More options provided than "
457                    "this filter supports.\n");
458             return AVERROR(EINVAL);
459         }
460         if (o->type == AV_OPT_TYPE_CONST)
461             continue;
462
463         val = av_get_token(&p, ":");
464         if (!val)
465             return AVERROR(ENOMEM);
466
467         av_dict_set(options, o->name, val, 0);
468
469         av_freep(&val);
470         if (*p)
471             p++;
472     }
473
474     return 0;
475 }
476
477 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
478 {
479     AVDictionary *options = NULL;
480     AVDictionaryEntry *e;
481     int ret=0;
482
483     if (args && *args && filter->filter->priv_class) {
484         if (strchr(args, '=')) {
485             /* assume a list of key1=value1:key2=value2:... */
486             ret = av_dict_parse_string(&options, args, "=", ":", 0);
487             if (ret < 0)
488                 goto fail;
489         } else {
490             ret = process_unnamed_options(filter, &options, args);
491             if (ret < 0)
492                 goto fail;
493         }
494     }
495
496     if (filter->filter->priv_class) {
497         ret = av_opt_set_dict(filter->priv, &options);
498         if (ret < 0) {
499             av_log(filter, AV_LOG_ERROR, "Error applying options to the filter.\n");
500             goto fail;
501         }
502     }
503
504     if (filter->filter->init) {
505         ret = filter->filter->init(filter, args);
506         if (ret < 0)
507             goto fail;
508     }
509
510     if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
511         av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
512         ret = AVERROR_OPTION_NOT_FOUND;
513         goto fail;
514     }
515
516 fail:
517     av_dict_free(&options);
518
519     return ret;
520 }
521
522 const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx)
523 {
524     return pads[pad_idx].name;
525 }
526
527 enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
528 {
529     return pads[pad_idx].type;
530 }
531
532 static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
533 {
534     return ff_filter_frame(link->dst->outputs[0], frame);
535 }
536
537 int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
538 {
539     int (*filter_frame)(AVFilterLink *, AVFrame *);
540     AVFilterPad *dst = link->dstpad;
541     AVFrame *out;
542
543     FF_DPRINTF_START(NULL, filter_frame);
544     ff_dlog_link(NULL, link, 1);
545
546     if (!(filter_frame = dst->filter_frame))
547         filter_frame = default_filter_frame;
548
549     /* copy the frame if needed */
550     if (dst->needs_writable && !av_frame_is_writable(frame)) {
551         av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
552
553         switch (link->type) {
554         case AVMEDIA_TYPE_VIDEO:
555             out = ff_get_video_buffer(link, link->w, link->h);
556             break;
557         case AVMEDIA_TYPE_AUDIO:
558             out = ff_get_audio_buffer(link, frame->nb_samples);
559             break;
560         default: return AVERROR(EINVAL);
561         }
562         if (!out) {
563             av_frame_free(&frame);
564             return AVERROR(ENOMEM);
565         }
566         av_frame_copy_props(out, frame);
567
568         switch (link->type) {
569         case AVMEDIA_TYPE_VIDEO:
570             av_image_copy(out->data, out->linesize, frame->data, frame->linesize,
571                           frame->format, frame->width, frame->height);
572             break;
573         case AVMEDIA_TYPE_AUDIO:
574             av_samples_copy(out->extended_data, frame->extended_data,
575                             0, 0, frame->nb_samples,
576                             av_get_channel_layout_nb_channels(frame->channel_layout),
577                             frame->format);
578             break;
579         default: return AVERROR(EINVAL);
580         }
581
582         av_frame_free(&frame);
583     } else
584         out = frame;
585
586     return filter_frame(link, out);
587 }