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.
37 #define MERGE_REF(ret, a, fmts, type, fail) \
42 if (!(tmp = av_realloc(ret->refs, \
43 sizeof(*tmp) * (ret->refcount + a->refcount)))) \
47 for (i = 0; i < a->refcount; i ++) { \
48 ret->refs[ret->refcount] = a->refs[i]; \
49 *ret->refs[ret->refcount++] = ret; \
58 * Add all formats common for a and b to ret, copy the refs and destroy
61 #define MERGE_FORMATS(ret, a, b, fmts, nb, type, fail) \
63 int i, j, k = 0, count = FFMIN(a->nb, b->nb); \
65 if (!(ret = av_mallocz(sizeof(*ret)))) \
69 if (!(ret->fmts = av_malloc(sizeof(*ret->fmts) * count))) \
71 for (i = 0; i < a->nb; i++) \
72 for (j = 0; j < b->nb; j++) \
73 if (a->fmts[i] == b->fmts[j]) { \
74 if(k >= FFMIN(a->nb, b->nb)){ \
75 av_log(NULL, AV_LOG_ERROR, "Duplicate formats in avfilter_merge_formats() detected\n"); \
80 ret->fmts[k++] = a->fmts[i]; \
84 /* check that there was at least one common format */ \
88 MERGE_REF(ret, a, fmts, type, fail); \
89 MERGE_REF(ret, b, fmts, type, fail); \
92 AVFilterFormats *ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b,
93 enum AVMediaType type)
95 AVFilterFormats *ret = NULL;
97 int alpha1=0, alpha2=0;
98 int chroma1=0, chroma2=0;
103 /* Do not lose chroma or alpha in merging.
104 It happens if both lists have formats with chroma (resp. alpha), but
105 the only formats in common do not have it (e.g. YUV+gray vs.
106 RGB+gray): in that case, the merging would select the gray format,
107 possibly causing a lossy conversion elsewhere in the graph.
108 To avoid that, pretend that there are no common formats to force the
109 insertion of a conversion filter. */
110 if (type == AVMEDIA_TYPE_VIDEO)
111 for (i = 0; i < a->format_count; i++)
112 for (j = 0; j < b->format_count; j++) {
113 const AVPixFmtDescriptor *adesc = av_pix_fmt_desc_get(a->formats[i]);
114 const AVPixFmtDescriptor *bdesc = av_pix_fmt_desc_get(b->formats[j]);
115 alpha2 |= adesc->flags & bdesc->flags & PIX_FMT_ALPHA;
116 chroma2|= adesc->nb_components > 1 && bdesc->nb_components > 1;
117 if (a->formats[i] == b->formats[j]) {
118 alpha1 |= adesc->flags & PIX_FMT_ALPHA;
119 chroma1|= adesc->nb_components > 1;
123 // If chroma or alpha can be lost through merging then do not merge
124 if (alpha2 > alpha1 || chroma2 > chroma1)
127 MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
132 av_freep(&ret->refs);
133 av_freep(&ret->formats);
139 AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a,
142 AVFilterFormats *ret = NULL;
144 if (a == b) return a;
146 if (a->format_count && b->format_count) {
147 MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
148 } else if (a->format_count) {
149 MERGE_REF(a, b, formats, AVFilterFormats, fail);
152 MERGE_REF(b, a, formats, AVFilterFormats, fail);
159 av_freep(&ret->refs);
160 av_freep(&ret->formats);
166 AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,
167 AVFilterChannelLayouts *b)
169 AVFilterChannelLayouts *ret = NULL;
170 unsigned a_all = a->all_layouts + a->all_counts;
171 unsigned b_all = b->all_layouts + b->all_counts;
172 int ret_max, ret_nb = 0, i, j, round;
174 if (a == b) return a;
176 /* Put the most generic set in a, to avoid doing everything twice */
178 FFSWAP(AVFilterChannelLayouts *, a, b);
179 FFSWAP(unsigned, a_all, b_all);
182 if (a_all == 1 && !b_all) {
183 /* keep only known layouts in b; works also for b_all = 1 */
184 for (i = j = 0; i < b->nb_channel_layouts; i++)
185 if (KNOWN(b->channel_layouts[i]))
186 b->channel_layouts[j++] = b->channel_layouts[i];
187 /* Not optimal: the unknown layouts of b may become known after
191 b->nb_channel_layouts = j;
193 MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, fail);
197 ret_max = a->nb_channel_layouts + b->nb_channel_layouts;
198 if (!(ret = av_mallocz(sizeof(*ret))) ||
199 !(ret->channel_layouts = av_malloc(sizeof(*ret->channel_layouts) *
203 /* a[known] intersect b[known] */
204 for (i = 0; i < a->nb_channel_layouts; i++) {
205 if (!KNOWN(a->channel_layouts[i]))
207 for (j = 0; j < b->nb_channel_layouts; j++) {
208 if (a->channel_layouts[i] == b->channel_layouts[j]) {
209 ret->channel_layouts[ret_nb++] = a->channel_layouts[i];
210 a->channel_layouts[i] = b->channel_layouts[j] = 0;
214 /* 1st round: a[known] intersect b[generic]
215 2nd round: a[generic] intersect b[known] */
216 for (round = 0; round < 2; round++) {
217 for (i = 0; i < a->nb_channel_layouts; i++) {
218 uint64_t fmt = a->channel_layouts[i], bfmt;
219 if (!fmt || !KNOWN(fmt))
221 bfmt = FF_COUNT2LAYOUT(av_get_channel_layout_nb_channels(fmt));
222 for (j = 0; j < b->nb_channel_layouts; j++)
223 if (b->channel_layouts[j] == bfmt)
224 ret->channel_layouts[ret_nb++] = a->channel_layouts[i];
226 /* 1st round: swap to prepare 2nd round; 2nd round: put it back */
227 FFSWAP(AVFilterChannelLayouts *, a, b);
229 /* a[generic] intersect b[generic] */
230 for (i = 0; i < a->nb_channel_layouts; i++) {
231 if (KNOWN(a->channel_layouts[i]))
233 for (j = 0; j < b->nb_channel_layouts; j++)
234 if (a->channel_layouts[i] == b->channel_layouts[j])
235 ret->channel_layouts[ret_nb++] = a->channel_layouts[i];
238 ret->nb_channel_layouts = ret_nb;
239 if (!ret->nb_channel_layouts)
241 MERGE_REF(ret, a, channel_layouts, AVFilterChannelLayouts, fail);
242 MERGE_REF(ret, b, channel_layouts, AVFilterChannelLayouts, fail);
247 av_freep(&ret->refs);
248 av_freep(&ret->channel_layouts);
254 int ff_fmt_is_in(int fmt, const int *fmts)
258 for (p = fmts; *p != -1; p++) {
265 #define COPY_INT_LIST(list_copy, list, type) { \
268 for (count = 0; list[count] != -1; count++) \
270 list_copy = av_calloc(count+1, sizeof(type)); \
272 memcpy(list_copy, list, sizeof(type) * count); \
273 list_copy[count] = -1; \
277 int *ff_copy_int_list(const int * const list)
280 COPY_INT_LIST(ret, list, int);
284 int64_t *ff_copy_int64_list(const int64_t * const list)
287 COPY_INT_LIST(ret, list, int64_t);
291 #define MAKE_FORMAT_LIST(type, field, count_field) \
295 for (count = 0; fmts[count] != -1; count++) \
297 formats = av_mallocz(sizeof(*formats)); \
298 if (!formats) return NULL; \
299 formats->count_field = count; \
301 formats->field = av_malloc(sizeof(*formats->field)*count); \
302 if (!formats->field) { \
308 AVFilterFormats *ff_make_format_list(const int *fmts)
310 MAKE_FORMAT_LIST(AVFilterFormats, formats, format_count);
312 formats->formats[count] = fmts[count];
317 AVFilterChannelLayouts *avfilter_make_format64_list(const int64_t *fmts)
319 MAKE_FORMAT_LIST(AVFilterChannelLayouts,
320 channel_layouts, nb_channel_layouts);
322 memcpy(formats->channel_layouts, fmts,
323 sizeof(*formats->channel_layouts) * count);
328 #define ADD_FORMAT(f, fmt, type, list, nb) \
332 if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) \
333 return AVERROR(ENOMEM); \
335 fmts = av_realloc((*f)->list, \
336 sizeof(*(*f)->list) * ((*f)->nb + 1));\
338 return AVERROR(ENOMEM); \
341 (*f)->list[(*f)->nb++] = fmt; \
344 int ff_add_format(AVFilterFormats **avff, int64_t fmt)
346 ADD_FORMAT(avff, fmt, int, formats, format_count);
350 int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
352 av_assert1(!(*l && (*l)->all_layouts));
353 ADD_FORMAT(l, channel_layout, uint64_t, channel_layouts, nb_channel_layouts);
357 AVFilterFormats *ff_all_formats(enum AVMediaType type)
359 AVFilterFormats *ret = NULL;
361 int num_formats = type == AVMEDIA_TYPE_VIDEO ? AV_PIX_FMT_NB :
362 type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
364 for (fmt = 0; fmt < num_formats; fmt++) {
365 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
366 if ((type != AVMEDIA_TYPE_VIDEO) ||
367 (type == AVMEDIA_TYPE_VIDEO && !(desc->flags & PIX_FMT_HWACCEL)))
368 ff_add_format(&ret, fmt);
374 const int64_t avfilter_all_channel_layouts[] = {
375 #include "all_channel_layouts.inc"
379 // AVFilterFormats *avfilter_make_all_channel_layouts(void)
381 // return avfilter_make_format64_list(avfilter_all_channel_layouts);
384 AVFilterFormats *ff_planar_sample_fmts(void)
386 AVFilterFormats *ret = NULL;
389 for (fmt = 0; fmt < AV_SAMPLE_FMT_NB; fmt++)
390 if (av_sample_fmt_is_planar(fmt))
391 ff_add_format(&ret, fmt);
396 AVFilterFormats *ff_all_samplerates(void)
398 AVFilterFormats *ret = av_mallocz(sizeof(*ret));
402 AVFilterChannelLayouts *ff_all_channel_layouts(void)
404 AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
407 ret->all_layouts = 1;
411 AVFilterChannelLayouts *ff_all_channel_counts(void)
413 AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
416 ret->all_layouts = ret->all_counts = 1;
420 #define FORMATS_REF(f, ref) \
423 f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount); \
424 f->refs[f->refcount-1] = ref; \
427 void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
432 void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
437 #define FIND_REF_INDEX(ref, idx) \
440 for (i = 0; i < (*ref)->refcount; i ++) \
441 if((*ref)->refs[i] == ref) { \
447 #define FORMATS_UNREF(ref, list) \
454 FIND_REF_INDEX(ref, idx); \
457 memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
458 sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
460 if(!--(*ref)->refcount) { \
461 av_free((*ref)->list); \
462 av_free((*ref)->refs); \
468 void ff_formats_unref(AVFilterFormats **ref)
470 FORMATS_UNREF(ref, formats);
473 void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
475 FORMATS_UNREF(ref, channel_layouts);
478 #define FORMATS_CHANGEREF(oldref, newref) \
482 FIND_REF_INDEX(oldref, idx); \
485 (*oldref)->refs[idx] = newref; \
491 void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
492 AVFilterChannelLayouts **newref)
494 FORMATS_CHANGEREF(oldref, newref);
497 void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
499 FORMATS_CHANGEREF(oldref, newref);
502 #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
506 for (i = 0; i < ctx->nb_inputs; i++) { \
507 if (ctx->inputs[i] && !ctx->inputs[i]->out_fmts) { \
508 ref(fmts, &ctx->inputs[i]->out_fmts); \
512 for (i = 0; i < ctx->nb_outputs; i++) { \
513 if (ctx->outputs[i] && !ctx->outputs[i]->in_fmts) { \
514 ref(fmts, &ctx->outputs[i]->in_fmts); \
520 av_freep(&fmts->list); \
521 av_freep(&fmts->refs); \
526 void ff_set_common_channel_layouts(AVFilterContext *ctx,
527 AVFilterChannelLayouts *layouts)
529 SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
530 ff_channel_layouts_ref, channel_layouts);
533 void ff_set_common_samplerates(AVFilterContext *ctx,
534 AVFilterFormats *samplerates)
536 SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
537 ff_formats_ref, formats);
541 * A helper for query_formats() which sets all links to the same list of
542 * formats. If there are no links hooked to this filter, the list of formats is
545 void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
547 SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
548 ff_formats_ref, formats);
551 static int default_query_formats_common(AVFilterContext *ctx,
552 AVFilterChannelLayouts *(layouts)(void))
554 enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
555 ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
558 ff_set_common_formats(ctx, ff_all_formats(type));
559 if (type == AVMEDIA_TYPE_AUDIO) {
560 ff_set_common_channel_layouts(ctx, layouts());
561 ff_set_common_samplerates(ctx, ff_all_samplerates());
567 int ff_default_query_formats(AVFilterContext *ctx)
569 return default_query_formats_common(ctx, ff_all_channel_layouts);
572 int ff_query_formats_all(AVFilterContext *ctx)
574 return default_query_formats_common(ctx, ff_all_channel_counts);
577 /* internal functions for parsing audio format arguments */
579 int ff_parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx)
582 int pix_fmt = av_get_pix_fmt(arg);
583 if (pix_fmt == AV_PIX_FMT_NONE) {
584 pix_fmt = strtol(arg, &tail, 0);
585 if (*tail || (unsigned)pix_fmt >= AV_PIX_FMT_NB) {
586 av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
587 return AVERROR(EINVAL);
594 int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
597 int sfmt = av_get_sample_fmt(arg);
598 if (sfmt == AV_SAMPLE_FMT_NONE) {
599 sfmt = strtol(arg, &tail, 0);
600 if (*tail || (unsigned)sfmt >= AV_SAMPLE_FMT_NB) {
601 av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
602 return AVERROR(EINVAL);
609 int ff_parse_time_base(AVRational *ret, const char *arg, void *log_ctx)
612 if(av_parse_ratio(&r, arg, INT_MAX, 0, log_ctx) < 0 ||r.num<=0 ||r.den<=0) {
613 av_log(log_ctx, AV_LOG_ERROR, "Invalid time base '%s'\n", arg);
614 return AVERROR(EINVAL);
620 int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
623 double srate = av_strtod(arg, &tail);
624 if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
625 av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
626 return AVERROR(EINVAL);
632 int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
635 int64_t chlayout = av_get_channel_layout(arg);
637 chlayout = strtol(arg, &tail, 10);
638 if (*tail || chlayout == 0) {
639 av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
640 return AVERROR(EINVAL);
656 for (cl = avfilter_all_channel_layouts; *cl != -1; cl++) {
657 av_get_channel_layout_string(buf, sizeof(buf), -1, *cl);