]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.c
twinvq: set block align for codecs and use it in size checks
[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 #include "libavutil/avstring.h"
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/common.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavutil/rational.h"
30 #include "libavutil/samplefmt.h"
31
32 #include "audio.h"
33 #include "avfilter.h"
34 #include "formats.h"
35 #include "internal.h"
36 #include "video.h"
37
38 unsigned avfilter_version(void)
39 {
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     link = av_mallocz(sizeof(*link));
92     if (!link)
93         return AVERROR(ENOMEM);
94
95     src->outputs[srcpad] = dst->inputs[dstpad] = link;
96
97     link->src     = src;
98     link->dst     = dst;
99     link->srcpad  = &src->output_pads[srcpad];
100     link->dstpad  = &dst->input_pads[dstpad];
101     link->type    = src->output_pads[srcpad].type;
102     assert(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
103     link->format  = -1;
104
105     return 0;
106 }
107
108 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
109                            unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
110 {
111     int ret;
112     unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
113
114     av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
115            "between the filter '%s' and the filter '%s'\n",
116            filt->name, link->src->name, link->dst->name);
117
118     link->dst->inputs[dstpad_idx] = NULL;
119     if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
120         /* failed to link output filter to new filter */
121         link->dst->inputs[dstpad_idx] = link;
122         return ret;
123     }
124
125     /* re-hookup the link to the new destination filter we inserted */
126     link->dst                     = filt;
127     link->dstpad                  = &filt->input_pads[filt_srcpad_idx];
128     filt->inputs[filt_srcpad_idx] = link;
129
130     /* if any information on supported media formats already exists on the
131      * link, we need to preserve that */
132     if (link->out_formats)
133         ff_formats_changeref(&link->out_formats,
134                              &filt->outputs[filt_dstpad_idx]->out_formats);
135     if (link->out_samplerates)
136         ff_formats_changeref(&link->out_samplerates,
137                              &filt->outputs[filt_dstpad_idx]->out_samplerates);
138     if (link->out_channel_layouts)
139         ff_channel_layouts_changeref(&link->out_channel_layouts,
140                                      &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
141
142     return 0;
143 }
144
145 int avfilter_config_links(AVFilterContext *filter)
146 {
147     int (*config_link)(AVFilterLink *);
148     unsigned i;
149     int ret;
150
151     for (i = 0; i < filter->nb_inputs; i ++) {
152         AVFilterLink *link = filter->inputs[i];
153
154         if (!link) continue;
155
156         switch (link->init_state) {
157         case AVLINK_INIT:
158             continue;
159         case AVLINK_STARTINIT:
160             av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
161             return 0;
162         case AVLINK_UNINIT:
163             link->init_state = AVLINK_STARTINIT;
164
165             if ((ret = avfilter_config_links(link->src)) < 0)
166                 return ret;
167
168             if (!(config_link = link->srcpad->config_props)) {
169                 if (link->src->nb_inputs != 1) {
170                     av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
171                                                     "with more than one input "
172                                                     "must set config_props() "
173                                                     "callbacks on all outputs\n");
174                     return AVERROR(EINVAL);
175                 }
176             } else if ((ret = config_link(link)) < 0) {
177                 av_log(link->src, AV_LOG_ERROR,
178                        "Failed to configure output pad on %s\n",
179                        link->src->name);
180                 return ret;
181             }
182
183             if (link->time_base.num == 0 && link->time_base.den == 0)
184                 link->time_base = link->src && link->src->nb_inputs ?
185                     link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
186
187             if (link->type == AVMEDIA_TYPE_VIDEO) {
188                 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
189                     link->sample_aspect_ratio = link->src->nb_inputs ?
190                         link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
191
192                 if (link->src->nb_inputs) {
193                     if (!link->w)
194                         link->w = link->src->inputs[0]->w;
195                     if (!link->h)
196                         link->h = link->src->inputs[0]->h;
197                 } else if (!link->w || !link->h) {
198                     av_log(link->src, AV_LOG_ERROR,
199                            "Video source filters must set their output link's "
200                            "width and height\n");
201                     return AVERROR(EINVAL);
202                 }
203             }
204
205             if ((config_link = link->dstpad->config_props))
206                 if ((ret = config_link(link)) < 0) {
207                     av_log(link->src, AV_LOG_ERROR,
208                            "Failed to configure input pad on %s\n",
209                            link->dst->name);
210                     return ret;
211                 }
212
213             link->init_state = AVLINK_INIT;
214         }
215     }
216
217     return 0;
218 }
219
220 void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
221 {
222     if (link->type == AVMEDIA_TYPE_VIDEO) {
223         av_dlog(ctx,
224                 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
225                 link, link->w, link->h,
226                 av_get_pix_fmt_name(link->format),
227                 link->src ? link->src->filter->name : "",
228                 link->dst ? link->dst->filter->name : "",
229                 end ? "\n" : "");
230     } else {
231         char buf[128];
232         av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
233
234         av_dlog(ctx,
235                 "link[%p r:%d cl:%s fmt:%-16s %-16s->%-16s]%s",
236                 link, link->sample_rate, buf,
237                 av_get_sample_fmt_name(link->format),
238                 link->src ? link->src->filter->name : "",
239                 link->dst ? link->dst->filter->name : "",
240                 end ? "\n" : "");
241     }
242 }
243
244 int ff_request_frame(AVFilterLink *link)
245 {
246     FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
247
248     if (link->srcpad->request_frame)
249         return link->srcpad->request_frame(link);
250     else if (link->src->inputs[0])
251         return ff_request_frame(link->src->inputs[0]);
252     else return -1;
253 }
254
255 int ff_poll_frame(AVFilterLink *link)
256 {
257     int i, min = INT_MAX;
258
259     if (link->srcpad->poll_frame)
260         return link->srcpad->poll_frame(link);
261
262     for (i = 0; i < link->src->nb_inputs; i++) {
263         int val;
264         if (!link->src->inputs[i])
265             return -1;
266         val = ff_poll_frame(link->src->inputs[i]);
267         min = FFMIN(min, val);
268     }
269
270     return min;
271 }
272
273 static AVFilter *first_filter;
274
275 AVFilter *avfilter_get_by_name(const char *name)
276 {
277     AVFilter *f = NULL;
278
279     if (!name)
280         return NULL;
281
282     while ((f = avfilter_next(f)))
283         if (!strcmp(f->name, name))
284             return f;
285
286     return NULL;
287 }
288
289 int avfilter_register(AVFilter *filter)
290 {
291     AVFilter **f = &first_filter;
292     while (*f)
293         f = &(*f)->next;
294     *f = filter;
295     filter->next = NULL;
296     return 0;
297 }
298
299 const AVFilter *avfilter_next(const AVFilter *prev)
300 {
301     return prev ? prev->next : first_filter;
302 }
303
304 #if FF_API_OLD_FILTER_REGISTER
305 AVFilter **av_filter_next(AVFilter **filter)
306 {
307     return filter ? &(*filter)->next : &first_filter;
308 }
309
310 void avfilter_uninit(void)
311 {
312 }
313 #endif
314
315 int avfilter_pad_count(const AVFilterPad *pads)
316 {
317     int count;
318
319     if (!pads)
320         return 0;
321
322     for (count = 0; pads->name; count++)
323         pads++;
324     return count;
325 }
326
327 static const char *filter_name(void *p)
328 {
329     AVFilterContext *filter = p;
330     return filter->filter->name;
331 }
332
333 static void *filter_child_next(void *obj, void *prev)
334 {
335     AVFilterContext *ctx = obj;
336     if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
337         return ctx->priv;
338     return NULL;
339 }
340
341 static const AVClass *filter_child_class_next(const AVClass *prev)
342 {
343     AVFilter *f = NULL;
344
345     while (prev && (f = avfilter_next(f)))
346         if (f->priv_class == prev)
347             break;
348
349     while ((f = avfilter_next(f)))
350         if (f->priv_class)
351             return f->priv_class;
352
353     return NULL;
354 }
355
356 #define OFFSET(x) offsetof(AVFilterContext, x)
357 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
358 static const AVOption avfilter_options[] = {
359     { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
360         { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
361         { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .unit = "thread_type" },
362     { NULL },
363 };
364
365 static const AVClass avfilter_class = {
366     .class_name = "AVFilter",
367     .item_name  = filter_name,
368     .version    = LIBAVUTIL_VERSION_INT,
369     .child_next = filter_child_next,
370     .child_class_next = filter_child_class_next,
371     .option           = avfilter_options,
372 };
373
374 static int default_execute(AVFilterContext *ctx, action_func *func, void *arg,
375                            int *ret, int nb_jobs)
376 {
377     int i;
378
379     for (i = 0; i < nb_jobs; i++) {
380         int r = func(ctx, arg, i, nb_jobs);
381         if (ret)
382             ret[i] = r;
383     }
384     return 0;
385 }
386
387 AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
388 {
389     AVFilterContext *ret;
390
391     if (!filter)
392         return NULL;
393
394     ret = av_mallocz(sizeof(AVFilterContext));
395     if (!ret)
396         return NULL;
397
398     ret->av_class = &avfilter_class;
399     ret->filter   = filter;
400     ret->name     = inst_name ? av_strdup(inst_name) : NULL;
401     if (filter->priv_size) {
402         ret->priv     = av_mallocz(filter->priv_size);
403         if (!ret->priv)
404             goto err;
405     }
406
407     av_opt_set_defaults(ret);
408     if (filter->priv_class) {
409         *(const AVClass**)ret->priv = filter->priv_class;
410         av_opt_set_defaults(ret->priv);
411     }
412
413     ret->internal = av_mallocz(sizeof(*ret->internal));
414     if (!ret->internal)
415         goto err;
416     ret->internal->execute = default_execute;
417
418     ret->nb_inputs = avfilter_pad_count(filter->inputs);
419     if (ret->nb_inputs ) {
420         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
421         if (!ret->input_pads)
422             goto err;
423         memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
424         ret->inputs       = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
425         if (!ret->inputs)
426             goto err;
427     }
428
429     ret->nb_outputs = avfilter_pad_count(filter->outputs);
430     if (ret->nb_outputs) {
431         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
432         if (!ret->output_pads)
433             goto err;
434         memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
435         ret->outputs      = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
436         if (!ret->outputs)
437             goto err;
438     }
439 #if FF_API_FOO_COUNT
440 FF_DISABLE_DEPRECATION_WARNINGS
441     ret->output_count = ret->nb_outputs;
442     ret->input_count  = ret->nb_inputs;
443 FF_ENABLE_DEPRECATION_WARNINGS
444 #endif
445
446     return ret;
447
448 err:
449     av_freep(&ret->inputs);
450     av_freep(&ret->input_pads);
451     ret->nb_inputs = 0;
452     av_freep(&ret->outputs);
453     av_freep(&ret->output_pads);
454     ret->nb_outputs = 0;
455     av_freep(&ret->priv);
456     av_freep(&ret->internal);
457     av_free(ret);
458     return NULL;
459 }
460
461 #if FF_API_AVFILTER_OPEN
462 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
463 {
464     *filter_ctx = ff_filter_alloc(filter, inst_name);
465     return *filter_ctx ? 0 : AVERROR(ENOMEM);
466 }
467 #endif
468
469 static void free_link(AVFilterLink *link)
470 {
471     if (!link)
472         return;
473
474     if (link->src)
475         link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
476     if (link->dst)
477         link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
478
479     ff_formats_unref(&link->in_formats);
480     ff_formats_unref(&link->out_formats);
481     ff_formats_unref(&link->in_samplerates);
482     ff_formats_unref(&link->out_samplerates);
483     ff_channel_layouts_unref(&link->in_channel_layouts);
484     ff_channel_layouts_unref(&link->out_channel_layouts);
485     av_freep(&link);
486 }
487
488 void avfilter_free(AVFilterContext *filter)
489 {
490     int i;
491
492     if (filter->graph)
493         ff_filter_graph_remove_filter(filter->graph, filter);
494
495     if (filter->filter->uninit)
496         filter->filter->uninit(filter);
497
498     for (i = 0; i < filter->nb_inputs; i++) {
499         free_link(filter->inputs[i]);
500     }
501     for (i = 0; i < filter->nb_outputs; i++) {
502         free_link(filter->outputs[i]);
503     }
504
505     if (filter->filter->priv_class)
506         av_opt_free(filter->priv);
507
508     av_freep(&filter->name);
509     av_freep(&filter->input_pads);
510     av_freep(&filter->output_pads);
511     av_freep(&filter->inputs);
512     av_freep(&filter->outputs);
513     av_freep(&filter->priv);
514     av_freep(&filter->internal);
515     av_free(filter);
516 }
517
518 /* process a list of value1:value2:..., each value corresponding
519  * to subsequent AVOption, in the order they are declared */
520 static int process_unnamed_options(AVFilterContext *ctx, AVDictionary **options,
521                                    const char *args)
522 {
523     const AVOption *o = NULL;
524     const char *p = args;
525     char *val;
526
527     while (*p) {
528         o = av_opt_next(ctx->priv, o);
529         if (!o) {
530             av_log(ctx, AV_LOG_ERROR, "More options provided than "
531                    "this filter supports.\n");
532             return AVERROR(EINVAL);
533         }
534         if (o->type == AV_OPT_TYPE_CONST)
535             continue;
536
537         val = av_get_token(&p, ":");
538         if (!val)
539             return AVERROR(ENOMEM);
540
541         av_dict_set(options, o->name, val, 0);
542
543         av_freep(&val);
544         if (*p)
545             p++;
546     }
547
548     return 0;
549 }
550
551 #if FF_API_AVFILTER_INIT_FILTER
552 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
553 {
554     return avfilter_init_str(filter, args);
555 }
556 #endif
557
558 int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
559 {
560     int ret = 0;
561
562     ret = av_opt_set_dict(ctx, options);
563     if (ret < 0) {
564         av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
565         return ret;
566     }
567
568     if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
569         ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
570         ctx->graph->internal->thread_execute) {
571         ctx->thread_type       = AVFILTER_THREAD_SLICE;
572         ctx->internal->execute = ctx->graph->internal->thread_execute;
573     } else {
574         ctx->thread_type = 0;
575     }
576
577     if (ctx->filter->priv_class) {
578         ret = av_opt_set_dict(ctx->priv, options);
579         if (ret < 0) {
580             av_log(ctx, AV_LOG_ERROR, "Error applying options to the filter.\n");
581             return ret;
582         }
583     }
584
585     if (ctx->filter->init)
586         ret = ctx->filter->init(ctx);
587     else if (ctx->filter->init_dict)
588         ret = ctx->filter->init_dict(ctx, options);
589
590     return ret;
591 }
592
593 int avfilter_init_str(AVFilterContext *filter, const char *args)
594 {
595     AVDictionary *options = NULL;
596     AVDictionaryEntry *e;
597     int ret = 0;
598
599     if (args && *args) {
600         if (!filter->filter->priv_class) {
601             av_log(filter, AV_LOG_ERROR, "This filter does not take any "
602                    "options, but options were provided: %s.\n", args);
603             return AVERROR(EINVAL);
604         }
605
606 #if FF_API_OLD_FILTER_OPTS
607         if (!strcmp(filter->filter->name, "scale") &&
608             strchr(args, ':') && strchr(args, ':') < strchr(args, '=')) {
609             /* old w:h:flags=<flags> syntax */
610             char *copy = av_strdup(args);
611             char *p;
612
613             av_log(filter, AV_LOG_WARNING, "The <w>:<h>:flags=<flags> option "
614                    "syntax is deprecated. Use either <w>:<h>:<flags> or "
615                    "w=<w>:h=<h>:flags=<flags>.\n");
616
617             if (!copy) {
618                 ret = AVERROR(ENOMEM);
619                 goto fail;
620             }
621
622             p = strrchr(copy, ':');
623             if (p) {
624                 *p++ = 0;
625                 ret = av_dict_parse_string(&options, p, "=", ":", 0);
626             }
627             if (ret >= 0)
628                 ret = process_unnamed_options(filter, &options, copy);
629             av_freep(&copy);
630
631             if (ret < 0)
632                 goto fail;
633         } else
634 #endif
635
636         if (strchr(args, '=')) {
637             /* assume a list of key1=value1:key2=value2:... */
638             ret = av_dict_parse_string(&options, args, "=", ":", 0);
639             if (ret < 0)
640                 goto fail;
641 #if FF_API_OLD_FILTER_OPTS
642         } else if (!strcmp(filter->filter->name, "format")     ||
643                    !strcmp(filter->filter->name, "noformat")   ||
644                    !strcmp(filter->filter->name, "frei0r")     ||
645                    !strcmp(filter->filter->name, "frei0r_src") ||
646                    !strcmp(filter->filter->name, "ocv")) {
647             /* a hack for compatibility with the old syntax
648              * replace colons with |s */
649             char *copy = av_strdup(args);
650             char *p    = copy;
651             int nb_leading = 0; // number of leading colons to skip
652
653             if (!copy) {
654                 ret = AVERROR(ENOMEM);
655                 goto fail;
656             }
657
658             if (!strcmp(filter->filter->name, "frei0r") ||
659                 !strcmp(filter->filter->name, "ocv"))
660                 nb_leading = 1;
661             else if (!strcmp(filter->filter->name, "frei0r_src"))
662                 nb_leading = 3;
663
664             while (nb_leading--) {
665                 p = strchr(p, ':');
666                 if (!p) {
667                     p = copy + strlen(copy);
668                     break;
669                 }
670                 p++;
671             }
672
673             if (strchr(p, ':')) {
674                 av_log(filter, AV_LOG_WARNING, "This syntax is deprecated. Use "
675                        "'|' to separate the list items.\n");
676             }
677
678             while ((p = strchr(p, ':')))
679                 *p++ = '|';
680
681             ret = process_unnamed_options(filter, &options, copy);
682             av_freep(&copy);
683
684             if (ret < 0)
685                 goto fail;
686 #endif
687         } else {
688             ret = process_unnamed_options(filter, &options, args);
689             if (ret < 0)
690                 goto fail;
691         }
692     }
693
694     ret = avfilter_init_dict(filter, &options);
695     if (ret < 0)
696         goto fail;
697
698     if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
699         av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
700         ret = AVERROR_OPTION_NOT_FOUND;
701         goto fail;
702     }
703
704 fail:
705     av_dict_free(&options);
706
707     return ret;
708 }
709
710 const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
711 {
712     return pads[pad_idx].name;
713 }
714
715 enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
716 {
717     return pads[pad_idx].type;
718 }
719
720 static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
721 {
722     return ff_filter_frame(link->dst->outputs[0], frame);
723 }
724
725 int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
726 {
727     int (*filter_frame)(AVFilterLink *, AVFrame *);
728     AVFilterPad *dst = link->dstpad;
729     AVFrame *out;
730
731     FF_DPRINTF_START(NULL, filter_frame);
732     ff_dlog_link(NULL, link, 1);
733
734     if (!(filter_frame = dst->filter_frame))
735         filter_frame = default_filter_frame;
736
737     /* copy the frame if needed */
738     if (dst->needs_writable && !av_frame_is_writable(frame)) {
739         av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
740
741         switch (link->type) {
742         case AVMEDIA_TYPE_VIDEO:
743             out = ff_get_video_buffer(link, link->w, link->h);
744             break;
745         case AVMEDIA_TYPE_AUDIO:
746             out = ff_get_audio_buffer(link, frame->nb_samples);
747             break;
748         default: return AVERROR(EINVAL);
749         }
750         if (!out) {
751             av_frame_free(&frame);
752             return AVERROR(ENOMEM);
753         }
754         av_frame_copy_props(out, frame);
755
756         switch (link->type) {
757         case AVMEDIA_TYPE_VIDEO:
758             av_image_copy(out->data, out->linesize, frame->data, frame->linesize,
759                           frame->format, frame->width, frame->height);
760             break;
761         case AVMEDIA_TYPE_AUDIO:
762             av_samples_copy(out->extended_data, frame->extended_data,
763                             0, 0, frame->nb_samples,
764                             av_get_channel_layout_nb_channels(frame->channel_layout),
765                             frame->format);
766             break;
767         default: return AVERROR(EINVAL);
768         }
769
770         av_frame_free(&frame);
771     } else
772         out = frame;
773
774     return filter_frame(link, out);
775 }
776
777 const AVClass *avfilter_get_class(void)
778 {
779     return &avfilter_class;
780 }