FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/buffersink.h"
25 
27 
28 #include "libavutil/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/bprint.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"
37 
39 {
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;
46  if (st->codec->codec_id == AV_CODEC_ID_MJPEG) {
48  } else if (st->codec->codec_id == AV_CODEC_ID_LJPEG) {
51  }
52  }
53  for (; *p != AV_PIX_FMT_NONE; p++) {
54  best= avcodec_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
55  if (*p == target)
56  break;
57  }
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),
63  codec->name,
64  av_get_pix_fmt_name(best));
65  return best;
66  }
67  }
68  return target;
69 }
70 
72 {
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)
77  break;
78  }
79  if (*p == -1) {
81  av_log(NULL, AV_LOG_ERROR, "Conversion will not be lossless.\n");
83  av_log(NULL, AV_LOG_WARNING,
84  "Incompatible sample format '%s' for codec '%s', auto-selecting format '%s'\n",
86  codec->name,
88  st->codec->sample_fmt = codec->sample_fmts[0];
89  }
90  }
91 }
92 
93 static char *choose_pix_fmts(OutputStream *ost)
94 {
95  if (ost->keep_pix_fmt) {
96  if (ost->filter)
99  if (ost->st->codec->pix_fmt == AV_PIX_FMT_NONE)
100  return NULL;
101  return av_strdup(av_get_pix_fmt_name(ost->st->codec->pix_fmt));
102  }
103  if (ost->st->codec->pix_fmt != AV_PIX_FMT_NONE) {
104  return av_strdup(av_get_pix_fmt_name(choose_pixel_fmt(ost->st, ost->enc, ost->st->codec->pix_fmt)));
105  } else if (ost->enc && ost->enc->pix_fmts) {
106  const enum AVPixelFormat *p;
107  AVIOContext *s = NULL;
108  uint8_t *ret;
109  int len;
110 
111  if (avio_open_dyn_buf(&s) < 0)
112  exit_program(1);
113 
114  p = ost->enc->pix_fmts;
116  if (ost->st->codec->codec_id == AV_CODEC_ID_MJPEG) {
118  } else if (ost->st->codec->codec_id == AV_CODEC_ID_LJPEG) {
121  }
122  }
123 
124  for (; *p != AV_PIX_FMT_NONE; p++) {
125  const char *name = av_get_pix_fmt_name(*p);
126  avio_printf(s, "%s|", name);
127  }
128  len = avio_close_dyn_buf(s, &ret);
129  ret[len - 1] = 0;
130  return ret;
131  } else
132  return NULL;
133 }
134 
135 /* Define a function for building a string containing a list of
136  * allowed formats. */
137 #define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name) \
138 static char *choose_ ## var ## s(OutputStream *ost) \
139 { \
140  if (ost->st->codec->var != none) { \
141  get_name(ost->st->codec->var); \
142  return av_strdup(name); \
143  } else if (ost->enc && ost->enc->supported_list) { \
144  const type *p; \
145  AVIOContext *s = NULL; \
146  uint8_t *ret; \
147  int len; \
148  \
149  if (avio_open_dyn_buf(&s) < 0) \
150  exit_program(1); \
151  \
152  for (p = ost->enc->supported_list; *p != none; p++) { \
153  get_name(*p); \
154  avio_printf(s, "%s|", name); \
155  } \
156  len = avio_close_dyn_buf(s, &ret); \
157  ret[len - 1] = 0; \
158  return ret; \
159  } else \
160  return NULL; \
161 }
162 
163 // DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE,
164 // GET_PIX_FMT_NAME)
165 
168 
171 
172 DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0,
174 
176 {
177  FilterGraph *fg = av_mallocz(sizeof(*fg));
178 
179  if (!fg)
180  exit_program(1);
181  fg->index = nb_filtergraphs;
182 
183  GROW_ARRAY(fg->outputs, fg->nb_outputs);
184  if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
185  exit_program(1);
186  fg->outputs[0]->ost = ost;
187  fg->outputs[0]->graph = fg;
188 
189  ost->filter = fg->outputs[0];
190 
191  GROW_ARRAY(fg->inputs, fg->nb_inputs);
192  if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
193  exit_program(1);
194  fg->inputs[0]->ist = ist;
195  fg->inputs[0]->graph = fg;
196 
197  GROW_ARRAY(ist->filters, ist->nb_filters);
198  ist->filters[ist->nb_filters - 1] = fg->inputs[0];
199 
201  filtergraphs[nb_filtergraphs - 1] = fg;
202 
203  return fg;
204 }
205 
207 {
208  InputStream *ist = NULL;
210  int i;
211 
212  // TODO: support other filter types
213  if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
214  av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
215  "currently.\n");
216  exit_program(1);
217  }
218 
219  if (in->name) {
221  AVStream *st = NULL;
222  char *p;
223  int file_idx = strtol(in->name, &p, 0);
224 
225  if (file_idx < 0 || file_idx >= nb_input_files) {
226  av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
227  file_idx, fg->graph_desc);
228  exit_program(1);
229  }
230  s = input_files[file_idx]->ctx;
231 
232  for (i = 0; i < s->nb_streams; i++) {
233  enum AVMediaType stream_type = s->streams[i]->codec->codec_type;
234  if (stream_type != type &&
235  !(stream_type == AVMEDIA_TYPE_SUBTITLE &&
236  type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
237  continue;
238  if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
239  st = s->streams[i];
240  break;
241  }
242  }
243  if (!st) {
244  av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
245  "matches no streams.\n", p, fg->graph_desc);
246  exit_program(1);
247  }
248  ist = input_streams[input_files[file_idx]->ist_index + st->index];
249  } else {
250  /* find the first unused stream of corresponding type */
251  for (i = 0; i < nb_input_streams; i++) {
252  ist = input_streams[i];
253  if (ist->st->codec->codec_type == type && ist->discard)
254  break;
255  }
256  if (i == nb_input_streams) {
257  av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
258  "unlabeled input pad %d on filter %s\n", in->pad_idx,
259  in->filter_ctx->name);
260  exit_program(1);
261  }
262  }
263  av_assert0(ist);
264 
265  ist->discard = 0;
266  ist->decoding_needed++;
267  ist->st->discard = AVDISCARD_NONE;
268 
269  GROW_ARRAY(fg->inputs, fg->nb_inputs);
270  if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
271  exit_program(1);
272  fg->inputs[fg->nb_inputs - 1]->ist = ist;
273  fg->inputs[fg->nb_inputs - 1]->graph = fg;
274 
275  GROW_ARRAY(ist->filters, ist->nb_filters);
276  ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
277 }
278 
279 static int insert_trim(OutputStream *ost, AVFilterContext **last_filter, int *pad_idx)
280 {
281  OutputFile *of = output_files[ost->file_index];
282  AVFilterGraph *graph = (*last_filter)->graph;
283  AVFilterContext *ctx;
284  const AVFilter *trim;
285  const char *name = ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO ? "trim" : "atrim";
286  char filter_name[128];
287  int ret = 0;
288 
289  if (of->recording_time == INT64_MAX && !of->start_time)
290  return 0;
291 
292  // Use with duration and without output starttime is buggy with trim filters
293  if (!of->start_time)
294  return 0;
295 
296  trim = avfilter_get_by_name(name);
297  if (!trim) {
298  av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
299  "recording time.\n", name);
301  }
302 
303  snprintf(filter_name, sizeof(filter_name), "%s for output stream %d:%d",
304  name, ost->file_index, ost->index);
305  ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
306  if (!ctx)
307  return AVERROR(ENOMEM);
308 
309  if (of->recording_time != INT64_MAX) {
310  ret = av_opt_set_double(ctx, "duration", (double)of->recording_time / 1e6,
312  }
313  if (ret >= 0 && of->start_time) {
314  ret = av_opt_set_double(ctx, "start", (double)of->start_time / 1e6,
316  }
317  if (ret < 0) {
318  av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
319  return ret;
320  }
321 
322  ret = avfilter_init_str(ctx, NULL);
323  if (ret < 0)
324  return ret;
325 
326  ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
327  if (ret < 0)
328  return ret;
329 
330  *last_filter = ctx;
331  *pad_idx = 0;
332  return 0;
333 }
334 
336 {
337  char *pix_fmts;
338  OutputStream *ost = ofilter->ost;
339  AVCodecContext *codec = ost->st->codec;
340  AVFilterContext *last_filter = out->filter_ctx;
341  int pad_idx = out->pad_idx;
342  int ret;
343  char name[255];
344 
345  snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
346  ret = avfilter_graph_create_filter(&ofilter->filter,
347  avfilter_get_by_name("buffersink"),
348  name, NULL, NULL, fg->graph);
349 
350  if (ret < 0)
351  return ret;
352 
353  if (codec->width || codec->height) {
354  char args[255];
356 
357  snprintf(args, sizeof(args), "%d:%d:0x%X",
358  codec->width,
359  codec->height,
360  (unsigned)ost->sws_flags);
361  snprintf(name, sizeof(name), "scaler for output stream %d:%d",
362  ost->file_index, ost->index);
363  if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
364  name, args, NULL, fg->graph)) < 0)
365  return ret;
366  if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
367  return ret;
368 
369  last_filter = filter;
370  pad_idx = 0;
371  }
372 
373  if ((pix_fmts = choose_pix_fmts(ost))) {
375  snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
376  ost->file_index, ost->index);
377  ret = avfilter_graph_create_filter(&filter,
378  avfilter_get_by_name("format"),
379  "format", pix_fmts, NULL,
380  fg->graph);
381  av_freep(&pix_fmts);
382  if (ret < 0)
383  return ret;
384  if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
385  return ret;
386 
387  last_filter = filter;
388  pad_idx = 0;
389  }
390 
391  if (ost->frame_rate.num && 0) {
392  AVFilterContext *fps;
393  char args[255];
394 
395  snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
396  ost->frame_rate.den);
397  snprintf(name, sizeof(name), "fps for output stream %d:%d",
398  ost->file_index, ost->index);
400  name, args, NULL, fg->graph);
401  if (ret < 0)
402  return ret;
403 
404  ret = avfilter_link(last_filter, pad_idx, fps, 0);
405  if (ret < 0)
406  return ret;
407  last_filter = fps;
408  pad_idx = 0;
409  }
410 
411  ret = insert_trim(ost, &last_filter, &pad_idx);
412  if (ret < 0)
413  return ret;
414 
415 
416  if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
417  return ret;
418 
419  return 0;
420 }
421 
423 {
424  OutputStream *ost = ofilter->ost;
425  AVCodecContext *codec = ost->st->codec;
426  AVFilterContext *last_filter = out->filter_ctx;
427  OutputFile *of = output_files[ost->file_index];
428  int pad_idx = out->pad_idx;
429  char *sample_fmts, *sample_rates, *channel_layouts;
430  char name[255];
431  int ret;
432 
433  snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
434  ret = avfilter_graph_create_filter(&ofilter->filter,
435  avfilter_get_by_name("abuffersink"),
436  name, NULL, NULL, fg->graph);
437  if (ret < 0)
438  return ret;
439  if ((ret = av_opt_set_int(ofilter->filter, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
440  return ret;
441 
442 #define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \
443  AVFilterContext *filt_ctx; \
444  \
445  av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
446  "similarly to -af " filter_name "=%s.\n", arg); \
447  \
448  ret = avfilter_graph_create_filter(&filt_ctx, \
449  avfilter_get_by_name(filter_name), \
450  filter_name, arg, NULL, fg->graph); \
451  if (ret < 0) \
452  return ret; \
453  \
454  ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0); \
455  if (ret < 0) \
456  return ret; \
457  \
458  last_filter = filt_ctx; \
459  pad_idx = 0; \
460 } while (0)
461  if (ost->audio_channels_mapped) {
462  int i;
463  AVBPrint pan_buf;
464  av_bprint_init(&pan_buf, 256, 8192);
465  av_bprintf(&pan_buf, "0x%"PRIx64,
467  for (i = 0; i < ost->audio_channels_mapped; i++)
468  if (ost->audio_channels_map[i] != -1)
469  av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
470 
471  AUTO_INSERT_FILTER("-map_channel", "pan", pan_buf.str);
472  av_bprint_finalize(&pan_buf, NULL);
473  }
474 
475  if (codec->channels && !codec->channel_layout)
477 
478  sample_fmts = choose_sample_fmts(ost);
479  sample_rates = choose_sample_rates(ost);
480  channel_layouts = choose_channel_layouts(ost);
481  if (sample_fmts || sample_rates || channel_layouts) {
482  AVFilterContext *format;
483  char args[256];
484  args[0] = 0;
485 
486  if (sample_fmts)
487  av_strlcatf(args, sizeof(args), "sample_fmts=%s:",
488  sample_fmts);
489  if (sample_rates)
490  av_strlcatf(args, sizeof(args), "sample_rates=%s:",
491  sample_rates);
492  if (channel_layouts)
493  av_strlcatf(args, sizeof(args), "channel_layouts=%s:",
494  channel_layouts);
495 
496  av_freep(&sample_fmts);
497  av_freep(&sample_rates);
498  av_freep(&channel_layouts);
499 
500  snprintf(name, sizeof(name), "audio format for output stream %d:%d",
501  ost->file_index, ost->index);
502  ret = avfilter_graph_create_filter(&format,
503  avfilter_get_by_name("aformat"),
504  name, args, NULL, fg->graph);
505  if (ret < 0)
506  return ret;
507 
508  ret = avfilter_link(last_filter, pad_idx, format, 0);
509  if (ret < 0)
510  return ret;
511 
512  last_filter = format;
513  pad_idx = 0;
514  }
515 
516  if (audio_volume != 256 && 0) {
517  char args[256];
518 
519  snprintf(args, sizeof(args), "%f", audio_volume / 256.);
520  AUTO_INSERT_FILTER("-vol", "volume", args);
521  }
522 
523  if (ost->apad && of->shortest) {
524  char args[256];
525  int i;
526 
527  for (i=0; i<of->ctx->nb_streams; i++)
528  if (of->ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
529  break;
530 
531  if (i<of->ctx->nb_streams) {
532  snprintf(args, sizeof(args), "%s", ost->apad);
533  AUTO_INSERT_FILTER("-apad", "apad", args);
534  }
535  }
536 
537  ret = insert_trim(ost, &last_filter, &pad_idx);
538  if (ret < 0)
539  return ret;
540 
541  if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
542  return ret;
543 
544  return 0;
545 }
546 
547 #define DESCRIBE_FILTER_LINK(f, inout, in) \
548 { \
549  AVFilterContext *ctx = inout->filter_ctx; \
550  AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads; \
551  int nb_pads = in ? ctx->input_count : ctx->output_count; \
552  AVIOContext *pb; \
553  \
554  if (avio_open_dyn_buf(&pb) < 0) \
555  exit_program(1); \
556  \
557  avio_printf(pb, "%s", ctx->filter->name); \
558  if (nb_pads > 1) \
559  avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
560  avio_w8(pb, 0); \
561  avio_close_dyn_buf(pb, &f->name); \
562 }
563 
565 {
566  av_freep(&ofilter->name);
567  DESCRIBE_FILTER_LINK(ofilter, out, 0);
568 
569  switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
570  case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
571  case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
572  default: av_assert0(0);
573  }
574 }
575 
577 {
579  int i, w, h;
580 
581  /* Compute the size of the canvas for the subtitles stream.
582  If the subtitles codec has set a size, use it. Otherwise use the
583  maximum dimensions of the video streams in the same file. */
584  w = ist->st->codec->width;
585  h = ist->st->codec->height;
586  if (!(w && h)) {
587  for (i = 0; i < avf->nb_streams; i++) {
588  if (avf->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
589  w = FFMAX(w, avf->streams[i]->codec->width);
590  h = FFMAX(h, avf->streams[i]->codec->height);
591  }
592  }
593  if (!(w && h)) {
594  w = FFMAX(w, 720);
595  h = FFMAX(h, 576);
596  }
597  av_log(avf, AV_LOG_INFO, "sub2video: using %dx%d canvas\n", w, h);
598  }
599  ist->sub2video.w = ist->st->codec->width = ist->resample_width = w;
600  ist->sub2video.h = ist->st->codec->height = ist->resample_height = h;
601 
602  /* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the
603  palettes for all rectangles are identical or compatible */
605 
606  ist->sub2video.frame = av_frame_alloc();
607  if (!ist->sub2video.frame)
608  return AVERROR(ENOMEM);
609  return 0;
610 }
611 
613  AVFilterInOut *in)
614 {
616  AVFilter *filter = avfilter_get_by_name("buffer");
617  InputStream *ist = ifilter->ist;
618  AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
619  ist->st->time_base;
620  AVRational fr = ist->framerate;
621  AVRational sar;
622  AVBPrint args;
623  char name[255];
624  int pad_idx = in->pad_idx;
625  int ret;
626 
627  if (!fr.num)
628  fr = av_guess_frame_rate(input_files[ist->file_index]->ctx, ist->st, NULL);
629 
630  if (ist->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
631  ret = sub2video_prepare(ist);
632  if (ret < 0)
633  return ret;
634  }
635 
636  sar = ist->st->sample_aspect_ratio.num ?
637  ist->st->sample_aspect_ratio :
639  if(!sar.den)
640  sar = (AVRational){0,1};
641  av_bprint_init(&args, 0, 1);
642  av_bprintf(&args,
643  "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
644  "pixel_aspect=%d/%d:sws_param=flags=%d", ist->resample_width,
646  tb.num, tb.den, sar.num, sar.den,
648  if (fr.num && fr.den)
649  av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
650  snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
651  ist->file_index, ist->st->index);
652 
653  if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter, name,
654  args.str, NULL, fg->graph)) < 0)
655  return ret;
656 
657  if (ist->framerate.num) {
658  AVFilterContext *setpts;
659 
660  snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
661  ist->file_index, ist->st->index);
662  if ((ret = avfilter_graph_create_filter(&setpts,
663  avfilter_get_by_name("setpts"),
664  name, "N", NULL,
665  fg->graph)) < 0)
666  return ret;
667 
668  if ((ret = avfilter_link(setpts, 0, first_filter, pad_idx)) < 0)
669  return ret;
670 
671  first_filter = setpts;
672  pad_idx = 0;
673  }
674 
675  if (do_deinterlace) {
676  AVFilterContext *yadif;
677 
678  snprintf(name, sizeof(name), "deinterlace input from stream %d:%d",
679  ist->file_index, ist->st->index);
680  if ((ret = avfilter_graph_create_filter(&yadif,
681  avfilter_get_by_name("yadif"),
682  name, "", NULL,
683  fg->graph)) < 0)
684  return ret;
685 
686  if ((ret = avfilter_link(yadif, 0, first_filter, pad_idx)) < 0)
687  return ret;
688 
689  first_filter = yadif;
690  pad_idx = 0;
691  }
692 
693  if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
694  return ret;
695  return 0;
696 }
697 
699  AVFilterInOut *in)
700 {
702  AVFilter *filter = avfilter_get_by_name("abuffer");
703  InputStream *ist = ifilter->ist;
704  int pad_idx = in->pad_idx;
705  AVBPrint args;
706  char name[255];
707  int ret;
708 
710  av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
711  1, ist->st->codec->sample_rate,
712  ist->st->codec->sample_rate,
714  if (ist->st->codec->channel_layout)
715  av_bprintf(&args, ":channel_layout=0x%"PRIx64,
716  ist->st->codec->channel_layout);
717  else
718  av_bprintf(&args, ":channels=%d", ist->st->codec->channels);
719  snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
720  ist->file_index, ist->st->index);
721 
722  if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter,
723  name, args.str, NULL,
724  fg->graph)) < 0)
725  return ret;
726 
727 #define AUTO_INSERT_FILTER_INPUT(opt_name, filter_name, arg) do { \
728  AVFilterContext *filt_ctx; \
729  \
730  av_log(NULL, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
731  "similarly to -af " filter_name "=%s.\n", arg); \
732  \
733  snprintf(name, sizeof(name), "graph %d %s for input stream %d:%d", \
734  fg->index, filter_name, ist->file_index, ist->st->index); \
735  ret = avfilter_graph_create_filter(&filt_ctx, \
736  avfilter_get_by_name(filter_name), \
737  name, arg, NULL, fg->graph); \
738  if (ret < 0) \
739  return ret; \
740  \
741  ret = avfilter_link(filt_ctx, 0, first_filter, pad_idx); \
742  if (ret < 0) \
743  return ret; \
744  \
745  first_filter = filt_ctx; \
746 } while (0)
747 
748  if (audio_sync_method > 0) {
749  char args[256] = {0};
750 
751  av_strlcatf(args, sizeof(args), "async=%d", audio_sync_method);
752  if (audio_drift_threshold != 0.1)
753  av_strlcatf(args, sizeof(args), ":min_hard_comp=%f", audio_drift_threshold);
754  if (!fg->reconfiguration)
755  av_strlcatf(args, sizeof(args), ":first_pts=0");
756  AUTO_INSERT_FILTER_INPUT("-async", "aresample", args);
757  }
758 
759 // if (ost->audio_channels_mapped) {
760 // int i;
761 // AVBPrint pan_buf;
762 // av_bprint_init(&pan_buf, 256, 8192);
763 // av_bprintf(&pan_buf, "0x%"PRIx64,
764 // av_get_default_channel_layout(ost->audio_channels_mapped));
765 // for (i = 0; i < ost->audio_channels_mapped; i++)
766 // if (ost->audio_channels_map[i] != -1)
767 // av_bprintf(&pan_buf, ":c%d=c%d", i, ost->audio_channels_map[i]);
768 // AUTO_INSERT_FILTER_INPUT("-map_channel", "pan", pan_buf.str);
769 // av_bprint_finalize(&pan_buf, NULL);
770 // }
771 
772  if (audio_volume != 256) {
773  char args[256];
774 
775  av_log(NULL, AV_LOG_WARNING, "-vol has been deprecated. Use the volume "
776  "audio filter instead.\n");
777 
778  snprintf(args, sizeof(args), "%f", audio_volume / 256.);
779  AUTO_INSERT_FILTER_INPUT("-vol", "volume", args);
780  }
781  if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
782  return ret;
783 
784  return 0;
785 }
786 
788  AVFilterInOut *in)
789 {
790  av_freep(&ifilter->name);
791  DESCRIBE_FILTER_LINK(ifilter, in, 1);
792 
793  switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
794  case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
795  case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
796  default: av_assert0(0);
797  }
798 }
799 
801 {
802  AVFilterInOut *inputs, *outputs, *cur;
803  int ret, i, init = !fg->graph, simple = !fg->graph_desc;
804  const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
805  fg->graph_desc;
806 
808  if (!(fg->graph = avfilter_graph_alloc()))
809  return AVERROR(ENOMEM);
810 
811  if (simple) {
812  OutputStream *ost = fg->outputs[0]->ost;
813  char args[512];
814  AVDictionaryEntry *e = NULL;
815 
816  snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
817  fg->graph->scale_sws_opts = av_strdup(args);
818 
819  args[0] = 0;
820  while ((e = av_dict_get(ost->swr_opts, "", e,
822  av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
823  }
824  if (strlen(args))
825  args[strlen(args)-1] = 0;
826  av_opt_set(fg->graph, "aresample_swr_opts", args, 0);
827 
828  args[0] = '\0';
829  while ((e = av_dict_get(fg->outputs[0]->ost->resample_opts, "", e,
831  av_strlcatf(args, sizeof(args), "%s=%s:", e->key, e->value);
832  }
833  if (strlen(args))
834  args[strlen(args) - 1] = '\0';
835  fg->graph->resample_lavr_opts = av_strdup(args);
836  }
837 
838  if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
839  return ret;
840 
841  if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
842  av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
843  "exactly one input and output.\n", graph_desc);
844  return AVERROR(EINVAL);
845  }
846 
847  for (cur = inputs; !simple && init && cur; cur = cur->next)
848  init_input_filter(fg, cur);
849 
850  for (cur = inputs, i = 0; cur; cur = cur->next, i++)
851  if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
852  return ret;
853  avfilter_inout_free(&inputs);
854 
855  if (!init || simple) {
856  /* we already know the mappings between lavfi outputs and output streams,
857  * so we can finish the setup */
858  for (cur = outputs, i = 0; cur; cur = cur->next, i++)
859  configure_output_filter(fg, fg->outputs[i], cur);
860  avfilter_inout_free(&outputs);
861 
862  if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
863  return ret;
864  } else {
865  /* wait until output mappings are processed */
866  for (cur = outputs; cur;) {
867  GROW_ARRAY(fg->outputs, fg->nb_outputs);
868  if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]))))
869  exit_program(1);
870  fg->outputs[fg->nb_outputs - 1]->graph = fg;
871  fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
872  cur = cur->next;
873  fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
874  }
875  }
876 
877  fg->reconfiguration = 1;
878  return 0;
879 }
880 
882 {
883  int i;
884  for (i = 0; i < fg->nb_inputs; i++)
885  if (fg->inputs[i]->ist == ist)
886  return 1;
887  return 0;
888 }
889