+ ost->filter = fg->outputs[0];
+
+ return 0;
+}
+
+static FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost)
+{
+ FilterGraph *fg = av_mallocz(sizeof(*fg));
+
+ if (!fg)
+ exit_program(1);
+ fg->index = nb_filtergraphs;
+
+ fg->outputs = grow_array(fg->outputs, sizeof(*fg->outputs), &fg->nb_outputs,
+ fg->nb_outputs + 1);
+ if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
+ exit_program(1);
+ fg->outputs[0]->ost = ost;
+ fg->outputs[0]->graph = fg;
+
+ fg->inputs = grow_array(fg->inputs, sizeof(*fg->inputs), &fg->nb_inputs,
+ fg->nb_inputs + 1);
+ if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
+ exit_program(1);
+ fg->inputs[0]->ist = ist;
+ fg->inputs[0]->graph = fg;
+
+ ist->filters = grow_array(ist->filters, sizeof(*ist->filters),
+ &ist->nb_filters, ist->nb_filters + 1);
+ ist->filters[ist->nb_filters - 1] = fg->inputs[0];
+
+ filtergraphs = grow_array(filtergraphs, sizeof(*filtergraphs),
+ &nb_filtergraphs, nb_filtergraphs + 1);
+ filtergraphs[nb_filtergraphs - 1] = fg;
+
+ return fg;
+}
+
+static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
+{
+ InputStream *ist;
+ enum AVMediaType type = in->filter_ctx->input_pads[in->pad_idx].type;
+ int i;
+
+ // TODO: support other filter types
+ if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
+ av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
+ "currently.\n");
+ exit_program(1);
+ }
+
+ if (in->name) {
+ AVFormatContext *s;
+ AVStream *st = NULL;
+ char *p;
+ int file_idx = strtol(in->name, &p, 0);
+
+ if (file_idx < 0 || file_idx >= nb_input_files) {
+ av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtegraph description %s.\n",
+ file_idx, fg->graph_desc);
+ exit_program(1);
+ }
+ s = input_files[file_idx]->ctx;
+
+ for (i = 0; i < s->nb_streams; i++) {
+ if (s->streams[i]->codec->codec_type != type)
+ continue;
+ if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
+ st = s->streams[i];
+ break;
+ }
+ }
+ if (!st) {
+ av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
+ "matches no streams.\n", p, fg->graph_desc);
+ exit_program(1);
+ }
+ ist = input_streams[input_files[file_idx]->ist_index + st->index];
+ } else {
+ /* find the first unused stream of corresponding type */
+ for (i = 0; i < nb_input_streams; i++) {
+ ist = input_streams[i];
+ if (ist->st->codec->codec_type == type && ist->discard)
+ break;
+ }
+ if (i == nb_input_streams) {
+ av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
+ "unlabeled input pad %d on filter %s", in->pad_idx,
+ in->filter_ctx->name);
+ exit_program(1);
+ }
+ }
+ ist->discard = 0;
+ ist->decoding_needed = 1;
+ ist->st->discard = AVDISCARD_NONE;
+
+ fg->inputs = grow_array(fg->inputs, sizeof(*fg->inputs),
+ &fg->nb_inputs, fg->nb_inputs + 1);
+ if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
+ exit_program(1);
+ fg->inputs[fg->nb_inputs - 1]->ist = ist;
+ fg->inputs[fg->nb_inputs - 1]->graph = fg;
+
+ ist->filters = grow_array(ist->filters, sizeof(*ist->filters),
+ &ist->nb_filters, ist->nb_filters + 1);
+ ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
+}
+
+static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
+{
+ char *pix_fmts;
+ AVCodecContext *codec = ofilter->ost->st->codec;
+ AVFilterContext *last_filter = out->filter_ctx;
+ int pad_idx = out->pad_idx;
+ int ret;
+
+
+ ret = avfilter_graph_create_filter(&ofilter->filter,
+ avfilter_get_by_name("buffersink"),
+ "out", NULL, pix_fmts, fg->graph);
+ if (ret < 0)
+ return ret;
+
+ if (codec->width || codec->height) {
+ char args[255];
+ AVFilterContext *filter;
+
+ snprintf(args, sizeof(args), "%d:%d:flags=0x%X",
+ codec->width,
+ codec->height,
+ (unsigned)ofilter->ost->sws_flags);
+ if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
+ NULL, args, NULL, fg->graph)) < 0)
+ return ret;
+ if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
+ return ret;
+
+ last_filter = filter;
+ pad_idx = 0;
+ }
+
+ if ((pix_fmts = choose_pix_fmts(ofilter->ost))) {
+ AVFilterContext *filter;
+ if ((ret = avfilter_graph_create_filter(&filter,
+ avfilter_get_by_name("format"),
+ "format", pix_fmts, NULL,
+ fg->graph)) < 0)
+ return ret;
+ if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
+ return ret;
+
+ last_filter = filter;
+ pad_idx = 0;
+ av_freep(&pix_fmts);
+ }
+
+ if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
+ return ret;
+
+ return 0;
+}
+
+static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
+{
+ OutputStream *ost = ofilter->ost;
+ AVCodecContext *codec = ost->st->codec;
+ AVFilterContext *last_filter = out->filter_ctx;
+ int pad_idx = out->pad_idx;
+ char *sample_fmts, *sample_rates, *channel_layouts;
+ int ret;
+
+ ret = avfilter_graph_create_filter(&ofilter->filter,
+ avfilter_get_by_name("abuffersink"),
+ "out", NULL, NULL, fg->graph);
+ if (ret < 0)
+ return ret;
+
+ if (codec->channels && !codec->channel_layout)
+ codec->channel_layout = av_get_default_channel_layout(codec->channels);
+
+ sample_fmts = choose_sample_fmts(ost);
+ sample_rates = choose_sample_rates(ost);
+ channel_layouts = choose_channel_layouts(ost);
+ if (sample_fmts || sample_rates || channel_layouts) {
+ AVFilterContext *format;
+ char args[256];
+ int len = 0;
+
+ if (sample_fmts)
+ len += snprintf(args + len, sizeof(args) - len, "sample_fmts=%s:",
+ sample_fmts);
+ if (sample_rates)
+ len += snprintf(args + len, sizeof(args) - len, "sample_rates=%s:",
+ sample_rates);
+ if (channel_layouts)
+ len += snprintf(args + len, sizeof(args) - len, "channel_layouts=%s:",
+ channel_layouts);
+ args[len - 1] = 0;
+
+ av_freep(&sample_fmts);
+ av_freep(&sample_rates);
+ av_freep(&channel_layouts);
+
+ ret = avfilter_graph_create_filter(&format,
+ avfilter_get_by_name("aformat"),
+ "aformat", args, NULL, fg->graph);
+ if (ret < 0)
+ return ret;
+
+ ret = avfilter_link(last_filter, pad_idx, format, 0);
+ if (ret < 0)
+ return ret;
+
+ last_filter = format;
+ pad_idx = 0;
+ }
+
+ if (audio_sync_method > 0) {
+ AVFilterContext *async;
+ char args[256];
+ int len = 0;
+
+ av_log(NULL, AV_LOG_WARNING, "-async has been deprecated. Used the "
+ "asyncts audio filter instead.\n");
+
+ if (audio_sync_method > 1)
+ len += snprintf(args + len, sizeof(args) - len, "compensate=1:"
+ "max_comp=%d:", audio_sync_method);
+ snprintf(args + len, sizeof(args) - len, "min_delta=%f",
+ audio_drift_threshold);
+
+ ret = avfilter_graph_create_filter(&async,
+ avfilter_get_by_name("asyncts"),
+ "async", args, NULL, fg->graph);
+ if (ret < 0)
+ return ret;
+
+ ret = avfilter_link(last_filter, pad_idx, async, 0);
+ if (ret < 0)
+ return ret;
+
+ last_filter = async;
+ pad_idx = 0;
+ }
+
+ if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
+ return ret;
+
+ return 0;
+}
+
+static int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
+{
+ switch (out->filter_ctx->output_pads[out->pad_idx].type) {
+ case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
+ case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
+ default: av_assert0(0);
+ }
+}
+
+static int configure_complex_filter(FilterGraph *fg)
+{
+ AVFilterInOut *inputs, *outputs, *cur;
+ int ret, i, init = !fg->graph;
+
+ avfilter_graph_free(&fg->graph);
+ if (!(fg->graph = avfilter_graph_alloc()))
+ return AVERROR(ENOMEM);
+
+ if ((ret = avfilter_graph_parse2(fg->graph, fg->graph_desc, &inputs, &outputs)) < 0)
+ return ret;
+
+ for (cur = inputs; init && cur; cur = cur->next)
+ init_input_filter(fg, cur);
+
+ for (cur = inputs, i = 0; cur; cur = cur->next, i++) {
+ InputFilter *ifilter = fg->inputs[i];
+ InputStream *ist = ifilter->ist;
+ AVRational sar;
+ AVFilter *filter;
+ char args[255];
+
+ switch (cur->filter_ctx->input_pads[cur->pad_idx].type) {
+ case AVMEDIA_TYPE_VIDEO:
+ sar = ist->st->sample_aspect_ratio.num ?
+ ist->st->sample_aspect_ratio :
+ ist->st->codec->sample_aspect_ratio;
+ snprintf(args, sizeof(args), "%d:%d:%d:%d:%d:%d:%d", ist->st->codec->width,
+ ist->st->codec->height, ist->st->codec->pix_fmt, 1, AV_TIME_BASE,
+ sar.num, sar.den);
+ filter = avfilter_get_by_name("buffer");
+ break;
+ case AVMEDIA_TYPE_AUDIO:
+ snprintf(args, sizeof(args), "time_base=%d/%d:sample_rate=%d:"
+ "sample_fmt=%s:channel_layout=0x%"PRIx64,
+ ist->st->time_base.num, ist->st->time_base.den,
+ ist->st->codec->sample_rate,
+ av_get_sample_fmt_name(ist->st->codec->sample_fmt),
+ ist->st->codec->channel_layout);
+ filter = avfilter_get_by_name("abuffer");
+ break;
+ default:
+ av_assert0(0);
+ }
+
+ if ((ret = avfilter_graph_create_filter(&ifilter->filter,
+ filter, cur->name,
+ args, NULL, fg->graph)) < 0)
+ return ret;
+ if ((ret = avfilter_link(ifilter->filter, 0,
+ cur->filter_ctx, cur->pad_idx)) < 0)
+ return ret;
+ }
+ avfilter_inout_free(&inputs);
+
+ if (!init) {
+ /* we already know the mappings between lavfi outputs and output streams,
+ * so we can finish the setup */
+ for (cur = outputs, i = 0; cur; cur = cur->next, i++)
+ configure_output_filter(fg, fg->outputs[i], cur);
+ avfilter_inout_free(&outputs);
+
+ if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
+ return ret;
+ } else {
+ /* wait until output mappings are processed */
+ for (cur = outputs; cur;) {
+ fg->outputs = grow_array(fg->outputs, sizeof(*fg->outputs),
+ &fg->nb_outputs, fg->nb_outputs + 1);
+ if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]))))
+ exit_program(1);
+ fg->outputs[fg->nb_outputs - 1]->graph = fg;
+ fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
+ cur = cur->next;
+ fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
+ }
+ }
+
+ return 0;
+}
+
+static int configure_complex_filters(void)
+{
+ int i, ret = 0;