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
25 #include "libavutil/avassert.h"
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavcodec/avcodec.h" // avcodec_find_best_pix_fmt_of_2()
31 #include "avfiltergraph.h"
35 #define OFFSET(x) offsetof(AVFilterGraph,x)
37 static const AVOption options[]={
38 {"scale_sws_opts" , "default scale filter options" , OFFSET(scale_sws_opts) , AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, 0 },
39 {"aresample_swr_opts" , "default aresample filter options" , OFFSET(aresample_swr_opts) , AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, 0 },
44 static const AVClass filtergraph_class = {
45 .class_name = "AVFilterGraph",
46 .item_name = av_default_item_name,
48 .version = LIBAVUTIL_VERSION_INT,
49 .category = AV_CLASS_CATEGORY_FILTER,
52 AVFilterGraph *avfilter_graph_alloc(void)
54 AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
57 ret->av_class = &filtergraph_class;
61 void avfilter_graph_free(AVFilterGraph **graph)
65 for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
66 avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
67 av_freep(&(*graph)->sink_links);
68 av_freep(&(*graph)->scale_sws_opts);
69 av_freep(&(*graph)->aresample_swr_opts);
70 av_freep(&(*graph)->resample_lavr_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 query_formats(AVFilterGraph *graph, AVClass *log_ctx)
244 int scaler_count = 0, resampler_count = 0;
246 for (j = 0; j < 2; j++) {
247 /* ask all the sub-filters for their supported media formats */
248 for (i = 0; i < graph->filter_count; i++) {
249 /* Call query_formats on sources first.
250 This is a temporary workaround for amerge,
251 until format renegociation is implemented. */
252 if (!graph->filters[i]->nb_inputs == j)
254 if (graph->filters[i]->filter->query_formats)
255 ret = filter_query_formats(graph->filters[i]);
257 ret = ff_default_query_formats(graph->filters[i]);
263 /* go through and merge as many format lists as possible */
264 for (i = 0; i < graph->filter_count; i++) {
265 AVFilterContext *filter = graph->filters[i];
267 for (j = 0; j < filter->nb_inputs; j++) {
268 AVFilterLink *link = filter->inputs[j];
269 int convert_needed = 0;
274 if (link->in_formats != link->out_formats &&
275 !ff_merge_formats(link->in_formats, link->out_formats,
278 if (link->type == AVMEDIA_TYPE_AUDIO) {
279 if (link->in_channel_layouts != link->out_channel_layouts &&
280 !ff_merge_channel_layouts(link->in_channel_layouts,
281 link->out_channel_layouts))
283 if (link->in_samplerates != link->out_samplerates &&
284 !ff_merge_samplerates(link->in_samplerates,
285 link->out_samplerates))
289 if (convert_needed) {
290 AVFilterContext *convert;
292 AVFilterLink *inlink, *outlink;
293 char scale_args[256];
296 /* couldn't merge format lists. auto-insert conversion filter */
297 switch (link->type) {
298 case AVMEDIA_TYPE_VIDEO:
299 if (!(filter = avfilter_get_by_name("scale"))) {
300 av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
301 "not present, cannot convert pixel formats.\n");
302 return AVERROR(EINVAL);
305 snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
307 if (graph->scale_sws_opts)
308 snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
310 snprintf(scale_args, sizeof(scale_args), "0:0");
312 if ((ret = avfilter_graph_create_filter(&convert, filter,
313 inst_name, scale_args, NULL,
317 case AVMEDIA_TYPE_AUDIO:
318 if (!(filter = avfilter_get_by_name("aresample"))) {
319 av_log(log_ctx, AV_LOG_ERROR, "'aresample' filter "
320 "not present, cannot convert audio formats.\n");
321 return AVERROR(EINVAL);
324 snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
326 scale_args[0] = '\0';
327 if (graph->aresample_swr_opts)
328 snprintf(scale_args, sizeof(scale_args), "%s",
329 graph->aresample_swr_opts);
330 if ((ret = avfilter_graph_create_filter(&convert, filter,
331 inst_name, graph->aresample_swr_opts,
336 return AVERROR(EINVAL);
339 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
342 filter_query_formats(convert);
343 inlink = convert->inputs[0];
344 outlink = convert->outputs[0];
345 if (!ff_merge_formats( inlink->in_formats, inlink->out_formats, inlink->type) ||
346 !ff_merge_formats(outlink->in_formats, outlink->out_formats, outlink->type))
347 ret |= AVERROR(ENOSYS);
348 if (inlink->type == AVMEDIA_TYPE_AUDIO &&
349 (!ff_merge_samplerates(inlink->in_samplerates,
350 inlink->out_samplerates) ||
351 !ff_merge_channel_layouts(inlink->in_channel_layouts,
352 inlink->out_channel_layouts)))
353 ret |= AVERROR(ENOSYS);
354 if (outlink->type == AVMEDIA_TYPE_AUDIO &&
355 (!ff_merge_samplerates(outlink->in_samplerates,
356 outlink->out_samplerates) ||
357 !ff_merge_channel_layouts(outlink->in_channel_layouts,
358 outlink->out_channel_layouts)))
359 ret |= AVERROR(ENOSYS);
362 av_log(log_ctx, AV_LOG_ERROR,
363 "Impossible to convert between the formats supported by the filter "
364 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
374 static int pick_format(AVFilterLink *link, AVFilterLink *ref)
376 if (!link || !link->in_formats)
379 if (link->type == AVMEDIA_TYPE_VIDEO) {
380 if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
381 int has_alpha= av_pix_fmt_desc_get(ref->format)->nb_components % 2 == 0;
382 enum AVPixelFormat best= AV_PIX_FMT_NONE;
384 for (i=0; i<link->in_formats->format_count; i++) {
385 enum AVPixelFormat p = link->in_formats->formats[i];
386 best= avcodec_find_best_pix_fmt_of_2(best, p, ref->format, has_alpha, NULL);
388 av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
389 av_get_pix_fmt_name(best), link->in_formats->format_count,
390 av_get_pix_fmt_name(ref->format), has_alpha);
391 link->in_formats->formats[0] = best;
395 link->in_formats->format_count = 1;
396 link->format = link->in_formats->formats[0];
398 if (link->type == AVMEDIA_TYPE_AUDIO) {
399 if (!link->in_samplerates->format_count) {
400 av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
401 " the link between filters %s and %s.\n", link->src->name,
403 return AVERROR(EINVAL);
405 link->in_samplerates->format_count = 1;
406 link->sample_rate = link->in_samplerates->formats[0];
408 if (link->in_channel_layouts->all_layouts) {
409 av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
410 " the link between filters %s and %s.\n", link->src->name,
412 return AVERROR(EINVAL);
414 link->in_channel_layouts->nb_channel_layouts = 1;
415 link->channel_layout = link->in_channel_layouts->channel_layouts[0];
416 if ((link->channels = FF_LAYOUT2COUNT(link->channel_layout)))
417 link->channel_layout = 0;
419 link->channels = av_get_channel_layout_nb_channels(link->channel_layout);
422 ff_formats_unref(&link->in_formats);
423 ff_formats_unref(&link->out_formats);
424 ff_formats_unref(&link->in_samplerates);
425 ff_formats_unref(&link->out_samplerates);
426 ff_channel_layouts_unref(&link->in_channel_layouts);
427 ff_channel_layouts_unref(&link->out_channel_layouts);
432 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
434 for (i = 0; i < filter->nb_inputs; i++) { \
435 AVFilterLink *link = filter->inputs[i]; \
438 if (!link->out_ ## list || link->out_ ## list->nb != 1) \
440 fmt = link->out_ ## list->var[0]; \
442 for (j = 0; j < filter->nb_outputs; j++) { \
443 AVFilterLink *out_link = filter->outputs[j]; \
446 if (link->type != out_link->type || \
447 out_link->in_ ## list->nb == 1) \
449 fmts = out_link->in_ ## list; \
451 if (!out_link->in_ ## list->nb) { \
452 add_format(&out_link->in_ ##list, fmt); \
456 for (k = 0; k < out_link->in_ ## list->nb; k++) \
457 if (fmts->var[k] == fmt) { \
458 fmts->var[0] = fmt; \
467 static int reduce_formats_on_filter(AVFilterContext *filter)
469 int i, j, k, ret = 0;
471 REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
472 format_count, ff_add_format);
473 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
474 format_count, ff_add_format);
476 /* reduce channel layouts */
477 for (i = 0; i < filter->nb_inputs; i++) {
478 AVFilterLink *inlink = filter->inputs[i];
481 if (!inlink->out_channel_layouts ||
482 inlink->out_channel_layouts->nb_channel_layouts != 1)
484 fmt = inlink->out_channel_layouts->channel_layouts[0];
486 for (j = 0; j < filter->nb_outputs; j++) {
487 AVFilterLink *outlink = filter->outputs[j];
488 AVFilterChannelLayouts *fmts;
490 fmts = outlink->in_channel_layouts;
491 if (inlink->type != outlink->type || fmts->nb_channel_layouts == 1)
494 if (fmts->all_layouts) {
495 /* Turn the infinite list into a singleton */
496 fmts->all_layouts = fmts->all_counts = 0;
497 ff_add_channel_layout(&outlink->in_channel_layouts, fmt);
501 for (k = 0; k < outlink->in_channel_layouts->nb_channel_layouts; k++) {
502 if (fmts->channel_layouts[k] == fmt) {
503 fmts->channel_layouts[0] = fmt;
504 fmts->nb_channel_layouts = 1;
515 static void reduce_formats(AVFilterGraph *graph)
522 for (i = 0; i < graph->filter_count; i++)
523 reduced |= reduce_formats_on_filter(graph->filters[i]);
527 static void swap_samplerates_on_filter(AVFilterContext *filter)
529 AVFilterLink *link = NULL;
533 for (i = 0; i < filter->nb_inputs; i++) {
534 link = filter->inputs[i];
536 if (link->type == AVMEDIA_TYPE_AUDIO &&
537 link->out_samplerates->format_count == 1)
540 if (i == filter->nb_inputs)
543 sample_rate = link->out_samplerates->formats[0];
545 for (i = 0; i < filter->nb_outputs; i++) {
546 AVFilterLink *outlink = filter->outputs[i];
547 int best_idx, best_diff = INT_MAX;
549 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
550 outlink->in_samplerates->format_count < 2)
553 for (j = 0; j < outlink->in_samplerates->format_count; j++) {
554 int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
556 if (diff < best_diff) {
561 FFSWAP(int, outlink->in_samplerates->formats[0],
562 outlink->in_samplerates->formats[best_idx]);
566 static void swap_samplerates(AVFilterGraph *graph)
570 for (i = 0; i < graph->filter_count; i++)
571 swap_samplerates_on_filter(graph->filters[i]);
574 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
575 #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
576 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
577 #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
578 #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
579 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
580 #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
582 /* allowable substitutions for channel pairs when comparing layouts,
583 * ordered by priority for both values */
584 static const uint64_t ch_subst[][2] = {
585 { CH_FRONT_PAIR, CH_CENTER_PAIR },
586 { CH_FRONT_PAIR, CH_WIDE_PAIR },
587 { CH_FRONT_PAIR, AV_CH_FRONT_CENTER },
588 { CH_CENTER_PAIR, CH_FRONT_PAIR },
589 { CH_CENTER_PAIR, CH_WIDE_PAIR },
590 { CH_CENTER_PAIR, AV_CH_FRONT_CENTER },
591 { CH_WIDE_PAIR, CH_FRONT_PAIR },
592 { CH_WIDE_PAIR, CH_CENTER_PAIR },
593 { CH_WIDE_PAIR, AV_CH_FRONT_CENTER },
594 { AV_CH_FRONT_CENTER, CH_FRONT_PAIR },
595 { AV_CH_FRONT_CENTER, CH_CENTER_PAIR },
596 { AV_CH_FRONT_CENTER, CH_WIDE_PAIR },
597 { CH_SIDE_PAIR, CH_DIRECT_PAIR },
598 { CH_SIDE_PAIR, CH_BACK_PAIR },
599 { CH_SIDE_PAIR, AV_CH_BACK_CENTER },
600 { CH_BACK_PAIR, CH_DIRECT_PAIR },
601 { CH_BACK_PAIR, CH_SIDE_PAIR },
602 { CH_BACK_PAIR, AV_CH_BACK_CENTER },
603 { AV_CH_BACK_CENTER, CH_BACK_PAIR },
604 { AV_CH_BACK_CENTER, CH_DIRECT_PAIR },
605 { AV_CH_BACK_CENTER, CH_SIDE_PAIR },
608 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
610 AVFilterLink *link = NULL;
613 for (i = 0; i < filter->nb_inputs; i++) {
614 link = filter->inputs[i];
616 if (link->type == AVMEDIA_TYPE_AUDIO &&
617 link->out_channel_layouts->nb_channel_layouts == 1)
620 if (i == filter->nb_inputs)
623 for (i = 0; i < filter->nb_outputs; i++) {
624 AVFilterLink *outlink = filter->outputs[i];
625 int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
627 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
628 outlink->in_channel_layouts->nb_channel_layouts < 2)
631 for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
632 uint64_t in_chlayout = link->out_channel_layouts->channel_layouts[0];
633 uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
634 int in_channels = av_get_channel_layout_nb_channels(in_chlayout);
635 int out_channels = av_get_channel_layout_nb_channels(out_chlayout);
636 int count_diff = out_channels - in_channels;
637 int matched_channels, extra_channels;
640 if (FF_LAYOUT2COUNT(in_chlayout) || FF_LAYOUT2COUNT(out_chlayout)) {
641 /* Compute score in case the input or output layout encodes
642 a channel count; in this case the score is not altered by
643 the computation afterwards, as in_chlayout and
644 out_chlayout have both been set to 0 */
645 if (FF_LAYOUT2COUNT(in_chlayout))
646 in_channels = FF_LAYOUT2COUNT(in_chlayout);
647 if (FF_LAYOUT2COUNT(out_chlayout))
648 out_channels = FF_LAYOUT2COUNT(out_chlayout);
649 score -= 10000 + FFABS(out_channels - in_channels) +
650 (in_channels > out_channels ? 10000 : 0);
651 in_chlayout = out_chlayout = 0;
652 /* Let the remaining computation run, even if the score
653 value is not altered */
656 /* channel substitution */
657 for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
658 uint64_t cmp0 = ch_subst[k][0];
659 uint64_t cmp1 = ch_subst[k][1];
660 if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
661 (out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
662 in_chlayout &= ~cmp0;
663 out_chlayout &= ~cmp1;
664 /* add score for channel match, minus a deduction for
665 having to do the substitution */
666 score += 10 * av_get_channel_layout_nb_channels(cmp1) - 2;
670 /* no penalty for LFE channel mismatch */
671 if ( (in_chlayout & AV_CH_LOW_FREQUENCY) &&
672 (out_chlayout & AV_CH_LOW_FREQUENCY))
674 in_chlayout &= ~AV_CH_LOW_FREQUENCY;
675 out_chlayout &= ~AV_CH_LOW_FREQUENCY;
677 matched_channels = av_get_channel_layout_nb_channels(in_chlayout &
679 extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
681 score += 10 * matched_channels - 5 * extra_channels;
683 if (score > best_score ||
684 (count_diff < best_count_diff && score == best_score)) {
687 best_count_diff = count_diff;
690 av_assert0(best_idx >= 0);
691 FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
692 outlink->in_channel_layouts->channel_layouts[best_idx]);
697 static void swap_channel_layouts(AVFilterGraph *graph)
701 for (i = 0; i < graph->filter_count; i++)
702 swap_channel_layouts_on_filter(graph->filters[i]);
705 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
707 AVFilterLink *link = NULL;
711 for (i = 0; i < filter->nb_inputs; i++) {
712 link = filter->inputs[i];
714 if (link->type == AVMEDIA_TYPE_AUDIO &&
715 link->out_formats->format_count == 1)
718 if (i == filter->nb_inputs)
721 format = link->out_formats->formats[0];
722 bps = av_get_bytes_per_sample(format);
724 for (i = 0; i < filter->nb_outputs; i++) {
725 AVFilterLink *outlink = filter->outputs[i];
726 int best_idx = -1, best_score = INT_MIN;
728 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
729 outlink->in_formats->format_count < 2)
732 for (j = 0; j < outlink->in_formats->format_count; j++) {
733 int out_format = outlink->in_formats->formats[j];
734 int out_bps = av_get_bytes_per_sample(out_format);
737 if (av_get_packed_sample_fmt(out_format) == format ||
738 av_get_planar_sample_fmt(out_format) == format) {
743 /* for s32 and float prefer double to prevent loss of information */
744 if (bps == 4 && out_bps == 8) {
749 /* prefer closest higher or equal bps */
750 score = -abs(out_bps - bps);
754 if (score > best_score) {
759 av_assert0(best_idx >= 0);
760 FFSWAP(int, outlink->in_formats->formats[0],
761 outlink->in_formats->formats[best_idx]);
765 static void swap_sample_fmts(AVFilterGraph *graph)
769 for (i = 0; i < graph->filter_count; i++)
770 swap_sample_fmts_on_filter(graph->filters[i]);
774 static int pick_formats(AVFilterGraph *graph)
781 for (i = 0; i < graph->filter_count; i++) {
782 AVFilterContext *filter = graph->filters[i];
783 if (filter->nb_inputs){
784 for (j = 0; j < filter->nb_inputs; j++){
785 if(filter->inputs[j]->in_formats && filter->inputs[j]->in_formats->format_count == 1) {
786 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
792 if (filter->nb_outputs){
793 for (j = 0; j < filter->nb_outputs; j++){
794 if(filter->outputs[j]->in_formats && filter->outputs[j]->in_formats->format_count == 1) {
795 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
801 if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
802 for (j = 0; j < filter->nb_outputs; j++) {
803 if(filter->outputs[j]->format<0) {
804 if ((ret = pick_format(filter->outputs[j], filter->inputs[0])) < 0)
813 for (i = 0; i < graph->filter_count; i++) {
814 AVFilterContext *filter = graph->filters[i];
816 for (j = 0; j < filter->nb_inputs; j++)
817 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
819 for (j = 0; j < filter->nb_outputs; j++)
820 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
827 * Configure the formats of all the links in the graph.
829 static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
833 /* find supported formats from sub-filters, and merge along links */
834 if ((ret = query_formats(graph, log_ctx)) < 0)
837 /* Once everything is merged, it's possible that we'll still have
838 * multiple valid media format choices. We try to minimize the amount
839 * of format conversion inside filters */
840 reduce_formats(graph);
842 /* for audio filters, ensure the best format, sample rate and channel layout
844 swap_sample_fmts(graph);
845 swap_samplerates(graph);
846 swap_channel_layouts(graph);
848 if ((ret = pick_formats(graph)) < 0)
854 static int ff_avfilter_graph_config_pointers(AVFilterGraph *graph,
858 int sink_links_count = 0, n = 0;
860 AVFilterLink **sinks;
862 for (i = 0; i < graph->filter_count; i++) {
863 f = graph->filters[i];
864 for (j = 0; j < f->nb_inputs; j++) {
865 f->inputs[j]->graph = graph;
866 f->inputs[j]->age_index = -1;
868 for (j = 0; j < f->nb_outputs; j++) {
869 f->outputs[j]->graph = graph;
870 f->outputs[j]->age_index= -1;
872 if (!f->nb_outputs) {
873 if (f->nb_inputs > INT_MAX - sink_links_count)
874 return AVERROR(EINVAL);
875 sink_links_count += f->nb_inputs;
878 sinks = av_calloc(sink_links_count, sizeof(*sinks));
880 return AVERROR(ENOMEM);
881 for (i = 0; i < graph->filter_count; i++) {
882 f = graph->filters[i];
883 if (!f->nb_outputs) {
884 for (j = 0; j < f->nb_inputs; j++) {
885 sinks[n] = f->inputs[j];
886 f->inputs[j]->age_index = n++;
890 av_assert0(n == sink_links_count);
891 graph->sink_links = sinks;
892 graph->sink_links_count = sink_links_count;
896 static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
902 for (i = 0; i < graph->filter_count; i++) {
903 f = graph->filters[i];
905 for (j = 0; j < f->nb_inputs; j++) {
906 AVFilterLink *link = f->inputs[j];
907 AVFilterContext *fifo_ctx;
911 if (!link->dstpad->needs_fifo)
914 fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
915 avfilter_get_by_name("fifo") :
916 avfilter_get_by_name("afifo");
918 snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
920 ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
925 ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
934 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
938 if ((ret = graph_check_validity(graphctx, log_ctx)))
940 if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
942 if ((ret = graph_config_formats(graphctx, log_ctx)))
944 if ((ret = graph_config_links(graphctx, log_ctx)))
946 if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
952 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
954 int i, r = AVERROR(ENOSYS);
959 if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
960 r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
961 if(r != AVERROR(ENOSYS))
968 for (i = 0; i < graph->filter_count; i++) {
969 AVFilterContext *filter = graph->filters[i];
970 if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
971 r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
972 if(r != AVERROR(ENOSYS)) {
973 if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
982 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
989 for (i = 0; i < graph->filter_count; i++) {
990 AVFilterContext *filter = graph->filters[i];
991 if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
992 AVFilterCommand **queue = &filter->command_queue, *next;
993 while (*queue && (*queue)->time <= ts)
994 queue = &(*queue)->next;
996 *queue = av_mallocz(sizeof(AVFilterCommand));
997 (*queue)->command = av_strdup(command);
998 (*queue)->arg = av_strdup(arg);
1000 (*queue)->flags = flags;
1001 (*queue)->next = next;
1002 if(flags & AVFILTER_CMD_FLAG_ONE)
1010 static void heap_bubble_up(AVFilterGraph *graph,
1011 AVFilterLink *link, int index)
1013 AVFilterLink **links = graph->sink_links;
1016 int parent = (index - 1) >> 1;
1017 if (links[parent]->current_pts >= link->current_pts)
1019 links[index] = links[parent];
1020 links[index]->age_index = index;
1023 links[index] = link;
1024 link->age_index = index;
1027 static void heap_bubble_down(AVFilterGraph *graph,
1028 AVFilterLink *link, int index)
1030 AVFilterLink **links = graph->sink_links;
1033 int child = 2 * index + 1;
1034 if (child >= graph->sink_links_count)
1036 if (child + 1 < graph->sink_links_count &&
1037 links[child + 1]->current_pts < links[child]->current_pts)
1039 if (link->current_pts < links[child]->current_pts)
1041 links[index] = links[child];
1042 links[index]->age_index = index;
1045 links[index] = link;
1046 link->age_index = index;
1049 void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
1051 heap_bubble_up (graph, link, link->age_index);
1052 heap_bubble_down(graph, link, link->age_index);
1056 int avfilter_graph_request_oldest(AVFilterGraph *graph)
1058 while (graph->sink_links_count) {
1059 AVFilterLink *oldest = graph->sink_links[0];
1060 int r = ff_request_frame(oldest);
1061 if (r != AVERROR_EOF)
1063 av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
1064 oldest->dst ? oldest->dst->name : "unknown",
1065 oldest->dstpad ? oldest->dstpad->name : "unknown");
1066 /* EOF: remove the link from the heap */
1067 if (oldest->age_index < --graph->sink_links_count)
1068 heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
1070 oldest->age_index = -1;