avconv: make input -ss accurate when transcoding
[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", 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, ist->st->codec->pix_fmt,
437              tb.num, tb.den, sar.num, sar.den);
438     snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
439              ist->file_index, ist->st->index);
440
441     if ((ret = avfilter_graph_create_filter(&ifilter->filter, buffer_filt, name,
442                                             args, NULL, fg->graph)) < 0)
443         return ret;
444     last_filter = ifilter->filter;
445
446     if (ist->framerate.num) {
447         AVFilterContext *setpts;
448
449         snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
450                  ist->file_index, ist->st->index);
451         if ((ret = avfilter_graph_create_filter(&setpts,
452                                                 avfilter_get_by_name("setpts"),
453                                                 name, "N", NULL,
454                                                 fg->graph)) < 0)
455             return ret;
456
457         if ((ret = avfilter_link(last_filter, 0, setpts, 0)) < 0)
458             return ret;
459
460         last_filter = setpts;
461     }
462
463     snprintf(name, sizeof(name), "trim for input stream %d:%d",
464              ist->file_index, ist->st->index);
465     ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
466                       AV_NOPTS_VALUE : 0, INT64_MAX, &last_filter, &pad_idx, name);
467     if (ret < 0)
468         return ret;
469
470     if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
471         return ret;
472     return 0;
473 }
474
475 static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
476                                         AVFilterInOut *in)
477 {
478     AVFilterContext *last_filter;
479     const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
480     InputStream *ist = ifilter->ist;
481     InputFile     *f = input_files[ist->file_index];
482     char args[255], name[255];
483     int ret, pad_idx = 0;
484
485     snprintf(args, sizeof(args), "time_base=%d/%d:sample_rate=%d:sample_fmt=%s"
486              ":channel_layout=0x%"PRIx64,
487              1, ist->st->codec->sample_rate,
488              ist->st->codec->sample_rate,
489              av_get_sample_fmt_name(ist->st->codec->sample_fmt),
490              ist->st->codec->channel_layout);
491     snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
492              ist->file_index, ist->st->index);
493
494     if ((ret = avfilter_graph_create_filter(&ifilter->filter, abuffer_filt,
495                                             name, args, NULL,
496                                             fg->graph)) < 0)
497         return ret;
498     last_filter = ifilter->filter;
499
500     if (audio_sync_method > 0) {
501         AVFilterContext *async;
502         int  len = 0;
503
504         av_log(NULL, AV_LOG_WARNING, "-async has been deprecated. Used the "
505                "asyncts audio filter instead.\n");
506
507         if (audio_sync_method > 1)
508             len += snprintf(args + len, sizeof(args) - len, "compensate=1:"
509                             "max_comp=%d:", audio_sync_method);
510         snprintf(args + len, sizeof(args) - len, "min_delta=%f",
511                  audio_drift_threshold);
512
513         snprintf(name, sizeof(name), "graph %d audio sync for input stream %d:%d",
514                  fg->index, ist->file_index, ist->st->index);
515         ret = avfilter_graph_create_filter(&async,
516                                            avfilter_get_by_name("asyncts"),
517                                            name, args, NULL, fg->graph);
518         if (ret < 0)
519             return ret;
520
521         ret = avfilter_link(last_filter, 0, async, 0);
522         if (ret < 0)
523             return ret;
524
525         last_filter = async;
526     }
527     if (audio_volume != 256) {
528         AVFilterContext *volume;
529
530         av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
531                "audio filter instead.\n");
532
533         snprintf(args, sizeof(args), "volume=%f", audio_volume / 256.0);
534
535         snprintf(name, sizeof(name), "graph %d volume for input stream %d:%d",
536                  fg->index, ist->file_index, ist->st->index);
537         ret = avfilter_graph_create_filter(&volume,
538                                            avfilter_get_by_name("volume"),
539                                            name, args, NULL, fg->graph);
540         if (ret < 0)
541             return ret;
542
543         ret = avfilter_link(last_filter, 0, volume, 0);
544         if (ret < 0)
545             return ret;
546
547         last_filter = volume;
548     }
549
550     snprintf(name, sizeof(name), "trim for input stream %d:%d",
551              ist->file_index, ist->st->index);
552     ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
553                       AV_NOPTS_VALUE : 0, INT64_MAX, &last_filter, &pad_idx, name);
554     if (ret < 0)
555         return ret;
556
557     if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
558         return ret;
559
560     return 0;
561 }
562
563 static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
564                                   AVFilterInOut *in)
565 {
566     av_freep(&ifilter->name);
567     DESCRIBE_FILTER_LINK(ifilter, in, 1);
568
569     switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
570     case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
571     case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
572     default: av_assert0(0);
573     }
574 }
575
576 int configure_filtergraph(FilterGraph *fg)
577 {
578     AVFilterInOut *inputs, *outputs, *cur;
579     int ret, i, init = !fg->graph, simple = !fg->graph_desc;
580     const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
581                                       fg->graph_desc;
582
583     avfilter_graph_free(&fg->graph);
584     if (!(fg->graph = avfilter_graph_alloc()))
585         return AVERROR(ENOMEM);
586
587     if (simple) {
588         OutputStream *ost = fg->outputs[0]->ost;
589         char args[512];
590         AVDictionaryEntry *e = NULL;
591
592         snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
593         fg->graph->scale_sws_opts = av_strdup(args);
594
595         args[0] = '\0';
596         while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
597                                 AV_DICT_IGNORE_SUFFIX))) {
598             av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
599         }
600         if (strlen(args))
601             args[strlen(args) - 1] = '\0';
602         fg->graph->resample_lavr_opts = av_strdup(args);
603     }
604
605     if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
606         return ret;
607
608     if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
609         av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
610                "exactly one input and output.\n", graph_desc);
611         return AVERROR(EINVAL);
612     }
613
614     for (cur = inputs; !simple && init && cur; cur = cur->next)
615         init_input_filter(fg, cur);
616
617     for (cur = inputs, i = 0; cur; cur = cur->next, i++)
618         if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
619             return ret;
620     avfilter_inout_free(&inputs);
621
622     if (!init || simple) {
623         /* we already know the mappings between lavfi outputs and output streams,
624          * so we can finish the setup */
625         for (cur = outputs, i = 0; cur; cur = cur->next, i++)
626             configure_output_filter(fg, fg->outputs[i], cur);
627         avfilter_inout_free(&outputs);
628
629         if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
630             return ret;
631     } else {
632         /* wait until output mappings are processed */
633         for (cur = outputs; cur;) {
634             GROW_ARRAY(fg->outputs, fg->nb_outputs);
635             if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]))))
636                 exit(1);
637             fg->outputs[fg->nb_outputs - 1]->graph   = fg;
638             fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
639             cur = cur->next;
640             fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
641         }
642     }
643
644     return 0;
645 }
646
647 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
648 {
649     int i;
650     for (i = 0; i < fg->nb_inputs; i++)
651         if (fg->inputs[i]->ist == ist)
652             return 1;
653     return 0;
654 }
655