h264_metadata: Add option to delete filler data
[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
e3fb74f7 22#include "libavutil/avassert.h"
b439c992 23#include "libavutil/avstring.h"
b3dd30db 24#include "libavutil/buffer.h"
a903f8f0 25#include "libavutil/channel_layout.h"
1d9c2dc8 26#include "libavutil/common.h"
b3dd30db 27#include "libavutil/hwcontext.h"
565e4993 28#include "libavutil/imgutils.h"
7950e519 29#include "libavutil/internal.h"
b439c992 30#include "libavutil/opt.h"
b4b66456 31#include "libavutil/pixdesc.h"
867ae7aa 32#include "libavutil/rational.h"
565e4993 33#include "libavutil/samplefmt.h"
472fb3bb 34
565e4993 35#include "audio.h"
4dbbcdee 36#include "avfilter.h"
ff1f51a8 37#include "formats.h"
9f0e31d2 38#include "internal.h"
565e4993 39#include "video.h"
4dbbcdee 40
bf5b5d2b
AK
41unsigned avfilter_version(void)
42{
540f1c7b
SS
43 return LIBAVFILTER_VERSION_INT;
44}
45
41600690 46const char *avfilter_configuration(void)
c1736936 47{
29ba0911 48 return LIBAV_CONFIGURATION;
c1736936
DB
49}
50
41600690 51const char *avfilter_license(void)
c1736936
DB
52{
53#define LICENSE_PREFIX "libavfilter license: "
a03be6e1 54 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
c1736936
DB
55}
56
fa417fcd
AK
57void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
58 AVFilterPad **pads, AVFilterLink ***links,
59 AVFilterPad *newpad)
24618441
VS
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));
bf5b5d2b
AK
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));
24618441
VS
70 (*links)[idx] = NULL;
71
ca857431 72 (*count)++;
bf5b5d2b 73 for (i = idx + 1; i < *count; i++)
ca857431
SS
74 if (*links[i])
75 (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
24618441
VS
76}
77
4dbbcdee
VS
78int avfilter_link(AVFilterContext *src, unsigned srcpad,
79 AVFilterContext *dst, unsigned dstpad)
80{
81 AVFilterLink *link;
82
9baeff95
AK
83 if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
84 src->outputs[srcpad] || dst->inputs[dstpad])
88b160a4 85 return AVERROR(EINVAL);
4dbbcdee 86
891aeeee
SS
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
7e2b15c0
AK
94 link = av_mallocz(sizeof(*link));
95 if (!link)
96 return AVERROR(ENOMEM);
97
98 src->outputs[srcpad] = dst->inputs[dstpad] = link;
4dbbcdee 99
1653c11f
VS
100 link->src = src;
101 link->dst = dst;
acc0490f
SS
102 link->srcpad = &src->output_pads[srcpad];
103 link->dstpad = &dst->input_pads[dstpad];
bdab614b 104 link->type = src->output_pads[srcpad].type;
716d413c 105 assert(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
bdab614b 106 link->format = -1;
01942f1d
VS
107
108 return 0;
109}
110
52362e9d 111int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
486adc55 112 unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
52362e9d 113{
db9dfa3c 114 int ret;
acc0490f
SS
115 unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
116
1a49a169 117 av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
08f8b51f
SS
118 "between the filter '%s' and the filter '%s'\n",
119 filt->name, link->src->name, link->dst->name);
52362e9d 120
acc0490f 121 link->dst->inputs[dstpad_idx] = NULL;
486adc55 122 if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
52362e9d 123 /* failed to link output filter to new filter */
acc0490f 124 link->dst->inputs[dstpad_idx] = link;
db9dfa3c 125 return ret;
52362e9d
VS
126 }
127
128 /* re-hookup the link to the new destination filter we inserted */
bf5b5d2b
AK
129 link->dst = filt;
130 link->dstpad = &filt->input_pads[filt_srcpad_idx];
486adc55 131 filt->inputs[filt_srcpad_idx] = link;
52362e9d 132
bdab614b 133 /* if any information on supported media formats already exists on the
07d0bba5 134 * link, we need to preserve that */
ca857431 135 if (link->out_formats)
b74a1da4 136 ff_formats_changeref(&link->out_formats,
bf5b5d2b 137 &filt->outputs[filt_dstpad_idx]->out_formats);
ff1f51a8 138 if (link->out_samplerates)
b74a1da4 139 ff_formats_changeref(&link->out_samplerates,
bf5b5d2b 140 &filt->outputs[filt_dstpad_idx]->out_samplerates);
ff1f51a8
AK
141 if (link->out_channel_layouts)
142 ff_channel_layouts_changeref(&link->out_channel_layouts,
143 &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
07d0bba5 144
52362e9d
VS
145 return 0;
146}
147
69818105 148int avfilter_config_links(AVFilterContext *filter)
01942f1d 149{
01942f1d 150 int (*config_link)(AVFilterLink *);
69818105 151 unsigned i;
69f73a89 152 int ret;
69818105 153
9baeff95 154 for (i = 0; i < filter->nb_inputs; i ++) {
e079d22e 155 AVFilterLink *link = filter->inputs[i];
d21cbbff 156
ca857431 157 if (!link) continue;
ac84c1ce
VG
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 }
69818105 163
ca857431 164 switch (link->init_state) {
69818105
VS
165 case AVLINK_INIT:
166 continue;
167 case AVLINK_STARTINIT:
7a9fd2a0
VS
168 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
169 return 0;
69818105
VS
170 case AVLINK_UNINIT:
171 link->init_state = AVLINK_STARTINIT;
172
69f73a89
SS
173 if ((ret = avfilter_config_links(link->src)) < 0)
174 return ret;
4dbbcdee 175
88c3b87b 176 if (!(config_link = link->srcpad->config_props)) {
9baeff95 177 if (link->src->nb_inputs != 1) {
88c3b87b
MNZ
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 }
65a80ee1
AC
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);
69f73a89 188 return ret;
65a80ee1 189 }
102fb0e3 190
867ae7aa 191 if (link->time_base.num == 0 && link->time_base.den == 0)
ac84c1ce 192 link->time_base = link->src->nb_inputs ?
cf5fe8ec 193 link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
867ae7aa 194
88c3b87b
MNZ
195 if (link->type == AVMEDIA_TYPE_VIDEO) {
196 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
9baeff95 197 link->sample_aspect_ratio = link->src->nb_inputs ?
88c3b87b
MNZ
198 link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
199
9baeff95 200 if (link->src->nb_inputs) {
48ff6683
NG
201 if (!link->frame_rate.num && !link->frame_rate.den)
202 link->frame_rate = link->src->inputs[0]->frame_rate;
88c3b87b
MNZ
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 }
910b5b82 214
b3dd30db 215 if (link->src->nb_inputs && link->src->inputs[0]->hw_frames_ctx &&
e3fb74f7
MT
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);
b3dd30db
AK
222 }
223
5b63b156
AK
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
69818105
VS
232 link->init_state = AVLINK_INIT;
233 }
234 }
235
4dbbcdee
VS
236 return 0;
237}
238
0b45334a 239void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
96da1c51 240{
7986e34d 241 if (link->type == AVMEDIA_TYPE_VIDEO) {
1a3eb042 242 av_log(ctx, AV_LOG_TRACE,
1d5b1885
SS
243 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
244 link, link->w, link->h,
59ee9f78 245 av_get_pix_fmt_name(link->format),
1d5b1885
SS
246 link->src ? link->src->filter->name : "",
247 link->dst ? link->dst->filter->name : "",
248 end ? "\n" : "");
7986e34d
SS
249 } else {
250 char buf[128];
251 av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
252
1a3eb042 253 av_log(ctx, AV_LOG_TRACE,
49c45a26 254 "link[%p r:%d cl:%s fmt:%-16s %-16s->%-16s]%s",
7986e34d
SS
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 }
96da1c51
SS
261}
262
803391f7 263int ff_request_frame(AVFilterLink *link)
4dbbcdee 264{
dfd2a005 265 FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
96da1c51 266
acc0490f
SS
267 if (link->srcpad->request_frame)
268 return link->srcpad->request_frame(link);
ca857431 269 else if (link->src->inputs[0])
803391f7 270 return ff_request_frame(link->src->inputs[0]);
88b160a4
HS
271 else
272 return AVERROR(EINVAL);
4dbbcdee
VS
273}
274
803391f7 275int ff_poll_frame(AVFilterLink *link)
7b02c484 276{
ca857431 277 int i, min = INT_MAX;
7b02c484 278
acc0490f
SS
279 if (link->srcpad->poll_frame)
280 return link->srcpad->poll_frame(link);
c245ddf2 281
9baeff95 282 for (i = 0; i < link->src->nb_inputs; i++) {
9dd08b4e 283 int val;
ca857431 284 if (!link->src->inputs[i])
88b160a4 285 return AVERROR(EINVAL);
803391f7 286 val = ff_poll_frame(link->src->inputs[i]);
9dd08b4e 287 min = FFMIN(min, val);
c245ddf2 288 }
7b02c484
VS
289
290 return min;
291}
292
fa2a34cd 293static AVFilter *first_filter;
3555d2e8 294
96a47364 295const AVFilter *avfilter_get_by_name(const char *name)
4dbbcdee 296{
884c7a6e 297 const AVFilter *f = NULL;
6d8c67a7 298
f160c6a1
AK
299 if (!name)
300 return NULL;
301
fa2a34cd
AK
302 while ((f = avfilter_next(f)))
303 if (!strcmp(f->name, name))
304 return f;
4dbbcdee 305
4dbbcdee
VS
306 return NULL;
307}
308
86a60fa1 309int avfilter_register(AVFilter *filter)
4dbbcdee 310{
fa2a34cd
AK
311 AVFilter **f = &first_filter;
312 while (*f)
313 f = &(*f)->next;
314 *f = filter;
315 filter->next = NULL;
86a60fa1 316 return 0;
4dbbcdee
VS
317}
318
fa2a34cd
AK
319const AVFilter *avfilter_next(const AVFilter *prev)
320{
321 return prev ? prev->next : first_filter;
322}
323
7e8fe4be 324int avfilter_pad_count(const AVFilterPad *pads)
4dbbcdee 325{
4dbbcdee
VS
326 int count;
327
1fce361d
MR
328 if (!pads)
329 return 0;
330
bf5b5d2b
AK
331 for (count = 0; pads->name; count++)
332 pads++;
4dbbcdee
VS
333 return count;
334}
335
336static const char *filter_name(void *p)
337{
338 AVFilterContext *filter = p;
339 return filter->filter->name;
340}
341
4d1f31ea
AK
342static void *filter_child_next(void *obj, void *prev)
343{
344 AVFilterContext *ctx = obj;
345 if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
346 return ctx->priv;
347 return NULL;
348}
349
350static const AVClass *filter_child_class_next(const AVClass *prev)
351{
884c7a6e 352 const AVFilter *f = NULL;
4d1f31ea 353
fa2a34cd
AK
354 while (prev && (f = avfilter_next(f)))
355 if (f->priv_class == prev)
4d1f31ea
AK
356 break;
357
fa2a34cd
AK
358 while ((f = avfilter_next(f)))
359 if (f->priv_class)
360 return f->priv_class;
4d1f31ea
AK
361
362 return NULL;
363}
364
129bb238
AK
365#define OFFSET(x) offsetof(AVFilterContext, x)
366#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
b5a13865 367static const AVOption avfilter_options[] = {
129bb238
AK
368 { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
369 { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
370 { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .unit = "thread_type" },
6d86cef0
MT
371 { "extra_hw_frames", "Number of extra hardware frames to allocate for the user",
372 OFFSET(extra_hw_frames), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
129bb238
AK
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
d79bd604
AK
466static void free_link(AVFilterLink *link)
467{
468 if (!link)
469 return;
470
471 if (link->src)
472 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
473 if (link->dst)
474 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
475
b3dd30db
AK
476 av_buffer_unref(&link->hw_frames_ctx);
477
d79bd604
AK
478 ff_formats_unref(&link->in_formats);
479 ff_formats_unref(&link->out_formats);
480 ff_formats_unref(&link->in_samplerates);
481 ff_formats_unref(&link->out_samplerates);
482 ff_channel_layouts_unref(&link->in_channel_layouts);
483 ff_channel_layouts_unref(&link->out_channel_layouts);
484 av_freep(&link);
485}
486
24de0edb 487void avfilter_free(AVFilterContext *filter)
4dbbcdee
VS
488{
489 int i;
490
1565cbc6
AK
491 if (filter->graph)
492 ff_filter_graph_remove_filter(filter->graph, filter);
493
ca857431 494 if (filter->filter->uninit)
4dbbcdee
VS
495 filter->filter->uninit(filter);
496
9baeff95 497 for (i = 0; i < filter->nb_inputs; i++) {
d79bd604 498 free_link(filter->inputs[i]);
4dbbcdee 499 }
9baeff95 500 for (i = 0; i < filter->nb_outputs; i++) {
d79bd604 501 free_link(filter->outputs[i]);
4dbbcdee
VS
502 }
503
b439c992
AK
504 if (filter->filter->priv_class)
505 av_opt_free(filter->priv);
506
07a844f3
MT
507 av_buffer_unref(&filter->hw_device_ctx);
508
f4cb4462
VS
509 av_freep(&filter->name);
510 av_freep(&filter->input_pads);
511 av_freep(&filter->output_pads);
512 av_freep(&filter->inputs);
513 av_freep(&filter->outputs);
514 av_freep(&filter->priv);
129bb238 515 av_freep(&filter->internal);
4dbbcdee
VS
516 av_free(filter);
517}
518
b439c992
AK
519/* process a list of value1:value2:..., each value corresponding
520 * to subsequent AVOption, in the order they are declared */
521static int process_unnamed_options(AVFilterContext *ctx, AVDictionary **options,
522 const char *args)
523{
524 const AVOption *o = NULL;
525 const char *p = args;
526 char *val;
527
528 while (*p) {
529 o = av_opt_next(ctx->priv, o);
530 if (!o) {
531 av_log(ctx, AV_LOG_ERROR, "More options provided than "
532 "this filter supports.\n");
533 return AVERROR(EINVAL);
534 }
535 if (o->type == AV_OPT_TYPE_CONST)
536 continue;
537
538 val = av_get_token(&p, ":");
539 if (!val)
540 return AVERROR(ENOMEM);
541
542 av_dict_set(options, o->name, val, 0);
543
544 av_freep(&val);
545 if (*p)
546 p++;
547 }
548
549 return 0;
550}
551
1ba95a9c
AK
552int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
553{
554 int ret = 0;
555
129bb238
AK
556 ret = av_opt_set_dict(ctx, options);
557 if (ret < 0) {
558 av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
559 return ret;
560 }
561
562 if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
563 ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
564 ctx->graph->internal->thread_execute) {
565 ctx->thread_type = AVFILTER_THREAD_SLICE;
566 ctx->internal->execute = ctx->graph->internal->thread_execute;
567 } else {
568 ctx->thread_type = 0;
569 }
570
1ba95a9c
AK
571 if (ctx->filter->priv_class) {
572 ret = av_opt_set_dict(ctx->priv, options);
573 if (ret < 0) {
574 av_log(ctx, AV_LOG_ERROR, "Error applying options to the filter.\n");
575 return ret;
576 }
577 }
578
579 if (ctx->filter->init)
580 ret = ctx->filter->init(ctx);
581 else if (ctx->filter->init_dict)
582 ret = ctx->filter->init_dict(ctx, options);
583
584 return ret;
585}
586
48a5adab
AK
587int avfilter_init_str(AVFilterContext *filter, const char *args)
588{
b439c992
AK
589 AVDictionary *options = NULL;
590 AVDictionaryEntry *e;
bf5b5d2b 591 int ret = 0;
4dbbcdee 592
62549f96
AK
593 if (args && *args) {
594 if (!filter->filter->priv_class) {
595 av_log(filter, AV_LOG_ERROR, "This filter does not take any "
596 "options, but options were provided: %s.\n", args);
597 return AVERROR(EINVAL);
598 }
599
b439c992
AK
600 if (strchr(args, '=')) {
601 /* assume a list of key1=value1:key2=value2:... */
602 ret = av_dict_parse_string(&options, args, "=", ":", 0);
603 if (ret < 0)
604 goto fail;
605 } else {
606 ret = process_unnamed_options(filter, &options, args);
607 if (ret < 0)
608 goto fail;
609 }
610 }
611
1ba95a9c 612 ret = avfilter_init_dict(filter, &options);
4fa1f52e
AK
613 if (ret < 0)
614 goto fail;
b439c992
AK
615
616 if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
617 av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
618 ret = AVERROR_OPTION_NOT_FOUND;
619 goto fail;
620 }
621
622fail:
623 av_dict_free(&options);
624
e079d22e 625 return ret;
84b9fbe0
AK
626}
627
4a37d4b3 628const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
84b9fbe0
AK
629{
630 return pads[pad_idx].name;
631}
632
4a37d4b3 633enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
84b9fbe0
AK
634{
635 return pads[pad_idx].type;
4dbbcdee 636}
565e4993 637
7e350379 638static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
565e4993
AK
639{
640 return ff_filter_frame(link->dst->outputs[0], frame);
641}
642
7e350379 643int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
565e4993 644{
7e350379 645 int (*filter_frame)(AVFilterLink *, AVFrame *);
565e4993 646 AVFilterPad *dst = link->dstpad;
abb5e37f
VG
647 AVFrame *out = NULL;
648 int ret;
565e4993
AK
649
650 FF_DPRINTF_START(NULL, filter_frame);
651 ff_dlog_link(NULL, link, 1);
652
653 if (!(filter_frame = dst->filter_frame))
654 filter_frame = default_filter_frame;
655
7e350379
AK
656 /* copy the frame if needed */
657 if (dst->needs_writable && !av_frame_is_writable(frame)) {
658 av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
565e4993
AK
659
660 switch (link->type) {
661 case AVMEDIA_TYPE_VIDEO:
7e350379 662 out = ff_get_video_buffer(link, link->w, link->h);
565e4993
AK
663 break;
664 case AVMEDIA_TYPE_AUDIO:
7e350379 665 out = ff_get_audio_buffer(link, frame->nb_samples);
565e4993 666 break;
abb5e37f
VG
667 default:
668 ret = AVERROR(EINVAL);
669 goto fail;
565e4993
AK
670 }
671 if (!out) {
abb5e37f
VG
672 ret = AVERROR(ENOMEM);
673 goto fail;
565e4993 674 }
abb5e37f
VG
675
676 ret = av_frame_copy_props(out, frame);
677 if (ret < 0)
678 goto fail;
565e4993
AK
679
680 switch (link->type) {
681 case AVMEDIA_TYPE_VIDEO:
682 av_image_copy(out->data, out->linesize, frame->data, frame->linesize,
7e350379 683 frame->format, frame->width, frame->height);
565e4993
AK
684 break;
685 case AVMEDIA_TYPE_AUDIO:
686 av_samples_copy(out->extended_data, frame->extended_data,
7e350379
AK
687 0, 0, frame->nb_samples,
688 av_get_channel_layout_nb_channels(frame->channel_layout),
565e4993
AK
689 frame->format);
690 break;
abb5e37f
VG
691 default:
692 ret = AVERROR(EINVAL);
693 goto fail;
565e4993
AK
694 }
695
7e350379 696 av_frame_free(&frame);
565e4993
AK
697 } else
698 out = frame;
699
700 return filter_frame(link, out);
abb5e37f
VG
701
702fail:
703 av_frame_free(&out);
704 av_frame_free(&frame);
705 return ret;
565e4993 706}
8114c101
AK
707
708const AVClass *avfilter_get_class(void)
709{
710 return &avfilter_class;
711}
6d86cef0
MT
712
713int ff_filter_init_hw_frames(AVFilterContext *avctx, AVFilterLink *link,
714 int default_pool_size)
715{
716 AVHWFramesContext *frames;
717
718 // Must already be set by caller.
719 av_assert0(link->hw_frames_ctx);
720
721 frames = (AVHWFramesContext*)link->hw_frames_ctx->data;
722
723 if (frames->initial_pool_size == 0) {
724 // Dynamic allocation is necessarily supported.
725 } else if (avctx->extra_hw_frames >= 0) {
726 frames->initial_pool_size += avctx->extra_hw_frames;
727 } else {
728 frames->initial_pool_size = default_pool_size;
729 }
730
731 return 0;
732}