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