lavfi: connect libavresample options to af_resample via AVFilterGraph
[ffmpeg.git] / libavfilter / avfiltergraph.c
1 /*
2  * filter graphs
3  * Copyright (c) 2008 Vitor Sessak
4  * Copyright (c) 2007 Bobby Bingham
5  *
6  * This file is part of Libav.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <ctype.h>
24 #include <string.h>
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/common.h"
29 #include "libavutil/log.h"
30 #include "avfilter.h"
31 #include "avfiltergraph.h"
32 #include "formats.h"
33 #include "internal.h"
34
35 static const AVClass filtergraph_class = {
36     .class_name = "AVFilterGraph",
37     .item_name  = av_default_item_name,
38     .version    = LIBAVUTIL_VERSION_INT,
39 };
40
41 AVFilterGraph *avfilter_graph_alloc(void)
42 {
43     AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
44     if (!ret)
45         return NULL;
46     ret->av_class = &filtergraph_class;
47     return ret;
48 }
49
50 void avfilter_graph_free(AVFilterGraph **graph)
51 {
52     if (!*graph)
53         return;
54     for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
55         avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
56     av_freep(&(*graph)->scale_sws_opts);
57     av_freep(&(*graph)->resample_lavr_opts);
58     av_freep(&(*graph)->filters);
59     av_freep(graph);
60 }
61
62 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
63 {
64     AVFilterContext **filters = av_realloc(graph->filters,
65                                            sizeof(AVFilterContext*) * (graph->filter_count+1));
66     if (!filters)
67         return AVERROR(ENOMEM);
68
69     graph->filters = filters;
70     graph->filters[graph->filter_count++] = filter;
71
72     return 0;
73 }
74
75 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
76                                  const char *name, const char *args, void *opaque,
77                                  AVFilterGraph *graph_ctx)
78 {
79     int ret;
80
81     if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
82         goto fail;
83     if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
84         goto fail;
85     if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
86         goto fail;
87     return 0;
88
89 fail:
90     if (*filt_ctx)
91         avfilter_free(*filt_ctx);
92     *filt_ctx = NULL;
93     return ret;
94 }
95
96 /**
97  * Check for the validity of graph.
98  *
99  * A graph is considered valid if all its input and output pads are
100  * connected.
101  *
102  * @return 0 in case of success, a negative value otherwise
103  */
104 static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
105 {
106     AVFilterContext *filt;
107     int i, j;
108
109     for (i = 0; i < graph->filter_count; i++) {
110         filt = graph->filters[i];
111
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);
118             }
119         }
120
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);
127             }
128         }
129     }
130
131     return 0;
132 }
133
134 /**
135  * Configure all the links of graphctx.
136  *
137  * @return 0 in case of success, a negative value otherwise
138  */
139 static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
140 {
141     AVFilterContext *filt;
142     int i, ret;
143
144     for (i=0; i < graph->filter_count; i++) {
145         filt = graph->filters[i];
146
147         if (!filt->nb_outputs) {
148             if ((ret = avfilter_config_links(filt)))
149                 return ret;
150         }
151     }
152
153     return 0;
154 }
155
156 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
157 {
158     int i;
159
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];
163
164     return NULL;
165 }
166
167 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
168 {
169     int i, j, ret;
170     int scaler_count = 0, resampler_count = 0;
171
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]);
176         else
177             ff_default_query_formats(graph->filters[i]);
178     }
179
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];
183
184         for (j = 0; j < filter->nb_inputs; j++) {
185             AVFilterLink *link = filter->inputs[j];
186             int convert_needed = 0;
187
188             if (!link)
189                 continue;
190
191             if (link->in_formats != link->out_formats &&
192                 !ff_merge_formats(link->in_formats,
193                                         link->out_formats))
194                 convert_needed = 1;
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))
199                     convert_needed = 1;
200                 if (link->in_samplerates != link->out_samplerates &&
201                     !ff_merge_samplerates(link->in_samplerates,
202                                           link->out_samplerates))
203                     convert_needed = 1;
204             }
205
206             if (convert_needed) {
207                 AVFilterContext *convert;
208                 AVFilter *filter;
209                 AVFilterLink *inlink, *outlink;
210                 char scale_args[256];
211                 char inst_name[30];
212
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);
220                     }
221
222                     snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
223                              scaler_count++);
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,
227                                                             graph)) < 0)
228                         return ret;
229                     break;
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);
235                     }
236
237                     snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
238                              resampler_count++);
239                     scale_args[0] = '\0';
240                     if (graph->resample_lavr_opts)
241                         snprintf(scale_args, sizeof(scale_args), "%s",
242                                  graph->resample_lavr_opts);
243                     if ((ret = avfilter_graph_create_filter(&convert, filter,
244                                                             inst_name, scale_args,
245                                                             NULL, graph)) < 0)
246                         return ret;
247                     break;
248                 default:
249                     return AVERROR(EINVAL);
250                 }
251
252                 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
253                     return ret;
254
255                 convert->filter->query_formats(convert);
256                 inlink  = convert->inputs[0];
257                 outlink = convert->outputs[0];
258                 if (!ff_merge_formats( inlink->in_formats,  inlink->out_formats) ||
259                     !ff_merge_formats(outlink->in_formats, outlink->out_formats))
260                     ret |= AVERROR(ENOSYS);
261                 if (inlink->type == AVMEDIA_TYPE_AUDIO &&
262                     (!ff_merge_samplerates(inlink->in_samplerates,
263                                            inlink->out_samplerates) ||
264                      !ff_merge_channel_layouts(inlink->in_channel_layouts,
265                                                inlink->out_channel_layouts)))
266                     ret |= AVERROR(ENOSYS);
267                 if (outlink->type == AVMEDIA_TYPE_AUDIO &&
268                     (!ff_merge_samplerates(outlink->in_samplerates,
269                                            outlink->out_samplerates) ||
270                      !ff_merge_channel_layouts(outlink->in_channel_layouts,
271                                                outlink->out_channel_layouts)))
272                     ret |= AVERROR(ENOSYS);
273
274                 if (ret < 0) {
275                     av_log(log_ctx, AV_LOG_ERROR,
276                            "Impossible to convert between the formats supported by the filter "
277                            "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
278                     return ret;
279                 }
280             }
281         }
282     }
283
284     return 0;
285 }
286
287 static int pick_format(AVFilterLink *link)
288 {
289     if (!link || !link->in_formats)
290         return 0;
291
292     link->in_formats->format_count = 1;
293     link->format = link->in_formats->formats[0];
294
295     if (link->type == AVMEDIA_TYPE_AUDIO) {
296         if (!link->in_samplerates->format_count) {
297             av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
298                    " the link between filters %s and %s.\n", link->src->name,
299                    link->dst->name);
300             return AVERROR(EINVAL);
301         }
302         link->in_samplerates->format_count = 1;
303         link->sample_rate = link->in_samplerates->formats[0];
304
305         if (!link->in_channel_layouts->nb_channel_layouts) {
306             av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
307                    "the link between filters %s and %s.\n", link->src->name,
308                    link->dst->name);
309             return AVERROR(EINVAL);
310         }
311         link->in_channel_layouts->nb_channel_layouts = 1;
312         link->channel_layout = link->in_channel_layouts->channel_layouts[0];
313     }
314
315     ff_formats_unref(&link->in_formats);
316     ff_formats_unref(&link->out_formats);
317     ff_formats_unref(&link->in_samplerates);
318     ff_formats_unref(&link->out_samplerates);
319     ff_channel_layouts_unref(&link->in_channel_layouts);
320     ff_channel_layouts_unref(&link->out_channel_layouts);
321
322     return 0;
323 }
324
325 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
326 do {                                                                   \
327     for (i = 0; i < filter->nb_inputs; i++) {                          \
328         AVFilterLink *link = filter->inputs[i];                        \
329         fmt_type fmt;                                                  \
330                                                                        \
331         if (!link->out_ ## list || link->out_ ## list->nb != 1)        \
332             continue;                                                  \
333         fmt = link->out_ ## list->var[0];                              \
334                                                                        \
335         for (j = 0; j < filter->nb_outputs; j++) {                     \
336             AVFilterLink *out_link = filter->outputs[j];               \
337             list_type *fmts;                                           \
338                                                                        \
339             if (link->type != out_link->type ||                        \
340                 out_link->in_ ## list->nb == 1)                        \
341                 continue;                                              \
342             fmts = out_link->in_ ## list;                              \
343                                                                        \
344             if (!out_link->in_ ## list->nb) {                          \
345                 add_format(&out_link->in_ ##list, fmt);                \
346                 break;                                                 \
347             }                                                          \
348                                                                        \
349             for (k = 0; k < out_link->in_ ## list->nb; k++)            \
350                 if (fmts->var[k] == fmt) {                             \
351                     fmts->var[0]  = fmt;                               \
352                     fmts->nb = 1;                                      \
353                     ret = 1;                                           \
354                     break;                                             \
355                 }                                                      \
356         }                                                              \
357     }                                                                  \
358 } while (0)
359
360 static int reduce_formats_on_filter(AVFilterContext *filter)
361 {
362     int i, j, k, ret = 0;
363
364     REDUCE_FORMATS(int,      AVFilterFormats,        formats,         formats,
365                    format_count, ff_add_format);
366     REDUCE_FORMATS(int,      AVFilterFormats,        samplerates,     formats,
367                    format_count, ff_add_format);
368     REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
369                    channel_layouts, nb_channel_layouts, ff_add_channel_layout);
370
371     return ret;
372 }
373
374 static void reduce_formats(AVFilterGraph *graph)
375 {
376     int i, reduced;
377
378     do {
379         reduced = 0;
380
381         for (i = 0; i < graph->filter_count; i++)
382             reduced |= reduce_formats_on_filter(graph->filters[i]);
383     } while (reduced);
384 }
385
386 static void swap_samplerates_on_filter(AVFilterContext *filter)
387 {
388     AVFilterLink *link = NULL;
389     int sample_rate;
390     int i, j;
391
392     for (i = 0; i < filter->nb_inputs; i++) {
393         link = filter->inputs[i];
394
395         if (link->type == AVMEDIA_TYPE_AUDIO &&
396             link->out_samplerates->format_count == 1)
397             break;
398     }
399     if (i == filter->nb_inputs)
400         return;
401
402     sample_rate = link->out_samplerates->formats[0];
403
404     for (i = 0; i < filter->nb_outputs; i++) {
405         AVFilterLink *outlink = filter->outputs[i];
406         int best_idx, best_diff = INT_MAX;
407
408         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
409             outlink->in_samplerates->format_count < 2)
410             continue;
411
412         for (j = 0; j < outlink->in_samplerates->format_count; j++) {
413             int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
414
415             if (diff < best_diff) {
416                 best_diff = diff;
417                 best_idx  = j;
418             }
419         }
420         FFSWAP(int, outlink->in_samplerates->formats[0],
421                outlink->in_samplerates->formats[best_idx]);
422     }
423 }
424
425 static void swap_samplerates(AVFilterGraph *graph)
426 {
427     int i;
428
429     for (i = 0; i < graph->filter_count; i++)
430         swap_samplerates_on_filter(graph->filters[i]);
431 }
432
433 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
434 #define CH_FRONT_PAIR  (AV_CH_FRONT_LEFT           | AV_CH_FRONT_RIGHT)
435 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT          | AV_CH_STEREO_RIGHT)
436 #define CH_WIDE_PAIR   (AV_CH_WIDE_LEFT            | AV_CH_WIDE_RIGHT)
437 #define CH_SIDE_PAIR   (AV_CH_SIDE_LEFT            | AV_CH_SIDE_RIGHT)
438 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
439 #define CH_BACK_PAIR   (AV_CH_BACK_LEFT            | AV_CH_BACK_RIGHT)
440
441 /* allowable substitutions for channel pairs when comparing layouts,
442  * ordered by priority for both values */
443 static const uint64_t ch_subst[][2] = {
444     { CH_FRONT_PAIR,      CH_CENTER_PAIR     },
445     { CH_FRONT_PAIR,      CH_WIDE_PAIR       },
446     { CH_FRONT_PAIR,      AV_CH_FRONT_CENTER },
447     { CH_CENTER_PAIR,     CH_FRONT_PAIR      },
448     { CH_CENTER_PAIR,     CH_WIDE_PAIR       },
449     { CH_CENTER_PAIR,     AV_CH_FRONT_CENTER },
450     { CH_WIDE_PAIR,       CH_FRONT_PAIR      },
451     { CH_WIDE_PAIR,       CH_CENTER_PAIR     },
452     { CH_WIDE_PAIR,       AV_CH_FRONT_CENTER },
453     { AV_CH_FRONT_CENTER, CH_FRONT_PAIR      },
454     { AV_CH_FRONT_CENTER, CH_CENTER_PAIR     },
455     { AV_CH_FRONT_CENTER, CH_WIDE_PAIR       },
456     { CH_SIDE_PAIR,       CH_DIRECT_PAIR     },
457     { CH_SIDE_PAIR,       CH_BACK_PAIR       },
458     { CH_SIDE_PAIR,       AV_CH_BACK_CENTER  },
459     { CH_BACK_PAIR,       CH_DIRECT_PAIR     },
460     { CH_BACK_PAIR,       CH_SIDE_PAIR       },
461     { CH_BACK_PAIR,       AV_CH_BACK_CENTER  },
462     { AV_CH_BACK_CENTER,  CH_BACK_PAIR       },
463     { AV_CH_BACK_CENTER,  CH_DIRECT_PAIR     },
464     { AV_CH_BACK_CENTER,  CH_SIDE_PAIR       },
465 };
466
467 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
468 {
469     AVFilterLink *link = NULL;
470     int i, j, k;
471
472     for (i = 0; i < filter->nb_inputs; i++) {
473         link = filter->inputs[i];
474
475         if (link->type == AVMEDIA_TYPE_AUDIO &&
476             link->out_channel_layouts->nb_channel_layouts == 1)
477             break;
478     }
479     if (i == filter->nb_inputs)
480         return;
481
482     for (i = 0; i < filter->nb_outputs; i++) {
483         AVFilterLink *outlink = filter->outputs[i];
484         int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
485
486         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
487             outlink->in_channel_layouts->nb_channel_layouts < 2)
488             continue;
489
490         for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
491             uint64_t  in_chlayout = link->out_channel_layouts->channel_layouts[0];
492             uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
493             int  in_channels      = av_get_channel_layout_nb_channels(in_chlayout);
494             int out_channels      = av_get_channel_layout_nb_channels(out_chlayout);
495             int count_diff        = out_channels - in_channels;
496             int matched_channels, extra_channels;
497             int score = 0;
498
499             /* channel substitution */
500             for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
501                 uint64_t cmp0 = ch_subst[k][0];
502                 uint64_t cmp1 = ch_subst[k][1];
503                 if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
504                     (out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
505                     in_chlayout  &= ~cmp0;
506                     out_chlayout &= ~cmp1;
507                     /* add score for channel match, minus a deduction for
508                        having to do the substitution */
509                     score += 10 * av_get_channel_layout_nb_channels(cmp1) - 2;
510                 }
511             }
512
513             /* no penalty for LFE channel mismatch */
514             if ( (in_chlayout & AV_CH_LOW_FREQUENCY) &&
515                 (out_chlayout & AV_CH_LOW_FREQUENCY))
516                 score += 10;
517             in_chlayout  &= ~AV_CH_LOW_FREQUENCY;
518             out_chlayout &= ~AV_CH_LOW_FREQUENCY;
519
520             matched_channels = av_get_channel_layout_nb_channels(in_chlayout &
521                                                                  out_chlayout);
522             extra_channels   = av_get_channel_layout_nb_channels(out_chlayout &
523                                                                  (~in_chlayout));
524             score += 10 * matched_channels - 5 * extra_channels;
525
526             if (score > best_score ||
527                 (count_diff < best_count_diff && score == best_score)) {
528                 best_score = score;
529                 best_idx   = j;
530                 best_count_diff = count_diff;
531             }
532         }
533         av_assert0(best_idx >= 0);
534         FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
535                outlink->in_channel_layouts->channel_layouts[best_idx]);
536     }
537
538 }
539
540 static void swap_channel_layouts(AVFilterGraph *graph)
541 {
542     int i;
543
544     for (i = 0; i < graph->filter_count; i++)
545         swap_channel_layouts_on_filter(graph->filters[i]);
546 }
547
548 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
549 {
550     AVFilterLink *link = NULL;
551     int format, bps;
552     int i, j;
553
554     for (i = 0; i < filter->nb_inputs; i++) {
555         link = filter->inputs[i];
556
557         if (link->type == AVMEDIA_TYPE_AUDIO &&
558             link->out_formats->format_count == 1)
559             break;
560     }
561     if (i == filter->nb_inputs)
562         return;
563
564     format = link->out_formats->formats[0];
565     bps    = av_get_bytes_per_sample(format);
566
567     for (i = 0; i < filter->nb_outputs; i++) {
568         AVFilterLink *outlink = filter->outputs[i];
569         int best_idx = -1, best_score = INT_MIN;
570
571         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
572             outlink->in_formats->format_count < 2)
573             continue;
574
575         for (j = 0; j < outlink->in_formats->format_count; j++) {
576             int out_format = outlink->in_formats->formats[j];
577             int out_bps    = av_get_bytes_per_sample(out_format);
578             int score;
579
580             if (av_get_packed_sample_fmt(out_format) == format ||
581                 av_get_planar_sample_fmt(out_format) == format) {
582                 best_idx   = j;
583                 break;
584             }
585
586             /* for s32 and float prefer double to prevent loss of information */
587             if (bps == 4 && out_bps == 8) {
588                 best_idx = j;
589                 break;
590             }
591
592             /* prefer closest higher or equal bps */
593             score = -abs(out_bps - bps);
594             if (out_bps >= bps)
595                 score += INT_MAX/2;
596
597             if (score > best_score) {
598                 best_score = score;
599                 best_idx   = j;
600             }
601         }
602         av_assert0(best_idx >= 0);
603         FFSWAP(int, outlink->in_formats->formats[0],
604                outlink->in_formats->formats[best_idx]);
605     }
606 }
607
608 static void swap_sample_fmts(AVFilterGraph *graph)
609 {
610     int i;
611
612     for (i = 0; i < graph->filter_count; i++)
613         swap_sample_fmts_on_filter(graph->filters[i]);
614
615 }
616
617 static int pick_formats(AVFilterGraph *graph)
618 {
619     int i, j, ret;
620
621     for (i = 0; i < graph->filter_count; i++) {
622         AVFilterContext *filter = graph->filters[i];
623
624         for (j = 0; j < filter->nb_inputs; j++)
625             if ((ret = pick_format(filter->inputs[j])) < 0)
626                 return ret;
627         for (j = 0; j < filter->nb_outputs; j++)
628             if ((ret = pick_format(filter->outputs[j])) < 0)
629                 return ret;
630     }
631     return 0;
632 }
633
634 /**
635  * Configure the formats of all the links in the graph.
636  */
637 static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
638 {
639     int ret;
640
641     /* find supported formats from sub-filters, and merge along links */
642     if ((ret = query_formats(graph, log_ctx)) < 0)
643         return ret;
644
645     /* Once everything is merged, it's possible that we'll still have
646      * multiple valid media format choices. We try to minimize the amount
647      * of format conversion inside filters */
648     reduce_formats(graph);
649
650     /* for audio filters, ensure the best format, sample rate and channel layout
651      * is selected */
652     swap_sample_fmts(graph);
653     swap_samplerates(graph);
654     swap_channel_layouts(graph);
655
656     if ((ret = pick_formats(graph)) < 0)
657         return ret;
658
659     return 0;
660 }
661
662 static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
663 {
664     AVFilterContext *f;
665     int i, j, ret;
666     int fifo_count = 0;
667
668     for (i = 0; i < graph->filter_count; i++) {
669         f = graph->filters[i];
670
671         for (j = 0; j < f->nb_inputs; j++) {
672             AVFilterLink *link = f->inputs[j];
673             AVFilterContext *fifo_ctx;
674             AVFilter *fifo;
675             char name[32];
676
677             if (!link->dstpad->needs_fifo)
678                 continue;
679
680             fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
681                    avfilter_get_by_name("fifo") :
682                    avfilter_get_by_name("afifo");
683
684             snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
685
686             ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
687                                                NULL, graph);
688             if (ret < 0)
689                 return ret;
690
691             ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
692             if (ret < 0)
693                 return ret;
694         }
695     }
696
697     return 0;
698 }
699
700 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
701 {
702     int ret;
703
704     if ((ret = graph_check_validity(graphctx, log_ctx)))
705         return ret;
706     if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
707         return ret;
708     if ((ret = graph_config_formats(graphctx, log_ctx)))
709         return ret;
710     if ((ret = graph_config_links(graphctx, log_ctx)))
711         return ret;
712
713     return 0;
714 }