]> git.sesse.net Git - ffmpeg/blob - avconv_filter.c
intrax8: K&R formatting cosmetics
[ffmpeg] / 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 FilterGraph *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
101     GROW_ARRAY(ist->filters, ist->nb_filters);
102     ist->filters[ist->nb_filters - 1] = fg->inputs[0];
103
104     GROW_ARRAY(filtergraphs, nb_filtergraphs);
105     filtergraphs[nb_filtergraphs - 1] = fg;
106
107     return fg;
108 }
109
110 static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
111 {
112     InputStream *ist = NULL;
113     enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
114     int i;
115
116     // TODO: support other filter types
117     if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
118         av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
119                "currently.\n");
120         exit(1);
121     }
122
123     if (in->name) {
124         AVFormatContext *s;
125         AVStream       *st = NULL;
126         char *p;
127         int file_idx = strtol(in->name, &p, 0);
128
129         if (file_idx < 0 || file_idx >= nb_input_files) {
130             av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtegraph description %s.\n",
131                    file_idx, fg->graph_desc);
132             exit(1);
133         }
134         s = input_files[file_idx]->ctx;
135
136         for (i = 0; i < s->nb_streams; i++) {
137             if (s->streams[i]->codecpar->codec_type != type)
138                 continue;
139             if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
140                 st = s->streams[i];
141                 break;
142             }
143         }
144         if (!st) {
145             av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
146                    "matches no streams.\n", p, fg->graph_desc);
147             exit(1);
148         }
149         ist = input_streams[input_files[file_idx]->ist_index + st->index];
150     } else {
151         /* find the first unused stream of corresponding type */
152         for (i = 0; i < nb_input_streams; i++) {
153             ist = input_streams[i];
154             if (ist->dec_ctx->codec_type == type && ist->discard)
155                 break;
156         }
157         if (i == nb_input_streams) {
158             av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
159                    "unlabeled input pad %d on filter %s\n", in->pad_idx,
160                    in->filter_ctx->name);
161             exit(1);
162         }
163     }
164     av_assert0(ist);
165
166     ist->discard         = 0;
167     ist->decoding_needed = 1;
168     ist->st->discard = AVDISCARD_NONE;
169
170     GROW_ARRAY(fg->inputs, fg->nb_inputs);
171     if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
172         exit(1);
173     fg->inputs[fg->nb_inputs - 1]->ist   = ist;
174     fg->inputs[fg->nb_inputs - 1]->graph = fg;
175
176     GROW_ARRAY(ist->filters, ist->nb_filters);
177     ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
178 }
179
180 int init_complex_filtergraph(FilterGraph *fg)
181 {
182     AVFilterInOut *inputs, *outputs, *cur;
183     AVFilterGraph *graph;
184     int ret = 0;
185
186     /* this graph is only used for determining the kinds of inputs
187      * and outputs we have, and is discarded on exit from this function */
188     graph = avfilter_graph_alloc();
189     if (!graph)
190         return AVERROR(ENOMEM);
191
192     ret = avfilter_graph_parse2(graph, fg->graph_desc, &inputs, &outputs);
193     if (ret < 0)
194         goto fail;
195
196     for (cur = inputs; cur; cur = cur->next)
197         init_input_filter(fg, cur);
198
199     for (cur = outputs; cur;) {
200         GROW_ARRAY(fg->outputs, fg->nb_outputs);
201         fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]));
202         if (!fg->outputs[fg->nb_outputs - 1])
203             exit(1);
204
205         fg->outputs[fg->nb_outputs - 1]->graph   = fg;
206         fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
207         fg->outputs[fg->nb_outputs - 1]->type    = avfilter_pad_get_type(cur->filter_ctx->output_pads,
208                                                                          cur->pad_idx);
209         cur = cur->next;
210         fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
211     }
212
213 fail:
214     avfilter_inout_free(&inputs);
215     avfilter_graph_free(&graph);
216     return ret;
217 }
218
219 static int insert_trim(int64_t start_time, int64_t duration,
220                        AVFilterContext **last_filter, int *pad_idx,
221                        const char *filter_name)
222 {
223     AVFilterGraph *graph = (*last_filter)->graph;
224     AVFilterContext *ctx;
225     const AVFilter *trim;
226     enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
227     const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
228     int ret = 0;
229
230     if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
231         return 0;
232
233     trim = avfilter_get_by_name(name);
234     if (!trim) {
235         av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
236                "recording time.\n", name);
237         return AVERROR_FILTER_NOT_FOUND;
238     }
239
240     ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
241     if (!ctx)
242         return AVERROR(ENOMEM);
243
244     if (duration != INT64_MAX) {
245         ret = av_opt_set_double(ctx, "duration", (double)duration / 1e6,
246                                 AV_OPT_SEARCH_CHILDREN);
247     }
248     if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
249         ret = av_opt_set_double(ctx, "start", (double)start_time / 1e6,
250                                 AV_OPT_SEARCH_CHILDREN);
251     }
252     if (ret < 0) {
253         av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
254         return ret;
255     }
256
257     ret = avfilter_init_str(ctx, NULL);
258     if (ret < 0)
259         return ret;
260
261     ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
262     if (ret < 0)
263         return ret;
264
265     *last_filter = ctx;
266     *pad_idx     = 0;
267     return 0;
268 }
269
270 static int insert_filter(AVFilterContext **last_filter, int *pad_idx,
271                          const char *filter_name, const char *args)
272 {
273     AVFilterGraph *graph = (*last_filter)->graph;
274     AVFilterContext *ctx;
275     int ret;
276
277     ret = avfilter_graph_create_filter(&ctx,
278                                        avfilter_get_by_name(filter_name),
279                                        filter_name, args, NULL, graph);
280     if (ret < 0)
281         return ret;
282
283     ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
284     if (ret < 0)
285         return ret;
286
287     *last_filter = ctx;
288     *pad_idx     = 0;
289     return 0;
290 }
291
292 static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
293 {
294     char *pix_fmts;
295     OutputStream *ost = ofilter->ost;
296     OutputFile    *of = output_files[ost->file_index];
297     AVCodecContext *codec = ost->enc_ctx;
298     AVFilterContext *last_filter = out->filter_ctx;
299     int pad_idx = out->pad_idx;
300     int ret;
301     char name[255];
302
303     snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
304     ret = avfilter_graph_create_filter(&ofilter->filter,
305                                        avfilter_get_by_name("buffersink"),
306                                        name, NULL, NULL, fg->graph);
307     if (ret < 0)
308         return ret;
309
310     if (codec->width || codec->height) {
311         char args[255];
312         AVFilterContext *filter;
313
314         snprintf(args, sizeof(args), "%d:%d:0x%X",
315                  codec->width,
316                  codec->height,
317                  (unsigned)ost->sws_flags);
318         snprintf(name, sizeof(name), "scaler for output stream %d:%d",
319                  ost->file_index, ost->index);
320         if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
321                                                 name, args, NULL, fg->graph)) < 0)
322             return ret;
323         if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
324             return ret;
325
326         last_filter = filter;
327         pad_idx = 0;
328     }
329
330     if ((pix_fmts = choose_pix_fmts(ost))) {
331         AVFilterContext *filter;
332         snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
333                  ost->file_index, ost->index);
334         ret = avfilter_graph_create_filter(&filter,
335                                            avfilter_get_by_name("format"),
336                                            "format", pix_fmts, NULL, fg->graph);
337         av_freep(&pix_fmts);
338         if (ret < 0)
339             return ret;
340         if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
341             return ret;
342
343         last_filter = filter;
344         pad_idx     = 0;
345     }
346
347     if (ost->frame_rate.num) {
348         AVFilterContext *fps;
349         char args[255];
350
351         snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
352                  ost->frame_rate.den);
353         snprintf(name, sizeof(name), "fps for output stream %d:%d",
354                  ost->file_index, ost->index);
355         ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
356                                            name, args, NULL, fg->graph);
357         if (ret < 0)
358             return ret;
359
360         ret = avfilter_link(last_filter, pad_idx, fps, 0);
361         if (ret < 0)
362             return ret;
363         last_filter = fps;
364         pad_idx = 0;
365     }
366
367     snprintf(name, sizeof(name), "trim for output stream %d:%d",
368              ost->file_index, ost->index);
369     ret = insert_trim(of->start_time, of->recording_time,
370                       &last_filter, &pad_idx, name);
371     if (ret < 0)
372         return ret;
373
374
375     if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
376         return ret;
377
378     return 0;
379 }
380
381 static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
382 {
383     OutputStream *ost = ofilter->ost;
384     OutputFile    *of = output_files[ost->file_index];
385     AVCodecContext *codec  = ost->enc_ctx;
386     AVFilterContext *last_filter = out->filter_ctx;
387     int pad_idx = out->pad_idx;
388     char *sample_fmts, *sample_rates, *channel_layouts;
389     char name[255];
390     int ret;
391
392
393     snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
394     ret = avfilter_graph_create_filter(&ofilter->filter,
395                                        avfilter_get_by_name("abuffersink"),
396                                        name, NULL, NULL, fg->graph);
397     if (ret < 0)
398         return ret;
399
400     if (codec->channels && !codec->channel_layout)
401         codec->channel_layout = av_get_default_channel_layout(codec->channels);
402
403     sample_fmts     = choose_sample_fmts(ost);
404     sample_rates    = choose_sample_rates(ost);
405     channel_layouts = choose_channel_layouts(ost);
406     if (sample_fmts || sample_rates || channel_layouts) {
407         AVFilterContext *format;
408         char args[256];
409         int len = 0;
410
411         if (sample_fmts)
412             len += snprintf(args + len, sizeof(args) - len, "sample_fmts=%s:",
413                             sample_fmts);
414         if (sample_rates)
415             len += snprintf(args + len, sizeof(args) - len, "sample_rates=%s:",
416                             sample_rates);
417         if (channel_layouts)
418             len += snprintf(args + len, sizeof(args) - len, "channel_layouts=%s:",
419                             channel_layouts);
420         args[len - 1] = 0;
421
422         av_freep(&sample_fmts);
423         av_freep(&sample_rates);
424         av_freep(&channel_layouts);
425
426         snprintf(name, sizeof(name), "audio format for output stream %d:%d",
427                  ost->file_index, ost->index);
428         ret = avfilter_graph_create_filter(&format,
429                                            avfilter_get_by_name("aformat"),
430                                            name, args, NULL, fg->graph);
431         if (ret < 0)
432             return ret;
433
434         ret = avfilter_link(last_filter, pad_idx, format, 0);
435         if (ret < 0)
436             return ret;
437
438         last_filter = format;
439         pad_idx = 0;
440     }
441
442     snprintf(name, sizeof(name), "trim for output stream %d:%d",
443              ost->file_index, ost->index);
444     ret = insert_trim(of->start_time, of->recording_time,
445                       &last_filter, &pad_idx, name);
446     if (ret < 0)
447         return ret;
448
449     if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
450         return ret;
451
452     return 0;
453 }
454
455 #define DESCRIBE_FILTER_LINK(f, inout, in)                         \
456 {                                                                  \
457     AVFilterContext *ctx = inout->filter_ctx;                      \
458     AVFilterPad *pads = in ? ctx->input_pads  : ctx->output_pads;  \
459     int       nb_pads = in ? ctx->nb_inputs   : ctx->nb_outputs;   \
460     AVIOContext *pb;                                               \
461                                                                    \
462     if (avio_open_dyn_buf(&pb) < 0)                                \
463         exit(1);                                                   \
464                                                                    \
465     avio_printf(pb, "%s", ctx->filter->name);                      \
466     if (nb_pads > 1)                                               \
467         avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
468     avio_w8(pb, 0);                                                \
469     avio_close_dyn_buf(pb, &f->name);                              \
470 }
471
472 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
473 {
474     av_freep(&ofilter->name);
475     DESCRIBE_FILTER_LINK(ofilter, out, 0);
476
477     switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
478     case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
479     case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
480     default: av_assert0(0);
481     }
482 }
483
484 static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
485                                         AVFilterInOut *in)
486 {
487     AVFilterContext *last_filter;
488     const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
489     InputStream *ist = ifilter->ist;
490     InputFile     *f = input_files[ist->file_index];
491     AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
492                                          ist->st->time_base;
493     AVBufferSrcParameters *par;
494     char name[255];
495     int ret, pad_idx = 0;
496
497     snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
498              ist->file_index, ist->st->index);
499
500     ifilter->filter = avfilter_graph_alloc_filter(fg->graph, buffer_filt, name);
501     if (!ifilter->filter)
502         return AVERROR(ENOMEM);
503
504     par = av_buffersrc_parameters_alloc();
505     if (!par)
506         return AVERROR(ENOMEM);
507
508     par->sample_aspect_ratio = ist->st->sample_aspect_ratio.num ?
509                                ist->st->sample_aspect_ratio :
510                                ist->dec_ctx->sample_aspect_ratio;
511     par->width               = ist->dec_ctx->width;
512     par->height              = ist->dec_ctx->height;
513     par->format              = ist->hwaccel_retrieve_data ?
514                                ist->hwaccel_retrieved_pix_fmt : ist->dec_ctx->pix_fmt;
515     par->time_base           = tb;
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, ist->dec_ctx->sample_rate };
600     par->sample_rate    = ist->dec_ctx->sample_rate;
601     par->format         = ist->dec_ctx->sample_fmt;
602     par->channel_layout = ist->dec_ctx->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 = !fg->graph_desc;
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 (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
723         av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
724                "exactly one input and output.\n", graph_desc);
725         return AVERROR(EINVAL);
726     }
727
728     for (cur = inputs, i = 0; cur; cur = cur->next, i++)
729         if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
730             return ret;
731     avfilter_inout_free(&inputs);
732
733     for (cur = outputs, i = 0; cur; cur = cur->next, i++) {
734         OutputFilter *ofilter = fg->outputs[i];
735         if (ofilter->ost)
736             configure_output_filter(fg, ofilter, cur);
737     }
738
739     avfilter_inout_free(&outputs);
740
741     if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
742         return ret;
743
744     return 0;
745 }
746
747 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
748 {
749     int i;
750     for (i = 0; i < fg->nb_inputs; i++)
751         if (fg->inputs[i]->ist == ist)
752             return 1;
753     return 0;
754 }
755