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