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/pixdesc.h"
23 #include "libavutil/audioconvert.h"
28 * Add all refs from a to ret and destroy a.
30 static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
34 for (i = 0; i < a->refcount; i++) {
35 ret->refs[ret->refcount] = a->refs[i];
36 *ret->refs[ret->refcount++] = ret;
44 AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
51 ret = av_mallocz(sizeof(AVFilterFormats));
53 /* merge list of formats */
54 ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
56 for (i = 0; i < a->format_count; i++)
57 for (j = 0; j < b->format_count; j++)
58 if (a->formats[i] == b->formats[j])
59 ret->formats[k++] = a->formats[i];
61 ret->format_count = k;
62 /* check that there was at least one common format */
63 if (!ret->format_count) {
64 av_free(ret->formats);
69 ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
77 int ff_fmt_is_in(int fmt, const int *fmts)
81 for (p = fmts; *p != -1; p++) {
88 #define MAKE_FORMAT_LIST() \
89 AVFilterFormats *formats; \
92 for (count = 0; fmts[count] != -1; count++) \
94 formats = av_mallocz(sizeof(AVFilterFormats)); \
95 if (!formats) return NULL; \
96 formats->format_count = count; \
98 formats->formats = av_malloc(sizeof(*formats->formats)*count); \
99 if (!formats->formats) { \
105 AVFilterFormats *avfilter_make_format_list(const int *fmts)
109 formats->formats[count] = fmts[count];
114 AVFilterFormats *avfilter_make_format64_list(const int64_t *fmts)
118 memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
123 int avfilter_add_format(AVFilterFormats **avff, int64_t fmt)
127 if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
128 return AVERROR(ENOMEM);
130 fmts = av_realloc((*avff)->formats,
131 sizeof(*(*avff)->formats) * ((*avff)->format_count+1));
133 return AVERROR(ENOMEM);
135 (*avff)->formats = fmts;
136 (*avff)->formats[(*avff)->format_count++] = fmt;
140 AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
142 AVFilterFormats *ret = NULL;
144 int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
145 type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
147 for (fmt = 0; fmt < num_formats; fmt++)
148 if ((type != AVMEDIA_TYPE_VIDEO) ||
149 (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
150 avfilter_add_format(&ret, fmt);
155 AVFilterFormats *avfilter_all_channel_layouts(void)
157 static int64_t chlayouts[] = {
160 AV_CH_LAYOUT_4POINT0,
162 AV_CH_LAYOUT_5POINT0,
163 AV_CH_LAYOUT_5POINT0_BACK,
164 AV_CH_LAYOUT_5POINT1,
165 AV_CH_LAYOUT_5POINT1_BACK,
166 AV_CH_LAYOUT_5POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX,
167 AV_CH_LAYOUT_7POINT1,
168 AV_CH_LAYOUT_7POINT1_WIDE,
169 AV_CH_LAYOUT_7POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX,
173 return avfilter_make_format64_list(chlayouts);
176 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
179 f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
180 f->refs[f->refcount-1] = ref;
183 static int find_ref_index(AVFilterFormats **ref)
186 for (i = 0; i < (*ref)->refcount; i++)
187 if ((*ref)->refs[i] == ref)
192 void avfilter_formats_unref(AVFilterFormats **ref)
199 idx = find_ref_index(ref);
202 memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
203 sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
205 if (!--(*ref)->refcount) {
206 av_free((*ref)->formats);
207 av_free((*ref)->refs);
213 void avfilter_formats_changeref(AVFilterFormats **oldref,
214 AVFilterFormats **newref)
216 int idx = find_ref_index(oldref);
219 (*oldref)->refs[idx] = newref;