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/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(AVFilterGraph));
45 #if FF_API_GRAPH_AVCLASS
46 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->input_count; 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->output_count; 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->output_count) {
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->input_count; 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 snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
218 snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
219 if ((ret = avfilter_graph_create_filter(&convert,
220 avfilter_get_by_name("scale"),
221 inst_name, scale_args, NULL,
225 case AVMEDIA_TYPE_AUDIO:
226 if (!(filter = avfilter_get_by_name("resample"))) {
227 av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
228 "not present, cannot convert audio formats.\n");
229 return AVERROR(EINVAL);
232 snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
234 if ((ret = avfilter_graph_create_filter(&convert,
235 avfilter_get_by_name("resample"),
236 inst_name, NULL, NULL, graph)) < 0)
240 return AVERROR(EINVAL);
243 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
246 convert->filter->query_formats(convert);
247 inlink = convert->inputs[0];
248 outlink = convert->outputs[0];
249 if (!ff_merge_formats( inlink->in_formats, inlink->out_formats) ||
250 !ff_merge_formats(outlink->in_formats, outlink->out_formats))
251 ret |= AVERROR(ENOSYS);
252 if (inlink->type == AVMEDIA_TYPE_AUDIO &&
253 (!ff_merge_samplerates(inlink->in_samplerates,
254 inlink->out_samplerates) ||
255 !ff_merge_channel_layouts(inlink->in_channel_layouts,
256 inlink->out_channel_layouts)))
257 ret |= AVERROR(ENOSYS);
258 if (outlink->type == AVMEDIA_TYPE_AUDIO &&
259 (!ff_merge_samplerates(outlink->in_samplerates,
260 outlink->out_samplerates) ||
261 !ff_merge_channel_layouts(outlink->in_channel_layouts,
262 outlink->out_channel_layouts)))
263 ret |= AVERROR(ENOSYS);
266 av_log(log_ctx, AV_LOG_ERROR,
267 "Impossible to convert between the formats supported by the filter "
268 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
278 static int pick_format(AVFilterLink *link)
280 if (!link || !link->in_formats)
283 link->in_formats->format_count = 1;
284 link->format = link->in_formats->formats[0];
286 if (link->type == AVMEDIA_TYPE_AUDIO) {
287 if (!link->in_samplerates->format_count) {
288 av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
289 " the link between filters %s and %s.\n", link->src->name,
291 return AVERROR(EINVAL);
293 link->in_samplerates->format_count = 1;
294 link->sample_rate = link->in_samplerates->formats[0];
296 if (!link->in_channel_layouts->nb_channel_layouts) {
297 av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
298 "the link between filters %s and %s.\n", link->src->name,
300 return AVERROR(EINVAL);
302 link->in_channel_layouts->nb_channel_layouts = 1;
303 link->channel_layout = link->in_channel_layouts->channel_layouts[0];
306 ff_formats_unref(&link->in_formats);
307 ff_formats_unref(&link->out_formats);
308 ff_formats_unref(&link->in_samplerates);
309 ff_formats_unref(&link->out_samplerates);
310 ff_channel_layouts_unref(&link->in_channel_layouts);
311 ff_channel_layouts_unref(&link->out_channel_layouts);
316 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
318 for (i = 0; i < filter->input_count; i++) { \
319 AVFilterLink *link = filter->inputs[i]; \
322 if (!link->out_ ## list || link->out_ ## list->nb != 1) \
324 fmt = link->out_ ## list->var[0]; \
326 for (j = 0; j < filter->output_count; j++) { \
327 AVFilterLink *out_link = filter->outputs[j]; \
330 if (link->type != out_link->type || \
331 out_link->in_ ## list->nb == 1) \
333 fmts = out_link->in_ ## list; \
335 if (!out_link->in_ ## list->nb) { \
336 add_format(&out_link->in_ ##list, fmt); \
340 for (k = 0; k < out_link->in_ ## list->nb; k++) \
341 if (fmts->var[k] == fmt) { \
342 fmts->var[0] = fmt; \
351 static int reduce_formats_on_filter(AVFilterContext *filter)
353 int i, j, k, ret = 0;
355 REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
356 format_count, ff_add_format);
357 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
358 format_count, ff_add_format);
359 REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
360 channel_layouts, nb_channel_layouts, ff_add_channel_layout);
365 static void reduce_formats(AVFilterGraph *graph)
372 for (i = 0; i < graph->filter_count; i++)
373 reduced |= reduce_formats_on_filter(graph->filters[i]);
377 static void swap_samplerates_on_filter(AVFilterContext *filter)
379 AVFilterLink *link = NULL;
383 for (i = 0; i < filter->input_count; i++) {
384 link = filter->inputs[i];
386 if (link->type == AVMEDIA_TYPE_AUDIO &&
387 link->out_samplerates->format_count == 1)
390 if (i == filter->input_count)
393 sample_rate = link->out_samplerates->formats[0];
395 for (i = 0; i < filter->output_count; i++) {
396 AVFilterLink *outlink = filter->outputs[i];
397 int best_idx, best_diff = INT_MAX;
399 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
400 outlink->in_samplerates->format_count < 2)
403 for (j = 0; j < outlink->in_samplerates->format_count; j++) {
404 int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
406 if (diff < best_diff) {
411 FFSWAP(int, outlink->in_samplerates->formats[0],
412 outlink->in_samplerates->formats[best_idx]);
416 static void swap_samplerates(AVFilterGraph *graph)
420 for (i = 0; i < graph->filter_count; i++)
421 swap_samplerates_on_filter(graph->filters[i]);
424 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
426 AVFilterLink *link = NULL;
430 for (i = 0; i < filter->input_count; i++) {
431 link = filter->inputs[i];
433 if (link->type == AVMEDIA_TYPE_AUDIO &&
434 link->out_channel_layouts->nb_channel_layouts == 1)
437 if (i == filter->input_count)
440 chlayout = link->out_channel_layouts->channel_layouts[0];
442 for (i = 0; i < filter->output_count; i++) {
443 AVFilterLink *outlink = filter->outputs[i];
444 int best_idx, best_score = INT_MIN;
446 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
447 outlink->in_channel_layouts->nb_channel_layouts < 2)
450 for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
451 uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
452 int matched_channels = av_get_channel_layout_nb_channels(chlayout &
454 int extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
456 int score = matched_channels - extra_channels;
458 if (score > best_score) {
463 FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
464 outlink->in_channel_layouts->channel_layouts[best_idx]);
469 static void swap_channel_layouts(AVFilterGraph *graph)
473 for (i = 0; i < graph->filter_count; i++)
474 swap_channel_layouts_on_filter(graph->filters[i]);
477 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
479 AVFilterLink *link = NULL;
483 for (i = 0; i < filter->input_count; i++) {
484 link = filter->inputs[i];
486 if (link->type == AVMEDIA_TYPE_AUDIO &&
487 link->out_formats->format_count == 1)
490 if (i == filter->input_count)
493 format = link->out_formats->formats[0];
494 bps = av_get_bytes_per_sample(format);
496 for (i = 0; i < filter->output_count; i++) {
497 AVFilterLink *outlink = filter->outputs[i];
498 int best_idx, best_score = INT_MIN;
500 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
501 outlink->in_formats->format_count < 2)
504 for (j = 0; j < outlink->in_formats->format_count; j++) {
505 int out_format = outlink->in_formats->formats[j];
506 int out_bps = av_get_bytes_per_sample(out_format);
509 if (av_get_packed_sample_fmt(out_format) == format ||
510 av_get_planar_sample_fmt(out_format) == format) {
515 /* for s32 and float prefer double to prevent loss of information */
516 if (bps == 4 && out_bps == 8) {
521 /* prefer closest higher or equal bps */
522 score = -abs(out_bps - bps);
526 if (score > best_score) {
531 FFSWAP(int, outlink->in_formats->formats[0],
532 outlink->in_formats->formats[best_idx]);
536 static void swap_sample_fmts(AVFilterGraph *graph)
540 for (i = 0; i < graph->filter_count; i++)
541 swap_sample_fmts_on_filter(graph->filters[i]);
545 static int pick_formats(AVFilterGraph *graph)
549 for (i = 0; i < graph->filter_count; i++) {
550 AVFilterContext *filter = graph->filters[i];
552 for (j = 0; j < filter->input_count; j++)
553 if ((ret = pick_format(filter->inputs[j])) < 0)
555 for (j = 0; j < filter->output_count; j++)
556 if ((ret = pick_format(filter->outputs[j])) < 0)
563 * Configure the formats of all the links in the graph.
565 static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
569 /* find supported formats from sub-filters, and merge along links */
570 if ((ret = query_formats(graph, log_ctx)) < 0)
573 /* Once everything is merged, it's possible that we'll still have
574 * multiple valid media format choices. We try to minimize the amount
575 * of format conversion inside filters */
576 reduce_formats(graph);
578 /* for audio filters, ensure the best format, sample rate and channel layout
580 swap_sample_fmts(graph);
581 swap_samplerates(graph);
582 swap_channel_layouts(graph);
584 if ((ret = pick_formats(graph)) < 0)
590 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
594 if ((ret = graph_check_validity(graphctx, log_ctx)))
596 if ((ret = graph_config_formats(graphctx, log_ctx)))
598 if ((ret = graph_config_links(graphctx, log_ctx)))