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/audioconvert.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/bprint.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/pixfmt.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/samplefmt.h"
36 enum PixelFormat choose_pixel_fmt(AVStream *st, AVCodec *codec, enum PixelFormat target)
38 if (codec && codec->pix_fmts) {
39 const enum PixelFormat *p = codec->pix_fmts;
40 int has_alpha= av_pix_fmt_descriptors[target].nb_components % 2 == 0;
41 enum PixelFormat best= PIX_FMT_NONE;
42 if (st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
43 if (st->codec->codec_id == AV_CODEC_ID_MJPEG) {
44 p = (const enum PixelFormat[]) { PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE };
45 } else if (st->codec->codec_id == AV_CODEC_ID_LJPEG) {
46 p = (const enum PixelFormat[]) { PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ444P, PIX_FMT_YUV420P,
47 PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_BGRA, PIX_FMT_NONE };
50 for (; *p != PIX_FMT_NONE; p++) {
51 best= avcodec_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
55 if (*p == PIX_FMT_NONE) {
56 if (target != PIX_FMT_NONE)
57 av_log(NULL, AV_LOG_WARNING,
58 "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
59 av_pix_fmt_descriptors[target].name,
61 av_pix_fmt_descriptors[best].name);
68 void choose_sample_fmt(AVStream *st, AVCodec *codec)
70 if (codec && codec->sample_fmts) {
71 const enum AVSampleFormat *p = codec->sample_fmts;
72 for (; *p != -1; p++) {
73 if (*p == st->codec->sample_fmt)
77 if((codec->capabilities & CODEC_CAP_LOSSLESS) && av_get_sample_fmt_name(st->codec->sample_fmt) > av_get_sample_fmt_name(codec->sample_fmts[0]))
78 av_log(NULL, AV_LOG_ERROR, "Conversion will not be lossless.\n");
79 if(av_get_sample_fmt_name(st->codec->sample_fmt))
80 av_log(NULL, AV_LOG_WARNING,
81 "Incompatible sample format '%s' for codec '%s', auto-selecting format '%s'\n",
82 av_get_sample_fmt_name(st->codec->sample_fmt),
84 av_get_sample_fmt_name(codec->sample_fmts[0]));
85 st->codec->sample_fmt = codec->sample_fmts[0];
90 static char *choose_pix_fmts(OutputStream *ost)
92 if (ost->keep_pix_fmt) {
94 avfilter_graph_set_auto_convert(ost->filter->graph->graph,
95 AVFILTER_AUTO_CONVERT_NONE);
96 if (ost->st->codec->pix_fmt == PIX_FMT_NONE)
98 return av_strdup(av_get_pix_fmt_name(ost->st->codec->pix_fmt));
100 if (ost->st->codec->pix_fmt != PIX_FMT_NONE) {
101 return av_strdup(av_get_pix_fmt_name(choose_pixel_fmt(ost->st, ost->enc, ost->st->codec->pix_fmt)));
102 } else if (ost->enc && ost->enc->pix_fmts) {
103 const enum PixelFormat *p;
104 AVIOContext *s = NULL;
108 if (avio_open_dyn_buf(&s) < 0)
111 p = ost->enc->pix_fmts;
112 if (ost->st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
113 if (ost->st->codec->codec_id == AV_CODEC_ID_MJPEG) {
114 p = (const enum PixelFormat[]) { PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE };
115 } else if (ost->st->codec->codec_id == AV_CODEC_ID_LJPEG) {
116 p = (const enum PixelFormat[]) { PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ444P, PIX_FMT_YUV420P,
117 PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_BGRA, PIX_FMT_NONE };
121 for (; *p != PIX_FMT_NONE; p++) {
122 const char *name = av_get_pix_fmt_name(*p);
123 avio_printf(s, "%s:", name);
125 len = avio_close_dyn_buf(s, &ret);
133 * Define a function for building a string containing a list of
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 PixelFormat, pix_fmt, pix_fmts, 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 fg->outputs = grow_array(fg->outputs, sizeof(*fg->outputs), &fg->nb_outputs,
184 if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
186 fg->outputs[0]->ost = ost;
187 fg->outputs[0]->graph = fg;
189 ost->filter = fg->outputs[0];
191 fg->inputs = grow_array(fg->inputs, sizeof(*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 ist->filters = grow_array(ist->filters, sizeof(*ist->filters),
199 &ist->nb_filters, ist->nb_filters + 1);
200 ist->filters[ist->nb_filters - 1] = fg->inputs[0];
202 filtergraphs = grow_array(filtergraphs, sizeof(*filtergraphs),
203 &nb_filtergraphs, nb_filtergraphs + 1);
204 filtergraphs[nb_filtergraphs - 1] = fg;
209 static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
211 InputStream *ist = NULL;
212 enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
215 // TODO: support other filter types
216 if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
217 av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
226 int file_idx = strtol(in->name, &p, 0);
228 if (file_idx < 0 || file_idx >= nb_input_files) {
229 av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
230 file_idx, fg->graph_desc);
233 s = input_files[file_idx]->ctx;
235 for (i = 0; i < s->nb_streams; i++) {
236 enum AVMediaType stream_type = s->streams[i]->codec->codec_type;
237 if (stream_type != type &&
238 !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
239 type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
241 if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
247 av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
248 "matches no streams.\n", p, fg->graph_desc);
251 ist = input_streams[input_files[file_idx]->ist_index + st->index];
253 /* find the first unused stream of corresponding type */
254 for (i = 0; i < nb_input_streams; i++) {
255 ist = input_streams[i];
256 if (ist->st->codec->codec_type == type && ist->discard)
259 if (i == nb_input_streams) {
260 av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
261 "unlabeled input pad %d on filter %s\n", in->pad_idx,
262 in->filter_ctx->name);
269 ist->decoding_needed++;
270 ist->st->discard = AVDISCARD_NONE;
272 fg->inputs = grow_array(fg->inputs, sizeof(*fg->inputs),
273 &fg->nb_inputs, fg->nb_inputs + 1);
274 if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
276 fg->inputs[fg->nb_inputs - 1]->ist = ist;
277 fg->inputs[fg->nb_inputs - 1]->graph = fg;
279 ist->filters = grow_array(ist->filters, sizeof(*ist->filters),
280 &ist->nb_filters, ist->nb_filters + 1);
281 ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
284 static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
287 OutputStream *ost = ofilter->ost;
288 AVCodecContext *codec = ost->st->codec;
289 AVFilterContext *last_filter = out->filter_ctx;
290 int pad_idx = out->pad_idx;
293 AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
295 snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
296 ret = avfilter_graph_create_filter(&ofilter->filter,
297 avfilter_get_by_name("ffbuffersink"),
298 name, NULL, NULL/*buffersink_params*/, fg->graph);
299 av_freep(&buffersink_params);
304 if (codec->width || codec->height) {
306 AVFilterContext *filter;
308 snprintf(args, sizeof(args), "%d:%d:flags=0x%X",
311 (unsigned)ost->sws_flags);
312 snprintf(name, sizeof(name), "scaler for output stream %d:%d",
313 ost->file_index, ost->index);
314 if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
315 name, args, NULL, fg->graph)) < 0)
317 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
320 last_filter = filter;
324 if ((pix_fmts = choose_pix_fmts(ost))) {
325 AVFilterContext *filter;
326 snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
327 ost->file_index, ost->index);
328 if ((ret = avfilter_graph_create_filter(&filter,
329 avfilter_get_by_name("format"),
330 "format", pix_fmts, NULL,
333 if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
336 last_filter = filter;
341 if (ost->frame_rate.num && 0) {
342 AVFilterContext *fps;
345 snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
346 ost->frame_rate.den);
347 snprintf(name, sizeof(name), "fps for output stream %d:%d",
348 ost->file_index, ost->index);
349 ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
350 name, args, NULL, fg->graph);
354 ret = avfilter_link(last_filter, pad_idx, fps, 0);
361 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
367 static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
369 OutputStream *ost = ofilter->ost;
370 AVCodecContext *codec = ost->st->codec;
371 AVFilterContext *last_filter = out->filter_ctx;
372 int pad_idx = out->pad_idx;
373 char *sample_fmts, *sample_rates, *channel_layouts;
378 snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
379 ret = avfilter_graph_create_filter(&ofilter->filter,
380 avfilter_get_by_name("ffabuffersink"),
381 name, NULL, NULL, 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 len += snprintf(args + len, sizeof(args) - len, "sample_fmts=%s:",
433 len += snprintf(args + len, sizeof(args) - len, "sample_rates=%s:",
436 len += snprintf(args + len, sizeof(args) - len, "channel_layouts=%s:",
440 av_freep(&sample_fmts);
441 av_freep(&sample_rates);
442 av_freep(&channel_layouts);
444 snprintf(name, sizeof(name), "audio format for output stream %d:%d",
445 ost->file_index, ost->index);
446 ret = avfilter_graph_create_filter(&format,
447 avfilter_get_by_name("aformat"),
448 name, args, NULL, fg->graph);
452 ret = avfilter_link(last_filter, pad_idx, format, 0);
456 last_filter = format;
460 if (audio_volume != 256 && 0) {
463 snprintf(args, sizeof(args), "%f", audio_volume / 256.);
464 AUTO_INSERT_FILTER("-vol", "volume", args);
467 if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
473 #define DESCRIBE_FILTER_LINK(f, inout, in) \
475 AVFilterContext *ctx = inout->filter_ctx; \
476 AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads; \
477 int nb_pads = in ? ctx->input_count : ctx->output_count; \
480 if (avio_open_dyn_buf(&pb) < 0) \
483 avio_printf(pb, "%s", ctx->filter->name); \
485 avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
487 avio_close_dyn_buf(pb, &f->name); \
490 int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
492 av_freep(&ofilter->name);
493 DESCRIBE_FILTER_LINK(ofilter, out, 0);
495 switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
496 case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
497 case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
498 default: av_assert0(0);
502 static int sub2video_prepare(InputStream *ist)
504 AVFormatContext *avf = input_files[ist->file_index]->ctx;
509 /* Compute the size of the canvas for the subtitles stream.
510 If the subtitles codec has set a size, use it. Otherwise use the
511 maximum dimensions of the video streams in the same file. */
512 w = ist->st->codec->width;
513 h = ist->st->codec->height;
515 for (i = 0; i < avf->nb_streams; i++) {
516 if (avf->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
517 w = FFMAX(w, avf->streams[i]->codec->width);
518 h = FFMAX(h, avf->streams[i]->codec->height);
525 av_log(avf, AV_LOG_INFO, "sub2video: using %dx%d canvas\n", w, h);
527 ist->sub2video.w = ist->st->codec->width = w;
528 ist->sub2video.h = ist->st->codec->height = h;
530 /* rectangles are PIX_FMT_PAL8, but we have no guarantee that the
531 palettes for all rectangles are identical or compatible */
532 ist->st->codec->pix_fmt = PIX_FMT_RGB32;
534 ret = av_image_alloc(image, linesize, w, h, PIX_FMT_RGB32, 32);
537 memset(image[0], 0, h * linesize[0]);
538 ist->sub2video.ref = avfilter_get_video_buffer_ref_from_arrays(
539 image, linesize, AV_PERM_READ | AV_PERM_PRESERVE,
540 w, h, PIX_FMT_RGB32);
541 if (!ist->sub2video.ref) {
543 return AVERROR(ENOMEM);
548 static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
551 AVFilterContext *first_filter = in->filter_ctx;
552 AVFilter *filter = avfilter_get_by_name("buffer");
553 InputStream *ist = ifilter->ist;
554 AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
556 AVRational fr = ist->framerate.num ? ist->framerate :
557 ist->st->r_frame_rate;
561 int pad_idx = in->pad_idx;
564 if (ist->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
565 ret = sub2video_prepare(ist);
570 sar = ist->st->sample_aspect_ratio.num ?
571 ist->st->sample_aspect_ratio :
572 ist->st->codec->sample_aspect_ratio;
574 sar = (AVRational){0,1};
575 av_bprint_init(&args, 0, 1);
577 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
578 "pixel_aspect=%d/%d:sws_param=flags=%d", ist->st->codec->width,
579 ist->st->codec->height, ist->st->codec->pix_fmt,
580 tb.num, tb.den, sar.num, sar.den,
581 SWS_BILINEAR + ((ist->st->codec->flags&CODEC_FLAG_BITEXACT) ? SWS_BITEXACT:0));
582 if (fr.num && fr.den)
583 av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
584 snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
585 ist->file_index, ist->st->index);
587 if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter, name,
588 args.str, NULL, fg->graph)) < 0)
591 if (ist->framerate.num) {
592 AVFilterContext *setpts;
594 snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
595 ist->file_index, ist->st->index);
596 if ((ret = avfilter_graph_create_filter(&setpts,
597 avfilter_get_by_name("setpts"),
602 if ((ret = avfilter_link(setpts, 0, first_filter, pad_idx)) < 0)
605 first_filter = setpts;
609 if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
614 static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
617 AVFilterContext *first_filter = in->filter_ctx;
618 AVFilter *filter = avfilter_get_by_name("abuffer");
619 InputStream *ist = ifilter->ist;
620 int pad_idx = in->pad_idx;
621 char args[255], name[255];
624 snprintf(args, sizeof(args), "time_base=%d/%d:sample_rate=%d:sample_fmt=%s"
625 ":channel_layout=0x%"PRIx64,
626 1, ist->st->codec->sample_rate,
627 ist->st->codec->sample_rate,
628 av_get_sample_fmt_name(ist->st->codec->sample_fmt),
629 ist->st->codec->channel_layout);
630 snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
631 ist->file_index, ist->st->index);
633 if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter,
638 #define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do { \
639 AVFilterContext *filt_ctx; \
641 av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
642 "similarly to -af " filter_name "=%s.\n", arg); \
644 snprintf(name, sizeof(name), "graph %d %s for input stream %d:%d", \
645 fg->index, filter_name, ist->file_index, ist->st->index); \
646 ret = avfilter_graph_create_filter(&filt_ctx, \
647 avfilter_get_by_name(filter_name), \
648 name, arg, NULL, fg->graph); \
652 ret = avfilter_link(filt_ctx, 0, first_filter, pad_idx); \
656 first_filter = filt_ctx; \
659 if (audio_sync_method > 0) {
660 char args[256] = {0};
662 av_strlcatf(args, sizeof(args), "min_comp=0.001:min_hard_comp=%f", audio_drift_threshold);
663 if (audio_sync_method > 1)
664 av_strlcatf(args, sizeof(args), ":max_soft_comp=%f", audio_sync_method/(double)ist->st->codec->sample_rate);
665 AUTO_INSERT_FILTER_INPUT("-async", "aresample", args);
668 // if (ost->audio_channels_mapped) {
671 // av_bprint_init(&pan_buf, 256, 8192);
672 // av_bprintf(&pan_buf, "0x%"PRIx64,
673 // av_get_default_channel_layout(ost->audio_channels_mapped));
674 // for (i = 0; i < ost->audio_channels_mapped; i++)
675 // if (ost->audio_channels_map[i] != -1)
676 // av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
677 // AUTO_INSERT_FILTER_INPUT("-map_channel", "pan", pan_buf.str);
678 // av_bprint_finalize(&pan_buf, NULL);
681 if (audio_volume != 256) {
684 snprintf(args, sizeof(args), "%f", audio_volume / 256.);
685 AUTO_INSERT_FILTER_INPUT("-vol", "volume", args);
687 if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
693 static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
696 av_freep(&ifilter->name);
697 DESCRIBE_FILTER_LINK(ifilter, in, 1);
699 switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
700 case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
701 case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
702 default: av_assert0(0);
706 int configure_filtergraph(FilterGraph *fg)
708 AVFilterInOut *inputs, *outputs, *cur;
709 int ret, i, init = !fg->graph, simple = !fg->graph_desc;
710 const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
713 avfilter_graph_free(&fg->graph);
714 if (!(fg->graph = avfilter_graph_alloc()))
715 return AVERROR(ENOMEM);
718 OutputStream *ost = fg->outputs[0]->ost;
720 snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
721 fg->graph->scale_sws_opts = av_strdup(args);
724 if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
727 if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
728 av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
729 "exactly one input and output.\n", graph_desc);
730 return AVERROR(EINVAL);
733 for (cur = inputs; !simple && init && cur; cur = cur->next)
734 init_input_filter(fg, cur);
736 for (cur = inputs, i = 0; cur; cur = cur->next, i++)
737 if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
739 avfilter_inout_free(&inputs);
741 if (!init || simple) {
742 /* we already know the mappings between lavfi outputs and output streams,
743 * so we can finish the setup */
744 for (cur = outputs, i = 0; cur; cur = cur->next, i++)
745 configure_output_filter(fg, fg->outputs[i], cur);
746 avfilter_inout_free(&outputs);
748 if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
751 /* wait until output mappings are processed */
752 for (cur = outputs; cur;) {
753 fg->outputs = grow_array(fg->outputs, sizeof(*fg->outputs),
754 &fg->nb_outputs, fg->nb_outputs + 1);
755 if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]))))
757 fg->outputs[fg->nb_outputs - 1]->graph = fg;
758 fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
760 fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
767 int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
770 for (i = 0; i < fg->nb_inputs; i++)
771 if (fg->inputs[i]->ist == ist)