FFmpeg  2.1.1
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 "config.h"
24 
25 #include <string.h>
26 
27 #include "libavutil/avassert.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/bprint.h"
30 #include "libavutil/channel_layout.h"
31 #include "libavutil/internal.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavcodec/avcodec.h" // avcodec_find_best_pix_fmt_of_2()
35 
36 #include "avfilter.h"
37 #include "formats.h"
38 #include "internal.h"
39 #include "thread.h"
40 
41 #define OFFSET(x) offsetof(AVFilterGraph, x)
42 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
43 static const AVOption filtergraph_options[] = {
44  { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
45  { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
46  { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .flags = FLAGS, .unit = "thread_type" },
47  { "threads", "Maximum number of threads", OFFSET(nb_threads),
48  AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
49  {"scale_sws_opts" , "default scale filter options" , OFFSET(scale_sws_opts) ,
50  AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
51  {"aresample_swr_opts" , "default aresample filter options" , OFFSET(aresample_swr_opts) ,
52  AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
53  { NULL },
54 };
55 
56 static const AVClass filtergraph_class = {
57  .class_name = "AVFilterGraph",
58  .item_name = av_default_item_name,
59  .version = LIBAVUTIL_VERSION_INT,
60  .option = filtergraph_options,
61  .category = AV_CLASS_CATEGORY_FILTER,
62 };
63 
64 #if !HAVE_THREADS
66 {
67 }
68 
70 {
71  graph->thread_type = 0;
72  graph->nb_threads = 1;
73  return 0;
74 }
75 #endif
76 
78 {
79  AVFilterGraph *ret = av_mallocz(sizeof(*ret));
80  if (!ret)
81  return NULL;
82 
83  ret->internal = av_mallocz(sizeof(*ret->internal));
84  if (!ret->internal) {
85  av_freep(&ret);
86  return NULL;
87  }
88 
91 
92  return ret;
93 }
94 
96 {
97  int i;
98  for (i = 0; i < graph->nb_filters; i++) {
99  if (graph->filters[i] == filter) {
100  FFSWAP(AVFilterContext*, graph->filters[i],
101  graph->filters[graph->nb_filters - 1]);
102  graph->nb_filters--;
103  return;
104  }
105  }
106 }
107 
109 {
110  if (!*graph)
111  return;
112 
113  while ((*graph)->nb_filters)
114  avfilter_free((*graph)->filters[0]);
115 
116  ff_graph_thread_free(*graph);
117 
118  av_freep(&(*graph)->sink_links);
119 
120  av_freep(&(*graph)->scale_sws_opts);
121  av_freep(&(*graph)->aresample_swr_opts);
122  av_freep(&(*graph)->resample_lavr_opts);
123  av_freep(&(*graph)->filters);
124  av_freep(&(*graph)->internal);
125  av_freep(graph);
126 }
127 
128 #if FF_API_AVFILTER_OPEN
129 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
130 {
132  sizeof(*filters) * (graph->nb_filters + 1));
133  if (!filters)
134  return AVERROR(ENOMEM);
135 
136  graph->filters = filters;
137  graph->filters[graph->nb_filters++] = filter;
138 
139 #if FF_API_FOO_COUNT
141  graph->filter_count_unused = graph->nb_filters;
143 #endif
144 
145  filter->graph = graph;
146 
147  return 0;
148 }
149 #endif
150 
152  const char *name, const char *args, void *opaque,
153  AVFilterGraph *graph_ctx)
154 {
155  int ret;
156 
157  *filt_ctx = avfilter_graph_alloc_filter(graph_ctx, filt, name);
158  if (!*filt_ctx)
159  return AVERROR(ENOMEM);
160 
161  ret = avfilter_init_str(*filt_ctx, args);
162  if (ret < 0)
163  goto fail;
164 
165  return 0;
166 
167 fail:
168  if (*filt_ctx)
169  avfilter_free(*filt_ctx);
170  *filt_ctx = NULL;
171  return ret;
172 }
173 
175 {
176  graph->disable_auto_convert = flags;
177 }
178 
180  const AVFilter *filter,
181  const char *name)
182 {
184 
185  if (graph->thread_type && !graph->internal->thread_execute) {
186  if (graph->execute) {
187  graph->internal->thread_execute = graph->execute;
188  } else {
189  int ret = ff_graph_thread_init(graph);
190  if (ret < 0) {
191  av_log(graph, AV_LOG_ERROR, "Error initializing threading.\n");
192  return NULL;
193  }
194  }
195  }
196 
197  s = ff_filter_alloc(filter, name);
198  if (!s)
199  return NULL;
200 
201  filters = av_realloc(graph->filters, sizeof(*filters) * (graph->nb_filters + 1));
202  if (!filters) {
203  avfilter_free(s);
204  return NULL;
205  }
206 
207  graph->filters = filters;
208  graph->filters[graph->nb_filters++] = s;
209 
210 #if FF_API_FOO_COUNT
212  graph->filter_count_unused = graph->nb_filters;
214 #endif
215 
216  s->graph = graph;
217 
218  return s;
219 }
220 
221 /**
222  * Check for the validity of graph.
223  *
224  * A graph is considered valid if all its input and output pads are
225  * connected.
226  *
227  * @return >= 0 in case of success, a negative value otherwise
228  */
229 static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
230 {
232  int i, j;
233 
234  for (i = 0; i < graph->nb_filters; i++) {
235  const AVFilterPad *pad;
236  filt = graph->filters[i];
237 
238  for (j = 0; j < filt->nb_inputs; j++) {
239  if (!filt->inputs[j] || !filt->inputs[j]->src) {
240  pad = &filt->input_pads[j];
241  av_log(log_ctx, AV_LOG_ERROR,
242  "Input pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any source\n",
243  pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
244  return AVERROR(EINVAL);
245  }
246  }
247 
248  for (j = 0; j < filt->nb_outputs; j++) {
249  if (!filt->outputs[j] || !filt->outputs[j]->dst) {
250  pad = &filt->output_pads[j];
251  av_log(log_ctx, AV_LOG_ERROR,
252  "Output pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any destination\n",
253  pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
254  return AVERROR(EINVAL);
255  }
256  }
257  }
258 
259  return 0;
260 }
261 
262 /**
263  * Configure all the links of graphctx.
264  *
265  * @return >= 0 in case of success, a negative value otherwise
266  */
267 static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
268 {
270  int i, ret;
271 
272  for (i = 0; i < graph->nb_filters; i++) {
273  filt = graph->filters[i];
274 
275  if (!filt->nb_outputs) {
276  if ((ret = avfilter_config_links(filt)))
277  return ret;
278  }
279  }
280 
281  return 0;
282 }
283 
285 {
286  int i;
287 
288  for (i = 0; i < graph->nb_filters; i++)
289  if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
290  return graph->filters[i];
291 
292  return NULL;
293 }
294 
296 {
297  if (!l)
298  return;
299  if (l->nb_channel_layouts) {
300  if (l->all_layouts || l->all_counts)
301  av_log(log, AV_LOG_WARNING, "All layouts set on non-empty list\n");
302  l->all_layouts = l->all_counts = 0;
303  } else {
304  if (l->all_counts && !l->all_layouts)
305  av_log(log, AV_LOG_WARNING, "All counts without all layouts\n");
306  l->all_layouts = 1;
307  }
308 }
309 
311 {
312  int ret, i;
314  AVFilterChannelLayouts *chlayouts;
315  AVFilterFormats *samplerates;
316  enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
317  ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
319 
320  if ((ret = ctx->filter->query_formats(ctx)) < 0) {
321  if (ret != AVERROR(EAGAIN))
322  av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
323  ctx->name, av_err2str(ret));
324  return ret;
325  }
326 
327  for (i = 0; i < ctx->nb_inputs; i++)
329  for (i = 0; i < ctx->nb_outputs; i++)
331 
332  formats = ff_all_formats(type);
333  if (!formats)
334  return AVERROR(ENOMEM);
335  ff_set_common_formats(ctx, formats);
336  if (type == AVMEDIA_TYPE_AUDIO) {
337  samplerates = ff_all_samplerates();
338  if (!samplerates)
339  return AVERROR(ENOMEM);
340  ff_set_common_samplerates(ctx, samplerates);
341  chlayouts = ff_all_channel_layouts();
342  if (!chlayouts)
343  return AVERROR(ENOMEM);
344  ff_set_common_channel_layouts(ctx, chlayouts);
345  }
346  return 0;
347 }
348 
350 {
351  int i;
352 
353  for (i = 0; i < f->nb_inputs; i++) {
354  if (!f->inputs[i]->out_formats)
355  return 0;
356  if (f->inputs[i]->type == AVMEDIA_TYPE_AUDIO &&
357  !(f->inputs[i]->out_samplerates &&
358  f->inputs[i]->out_channel_layouts))
359  return 0;
360  }
361  for (i = 0; i < f->nb_outputs; i++) {
362  if (!f->outputs[i]->in_formats)
363  return 0;
364  if (f->outputs[i]->type == AVMEDIA_TYPE_AUDIO &&
365  !(f->outputs[i]->in_samplerates &&
366  f->outputs[i]->in_channel_layouts))
367  return 0;
368  }
369  return 1;
370 }
371 
373 {
374  AVFilterFormats *a = av_memdup(arg, sizeof(*arg));
375  if (a) {
376  a->refcount = 0;
377  a->refs = NULL;
378  a->formats = av_memdup(a->formats, sizeof(*a->formats) * a->nb_formats);
379  if (!a->formats && arg->formats)
380  av_freep(&a);
381  }
382  return a;
383 }
384 
386  AVFilterFormats *b_arg,
387  enum AVMediaType type,
388  int is_sample_rate)
389 {
390  AVFilterFormats *a, *b, *ret;
391  if (a_arg == b_arg)
392  return 1;
393  a = clone_filter_formats(a_arg);
394  b = clone_filter_formats(b_arg);
395 
396  if (!a || !b) {
397  if (a)
398  av_freep(&a->formats);
399  if (b)
400  av_freep(&b->formats);
401 
402  av_freep(&a);
403  av_freep(&b);
404 
405  return 0;
406  }
407 
408  if (is_sample_rate) {
409  ret = ff_merge_samplerates(a, b);
410  } else {
411  ret = ff_merge_formats(a, b, type);
412  }
413  if (ret) {
414  av_freep(&ret->formats);
415  av_freep(&ret->refs);
416  av_freep(&ret);
417  return 1;
418  } else {
419  av_freep(&a->formats);
420  av_freep(&b->formats);
421  av_freep(&a);
422  av_freep(&b);
423  return 0;
424  }
425 }
426 
427 /**
428  * Perform one round of query_formats() and merging formats lists on the
429  * filter graph.
430  * @return >=0 if all links formats lists could be queried and merged;
431  * AVERROR(EAGAIN) some progress was made in the queries or merging
432  * and a later call may succeed;
433  * AVERROR(EIO) (may be changed) plus a log message if no progress
434  * was made and the negotiation is stuck;
435  * a negative error code if some other error happened
436  */
437 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
438 {
439  int i, j, ret;
440  int scaler_count = 0, resampler_count = 0;
441  int count_queried = 0; /* successful calls to query_formats() */
442  int count_merged = 0; /* successful merge of formats lists */
443  int count_already_merged = 0; /* lists already merged */
444  int count_delayed = 0; /* lists that need to be merged later */
445 
446  for (i = 0; i < graph->nb_filters; i++) {
447  AVFilterContext *f = graph->filters[i];
448  if (formats_declared(f))
449  continue;
450  if (f->filter->query_formats)
451  ret = filter_query_formats(f);
452  else
453  ret = ff_default_query_formats(f);
454  if (ret < 0 && ret != AVERROR(EAGAIN))
455  return ret;
456  /* note: EAGAIN could indicate a partial success, not counted yet */
457  count_queried += ret >= 0;
458  }
459 
460  /* go through and merge as many format lists as possible */
461  for (i = 0; i < graph->nb_filters; i++) {
462  AVFilterContext *filter = graph->filters[i];
463 
464  for (j = 0; j < filter->nb_inputs; j++) {
465  AVFilterLink *link = filter->inputs[j];
466  int convert_needed = 0;
467 
468  if (!link)
469  continue;
470 
471  if (link->in_formats != link->out_formats
472  && link->in_formats && link->out_formats)
473  if (!can_merge_formats(link->in_formats, link->out_formats,
474  link->type, 0))
475  convert_needed = 1;
476  if (link->type == AVMEDIA_TYPE_AUDIO) {
477  if (link->in_samplerates != link->out_samplerates
478  && link->in_samplerates && link->out_samplerates)
480  link->out_samplerates,
481  0, 1))
482  convert_needed = 1;
483  }
484 
485 #define MERGE_DISPATCH(field, statement) \
486  if (!(link->in_ ## field && link->out_ ## field)) { \
487  count_delayed++; \
488  } else if (link->in_ ## field == link->out_ ## field) { \
489  count_already_merged++; \
490  } else if (!convert_needed) { \
491  count_merged++; \
492  statement \
493  }
494 
495  if (link->type == AVMEDIA_TYPE_AUDIO) {
496  MERGE_DISPATCH(channel_layouts,
498  link->out_channel_layouts))
499  convert_needed = 1;
500  )
501  MERGE_DISPATCH(samplerates,
503  link->out_samplerates))
504  convert_needed = 1;
505  )
506  }
508  if (!ff_merge_formats(link->in_formats, link->out_formats,
509  link->type))
510  convert_needed = 1;
511  )
512 #undef MERGE_DISPATCH
513 
514  if (convert_needed) {
516  AVFilter *filter;
517  AVFilterLink *inlink, *outlink;
518  char scale_args[256];
519  char inst_name[30];
520 
521  /* couldn't merge format lists. auto-insert conversion filter */
522  switch (link->type) {
523  case AVMEDIA_TYPE_VIDEO:
524  if (!(filter = avfilter_get_by_name("scale"))) {
525  av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
526  "not present, cannot convert pixel formats.\n");
527  return AVERROR(EINVAL);
528  }
529 
530  snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
531  scaler_count++);
532 
533  if ((ret = avfilter_graph_create_filter(&convert, filter,
534  inst_name, graph->scale_sws_opts, NULL,
535  graph)) < 0)
536  return ret;
537  break;
538  case AVMEDIA_TYPE_AUDIO:
539  if (!(filter = avfilter_get_by_name("aresample"))) {
540  av_log(log_ctx, AV_LOG_ERROR, "'aresample' filter "
541  "not present, cannot convert audio formats.\n");
542  return AVERROR(EINVAL);
543  }
544 
545  snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
546  resampler_count++);
547  scale_args[0] = '\0';
548  if (graph->aresample_swr_opts)
549  snprintf(scale_args, sizeof(scale_args), "%s",
550  graph->aresample_swr_opts);
551  if ((ret = avfilter_graph_create_filter(&convert, filter,
552  inst_name, graph->aresample_swr_opts,
553  NULL, graph)) < 0)
554  return ret;
555  break;
556  default:
557  return AVERROR(EINVAL);
558  }
559 
560  if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
561  return ret;
562 
563  filter_query_formats(convert);
564  inlink = convert->inputs[0];
565  outlink = convert->outputs[0];
566  if (!ff_merge_formats( inlink->in_formats, inlink->out_formats, inlink->type) ||
567  !ff_merge_formats(outlink->in_formats, outlink->out_formats, outlink->type))
568  ret |= AVERROR(ENOSYS);
569  if (inlink->type == AVMEDIA_TYPE_AUDIO &&
571  inlink->out_samplerates) ||
573  inlink->out_channel_layouts)))
574  ret |= AVERROR(ENOSYS);
575  if (outlink->type == AVMEDIA_TYPE_AUDIO &&
577  outlink->out_samplerates) ||
579  outlink->out_channel_layouts)))
580  ret |= AVERROR(ENOSYS);
581 
582  if (ret < 0) {
583  av_log(log_ctx, AV_LOG_ERROR,
584  "Impossible to convert between the formats supported by the filter "
585  "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
586  return ret;
587  }
588  }
589  }
590  }
591 
592  av_log(graph, AV_LOG_DEBUG, "query_formats: "
593  "%d queried, %d merged, %d already done, %d delayed\n",
594  count_queried, count_merged, count_already_merged, count_delayed);
595  if (count_delayed) {
596  AVBPrint bp;
597 
598  /* if count_queried > 0, one filter at least did set its formats,
599  that will give additional information to its neighbour;
600  if count_merged > 0, one pair of formats lists at least was merged,
601  that will give additional information to all connected filters;
602  in both cases, progress was made and a new round must be done */
603  if (count_queried || count_merged)
604  return AVERROR(EAGAIN);
606  for (i = 0; i < graph->nb_filters; i++)
607  if (!formats_declared(graph->filters[i]))
608  av_bprintf(&bp, "%s%s", bp.len ? ", " : "",
609  graph->filters[i]->name);
610  av_log(graph, AV_LOG_ERROR,
611  "The following filters could not choose their formats: %s\n"
612  "Consider inserting the (a)format filter near their input or "
613  "output.\n", bp.str);
614  return AVERROR(EIO);
615  }
616  return 0;
617 }
618 
619 static int pick_format(AVFilterLink *link, AVFilterLink *ref)
620 {
621  if (!link || !link->in_formats)
622  return 0;
623 
624  if (link->type == AVMEDIA_TYPE_VIDEO) {
625  if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
626  int has_alpha= av_pix_fmt_desc_get(ref->format)->nb_components % 2 == 0;
627  enum AVPixelFormat best= AV_PIX_FMT_NONE;
628  int i;
629  for (i=0; i<link->in_formats->nb_formats; i++) {
630  enum AVPixelFormat p = link->in_formats->formats[i];
631  best= avcodec_find_best_pix_fmt_of_2(best, p, ref->format, has_alpha, NULL);
632  }
633  av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
635  av_get_pix_fmt_name(ref->format), has_alpha);
636  link->in_formats->formats[0] = best;
637  }
638  }
639 
640  link->in_formats->nb_formats = 1;
641  link->format = link->in_formats->formats[0];
642 
643  if (link->type == AVMEDIA_TYPE_AUDIO) {
644  if (!link->in_samplerates->nb_formats) {
645  av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
646  " the link between filters %s and %s.\n", link->src->name,
647  link->dst->name);
648  return AVERROR(EINVAL);
649  }
650  link->in_samplerates->nb_formats = 1;
651  link->sample_rate = link->in_samplerates->formats[0];
652 
653  if (link->in_channel_layouts->all_layouts) {
654  av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
655  " the link between filters %s and %s.\n", link->src->name,
656  link->dst->name);
657  return AVERROR(EINVAL);
658  }
661  if ((link->channels = FF_LAYOUT2COUNT(link->channel_layout)))
662  link->channel_layout = 0;
663  else
665  }
666 
673 
674  return 0;
675 }
676 
677 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
678 do { \
679  for (i = 0; i < filter->nb_inputs; i++) { \
680  AVFilterLink *link = filter->inputs[i]; \
681  fmt_type fmt; \
682  \
683  if (!link->out_ ## list || link->out_ ## list->nb != 1) \
684  continue; \
685  fmt = link->out_ ## list->var[0]; \
686  \
687  for (j = 0; j < filter->nb_outputs; j++) { \
688  AVFilterLink *out_link = filter->outputs[j]; \
689  list_type *fmts; \
690  \
691  if (link->type != out_link->type || \
692  out_link->in_ ## list->nb == 1) \
693  continue; \
694  fmts = out_link->in_ ## list; \
695  \
696  if (!out_link->in_ ## list->nb) { \
697  add_format(&out_link->in_ ##list, fmt); \
698  ret = 1; \
699  break; \
700  } \
701  \
702  for (k = 0; k < out_link->in_ ## list->nb; k++) \
703  if (fmts->var[k] == fmt) { \
704  fmts->var[0] = fmt; \
705  fmts->nb = 1; \
706  ret = 1; \
707  break; \
708  } \
709  } \
710  } \
711 } while (0)
712 
714 {
715  int i, j, k, ret = 0;
716 
718  nb_formats, ff_add_format);
719  REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
720  nb_formats, ff_add_format);
721 
722  /* reduce channel layouts */
723  for (i = 0; i < filter->nb_inputs; i++) {
724  AVFilterLink *inlink = filter->inputs[i];
725  uint64_t fmt;
726 
727  if (!inlink->out_channel_layouts ||
729  continue;
730  fmt = inlink->out_channel_layouts->channel_layouts[0];
731 
732  for (j = 0; j < filter->nb_outputs; j++) {
733  AVFilterLink *outlink = filter->outputs[j];
735 
736  fmts = outlink->in_channel_layouts;
737  if (inlink->type != outlink->type || fmts->nb_channel_layouts == 1)
738  continue;
739 
740  if (fmts->all_layouts) {
741  /* Turn the infinite list into a singleton */
742  fmts->all_layouts = fmts->all_counts = 0;
744  break;
745  }
746 
747  for (k = 0; k < outlink->in_channel_layouts->nb_channel_layouts; k++) {
748  if (fmts->channel_layouts[k] == fmt) {
749  fmts->channel_layouts[0] = fmt;
750  fmts->nb_channel_layouts = 1;
751  ret = 1;
752  break;
753  }
754  }
755  }
756  }
757 
758  return ret;
759 }
760 
761 static void reduce_formats(AVFilterGraph *graph)
762 {
763  int i, reduced;
764 
765  do {
766  reduced = 0;
767 
768  for (i = 0; i < graph->nb_filters; i++)
769  reduced |= reduce_formats_on_filter(graph->filters[i]);
770  } while (reduced);
771 }
772 
774 {
775  AVFilterLink *link = NULL;
776  int sample_rate;
777  int i, j;
778 
779  for (i = 0; i < filter->nb_inputs; i++) {
780  link = filter->inputs[i];
781 
782  if (link->type == AVMEDIA_TYPE_AUDIO &&
783  link->out_samplerates->nb_formats== 1)
784  break;
785  }
786  if (i == filter->nb_inputs)
787  return;
788 
789  sample_rate = link->out_samplerates->formats[0];
790 
791  for (i = 0; i < filter->nb_outputs; i++) {
792  AVFilterLink *outlink = filter->outputs[i];
793  int best_idx, best_diff = INT_MAX;
794 
795  if (outlink->type != AVMEDIA_TYPE_AUDIO ||
796  outlink->in_samplerates->nb_formats < 2)
797  continue;
798 
799  for (j = 0; j < outlink->in_samplerates->nb_formats; j++) {
800  int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
801 
802  if (diff < best_diff) {
803  best_diff = diff;
804  best_idx = j;
805  }
806  }
807  FFSWAP(int, outlink->in_samplerates->formats[0],
808  outlink->in_samplerates->formats[best_idx]);
809  }
810 }
811 
812 static void swap_samplerates(AVFilterGraph *graph)
813 {
814  int i;
815 
816  for (i = 0; i < graph->nb_filters; i++)
818 }
819 
820 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
821 #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
822 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
823 #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
824 #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
825 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
826 #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
827 
828 /* allowable substitutions for channel pairs when comparing layouts,
829  * ordered by priority for both values */
830 static const uint64_t ch_subst[][2] = {
852 };
853 
855 {
856  AVFilterLink *link = NULL;
857  int i, j, k;
858 
859  for (i = 0; i < filter->nb_inputs; i++) {
860  link = filter->inputs[i];
861 
862  if (link->type == AVMEDIA_TYPE_AUDIO &&
864  break;
865  }
866  if (i == filter->nb_inputs)
867  return;
868 
869  for (i = 0; i < filter->nb_outputs; i++) {
870  AVFilterLink *outlink = filter->outputs[i];
871  int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
872 
873  if (outlink->type != AVMEDIA_TYPE_AUDIO ||
875  continue;
876 
877  for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
878  uint64_t in_chlayout = link->out_channel_layouts->channel_layouts[0];
879  uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
880  int in_channels = av_get_channel_layout_nb_channels(in_chlayout);
881  int out_channels = av_get_channel_layout_nb_channels(out_chlayout);
882  int count_diff = out_channels - in_channels;
883  int matched_channels, extra_channels;
884  int score = 100000;
885 
886  if (FF_LAYOUT2COUNT(in_chlayout) || FF_LAYOUT2COUNT(out_chlayout)) {
887  /* Compute score in case the input or output layout encodes
888  a channel count; in this case the score is not altered by
889  the computation afterwards, as in_chlayout and
890  out_chlayout have both been set to 0 */
891  if (FF_LAYOUT2COUNT(in_chlayout))
892  in_channels = FF_LAYOUT2COUNT(in_chlayout);
893  if (FF_LAYOUT2COUNT(out_chlayout))
894  out_channels = FF_LAYOUT2COUNT(out_chlayout);
895  score -= 10000 + FFABS(out_channels - in_channels) +
896  (in_channels > out_channels ? 10000 : 0);
897  in_chlayout = out_chlayout = 0;
898  /* Let the remaining computation run, even if the score
899  value is not altered */
900  }
901 
902  /* channel substitution */
903  for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
904  uint64_t cmp0 = ch_subst[k][0];
905  uint64_t cmp1 = ch_subst[k][1];
906  if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
907  (out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
908  in_chlayout &= ~cmp0;
909  out_chlayout &= ~cmp1;
910  /* add score for channel match, minus a deduction for
911  having to do the substitution */
912  score += 10 * av_get_channel_layout_nb_channels(cmp1) - 2;
913  }
914  }
915 
916  /* no penalty for LFE channel mismatch */
917  if ( (in_chlayout & AV_CH_LOW_FREQUENCY) &&
918  (out_chlayout & AV_CH_LOW_FREQUENCY))
919  score += 10;
920  in_chlayout &= ~AV_CH_LOW_FREQUENCY;
921  out_chlayout &= ~AV_CH_LOW_FREQUENCY;
922 
923  matched_channels = av_get_channel_layout_nb_channels(in_chlayout &
924  out_chlayout);
925  extra_channels = av_get_channel_layout_nb_channels(out_chlayout &
926  (~in_chlayout));
927  score += 10 * matched_channels - 5 * extra_channels;
928 
929  if (score > best_score ||
930  (count_diff < best_count_diff && score == best_score)) {
931  best_score = score;
932  best_idx = j;
933  best_count_diff = count_diff;
934  }
935  }
936  av_assert0(best_idx >= 0);
937  FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
938  outlink->in_channel_layouts->channel_layouts[best_idx]);
939  }
940 
941 }
942 
944 {
945  int i;
946 
947  for (i = 0; i < graph->nb_filters; i++)
949 }
950 
952 {
953  AVFilterLink *link = NULL;
954  int format, bps;
955  int i, j;
956 
957  for (i = 0; i < filter->nb_inputs; i++) {
958  link = filter->inputs[i];
959 
960  if (link->type == AVMEDIA_TYPE_AUDIO &&
961  link->out_formats->nb_formats == 1)
962  break;
963  }
964  if (i == filter->nb_inputs)
965  return;
966 
967  format = link->out_formats->formats[0];
968  bps = av_get_bytes_per_sample(format);
969 
970  for (i = 0; i < filter->nb_outputs; i++) {
971  AVFilterLink *outlink = filter->outputs[i];
972  int best_idx = -1, best_score = INT_MIN;
973 
974  if (outlink->type != AVMEDIA_TYPE_AUDIO ||
975  outlink->in_formats->nb_formats < 2)
976  continue;
977 
978  for (j = 0; j < outlink->in_formats->nb_formats; j++) {
979  int out_format = outlink->in_formats->formats[j];
980  int out_bps = av_get_bytes_per_sample(out_format);
981  int score;
982 
983  if (av_get_packed_sample_fmt(out_format) == format ||
984  av_get_planar_sample_fmt(out_format) == format) {
985  best_idx = j;
986  break;
987  }
988 
989  /* for s32 and float prefer double to prevent loss of information */
990  if (bps == 4 && out_bps == 8) {
991  best_idx = j;
992  break;
993  }
994 
995  /* prefer closest higher or equal bps */
996  score = -abs(out_bps - bps);
997  if (out_bps >= bps)
998  score += INT_MAX/2;
999 
1000  if (score > best_score) {
1001  best_score = score;
1002  best_idx = j;
1003  }
1004  }
1005  av_assert0(best_idx >= 0);
1006  FFSWAP(int, outlink->in_formats->formats[0],
1007  outlink->in_formats->formats[best_idx]);
1008  }
1009 }
1010 
1011 static void swap_sample_fmts(AVFilterGraph *graph)
1012 {
1013  int i;
1014 
1015  for (i = 0; i < graph->nb_filters; i++)
1017 
1018 }
1019 
1020 static int pick_formats(AVFilterGraph *graph)
1021 {
1022  int i, j, ret;
1023  int change;
1024 
1025  do{
1026  change = 0;
1027  for (i = 0; i < graph->nb_filters; i++) {
1028  AVFilterContext *filter = graph->filters[i];
1029  if (filter->nb_inputs){
1030  for (j = 0; j < filter->nb_inputs; j++){
1031  if(filter->inputs[j]->in_formats && filter->inputs[j]->in_formats->nb_formats == 1) {
1032  if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1033  return ret;
1034  change = 1;
1035  }
1036  }
1037  }
1038  if (filter->nb_outputs){
1039  for (j = 0; j < filter->nb_outputs; j++){
1040  if(filter->outputs[j]->in_formats && filter->outputs[j]->in_formats->nb_formats == 1) {
1041  if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1042  return ret;
1043  change = 1;
1044  }
1045  }
1046  }
1047  if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
1048  for (j = 0; j < filter->nb_outputs; j++) {
1049  if(filter->outputs[j]->format<0) {
1050  if ((ret = pick_format(filter->outputs[j], filter->inputs[0])) < 0)
1051  return ret;
1052  change = 1;
1053  }
1054  }
1055  }
1056  }
1057  }while(change);
1058 
1059  for (i = 0; i < graph->nb_filters; i++) {
1060  AVFilterContext *filter = graph->filters[i];
1061 
1062  for (j = 0; j < filter->nb_inputs; j++)
1063  if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1064  return ret;
1065  for (j = 0; j < filter->nb_outputs; j++)
1066  if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1067  return ret;
1068  }
1069  return 0;
1070 }
1071 
1072 /**
1073  * Configure the formats of all the links in the graph.
1074  */
1075 static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
1076 {
1077  int ret;
1078 
1079  /* find supported formats from sub-filters, and merge along links */
1080  while ((ret = query_formats(graph, log_ctx)) == AVERROR(EAGAIN))
1081  av_log(graph, AV_LOG_DEBUG, "query_formats not finished\n");
1082  if (ret < 0)
1083  return ret;
1084 
1085  /* Once everything is merged, it's possible that we'll still have
1086  * multiple valid media format choices. We try to minimize the amount
1087  * of format conversion inside filters */
1088  reduce_formats(graph);
1089 
1090  /* for audio filters, ensure the best format, sample rate and channel layout
1091  * is selected */
1092  swap_sample_fmts(graph);
1093  swap_samplerates(graph);
1094  swap_channel_layouts(graph);
1095 
1096  if ((ret = pick_formats(graph)) < 0)
1097  return ret;
1098 
1099  return 0;
1100 }
1101 
1103  AVClass *log_ctx)
1104 {
1105  unsigned i, j;
1106  int sink_links_count = 0, n = 0;
1107  AVFilterContext *f;
1108  AVFilterLink **sinks;
1109 
1110  for (i = 0; i < graph->nb_filters; i++) {
1111  f = graph->filters[i];
1112  for (j = 0; j < f->nb_inputs; j++) {
1113  f->inputs[j]->graph = graph;
1114  f->inputs[j]->age_index = -1;
1115  }
1116  for (j = 0; j < f->nb_outputs; j++) {
1117  f->outputs[j]->graph = graph;
1118  f->outputs[j]->age_index= -1;
1119  }
1120  if (!f->nb_outputs) {
1121  if (f->nb_inputs > INT_MAX - sink_links_count)
1122  return AVERROR(EINVAL);
1123  sink_links_count += f->nb_inputs;
1124  }
1125  }
1126  sinks = av_calloc(sink_links_count, sizeof(*sinks));
1127  if (!sinks)
1128  return AVERROR(ENOMEM);
1129  for (i = 0; i < graph->nb_filters; i++) {
1130  f = graph->filters[i];
1131  if (!f->nb_outputs) {
1132  for (j = 0; j < f->nb_inputs; j++) {
1133  sinks[n] = f->inputs[j];
1134  f->inputs[j]->age_index = n++;
1135  }
1136  }
1137  }
1138  av_assert0(n == sink_links_count);
1139  graph->sink_links = sinks;
1140  graph->sink_links_count = sink_links_count;
1141  return 0;
1142 }
1143 
1144 static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
1145 {
1146  AVFilterContext *f;
1147  int i, j, ret;
1148  int fifo_count = 0;
1149 
1150  for (i = 0; i < graph->nb_filters; i++) {
1151  f = graph->filters[i];
1152 
1153  for (j = 0; j < f->nb_inputs; j++) {
1154  AVFilterLink *link = f->inputs[j];
1155  AVFilterContext *fifo_ctx;
1156  AVFilter *fifo;
1157  char name[32];
1158 
1159  if (!link->dstpad->needs_fifo)
1160  continue;
1161 
1162  fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
1163  avfilter_get_by_name("fifo") :
1164  avfilter_get_by_name("afifo");
1165 
1166  snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
1167 
1168  ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
1169  NULL, graph);
1170  if (ret < 0)
1171  return ret;
1172 
1173  ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
1174  if (ret < 0)
1175  return ret;
1176  }
1177  }
1178 
1179  return 0;
1180 }
1181 
1182 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
1183 {
1184  int ret;
1185 
1186  if ((ret = graph_check_validity(graphctx, log_ctx)))
1187  return ret;
1188  if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
1189  return ret;
1190  if ((ret = graph_config_formats(graphctx, log_ctx)))
1191  return ret;
1192  if ((ret = graph_config_links(graphctx, log_ctx)))
1193  return ret;
1194  if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
1195  return ret;
1196 
1197  return 0;
1198 }
1199 
1200 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
1201 {
1202  int i, r = AVERROR(ENOSYS);
1203 
1204  if (!graph)
1205  return r;
1206 
1207  if ((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
1208  r = avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
1209  if (r != AVERROR(ENOSYS))
1210  return r;
1211  }
1212 
1213  if (res_len && res)
1214  res[0] = 0;
1215 
1216  for (i = 0; i < graph->nb_filters; i++) {
1217  AVFilterContext *filter = graph->filters[i];
1218  if (!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)) {
1219  r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
1220  if (r != AVERROR(ENOSYS)) {
1221  if ((flags & AVFILTER_CMD_FLAG_ONE) || r < 0)
1222  return r;
1223  }
1224  }
1225  }
1226 
1227  return r;
1228 }
1229 
1230 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
1231 {
1232  int i;
1233 
1234  if(!graph)
1235  return 0;
1236 
1237  for (i = 0; i < graph->nb_filters; i++) {
1238  AVFilterContext *filter = graph->filters[i];
1239  if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
1240  AVFilterCommand **queue = &filter->command_queue, *next;
1241  while (*queue && (*queue)->time <= ts)
1242  queue = &(*queue)->next;
1243  next = *queue;
1244  *queue = av_mallocz(sizeof(AVFilterCommand));
1245  (*queue)->command = av_strdup(command);
1246  (*queue)->arg = av_strdup(arg);
1247  (*queue)->time = ts;
1248  (*queue)->flags = flags;
1249  (*queue)->next = next;
1250  if(flags & AVFILTER_CMD_FLAG_ONE)
1251  return 0;
1252  }
1253  }
1254 
1255  return 0;
1256 }
1257 
1258 static void heap_bubble_up(AVFilterGraph *graph,
1259  AVFilterLink *link, int index)
1260 {
1261  AVFilterLink **links = graph->sink_links;
1262 
1263  while (index) {
1264  int parent = (index - 1) >> 1;
1265  if (links[parent]->current_pts >= link->current_pts)
1266  break;
1267  links[index] = links[parent];
1268  links[index]->age_index = index;
1269  index = parent;
1270  }
1271  links[index] = link;
1272  link->age_index = index;
1273 }
1274 
1275 static void heap_bubble_down(AVFilterGraph *graph,
1276  AVFilterLink *link, int index)
1277 {
1278  AVFilterLink **links = graph->sink_links;
1279 
1280  while (1) {
1281  int child = 2 * index + 1;
1282  if (child >= graph->sink_links_count)
1283  break;
1284  if (child + 1 < graph->sink_links_count &&
1285  links[child + 1]->current_pts < links[child]->current_pts)
1286  child++;
1287  if (link->current_pts < links[child]->current_pts)
1288  break;
1289  links[index] = links[child];
1290  links[index]->age_index = index;
1291  index = child;
1292  }
1293  links[index] = link;
1294  link->age_index = index;
1295 }
1296 
1298 {
1299  heap_bubble_up (graph, link, link->age_index);
1300  heap_bubble_down(graph, link, link->age_index);
1301 }
1302 
1303 
1305 {
1306  while (graph->sink_links_count) {
1307  AVFilterLink *oldest = graph->sink_links[0];
1308  int r = ff_request_frame(oldest);
1309  if (r != AVERROR_EOF)
1310  return r;
1311  av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
1312  oldest->dst ? oldest->dst->name : "unknown",
1313  oldest->dstpad ? oldest->dstpad->name : "unknown");
1314  /* EOF: remove the link from the heap */
1315  if (oldest->age_index < --graph->sink_links_count)
1316  heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
1317  oldest->age_index);
1318  oldest->age_index = -1;
1319  }
1320  return AVERROR_EOF;
1321 }
const char * name
Definition: avisynth_c.h:675
void * av_memdup(const void *p, size_t size)
Duplicate the buffer p.
Definition: mem.c:268
void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
Enable or disable automatic format conversion inside the graph.
const char * s
Definition: avisynth_c.h:668
void * av_calloc(size_t nmemb, size_t size) av_malloc_attrib
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
Definition: mem.c:249
int sink_links_count
Definition: avfilter.h:1232
AVFilterContext * ff_filter_alloc(const AVFilter *filter, const char *inst_name)
Allocate a new filter context and return it.
Definition: avfilter.c:592
int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, int flags, double ts)
Queue a command for one or more filter instances.
int thread_type
Type of multithreading allowed for filters in this graph.
Definition: avfilter.h:1187
static void swap_samplerates(AVFilterGraph *graph)
char * scale_sws_opts
sws options to use for the auto-inserted scale filters
Definition: avfilter.h:1169
AVOption.
Definition: opt.h:253
static void swap_sample_fmts(AVFilterGraph *graph)
void avfilter_free(AVFilterContext *filter)
Free a filter context.
Definition: avfilter.c:693
const char * name
Filter name.
Definition: avfilter.h:468
AVFilterGraphInternal * internal
Opaque object for libavfilter internal use.
Definition: avfilter.h:1199
const char * fmt
Definition: avisynth_c.h:669
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: avcodec.h:4153
#define LIBAVUTIL_VERSION_INT
Definition: avcodec.h:820
char * av_strdup(const char *s) av_malloc_attrib
Duplicate the string s.
Definition: mem.c:256
int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
Check validity and configure all the links and formats in the graph.
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:1064
static const uint64_t ch_subst[][2]
const char * b
Definition: vf_curves.c:105
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:1860
void avfilter_graph_free(AVFilterGraph **graph)
Free a graph, destroy its links, and set *graph to NULL.
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVMediaType type
AVFilterPad type.
Definition: internal.h:71
static int reduce_formats_on_filter(AVFilterContext *filter)
static enum AVSampleFormat formats[]
int(* query_formats)(AVFilterContext *)
Query formats supported by the filter on its inputs and outputs.
Definition: avfilter.h:587
Pixel format.
Definition: avcodec.h:4533
static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
AVFilterContext * avfilter_graph_alloc_filter(AVFilterGraph *graph, const AVFilter *filter, const char *name)
Create a new filter instance in a filter graph.
enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt)
Get the planar alternative form of the given sample format.
Definition: samplefmt.c:82
#define AVFILTER_THREAD_SLICE
Process multiple parts of the frame concurrently.
Definition: avfilter.h:622
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
#define OFFSET(x)
Definition: avfiltergraph.c:41
const char * name
Pad name.
Definition: internal.h:66
#define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format)
static int pick_format(AVFilterLink *link, AVFilterLink *ref)
static uint8_t * res
Definition: ffhash.c:43
const char * av_default_item_name(void *ctx)
Return the context name.
Definition: log.c:145
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:55
#define FF_LAYOUT2COUNT(l)
Decode a channel count encoded as a channel layout.
Definition: formats.h:108
AVS_FilterInfo AVS_Value child
Definition: avisynth_c.h:635
struct AVFilterFormats *** refs
references to this list
Definition: formats.h:69
#define AV_CH_LOW_FREQUENCY
int avfilter_graph_create_filter(AVFilterContext **filt_ctx, const AVFilter *filt, const char *name, const char *args, void *opaque, AVFilterGraph *graph_ctx)
Create and add a filter instance into an existing graph.
static int pick_formats(AVFilterGraph *graph)
int nb_threads
Maximum number of threads used by filters in this graph.
Definition: avfilter.h:1194
int avfilter_config_links(AVFilterContext *filter)
Negotiate the media format, dimensions, etc of all inputs to a filter.
Definition: avfilter.c:219
char * aresample_swr_opts
swr options to use for the auto-inserted aresample filters, Access ONLY through AVOptions ...
Definition: avfilter.h:1222
void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:531
#define FFSWAP(type, a, b)
Definition: avcodec.h:928
static const vf_info_t *const filters[]
Definition: vf_mp.c:134
AVFilterFormats * ff_all_formats(enum AVMediaType type)
Return a list of all formats supported by FFmpeg for the given media type.
Definition: formats.c:343
A filter pad used for either input or output.
Definition: internal.h:60
unsigned refcount
number of references to this list
Definition: formats.h:68
#define av_err2str(errnum)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
AVFilterLink ** sink_links
Private fields.
Definition: avfilter.h:1231
#define FLAGS
Definition: avfiltergraph.c:42
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:336
unsigned nb_outputs
number of output pads
Definition: avfilter.h:646
const char * r
Definition: vf_curves.c:103
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: avcodec.h:4168
uint64_t * channel_layouts
list of channel layouts
Definition: formats.h:86
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
const char * arg
Definition: jacosubdec.c:69
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:330
static void heap_bubble_down(AVFilterGraph *graph, AVFilterLink *link, int index)
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
#define CH_DIRECT_PAIR
char all_counts
accept any channel layout or count
Definition: formats.h:89
goto fail
Definition: avfilter.c:963
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
Definition: utils.c:70
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Init a print buffer.
Definition: bprint.c:69
common internal API header
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
Definition: mpegaudioenc.c:312
#define FF_ARRAY_ELEMS(a)
Definition: avcodec.h:929
static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
Configure the formats of all the links in the graph.
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:59
Buffer to print data progressively.
Definition: bprint.h:77
char * name
name of this filter instance
Definition: avfilter.h:632
static void sanitize_channel_layouts(void *log, AVFilterChannelLayouts *l)
unsigned nb_inputs
number of input pads
Definition: avfilter.h:639
void * av_realloc(void *ptr, size_t size) 1(2)
Allocate or reallocate a block of memory.
Definition: mem.c:141
static void swap_sample_fmts_on_filter(AVFilterContext *filter)
struct AVFilterCommand * next
Definition: internal.h:48
ret
Definition: avfilter.c:961
const AVFilter * filter
the AVFilter of which this is an instance
Definition: avfilter.h:630
#define CH_CENTER_PAIR
static const AVOption filtergraph_options[]
Definition: avfiltergraph.c:43
static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
Check for the validity of graph.
static int convert(uint8_t x)
Definition: xbmdec.c:35
#define AV_BPRINT_SIZE_AUTOMATIC
Definition: bprint.h:92
AVFilterGraph * avfilter_graph_alloc(void)
Allocate a filter graph.
Definition: avfiltergraph.c:77
unsigned nb_formats
number of formats
Definition: formats.h:65
int n
Definition: avisynth_c.h:588
#define AVFILTER_CMD_FLAG_ONE
Stop once a filter understood the command (for target=all for example), fast filters are favored auto...
Definition: avfilter.h:957
#define AV_CH_FRONT_CENTER
static AVFilterFormats * clone_filter_formats(AVFilterFormats *arg)
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout (w...
Definition: formats.c:388
static int can_merge_formats(AVFilterFormats *a_arg, AVFilterFormats *b_arg, enum AVMediaType type, int is_sample_rate)
A list of supported channel layouts.
Definition: formats.h:85
int avfilter_init_str(AVFilterContext *ctx, const char *args)
Initialize a filter with the supplied parameters.
Definition: avfilter.c:855
Main libavfilter public API header.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1938
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:642
int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt, unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
Insert a filter in the middle of an existing link.
Definition: avfilter.c:182
enum AVPixelFormat avcodec_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2, enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
Find the best pixel format to convert to given a certain source pixel format and a selection of two d...
Definition: imgconvert.c:221
void ff_graph_thread_free(AVFilterGraph *graph)
Definition: avfiltergraph.c:65
int ff_default_query_formats(AVFilterContext *ctx)
Definition: formats.c:553
sample_rate
static void heap_bubble_up(AVFilterGraph *graph, AVFilterLink *link, int index)
static int formats_declared(AVFilterContext *f)
char all_layouts
accept any known channel layout
Definition: formats.h:88
AVFilterPad * input_pads
array of input pads
Definition: avfilter.h:634
AVFilterChannelLayouts * ff_merge_channel_layouts(AVFilterChannelLayouts *a, AVFilterChannelLayouts *b)
Return a channel layouts/samplerates list which contains the intersection of the layouts/samplerates ...
Definition: formats.c:166
AVFilterFormats * ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b, enum AVMediaType type)
Return a format list which contains the intersection of the formats of a and b.
Definition: formats.c:92
static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Definition: vf_drawtext.c:602
enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt)
Get the packed alternative form of the given sample format.
Definition: samplefmt.c:73
void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
Update the position of a link in the age heap.
void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
Remove a reference to a channel layouts list.
Definition: formats.c:459
int avfilter_graph_request_oldest(AVFilterGraph *graph)
Request a frame on the oldest sink link.
int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
Make the filter instance process a command.
Definition: avfilter.c:437
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:464
int index
Definition: gxfenc.c:89
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:635
#define AVFILTER_CMD_FLAG_FAST
Only execute command when its fast (like a video out that supports contrast adjustment in hw) ...
Definition: avfilter.h:958
#define CH_BACK_PAIR
AVMediaType
Definition: avutil.h:180
void ff_formats_unref(AVFilterFormats **ref)
If *ref is non-NULL, remove *ref as a reference to the format list it currently points to...
Definition: formats.c:454
unsigned nb_filters
Definition: avfilter.h:1166
#define snprintf
Definition: snprintf.h:34
AVFilter * avfilter_get_by_name(const char *name)
Get a filter definition matching the given name.
Definition: avfilter.c:460
#define type
static void swap_channel_layouts_on_filter(AVFilterContext *filter)
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:382
#define FFABS(a)
Definition: avcodec.h:920
static const int8_t filt[NUMTAPS]
Definition: af_earwax.c:39
static int flags
Definition: cpu.c:45
#define AVERROR_EOF
#define AV_CH_BACK_CENTER
static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
Perform one round of query_formats() and merging formats lists on the filter graph.
void ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:519
int ff_graph_thread_init(AVFilterGraph *graph)
Definition: avfiltergraph.c:69
static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
Configure all the links of graphctx.
static void swap_channel_layouts(AVFilterGraph *graph)
const AVClass * av_class
Definition: avfilter.h:1159
#define MERGE_DISPATCH(field, statement)
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:78
int nb_channel_layouts
number of channel layouts
Definition: formats.h:87
#define CH_FRONT_PAIR
static void reduce_formats(AVFilterGraph *graph)
struct AVFilterGraph * graph
filtergraph this filter belongs to
Definition: avfilter.h:650
int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
Send a command to one or more filter instances.
unsigned bps
Definition: movenc.c:962
static void swap_samplerates_on_filter(AVFilterContext *filter)
AVFilterContext ** filters
Definition: avfilter.h:1164
void av_bprintf(AVBPrint *buf, const char *fmt,...) av_printf_format(2
Append a formatted string to a print buffer.
void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
Remove a filter from a graph;.
Definition: avfiltergraph.c:95
avfilter_execute_func * thread_execute
Definition: internal.h:149
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:79
AVFilterPad * output_pads
array of output pads
Definition: avfilter.h:641
A list of supported formats for one end of a filter link.
Definition: formats.h:64
struct AVFilterCommand * command_queue
Definition: avfilter.h:675
#define AVERROR(e)
static const AVClass filtergraph_class
Definition: avfiltergraph.c:56
avfilter_execute_func * execute
This callback may be set by the caller immediately after allocating the graph and before adding any f...
Definition: avfilter.h:1220
An instance of a filter.
Definition: avfilter.h:627
#define CH_SIDE_PAIR
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
double time
time expressed in seconds
Definition: internal.h:44
void ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:512
int needs_fifo
The filter expects a fifo to be inserted on its input link, typically because it has a delay...
Definition: internal.h:143
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:335
static int filter_query_formats(AVFilterContext *ctx)
AVFilterFormats * ff_merge_samplerates(AVFilterFormats *a, AVFilterFormats *b)
Definition: formats.c:139
internal API functions
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:104
static int ff_avfilter_graph_config_pointers(AVFilterGraph *graph, AVClass *log_ctx)
#define CH_WIDE_PAIR
AVFilterContext * avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
Get a filter instance with name name from graph.
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:241
unsigned disable_auto_convert
Definition: avfilter.h:1234
int * formats
list of media formats
Definition: formats.h:66