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