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 "libavutil/avassert.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/bprint.h"
30 #include "libavutil/channel_layout.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/pixfmt.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/samplefmt.h"
37 enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodec *codec, enum AVPixelFormat target)
39 if (codec && codec->pix_fmts) {
40 const enum AVPixelFormat *p = codec->pix_fmts;
41 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target);
42 int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
43 enum AVPixelFormat best= AV_PIX_FMT_NONE;
44 if (st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
45 if (st->codec->codec_id == AV_CODEC_ID_MJPEG) {
46 p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE };
47 } else if (st->codec->codec_id == AV_CODEC_ID_LJPEG) {
48 p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUV420P,
49 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE };
52 for (; *p != AV_PIX_FMT_NONE; p++) {
53 best= avcodec_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
57 if (*p == AV_PIX_FMT_NONE) {
58 if (target != AV_PIX_FMT_NONE)
59 av_log(NULL, AV_LOG_WARNING,
60 "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
61 av_get_pix_fmt_name(target),
63 av_get_pix_fmt_name(best));
70 void choose_sample_fmt(AVStream *st, AVCodec *codec)
72 if (codec && codec->sample_fmts) {
73 const enum AVSampleFormat *p = codec->sample_fmts;
74 for (; *p != -1; p++) {
75 if (*p == st->codec->sample_fmt)
79 if((codec->capabilities & CODEC_CAP_LOSSLESS) && av_get_sample_fmt_name(st->codec->sample_fmt) > av_get_sample_fmt_name(codec->sample_fmts[0]))
80 av_log(NULL, AV_LOG_ERROR, "Conversion will not be lossless.\n");
81 if(av_get_sample_fmt_name(st->codec->sample_fmt))
82 av_log(NULL, AV_LOG_WARNING,
83 "Incompatible sample format '%s' for codec '%s', auto-selecting format '%s'\n",
84 av_get_sample_fmt_name(st->codec->sample_fmt),
86 av_get_sample_fmt_name(codec->sample_fmts[0]));
87 st->codec->sample_fmt = codec->sample_fmts[0];
92 static char *choose_pix_fmts(OutputStream *ost)
94 if (ost->keep_pix_fmt) {
96 avfilter_graph_set_auto_convert(ost->filter->graph->graph,
97 AVFILTER_AUTO_CONVERT_NONE);
98 if (ost->st->codec->pix_fmt == AV_PIX_FMT_NONE)
100 return av_strdup(av_get_pix_fmt_name(ost->st->codec->pix_fmt));
102 if (ost->st->codec->pix_fmt != AV_PIX_FMT_NONE) {
103 return av_strdup(av_get_pix_fmt_name(choose_pixel_fmt(ost->st, ost->enc, ost->st->codec->pix_fmt)));
104 } else if (ost->enc && ost->enc->pix_fmts) {
105 const enum AVPixelFormat *p;
106 AVIOContext *s = NULL;
110 if (avio_open_dyn_buf(&s) < 0)
113 p = ost->enc->pix_fmts;
114 if (ost->st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
115 if (ost->st->codec->codec_id == AV_CODEC_ID_MJPEG) {
116 p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE };
117 } else if (ost->st->codec->codec_id == AV_CODEC_ID_LJPEG) {
118 p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUV420P,
119 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE };
123 for (; *p != AV_PIX_FMT_NONE; p++) {
124 const char *name = av_get_pix_fmt_name(*p);
125 avio_printf(s, "%s:", name);
127 len = avio_close_dyn_buf(s, &ret);
134 /* Define a function for building a string containing a list of
135 * allowed formats. */
136 #define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name, separator)\
137 static char *choose_ ## var ## s(OutputStream *ost) \
139 if (ost->st->codec->var != none) { \
140 get_name(ost->st->codec->var); \
141 return av_strdup(name); \
142 } else if (ost->enc->supported_list) { \
144 AVIOContext *s = NULL; \
148 if (avio_open_dyn_buf(&s) < 0) \
151 for (p = ost->enc->supported_list; *p != none; p++) { \
153 avio_printf(s, "%s" separator, name); \
155 len = avio_close_dyn_buf(s, &ret); \
162 // DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE,
163 // GET_PIX_FMT_NAME, ":")
165 DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts,
166 AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME, ",")
168 DEF_CHOOSE_FORMAT(int, sample_rate, supported_samplerates, 0,
169 GET_SAMPLE_RATE_NAME, ",")
171 DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0,
172 GET_CH_LAYOUT_NAME, ",")
174 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost)
176 FilterGraph *fg = av_mallocz(sizeof(*fg));
180 fg->index = nb_filtergraphs;
182 GROW_ARRAY(fg->outputs, fg->nb_outputs);
183 if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
185 fg->outputs[0]->ost = ost;
186 fg->outputs[0]->graph = fg;
188 ost->filter = fg->outputs[0];
190 GROW_ARRAY(fg->inputs, fg->nb_inputs);
191 if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
193 fg->inputs[0]->ist = ist;
194 fg->inputs[0]->graph = fg;
196 GROW_ARRAY(ist->filters, ist->nb_filters);
197 ist->filters[ist->nb_filters - 1] = fg->inputs[0];
199 GROW_ARRAY(filtergraphs, nb_filtergraphs);
200 filtergraphs[nb_filtergraphs - 1] = fg;
205 static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
207 InputStream *ist = NULL;
208 enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
211 // TODO: support other filter types
212 if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
213 av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
222 int file_idx = strtol(in->name, &p, 0);
224 if (file_idx < 0 || file_idx >= nb_input_files) {
225 av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
226 file_idx, fg->graph_desc);
229 s = input_files[file_idx]->ctx;
231 for (i = 0; i < s->nb_streams; i++) {
232 enum AVMediaType stream_type = s->streams[i]->codec->codec_type;
233 if (stream_type != type &&
234 !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
235 type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
237 if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
243 av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
244 "matches no streams.\n", p, fg->graph_desc);
247 ist = input_streams[input_files[file_idx]->ist_index + st->index];
249 /* find the first unused stream of corresponding type */
250 for (i = 0; i < nb_input_streams; i++) {
251 ist = input_streams[i];
252 if (ist->st->codec->codec_type == type && ist->discard)
255 if (i == nb_input_streams) {
256 av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
257 "unlabeled input pad %d on filter %s\n", in->pad_idx,
258 in->filter_ctx->name);
265 ist->decoding_needed++;
266 ist->st->discard = AVDISCARD_NONE;
268 GROW_ARRAY(fg->inputs, fg->nb_inputs);
269 if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
271 fg->inputs[fg->nb_inputs - 1]->ist = ist;
272 fg->inputs[fg->nb_inputs - 1]->graph = fg;
274 GROW_ARRAY(ist->filters, ist->nb_filters);
275 ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
278 static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
281 OutputStream *ost = ofilter->ost;
282 AVCodecContext *codec = ost->st->codec;
283 AVFilterContext *last_filter = out->filter_ctx;
284 int pad_idx = out->pad_idx;
287 AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
289 snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
290 ret = avfilter_graph_create_filter(&ofilter->filter,
291 avfilter_get_by_name("ffbuffersink"),
292 name, NULL, NULL, fg->graph);
293 av_freep(&buffersink_params);
298 if (codec->width || codec->height) {
300 AVFilterContext *filter;
302 snprintf(args, sizeof(args), "%d:%d:flags=0x%X",
305 (unsigned)ost->sws_flags);
306 snprintf(name, sizeof(name), "scaler for output stream %d:%d",
307 ost->file_index, ost->index);
308 if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
309 name, args, NULL, fg->graph)) < 0)
311 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
314 last_filter = filter;
318 if ((pix_fmts = choose_pix_fmts(ost))) {
319 AVFilterContext *filter;
320 snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
321 ost->file_index, ost->index);
322 if ((ret = avfilter_graph_create_filter(&filter,
323 avfilter_get_by_name("format"),
324 "format", pix_fmts, NULL,
327 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
330 last_filter = filter;
335 if (ost->frame_rate.num && 0) {
336 AVFilterContext *fps;
339 snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
340 ost->frame_rate.den);
341 snprintf(name, sizeof(name), "fps for output stream %d:%d",
342 ost->file_index, ost->index);
343 ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
344 name, args, NULL, fg->graph);
348 ret = avfilter_link(last_filter, pad_idx, fps, 0);
355 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
361 static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
363 OutputStream *ost = ofilter->ost;
364 AVCodecContext *codec = ost->st->codec;
365 AVFilterContext *last_filter = out->filter_ctx;
366 int pad_idx = out->pad_idx;
367 char *sample_fmts, *sample_rates, *channel_layouts;
370 AVABufferSinkParams *params = av_abuffersink_params_alloc();
373 return AVERROR(ENOMEM);
374 params->all_channel_counts = 1;
375 snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
376 ret = avfilter_graph_create_filter(&ofilter->filter,
377 avfilter_get_by_name("ffabuffersink"),
378 name, NULL, params, fg->graph);
383 #define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \
384 AVFilterContext *filt_ctx; \
386 av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
387 "similarly to -af " filter_name "=%s.\n", arg); \
389 ret = avfilter_graph_create_filter(&filt_ctx, \
390 avfilter_get_by_name(filter_name), \
391 filter_name, arg, NULL, fg->graph); \
395 ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0); \
399 last_filter = filt_ctx; \
402 if (ost->audio_channels_mapped) {
405 av_bprint_init(&pan_buf, 256, 8192);
406 av_bprintf(&pan_buf, "0x%"PRIx64,
407 av_get_default_channel_layout(ost->audio_channels_mapped));
408 for (i = 0; i < ost->audio_channels_mapped; i++)
409 if (ost->audio_channels_map[i] != -1)
410 av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
412 AUTO_INSERT_FILTER("-map_channel", "pan", pan_buf.str);
413 av_bprint_finalize(&pan_buf, NULL);
416 if (codec->channels && !codec->channel_layout)
417 codec->channel_layout = av_get_default_channel_layout(codec->channels);
419 sample_fmts = choose_sample_fmts(ost);
420 sample_rates = choose_sample_rates(ost);
421 channel_layouts = choose_channel_layouts(ost);
422 if (sample_fmts || sample_rates || channel_layouts) {
423 AVFilterContext *format;
428 av_strlcatf(args, sizeof(args), "sample_fmts=%s:",
431 av_strlcatf(args, sizeof(args), "sample_rates=%s:",
434 av_strlcatf(args, sizeof(args), "channel_layouts=%s:",
437 av_freep(&sample_fmts);
438 av_freep(&sample_rates);
439 av_freep(&channel_layouts);
441 snprintf(name, sizeof(name), "audio format for output stream %d:%d",
442 ost->file_index, ost->index);
443 ret = avfilter_graph_create_filter(&format,
444 avfilter_get_by_name("aformat"),
445 name, args, NULL, fg->graph);
449 ret = avfilter_link(last_filter, pad_idx, format, 0);
453 last_filter = format;
457 if (audio_volume != 256 && 0) {
460 snprintf(args, sizeof(args), "%f", audio_volume / 256.);
461 AUTO_INSERT_FILTER("-vol", "volume", args);
464 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
470 #define DESCRIBE_FILTER_LINK(f, inout, in) \
472 AVFilterContext *ctx = inout->filter_ctx; \
473 AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads; \
474 int nb_pads = in ? ctx->input_count : ctx->output_count; \
477 if (avio_open_dyn_buf(&pb) < 0) \
480 avio_printf(pb, "%s", ctx->filter->name); \
482 avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
484 avio_close_dyn_buf(pb, &f->name); \
487 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
489 av_freep(&ofilter->name);
490 DESCRIBE_FILTER_LINK(ofilter, out, 0);
492 switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
493 case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
494 case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
495 default: av_assert0(0);
499 static int sub2video_prepare(InputStream *ist)
501 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->st->codec->pix_fmt = AV_PIX_FMT_RGB32;
531 ret = av_image_alloc(image, linesize, w, h, AV_PIX_FMT_RGB32, 32);
534 memset(image[0], 0, h * linesize[0]);
535 ist->sub2video.ref = avfilter_get_video_buffer_ref_from_arrays(
536 image, linesize, AV_PERM_READ | AV_PERM_PRESERVE,
537 w, h, AV_PIX_FMT_RGB32);
538 if (!ist->sub2video.ref) {
540 return AVERROR(ENOMEM);
545 static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
548 AVFilterContext *first_filter = in->filter_ctx;
549 AVFilter *filter = avfilter_get_by_name("buffer");
550 InputStream *ist = ifilter->ist;
551 AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
553 AVRational fr = ist->framerate.num ? ist->framerate :
554 ist->st->r_frame_rate;
558 int pad_idx = in->pad_idx;
561 if (!ist->framerate.num && ist->st->codec->ticks_per_frame>1) {
562 AVRational codec_fr = av_inv_q(ist->st->codec->time_base);
563 AVRational avg_fr = ist->st->avg_frame_rate;
564 codec_fr.den *= ist->st->codec->ticks_per_frame;
565 if ( codec_fr.num>0 && codec_fr.den>0 && av_q2d(codec_fr) < av_q2d(fr)*0.7
566 && fabs(1.0 - av_q2d(av_div_q(avg_fr, fr)))>0.1)
570 if (ist->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
571 ret = sub2video_prepare(ist);
576 sar = ist->st->sample_aspect_ratio.num ?
577 ist->st->sample_aspect_ratio :
578 ist->st->codec->sample_aspect_ratio;
580 sar = (AVRational){0,1};
581 av_bprint_init(&args, 0, 1);
583 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
584 "pixel_aspect=%d/%d:sws_param=flags=%d", ist->resample_width,
585 ist->resample_height, ist->resample_pix_fmt,
586 tb.num, tb.den, sar.num, sar.den,
587 SWS_BILINEAR + ((ist->st->codec->flags&CODEC_FLAG_BITEXACT) ? SWS_BITEXACT:0));
588 if (fr.num && fr.den)
589 av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
590 snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
591 ist->file_index, ist->st->index);
593 if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter, name,
594 args.str, NULL, fg->graph)) < 0)
597 if (ist->framerate.num) {
598 AVFilterContext *setpts;
600 snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
601 ist->file_index, ist->st->index);
602 if ((ret = avfilter_graph_create_filter(&setpts,
603 avfilter_get_by_name("setpts"),
608 if ((ret = avfilter_link(setpts, 0, first_filter, pad_idx)) < 0)
611 first_filter = setpts;
615 if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
620 static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
623 AVFilterContext *first_filter = in->filter_ctx;
624 AVFilter *filter = avfilter_get_by_name("abuffer");
625 InputStream *ist = ifilter->ist;
626 int pad_idx = in->pad_idx;
631 av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
632 av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
633 1, ist->st->codec->sample_rate,
634 ist->st->codec->sample_rate,
635 av_get_sample_fmt_name(ist->st->codec->sample_fmt));
636 if (ist->st->codec->channel_layout)
637 av_bprintf(&args, ":channel_layout=0x%"PRIx64,
638 ist->st->codec->channel_layout);
640 av_bprintf(&args, ":channels=%d", ist->st->codec->channels);
641 snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
642 ist->file_index, ist->st->index);
644 if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter,
645 name, args.str, NULL,
649 #define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do { \
650 AVFilterContext *filt_ctx; \
652 av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
653 "similarly to -af " filter_name "=%s.\n", arg); \
655 snprintf(name, sizeof(name), "graph %d %s for input stream %d:%d", \
656 fg->index, filter_name, ist->file_index, ist->st->index); \
657 ret = avfilter_graph_create_filter(&filt_ctx, \
658 avfilter_get_by_name(filter_name), \
659 name, arg, NULL, fg->graph); \
663 ret = avfilter_link(filt_ctx, 0, first_filter, pad_idx); \
667 first_filter = filt_ctx; \
670 if (audio_sync_method > 0) {
671 char args[256] = {0};
673 av_strlcatf(args, sizeof(args), "async=%d", audio_sync_method);
674 if (audio_drift_threshold != 0.1)
675 av_strlcatf(args, sizeof(args), ":min_hard_comp=%f", audio_drift_threshold);
676 AUTO_INSERT_FILTER_INPUT("-async", "aresample", args);
679 // if (ost->audio_channels_mapped) {
682 // av_bprint_init(&pan_buf, 256, 8192);
683 // av_bprintf(&pan_buf, "0x%"PRIx64,
684 // av_get_default_channel_layout(ost->audio_channels_mapped));
685 // for (i = 0; i < ost->audio_channels_mapped; i++)
686 // if (ost->audio_channels_map[i] != -1)
687 // av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
688 // AUTO_INSERT_FILTER_INPUT("-map_channel", "pan", pan_buf.str);
689 // av_bprint_finalize(&pan_buf, NULL);
692 if (audio_volume != 256) {
695 av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
696 "audio filter instead.\n");
698 snprintf(args, sizeof(args), "%f", audio_volume / 256.);
699 AUTO_INSERT_FILTER_INPUT("-vol", "volume", args);
701 if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
707 static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
710 av_freep(&ifilter->name);
711 DESCRIBE_FILTER_LINK(ifilter, in, 1);
713 switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
714 case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
715 case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
716 default: av_assert0(0);
720 int configure_filtergraph(FilterGraph *fg)
722 AVFilterInOut *inputs, *outputs, *cur;
723 int ret, i, init = !fg->graph, simple = !fg->graph_desc;
724 const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
727 avfilter_graph_free(&fg->graph);
728 if (!(fg->graph = avfilter_graph_alloc()))
729 return AVERROR(ENOMEM);
732 OutputStream *ost = fg->outputs[0]->ost;
734 snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
735 fg->graph->scale_sws_opts = av_strdup(args);
738 if (ost->swr_filter_type != SWR_FILTER_TYPE_KAISER)
739 av_strlcatf(args, sizeof(args), "filter_type=%d:", (int)ost->swr_filter_type);
740 if (ost->swr_dither_method)
741 av_strlcatf(args, sizeof(args), "dither_method=%d:", (int)ost->swr_dither_method);
742 if (ost->swr_dither_scale != 1.0)
743 av_strlcatf(args, sizeof(args), "dither_scale=%f:", ost->swr_dither_scale);
745 args[strlen(args)-1] = 0;
746 av_opt_set(fg->graph, "aresample_swr_opts", args, 0);
749 if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
752 if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
753 av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
754 "exactly one input and output.\n", graph_desc);
755 return AVERROR(EINVAL);
758 for (cur = inputs; !simple && init && cur; cur = cur->next)
759 init_input_filter(fg, cur);
761 for (cur = inputs, i = 0; cur; cur = cur->next, i++)
762 if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
764 avfilter_inout_free(&inputs);
766 if (!init || simple) {
767 /* we already know the mappings between lavfi outputs and output streams,
768 * so we can finish the setup */
769 for (cur = outputs, i = 0; cur; cur = cur->next, i++)
770 configure_output_filter(fg, fg->outputs[i], cur);
771 avfilter_inout_free(&outputs);
773 if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
776 /* wait until output mappings are processed */
777 for (cur = outputs; cur;) {
778 GROW_ARRAY(fg->outputs, fg->nb_outputs);
779 if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]))))
781 fg->outputs[fg->nb_outputs - 1]->graph = fg;
782 fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
784 fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
791 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
794 for (i = 0; i < fg->nb_inputs; i++)
795 if (fg->inputs[i]->ist == ist)