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