2 * ffmpeg filter configuration
4 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavfilter/avfilter.h"
24 #include "libavfilter/buffersink.h"
26 #include "libavresample/avresample.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/bprint.h"
31 #include "libavutil/channel_layout.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/pixfmt.h"
35 #include "libavutil/imgutils.h"
36 #include "libavutil/samplefmt.h"
38 enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodec *codec, enum AVPixelFormat target)
40 if (codec && codec->pix_fmts) {
41 const enum AVPixelFormat *p = codec->pix_fmts;
42 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target);
43 int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
44 enum AVPixelFormat best= AV_PIX_FMT_NONE;
45 if (st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
46 if (st->codec->codec_id == AV_CODEC_ID_MJPEG) {
47 p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE };
48 } else if (st->codec->codec_id == AV_CODEC_ID_LJPEG) {
49 p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUV420P,
50 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE };
53 for (; *p != AV_PIX_FMT_NONE; p++) {
54 best= avcodec_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
58 if (*p == AV_PIX_FMT_NONE) {
59 if (target != AV_PIX_FMT_NONE)
60 av_log(NULL, AV_LOG_WARNING,
61 "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
62 av_get_pix_fmt_name(target),
64 av_get_pix_fmt_name(best));
71 void choose_sample_fmt(AVStream *st, AVCodec *codec)
73 if (codec && codec->sample_fmts) {
74 const enum AVSampleFormat *p = codec->sample_fmts;
75 for (; *p != -1; p++) {
76 if (*p == st->codec->sample_fmt)
80 if((codec->capabilities & CODEC_CAP_LOSSLESS) && av_get_sample_fmt_name(st->codec->sample_fmt) > av_get_sample_fmt_name(codec->sample_fmts[0]))
81 av_log(NULL, AV_LOG_ERROR, "Conversion will not be lossless.\n");
82 if(av_get_sample_fmt_name(st->codec->sample_fmt))
83 av_log(NULL, AV_LOG_WARNING,
84 "Incompatible sample format '%s' for codec '%s', auto-selecting format '%s'\n",
85 av_get_sample_fmt_name(st->codec->sample_fmt),
87 av_get_sample_fmt_name(codec->sample_fmts[0]));
88 st->codec->sample_fmt = codec->sample_fmts[0];
93 static char *choose_pix_fmts(OutputStream *ost)
95 if (ost->keep_pix_fmt) {
97 avfilter_graph_set_auto_convert(ost->filter->graph->graph,
98 AVFILTER_AUTO_CONVERT_NONE);
99 if (ost->st->codec->pix_fmt == AV_PIX_FMT_NONE)
101 return av_strdup(av_get_pix_fmt_name(ost->st->codec->pix_fmt));
103 if (ost->st->codec->pix_fmt != AV_PIX_FMT_NONE) {
104 return av_strdup(av_get_pix_fmt_name(choose_pixel_fmt(ost->st, ost->enc, ost->st->codec->pix_fmt)));
105 } else if (ost->enc && ost->enc->pix_fmts) {
106 const enum AVPixelFormat *p;
107 AVIOContext *s = NULL;
111 if (avio_open_dyn_buf(&s) < 0)
114 p = ost->enc->pix_fmts;
115 if (ost->st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
116 if (ost->st->codec->codec_id == AV_CODEC_ID_MJPEG) {
117 p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE };
118 } else if (ost->st->codec->codec_id == AV_CODEC_ID_LJPEG) {
119 p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUV420P,
120 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE };
124 for (; *p != AV_PIX_FMT_NONE; p++) {
125 const char *name = av_get_pix_fmt_name(*p);
126 avio_printf(s, "%s|", name);
128 len = avio_close_dyn_buf(s, &ret);
135 /* Define a function for building a string containing a list of
136 * allowed formats. */
137 #define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name, separator)\
138 static char *choose_ ## var ## s(OutputStream *ost) \
140 if (ost->st->codec->var != none) { \
141 get_name(ost->st->codec->var); \
142 return av_strdup(name); \
143 } else if (ost->enc && ost->enc->supported_list) { \
145 AVIOContext *s = NULL; \
149 if (avio_open_dyn_buf(&s) < 0) \
152 for (p = ost->enc->supported_list; *p != none; p++) { \
154 avio_printf(s, "%s" separator, name); \
156 len = avio_close_dyn_buf(s, &ret); \
163 // DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE,
164 // GET_PIX_FMT_NAME, ":")
166 DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts,
167 AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME, "|")
169 DEF_CHOOSE_FORMAT(int, sample_rate, supported_samplerates, 0,
170 GET_SAMPLE_RATE_NAME, "|")
172 DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0,
173 GET_CH_LAYOUT_NAME, "|")
175 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost)
177 FilterGraph *fg = av_mallocz(sizeof(*fg));
181 fg->index = nb_filtergraphs;
183 GROW_ARRAY(fg->outputs, fg->nb_outputs);
184 if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
186 fg->outputs[0]->ost = ost;
187 fg->outputs[0]->graph = fg;
189 ost->filter = fg->outputs[0];
191 GROW_ARRAY(fg->inputs, fg->nb_inputs);
192 if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
194 fg->inputs[0]->ist = ist;
195 fg->inputs[0]->graph = fg;
197 GROW_ARRAY(ist->filters, ist->nb_filters);
198 ist->filters[ist->nb_filters - 1] = fg->inputs[0];
200 GROW_ARRAY(filtergraphs, nb_filtergraphs);
201 filtergraphs[nb_filtergraphs - 1] = fg;
206 static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
208 InputStream *ist = NULL;
209 enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
212 // TODO: support other filter types
213 if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
214 av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
223 int file_idx = strtol(in->name, &p, 0);
225 if (file_idx < 0 || file_idx >= nb_input_files) {
226 av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
227 file_idx, fg->graph_desc);
230 s = input_files[file_idx]->ctx;
232 for (i = 0; i < s->nb_streams; i++) {
233 enum AVMediaType stream_type = s->streams[i]->codec->codec_type;
234 if (stream_type != type &&
235 !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
236 type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
238 if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
244 av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
245 "matches no streams.\n", p, fg->graph_desc);
248 ist = input_streams[input_files[file_idx]->ist_index + st->index];
250 /* find the first unused stream of corresponding type */
251 for (i = 0; i < nb_input_streams; i++) {
252 ist = input_streams[i];
253 if (ist->st->codec->codec_type == type && ist->discard)
256 if (i == nb_input_streams) {
257 av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
258 "unlabeled input pad %d on filter %s\n", in->pad_idx,
259 in->filter_ctx->name);
266 ist->decoding_needed++;
267 ist->st->discard = AVDISCARD_NONE;
269 GROW_ARRAY(fg->inputs, fg->nb_inputs);
270 if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
272 fg->inputs[fg->nb_inputs - 1]->ist = ist;
273 fg->inputs[fg->nb_inputs - 1]->graph = fg;
275 GROW_ARRAY(ist->filters, ist->nb_filters);
276 ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
279 static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
282 OutputStream *ost = ofilter->ost;
283 AVCodecContext *codec = ost->st->codec;
284 AVFilterContext *last_filter = out->filter_ctx;
285 int pad_idx = out->pad_idx;
288 AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
290 snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
291 ret = avfilter_graph_create_filter(&ofilter->filter,
292 avfilter_get_by_name("buffersink"),
293 name, NULL, NULL, fg->graph);
294 av_freep(&buffersink_params);
299 if (codec->width || codec->height) {
301 AVFilterContext *filter;
303 snprintf(args, sizeof(args), "%d:%d:flags=0x%X",
306 (unsigned)ost->sws_flags);
307 snprintf(name, sizeof(name), "scaler for output stream %d:%d",
308 ost->file_index, ost->index);
309 if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
310 name, args, NULL, fg->graph)) < 0)
312 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
315 last_filter = filter;
319 if ((pix_fmts = choose_pix_fmts(ost))) {
320 AVFilterContext *filter;
321 snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
322 ost->file_index, ost->index);
323 if ((ret = avfilter_graph_create_filter(&filter,
324 avfilter_get_by_name("format"),
325 "format", pix_fmts, NULL,
328 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
331 last_filter = filter;
336 if (ost->frame_rate.num && 0) {
337 AVFilterContext *fps;
340 snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
341 ost->frame_rate.den);
342 snprintf(name, sizeof(name), "fps for output stream %d:%d",
343 ost->file_index, ost->index);
344 ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
345 name, args, NULL, fg->graph);
349 ret = avfilter_link(last_filter, pad_idx, fps, 0);
356 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
362 static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
364 OutputStream *ost = ofilter->ost;
365 AVCodecContext *codec = ost->st->codec;
366 AVFilterContext *last_filter = out->filter_ctx;
367 int pad_idx = out->pad_idx;
368 char *sample_fmts, *sample_rates, *channel_layouts;
371 AVABufferSinkParams *params = av_abuffersink_params_alloc();
374 return AVERROR(ENOMEM);
375 params->all_channel_counts = 1;
376 snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
377 ret = avfilter_graph_create_filter(&ofilter->filter,
378 avfilter_get_by_name("abuffersink"),
379 name, NULL, params, fg->graph);
384 #define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \
385 AVFilterContext *filt_ctx; \
387 av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
388 "similarly to -af " filter_name "=%s.\n", arg); \
390 ret = avfilter_graph_create_filter(&filt_ctx, \
391 avfilter_get_by_name(filter_name), \
392 filter_name, arg, NULL, fg->graph); \
396 ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0); \
400 last_filter = filt_ctx; \
403 if (ost->audio_channels_mapped) {
406 av_bprint_init(&pan_buf, 256, 8192);
407 av_bprintf(&pan_buf, "0x%"PRIx64,
408 av_get_default_channel_layout(ost->audio_channels_mapped));
409 for (i = 0; i < ost->audio_channels_mapped; i++)
410 if (ost->audio_channels_map[i] != -1)
411 av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
413 AUTO_INSERT_FILTER("-map_channel", "pan", pan_buf.str);
414 av_bprint_finalize(&pan_buf, NULL);
417 if (codec->channels && !codec->channel_layout)
418 codec->channel_layout = av_get_default_channel_layout(codec->channels);
420 sample_fmts = choose_sample_fmts(ost);
421 sample_rates = choose_sample_rates(ost);
422 channel_layouts = choose_channel_layouts(ost);
423 if (sample_fmts || sample_rates || channel_layouts) {
424 AVFilterContext *format;
429 av_strlcatf(args, sizeof(args), "sample_fmts=%s:",
432 av_strlcatf(args, sizeof(args), "sample_rates=%s:",
435 av_strlcatf(args, sizeof(args), "channel_layouts=%s:",
438 av_freep(&sample_fmts);
439 av_freep(&sample_rates);
440 av_freep(&channel_layouts);
442 snprintf(name, sizeof(name), "audio format for output stream %d:%d",
443 ost->file_index, ost->index);
444 ret = avfilter_graph_create_filter(&format,
445 avfilter_get_by_name("aformat"),
446 name, args, NULL, fg->graph);
450 ret = avfilter_link(last_filter, pad_idx, format, 0);
454 last_filter = format;
458 if (audio_volume != 256 && 0) {
461 snprintf(args, sizeof(args), "%f", audio_volume / 256.);
462 AUTO_INSERT_FILTER("-vol", "volume", args);
465 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
471 #define DESCRIBE_FILTER_LINK(f, inout, in) \
473 AVFilterContext *ctx = inout->filter_ctx; \
474 AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads; \
475 int nb_pads = in ? ctx->input_count : ctx->output_count; \
478 if (avio_open_dyn_buf(&pb) < 0) \
481 avio_printf(pb, "%s", ctx->filter->name); \
483 avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
485 avio_close_dyn_buf(pb, &f->name); \
488 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
490 av_freep(&ofilter->name);
491 DESCRIBE_FILTER_LINK(ofilter, out, 0);
493 switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
494 case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
495 case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
496 default: av_assert0(0);
500 static int sub2video_prepare(InputStream *ist)
502 AVFormatContext *avf = input_files[ist->file_index]->ctx;
505 /* Compute the size of the canvas for the subtitles stream.
506 If the subtitles codec has set a size, use it. Otherwise use the
507 maximum dimensions of the video streams in the same file. */
508 w = ist->st->codec->width;
509 h = ist->st->codec->height;
511 for (i = 0; i < avf->nb_streams; i++) {
512 if (avf->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
513 w = FFMAX(w, avf->streams[i]->codec->width);
514 h = FFMAX(h, avf->streams[i]->codec->height);
521 av_log(avf, AV_LOG_INFO, "sub2video: using %dx%d canvas\n", w, h);
523 ist->sub2video.w = ist->st->codec->width = ist->resample_width = w;
524 ist->sub2video.h = ist->st->codec->height = ist->resample_height = h;
526 /* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the
527 palettes for all rectangles are identical or compatible */
528 ist->resample_pix_fmt = ist->st->codec->pix_fmt = AV_PIX_FMT_RGB32;
530 ist->sub2video.frame = av_frame_alloc();
531 if (!ist->sub2video.frame)
532 return AVERROR(ENOMEM);
536 static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
539 AVFilterContext *first_filter = in->filter_ctx;
540 AVFilter *filter = avfilter_get_by_name("buffer");
541 InputStream *ist = ifilter->ist;
542 AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
544 AVRational fr = ist->framerate;
548 int pad_idx = in->pad_idx;
552 fr = av_guess_frame_rate(input_files[ist->file_index]->ctx, ist->st, NULL);
554 if (ist->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
555 ret = sub2video_prepare(ist);
560 sar = ist->st->sample_aspect_ratio.num ?
561 ist->st->sample_aspect_ratio :
562 ist->st->codec->sample_aspect_ratio;
564 sar = (AVRational){0,1};
565 av_bprint_init(&args, 0, 1);
567 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
568 "pixel_aspect=%d/%d:sws_param=flags=%d", ist->resample_width,
569 ist->resample_height, ist->resample_pix_fmt,
570 tb.num, tb.den, sar.num, sar.den,
571 SWS_BILINEAR + ((ist->st->codec->flags&CODEC_FLAG_BITEXACT) ? SWS_BITEXACT:0));
572 if (fr.num && fr.den)
573 av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
574 snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
575 ist->file_index, ist->st->index);
577 if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter, name,
578 args.str, NULL, fg->graph)) < 0)
581 if (ist->framerate.num) {
582 AVFilterContext *setpts;
584 snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
585 ist->file_index, ist->st->index);
586 if ((ret = avfilter_graph_create_filter(&setpts,
587 avfilter_get_by_name("setpts"),
592 if ((ret = avfilter_link(setpts, 0, first_filter, pad_idx)) < 0)
595 first_filter = setpts;
599 if (do_deinterlace) {
600 AVFilterContext *yadif;
602 snprintf(name, sizeof(name), "deinterlace input from stream %d:%d",
603 ist->file_index, ist->st->index);
604 if ((ret = avfilter_graph_create_filter(&yadif,
605 avfilter_get_by_name("yadif"),
610 if ((ret = avfilter_link(yadif, 0, first_filter, pad_idx)) < 0)
613 first_filter = yadif;
617 if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
622 static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
625 AVFilterContext *first_filter = in->filter_ctx;
626 AVFilter *filter = avfilter_get_by_name("abuffer");
627 InputStream *ist = ifilter->ist;
628 int pad_idx = in->pad_idx;
633 av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
634 av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
635 1, ist->st->codec->sample_rate,
636 ist->st->codec->sample_rate,
637 av_get_sample_fmt_name(ist->st->codec->sample_fmt));
638 if (ist->st->codec->channel_layout)
639 av_bprintf(&args, ":channel_layout=0x%"PRIx64,
640 ist->st->codec->channel_layout);
642 av_bprintf(&args, ":channels=%d", ist->st->codec->channels);
643 snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
644 ist->file_index, ist->st->index);
646 if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter,
647 name, args.str, NULL,
651 #define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do { \
652 AVFilterContext *filt_ctx; \
654 av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
655 "similarly to -af " filter_name "=%s.\n", arg); \
657 snprintf(name, sizeof(name), "graph %d %s for input stream %d:%d", \
658 fg->index, filter_name, ist->file_index, ist->st->index); \
659 ret = avfilter_graph_create_filter(&filt_ctx, \
660 avfilter_get_by_name(filter_name), \
661 name, arg, NULL, fg->graph); \
665 ret = avfilter_link(filt_ctx, 0, first_filter, pad_idx); \
669 first_filter = filt_ctx; \
672 if (audio_sync_method > 0) {
673 char args[256] = {0};
675 av_strlcatf(args, sizeof(args), "async=%d", audio_sync_method);
676 if (audio_drift_threshold != 0.1)
677 av_strlcatf(args, sizeof(args), ":min_hard_comp=%f", audio_drift_threshold);
678 if (!fg->reconfiguration)
679 av_strlcatf(args, sizeof(args), ":first_pts=0");
680 AUTO_INSERT_FILTER_INPUT("-async", "aresample", args);
683 // if (ost->audio_channels_mapped) {
686 // av_bprint_init(&pan_buf, 256, 8192);
687 // av_bprintf(&pan_buf, "0x%"PRIx64,
688 // av_get_default_channel_layout(ost->audio_channels_mapped));
689 // for (i = 0; i < ost->audio_channels_mapped; i++)
690 // if (ost->audio_channels_map[i] != -1)
691 // av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
692 // AUTO_INSERT_FILTER_INPUT("-map_channel", "pan", pan_buf.str);
693 // av_bprint_finalize(&pan_buf, NULL);
696 if (audio_volume != 256) {
699 av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
700 "audio filter instead.\n");
702 snprintf(args, sizeof(args), "%f", audio_volume / 256.);
703 AUTO_INSERT_FILTER_INPUT("-vol", "volume", args);
705 if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
711 static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
714 av_freep(&ifilter->name);
715 DESCRIBE_FILTER_LINK(ifilter, in, 1);
717 switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
718 case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
719 case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
720 default: av_assert0(0);
724 int configure_filtergraph(FilterGraph *fg)
726 AVFilterInOut *inputs, *outputs, *cur;
727 int ret, i, init = !fg->graph, simple = !fg->graph_desc;
728 const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
731 avfilter_graph_free(&fg->graph);
732 if (!(fg->graph = avfilter_graph_alloc()))
733 return AVERROR(ENOMEM);
736 OutputStream *ost = fg->outputs[0]->ost;
738 AVDictionaryEntry *e = NULL;
740 snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
741 fg->graph->scale_sws_opts = av_strdup(args);
744 while ((e = av_dict_get(ost->swr_opts, "", e,
745 AV_DICT_IGNORE_SUFFIX))) {
746 av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
749 args[strlen(args)-1] = 0;
750 av_opt_set(fg->graph, "aresample_swr_opts", args, 0);
753 while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
754 AV_DICT_IGNORE_SUFFIX))) {
755 av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
758 args[strlen(args) - 1] = '\0';
759 fg->graph->resample_lavr_opts = av_strdup(args);
762 if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
765 if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
766 av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
767 "exactly one input and output.\n", graph_desc);
768 return AVERROR(EINVAL);
771 for (cur = inputs; !simple && init && cur; cur = cur->next)
772 init_input_filter(fg, cur);
774 for (cur = inputs, i = 0; cur; cur = cur->next, i++)
775 if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
777 avfilter_inout_free(&inputs);
779 if (!init || simple) {
780 /* we already know the mappings between lavfi outputs and output streams,
781 * so we can finish the setup */
782 for (cur = outputs, i = 0; cur; cur = cur->next, i++)
783 configure_output_filter(fg, fg->outputs[i], cur);
784 avfilter_inout_free(&outputs);
786 if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
789 /* wait until output mappings are processed */
790 for (cur = outputs; cur;) {
791 GROW_ARRAY(fg->outputs, fg->nb_outputs);
792 if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]))))
794 fg->outputs[fg->nb_outputs - 1]->graph = fg;
795 fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
797 fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
801 fg->reconfiguration = 1;
805 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
808 for (i = 0; i < fg->nb_inputs; i++)
809 if (fg->inputs[i]->ist == ist)