FFmpeg
formats.c
Go to the documentation of this file.
1 /*
2  * Filter layer - format negotiation
3  * Copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/avassert.h"
24 #include "libavutil/common.h"
25 #include "libavutil/eval.h"
26 #include "libavutil/pixdesc.h"
27 #include "avfilter.h"
28 #include "internal.h"
29 #include "formats.h"
30 
31 #define KNOWN(l) (!FF_LAYOUT2COUNT(l)) /* for readability */
32 
33 /**
34  * Add all refs from a to ret and destroy a.
35  */
36 #define MERGE_REF(ret, a, fmts, type, fail_statement) \
37 do { \
38  type ***tmp; \
39  int i; \
40  \
41  if (!(tmp = av_realloc_array(ret->refs, ret->refcount + a->refcount, \
42  sizeof(*tmp)))) \
43  { fail_statement } \
44  ret->refs = tmp; \
45  \
46  for (i = 0; i < a->refcount; i ++) { \
47  ret->refs[ret->refcount] = a->refs[i]; \
48  *ret->refs[ret->refcount++] = ret; \
49  } \
50  \
51  av_freep(&a->refs); \
52  av_freep(&a->fmts); \
53  av_freep(&a); \
54 } while (0)
55 
56 /**
57  * Add all formats common to a and b to a, add b's refs to a and destroy b.
58  * If check is set, nothing is modified and it is only checked whether
59  * the formats are compatible.
60  * If empty_allowed is set and one of a,b->nb is zero, the lists are
61  * merged; otherwise, 0 (for nonmergeability) is returned.
62  */
63 #define MERGE_FORMATS(a, b, fmts, nb, type, check, empty_allowed) \
64 do { \
65  int i, j, k = 0, skip = 0; \
66  \
67  if (empty_allowed) { \
68  if (!a->nb || !b->nb) { \
69  if (check) \
70  return 1; \
71  if (!a->nb) \
72  FFSWAP(type *, a, b); \
73  skip = 1; \
74  } \
75  } \
76  if (!skip) { \
77  for (i = 0; i < a->nb; i++) \
78  for (j = 0; j < b->nb; j++) \
79  if (a->fmts[i] == b->fmts[j]) { \
80  if (check) \
81  return 1; \
82  a->fmts[k++] = a->fmts[i]; \
83  break; \
84  } \
85  /* Check that there was at least one common format. \
86  * Notice that both a and b are unchanged if not. */ \
87  if (!k) \
88  return 0; \
89  av_assert2(!check); \
90  a->nb = k; \
91  } \
92  \
93  MERGE_REF(a, b, fmts, type, return AVERROR(ENOMEM);); \
94 } while (0)
95 
97  enum AVMediaType type, int check)
98 {
99  int i, j;
100  int alpha1=0, alpha2=0;
101  int chroma1=0, chroma2=0;
102 
103  av_assert2(check || (a->refcount && b->refcount));
104 
105  if (a == b)
106  return 1;
107 
108  /* Do not lose chroma or alpha in merging.
109  It happens if both lists have formats with chroma (resp. alpha), but
110  the only formats in common do not have it (e.g. YUV+gray vs.
111  RGB+gray): in that case, the merging would select the gray format,
112  possibly causing a lossy conversion elsewhere in the graph.
113  To avoid that, pretend that there are no common formats to force the
114  insertion of a conversion filter. */
115  if (type == AVMEDIA_TYPE_VIDEO)
116  for (i = 0; i < a->nb_formats; i++) {
117  const AVPixFmtDescriptor *const adesc = av_pix_fmt_desc_get(a->formats[i]);
118  for (j = 0; j < b->nb_formats; j++) {
119  const AVPixFmtDescriptor *bdesc = av_pix_fmt_desc_get(b->formats[j]);
120  alpha2 |= adesc->flags & bdesc->flags & AV_PIX_FMT_FLAG_ALPHA;
121  chroma2|= adesc->nb_components > 1 && bdesc->nb_components > 1;
122  if (a->formats[i] == b->formats[j]) {
123  alpha1 |= adesc->flags & AV_PIX_FMT_FLAG_ALPHA;
124  chroma1|= adesc->nb_components > 1;
125  }
126  }
127  }
128 
129  // If chroma or alpha can be lost through merging then do not merge
130  if (alpha2 > alpha1 || chroma2 > chroma1)
131  return 0;
132 
133  MERGE_FORMATS(a, b, formats, nb_formats, AVFilterFormats, check, 0);
134 
135  return 1;
136 }
137 
138 
139 /**
140  * Check the formats lists for compatibility for merging without actually
141  * merging.
142  *
143  * @return 1 if they are compatible, 0 if not.
144  */
145 static int can_merge_pix_fmts(const void *a, const void *b)
146 {
149 }
150 
151 /**
152  * Merge the formats lists if they are compatible and update all the
153  * references of a and b to point to the combined list and free the old
154  * lists as needed. The combined list usually contains the intersection of
155  * the lists of a and b.
156  *
157  * Both a and b must have owners (i.e. refcount > 0) for these functions.
158  *
159  * @return 1 if merging succeeded, 0 if a and b are incompatible
160  * and negative AVERROR code on failure.
161  * a and b are unmodified if 0 is returned.
162  */
163 static int merge_pix_fmts(void *a, void *b)
164 {
166 }
167 
168 /**
169  * See can_merge_pix_fmts().
170  */
171 static int can_merge_sample_fmts(const void *a, const void *b)
172 {
175 }
176 
177 /**
178  * See merge_pix_fmts().
179  */
180 static int merge_sample_fmts(void *a, void *b)
181 {
183 }
184 
186  AVFilterFormats *b, int check)
187 {
188  av_assert2(check || (a->refcount && b->refcount));
189  if (a == b) return 1;
190 
191  MERGE_FORMATS(a, b, formats, nb_formats, AVFilterFormats, check, 1);
192  return 1;
193 }
194 
195 /**
196  * See can_merge_pix_fmts().
197  */
198 static int can_merge_samplerates(const void *a, const void *b)
199 {
201 }
202 
203 /**
204  * See merge_pix_fmts().
205  */
206 static int merge_samplerates(void *a, void *b)
207 {
208  return merge_samplerates_internal(a, b, 0);
209 }
210 
211 /**
212  * See merge_pix_fmts().
213  */
214 static int merge_channel_layouts(void *va, void *vb)
215 {
218  uint64_t *channel_layouts;
219  unsigned a_all = a->all_layouts + a->all_counts;
220  unsigned b_all = b->all_layouts + b->all_counts;
221  int ret_max, ret_nb = 0, i, j, round;
222 
223  av_assert2(a->refcount && b->refcount);
224 
225  if (a == b) return 1;
226 
227  /* Put the most generic set in a, to avoid doing everything twice */
228  if (a_all < b_all) {
230  FFSWAP(unsigned, a_all, b_all);
231  }
232  if (a_all) {
233  if (a_all == 1 && !b_all) {
234  /* keep only known layouts in b; works also for b_all = 1 */
235  for (i = j = 0; i < b->nb_channel_layouts; i++)
236  if (KNOWN(b->channel_layouts[i]))
237  b->channel_layouts[j++] = b->channel_layouts[i];
238  /* Not optimal: the unknown layouts of b may become known after
239  another merge. */
240  if (!j)
241  return 0;
242  b->nb_channel_layouts = j;
243  }
245  return 1;
246  }
247 
248  ret_max = a->nb_channel_layouts + b->nb_channel_layouts;
249  if (!(channel_layouts = av_malloc_array(ret_max, sizeof(*channel_layouts))))
250  return AVERROR(ENOMEM);
251 
252  /* a[known] intersect b[known] */
253  for (i = 0; i < a->nb_channel_layouts; i++) {
254  if (!KNOWN(a->channel_layouts[i]))
255  continue;
256  for (j = 0; j < b->nb_channel_layouts; j++) {
257  if (a->channel_layouts[i] == b->channel_layouts[j]) {
258  channel_layouts[ret_nb++] = a->channel_layouts[i];
259  a->channel_layouts[i] = b->channel_layouts[j] = 0;
260  break;
261  }
262  }
263  }
264  /* 1st round: a[known] intersect b[generic]
265  2nd round: a[generic] intersect b[known] */
266  for (round = 0; round < 2; round++) {
267  for (i = 0; i < a->nb_channel_layouts; i++) {
268  uint64_t fmt = a->channel_layouts[i], bfmt;
269  if (!fmt || !KNOWN(fmt))
270  continue;
272  for (j = 0; j < b->nb_channel_layouts; j++)
273  if (b->channel_layouts[j] == bfmt)
274  channel_layouts[ret_nb++] = a->channel_layouts[i];
275  }
276  /* 1st round: swap to prepare 2nd round; 2nd round: put it back */
278  }
279  /* a[generic] intersect b[generic] */
280  for (i = 0; i < a->nb_channel_layouts; i++) {
281  if (KNOWN(a->channel_layouts[i]))
282  continue;
283  for (j = 0; j < b->nb_channel_layouts; j++)
284  if (a->channel_layouts[i] == b->channel_layouts[j])
285  channel_layouts[ret_nb++] = a->channel_layouts[i];
286  }
287 
288  if (!ret_nb) {
290  return 0;
291  }
292 
293  if (a->refcount > b->refcount)
295 
297  { av_free(channel_layouts); return AVERROR(ENOMEM); });
298  av_freep(&b->channel_layouts);
299  b->channel_layouts = channel_layouts;
300  b->nb_channel_layouts = ret_nb;
301  return 1;
302 }
303 
304 static const AVFilterFormatsMerger mergers_video[] = {
305  {
306  .offset = offsetof(AVFilterFormatsConfig, formats),
307  .merge = merge_pix_fmts,
308  .can_merge = can_merge_pix_fmts,
309  },
310 };
311 
312 static const AVFilterFormatsMerger mergers_audio[] = {
313  {
314  .offset = offsetof(AVFilterFormatsConfig, channel_layouts),
315  .merge = merge_channel_layouts,
316  .can_merge = NULL,
317  },
318  {
319  .offset = offsetof(AVFilterFormatsConfig, samplerates),
320  .merge = merge_samplerates,
321  .can_merge = can_merge_samplerates,
322  },
323  {
324  .offset = offsetof(AVFilterFormatsConfig, formats),
325  .merge = merge_sample_fmts,
326  .can_merge = can_merge_sample_fmts,
327  },
328 };
329 
330 static const AVFilterNegotiation negotiate_video = {
332  .mergers = mergers_video,
333  .conversion_filter = "scale",
334  .conversion_opts_offset = offsetof(AVFilterGraph, scale_sws_opts),
335 };
336 
337 static const AVFilterNegotiation negotiate_audio = {
339  .mergers = mergers_audio,
340  .conversion_filter = "aresample",
341  .conversion_opts_offset = offsetof(AVFilterGraph, aresample_swr_opts),
342 };
343 
345 {
346  switch (link->type) {
347  case AVMEDIA_TYPE_VIDEO: return &negotiate_video;
348  case AVMEDIA_TYPE_AUDIO: return &negotiate_audio;
349  default: return NULL;
350  }
351 }
352 
353 int ff_fmt_is_in(int fmt, const int *fmts)
354 {
355  const int *p;
356 
357  for (p = fmts; *p != -1; p++) {
358  if (fmt == *p)
359  return 1;
360  }
361  return 0;
362 }
363 
364 #define MAKE_FORMAT_LIST(type, field, count_field) \
365  type *formats; \
366  int count = 0; \
367  if (fmts) \
368  for (count = 0; fmts[count] != -1; count++) \
369  ; \
370  formats = av_mallocz(sizeof(*formats)); \
371  if (!formats) \
372  return NULL; \
373  formats->count_field = count; \
374  if (count) { \
375  formats->field = av_malloc_array(count, sizeof(*formats->field)); \
376  if (!formats->field) { \
377  av_freep(&formats); \
378  return NULL; \
379  } \
380  }
381 
382 AVFilterFormats *ff_make_format_list(const int *fmts)
383 {
385  while (count--)
386  formats->formats[count] = fmts[count];
387 
388  return formats;
389 }
390 
391 AVFilterChannelLayouts *ff_make_format64_list(const int64_t *fmts)
392 {
394  channel_layouts, nb_channel_layouts);
395  if (count)
396  memcpy(formats->channel_layouts, fmts,
397  sizeof(*formats->channel_layouts) * count);
398 
399  return formats;
400 }
401 
402 #define ADD_FORMAT(f, fmt, unref_fn, type, list, nb) \
403 do { \
404  type *fmts; \
405  \
406  if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) { \
407  return AVERROR(ENOMEM); \
408  } \
409  \
410  fmts = av_realloc_array((*f)->list, (*f)->nb + 1, \
411  sizeof(*(*f)->list)); \
412  if (!fmts) { \
413  unref_fn(f); \
414  return AVERROR(ENOMEM); \
415  } \
416  \
417  (*f)->list = fmts; \
418  (*f)->list[(*f)->nb++] = fmt; \
419 } while (0)
420 
421 int ff_add_format(AVFilterFormats **avff, int64_t fmt)
422 {
423  ADD_FORMAT(avff, fmt, ff_formats_unref, int, formats, nb_formats);
424  return 0;
425 }
426 
427 int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
428 {
429  av_assert1(!(*l && (*l)->all_layouts));
430  ADD_FORMAT(l, channel_layout, ff_channel_layouts_unref, uint64_t, channel_layouts, nb_channel_layouts);
431  return 0;
432 }
433 
435 {
436  int fmts[2] = { fmt, -1 };
437  return ff_make_format_list(fmts);
438 }
439 
441 {
443 
444  if (type == AVMEDIA_TYPE_VIDEO) {
445  return ff_formats_pixdesc_filter(0, 0);
446  } else if (type == AVMEDIA_TYPE_AUDIO) {
447  enum AVSampleFormat fmt = 0;
448  while (av_get_sample_fmt_name(fmt)) {
449  if (ff_add_format(&ret, fmt) < 0)
450  return NULL;
451  fmt++;
452  }
453  }
454 
455  return ret;
456 }
457 
458 AVFilterFormats *ff_formats_pixdesc_filter(unsigned want, unsigned rej)
459 {
460  unsigned nb_formats, fmt, flags;
462 
463  while (1) {
464  nb_formats = 0;
465  for (fmt = 0;; fmt++) {
467  if (!desc)
468  break;
469  flags = desc->flags;
470  if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
471  !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
472  (desc->log2_chroma_w || desc->log2_chroma_h))
474  if ((flags & (want | rej)) != want)
475  continue;
476  if (formats)
477  formats->formats[nb_formats] = fmt;
478  nb_formats++;
479  }
480  if (formats) {
481  av_assert0(formats->nb_formats == nb_formats);
482  return formats;
483  }
484  formats = av_mallocz(sizeof(*formats));
485  if (!formats)
486  return NULL;
487  formats->nb_formats = nb_formats;
488  if (nb_formats) {
489  formats->formats = av_malloc_array(nb_formats, sizeof(*formats->formats));
490  if (!formats->formats) {
491  av_freep(&formats);
492  return NULL;
493  }
494  }
495  }
496 }
497 
499 {
501  int fmt;
502 
503  for (fmt = 0; av_get_bytes_per_sample(fmt)>0; fmt++)
504  if (av_sample_fmt_is_planar(fmt))
505  if (ff_add_format(&ret, fmt) < 0)
506  return NULL;
507 
508  return ret;
509 }
510 
512 {
513  AVFilterFormats *ret = av_mallocz(sizeof(*ret));
514  return ret;
515 }
516 
518 {
520  if (!ret)
521  return NULL;
522  ret->all_layouts = 1;
523  return ret;
524 }
525 
527 {
529  if (!ret)
530  return NULL;
531  ret->all_layouts = ret->all_counts = 1;
532  return ret;
533 }
534 
535 #define FORMATS_REF(f, ref, unref_fn) \
536  void *tmp; \
537  \
538  if (!f) \
539  return AVERROR(ENOMEM); \
540  \
541  tmp = av_realloc_array(f->refs, sizeof(*f->refs), f->refcount + 1); \
542  if (!tmp) { \
543  unref_fn(&f); \
544  return AVERROR(ENOMEM); \
545  } \
546  f->refs = tmp; \
547  f->refs[f->refcount++] = ref; \
548  *ref = f; \
549  return 0
550 
552 {
554 }
555 
557 {
559 }
560 
561 #define FIND_REF_INDEX(ref, idx) \
562 do { \
563  int i; \
564  for (i = 0; i < (*ref)->refcount; i ++) \
565  if((*ref)->refs[i] == ref) { \
566  idx = i; \
567  break; \
568  } \
569 } while (0)
570 
571 #define FORMATS_UNREF(ref, list) \
572 do { \
573  int idx = -1; \
574  \
575  if (!*ref) \
576  return; \
577  \
578  FIND_REF_INDEX(ref, idx); \
579  \
580  if (idx >= 0) { \
581  memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
582  sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
583  --(*ref)->refcount; \
584  } \
585  if (!(*ref)->refcount) { \
586  av_free((*ref)->list); \
587  av_free((*ref)->refs); \
588  av_free(*ref); \
589  } \
590  *ref = NULL; \
591 } while (0)
592 
594 {
596 }
597 
599 {
601 }
602 
603 #define FORMATS_CHANGEREF(oldref, newref) \
604 do { \
605  int idx = -1; \
606  \
607  FIND_REF_INDEX(oldref, idx); \
608  \
609  if (idx >= 0) { \
610  (*oldref)->refs[idx] = newref; \
611  *newref = *oldref; \
612  *oldref = NULL; \
613  } \
614 } while (0)
615 
617  AVFilterChannelLayouts **newref)
618 {
619  FORMATS_CHANGEREF(oldref, newref);
620 }
621 
623 {
624  FORMATS_CHANGEREF(oldref, newref);
625 }
626 
627 #define SET_COMMON_FORMATS(ctx, fmts, media_type, ref_fn, unref_fn) \
628  int i; \
629  \
630  if (!fmts) \
631  return AVERROR(ENOMEM); \
632  \
633  for (i = 0; i < ctx->nb_inputs; i++) { \
634  AVFilterLink *const link = ctx->inputs[i]; \
635  if (link && !link->outcfg.fmts && \
636  (media_type == AVMEDIA_TYPE_UNKNOWN || link->type == media_type)) { \
637  int ret = ref_fn(fmts, &ctx->inputs[i]->outcfg.fmts); \
638  if (ret < 0) { \
639  return ret; \
640  } \
641  } \
642  } \
643  for (i = 0; i < ctx->nb_outputs; i++) { \
644  AVFilterLink *const link = ctx->outputs[i]; \
645  if (link && !link->incfg.fmts && \
646  (media_type == AVMEDIA_TYPE_UNKNOWN || link->type == media_type)) { \
647  int ret = ref_fn(fmts, &ctx->outputs[i]->incfg.fmts); \
648  if (ret < 0) { \
649  return ret; \
650  } \
651  } \
652  } \
653  \
654  if (!fmts->refcount) \
655  unref_fn(&fmts); \
656  \
657  return 0;
658 
661 {
664 }
665 
667  const int64_t *fmts)
668 {
670 }
671 
673 {
675 }
676 
678  AVFilterFormats *samplerates)
679 {
682 }
683 
685  const int *samplerates)
686 {
687  return ff_set_common_samplerates(ctx, ff_make_format_list(samplerates));
688 }
689 
691 {
693 }
694 
695 /**
696  * A helper for query_formats() which sets all links to the same list of
697  * formats. If there are no links hooked to this filter, the list of formats is
698  * freed.
699  */
701 {
704 }
705 
707 {
709 }
710 
712 {
713  const AVFilter *const f = ctx->filter;
715  enum AVMediaType type;
716  int ret;
717 
718  switch (f->formats_state) {
721  formats = ff_make_format_list(f->formats.pixels_list);
722  break;
725  formats = ff_make_format_list(f->formats.samples_list);
726  break;
729  formats = ff_make_formats_list_singleton(f->formats.pix_fmt);
730  break;
733  formats = ff_make_formats_list_singleton(f->formats.sample_fmt);
734  break;
735  default:
736  av_assert2(!"Unreachable");
737  /* Intended fallthrough */
740  type = ctx->nb_inputs ? ctx->inputs [0]->type :
741  ctx->nb_outputs ? ctx->outputs[0]->type : AVMEDIA_TYPE_VIDEO;
743  break;
744  }
745 
747  if (ret < 0)
748  return ret;
749  if (type == AVMEDIA_TYPE_AUDIO) {
751  if (ret < 0)
752  return ret;
754  if (ret < 0)
755  return ret;
756  }
757 
758  return 0;
759 }
760 
761 /* internal functions for parsing audio format arguments */
762 
763 int ff_parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx)
764 {
765  char *tail;
766  int pix_fmt = av_get_pix_fmt(arg);
767  if (pix_fmt == AV_PIX_FMT_NONE) {
768  pix_fmt = strtol(arg, &tail, 0);
769  if (*tail || !av_pix_fmt_desc_get(pix_fmt)) {
770  av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
771  return AVERROR(EINVAL);
772  }
773  }
774  *ret = pix_fmt;
775  return 0;
776 }
777 
778 int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
779 {
780  char *tail;
781  double srate = av_strtod(arg, &tail);
782  if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
783  av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
784  return AVERROR(EINVAL);
785  }
786  *ret = srate;
787  return 0;
788 }
789 
790 int ff_parse_channel_layout(int64_t *ret, int *nret, const char *arg,
791  void *log_ctx)
792 {
793  int64_t chlayout;
794  int nb_channels;
795 
796  if (av_get_extended_channel_layout(arg, &chlayout, &nb_channels) < 0) {
797  av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
798  return AVERROR(EINVAL);
799  }
800  if (!chlayout && !nret) {
801  av_log(log_ctx, AV_LOG_ERROR, "Unknown channel layout '%s' is not supported.\n", arg);
802  return AVERROR(EINVAL);
803  }
804  *ret = chlayout;
805  if (nret)
806  *nret = nb_channels;
807 
808  return 0;
809 }
810 
811 static int check_list(void *log, const char *name, const AVFilterFormats *fmts)
812 {
813  unsigned i, j;
814 
815  if (!fmts)
816  return 0;
817  if (!fmts->nb_formats) {
818  av_log(log, AV_LOG_ERROR, "Empty %s list\n", name);
819  return AVERROR(EINVAL);
820  }
821  for (i = 0; i < fmts->nb_formats; i++) {
822  for (j = i + 1; j < fmts->nb_formats; j++) {
823  if (fmts->formats[i] == fmts->formats[j]) {
824  av_log(log, AV_LOG_ERROR, "Duplicated %s\n", name);
825  return AVERROR(EINVAL);
826  }
827  }
828  }
829  return 0;
830 }
831 
832 int ff_formats_check_pixel_formats(void *log, const AVFilterFormats *fmts)
833 {
834  return check_list(log, "pixel format", fmts);
835 }
836 
837 int ff_formats_check_sample_formats(void *log, const AVFilterFormats *fmts)
838 {
839  return check_list(log, "sample format", fmts);
840 }
841 
842 int ff_formats_check_sample_rates(void *log, const AVFilterFormats *fmts)
843 {
844  if (!fmts || !fmts->nb_formats)
845  return 0;
846  return check_list(log, "sample rate", fmts);
847 }
848 
849 static int layouts_compatible(uint64_t a, uint64_t b)
850 {
851  return a == b ||
854 }
855 
857 {
858  unsigned i, j;
859 
860  if (!fmts)
861  return 0;
862  if (fmts->all_layouts < fmts->all_counts) {
863  av_log(log, AV_LOG_ERROR, "Inconsistent generic list\n");
864  return AVERROR(EINVAL);
865  }
866  if (!fmts->all_layouts && !fmts->nb_channel_layouts) {
867  av_log(log, AV_LOG_ERROR, "Empty channel layout list\n");
868  return AVERROR(EINVAL);
869  }
870  for (i = 0; i < fmts->nb_channel_layouts; i++) {
871  for (j = i + 1; j < fmts->nb_channel_layouts; j++) {
872  if (layouts_compatible(fmts->channel_layouts[i], fmts->channel_layouts[j])) {
873  av_log(log, AV_LOG_ERROR, "Duplicated or redundant channel layout\n");
874  return AVERROR(EINVAL);
875  }
876  }
877  }
878  return 0;
879 }
formats
formats
Definition: signature.h:48
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
can_merge_sample_fmts
static int can_merge_sample_fmts(const void *a, const void *b)
See can_merge_pix_fmts().
Definition: formats.c:170
merge_formats_internal
static int merge_formats_internal(AVFilterFormats *a, AVFilterFormats *b, enum AVMediaType type, int check)
Definition: formats.c:95
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:381
can_merge_pix_fmts
static int can_merge_pix_fmts(const void *a, const void *b)
Check the formats lists for compatibility for merging without actually merging.
Definition: formats.c:144
merge_channel_layouts
static int merge_channel_layouts(void *va, void *vb)
See merge_pix_fmts().
Definition: formats.c:213
ff_channel_layouts_ref
int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
Add *ref as a new reference to f.
Definition: formats.c:550
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2564
ff_formats_check_pixel_formats
int ff_formats_check_pixel_formats(void *log, const AVFilterFormats *fmts)
Check that fmts is a valid pixel formats list.
Definition: formats.c:831
ff_set_common_samplerates_from_list
int ff_set_common_samplerates_from_list(AVFilterContext *ctx, const int *samplerates)
Equivalent to ff_set_common_samplerates(ctx, ff_make_format_list(samplerates))
Definition: formats.c:683
ff_all_channel_counts
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition.
Definition: formats.c:525
ff_make_format64_list
AVFilterChannelLayouts * ff_make_format64_list(const int64_t *fmts)
Definition: formats.c:390
pixdesc.h
b
#define b
Definition: input.c:40
mergers_video
static const AVFilterFormatsMerger mergers_video[]
Definition: formats.c:303
MERGE_FORMATS
#define MERGE_FORMATS(a, b, fmts, nb, type, check, empty_allowed)
Add all formats common to a and b to a, add b's refs to a and destroy b.
Definition: formats.c:63
ff_set_common_all_samplerates
int ff_set_common_all_samplerates(AVFilterContext *ctx)
Equivalent to ff_set_common_samplerates(ctx, ff_all_samplerates())
Definition: formats.c:689
AVFilterFormats::formats
int * formats
list of media formats
Definition: formats.h:66
merge_samplerates_internal
static int merge_samplerates_internal(AVFilterFormats *a, AVFilterFormats *b, int check)
Definition: formats.c:184
FF_FILTER_FORMATS_SAMPLEFMTS_LIST
@ FF_FILTER_FORMATS_SAMPLEFMTS_LIST
formats.samples_list active.
Definition: internal.h:163
ff_make_formats_list_singleton
AVFilterFormats * ff_make_formats_list_singleton(int fmt)
Equivalent to ff_make_format_list({const int[]}{ fmt, -1 })
Definition: formats.c:433
FORMATS_UNREF
#define FORMATS_UNREF(ref, list)
Definition: formats.c:570
ff_set_common_channel_layouts_from_list
int ff_set_common_channel_layouts_from_list(AVFilterContext *ctx, const int64_t *fmts)
Equivalent to ff_set_common_channel_layouts(ctx, ff_make_format64_list(fmts))
Definition: formats.c:665
FF_LAYOUT2COUNT
#define FF_LAYOUT2COUNT(l)
Decode a channel count encoded as a channel layout.
Definition: formats.h:108
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
formats.h
SET_COMMON_FORMATS
#define SET_COMMON_FORMATS(ctx, fmts, media_type, ref_fn, unref_fn)
Definition: formats.c:626
AV_PIX_FMT_FLAG_HWACCEL
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:128
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AVFilterNegotiation
Callbacks and properties to describe the steps of a format negotiation.
Definition: formats.h:415
ff_all_formats
AVFilterFormats * ff_all_formats(enum AVMediaType type)
Return a list of all formats supported by FFmpeg for the given media type.
Definition: formats.c:439
AVFilterNegotiation::nb_mergers
unsigned nb_mergers
Definition: formats.h:416
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
ff_set_common_formats
int 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:699
FF_FILTER_FORMATS_SINGLE_SAMPLEFMT
@ FF_FILTER_FORMATS_SINGLE_SAMPLEFMT
formats.sample_fmt active.
Definition: internal.h:165
check
#define check(x, y, S, v)
Definition: motion_est_template.c:405
ff_add_channel_layout
int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
Definition: formats.c:426
AVFilterChannelLayouts::channel_layouts
uint64_t * channel_layouts
list of channel layouts
Definition: formats.h:86
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ff_formats_ref
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:555
ff_set_common_formats_from_list
int ff_set_common_formats_from_list(AVFilterContext *ctx, const int *fmts)
Equivalent to ff_set_common_formats(ctx, ff_make_format_list(fmts))
Definition: formats.c:705
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
av_sample_fmt_is_planar
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:112
AV_PIX_FMT_FLAG_ALPHA
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition: pixdesc.h:147
ctx
AVFormatContext * ctx
Definition: movenc.c:48
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demuxing_decoding.c:41
av_get_sample_fmt_name
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
Definition: samplefmt.c:49
f
#define f(width, name)
Definition: cbs_vp9.c:255
link
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a link
Definition: filter_design.txt:23
arg
const char * arg
Definition: jacosubdec.c:67
ff_formats_changeref
void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
Definition: formats.c:621
NULL
#define NULL
Definition: coverity.c:32
ff_filter_get_negotiation
const AVFilterNegotiation * ff_filter_get_negotiation(AVFilterLink *link)
Definition: formats.c:343
MAKE_FORMAT_LIST
#define MAKE_FORMAT_LIST(type, field, count_field)
Definition: formats.c:363
merge_pix_fmts
static int merge_pix_fmts(void *a, void *b)
Merge the formats lists if they are compatible and update all the references of a and b to point to t...
Definition: formats.c:162
AVPixFmtDescriptor::nb_components
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:71
ff_add_format
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:420
AVFilterFormats::nb_formats
unsigned nb_formats
number of formats
Definition: formats.h:65
ff_fmt_is_in
int ff_fmt_is_in(int fmt, const int *fmts)
Tell if an integer is contained in the provided -1-terminated list of integers.
Definition: formats.c:352
negotiate_audio
static const AVFilterNegotiation negotiate_audio
Definition: formats.c:336
AVFilterGraph
Definition: avfilter.h:845
merge_samplerates
static int merge_samplerates(void *a, void *b)
See merge_pix_fmts().
Definition: formats.c:205
ff_set_common_all_channel_counts
int ff_set_common_all_channel_counts(AVFilterContext *ctx)
Equivalent to ff_set_common_channel_layouts(ctx, ff_all_channel_counts())
Definition: formats.c:671
AVPixFmtDescriptor::flags
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:94
ff_channel_layouts_unref
void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
Remove a reference to a channel layouts list.
Definition: formats.c:597
AVFilterFormatsConfig
Lists of formats / etc.
Definition: avfilter.h:485
av_get_channel_layout_nb_channels
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
Definition: channel_layout.c:226
ff_formats_check_sample_formats
int ff_formats_check_sample_formats(void *log, const AVFilterFormats *fmts)
Check that fmts is a valid sample formats list.
Definition: formats.c:836
eval.h
MERGE_REF
#define MERGE_REF(ret, a, fmts, type, fail_statement)
Add all refs from a to ret and destroy a.
Definition: formats.c:36
AVMediaType
AVMediaType
Definition: avutil.h:199
FF_PIX_FMT_FLAG_SW_FLAT_SUB
#define FF_PIX_FMT_FLAG_SW_FLAT_SUB
Definition: formats.h:242
ff_default_query_formats
int ff_default_query_formats(AVFilterContext *ctx)
Definition: formats.c:710
ff_parse_channel_layout
int ff_parse_channel_layout(int64_t *ret, int *nret, const char *arg, void *log_ctx)
Parse a channel layout or a corresponding integer representation.
Definition: formats.c:789
check_list
static int check_list(void *log, const char *name, const AVFilterFormats *fmts)
Definition: formats.c:810
AVFilterChannelLayouts::all_layouts
char all_layouts
accept any known channel layout
Definition: formats.h:88
AVFilterChannelLayouts::all_counts
char all_counts
accept any channel layout or count
Definition: formats.h:89
AVMEDIA_TYPE_UNKNOWN
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:200
KNOWN
#define KNOWN(l)
Definition: formats.c:31
ff_formats_check_channel_layouts
int ff_formats_check_channel_layouts(void *log, const AVFilterChannelLayouts *fmts)
Check that fmts is a valid channel layouts list.
Definition: formats.c:855
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
ff_all_channel_layouts
AVFilterChannelLayouts * ff_all_channel_layouts(void)
Construct an empty AVFilterChannelLayouts/AVFilterFormats struct – representing any channel layout (w...
Definition: formats.c:516
ADD_FORMAT
#define ADD_FORMAT(f, fmt, unref_fn, type, list, nb)
Definition: formats.c:401
internal.h
ff_formats_unref
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:592
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
ff_formats_pixdesc_filter
AVFilterFormats * ff_formats_pixdesc_filter(unsigned want, unsigned rej)
Construct a formats list containing all pixel formats with certain properties.
Definition: formats.c:457
FORMATS_REF
#define FORMATS_REF(f, ref, unref_fn)
Definition: formats.c:534
ff_formats_check_sample_rates
int ff_formats_check_sample_rates(void *log, const AVFilterFormats *fmts)
Check that fmts is a valid sample rates list.
Definition: formats.c:841
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
round
static av_always_inline av_const double round(double x)
Definition: libm.h:444
av_get_bytes_per_sample
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
FF_FILTER_FORMATS_PASSTHROUGH
@ FF_FILTER_FORMATS_PASSTHROUGH
The default value meaning that this filter supports all formats and (for audio) sample rates and chan...
Definition: internal.h:160
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
common.h
FORMATS_CHANGEREF
#define FORMATS_CHANGEREF(oldref, newref)
Definition: formats.c:602
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:263
ff_planar_sample_fmts
AVFilterFormats * ff_planar_sample_fmts(void)
Construct a formats list containing all planar sample formats.
Definition: formats.c:497
mergers_audio
static const AVFilterFormatsMerger mergers_audio[]
Definition: formats.c:311
AVFilter
Filter definition.
Definition: avfilter.h:149
ret
ret
Definition: filter_design.txt:187
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
av_strtod
double av_strtod(const char *numstr, char **tail)
Parse the string in numstr and return its value as a double.
Definition: eval.c:106
FF_COUNT2LAYOUT
#define FF_COUNT2LAYOUT(c)
Encode a channel count as a channel layout.
Definition: formats.h:102
av_get_pix_fmt
enum AVPixelFormat av_get_pix_fmt(const char *name)
Return the pixel format corresponding to name.
Definition: pixdesc.c:2496
ff_all_samplerates
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:510
channel_layout.h
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
avfilter.h
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
AV_PIX_FMT_FLAG_PLANAR
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:132
can_merge_samplerates
static int can_merge_samplerates(const void *a, const void *b)
See can_merge_pix_fmts().
Definition: formats.c:197
av_get_extended_channel_layout
int av_get_extended_channel_layout(const char *name, uint64_t *channel_layout, int *nb_channels)
Return a channel layout and the number of channels based on the specified name.
Definition: channel_layout.c:161
AVFilterContext
An instance of a filter.
Definition: avfilter.h:386
layouts_compatible
static int layouts_compatible(uint64_t a, uint64_t b)
Definition: formats.c:848
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVFilterChannelLayouts::nb_channel_layouts
int nb_channel_layouts
number of channel layouts
Definition: formats.h:87
FF_FILTER_FORMATS_PIXFMT_LIST
@ FF_FILTER_FORMATS_PIXFMT_LIST
formats.pixels_list active.
Definition: internal.h:162
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
channel_layouts
static const uint16_t channel_layouts[7]
Definition: dca_lbr.c:114
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
negotiate_video
static const AVFilterNegotiation negotiate_video
Definition: formats.c:329
FF_FILTER_FORMATS_QUERY_FUNC
@ FF_FILTER_FORMATS_QUERY_FUNC
formats.query active.
Definition: internal.h:161
ff_parse_sample_rate
int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
Parse a sample rate.
Definition: formats.c:777
ff_parse_pixel_format
int ff_parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx)
Parse a pixel format.
Definition: formats.c:762
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ff_set_common_samplerates
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:676
ff_channel_layouts_changeref
void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref, AVFilterChannelLayouts **newref)
Definition: formats.c:615
merge_sample_fmts
static int merge_sample_fmts(void *a, void *b)
See merge_pix_fmts().
Definition: formats.c:179
FF_FILTER_FORMATS_SINGLE_PIXFMT
@ FF_FILTER_FORMATS_SINGLE_PIXFMT
formats.pix_fmt active
Definition: internal.h:164
ff_set_common_channel_layouts
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *channel_layouts)
Helpers for query_formats() which set all free audio links to the same list of channel layouts/sample...
Definition: formats.c:658
nb_channels
int nb_channels
Definition: channel_layout.c:81