FFmpeg  1.2.12
ffmpeg_filter.c
Go to the documentation of this file.
1 /*
2  * ffmpeg filter configuration
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include "ffmpeg.h"
22 
23 #include "libavfilter/avfilter.h"
24 #include "libavfilter/avfiltergraph.h"
25 #include "libavfilter/buffersink.h"
26 
28 
29 #include "libavutil/avassert.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/bprint.h"
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/pixfmt.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/samplefmt.h"
38 
40 {
41  if (codec && codec->pix_fmts) {
42  const enum AVPixelFormat *p = codec->pix_fmts;
43  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(target);
44  int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
45  enum AVPixelFormat best= AV_PIX_FMT_NONE;
49 
51  if (st->codec->codec_id == AV_CODEC_ID_MJPEG) {
52  p = mjpeg_formats;
53  } else if (st->codec->codec_id == AV_CODEC_ID_LJPEG) {
54  p =ljpeg_formats;
55  }
56  }
57  for (; *p != AV_PIX_FMT_NONE; p++) {
58  best= avcodec_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
59  if (*p == target)
60  break;
61  }
62  if (*p == AV_PIX_FMT_NONE) {
63  if (target != AV_PIX_FMT_NONE)
65  "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
66  av_get_pix_fmt_name(target),
67  codec->name,
68  av_get_pix_fmt_name(best));
69  return best;
70  }
71  }
72  return target;
73 }
74 
76 {
77  if (codec && codec->sample_fmts) {
78  const enum AVSampleFormat *p = codec->sample_fmts;
79  for (; *p != -1; p++) {
80  if (*p == st->codec->sample_fmt)
81  break;
82  }
83  if (*p == -1) {
85  av_log(NULL, AV_LOG_ERROR, "Conversion will not be lossless.\n");
88  "Incompatible sample format '%s' for codec '%s', auto-selecting format '%s'\n",
90  codec->name,
92  st->codec->sample_fmt = codec->sample_fmts[0];
93  }
94  }
95 }
96 
97 static char *choose_pix_fmts(OutputStream *ost)
98 {
99  if (ost->keep_pix_fmt) {
100  if (ost->filter)
103  if (ost->st->codec->pix_fmt == AV_PIX_FMT_NONE)
104  return NULL;
105  return av_strdup(av_get_pix_fmt_name(ost->st->codec->pix_fmt));
106  }
107  if (ost->st->codec->pix_fmt != AV_PIX_FMT_NONE) {
108  return av_strdup(av_get_pix_fmt_name(choose_pixel_fmt(ost->st, ost->enc, ost->st->codec->pix_fmt)));
109  } else if (ost->enc && ost->enc->pix_fmts) {
110  const enum AVPixelFormat *p;
111  AVIOContext *s = NULL;
112  uint8_t *ret;
113  int len;
114 
115  if (avio_open_dyn_buf(&s) < 0)
116  exit(1);
117 
118  p = ost->enc->pix_fmts;
120  if (ost->st->codec->codec_id == AV_CODEC_ID_MJPEG) {
122  } else if (ost->st->codec->codec_id == AV_CODEC_ID_LJPEG) {
125  }
126  }
127 
128  for (; *p != AV_PIX_FMT_NONE; p++) {
129  const char *name = av_get_pix_fmt_name(*p);
130  avio_printf(s, "%s:", name);
131  }
132  len = avio_close_dyn_buf(s, &ret);
133  ret[len - 1] = 0;
134  return ret;
135  } else
136  return NULL;
137 }
138 
139 /* Define a function for building a string containing a list of
140  * allowed formats. */
141 #define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name, separator)\
142 static char *choose_ ## var ## s(OutputStream *ost) \
143 { \
144  if (ost->st->codec->var != none) { \
145  get_name(ost->st->codec->var); \
146  return av_strdup(name); \
147  } else if (ost->enc && ost->enc->supported_list) { \
148  const type *p; \
149  AVIOContext *s = NULL; \
150  uint8_t *ret; \
151  int len; \
152  \
153  if (avio_open_dyn_buf(&s) < 0) \
154  exit(1); \
155  \
156  for (p = ost->enc->supported_list; *p != none; p++) { \
157  get_name(*p); \
158  avio_printf(s, "%s" separator, name); \
159  } \
160  len = avio_close_dyn_buf(s, &ret); \
161  ret[len - 1] = 0; \
162  return ret; \
163  } else \
164  return NULL; \
165 }
166 
167 // DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE,
168 // GET_PIX_FMT_NAME, ":")
169 
172 
175 
176 DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0,
177  GET_CH_LAYOUT_NAME, ",")
178 
180 {
181  FilterGraph *fg = av_mallocz(sizeof(*fg));
182 
183  if (!fg)
184  exit(1);
185  fg->index = nb_filtergraphs;
186 
187  GROW_ARRAY(fg->outputs, fg->nb_outputs);
188  if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
189  exit(1);
190  fg->outputs[0]->ost = ost;
191  fg->outputs[0]->graph = fg;
192 
193  ost->filter = fg->outputs[0];
194 
195  GROW_ARRAY(fg->inputs, fg->nb_inputs);
196  if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
197  exit(1);
198  fg->inputs[0]->ist = ist;
199  fg->inputs[0]->graph = fg;
200 
201  GROW_ARRAY(ist->filters, ist->nb_filters);
202  ist->filters[ist->nb_filters - 1] = fg->inputs[0];
203 
205  filtergraphs[nb_filtergraphs - 1] = fg;
206 
207  return fg;
208 }
209 
211 {
212  InputStream *ist = NULL;
214  int i;
215 
216  // TODO: support other filter types
217  if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
218  av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
219  "currently.\n");
220  exit(1);
221  }
222 
223  if (in->name) {
224  AVFormatContext *s;
225  AVStream *st = NULL;
226  char *p;
227  int file_idx = strtol(in->name, &p, 0);
228 
229  if (file_idx < 0 || file_idx >= nb_input_files) {
230  av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
231  file_idx, fg->graph_desc);
232  exit(1);
233  }
234  s = input_files[file_idx]->ctx;
235 
236  for (i = 0; i < s->nb_streams; i++) {
237  enum AVMediaType stream_type = s->streams[i]->codec->codec_type;
238  if (stream_type != type &&
239  !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
240  type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
241  continue;
242  if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
243  st = s->streams[i];
244  break;
245  }
246  }
247  if (!st) {
248  av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
249  "matches no streams.\n", p, fg->graph_desc);
250  exit(1);
251  }
252  ist = input_streams[input_files[file_idx]->ist_index + st->index];
253  } else {
254  /* find the first unused stream of corresponding type */
255  for (i = 0; i < nb_input_streams; i++) {
256  ist = input_streams[i];
257  if (ist->st->codec->codec_type == type && ist->discard)
258  break;
259  }
260  if (i == nb_input_streams) {
261  av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
262  "unlabeled input pad %d on filter %s\n", in->pad_idx,
263  in->filter_ctx->name);
264  exit(1);
265  }
266  }
267  av_assert0(ist);
268 
269  ist->discard = 0;
270  ist->decoding_needed++;
271  ist->st->discard = AVDISCARD_NONE;
272 
273  GROW_ARRAY(fg->inputs, fg->nb_inputs);
274  if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
275  exit(1);
276  fg->inputs[fg->nb_inputs - 1]->ist = ist;
277  fg->inputs[fg->nb_inputs - 1]->graph = fg;
278 
279  GROW_ARRAY(ist->filters, ist->nb_filters);
280  ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
281 }
282 
284 {
285  char *pix_fmts;
286  OutputStream *ost = ofilter->ost;
287  AVCodecContext *codec = ost->st->codec;
288  AVFilterContext *last_filter = out->filter_ctx;
289  int pad_idx = out->pad_idx;
290  int ret;
291  char name[255];
292  AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
293 
294  snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
295  ret = avfilter_graph_create_filter(&ofilter->filter,
296  avfilter_get_by_name("ffbuffersink"),
297  name, NULL, NULL, fg->graph);
298  av_freep(&buffersink_params);
299 
300  if (ret < 0)
301  return ret;
302 
303  if (codec->width || codec->height) {
304  char args[255];
306 
307  snprintf(args, sizeof(args), "%d:%d:flags=0x%X",
308  codec->width,
309  codec->height,
310  (unsigned)ost->sws_flags);
311  snprintf(name, sizeof(name), "scaler for output stream %d:%d",
312  ost->file_index, ost->index);
313  if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
314  name, args, NULL, fg->graph)) < 0)
315  return ret;
316  if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
317  return ret;
318 
319  last_filter = filter;
320  pad_idx = 0;
321  }
322 
323  if ((pix_fmts = choose_pix_fmts(ost))) {
325  snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
326  ost->file_index, ost->index);
327  if ((ret = avfilter_graph_create_filter(&filter,
328  avfilter_get_by_name("format"),
329  "format", pix_fmts, NULL,
330  fg->graph)) < 0)
331  return ret;
332  if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
333  return ret;
334 
335  last_filter = filter;
336  pad_idx = 0;
337  av_freep(&pix_fmts);
338  }
339 
340  if (ost->frame_rate.num && 0) {
341  AVFilterContext *fps;
342  char args[255];
343 
344  snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
345  ost->frame_rate.den);
346  snprintf(name, sizeof(name), "fps for output stream %d:%d",
347  ost->file_index, ost->index);
349  name, args, NULL, fg->graph);
350  if (ret < 0)
351  return ret;
352 
353  ret = avfilter_link(last_filter, pad_idx, fps, 0);
354  if (ret < 0)
355  return ret;
356  last_filter = fps;
357  pad_idx = 0;
358  }
359 
360  if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
361  return ret;
362 
363  return 0;
364 }
365 
367 {
368  OutputStream *ost = ofilter->ost;
369  AVCodecContext *codec = ost->st->codec;
370  AVFilterContext *last_filter = out->filter_ctx;
371  int pad_idx = out->pad_idx;
372  char *sample_fmts, *sample_rates, *channel_layouts;
373  char name[255];
374  int ret;
376 
377  if (!params)
378  return AVERROR(ENOMEM);
379  params->all_channel_counts = 1;
380  snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
381  ret = avfilter_graph_create_filter(&ofilter->filter,
382  avfilter_get_by_name("ffabuffersink"),
383  name, NULL, params, fg->graph);
384  av_freep(&params);
385  if (ret < 0)
386  return ret;
387 
388 #define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \
389  AVFilterContext *filt_ctx; \
390  \
391  av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
392  "similarly to -af " filter_name "=%s.\n", arg); \
393  \
394  ret = avfilter_graph_create_filter(&filt_ctx, \
395  avfilter_get_by_name(filter_name), \
396  filter_name, arg, NULL, fg->graph); \
397  if (ret < 0) \
398  return ret; \
399  \
400  ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0); \
401  if (ret < 0) \
402  return ret; \
403  \
404  last_filter = filt_ctx; \
405  pad_idx = 0; \
406 } while (0)
407  if (ost->audio_channels_mapped) {
408  int i;
409  AVBPrint pan_buf;
410  av_bprint_init(&pan_buf, 256, 8192);
411  av_bprintf(&pan_buf, "0x%"PRIx64,
413  for (i = 0; i < ost->audio_channels_mapped; i++)
414  if (ost->audio_channels_map[i] != -1)
415  av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
416 
417  AUTO_INSERT_FILTER("-map_channel", "pan", pan_buf.str);
418  av_bprint_finalize(&pan_buf, NULL);
419  }
420 
421  if (codec->channels && !codec->channel_layout)
423 
424  sample_fmts = choose_sample_fmts(ost);
425  sample_rates = choose_sample_rates(ost);
426  channel_layouts = choose_channel_layouts(ost);
427  if (sample_fmts || sample_rates || channel_layouts) {
428  AVFilterContext *format;
429  char args[256];
430  args[0] = 0;
431 
432  if (sample_fmts)
433  av_strlcatf(args, sizeof(args), "sample_fmts=%s:",
434  sample_fmts);
435  if (sample_rates)
436  av_strlcatf(args, sizeof(args), "sample_rates=%s:",
437  sample_rates);
438  if (channel_layouts)
439  av_strlcatf(args, sizeof(args), "channel_layouts=%s:",
440  channel_layouts);
441 
442  av_freep(&sample_fmts);
443  av_freep(&sample_rates);
444  av_freep(&channel_layouts);
445 
446  snprintf(name, sizeof(name), "audio format for output stream %d:%d",
447  ost->file_index, ost->index);
448  ret = avfilter_graph_create_filter(&format,
449  avfilter_get_by_name("aformat"),
450  name, args, NULL, fg->graph);
451  if (ret < 0)
452  return ret;
453 
454  ret = avfilter_link(last_filter, pad_idx, format, 0);
455  if (ret < 0)
456  return ret;
457 
458  last_filter = format;
459  pad_idx = 0;
460  }
461 
462  if (audio_volume != 256 && 0) {
463  char args[256];
464 
465  snprintf(args, sizeof(args), "%f", audio_volume / 256.);
466  AUTO_INSERT_FILTER("-vol", "volume", args);
467  }
468 
469  if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
470  return ret;
471 
472  return 0;
473 }
474 
475 #define DESCRIBE_FILTER_LINK(f, inout, in) \
476 { \
477  AVFilterContext *ctx = inout->filter_ctx; \
478  AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads; \
479  int nb_pads = in ? ctx->input_count : ctx->output_count; \
480  AVIOContext *pb; \
481  \
482  if (avio_open_dyn_buf(&pb) < 0) \
483  exit(1); \
484  \
485  avio_printf(pb, "%s", ctx->filter->name); \
486  if (nb_pads > 1) \
487  avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
488  avio_w8(pb, 0); \
489  avio_close_dyn_buf(pb, &f->name); \
490 }
491 
493 {
494  av_freep(&ofilter->name);
495  DESCRIBE_FILTER_LINK(ofilter, out, 0);
496 
497  switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
498  case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
499  case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
500  default: av_assert0(0);
501  }
502 }
503 
505 {
507  int i, ret, w, h;
508  uint8_t *image[4];
509  int linesize[4];
510 
511  /* Compute the size of the canvas for the subtitles stream.
512  If the subtitles codec has set a size, use it. Otherwise use the
513  maximum dimensions of the video streams in the same file. */
514  w = ist->st->codec->width;
515  h = ist->st->codec->height;
516  if (!(w && h)) {
517  for (i = 0; i < avf->nb_streams; i++) {
518  if (avf->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
519  w = FFMAX(w, avf->streams[i]->codec->width);
520  h = FFMAX(h, avf->streams[i]->codec->height);
521  }
522  }
523  if (!(w && h)) {
524  w = FFMAX(w, 720);
525  h = FFMAX(h, 576);
526  }
527  av_log(avf, AV_LOG_INFO, "sub2video: using %dx%d canvas\n", w, h);
528  }
529  ist->sub2video.w = ist->st->codec->width = ist->resample_width = w;
530  ist->sub2video.h = ist->st->codec->height = ist->resample_height = h;
531 
532  /* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the
533  palettes for all rectangles are identical or compatible */
535 
536  ret = av_image_alloc(image, linesize, w, h, AV_PIX_FMT_RGB32, 32);
537  if (ret < 0)
538  return ret;
539  memset(image[0], 0, h * linesize[0]);
541  image, linesize, AV_PERM_READ | AV_PERM_PRESERVE,
542  w, h, AV_PIX_FMT_RGB32);
543  if (!ist->sub2video.ref) {
544  av_free(image[0]);
545  return AVERROR(ENOMEM);
546  }
547  return 0;
548 }
549 
551  AVFilterInOut *in)
552 {
553  AVFilterContext *first_filter = in->filter_ctx;
554  AVFilter *filter = avfilter_get_by_name("buffer");
555  InputStream *ist = ifilter->ist;
556  AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
557  ist->st->time_base;
558  AVRational fr = ist->framerate.num ? ist->framerate :
559  ist->st->r_frame_rate;
560  AVRational sar;
561  AVBPrint args;
562  char name[255];
563  int pad_idx = in->pad_idx;
564  int ret;
565 
566  if (!ist->framerate.num && ist->st->codec->ticks_per_frame>1) {
567  AVRational codec_fr = av_inv_q(ist->st->codec->time_base);
568  AVRational avg_fr = ist->st->avg_frame_rate;
569  codec_fr.den *= ist->st->codec->ticks_per_frame;
570  if ( codec_fr.num>0 && codec_fr.den>0 && av_q2d(codec_fr) < av_q2d(fr)*0.7
571  && fabs(1.0 - av_q2d(av_div_q(avg_fr, fr)))>0.1)
572  fr = codec_fr;
573  }
574 
575  if (ist->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
576  ret = sub2video_prepare(ist);
577  if (ret < 0)
578  return ret;
579  }
580 
581  sar = ist->st->sample_aspect_ratio.num ?
582  ist->st->sample_aspect_ratio :
584  if(!sar.den)
585  sar = (AVRational){0,1};
586  av_bprint_init(&args, 0, 1);
587  av_bprintf(&args,
588  "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
589  "pixel_aspect=%d/%d:sws_param=flags=%d", ist->resample_width,
591  tb.num, tb.den, sar.num, sar.den,
593  if (fr.num && fr.den)
594  av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
595  snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
596  ist->file_index, ist->st->index);
597 
598  if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter, name,
599  args.str, NULL, fg->graph)) < 0)
600  return ret;
601 
602  if (ist->framerate.num) {
603  AVFilterContext *setpts;
604 
605  snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
606  ist->file_index, ist->st->index);
607  if ((ret = avfilter_graph_create_filter(&setpts,
608  avfilter_get_by_name("setpts"),
609  name, "N", NULL,
610  fg->graph)) < 0)
611  return ret;
612 
613  if ((ret = avfilter_link(setpts, 0, first_filter, pad_idx)) < 0)
614  return ret;
615 
616  first_filter = setpts;
617  pad_idx = 0;
618  }
619 
620  if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
621  return ret;
622  return 0;
623 }
624 
626  AVFilterInOut *in)
627 {
628  AVFilterContext *first_filter = in->filter_ctx;
629  AVFilter *filter = avfilter_get_by_name("abuffer");
630  InputStream *ist = ifilter->ist;
631  int pad_idx = in->pad_idx;
632  AVBPrint args;
633  char name[255];
634  int ret;
635 
637  av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
638  1, ist->st->codec->sample_rate,
639  ist->st->codec->sample_rate,
641  if (ist->st->codec->channel_layout)
642  av_bprintf(&args, ":channel_layout=0x%"PRIx64,
643  ist->st->codec->channel_layout);
644  else
645  av_bprintf(&args, ":channels=%d", ist->st->codec->channels);
646  snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
647  ist->file_index, ist->st->index);
648 
649  if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter,
650  name, args.str, NULL,
651  fg->graph)) < 0)
652  return ret;
653 
654 #define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do { \
655  AVFilterContext *filt_ctx; \
656  \
657  av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
658  "similarly to -af " filter_name "=%s.\n", arg); \
659  \
660  snprintf(name, sizeof(name), "graph %d %s for input stream %d:%d", \
661  fg->index, filter_name, ist->file_index, ist->st->index); \
662  ret = avfilter_graph_create_filter(&filt_ctx, \
663  avfilter_get_by_name(filter_name), \
664  name, arg, NULL, fg->graph); \
665  if (ret < 0) \
666  return ret; \
667  \
668  ret = avfilter_link(filt_ctx, 0, first_filter, pad_idx); \
669  if (ret < 0) \
670  return ret; \
671  \
672  first_filter = filt_ctx; \
673 } while (0)
674 
675  if (audio_sync_method > 0) {
676  char args[256] = {0};
677 
678  av_strlcatf(args, sizeof(args), "async=%d", audio_sync_method);
679  if (audio_drift_threshold != 0.1)
680  av_strlcatf(args, sizeof(args), ":min_hard_comp=%f", audio_drift_threshold);
681  if (!fg->reconfiguration)
682  av_strlcatf(args, sizeof(args), ":first_pts=0");
683  AUTO_INSERT_FILTER_INPUT("-async", "aresample", args);
684  }
685 
686 // if (ost->audio_channels_mapped) {
687 // int i;
688 // AVBPrint pan_buf;
689 // av_bprint_init(&pan_buf, 256, 8192);
690 // av_bprintf(&pan_buf, "0x%"PRIx64,
691 // av_get_default_channel_layout(ost->audio_channels_mapped));
692 // for (i = 0; i < ost->audio_channels_mapped; i++)
693 // if (ost->audio_channels_map[i] != -1)
694 // av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
695 // AUTO_INSERT_FILTER_INPUT("-map_channel", "pan", pan_buf.str);
696 // av_bprint_finalize(&pan_buf, NULL);
697 // }
698 
699  if (audio_volume != 256) {
700  char args[256];
701 
702  av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
703  "audio filter instead.\n");
704 
705  snprintf(args, sizeof(args), "%f", audio_volume / 256.);
706  AUTO_INSERT_FILTER_INPUT("-vol", "volume", args);
707  }
708  if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
709  return ret;
710 
711  return 0;
712 }
713 
715  AVFilterInOut *in)
716 {
717  av_freep(&ifilter->name);
718  DESCRIBE_FILTER_LINK(ifilter, in, 1);
719 
720  switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
721  case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
722  case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
723  default: av_assert0(0);
724  }
725 }
726 
728 {
729  AVFilterInOut *inputs, *outputs, *cur;
730  int ret, i, init = !fg->graph, simple = !fg->graph_desc;
731  const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
732  fg->graph_desc;
733 
735  if (!(fg->graph = avfilter_graph_alloc()))
736  return AVERROR(ENOMEM);
737 
738  if (simple) {
739  OutputStream *ost = fg->outputs[0]->ost;
740  char args[512];
741  AVDictionaryEntry *e = NULL;
742 
743  snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
744  fg->graph->scale_sws_opts = av_strdup(args);
745 
746  args[0] = 0;
747  while ((e = av_dict_get(ost->swr_opts, "", e,
749  av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
750  }
751  if (strlen(args))
752  args[strlen(args)-1] = 0;
753  av_opt_set(fg->graph, "aresample_swr_opts", args, 0);
754 
755  args[0] = '\0';
756  while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
758  av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
759  }
760  if (strlen(args))
761  args[strlen(args) - 1] = '\0';
762  fg->graph->resample_lavr_opts = av_strdup(args);
763  }
764 
765  if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
766  return ret;
767 
768  if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
769  av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
770  "exactly one input and output.\n", graph_desc);
771  return AVERROR(EINVAL);
772  }
773 
774  for (cur = inputs; !simple && init && cur; cur = cur->next)
775  init_input_filter(fg, cur);
776 
777  for (cur = inputs, i = 0; cur; cur = cur->next, i++)
778  if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
779  return ret;
780  avfilter_inout_free(&inputs);
781 
782  if (!init || simple) {
783  /* we already know the mappings between lavfi outputs and output streams,
784  * so we can finish the setup */
785  for (cur = outputs, i = 0; cur; cur = cur->next, i++)
786  configure_output_filter(fg, fg->outputs[i], cur);
787  avfilter_inout_free(&outputs);
788 
789  if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
790  return ret;
791  } else {
792  /* wait until output mappings are processed */
793  for (cur = outputs; cur;) {
794  GROW_ARRAY(fg->outputs, fg->nb_outputs);
795  if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]))))
796  exit(1);
797  fg->outputs[fg->nb_outputs - 1]->graph = fg;
798  fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
799  cur = cur->next;
800  fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
801  }
802  }
803 
804  fg->reconfiguration = 1;
805  return 0;
806 }
807 
809 {
810  int i;
811  for (i = 0; i < fg->nb_inputs; i++)
812  if (fg->inputs[i]->ist == ist)
813  return 1;
814  return 0;
815 }
816