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