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/audioconvert.h"
27 #include "libavutil/avassert.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavcodec/avcodec.h" // avcodec_find_best_pix_fmt2()
31 #include "avfiltergraph.h"
35 #include "libavutil/audioconvert.h"
36 #include "libavutil/avassert.h"
37 #include "libavutil/log.h"
39 static const AVClass filtergraph_class = {
40 .class_name = "AVFilterGraph",
41 .item_name = av_default_item_name,
42 .version = LIBAVUTIL_VERSION_INT,
43 .category = AV_CLASS_CATEGORY_FILTER,
46 AVFilterGraph *avfilter_graph_alloc(void)
48 AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
51 ret->av_class = &filtergraph_class;
55 void avfilter_graph_free(AVFilterGraph **graph)
59 for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
60 avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
61 av_freep(&(*graph)->sink_links);
62 av_freep(&(*graph)->scale_sws_opts);
63 av_freep(&(*graph)->filters);
67 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
69 AVFilterContext **filters = av_realloc(graph->filters,
70 sizeof(AVFilterContext*) * (graph->filter_count+1));
72 return AVERROR(ENOMEM);
74 graph->filters = filters;
75 graph->filters[graph->filter_count++] = filter;
80 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
81 const char *name, const char *args, void *opaque,
82 AVFilterGraph *graph_ctx)
86 if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
88 if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
90 if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
96 avfilter_free(*filt_ctx);
101 void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
103 graph->disable_auto_convert = flags;
107 * Check for the validity of graph.
109 * A graph is considered valid if all its input and output pads are
112 * @return 0 in case of success, a negative value otherwise
114 static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
116 AVFilterContext *filt;
119 for (i = 0; i < graph->filter_count; i++) {
120 filt = graph->filters[i];
122 for (j = 0; j < filt->nb_inputs; j++) {
123 if (!filt->inputs[j] || !filt->inputs[j]->src) {
124 av_log(log_ctx, AV_LOG_ERROR,
125 "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
126 filt->input_pads[j].name, filt->name, filt->filter->name);
127 return AVERROR(EINVAL);
131 for (j = 0; j < filt->nb_outputs; j++) {
132 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
133 av_log(log_ctx, AV_LOG_ERROR,
134 "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
135 filt->output_pads[j].name, filt->name, filt->filter->name);
136 return AVERROR(EINVAL);
145 * Configure all the links of graphctx.
147 * @return 0 in case of success, a negative value otherwise
149 static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
151 AVFilterContext *filt;
154 for (i=0; i < graph->filter_count; i++) {
155 filt = graph->filters[i];
157 if (!filt->nb_outputs) {
158 if ((ret = avfilter_config_links(filt)))
166 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
170 for (i = 0; i < graph->filter_count; i++)
171 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
172 return graph->filters[i];
177 static int filter_query_formats(AVFilterContext *ctx)
180 AVFilterFormats *formats;
181 AVFilterChannelLayouts *chlayouts;
182 AVFilterFormats *samplerates;
183 enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
184 ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
187 if ((ret = ctx->filter->query_formats(ctx)) < 0)
190 formats = ff_all_formats(type);
192 return AVERROR(ENOMEM);
193 ff_set_common_formats(ctx, formats);
194 if (type == AVMEDIA_TYPE_AUDIO) {
195 samplerates = ff_all_samplerates();
197 return AVERROR(ENOMEM);
198 ff_set_common_samplerates(ctx, samplerates);
199 chlayouts = ff_all_channel_layouts();
201 return AVERROR(ENOMEM);
202 ff_set_common_channel_layouts(ctx, chlayouts);
207 static int insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
208 const char *filt_name, const char *filt_args)
210 static int auto_count = 0, ret;
212 AVFilterContext *filt_ctx;
214 if (graph->disable_auto_convert) {
215 av_log(NULL, AV_LOG_ERROR,
216 "The filters '%s' and '%s' do not have a common format "
217 "and automatic conversion is disabled.\n",
218 link->src->name, link->dst->name);
219 return AVERROR(EINVAL);
222 snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
223 filt_name, auto_count++);
225 if ((ret = avfilter_graph_create_filter(&filt_ctx,
226 avfilter_get_by_name(filt_name),
227 inst_name, filt_args, NULL, graph)) < 0)
229 if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
232 filter_query_formats(filt_ctx);
234 if ( ((link = filt_ctx-> inputs[0]) &&
235 !ff_merge_formats(link->in_formats, link->out_formats)) ||
236 ((link = filt_ctx->outputs[0]) &&
237 !ff_merge_formats(link->in_formats, link->out_formats))
239 av_log(NULL, AV_LOG_ERROR,
240 "Impossible to convert between the formats supported by the filter "
241 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
242 return AVERROR(EINVAL);
245 if (link->type == AVMEDIA_TYPE_AUDIO &&
246 (((link = filt_ctx-> inputs[0]) &&
247 !ff_merge_channel_layouts(link->in_channel_layouts, link->out_channel_layouts)) ||
248 ((link = filt_ctx->outputs[0]) &&
249 !ff_merge_channel_layouts(link->in_channel_layouts, link->out_channel_layouts)))
251 av_log(NULL, AV_LOG_ERROR,
252 "Impossible to convert between the channel layouts formats supported by the filter "
253 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
254 return AVERROR(EINVAL);
260 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
264 AVFilterFormats *formats;
265 AVFilterChannelLayouts *chlayouts;
266 AVFilterFormats *samplerates;
267 int scaler_count = 0, resampler_count = 0;
269 for (j = 0; j < 2; j++) {
270 /* ask all the sub-filters for their supported media formats */
271 for (i = 0; i < graph->filter_count; i++) {
272 /* Call query_formats on sources first.
273 This is a temporary workaround for amerge,
274 until format renegociation is implemented. */
275 if (!graph->filters[i]->nb_inputs == j)
277 if (graph->filters[i]->filter->query_formats)
278 ret = filter_query_formats(graph->filters[i]);
280 ret = ff_default_query_formats(graph->filters[i]);
286 /* go through and merge as many format lists as possible */
287 for (i = 0; i < graph->filter_count; i++) {
288 AVFilterContext *filter = graph->filters[i];
290 for (j = 0; j < filter->nb_inputs; j++) {
291 AVFilterLink *link = filter->inputs[j];
295 if (!link->in_formats || !link->out_formats)
296 return AVERROR(EINVAL);
298 if (link->type == AVMEDIA_TYPE_VIDEO &&
299 !ff_merge_formats(link->in_formats, link->out_formats)) {
301 /* couldn't merge format lists, auto-insert scale filter */
302 snprintf(filt_args, sizeof(filt_args), "0:0:%s",
303 graph->scale_sws_opts);
304 if (ret = insert_conv_filter(graph, link, "scale", filt_args))
307 else if (link->type == AVMEDIA_TYPE_AUDIO) {
308 if (!link->in_channel_layouts || !link->out_channel_layouts)
309 return AVERROR(EINVAL);
311 /* Merge all three list before checking: that way, in all
312 * three categories, aconvert will use a common format
313 * whenever possible. */
314 formats = ff_merge_formats(link->in_formats, link->out_formats);
315 chlayouts = ff_merge_channel_layouts(link->in_channel_layouts , link->out_channel_layouts);
316 samplerates = ff_merge_samplerates (link->in_samplerates, link->out_samplerates);
318 if (!formats || !chlayouts || !samplerates)
319 if (ret = insert_conv_filter(graph, link, "aresample", NULL))
322 int convert_needed = 0;
327 if (link->in_formats != link->out_formats &&
328 !ff_merge_formats(link->in_formats,
331 if (link->type == AVMEDIA_TYPE_AUDIO) {
332 if (link->in_channel_layouts != link->out_channel_layouts &&
333 !ff_merge_channel_layouts(link->in_channel_layouts,
334 link->out_channel_layouts))
336 if (link->in_samplerates != link->out_samplerates &&
337 !ff_merge_samplerates(link->in_samplerates,
338 link->out_samplerates))
342 if (convert_needed) {
343 AVFilterContext *convert;
345 AVFilterLink *inlink, *outlink;
346 char scale_args[256];
349 /* couldn't merge format lists. auto-insert conversion filter */
350 switch (link->type) {
351 case AVMEDIA_TYPE_VIDEO:
352 if (!(filter = avfilter_get_by_name("scale"))) {
353 av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
354 "not present, cannot convert pixel formats.\n");
355 return AVERROR(EINVAL);
358 snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
360 snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
361 if ((ret = avfilter_graph_create_filter(&convert, filter,
362 inst_name, scale_args, NULL,
366 case AVMEDIA_TYPE_AUDIO:
367 if (!(filter = avfilter_get_by_name("aresample"))) {
368 av_log(log_ctx, AV_LOG_ERROR, "'aresample' filter "
369 "not present, cannot convert audio formats.\n");
370 return AVERROR(EINVAL);
373 snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
375 if ((ret = avfilter_graph_create_filter(&convert, filter,
376 inst_name, NULL, NULL, graph)) < 0)
380 return AVERROR(EINVAL);
383 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
386 filter_query_formats(convert);
387 inlink = convert->inputs[0];
388 outlink = convert->outputs[0];
389 if (!ff_merge_formats( inlink->in_formats, inlink->out_formats) ||
390 !ff_merge_formats(outlink->in_formats, outlink->out_formats))
391 ret |= AVERROR(ENOSYS);
392 if (inlink->type == AVMEDIA_TYPE_AUDIO &&
393 (!ff_merge_samplerates(inlink->in_samplerates,
394 inlink->out_samplerates) ||
395 !ff_merge_channel_layouts(inlink->in_channel_layouts,
396 inlink->out_channel_layouts)))
397 ret |= AVERROR(ENOSYS);
398 if (outlink->type == AVMEDIA_TYPE_AUDIO &&
399 (!ff_merge_samplerates(outlink->in_samplerates,
400 outlink->out_samplerates) ||
401 !ff_merge_channel_layouts(outlink->in_channel_layouts,
402 outlink->out_channel_layouts)))
403 ret |= AVERROR(ENOSYS);
406 av_log(log_ctx, AV_LOG_ERROR,
407 "Impossible to convert between the formats supported by the filter "
408 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
419 static int pick_format(AVFilterLink *link, AVFilterLink *ref)
421 if (!link || !link->in_formats)
424 if (link->type == AVMEDIA_TYPE_VIDEO) {
425 if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
426 int has_alpha= av_pix_fmt_descriptors[ref->format].nb_components % 2 == 0;
427 enum PixelFormat best= PIX_FMT_NONE;
429 for (i=0; i<link->in_formats->format_count; i++) {
430 enum PixelFormat p = link->in_formats->formats[i];
431 best= avcodec_find_best_pix_fmt2(best, p, ref->format, has_alpha, NULL);
433 av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
434 av_get_pix_fmt_name(best), link->in_formats->format_count,
435 av_get_pix_fmt_name(ref->format), has_alpha);
436 link->in_formats->formats[0] = best;
440 link->in_formats->format_count = 1;
441 link->format = link->in_formats->formats[0];
443 if (link->type == AVMEDIA_TYPE_AUDIO) {
444 if (!link->in_samplerates->format_count) {
445 av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
446 " the link between filters %s and %s.\n", link->src->name,
448 return AVERROR(EINVAL);
450 link->in_samplerates->format_count = 1;
451 link->sample_rate = link->in_samplerates->formats[0];
453 if (!link->in_channel_layouts->nb_channel_layouts) {
454 av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
455 "the link between filters %s and %s.\n", link->src->name,
457 return AVERROR(EINVAL);
459 link->in_channel_layouts->nb_channel_layouts = 1;
460 link->channel_layout = link->in_channel_layouts->channel_layouts[0];
463 ff_formats_unref(&link->in_formats);
464 ff_formats_unref(&link->out_formats);
465 ff_formats_unref(&link->in_samplerates);
466 ff_formats_unref(&link->out_samplerates);
467 ff_channel_layouts_unref(&link->in_channel_layouts);
468 ff_channel_layouts_unref(&link->out_channel_layouts);
473 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
475 for (i = 0; i < filter->nb_inputs; i++) { \
476 AVFilterLink *link = filter->inputs[i]; \
479 if (!link->out_ ## list || link->out_ ## list->nb != 1) \
481 fmt = link->out_ ## list->var[0]; \
483 for (j = 0; j < filter->nb_outputs; j++) { \
484 AVFilterLink *out_link = filter->outputs[j]; \
487 if (link->type != out_link->type || \
488 out_link->in_ ## list->nb == 1) \
490 fmts = out_link->in_ ## list; \
492 if (!out_link->in_ ## list->nb) { \
493 add_format(&out_link->in_ ##list, fmt); \
497 for (k = 0; k < out_link->in_ ## list->nb; k++) \
498 if (fmts->var[k] == fmt) { \
499 fmts->var[0] = fmt; \
508 static int reduce_formats_on_filter(AVFilterContext *filter)
510 int i, j, k, ret = 0;
512 REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
513 format_count, ff_add_format);
514 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
515 format_count, ff_add_format);
516 REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
517 channel_layouts, nb_channel_layouts, ff_add_channel_layout);
522 static void reduce_formats(AVFilterGraph *graph)
529 for (i = 0; i < graph->filter_count; i++)
530 reduced |= reduce_formats_on_filter(graph->filters[i]);
534 static void swap_samplerates_on_filter(AVFilterContext *filter)
536 AVFilterLink *link = NULL;
540 for (i = 0; i < filter->nb_inputs; i++) {
541 link = filter->inputs[i];
543 if (link->type == AVMEDIA_TYPE_AUDIO &&
544 link->out_samplerates->format_count == 1)
547 if (i == filter->nb_inputs)
550 sample_rate = link->out_samplerates->formats[0];
552 for (i = 0; i < filter->nb_outputs; i++) {
553 AVFilterLink *outlink = filter->outputs[i];
554 int best_idx, best_diff = INT_MAX;
556 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
557 outlink->in_samplerates->format_count < 2)
560 for (j = 0; j < outlink->in_samplerates->format_count; j++) {
561 int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
563 if (diff < best_diff) {
568 FFSWAP(int, outlink->in_samplerates->formats[0],
569 outlink->in_samplerates->formats[best_idx]);
573 static void swap_samplerates(AVFilterGraph *graph)
577 for (i = 0; i < graph->filter_count; i++)
578 swap_samplerates_on_filter(graph->filters[i]);
581 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
582 #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
583 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
584 #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
585 #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
586 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
587 #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
589 /* allowable substitutions for channel pairs when comparing layouts,
590 * ordered by priority for both values */
591 static const uint64_t ch_subst[][2] = {
592 { CH_FRONT_PAIR, CH_CENTER_PAIR },
593 { CH_FRONT_PAIR, CH_WIDE_PAIR },
594 { CH_FRONT_PAIR, AV_CH_FRONT_CENTER },
595 { CH_CENTER_PAIR, CH_FRONT_PAIR },
596 { CH_CENTER_PAIR, CH_WIDE_PAIR },
597 { CH_CENTER_PAIR, AV_CH_FRONT_CENTER },
598 { CH_WIDE_PAIR, CH_FRONT_PAIR },
599 { CH_WIDE_PAIR, CH_CENTER_PAIR },
600 { CH_WIDE_PAIR, AV_CH_FRONT_CENTER },
601 { AV_CH_FRONT_CENTER, CH_FRONT_PAIR },
602 { AV_CH_FRONT_CENTER, CH_CENTER_PAIR },
603 { AV_CH_FRONT_CENTER, CH_WIDE_PAIR },
604 { CH_SIDE_PAIR, CH_DIRECT_PAIR },
605 { CH_SIDE_PAIR, CH_BACK_PAIR },
606 { CH_SIDE_PAIR, AV_CH_BACK_CENTER },
607 { CH_BACK_PAIR, CH_DIRECT_PAIR },
608 { CH_BACK_PAIR, CH_SIDE_PAIR },
609 { CH_BACK_PAIR, AV_CH_BACK_CENTER },
610 { AV_CH_BACK_CENTER, CH_BACK_PAIR },
611 { AV_CH_BACK_CENTER, CH_DIRECT_PAIR },
612 { AV_CH_BACK_CENTER, CH_SIDE_PAIR },
615 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
617 AVFilterLink *link = NULL;
620 for (i = 0; i < filter->nb_inputs; i++) {
621 link = filter->inputs[i];
623 if (link->type == AVMEDIA_TYPE_AUDIO &&
624 link->out_channel_layouts->nb_channel_layouts == 1)
627 if (i == filter->nb_inputs)
630 for (i = 0; i < filter->nb_outputs; i++) {
631 AVFilterLink *outlink = filter->outputs[i];
632 int best_idx, best_score = INT_MIN, best_count_diff = INT_MAX;
634 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
635 outlink->in_channel_layouts->nb_channel_layouts < 2)
638 for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
639 uint64_t in_chlayout = link->out_channel_layouts->channel_layouts[0];
640 uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
641 int in_channels = av_get_channel_layout_nb_channels(in_chlayout);
642 int out_channels = av_get_channel_layout_nb_channels(out_chlayout);
643 int count_diff = out_channels - in_channels;
644 int matched_channels, extra_channels;
647 /* channel substitution */
648 for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
649 uint64_t cmp0 = ch_subst[k][0];
650 uint64_t cmp1 = ch_subst[k][1];
651 if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
652 (out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
653 in_chlayout &= ~cmp0;
654 out_chlayout &= ~cmp1;
655 /* add score for channel match, minus a deduction for
656 having to do the substitution */
657 score += 10 * av_get_channel_layout_nb_channels(cmp1) - 2;
661 /* no penalty for LFE channel mismatch */
662 if ( (in_chlayout & AV_CH_LOW_FREQUENCY) &&
663 (out_chlayout & AV_CH_LOW_FREQUENCY))
665 in_chlayout &= ~AV_CH_LOW_FREQUENCY;
666 out_chlayout &= ~AV_CH_LOW_FREQUENCY;
668 matched_channels = av_get_channel_layout_nb_channels(in_chlayout &
670 extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
672 score += 10 * matched_channels - 5 * extra_channels;
674 if (score > best_score ||
675 (count_diff < best_count_diff && score == best_score)) {
678 best_count_diff = count_diff;
681 FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
682 outlink->in_channel_layouts->channel_layouts[best_idx]);
687 static void swap_channel_layouts(AVFilterGraph *graph)
691 for (i = 0; i < graph->filter_count; i++)
692 swap_channel_layouts_on_filter(graph->filters[i]);
695 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
697 AVFilterLink *link = NULL;
701 for (i = 0; i < filter->nb_inputs; i++) {
702 link = filter->inputs[i];
704 if (link->type == AVMEDIA_TYPE_AUDIO &&
705 link->out_formats->format_count == 1)
708 if (i == filter->nb_inputs)
711 format = link->out_formats->formats[0];
712 bps = av_get_bytes_per_sample(format);
714 for (i = 0; i < filter->nb_outputs; i++) {
715 AVFilterLink *outlink = filter->outputs[i];
716 int best_idx = -1, best_score = INT_MIN;
718 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
719 outlink->in_formats->format_count < 2)
722 for (j = 0; j < outlink->in_formats->format_count; j++) {
723 int out_format = outlink->in_formats->formats[j];
724 int out_bps = av_get_bytes_per_sample(out_format);
727 if (av_get_packed_sample_fmt(out_format) == format ||
728 av_get_planar_sample_fmt(out_format) == format) {
733 /* for s32 and float prefer double to prevent loss of information */
734 if (bps == 4 && out_bps == 8) {
739 /* prefer closest higher or equal bps */
740 score = -abs(out_bps - bps);
744 if (score > best_score) {
749 av_assert0(best_idx >= 0);
750 FFSWAP(int, outlink->in_formats->formats[0],
751 outlink->in_formats->formats[best_idx]);
755 static void swap_sample_fmts(AVFilterGraph *graph)
759 for (i = 0; i < graph->filter_count; i++)
760 swap_sample_fmts_on_filter(graph->filters[i]);
764 static int pick_formats(AVFilterGraph *graph)
771 for (i = 0; i < graph->filter_count; i++) {
772 AVFilterContext *filter = graph->filters[i];
773 if (filter->nb_inputs){
774 for (j = 0; j < filter->nb_inputs; j++){
775 if(filter->inputs[j]->in_formats && filter->inputs[j]->in_formats->format_count == 1) {
776 pick_format(filter->inputs[j], NULL);
781 if (filter->nb_outputs){
782 for (j = 0; j < filter->nb_outputs; j++){
783 if(filter->outputs[j]->in_formats && filter->outputs[j]->in_formats->format_count == 1) {
784 pick_format(filter->outputs[j], NULL);
789 if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
790 for (j = 0; j < filter->nb_outputs; j++) {
791 if(filter->outputs[j]->format<0) {
792 pick_format(filter->outputs[j], filter->inputs[0]);
800 for (i = 0; i < graph->filter_count; i++) {
801 AVFilterContext *filter = graph->filters[i];
803 for (j = 0; j < filter->nb_inputs; j++)
804 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
806 for (j = 0; j < filter->nb_outputs; j++)
807 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
814 * Configure the formats of all the links in the graph.
816 static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
820 /* find supported formats from sub-filters, and merge along links */
821 if ((ret = query_formats(graph, log_ctx)) < 0)
824 /* Once everything is merged, it's possible that we'll still have
825 * multiple valid media format choices. We try to minimize the amount
826 * of format conversion inside filters */
827 reduce_formats(graph);
829 /* for audio filters, ensure the best format, sample rate and channel layout
831 swap_sample_fmts(graph);
832 swap_samplerates(graph);
833 swap_channel_layouts(graph);
835 if ((ret = pick_formats(graph)) < 0)
841 static int ff_avfilter_graph_config_pointers(AVFilterGraph *graph,
845 int sink_links_count = 0, n = 0;
847 AVFilterLink **sinks;
849 for (i = 0; i < graph->filter_count; i++) {
850 f = graph->filters[i];
851 for (j = 0; j < f->nb_inputs; j++) {
852 f->inputs[j]->graph = graph;
853 f->inputs[j]->age_index = -1;
855 for (j = 0; j < f->nb_outputs; j++) {
856 f->outputs[j]->graph = graph;
857 f->outputs[j]->age_index= -1;
859 if (!f->nb_outputs) {
860 if (f->nb_inputs > INT_MAX - sink_links_count)
861 return AVERROR(EINVAL);
862 sink_links_count += f->nb_inputs;
865 sinks = av_calloc(sink_links_count, sizeof(*sinks));
867 return AVERROR(ENOMEM);
868 for (i = 0; i < graph->filter_count; i++) {
869 f = graph->filters[i];
870 if (!f->nb_outputs) {
871 for (j = 0; j < f->nb_inputs; j++) {
872 sinks[n] = f->inputs[j];
873 f->inputs[j]->age_index = n++;
877 av_assert0(n == sink_links_count);
878 graph->sink_links = sinks;
879 graph->sink_links_count = sink_links_count;
883 static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
889 for (i = 0; i < graph->filter_count; i++) {
890 f = graph->filters[i];
892 for (j = 0; j < f->nb_inputs; j++) {
893 AVFilterLink *link = f->inputs[j];
894 AVFilterContext *fifo_ctx;
898 if (!link->dstpad->needs_fifo)
901 fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
902 avfilter_get_by_name("fifo") :
903 avfilter_get_by_name("afifo");
905 snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
907 ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
912 ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
921 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
925 if ((ret = graph_check_validity(graphctx, log_ctx)))
927 if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
929 if ((ret = graph_config_formats(graphctx, log_ctx)))
931 if ((ret = graph_config_links(graphctx, log_ctx)))
933 if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
939 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
941 int i, r = AVERROR(ENOSYS);
946 if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
947 r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
948 if(r != AVERROR(ENOSYS))
955 for (i = 0; i < graph->filter_count; i++) {
956 AVFilterContext *filter = graph->filters[i];
957 if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
958 r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
959 if(r != AVERROR(ENOSYS)) {
960 if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
969 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
976 for (i = 0; i < graph->filter_count; i++) {
977 AVFilterContext *filter = graph->filters[i];
978 if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
979 AVFilterCommand **que = &filter->command_queue, *next;
980 while(*que && (*que)->time <= ts)
983 *que= av_mallocz(sizeof(AVFilterCommand));
984 (*que)->command = av_strdup(command);
985 (*que)->arg = av_strdup(arg);
987 (*que)->flags = flags;
989 if(flags & AVFILTER_CMD_FLAG_ONE)
997 static void heap_bubble_up(AVFilterGraph *graph,
998 AVFilterLink *link, int index)
1000 AVFilterLink **links = graph->sink_links;
1003 int parent = (index - 1) >> 1;
1004 if (links[parent]->current_pts >= link->current_pts)
1006 links[index] = links[parent];
1007 links[index]->age_index = index;
1010 links[index] = link;
1011 link->age_index = index;
1014 static void heap_bubble_down(AVFilterGraph *graph,
1015 AVFilterLink *link, int index)
1017 AVFilterLink **links = graph->sink_links;
1020 int child = 2 * index + 1;
1021 if (child >= graph->sink_links_count)
1023 if (child + 1 < graph->sink_links_count &&
1024 links[child + 1]->current_pts < links[child]->current_pts)
1026 if (link->current_pts < links[child]->current_pts)
1028 links[index] = links[child];
1029 links[index]->age_index = index;
1032 links[index] = link;
1033 link->age_index = index;
1036 void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
1038 heap_bubble_up (graph, link, link->age_index);
1039 heap_bubble_down(graph, link, link->age_index);
1043 int avfilter_graph_request_oldest(AVFilterGraph *graph)
1045 while (graph->sink_links_count) {
1046 AVFilterLink *oldest = graph->sink_links[0];
1047 int r = ff_request_frame(oldest);
1048 if (r != AVERROR_EOF)
1050 av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
1051 oldest->dst ? oldest->dst->name : "unknown",
1052 oldest->dstpad ? oldest->dstpad->name : "unknown");
1053 /* EOF: remove the link from the heap */
1054 if (oldest->age_index < --graph->sink_links_count)
1055 heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
1057 oldest->age_index = -1;