]> git.sesse.net Git - ffmpeg/blob - libavfilter/formats.c
Fix compilation with --disable-everything --enable-outdev=alsa.
[ffmpeg] / libavfilter / formats.c
1 /*
2  * Filter layer - format negotiation
3  * Copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "libavutil/eval.h"
23 #include "libavutil/pixdesc.h"
24 #include "libavutil/audioconvert.h"
25 #include "avfilter.h"
26 #include "internal.h"
27
28 /**
29  * Add all refs from a to ret and destroy a.
30  */
31 static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
32 {
33     int i;
34
35     for (i = 0; i < a->refcount; i++) {
36         ret->refs[ret->refcount] = a->refs[i];
37         *ret->refs[ret->refcount++] = ret;
38     }
39
40     av_free(a->refs);
41     av_free(a->formats);
42     av_free(a);
43 }
44
45 AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
46 {
47     AVFilterFormats *ret;
48     unsigned i, j, k = 0;
49
50     if (a == b) return a;
51
52     ret = av_mallocz(sizeof(AVFilterFormats));
53
54     /* merge list of formats */
55     ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
56                                                            b->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];
61
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);
66         av_free(ret);
67         return NULL;
68     }
69
70     ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
71
72     merge_ref(ret, a);
73     merge_ref(ret, b);
74
75     return ret;
76 }
77
78 int ff_fmt_is_in(int fmt, const int *fmts)
79 {
80     const int *p;
81
82     for (p = fmts; *p != -1; p++) {
83         if (fmt == *p)
84             return 1;
85     }
86     return 0;
87 }
88
89 #define MAKE_FORMAT_LIST()                                              \
90     AVFilterFormats *formats;                                           \
91     int count = 0;                                                      \
92     if (fmts)                                                           \
93         for (count = 0; fmts[count] != -1; count++)                     \
94             ;                                                           \
95     formats = av_mallocz(sizeof(AVFilterFormats));                      \
96     if (!formats) return NULL;                                          \
97     formats->format_count = count;                                      \
98     if (count) {                                                        \
99         formats->formats = av_malloc(sizeof(*formats->formats)*count);  \
100         if (!formats->formats) {                                        \
101             av_free(formats);                                           \
102             return NULL;                                                \
103         }                                                               \
104     }
105
106 AVFilterFormats *avfilter_make_format_list(const int *fmts)
107 {
108     MAKE_FORMAT_LIST();
109     while (count--)
110         formats->formats[count] = fmts[count];
111
112     return formats;
113 }
114
115 AVFilterFormats *avfilter_make_format64_list(const int64_t *fmts)
116 {
117     MAKE_FORMAT_LIST();
118     if (count)
119         memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
120
121     return formats;
122 }
123
124 int avfilter_add_format(AVFilterFormats **avff, int64_t fmt)
125 {
126     int64_t *fmts;
127
128     if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
129         return AVERROR(ENOMEM);
130
131     fmts = av_realloc((*avff)->formats,
132                       sizeof(*(*avff)->formats) * ((*avff)->format_count+1));
133     if (!fmts)
134         return AVERROR(ENOMEM);
135
136     (*avff)->formats = fmts;
137     (*avff)->formats[(*avff)->format_count++] = fmt;
138     return 0;
139 }
140
141 #if FF_API_OLD_ALL_FORMATS_API
142 AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
143 {
144     return avfilter_make_all_formats(type);
145 }
146 #endif
147
148 AVFilterFormats *avfilter_make_all_formats(enum AVMediaType type)
149 {
150     AVFilterFormats *ret = NULL;
151     int fmt;
152     int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB    :
153                       type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
154
155     for (fmt = 0; fmt < num_formats; fmt++)
156         if ((type != AVMEDIA_TYPE_VIDEO) ||
157             (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
158             avfilter_add_format(&ret, fmt);
159
160     return ret;
161 }
162
163 const int64_t avfilter_all_channel_layouts[] = {
164 #include "all_channel_layouts.h"
165     -1
166 };
167
168 AVFilterFormats *avfilter_make_all_channel_layouts(void)
169 {
170     return avfilter_make_format64_list(avfilter_all_channel_layouts);
171 }
172
173 AVFilterFormats *avfilter_make_all_packing_formats(void)
174 {
175     static int packing[] = {
176         AVFILTER_PACKED,
177         AVFILTER_PLANAR,
178         -1,
179     };
180
181     return avfilter_make_format_list(packing);
182 }
183
184 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
185 {
186     *ref = f;
187     f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
188     f->refs[f->refcount-1] = ref;
189 }
190
191 static int find_ref_index(AVFilterFormats **ref)
192 {
193     int i;
194     for (i = 0; i < (*ref)->refcount; i++)
195         if ((*ref)->refs[i] == ref)
196             return i;
197     return -1;
198 }
199
200 void avfilter_formats_unref(AVFilterFormats **ref)
201 {
202     int idx;
203
204     if (!*ref)
205         return;
206
207     idx = find_ref_index(ref);
208
209     if (idx >= 0)
210         memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
211             sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
212
213     if (!--(*ref)->refcount) {
214         av_free((*ref)->formats);
215         av_free((*ref)->refs);
216         av_free(*ref);
217     }
218     *ref = NULL;
219 }
220
221 void avfilter_formats_changeref(AVFilterFormats **oldref,
222                                 AVFilterFormats **newref)
223 {
224     int idx = find_ref_index(oldref);
225
226     if (idx >= 0) {
227         (*oldref)->refs[idx] = newref;
228         *newref = *oldref;
229         *oldref = NULL;
230     }
231 }
232
233 /* internal functions for parsing audio format arguments */
234
235 int ff_parse_pixel_format(enum PixelFormat *ret, const char *arg, void *log_ctx)
236 {
237     char *tail;
238     int pix_fmt = av_get_pix_fmt(arg);
239     if (pix_fmt == PIX_FMT_NONE) {
240         pix_fmt = strtol(arg, &tail, 0);
241         if (*tail || (unsigned)pix_fmt >= PIX_FMT_NB) {
242             av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
243             return AVERROR(EINVAL);
244         }
245     }
246     *ret = pix_fmt;
247     return 0;
248 }
249
250 int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
251 {
252     char *tail;
253     int sfmt = av_get_sample_fmt(arg);
254     if (sfmt == AV_SAMPLE_FMT_NONE) {
255         sfmt = strtol(arg, &tail, 0);
256         if (*tail || (unsigned)sfmt >= AV_SAMPLE_FMT_NB) {
257             av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
258             return AVERROR(EINVAL);
259         }
260     }
261     *ret = sfmt;
262     return 0;
263 }
264
265 int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
266 {
267     char *tail;
268     double srate = av_strtod(arg, &tail);
269     if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
270         av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
271         return AVERROR(EINVAL);
272     }
273     *ret = srate;
274     return 0;
275 }
276
277 int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
278 {
279     char *tail;
280     int64_t chlayout = av_get_channel_layout(arg);
281     if (chlayout == 0) {
282         chlayout = strtol(arg, &tail, 10);
283         if (*tail || chlayout == 0) {
284             av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
285             return AVERROR(EINVAL);
286         }
287     }
288     *ret = chlayout;
289     return 0;
290 }
291
292 int ff_parse_packing_format(int *ret, const char *arg, void *log_ctx)
293 {
294     char *tail;
295     int planar = strtol(arg, &tail, 10);
296     if (*tail) {
297         planar = !strcmp(arg, "packed") ? 0:
298                  !strcmp(arg, "planar") ? 1: -1;
299     }
300
301     if (planar != 0 && planar != 1) {
302         av_log(log_ctx, AV_LOG_ERROR, "Invalid packing format '%s'\n", arg);
303         return AVERROR(EINVAL);
304     }
305     *ret = planar;
306     return 0;
307 }
308
309 #ifdef TEST
310
311 #undef printf
312
313 int main(void)
314 {
315     const int64_t *cl;
316     char buf[512];
317
318     for (cl = avfilter_all_channel_layouts; *cl != -1; cl++) {
319         av_get_channel_layout_string(buf, sizeof(buf), -1, *cl);
320         printf("%s\n", buf);
321     }
322
323     return 0;
324 }
325
326 #endif