0e276506f9d2543153150f4b85abc488c883d130
[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 "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(OutputStream *ost, AVFilterContext **last_filter, int *pad_idx)
177 {
178     OutputFile *of = output_files[ost->file_index];
179     AVFilterGraph *graph = (*last_filter)->graph;
180     AVFilterContext *ctx;
181     const AVFilter *trim;
182     const char *name = ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO ? "trim" : "atrim";
183     char filter_name[128];
184     int ret = 0;
185
186     if (of->recording_time == INT64_MAX && of->start_time == AV_NOPTS_VALUE)
187         return 0;
188
189     trim = avfilter_get_by_name(name);
190     if (!trim) {
191         av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
192                "recording time.\n", name);
193         return AVERROR_FILTER_NOT_FOUND;
194     }
195
196     snprintf(filter_name, sizeof(filter_name), "%s for output stream %d:%d",
197              name, ost->file_index, ost->index);
198     ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
199     if (!ctx)
200         return AVERROR(ENOMEM);
201
202     if (of->recording_time != INT64_MAX) {
203         ret = av_opt_set_double(ctx, "duration", (double)of->recording_time / 1e6,
204                                 AV_OPT_SEARCH_CHILDREN);
205     }
206     if (ret >= 0 && of->start_time != AV_NOPTS_VALUE) {
207         ret = av_opt_set_double(ctx, "start", (double)of->start_time / 1e6,
208                                 AV_OPT_SEARCH_CHILDREN);
209     }
210     if (ret < 0) {
211         av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
212         return ret;
213     }
214
215     ret = avfilter_init_str(ctx, NULL);
216     if (ret < 0)
217         return ret;
218
219     ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
220     if (ret < 0)
221         return ret;
222
223     *last_filter = ctx;
224     *pad_idx     = 0;
225     return 0;
226 }
227
228 static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
229 {
230     char *pix_fmts;
231     OutputStream *ost = ofilter->ost;
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     ret = insert_trim(ost, &last_filter, &pad_idx);
303     if (ret < 0)
304         return ret;
305
306
307     if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
308         return ret;
309
310     return 0;
311 }
312
313 static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
314 {
315     OutputStream *ost = ofilter->ost;
316     AVCodecContext *codec  = ost->st->codec;
317     AVFilterContext *last_filter = out->filter_ctx;
318     int pad_idx = out->pad_idx;
319     char *sample_fmts, *sample_rates, *channel_layouts;
320     char name[255];
321     int ret;
322
323
324     snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
325     ret = avfilter_graph_create_filter(&ofilter->filter,
326                                        avfilter_get_by_name("abuffersink"),
327                                        name, NULL, NULL, fg->graph);
328     if (ret < 0)
329         return ret;
330
331     if (codec->channels && !codec->channel_layout)
332         codec->channel_layout = av_get_default_channel_layout(codec->channels);
333
334     sample_fmts     = choose_sample_fmts(ost);
335     sample_rates    = choose_sample_rates(ost);
336     channel_layouts = choose_channel_layouts(ost);
337     if (sample_fmts || sample_rates || channel_layouts) {
338         AVFilterContext *format;
339         char args[256];
340         int len = 0;
341
342         if (sample_fmts)
343             len += snprintf(args + len, sizeof(args) - len, "sample_fmts=%s:",
344                             sample_fmts);
345         if (sample_rates)
346             len += snprintf(args + len, sizeof(args) - len, "sample_rates=%s:",
347                             sample_rates);
348         if (channel_layouts)
349             len += snprintf(args + len, sizeof(args) - len, "channel_layouts=%s:",
350                             channel_layouts);
351         args[len - 1] = 0;
352
353         av_freep(&sample_fmts);
354         av_freep(&sample_rates);
355         av_freep(&channel_layouts);
356
357         snprintf(name, sizeof(name), "audio format for output stream %d:%d",
358                  ost->file_index, ost->index);
359         ret = avfilter_graph_create_filter(&format,
360                                            avfilter_get_by_name("aformat"),
361                                            name, args, NULL, fg->graph);
362         if (ret < 0)
363             return ret;
364
365         ret = avfilter_link(last_filter, pad_idx, format, 0);
366         if (ret < 0)
367             return ret;
368
369         last_filter = format;
370         pad_idx = 0;
371     }
372
373     ret = insert_trim(ost, &last_filter, &pad_idx);
374     if (ret < 0)
375         return ret;
376
377     if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
378         return ret;
379
380     return 0;
381 }
382
383 #define DESCRIBE_FILTER_LINK(f, inout, in)                         \
384 {                                                                  \
385     AVFilterContext *ctx = inout->filter_ctx;                      \
386     AVFilterPad *pads = in ? ctx->input_pads  : ctx->output_pads;  \
387     int       nb_pads = in ? ctx->nb_inputs   : ctx->nb_outputs;   \
388     AVIOContext *pb;                                               \
389                                                                    \
390     if (avio_open_dyn_buf(&pb) < 0)                                \
391         exit(1);                                                   \
392                                                                    \
393     avio_printf(pb, "%s", ctx->filter->name);                      \
394     if (nb_pads > 1)                                               \
395         avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
396     avio_w8(pb, 0);                                                \
397     avio_close_dyn_buf(pb, &f->name);                              \
398 }
399
400 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
401 {
402     av_freep(&ofilter->name);
403     DESCRIBE_FILTER_LINK(ofilter, out, 0);
404
405     switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
406     case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
407     case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
408     default: av_assert0(0);
409     }
410 }
411
412 static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
413                                         AVFilterInOut *in)
414 {
415     AVFilterContext *last_filter;
416     const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
417     InputStream *ist = ifilter->ist;
418     AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
419                                          ist->st->time_base;
420     AVRational sar;
421     char args[255], name[255];
422     int ret;
423
424     sar = ist->st->sample_aspect_ratio.num ?
425           ist->st->sample_aspect_ratio :
426           ist->st->codec->sample_aspect_ratio;
427     snprintf(args, sizeof(args), "%d:%d:%d:%d:%d:%d:%d", ist->st->codec->width,
428              ist->st->codec->height, ist->st->codec->pix_fmt,
429              tb.num, tb.den, sar.num, sar.den);
430     snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
431              ist->file_index, ist->st->index);
432
433     if ((ret = avfilter_graph_create_filter(&ifilter->filter, buffer_filt, name,
434                                             args, NULL, fg->graph)) < 0)
435         return ret;
436     last_filter = ifilter->filter;
437
438     if (ist->framerate.num) {
439         AVFilterContext *setpts;
440
441         snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
442                  ist->file_index, ist->st->index);
443         if ((ret = avfilter_graph_create_filter(&setpts,
444                                                 avfilter_get_by_name("setpts"),
445                                                 name, "N", NULL,
446                                                 fg->graph)) < 0)
447             return ret;
448
449         if ((ret = avfilter_link(last_filter, 0, setpts, 0)) < 0)
450             return ret;
451
452         last_filter = setpts;
453     }
454
455     if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
456         return ret;
457     return 0;
458 }
459
460 static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
461                                         AVFilterInOut *in)
462 {
463     AVFilterContext *last_filter;
464     const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
465     InputStream *ist = ifilter->ist;
466     char args[255], name[255];
467     int ret;
468
469     snprintf(args, sizeof(args), "time_base=%d/%d:sample_rate=%d:sample_fmt=%s"
470              ":channel_layout=0x%"PRIx64,
471              1, ist->st->codec->sample_rate,
472              ist->st->codec->sample_rate,
473              av_get_sample_fmt_name(ist->st->codec->sample_fmt),
474              ist->st->codec->channel_layout);
475     snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
476              ist->file_index, ist->st->index);
477
478     if ((ret = avfilter_graph_create_filter(&ifilter->filter, abuffer_filt,
479                                             name, args, NULL,
480                                             fg->graph)) < 0)
481         return ret;
482     last_filter = ifilter->filter;
483
484     if (audio_sync_method > 0) {
485         AVFilterContext *async;
486         int  len = 0;
487
488         av_log(NULL, AV_LOG_WARNING, "-async has been deprecated. Used the "
489                "asyncts audio filter instead.\n");
490
491         if (audio_sync_method > 1)
492             len += snprintf(args + len, sizeof(args) - len, "compensate=1:"
493                             "max_comp=%d:", audio_sync_method);
494         snprintf(args + len, sizeof(args) - len, "min_delta=%f",
495                  audio_drift_threshold);
496
497         snprintf(name, sizeof(name), "graph %d audio sync for input stream %d:%d",
498                  fg->index, ist->file_index, ist->st->index);
499         ret = avfilter_graph_create_filter(&async,
500                                            avfilter_get_by_name("asyncts"),
501                                            name, args, NULL, fg->graph);
502         if (ret < 0)
503             return ret;
504
505         ret = avfilter_link(last_filter, 0, async, 0);
506         if (ret < 0)
507             return ret;
508
509         last_filter = async;
510     }
511     if (audio_volume != 256) {
512         AVFilterContext *volume;
513
514         av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
515                "audio filter instead.\n");
516
517         snprintf(args, sizeof(args), "volume=%f", audio_volume / 256.0);
518
519         snprintf(name, sizeof(name), "graph %d volume for input stream %d:%d",
520                  fg->index, ist->file_index, ist->st->index);
521         ret = avfilter_graph_create_filter(&volume,
522                                            avfilter_get_by_name("volume"),
523                                            name, args, NULL, fg->graph);
524         if (ret < 0)
525             return ret;
526
527         ret = avfilter_link(last_filter, 0, volume, 0);
528         if (ret < 0)
529             return ret;
530
531         last_filter = volume;
532     }
533     if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
534         return ret;
535
536     return 0;
537 }
538
539 static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
540                                   AVFilterInOut *in)
541 {
542     av_freep(&ifilter->name);
543     DESCRIBE_FILTER_LINK(ifilter, in, 1);
544
545     switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
546     case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
547     case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
548     default: av_assert0(0);
549     }
550 }
551
552 int configure_filtergraph(FilterGraph *fg)
553 {
554     AVFilterInOut *inputs, *outputs, *cur;
555     int ret, i, init = !fg->graph, simple = !fg->graph_desc;
556     const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
557                                       fg->graph_desc;
558
559     avfilter_graph_free(&fg->graph);
560     if (!(fg->graph = avfilter_graph_alloc()))
561         return AVERROR(ENOMEM);
562
563     if (simple) {
564         OutputStream *ost = fg->outputs[0]->ost;
565         char args[512];
566         AVDictionaryEntry *e = NULL;
567
568         snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
569         fg->graph->scale_sws_opts = av_strdup(args);
570
571         args[0] = '\0';
572         while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
573                                 AV_DICT_IGNORE_SUFFIX))) {
574             av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
575         }
576         if (strlen(args))
577             args[strlen(args) - 1] = '\0';
578         fg->graph->resample_lavr_opts = av_strdup(args);
579     }
580
581     if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
582         return ret;
583
584     if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
585         av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
586                "exactly one input and output.\n", graph_desc);
587         return AVERROR(EINVAL);
588     }
589
590     for (cur = inputs; !simple && init && cur; cur = cur->next)
591         init_input_filter(fg, cur);
592
593     for (cur = inputs, i = 0; cur; cur = cur->next, i++)
594         if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
595             return ret;
596     avfilter_inout_free(&inputs);
597
598     if (!init || simple) {
599         /* we already know the mappings between lavfi outputs and output streams,
600          * so we can finish the setup */
601         for (cur = outputs, i = 0; cur; cur = cur->next, i++)
602             configure_output_filter(fg, fg->outputs[i], cur);
603         avfilter_inout_free(&outputs);
604
605         if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
606             return ret;
607     } else {
608         /* wait until output mappings are processed */
609         for (cur = outputs; cur;) {
610             GROW_ARRAY(fg->outputs, fg->nb_outputs);
611             if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]))))
612                 exit(1);
613             fg->outputs[fg->nb_outputs - 1]->graph   = fg;
614             fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
615             cur = cur->next;
616             fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
617         }
618     }
619
620     return 0;
621 }
622
623 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
624 {
625     int i;
626     for (i = 0; i < fg->nb_inputs; i++)
627         if (fg->inputs[i]->ist == ist)
628             return 1;
629     return 0;
630 }
631