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/pixdesc.h"
28 * Add all refs from a to ret and destroy a.
30 #define MERGE_REF(ret, a, fmts, type, fail) \
35 if (!(tmp = av_realloc(ret->refs, \
36 sizeof(*tmp) * (ret->refcount + a->refcount)))) \
40 for (i = 0; i < a->refcount; i ++) { \
41 ret->refs[ret->refcount] = a->refs[i]; \
42 *ret->refs[ret->refcount++] = ret; \
51 * Add all formats common for a and b to ret, copy the refs and destroy
54 #define MERGE_FORMATS(ret, a, b, fmts, nb, type, fail) \
56 int i, j, k = 0, count = FFMIN(a->nb, b->nb); \
58 if (!(ret = av_mallocz(sizeof(*ret)))) \
62 if (!(ret->fmts = av_malloc(sizeof(*ret->fmts) * count))) \
64 for (i = 0; i < a->nb; i++) \
65 for (j = 0; j < b->nb; j++) \
66 if (a->fmts[i] == b->fmts[j]) \
67 ret->fmts[k++] = a->fmts[i]; \
71 /* check that there was at least one common format */ \
75 MERGE_REF(ret, a, fmts, type, fail); \
76 MERGE_REF(ret, b, fmts, type, fail); \
79 AVFilterFormats *ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
81 AVFilterFormats *ret = NULL;
86 MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
92 av_freep(&ret->formats);
98 AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a,
101 AVFilterFormats *ret = NULL;
103 if (a == b) return a;
105 if (a->format_count && b->format_count) {
106 MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
107 } else if (a->format_count) {
108 MERGE_REF(a, b, formats, AVFilterFormats, fail);
111 MERGE_REF(b, a, formats, AVFilterFormats, fail);
118 av_freep(&ret->refs);
119 av_freep(&ret->formats);
125 AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,
126 AVFilterChannelLayouts *b)
128 AVFilterChannelLayouts *ret = NULL;
130 if (a == b) return a;
132 if (a->nb_channel_layouts && b->nb_channel_layouts) {
133 MERGE_FORMATS(ret, a, b, channel_layouts, nb_channel_layouts,
134 AVFilterChannelLayouts, fail);
135 } else if (a->nb_channel_layouts) {
136 MERGE_REF(a, b, channel_layouts, AVFilterChannelLayouts, fail);
139 MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, fail);
146 av_freep(&ret->refs);
147 av_freep(&ret->channel_layouts);
153 int ff_fmt_is_in(int fmt, const int *fmts)
157 for (p = fmts; *p != PIX_FMT_NONE; p++) {
164 AVFilterFormats *ff_make_format_list(const int *fmts)
166 AVFilterFormats *formats;
169 for (count = 0; fmts[count] != -1; count++)
172 formats = av_mallocz(sizeof(*formats));
174 formats->formats = av_malloc(sizeof(*formats->formats) * count);
175 formats->format_count = count;
176 memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
181 #define ADD_FORMAT(f, fmt, type, list, nb) \
185 if (!(*f) && !(*f = av_mallocz(sizeof(**f)))) \
186 return AVERROR(ENOMEM); \
188 fmts = av_realloc((*f)->list, \
189 sizeof(*(*f)->list) * ((*f)->nb + 1));\
191 return AVERROR(ENOMEM); \
194 (*f)->list[(*f)->nb++] = fmt; \
198 int ff_add_format(AVFilterFormats **avff, int fmt)
200 ADD_FORMAT(avff, fmt, int, formats, format_count);
203 int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
205 ADD_FORMAT(l, channel_layout, uint64_t, channel_layouts, nb_channel_layouts);
208 AVFilterFormats *ff_all_formats(enum AVMediaType type)
210 AVFilterFormats *ret = NULL;
212 int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
213 type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
215 for (fmt = 0; fmt < num_formats; fmt++)
216 if ((type != AVMEDIA_TYPE_VIDEO) ||
217 (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
218 ff_add_format(&ret, fmt);
223 AVFilterFormats *ff_all_samplerates(void)
225 AVFilterFormats *ret = av_mallocz(sizeof(*ret));
229 AVFilterChannelLayouts *ff_all_channel_layouts(void)
231 AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
235 #define FORMATS_REF(f, ref) \
238 f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount); \
239 f->refs[f->refcount-1] = ref; \
242 void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
247 void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
252 #define FIND_REF_INDEX(ref, idx) \
255 for (i = 0; i < (*ref)->refcount; i ++) \
256 if((*ref)->refs[i] == ref) { \
262 #define FORMATS_UNREF(ref, list) \
269 FIND_REF_INDEX(ref, idx); \
272 memmove((*ref)->refs + idx, (*ref)->refs + idx + 1, \
273 sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
275 if(!--(*ref)->refcount) { \
276 av_free((*ref)->list); \
277 av_free((*ref)->refs); \
283 void ff_formats_unref(AVFilterFormats **ref)
285 FORMATS_UNREF(ref, formats);
288 void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
290 FORMATS_UNREF(ref, channel_layouts);
293 #define FORMATS_CHANGEREF(oldref, newref) \
297 FIND_REF_INDEX(oldref, idx); \
300 (*oldref)->refs[idx] = newref; \
306 void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
307 AVFilterChannelLayouts **newref)
309 FORMATS_CHANGEREF(oldref, newref);
312 void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
314 FORMATS_CHANGEREF(oldref, newref);
317 #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
321 for (i = 0; i < ctx->input_count; i++) { \
322 if (ctx->inputs[i]) { \
323 ref(fmts, &ctx->inputs[i]->out_fmts); \
327 for (i = 0; i < ctx->output_count; i++) { \
328 if (ctx->outputs[i]) { \
329 ref(fmts, &ctx->outputs[i]->in_fmts); \
335 av_freep(&fmts->list); \
336 av_freep(&fmts->refs); \
341 void ff_set_common_channel_layouts(AVFilterContext *ctx,
342 AVFilterChannelLayouts *layouts)
344 SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
345 ff_channel_layouts_ref, channel_layouts);
348 void ff_set_common_samplerates(AVFilterContext *ctx,
349 AVFilterFormats *samplerates)
351 SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
352 ff_formats_ref, formats);
356 * A helper for query_formats() which sets all links to the same list of
357 * formats. If there are no links hooked to this filter, the list of formats is
360 void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
362 SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
363 ff_formats_ref, formats);
366 int ff_default_query_formats(AVFilterContext *ctx)
368 enum AVMediaType type = ctx->inputs && ctx->inputs [0] ? ctx->inputs [0]->type :
369 ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
372 ff_set_common_formats(ctx, ff_all_formats(type));
373 if (type == AVMEDIA_TYPE_AUDIO) {
374 ff_set_common_channel_layouts(ctx, ff_all_channel_layouts());
375 ff_set_common_samplerates(ctx, ff_all_samplerates());
381 #if FF_API_FILTERS_PUBLIC
382 int avfilter_default_query_formats(AVFilterContext *ctx)
384 return ff_default_query_formats(ctx);
386 void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
388 ff_set_common_formats(ctx, formats);
390 AVFilterFormats *avfilter_make_format_list(const int *fmts)
392 return ff_make_format_list(fmts);
394 int avfilter_add_format(AVFilterFormats **avff, int fmt)
396 return ff_add_format(avff, fmt);
398 AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
400 return ff_all_formats(type);
402 AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
404 return ff_merge_formats(a, b);
406 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
408 ff_formats_ref(f, ref);
410 void avfilter_formats_unref(AVFilterFormats **ref)
412 ff_formats_unref(ref);
414 void avfilter_formats_changeref(AVFilterFormats **oldref,
415 AVFilterFormats **newref)
417 ff_formats_changeref(oldref, newref);