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)
94 AVFilterFormats *ret = NULL;
99 MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
104 av_freep(&ret->refs);
105 av_freep(&ret->formats);
111 AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a,
114 AVFilterFormats *ret = NULL;
116 if (a == b) return a;
118 if (a->format_count && b->format_count) {
119 MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
120 } else if (a->format_count) {
121 MERGE_REF(a, b, formats, AVFilterFormats, fail);
124 MERGE_REF(b, a, formats, AVFilterFormats, fail);
131 av_freep(&ret->refs);
132 av_freep(&ret->formats);
138 AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,
139 AVFilterChannelLayouts *b)
141 AVFilterChannelLayouts *ret = NULL;
142 unsigned a_all = a->all_layouts + a->all_counts;
143 unsigned b_all = b->all_layouts + b->all_counts;
144 int ret_max, ret_nb = 0, i, j, round;
146 if (a == b) return a;
148 /* Put the most generic set in a, to avoid doing everything twice */
150 FFSWAP(AVFilterChannelLayouts *, a, b);
151 FFSWAP(unsigned, a_all, b_all);
154 if (a_all == 1 && !b_all) {
155 /* keep only known layouts in b; works also for b_all = 1 */
156 for (i = j = 0; i < b->nb_channel_layouts; i++)
157 if (KNOWN(b->channel_layouts[i]))
158 b->channel_layouts[j++] = b->channel_layouts[i];
159 b->nb_channel_layouts = j;
161 MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, fail);
165 ret_max = a->nb_channel_layouts + b->nb_channel_layouts;
166 if (!(ret = av_mallocz(sizeof(*ret))) ||
167 !(ret->channel_layouts = av_malloc(sizeof(*ret->channel_layouts) *
171 /* a[known] intersect b[known] */
172 for (i = 0; i < a->nb_channel_layouts; i++) {
173 if (!KNOWN(a->channel_layouts[i]))
175 for (j = 0; j < b->nb_channel_layouts; j++) {
176 if (a->channel_layouts[i] == b->channel_layouts[j]) {
177 ret->channel_layouts[ret_nb++] = a->channel_layouts[i];
178 a->channel_layouts[i] = b->channel_layouts[j] = 0;
182 /* 1st round: a[known] intersect b[generic]
183 2nd round: a[generic] intersect b[known] */
184 for (round = 0; round < 2; round++) {
185 for (i = 0; i < a->nb_channel_layouts; i++) {
186 uint64_t fmt = a->channel_layouts[i], bfmt;
187 if (!fmt || !KNOWN(fmt))
189 bfmt = FF_COUNT2LAYOUT(av_get_channel_layout_nb_channels(fmt));
190 for (j = 0; j < b->nb_channel_layouts; j++)
191 if (b->channel_layouts[j] == bfmt)
192 ret->channel_layouts[ret_nb++] = a->channel_layouts[i];
194 /* 1st round: swap to prepare 2nd round; 2nd round: put it back */
195 FFSWAP(AVFilterChannelLayouts *, a, b);
197 /* a[generic] intersect b[generic] */
198 for (i = 0; i < a->nb_channel_layouts; i++) {
199 if (KNOWN(a->channel_layouts[i]))
201 for (j = 0; j < b->nb_channel_layouts; j++)
202 if (a->channel_layouts[i] == b->channel_layouts[j])
203 ret->channel_layouts[ret_nb++] = a->channel_layouts[i];
206 ret->nb_channel_layouts = ret_nb;
207 if (!ret->nb_channel_layouts)
209 MERGE_REF(ret, a, channel_layouts, AVFilterChannelLayouts, fail);
210 MERGE_REF(ret, b, channel_layouts, AVFilterChannelLayouts, fail);
215 av_freep(&ret->refs);
216 av_freep(&ret->channel_layouts);
222 int ff_fmt_is_in(int fmt, const int *fmts)
226 for (p = fmts; *p != -1; p++) {
233 #define COPY_INT_LIST(list_copy, list, type) { \
236 for (count = 0; list[count] != -1; count++) \
238 list_copy = av_calloc(count+1, sizeof(type)); \
240 memcpy(list_copy, list, sizeof(type) * count); \
241 list_copy[count] = -1; \
245 int *ff_copy_int_list(const int * const list)
248 COPY_INT_LIST(ret, list, int);
252 int64_t *ff_copy_int64_list(const int64_t * const list)
255 COPY_INT_LIST(ret, list, int64_t);
259 #define MAKE_FORMAT_LIST(type, field, count_field) \
263 for (count = 0; fmts[count] != -1; count++) \
265 formats = av_mallocz(sizeof(*formats)); \
266 if (!formats) return NULL; \
267 formats->count_field = count; \
269 formats->field = av_malloc(sizeof(*formats->field)*count); \
270 if (!formats->field) { \
276 AVFilterFormats *ff_make_format_list(const int *fmts)
278 MAKE_FORMAT_LIST(AVFilterFormats, formats, format_count);
280 formats->formats[count] = fmts[count];
285 AVFilterChannelLayouts *avfilter_make_format64_list(const int64_t *fmts)
287 MAKE_FORMAT_LIST(AVFilterChannelLayouts,
288 channel_layouts, nb_channel_layouts);
290 memcpy(formats->channel_layouts, fmts,
291 sizeof(*formats->channel_layouts) * count);
296 #define ADD_FORMAT(f, fmt, type, list, nb) \
300 if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) \
301 return AVERROR(ENOMEM); \
303 fmts = av_realloc((*f)->list, \
304 sizeof(*(*f)->list) * ((*f)->nb + 1));\
306 return AVERROR(ENOMEM); \
309 (*f)->list[(*f)->nb++] = fmt; \
312 int ff_add_format(AVFilterFormats **avff, int64_t fmt)
314 ADD_FORMAT(avff, fmt, int, formats, format_count);
318 int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
320 av_assert1(!(*l && (*l)->all_layouts));
321 ADD_FORMAT(l, channel_layout, uint64_t, channel_layouts, nb_channel_layouts);
325 AVFilterFormats *ff_all_formats(enum AVMediaType type)
327 AVFilterFormats *ret = NULL;
329 int num_formats = type == AVMEDIA_TYPE_VIDEO ? AV_PIX_FMT_NB :
330 type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
332 for (fmt = 0; fmt < num_formats; fmt++) {
333 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
334 if ((type != AVMEDIA_TYPE_VIDEO) ||
335 (type == AVMEDIA_TYPE_VIDEO && !(desc->flags & PIX_FMT_HWACCEL)))
336 ff_add_format(&ret, fmt);
342 const int64_t avfilter_all_channel_layouts[] = {
343 #include "all_channel_layouts.inc"
347 // AVFilterFormats *avfilter_make_all_channel_layouts(void)
349 // return avfilter_make_format64_list(avfilter_all_channel_layouts);
352 AVFilterFormats *ff_planar_sample_fmts(void)
354 AVFilterFormats *ret = NULL;
357 for (fmt = 0; fmt < AV_SAMPLE_FMT_NB; fmt++)
358 if (av_sample_fmt_is_planar(fmt))
359 ff_add_format(&ret, fmt);
364 AVFilterFormats *ff_all_samplerates(void)
366 AVFilterFormats *ret = av_mallocz(sizeof(*ret));
370 AVFilterChannelLayouts *ff_all_channel_layouts(void)
372 AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
375 ret->all_layouts = 1;
379 AVFilterChannelLayouts *ff_all_channel_counts(void)
381 AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
384 ret->all_layouts = ret->all_counts = 1;
388 #define FORMATS_REF(f, ref) \
391 f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount); \
392 f->refs[f->refcount-1] = ref; \
395 void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
400 void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
405 #define FIND_REF_INDEX(ref, idx) \
408 for (i = 0; i < (*ref)->refcount; i ++) \
409 if((*ref)->refs[i] == ref) { \
415 #define FORMATS_UNREF(ref, list) \
422 FIND_REF_INDEX(ref, idx); \
425 memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
426 sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
428 if(!--(*ref)->refcount) { \
429 av_free((*ref)->list); \
430 av_free((*ref)->refs); \
436 void ff_formats_unref(AVFilterFormats **ref)
438 FORMATS_UNREF(ref, formats);
441 void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
443 FORMATS_UNREF(ref, channel_layouts);
446 #define FORMATS_CHANGEREF(oldref, newref) \
450 FIND_REF_INDEX(oldref, idx); \
453 (*oldref)->refs[idx] = newref; \
459 void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
460 AVFilterChannelLayouts **newref)
462 FORMATS_CHANGEREF(oldref, newref);
465 void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
467 FORMATS_CHANGEREF(oldref, newref);
470 #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
474 for (i = 0; i < ctx->nb_inputs; i++) { \
475 if (ctx->inputs[i] && !ctx->inputs[i]->out_fmts) { \
476 ref(fmts, &ctx->inputs[i]->out_fmts); \
480 for (i = 0; i < ctx->nb_outputs; i++) { \
481 if (ctx->outputs[i] && !ctx->outputs[i]->in_fmts) { \
482 ref(fmts, &ctx->outputs[i]->in_fmts); \
488 av_freep(&fmts->list); \
489 av_freep(&fmts->refs); \
494 void ff_set_common_channel_layouts(AVFilterContext *ctx,
495 AVFilterChannelLayouts *layouts)
497 SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
498 ff_channel_layouts_ref, channel_layouts);
501 void ff_set_common_samplerates(AVFilterContext *ctx,
502 AVFilterFormats *samplerates)
504 SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
505 ff_formats_ref, formats);
509 * A helper for query_formats() which sets all links to the same list of
510 * formats. If there are no links hooked to this filter, the list of formats is
513 void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
515 SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
516 ff_formats_ref, formats);
519 static int default_query_formats_common(AVFilterContext *ctx,
520 AVFilterChannelLayouts *(layouts)(void))
522 enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
523 ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
526 ff_set_common_formats(ctx, ff_all_formats(type));
527 if (type == AVMEDIA_TYPE_AUDIO) {
528 ff_set_common_channel_layouts(ctx, layouts());
529 ff_set_common_samplerates(ctx, ff_all_samplerates());
535 int ff_default_query_formats(AVFilterContext *ctx)
537 return default_query_formats_common(ctx, ff_all_channel_layouts);
540 int ff_query_formats_all(AVFilterContext *ctx)
542 return default_query_formats_common(ctx, ff_all_channel_counts);
545 /* internal functions for parsing audio format arguments */
547 int ff_parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx)
550 int pix_fmt = av_get_pix_fmt(arg);
551 if (pix_fmt == AV_PIX_FMT_NONE) {
552 pix_fmt = strtol(arg, &tail, 0);
553 if (*tail || (unsigned)pix_fmt >= AV_PIX_FMT_NB) {
554 av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
555 return AVERROR(EINVAL);
562 int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
565 int sfmt = av_get_sample_fmt(arg);
566 if (sfmt == AV_SAMPLE_FMT_NONE) {
567 sfmt = strtol(arg, &tail, 0);
568 if (*tail || (unsigned)sfmt >= AV_SAMPLE_FMT_NB) {
569 av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
570 return AVERROR(EINVAL);
577 int ff_parse_time_base(AVRational *ret, const char *arg, void *log_ctx)
580 if(av_parse_ratio(&r, arg, INT_MAX, 0, log_ctx) < 0 ||r.num<=0 ||r.den<=0) {
581 av_log(log_ctx, AV_LOG_ERROR, "Invalid time base '%s'\n", arg);
582 return AVERROR(EINVAL);
588 int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
591 double srate = av_strtod(arg, &tail);
592 if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
593 av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
594 return AVERROR(EINVAL);
600 int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
603 int64_t chlayout = av_get_channel_layout(arg);
605 chlayout = strtol(arg, &tail, 10);
606 if (*tail || chlayout == 0) {
607 av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
608 return AVERROR(EINVAL);
624 for (cl = avfilter_all_channel_layouts; *cl != -1; cl++) {
625 av_get_channel_layout_string(buf, sizeof(buf), -1, *cl);