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