]> git.sesse.net Git - ffmpeg/blob - libavfilter/formats.c
lavfi: make formats API private on next bump.
[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_all_samplerates(void)
224 {
225     AVFilterFormats *ret = av_mallocz(sizeof(*ret));
226     return ret;
227 }
228
229 AVFilterChannelLayouts *ff_all_channel_layouts(void)
230 {
231     AVFilterChannelLayouts *ret = av_mallocz(sizeof(*ret));
232     return ret;
233 }
234
235 #define FORMATS_REF(f, ref)                                          \
236 do {                                                                 \
237     *ref = f;                                                        \
238     f->refs = av_realloc(f->refs, sizeof(*f->refs) * ++f->refcount); \
239     f->refs[f->refcount-1] = ref;                                    \
240 } while (0)
241
242 void ff_channel_layouts_ref(AVFilterChannelLayouts *f, AVFilterChannelLayouts **ref)
243 {
244     FORMATS_REF(f, ref);
245 }
246
247 void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
248 {
249     FORMATS_REF(f, ref);
250 }
251
252 #define FIND_REF_INDEX(ref, idx)            \
253 do {                                        \
254     int i;                                  \
255     for (i = 0; i < (*ref)->refcount; i ++) \
256         if((*ref)->refs[i] == ref) {        \
257             idx = i;                        \
258             break;                          \
259         }                                   \
260 } while (0)
261
262 #define FORMATS_UNREF(ref, list)                                   \
263 do {                                                               \
264     int idx = -1;                                                  \
265                                                                    \
266     if (!*ref)                                                     \
267         return;                                                    \
268                                                                    \
269     FIND_REF_INDEX(ref, idx);                                      \
270                                                                    \
271     if (idx >= 0)                                                  \
272         memmove((*ref)->refs + idx, (*ref)->refs + idx + 1,        \
273             sizeof(*(*ref)->refs) * ((*ref)->refcount - idx - 1)); \
274                                                                    \
275     if(!--(*ref)->refcount) {                                      \
276         av_free((*ref)->list);                                     \
277         av_free((*ref)->refs);                                     \
278         av_free(*ref);                                             \
279     }                                                              \
280     *ref = NULL;                                                   \
281 } while (0)
282
283 void ff_formats_unref(AVFilterFormats **ref)
284 {
285     FORMATS_UNREF(ref, formats);
286 }
287
288 void ff_channel_layouts_unref(AVFilterChannelLayouts **ref)
289 {
290     FORMATS_UNREF(ref, channel_layouts);
291 }
292
293 #define FORMATS_CHANGEREF(oldref, newref)       \
294 do {                                            \
295     int idx = -1;                               \
296                                                 \
297     FIND_REF_INDEX(oldref, idx);                \
298                                                 \
299     if (idx >= 0) {                             \
300         (*oldref)->refs[idx] = newref;          \
301         *newref = *oldref;                      \
302         *oldref = NULL;                         \
303     }                                           \
304 } while (0)
305
306 void ff_channel_layouts_changeref(AVFilterChannelLayouts **oldref,
307                                   AVFilterChannelLayouts **newref)
308 {
309     FORMATS_CHANGEREF(oldref, newref);
310 }
311
312 void ff_formats_changeref(AVFilterFormats **oldref, AVFilterFormats **newref)
313 {
314     FORMATS_CHANGEREF(oldref, newref);
315 }
316
317 #define SET_COMMON_FORMATS(ctx, fmts, in_fmts, out_fmts, ref, list) \
318 {                                                                   \
319     int count = 0, i;                                               \
320                                                                     \
321     for (i = 0; i < ctx->input_count; i++) {                        \
322         if (ctx->inputs[i]) {                                       \
323             ref(fmts, &ctx->inputs[i]->out_fmts);                   \
324             count++;                                                \
325         }                                                           \
326     }                                                               \
327     for (i = 0; i < ctx->output_count; i++) {                       \
328         if (ctx->outputs[i]) {                                      \
329             ref(fmts, &ctx->outputs[i]->in_fmts);                   \
330             count++;                                                \
331         }                                                           \
332     }                                                               \
333                                                                     \
334     if (!count) {                                                   \
335         av_freep(&fmts->list);                                      \
336         av_freep(&fmts->refs);                                      \
337         av_freep(&fmts);                                            \
338     }                                                               \
339 }
340
341 void ff_set_common_channel_layouts(AVFilterContext *ctx,
342                                    AVFilterChannelLayouts *layouts)
343 {
344     SET_COMMON_FORMATS(ctx, layouts, in_channel_layouts, out_channel_layouts,
345                        ff_channel_layouts_ref, channel_layouts);
346 }
347
348 void ff_set_common_samplerates(AVFilterContext *ctx,
349                                AVFilterFormats *samplerates)
350 {
351     SET_COMMON_FORMATS(ctx, samplerates, in_samplerates, out_samplerates,
352                        ff_formats_ref, formats);
353 }
354
355 /**
356  * A helper for query_formats() which sets all links to the same list of
357  * formats. If there are no links hooked to this filter, the list of formats is
358  * freed.
359  */
360 void ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
361 {
362     SET_COMMON_FORMATS(ctx, formats, in_formats, out_formats,
363                        ff_formats_ref, formats);
364 }
365
366 int ff_default_query_formats(AVFilterContext *ctx)
367 {
368     enum AVMediaType type = ctx->inputs  && ctx->inputs [0] ? ctx->inputs [0]->type :
369                             ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
370                             AVMEDIA_TYPE_VIDEO;
371
372     ff_set_common_formats(ctx, ff_all_formats(type));
373     if (type == AVMEDIA_TYPE_AUDIO) {
374         ff_set_common_channel_layouts(ctx, ff_all_channel_layouts());
375         ff_set_common_samplerates(ctx, ff_all_samplerates());
376     }
377
378     return 0;
379 }
380
381 #if FF_API_FILTERS_PUBLIC
382 int avfilter_default_query_formats(AVFilterContext *ctx)
383 {
384     return ff_default_query_formats(ctx);
385 }
386 void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
387 {
388     ff_set_common_formats(ctx, formats);
389 }
390 AVFilterFormats *avfilter_make_format_list(const int *fmts)
391 {
392     return ff_make_format_list(fmts);
393 }
394 int avfilter_add_format(AVFilterFormats **avff, int fmt)
395 {
396     return ff_add_format(avff, fmt);
397 }
398 AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
399 {
400     return ff_all_formats(type);
401 }
402 AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
403 {
404     return ff_merge_formats(a, b);
405 }
406 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
407 {
408     ff_formats_ref(f, ref);
409 }
410 void avfilter_formats_unref(AVFilterFormats **ref)
411 {
412     ff_formats_unref(ref);
413 }
414 void avfilter_formats_changeref(AVFilterFormats **oldref,
415                                 AVFilterFormats **newref)
416 {
417     ff_formats_changeref(oldref, newref);
418 }
419 #endif