lavfi: support unknown channel layouts.
[ffmpeg.git] / 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/avassert.h"
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/common.h"
25 #include "libavutil/eval.h"
26 #include "libavutil/pixdesc.h"
27 #include "libavutil/parseutils.h"
28 #include "avfilter.h"
29 #include "internal.h"
30 #include "formats.h"
31
32 #define KNOWN(l) (!FF_LAYOUT2COUNT(l)) /* for readability */
33
34 /**
35  * Add all refs from a to ret and destroy a.
36  */
37 #define MERGE_REF(ret, a, fmts, type, fail)                                \
38 do {                                                                       \
39     type ***tmp;                                                           \
40     int i;                                                                 \
41                                                                            \
42     if (!(tmp = av_realloc(ret->refs,                                      \
43                            sizeof(*tmp) * (ret->refcount + a->refcount)))) \
44         goto fail;                                                         \
45     ret->refs = tmp;                                                       \
46                                                                            \
47     for (i = 0; i < a->refcount; i ++) {                                   \
48         ret->refs[ret->refcount] = a->refs[i];                             \
49         *ret->refs[ret->refcount++] = ret;                                 \
50     }                                                                      \
51                                                                            \
52     av_freep(&a->refs);                                                    \
53     av_freep(&a->fmts);                                                    \
54     av_freep(&a);                                                          \
55 } while (0)
56
57 /**
58  * Add all formats common for a and b to ret, copy the refs and destroy
59  * a and b.
60  */
61 #define MERGE_FORMATS(ret, a, b, fmts, nb, type, fail)                          \
62 do {                                                                            \
63     int i, j, k = 0, count = FFMIN(a->nb, b->nb);                               \
64                                                                                 \
65     if (!(ret = av_mallocz(sizeof(*ret))))                                      \
66         goto fail;                                                              \
67                                                                                 \
68     if (count) {                                                                \
69         if (!(ret->fmts = av_malloc(sizeof(*ret->fmts) * count)))               \
70             goto fail;                                                          \
71         for (i = 0; i < a->nb; i++)                                             \
72             for (j = 0; j < b->nb; j++)                                         \
73                 if (a->fmts[i] == b->fmts[j]) {                                 \
74                     if(k >= FFMIN(a->nb, b->nb)){                               \
75                         av_log(NULL, AV_LOG_ERROR, "Duplicate formats in avfilter_merge_formats() detected\n"); \
76                         av_free(ret->fmts);                                     \
77                         av_free(ret);                                           \
78                         return NULL;                                            \
79                     }                                                           \
80                     ret->fmts[k++] = a->fmts[i];                                \
81                 }                                                               \
82     }                                                                           \
83     ret->nb = k;                                                                \
84     /* check that there was at least one common format */                       \
85     if (!ret->nb)                                                               \
86         goto fail;                                                              \
87                                                                                 \
88     MERGE_REF(ret, a, fmts, type, fail);                                        \
89     MERGE_REF(ret, b, fmts, type, fail);                                        \
90 } while (0)
91
92 AVFilterFormats *ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
93 {
94     AVFilterFormats *ret = NULL;
95
96     if (a == b)
97         return a;
98
99     MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
100
101     return ret;
102 fail:
103     if (ret) {
104         av_freep(&ret->refs);
105         av_freep(&ret->formats);
106     }
107     av_freep(&ret);
108     return NULL;
109 }
110
111 AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a,
112                                       AVFilterFormats *b)
113 {
114     AVFilterFormats *ret = NULL;
115
116     if (a == b) return a;
117
118     if (a->format_count && b->format_count) {
119         MERGE_FORMATS(ret, a, b, formats, format_count, AVFilterFormats, fail);
120     } else if (a->format_count) {
121         MERGE_REF(a, b, formats, AVFilterFormats, fail);
122         ret = a;
123     } else {
124         MERGE_REF(b, a, formats, AVFilterFormats, fail);
125         ret = b;
126     }
127
128     return ret;
129 fail:
130     if (ret) {
131         av_freep(&ret->refs);
132         av_freep(&ret->formats);
133     }
134     av_freep(&ret);
135     return NULL;
136 }
137
138 AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,
139                                                  AVFilterChannelLayouts *b)
140 {
141     AVFilterChannelLayouts *ret = NULL;
142     unsigned a_all = a->all_layouts + a->all_counts;
143     unsigned b_all = b->all_layouts + b->all_counts;
144     int ret_max, ret_nb = 0, i, j, round;
145
146     if (a == b) return a;
147
148     /* Put the most generic set in a, to avoid doing everything twice */
149     if (a_all < b_all) {
150         FFSWAP(AVFilterChannelLayouts *, a, b);
151         FFSWAP(unsigned, a_all, b_all);
152     }
153     if (a_all) {
154         if (a_all == 1 && !b_all) {
155             /* keep only known layouts in b; works also for b_all = 1 */
156             for (i = j = 0; i < b->nb_channel_layouts; i++)
157                 if (KNOWN(b->channel_layouts[i]))
158                     b->channel_layouts[j++] = b->channel_layouts[i];
159             b->nb_channel_layouts = j;
160         }
161         MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, fail);
162         return b;
163     }
164
165     ret_max = a->nb_channel_layouts + b->nb_channel_layouts;
166     if (!(ret = av_mallocz(sizeof(*ret))) ||
167         !(ret->channel_layouts = av_malloc(sizeof(*ret->channel_layouts) *
168                                            ret_max)))
169         goto fail;
170
171     /* a[known] intersect b[known] */
172     for (i = 0; i < a->nb_channel_layouts; i++) {
173         if (!KNOWN(a->channel_layouts[i]))
174             continue;
175         for (j = 0; j < b->nb_channel_layouts; j++) {
176             if (a->channel_layouts[i] == b->channel_layouts[j]) {
177                 ret->channel_layouts[ret_nb++] = a->channel_layouts[i];
178                 a->channel_layouts[i] = b->channel_layouts[j] = 0;
179             }
180         }
181     }
182     /* 1st round: a[known] intersect b[generic]
183        2nd round: a[generic] intersect b[known] */
184     for (round = 0; round < 2; round++) {
185         for (i = 0; i < a->nb_channel_layouts; i++) {
186             uint64_t fmt = a->channel_layouts[i], bfmt;
187             if (!fmt || !KNOWN(fmt))
188                 continue;
189             bfmt = FF_COUNT2LAYOUT(av_get_channel_layout_nb_channels(fmt));
190             for (j = 0; j < b->nb_channel_layouts; j++)
191                 if (b->channel_layouts[j] == bfmt)
192                     ret->channel_layouts[ret_nb++] = a->channel_layouts[i];
193         }
194         /* 1st round: swap to prepare 2nd round; 2nd round: put it back */
195         FFSWAP(AVFilterChannelLayouts *, a, b);
196     }
197     /* a[generic] intersect b[generic] */
198     for (i = 0; i < a->nb_channel_layouts; i++) {
199         if (KNOWN(a->channel_layouts[i]))
200             continue;
201         for (j = 0; j < b->nb_channel_layouts; j++)
202             if (a->channel_layouts[i] == b->channel_layouts[j])
203                 ret->channel_layouts[ret_nb++] = a->channel_layouts[i];
204     }
205
206     ret->nb_channel_layouts = ret_nb;
207     if (!ret->nb_channel_layouts)
208         goto fail;
209     MERGE_REF(ret, a, channel_layouts, AVFilterChannelLayouts, fail);
210     MERGE_REF(ret, b, channel_layouts, AVFilterChannelLayouts, fail);
211     return ret;
212
213 fail:
214     if (ret) {
215         av_freep(&ret->refs);
216         av_freep(&ret->channel_layouts);
217     }
218     av_freep(&ret);
219     return NULL;
220 }
221
222 int ff_fmt_is_in(int fmt, const int *fmts)
223 {
224     const int *p;
225
226     for (p = fmts; *p != -1; p++) {
227         if (fmt == *p)
228             return 1;
229     }
230     return 0;
231 }
232
233 #define COPY_INT_LIST(list_copy, list, type) {                          \
234     int count = 0;                                                      \
235     if (list)                                                           \
236         for (count = 0; list[count] != -1; count++)                     \
237             ;                                                           \
238     list_copy = av_calloc(count+1, sizeof(type));                       \
239     if (list_copy) {                                                    \
240         memcpy(list_copy, list, sizeof(type) * count);                  \
241         list_copy[count] = -1;                                          \
242     }                                                                   \
243 }
244
245 int *ff_copy_int_list(const int * const list)
246 {
247     int *ret = NULL;
248     COPY_INT_LIST(ret, list, int);
249     return ret;
250 }
251
252 int64_t *ff_copy_int64_list(const int64_t * const list)
253 {
254     int64_t *ret = NULL;
255     COPY_INT_LIST(ret, list, int64_t);
256     return ret;
257 }
258
259 #define MAKE_FORMAT_LIST(type, field, count_field)                      \
260     type *formats;                                                      \
261     int count = 0;                                                      \
262     if (fmts)                                                           \
263         for (count = 0; fmts[count] != -1; count++)                     \
264             ;                                                           \
265     formats = av_mallocz(sizeof(*formats));                             \
266     if (!formats) return NULL;                                          \
267     formats->count_field = count;                                       \
268     if (count) {                                                        \
269         formats->field = av_malloc(sizeof(*formats->field)*count);      \
270         if (!formats->field) {                                          \
271             av_free(formats);                                           \
272             return NULL;                                                \
273         }                                                               \
274     }
275
276 AVFilterFormats *ff_make_format_list(const int *fmts)
277 {
278     MAKE_FORMAT_LIST(AVFilterFormats, formats, format_count);
279     while (count--)
280         formats->formats[count] = fmts[count];
281
282     return formats;
283 }
284
285 AVFilterChannelLayouts *avfilter_make_format64_list(const int64_t *fmts)
286 {
287     MAKE_FORMAT_LIST(AVFilterChannelLayouts,
288                      channel_layouts, nb_channel_layouts);
289     if (count)
290         memcpy(formats->channel_layouts, fmts,
291                sizeof(*formats->channel_layouts) * count);
292
293     return formats;
294 }
295
296 #define ADD_FORMAT(f, fmt, type, list, nb)                  \
297 do {                                                        \
298     type *fmts;                                             \
299                                                             \
300     if (!(*f) && !(*f = av_mallocz(sizeof(**f))))           \
301         return AVERROR(ENOMEM);                             \
302                                                             \
303     fmts = av_realloc((*f)->list,                           \
304                       sizeof(*(*f)->list) * ((*f)->nb + 1));\
305     if (!fmts)                                              \
306         return AVERROR(ENOMEM);                             \
307                                                             \
308     (*f)->list = fmts;                                      \
309     (*f)->list[(*f)->nb++] = fmt;                           \
310 } while (0)
311
312 int ff_add_format(AVFilterFormats **avff, int64_t fmt)
313 {
314     ADD_FORMAT(avff, fmt, int, formats, format_count);
315     return 0;
316 }
317
318 int ff_add_channel_layout(AVFilterChannelLayouts **l, uint64_t channel_layout)
319 {
320     av_assert1(!(*l && (*l)->all_layouts));
321     ADD_FORMAT(l, channel_layout, uint64_t, channel_layouts, nb_channel_layouts);
322     return 0;
323 }
324
325 AVFilterFormats *ff_all_formats(enum AVMediaType type)
326 {
327     AVFilterFormats *ret = NULL;
328     int fmt;
329     int num_formats = type == AVMEDIA_TYPE_VIDEO ? AV_PIX_FMT_NB    :
330                       type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
331
332     for (fmt = 0; fmt < num_formats; fmt++) {
333         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
334         if ((type != AVMEDIA_TYPE_VIDEO) ||
335             (type == AVMEDIA_TYPE_VIDEO && !(desc->flags & PIX_FMT_HWACCEL)))
336             ff_add_format(&ret, fmt);
337     }
338
339     return ret;
340 }
341
342 const int64_t avfilter_all_channel_layouts[] = {
343 #include "all_channel_layouts.inc"
344     -1
345 };
346
347 // AVFilterFormats *avfilter_make_all_channel_layouts(void)
348 // {
349 //     return avfilter_make_format64_list(avfilter_all_channel_layouts);
350 // }
351
352 AVFilterFormats *ff_planar_sample_fmts(void)
353 {
354     AVFilterFormats *ret = NULL;
355     int fmt;
356
357     for (fmt = 0; fmt < AV_SAMPLE_FMT_NB; fmt++)
358         if (av_sample_fmt_is_planar(fmt))
359             ff_add_format(&ret, fmt);
360
361     return ret;
362 }
363
364 AVFilterFormats *ff_all_samplerates(void)
365 {
366     AVFilterFormats *ret = av_mallocz(sizeof(*ret));
367     return ret;
368 }
369
370 AVFilterChannelLayouts *ff_all_channel_layouts(void)
371 {
372     AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
373     if (!ret)
374         return NULL;
375     ret->all_layouts = 1;
376     return ret;
377 }
378
379 #define FORMATS_REF(f, ref)                                          \
380 do {                                                                 \
381     *ref = f;                                                        \
382     f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount); \
383     f->refs[f->refcount-1] = ref;                                    \
384 } while (0)
385
386 void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
387 {
388     FORMATS_REF(f, ref);
389 }
390
391 void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
392 {
393     FORMATS_REF(f, ref);
394 }
395
396 #define FIND_REF_INDEX(ref, idx)            \
397 do {                                        \
398     int i;                                  \
399     for (i = 0; i < (*ref)->refcount; i ++) \
400         if((*ref)->refs[i] == ref) {        \
401             idx = i;                        \
402             break;                          \
403         }                                   \
404 } while (0)
405
406 #define FORMATS_UNREF(ref, list)                                   \
407 do {                                                               \
408     int idx = -1;                                                  \
409                                                                    \
410     if (!*ref)                                                     \
411         return;                                                    \
412                                                                    \
413     FIND_REF_INDEX(ref, idx);                                      \
414                                                                    \
415     if (idx >= 0)                                                  \
416         memmove((*ref)->refs + idx, (*ref)->refs + idx + 1,        \
417             sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
418                                                                    \
419     if(!--(*ref)->refcount) {                                      \
420         av_free((*ref)->list);                                     \
421         av_free((*ref)->refs);                                     \
422         av_free(*ref);                                             \
423     }                                                              \
424     *ref = NULL;                                                   \
425 } while (0)
426
427 void ff_formats_unref(AVFilterFormats **ref)
428 {
429     FORMATS_UNREF(ref, formats);
430 }
431
432 void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
433 {
434     FORMATS_UNREF(ref, channel_layouts);
435 }
436
437 #define FORMATS_CHANGEREF(oldref, newref)       \
438 do {                                            \
439     int idx = -1;                               \
440                                                 \
441     FIND_REF_INDEX(oldref, idx);                \
442                                                 \
443     if (idx >= 0) {                             \
444         (*oldref)->refs[idx] = newref;          \
445         *newref = *oldref;                      \
446         *oldref = NULL;                         \
447     }                                           \
448 } while (0)
449
450 void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
451                                   AVFilterChannelLayouts **newref)
452 {
453     FORMATS_CHANGEREF(oldref, newref);
454 }
455
456 void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
457 {
458     FORMATS_CHANGEREF(oldref, newref);
459 }
460
461 #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
462 {                                                                   \
463     int count = 0, i;                                               \
464                                                                     \
465     for (i = 0; i < ctx->nb_inputs; i++) {                          \
466         if (ctx->inputs[i] && !ctx->inputs[i]->out_fmts) {          \
467             ref(fmts, &ctx->inputs[i]->out_fmts);                   \
468             count++;                                                \
469         }                                                           \
470     }                                                               \
471     for (i = 0; i < ctx->nb_outputs; i++) {                         \
472         if (ctx->outputs[i] && !ctx->outputs[i]->in_fmts) {         \
473             ref(fmts, &ctx->outputs[i]->in_fmts);                   \
474             count++;                                                \
475         }                                                           \
476     }                                                               \
477                                                                     \
478     if (!count) {                                                   \
479         av_freep(&fmts->list);                                      \
480         av_freep(&fmts->refs);                                      \
481         av_freep(&fmts);                                            \
482     }                                                               \
483 }
484
485 void ff_set_common_channel_layouts(AVFilterContext *ctx,
486                                    AVFilterChannelLayouts *layouts)
487 {
488     SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
489                        ff_channel_layouts_ref, channel_layouts);
490 }
491
492 void ff_set_common_samplerates(AVFilterContext *ctx,
493                                AVFilterFormats *samplerates)
494 {
495     SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
496                        ff_formats_ref, formats);
497 }
498
499 /**
500  * A helper for query_formats() which sets all links to the same list of
501  * formats. If there are no links hooked to this filter, the list of formats is
502  * freed.
503  */
504 void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
505 {
506     SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
507                        ff_formats_ref, formats);
508 }
509
510 int ff_default_query_formats(AVFilterContext *ctx)
511 {
512     enum AVMediaType type = ctx->inputs  && ctx->inputs [0] ? ctx->inputs [0]->type :
513                             ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
514                             AVMEDIA_TYPE_VIDEO;
515
516     ff_set_common_formats(ctx, ff_all_formats(type));
517     if (type == AVMEDIA_TYPE_AUDIO) {
518         ff_set_common_channel_layouts(ctx, ff_all_channel_layouts());
519         ff_set_common_samplerates(ctx, ff_all_samplerates());
520     }
521
522     return 0;
523 }
524
525 /* internal functions for parsing audio format arguments */
526
527 int ff_parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx)
528 {
529     char *tail;
530     int pix_fmt = av_get_pix_fmt(arg);
531     if (pix_fmt == AV_PIX_FMT_NONE) {
532         pix_fmt = strtol(arg, &tail, 0);
533         if (*tail || (unsigned)pix_fmt >= AV_PIX_FMT_NB) {
534             av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
535             return AVERROR(EINVAL);
536         }
537     }
538     *ret = pix_fmt;
539     return 0;
540 }
541
542 int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
543 {
544     char *tail;
545     int sfmt = av_get_sample_fmt(arg);
546     if (sfmt == AV_SAMPLE_FMT_NONE) {
547         sfmt = strtol(arg, &tail, 0);
548         if (*tail || (unsigned)sfmt >= AV_SAMPLE_FMT_NB) {
549             av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
550             return AVERROR(EINVAL);
551         }
552     }
553     *ret = sfmt;
554     return 0;
555 }
556
557 int ff_parse_time_base(AVRational *ret, const char *arg, void *log_ctx)
558 {
559     AVRational r;
560     if(av_parse_ratio(&r, arg, INT_MAX, 0, log_ctx) < 0 ||r.num<=0  ||r.den<=0) {
561         av_log(log_ctx, AV_LOG_ERROR, "Invalid time base '%s'\n", arg);
562         return AVERROR(EINVAL);
563     }
564     *ret = r;
565     return 0;
566 }
567
568 int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
569 {
570     char *tail;
571     double srate = av_strtod(arg, &tail);
572     if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
573         av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
574         return AVERROR(EINVAL);
575     }
576     *ret = srate;
577     return 0;
578 }
579
580 int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
581 {
582     char *tail;
583     int64_t chlayout = av_get_channel_layout(arg);
584     if (chlayout == 0) {
585         chlayout = strtol(arg, &tail, 10);
586         if (*tail || chlayout == 0) {
587             av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
588             return AVERROR(EINVAL);
589         }
590     }
591     *ret = chlayout;
592     return 0;
593 }
594
595 #ifdef TEST
596
597 #undef printf
598
599 int main(void)
600 {
601     const int64_t *cl;
602     char buf[512];
603
604     for (cl = avfilter_all_channel_layouts; *cl != -1; cl++) {
605         av_get_channel_layout_string(buf, sizeof(buf), -1, *cl);
606         printf("%s\n", buf);
607     }
608
609     return 0;
610 }
611
612 #endif
613