FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
avfiltergraph.c
Go to the documentation of this file.
1 /*
2  * filter graphs
3  * Copyright (c) 2008 Vitor Sessak
4  * Copyright (c) 2007 Bobby Bingham
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include <ctype.h>
24 #include <string.h>
25 
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavcodec/avcodec.h" // avcodec_find_best_pix_fmt_of_2()
32 #include "avfilter.h"
33 #include "avfiltergraph.h"
34 #include "formats.h"
35 #include "internal.h"
36 
37 #define OFFSET(x) offsetof(AVFilterGraph,x)
38 
39 static const AVOption options[]={
40 {"scale_sws_opts" , "default scale filter options" , OFFSET(scale_sws_opts) , AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, 0 },
41 {"aresample_swr_opts" , "default aresample filter options" , OFFSET(aresample_swr_opts) , AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, 0 },
42 {0}
43 };
44 
45 
46 static const AVClass filtergraph_class = {
47  .class_name = "AVFilterGraph",
48  .item_name = av_default_item_name,
49  .option = options,
50  .version = LIBAVUTIL_VERSION_INT,
51  .category = AV_CLASS_CATEGORY_FILTER,
52 };
53 
55 {
56  AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
57  if (!ret)
58  return NULL;
60  return ret;
61 }
62 
64 {
65  if (!*graph)
66  return;
67  for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
68  avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
69  av_freep(&(*graph)->sink_links);
70  av_freep(&(*graph)->scale_sws_opts);
71  av_freep(&(*graph)->aresample_swr_opts);
72  av_freep(&(*graph)->filters);
73  av_freep(graph);
74 }
75 
77 {
79  sizeof(AVFilterContext*) * (graph->filter_count+1));
80  if (!filters)
81  return AVERROR(ENOMEM);
82 
83  graph->filters = filters;
84  graph->filters[graph->filter_count++] = filter;
85 
86  return 0;
87 }
88 
90  const char *name, const char *args, void *opaque,
91  AVFilterGraph *graph_ctx)
92 {
93  int ret;
94 
95  if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
96  goto fail;
97  if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
98  goto fail;
99  if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
100  goto fail;
101  return 0;
102 
103 fail:
104  if (*filt_ctx)
105  avfilter_free(*filt_ctx);
106  *filt_ctx = NULL;
107  return ret;
108 }
109 
111 {
112  graph->disable_auto_convert = flags;
113 }
114 
115 /**
116  * Check for the validity of graph.
117  *
118  * A graph is considered valid if all its input and output pads are
119  * connected.
120  *
121  * @return 0 in case of success, a negative value otherwise
122  */
123 static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
124 {
126  int i, j;
127 
128  for (i = 0; i < graph->filter_count; i++) {
129  const AVFilterPad *pad;
130  filt = graph->filters[i];
131 
132  for (j = 0; j < filt->nb_inputs; j++) {
133  if (!filt->inputs[j] || !filt->inputs[j]->src) {
134  pad = &filt->input_pads[j];
135  av_log(log_ctx, AV_LOG_ERROR,
136  "Input pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any source\n",
137  pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
138  return AVERROR(EINVAL);
139  }
140  }
141 
142  for (j = 0; j < filt->nb_outputs; j++) {
143  if (!filt->outputs[j] || !filt->outputs[j]->dst) {
144  pad = &filt->output_pads[j];
145  av_log(log_ctx, AV_LOG_ERROR,
146  "Output pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any destination\n",
147  pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
148  return AVERROR(EINVAL);
149  }
150  }
151  }
152 
153  return 0;
154 }
155 
156 /**
157  * Configure all the links of graphctx.
158  *
159  * @return 0 in case of success, a negative value otherwise
160  */
161 static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
162 {
164  int i, ret;
165 
166  for (i=0; i < graph->filter_count; i++) {
167  filt = graph->filters[i];
168 
169  if (!filt->nb_outputs) {
170  if ((ret = avfilter_config_links(filt)))
171  return ret;
172  }
173  }
174 
175  return 0;
176 }
177 
179 {
180  int i;
181 
182  for (i = 0; i < graph->filter_count; i++)
183  if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
184  return graph->filters[i];
185 
186  return NULL;
187 }
188 
190 {
191  int ret;
193  AVFilterChannelLayouts *chlayouts;
194  AVFilterFormats *samplerates;
195  enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
196  ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
198 
199  if ((ret = ctx->filter->query_formats(ctx)) < 0) {
200  av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
201  ctx->name, av_err2str(ret));
202  return ret;
203  }
204 
205  formats = ff_all_formats(type);
206  if (!formats)
207  return AVERROR(ENOMEM);
208  ff_set_common_formats(ctx, formats);
209  if (type == AVMEDIA_TYPE_AUDIO) {
210  samplerates = ff_all_samplerates();
211  if (!samplerates)
212  return AVERROR(ENOMEM);
213  ff_set_common_samplerates(ctx, samplerates);
214  chlayouts = ff_all_channel_layouts();
215  if (!chlayouts)
216  return AVERROR(ENOMEM);
217  ff_set_common_channel_layouts(ctx, chlayouts);
218  }
219  return 0;
220 }
221 
223  const char *filt_name, const char *filt_args)
224 {
225  static int auto_count = 0, ret;
226  char inst_name[32];
227  AVFilterContext *filt_ctx;
228 
229  if (graph->disable_auto_convert) {
231  "The filters '%s' and '%s' do not have a common format "
232  "and automatic conversion is disabled.\n",
233  link->src->name, link->dst->name);
234  return AVERROR(EINVAL);
235  }
236 
237  snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
238  filt_name, auto_count++);
239 
240  if ((ret = avfilter_graph_create_filter(&filt_ctx,
241  avfilter_get_by_name(filt_name),
242  inst_name, filt_args, NULL, graph)) < 0)
243  return ret;
244  if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
245  return ret;
246 
247  filter_query_formats(filt_ctx);
248 
249  if ( ((link = filt_ctx-> inputs[0]) &&
250  !ff_merge_formats(link->in_formats, link->out_formats)) ||
251  ((link = filt_ctx->outputs[0]) &&
252  !ff_merge_formats(link->in_formats, link->out_formats))
253  ) {
255  "Impossible to convert between the formats supported by the filter "
256  "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
257  return AVERROR(EINVAL);
258  }
259 
260  if (link->type == AVMEDIA_TYPE_AUDIO &&
261  (((link = filt_ctx-> inputs[0]) &&
263  ((link = filt_ctx->outputs[0]) &&
265  ) {
267  "Impossible to convert between the channel layouts formats supported by the filter "
268  "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
269  return AVERROR(EINVAL);
270  }
271 
272  return 0;
273 }
274 
275 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
276 {
277  int i, j, ret;
278 #if 0
279  char filt_args[128];
281  AVFilterChannelLayouts *chlayouts;
282  AVFilterFormats *samplerates;
283 #endif
284  int scaler_count = 0, resampler_count = 0;
285 
286  for (j = 0; j < 2; j++) {
287  /* ask all the sub-filters for their supported media formats */
288  for (i = 0; i < graph->filter_count; i++) {
289  /* Call query_formats on sources first.
290  This is a temporary workaround for amerge,
291  until format renegociation is implemented. */
292  if (!graph->filters[i]->nb_inputs == j)
293  continue;
294  if (graph->filters[i]->filter->query_formats)
295  ret = filter_query_formats(graph->filters[i]);
296  else
297  ret = ff_default_query_formats(graph->filters[i]);
298  if (ret < 0)
299  return ret;
300  }
301  }
302 
303  /* go through and merge as many format lists as possible */
304  for (i = 0; i < graph->filter_count; i++) {
305  AVFilterContext *filter = graph->filters[i];
306 
307  for (j = 0; j < filter->nb_inputs; j++) {
308  AVFilterLink *link = filter->inputs[j];
309 #if 0
310  if (!link) continue;
311 
312  if (!link->in_formats || !link->out_formats)
313  return AVERROR(EINVAL);
314 
315  if (link->type == AVMEDIA_TYPE_VIDEO &&
316  !ff_merge_formats(link->in_formats, link->out_formats)) {
317 
318  /* couldn't merge format lists, auto-insert scale filter */
319  snprintf(filt_args, sizeof(filt_args), "0:0:%s",
320  graph->scale_sws_opts);
321  if (ret = insert_conv_filter(graph, link, "scale", filt_args))
322  return ret;
323  }
324  else if (link->type == AVMEDIA_TYPE_AUDIO) {
325  if (!link->in_channel_layouts || !link->out_channel_layouts)
326  return AVERROR(EINVAL);
327 
328  /* Merge all three list before checking: that way, in all
329  * three categories, aconvert will use a common format
330  * whenever possible. */
331  formats = ff_merge_formats(link->in_formats, link->out_formats);
333  samplerates = ff_merge_samplerates (link->in_samplerates, link->out_samplerates);
334 
335  if (!formats || !chlayouts || !samplerates)
336  if (ret = insert_conv_filter(graph, link, "aresample", NULL))
337  return ret;
338 #else
339  int convert_needed = 0;
340 
341  if (!link)
342  continue;
343 
344  if (link->in_formats != link->out_formats &&
346  link->out_formats))
347  convert_needed = 1;
348  if (link->type == AVMEDIA_TYPE_AUDIO) {
349  if (link->in_channel_layouts != link->out_channel_layouts &&
351  link->out_channel_layouts))
352  convert_needed = 1;
353  if (link->in_samplerates != link->out_samplerates &&
355  link->out_samplerates))
356  convert_needed = 1;
357  }
358 
359  if (convert_needed) {
361  AVFilter *filter;
362  AVFilterLink *inlink, *outlink;
363  char scale_args[256];
364  char inst_name[30];
365 
366  /* couldn't merge format lists. auto-insert conversion filter */
367  switch (link->type) {
368  case AVMEDIA_TYPE_VIDEO:
369  if (!(filter = avfilter_get_by_name("scale"))) {
370  av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
371  "not present, cannot convert pixel formats.\n");
372  return AVERROR(EINVAL);
373  }
374 
375  snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
376  scaler_count++);
377  av_strlcpy(scale_args, "0:0", sizeof(scale_args));
378  if (graph->scale_sws_opts) {
379  av_strlcat(scale_args, ":", sizeof(scale_args));
380  av_strlcat(scale_args, graph->scale_sws_opts, sizeof(scale_args));
381  }
382  if ((ret = avfilter_graph_create_filter(&convert, filter,
383  inst_name, scale_args, NULL,
384  graph)) < 0)
385  return ret;
386  break;
387  case AVMEDIA_TYPE_AUDIO:
388  if (!(filter = avfilter_get_by_name("aresample"))) {
389  av_log(log_ctx, AV_LOG_ERROR, "'aresample' filter "
390  "not present, cannot convert audio formats.\n");
391  return AVERROR(EINVAL);
392  }
393 
394  snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
395  resampler_count++);
396  if ((ret = avfilter_graph_create_filter(&convert, filter,
397  inst_name, graph->aresample_swr_opts, NULL, graph)) < 0)
398  return ret;
399  break;
400  default:
401  return AVERROR(EINVAL);
402  }
403 
404  if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
405  return ret;
406 
407  filter_query_formats(convert);
408  inlink = convert->inputs[0];
409  outlink = convert->outputs[0];
410  if (!ff_merge_formats( inlink->in_formats, inlink->out_formats) ||
411  !ff_merge_formats(outlink->in_formats, outlink->out_formats))
412  ret |= AVERROR(ENOSYS);
413  if (inlink->type == AVMEDIA_TYPE_AUDIO &&
415  inlink->out_samplerates) ||
417  inlink->out_channel_layouts)))
418  ret |= AVERROR(ENOSYS);
419  if (outlink->type == AVMEDIA_TYPE_AUDIO &&
421  outlink->out_samplerates) ||
423  outlink->out_channel_layouts)))
424  ret |= AVERROR(ENOSYS);
425 
426  if (ret < 0) {
427  av_log(log_ctx, AV_LOG_ERROR,
428  "Impossible to convert between the formats supported by the filter "
429  "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
430  return ret;
431  }
432 #endif
433  }
434  }
435  }
436 
437  return 0;
438 }
439 
440 static int pick_format(AVFilterLink *link, AVFilterLink *ref)
441 {
442  if (!link || !link->in_formats)
443  return 0;
444 
445  if (link->type == AVMEDIA_TYPE_VIDEO) {
446  if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
447  int has_alpha= av_pix_fmt_desc_get(ref->format)->nb_components % 2 == 0;
448  enum AVPixelFormat best= AV_PIX_FMT_NONE;
449  int i;
450  for (i=0; i<link->in_formats->format_count; i++) {
451  enum AVPixelFormat p = link->in_formats->formats[i];
452  best= avcodec_find_best_pix_fmt_of_2(best, p, ref->format, has_alpha, NULL);
453  }
454  av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
456  av_get_pix_fmt_name(ref->format), has_alpha);
457  link->in_formats->formats[0] = best;
458  }
459  }
460 
461  link->in_formats->format_count = 1;
462  link->format = link->in_formats->formats[0];
463 
464  if (link->type == AVMEDIA_TYPE_AUDIO) {
465  if (!link->in_samplerates->format_count) {
466  av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
467  " the link between filters %s and %s.\n", link->src->name,
468  link->dst->name);
469  return AVERROR(EINVAL);
470  }
471  link->in_samplerates->format_count = 1;
472  link->sample_rate = link->in_samplerates->formats[0];
473 
475  av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
476  "the link between filters %s and %s.\n", link->src->name,
477  link->dst->name);
478  return AVERROR(EINVAL);
479  }
483  }
484 
491 
492  return 0;
493 }
494 
495 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
496 do { \
497  for (i = 0; i < filter->nb_inputs; i++) { \
498  AVFilterLink *link = filter->inputs[i]; \
499  fmt_type fmt; \
500  \
501  if (!link->out_ ## list || link->out_ ## list->nb != 1) \
502  continue; \
503  fmt = link->out_ ## list->var[0]; \
504  \
505  for (j = 0; j < filter->nb_outputs; j++) { \
506  AVFilterLink *out_link = filter->outputs[j]; \
507  list_type *fmts; \
508  \
509  if (link->type != out_link->type || \
510  out_link->in_ ## list->nb == 1) \
511  continue; \
512  fmts = out_link->in_ ## list; \
513  \
514  if (!out_link->in_ ## list->nb) { \
515  add_format(&out_link->in_ ##list, fmt); \
516  break; \
517  } \
518  \
519  for (k = 0; k < out_link->in_ ## list->nb; k++) \
520  if (fmts->var[k] == fmt) { \
521  fmts->var[0] = fmt; \
522  fmts->nb = 1; \
523  ret = 1; \
524  break; \
525  } \
526  } \
527  } \
528 } while (0)
529 
531 {
532  int i, j, k, ret = 0;
533 
534  REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
535  format_count, ff_add_format);
536  REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
537  format_count, ff_add_format);
538  REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
539  channel_layouts, nb_channel_layouts, ff_add_channel_layout);
540 
541  return ret;
542 }
543 
544 static void reduce_formats(AVFilterGraph *graph)
545 {
546  int i, reduced;
547 
548  do {
549  reduced = 0;
550 
551  for (i = 0; i < graph->filter_count; i++)
552  reduced |= reduce_formats_on_filter(graph->filters[i]);
553  } while (reduced);
554 }
555 
557 {
558  AVFilterLink *link = NULL;
559  int sample_rate;
560  int i, j;
561 
562  for (i = 0; i < filter->nb_inputs; i++) {
563  link = filter->inputs[i];
564 
565  if (link->type == AVMEDIA_TYPE_AUDIO &&
566  link->out_samplerates->format_count == 1)
567  break;
568  }
569  if (i == filter->nb_inputs)
570  return;
571 
572  sample_rate = link->out_samplerates->formats[0];
573 
574  for (i = 0; i < filter->nb_outputs; i++) {
575  AVFilterLink *outlink = filter->outputs[i];
576  int best_idx, best_diff = INT_MAX;
577 
578  if (outlink->type != AVMEDIA_TYPE_AUDIO ||
579  outlink->in_samplerates->format_count < 2)
580  continue;
581 
582  for (j = 0; j < outlink->in_samplerates->format_count; j++) {
583  int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
584 
585  if (diff < best_diff) {
586  best_diff = diff;
587  best_idx = j;
588  }
589  }
590  FFSWAP(int, outlink->in_samplerates->formats[0],
591  outlink->in_samplerates->formats[best_idx]);
592  }
593 }
594 
595 static void swap_samplerates(AVFilterGraph *graph)
596 {
597  int i;
598 
599  for (i = 0; i < graph->filter_count; i++)
601 }
602 
603 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
604 #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
605 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
606 #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
607 #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
608 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
609 #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
610 
611 /* allowable substitutions for channel pairs when comparing layouts,
612  * ordered by priority for both values */
613 static const uint64_t ch_subst[][2] = {
635 };
636 
638 {
639  AVFilterLink *link = NULL;
640  int i, j, k;
641 
642  for (i = 0; i < filter->nb_inputs; i++) {
643  link = filter->inputs[i];
644 
645  if (link->type == AVMEDIA_TYPE_AUDIO &&
647  break;
648  }
649  if (i == filter->nb_inputs)
650  return;
651 
652  for (i = 0; i < filter->nb_outputs; i++) {
653  AVFilterLink *outlink = filter->outputs[i];
654  int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
655 
656  if (outlink->type != AVMEDIA_TYPE_AUDIO ||
658  continue;
659 
660  for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
661  uint64_t in_chlayout = link->out_channel_layouts->channel_layouts[0];
662  uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
663  int in_channels = av_get_channel_layout_nb_channels(in_chlayout);
664  int out_channels = av_get_channel_layout_nb_channels(out_chlayout);
665  int count_diff = out_channels - in_channels;
666  int matched_channels, extra_channels;
667  int score = 0;
668 
669  /* channel substitution */
670  for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
671  uint64_t cmp0 = ch_subst[k][0];
672  uint64_t cmp1 = ch_subst[k][1];
673  if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
674  (out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
675  in_chlayout &= ~cmp0;
676  out_chlayout &= ~cmp1;
677  /* add score for channel match, minus a deduction for
678  having to do the substitution */
679  score += 10 * av_get_channel_layout_nb_channels(cmp1) - 2;
680  }
681  }
682 
683  /* no penalty for LFE channel mismatch */
684  if ( (in_chlayout & AV_CH_LOW_FREQUENCY) &&
685  (out_chlayout & AV_CH_LOW_FREQUENCY))
686  score += 10;
687  in_chlayout &= ~AV_CH_LOW_FREQUENCY;
688  out_chlayout &= ~AV_CH_LOW_FREQUENCY;
689 
690  matched_channels = av_get_channel_layout_nb_channels(in_chlayout &
691  out_chlayout);
692  extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
693  (~in_chlayout));
694  score += 10 * matched_channels - 5 * extra_channels;
695 
696  if (score > best_score ||
697  (count_diff < best_count_diff && score == best_score)) {
698  best_score = score;
699  best_idx = j;
700  best_count_diff = count_diff;
701  }
702  }
703  av_assert0(best_idx >= 0);
704  FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
705  outlink->in_channel_layouts->channel_layouts[best_idx]);
706  }
707 
708 }
709 
711 {
712  int i;
713 
714  for (i = 0; i < graph->filter_count; i++)
716 }
717 
719 {
720  AVFilterLink *link = NULL;
721  int format, bps;
722  int i, j;
723 
724  for (i = 0; i < filter->nb_inputs; i++) {
725  link = filter->inputs[i];
726 
727  if (link->type == AVMEDIA_TYPE_AUDIO &&
728  link->out_formats->format_count == 1)
729  break;
730  }
731  if (i == filter->nb_inputs)
732  return;
733 
734  format = link->out_formats->formats[0];
735  bps = av_get_bytes_per_sample(format);
736 
737  for (i = 0; i < filter->nb_outputs; i++) {
738  AVFilterLink *outlink = filter->outputs[i];
739  int best_idx = -1, best_score = INT_MIN;
740 
741  if (outlink->type != AVMEDIA_TYPE_AUDIO ||
742  outlink->in_formats->format_count < 2)
743  continue;
744 
745  for (j = 0; j < outlink->in_formats->format_count; j++) {
746  int out_format = outlink->in_formats->formats[j];
747  int out_bps = av_get_bytes_per_sample(out_format);
748  int score;
749 
750  if (av_get_packed_sample_fmt(out_format) == format ||
751  av_get_planar_sample_fmt(out_format) == format) {
752  best_idx = j;
753  break;
754  }
755 
756  /* for s32 and float prefer double to prevent loss of information */
757  if (bps == 4 && out_bps == 8) {
758  best_idx = j;
759  break;
760  }
761 
762  /* prefer closest higher or equal bps */
763  score = -abs(out_bps - bps);
764  if (out_bps >= bps)
765  score += INT_MAX/2;
766 
767  if (score > best_score) {
768  best_score = score;
769  best_idx = j;
770  }
771  }
772  av_assert0(best_idx >= 0);
773  FFSWAP(int, outlink->in_formats->formats[0],
774  outlink->in_formats->formats[best_idx]);
775  }
776 }
777 
778 static void swap_sample_fmts(AVFilterGraph *graph)
779 {
780  int i;
781 
782  for (i = 0; i < graph->filter_count; i++)
784 
785 }
786 
787 static int pick_formats(AVFilterGraph *graph)
788 {
789  int i, j, ret;
790  int change;
791 
792  do{
793  change = 0;
794  for (i = 0; i < graph->filter_count; i++) {
795  AVFilterContext *filter = graph->filters[i];
796  if (filter->nb_inputs){
797  for (j = 0; j < filter->nb_inputs; j++){
798  if(filter->inputs[j]->in_formats && filter->inputs[j]->in_formats->format_count == 1) {
799  if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
800  return ret;
801  change = 1;
802  }
803  }
804  }
805  if (filter->nb_outputs){
806  for (j = 0; j < filter->nb_outputs; j++){
807  if(filter->outputs[j]->in_formats && filter->outputs[j]->in_formats->format_count == 1) {
808  if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
809  return ret;
810  change = 1;
811  }
812  }
813  }
814  if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
815  for (j = 0; j < filter->nb_outputs; j++) {
816  if(filter->outputs[j]->format<0) {
817  if ((ret = pick_format(filter->outputs[j], filter->inputs[0])) < 0)
818  return ret;
819  change = 1;
820  }
821  }
822  }
823  }
824  }while(change);
825 
826  for (i = 0; i < graph->filter_count; i++) {
827  AVFilterContext *filter = graph->filters[i];
828 
829  for (j = 0; j < filter->nb_inputs; j++)
830  if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
831  return ret;
832  for (j = 0; j < filter->nb_outputs; j++)
833  if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
834  return ret;
835  }
836  return 0;
837 }
838 
839 /**
840  * Configure the formats of all the links in the graph.
841  */
842 static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
843 {
844  int ret;
845 
846  /* find supported formats from sub-filters, and merge along links */
847  if ((ret = query_formats(graph, log_ctx)) < 0)
848  return ret;
849 
850  /* Once everything is merged, it's possible that we'll still have
851  * multiple valid media format choices. We try to minimize the amount
852  * of format conversion inside filters */
853  reduce_formats(graph);
854 
855  /* for audio filters, ensure the best format, sample rate and channel layout
856  * is selected */
857  swap_sample_fmts(graph);
858  swap_samplerates(graph);
859  swap_channel_layouts(graph);
860 
861  if ((ret = pick_formats(graph)) < 0)
862  return ret;
863 
864  return 0;
865 }
866 
868  AVClass *log_ctx)
869 {
870  unsigned i, j;
871  int sink_links_count = 0, n = 0;
872  AVFilterContext *f;
873  AVFilterLink **sinks;
874 
875  for (i = 0; i < graph->filter_count; i++) {
876  f = graph->filters[i];
877  for (j = 0; j < f->nb_inputs; j++) {
878  f->inputs[j]->graph = graph;
879  f->inputs[j]->age_index = -1;
880  }
881  for (j = 0; j < f->nb_outputs; j++) {
882  f->outputs[j]->graph = graph;
883  f->outputs[j]->age_index= -1;
884  }
885  if (!f->nb_outputs) {
886  if (f->nb_inputs > INT_MAX - sink_links_count)
887  return AVERROR(EINVAL);
888  sink_links_count += f->nb_inputs;
889  }
890  }
891  sinks = av_calloc(sink_links_count, sizeof(*sinks));
892  if (!sinks)
893  return AVERROR(ENOMEM);
894  for (i = 0; i < graph->filter_count; i++) {
895  f = graph->filters[i];
896  if (!f->nb_outputs) {
897  for (j = 0; j < f->nb_inputs; j++) {
898  sinks[n] = f->inputs[j];
899  f->inputs[j]->age_index = n++;
900  }
901  }
902  }
903  av_assert0(n == sink_links_count);
904  graph->sink_links = sinks;
906  return 0;
907 }
908 
909 static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
910 {
911  AVFilterContext *f;
912  int i, j, ret;
913  int fifo_count = 0;
914 
915  for (i = 0; i < graph->filter_count; i++) {
916  f = graph->filters[i];
917 
918  for (j = 0; j < f->nb_inputs; j++) {
919  AVFilterLink *link = f->inputs[j];
920  AVFilterContext *fifo_ctx;
921  AVFilter *fifo;
922  char name[32];
923 
924  if (!link->dstpad->needs_fifo)
925  continue;
926 
927  fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
928  avfilter_get_by_name("fifo") :
929  avfilter_get_by_name("afifo");
930 
931  snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
932 
933  ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
934  NULL, graph);
935  if (ret < 0)
936  return ret;
937 
938  ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
939  if (ret < 0)
940  return ret;
941  }
942  }
943 
944  return 0;
945 }
946 
947 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
948 {
949  int ret;
950 
951  if ((ret = graph_check_validity(graphctx, log_ctx)))
952  return ret;
953  if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
954  return ret;
955  if ((ret = graph_config_formats(graphctx, log_ctx)))
956  return ret;
957  if ((ret = graph_config_links(graphctx, log_ctx)))
958  return ret;
959  if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
960  return ret;
961 
962  return 0;
963 }
964 
965 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
966 {
967  int i, r = AVERROR(ENOSYS);
968 
969  if(!graph)
970  return r;
971 
972  if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
973  r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
974  if(r != AVERROR(ENOSYS))
975  return r;
976  }
977 
978  if(res_len && res)
979  res[0]= 0;
980 
981  for (i = 0; i < graph->filter_count; i++) {
982  AVFilterContext *filter = graph->filters[i];
983  if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
984  r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
985  if(r != AVERROR(ENOSYS)) {
986  if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
987  return r;
988  }
989  }
990  }
991 
992  return r;
993 }
994 
995 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
996 {
997  int i;
998 
999  if(!graph)
1000  return 0;
1001 
1002  for (i = 0; i < graph->filter_count; i++) {
1003  AVFilterContext *filter = graph->filters[i];
1004  if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
1005  AVFilterCommand **queue = &filter->command_queue, *next;
1006  while (*queue && (*queue)->time <= ts)
1007  queue = &(*queue)->next;
1008  next = *queue;
1009  *queue = av_mallocz(sizeof(AVFilterCommand));
1010  (*queue)->command = av_strdup(command);
1011  (*queue)->arg = av_strdup(arg);
1012  (*queue)->time = ts;
1013  (*queue)->flags = flags;
1014  (*queue)->next = next;
1015  if(flags & AVFILTER_CMD_FLAG_ONE)
1016  return 0;
1017  }
1018  }
1019 
1020  return 0;
1021 }
1022 
1023 static void heap_bubble_up(AVFilterGraph *graph,
1024  AVFilterLink *link, int index)
1025 {
1026  AVFilterLink **links = graph->sink_links;
1027 
1028  while (index) {
1029  int parent = (index - 1) >> 1;
1030  if (links[parent]->current_pts >= link->current_pts)
1031  break;
1032  links[index] = links[parent];
1033  links[index]->age_index = index;
1034  index = parent;
1035  }
1036  links[index] = link;
1037  link->age_index = index;
1038 }
1039 
1040 static void heap_bubble_down(AVFilterGraph *graph,
1041  AVFilterLink *link, int index)
1042 {
1043  AVFilterLink **links = graph->sink_links;
1044 
1045  while (1) {
1046  int child = 2 * index + 1;
1047  if (child >= graph->sink_links_count)
1048  break;
1049  if (child + 1 < graph->sink_links_count &&
1050  links[child + 1]->current_pts < links[child]->current_pts)
1051  child++;
1052  if (link->current_pts < links[child]->current_pts)
1053  break;
1054  links[index] = links[child];
1055  links[index]->age_index = index;
1056  index = child;
1057  }
1058  links[index] = link;
1059  link->age_index = index;
1060 }
1061 
1063 {
1064  heap_bubble_up (graph, link, link->age_index);
1065  heap_bubble_down(graph, link, link->age_index);
1066 }
1067 
1068 
1070 {
1071  while (graph->sink_links_count) {
1072  AVFilterLink *oldest = graph->sink_links[0];
1073  int r = ff_request_frame(oldest);
1074  if (r != AVERROR_EOF)
1075  return r;
1076  av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
1077  oldest->dst ? oldest->dst->name : "unknown",
1078  oldest->dstpad ? oldest->dstpad->name : "unknown");
1079  /* EOF: remove the link from the heap */
1080  if (oldest->age_index < --graph->sink_links_count)
1081  heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
1082  oldest->age_index);
1083  oldest->age_index = -1;
1084  }
1085  return AVERROR_EOF;
1086 }