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