2 * Filter layer - format negotiation
3 * Copyright (c) 2007 Bobby Bingham
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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/pixdesc.h"
29 * Add all refs from a to ret and destroy a.
31 #define MERGE_REF(ret, a, fmts, type, fail) \
36 if (!(tmp = av_realloc(ret->refs, \
37 sizeof(*tmp) * (ret->refcount + a->refcount)))) \
41 for (i = 0; i < a->refcount; i ++) { \
42 ret->refs[ret->refcount] = a->refs[i]; \
43 *ret->refs[ret->refcount++] = ret; \
52 * Add all formats common for a and b to ret, copy the refs and destroy
55 #define MERGE_FORMATS(ret, a, b, fmts, nb, type, fail) \
57 int i, j, k = 0, count = FFMIN(a->nb, b->nb); \
59 if (!(ret = av_mallocz(sizeof(*ret)))) \
63 if (!(ret->fmts = av_malloc(sizeof(*ret->fmts) * count))) \
65 for (i = 0; i < a->nb; i++) \
66 for (j = 0; j < b->nb; j++) \
67 if (a->fmts[i] == b->fmts[j]) \
68 ret->fmts[k++] = a->fmts[i]; \
72 /* check that there was at least one common format */ \
76 MERGE_REF(ret, a, fmts, type, fail); \
77 MERGE_REF(ret, b, fmts, type, fail); \
80 AVFilterFormats *ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
82 AVFilterFormats *ret = NULL;
87 MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
93 av_freep(&ret->formats);
99 AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a,
102 AVFilterFormats *ret = NULL;
104 if (a == b) return a;
106 if (a->format_count && b->format_count) {
107 MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
108 } else if (a->format_count) {
109 MERGE_REF(a, b, formats, AVFilterFormats, fail);
112 MERGE_REF(b, a, formats, AVFilterFormats, fail);
119 av_freep(&ret->refs);
120 av_freep(&ret->formats);
126 AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,
127 AVFilterChannelLayouts *b)
129 AVFilterChannelLayouts *ret = NULL;
131 if (a == b) return a;
133 if (a->nb_channel_layouts && b->nb_channel_layouts) {
134 MERGE_FORMATS(ret, a, b, channel_layouts, nb_channel_layouts,
135 AVFilterChannelLayouts, fail);
136 } else if (a->nb_channel_layouts) {
137 MERGE_REF(a, b, channel_layouts, AVFilterChannelLayouts, fail);
140 MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, fail);
147 av_freep(&ret->refs);
148 av_freep(&ret->channel_layouts);
154 int ff_fmt_is_in(int fmt, const int *fmts)
158 for (p = fmts; *p != AV_PIX_FMT_NONE; p++) {
165 AVFilterFormats *ff_make_format_list(const int *fmts)
167 AVFilterFormats *formats;
170 for (count = 0; fmts[count] != -1; count++)
173 formats = av_mallocz(sizeof(*formats));
175 formats->formats = av_malloc(sizeof(*formats->formats) * count);
176 formats->format_count = count;
177 memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
182 #define ADD_FORMAT(f, fmt, type, list, nb) \
186 if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) \
187 return AVERROR(ENOMEM); \
189 fmts = av_realloc((*f)->list, \
190 sizeof(*(*f)->list) * ((*f)->nb + 1));\
192 return AVERROR(ENOMEM); \
195 (*f)->list[(*f)->nb++] = fmt; \
199 int ff_add_format(AVFilterFormats **avff, int fmt)
201 ADD_FORMAT(avff, fmt, int, formats, format_count);
204 int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
206 ADD_FORMAT(l, channel_layout, uint64_t, channel_layouts, nb_channel_layouts);
209 AVFilterFormats *ff_all_formats(enum AVMediaType type)
211 AVFilterFormats *ret = NULL;
213 int num_formats = type == AVMEDIA_TYPE_VIDEO ? AV_PIX_FMT_NB :
214 type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
216 for (fmt = 0; fmt < num_formats; fmt++) {
217 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
218 if ((type != AVMEDIA_TYPE_VIDEO) ||
219 (type == AVMEDIA_TYPE_VIDEO && !(desc->flags & PIX_FMT_HWACCEL)))
220 ff_add_format(&ret, fmt);
226 AVFilterFormats *ff_planar_sample_fmts(void)
228 AVFilterFormats *ret = NULL;
231 for (fmt = 0; fmt < AV_SAMPLE_FMT_NB; fmt++)
232 if (av_sample_fmt_is_planar(fmt))
233 ff_add_format(&ret, fmt);
238 AVFilterFormats *ff_all_samplerates(void)
240 AVFilterFormats *ret = av_mallocz(sizeof(*ret));
244 AVFilterChannelLayouts *ff_all_channel_layouts(void)
246 AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
250 #define FORMATS_REF(f, ref) \
253 f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount); \
254 f->refs[f->refcount-1] = ref; \
257 void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
262 void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
267 #define FIND_REF_INDEX(ref, idx) \
270 for (i = 0; i < (*ref)->refcount; i ++) \
271 if((*ref)->refs[i] == ref) { \
277 #define FORMATS_UNREF(ref, list) \
284 FIND_REF_INDEX(ref, idx); \
287 memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
288 sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
290 if(!--(*ref)->refcount) { \
291 av_free((*ref)->list); \
292 av_free((*ref)->refs); \
298 void ff_formats_unref(AVFilterFormats **ref)
300 FORMATS_UNREF(ref, formats);
303 void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
305 FORMATS_UNREF(ref, channel_layouts);
308 #define FORMATS_CHANGEREF(oldref, newref) \
312 FIND_REF_INDEX(oldref, idx); \
315 (*oldref)->refs[idx] = newref; \
321 void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
322 AVFilterChannelLayouts **newref)
324 FORMATS_CHANGEREF(oldref, newref);
327 void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
329 FORMATS_CHANGEREF(oldref, newref);
332 #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
336 for (i = 0; i < ctx->nb_inputs; i++) { \
337 if (ctx->inputs[i]) { \
338 ref(fmts, &ctx->inputs[i]->out_fmts); \
342 for (i = 0; i < ctx->nb_outputs; i++) { \
343 if (ctx->outputs[i]) { \
344 ref(fmts, &ctx->outputs[i]->in_fmts); \
350 av_freep(&fmts->list); \
351 av_freep(&fmts->refs); \
356 void ff_set_common_channel_layouts(AVFilterContext *ctx,
357 AVFilterChannelLayouts *layouts)
359 SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
360 ff_channel_layouts_ref, channel_layouts);
363 void ff_set_common_samplerates(AVFilterContext *ctx,
364 AVFilterFormats *samplerates)
366 SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
367 ff_formats_ref, formats);
371 * A helper for query_formats() which sets all links to the same list of
372 * formats. If there are no links hooked to this filter, the list of formats is
375 void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
377 SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
378 ff_formats_ref, formats);
381 int ff_default_query_formats(AVFilterContext *ctx)
383 enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
384 ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
387 ff_set_common_formats(ctx, ff_all_formats(type));
388 if (type == AVMEDIA_TYPE_AUDIO) {
389 ff_set_common_channel_layouts(ctx, ff_all_channel_layouts());
390 ff_set_common_samplerates(ctx, ff_all_samplerates());