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"
27 #include "libavutil/parseutils.h"
32 #define KNOWN(l) (!FF_LAYOUT2COUNT(l)) /* for readability */
35 * Add all refs from a to ret and destroy a.
36 * ret->refs must have enough spare room left for this.
38 #define MERGE_REF_NO_ALLOC(ret, a, fmts) \
41 for (i = 0; i < a->refcount; i ++) { \
42 ret->refs[ret->refcount] = a->refs[i]; \
43 *ret->refs[ret->refcount++] = ret; \
51 #define MERGE_REF(ret, a, fmts, type, fail_statement) \
55 if (!(tmp = av_realloc_array(ret->refs, ret->refcount + a->refcount, \
59 MERGE_REF_NO_ALLOC(ret, a, fmts); \
63 * Add all formats common for a and b to ret, copy the refs and destroy
66 #define MERGE_FORMATS(ret, a, b, fmts, nb, type, fail) \
68 int i, j, k = 0, count = FFMIN(a->nb, b->nb); \
71 if (!(ret = av_mallocz(sizeof(*ret)))) \
75 if (!(ret->fmts = av_malloc_array(count, sizeof(*ret->fmts)))) \
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(k >= FFMIN(a->nb, b->nb)){ \
81 av_log(NULL, AV_LOG_ERROR, "Duplicate formats in %s detected\n", __FUNCTION__); \
86 ret->fmts[k++] = a->fmts[i]; \
90 /* check that there was at least one common format */ \
94 tmp = av_realloc_array(NULL, a->refcount + b->refcount, sizeof(*tmp)); \
99 MERGE_REF_NO_ALLOC(ret, a, fmts); \
100 MERGE_REF_NO_ALLOC(ret, b, fmts); \
103 AVFilterFormats *ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b,
104 enum AVMediaType type)
106 AVFilterFormats *ret = NULL;
108 int alpha1=0, alpha2=0;
109 int chroma1=0, chroma2=0;
114 /* Do not lose chroma or alpha in merging.
115 It happens if both lists have formats with chroma (resp. alpha), but
116 the only formats in common do not have it (e.g. YUV+gray vs.
117 RGB+gray): in that case, the merging would select the gray format,
118 possibly causing a lossy conversion elsewhere in the graph.
119 To avoid that, pretend that there are no common formats to force the
120 insertion of a conversion filter. */
121 if (type == AVMEDIA_TYPE_VIDEO)
122 for (i = 0; i < a->nb_formats; i++)
123 for (j = 0; j < b->nb_formats; j++) {
124 const AVPixFmtDescriptor *adesc = av_pix_fmt_desc_get(a->formats[i]);
125 const AVPixFmtDescriptor *bdesc = av_pix_fmt_desc_get(b->formats[j]);
126 alpha2 |= adesc->flags & bdesc->flags & AV_PIX_FMT_FLAG_ALPHA;
127 chroma2|= adesc->nb_components > 1 && bdesc->nb_components > 1;
128 if (a->formats[i] == b->formats[j]) {
129 alpha1 |= adesc->flags & AV_PIX_FMT_FLAG_ALPHA;
130 chroma1|= adesc->nb_components > 1;
134 // If chroma or alpha can be lost through merging then do not merge
135 if (alpha2 > alpha1 || chroma2 > chroma1)
138 MERGE_FORMATS(ret, a, b, formats, nb_formats, AVFilterFormats, fail);
143 av_assert1(!ret->refs);
144 av_freep(&ret->formats);
150 AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a,
153 AVFilterFormats *ret = NULL;
155 if (a == b) return a;
157 if (a->nb_formats && b->nb_formats) {
158 MERGE_FORMATS(ret, a, b, formats, nb_formats, AVFilterFormats, fail);
159 } else if (a->nb_formats) {
160 MERGE_REF(a, b, formats, AVFilterFormats, return NULL;);
163 MERGE_REF(b, a, formats, AVFilterFormats, return NULL;);
170 av_assert1(!ret->refs);
171 av_freep(&ret->formats);
177 AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,
178 AVFilterChannelLayouts *b)
180 uint64_t *channel_layouts;
181 unsigned a_all = a->all_layouts + a->all_counts;
182 unsigned b_all = b->all_layouts + b->all_counts;
183 int ret_max, ret_nb = 0, i, j, round;
185 if (a == b) return a;
187 /* Put the most generic set in a, to avoid doing everything twice */
189 FFSWAP(AVFilterChannelLayouts *, a, b);
190 FFSWAP(unsigned, a_all, b_all);
193 if (a_all == 1 && !b_all) {
194 /* keep only known layouts in b; works also for b_all = 1 */
195 for (i = j = 0; i < b->nb_channel_layouts; i++)
196 if (KNOWN(b->channel_layouts[i]))
197 b->channel_layouts[j++] = b->channel_layouts[i];
198 /* Not optimal: the unknown layouts of b may become known after
202 b->nb_channel_layouts = j;
204 MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, return NULL;);
208 ret_max = a->nb_channel_layouts + b->nb_channel_layouts;
209 if (!(channel_layouts = av_malloc_array(ret_max, sizeof(*channel_layouts))))
212 /* a[known] intersect b[known] */
213 for (i = 0; i < a->nb_channel_layouts; i++) {
214 if (!KNOWN(a->channel_layouts[i]))
216 for (j = 0; j < b->nb_channel_layouts; j++) {
217 if (a->channel_layouts[i] == b->channel_layouts[j]) {
218 channel_layouts[ret_nb++] = a->channel_layouts[i];
219 a->channel_layouts[i] = b->channel_layouts[j] = 0;
224 /* 1st round: a[known] intersect b[generic]
225 2nd round: a[generic] intersect b[known] */
226 for (round = 0; round < 2; round++) {
227 for (i = 0; i < a->nb_channel_layouts; i++) {
228 uint64_t fmt = a->channel_layouts[i], bfmt;
229 if (!fmt || !KNOWN(fmt))
231 bfmt = FF_COUNT2LAYOUT(av_get_channel_layout_nb_channels(fmt));
232 for (j = 0; j < b->nb_channel_layouts; j++)
233 if (b->channel_layouts[j] == bfmt)
234 channel_layouts[ret_nb++] = a->channel_layouts[i];
236 /* 1st round: swap to prepare 2nd round; 2nd round: put it back */
237 FFSWAP(AVFilterChannelLayouts *, a, b);
239 /* a[generic] intersect b[generic] */
240 for (i = 0; i < a->nb_channel_layouts; i++) {
241 if (KNOWN(a->channel_layouts[i]))
243 for (j = 0; j < b->nb_channel_layouts; j++)
244 if (a->channel_layouts[i] == b->channel_layouts[j])
245 channel_layouts[ret_nb++] = a->channel_layouts[i];
251 if (a->refcount > b->refcount)
252 FFSWAP(AVFilterChannelLayouts *, a, b);
254 MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, goto fail;);
255 av_freep(&b->channel_layouts);
256 b->channel_layouts = channel_layouts;
257 b->nb_channel_layouts = ret_nb;
261 av_free(channel_layouts);
265 int ff_fmt_is_in(int fmt, const int *fmts)
269 for (p = fmts; *p != -1; p++) {
276 #define MAKE_FORMAT_LIST(type, field, count_field) \
280 for (count = 0; fmts[count] != -1; count++) \
282 formats = av_mallocz(sizeof(*formats)); \
285 formats->count_field = count; \
287 formats->field = av_malloc_array(count, sizeof(*formats->field)); \
288 if (!formats->field) { \
289 av_freep(&formats); \
294 AVFilterFormats *ff_make_format_list(const int *fmts)
296 MAKE_FORMAT_LIST(AVFilterFormats, formats, nb_formats);
298 formats->formats[count] = fmts[count];
303 AVFilterChannelLayouts *ff_make_format64_list(const int64_t *fmts)
305 MAKE_FORMAT_LIST(AVFilterChannelLayouts,
306 channel_layouts, nb_channel_layouts);
308 memcpy(formats->channel_layouts, fmts,
309 sizeof(*formats->channel_layouts) * count);
314 #if LIBAVFILTER_VERSION_MAJOR < 8
315 AVFilterChannelLayouts *avfilter_make_format64_list(const int64_t *fmts)
317 return ff_make_format64_list(fmts);
321 #define ADD_FORMAT(f, fmt, unref_fn, type, list, nb) \
326 if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) { \
327 return AVERROR(ENOMEM); \
330 fmts = av_realloc_array((*f)->list, (*f)->nb + 1, \
331 sizeof(*(*f)->list)); \
336 return AVERROR(ENOMEM); \
340 (*f)->list[(*f)->nb++] = fmt; \
343 int ff_add_format(AVFilterFormats **avff, int64_t fmt)
345 ADD_FORMAT(avff, fmt, ff_formats_unref, int, formats, nb_formats);
349 int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
351 av_assert1(!(*l && (*l)->all_layouts));
352 ADD_FORMAT(l, channel_layout, ff_channel_layouts_unref, uint64_t, channel_layouts, nb_channel_layouts);
356 AVFilterFormats *ff_all_formats(enum AVMediaType type)
358 AVFilterFormats *ret = NULL;
360 if (type == AVMEDIA_TYPE_VIDEO) {
361 const AVPixFmtDescriptor *desc = NULL;
362 while ((desc = av_pix_fmt_desc_next(desc))) {
363 if (ff_add_format(&ret, av_pix_fmt_desc_get_id(desc)) < 0)
366 } else if (type == AVMEDIA_TYPE_AUDIO) {
367 enum AVSampleFormat fmt = 0;
368 while (av_get_sample_fmt_name(fmt)) {
369 if (ff_add_format(&ret, fmt) < 0)
378 int ff_formats_pixdesc_filter(AVFilterFormats **rfmts, unsigned want, unsigned rej)
380 unsigned nb_formats, fmt, flags;
381 AVFilterFormats *formats = NULL;
385 for (fmt = 0;; fmt++) {
386 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
390 if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
391 !(desc->flags & AV_PIX_FMT_FLAG_PLANAR) &&
392 (desc->log2_chroma_w || desc->log2_chroma_h))
393 flags |= FF_PIX_FMT_FLAG_SW_FLAT_SUB;
394 if ((flags & (want | rej)) != want)
397 formats->formats[nb_formats] = fmt;
401 av_assert0(formats->nb_formats == nb_formats);
405 formats = av_mallocz(sizeof(*formats));
407 return AVERROR(ENOMEM);
408 formats->nb_formats = nb_formats;
410 formats->formats = av_malloc_array(nb_formats, sizeof(*formats->formats));
411 if (!formats->formats) {
413 return AVERROR(ENOMEM);
419 AVFilterFormats *ff_planar_sample_fmts(void)
421 AVFilterFormats *ret = NULL;
424 for (fmt = 0; av_get_bytes_per_sample(fmt)>0; fmt++)
425 if (av_sample_fmt_is_planar(fmt))
426 if (ff_add_format(&ret, fmt) < 0)
432 AVFilterFormats *ff_all_samplerates(void)
434 AVFilterFormats *ret = av_mallocz(sizeof(*ret));
438 AVFilterChannelLayouts *ff_all_channel_layouts(void)
440 AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
443 ret->all_layouts = 1;
447 AVFilterChannelLayouts *ff_all_channel_counts(void)
449 AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
452 ret->all_layouts = ret->all_counts = 1;
456 #define FORMATS_REF(f, ref, unref_fn) \
460 return AVERROR(ENOMEM); \
462 tmp = av_realloc_array(f->refs, sizeof(*f->refs), f->refcount + 1); \
465 return AVERROR(ENOMEM); \
468 f->refs[f->refcount++] = ref; \
472 int ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
474 FORMATS_REF(f, ref, ff_channel_layouts_unref);
477 int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
479 FORMATS_REF(f, ref, ff_formats_unref);
482 #define FIND_REF_INDEX(ref, idx) \
485 for (i = 0; i < (*ref)->refcount; i ++) \
486 if((*ref)->refs[i] == ref) { \
492 #define FORMATS_UNREF(ref, list) \
496 if (!ref || !*ref || !(*ref)->refs) \
499 FIND_REF_INDEX(ref, idx); \
502 memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
503 sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
505 if(!--(*ref)->refcount) { \
506 av_free((*ref)->list); \
507 av_free((*ref)->refs); \
513 void ff_formats_unref(AVFilterFormats **ref)
515 FORMATS_UNREF(ref, formats);
518 void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
520 FORMATS_UNREF(ref, channel_layouts);
523 #define FORMATS_CHANGEREF(oldref, newref) \
527 FIND_REF_INDEX(oldref, idx); \
530 (*oldref)->refs[idx] = newref; \
536 void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
537 AVFilterChannelLayouts **newref)
539 FORMATS_CHANGEREF(oldref, newref);
542 void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
544 FORMATS_CHANGEREF(oldref, newref);
547 #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref_fn, unref_fn, list) \
551 return AVERROR(ENOMEM); \
553 for (i = 0; i < ctx->nb_inputs; i++) { \
554 if (ctx->inputs[i] && !ctx->inputs[i]->out_fmts) { \
555 int ret = ref_fn(fmts, &ctx->inputs[i]->out_fmts); \
559 av_freep(&fmts->list); \
566 for (i = 0; i < ctx->nb_outputs; i++) { \
567 if (ctx->outputs[i] && !ctx->outputs[i]->in_fmts) { \
568 int ret = ref_fn(fmts, &ctx->outputs[i]->in_fmts); \
572 av_freep(&fmts->list); \
581 av_freep(&fmts->list); \
582 av_freep(&fmts->refs); \
588 int ff_set_common_channel_layouts(AVFilterContext *ctx,
589 AVFilterChannelLayouts *layouts)
591 SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
592 ff_channel_layouts_ref, ff_channel_layouts_unref, channel_layouts);
595 int ff_set_common_samplerates(AVFilterContext *ctx,
596 AVFilterFormats *samplerates)
598 SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
599 ff_formats_ref, ff_formats_unref, formats);
603 * A helper for query_formats() which sets all links to the same list of
604 * formats. If there are no links hooked to this filter, the list of formats is
607 int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
609 SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
610 ff_formats_ref, ff_formats_unref, formats);
613 static int default_query_formats_common(AVFilterContext *ctx,
614 AVFilterChannelLayouts *(layouts)(void))
617 enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
618 ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
621 ret = ff_set_common_formats(ctx, ff_all_formats(type));
624 if (type == AVMEDIA_TYPE_AUDIO) {
625 ret = ff_set_common_channel_layouts(ctx, layouts());
628 ret = ff_set_common_samplerates(ctx, ff_all_samplerates());
636 int ff_default_query_formats(AVFilterContext *ctx)
638 return default_query_formats_common(ctx, ff_all_channel_counts);
641 int ff_query_formats_all_layouts(AVFilterContext *ctx)
643 return default_query_formats_common(ctx, ff_all_channel_layouts);
646 /* internal functions for parsing audio format arguments */
648 int ff_parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx)
651 int pix_fmt = av_get_pix_fmt(arg);
652 if (pix_fmt == AV_PIX_FMT_NONE) {
653 pix_fmt = strtol(arg, &tail, 0);
654 if (*tail || !av_pix_fmt_desc_get(pix_fmt)) {
655 av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
656 return AVERROR(EINVAL);
663 int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
666 int sfmt = av_get_sample_fmt(arg);
667 if (sfmt == AV_SAMPLE_FMT_NONE) {
668 sfmt = strtol(arg, &tail, 0);
669 if (*tail || av_get_bytes_per_sample(sfmt)<=0) {
670 av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
671 return AVERROR(EINVAL);
678 int ff_parse_time_base(AVRational *ret, const char *arg, void *log_ctx)
681 if(av_parse_ratio(&r, arg, INT_MAX, 0, log_ctx) < 0 ||r.num<=0 ||r.den<=0) {
682 av_log(log_ctx, AV_LOG_ERROR, "Invalid time base '%s'\n", arg);
683 return AVERROR(EINVAL);
689 int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
692 double srate = av_strtod(arg, &tail);
693 if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
694 av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
695 return AVERROR(EINVAL);
701 int ff_parse_channel_layout(int64_t *ret, int *nret, const char *arg,
707 if (av_get_extended_channel_layout(arg, &chlayout, &nb_channels) < 0) {
708 av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
709 return AVERROR(EINVAL);
711 if (!chlayout && !nret) {
712 av_log(log_ctx, AV_LOG_ERROR, "Unknown channel layout '%s' is not supported.\n", arg);
713 return AVERROR(EINVAL);