3 * Copyright (c) 2008 Vitor Sessak
4 * Copyright (c) 2007 Bobby Bingham
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "avfiltergraph.h"
31 #include "libavutil/audioconvert.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/common.h"
34 #include "libavutil/log.h"
36 static const AVClass filtergraph_class = {
37 .class_name = "AVFilterGraph",
38 .item_name = av_default_item_name,
39 .version = LIBAVUTIL_VERSION_INT,
42 AVFilterGraph *avfilter_graph_alloc(void)
44 AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
47 ret->av_class = &filtergraph_class;
51 void avfilter_graph_free(AVFilterGraph **graph)
55 for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
56 avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
57 av_freep(&(*graph)->scale_sws_opts);
58 av_freep(&(*graph)->filters);
62 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
64 AVFilterContext **filters = av_realloc(graph->filters,
65 sizeof(AVFilterContext*) * (graph->filter_count+1));
67 return AVERROR(ENOMEM);
69 graph->filters = filters;
70 graph->filters[graph->filter_count++] = filter;
75 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
76 const char *name, const char *args, void *opaque,
77 AVFilterGraph *graph_ctx)
81 if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
83 if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
85 if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
91 avfilter_free(*filt_ctx);
97 * Check for the validity of graph.
99 * A graph is considered valid if all its input and output pads are
102 * @return 0 in case of success, a negative value otherwise
104 static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
106 AVFilterContext *filt;
109 for (i = 0; i < graph->filter_count; i++) {
110 filt = graph->filters[i];
112 for (j = 0; j < filt->nb_inputs; j++) {
113 if (!filt->inputs[j] || !filt->inputs[j]->src) {
114 av_log(log_ctx, AV_LOG_ERROR,
115 "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
116 filt->input_pads[j].name, filt->name, filt->filter->name);
117 return AVERROR(EINVAL);
121 for (j = 0; j < filt->nb_outputs; j++) {
122 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
123 av_log(log_ctx, AV_LOG_ERROR,
124 "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
125 filt->output_pads[j].name, filt->name, filt->filter->name);
126 return AVERROR(EINVAL);
135 * Configure all the links of graphctx.
137 * @return 0 in case of success, a negative value otherwise
139 static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
141 AVFilterContext *filt;
144 for (i=0; i < graph->filter_count; i++) {
145 filt = graph->filters[i];
147 if (!filt->nb_outputs) {
148 if ((ret = avfilter_config_links(filt)))
156 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
160 for (i = 0; i < graph->filter_count; i++)
161 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
162 return graph->filters[i];
167 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
170 int scaler_count = 0, resampler_count = 0;
172 /* ask all the sub-filters for their supported media formats */
173 for (i = 0; i < graph->filter_count; i++) {
174 if (graph->filters[i]->filter->query_formats)
175 graph->filters[i]->filter->query_formats(graph->filters[i]);
177 ff_default_query_formats(graph->filters[i]);
180 /* go through and merge as many format lists as possible */
181 for (i = 0; i < graph->filter_count; i++) {
182 AVFilterContext *filter = graph->filters[i];
184 for (j = 0; j < filter->nb_inputs; j++) {
185 AVFilterLink *link = filter->inputs[j];
186 int convert_needed = 0;
191 if (link->in_formats != link->out_formats &&
192 !ff_merge_formats(link->in_formats,
195 if (link->type == AVMEDIA_TYPE_AUDIO) {
196 if (link->in_channel_layouts != link->out_channel_layouts &&
197 !ff_merge_channel_layouts(link->in_channel_layouts,
198 link->out_channel_layouts))
200 if (link->in_samplerates != link->out_samplerates &&
201 !ff_merge_samplerates(link->in_samplerates,
202 link->out_samplerates))
206 if (convert_needed) {
207 AVFilterContext *convert;
209 AVFilterLink *inlink, *outlink;
210 char scale_args[256];
213 /* couldn't merge format lists. auto-insert conversion filter */
214 switch (link->type) {
215 case AVMEDIA_TYPE_VIDEO:
216 if (!(filter = avfilter_get_by_name("scale"))) {
217 av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
218 "not present, cannot convert pixel formats.\n");
219 return AVERROR(EINVAL);
222 snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
224 snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
225 if ((ret = avfilter_graph_create_filter(&convert, filter,
226 inst_name, scale_args, NULL,
230 case AVMEDIA_TYPE_AUDIO:
231 if (!(filter = avfilter_get_by_name("resample"))) {
232 av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
233 "not present, cannot convert audio formats.\n");
234 return AVERROR(EINVAL);
237 snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
239 if ((ret = avfilter_graph_create_filter(&convert, filter,
240 inst_name, NULL, NULL, graph)) < 0)
244 return AVERROR(EINVAL);
247 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
250 convert->filter->query_formats(convert);
251 inlink = convert->inputs[0];
252 outlink = convert->outputs[0];
253 if (!ff_merge_formats( inlink->in_formats, inlink->out_formats) ||
254 !ff_merge_formats(outlink->in_formats, outlink->out_formats))
255 ret |= AVERROR(ENOSYS);
256 if (inlink->type == AVMEDIA_TYPE_AUDIO &&
257 (!ff_merge_samplerates(inlink->in_samplerates,
258 inlink->out_samplerates) ||
259 !ff_merge_channel_layouts(inlink->in_channel_layouts,
260 inlink->out_channel_layouts)))
261 ret |= AVERROR(ENOSYS);
262 if (outlink->type == AVMEDIA_TYPE_AUDIO &&
263 (!ff_merge_samplerates(outlink->in_samplerates,
264 outlink->out_samplerates) ||
265 !ff_merge_channel_layouts(outlink->in_channel_layouts,
266 outlink->out_channel_layouts)))
267 ret |= AVERROR(ENOSYS);
270 av_log(log_ctx, AV_LOG_ERROR,
271 "Impossible to convert between the formats supported by the filter "
272 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
282 static int pick_format(AVFilterLink *link)
284 if (!link || !link->in_formats)
287 link->in_formats->format_count = 1;
288 link->format = link->in_formats->formats[0];
290 if (link->type == AVMEDIA_TYPE_AUDIO) {
291 if (!link->in_samplerates->format_count) {
292 av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
293 " the link between filters %s and %s.\n", link->src->name,
295 return AVERROR(EINVAL);
297 link->in_samplerates->format_count = 1;
298 link->sample_rate = link->in_samplerates->formats[0];
300 if (!link->in_channel_layouts->nb_channel_layouts) {
301 av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
302 "the link between filters %s and %s.\n", link->src->name,
304 return AVERROR(EINVAL);
306 link->in_channel_layouts->nb_channel_layouts = 1;
307 link->channel_layout = link->in_channel_layouts->channel_layouts[0];
310 ff_formats_unref(&link->in_formats);
311 ff_formats_unref(&link->out_formats);
312 ff_formats_unref(&link->in_samplerates);
313 ff_formats_unref(&link->out_samplerates);
314 ff_channel_layouts_unref(&link->in_channel_layouts);
315 ff_channel_layouts_unref(&link->out_channel_layouts);
320 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
322 for (i = 0; i < filter->nb_inputs; i++) { \
323 AVFilterLink *link = filter->inputs[i]; \
326 if (!link->out_ ## list || link->out_ ## list->nb != 1) \
328 fmt = link->out_ ## list->var[0]; \
330 for (j = 0; j < filter->nb_outputs; j++) { \
331 AVFilterLink *out_link = filter->outputs[j]; \
334 if (link->type != out_link->type || \
335 out_link->in_ ## list->nb == 1) \
337 fmts = out_link->in_ ## list; \
339 if (!out_link->in_ ## list->nb) { \
340 add_format(&out_link->in_ ##list, fmt); \
344 for (k = 0; k < out_link->in_ ## list->nb; k++) \
345 if (fmts->var[k] == fmt) { \
346 fmts->var[0] = fmt; \
355 static int reduce_formats_on_filter(AVFilterContext *filter)
357 int i, j, k, ret = 0;
359 REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
360 format_count, ff_add_format);
361 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
362 format_count, ff_add_format);
363 REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
364 channel_layouts, nb_channel_layouts, ff_add_channel_layout);
369 static void reduce_formats(AVFilterGraph *graph)
376 for (i = 0; i < graph->filter_count; i++)
377 reduced |= reduce_formats_on_filter(graph->filters[i]);
381 static void swap_samplerates_on_filter(AVFilterContext *filter)
383 AVFilterLink *link = NULL;
387 for (i = 0; i < filter->nb_inputs; i++) {
388 link = filter->inputs[i];
390 if (link->type == AVMEDIA_TYPE_AUDIO &&
391 link->out_samplerates->format_count == 1)
394 if (i == filter->nb_inputs)
397 sample_rate = link->out_samplerates->formats[0];
399 for (i = 0; i < filter->nb_outputs; i++) {
400 AVFilterLink *outlink = filter->outputs[i];
401 int best_idx, best_diff = INT_MAX;
403 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
404 outlink->in_samplerates->format_count < 2)
407 for (j = 0; j < outlink->in_samplerates->format_count; j++) {
408 int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
410 if (diff < best_diff) {
415 FFSWAP(int, outlink->in_samplerates->formats[0],
416 outlink->in_samplerates->formats[best_idx]);
420 static void swap_samplerates(AVFilterGraph *graph)
424 for (i = 0; i < graph->filter_count; i++)
425 swap_samplerates_on_filter(graph->filters[i]);
428 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
429 #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
430 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
431 #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
432 #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
433 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
434 #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
436 /* allowable substitutions for channel pairs when comparing layouts,
437 * ordered by priority for both values */
438 static const uint64_t ch_subst[][2] = {
439 { CH_FRONT_PAIR, CH_CENTER_PAIR },
440 { CH_FRONT_PAIR, CH_WIDE_PAIR },
441 { CH_FRONT_PAIR, AV_CH_FRONT_CENTER },
442 { CH_CENTER_PAIR, CH_FRONT_PAIR },
443 { CH_CENTER_PAIR, CH_WIDE_PAIR },
444 { CH_CENTER_PAIR, AV_CH_FRONT_CENTER },
445 { CH_WIDE_PAIR, CH_FRONT_PAIR },
446 { CH_WIDE_PAIR, CH_CENTER_PAIR },
447 { CH_WIDE_PAIR, AV_CH_FRONT_CENTER },
448 { AV_CH_FRONT_CENTER, CH_FRONT_PAIR },
449 { AV_CH_FRONT_CENTER, CH_CENTER_PAIR },
450 { AV_CH_FRONT_CENTER, CH_WIDE_PAIR },
451 { CH_SIDE_PAIR, CH_DIRECT_PAIR },
452 { CH_SIDE_PAIR, CH_BACK_PAIR },
453 { CH_SIDE_PAIR, AV_CH_BACK_CENTER },
454 { CH_BACK_PAIR, CH_DIRECT_PAIR },
455 { CH_BACK_PAIR, CH_SIDE_PAIR },
456 { CH_BACK_PAIR, AV_CH_BACK_CENTER },
457 { AV_CH_BACK_CENTER, CH_BACK_PAIR },
458 { AV_CH_BACK_CENTER, CH_DIRECT_PAIR },
459 { AV_CH_BACK_CENTER, CH_SIDE_PAIR },
462 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
464 AVFilterLink *link = NULL;
467 for (i = 0; i < filter->nb_inputs; i++) {
468 link = filter->inputs[i];
470 if (link->type == AVMEDIA_TYPE_AUDIO &&
471 link->out_channel_layouts->nb_channel_layouts == 1)
474 if (i == filter->nb_inputs)
477 for (i = 0; i < filter->nb_outputs; i++) {
478 AVFilterLink *outlink = filter->outputs[i];
479 int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
481 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
482 outlink->in_channel_layouts->nb_channel_layouts < 2)
485 for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
486 uint64_t in_chlayout = link->out_channel_layouts->channel_layouts[0];
487 uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
488 int in_channels = av_get_channel_layout_nb_channels(in_chlayout);
489 int out_channels = av_get_channel_layout_nb_channels(out_chlayout);
490 int count_diff = out_channels - in_channels;
491 int matched_channels, extra_channels;
494 /* channel substitution */
495 for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
496 uint64_t cmp0 = ch_subst[k][0];
497 uint64_t cmp1 = ch_subst[k][1];
498 if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
499 (out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
500 in_chlayout &= ~cmp0;
501 out_chlayout &= ~cmp1;
502 /* add score for channel match, minus a deduction for
503 having to do the substitution */
504 score += 10 * av_get_channel_layout_nb_channels(cmp1) - 2;
508 /* no penalty for LFE channel mismatch */
509 if ( (in_chlayout & AV_CH_LOW_FREQUENCY) &&
510 (out_chlayout & AV_CH_LOW_FREQUENCY))
512 in_chlayout &= ~AV_CH_LOW_FREQUENCY;
513 out_chlayout &= ~AV_CH_LOW_FREQUENCY;
515 matched_channels = av_get_channel_layout_nb_channels(in_chlayout &
517 extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
519 score += 10 * matched_channels - 5 * extra_channels;
521 if (score > best_score ||
522 (count_diff < best_count_diff && score == best_score)) {
525 best_count_diff = count_diff;
528 av_assert0(best_idx >= 0);
529 FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
530 outlink->in_channel_layouts->channel_layouts[best_idx]);
535 static void swap_channel_layouts(AVFilterGraph *graph)
539 for (i = 0; i < graph->filter_count; i++)
540 swap_channel_layouts_on_filter(graph->filters[i]);
543 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
545 AVFilterLink *link = NULL;
549 for (i = 0; i < filter->nb_inputs; i++) {
550 link = filter->inputs[i];
552 if (link->type == AVMEDIA_TYPE_AUDIO &&
553 link->out_formats->format_count == 1)
556 if (i == filter->nb_inputs)
559 format = link->out_formats->formats[0];
560 bps = av_get_bytes_per_sample(format);
562 for (i = 0; i < filter->nb_outputs; i++) {
563 AVFilterLink *outlink = filter->outputs[i];
564 int best_idx = -1, best_score = INT_MIN;
566 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
567 outlink->in_formats->format_count < 2)
570 for (j = 0; j < outlink->in_formats->format_count; j++) {
571 int out_format = outlink->in_formats->formats[j];
572 int out_bps = av_get_bytes_per_sample(out_format);
575 if (av_get_packed_sample_fmt(out_format) == format ||
576 av_get_planar_sample_fmt(out_format) == format) {
581 /* for s32 and float prefer double to prevent loss of information */
582 if (bps == 4 && out_bps == 8) {
587 /* prefer closest higher or equal bps */
588 score = -abs(out_bps - bps);
592 if (score > best_score) {
597 av_assert0(best_idx >= 0);
598 FFSWAP(int, outlink->in_formats->formats[0],
599 outlink->in_formats->formats[best_idx]);
603 static void swap_sample_fmts(AVFilterGraph *graph)
607 for (i = 0; i < graph->filter_count; i++)
608 swap_sample_fmts_on_filter(graph->filters[i]);
612 static int pick_formats(AVFilterGraph *graph)
616 for (i = 0; i < graph->filter_count; i++) {
617 AVFilterContext *filter = graph->filters[i];
619 for (j = 0; j < filter->nb_inputs; j++)
620 if ((ret = pick_format(filter->inputs[j])) < 0)
622 for (j = 0; j < filter->nb_outputs; j++)
623 if ((ret = pick_format(filter->outputs[j])) < 0)
630 * Configure the formats of all the links in the graph.
632 static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
636 /* find supported formats from sub-filters, and merge along links */
637 if ((ret = query_formats(graph, log_ctx)) < 0)
640 /* Once everything is merged, it's possible that we'll still have
641 * multiple valid media format choices. We try to minimize the amount
642 * of format conversion inside filters */
643 reduce_formats(graph);
645 /* for audio filters, ensure the best format, sample rate and channel layout
647 swap_sample_fmts(graph);
648 swap_samplerates(graph);
649 swap_channel_layouts(graph);
651 if ((ret = pick_formats(graph)) < 0)
657 static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
663 for (i = 0; i < graph->filter_count; i++) {
664 f = graph->filters[i];
666 for (j = 0; j < f->nb_inputs; j++) {
667 AVFilterLink *link = f->inputs[j];
668 AVFilterContext *fifo_ctx;
672 if (!link->dstpad->needs_fifo)
675 fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
676 avfilter_get_by_name("fifo") :
677 avfilter_get_by_name("afifo");
679 snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
681 ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
686 ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
695 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
699 if ((ret = graph_check_validity(graphctx, log_ctx)))
701 if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
703 if ((ret = graph_config_formats(graphctx, log_ctx)))
705 if ((ret = graph_config_links(graphctx, log_ctx)))