lavfi: add audio channel packing negotiation fields
[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/pixdesc.h"
23 #include "libavutil/audioconvert.h"
24 #include "avfilter.h"
25 #include "internal.h"
26
27 /**
28  * Add all refs from a to ret and destroy a.
29  */
30 static void merge_ref(AVFilterFormats *ret, AVFilterFormats *a)
31 {
32     int i;
33
34     for (i = 0; i < a->refcount; i++) {
35         ret->refs[ret->refcount] = a->refs[i];
36         *ret->refs[ret->refcount++] = ret;
37     }
38
39     av_free(a->refs);
40     av_free(a->formats);
41     av_free(a);
42 }
43
44 AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b)
45 {
46     AVFilterFormats *ret;
47     unsigned i, j, k = 0;
48
49     if (a == b) return a;
50
51     ret = av_mallocz(sizeof(AVFilterFormats));
52
53     /* merge list of formats */
54     ret->formats = av_malloc(sizeof(*ret->formats) * FFMIN(a->format_count,
55                                                            b->format_count));
56     for (i = 0; i < a->format_count; i++)
57         for (j = 0; j < b->format_count; j++)
58             if (a->formats[i] == b->formats[j])
59                 ret->formats[k++] = a->formats[i];
60
61     ret->format_count = k;
62     /* check that there was at least one common format */
63     if (!ret->format_count) {
64         av_free(ret->formats);
65         av_free(ret);
66         return NULL;
67     }
68
69     ret->refs = av_malloc(sizeof(AVFilterFormats**)*(a->refcount+b->refcount));
70
71     merge_ref(ret, a);
72     merge_ref(ret, b);
73
74     return ret;
75 }
76
77 int ff_fmt_is_in(int fmt, const int *fmts)
78 {
79     const int *p;
80
81     for (p = fmts; *p != -1; p++) {
82         if (fmt == *p)
83             return 1;
84     }
85     return 0;
86 }
87
88 #define MAKE_FORMAT_LIST()                                              \
89     AVFilterFormats *formats;                                           \
90     int count = 0;                                                      \
91     if (fmts)                                                           \
92         for (count = 0; fmts[count] != -1; count++)                     \
93             ;                                                           \
94     formats = av_mallocz(sizeof(AVFilterFormats));                      \
95     if (!formats) return NULL;                                          \
96     formats->format_count = count;                                      \
97     if (count) {                                                        \
98         formats->formats = av_malloc(sizeof(*formats->formats)*count);  \
99         if (!formats->formats) {                                        \
100             av_free(formats);                                           \
101             return NULL;                                                \
102         }                                                               \
103     }
104
105 AVFilterFormats *avfilter_make_format_list(const int *fmts)
106 {
107     MAKE_FORMAT_LIST();
108     while (count--)
109         formats->formats[count] = fmts[count];
110
111     return formats;
112 }
113
114 AVFilterFormats *avfilter_make_format64_list(const int64_t *fmts)
115 {
116     MAKE_FORMAT_LIST();
117     if (count)
118         memcpy(formats->formats, fmts, sizeof(*formats->formats) * count);
119
120     return formats;
121 }
122
123 int avfilter_add_format(AVFilterFormats **avff, int64_t fmt)
124 {
125     int64_t *fmts;
126
127     if (!(*avff) && !(*avff = av_mallocz(sizeof(AVFilterFormats))))
128         return AVERROR(ENOMEM);
129
130     fmts = av_realloc((*avff)->formats,
131                       sizeof(*(*avff)->formats) * ((*avff)->format_count+1));
132     if (!fmts)
133         return AVERROR(ENOMEM);
134
135     (*avff)->formats = fmts;
136     (*avff)->formats[(*avff)->format_count++] = fmt;
137     return 0;
138 }
139
140 AVFilterFormats *avfilter_all_formats(enum AVMediaType type)
141 {
142     AVFilterFormats *ret = NULL;
143     int fmt;
144     int num_formats = type == AVMEDIA_TYPE_VIDEO ? PIX_FMT_NB    :
145                       type == AVMEDIA_TYPE_AUDIO ? AV_SAMPLE_FMT_NB : 0;
146
147     for (fmt = 0; fmt < num_formats; fmt++)
148         if ((type != AVMEDIA_TYPE_VIDEO) ||
149             (type == AVMEDIA_TYPE_VIDEO && !(av_pix_fmt_descriptors[fmt].flags & PIX_FMT_HWACCEL)))
150             avfilter_add_format(&ret, fmt);
151
152     return ret;
153 }
154
155 AVFilterFormats *avfilter_all_channel_layouts(void)
156 {
157     static int64_t chlayouts[] = {
158         AV_CH_LAYOUT_MONO,
159         AV_CH_LAYOUT_STEREO,
160         AV_CH_LAYOUT_4POINT0,
161         AV_CH_LAYOUT_QUAD,
162         AV_CH_LAYOUT_5POINT0,
163         AV_CH_LAYOUT_5POINT0_BACK,
164         AV_CH_LAYOUT_5POINT1,
165         AV_CH_LAYOUT_5POINT1_BACK,
166         AV_CH_LAYOUT_5POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX,
167         AV_CH_LAYOUT_7POINT1,
168         AV_CH_LAYOUT_7POINT1_WIDE,
169         AV_CH_LAYOUT_7POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX,
170         -1,
171     };
172
173     return avfilter_make_format64_list(chlayouts);
174 }
175
176 AVFilterFormats *avfilter_all_packing_formats(void)
177 {
178     static int packing[] = {
179         AVFILTER_PACKED,
180         AVFILTER_PLANAR,
181         -1,
182     };
183
184     return avfilter_make_format_list(packing);
185 }
186
187 void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
188 {
189     *ref = f;
190     f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);
191     f->refs[f->refcount-1] = ref;
192 }
193
194 static int find_ref_index(AVFilterFormats **ref)
195 {
196     int i;
197     for (i = 0; i < (*ref)->refcount; i++)
198         if ((*ref)->refs[i] == ref)
199             return i;
200     return -1;
201 }
202
203 void avfilter_formats_unref(AVFilterFormats **ref)
204 {
205     int idx;
206
207     if (!*ref)
208         return;
209
210     idx = find_ref_index(ref);
211
212     if (idx >= 0)
213         memmove((*ref)->refs + idx, (*ref)->refs + idx+1,
214             sizeof(AVFilterFormats**) * ((*ref)->refcount-idx-1));
215
216     if (!--(*ref)->refcount) {
217         av_free((*ref)->formats);
218         av_free((*ref)->refs);
219         av_free(*ref);
220     }
221     *ref = NULL;
222 }
223
224 void avfilter_formats_changeref(AVFilterFormats **oldref,
225                                 AVFilterFormats **newref)
226 {
227     int idx = find_ref_index(oldref);
228
229     if (idx >= 0) {
230         (*oldref)->refs[idx] = newref;
231         *newref = *oldref;
232         *oldref = NULL;
233     }
234 }
235