]> git.sesse.net Git - ffmpeg/blob - libavfilter/formats.c
lavfi: add channelsplit audio filter.
[ffmpeg] / libavfilter / formats.c
1 /*
2  * Filter layer - format negotiation
3  * Copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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/pixdesc.h"
23 #include "avfilter.h"
24 #include "internal.h"
25 #include "formats.h"
26
27 /**
28  * Add all refs from a to ret and destroy a.
29  */
30 #define MERGE_REF(ret, a, fmts, type, fail)                                \
31 do {                                                                       \
32     type ***tmp;                                                           \
33     int i;                                                                 \
34                                                                            \
35     if (!(tmp = av_realloc(ret->refs,                                      \
36                            sizeof(*tmp) * (ret->refcount + a->refcount)))) \
37         goto fail;                                                         \
38     ret->refs = tmp;                                                       \
39                                                                            \
40     for (i = 0; i < a->refcount; i ++) {                                   \
41         ret->refs[ret->refcount] = a->refs[i];                             \
42         *ret->refs[ret->refcount++] = ret;                                 \
43     }                                                                      \
44                                                                            \
45     av_freep(&a->refs);                                                    \
46     av_freep(&a->fmts);                                                    \
47     av_freep(&a);                                                          \
48 } while (0)
49
50 /**
51  * Add all formats common for a and b to ret, copy the refs and destroy
52  * a and b.
53  */
54 #define MERGE_FORMATS(ret, a, b, fmts, nb, type, fail)                          \
55 do {                                                                            \
56     int i, j, k = 0, count = FFMIN(a->nb, b->nb);                               \
57                                                                                 \
58     if (!(ret = av_mallocz(sizeof(*ret))))                                      \
59         goto fail;                                                              \
60                                                                                 \
61     if (count) {                                                                \
62         if (!(ret->fmts = av_malloc(sizeof(*ret->fmts) * count)))               \
63             goto fail;                                                          \
64         for (i = 0; i < a->nb; i++)                                             \
65             for (j = 0; j < b->nb; j++)                                         \
66                 if (a->fmts[i] == b->fmts[j])                                   \
67                     ret->fmts[k++] = a->fmts[i];                                \
68                                                                                 \
69         ret->nb = k;                                                            \
70     }                                                                           \
71     /* check that there was at least one common format */                       \
72     if (!ret->nb)                                                               \
73         goto fail;                                                              \
74                                                                                 \
75     MERGE_REF(ret, a, fmts, type, fail);                                        \
76     MERGE_REF(ret, b, fmts, type, fail);                                        \
77 } while (0)
78
79 AVFilterFormats *ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
80 {
81     AVFilterFormats *ret = NULL;
82
83     if (a == b)
84         return a;
85
86     MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
87
88     return ret;
89 fail:
90     if (ret) {
91         av_freep(&ret->refs);
92         av_freep(&ret->formats);
93     }
94     av_freep(&ret);
95     return NULL;
96 }
97
98 AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a,
99                                       AVFilterFormats *b)
100 {
101     AVFilterFormats *ret = NULL;
102
103     if (a == b) return a;
104
105     if (a->format_count && b->format_count) {
106         MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
107     } else if (a->format_count) {
108         MERGE_REF(a, b, formats, AVFilterFormats, fail);
109         ret = a;
110     } else {
111         MERGE_REF(b, a, formats, AVFilterFormats, fail);
112         ret = b;
113     }
114
115     return ret;
116 fail:
117     if (ret) {
118         av_freep(&ret->refs);
119         av_freep(&ret->formats);
120     }
121     av_freep(&ret);
122     return NULL;
123 }
124
125 AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,
126                                                  AVFilterChannelLayouts *b)
127 {
128     AVFilterChannelLayouts *ret = NULL;
129
130     if (a == b) return a;
131
132     if (a->nb_channel_layouts && b->nb_channel_layouts) {
133         MERGE_FORMATS(ret, a, b, channel_layouts, nb_channel_layouts,
134                       AVFilterChannelLayouts, fail);
135     } else if (a->nb_channel_layouts) {
136         MERGE_REF(a, b, channel_layouts, AVFilterChannelLayouts, fail);
137         ret = a;
138     } else {
139         MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, fail);
140         ret = b;
141     }
142
143     return ret;
144 fail:
145     if (ret) {
146         av_freep(&ret->refs);
147         av_freep(&ret->channel_layouts);
148     }
149     av_freep(&ret);
150     return NULL;
151 }
152
153 int ff_fmt_is_in(int fmt, const int *fmts)
154 {
155     const int *p;
156
157     for (p = fmts; *p != PIX_FMT_NONE; p++) {
158         if (fmt == *p)
159             return 1;
160     }
161     return 0;
162 }
163
164 AVFilterFormats *ff_make_format_list(const int *fmts)
165 {
166     AVFilterFormats *formats;
167     int count;
168
169     for (count = 0; fmts[count] != -1; count++)
170         ;
171
172     formats               = av_mallocz(sizeof(*formats));
173     if (count)
174         formats->formats  = av_malloc(sizeof(*formats->formats) * count);
175     formats->format_count = count;
176     memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
177
178     return formats;
179 }
180
181 #define ADD_FORMAT(f, fmt, type, list, nb)                  \
182 do {                                                        \
183     type *fmts;                                             \
184                                                             \
185     if (!(*f) && !(*f = av_mallocz(sizeof(**f))))           \
186         return AVERROR(ENOMEM);                             \
187                                                             \
188     fmts = av_realloc((*f)->list,                           \
189                       sizeof(*(*f)->list) * ((*f)->nb + 1));\
190     if (!fmts)                                              \
191         return AVERROR(ENOMEM);                             \
192                                                             \
193     (*f)->list = fmts;                                      \
194     (*f)->list[(*f)->nb++] = fmt;                           \
195     return 0;                                               \
196 } while (0)
197
198 int ff_add_format(AVFilterFormats **avff, int fmt)
199 {
200     ADD_FORMAT(avff, fmt, int, formats, format_count);
201 }
202
203 int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
204 {
205     ADD_FORMAT(l, channel_layout, uint64_t, channel_layouts, nb_channel_layouts);
206 }
207
208 AVFilterFormats *ff_all_formats(enum AVMediaType type)
209 {
210     AVFilterFormats *ret = NULL;
211     int fmt;
212     int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB    :
213                       type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
214
215     for (fmt = 0; fmt < num_formats; fmt++)
216         if ((type != AVMEDIA_TYPE_VIDEO) ||
217             (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
218             ff_add_format(&ret, fmt);
219
220     return ret;
221 }
222
223 AVFilterFormats *ff_planar_sample_fmts(void)
224 {
225     AVFilterFormats *ret = NULL;
226     int fmt;
227
228     for (fmt = 0; fmt < AV_SAMPLE_FMT_NB; fmt++)
229         if (av_sample_fmt_is_planar(fmt))
230             ff_add_format(&ret, fmt);
231
232     return ret;
233 }
234
235 AVFilterFormats *ff_all_samplerates(void)
236 {
237     AVFilterFormats *ret = av_mallocz(sizeof(*ret));
238     return ret;
239 }
240
241 AVFilterChannelLayouts *ff_all_channel_layouts(void)
242 {
243     AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
244     return ret;
245 }
246
247 #define FORMATS_REF(f, ref)                                          \
248 do {                                                                 \
249     *ref = f;                                                        \
250     f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount); \
251     f->refs[f->refcount-1] = ref;                                    \
252 } while (0)
253
254 void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
255 {
256     FORMATS_REF(f, ref);
257 }
258
259 void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
260 {
261     FORMATS_REF(f, ref);
262 }
263
264 #define FIND_REF_INDEX(ref, idx)            \
265 do {                                        \
266     int i;                                  \
267     for (i = 0; i < (*ref)->refcount; i ++) \
268         if((*ref)->refs[i] == ref) {        \
269             idx = i;                        \
270             break;                          \
271         }                                   \
272 } while (0)
273
274 #define FORMATS_UNREF(ref, list)                                   \
275 do {                                                               \
276     int idx = -1;                                                  \
277                                                                    \
278     if (!*ref)                                                     \
279         return;                                                    \
280                                                                    \
281     FIND_REF_INDEX(ref, idx);                                      \
282                                                                    \
283     if (idx >= 0)                                                  \
284         memmove((*ref)->refs + idx, (*ref)->refs + idx + 1,        \
285             sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
286                                                                    \
287     if(!--(*ref)->refcount) {                                      \
288         av_free((*ref)->list);                                     \
289         av_free((*ref)->refs);                                     \
290         av_free(*ref);                                             \
291     }                                                              \
292     *ref = NULL;                                                   \
293 } while (0)
294
295 void ff_formats_unref(AVFilterFormats **ref)
296 {
297     FORMATS_UNREF(ref, formats);
298 }
299
300 void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
301 {
302     FORMATS_UNREF(ref, channel_layouts);
303 }
304
305 #define FORMATS_CHANGEREF(oldref, newref)       \
306 do {                                            \
307     int idx = -1;                               \
308                                                 \
309     FIND_REF_INDEX(oldref, idx);                \
310                                                 \
311     if (idx >= 0) {                             \
312         (*oldref)->refs[idx] = newref;          \
313         *newref = *oldref;                      \
314         *oldref = NULL;                         \
315     }                                           \
316 } while (0)
317
318 void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
319                                   AVFilterChannelLayouts **newref)
320 {
321     FORMATS_CHANGEREF(oldref, newref);
322 }
323
324 void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
325 {
326     FORMATS_CHANGEREF(oldref, newref);
327 }
328
329 #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
330 {                                                                   \
331     int count = 0, i;                                               \
332                                                                     \
333     for (i = 0; i < ctx->input_count; i++) {                        \
334         if (ctx->inputs[i]) {                                       \
335             ref(fmts, &ctx->inputs[i]->out_fmts);                   \
336             count++;                                                \
337         }                                                           \
338     }                                                               \
339     for (i = 0; i < ctx->output_count; i++) {                       \
340         if (ctx->outputs[i]) {                                      \
341             ref(fmts, &ctx->outputs[i]->in_fmts);                   \
342             count++;                                                \
343         }                                                           \
344     }                                                               \
345                                                                     \
346     if (!count) {                                                   \
347         av_freep(&fmts->list);                                      \
348         av_freep(&fmts->refs);                                      \
349         av_freep(&fmts);                                            \
350     }                                                               \
351 }
352
353 void ff_set_common_channel_layouts(AVFilterContext *ctx,
354                                    AVFilterChannelLayouts *layouts)
355 {
356     SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
357                        ff_channel_layouts_ref, channel_layouts);
358 }
359
360 void ff_set_common_samplerates(AVFilterContext *ctx,
361                                AVFilterFormats *samplerates)
362 {
363     SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
364                        ff_formats_ref, formats);
365 }
366
367 /**
368  * A helper for query_formats() which sets all links to the same list of
369  * formats. If there are no links hooked to this filter, the list of formats is
370  * freed.
371  */
372 void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
373 {
374     SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
375                        ff_formats_ref, formats);
376 }
377
378 int ff_default_query_formats(AVFilterContext *ctx)
379 {
380     enum AVMediaType type = ctx->inputs  && ctx->inputs [0] ? ctx->inputs [0]->type :
381                             ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
382                             AVMEDIA_TYPE_VIDEO;
383
384     ff_set_common_formats(ctx, ff_all_formats(type));
385     if (type == AVMEDIA_TYPE_AUDIO) {
386         ff_set_common_channel_layouts(ctx, ff_all_channel_layouts());
387         ff_set_common_samplerates(ctx, ff_all_samplerates());
388     }
389
390     return 0;
391 }
392
393 #if FF_API_FILTERS_PUBLIC
394 int avfilter_default_query_formats(AVFilterContext *ctx)
395 {
396     return ff_default_query_formats(ctx);
397 }
398 void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
399 {
400     ff_set_common_formats(ctx, formats);
401 }
402 AVFilterFormats *avfilter_make_format_list(const int *fmts)
403 {
404     return ff_make_format_list(fmts);
405 }
406 int avfilter_add_format(AVFilterFormats **avff, int fmt)
407 {
408     return ff_add_format(avff, fmt);
409 }
410 AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
411 {
412     return ff_all_formats(type);
413 }
414 AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
415 {
416     return ff_merge_formats(a, b);
417 }
418 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
419 {
420     ff_formats_ref(f, ref);
421 }
422 void avfilter_formats_unref(AVFilterFormats **ref)
423 {
424     ff_formats_unref(ref);
425 }
426 void avfilter_formats_changeref(AVFilterFormats **oldref,
427                                 AVFilterFormats **newref)
428 {
429     ff_formats_changeref(oldref, newref);
430 }
431 #endif