3 * Copyright (c) 2007 Bobby Bingham
5 * This file is part of Libav.
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.
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.
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
22 #include "libavutil/avstring.h"
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/common.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavutil/rational.h"
30 #include "libavutil/samplefmt.h"
38 unsigned avfilter_version(void)
40 return LIBAVFILTER_VERSION_INT
;
43 const char *avfilter_configuration(void)
45 return LIBAV_CONFIGURATION
;
48 const char *avfilter_license(void)
50 #define LICENSE_PREFIX "libavfilter license: "
51 return LICENSE_PREFIX LIBAV_LICENSE
+ sizeof(LICENSE_PREFIX
) - 1;
54 void ff_insert_pad(unsigned idx
, unsigned *count
, size_t padidx_off
,
55 AVFilterPad
**pads
, AVFilterLink
***links
,
60 idx
= FFMIN(idx
, *count
);
62 *pads
= av_realloc(*pads
, sizeof(AVFilterPad
) * (*count
+ 1));
63 *links
= av_realloc(*links
, sizeof(AVFilterLink
*) * (*count
+ 1));
64 memmove(*pads
+ idx
+ 1, *pads
+ idx
, sizeof(AVFilterPad
) * (*count
- idx
));
65 memmove(*links
+ idx
+ 1, *links
+ idx
, sizeof(AVFilterLink
*) * (*count
- idx
));
66 memcpy(*pads
+ idx
, newpad
, sizeof(AVFilterPad
));
70 for (i
= idx
+ 1; i
< *count
; i
++)
72 (*(unsigned *)((uint8_t *) *links
[i
] + padidx_off
))++;
75 int avfilter_link(AVFilterContext
*src
, unsigned srcpad
,
76 AVFilterContext
*dst
, unsigned dstpad
)
80 if (src
->nb_outputs
<= srcpad
|| dst
->nb_inputs
<= dstpad
||
81 src
->outputs
[srcpad
] || dst
->inputs
[dstpad
])
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
);
91 link
= av_mallocz(sizeof(*link
));
93 return AVERROR(ENOMEM
);
95 src
->outputs
[srcpad
] = dst
->inputs
[dstpad
] = link
;
99 link
->srcpad
= &src
->output_pads
[srcpad
];
100 link
->dstpad
= &dst
->input_pads
[dstpad
];
101 link
->type
= src
->output_pads
[srcpad
].type
;
102 assert(AV_PIX_FMT_NONE
== -1 && AV_SAMPLE_FMT_NONE
== -1);
108 int avfilter_insert_filter(AVFilterLink
*link
, AVFilterContext
*filt
,
109 unsigned filt_srcpad_idx
, unsigned filt_dstpad_idx
)
112 unsigned dstpad_idx
= link
->dstpad
- link
->dst
->input_pads
;
114 av_log(link
->dst
, AV_LOG_VERBOSE
, "auto-inserting filter '%s' "
115 "between the filter '%s' and the filter '%s'\n",
116 filt
->name
, link
->src
->name
, link
->dst
->name
);
118 link
->dst
->inputs
[dstpad_idx
] = NULL
;
119 if ((ret
= avfilter_link(filt
, filt_dstpad_idx
, link
->dst
, dstpad_idx
)) < 0) {
120 /* failed to link output filter to new filter */
121 link
->dst
->inputs
[dstpad_idx
] = link
;
125 /* re-hookup the link to the new destination filter we inserted */
127 link
->dstpad
= &filt
->input_pads
[filt_srcpad_idx
];
128 filt
->inputs
[filt_srcpad_idx
] = link
;
130 /* if any information on supported media formats already exists on the
131 * link, we need to preserve that */
132 if (link
->out_formats
)
133 ff_formats_changeref(&link
->out_formats
,
134 &filt
->outputs
[filt_dstpad_idx
]->out_formats
);
135 if (link
->out_samplerates
)
136 ff_formats_changeref(&link
->out_samplerates
,
137 &filt
->outputs
[filt_dstpad_idx
]->out_samplerates
);
138 if (link
->out_channel_layouts
)
139 ff_channel_layouts_changeref(&link
->out_channel_layouts
,
140 &filt
->outputs
[filt_dstpad_idx
]->out_channel_layouts
);
145 int avfilter_config_links(AVFilterContext
*filter
)
147 int (*config_link
)(AVFilterLink
*);
151 for (i
= 0; i
< filter
->nb_inputs
; i
++) {
152 AVFilterLink
*link
= filter
->inputs
[i
];
156 switch (link
->init_state
) {
159 case AVLINK_STARTINIT
:
160 av_log(filter
, AV_LOG_INFO
, "circular filter chain detected\n");
163 link
->init_state
= AVLINK_STARTINIT
;
165 if ((ret
= avfilter_config_links(link
->src
)) < 0)
168 if (!(config_link
= link
->srcpad
->config_props
)) {
169 if (link
->src
->nb_inputs
!= 1) {
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
);
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",
183 if (link
->time_base
.num
== 0 && link
->time_base
.den
== 0)
184 link
->time_base
= link
->src
&& link
->src
->nb_inputs ?
185 link
->src
->inputs
[0]->time_base
: AV_TIME_BASE_Q
;
187 if (link
->type
== AVMEDIA_TYPE_VIDEO
) {
188 if (!link
->sample_aspect_ratio
.num
&& !link
->sample_aspect_ratio
.den
)
189 link
->sample_aspect_ratio
= link
->src
->nb_inputs ?
190 link
->src
->inputs
[0]->sample_aspect_ratio
: (AVRational
){1,1};
192 if (link
->src
->nb_inputs
) {
194 link
->w
= link
->src
->inputs
[0]->w
;
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
);
205 if ((config_link
= link
->dstpad
->config_props
))
206 if ((ret
= config_link(link
)) < 0) {
207 av_log(link
->src
, AV_LOG_ERROR
,
208 "Failed to configure input pad on %s\n",
213 link
->init_state
= AVLINK_INIT
;
220 void ff_dlog_link(void *ctx
, AVFilterLink
*link
, int end
)
222 if (link
->type
== AVMEDIA_TYPE_VIDEO
) {
224 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
225 link
, link
->w
, link
->h
,
226 av_get_pix_fmt_name(link
->format
),
227 link
->src ? link
->src
->filter
->name
: "",
228 link
->dst ? link
->dst
->filter
->name
: "",
232 av_get_channel_layout_string(buf
, sizeof(buf
), -1, link
->channel_layout
);
235 "link[%p r:%d cl:%s fmt:%-16s %-16s->%-16s]%s",
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
: "",
244 int ff_request_frame(AVFilterLink
*link
)
246 FF_DPRINTF_START(NULL
, request_frame
); ff_dlog_link(NULL
, link
, 1);
248 if (link
->srcpad
->request_frame
)
249 return link
->srcpad
->request_frame(link
);
250 else if (link
->src
->inputs
[0])
251 return ff_request_frame(link
->src
->inputs
[0]);
255 int ff_poll_frame(AVFilterLink
*link
)
257 int i
, min
= INT_MAX
;
259 if (link
->srcpad
->poll_frame
)
260 return link
->srcpad
->poll_frame(link
);
262 for (i
= 0; i
< link
->src
->nb_inputs
; i
++) {
264 if (!link
->src
->inputs
[i
])
266 val
= ff_poll_frame(link
->src
->inputs
[i
]);
267 min
= FFMIN(min
, val
);
273 static AVFilter
*first_filter
;
275 #if !FF_API_NOCONST_GET_NAME
278 AVFilter
*avfilter_get_by_name(const char *name
)
280 const AVFilter
*f
= NULL
;
285 while ((f
= avfilter_next(f
)))
286 if (!strcmp(f
->name
, name
))
292 int avfilter_register(AVFilter
*filter
)
294 AVFilter
**f
= &first_filter
;
302 const AVFilter
*avfilter_next(const AVFilter
*prev
)
304 return prev ? prev
->next
: first_filter
;
307 #if FF_API_OLD_FILTER_REGISTER
308 AVFilter
**av_filter_next(AVFilter
**filter
)
310 return filter ?
&(*filter
)->next
: &first_filter
;
313 void avfilter_uninit(void)
318 int avfilter_pad_count(const AVFilterPad
*pads
)
325 for (count
= 0; pads
->name
; count
++)
330 static const char *filter_name(void *p
)
332 AVFilterContext
*filter
= p
;
333 return filter
->filter
->name
;
336 static void *filter_child_next(void *obj
, void *prev
)
338 AVFilterContext
*ctx
= obj
;
339 if (!prev
&& ctx
->filter
&& ctx
->filter
->priv_class
&& ctx
->priv
)
344 static const AVClass
*filter_child_class_next(const AVClass
*prev
)
346 const AVFilter
*f
= NULL
;
348 while (prev
&& (f
= avfilter_next(f
)))
349 if (f
->priv_class
== prev
)
352 while ((f
= avfilter_next(f
)))
354 return f
->priv_class
;
359 #define OFFSET(x) offsetof(AVFilterContext, x)
360 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
361 static const AVOption avfilter_options
[] = {
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" },
368 static const AVClass avfilter_class
= {
369 .class_name
= "AVFilter",
370 .item_name
= filter_name
,
371 .version
= LIBAVUTIL_VERSION_INT
,
372 .child_next
= filter_child_next
,
373 .child_class_next
= filter_child_class_next
,
374 .option
= avfilter_options
,
377 static int default_execute(AVFilterContext
*ctx
, avfilter_action_func
*func
, void *arg
,
378 int *ret
, int nb_jobs
)
382 for (i
= 0; i
< nb_jobs
; i
++) {
383 int r
= func(ctx
, arg
, i
, nb_jobs
);
390 AVFilterContext
*ff_filter_alloc(const AVFilter
*filter
, const char *inst_name
)
392 AVFilterContext
*ret
;
397 ret
= av_mallocz(sizeof(AVFilterContext
));
401 ret
->av_class
= &avfilter_class
;
402 ret
->filter
= filter
;
403 ret
->name
= inst_name ?
av_strdup(inst_name
) : NULL
;
404 if (filter
->priv_size
) {
405 ret
->priv
= av_mallocz(filter
->priv_size
);
410 av_opt_set_defaults(ret
);
411 if (filter
->priv_class
) {
412 *(const AVClass
**)ret
->priv
= filter
->priv_class
;
413 av_opt_set_defaults(ret
->priv
);
416 ret
->internal
= av_mallocz(sizeof(*ret
->internal
));
419 ret
->internal
->execute
= default_execute
;
421 ret
->nb_inputs
= avfilter_pad_count(filter
->inputs
);
422 if (ret
->nb_inputs
) {
423 ret
->input_pads
= av_malloc(sizeof(AVFilterPad
) * ret
->nb_inputs
);
424 if (!ret
->input_pads
)
426 memcpy(ret
->input_pads
, filter
->inputs
, sizeof(AVFilterPad
) * ret
->nb_inputs
);
427 ret
->inputs
= av_mallocz(sizeof(AVFilterLink
*) * ret
->nb_inputs
);
432 ret
->nb_outputs
= avfilter_pad_count(filter
->outputs
);
433 if (ret
->nb_outputs
) {
434 ret
->output_pads
= av_malloc(sizeof(AVFilterPad
) * ret
->nb_outputs
);
435 if (!ret
->output_pads
)
437 memcpy(ret
->output_pads
, filter
->outputs
, sizeof(AVFilterPad
) * ret
->nb_outputs
);
438 ret
->outputs
= av_mallocz(sizeof(AVFilterLink
*) * ret
->nb_outputs
);
443 FF_DISABLE_DEPRECATION_WARNINGS
444 ret
->output_count
= ret
->nb_outputs
;
445 ret
->input_count
= ret
->nb_inputs
;
446 FF_ENABLE_DEPRECATION_WARNINGS
452 av_freep(&ret
->inputs
);
453 av_freep(&ret
->input_pads
);
455 av_freep(&ret
->outputs
);
456 av_freep(&ret
->output_pads
);
458 av_freep(&ret
->priv
);
459 av_freep(&ret
->internal
);
464 #if FF_API_AVFILTER_OPEN
465 int avfilter_open(AVFilterContext
**filter_ctx
, AVFilter
*filter
, const char *inst_name
)
467 *filter_ctx
= ff_filter_alloc(filter
, inst_name
);
468 return *filter_ctx ?
0 : AVERROR(ENOMEM
);
472 static void free_link(AVFilterLink
*link
)
478 link
->src
->outputs
[link
->srcpad
- link
->src
->output_pads
] = NULL
;
480 link
->dst
->inputs
[link
->dstpad
- link
->dst
->input_pads
] = NULL
;
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
);
491 void avfilter_free(AVFilterContext
*filter
)
496 ff_filter_graph_remove_filter(filter
->graph
, filter
);
498 if (filter
->filter
->uninit
)
499 filter
->filter
->uninit(filter
);
501 for (i
= 0; i
< filter
->nb_inputs
; i
++) {
502 free_link(filter
->inputs
[i
]);
504 for (i
= 0; i
< filter
->nb_outputs
; i
++) {
505 free_link(filter
->outputs
[i
]);
508 if (filter
->filter
->priv_class
)
509 av_opt_free(filter
->priv
);
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
);
517 av_freep(&filter
->internal
);
521 /* process a list of value1:value2:..., each value corresponding
522 * to subsequent AVOption, in the order they are declared */
523 static int process_unnamed_options(AVFilterContext
*ctx
, AVDictionary
**options
,
526 const AVOption
*o
= NULL
;
527 const char *p
= args
;
531 o
= av_opt_next(ctx
->priv
, o
);
533 av_log(ctx
, AV_LOG_ERROR
, "More options provided than "
534 "this filter supports.\n");
535 return AVERROR(EINVAL
);
537 if (o
->type
== AV_OPT_TYPE_CONST
)
540 val
= av_get_token(&p
, ":");
542 return AVERROR(ENOMEM
);
544 av_dict_set(options
, o
->name
, val
, 0);
554 #if FF_API_AVFILTER_INIT_FILTER
555 int avfilter_init_filter(AVFilterContext
*filter
, const char *args
, void *opaque
)
557 return avfilter_init_str(filter
, args
);
561 int avfilter_init_dict(AVFilterContext
*ctx
, AVDictionary
**options
)
565 ret
= av_opt_set_dict(ctx
, options
);
567 av_log(ctx
, AV_LOG_ERROR
, "Error applying generic filter options.\n");
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
;
577 ctx
->thread_type
= 0;
580 if (ctx
->filter
->priv_class
) {
581 ret
= av_opt_set_dict(ctx
->priv
, options
);
583 av_log(ctx
, AV_LOG_ERROR
, "Error applying options to the filter.\n");
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
);
596 int avfilter_init_str(AVFilterContext
*filter
, const char *args
)
598 AVDictionary
*options
= NULL
;
599 AVDictionaryEntry
*e
;
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
);
609 #if FF_API_OLD_FILTER_OPTS
610 if (!strcmp(filter
->filter
->name
, "scale") &&
611 strchr(args
, ':') && strchr(args
, ':') < strchr(args
, '=')) {
612 /* old w:h:flags=<flags> syntax */
613 char *copy
= av_strdup(args
);
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");
621 ret
= AVERROR(ENOMEM
);
625 p
= strrchr(copy
, ':');
628 ret
= av_dict_parse_string(&options
, p
, "=", ":", 0);
631 ret
= process_unnamed_options(filter
, &options
, copy
);
639 if (strchr(args
, '=')) {
640 /* assume a list of key1=value1:key2=value2:... */
641 ret
= av_dict_parse_string(&options
, args
, "=", ":", 0);
644 #if FF_API_OLD_FILTER_OPTS
645 } else if (!strcmp(filter
->filter
->name
, "format") ||
646 !strcmp(filter
->filter
->name
, "noformat") ||
647 !strcmp(filter
->filter
->name
, "frei0r") ||
648 !strcmp(filter
->filter
->name
, "frei0r_src") ||
649 !strcmp(filter
->filter
->name
, "ocv")) {
650 /* a hack for compatibility with the old syntax
651 * replace colons with |s */
652 char *copy
= av_strdup(args
);
654 int nb_leading
= 0; // number of leading colons to skip
657 ret
= AVERROR(ENOMEM
);
661 if (!strcmp(filter
->filter
->name
, "frei0r") ||
662 !strcmp(filter
->filter
->name
, "ocv"))
664 else if (!strcmp(filter
->filter
->name
, "frei0r_src"))
667 while (nb_leading
--) {
670 p
= copy
+ strlen(copy
);
676 if (strchr(p
, ':')) {
677 av_log(filter
, AV_LOG_WARNING
, "This syntax is deprecated. Use "
678 "'|' to separate the list items.\n");
681 while ((p
= strchr(p
, ':')))
684 ret
= process_unnamed_options(filter
, &options
, copy
);
691 ret
= process_unnamed_options(filter
, &options
, args
);
697 ret
= avfilter_init_dict(filter
, &options
);
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
;
708 av_dict_free(&options
);
713 const char *avfilter_pad_get_name(const AVFilterPad
*pads
, int pad_idx
)
715 return pads
[pad_idx
].name
;
718 enum AVMediaType
avfilter_pad_get_type(const AVFilterPad
*pads
, int pad_idx
)
720 return pads
[pad_idx
].type
;
723 static int default_filter_frame(AVFilterLink
*link
, AVFrame
*frame
)
725 return ff_filter_frame(link
->dst
->outputs
[0], frame
);
728 int ff_filter_frame(AVFilterLink
*link
, AVFrame
*frame
)
730 int (*filter_frame
)(AVFilterLink
*, AVFrame
*);
731 AVFilterPad
*dst
= link
->dstpad
;
734 FF_DPRINTF_START(NULL
, filter_frame
);
735 ff_dlog_link(NULL
, link
, 1);
737 if (!(filter_frame
= dst
->filter_frame
))
738 filter_frame
= default_filter_frame
;
740 /* copy the frame if needed */
741 if (dst
->needs_writable
&& !av_frame_is_writable(frame
)) {
742 av_log(link
->dst
, AV_LOG_DEBUG
, "Copying data in avfilter.\n");
744 switch (link
->type
) {
745 case AVMEDIA_TYPE_VIDEO
:
746 out
= ff_get_video_buffer(link
, link
->w
, link
->h
);
748 case AVMEDIA_TYPE_AUDIO
:
749 out
= ff_get_audio_buffer(link
, frame
->nb_samples
);
751 default: return AVERROR(EINVAL
);
754 av_frame_free(&frame
);
755 return AVERROR(ENOMEM
);
757 av_frame_copy_props(out
, frame
);
759 switch (link
->type
) {
760 case AVMEDIA_TYPE_VIDEO
:
761 av_image_copy(out
->data
, out
->linesize
, frame
->data
, frame
->linesize
,
762 frame
->format
, frame
->width
, frame
->height
);
764 case AVMEDIA_TYPE_AUDIO
:
765 av_samples_copy(out
->extended_data
, frame
->extended_data
,
766 0, 0, frame
->nb_samples
,
767 av_get_channel_layout_nb_channels(frame
->channel_layout
),
770 default: return AVERROR(EINVAL
);
773 av_frame_free(&frame
);
777 return filter_frame(link
, out
);
780 const AVClass
*avfilter_get_class(void)
782 return &avfilter_class
;