2 * Filter layer - format negotiation
3 * Copyright (c) 2007 Bobby Bingham
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/avassert.h"
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/common.h"
25 #include "libavutil/eval.h"
26 #include "libavutil/pixdesc.h"
31 #define KNOWN(l) (!FF_LAYOUT2COUNT(l)) /* for readability */
34 * Add all refs from a to ret and destroy a.
36 #define MERGE_REF(ret, a, fmts, type, fail_statement) \
41 if (!(tmp = av_realloc_array(ret->refs, ret->refcount + a->refcount, \
46 for (i = 0; i < a->refcount; i ++) { \
47 ret->refs[ret->refcount] = a->refs[i]; \
48 *ret->refs[ret->refcount++] = ret; \
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, it is treated as error.
63 #define MERGE_FORMATS(a, b, fmts, nb, type, check, empty_allowed) \
65 int i, j, k = 0, skip = 0; \
67 if (empty_allowed) { \
68 if (!a->nb || !b->nb) { \
72 FFSWAP(type *, a, b); \
77 for (i = 0; i < a->nb; i++) \
78 for (j = 0; j < b->nb; j++) \
79 if (a->fmts[i] == b->fmts[j]) { \
82 a->fmts[k++] = a->fmts[i]; \
85 /* Check that there was at least one common format. \
86 * Notice that both a and b are unchanged if not. */ \
93 MERGE_REF(a, b, fmts, type, return AVERROR(ENOMEM);); \
96 static int merge_formats_internal(AVFilterFormats *a, AVFilterFormats *b,
97 enum AVMediaType type, int check)
100 int alpha1=0, alpha2=0;
101 int chroma1=0, chroma2=0;
106 /* Do not lose chroma or alpha in merging.
107 It happens if both lists have formats with chroma (resp. alpha), but
108 the only formats in common do not have it (e.g. YUV+gray vs.
109 RGB+gray): in that case, the merging would select the gray format,
110 possibly causing a lossy conversion elsewhere in the graph.
111 To avoid that, pretend that there are no common formats to force the
112 insertion of a conversion filter. */
113 if (type == AVMEDIA_TYPE_VIDEO)
114 for (i = 0; i < a->nb_formats; i++)
115 for (j = 0; j < b->nb_formats; j++) {
116 const AVPixFmtDescriptor *adesc = av_pix_fmt_desc_get(a->formats[i]);
117 const AVPixFmtDescriptor *bdesc = av_pix_fmt_desc_get(b->formats[j]);
118 alpha2 |= adesc->flags & bdesc->flags & AV_PIX_FMT_FLAG_ALPHA;
119 chroma2|= adesc->nb_components > 1 && bdesc->nb_components > 1;
120 if (a->formats[i] == b->formats[j]) {
121 alpha1 |= adesc->flags & AV_PIX_FMT_FLAG_ALPHA;
122 chroma1|= adesc->nb_components > 1;
126 // If chroma or alpha can be lost through merging then do not merge
127 if (alpha2 > alpha1 || chroma2 > chroma1)
130 MERGE_FORMATS(a, b, formats, nb_formats, AVFilterFormats, check, 0);
135 int ff_can_merge_formats(const AVFilterFormats *a, const AVFilterFormats *b,
136 enum AVMediaType type)
138 return merge_formats_internal((AVFilterFormats *)a,
139 (AVFilterFormats *)b, type, 1);
142 int ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b,
143 enum AVMediaType type)
145 av_assert2(a->refcount && b->refcount);
146 return merge_formats_internal(a, b, type, 0);
149 static int merge_samplerates_internal(AVFilterFormats *a,
150 AVFilterFormats *b, int check)
152 if (a == b) return 1;
154 MERGE_FORMATS(a, b, formats, nb_formats, AVFilterFormats, check, 1);
158 int ff_can_merge_samplerates(const AVFilterFormats *a, const AVFilterFormats *b)
160 return merge_samplerates_internal((AVFilterFormats *)a, (AVFilterFormats *)b, 1);
163 int ff_merge_samplerates(AVFilterFormats *a, AVFilterFormats *b)
165 av_assert2(a->refcount && b->refcount);
166 return merge_samplerates_internal(a, b, 0);
169 int ff_merge_channel_layouts(AVFilterChannelLayouts *a,
170 AVFilterChannelLayouts *b)
172 uint64_t *channel_layouts;
173 unsigned a_all = a->all_layouts + a->all_counts;
174 unsigned b_all = b->all_layouts + b->all_counts;
175 int ret_max, ret_nb = 0, i, j, round;
177 av_assert2(a->refcount && b->refcount);
179 if (a == b) return 1;
181 /* Put the most generic set in a, to avoid doing everything twice */
183 FFSWAP(AVFilterChannelLayouts *, a, b);
184 FFSWAP(unsigned, a_all, b_all);
187 if (a_all == 1 && !b_all) {
188 /* keep only known layouts in b; works also for b_all = 1 */
189 for (i = j = 0; i < b->nb_channel_layouts; i++)
190 if (KNOWN(b->channel_layouts[i]))
191 b->channel_layouts[j++] = b->channel_layouts[i];
192 /* Not optimal: the unknown layouts of b may become known after
196 b->nb_channel_layouts = j;
198 MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, return AVERROR(ENOMEM););
202 ret_max = a->nb_channel_layouts + b->nb_channel_layouts;
203 if (!(channel_layouts = av_malloc_array(ret_max, sizeof(*channel_layouts))))
204 return AVERROR(ENOMEM);
206 /* a[known] intersect b[known] */
207 for (i = 0; i < a->nb_channel_layouts; i++) {
208 if (!KNOWN(a->channel_layouts[i]))
210 for (j = 0; j < b->nb_channel_layouts; j++) {
211 if (a->channel_layouts[i] == b->channel_layouts[j]) {
212 channel_layouts[ret_nb++] = a->channel_layouts[i];
213 a->channel_layouts[i] = b->channel_layouts[j] = 0;
218 /* 1st round: a[known] intersect b[generic]
219 2nd round: a[generic] intersect b[known] */
220 for (round = 0; round < 2; round++) {
221 for (i = 0; i < a->nb_channel_layouts; i++) {
222 uint64_t fmt = a->channel_layouts[i], bfmt;
223 if (!fmt || !KNOWN(fmt))
225 bfmt = FF_COUNT2LAYOUT(av_get_channel_layout_nb_channels(fmt));
226 for (j = 0; j < b->nb_channel_layouts; j++)
227 if (b->channel_layouts[j] == bfmt)
228 channel_layouts[ret_nb++] = a->channel_layouts[i];
230 /* 1st round: swap to prepare 2nd round; 2nd round: put it back */
231 FFSWAP(AVFilterChannelLayouts *, a, b);
233 /* a[generic] intersect b[generic] */
234 for (i = 0; i < a->nb_channel_layouts; i++) {
235 if (KNOWN(a->channel_layouts[i]))
237 for (j = 0; j < b->nb_channel_layouts; j++)
238 if (a->channel_layouts[i] == b->channel_layouts[j])
239 channel_layouts[ret_nb++] = a->channel_layouts[i];
243 av_free(channel_layouts);
247 if (a->refcount > b->refcount)
248 FFSWAP(AVFilterChannelLayouts *, a, b);
250 MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts,
251 { av_free(channel_layouts); return AVERROR(ENOMEM); });
252 av_freep(&b->channel_layouts);
253 b->channel_layouts = channel_layouts;
254 b->nb_channel_layouts = ret_nb;
258 int ff_fmt_is_in(int fmt, const int *fmts)
262 for (p = fmts; *p != -1; p++) {
269 #define MAKE_FORMAT_LIST(type, field, count_field) \
273 for (count = 0; fmts[count] != -1; count++) \
275 formats = av_mallocz(sizeof(*formats)); \
278 formats->count_field = count; \
280 formats->field = av_malloc_array(count, sizeof(*formats->field)); \
281 if (!formats->field) { \
282 av_freep(&formats); \
287 AVFilterFormats *ff_make_format_list(const int *fmts)
289 MAKE_FORMAT_LIST(AVFilterFormats, formats, nb_formats);
291 formats->formats[count] = fmts[count];
296 AVFilterChannelLayouts *ff_make_format64_list(const int64_t *fmts)
298 MAKE_FORMAT_LIST(AVFilterChannelLayouts,
299 channel_layouts, nb_channel_layouts);
301 memcpy(formats->channel_layouts, fmts,
302 sizeof(*formats->channel_layouts) * count);
307 #if LIBAVFILTER_VERSION_MAJOR < 8
308 AVFilterChannelLayouts *avfilter_make_format64_list(const int64_t *fmts)
310 return ff_make_format64_list(fmts);
314 #define ADD_FORMAT(f, fmt, unref_fn, type, list, nb) \
319 if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) { \
320 return AVERROR(ENOMEM); \
323 fmts = av_realloc_array((*f)->list, (*f)->nb + 1, \
324 sizeof(*(*f)->list)); \
329 return AVERROR(ENOMEM); \
333 (*f)->list[(*f)->nb++] = fmt; \
336 int ff_add_format(AVFilterFormats **avff, int64_t fmt)
338 ADD_FORMAT(avff, fmt, ff_formats_unref, int, formats, nb_formats);
342 int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
344 av_assert1(!(*l && (*l)->all_layouts));
345 ADD_FORMAT(l, channel_layout, ff_channel_layouts_unref, uint64_t, channel_layouts, nb_channel_layouts);
349 AVFilterFormats *ff_all_formats(enum AVMediaType type)
351 AVFilterFormats *ret = NULL;
353 if (type == AVMEDIA_TYPE_VIDEO) {
354 const AVPixFmtDescriptor *desc = NULL;
355 while ((desc = av_pix_fmt_desc_next(desc))) {
356 if (ff_add_format(&ret, av_pix_fmt_desc_get_id(desc)) < 0)
359 } else if (type == AVMEDIA_TYPE_AUDIO) {
360 enum AVSampleFormat fmt = 0;
361 while (av_get_sample_fmt_name(fmt)) {
362 if (ff_add_format(&ret, fmt) < 0)
371 int ff_formats_pixdesc_filter(AVFilterFormats **rfmts, unsigned want, unsigned rej)
373 unsigned nb_formats, fmt, flags;
374 AVFilterFormats *formats = NULL;
378 for (fmt = 0;; fmt++) {
379 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
383 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
384 !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
385 (desc->log2_chroma_w || desc->log2_chroma_h))
386 flags |= FF_PIX_FMT_FLAG_SW_FLAT_SUB;
387 if ((flags & (want | rej)) != want)
390 formats->formats[nb_formats] = fmt;
394 av_assert0(formats->nb_formats == nb_formats);
398 formats = av_mallocz(sizeof(*formats));
400 return AVERROR(ENOMEM);
401 formats->nb_formats = nb_formats;
403 formats->formats = av_malloc_array(nb_formats, sizeof(*formats->formats));
404 if (!formats->formats) {
406 return AVERROR(ENOMEM);
412 AVFilterFormats *ff_planar_sample_fmts(void)
414 AVFilterFormats *ret = NULL;
417 for (fmt = 0; av_get_bytes_per_sample(fmt)>0; fmt++)
418 if (av_sample_fmt_is_planar(fmt))
419 if (ff_add_format(&ret, fmt) < 0)
425 AVFilterFormats *ff_all_samplerates(void)
427 AVFilterFormats *ret = av_mallocz(sizeof(*ret));
431 AVFilterChannelLayouts *ff_all_channel_layouts(void)
433 AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
436 ret->all_layouts = 1;
440 AVFilterChannelLayouts *ff_all_channel_counts(void)
442 AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
445 ret->all_layouts = ret->all_counts = 1;
449 #define FORMATS_REF(f, ref, unref_fn) \
453 return AVERROR(ENOMEM); \
455 tmp = av_realloc_array(f->refs, sizeof(*f->refs), f->refcount + 1); \
458 return AVERROR(ENOMEM); \
461 f->refs[f->refcount++] = ref; \
465 int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
467 FORMATS_REF(f, ref, ff_channel_layouts_unref);
470 int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
472 FORMATS_REF(f, ref, ff_formats_unref);
475 #define FIND_REF_INDEX(ref, idx) \
478 for (i = 0; i < (*ref)->refcount; i ++) \
479 if((*ref)->refs[i] == ref) { \
485 #define FORMATS_UNREF(ref, list) \
489 if (!ref || !*ref || !(*ref)->refs) \
492 FIND_REF_INDEX(ref, idx); \
495 memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
496 sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
498 if(!--(*ref)->refcount) { \
499 av_free((*ref)->list); \
500 av_free((*ref)->refs); \
506 void ff_formats_unref(AVFilterFormats **ref)
508 FORMATS_UNREF(ref, formats);
511 void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
513 FORMATS_UNREF(ref, channel_layouts);
516 #define FORMATS_CHANGEREF(oldref, newref) \
520 FIND_REF_INDEX(oldref, idx); \
523 (*oldref)->refs[idx] = newref; \
529 void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
530 AVFilterChannelLayouts **newref)
532 FORMATS_CHANGEREF(oldref, newref);
535 void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
537 FORMATS_CHANGEREF(oldref, newref);
540 #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref_fn, unref_fn, list) \
544 return AVERROR(ENOMEM); \
546 for (i = 0; i < ctx->nb_inputs; i++) { \
547 if (ctx->inputs[i] && !ctx->inputs[i]->out_fmts) { \
548 int ret = ref_fn(fmts, &ctx->inputs[i]->out_fmts); \
552 av_freep(&fmts->list); \
559 for (i = 0; i < ctx->nb_outputs; i++) { \
560 if (ctx->outputs[i] && !ctx->outputs[i]->in_fmts) { \
561 int ret = ref_fn(fmts, &ctx->outputs[i]->in_fmts); \
565 av_freep(&fmts->list); \
574 av_freep(&fmts->list); \
575 av_freep(&fmts->refs); \
581 int ff_set_common_channel_layouts(AVFilterContext *ctx,
582 AVFilterChannelLayouts *layouts)
584 SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
585 ff_channel_layouts_ref, ff_channel_layouts_unref, channel_layouts);
588 int ff_set_common_samplerates(AVFilterContext *ctx,
589 AVFilterFormats *samplerates)
591 SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
592 ff_formats_ref, ff_formats_unref, formats);
596 * A helper for query_formats() which sets all links to the same list of
597 * formats. If there are no links hooked to this filter, the list of formats is
600 int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
602 SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
603 ff_formats_ref, ff_formats_unref, formats);
606 int ff_default_query_formats(AVFilterContext *ctx)
609 enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
610 ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
613 ret = ff_set_common_formats(ctx, ff_all_formats(type));
616 if (type == AVMEDIA_TYPE_AUDIO) {
617 ret = ff_set_common_channel_layouts(ctx, ff_all_channel_counts());
620 ret = ff_set_common_samplerates(ctx, ff_all_samplerates());
628 /* internal functions for parsing audio format arguments */
630 int ff_parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx)
633 int pix_fmt = av_get_pix_fmt(arg);
634 if (pix_fmt == AV_PIX_FMT_NONE) {
635 pix_fmt = strtol(arg, &tail, 0);
636 if (*tail || !av_pix_fmt_desc_get(pix_fmt)) {
637 av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
638 return AVERROR(EINVAL);
645 int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
648 double srate = av_strtod(arg, &tail);
649 if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
650 av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
651 return AVERROR(EINVAL);
657 int ff_parse_channel_layout(int64_t *ret, int *nret, const char *arg,
663 if (av_get_extended_channel_layout(arg, &chlayout, &nb_channels) < 0) {
664 av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
665 return AVERROR(EINVAL);
667 if (!chlayout && !nret) {
668 av_log(log_ctx, AV_LOG_ERROR, "Unknown channel layout '%s' is not supported.\n", arg);
669 return AVERROR(EINVAL);