3 * Copyright (c) 2008 Vitor Sessak
4 * Copyright (c) 2007 Bobby Bingham
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libavutil/avassert.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavcodec/avcodec.h" // avcodec_find_best_pix_fmt_of_2()
32 #include "avfiltergraph.h"
36 #define OFFSET(x) offsetof(AVFilterGraph,x)
38 static const AVOption options[]={
39 {"scale_sws_opts" , "default scale filter options" , OFFSET(scale_sws_opts) , AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, 0 },
40 {"aresample_swr_opts" , "default aresample filter options" , OFFSET(aresample_swr_opts) , AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, 0 },
45 static const AVClass filtergraph_class = {
46 .class_name = "AVFilterGraph",
47 .item_name = av_default_item_name,
49 .version = LIBAVUTIL_VERSION_INT,
50 .category = AV_CLASS_CATEGORY_FILTER,
53 AVFilterGraph *avfilter_graph_alloc(void)
55 AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
58 ret->av_class = &filtergraph_class;
62 void avfilter_graph_free(AVFilterGraph **graph)
66 for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
67 avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
68 av_freep(&(*graph)->sink_links);
69 av_freep(&(*graph)->scale_sws_opts);
70 av_freep(&(*graph)->aresample_swr_opts);
71 av_freep(&(*graph)->filters);
75 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
77 AVFilterContext **filters = av_realloc(graph->filters,
78 sizeof(AVFilterContext*) * (graph->filter_count+1));
80 return AVERROR(ENOMEM);
82 graph->filters = filters;
83 graph->filters[graph->filter_count++] = filter;
88 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
89 const char *name, const char *args, void *opaque,
90 AVFilterGraph *graph_ctx)
94 if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
96 if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
98 if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
104 avfilter_free(*filt_ctx);
109 void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
111 graph->disable_auto_convert = flags;
115 * Check for the validity of graph.
117 * A graph is considered valid if all its input and output pads are
120 * @return 0 in case of success, a negative value otherwise
122 static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
124 AVFilterContext *filt;
127 for (i = 0; i < graph->filter_count; i++) {
128 const AVFilterPad *pad;
129 filt = graph->filters[i];
131 for (j = 0; j < filt->nb_inputs; j++) {
132 if (!filt->inputs[j] || !filt->inputs[j]->src) {
133 pad = &filt->input_pads[j];
134 av_log(log_ctx, AV_LOG_ERROR,
135 "Input pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any source\n",
136 pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
137 return AVERROR(EINVAL);
141 for (j = 0; j < filt->nb_outputs; j++) {
142 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
143 pad = &filt->output_pads[j];
144 av_log(log_ctx, AV_LOG_ERROR,
145 "Output pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any destination\n",
146 pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
147 return AVERROR(EINVAL);
156 * Configure all the links of graphctx.
158 * @return 0 in case of success, a negative value otherwise
160 static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
162 AVFilterContext *filt;
165 for (i=0; i < graph->filter_count; i++) {
166 filt = graph->filters[i];
168 if (!filt->nb_outputs) {
169 if ((ret = avfilter_config_links(filt)))
177 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
181 for (i = 0; i < graph->filter_count; i++)
182 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
183 return graph->filters[i];
188 static void sanitize_channel_layouts(void *log, AVFilterChannelLayouts *l)
192 if (l->nb_channel_layouts) {
193 if (l->all_layouts || l->all_counts)
194 av_log(log, AV_LOG_WARNING, "All layouts set on non-empty list\n");
195 l->all_layouts = l->all_counts = 0;
197 if (l->all_counts && !l->all_layouts)
198 av_log(log, AV_LOG_WARNING, "All counts without all layouts\n");
203 static int filter_query_formats(AVFilterContext *ctx)
206 AVFilterFormats *formats;
207 AVFilterChannelLayouts *chlayouts;
208 AVFilterFormats *samplerates;
209 enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
210 ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
213 if ((ret = ctx->filter->query_formats(ctx)) < 0) {
214 av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
215 ctx->name, av_err2str(ret));
219 for (i = 0; i < ctx->nb_inputs; i++)
220 sanitize_channel_layouts(ctx, ctx->inputs[i]->out_channel_layouts);
221 for (i = 0; i < ctx->nb_outputs; i++)
222 sanitize_channel_layouts(ctx, ctx->outputs[i]->in_channel_layouts);
224 formats = ff_all_formats(type);
226 return AVERROR(ENOMEM);
227 ff_set_common_formats(ctx, formats);
228 if (type == AVMEDIA_TYPE_AUDIO) {
229 samplerates = ff_all_samplerates();
231 return AVERROR(ENOMEM);
232 ff_set_common_samplerates(ctx, samplerates);
233 chlayouts = ff_all_channel_layouts();
235 return AVERROR(ENOMEM);
236 ff_set_common_channel_layouts(ctx, chlayouts);
241 static int insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
242 const char *filt_name, const char *filt_args)
244 static int auto_count = 0, ret;
246 AVFilterContext *filt_ctx;
248 if (graph->disable_auto_convert) {
249 av_log(NULL, AV_LOG_ERROR,
250 "The filters '%s' and '%s' do not have a common format "
251 "and automatic conversion is disabled.\n",
252 link->src->name, link->dst->name);
253 return AVERROR(EINVAL);
256 snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
257 filt_name, auto_count++);
259 if ((ret = avfilter_graph_create_filter(&filt_ctx,
260 avfilter_get_by_name(filt_name),
261 inst_name, filt_args, NULL, graph)) < 0)
263 if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
266 filter_query_formats(filt_ctx);
268 if ( ((link = filt_ctx-> inputs[0]) &&
269 !ff_merge_formats(link->in_formats, link->out_formats)) ||
270 ((link = filt_ctx->outputs[0]) &&
271 !ff_merge_formats(link->in_formats, link->out_formats))
273 av_log(NULL, AV_LOG_ERROR,
274 "Impossible to convert between the formats supported by the filter "
275 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
276 return AVERROR(EINVAL);
279 if (link->type == AVMEDIA_TYPE_AUDIO &&
280 (((link = filt_ctx-> inputs[0]) &&
281 !ff_merge_channel_layouts(link->in_channel_layouts, link->out_channel_layouts)) ||
282 ((link = filt_ctx->outputs[0]) &&
283 !ff_merge_channel_layouts(link->in_channel_layouts, link->out_channel_layouts)))
285 av_log(NULL, AV_LOG_ERROR,
286 "Impossible to convert between the channel layouts formats supported by the filter "
287 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
288 return AVERROR(EINVAL);
294 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
299 AVFilterFormats *formats;
300 AVFilterChannelLayouts *chlayouts;
301 AVFilterFormats *samplerates;
303 int scaler_count = 0, resampler_count = 0;
305 for (j = 0; j < 2; j++) {
306 /* ask all the sub-filters for their supported media formats */
307 for (i = 0; i < graph->filter_count; i++) {
308 /* Call query_formats on sources first.
309 This is a temporary workaround for amerge,
310 until format renegociation is implemented. */
311 if (!graph->filters[i]->nb_inputs == j)
313 if (graph->filters[i]->filter->query_formats)
314 ret = filter_query_formats(graph->filters[i]);
316 ret = ff_default_query_formats(graph->filters[i]);
322 /* go through and merge as many format lists as possible */
323 for (i = 0; i < graph->filter_count; i++) {
324 AVFilterContext *filter = graph->filters[i];
326 for (j = 0; j < filter->nb_inputs; j++) {
327 AVFilterLink *link = filter->inputs[j];
331 if (!link->in_formats || !link->out_formats)
332 return AVERROR(EINVAL);
334 if (link->type == AVMEDIA_TYPE_VIDEO &&
335 !ff_merge_formats(link->in_formats, link->out_formats)) {
337 /* couldn't merge format lists, auto-insert scale filter */
338 snprintf(filt_args, sizeof(filt_args), "0:0:%s",
339 graph->scale_sws_opts);
340 if (ret = insert_conv_filter(graph, link, "scale", filt_args))
343 else if (link->type == AVMEDIA_TYPE_AUDIO) {
344 if (!link->in_channel_layouts || !link->out_channel_layouts)
345 return AVERROR(EINVAL);
347 /* Merge all three list before checking: that way, in all
348 * three categories, aconvert will use a common format
349 * whenever possible. */
350 formats = ff_merge_formats(link->in_formats, link->out_formats);
351 chlayouts = ff_merge_channel_layouts(link->in_channel_layouts , link->out_channel_layouts);
352 samplerates = ff_merge_samplerates (link->in_samplerates, link->out_samplerates);
354 if (!formats || !chlayouts || !samplerates)
355 if (ret = insert_conv_filter(graph, link, "aresample", NULL))
358 int convert_needed = 0;
363 if (link->in_formats != link->out_formats &&
364 !ff_merge_formats(link->in_formats,
367 if (link->type == AVMEDIA_TYPE_AUDIO) {
368 if (link->in_channel_layouts != link->out_channel_layouts &&
369 !ff_merge_channel_layouts(link->in_channel_layouts,
370 link->out_channel_layouts))
372 if (link->in_samplerates != link->out_samplerates &&
373 !ff_merge_samplerates(link->in_samplerates,
374 link->out_samplerates))
378 if (convert_needed) {
379 AVFilterContext *convert;
381 AVFilterLink *inlink, *outlink;
382 char scale_args[256];
385 /* couldn't merge format lists. auto-insert conversion filter */
386 switch (link->type) {
387 case AVMEDIA_TYPE_VIDEO:
388 if (!(filter = avfilter_get_by_name("scale"))) {
389 av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
390 "not present, cannot convert pixel formats.\n");
391 return AVERROR(EINVAL);
394 snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
396 if (graph->scale_sws_opts)
397 snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
399 snprintf(scale_args, sizeof(scale_args), "0:0");
401 if ((ret = avfilter_graph_create_filter(&convert, filter,
402 inst_name, scale_args, NULL,
406 case AVMEDIA_TYPE_AUDIO:
407 if (!(filter = avfilter_get_by_name("aresample"))) {
408 av_log(log_ctx, AV_LOG_ERROR, "'aresample' filter "
409 "not present, cannot convert audio formats.\n");
410 return AVERROR(EINVAL);
413 snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
415 if ((ret = avfilter_graph_create_filter(&convert, filter,
416 inst_name, graph->aresample_swr_opts, NULL, graph)) < 0)
420 return AVERROR(EINVAL);
423 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
426 filter_query_formats(convert);
427 inlink = convert->inputs[0];
428 outlink = convert->outputs[0];
429 if (!ff_merge_formats( inlink->in_formats, inlink->out_formats) ||
430 !ff_merge_formats(outlink->in_formats, outlink->out_formats))
431 ret |= AVERROR(ENOSYS);
432 if (inlink->type == AVMEDIA_TYPE_AUDIO &&
433 (!ff_merge_samplerates(inlink->in_samplerates,
434 inlink->out_samplerates) ||
435 !ff_merge_channel_layouts(inlink->in_channel_layouts,
436 inlink->out_channel_layouts)))
437 ret |= AVERROR(ENOSYS);
438 if (outlink->type == AVMEDIA_TYPE_AUDIO &&
439 (!ff_merge_samplerates(outlink->in_samplerates,
440 outlink->out_samplerates) ||
441 !ff_merge_channel_layouts(outlink->in_channel_layouts,
442 outlink->out_channel_layouts)))
443 ret |= AVERROR(ENOSYS);
446 av_log(log_ctx, AV_LOG_ERROR,
447 "Impossible to convert between the formats supported by the filter "
448 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
459 static int pick_format(AVFilterLink *link, AVFilterLink *ref)
461 if (!link || !link->in_formats)
464 if (link->type == AVMEDIA_TYPE_VIDEO) {
465 if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
466 int has_alpha= av_pix_fmt_desc_get(ref->format)->nb_components % 2 == 0;
467 enum AVPixelFormat best= AV_PIX_FMT_NONE;
469 for (i=0; i<link->in_formats->format_count; i++) {
470 enum AVPixelFormat p = link->in_formats->formats[i];
471 best= avcodec_find_best_pix_fmt_of_2(best, p, ref->format, has_alpha, NULL);
473 av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
474 av_get_pix_fmt_name(best), link->in_formats->format_count,
475 av_get_pix_fmt_name(ref->format), has_alpha);
476 link->in_formats->formats[0] = best;
480 link->in_formats->format_count = 1;
481 link->format = link->in_formats->formats[0];
483 if (link->type == AVMEDIA_TYPE_AUDIO) {
484 if (!link->in_samplerates->format_count) {
485 av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
486 " the link between filters %s and %s.\n", link->src->name,
488 return AVERROR(EINVAL);
490 link->in_samplerates->format_count = 1;
491 link->sample_rate = link->in_samplerates->formats[0];
493 if (link->in_channel_layouts->all_layouts) {
494 av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
495 "the link between filters %s and %s.\n", link->src->name,
497 return AVERROR(EINVAL);
499 link->in_channel_layouts->nb_channel_layouts = 1;
500 link->channel_layout = link->in_channel_layouts->channel_layouts[0];
501 if ((link->channels = FF_LAYOUT2COUNT(link->channel_layout)))
502 link->channel_layout = 0;
504 link->channels = av_get_channel_layout_nb_channels(link->channel_layout);
507 ff_formats_unref(&link->in_formats);
508 ff_formats_unref(&link->out_formats);
509 ff_formats_unref(&link->in_samplerates);
510 ff_formats_unref(&link->out_samplerates);
511 ff_channel_layouts_unref(&link->in_channel_layouts);
512 ff_channel_layouts_unref(&link->out_channel_layouts);
517 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
519 for (i = 0; i < filter->nb_inputs; i++) { \
520 AVFilterLink *link = filter->inputs[i]; \
523 if (!link->out_ ## list || link->out_ ## list->nb != 1) \
525 fmt = link->out_ ## list->var[0]; \
527 for (j = 0; j < filter->nb_outputs; j++) { \
528 AVFilterLink *out_link = filter->outputs[j]; \
531 if (link->type != out_link->type || \
532 out_link->in_ ## list->nb == 1) \
534 fmts = out_link->in_ ## list; \
536 if (!out_link->in_ ## list->nb) { \
537 add_format(&out_link->in_ ##list, fmt); \
541 for (k = 0; k < out_link->in_ ## list->nb; k++) \
542 if (fmts->var[k] == fmt) { \
543 fmts->var[0] = fmt; \
552 static int reduce_formats_on_filter(AVFilterContext *filter)
554 int i, j, k, ret = 0;
556 REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
557 format_count, ff_add_format);
558 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
559 format_count, ff_add_format);
561 /* reduce channel layouts */
562 for (i = 0; i < filter->nb_inputs; i++) {
563 AVFilterLink *inlink = filter->inputs[i];
566 if (!inlink->out_channel_layouts ||
567 inlink->out_channel_layouts->nb_channel_layouts != 1)
569 fmt = inlink->out_channel_layouts->channel_layouts[0];
571 for (j = 0; j < filter->nb_outputs; j++) {
572 AVFilterLink *outlink = filter->outputs[j];
573 AVFilterChannelLayouts *fmts;
575 fmts = outlink->in_channel_layouts;
576 if (inlink->type != outlink->type || fmts->nb_channel_layouts == 1)
579 if (fmts->all_layouts) {
580 /* Turn the infinite list into a singleton */
581 fmts->all_layouts = fmts->all_counts = 0;
582 ff_add_channel_layout(&outlink->in_channel_layouts, fmt);
586 for (k = 0; k < outlink->in_channel_layouts->nb_channel_layouts; k++) {
587 if (fmts->channel_layouts[k] == fmt) {
588 fmts->channel_layouts[0] = fmt;
589 fmts->nb_channel_layouts = 1;
600 static void reduce_formats(AVFilterGraph *graph)
607 for (i = 0; i < graph->filter_count; i++)
608 reduced |= reduce_formats_on_filter(graph->filters[i]);
612 static void swap_samplerates_on_filter(AVFilterContext *filter)
614 AVFilterLink *link = NULL;
618 for (i = 0; i < filter->nb_inputs; i++) {
619 link = filter->inputs[i];
621 if (link->type == AVMEDIA_TYPE_AUDIO &&
622 link->out_samplerates->format_count == 1)
625 if (i == filter->nb_inputs)
628 sample_rate = link->out_samplerates->formats[0];
630 for (i = 0; i < filter->nb_outputs; i++) {
631 AVFilterLink *outlink = filter->outputs[i];
632 int best_idx, best_diff = INT_MAX;
634 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
635 outlink->in_samplerates->format_count < 2)
638 for (j = 0; j < outlink->in_samplerates->format_count; j++) {
639 int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
641 if (diff < best_diff) {
646 FFSWAP(int, outlink->in_samplerates->formats[0],
647 outlink->in_samplerates->formats[best_idx]);
651 static void swap_samplerates(AVFilterGraph *graph)
655 for (i = 0; i < graph->filter_count; i++)
656 swap_samplerates_on_filter(graph->filters[i]);
659 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
660 #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
661 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
662 #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
663 #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
664 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
665 #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
667 /* allowable substitutions for channel pairs when comparing layouts,
668 * ordered by priority for both values */
669 static const uint64_t ch_subst[][2] = {
670 { CH_FRONT_PAIR, CH_CENTER_PAIR },
671 { CH_FRONT_PAIR, CH_WIDE_PAIR },
672 { CH_FRONT_PAIR, AV_CH_FRONT_CENTER },
673 { CH_CENTER_PAIR, CH_FRONT_PAIR },
674 { CH_CENTER_PAIR, CH_WIDE_PAIR },
675 { CH_CENTER_PAIR, AV_CH_FRONT_CENTER },
676 { CH_WIDE_PAIR, CH_FRONT_PAIR },
677 { CH_WIDE_PAIR, CH_CENTER_PAIR },
678 { CH_WIDE_PAIR, AV_CH_FRONT_CENTER },
679 { AV_CH_FRONT_CENTER, CH_FRONT_PAIR },
680 { AV_CH_FRONT_CENTER, CH_CENTER_PAIR },
681 { AV_CH_FRONT_CENTER, CH_WIDE_PAIR },
682 { CH_SIDE_PAIR, CH_DIRECT_PAIR },
683 { CH_SIDE_PAIR, CH_BACK_PAIR },
684 { CH_SIDE_PAIR, AV_CH_BACK_CENTER },
685 { CH_BACK_PAIR, CH_DIRECT_PAIR },
686 { CH_BACK_PAIR, CH_SIDE_PAIR },
687 { CH_BACK_PAIR, AV_CH_BACK_CENTER },
688 { AV_CH_BACK_CENTER, CH_BACK_PAIR },
689 { AV_CH_BACK_CENTER, CH_DIRECT_PAIR },
690 { AV_CH_BACK_CENTER, CH_SIDE_PAIR },
693 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
695 AVFilterLink *link = NULL;
698 for (i = 0; i < filter->nb_inputs; i++) {
699 link = filter->inputs[i];
701 if (link->type == AVMEDIA_TYPE_AUDIO &&
702 link->out_channel_layouts->nb_channel_layouts == 1)
705 if (i == filter->nb_inputs)
708 for (i = 0; i < filter->nb_outputs; i++) {
709 AVFilterLink *outlink = filter->outputs[i];
710 int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
712 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
713 outlink->in_channel_layouts->nb_channel_layouts < 2)
716 for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
717 uint64_t in_chlayout = link->out_channel_layouts->channel_layouts[0];
718 uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
719 int in_channels = av_get_channel_layout_nb_channels(in_chlayout);
720 int out_channels = av_get_channel_layout_nb_channels(out_chlayout);
721 int count_diff = out_channels - in_channels;
722 int matched_channels, extra_channels;
725 if (FF_LAYOUT2COUNT(in_chlayout) || FF_LAYOUT2COUNT(out_chlayout)) {
726 /* Compute score in case the input or output layout encodes
727 a channel count; in this case the score is not altered by
728 the computation afterwards, as in_chlayout and
729 out_chlayout have both been set to 0 */
730 if (FF_LAYOUT2COUNT(in_chlayout))
731 in_channels = FF_LAYOUT2COUNT(in_chlayout);
732 if (FF_LAYOUT2COUNT(out_chlayout))
733 out_channels = FF_LAYOUT2COUNT(out_chlayout);
734 score -= 10000 + FFABS(out_channels - in_channels) +
735 (in_channels > out_channels ? 10000 : 0);
736 in_chlayout = out_chlayout = 0;
737 /* Let the remaining computation run, even if the score
738 value is not altered */
741 /* channel substitution */
742 for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
743 uint64_t cmp0 = ch_subst[k][0];
744 uint64_t cmp1 = ch_subst[k][1];
745 if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
746 (out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
747 in_chlayout &= ~cmp0;
748 out_chlayout &= ~cmp1;
749 /* add score for channel match, minus a deduction for
750 having to do the substitution */
751 score += 10 * av_get_channel_layout_nb_channels(cmp1) - 2;
755 /* no penalty for LFE channel mismatch */
756 if ( (in_chlayout & AV_CH_LOW_FREQUENCY) &&
757 (out_chlayout & AV_CH_LOW_FREQUENCY))
759 in_chlayout &= ~AV_CH_LOW_FREQUENCY;
760 out_chlayout &= ~AV_CH_LOW_FREQUENCY;
762 matched_channels = av_get_channel_layout_nb_channels(in_chlayout &
764 extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
766 score += 10 * matched_channels - 5 * extra_channels;
768 if (score > best_score ||
769 (count_diff < best_count_diff && score == best_score)) {
772 best_count_diff = count_diff;
775 av_assert0(best_idx >= 0);
776 FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
777 outlink->in_channel_layouts->channel_layouts[best_idx]);
782 static void swap_channel_layouts(AVFilterGraph *graph)
786 for (i = 0; i < graph->filter_count; i++)
787 swap_channel_layouts_on_filter(graph->filters[i]);
790 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
792 AVFilterLink *link = NULL;
796 for (i = 0; i < filter->nb_inputs; i++) {
797 link = filter->inputs[i];
799 if (link->type == AVMEDIA_TYPE_AUDIO &&
800 link->out_formats->format_count == 1)
803 if (i == filter->nb_inputs)
806 format = link->out_formats->formats[0];
807 bps = av_get_bytes_per_sample(format);
809 for (i = 0; i < filter->nb_outputs; i++) {
810 AVFilterLink *outlink = filter->outputs[i];
811 int best_idx = -1, best_score = INT_MIN;
813 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
814 outlink->in_formats->format_count < 2)
817 for (j = 0; j < outlink->in_formats->format_count; j++) {
818 int out_format = outlink->in_formats->formats[j];
819 int out_bps = av_get_bytes_per_sample(out_format);
822 if (av_get_packed_sample_fmt(out_format) == format ||
823 av_get_planar_sample_fmt(out_format) == format) {
828 /* for s32 and float prefer double to prevent loss of information */
829 if (bps == 4 && out_bps == 8) {
834 /* prefer closest higher or equal bps */
835 score = -abs(out_bps - bps);
839 if (score > best_score) {
844 av_assert0(best_idx >= 0);
845 FFSWAP(int, outlink->in_formats->formats[0],
846 outlink->in_formats->formats[best_idx]);
850 static void swap_sample_fmts(AVFilterGraph *graph)
854 for (i = 0; i < graph->filter_count; i++)
855 swap_sample_fmts_on_filter(graph->filters[i]);
859 static int pick_formats(AVFilterGraph *graph)
866 for (i = 0; i < graph->filter_count; i++) {
867 AVFilterContext *filter = graph->filters[i];
868 if (filter->nb_inputs){
869 for (j = 0; j < filter->nb_inputs; j++){
870 if(filter->inputs[j]->in_formats && filter->inputs[j]->in_formats->format_count == 1) {
871 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
877 if (filter->nb_outputs){
878 for (j = 0; j < filter->nb_outputs; j++){
879 if(filter->outputs[j]->in_formats && filter->outputs[j]->in_formats->format_count == 1) {
880 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
886 if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
887 for (j = 0; j < filter->nb_outputs; j++) {
888 if(filter->outputs[j]->format<0) {
889 if ((ret = pick_format(filter->outputs[j], filter->inputs[0])) < 0)
898 for (i = 0; i < graph->filter_count; i++) {
899 AVFilterContext *filter = graph->filters[i];
901 for (j = 0; j < filter->nb_inputs; j++)
902 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
904 for (j = 0; j < filter->nb_outputs; j++)
905 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
912 * Configure the formats of all the links in the graph.
914 static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
918 /* find supported formats from sub-filters, and merge along links */
919 if ((ret = query_formats(graph, log_ctx)) < 0)
922 /* Once everything is merged, it's possible that we'll still have
923 * multiple valid media format choices. We try to minimize the amount
924 * of format conversion inside filters */
925 reduce_formats(graph);
927 /* for audio filters, ensure the best format, sample rate and channel layout
929 swap_sample_fmts(graph);
930 swap_samplerates(graph);
931 swap_channel_layouts(graph);
933 if ((ret = pick_formats(graph)) < 0)
939 static int ff_avfilter_graph_config_pointers(AVFilterGraph *graph,
943 int sink_links_count = 0, n = 0;
945 AVFilterLink **sinks;
947 for (i = 0; i < graph->filter_count; i++) {
948 f = graph->filters[i];
949 for (j = 0; j < f->nb_inputs; j++) {
950 f->inputs[j]->graph = graph;
951 f->inputs[j]->age_index = -1;
953 for (j = 0; j < f->nb_outputs; j++) {
954 f->outputs[j]->graph = graph;
955 f->outputs[j]->age_index= -1;
957 if (!f->nb_outputs) {
958 if (f->nb_inputs > INT_MAX - sink_links_count)
959 return AVERROR(EINVAL);
960 sink_links_count += f->nb_inputs;
963 sinks = av_calloc(sink_links_count, sizeof(*sinks));
965 return AVERROR(ENOMEM);
966 for (i = 0; i < graph->filter_count; i++) {
967 f = graph->filters[i];
968 if (!f->nb_outputs) {
969 for (j = 0; j < f->nb_inputs; j++) {
970 sinks[n] = f->inputs[j];
971 f->inputs[j]->age_index = n++;
975 av_assert0(n == sink_links_count);
976 graph->sink_links = sinks;
977 graph->sink_links_count = sink_links_count;
981 static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
987 for (i = 0; i < graph->filter_count; i++) {
988 f = graph->filters[i];
990 for (j = 0; j < f->nb_inputs; j++) {
991 AVFilterLink *link = f->inputs[j];
992 AVFilterContext *fifo_ctx;
996 if (!link->dstpad->needs_fifo)
999 fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
1000 avfilter_get_by_name("fifo") :
1001 avfilter_get_by_name("afifo");
1003 snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
1005 ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
1010 ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
1019 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
1023 if ((ret = graph_check_validity(graphctx, log_ctx)))
1025 if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
1027 if ((ret = graph_config_formats(graphctx, log_ctx)))
1029 if ((ret = graph_config_links(graphctx, log_ctx)))
1031 if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
1037 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
1039 int i, r = AVERROR(ENOSYS);
1044 if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
1045 r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
1046 if(r != AVERROR(ENOSYS))
1053 for (i = 0; i < graph->filter_count; i++) {
1054 AVFilterContext *filter = graph->filters[i];
1055 if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
1056 r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
1057 if(r != AVERROR(ENOSYS)) {
1058 if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
1067 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
1074 for (i = 0; i < graph->filter_count; i++) {
1075 AVFilterContext *filter = graph->filters[i];
1076 if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
1077 AVFilterCommand **queue = &filter->command_queue, *next;
1078 while (*queue && (*queue)->time <= ts)
1079 queue = &(*queue)->next;
1081 *queue = av_mallocz(sizeof(AVFilterCommand));
1082 (*queue)->command = av_strdup(command);
1083 (*queue)->arg = av_strdup(arg);
1084 (*queue)->time = ts;
1085 (*queue)->flags = flags;
1086 (*queue)->next = next;
1087 if(flags & AVFILTER_CMD_FLAG_ONE)
1095 static void heap_bubble_up(AVFilterGraph *graph,
1096 AVFilterLink *link, int index)
1098 AVFilterLink **links = graph->sink_links;
1101 int parent = (index - 1) >> 1;
1102 if (links[parent]->current_pts >= link->current_pts)
1104 links[index] = links[parent];
1105 links[index]->age_index = index;
1108 links[index] = link;
1109 link->age_index = index;
1112 static void heap_bubble_down(AVFilterGraph *graph,
1113 AVFilterLink *link, int index)
1115 AVFilterLink **links = graph->sink_links;
1118 int child = 2 * index + 1;
1119 if (child >= graph->sink_links_count)
1121 if (child + 1 < graph->sink_links_count &&
1122 links[child + 1]->current_pts < links[child]->current_pts)
1124 if (link->current_pts < links[child]->current_pts)
1126 links[index] = links[child];
1127 links[index]->age_index = index;
1130 links[index] = link;
1131 link->age_index = index;
1134 void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
1136 heap_bubble_up (graph, link, link->age_index);
1137 heap_bubble_down(graph, link, link->age_index);
1141 int avfilter_graph_request_oldest(AVFilterGraph *graph)
1143 while (graph->sink_links_count) {
1144 AVFilterLink *oldest = graph->sink_links[0];
1145 int r = ff_request_frame(oldest);
1146 if (r != AVERROR_EOF)
1148 av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
1149 oldest->dst ? oldest->dst->name : "unknown",
1150 oldest->dstpad ? oldest->dstpad->name : "unknown");
1151 /* EOF: remove the link from the heap */
1152 if (oldest->age_index < --graph->sink_links_count)
1153 heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
1155 oldest->age_index = -1;