lavfi: implement ff_all_channel_counts().
[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 AVFilterChannelLayouts *ff_all_channel_counts(void)
380 {
381     AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
382     if (!ret)
383         return NULL;
384     ret->all_layouts = ret->all_counts = 1;
385     return ret;
386 }
387
388 #define FORMATS_REF(f, ref)                                          \
389 do {                                                                 \
390     *ref = f;                                                        \
391     f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount); \
392     f->refs[f->refcount-1] = ref;                                    \
393 } while (0)
394
395 void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
396 {
397     FORMATS_REF(f, ref);
398 }
399
400 void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
401 {
402     FORMATS_REF(f, ref);
403 }
404
405 #define FIND_REF_INDEX(ref, idx)            \
406 do {                                        \
407     int i;                                  \
408     for (i = 0; i < (*ref)->refcount; i ++) \
409         if((*ref)->refs[i] == ref) {        \
410             idx = i;                        \
411             break;                          \
412         }                                   \
413 } while (0)
414
415 #define FORMATS_UNREF(ref, list)                                   \
416 do {                                                               \
417     int idx = -1;                                                  \
418                                                                    \
419     if (!*ref)                                                     \
420         return;                                                    \
421                                                                    \
422     FIND_REF_INDEX(ref, idx);                                      \
423                                                                    \
424     if (idx >= 0)                                                  \
425         memmove((*ref)->refs + idx, (*ref)->refs + idx + 1,        \
426             sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
427                                                                    \
428     if(!--(*ref)->refcount) {                                      \
429         av_free((*ref)->list);                                     \
430         av_free((*ref)->refs);                                     \
431         av_free(*ref);                                             \
432     }                                                              \
433     *ref = NULL;                                                   \
434 } while (0)
435
436 void ff_formats_unref(AVFilterFormats **ref)
437 {
438     FORMATS_UNREF(ref, formats);
439 }
440
441 void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
442 {
443     FORMATS_UNREF(ref, channel_layouts);
444 }
445
446 #define FORMATS_CHANGEREF(oldref, newref)       \
447 do {                                            \
448     int idx = -1;                               \
449                                                 \
450     FIND_REF_INDEX(oldref, idx);                \
451                                                 \
452     if (idx >= 0) {                             \
453         (*oldref)->refs[idx] = newref;          \
454         *newref = *oldref;                      \
455         *oldref = NULL;                         \
456     }                                           \
457 } while (0)
458
459 void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
460                                   AVFilterChannelLayouts **newref)
461 {
462     FORMATS_CHANGEREF(oldref, newref);
463 }
464
465 void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
466 {
467     FORMATS_CHANGEREF(oldref, newref);
468 }
469
470 #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
471 {                                                                   \
472     int count = 0, i;                                               \
473                                                                     \
474     for (i = 0; i < ctx->nb_inputs; i++) {                          \
475         if (ctx->inputs[i] && !ctx->inputs[i]->out_fmts) {          \
476             ref(fmts, &ctx->inputs[i]->out_fmts);                   \
477             count++;                                                \
478         }                                                           \
479     }                                                               \
480     for (i = 0; i < ctx->nb_outputs; i++) {                         \
481         if (ctx->outputs[i] && !ctx->outputs[i]->in_fmts) {         \
482             ref(fmts, &ctx->outputs[i]->in_fmts);                   \
483             count++;                                                \
484         }                                                           \
485     }                                                               \
486                                                                     \
487     if (!count) {                                                   \
488         av_freep(&fmts->list);                                      \
489         av_freep(&fmts->refs);                                      \
490         av_freep(&fmts);                                            \
491     }                                                               \
492 }
493
494 void ff_set_common_channel_layouts(AVFilterContext *ctx,
495                                    AVFilterChannelLayouts *layouts)
496 {
497     SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
498                        ff_channel_layouts_ref, channel_layouts);
499 }
500
501 void ff_set_common_samplerates(AVFilterContext *ctx,
502                                AVFilterFormats *samplerates)
503 {
504     SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
505                        ff_formats_ref, formats);
506 }
507
508 /**
509  * A helper for query_formats() which sets all links to the same list of
510  * formats. If there are no links hooked to this filter, the list of formats is
511  * freed.
512  */
513 void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
514 {
515     SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
516                        ff_formats_ref, formats);
517 }
518
519 int ff_default_query_formats(AVFilterContext *ctx)
520 {
521     enum AVMediaType type = ctx->inputs  && ctx->inputs [0] ? ctx->inputs [0]->type :
522                             ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
523                             AVMEDIA_TYPE_VIDEO;
524
525     ff_set_common_formats(ctx, ff_all_formats(type));
526     if (type == AVMEDIA_TYPE_AUDIO) {
527         ff_set_common_channel_layouts(ctx, ff_all_channel_layouts());
528         ff_set_common_samplerates(ctx, ff_all_samplerates());
529     }
530
531     return 0;
532 }
533
534 /* internal functions for parsing audio format arguments */
535
536 int ff_parse_pixel_format(enum AVPixelFormat *ret, const char *arg, void *log_ctx)
537 {
538     char *tail;
539     int pix_fmt = av_get_pix_fmt(arg);
540     if (pix_fmt == AV_PIX_FMT_NONE) {
541         pix_fmt = strtol(arg, &tail, 0);
542         if (*tail || (unsigned)pix_fmt >= AV_PIX_FMT_NB) {
543             av_log(log_ctx, AV_LOG_ERROR, "Invalid pixel format '%s'\n", arg);
544             return AVERROR(EINVAL);
545         }
546     }
547     *ret = pix_fmt;
548     return 0;
549 }
550
551 int ff_parse_sample_format(int *ret, const char *arg, void *log_ctx)
552 {
553     char *tail;
554     int sfmt = av_get_sample_fmt(arg);
555     if (sfmt == AV_SAMPLE_FMT_NONE) {
556         sfmt = strtol(arg, &tail, 0);
557         if (*tail || (unsigned)sfmt >= AV_SAMPLE_FMT_NB) {
558             av_log(log_ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n", arg);
559             return AVERROR(EINVAL);
560         }
561     }
562     *ret = sfmt;
563     return 0;
564 }
565
566 int ff_parse_time_base(AVRational *ret, const char *arg, void *log_ctx)
567 {
568     AVRational r;
569     if(av_parse_ratio(&r, arg, INT_MAX, 0, log_ctx) < 0 ||r.num<=0  ||r.den<=0) {
570         av_log(log_ctx, AV_LOG_ERROR, "Invalid time base '%s'\n", arg);
571         return AVERROR(EINVAL);
572     }
573     *ret = r;
574     return 0;
575 }
576
577 int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
578 {
579     char *tail;
580     double srate = av_strtod(arg, &tail);
581     if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
582         av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
583         return AVERROR(EINVAL);
584     }
585     *ret = srate;
586     return 0;
587 }
588
589 int ff_parse_channel_layout(int64_t *ret, const char *arg, void *log_ctx)
590 {
591     char *tail;
592     int64_t chlayout = av_get_channel_layout(arg);
593     if (chlayout == 0) {
594         chlayout = strtol(arg, &tail, 10);
595         if (*tail || chlayout == 0) {
596             av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
597             return AVERROR(EINVAL);
598         }
599     }
600     *ret = chlayout;
601     return 0;
602 }
603
604 #ifdef TEST
605
606 #undef printf
607
608 int main(void)
609 {
610     const int64_t *cl;
611     char buf[512];
612
613     for (cl = avfilter_all_channel_layouts; *cl != -1; cl++) {
614         av_get_channel_layout_string(buf, sizeof(buf), -1, *cl);
615         printf("%s\n", buf);
616     }
617
618     return 0;
619 }
620
621 #endif
622