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);
96 int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
98 AVFilterContext *filt;
101 for (i = 0; i < graph->filter_count; i++) {
102 filt = graph->filters[i];
104 for (j = 0; j < filt->input_count; j++) {
105 if (!filt->inputs[j] || !filt->inputs[j]->src) {
106 av_log(log_ctx, AV_LOG_ERROR,
107 "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
108 filt->input_pads[j].name, filt->name, filt->filter->name);
109 return AVERROR(EINVAL);
113 for (j = 0; j < filt->output_count; j++) {
114 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
115 av_log(log_ctx, AV_LOG_ERROR,
116 "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
117 filt->output_pads[j].name, filt->name, filt->filter->name);
118 return AVERROR(EINVAL);
126 int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
128 AVFilterContext *filt;
131 for (i=0; i < graph->filter_count; i++) {
132 filt = graph->filters[i];
134 if (!filt->output_count) {
135 if ((ret = avfilter_config_links(filt)))
143 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
147 for (i = 0; i < graph->filter_count; i++)
148 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
149 return graph->filters[i];
154 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
157 int scaler_count = 0, resampler_count = 0;
159 /* ask all the sub-filters for their supported media formats */
160 for (i = 0; i < graph->filter_count; i++) {
161 if (graph->filters[i]->filter->query_formats)
162 graph->filters[i]->filter->query_formats(graph->filters[i]);
164 ff_default_query_formats(graph->filters[i]);
167 /* go through and merge as many format lists as possible */
168 for (i = 0; i < graph->filter_count; i++) {
169 AVFilterContext *filter = graph->filters[i];
171 for (j = 0; j < filter->input_count; j++) {
172 AVFilterLink *link = filter->inputs[j];
173 int convert_needed = 0;
178 if (link->in_formats != link->out_formats &&
179 !avfilter_merge_formats(link->in_formats,
182 if (link->type == AVMEDIA_TYPE_AUDIO) {
183 if (link->in_channel_layouts != link->out_channel_layouts &&
184 !ff_merge_channel_layouts(link->in_channel_layouts,
185 link->out_channel_layouts))
187 if (link->in_samplerates != link->out_samplerates &&
188 !ff_merge_samplerates(link->in_samplerates,
189 link->out_samplerates))
193 if (convert_needed) {
194 AVFilterContext *convert;
196 AVFilterLink *inlink, *outlink;
197 char scale_args[256];
200 /* couldn't merge format lists. auto-insert conversion filter */
201 switch (link->type) {
202 case AVMEDIA_TYPE_VIDEO:
203 snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
205 snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
206 if ((ret = avfilter_graph_create_filter(&convert,
207 avfilter_get_by_name("scale"),
208 inst_name, scale_args, NULL,
212 case AVMEDIA_TYPE_AUDIO:
213 if (!(filter = avfilter_get_by_name("resample"))) {
214 av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
215 "not present, cannot convert audio formats.\n");
216 return AVERROR(EINVAL);
219 snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
221 if ((ret = avfilter_graph_create_filter(&convert,
222 avfilter_get_by_name("resample"),
223 inst_name, NULL, NULL, graph)) < 0)
227 return AVERROR(EINVAL);
230 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
233 convert->filter->query_formats(convert);
234 inlink = convert->inputs[0];
235 outlink = convert->outputs[0];
236 if (!avfilter_merge_formats( inlink->in_formats, inlink->out_formats) ||
237 !avfilter_merge_formats(outlink->in_formats, outlink->out_formats))
238 ret |= AVERROR(ENOSYS);
239 if (inlink->type == AVMEDIA_TYPE_AUDIO &&
240 (!ff_merge_samplerates(inlink->in_samplerates,
241 inlink->out_samplerates) ||
242 !ff_merge_channel_layouts(inlink->in_channel_layouts,
243 inlink->out_channel_layouts)))
244 ret |= AVERROR(ENOSYS);
245 if (outlink->type == AVMEDIA_TYPE_AUDIO &&
246 (!ff_merge_samplerates(outlink->in_samplerates,
247 outlink->out_samplerates) ||
248 !ff_merge_channel_layouts(outlink->in_channel_layouts,
249 outlink->out_channel_layouts)))
250 ret |= AVERROR(ENOSYS);
253 av_log(log_ctx, AV_LOG_ERROR,
254 "Impossible to convert between the formats supported by the filter "
255 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
265 static int pick_format(AVFilterLink *link)
267 if (!link || !link->in_formats)
270 link->in_formats->format_count = 1;
271 link->format = link->in_formats->formats[0];
273 if (link->type == AVMEDIA_TYPE_AUDIO) {
274 if (!link->in_samplerates->format_count) {
275 av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
276 " the link between filters %s and %s.\n", link->src->name,
278 return AVERROR(EINVAL);
280 link->in_samplerates->format_count = 1;
281 link->sample_rate = link->in_samplerates->formats[0];
283 if (!link->in_channel_layouts->nb_channel_layouts) {
284 av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
285 "the link between filters %s and %s.\n", link->src->name,
287 return AVERROR(EINVAL);
289 link->in_channel_layouts->nb_channel_layouts = 1;
290 link->channel_layout = link->in_channel_layouts->channel_layouts[0];
293 avfilter_formats_unref(&link->in_formats);
294 avfilter_formats_unref(&link->out_formats);
295 avfilter_formats_unref(&link->in_samplerates);
296 avfilter_formats_unref(&link->out_samplerates);
297 ff_channel_layouts_unref(&link->in_channel_layouts);
298 ff_channel_layouts_unref(&link->out_channel_layouts);
303 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
305 for (i = 0; i < filter->input_count; i++) { \
306 AVFilterLink *link = filter->inputs[i]; \
309 if (!link->out_ ## list || link->out_ ## list->nb != 1) \
311 fmt = link->out_ ## list->var[0]; \
313 for (j = 0; j < filter->output_count; j++) { \
314 AVFilterLink *out_link = filter->outputs[j]; \
317 if (link->type != out_link->type || \
318 out_link->in_ ## list->nb == 1) \
320 fmts = out_link->in_ ## list; \
322 if (!out_link->in_ ## list->nb) { \
323 add_format(&out_link->in_ ##list, fmt); \
327 for (k = 0; k < out_link->in_ ## list->nb; k++) \
328 if (fmts->var[k] == fmt) { \
329 fmts->var[0] = fmt; \
338 static int reduce_formats_on_filter(AVFilterContext *filter)
340 int i, j, k, ret = 0;
342 REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
343 format_count, avfilter_add_format);
344 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
345 format_count, avfilter_add_format);
346 REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
347 channel_layouts, nb_channel_layouts, ff_add_channel_layout);
352 static void reduce_formats(AVFilterGraph *graph)
359 for (i = 0; i < graph->filter_count; i++)
360 reduced |= reduce_formats_on_filter(graph->filters[i]);
364 static void swap_samplerates_on_filter(AVFilterContext *filter)
366 AVFilterLink *link = NULL;
370 for (i = 0; i < filter->input_count; i++) {
371 link = filter->inputs[i];
373 if (link->type == AVMEDIA_TYPE_AUDIO &&
374 link->out_samplerates->format_count == 1)
377 if (i == filter->input_count)
380 sample_rate = link->out_samplerates->formats[0];
382 for (i = 0; i < filter->output_count; i++) {
383 AVFilterLink *outlink = filter->outputs[i];
384 int best_idx, best_diff = INT_MAX;
386 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
387 outlink->in_samplerates->format_count < 2)
390 for (j = 0; j < outlink->in_samplerates->format_count; j++) {
391 int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
393 if (diff < best_diff) {
398 FFSWAP(int, outlink->in_samplerates->formats[0],
399 outlink->in_samplerates->formats[best_idx]);
403 static void swap_samplerates(AVFilterGraph *graph)
407 for (i = 0; i < graph->filter_count; i++)
408 swap_samplerates_on_filter(graph->filters[i]);
411 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
413 AVFilterLink *link = NULL;
417 for (i = 0; i < filter->input_count; i++) {
418 link = filter->inputs[i];
420 if (link->type == AVMEDIA_TYPE_AUDIO &&
421 link->out_channel_layouts->nb_channel_layouts == 1)
424 if (i == filter->input_count)
427 chlayout = link->out_channel_layouts->channel_layouts[0];
429 for (i = 0; i < filter->output_count; i++) {
430 AVFilterLink *outlink = filter->outputs[i];
431 int best_idx, best_score = INT_MIN;
433 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
434 outlink->in_channel_layouts->nb_channel_layouts < 2)
437 for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
438 uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
439 int matched_channels = av_get_channel_layout_nb_channels(chlayout &
441 int extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
443 int score = matched_channels - extra_channels;
445 if (score > best_score) {
450 FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
451 outlink->in_channel_layouts->channel_layouts[best_idx]);
456 static void swap_channel_layouts(AVFilterGraph *graph)
460 for (i = 0; i < graph->filter_count; i++)
461 swap_channel_layouts_on_filter(graph->filters[i]);
464 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
466 AVFilterLink *link = NULL;
470 for (i = 0; i < filter->input_count; i++) {
471 link = filter->inputs[i];
473 if (link->type == AVMEDIA_TYPE_AUDIO &&
474 link->out_formats->format_count == 1)
477 if (i == filter->input_count)
480 format = link->out_formats->formats[0];
481 bps = av_get_bytes_per_sample(format);
483 for (i = 0; i < filter->output_count; i++) {
484 AVFilterLink *outlink = filter->outputs[i];
485 int best_idx, best_score = INT_MIN;
487 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
488 outlink->in_formats->format_count < 2)
491 for (j = 0; j < outlink->in_formats->format_count; j++) {
492 int out_format = outlink->in_formats->formats[j];
493 int out_bps = av_get_bytes_per_sample(out_format);
496 if (av_get_packed_sample_fmt(out_format) == format ||
497 av_get_planar_sample_fmt(out_format) == format) {
502 /* for s32 and float prefer double to prevent loss of information */
503 if (bps == 4 && out_bps == 8) {
508 /* prefer closest higher or equal bps */
509 score = -abs(out_bps - bps);
513 if (score > best_score) {
518 FFSWAP(int, outlink->in_formats->formats[0],
519 outlink->in_formats->formats[best_idx]);
523 static void swap_sample_fmts(AVFilterGraph *graph)
527 for (i = 0; i < graph->filter_count; i++)
528 swap_sample_fmts_on_filter(graph->filters[i]);
532 static int pick_formats(AVFilterGraph *graph)
536 for (i = 0; i < graph->filter_count; i++) {
537 AVFilterContext *filter = graph->filters[i];
539 for (j = 0; j < filter->input_count; j++)
540 if ((ret = pick_format(filter->inputs[j])) < 0)
542 for (j = 0; j < filter->output_count; j++)
543 if ((ret = pick_format(filter->outputs[j])) < 0)
549 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
553 /* find supported formats from sub-filters, and merge along links */
554 if ((ret = query_formats(graph, log_ctx)) < 0)
557 /* Once everything is merged, it's possible that we'll still have
558 * multiple valid media format choices. We try to minimize the amount
559 * of format conversion inside filters */
560 reduce_formats(graph);
562 /* for audio filters, ensure the best format, sample rate and channel layout
564 swap_sample_fmts(graph);
565 swap_samplerates(graph);
566 swap_channel_layouts(graph);
568 if ((ret = pick_formats(graph)) < 0)
574 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
578 if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
580 if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
582 if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))