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/eval.h"
23 #include "libavutil/pixdesc.h"
24 #include "libavutil/audioconvert.h"
29 * Add all refs from a to ret and destroy a.
31 static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
35 for (i = 0; i < a->refcount; i++) {
36 ret->refs[ret->refcount] = a->refs[i];
37 *ret->refs[ret->refcount++] = ret;
45 AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
52 ret = av_mallocz(sizeof(AVFilterFormats));
54 /* merge list of formats */
55 ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
57 for (i = 0; i < a->format_count; i++)
58 for (j = 0; j < b->format_count; j++)
59 if (a->formats[i] == b->formats[j])
60 ret->formats[k++] = a->formats[i];
62 ret->format_count = k;
63 /* check that there was at least one common format */
64 if (!ret->format_count) {
65 av_free(ret->formats);
70 ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
78 int ff_fmt_is_in(int fmt, const int *fmts)
82 for (p = fmts; *p != -1; p++) {
89 #define MAKE_FORMAT_LIST() \
90 AVFilterFormats *formats; \
93 for (count = 0; fmts[count] != -1; count++) \
95 formats = av_mallocz(sizeof(AVFilterFormats)); \
96 if (!formats) return NULL; \
97 formats->format_count = count; \
99 formats->formats = av_malloc(sizeof(*formats->formats)*count); \
100 if (!formats->formats) { \
106 AVFilterFormats *avfilter_make_format_list(const int *fmts)
110 formats->formats[count] = fmts[count];
115 AVFilterFormats *avfilter_make_format64_list(const int64_t *fmts)
119 memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
124 int avfilter_add_format(AVFilterFormats **avff, int64_t fmt)
128 if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
129 return AVERROR(ENOMEM);
131 fmts = av_realloc((*avff)->formats,
132 sizeof(*(*avff)->formats) * ((*avff)->format_count+1));
134 return AVERROR(ENOMEM);
136 (*avff)->formats = fmts;
137 (*avff)->formats[(*avff)->format_count++] = fmt;
141 AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
143 AVFilterFormats *ret = NULL;
145 int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB :
146 type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
148 for (fmt = 0; fmt < num_formats; fmt++)
149 if ((type != AVMEDIA_TYPE_VIDEO) ||
150 (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
151 avfilter_add_format(&ret, fmt);
156 AVFilterFormats *avfilter_all_channel_layouts(void)
158 static int64_t chlayouts[] = {
161 AV_CH_LAYOUT_4POINT0,
163 AV_CH_LAYOUT_5POINT0,
164 AV_CH_LAYOUT_5POINT0_BACK,
165 AV_CH_LAYOUT_5POINT1,
166 AV_CH_LAYOUT_5POINT1_BACK,
167 AV_CH_LAYOUT_5POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX,
168 AV_CH_LAYOUT_7POINT1,
169 AV_CH_LAYOUT_7POINT1_WIDE,
170 AV_CH_LAYOUT_7POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX,
174 return avfilter_make_format64_list(chlayouts);
177 AVFilterFormats *avfilter_all_packing_formats(void)
179 static int packing[] = {
185 return avfilter_make_format_list(packing);
188 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
191 f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
192 f->refs[f->refcount-1] = ref;
195 static int find_ref_index(AVFilterFormats **ref)
198 for (i = 0; i < (*ref)->refcount; i++)
199 if ((*ref)->refs[i] == ref)
204 void avfilter_formats_unref(AVFilterFormats **ref)
211 idx = find_ref_index(ref);
214 memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
215 sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
217 if (!--(*ref)->refcount) {
218 av_free((*ref)->formats);
219 av_free((*ref)->refs);
225 void avfilter_formats_changeref(AVFilterFormats **oldref,
226 AVFilterFormats **newref)
228 int idx = find_ref_index(oldref);
231 (*oldref)->refs[idx] = newref;
237 /* internal functions for parsing audio format arguments */
239 int ff_parse_pixel_format(enum PixelFormat *ret, const char *arg, void *log_ctx)
242 int pix_fmt = av_get_pix_fmt(arg);
243 if (pix_fmt == PIX_FMT_NONE) {
244 pix_fmt = strtol(arg, &tail, 0);
245 if (*tail || (unsigned)pix_fmt >= PIX_FMT_NB) {
246 av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
247 return AVERROR(EINVAL);
254 int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
257 int sfmt = av_get_sample_fmt(arg);
258 if (sfmt == AV_SAMPLE_FMT_NONE) {
259 sfmt = strtol(arg, &tail, 0);
260 if (*tail || (unsigned)sfmt >= AV_SAMPLE_FMT_NB) {
261 av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
262 return AVERROR(EINVAL);
269 int ff_parse_sample_rate(unsigned *ret, const char *arg, void *log_ctx)
272 double srate = av_strtod(arg, &tail);
273 if (*tail || srate < 1 || (int)srate != srate) {
274 av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
275 return AVERROR(EINVAL);
281 int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
284 int64_t chlayout = av_get_channel_layout(arg);
286 chlayout = strtol(arg, &tail, 10);
287 if (*tail || chlayout == 0) {
288 av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
289 return AVERROR(EINVAL);
296 int ff_parse_packing_format(int *ret, const char *arg, void *log_ctx)
299 int planar = strtol(arg, &tail, 10);
301 planar = !strcmp(arg, "packed") ? 0:
302 !strcmp(arg, "planar") ? 1: -1;
305 if (planar != 0 && planar != 1) {
306 av_log(log_ctx, AV_LOG_ERROR, "Invalid packing format '%s'\n", arg);
307 return AVERROR(EINVAL);