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 AVDictionaryEntry *strict_dict = av_dict_get(ost->opts, "strict", NULL, 0);
97 // used by choose_pixel_fmt() and below
98 av_opt_set(ost->st->codec, "strict", strict_dict->value, 0);
100 if (ost->keep_pix_fmt) {
102 avfilter_graph_set_auto_convert(ost->filter->graph->graph,
103 AVFILTER_AUTO_CONVERT_NONE);
104 if (ost->st->codec->pix_fmt == AV_PIX_FMT_NONE)
106 return av_strdup(av_get_pix_fmt_name(ost->st->codec->pix_fmt));
108 if (ost->st->codec->pix_fmt != AV_PIX_FMT_NONE) {
109 return av_strdup(av_get_pix_fmt_name(choose_pixel_fmt(ost->st, ost->enc, ost->st->codec->pix_fmt)));
110 } else if (ost->enc && ost->enc->pix_fmts) {
111 const enum AVPixelFormat *p;
112 AVIOContext *s = NULL;
116 if (avio_open_dyn_buf(&s) < 0)
119 p = ost->enc->pix_fmts;
120 if (ost->st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
121 if (ost->st->codec->codec_id == AV_CODEC_ID_MJPEG) {
122 p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE };
123 } else if (ost->st->codec->codec_id == AV_CODEC_ID_LJPEG) {
124 p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUV420P,
125 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE };
129 for (; *p != AV_PIX_FMT_NONE; p++) {
130 const char *name = av_get_pix_fmt_name(*p);
131 avio_printf(s, "%s|", name);
133 len = avio_close_dyn_buf(s, &ret);
140 /* Define a function for building a string containing a list of
141 * allowed formats. */
142 #define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name) \
143 static char *choose_ ## var ## s(OutputStream *ost) \
145 if (ost->st->codec->var != none) { \
146 get_name(ost->st->codec->var); \
147 return av_strdup(name); \
148 } else if (ost->enc && ost->enc->supported_list) { \
150 AVIOContext *s = NULL; \
154 if (avio_open_dyn_buf(&s) < 0) \
157 for (p = ost->enc->supported_list; *p != none; p++) { \
159 avio_printf(s, "%s|", name); \
161 len = avio_close_dyn_buf(s, &ret); \
168 // DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE,
171 DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts,
172 AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME)
174 DEF_CHOOSE_FORMAT(int, sample_rate, supported_samplerates, 0,
175 GET_SAMPLE_RATE_NAME)
177 DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0,
180 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost)
182 FilterGraph *fg = av_mallocz(sizeof(*fg));
186 fg->index = nb_filtergraphs;
188 GROW_ARRAY(fg->outputs, fg->nb_outputs);
189 if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
191 fg->outputs[0]->ost = ost;
192 fg->outputs[0]->graph = fg;
194 ost->filter = fg->outputs[0];
196 GROW_ARRAY(fg->inputs, fg->nb_inputs);
197 if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
199 fg->inputs[0]->ist = ist;
200 fg->inputs[0]->graph = fg;
202 GROW_ARRAY(ist->filters, ist->nb_filters);
203 ist->filters[ist->nb_filters - 1] = fg->inputs[0];
205 GROW_ARRAY(filtergraphs, nb_filtergraphs);
206 filtergraphs[nb_filtergraphs - 1] = fg;
211 static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
213 InputStream *ist = NULL;
214 enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
217 // TODO: support other filter types
218 if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
219 av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
228 int file_idx = strtol(in->name, &p, 0);
230 if (file_idx < 0 || file_idx >= nb_input_files) {
231 av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
232 file_idx, fg->graph_desc);
235 s = input_files[file_idx]->ctx;
237 for (i = 0; i < s->nb_streams; i++) {
238 enum AVMediaType stream_type = s->streams[i]->codec->codec_type;
239 if (stream_type != type &&
240 !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
241 type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
243 if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
249 av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
250 "matches no streams.\n", p, fg->graph_desc);
253 ist = input_streams[input_files[file_idx]->ist_index + st->index];
255 /* find the first unused stream of corresponding type */
256 for (i = 0; i < nb_input_streams; i++) {
257 ist = input_streams[i];
258 if (ist->st->codec->codec_type == type && ist->discard)
261 if (i == nb_input_streams) {
262 av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
263 "unlabeled input pad %d on filter %s\n", in->pad_idx,
264 in->filter_ctx->name);
271 ist->decoding_needed++;
272 ist->st->discard = AVDISCARD_NONE;
274 GROW_ARRAY(fg->inputs, fg->nb_inputs);
275 if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
277 fg->inputs[fg->nb_inputs - 1]->ist = ist;
278 fg->inputs[fg->nb_inputs - 1]->graph = fg;
280 GROW_ARRAY(ist->filters, ist->nb_filters);
281 ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
284 static int insert_trim(int64_t start_time, int64_t duration,
285 AVFilterContext **last_filter, int *pad_idx,
286 const char *filter_name)
288 AVFilterGraph *graph = (*last_filter)->graph;
289 AVFilterContext *ctx;
290 const AVFilter *trim;
291 enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
292 const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
295 if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
298 trim = avfilter_get_by_name(name);
300 av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
301 "recording time.\n", name);
302 return AVERROR_FILTER_NOT_FOUND;
305 ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
307 return AVERROR(ENOMEM);
309 if (duration != INT64_MAX) {
310 ret = av_opt_set_int(ctx, "durationi", duration,
311 AV_OPT_SEARCH_CHILDREN);
313 if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
314 ret = av_opt_set_int(ctx, "starti", start_time,
315 AV_OPT_SEARCH_CHILDREN);
318 av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
322 ret = avfilter_init_str(ctx, NULL);
326 ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
335 static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
338 OutputStream *ost = ofilter->ost;
339 OutputFile *of = output_files[ost->file_index];
340 AVCodecContext *codec = ost->st->codec;
341 AVFilterContext *last_filter = out->filter_ctx;
342 int pad_idx = out->pad_idx;
346 snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
347 ret = avfilter_graph_create_filter(&ofilter->filter,
348 avfilter_get_by_name("buffersink"),
349 name, NULL, NULL, fg->graph);
354 if (codec->width || codec->height) {
356 AVFilterContext *filter;
358 snprintf(args, sizeof(args), "%d:%d:0x%X",
361 (unsigned)ost->sws_flags);
362 snprintf(name, sizeof(name), "scaler for output stream %d:%d",
363 ost->file_index, ost->index);
364 if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
365 name, args, NULL, fg->graph)) < 0)
367 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
370 last_filter = filter;
374 if ((pix_fmts = choose_pix_fmts(ost))) {
375 AVFilterContext *filter;
376 snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
377 ost->file_index, ost->index);
378 ret = avfilter_graph_create_filter(&filter,
379 avfilter_get_by_name("format"),
380 "format", pix_fmts, NULL,
385 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
388 last_filter = filter;
392 if (ost->frame_rate.num && 0) {
393 AVFilterContext *fps;
396 snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
397 ost->frame_rate.den);
398 snprintf(name, sizeof(name), "fps for output stream %d:%d",
399 ost->file_index, ost->index);
400 ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
401 name, args, NULL, fg->graph);
405 ret = avfilter_link(last_filter, pad_idx, fps, 0);
412 snprintf(name, sizeof(name), "trim for output stream %d:%d",
413 ost->file_index, ost->index);
414 ret = insert_trim(of->start_time, of->recording_time,
415 &last_filter, &pad_idx, name);
420 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
426 static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
428 OutputStream *ost = ofilter->ost;
429 OutputFile *of = output_files[ost->file_index];
430 AVCodecContext *codec = ost->st->codec;
431 AVFilterContext *last_filter = out->filter_ctx;
432 int pad_idx = out->pad_idx;
433 char *sample_fmts, *sample_rates, *channel_layouts;
437 snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
438 ret = avfilter_graph_create_filter(&ofilter->filter,
439 avfilter_get_by_name("abuffersink"),
440 name, NULL, NULL, fg->graph);
443 if ((ret = av_opt_set_int(ofilter->filter, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
446 #define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \
447 AVFilterContext *filt_ctx; \
449 av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
450 "similarly to -af " filter_name "=%s.\n", arg); \
452 ret = avfilter_graph_create_filter(&filt_ctx, \
453 avfilter_get_by_name(filter_name), \
454 filter_name, arg, NULL, fg->graph); \
458 ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0); \
462 last_filter = filt_ctx; \
465 if (ost->audio_channels_mapped) {
468 av_bprint_init(&pan_buf, 256, 8192);
469 av_bprintf(&pan_buf, "0x%"PRIx64,
470 av_get_default_channel_layout(ost->audio_channels_mapped));
471 for (i = 0; i < ost->audio_channels_mapped; i++)
472 if (ost->audio_channels_map[i] != -1)
473 av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
475 AUTO_INSERT_FILTER("-map_channel", "pan", pan_buf.str);
476 av_bprint_finalize(&pan_buf, NULL);
479 if (codec->channels && !codec->channel_layout)
480 codec->channel_layout = av_get_default_channel_layout(codec->channels);
482 sample_fmts = choose_sample_fmts(ost);
483 sample_rates = choose_sample_rates(ost);
484 channel_layouts = choose_channel_layouts(ost);
485 if (sample_fmts || sample_rates || channel_layouts) {
486 AVFilterContext *format;
491 av_strlcatf(args, sizeof(args), "sample_fmts=%s:",
494 av_strlcatf(args, sizeof(args), "sample_rates=%s:",
497 av_strlcatf(args, sizeof(args), "channel_layouts=%s:",
500 av_freep(&sample_fmts);
501 av_freep(&sample_rates);
502 av_freep(&channel_layouts);
504 snprintf(name, sizeof(name), "audio format for output stream %d:%d",
505 ost->file_index, ost->index);
506 ret = avfilter_graph_create_filter(&format,
507 avfilter_get_by_name("aformat"),
508 name, args, NULL, fg->graph);
512 ret = avfilter_link(last_filter, pad_idx, format, 0);
516 last_filter = format;
520 if (audio_volume != 256 && 0) {
523 snprintf(args, sizeof(args), "%f", audio_volume / 256.);
524 AUTO_INSERT_FILTER("-vol", "volume", args);
527 if (ost->apad && of->shortest) {
531 for (i=0; i<of->ctx->nb_streams; i++)
532 if (of->ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
535 if (i<of->ctx->nb_streams) {
536 snprintf(args, sizeof(args), "%s", ost->apad);
537 AUTO_INSERT_FILTER("-apad", "apad", args);
541 snprintf(name, sizeof(name), "trim for output stream %d:%d",
542 ost->file_index, ost->index);
543 ret = insert_trim(of->start_time, of->recording_time,
544 &last_filter, &pad_idx, name);
548 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
554 #define DESCRIBE_FILTER_LINK(f, inout, in) \
556 AVFilterContext *ctx = inout->filter_ctx; \
557 AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads; \
558 int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs; \
561 if (avio_open_dyn_buf(&pb) < 0) \
564 avio_printf(pb, "%s", ctx->filter->name); \
566 avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
568 avio_close_dyn_buf(pb, &f->name); \
571 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
573 av_freep(&ofilter->name);
574 DESCRIBE_FILTER_LINK(ofilter, out, 0);
576 switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
577 case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
578 case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
579 default: av_assert0(0);
583 static int sub2video_prepare(InputStream *ist)
585 AVFormatContext *avf = input_files[ist->file_index]->ctx;
588 /* Compute the size of the canvas for the subtitles stream.
589 If the subtitles codec has set a size, use it. Otherwise use the
590 maximum dimensions of the video streams in the same file. */
591 w = ist->st->codec->width;
592 h = ist->st->codec->height;
594 for (i = 0; i < avf->nb_streams; i++) {
595 if (avf->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
596 w = FFMAX(w, avf->streams[i]->codec->width);
597 h = FFMAX(h, avf->streams[i]->codec->height);
604 av_log(avf, AV_LOG_INFO, "sub2video: using %dx%d canvas\n", w, h);
606 ist->sub2video.w = ist->st->codec->width = ist->resample_width = w;
607 ist->sub2video.h = ist->st->codec->height = ist->resample_height = h;
609 /* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the
610 palettes for all rectangles are identical or compatible */
611 ist->resample_pix_fmt = ist->st->codec->pix_fmt = AV_PIX_FMT_RGB32;
613 ist->sub2video.frame = av_frame_alloc();
614 if (!ist->sub2video.frame)
615 return AVERROR(ENOMEM);
619 static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
622 AVFilterContext *last_filter;
623 const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
624 InputStream *ist = ifilter->ist;
625 InputFile *f = input_files[ist->file_index];
626 AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
628 AVRational fr = ist->framerate;
632 int ret, pad_idx = 0;
634 if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
635 av_log(NULL, AV_LOG_ERROR, "Cannot connect video filter to audio input\n");
636 return AVERROR(EINVAL);
640 fr = av_guess_frame_rate(input_files[ist->file_index]->ctx, ist->st, NULL);
642 if (ist->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
643 ret = sub2video_prepare(ist);
648 sar = ist->st->sample_aspect_ratio.num ?
649 ist->st->sample_aspect_ratio :
650 ist->st->codec->sample_aspect_ratio;
652 sar = (AVRational){0,1};
653 av_bprint_init(&args, 0, 1);
655 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
656 "pixel_aspect=%d/%d:sws_param=flags=%d", ist->resample_width,
657 ist->resample_height, ist->resample_pix_fmt,
658 tb.num, tb.den, sar.num, sar.den,
659 SWS_BILINEAR + ((ist->st->codec->flags&CODEC_FLAG_BITEXACT) ? SWS_BITEXACT:0));
660 if (fr.num && fr.den)
661 av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
662 snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
663 ist->file_index, ist->st->index);
665 if ((ret = avfilter_graph_create_filter(&ifilter->filter, buffer_filt, name,
666 args.str, NULL, fg->graph)) < 0)
668 last_filter = ifilter->filter;
670 if (ist->framerate.num) {
671 AVFilterContext *setpts;
673 snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
674 ist->file_index, ist->st->index);
675 if ((ret = avfilter_graph_create_filter(&setpts,
676 avfilter_get_by_name("setpts"),
681 if ((ret = avfilter_link(last_filter, 0, setpts, 0)) < 0)
684 last_filter = setpts;
687 if (do_deinterlace) {
688 AVFilterContext *yadif;
690 snprintf(name, sizeof(name), "deinterlace input from stream %d:%d",
691 ist->file_index, ist->st->index);
692 if ((ret = avfilter_graph_create_filter(&yadif,
693 avfilter_get_by_name("yadif"),
698 if ((ret = avfilter_link(last_filter, 0, yadif, 0)) < 0)
704 snprintf(name, sizeof(name), "trim for input stream %d:%d",
705 ist->file_index, ist->st->index);
706 ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
707 AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name);
711 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
716 static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
719 AVFilterContext *last_filter;
720 const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
721 InputStream *ist = ifilter->ist;
722 InputFile *f = input_files[ist->file_index];
725 int ret, pad_idx = 0;
727 if (ist->st->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
728 av_log(NULL, AV_LOG_ERROR, "Cannot connect audio filter to non audio input\n");
729 return AVERROR(EINVAL);
732 av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
733 av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
734 1, ist->st->codec->sample_rate,
735 ist->st->codec->sample_rate,
736 av_get_sample_fmt_name(ist->st->codec->sample_fmt));
737 if (ist->st->codec->channel_layout)
738 av_bprintf(&args, ":channel_layout=0x%"PRIx64,
739 ist->st->codec->channel_layout);
741 av_bprintf(&args, ":channels=%d", ist->st->codec->channels);
742 snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
743 ist->file_index, ist->st->index);
745 if ((ret = avfilter_graph_create_filter(&ifilter->filter, abuffer_filt,
746 name, args.str, NULL,
749 last_filter = ifilter->filter;
751 #define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do { \
752 AVFilterContext *filt_ctx; \
754 av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
755 "similarly to -af " filter_name "=%s.\n", arg); \
757 snprintf(name, sizeof(name), "graph %d %s for input stream %d:%d", \
758 fg->index, filter_name, ist->file_index, ist->st->index); \
759 ret = avfilter_graph_create_filter(&filt_ctx, \
760 avfilter_get_by_name(filter_name), \
761 name, arg, NULL, fg->graph); \
765 ret = avfilter_link(last_filter, 0, filt_ctx, 0); \
769 last_filter = filt_ctx; \
772 if (audio_sync_method > 0) {
773 char args[256] = {0};
775 av_strlcatf(args, sizeof(args), "async=%d", audio_sync_method);
776 if (audio_drift_threshold != 0.1)
777 av_strlcatf(args, sizeof(args), ":min_hard_comp=%f", audio_drift_threshold);
778 if (!fg->reconfiguration)
779 av_strlcatf(args, sizeof(args), ":first_pts=0");
780 AUTO_INSERT_FILTER_INPUT("-async", "aresample", args);
783 // if (ost->audio_channels_mapped) {
786 // av_bprint_init(&pan_buf, 256, 8192);
787 // av_bprintf(&pan_buf, "0x%"PRIx64,
788 // av_get_default_channel_layout(ost->audio_channels_mapped));
789 // for (i = 0; i < ost->audio_channels_mapped; i++)
790 // if (ost->audio_channels_map[i] != -1)
791 // av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
792 // AUTO_INSERT_FILTER_INPUT("-map_channel", "pan", pan_buf.str);
793 // av_bprint_finalize(&pan_buf, NULL);
796 if (audio_volume != 256) {
799 av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
800 "audio filter instead.\n");
802 snprintf(args, sizeof(args), "%f", audio_volume / 256.);
803 AUTO_INSERT_FILTER_INPUT("-vol", "volume", args);
806 snprintf(name, sizeof(name), "trim for input stream %d:%d",
807 ist->file_index, ist->st->index);
808 ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
809 AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name);
813 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
819 static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
822 av_freep(&ifilter->name);
823 DESCRIBE_FILTER_LINK(ifilter, in, 1);
825 switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
826 case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
827 case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
828 default: av_assert0(0);
832 int configure_filtergraph(FilterGraph *fg)
834 AVFilterInOut *inputs, *outputs, *cur;
835 int ret, i, init = !fg->graph, simple = !fg->graph_desc;
836 const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
839 avfilter_graph_free(&fg->graph);
840 if (!(fg->graph = avfilter_graph_alloc()))
841 return AVERROR(ENOMEM);
844 OutputStream *ost = fg->outputs[0]->ost;
846 AVDictionaryEntry *e = NULL;
848 snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
849 fg->graph->scale_sws_opts = av_strdup(args);
852 while ((e = av_dict_get(ost->swr_opts, "", e,
853 AV_DICT_IGNORE_SUFFIX))) {
854 av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
857 args[strlen(args)-1] = 0;
858 av_opt_set(fg->graph, "aresample_swr_opts", args, 0);
861 while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
862 AV_DICT_IGNORE_SUFFIX))) {
863 av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
866 args[strlen(args) - 1] = '\0';
867 fg->graph->resample_lavr_opts = av_strdup(args);
869 e = av_dict_get(ost->opts, "threads", NULL, 0);
871 av_opt_set(fg->graph, "threads", e->value, 0);
874 if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
877 if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
878 av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
879 "exactly one input and output.\n", graph_desc);
880 return AVERROR(EINVAL);
883 for (cur = inputs; !simple && init && cur; cur = cur->next)
884 init_input_filter(fg, cur);
886 for (cur = inputs, i = 0; cur; cur = cur->next, i++)
887 if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
889 avfilter_inout_free(&inputs);
891 if (!init || simple) {
892 /* we already know the mappings between lavfi outputs and output streams,
893 * so we can finish the setup */
894 for (cur = outputs, i = 0; cur; cur = cur->next, i++)
895 configure_output_filter(fg, fg->outputs[i], cur);
896 avfilter_inout_free(&outputs);
898 if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
901 /* wait until output mappings are processed */
902 for (cur = outputs; cur;) {
903 GROW_ARRAY(fg->outputs, fg->nb_outputs);
904 if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]))))
906 fg->outputs[fg->nb_outputs - 1]->graph = fg;
907 fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
909 fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
913 fg->reconfiguration = 1;
917 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
920 for (i = 0; i < fg->nb_inputs; i++)
921 if (fg->inputs[i]->ist == ist)