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/common.h"
23 #include "libavutil/eval.h"
24 #include "libavutil/pixdesc.h"
25 #include "libavutil/parseutils.h"
26 #include "libavutil/audioconvert.h"
32 * Add all refs from a to ret and destroy a.
34 #define MERGE_REF(ret, a, fmts, type, fail) \
39 if (!(tmp = av_realloc(ret->refs, \
40 sizeof(*tmp) * (ret->refcount + a->refcount)))) \
44 for (i = 0; i < a->refcount; i ++) { \
45 ret->refs[ret->refcount] = a->refs[i]; \
46 *ret->refs[ret->refcount++] = ret; \
55 * Add all formats common for a and b to ret, copy the refs and destroy
58 #define MERGE_FORMATS(ret, a, b, fmts, nb, type, fail) \
60 int i, j, k = 0, count = FFMIN(a->nb, b->nb); \
62 if (!(ret = av_mallocz(sizeof(*ret)))) \
66 if (!(ret->fmts = av_malloc(sizeof(*ret->fmts) * count))) \
68 for (i = 0; i < a->nb; i++) \
69 for (j = 0; j < b->nb; j++) \
70 if (a->fmts[i] == b->fmts[j]) { \
71 if(k >= FFMIN(a->nb, b->nb)){ \
72 av_log(0, AV_LOG_ERROR, "Duplicate formats in avfilter_merge_formats() detected\n"); \
77 ret->fmts[k++] = a->fmts[i]; \
81 /* check that there was at least one common format */ \
85 MERGE_REF(ret, a, fmts, type, fail); \
86 MERGE_REF(ret, b, fmts, type, fail); \
89 AVFilterFormats *ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
91 AVFilterFormats *ret = NULL;
96 MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
101 av_freep(&ret->refs);
102 av_freep(&ret->formats);
108 AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a,
111 AVFilterFormats *ret = NULL;
113 if (a == b) return a;
115 if (a->format_count && b->format_count) {
116 MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
117 } else if (a->format_count) {
118 MERGE_REF(a, b, formats, AVFilterFormats, fail);
121 MERGE_REF(b, a, formats, AVFilterFormats, fail);
128 av_freep(&ret->refs);
129 av_freep(&ret->formats);
135 AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,
136 AVFilterChannelLayouts *b)
138 AVFilterChannelLayouts *ret = NULL;
140 if (a == b) return a;
142 if (a->nb_channel_layouts && b->nb_channel_layouts) {
143 MERGE_FORMATS(ret, a, b, channel_layouts, nb_channel_layouts,
144 AVFilterChannelLayouts, fail);
145 } else if (a->nb_channel_layouts) {
146 MERGE_REF(a, b, channel_layouts, AVFilterChannelLayouts, fail);
149 MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, fail);
156 av_freep(&ret->refs);
157 av_freep(&ret->channel_layouts);
163 int ff_fmt_is_in(int fmt, const int *fmts)
167 for (p = fmts; *p != -1; p++) {
174 #define COPY_INT_LIST(list_copy, list, type) { \
177 for (count = 0; list[count] != -1; count++) \
179 list_copy = av_calloc(count+1, sizeof(type)); \
181 memcpy(list_copy, list, sizeof(type) * count); \
182 list_copy[count] = -1; \
186 int *ff_copy_int_list(const int * const list)
189 COPY_INT_LIST(ret, list, int);
193 int64_t *ff_copy_int64_list(const int64_t * const list)
196 COPY_INT_LIST(ret, list, int64_t);
200 #define MAKE_FORMAT_LIST(type, field, count_field) \
204 for (count = 0; fmts[count] != -1; count++) \
206 formats = av_mallocz(sizeof(*formats)); \
207 if (!formats) return NULL; \
208 formats->count_field = count; \
210 formats->field = av_malloc(sizeof(*formats->field)*count); \
211 if (!formats->field) { \
217 AVFilterFormats *ff_make_format_list(const int *fmts)
219 MAKE_FORMAT_LIST(AVFilterFormats, formats, format_count);
221 formats->formats[count] = fmts[count];
226 AVFilterChannelLayouts *avfilter_make_format64_list(const int64_t *fmts)
228 MAKE_FORMAT_LIST(AVFilterChannelLayouts,
229 channel_layouts, nb_channel_layouts);
231 memcpy(formats->channel_layouts, fmts,
232 sizeof(*formats->channel_layouts) * count);
237 #define ADD_FORMAT(f, fmt, type, list, nb) \
241 if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) \
242 return AVERROR(ENOMEM); \
244 fmts = av_realloc((*f)->list, \
245 sizeof(*(*f)->list) * ((*f)->nb + 1));\
247 return AVERROR(ENOMEM); \
250 (*f)->list[(*f)->nb++] = fmt; \
254 int ff_add_format(AVFilterFormats **avff, int64_t fmt)
256 ADD_FORMAT(avff, fmt, int, formats, format_count);
259 int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
261 ADD_FORMAT(l, channel_layout, uint64_t, channel_layouts, nb_channel_layouts);
264 AVFilterFormats *ff_all_formats(enum AVMediaType type)
266 AVFilterFormats *ret = NULL;
268 int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
269 type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
271 for (fmt = 0; fmt < num_formats; fmt++)
272 if ((type != AVMEDIA_TYPE_VIDEO) ||
273 (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
274 ff_add_format(&ret, fmt);
279 const int64_t avfilter_all_channel_layouts[] = {
280 #include "all_channel_layouts.inc"
284 // AVFilterFormats *avfilter_make_all_channel_layouts(void)
286 // return avfilter_make_format64_list(avfilter_all_channel_layouts);
289 AVFilterFormats *ff_planar_sample_fmts(void)
291 AVFilterFormats *ret = NULL;
294 for (fmt = 0; fmt < AV_SAMPLE_FMT_NB; fmt++)
295 if (av_sample_fmt_is_planar(fmt))
296 ff_add_format(&ret, fmt);
301 AVFilterFormats *ff_all_samplerates(void)
303 AVFilterFormats *ret = av_mallocz(sizeof(*ret));
307 AVFilterChannelLayouts *ff_all_channel_layouts(void)
309 AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
313 #define FORMATS_REF(f, ref) \
316 f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount); \
317 f->refs[f->refcount-1] = ref; \
320 void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
325 void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
330 #define FIND_REF_INDEX(ref, idx) \
333 for (i = 0; i < (*ref)->refcount; i ++) \
334 if((*ref)->refs[i] == ref) { \
340 #define FORMATS_UNREF(ref, list) \
347 FIND_REF_INDEX(ref, idx); \
350 memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
351 sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
353 if(!--(*ref)->refcount) { \
354 av_free((*ref)->list); \
355 av_free((*ref)->refs); \
361 void ff_formats_unref(AVFilterFormats **ref)
363 FORMATS_UNREF(ref, formats);
366 void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
368 FORMATS_UNREF(ref, channel_layouts);
371 #define FORMATS_CHANGEREF(oldref, newref) \
375 FIND_REF_INDEX(oldref, idx); \
378 (*oldref)->refs[idx] = newref; \
384 void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
385 AVFilterChannelLayouts **newref)
387 FORMATS_CHANGEREF(oldref, newref);
390 void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
392 FORMATS_CHANGEREF(oldref, newref);
395 #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
399 for (i = 0; i < ctx->nb_inputs; i++) { \
400 if (ctx->inputs[i] && !ctx->inputs[i]->out_fmts) { \
401 ref(fmts, &ctx->inputs[i]->out_fmts); \
405 for (i = 0; i < ctx->nb_outputs; i++) { \
406 if (ctx->outputs[i] && !ctx->outputs[i]->in_fmts) { \
407 ref(fmts, &ctx->outputs[i]->in_fmts); \
413 av_freep(&fmts->list); \
414 av_freep(&fmts->refs); \
419 void ff_set_common_channel_layouts(AVFilterContext *ctx,
420 AVFilterChannelLayouts *layouts)
422 SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
423 ff_channel_layouts_ref, channel_layouts);
426 void ff_set_common_samplerates(AVFilterContext *ctx,
427 AVFilterFormats *samplerates)
429 SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
430 ff_formats_ref, formats);
434 * A helper for query_formats() which sets all links to the same list of
435 * formats. If there are no links hooked to this filter, the list of formats is
438 void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
440 SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
441 ff_formats_ref, formats);
444 int ff_default_query_formats(AVFilterContext *ctx)
446 enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
447 ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
450 ff_set_common_formats(ctx, ff_all_formats(type));
451 if (type == AVMEDIA_TYPE_AUDIO) {
452 ff_set_common_channel_layouts(ctx, ff_all_channel_layouts());
453 ff_set_common_samplerates(ctx, ff_all_samplerates());
459 /* internal functions for parsing audio format arguments */
461 int ff_parse_pixel_format(enum PixelFormat *ret, const char *arg, void *log_ctx)
464 int pix_fmt = av_get_pix_fmt(arg);
465 if (pix_fmt == PIX_FMT_NONE) {
466 pix_fmt = strtol(arg, &tail, 0);
467 if (*tail || (unsigned)pix_fmt >= PIX_FMT_NB) {
468 av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
469 return AVERROR(EINVAL);
476 int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
479 int sfmt = av_get_sample_fmt(arg);
480 if (sfmt == AV_SAMPLE_FMT_NONE) {
481 sfmt = strtol(arg, &tail, 0);
482 if (*tail || (unsigned)sfmt >= AV_SAMPLE_FMT_NB) {
483 av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
484 return AVERROR(EINVAL);
491 int ff_parse_time_base(AVRational *ret, const char *arg, void *log_ctx)
494 if(av_parse_ratio(&r, arg, INT_MAX, 0, log_ctx) < 0 ||r.num<=0 ||r.den<=0) {
495 av_log(log_ctx, AV_LOG_ERROR, "Invalid time base '%s'\n", arg);
496 return AVERROR(EINVAL);
502 int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
505 double srate = av_strtod(arg, &tail);
506 if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
507 av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
508 return AVERROR(EINVAL);
514 int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
517 int64_t chlayout = av_get_channel_layout(arg);
519 chlayout = strtol(arg, &tail, 10);
520 if (*tail || chlayout == 0) {
521 av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
522 return AVERROR(EINVAL);
538 for (cl = avfilter_all_channel_layouts; *cl != -1; cl++) {
539 av_get_channel_layout_string(buf, sizeof(buf), -1, *cl);