ffccd93eee5c0ac5727dd762c9bd0ed6ff584696
[ffmpeg.git] / avconv_filter.c
1 /*
2  * avconv filter configuration
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "avconv.h"
22
23 #include "libavfilter/avfilter.h"
24
25 #include "libavresample/avresample.h"
26
27 #include "libavutil/avassert.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/pixfmt.h"
33 #include "libavutil/samplefmt.h"
34
35 /* Define a function for building a string containing a list of
36  * allowed formats. */
37 #define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name)           \
38 static char *choose_ ## var ## s(OutputStream *ost)                            \
39 {                                                                              \
40     if (ost->st->codec->var != none) {                                         \
41         get_name(ost->st->codec->var);                                         \
42         return av_strdup(name);                                                \
43     } else if (ost->enc && ost->enc->supported_list) {                         \
44         const type *p;                                                         \
45         AVIOContext *s = NULL;                                                 \
46         uint8_t *ret;                                                          \
47         int len;                                                               \
48                                                                                \
49         if (avio_open_dyn_buf(&s) < 0)                                         \
50             exit(1);                                                           \
51                                                                                \
52         for (p = ost->enc->supported_list; *p != none; p++) {                  \
53             get_name(*p);                                                      \
54             avio_printf(s, "%s|", name);                                       \
55         }                                                                      \
56         len = avio_close_dyn_buf(s, &ret);                                     \
57         ret[len - 1] = 0;                                                      \
58         return ret;                                                            \
59     } else                                                                     \
60         return NULL;                                                           \
61 }
62
63 DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE,
64                   GET_PIX_FMT_NAME)
65
66 DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts,
67                   AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME)
68
69 DEF_CHOOSE_FORMAT(int, sample_rate, supported_samplerates, 0,
70                   GET_SAMPLE_RATE_NAME)
71
72 DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0,
73                   GET_CH_LAYOUT_NAME)
74
75 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost)
76 {
77     FilterGraph *fg = av_mallocz(sizeof(*fg));
78
79     if (!fg)
80         exit(1);
81     fg->index = nb_filtergraphs;
82
83     GROW_ARRAY(fg->outputs, fg->nb_outputs);
84     if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
85         exit(1);
86     fg->outputs[0]->ost   = ost;
87     fg->outputs[0]->graph = fg;
88
89     ost->filter = fg->outputs[0];
90
91     GROW_ARRAY(fg->inputs, fg->nb_inputs);
92     if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
93         exit(1);
94     fg->inputs[0]->ist   = ist;
95     fg->inputs[0]->graph = fg;
96
97     GROW_ARRAY(ist->filters, ist->nb_filters);
98     ist->filters[ist->nb_filters - 1] = fg->inputs[0];
99
100     GROW_ARRAY(filtergraphs, nb_filtergraphs);
101     filtergraphs[nb_filtergraphs - 1] = fg;
102
103     return fg;
104 }
105
106 static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
107 {
108     InputStream *ist = NULL;
109     enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
110     int i;
111
112     // TODO: support other filter types
113     if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
114         av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
115                "currently.\n");
116         exit(1);
117     }
118
119     if (in->name) {
120         AVFormatContext *s;
121         AVStream       *st = NULL;
122         char *p;
123         int file_idx = strtol(in->name, &p, 0);
124
125         if (file_idx < 0 || file_idx >= nb_input_files) {
126             av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtegraph description %s.\n",
127                    file_idx, fg->graph_desc);
128             exit(1);
129         }
130         s = input_files[file_idx]->ctx;
131
132         for (i = 0; i < s->nb_streams; i++) {
133             if (s->streams[i]->codec->codec_type != type)
134                 continue;
135             if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
136                 st = s->streams[i];
137                 break;
138             }
139         }
140         if (!st) {
141             av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
142                    "matches no streams.\n", p, fg->graph_desc);
143             exit(1);
144         }
145         ist = input_streams[input_files[file_idx]->ist_index + st->index];
146     } else {
147         /* find the first unused stream of corresponding type */
148         for (i = 0; i < nb_input_streams; i++) {
149             ist = input_streams[i];
150             if (ist->st->codec->codec_type == type && ist->discard)
151                 break;
152         }
153         if (i == nb_input_streams) {
154             av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
155                    "unlabeled input pad %d on filter %s\n", in->pad_idx,
156                    in->filter_ctx->name);
157             exit(1);
158         }
159     }
160     av_assert0(ist);
161
162     ist->discard         = 0;
163     ist->decoding_needed = 1;
164     ist->st->discard = AVDISCARD_NONE;
165
166     GROW_ARRAY(fg->inputs, fg->nb_inputs);
167     if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
168         exit(1);
169     fg->inputs[fg->nb_inputs - 1]->ist   = ist;
170     fg->inputs[fg->nb_inputs - 1]->graph = fg;
171
172     GROW_ARRAY(ist->filters, ist->nb_filters);
173     ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
174 }
175
176 static int insert_trim(int64_t start_time, int64_t duration,
177                        AVFilterContext **last_filter, int *pad_idx,
178                        const char *filter_name)
179 {
180     AVFilterGraph *graph = (*last_filter)->graph;
181     AVFilterContext *ctx;
182     const AVFilter *trim;
183     enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
184     const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
185     int ret = 0;
186
187     if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
188         return 0;
189
190     trim = avfilter_get_by_name(name);
191     if (!trim) {
192         av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
193                "recording time.\n", name);
194         return AVERROR_FILTER_NOT_FOUND;
195     }
196
197     ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
198     if (!ctx)
199         return AVERROR(ENOMEM);
200
201     if (duration != INT64_MAX) {
202         ret = av_opt_set_double(ctx, "duration", (double)duration / 1e6,
203                                 AV_OPT_SEARCH_CHILDREN);
204     }
205     if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
206         ret = av_opt_set_double(ctx, "start", (double)start_time / 1e6,
207                                 AV_OPT_SEARCH_CHILDREN);
208     }
209     if (ret < 0) {
210         av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
211         return ret;
212     }
213
214     ret = avfilter_init_str(ctx, NULL);
215     if (ret < 0)
216         return ret;
217
218     ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
219     if (ret < 0)
220         return ret;
221
222     *last_filter = ctx;
223     *pad_idx     = 0;
224     return 0;
225 }
226
227 static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
228 {
229     char *pix_fmts;
230     OutputStream *ost = ofilter->ost;
231     OutputFile    *of = output_files[ost->file_index];
232     AVCodecContext *codec = ost->st->codec;
233     AVFilterContext *last_filter = out->filter_ctx;
234     int pad_idx = out->pad_idx;
235     int ret;
236     char name[255];
237
238     snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
239     ret = avfilter_graph_create_filter(&ofilter->filter,
240                                        avfilter_get_by_name("buffersink"),
241                                        name, NULL, NULL, fg->graph);
242     if (ret < 0)
243         return ret;
244
245     if (codec->width || codec->height) {
246         char args[255];
247         AVFilterContext *filter;
248
249         snprintf(args, sizeof(args), "%d:%d:0x%X",
250                  codec->width,
251                  codec->height,
252                  (unsigned)ost->sws_flags);
253         snprintf(name, sizeof(name), "scaler for output stream %d:%d",
254                  ost->file_index, ost->index);
255         if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
256                                                 name, args, NULL, fg->graph)) < 0)
257             return ret;
258         if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
259             return ret;
260
261         last_filter = filter;
262         pad_idx = 0;
263     }
264
265     if ((pix_fmts = choose_pix_fmts(ost))) {
266         AVFilterContext *filter;
267         snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
268                  ost->file_index, ost->index);
269         if ((ret = avfilter_graph_create_filter(&filter,
270                                                 avfilter_get_by_name("format"),
271                                                 "format", pix_fmts, NULL,
272                                                 fg->graph)) < 0)
273             return ret;
274         if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
275             return ret;
276
277         last_filter = filter;
278         pad_idx     = 0;
279         av_freep(&pix_fmts);
280     }
281
282     if (ost->frame_rate.num) {
283         AVFilterContext *fps;
284         char args[255];
285
286         snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
287                  ost->frame_rate.den);
288         snprintf(name, sizeof(name), "fps for output stream %d:%d",
289                  ost->file_index, ost->index);
290         ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
291                                            name, args, NULL, fg->graph);
292         if (ret < 0)
293             return ret;
294
295         ret = avfilter_link(last_filter, pad_idx, fps, 0);
296         if (ret < 0)
297             return ret;
298         last_filter = fps;
299         pad_idx = 0;
300     }
301
302     snprintf(name, sizeof(name), "trim for output stream %d:%d",
303              ost->file_index, ost->index);
304     ret = insert_trim(of->start_time, of->recording_time,
305                       &last_filter, &pad_idx, name);
306     if (ret < 0)
307         return ret;
308
309
310     if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
311         return ret;
312
313     return 0;
314 }
315
316 static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
317 {
318     OutputStream *ost = ofilter->ost;
319     OutputFile    *of = output_files[ost->file_index];
320     AVCodecContext *codec  = ost->st->codec;
321     AVFilterContext *last_filter = out->filter_ctx;
322     int pad_idx = out->pad_idx;
323     char *sample_fmts, *sample_rates, *channel_layouts;
324     char name[255];
325     int ret;
326
327
328     snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
329     ret = avfilter_graph_create_filter(&ofilter->filter,
330                                        avfilter_get_by_name("abuffersink"),
331                                        name, NULL, NULL, fg->graph);
332     if (ret < 0)
333         return ret;
334
335     if (codec->channels && !codec->channel_layout)
336         codec->channel_layout = av_get_default_channel_layout(codec->channels);
337
338     sample_fmts     = choose_sample_fmts(ost);
339     sample_rates    = choose_sample_rates(ost);
340     channel_layouts = choose_channel_layouts(ost);
341     if (sample_fmts || sample_rates || channel_layouts) {
342         AVFilterContext *format;
343         char args[256];
344         int len = 0;
345
346         if (sample_fmts)
347             len += snprintf(args + len, sizeof(args) - len, "sample_fmts=%s:",
348                             sample_fmts);
349         if (sample_rates)
350             len += snprintf(args + len, sizeof(args) - len, "sample_rates=%s:",
351                             sample_rates);
352         if (channel_layouts)
353             len += snprintf(args + len, sizeof(args) - len, "channel_layouts=%s:",
354                             channel_layouts);
355         args[len - 1] = 0;
356
357         av_freep(&sample_fmts);
358         av_freep(&sample_rates);
359         av_freep(&channel_layouts);
360
361         snprintf(name, sizeof(name), "audio format for output stream %d:%d",
362                  ost->file_index, ost->index);
363         ret = avfilter_graph_create_filter(&format,
364                                            avfilter_get_by_name("aformat"),
365                                            name, args, NULL, fg->graph);
366         if (ret < 0)
367             return ret;
368
369         ret = avfilter_link(last_filter, pad_idx, format, 0);
370         if (ret < 0)
371             return ret;
372
373         last_filter = format;
374         pad_idx = 0;
375     }
376
377     snprintf(name, sizeof(name), "trim for output stream %d:%d",
378              ost->file_index, ost->index);
379     ret = insert_trim(of->start_time, of->recording_time,
380                       &last_filter, &pad_idx, name);
381     if (ret < 0)
382         return ret;
383
384     if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
385         return ret;
386
387     return 0;
388 }
389
390 #define DESCRIBE_FILTER_LINK(f, inout, in)                         \
391 {                                                                  \
392     AVFilterContext *ctx = inout->filter_ctx;                      \
393     AVFilterPad *pads = in ? ctx->input_pads  : ctx->output_pads;  \
394     int       nb_pads = in ? ctx->nb_inputs   : ctx->nb_outputs;   \
395     AVIOContext *pb;                                               \
396                                                                    \
397     if (avio_open_dyn_buf(&pb) < 0)                                \
398         exit(1);                                                   \
399                                                                    \
400     avio_printf(pb, "%s", ctx->filter->name);                      \
401     if (nb_pads > 1)                                               \
402         avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
403     avio_w8(pb, 0);                                                \
404     avio_close_dyn_buf(pb, &f->name);                              \
405 }
406
407 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
408 {
409     av_freep(&ofilter->name);
410     DESCRIBE_FILTER_LINK(ofilter, out, 0);
411
412     switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
413     case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
414     case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
415     default: av_assert0(0);
416     }
417 }
418
419 static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
420                                         AVFilterInOut *in)
421 {
422     AVFilterContext *last_filter;
423     const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
424     InputStream *ist = ifilter->ist;
425     InputFile     *f = input_files[ist->file_index];
426     AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
427                                          ist->st->time_base;
428     AVRational sar;
429     char args[255], name[255];
430     int ret, pad_idx = 0;
431
432     sar = ist->st->sample_aspect_ratio.num ?
433           ist->st->sample_aspect_ratio :
434           ist->st->codec->sample_aspect_ratio;
435     snprintf(args, sizeof(args), "%d:%d:%d:%d:%d:%d:%d", ist->st->codec->width,
436              ist->st->codec->height,
437              ist->hwaccel_retrieve_data ? ist->hwaccel_retrieved_pix_fmt : ist->st->codec->pix_fmt,
438              tb.num, tb.den, sar.num, sar.den);
439     snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
440              ist->file_index, ist->st->index);
441
442     if ((ret = avfilter_graph_create_filter(&ifilter->filter, buffer_filt, name,
443                                             args, NULL, fg->graph)) < 0)
444         return ret;
445     last_filter = ifilter->filter;
446
447     if (ist->framerate.num) {
448         AVFilterContext *setpts;
449
450         snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
451                  ist->file_index, ist->st->index);
452         if ((ret = avfilter_graph_create_filter(&setpts,
453                                                 avfilter_get_by_name("setpts"),
454                                                 name, "N", NULL,
455                                                 fg->graph)) < 0)
456             return ret;
457
458         if ((ret = avfilter_link(last_filter, 0, setpts, 0)) < 0)
459             return ret;
460
461         last_filter = setpts;
462     }
463
464     snprintf(name, sizeof(name), "trim for input stream %d:%d",
465              ist->file_index, ist->st->index);
466     ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
467                       AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name);
468     if (ret < 0)
469         return ret;
470
471     if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
472         return ret;
473     return 0;
474 }
475
476 static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
477                                         AVFilterInOut *in)
478 {
479     AVFilterContext *last_filter;
480     const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
481     InputStream *ist = ifilter->ist;
482     InputFile     *f = input_files[ist->file_index];
483     char args[255], name[255];
484     int ret, pad_idx = 0;
485
486     snprintf(args, sizeof(args), "time_base=%d/%d:sample_rate=%d:sample_fmt=%s"
487              ":channel_layout=0x%"PRIx64,
488              1, ist->st->codec->sample_rate,
489              ist->st->codec->sample_rate,
490              av_get_sample_fmt_name(ist->st->codec->sample_fmt),
491              ist->st->codec->channel_layout);
492     snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
493              ist->file_index, ist->st->index);
494
495     if ((ret = avfilter_graph_create_filter(&ifilter->filter, abuffer_filt,
496                                             name, args, NULL,
497                                             fg->graph)) < 0)
498         return ret;
499     last_filter = ifilter->filter;
500
501     if (audio_sync_method > 0) {
502         AVFilterContext *async;
503         int  len = 0;
504
505         av_log(NULL, AV_LOG_WARNING, "-async has been deprecated. Used the "
506                "asyncts audio filter instead.\n");
507
508         if (audio_sync_method > 1)
509             len += snprintf(args + len, sizeof(args) - len, "compensate=1:"
510                             "max_comp=%d:", audio_sync_method);
511         snprintf(args + len, sizeof(args) - len, "min_delta=%f",
512                  audio_drift_threshold);
513
514         snprintf(name, sizeof(name), "graph %d audio sync for input stream %d:%d",
515                  fg->index, ist->file_index, ist->st->index);
516         ret = avfilter_graph_create_filter(&async,
517                                            avfilter_get_by_name("asyncts"),
518                                            name, args, NULL, fg->graph);
519         if (ret < 0)
520             return ret;
521
522         ret = avfilter_link(last_filter, 0, async, 0);
523         if (ret < 0)
524             return ret;
525
526         last_filter = async;
527     }
528     if (audio_volume != 256) {
529         AVFilterContext *volume;
530
531         av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
532                "audio filter instead.\n");
533
534         snprintf(args, sizeof(args), "volume=%f", audio_volume / 256.0);
535
536         snprintf(name, sizeof(name), "graph %d volume for input stream %d:%d",
537                  fg->index, ist->file_index, ist->st->index);
538         ret = avfilter_graph_create_filter(&volume,
539                                            avfilter_get_by_name("volume"),
540                                            name, args, NULL, fg->graph);
541         if (ret < 0)
542             return ret;
543
544         ret = avfilter_link(last_filter, 0, volume, 0);
545         if (ret < 0)
546             return ret;
547
548         last_filter = volume;
549     }
550
551     snprintf(name, sizeof(name), "trim for input stream %d:%d",
552              ist->file_index, ist->st->index);
553     ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
554                       AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name);
555     if (ret < 0)
556         return ret;
557
558     if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
559         return ret;
560
561     return 0;
562 }
563
564 static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
565                                   AVFilterInOut *in)
566 {
567     av_freep(&ifilter->name);
568     DESCRIBE_FILTER_LINK(ifilter, in, 1);
569
570     switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
571     case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
572     case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
573     default: av_assert0(0);
574     }
575 }
576
577 int configure_filtergraph(FilterGraph *fg)
578 {
579     AVFilterInOut *inputs, *outputs, *cur;
580     int ret, i, init = !fg->graph, simple = !fg->graph_desc;
581     const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
582                                       fg->graph_desc;
583
584     avfilter_graph_free(&fg->graph);
585     if (!(fg->graph = avfilter_graph_alloc()))
586         return AVERROR(ENOMEM);
587
588     if (simple) {
589         OutputStream *ost = fg->outputs[0]->ost;
590         char args[512];
591         AVDictionaryEntry *e = NULL;
592
593         snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
594         fg->graph->scale_sws_opts = av_strdup(args);
595
596         args[0] = '\0';
597         while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
598                                 AV_DICT_IGNORE_SUFFIX))) {
599             av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
600         }
601         if (strlen(args))
602             args[strlen(args) - 1] = '\0';
603         fg->graph->resample_lavr_opts = av_strdup(args);
604     }
605
606     if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
607         return ret;
608
609     if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
610         av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
611                "exactly one input and output.\n", graph_desc);
612         return AVERROR(EINVAL);
613     }
614
615     for (cur = inputs; !simple && init && cur; cur = cur->next)
616         init_input_filter(fg, cur);
617
618     for (cur = inputs, i = 0; cur; cur = cur->next, i++)
619         if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
620             return ret;
621     avfilter_inout_free(&inputs);
622
623     if (!init || simple) {
624         /* we already know the mappings between lavfi outputs and output streams,
625          * so we can finish the setup */
626         for (cur = outputs, i = 0; cur; cur = cur->next, i++)
627             configure_output_filter(fg, fg->outputs[i], cur);
628         avfilter_inout_free(&outputs);
629
630         if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
631             return ret;
632     } else {
633         /* wait until output mappings are processed */
634         for (cur = outputs; cur;) {
635             GROW_ARRAY(fg->outputs, fg->nb_outputs);
636             if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]))))
637                 exit(1);
638             fg->outputs[fg->nb_outputs - 1]->graph   = fg;
639             fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
640             cur = cur->next;
641             fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
642         }
643     }
644
645     return 0;
646 }
647
648 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
649 {
650     int i;
651     for (i = 0; i < fg->nb_inputs; i++)
652         if (fg->inputs[i]->ist == ist)
653             return 1;
654     return 0;
655 }
656