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