avformat/flvenc: add add_keyframe_index option
[ffmpeg.git] / ffmpeg_filter.c
1 /*
2  * ffmpeg filter configuration
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "ffmpeg.h"
24
25 #include "libavfilter/avfilter.h"
26 #include "libavfilter/buffersink.h"
27 #include "libavfilter/buffersrc.h"
28
29 #include "libavresample/avresample.h"
30
31 #include "libavutil/avassert.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/bprint.h"
34 #include "libavutil/channel_layout.h"
35 #include "libavutil/display.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/pixdesc.h"
38 #include "libavutil/pixfmt.h"
39 #include "libavutil/imgutils.h"
40 #include "libavutil/samplefmt.h"
41
42 int filter_nbthreads = -1;
43 int filter_complex_nbthreads = -1;
44
45 static const enum AVPixelFormat *get_compliance_unofficial_pix_fmts(enum AVCodecID codec_id, const enum AVPixelFormat default_formats[])
46 {
47     static const enum AVPixelFormat mjpeg_formats[] =
48         { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
49           AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV444P,
50           AV_PIX_FMT_NONE };
51     static const enum AVPixelFormat ljpeg_formats[] =
52         { AV_PIX_FMT_BGR24   , AV_PIX_FMT_BGRA    , AV_PIX_FMT_BGR0,
53           AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
54           AV_PIX_FMT_YUV420P , AV_PIX_FMT_YUV444P , AV_PIX_FMT_YUV422P,
55           AV_PIX_FMT_NONE};
56
57     if (codec_id == AV_CODEC_ID_MJPEG) {
58         return mjpeg_formats;
59     } else if (codec_id == AV_CODEC_ID_LJPEG) {
60         return ljpeg_formats;
61     } else {
62         return default_formats;
63     }
64 }
65
66 enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodecContext *enc_ctx, AVCodec *codec, enum AVPixelFormat target)
67 {
68     if (codec && codec->pix_fmts) {
69         const enum AVPixelFormat *p = codec->pix_fmts;
70         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target);
71         int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
72         enum AVPixelFormat best= AV_PIX_FMT_NONE;
73
74         if (enc_ctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
75             p = get_compliance_unofficial_pix_fmts(enc_ctx->codec_id, p);
76         }
77         for (; *p != AV_PIX_FMT_NONE; p++) {
78             best= avcodec_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
79             if (*p == target)
80                 break;
81         }
82         if (*p == AV_PIX_FMT_NONE) {
83             if (target != AV_PIX_FMT_NONE)
84                 av_log(NULL, AV_LOG_WARNING,
85                        "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
86                        av_get_pix_fmt_name(target),
87                        codec->name,
88                        av_get_pix_fmt_name(best));
89             return best;
90         }
91     }
92     return target;
93 }
94
95 void choose_sample_fmt(AVStream *st, AVCodec *codec)
96 {
97     if (codec && codec->sample_fmts) {
98         const enum AVSampleFormat *p = codec->sample_fmts;
99         for (; *p != -1; p++) {
100             if (*p == st->codecpar->format)
101                 break;
102         }
103         if (*p == -1) {
104             if((codec->capabilities & AV_CODEC_CAP_LOSSLESS) && av_get_sample_fmt_name(st->codecpar->format) > av_get_sample_fmt_name(codec->sample_fmts[0]))
105                 av_log(NULL, AV_LOG_ERROR, "Conversion will not be lossless.\n");
106             if(av_get_sample_fmt_name(st->codecpar->format))
107             av_log(NULL, AV_LOG_WARNING,
108                    "Incompatible sample format '%s' for codec '%s', auto-selecting format '%s'\n",
109                    av_get_sample_fmt_name(st->codecpar->format),
110                    codec->name,
111                    av_get_sample_fmt_name(codec->sample_fmts[0]));
112             st->codecpar->format = codec->sample_fmts[0];
113         }
114     }
115 }
116
117 static char *choose_pix_fmts(OutputStream *ost)
118 {
119     AVDictionaryEntry *strict_dict = av_dict_get(ost->encoder_opts, "strict", NULL, 0);
120     if (strict_dict)
121         // used by choose_pixel_fmt() and below
122         av_opt_set(ost->enc_ctx, "strict", strict_dict->value, 0);
123
124      if (ost->keep_pix_fmt) {
125         if (ost->filter)
126             avfilter_graph_set_auto_convert(ost->filter->graph->graph,
127                                             AVFILTER_AUTO_CONVERT_NONE);
128         if (ost->enc_ctx->pix_fmt == AV_PIX_FMT_NONE)
129             return NULL;
130         return av_strdup(av_get_pix_fmt_name(ost->enc_ctx->pix_fmt));
131     }
132     if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
133         return av_strdup(av_get_pix_fmt_name(choose_pixel_fmt(ost->st, ost->enc_ctx, ost->enc, ost->enc_ctx->pix_fmt)));
134     } else if (ost->enc && ost->enc->pix_fmts) {
135         const enum AVPixelFormat *p;
136         AVIOContext *s = NULL;
137         uint8_t *ret;
138         int len;
139
140         if (avio_open_dyn_buf(&s) < 0)
141             exit_program(1);
142
143         p = ost->enc->pix_fmts;
144         if (ost->enc_ctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
145             p = get_compliance_unofficial_pix_fmts(ost->enc_ctx->codec_id, p);
146         }
147
148         for (; *p != AV_PIX_FMT_NONE; p++) {
149             const char *name = av_get_pix_fmt_name(*p);
150             avio_printf(s, "%s|", name);
151         }
152         len = avio_close_dyn_buf(s, &ret);
153         ret[len - 1] = 0;
154         return ret;
155     } else
156         return NULL;
157 }
158
159 /* Define a function for building a string containing a list of
160  * allowed formats. */
161 #define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name)           \
162 static char *choose_ ## var ## s(OutputStream *ost)                            \
163 {                                                                              \
164     if (ost->enc_ctx->var != none) {                                           \
165         get_name(ost->enc_ctx->var);                                           \
166         return av_strdup(name);                                                \
167     } else if (ost->enc && ost->enc->supported_list) {                         \
168         const type *p;                                                         \
169         AVIOContext *s = NULL;                                                 \
170         uint8_t *ret;                                                          \
171         int len;                                                               \
172                                                                                \
173         if (avio_open_dyn_buf(&s) < 0)                                         \
174             exit_program(1);                                                           \
175                                                                                \
176         for (p = ost->enc->supported_list; *p != none; p++) {                  \
177             get_name(*p);                                                      \
178             avio_printf(s, "%s|", name);                                       \
179         }                                                                      \
180         len = avio_close_dyn_buf(s, &ret);                                     \
181         ret[len - 1] = 0;                                                      \
182         return ret;                                                            \
183     } else                                                                     \
184         return NULL;                                                           \
185 }
186
187 // DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE,
188 //                   GET_PIX_FMT_NAME)
189
190 DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts,
191                   AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME)
192
193 DEF_CHOOSE_FORMAT(int, sample_rate, supported_samplerates, 0,
194                   GET_SAMPLE_RATE_NAME)
195
196 DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0,
197                   GET_CH_LAYOUT_NAME)
198
199 int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
200 {
201     FilterGraph *fg = av_mallocz(sizeof(*fg));
202
203     if (!fg)
204         exit_program(1);
205     fg->index = nb_filtergraphs;
206
207     GROW_ARRAY(fg->outputs, fg->nb_outputs);
208     if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
209         exit_program(1);
210     fg->outputs[0]->ost   = ost;
211     fg->outputs[0]->graph = fg;
212
213     ost->filter = fg->outputs[0];
214
215     GROW_ARRAY(fg->inputs, fg->nb_inputs);
216     if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
217         exit_program(1);
218     fg->inputs[0]->ist   = ist;
219     fg->inputs[0]->graph = fg;
220     fg->inputs[0]->format = -1;
221
222     GROW_ARRAY(ist->filters, ist->nb_filters);
223     ist->filters[ist->nb_filters - 1] = fg->inputs[0];
224
225     GROW_ARRAY(filtergraphs, nb_filtergraphs);
226     filtergraphs[nb_filtergraphs - 1] = fg;
227
228     return 0;
229 }
230
231 static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
232 {
233     InputStream *ist = NULL;
234     enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
235     int i;
236
237     // TODO: support other filter types
238     if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
239         av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
240                "currently.\n");
241         exit_program(1);
242     }
243
244     if (in->name) {
245         AVFormatContext *s;
246         AVStream       *st = NULL;
247         char *p;
248         int file_idx = strtol(in->name, &p, 0);
249
250         if (file_idx < 0 || file_idx >= nb_input_files) {
251             av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
252                    file_idx, fg->graph_desc);
253             exit_program(1);
254         }
255         s = input_files[file_idx]->ctx;
256
257         for (i = 0; i < s->nb_streams; i++) {
258             enum AVMediaType stream_type = s->streams[i]->codecpar->codec_type;
259             if (stream_type != type &&
260                 !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
261                   type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
262                 continue;
263             if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
264                 st = s->streams[i];
265                 break;
266             }
267         }
268         if (!st) {
269             av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
270                    "matches no streams.\n", p, fg->graph_desc);
271             exit_program(1);
272         }
273         ist = input_streams[input_files[file_idx]->ist_index + st->index];
274     } else {
275         /* find the first unused stream of corresponding type */
276         for (i = 0; i < nb_input_streams; i++) {
277             ist = input_streams[i];
278             if (ist->dec_ctx->codec_type == type && ist->discard)
279                 break;
280         }
281         if (i == nb_input_streams) {
282             av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
283                    "unlabeled input pad %d on filter %s\n", in->pad_idx,
284                    in->filter_ctx->name);
285             exit_program(1);
286         }
287     }
288     av_assert0(ist);
289
290     ist->discard         = 0;
291     ist->decoding_needed |= DECODING_FOR_FILTER;
292     ist->st->discard = AVDISCARD_NONE;
293
294     GROW_ARRAY(fg->inputs, fg->nb_inputs);
295     if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
296         exit_program(1);
297     fg->inputs[fg->nb_inputs - 1]->ist   = ist;
298     fg->inputs[fg->nb_inputs - 1]->graph = fg;
299     fg->inputs[fg->nb_inputs - 1]->format = -1;
300
301     GROW_ARRAY(ist->filters, ist->nb_filters);
302     ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
303 }
304
305 int init_complex_filtergraph(FilterGraph *fg)
306 {
307     AVFilterInOut *inputs, *outputs, *cur;
308     AVFilterGraph *graph;
309     int ret = 0;
310
311     /* this graph is only used for determining the kinds of inputs
312      * and outputs we have, and is discarded on exit from this function */
313     graph = avfilter_graph_alloc();
314     if (!graph)
315         return AVERROR(ENOMEM);
316
317     ret = avfilter_graph_parse2(graph, fg->graph_desc, &inputs, &outputs);
318     if (ret < 0)
319         goto fail;
320
321     for (cur = inputs; cur; cur = cur->next)
322         init_input_filter(fg, cur);
323
324     for (cur = outputs; cur;) {
325         GROW_ARRAY(fg->outputs, fg->nb_outputs);
326         fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]));
327         if (!fg->outputs[fg->nb_outputs - 1])
328             exit_program(1);
329
330         fg->outputs[fg->nb_outputs - 1]->graph   = fg;
331         fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
332         fg->outputs[fg->nb_outputs - 1]->type    = avfilter_pad_get_type(cur->filter_ctx->output_pads,
333                                                                          cur->pad_idx);
334         cur = cur->next;
335         fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
336     }
337
338 fail:
339     avfilter_inout_free(&inputs);
340     avfilter_graph_free(&graph);
341     return ret;
342 }
343
344 static int insert_trim(int64_t start_time, int64_t duration,
345                        AVFilterContext **last_filter, int *pad_idx,
346                        const char *filter_name)
347 {
348     AVFilterGraph *graph = (*last_filter)->graph;
349     AVFilterContext *ctx;
350     const AVFilter *trim;
351     enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
352     const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
353     int ret = 0;
354
355     if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
356         return 0;
357
358     trim = avfilter_get_by_name(name);
359     if (!trim) {
360         av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
361                "recording time.\n", name);
362         return AVERROR_FILTER_NOT_FOUND;
363     }
364
365     ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
366     if (!ctx)
367         return AVERROR(ENOMEM);
368
369     if (duration != INT64_MAX) {
370         ret = av_opt_set_int(ctx, "durationi", duration,
371                                 AV_OPT_SEARCH_CHILDREN);
372     }
373     if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
374         ret = av_opt_set_int(ctx, "starti", start_time,
375                                 AV_OPT_SEARCH_CHILDREN);
376     }
377     if (ret < 0) {
378         av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
379         return ret;
380     }
381
382     ret = avfilter_init_str(ctx, NULL);
383     if (ret < 0)
384         return ret;
385
386     ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
387     if (ret < 0)
388         return ret;
389
390     *last_filter = ctx;
391     *pad_idx     = 0;
392     return 0;
393 }
394
395 static int insert_filter(AVFilterContext **last_filter, int *pad_idx,
396                          const char *filter_name, const char *args)
397 {
398     AVFilterGraph *graph = (*last_filter)->graph;
399     AVFilterContext *ctx;
400     int ret;
401
402     ret = avfilter_graph_create_filter(&ctx,
403                                        avfilter_get_by_name(filter_name),
404                                        filter_name, args, NULL, graph);
405     if (ret < 0)
406         return ret;
407
408     ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
409     if (ret < 0)
410         return ret;
411
412     *last_filter = ctx;
413     *pad_idx     = 0;
414     return 0;
415 }
416
417 static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
418 {
419     char *pix_fmts;
420     OutputStream *ost = ofilter->ost;
421     OutputFile    *of = output_files[ost->file_index];
422     AVCodecContext *codec = ost->enc_ctx;
423     AVFilterContext *last_filter = out->filter_ctx;
424     int pad_idx = out->pad_idx;
425     int ret;
426     char name[255];
427
428     snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
429     ret = avfilter_graph_create_filter(&ofilter->filter,
430                                        avfilter_get_by_name("buffersink"),
431                                        name, NULL, NULL, fg->graph);
432
433     if (ret < 0)
434         return ret;
435
436     if (!hw_device_ctx && (codec->width || codec->height)) {
437         char args[255];
438         AVFilterContext *filter;
439         AVDictionaryEntry *e = NULL;
440
441         snprintf(args, sizeof(args), "%d:%d",
442                  codec->width,
443                  codec->height);
444
445         while ((e = av_dict_get(ost->sws_dict, "", e,
446                                 AV_DICT_IGNORE_SUFFIX))) {
447             av_strlcatf(args, sizeof(args), ":%s=%s", e->key, e->value);
448         }
449
450         snprintf(name, sizeof(name), "scaler for output stream %d:%d",
451                  ost->file_index, ost->index);
452         if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
453                                                 name, args, NULL, fg->graph)) < 0)
454             return ret;
455         if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
456             return ret;
457
458         last_filter = filter;
459         pad_idx = 0;
460     }
461
462     if ((pix_fmts = choose_pix_fmts(ost))) {
463         AVFilterContext *filter;
464         snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
465                  ost->file_index, ost->index);
466         ret = avfilter_graph_create_filter(&filter,
467                                            avfilter_get_by_name("format"),
468                                            "format", pix_fmts, NULL, fg->graph);
469         av_freep(&pix_fmts);
470         if (ret < 0)
471             return ret;
472         if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
473             return ret;
474
475         last_filter = filter;
476         pad_idx     = 0;
477     }
478
479     if (ost->frame_rate.num && 0) {
480         AVFilterContext *fps;
481         char args[255];
482
483         snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
484                  ost->frame_rate.den);
485         snprintf(name, sizeof(name), "fps for output stream %d:%d",
486                  ost->file_index, ost->index);
487         ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
488                                            name, args, NULL, fg->graph);
489         if (ret < 0)
490             return ret;
491
492         ret = avfilter_link(last_filter, pad_idx, fps, 0);
493         if (ret < 0)
494             return ret;
495         last_filter = fps;
496         pad_idx = 0;
497     }
498
499     snprintf(name, sizeof(name), "trim for output stream %d:%d",
500              ost->file_index, ost->index);
501     ret = insert_trim(of->start_time, of->recording_time,
502                       &last_filter, &pad_idx, name);
503     if (ret < 0)
504         return ret;
505
506
507     if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
508         return ret;
509
510     return 0;
511 }
512
513 static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
514 {
515     OutputStream *ost = ofilter->ost;
516     OutputFile    *of = output_files[ost->file_index];
517     AVCodecContext *codec  = ost->enc_ctx;
518     AVFilterContext *last_filter = out->filter_ctx;
519     int pad_idx = out->pad_idx;
520     char *sample_fmts, *sample_rates, *channel_layouts;
521     char name[255];
522     int ret;
523
524     snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
525     ret = avfilter_graph_create_filter(&ofilter->filter,
526                                        avfilter_get_by_name("abuffersink"),
527                                        name, NULL, NULL, fg->graph);
528     if (ret < 0)
529         return ret;
530     if ((ret = av_opt_set_int(ofilter->filter, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
531         return ret;
532
533 #define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do {                 \
534     AVFilterContext *filt_ctx;                                              \
535                                                                             \
536     av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi "            \
537            "similarly to -af " filter_name "=%s.\n", arg);                  \
538                                                                             \
539     ret = avfilter_graph_create_filter(&filt_ctx,                           \
540                                        avfilter_get_by_name(filter_name),   \
541                                        filter_name, arg, NULL, fg->graph);  \
542     if (ret < 0)                                                            \
543         return ret;                                                         \
544                                                                             \
545     ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0);                 \
546     if (ret < 0)                                                            \
547         return ret;                                                         \
548                                                                             \
549     last_filter = filt_ctx;                                                 \
550     pad_idx = 0;                                                            \
551 } while (0)
552     if (ost->audio_channels_mapped) {
553         int i;
554         AVBPrint pan_buf;
555         av_bprint_init(&pan_buf, 256, 8192);
556         av_bprintf(&pan_buf, "0x%"PRIx64,
557                    av_get_default_channel_layout(ost->audio_channels_mapped));
558         for (i = 0; i < ost->audio_channels_mapped; i++)
559             if (ost->audio_channels_map[i] != -1)
560                 av_bprintf(&pan_buf, "|c%d=c%d", i, ost->audio_channels_map[i]);
561
562         AUTO_INSERT_FILTER("-map_channel", "pan", pan_buf.str);
563         av_bprint_finalize(&pan_buf, NULL);
564     }
565
566     if (codec->channels && !codec->channel_layout)
567         codec->channel_layout = av_get_default_channel_layout(codec->channels);
568
569     sample_fmts     = choose_sample_fmts(ost);
570     sample_rates    = choose_sample_rates(ost);
571     channel_layouts = choose_channel_layouts(ost);
572     if (sample_fmts || sample_rates || channel_layouts) {
573         AVFilterContext *format;
574         char args[256];
575         args[0] = 0;
576
577         if (sample_fmts)
578             av_strlcatf(args, sizeof(args), "sample_fmts=%s:",
579                             sample_fmts);
580         if (sample_rates)
581             av_strlcatf(args, sizeof(args), "sample_rates=%s:",
582                             sample_rates);
583         if (channel_layouts)
584             av_strlcatf(args, sizeof(args), "channel_layouts=%s:",
585                             channel_layouts);
586
587         av_freep(&sample_fmts);
588         av_freep(&sample_rates);
589         av_freep(&channel_layouts);
590
591         snprintf(name, sizeof(name), "audio format for output stream %d:%d",
592                  ost->file_index, ost->index);
593         ret = avfilter_graph_create_filter(&format,
594                                            avfilter_get_by_name("aformat"),
595                                            name, args, NULL, fg->graph);
596         if (ret < 0)
597             return ret;
598
599         ret = avfilter_link(last_filter, pad_idx, format, 0);
600         if (ret < 0)
601             return ret;
602
603         last_filter = format;
604         pad_idx = 0;
605     }
606
607     if (audio_volume != 256 && 0) {
608         char args[256];
609
610         snprintf(args, sizeof(args), "%f", audio_volume / 256.);
611         AUTO_INSERT_FILTER("-vol", "volume", args);
612     }
613
614     if (ost->apad && of->shortest) {
615         char args[256];
616         int i;
617
618         for (i=0; i<of->ctx->nb_streams; i++)
619             if (of->ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
620                 break;
621
622         if (i<of->ctx->nb_streams) {
623             snprintf(args, sizeof(args), "%s", ost->apad);
624             AUTO_INSERT_FILTER("-apad", "apad", args);
625         }
626     }
627
628     snprintf(name, sizeof(name), "trim for output stream %d:%d",
629              ost->file_index, ost->index);
630     ret = insert_trim(of->start_time, of->recording_time,
631                       &last_filter, &pad_idx, name);
632     if (ret < 0)
633         return ret;
634
635     if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
636         return ret;
637
638     return 0;
639 }
640
641 #define DESCRIBE_FILTER_LINK(f, inout, in)                         \
642 {                                                                  \
643     AVFilterContext *ctx = inout->filter_ctx;                      \
644     AVFilterPad *pads = in ? ctx->input_pads  : ctx->output_pads;  \
645     int       nb_pads = in ? ctx->nb_inputs   : ctx->nb_outputs;   \
646     AVIOContext *pb;                                               \
647                                                                    \
648     if (avio_open_dyn_buf(&pb) < 0)                                \
649         exit_program(1);                                           \
650                                                                    \
651     avio_printf(pb, "%s", ctx->filter->name);                      \
652     if (nb_pads > 1)                                               \
653         avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
654     avio_w8(pb, 0);                                                \
655     avio_close_dyn_buf(pb, &f->name);                              \
656 }
657
658 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
659 {
660     av_freep(&ofilter->name);
661     DESCRIBE_FILTER_LINK(ofilter, out, 0);
662
663     if (!ofilter->ost) {
664         av_log(NULL, AV_LOG_FATAL, "Filter %s has an unconnected output\n", ofilter->name);
665         exit_program(1);
666     }
667
668     switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
669     case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
670     case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
671     default: av_assert0(0);
672     }
673 }
674
675 static int sub2video_prepare(InputStream *ist, InputFilter *ifilter)
676 {
677     AVFormatContext *avf = input_files[ist->file_index]->ctx;
678     int i, w, h;
679
680     /* Compute the size of the canvas for the subtitles stream.
681        If the subtitles codecpar has set a size, use it. Otherwise use the
682        maximum dimensions of the video streams in the same file. */
683     w = ifilter->width;
684     h = ifilter->height;
685     if (!(w && h)) {
686         for (i = 0; i < avf->nb_streams; i++) {
687             if (avf->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
688                 w = FFMAX(w, avf->streams[i]->codecpar->width);
689                 h = FFMAX(h, avf->streams[i]->codecpar->height);
690             }
691         }
692         if (!(w && h)) {
693             w = FFMAX(w, 720);
694             h = FFMAX(h, 576);
695         }
696         av_log(avf, AV_LOG_INFO, "sub2video: using %dx%d canvas\n", w, h);
697     }
698     ist->sub2video.w = ist->resample_width  = ifilter->width  = w;
699     ist->sub2video.h = ist->resample_height = ifilter->height = h;
700
701     /* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the
702        palettes for all rectangles are identical or compatible */
703     ist->resample_pix_fmt = ifilter->format = AV_PIX_FMT_RGB32;
704
705     ist->sub2video.frame = av_frame_alloc();
706     if (!ist->sub2video.frame)
707         return AVERROR(ENOMEM);
708     ist->sub2video.last_pts = INT64_MIN;
709     return 0;
710 }
711
712 static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
713                                         AVFilterInOut *in)
714 {
715     AVFilterContext *last_filter;
716     const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
717     InputStream *ist = ifilter->ist;
718     InputFile     *f = input_files[ist->file_index];
719     AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
720                                          ist->st->time_base;
721     AVRational fr = ist->framerate;
722     AVRational sar;
723     AVBPrint args;
724     char name[255];
725     int ret, pad_idx = 0;
726     int64_t tsoffset = 0;
727     AVBufferSrcParameters *par = av_buffersrc_parameters_alloc();
728
729     if (!par)
730         return AVERROR(ENOMEM);
731     memset(par, 0, sizeof(*par));
732     par->format = AV_PIX_FMT_NONE;
733
734     if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
735         av_log(NULL, AV_LOG_ERROR, "Cannot connect video filter to audio input\n");
736         ret = AVERROR(EINVAL);
737         goto fail;
738     }
739
740     if (!fr.num)
741         fr = av_guess_frame_rate(input_files[ist->file_index]->ctx, ist->st, NULL);
742
743     if (ist->dec_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
744         ret = sub2video_prepare(ist, ifilter);
745         if (ret < 0)
746             goto fail;
747     }
748
749     sar = ifilter->sample_aspect_ratio;
750     if(!sar.den)
751         sar = (AVRational){0,1};
752     av_bprint_init(&args, 0, 1);
753     av_bprintf(&args,
754              "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
755              "pixel_aspect=%d/%d:sws_param=flags=%d",
756              ifilter->width, ifilter->height, ifilter->format,
757              tb.num, tb.den, sar.num, sar.den,
758              SWS_BILINEAR + ((ist->dec_ctx->flags&AV_CODEC_FLAG_BITEXACT) ? SWS_BITEXACT:0));
759     if (fr.num && fr.den)
760         av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
761     snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
762              ist->file_index, ist->st->index);
763
764
765     if ((ret = avfilter_graph_create_filter(&ifilter->filter, buffer_filt, name,
766                                             args.str, NULL, fg->graph)) < 0)
767         goto fail;
768     par->hw_frames_ctx = ifilter->hw_frames_ctx;
769     ret = av_buffersrc_parameters_set(ifilter->filter, par);
770     if (ret < 0)
771         goto fail;
772     av_freep(&par);
773     last_filter = ifilter->filter;
774
775     if (ist->autorotate) {
776         double theta = get_rotation(ist->st);
777
778         if (fabs(theta - 90) < 1.0) {
779             ret = insert_filter(&last_filter, &pad_idx, "transpose", "clock");
780         } else if (fabs(theta - 180) < 1.0) {
781             ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL);
782             if (ret < 0)
783                 return ret;
784             ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
785         } else if (fabs(theta - 270) < 1.0) {
786             ret = insert_filter(&last_filter, &pad_idx, "transpose", "cclock");
787         } else if (fabs(theta) > 1.0) {
788             char rotate_buf[64];
789             snprintf(rotate_buf, sizeof(rotate_buf), "%f*PI/180", theta);
790             ret = insert_filter(&last_filter, &pad_idx, "rotate", rotate_buf);
791         }
792         if (ret < 0)
793             return ret;
794     }
795
796     if (ist->framerate.num) {
797         AVFilterContext *setpts;
798
799         snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
800                  ist->file_index, ist->st->index);
801         if ((ret = avfilter_graph_create_filter(&setpts,
802                                                 avfilter_get_by_name("setpts"),
803                                                 name, "N", NULL,
804                                                 fg->graph)) < 0)
805             return ret;
806
807         if ((ret = avfilter_link(last_filter, 0, setpts, 0)) < 0)
808             return ret;
809
810         last_filter = setpts;
811     }
812
813     if (do_deinterlace) {
814         AVFilterContext *yadif;
815
816         snprintf(name, sizeof(name), "deinterlace input from stream %d:%d",
817                  ist->file_index, ist->st->index);
818         if ((ret = avfilter_graph_create_filter(&yadif,
819                                                 avfilter_get_by_name("yadif"),
820                                                 name, "", NULL,
821                                                 fg->graph)) < 0)
822             return ret;
823
824         if ((ret = avfilter_link(last_filter, 0, yadif, 0)) < 0)
825             return ret;
826
827         last_filter = yadif;
828     }
829
830     snprintf(name, sizeof(name), "trim for input stream %d:%d",
831              ist->file_index, ist->st->index);
832     if (copy_ts) {
833         tsoffset = f->start_time == AV_NOPTS_VALUE ? 0 : f->start_time;
834         if (!start_at_zero && f->ctx->start_time != AV_NOPTS_VALUE)
835             tsoffset += f->ctx->start_time;
836     }
837     ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
838                       AV_NOPTS_VALUE : tsoffset, f->recording_time,
839                       &last_filter, &pad_idx, name);
840     if (ret < 0)
841         return ret;
842
843     if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
844         return ret;
845     return 0;
846 fail:
847     av_freep(&par);
848
849     return ret;
850 }
851
852 static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
853                                         AVFilterInOut *in)
854 {
855     AVFilterContext *last_filter;
856     const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
857     InputStream *ist = ifilter->ist;
858     InputFile     *f = input_files[ist->file_index];
859     AVBPrint args;
860     char name[255];
861     int ret, pad_idx = 0;
862     int64_t tsoffset = 0;
863
864     if (ist->dec_ctx->codec_type != AVMEDIA_TYPE_AUDIO) {
865         av_log(NULL, AV_LOG_ERROR, "Cannot connect audio filter to non audio input\n");
866         return AVERROR(EINVAL);
867     }
868
869     av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
870     av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
871              1, ifilter->sample_rate,
872              ifilter->sample_rate,
873              av_get_sample_fmt_name(ifilter->format));
874     if (ifilter->channel_layout)
875         av_bprintf(&args, ":channel_layout=0x%"PRIx64,
876                    ifilter->channel_layout);
877     else
878         av_bprintf(&args, ":channels=%d", ifilter->channels);
879     snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
880              ist->file_index, ist->st->index);
881
882     if ((ret = avfilter_graph_create_filter(&ifilter->filter, abuffer_filt,
883                                             name, args.str, NULL,
884                                             fg->graph)) < 0)
885         return ret;
886     last_filter = ifilter->filter;
887
888 #define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do {                 \
889     AVFilterContext *filt_ctx;                                              \
890                                                                             \
891     av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi "            \
892            "similarly to -af " filter_name "=%s.\n", arg);                  \
893                                                                             \
894     snprintf(name, sizeof(name), "graph %d %s for input stream %d:%d",      \
895                 fg->index, filter_name, ist->file_index, ist->st->index);   \
896     ret = avfilter_graph_create_filter(&filt_ctx,                           \
897                                        avfilter_get_by_name(filter_name),   \
898                                        name, arg, NULL, fg->graph);         \
899     if (ret < 0)                                                            \
900         return ret;                                                         \
901                                                                             \
902     ret = avfilter_link(last_filter, 0, filt_ctx, 0);                       \
903     if (ret < 0)                                                            \
904         return ret;                                                         \
905                                                                             \
906     last_filter = filt_ctx;                                                 \
907 } while (0)
908
909     if (audio_sync_method > 0) {
910         char args[256] = {0};
911
912         av_strlcatf(args, sizeof(args), "async=%d", audio_sync_method);
913         if (audio_drift_threshold != 0.1)
914             av_strlcatf(args, sizeof(args), ":min_hard_comp=%f", audio_drift_threshold);
915         if (!fg->reconfiguration)
916             av_strlcatf(args, sizeof(args), ":first_pts=0");
917         AUTO_INSERT_FILTER_INPUT("-async", "aresample", args);
918     }
919
920 //     if (ost->audio_channels_mapped) {
921 //         int i;
922 //         AVBPrint pan_buf;
923 //         av_bprint_init(&pan_buf, 256, 8192);
924 //         av_bprintf(&pan_buf, "0x%"PRIx64,
925 //                    av_get_default_channel_layout(ost->audio_channels_mapped));
926 //         for (i = 0; i < ost->audio_channels_mapped; i++)
927 //             if (ost->audio_channels_map[i] != -1)
928 //                 av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
929 //         AUTO_INSERT_FILTER_INPUT("-map_channel", "pan", pan_buf.str);
930 //         av_bprint_finalize(&pan_buf, NULL);
931 //     }
932
933     if (audio_volume != 256) {
934         char args[256];
935
936         av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
937                "audio filter instead.\n");
938
939         snprintf(args, sizeof(args), "%f", audio_volume / 256.);
940         AUTO_INSERT_FILTER_INPUT("-vol", "volume", args);
941     }
942
943     snprintf(name, sizeof(name), "trim for input stream %d:%d",
944              ist->file_index, ist->st->index);
945     if (copy_ts) {
946         tsoffset = f->start_time == AV_NOPTS_VALUE ? 0 : f->start_time;
947         if (!start_at_zero && f->ctx->start_time != AV_NOPTS_VALUE)
948             tsoffset += f->ctx->start_time;
949     }
950     ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
951                       AV_NOPTS_VALUE : tsoffset, f->recording_time,
952                       &last_filter, &pad_idx, name);
953     if (ret < 0)
954         return ret;
955
956     if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
957         return ret;
958
959     return 0;
960 }
961
962 static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
963                                   AVFilterInOut *in)
964 {
965     av_freep(&ifilter->name);
966     DESCRIBE_FILTER_LINK(ifilter, in, 1);
967
968     if (!ifilter->ist->dec) {
969         av_log(NULL, AV_LOG_ERROR,
970                "No decoder for stream #%d:%d, filtering impossible\n",
971                ifilter->ist->file_index, ifilter->ist->st->index);
972         return AVERROR_DECODER_NOT_FOUND;
973     }
974     switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
975     case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
976     case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
977     default: av_assert0(0);
978     }
979 }
980
981 int configure_filtergraph(FilterGraph *fg)
982 {
983     AVFilterInOut *inputs, *outputs, *cur;
984     int ret, i, simple = filtergraph_is_simple(fg);
985     const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
986                                       fg->graph_desc;
987
988     avfilter_graph_free(&fg->graph);
989     if (!(fg->graph = avfilter_graph_alloc()))
990         return AVERROR(ENOMEM);
991
992     if (simple) {
993         OutputStream *ost = fg->outputs[0]->ost;
994         char args[512];
995         AVDictionaryEntry *e = NULL;
996
997         fg->graph->nb_threads = filter_complex_nbthreads;
998
999         args[0] = 0;
1000         while ((e = av_dict_get(ost->sws_dict, "", e,
1001                                 AV_DICT_IGNORE_SUFFIX))) {
1002             av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
1003         }
1004         if (strlen(args))
1005             args[strlen(args)-1] = 0;
1006         fg->graph->scale_sws_opts = av_strdup(args);
1007
1008         args[0] = 0;
1009         while ((e = av_dict_get(ost->swr_opts, "", e,
1010                                 AV_DICT_IGNORE_SUFFIX))) {
1011             av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
1012         }
1013         if (strlen(args))
1014             args[strlen(args)-1] = 0;
1015         av_opt_set(fg->graph, "aresample_swr_opts", args, 0);
1016
1017         args[0] = '\0';
1018         while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
1019                                 AV_DICT_IGNORE_SUFFIX))) {
1020             av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
1021         }
1022         if (strlen(args))
1023             args[strlen(args) - 1] = '\0';
1024         fg->graph->resample_lavr_opts = av_strdup(args);
1025
1026         e = av_dict_get(ost->encoder_opts, "threads", NULL, 0);
1027         if (e)
1028             av_opt_set(fg->graph, "threads", e->value, 0);
1029     } else {
1030         fg->graph->nb_threads = filter_nbthreads;
1031     }
1032
1033     if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
1034         return ret;
1035
1036     if (hw_device_ctx) {
1037         for (i = 0; i < fg->graph->nb_filters; i++) {
1038             fg->graph->filters[i]->hw_device_ctx = av_buffer_ref(hw_device_ctx);
1039         }
1040     }
1041
1042     if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
1043         const char *num_inputs;
1044         const char *num_outputs;
1045         if (!outputs) {
1046             num_outputs = "0";
1047         } else if (outputs->next) {
1048             num_outputs = ">1";
1049         } else {
1050             num_outputs = "1";
1051         }
1052         if (!inputs) {
1053             num_inputs = "0";
1054         } else if (inputs->next) {
1055             num_inputs = ">1";
1056         } else {
1057             num_inputs = "1";
1058         }
1059         av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' was expected "
1060                "to have exactly 1 input and 1 output."
1061                " However, it had %s input(s) and %s output(s)."
1062                " Please adjust, or use a complex filtergraph (-filter_complex) instead.\n",
1063                graph_desc, num_inputs, num_outputs);
1064         return AVERROR(EINVAL);
1065     }
1066
1067     for (cur = inputs, i = 0; cur; cur = cur->next, i++)
1068         if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0) {
1069             avfilter_inout_free(&inputs);
1070             avfilter_inout_free(&outputs);
1071             return ret;
1072         }
1073     avfilter_inout_free(&inputs);
1074
1075     for (cur = outputs, i = 0; cur; cur = cur->next, i++)
1076         configure_output_filter(fg, fg->outputs[i], cur);
1077     avfilter_inout_free(&outputs);
1078
1079     if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
1080         return ret;
1081
1082     fg->reconfiguration = 1;
1083
1084     for (i = 0; i < fg->nb_outputs; i++) {
1085         OutputStream *ost = fg->outputs[i]->ost;
1086         if (!ost->enc) {
1087             /* identical to the same check in ffmpeg.c, needed because
1088                complex filter graphs are initialized earlier */
1089             av_log(NULL, AV_LOG_ERROR, "Encoder (codec %s) not found for output stream #%d:%d\n",
1090                      avcodec_get_name(ost->st->codecpar->codec_id), ost->file_index, ost->index);
1091             return AVERROR(EINVAL);
1092         }
1093         if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
1094             !(ost->enc->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE))
1095             av_buffersink_set_frame_size(ost->filter->filter,
1096                                          ost->enc_ctx->frame_size);
1097     }
1098
1099     return 0;
1100 }
1101
1102 int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame)
1103 {
1104     av_buffer_unref(&ifilter->hw_frames_ctx);
1105
1106     ifilter->format = frame->format;
1107
1108     ifilter->width               = frame->width;
1109     ifilter->height              = frame->height;
1110     ifilter->sample_aspect_ratio = frame->sample_aspect_ratio;
1111
1112     ifilter->sample_rate         = frame->sample_rate;
1113     ifilter->channels            = av_frame_get_channels(frame);
1114     ifilter->channel_layout      = frame->channel_layout;
1115
1116     if (frame->hw_frames_ctx) {
1117         ifilter->hw_frames_ctx = av_buffer_ref(frame->hw_frames_ctx);
1118         if (!ifilter->hw_frames_ctx)
1119             return AVERROR(ENOMEM);
1120     }
1121
1122     return 0;
1123 }
1124
1125 int ifilter_parameters_from_decoder(InputFilter *ifilter, const AVCodecContext *avctx)
1126 {
1127     av_buffer_unref(&ifilter->hw_frames_ctx);
1128
1129     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO)
1130         ifilter->format = avctx->pix_fmt;
1131     else
1132         ifilter->format = avctx->sample_fmt;
1133
1134     ifilter->width               = avctx->width;
1135     ifilter->height              = avctx->height;
1136     if (ifilter->ist && ifilter->ist->st && ifilter->ist->st->sample_aspect_ratio.num)
1137         ifilter->sample_aspect_ratio = ifilter->ist->st->sample_aspect_ratio;
1138     else
1139         ifilter->sample_aspect_ratio = avctx->sample_aspect_ratio;
1140
1141     ifilter->sample_rate         = avctx->sample_rate;
1142     ifilter->channels            = avctx->channels;
1143     ifilter->channel_layout      = avctx->channel_layout;
1144
1145     if (avctx->hw_frames_ctx) {
1146         ifilter->hw_frames_ctx = av_buffer_ref(avctx->hw_frames_ctx);
1147         if (!ifilter->hw_frames_ctx)
1148             return AVERROR(ENOMEM);
1149     }
1150
1151     return 0;
1152 }
1153
1154 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
1155 {
1156     int i;
1157     for (i = 0; i < fg->nb_inputs; i++)
1158         if (fg->inputs[i]->ist == ist)
1159             return 1;
1160     return 0;
1161 }
1162
1163 int filtergraph_is_simple(FilterGraph *fg)
1164 {
1165     return !fg->graph_desc;
1166 }