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/avassert.h"
23 #include "libavutil/avstring.h"
24 #include "libavutil/buffer.h"
25 #include "libavutil/channel_layout.h"
26 #include "libavutil/common.h"
27 #include "libavutil/hwcontext.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/rational.h"
33 #include "libavutil/samplefmt.h"
41 unsigned avfilter_version(void)
43 return LIBAVFILTER_VERSION_INT;
46 const char *avfilter_configuration(void)
48 return LIBAV_CONFIGURATION;
51 const char *avfilter_license(void)
53 #define LICENSE_PREFIX "libavfilter license: "
54 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
57 void ff_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
58 AVFilterPad **pads, AVFilterLink ***links,
63 idx = FFMIN(idx, *count);
65 *pads = av_realloc(*pads, sizeof(AVFilterPad) * (*count + 1));
66 *links = av_realloc(*links, sizeof(AVFilterLink*) * (*count + 1));
67 memmove(*pads + idx + 1, *pads + idx, sizeof(AVFilterPad) * (*count - idx));
68 memmove(*links + idx + 1, *links + idx, sizeof(AVFilterLink*) * (*count - idx));
69 memcpy(*pads + idx, newpad, sizeof(AVFilterPad));
73 for (i = idx + 1; i < *count; i++)
75 (*(unsigned *)((uint8_t *) *links[i] + padidx_off))++;
78 int avfilter_link(AVFilterContext *src, unsigned srcpad,
79 AVFilterContext *dst, unsigned dstpad)
83 if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
84 src->outputs[srcpad] || dst->inputs[dstpad])
85 return AVERROR(EINVAL);
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);
94 link = av_mallocz(sizeof(*link));
96 return AVERROR(ENOMEM);
98 src->outputs[srcpad] = dst->inputs[dstpad] = link;
102 link->srcpad = &src->output_pads[srcpad];
103 link->dstpad = &dst->input_pads[dstpad];
104 link->type = src->output_pads[srcpad].type;
105 assert(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
111 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
112 unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
115 unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
117 av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
118 "between the filter '%s' and the filter '%s'\n",
119 filt->name, link->src->name, link->dst->name);
121 link->dst->inputs[dstpad_idx] = NULL;
122 if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
123 /* failed to link output filter to new filter */
124 link->dst->inputs[dstpad_idx] = link;
128 /* re-hookup the link to the new destination filter we inserted */
130 link->dstpad = &filt->input_pads[filt_srcpad_idx];
131 filt->inputs[filt_srcpad_idx] = link;
133 /* if any information on supported media formats already exists on the
134 * link, we need to preserve that */
135 if (link->out_formats)
136 ff_formats_changeref(&link->out_formats,
137 &filt->outputs[filt_dstpad_idx]->out_formats);
138 if (link->out_samplerates)
139 ff_formats_changeref(&link->out_samplerates,
140 &filt->outputs[filt_dstpad_idx]->out_samplerates);
141 if (link->out_channel_layouts)
142 ff_channel_layouts_changeref(&link->out_channel_layouts,
143 &filt->outputs[filt_dstpad_idx]->out_channel_layouts);
148 int avfilter_config_links(AVFilterContext *filter)
150 int (*config_link)(AVFilterLink *);
154 for (i = 0; i < filter->nb_inputs; i ++) {
155 AVFilterLink *link = filter->inputs[i];
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);
164 switch (link->init_state) {
167 case AVLINK_STARTINIT:
168 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
171 link->init_state = AVLINK_STARTINIT;
173 if ((ret = avfilter_config_links(link->src)) < 0)
176 if (!(config_link = link->srcpad->config_props)) {
177 if (link->src->nb_inputs != 1) {
178 av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
179 "with more than one input "
180 "must set config_props() "
181 "callbacks on all outputs\n");
182 return AVERROR(EINVAL);
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",
191 if (link->time_base.num == 0 && link->time_base.den == 0)
192 link->time_base = link->src->nb_inputs ?
193 link->src->inputs[0]->time_base : AV_TIME_BASE_Q;
195 if (link->type == AVMEDIA_TYPE_VIDEO) {
196 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
197 link->sample_aspect_ratio = link->src->nb_inputs ?
198 link->src->inputs[0]->sample_aspect_ratio : (AVRational){1,1};
200 if (link->src->nb_inputs) {
201 if (!link->frame_rate.num && !link->frame_rate.den)
202 link->frame_rate = link->src->inputs[0]->frame_rate;
204 link->w = link->src->inputs[0]->w;
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);
215 if (link->src->nb_inputs && link->src->inputs[0]->hw_frames_ctx &&
216 !(link->src->filter->flags_internal & FF_FILTER_FLAG_HWFRAME_AWARE)) {
217 av_assert0(!link->hw_frames_ctx &&
218 "should not be set by non-hwframe-aware filter");
219 link->hw_frames_ctx = av_buffer_ref(link->src->inputs[0]->hw_frames_ctx);
220 if (!link->hw_frames_ctx)
221 return AVERROR(ENOMEM);
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",
232 link->init_state = AVLINK_INIT;
239 void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
241 if (link->type == AVMEDIA_TYPE_VIDEO) {
242 av_log(ctx, AV_LOG_TRACE,
243 "link[%p s:%dx%d fmt:%-16s %-16s->%-16s]%s",
244 link, link->w, link->h,
245 av_get_pix_fmt_name(link->format),
246 link->src ? link->src->filter->name : "",
247 link->dst ? link->dst->filter->name : "",
251 av_get_channel_layout_string(buf, sizeof(buf), -1, link->channel_layout);
253 av_log(ctx, AV_LOG_TRACE,
254 "link[%p r:%d cl:%s fmt:%-16s %-16s->%-16s]%s",
255 link, link->sample_rate, buf,
256 av_get_sample_fmt_name(link->format),
257 link->src ? link->src->filter->name : "",
258 link->dst ? link->dst->filter->name : "",
263 int ff_request_frame(AVFilterLink *link)
265 FF_DPRINTF_START(NULL, request_frame); ff_dlog_link(NULL, link, 1);
267 if (link->srcpad->request_frame)
268 return link->srcpad->request_frame(link);
269 else if (link->src->inputs[0])
270 return ff_request_frame(link->src->inputs[0]);
272 return AVERROR(EINVAL);
275 int ff_poll_frame(AVFilterLink *link)
277 int i, min = INT_MAX;
279 if (link->srcpad->poll_frame)
280 return link->srcpad->poll_frame(link);
282 for (i = 0; i < link->src->nb_inputs; i++) {
284 if (!link->src->inputs[i])
285 return AVERROR(EINVAL);
286 val = ff_poll_frame(link->src->inputs[i]);
287 min = FFMIN(min, val);
293 static AVFilter *first_filter;
295 const AVFilter *avfilter_get_by_name(const char *name)
297 const AVFilter *f = NULL;
302 while ((f = avfilter_next(f)))
303 if (!strcmp(f->name, name))
309 int avfilter_register(AVFilter *filter)
311 AVFilter **f = &first_filter;
319 const AVFilter *avfilter_next(const AVFilter *prev)
321 return prev ? prev->next : first_filter;
324 int avfilter_pad_count(const AVFilterPad *pads)
331 for (count = 0; pads->name; count++)
336 static const char *filter_name(void *p)
338 AVFilterContext *filter = p;
339 return filter->filter->name;
342 static void *filter_child_next(void *obj, void *prev)
344 AVFilterContext *ctx = obj;
345 if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
350 static const AVClass *filter_child_class_next(const AVClass *prev)
352 const AVFilter *f = NULL;
354 while (prev && (f = avfilter_next(f)))
355 if (f->priv_class == prev)
358 while ((f = avfilter_next(f)))
360 return f->priv_class;
365 #define OFFSET(x) offsetof(AVFilterContext, x)
366 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
367 static const AVOption avfilter_options[] = {
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" },
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 },
376 static const AVClass avfilter_class = {
377 .class_name = "AVFilter",
378 .item_name = filter_name,
379 .version = LIBAVUTIL_VERSION_INT,
380 .child_next = filter_child_next,
381 .child_class_next = filter_child_class_next,
382 .option = avfilter_options,
385 static int default_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg,
386 int *ret, int nb_jobs)
390 for (i = 0; i < nb_jobs; i++) {
391 int r = func(ctx, arg, i, nb_jobs);
398 AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
400 AVFilterContext *ret;
405 ret = av_mallocz(sizeof(AVFilterContext));
409 ret->av_class = &avfilter_class;
410 ret->filter = filter;
411 ret->name = inst_name ? av_strdup(inst_name) : NULL;
412 if (filter->priv_size) {
413 ret->priv = av_mallocz(filter->priv_size);
418 av_opt_set_defaults(ret);
419 if (filter->priv_class) {
420 *(const AVClass**)ret->priv = filter->priv_class;
421 av_opt_set_defaults(ret->priv);
424 ret->internal = av_mallocz(sizeof(*ret->internal));
427 ret->internal->execute = default_execute;
429 ret->nb_inputs = avfilter_pad_count(filter->inputs);
430 if (ret->nb_inputs ) {
431 ret->input_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
432 if (!ret->input_pads)
434 memcpy(ret->input_pads, filter->inputs, sizeof(AVFilterPad) * ret->nb_inputs);
435 ret->inputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_inputs);
440 ret->nb_outputs = avfilter_pad_count(filter->outputs);
441 if (ret->nb_outputs) {
442 ret->output_pads = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
443 if (!ret->output_pads)
445 memcpy(ret->output_pads, filter->outputs, sizeof(AVFilterPad) * ret->nb_outputs);
446 ret->outputs = av_mallocz(sizeof(AVFilterLink*) * ret->nb_outputs);
454 av_freep(&ret->inputs);
455 av_freep(&ret->input_pads);
457 av_freep(&ret->outputs);
458 av_freep(&ret->output_pads);
460 av_freep(&ret->priv);
461 av_freep(&ret->internal);
466 static void free_link(AVFilterLink *link)
472 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
474 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
476 av_buffer_unref(&link->hw_frames_ctx);
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);
487 void avfilter_free(AVFilterContext *filter)
492 ff_filter_graph_remove_filter(filter->graph, filter);
494 if (filter->filter->uninit)
495 filter->filter->uninit(filter);
497 for (i = 0; i < filter->nb_inputs; i++) {
498 free_link(filter->inputs[i]);
500 for (i = 0; i < filter->nb_outputs; i++) {
501 free_link(filter->outputs[i]);
504 if (filter->filter->priv_class)
505 av_opt_free(filter->priv);
507 av_buffer_unref(&filter->hw_device_ctx);
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);
515 av_freep(&filter->internal);
519 /* process a list of value1:value2:..., each value corresponding
520 * to subsequent AVOption, in the order they are declared */
521 static int process_unnamed_options(AVFilterContext *ctx, AVDictionary **options,
524 const AVOption *o = NULL;
525 const char *p = args;
529 o = av_opt_next(ctx->priv, o);
531 av_log(ctx, AV_LOG_ERROR, "More options provided than "
532 "this filter supports.\n");
533 return AVERROR(EINVAL);
535 if (o->type == AV_OPT_TYPE_CONST)
538 val = av_get_token(&p, ":");
540 return AVERROR(ENOMEM);
542 av_dict_set(options, o->name, val, 0);
552 int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
556 ret = av_opt_set_dict(ctx, options);
558 av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
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;
568 ctx->thread_type = 0;
571 if (ctx->filter->priv_class) {
572 ret = av_opt_set_dict(ctx->priv, options);
574 av_log(ctx, AV_LOG_ERROR, "Error applying options to the filter.\n");
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);
587 int avfilter_init_str(AVFilterContext *filter, const char *args)
589 AVDictionary *options = NULL;
590 AVDictionaryEntry *e;
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);
600 if (strchr(args, '=')) {
601 /* assume a list of key1=value1:key2=value2:... */
602 ret = av_dict_parse_string(&options, args, "=", ":", 0);
606 ret = process_unnamed_options(filter, &options, args);
612 ret = avfilter_init_dict(filter, &options);
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;
623 av_dict_free(&options);
628 const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
630 return pads[pad_idx].name;
633 enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
635 return pads[pad_idx].type;
638 static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
640 return ff_filter_frame(link->dst->outputs[0], frame);
643 int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
645 int (*filter_frame)(AVFilterLink *, AVFrame *);
646 AVFilterPad *dst = link->dstpad;
650 FF_DPRINTF_START(NULL, filter_frame);
651 ff_dlog_link(NULL, link, 1);
653 if (!(filter_frame = dst->filter_frame))
654 filter_frame = default_filter_frame;
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");
660 switch (link->type) {
661 case AVMEDIA_TYPE_VIDEO:
662 out = ff_get_video_buffer(link, link->w, link->h);
664 case AVMEDIA_TYPE_AUDIO:
665 out = ff_get_audio_buffer(link, frame->nb_samples);
668 ret = AVERROR(EINVAL);
672 ret = AVERROR(ENOMEM);
676 ret = av_frame_copy_props(out, frame);
680 switch (link->type) {
681 case AVMEDIA_TYPE_VIDEO:
682 av_image_copy(out->data, out->linesize, frame->data, frame->linesize,
683 frame->format, frame->width, frame->height);
685 case AVMEDIA_TYPE_AUDIO:
686 av_samples_copy(out->extended_data, frame->extended_data,
687 0, 0, frame->nb_samples,
688 av_get_channel_layout_nb_channels(frame->channel_layout),
692 ret = AVERROR(EINVAL);
696 av_frame_free(&frame);
700 return filter_frame(link, out);
704 av_frame_free(&frame);
708 const AVClass *avfilter_get_class(void)
710 return &avfilter_class;
713 int ff_filter_init_hw_frames(AVFilterContext *avctx, AVFilterLink *link,
714 int default_pool_size)
716 AVHWFramesContext *frames;
718 // Must already be set by caller.
719 av_assert0(link->hw_frames_ctx);
721 frames = (AVHWFramesContext*)link->hw_frames_ctx->data;
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;
728 frames->initial_pool_size = default_pool_size;