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 const enum AVPixelFormat mjpeg_formats[] = { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE };
46 const enum AVPixelFormat ljpeg_formats[] = { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUV420P,
47 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE };
49 if (st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
50 if (st->codec->codec_id == AV_CODEC_ID_MJPEG) {
52 } else if (st->codec->codec_id == AV_CODEC_ID_LJPEG) {
56 for (; *p != AV_PIX_FMT_NONE; p++) {
57 best= avcodec_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
61 if (*p == AV_PIX_FMT_NONE) {
62 if (target != AV_PIX_FMT_NONE)
63 av_log(NULL, AV_LOG_WARNING,
64 "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
65 av_get_pix_fmt_name(target),
67 av_get_pix_fmt_name(best));
74 void choose_sample_fmt(AVStream *st, AVCodec *codec)
76 if (codec && codec->sample_fmts) {
77 const enum AVSampleFormat *p = codec->sample_fmts;
78 for (; *p != -1; p++) {
79 if (*p == st->codec->sample_fmt)
83 if((codec->capabilities & CODEC_CAP_LOSSLESS) && av_get_sample_fmt_name(st->codec->sample_fmt) > av_get_sample_fmt_name(codec->sample_fmts[0]))
84 av_log(NULL, AV_LOG_ERROR, "Conversion will not be lossless.\n");
85 if(av_get_sample_fmt_name(st->codec->sample_fmt))
86 av_log(NULL, AV_LOG_WARNING,
87 "Incompatible sample format '%s' for codec '%s', auto-selecting format '%s'\n",
88 av_get_sample_fmt_name(st->codec->sample_fmt),
90 av_get_sample_fmt_name(codec->sample_fmts[0]));
91 st->codec->sample_fmt = codec->sample_fmts[0];
96 static char *choose_pix_fmts(OutputStream *ost)
98 AVDictionaryEntry *strict_dict = av_dict_get(ost->opts, "strict", NULL, 0);
100 // used by choose_pixel_fmt() and below
101 av_opt_set(ost->st->codec, "strict", strict_dict->value, 0);
103 if (ost->keep_pix_fmt) {
105 avfilter_graph_set_auto_convert(ost->filter->graph->graph,
106 AVFILTER_AUTO_CONVERT_NONE);
107 if (ost->st->codec->pix_fmt == AV_PIX_FMT_NONE)
109 return av_strdup(av_get_pix_fmt_name(ost->st->codec->pix_fmt));
111 if (ost->st->codec->pix_fmt != AV_PIX_FMT_NONE) {
112 return av_strdup(av_get_pix_fmt_name(choose_pixel_fmt(ost->st, ost->enc, ost->st->codec->pix_fmt)));
113 } else if (ost->enc && ost->enc->pix_fmts) {
114 const enum AVPixelFormat *p;
115 AVIOContext *s = NULL;
119 if (avio_open_dyn_buf(&s) < 0)
122 p = ost->enc->pix_fmts;
123 if (ost->st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
124 if (ost->st->codec->codec_id == AV_CODEC_ID_MJPEG) {
125 p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE };
126 } else if (ost->st->codec->codec_id == AV_CODEC_ID_LJPEG) {
127 p = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUV420P,
128 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_BGRA, AV_PIX_FMT_NONE };
132 for (; *p != AV_PIX_FMT_NONE; p++) {
133 const char *name = av_get_pix_fmt_name(*p);
134 avio_printf(s, "%s|", name);
136 len = avio_close_dyn_buf(s, &ret);
143 /* Define a function for building a string containing a list of
144 * allowed formats. */
145 #define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name) \
146 static char *choose_ ## var ## s(OutputStream *ost) \
148 if (ost->st->codec->var != none) { \
149 get_name(ost->st->codec->var); \
150 return av_strdup(name); \
151 } else if (ost->enc && ost->enc->supported_list) { \
153 AVIOContext *s = NULL; \
157 if (avio_open_dyn_buf(&s) < 0) \
160 for (p = ost->enc->supported_list; *p != none; p++) { \
162 avio_printf(s, "%s|", name); \
164 len = avio_close_dyn_buf(s, &ret); \
171 // DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE,
174 DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts,
175 AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME)
177 DEF_CHOOSE_FORMAT(int, sample_rate, supported_samplerates, 0,
178 GET_SAMPLE_RATE_NAME)
180 DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0,
183 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost)
185 FilterGraph *fg = av_mallocz(sizeof(*fg));
189 fg->index = nb_filtergraphs;
191 GROW_ARRAY(fg->outputs, fg->nb_outputs);
192 if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
194 fg->outputs[0]->ost = ost;
195 fg->outputs[0]->graph = fg;
197 ost->filter = fg->outputs[0];
199 GROW_ARRAY(fg->inputs, fg->nb_inputs);
200 if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
202 fg->inputs[0]->ist = ist;
203 fg->inputs[0]->graph = fg;
205 GROW_ARRAY(ist->filters, ist->nb_filters);
206 ist->filters[ist->nb_filters - 1] = fg->inputs[0];
208 GROW_ARRAY(filtergraphs, nb_filtergraphs);
209 filtergraphs[nb_filtergraphs - 1] = fg;
214 static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
216 InputStream *ist = NULL;
217 enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
220 // TODO: support other filter types
221 if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
222 av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
231 int file_idx = strtol(in->name, &p, 0);
233 if (file_idx < 0 || file_idx >= nb_input_files) {
234 av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
235 file_idx, fg->graph_desc);
238 s = input_files[file_idx]->ctx;
240 for (i = 0; i < s->nb_streams; i++) {
241 enum AVMediaType stream_type = s->streams[i]->codec->codec_type;
242 if (stream_type != type &&
243 !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
244 type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
246 if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
252 av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
253 "matches no streams.\n", p, fg->graph_desc);
256 ist = input_streams[input_files[file_idx]->ist_index + st->index];
258 /* find the first unused stream of corresponding type */
259 for (i = 0; i < nb_input_streams; i++) {
260 ist = input_streams[i];
261 if (ist->st->codec->codec_type == type && ist->discard)
264 if (i == nb_input_streams) {
265 av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
266 "unlabeled input pad %d on filter %s\n", in->pad_idx,
267 in->filter_ctx->name);
274 ist->decoding_needed++;
275 ist->st->discard = AVDISCARD_NONE;
277 GROW_ARRAY(fg->inputs, fg->nb_inputs);
278 if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
280 fg->inputs[fg->nb_inputs - 1]->ist = ist;
281 fg->inputs[fg->nb_inputs - 1]->graph = fg;
283 GROW_ARRAY(ist->filters, ist->nb_filters);
284 ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
287 static int insert_trim(int64_t start_time, int64_t duration,
288 AVFilterContext **last_filter, int *pad_idx,
289 const char *filter_name)
291 AVFilterGraph *graph = (*last_filter)->graph;
292 AVFilterContext *ctx;
293 const AVFilter *trim;
294 enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
295 const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
298 if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
301 trim = avfilter_get_by_name(name);
303 av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
304 "recording time.\n", name);
305 return AVERROR_FILTER_NOT_FOUND;
308 ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
310 return AVERROR(ENOMEM);
312 if (duration != INT64_MAX) {
313 ret = av_opt_set_int(ctx, "durationi", duration,
314 AV_OPT_SEARCH_CHILDREN);
316 if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
317 ret = av_opt_set_int(ctx, "starti", start_time,
318 AV_OPT_SEARCH_CHILDREN);
321 av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
325 ret = avfilter_init_str(ctx, NULL);
329 ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
338 static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
341 OutputStream *ost = ofilter->ost;
342 OutputFile *of = output_files[ost->file_index];
343 AVCodecContext *codec = ost->st->codec;
344 AVFilterContext *last_filter = out->filter_ctx;
345 int pad_idx = out->pad_idx;
349 snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
350 ret = avfilter_graph_create_filter(&ofilter->filter,
351 avfilter_get_by_name("buffersink"),
352 name, NULL, NULL, fg->graph);
357 if (codec->width || codec->height) {
359 AVFilterContext *filter;
361 snprintf(args, sizeof(args), "%d:%d:0x%X",
364 (unsigned)ost->sws_flags);
365 snprintf(name, sizeof(name), "scaler for output stream %d:%d",
366 ost->file_index, ost->index);
367 if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
368 name, args, NULL, fg->graph)) < 0)
370 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
373 last_filter = filter;
377 if ((pix_fmts = choose_pix_fmts(ost))) {
378 AVFilterContext *filter;
379 snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
380 ost->file_index, ost->index);
381 ret = avfilter_graph_create_filter(&filter,
382 avfilter_get_by_name("format"),
383 "format", pix_fmts, NULL,
388 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
391 last_filter = filter;
395 if (ost->frame_rate.num && 0) {
396 AVFilterContext *fps;
399 snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
400 ost->frame_rate.den);
401 snprintf(name, sizeof(name), "fps for output stream %d:%d",
402 ost->file_index, ost->index);
403 ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
404 name, args, NULL, fg->graph);
408 ret = avfilter_link(last_filter, pad_idx, fps, 0);
415 snprintf(name, sizeof(name), "trim for output stream %d:%d",
416 ost->file_index, ost->index);
417 ret = insert_trim(of->start_time, of->recording_time,
418 &last_filter, &pad_idx, name);
423 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
429 static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
431 OutputStream *ost = ofilter->ost;
432 OutputFile *of = output_files[ost->file_index];
433 AVCodecContext *codec = ost->st->codec;
434 AVFilterContext *last_filter = out->filter_ctx;
435 int pad_idx = out->pad_idx;
436 char *sample_fmts, *sample_rates, *channel_layouts;
440 snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
441 ret = avfilter_graph_create_filter(&ofilter->filter,
442 avfilter_get_by_name("abuffersink"),
443 name, NULL, NULL, fg->graph);
446 if ((ret = av_opt_set_int(ofilter->filter, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
449 #define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \
450 AVFilterContext *filt_ctx; \
452 av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
453 "similarly to -af " filter_name "=%s.\n", arg); \
455 ret = avfilter_graph_create_filter(&filt_ctx, \
456 avfilter_get_by_name(filter_name), \
457 filter_name, arg, NULL, fg->graph); \
461 ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0); \
465 last_filter = filt_ctx; \
468 if (ost->audio_channels_mapped) {
471 av_bprint_init(&pan_buf, 256, 8192);
472 av_bprintf(&pan_buf, "0x%"PRIx64,
473 av_get_default_channel_layout(ost->audio_channels_mapped));
474 for (i = 0; i < ost->audio_channels_mapped; i++)
475 if (ost->audio_channels_map[i] != -1)
476 av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
478 AUTO_INSERT_FILTER("-map_channel", "pan", pan_buf.str);
479 av_bprint_finalize(&pan_buf, NULL);
482 if (codec->channels && !codec->channel_layout)
483 codec->channel_layout = av_get_default_channel_layout(codec->channels);
485 sample_fmts = choose_sample_fmts(ost);
486 sample_rates = choose_sample_rates(ost);
487 channel_layouts = choose_channel_layouts(ost);
488 if (sample_fmts || sample_rates || channel_layouts) {
489 AVFilterContext *format;
494 av_strlcatf(args, sizeof(args), "sample_fmts=%s:",
497 av_strlcatf(args, sizeof(args), "sample_rates=%s:",
500 av_strlcatf(args, sizeof(args), "channel_layouts=%s:",
503 av_freep(&sample_fmts);
504 av_freep(&sample_rates);
505 av_freep(&channel_layouts);
507 snprintf(name, sizeof(name), "audio format for output stream %d:%d",
508 ost->file_index, ost->index);
509 ret = avfilter_graph_create_filter(&format,
510 avfilter_get_by_name("aformat"),
511 name, args, NULL, fg->graph);
515 ret = avfilter_link(last_filter, pad_idx, format, 0);
519 last_filter = format;
523 if (audio_volume != 256 && 0) {
526 snprintf(args, sizeof(args), "%f", audio_volume / 256.);
527 AUTO_INSERT_FILTER("-vol", "volume", args);
530 if (ost->apad && of->shortest) {
534 for (i=0; i<of->ctx->nb_streams; i++)
535 if (of->ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
538 if (i<of->ctx->nb_streams) {
539 snprintf(args, sizeof(args), "%s", ost->apad);
540 AUTO_INSERT_FILTER("-apad", "apad", args);
544 snprintf(name, sizeof(name), "trim for output stream %d:%d",
545 ost->file_index, ost->index);
546 ret = insert_trim(of->start_time, of->recording_time,
547 &last_filter, &pad_idx, name);
551 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
557 #define DESCRIBE_FILTER_LINK(f, inout, in) \
559 AVFilterContext *ctx = inout->filter_ctx; \
560 AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads; \
561 int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs; \
564 if (avio_open_dyn_buf(&pb) < 0) \
567 avio_printf(pb, "%s", ctx->filter->name); \
569 avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
571 avio_close_dyn_buf(pb, &f->name); \
574 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
576 av_freep(&ofilter->name);
577 DESCRIBE_FILTER_LINK(ofilter, out, 0);
579 switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
580 case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
581 case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
582 default: av_assert0(0);
586 static int sub2video_prepare(InputStream *ist)
588 AVFormatContext *avf = input_files[ist->file_index]->ctx;
591 /* Compute the size of the canvas for the subtitles stream.
592 If the subtitles codec has set a size, use it. Otherwise use the
593 maximum dimensions of the video streams in the same file. */
594 w = ist->st->codec->width;
595 h = ist->st->codec->height;
597 for (i = 0; i < avf->nb_streams; i++) {
598 if (avf->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
599 w = FFMAX(w, avf->streams[i]->codec->width);
600 h = FFMAX(h, avf->streams[i]->codec->height);
607 av_log(avf, AV_LOG_INFO, "sub2video: using %dx%d canvas\n", w, h);
609 ist->sub2video.w = ist->st->codec->width = ist->resample_width = w;
610 ist->sub2video.h = ist->st->codec->height = ist->resample_height = h;
612 /* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the
613 palettes for all rectangles are identical or compatible */
614 ist->resample_pix_fmt = ist->st->codec->pix_fmt = AV_PIX_FMT_RGB32;
616 ist->sub2video.frame = av_frame_alloc();
617 if (!ist->sub2video.frame)
618 return AVERROR(ENOMEM);
622 static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
625 AVFilterContext *last_filter;
626 const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
627 InputStream *ist = ifilter->ist;
628 InputFile *f = input_files[ist->file_index];
629 AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
631 AVRational fr = ist->framerate;
635 int ret, pad_idx = 0;
637 if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
638 av_log(NULL, AV_LOG_ERROR, "Cannot connect video filter to audio input\n");
639 return AVERROR(EINVAL);
643 fr = av_guess_frame_rate(input_files[ist->file_index]->ctx, ist->st, NULL);
645 if (ist->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
646 ret = sub2video_prepare(ist);
651 sar = ist->st->sample_aspect_ratio.num ?
652 ist->st->sample_aspect_ratio :
653 ist->st->codec->sample_aspect_ratio;
655 sar = (AVRational){0,1};
656 av_bprint_init(&args, 0, 1);
658 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
659 "pixel_aspect=%d/%d:sws_param=flags=%d", ist->resample_width,
660 ist->resample_height, ist->resample_pix_fmt,
661 tb.num, tb.den, sar.num, sar.den,
662 SWS_BILINEAR + ((ist->st->codec->flags&CODEC_FLAG_BITEXACT) ? SWS_BITEXACT:0));
663 if (fr.num && fr.den)
664 av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
665 snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
666 ist->file_index, ist->st->index);
668 if ((ret = avfilter_graph_create_filter(&ifilter->filter, buffer_filt, name,
669 args.str, NULL, fg->graph)) < 0)
671 last_filter = ifilter->filter;
673 if (ist->framerate.num) {
674 AVFilterContext *setpts;
676 snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
677 ist->file_index, ist->st->index);
678 if ((ret = avfilter_graph_create_filter(&setpts,
679 avfilter_get_by_name("setpts"),
684 if ((ret = avfilter_link(last_filter, 0, setpts, 0)) < 0)
687 last_filter = setpts;
690 if (do_deinterlace) {
691 AVFilterContext *yadif;
693 snprintf(name, sizeof(name), "deinterlace input from stream %d:%d",
694 ist->file_index, ist->st->index);
695 if ((ret = avfilter_graph_create_filter(&yadif,
696 avfilter_get_by_name("yadif"),
701 if ((ret = avfilter_link(last_filter, 0, yadif, 0)) < 0)
707 snprintf(name, sizeof(name), "trim for input stream %d:%d",
708 ist->file_index, ist->st->index);
709 ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
710 AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name);
714 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
719 static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
722 AVFilterContext *last_filter;
723 const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
724 InputStream *ist = ifilter->ist;
725 InputFile *f = input_files[ist->file_index];
728 int ret, pad_idx = 0;
730 if (ist->st->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
731 av_log(NULL, AV_LOG_ERROR, "Cannot connect audio filter to non audio input\n");
732 return AVERROR(EINVAL);
735 av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
736 av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
737 1, ist->st->codec->sample_rate,
738 ist->st->codec->sample_rate,
739 av_get_sample_fmt_name(ist->st->codec->sample_fmt));
740 if (ist->st->codec->channel_layout)
741 av_bprintf(&args, ":channel_layout=0x%"PRIx64,
742 ist->st->codec->channel_layout);
744 av_bprintf(&args, ":channels=%d", ist->st->codec->channels);
745 snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
746 ist->file_index, ist->st->index);
748 if ((ret = avfilter_graph_create_filter(&ifilter->filter, abuffer_filt,
749 name, args.str, NULL,
752 last_filter = ifilter->filter;
754 #define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do { \
755 AVFilterContext *filt_ctx; \
757 av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
758 "similarly to -af " filter_name "=%s.\n", arg); \
760 snprintf(name, sizeof(name), "graph %d %s for input stream %d:%d", \
761 fg->index, filter_name, ist->file_index, ist->st->index); \
762 ret = avfilter_graph_create_filter(&filt_ctx, \
763 avfilter_get_by_name(filter_name), \
764 name, arg, NULL, fg->graph); \
768 ret = avfilter_link(last_filter, 0, filt_ctx, 0); \
772 last_filter = filt_ctx; \
775 if (audio_sync_method > 0) {
776 char args[256] = {0};
778 av_strlcatf(args, sizeof(args), "async=%d", audio_sync_method);
779 if (audio_drift_threshold != 0.1)
780 av_strlcatf(args, sizeof(args), ":min_hard_comp=%f", audio_drift_threshold);
781 if (!fg->reconfiguration)
782 av_strlcatf(args, sizeof(args), ":first_pts=0");
783 AUTO_INSERT_FILTER_INPUT("-async", "aresample", args);
786 // if (ost->audio_channels_mapped) {
789 // av_bprint_init(&pan_buf, 256, 8192);
790 // av_bprintf(&pan_buf, "0x%"PRIx64,
791 // av_get_default_channel_layout(ost->audio_channels_mapped));
792 // for (i = 0; i < ost->audio_channels_mapped; i++)
793 // if (ost->audio_channels_map[i] != -1)
794 // av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
795 // AUTO_INSERT_FILTER_INPUT("-map_channel", "pan", pan_buf.str);
796 // av_bprint_finalize(&pan_buf, NULL);
799 if (audio_volume != 256) {
802 av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
803 "audio filter instead.\n");
805 snprintf(args, sizeof(args), "%f", audio_volume / 256.);
806 AUTO_INSERT_FILTER_INPUT("-vol", "volume", args);
809 snprintf(name, sizeof(name), "trim for input stream %d:%d",
810 ist->file_index, ist->st->index);
811 ret = insert_trim(((f->start_time == AV_NOPTS_VALUE) || !f->accurate_seek) ?
812 AV_NOPTS_VALUE : 0, f->recording_time, &last_filter, &pad_idx, name);
816 if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
822 static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
825 av_freep(&ifilter->name);
826 DESCRIBE_FILTER_LINK(ifilter, in, 1);
828 switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
829 case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
830 case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
831 default: av_assert0(0);
835 int configure_filtergraph(FilterGraph *fg)
837 AVFilterInOut *inputs, *outputs, *cur;
838 int ret, i, init = !fg->graph, simple = !fg->graph_desc;
839 const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
842 avfilter_graph_free(&fg->graph);
843 if (!(fg->graph = avfilter_graph_alloc()))
844 return AVERROR(ENOMEM);
847 OutputStream *ost = fg->outputs[0]->ost;
849 AVDictionaryEntry *e = NULL;
851 snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
852 fg->graph->scale_sws_opts = av_strdup(args);
855 while ((e = av_dict_get(ost->swr_opts, "", e,
856 AV_DICT_IGNORE_SUFFIX))) {
857 av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
860 args[strlen(args)-1] = 0;
861 av_opt_set(fg->graph, "aresample_swr_opts", args, 0);
864 while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
865 AV_DICT_IGNORE_SUFFIX))) {
866 av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
869 args[strlen(args) - 1] = '\0';
870 fg->graph->resample_lavr_opts = av_strdup(args);
872 e = av_dict_get(ost->opts, "threads", NULL, 0);
874 av_opt_set(fg->graph, "threads", e->value, 0);
877 if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
880 if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
881 av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
882 "exactly one input and output.\n", graph_desc);
883 return AVERROR(EINVAL);
886 for (cur = inputs; !simple && init && cur; cur = cur->next)
887 init_input_filter(fg, cur);
889 for (cur = inputs, i = 0; cur; cur = cur->next, i++)
890 if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
892 avfilter_inout_free(&inputs);
894 if (!init || simple) {
895 /* we already know the mappings between lavfi outputs and output streams,
896 * so we can finish the setup */
897 for (cur = outputs, i = 0; cur; cur = cur->next, i++)
898 configure_output_filter(fg, fg->outputs[i], cur);
899 avfilter_inout_free(&outputs);
901 if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
904 /* wait until output mappings are processed */
905 for (cur = outputs; cur;) {
906 GROW_ARRAY(fg->outputs, fg->nb_outputs);
907 if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]))))
909 fg->outputs[fg->nb_outputs - 1]->graph = fg;
910 fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
912 fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
916 fg->reconfiguration = 1;
920 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
923 for (i = 0; i < fg->nb_inputs; i++)
924 if (fg->inputs[i]->ist == ist)