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/avfiltergraph.h"
25 #include "libavfilter/buffersink.h"
27 #include "libavresample/avresample.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/bprint.h"
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/pixfmt.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/samplefmt.h"
39 enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodec *codec, enum AVPixelFormat target)
41 if (codec && codec->pix_fmts) {
42 const enum AVPixelFormat *p = codec->pix_fmts;
43 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target);
44 int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
45 enum AVPixelFormat best= AV_PIX_FMT_NONE;
46 if (st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
47 if (st->codec->codec_id == AV_CODEC_ID_MJPEG) {
48 p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE };
49 } else if (st->codec->codec_id == AV_CODEC_ID_LJPEG) {
50 p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUV420P,
51 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE };
54 for (; *p != AV_PIX_FMT_NONE; p++) {
55 best= avcodec_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
59 if (*p == AV_PIX_FMT_NONE) {
60 if (target != AV_PIX_FMT_NONE)
61 av_log(NULL, AV_LOG_WARNING,
62 "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
63 av_get_pix_fmt_name(target),
65 av_get_pix_fmt_name(best));
72 void choose_sample_fmt(AVStream *st, AVCodec *codec)
74 if (codec && codec->sample_fmts) {
75 const enum AVSampleFormat *p = codec->sample_fmts;
76 for (; *p != -1; p++) {
77 if (*p == st->codec->sample_fmt)
81 if((codec->capabilities & CODEC_CAP_LOSSLESS) && av_get_sample_fmt_name(st->codec->sample_fmt) > av_get_sample_fmt_name(codec->sample_fmts[0]))
82 av_log(NULL, AV_LOG_ERROR, "Conversion will not be lossless.\n");
83 if(av_get_sample_fmt_name(st->codec->sample_fmt))
84 av_log(NULL, AV_LOG_WARNING,
85 "Incompatible sample format '%s' for codec '%s', auto-selecting format '%s'\n",
86 av_get_sample_fmt_name(st->codec->sample_fmt),
88 av_get_sample_fmt_name(codec->sample_fmts[0]));
89 st->codec->sample_fmt = codec->sample_fmts[0];
94 static char *choose_pix_fmts(OutputStream *ost)
96 if (ost->keep_pix_fmt) {
98 avfilter_graph_set_auto_convert(ost->filter->graph->graph,
99 AVFILTER_AUTO_CONVERT_NONE);
100 if (ost->st->codec->pix_fmt == AV_PIX_FMT_NONE)
102 return av_strdup(av_get_pix_fmt_name(ost->st->codec->pix_fmt));
104 if (ost->st->codec->pix_fmt != AV_PIX_FMT_NONE) {
105 return av_strdup(av_get_pix_fmt_name(choose_pixel_fmt(ost->st, ost->enc, ost->st->codec->pix_fmt)));
106 } else if (ost->enc && ost->enc->pix_fmts) {
107 const enum AVPixelFormat *p;
108 AVIOContext *s = NULL;
112 if (avio_open_dyn_buf(&s) < 0)
115 p = ost->enc->pix_fmts;
116 if (ost->st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
117 if (ost->st->codec->codec_id == AV_CODEC_ID_MJPEG) {
118 p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE };
119 } else if (ost->st->codec->codec_id == AV_CODEC_ID_LJPEG) {
120 p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUV420P,
121 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE };
125 for (; *p != AV_PIX_FMT_NONE; p++) {
126 const char *name = av_get_pix_fmt_name(*p);
127 avio_printf(s, "%s:", name);
129 len = avio_close_dyn_buf(s, &ret);
136 /* Define a function for building a string containing a list of
137 * allowed formats. */
138 #define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name, separator)\
139 static char *choose_ ## var ## s(OutputStream *ost) \
141 if (ost->st->codec->var != none) { \
142 get_name(ost->st->codec->var); \
143 return av_strdup(name); \
144 } else if (ost->enc && ost->enc->supported_list) { \
146 AVIOContext *s = NULL; \
150 if (avio_open_dyn_buf(&s) < 0) \
153 for (p = ost->enc->supported_list; *p != none; p++) { \
155 avio_printf(s, "%s" separator, name); \
157 len = avio_close_dyn_buf(s, &ret); \
164 // DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE,
165 // GET_PIX_FMT_NAME, ":")
167 DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts,
168 AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME, ",")
170 DEF_CHOOSE_FORMAT(int, sample_rate, supported_samplerates, 0,
171 GET_SAMPLE_RATE_NAME, ",")
173 DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0,
174 GET_CH_LAYOUT_NAME, ",")
176 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost)
178 FilterGraph *fg = av_mallocz(sizeof(*fg));
182 fg->index = nb_filtergraphs;
184 GROW_ARRAY(fg->outputs, fg->nb_outputs);
185 if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
187 fg->outputs[0]->ost = ost;
188 fg->outputs[0]->graph = fg;
190 ost->filter = fg->outputs[0];
192 GROW_ARRAY(fg->inputs, fg->nb_inputs);
193 if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
195 fg->inputs[0]->ist = ist;
196 fg->inputs[0]->graph = fg;
198 GROW_ARRAY(ist->filters, ist->nb_filters);
199 ist->filters[ist->nb_filters - 1] = fg->inputs[0];
201 GROW_ARRAY(filtergraphs, nb_filtergraphs);
202 filtergraphs[nb_filtergraphs - 1] = fg;
207 static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
209 InputStream *ist = NULL;
210 enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
213 // TODO: support other filter types
214 if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
215 av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
224 int file_idx = strtol(in->name, &p, 0);
226 if (file_idx < 0 || file_idx >= nb_input_files) {
227 av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
228 file_idx, fg->graph_desc);
231 s = input_files[file_idx]->ctx;
233 for (i = 0; i < s->nb_streams; i++) {
234 enum AVMediaType stream_type = s->streams[i]->codec->codec_type;
235 if (stream_type != type &&
236 !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
237 type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
239 if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
245 av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
246 "matches no streams.\n", p, fg->graph_desc);
249 ist = input_streams[input_files[file_idx]->ist_index + st->index];
251 /* find the first unused stream of corresponding type */
252 for (i = 0; i < nb_input_streams; i++) {
253 ist = input_streams[i];
254 if (ist->st->codec->codec_type == type && ist->discard)
257 if (i == nb_input_streams) {
258 av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
259 "unlabeled input pad %d on filter %s\n", in->pad_idx,
260 in->filter_ctx->name);
267 ist->decoding_needed++;
268 ist->st->discard = AVDISCARD_NONE;
270 GROW_ARRAY(fg->inputs, fg->nb_inputs);
271 if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
273 fg->inputs[fg->nb_inputs - 1]->ist = ist;
274 fg->inputs[fg->nb_inputs - 1]->graph = fg;
276 GROW_ARRAY(ist->filters, ist->nb_filters);
277 ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
280 static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
283 OutputStream *ost = ofilter->ost;
284 AVCodecContext *codec = ost->st->codec;
285 AVFilterContext *last_filter = out->filter_ctx;
286 int pad_idx = out->pad_idx;
289 AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
291 snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
292 ret = avfilter_graph_create_filter(&ofilter->filter,
293 avfilter_get_by_name("buffersink"),
294 name, NULL, NULL, fg->graph);
295 av_freep(&buffersink_params);
300 if (codec->width || codec->height) {
302 AVFilterContext *filter;
304 snprintf(args, sizeof(args), "%d:%d:flags=0x%X",
307 (unsigned)ost->sws_flags);
308 snprintf(name, sizeof(name), "scaler for output stream %d:%d",
309 ost->file_index, ost->index);
310 if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
311 name, args, NULL, fg->graph)) < 0)
313 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
316 last_filter = filter;
320 if ((pix_fmts = choose_pix_fmts(ost))) {
321 AVFilterContext *filter;
322 snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
323 ost->file_index, ost->index);
324 if ((ret = avfilter_graph_create_filter(&filter,
325 avfilter_get_by_name("format"),
326 "format", pix_fmts, NULL,
329 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
332 last_filter = filter;
337 if (ost->frame_rate.num && 0) {
338 AVFilterContext *fps;
341 snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
342 ost->frame_rate.den);
343 snprintf(name, sizeof(name), "fps for output stream %d:%d",
344 ost->file_index, ost->index);
345 ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
346 name, args, NULL, fg->graph);
350 ret = avfilter_link(last_filter, pad_idx, fps, 0);
357 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
363 static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
365 OutputStream *ost = ofilter->ost;
366 AVCodecContext *codec = ost->st->codec;
367 AVFilterContext *last_filter = out->filter_ctx;
368 int pad_idx = out->pad_idx;
369 char *sample_fmts, *sample_rates, *channel_layouts;
372 AVABufferSinkParams *params = av_abuffersink_params_alloc();
375 return AVERROR(ENOMEM);
376 params->all_channel_counts = 1;
377 snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
378 ret = avfilter_graph_create_filter(&ofilter->filter,
379 avfilter_get_by_name("abuffersink"),
380 name, NULL, params, fg->graph);
385 #define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \
386 AVFilterContext *filt_ctx; \
388 av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
389 "similarly to -af " filter_name "=%s.\n", arg); \
391 ret = avfilter_graph_create_filter(&filt_ctx, \
392 avfilter_get_by_name(filter_name), \
393 filter_name, arg, NULL, fg->graph); \
397 ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0); \
401 last_filter = filt_ctx; \
404 if (ost->audio_channels_mapped) {
407 av_bprint_init(&pan_buf, 256, 8192);
408 av_bprintf(&pan_buf, "0x%"PRIx64,
409 av_get_default_channel_layout(ost->audio_channels_mapped));
410 for (i = 0; i < ost->audio_channels_mapped; i++)
411 if (ost->audio_channels_map[i] != -1)
412 av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
414 AUTO_INSERT_FILTER("-map_channel", "pan", pan_buf.str);
415 av_bprint_finalize(&pan_buf, NULL);
418 if (codec->channels && !codec->channel_layout)
419 codec->channel_layout = av_get_default_channel_layout(codec->channels);
421 sample_fmts = choose_sample_fmts(ost);
422 sample_rates = choose_sample_rates(ost);
423 channel_layouts = choose_channel_layouts(ost);
424 if (sample_fmts || sample_rates || channel_layouts) {
425 AVFilterContext *format;
430 av_strlcatf(args, sizeof(args), "sample_fmts=%s:",
433 av_strlcatf(args, sizeof(args), "sample_rates=%s:",
436 av_strlcatf(args, sizeof(args), "channel_layouts=%s:",
439 av_freep(&sample_fmts);
440 av_freep(&sample_rates);
441 av_freep(&channel_layouts);
443 snprintf(name, sizeof(name), "audio format for output stream %d:%d",
444 ost->file_index, ost->index);
445 ret = avfilter_graph_create_filter(&format,
446 avfilter_get_by_name("aformat"),
447 name, args, NULL, fg->graph);
451 ret = avfilter_link(last_filter, pad_idx, format, 0);
455 last_filter = format;
459 if (audio_volume != 256 && 0) {
462 snprintf(args, sizeof(args), "%f", audio_volume / 256.);
463 AUTO_INSERT_FILTER("-vol", "volume", args);
466 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
472 #define DESCRIBE_FILTER_LINK(f, inout, in) \
474 AVFilterContext *ctx = inout->filter_ctx; \
475 AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads; \
476 int nb_pads = in ? ctx->input_count : ctx->output_count; \
479 if (avio_open_dyn_buf(&pb) < 0) \
482 avio_printf(pb, "%s", ctx->filter->name); \
484 avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
486 avio_close_dyn_buf(pb, &f->name); \
489 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
491 av_freep(&ofilter->name);
492 DESCRIBE_FILTER_LINK(ofilter, out, 0);
494 switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
495 case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
496 case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
497 default: av_assert0(0);
501 static int sub2video_prepare(InputStream *ist)
503 AVFormatContext *avf = input_files[ist->file_index]->ctx;
506 /* Compute the size of the canvas for the subtitles stream.
507 If the subtitles codec has set a size, use it. Otherwise use the
508 maximum dimensions of the video streams in the same file. */
509 w = ist->st->codec->width;
510 h = ist->st->codec->height;
512 for (i = 0; i < avf->nb_streams; i++) {
513 if (avf->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
514 w = FFMAX(w, avf->streams[i]->codec->width);
515 h = FFMAX(h, avf->streams[i]->codec->height);
522 av_log(avf, AV_LOG_INFO, "sub2video: using %dx%d canvas\n", w, h);
524 ist->sub2video.w = ist->st->codec->width = ist->resample_width = w;
525 ist->sub2video.h = ist->st->codec->height = ist->resample_height = h;
527 /* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the
528 palettes for all rectangles are identical or compatible */
529 ist->resample_pix_fmt = ist->st->codec->pix_fmt = AV_PIX_FMT_RGB32;
531 ist->sub2video.frame = av_frame_alloc();
532 if (!ist->sub2video.frame)
533 return AVERROR(ENOMEM);
537 static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
540 AVFilterContext *first_filter = in->filter_ctx;
541 AVFilter *filter = avfilter_get_by_name("buffer");
542 InputStream *ist = ifilter->ist;
543 AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
545 AVRational fr = ist->framerate;
549 int pad_idx = in->pad_idx;
553 fr = av_guess_frame_rate(input_files[ist->file_index]->ctx, ist->st, NULL);
555 if (ist->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
556 ret = sub2video_prepare(ist);
561 sar = ist->st->sample_aspect_ratio.num ?
562 ist->st->sample_aspect_ratio :
563 ist->st->codec->sample_aspect_ratio;
565 sar = (AVRational){0,1};
566 av_bprint_init(&args, 0, 1);
568 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
569 "pixel_aspect=%d/%d:sws_param=flags=%d", ist->resample_width,
570 ist->resample_height, ist->resample_pix_fmt,
571 tb.num, tb.den, sar.num, sar.den,
572 SWS_BILINEAR + ((ist->st->codec->flags&CODEC_FLAG_BITEXACT) ? SWS_BITEXACT:0));
573 if (fr.num && fr.den)
574 av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
575 snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
576 ist->file_index, ist->st->index);
578 if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter, name,
579 args.str, NULL, fg->graph)) < 0)
582 if (ist->framerate.num) {
583 AVFilterContext *setpts;
585 snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
586 ist->file_index, ist->st->index);
587 if ((ret = avfilter_graph_create_filter(&setpts,
588 avfilter_get_by_name("setpts"),
593 if ((ret = avfilter_link(setpts, 0, first_filter, pad_idx)) < 0)
596 first_filter = setpts;
600 if (do_deinterlace) {
601 AVFilterContext *yadif;
603 snprintf(name, sizeof(name), "deinterlace input from stream %d:%d",
604 ist->file_index, ist->st->index);
605 if ((ret = avfilter_graph_create_filter(&yadif,
606 avfilter_get_by_name("yadif"),
611 if ((ret = avfilter_link(yadif, 0, first_filter, pad_idx)) < 0)
614 first_filter = yadif;
618 if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
623 static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
626 AVFilterContext *first_filter = in->filter_ctx;
627 AVFilter *filter = avfilter_get_by_name("abuffer");
628 InputStream *ist = ifilter->ist;
629 int pad_idx = in->pad_idx;
634 av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
635 av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
636 1, ist->st->codec->sample_rate,
637 ist->st->codec->sample_rate,
638 av_get_sample_fmt_name(ist->st->codec->sample_fmt));
639 if (ist->st->codec->channel_layout)
640 av_bprintf(&args, ":channel_layout=0x%"PRIx64,
641 ist->st->codec->channel_layout);
643 av_bprintf(&args, ":channels=%d", ist->st->codec->channels);
644 snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
645 ist->file_index, ist->st->index);
647 if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter,
648 name, args.str, NULL,
652 #define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do { \
653 AVFilterContext *filt_ctx; \
655 av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
656 "similarly to -af " filter_name "=%s.\n", arg); \
658 snprintf(name, sizeof(name), "graph %d %s for input stream %d:%d", \
659 fg->index, filter_name, ist->file_index, ist->st->index); \
660 ret = avfilter_graph_create_filter(&filt_ctx, \
661 avfilter_get_by_name(filter_name), \
662 name, arg, NULL, fg->graph); \
666 ret = avfilter_link(filt_ctx, 0, first_filter, pad_idx); \
670 first_filter = filt_ctx; \
673 if (audio_sync_method > 0) {
674 char args[256] = {0};
676 av_strlcatf(args, sizeof(args), "async=%d", audio_sync_method);
677 if (audio_drift_threshold != 0.1)
678 av_strlcatf(args, sizeof(args), ":min_hard_comp=%f", audio_drift_threshold);
679 if (!fg->reconfiguration)
680 av_strlcatf(args, sizeof(args), ":first_pts=0");
681 AUTO_INSERT_FILTER_INPUT("-async", "aresample", args);
684 // if (ost->audio_channels_mapped) {
687 // av_bprint_init(&pan_buf, 256, 8192);
688 // av_bprintf(&pan_buf, "0x%"PRIx64,
689 // av_get_default_channel_layout(ost->audio_channels_mapped));
690 // for (i = 0; i < ost->audio_channels_mapped; i++)
691 // if (ost->audio_channels_map[i] != -1)
692 // av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
693 // AUTO_INSERT_FILTER_INPUT("-map_channel", "pan", pan_buf.str);
694 // av_bprint_finalize(&pan_buf, NULL);
697 if (audio_volume != 256) {
700 av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
701 "audio filter instead.\n");
703 snprintf(args, sizeof(args), "%f", audio_volume / 256.);
704 AUTO_INSERT_FILTER_INPUT("-vol", "volume", args);
706 if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
712 static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
715 av_freep(&ifilter->name);
716 DESCRIBE_FILTER_LINK(ifilter, in, 1);
718 switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
719 case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
720 case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
721 default: av_assert0(0);
725 int configure_filtergraph(FilterGraph *fg)
727 AVFilterInOut *inputs, *outputs, *cur;
728 int ret, i, init = !fg->graph, simple = !fg->graph_desc;
729 const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
732 avfilter_graph_free(&fg->graph);
733 if (!(fg->graph = avfilter_graph_alloc()))
734 return AVERROR(ENOMEM);
737 OutputStream *ost = fg->outputs[0]->ost;
739 AVDictionaryEntry *e = NULL;
741 snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
742 fg->graph->scale_sws_opts = av_strdup(args);
745 while ((e = av_dict_get(ost->swr_opts, "", e,
746 AV_DICT_IGNORE_SUFFIX))) {
747 av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
750 args[strlen(args)-1] = 0;
751 av_opt_set(fg->graph, "aresample_swr_opts", args, 0);
754 while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
755 AV_DICT_IGNORE_SUFFIX))) {
756 av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
759 args[strlen(args) - 1] = '\0';
760 fg->graph->resample_lavr_opts = av_strdup(args);
763 if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
766 if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
767 av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
768 "exactly one input and output.\n", graph_desc);
769 return AVERROR(EINVAL);
772 for (cur = inputs; !simple && init && cur; cur = cur->next)
773 init_input_filter(fg, cur);
775 for (cur = inputs, i = 0; cur; cur = cur->next, i++)
776 if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
778 avfilter_inout_free(&inputs);
780 if (!init || simple) {
781 /* we already know the mappings between lavfi outputs and output streams,
782 * so we can finish the setup */
783 for (cur = outputs, i = 0; cur; cur = cur->next, i++)
784 configure_output_filter(fg, fg->outputs[i], cur);
785 avfilter_inout_free(&outputs);
787 if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
790 /* wait until output mappings are processed */
791 for (cur = outputs; cur;) {
792 GROW_ARRAY(fg->outputs, fg->nb_outputs);
793 if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]))))
795 fg->outputs[fg->nb_outputs - 1]->graph = fg;
796 fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
798 fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
802 fg->reconfiguration = 1;
806 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
809 for (i = 0; i < fg->nb_inputs; i++)
810 if (fg->inputs[i]->ist == ist)