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