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
25 #include "libavutil/avassert.h"
26 #include "libavutil/avstring.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/common.h"
29 #include "libavutil/log.h"
34 static const AVClass filtergraph_class = {
35 .class_name = "AVFilterGraph",
36 .item_name = av_default_item_name,
37 .version = LIBAVUTIL_VERSION_INT,
40 AVFilterGraph *avfilter_graph_alloc(void)
42 AVFilterGraph *ret = av_mallocz(sizeof(*ret));
45 ret->av_class = &filtergraph_class;
49 void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
52 for (i = 0; i < graph->nb_filters; i++) {
53 if (graph->filters[i] == filter) {
54 FFSWAP(AVFilterContext*, graph->filters[i],
55 graph->filters[graph->nb_filters - 1]);
62 void avfilter_graph_free(AVFilterGraph **graph)
67 while ((*graph)->nb_filters)
68 avfilter_free((*graph)->filters[0]);
70 av_freep(&(*graph)->scale_sws_opts);
71 av_freep(&(*graph)->resample_lavr_opts);
72 av_freep(&(*graph)->filters);
76 #if FF_API_AVFILTER_OPEN
77 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
79 AVFilterContext **filters = av_realloc(graph->filters,
80 sizeof(*filters) * (graph->nb_filters + 1));
82 return AVERROR(ENOMEM);
84 graph->filters = filters;
85 graph->filters[graph->nb_filters++] = filter;
88 graph->filter_count = graph->nb_filters;
91 filter->graph = graph;
97 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
98 const char *name, const char *args, void *opaque,
99 AVFilterGraph *graph_ctx)
103 *filt_ctx = avfilter_graph_alloc_filter(graph_ctx, filt, name);
105 return AVERROR(ENOMEM);
107 ret = avfilter_init_str(*filt_ctx, args);
115 avfilter_free(*filt_ctx);
120 AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
121 const AVFilter *filter,
124 AVFilterContext **filters, *s;
126 s = ff_filter_alloc(filter, name);
130 filters = av_realloc(graph->filters, sizeof(*filters) * (graph->nb_filters + 1));
136 graph->filters = filters;
137 graph->filters[graph->nb_filters++] = s;
140 graph->filter_count = graph->nb_filters;
149 * Check for the validity of graph.
151 * A graph is considered valid if all its input and output pads are
154 * @return 0 in case of success, a negative value otherwise
156 static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
158 AVFilterContext *filt;
161 for (i = 0; i < graph->nb_filters; i++) {
162 filt = graph->filters[i];
164 for (j = 0; j < filt->nb_inputs; j++) {
165 if (!filt->inputs[j] || !filt->inputs[j]->src) {
166 av_log(log_ctx, AV_LOG_ERROR,
167 "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
168 filt->input_pads[j].name, filt->name, filt->filter->name);
169 return AVERROR(EINVAL);
173 for (j = 0; j < filt->nb_outputs; j++) {
174 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
175 av_log(log_ctx, AV_LOG_ERROR,
176 "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
177 filt->output_pads[j].name, filt->name, filt->filter->name);
178 return AVERROR(EINVAL);
187 * Configure all the links of graphctx.
189 * @return 0 in case of success, a negative value otherwise
191 static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
193 AVFilterContext *filt;
196 for (i = 0; i < graph->nb_filters; i++) {
197 filt = graph->filters[i];
199 if (!filt->nb_outputs) {
200 if ((ret = avfilter_config_links(filt)))
208 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
212 for (i = 0; i < graph->nb_filters; i++)
213 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
214 return graph->filters[i];
219 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
222 int scaler_count = 0, resampler_count = 0;
224 /* ask all the sub-filters for their supported media formats */
225 for (i = 0; i < graph->nb_filters; i++) {
226 if (graph->filters[i]->filter->query_formats)
227 graph->filters[i]->filter->query_formats(graph->filters[i]);
229 ff_default_query_formats(graph->filters[i]);
232 /* go through and merge as many format lists as possible */
233 for (i = 0; i < graph->nb_filters; i++) {
234 AVFilterContext *filter = graph->filters[i];
236 for (j = 0; j < filter->nb_inputs; j++) {
237 AVFilterLink *link = filter->inputs[j];
238 int convert_needed = 0;
243 if (link->in_formats != link->out_formats &&
244 !ff_merge_formats(link->in_formats,
247 if (link->type == AVMEDIA_TYPE_AUDIO) {
248 if (link->in_channel_layouts != link->out_channel_layouts &&
249 !ff_merge_channel_layouts(link->in_channel_layouts,
250 link->out_channel_layouts))
252 if (link->in_samplerates != link->out_samplerates &&
253 !ff_merge_samplerates(link->in_samplerates,
254 link->out_samplerates))
258 if (convert_needed) {
259 AVFilterContext *convert;
261 AVFilterLink *inlink, *outlink;
262 char scale_args[256];
265 /* couldn't merge format lists. auto-insert conversion filter */
266 switch (link->type) {
267 case AVMEDIA_TYPE_VIDEO:
268 if (!(filter = avfilter_get_by_name("scale"))) {
269 av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
270 "not present, cannot convert pixel formats.\n");
271 return AVERROR(EINVAL);
274 snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
276 av_strlcpy(scale_args, "0:0", sizeof(scale_args));
277 if (graph->scale_sws_opts) {
278 av_strlcat(scale_args, ":", sizeof(scale_args));
279 av_strlcat(scale_args, graph->scale_sws_opts, sizeof(scale_args));
281 if ((ret = avfilter_graph_create_filter(&convert, filter,
282 inst_name, scale_args, NULL,
286 case AVMEDIA_TYPE_AUDIO:
287 if (!(filter = avfilter_get_by_name("resample"))) {
288 av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
289 "not present, cannot convert audio formats.\n");
290 return AVERROR(EINVAL);
293 snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
295 scale_args[0] = '\0';
296 if (graph->resample_lavr_opts)
297 snprintf(scale_args, sizeof(scale_args), "%s",
298 graph->resample_lavr_opts);
299 if ((ret = avfilter_graph_create_filter(&convert, filter,
300 inst_name, scale_args,
305 return AVERROR(EINVAL);
308 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
311 convert->filter->query_formats(convert);
312 inlink = convert->inputs[0];
313 outlink = convert->outputs[0];
314 if (!ff_merge_formats( inlink->in_formats, inlink->out_formats) ||
315 !ff_merge_formats(outlink->in_formats, outlink->out_formats))
316 ret |= AVERROR(ENOSYS);
317 if (inlink->type == AVMEDIA_TYPE_AUDIO &&
318 (!ff_merge_samplerates(inlink->in_samplerates,
319 inlink->out_samplerates) ||
320 !ff_merge_channel_layouts(inlink->in_channel_layouts,
321 inlink->out_channel_layouts)))
322 ret |= AVERROR(ENOSYS);
323 if (outlink->type == AVMEDIA_TYPE_AUDIO &&
324 (!ff_merge_samplerates(outlink->in_samplerates,
325 outlink->out_samplerates) ||
326 !ff_merge_channel_layouts(outlink->in_channel_layouts,
327 outlink->out_channel_layouts)))
328 ret |= AVERROR(ENOSYS);
331 av_log(log_ctx, AV_LOG_ERROR,
332 "Impossible to convert between the formats supported by the filter "
333 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
343 static int pick_format(AVFilterLink *link)
345 if (!link || !link->in_formats)
348 link->in_formats->format_count = 1;
349 link->format = link->in_formats->formats[0];
351 if (link->type == AVMEDIA_TYPE_AUDIO) {
352 if (!link->in_samplerates->format_count) {
353 av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
354 " the link between filters %s and %s.\n", link->src->name,
356 return AVERROR(EINVAL);
358 link->in_samplerates->format_count = 1;
359 link->sample_rate = link->in_samplerates->formats[0];
361 if (!link->in_channel_layouts->nb_channel_layouts) {
362 av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
363 "the link between filters %s and %s.\n", link->src->name,
365 return AVERROR(EINVAL);
367 link->in_channel_layouts->nb_channel_layouts = 1;
368 link->channel_layout = link->in_channel_layouts->channel_layouts[0];
371 ff_formats_unref(&link->in_formats);
372 ff_formats_unref(&link->out_formats);
373 ff_formats_unref(&link->in_samplerates);
374 ff_formats_unref(&link->out_samplerates);
375 ff_channel_layouts_unref(&link->in_channel_layouts);
376 ff_channel_layouts_unref(&link->out_channel_layouts);
381 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
383 for (i = 0; i < filter->nb_inputs; i++) { \
384 AVFilterLink *link = filter->inputs[i]; \
387 if (!link->out_ ## list || link->out_ ## list->nb != 1) \
389 fmt = link->out_ ## list->var[0]; \
391 for (j = 0; j < filter->nb_outputs; j++) { \
392 AVFilterLink *out_link = filter->outputs[j]; \
395 if (link->type != out_link->type || \
396 out_link->in_ ## list->nb == 1) \
398 fmts = out_link->in_ ## list; \
400 if (!out_link->in_ ## list->nb) { \
401 add_format(&out_link->in_ ##list, fmt); \
405 for (k = 0; k < out_link->in_ ## list->nb; k++) \
406 if (fmts->var[k] == fmt) { \
407 fmts->var[0] = fmt; \
416 static int reduce_formats_on_filter(AVFilterContext *filter)
418 int i, j, k, ret = 0;
420 REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
421 format_count, ff_add_format);
422 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
423 format_count, ff_add_format);
424 REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
425 channel_layouts, nb_channel_layouts, ff_add_channel_layout);
430 static void reduce_formats(AVFilterGraph *graph)
437 for (i = 0; i < graph->nb_filters; i++)
438 reduced |= reduce_formats_on_filter(graph->filters[i]);
442 static void swap_samplerates_on_filter(AVFilterContext *filter)
444 AVFilterLink *link = NULL;
448 for (i = 0; i < filter->nb_inputs; i++) {
449 link = filter->inputs[i];
451 if (link->type == AVMEDIA_TYPE_AUDIO &&
452 link->out_samplerates->format_count == 1)
455 if (i == filter->nb_inputs)
458 sample_rate = link->out_samplerates->formats[0];
460 for (i = 0; i < filter->nb_outputs; i++) {
461 AVFilterLink *outlink = filter->outputs[i];
462 int best_idx, best_diff = INT_MAX;
464 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
465 outlink->in_samplerates->format_count < 2)
468 for (j = 0; j < outlink->in_samplerates->format_count; j++) {
469 int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
471 if (diff < best_diff) {
476 FFSWAP(int, outlink->in_samplerates->formats[0],
477 outlink->in_samplerates->formats[best_idx]);
481 static void swap_samplerates(AVFilterGraph *graph)
485 for (i = 0; i < graph->nb_filters; i++)
486 swap_samplerates_on_filter(graph->filters[i]);
489 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
490 #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
491 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
492 #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
493 #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
494 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
495 #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
497 /* allowable substitutions for channel pairs when comparing layouts,
498 * ordered by priority for both values */
499 static const uint64_t ch_subst[][2] = {
500 { CH_FRONT_PAIR, CH_CENTER_PAIR },
501 { CH_FRONT_PAIR, CH_WIDE_PAIR },
502 { CH_FRONT_PAIR, AV_CH_FRONT_CENTER },
503 { CH_CENTER_PAIR, CH_FRONT_PAIR },
504 { CH_CENTER_PAIR, CH_WIDE_PAIR },
505 { CH_CENTER_PAIR, AV_CH_FRONT_CENTER },
506 { CH_WIDE_PAIR, CH_FRONT_PAIR },
507 { CH_WIDE_PAIR, CH_CENTER_PAIR },
508 { CH_WIDE_PAIR, AV_CH_FRONT_CENTER },
509 { AV_CH_FRONT_CENTER, CH_FRONT_PAIR },
510 { AV_CH_FRONT_CENTER, CH_CENTER_PAIR },
511 { AV_CH_FRONT_CENTER, CH_WIDE_PAIR },
512 { CH_SIDE_PAIR, CH_DIRECT_PAIR },
513 { CH_SIDE_PAIR, CH_BACK_PAIR },
514 { CH_SIDE_PAIR, AV_CH_BACK_CENTER },
515 { CH_BACK_PAIR, CH_DIRECT_PAIR },
516 { CH_BACK_PAIR, CH_SIDE_PAIR },
517 { CH_BACK_PAIR, AV_CH_BACK_CENTER },
518 { AV_CH_BACK_CENTER, CH_BACK_PAIR },
519 { AV_CH_BACK_CENTER, CH_DIRECT_PAIR },
520 { AV_CH_BACK_CENTER, CH_SIDE_PAIR },
523 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
525 AVFilterLink *link = NULL;
528 for (i = 0; i < filter->nb_inputs; i++) {
529 link = filter->inputs[i];
531 if (link->type == AVMEDIA_TYPE_AUDIO &&
532 link->out_channel_layouts->nb_channel_layouts == 1)
535 if (i == filter->nb_inputs)
538 for (i = 0; i < filter->nb_outputs; i++) {
539 AVFilterLink *outlink = filter->outputs[i];
540 int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
542 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
543 outlink->in_channel_layouts->nb_channel_layouts < 2)
546 for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
547 uint64_t in_chlayout = link->out_channel_layouts->channel_layouts[0];
548 uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
549 int in_channels = av_get_channel_layout_nb_channels(in_chlayout);
550 int out_channels = av_get_channel_layout_nb_channels(out_chlayout);
551 int count_diff = out_channels - in_channels;
552 int matched_channels, extra_channels;
555 /* channel substitution */
556 for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
557 uint64_t cmp0 = ch_subst[k][0];
558 uint64_t cmp1 = ch_subst[k][1];
559 if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
560 (out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
561 in_chlayout &= ~cmp0;
562 out_chlayout &= ~cmp1;
563 /* add score for channel match, minus a deduction for
564 having to do the substitution */
565 score += 10 * av_get_channel_layout_nb_channels(cmp1) - 2;
569 /* no penalty for LFE channel mismatch */
570 if ( (in_chlayout & AV_CH_LOW_FREQUENCY) &&
571 (out_chlayout & AV_CH_LOW_FREQUENCY))
573 in_chlayout &= ~AV_CH_LOW_FREQUENCY;
574 out_chlayout &= ~AV_CH_LOW_FREQUENCY;
576 matched_channels = av_get_channel_layout_nb_channels(in_chlayout &
578 extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
580 score += 10 * matched_channels - 5 * extra_channels;
582 if (score > best_score ||
583 (count_diff < best_count_diff && score == best_score)) {
586 best_count_diff = count_diff;
589 av_assert0(best_idx >= 0);
590 FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
591 outlink->in_channel_layouts->channel_layouts[best_idx]);
596 static void swap_channel_layouts(AVFilterGraph *graph)
600 for (i = 0; i < graph->nb_filters; i++)
601 swap_channel_layouts_on_filter(graph->filters[i]);
604 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
606 AVFilterLink *link = NULL;
610 for (i = 0; i < filter->nb_inputs; i++) {
611 link = filter->inputs[i];
613 if (link->type == AVMEDIA_TYPE_AUDIO &&
614 link->out_formats->format_count == 1)
617 if (i == filter->nb_inputs)
620 format = link->out_formats->formats[0];
621 bps = av_get_bytes_per_sample(format);
623 for (i = 0; i < filter->nb_outputs; i++) {
624 AVFilterLink *outlink = filter->outputs[i];
625 int best_idx = -1, best_score = INT_MIN;
627 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
628 outlink->in_formats->format_count < 2)
631 for (j = 0; j < outlink->in_formats->format_count; j++) {
632 int out_format = outlink->in_formats->formats[j];
633 int out_bps = av_get_bytes_per_sample(out_format);
636 if (av_get_packed_sample_fmt(out_format) == format ||
637 av_get_planar_sample_fmt(out_format) == format) {
642 /* for s32 and float prefer double to prevent loss of information */
643 if (bps == 4 && out_bps == 8) {
648 /* prefer closest higher or equal bps */
649 score = -abs(out_bps - bps);
653 if (score > best_score) {
658 av_assert0(best_idx >= 0);
659 FFSWAP(int, outlink->in_formats->formats[0],
660 outlink->in_formats->formats[best_idx]);
664 static void swap_sample_fmts(AVFilterGraph *graph)
668 for (i = 0; i < graph->nb_filters; i++)
669 swap_sample_fmts_on_filter(graph->filters[i]);
673 static int pick_formats(AVFilterGraph *graph)
677 for (i = 0; i < graph->nb_filters; i++) {
678 AVFilterContext *filter = graph->filters[i];
680 for (j = 0; j < filter->nb_inputs; j++)
681 if ((ret = pick_format(filter->inputs[j])) < 0)
683 for (j = 0; j < filter->nb_outputs; j++)
684 if ((ret = pick_format(filter->outputs[j])) < 0)
691 * Configure the formats of all the links in the graph.
693 static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
697 /* find supported formats from sub-filters, and merge along links */
698 if ((ret = query_formats(graph, log_ctx)) < 0)
701 /* Once everything is merged, it's possible that we'll still have
702 * multiple valid media format choices. We try to minimize the amount
703 * of format conversion inside filters */
704 reduce_formats(graph);
706 /* for audio filters, ensure the best format, sample rate and channel layout
708 swap_sample_fmts(graph);
709 swap_samplerates(graph);
710 swap_channel_layouts(graph);
712 if ((ret = pick_formats(graph)) < 0)
718 static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
724 for (i = 0; i < graph->nb_filters; i++) {
725 f = graph->filters[i];
727 for (j = 0; j < f->nb_inputs; j++) {
728 AVFilterLink *link = f->inputs[j];
729 AVFilterContext *fifo_ctx;
733 if (!link->dstpad->needs_fifo)
736 fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
737 avfilter_get_by_name("fifo") :
738 avfilter_get_by_name("afifo");
740 snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
742 ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
747 ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
756 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
760 if ((ret = graph_check_validity(graphctx, log_ctx)))
762 if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
764 if ((ret = graph_config_formats(graphctx, log_ctx)))
766 if ((ret = graph_config_links(graphctx, log_ctx)))