]> git.sesse.net Git - ffmpeg/blob - libavfilter/formats.c
Autodetect LOAS in transport streams.
[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 AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
142 {
143     AVFilterFormats *ret = NULL;
144     int fmt;
145     int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB    :
146                       type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
147
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);
152
153     return ret;
154 }
155
156 AVFilterFormats *avfilter_all_channel_layouts(void)
157 {
158     static int64_t chlayouts[] = {
159         AV_CH_LAYOUT_MONO,
160         AV_CH_LAYOUT_STEREO,
161         AV_CH_LAYOUT_4POINT0,
162         AV_CH_LAYOUT_QUAD,
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,
171         -1,
172     };
173
174     return avfilter_make_format64_list(chlayouts);
175 }
176
177 AVFilterFormats *avfilter_all_packing_formats(void)
178 {
179     static int packing[] = {
180         AVFILTER_PACKED,
181         AVFILTER_PLANAR,
182         -1,
183     };
184
185     return avfilter_make_format_list(packing);
186 }
187
188 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
189 {
190     *ref = f;
191     f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
192     f->refs[f->refcount-1] = ref;
193 }
194
195 static int find_ref_index(AVFilterFormats **ref)
196 {
197     int i;
198     for (i = 0; i < (*ref)->refcount; i++)
199         if ((*ref)->refs[i] == ref)
200             return i;
201     return -1;
202 }
203
204 void avfilter_formats_unref(AVFilterFormats **ref)
205 {
206     int idx;
207
208     if (!*ref)
209         return;
210
211     idx = find_ref_index(ref);
212
213     if (idx >= 0)
214         memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
215             sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
216
217     if (!--(*ref)->refcount) {
218         av_free((*ref)->formats);
219         av_free((*ref)->refs);
220         av_free(*ref);
221     }
222     *ref = NULL;
223 }
224
225 void avfilter_formats_changeref(AVFilterFormats **oldref,
226                                 AVFilterFormats **newref)
227 {
228     int idx = find_ref_index(oldref);
229
230     if (idx >= 0) {
231         (*oldref)->refs[idx] = newref;
232         *newref = *oldref;
233         *oldref = NULL;
234     }
235 }
236
237 /* internal functions for parsing audio format arguments */
238
239 int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
240 {
241     char *tail;
242     int sfmt = av_get_sample_fmt(arg);
243     if (sfmt == AV_SAMPLE_FMT_NONE) {
244         sfmt = strtol(arg, &tail, 0);
245         if (*tail || (unsigned)sfmt >= AV_SAMPLE_FMT_NB) {
246             av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
247             return AVERROR(EINVAL);
248         }
249     }
250     *ret = sfmt;
251     return 0;
252 }
253
254 int ff_parse_sample_rate(unsigned *ret, const char *arg, void *log_ctx)
255 {
256     char *tail;
257     double srate = av_strtod(arg, &tail);
258     if (*tail || srate < 1 || (int)srate != srate) {
259         av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
260         return AVERROR(EINVAL);
261     }
262     *ret = srate;
263     return 0;
264 }
265
266 int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
267 {
268     char *tail;
269     int64_t chlayout = av_get_channel_layout(arg);
270     if (chlayout == 0) {
271         chlayout = strtol(arg, &tail, 10);
272         if (*tail || chlayout == 0) {
273             av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
274             return AVERROR(EINVAL);
275         }
276     }
277     *ret = chlayout;
278     return 0;
279 }
280
281 int ff_parse_packing_format(int *ret, const char *arg, void *log_ctx)
282 {
283     char *tail;
284     int planar = strtol(arg, &tail, 10);
285     if (*tail) {
286         planar = !strcmp(arg, "packed") ? 0:
287                  !strcmp(arg, "planar") ? 1: -1;
288     }
289
290     if (planar != 0 && planar != 1) {
291         av_log(log_ctx, AV_LOG_ERROR, "Invalid packing format '%s'\n", arg);
292         return AVERROR(EINVAL);
293     }
294     *ret = planar;
295     return 0;
296 }
297