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, nb_formats, 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->nb_formats && b->nb_formats) {
107 MERGE_FORMATS(ret, a, b, formats, nb_formats, AVFilterFormats, fail);
108 } else if (a->nb_formats) {
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));
177 formats->formats = av_malloc(sizeof(*formats->formats) * count);
178 if (!formats->formats) {
183 formats->nb_formats = count;
184 memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
189 #define ADD_FORMAT(f, fmt, type, list, nb) \
193 if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) \
194 return AVERROR(ENOMEM); \
196 fmts = av_realloc((*f)->list, \
197 sizeof(*(*f)->list) * ((*f)->nb + 1));\
200 return AVERROR(ENOMEM); \
204 (*f)->list[(*f)->nb++] = fmt; \
208 int ff_add_format(AVFilterFormats **avff, int fmt)
210 ADD_FORMAT(avff, fmt, int, formats, nb_formats);
213 int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
215 ADD_FORMAT(l, channel_layout, uint64_t, channel_layouts, nb_channel_layouts);
218 AVFilterFormats *ff_all_formats(enum AVMediaType type)
220 AVFilterFormats *ret = NULL;
222 if (type == AVMEDIA_TYPE_VIDEO) {
223 const AVPixFmtDescriptor *desc = NULL;
224 while ((desc = av_pix_fmt_desc_next(desc))) {
225 ff_add_format(&ret, av_pix_fmt_desc_get_id(desc));
227 } else if (type == AVMEDIA_TYPE_AUDIO) {
228 enum AVSampleFormat fmt = 0;
229 while (av_get_sample_fmt_name(fmt)) {
230 ff_add_format(&ret, fmt);
238 AVFilterFormats *ff_planar_sample_fmts(void)
240 AVFilterFormats *ret = NULL;
243 for (fmt = 0; fmt < AV_SAMPLE_FMT_NB; fmt++)
244 if (av_sample_fmt_is_planar(fmt))
245 ff_add_format(&ret, fmt);
250 AVFilterFormats *ff_all_samplerates(void)
252 AVFilterFormats *ret = av_mallocz(sizeof(*ret));
256 AVFilterChannelLayouts *ff_all_channel_layouts(void)
258 AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
262 #define FORMATS_REF(f, ref) \
265 f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount); \
268 f->refs[f->refcount-1] = ref; \
271 void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
276 void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
281 #define FIND_REF_INDEX(ref, idx) \
284 for (i = 0; i < (*ref)->refcount; i ++) \
285 if((*ref)->refs[i] == ref) { \
291 #define FORMATS_UNREF(ref, list) \
298 FIND_REF_INDEX(ref, idx); \
301 memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
302 sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
304 if(!--(*ref)->refcount) { \
305 av_free((*ref)->list); \
306 av_free((*ref)->refs); \
312 void ff_formats_unref(AVFilterFormats **ref)
314 FORMATS_UNREF(ref, formats);
317 void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
319 FORMATS_UNREF(ref, channel_layouts);
322 #define FORMATS_CHANGEREF(oldref, newref) \
326 FIND_REF_INDEX(oldref, idx); \
329 (*oldref)->refs[idx] = newref; \
335 void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
336 AVFilterChannelLayouts **newref)
338 FORMATS_CHANGEREF(oldref, newref);
341 void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
343 FORMATS_CHANGEREF(oldref, newref);
346 #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
350 for (i = 0; i < ctx->nb_inputs; i++) { \
351 if (ctx->inputs[i]) { \
352 ref(fmts, &ctx->inputs[i]->out_fmts); \
356 for (i = 0; i < ctx->nb_outputs; i++) { \
357 if (ctx->outputs[i]) { \
358 ref(fmts, &ctx->outputs[i]->in_fmts); \
364 av_freep(&fmts->list); \
365 av_freep(&fmts->refs); \
370 void ff_set_common_channel_layouts(AVFilterContext *ctx,
371 AVFilterChannelLayouts *layouts)
373 SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
374 ff_channel_layouts_ref, channel_layouts);
377 void ff_set_common_samplerates(AVFilterContext *ctx,
378 AVFilterFormats *samplerates)
380 SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
381 ff_formats_ref, formats);
385 * A helper for query_formats() which sets all links to the same list of
386 * formats. If there are no links hooked to this filter, the list of formats is
389 void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
391 SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
392 ff_formats_ref, formats);
395 int ff_default_query_formats(AVFilterContext *ctx)
397 enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
398 ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
401 ff_set_common_formats(ctx, ff_all_formats(type));
402 if (type == AVMEDIA_TYPE_AUDIO) {
403 ff_set_common_channel_layouts(ctx, ff_all_channel_layouts());
404 ff_set_common_samplerates(ctx, ff_all_samplerates());