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