OSDN Git Service

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