/src/ffmpeg/libavutil/channel_layout.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at> |
3 | | * |
4 | | * This file is part of FFmpeg. |
5 | | * |
6 | | * FFmpeg is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * FFmpeg is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with FFmpeg; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | | */ |
20 | | |
21 | | /** |
22 | | * @file |
23 | | * audio channel layout utility functions |
24 | | */ |
25 | | |
26 | | #include <stdint.h> |
27 | | #include <stdlib.h> |
28 | | #include <string.h> |
29 | | |
30 | | #include "avassert.h" |
31 | | #include "channel_layout.h" |
32 | | #include "bprint.h" |
33 | | #include "common.h" |
34 | | #include "error.h" |
35 | | #include "attributes.h" |
36 | | #include "macros.h" |
37 | | #include "mem.h" |
38 | | #include "opt.h" |
39 | | |
40 | 0 | #define CHAN_IS_AMBI(x) ((x) >= AV_CHAN_AMBISONIC_BASE &&\ |
41 | 0 | (x) <= AV_CHAN_AMBISONIC_END) |
42 | | |
43 | | struct channel_name { |
44 | | const char *name; |
45 | | const char *description; |
46 | | }; |
47 | | |
48 | | static const struct channel_name channel_names[] = { |
49 | | [AV_CHAN_FRONT_LEFT ] = { "FL", "front left" }, |
50 | | [AV_CHAN_FRONT_RIGHT ] = { "FR", "front right" }, |
51 | | [AV_CHAN_FRONT_CENTER ] = { "FC", "front center" }, |
52 | | [AV_CHAN_LOW_FREQUENCY ] = { "LFE", "low frequency" }, |
53 | | [AV_CHAN_BACK_LEFT ] = { "BL", "back left" }, |
54 | | [AV_CHAN_BACK_RIGHT ] = { "BR", "back right" }, |
55 | | [AV_CHAN_FRONT_LEFT_OF_CENTER ] = { "FLC", "front left-of-center" }, |
56 | | [AV_CHAN_FRONT_RIGHT_OF_CENTER] = { "FRC", "front right-of-center" }, |
57 | | [AV_CHAN_BACK_CENTER ] = { "BC", "back center" }, |
58 | | [AV_CHAN_SIDE_LEFT ] = { "SL", "side left" }, |
59 | | [AV_CHAN_SIDE_RIGHT ] = { "SR", "side right" }, |
60 | | [AV_CHAN_TOP_CENTER ] = { "TC", "top center" }, |
61 | | [AV_CHAN_TOP_FRONT_LEFT ] = { "TFL", "top front left" }, |
62 | | [AV_CHAN_TOP_FRONT_CENTER ] = { "TFC", "top front center" }, |
63 | | [AV_CHAN_TOP_FRONT_RIGHT ] = { "TFR", "top front right" }, |
64 | | [AV_CHAN_TOP_BACK_LEFT ] = { "TBL", "top back left" }, |
65 | | [AV_CHAN_TOP_BACK_CENTER ] = { "TBC", "top back center" }, |
66 | | [AV_CHAN_TOP_BACK_RIGHT ] = { "TBR", "top back right" }, |
67 | | [AV_CHAN_STEREO_LEFT ] = { "DL", "downmix left" }, |
68 | | [AV_CHAN_STEREO_RIGHT ] = { "DR", "downmix right" }, |
69 | | [AV_CHAN_WIDE_LEFT ] = { "WL", "wide left" }, |
70 | | [AV_CHAN_WIDE_RIGHT ] = { "WR", "wide right" }, |
71 | | [AV_CHAN_SURROUND_DIRECT_LEFT ] = { "SDL", "surround direct left" }, |
72 | | [AV_CHAN_SURROUND_DIRECT_RIGHT] = { "SDR", "surround direct right" }, |
73 | | [AV_CHAN_LOW_FREQUENCY_2 ] = { "LFE2", "low frequency 2" }, |
74 | | [AV_CHAN_TOP_SIDE_LEFT ] = { "TSL", "top side left" }, |
75 | | [AV_CHAN_TOP_SIDE_RIGHT ] = { "TSR", "top side right" }, |
76 | | [AV_CHAN_BOTTOM_FRONT_CENTER ] = { "BFC", "bottom front center" }, |
77 | | [AV_CHAN_BOTTOM_FRONT_LEFT ] = { "BFL", "bottom front left" }, |
78 | | [AV_CHAN_BOTTOM_FRONT_RIGHT ] = { "BFR", "bottom front right" }, |
79 | | [AV_CHAN_SIDE_SURROUND_LEFT ] = { "SSL", "side surround left" }, |
80 | | [AV_CHAN_SIDE_SURROUND_RIGHT ] = { "SSR", "side surround right" }, |
81 | | [AV_CHAN_TOP_SURROUND_LEFT ] = { "TTL", "top surround left" }, |
82 | | [AV_CHAN_TOP_SURROUND_RIGHT ] = { "TTR", "top surround right" }, |
83 | | [AV_CHAN_BINAURAL_LEFT ] = { "BIL", "binaural left" }, |
84 | | [AV_CHAN_BINAURAL_RIGHT ] = { "BIR", "binaural right" }, |
85 | | }; |
86 | | |
87 | | void av_channel_name_bprint(AVBPrint *bp, enum AVChannel channel_id) |
88 | 0 | { |
89 | 0 | if (channel_id >= AV_CHAN_AMBISONIC_BASE && |
90 | 0 | channel_id <= AV_CHAN_AMBISONIC_END) |
91 | 0 | av_bprintf(bp, "AMBI%d", channel_id - AV_CHAN_AMBISONIC_BASE); |
92 | 0 | else if ((unsigned)channel_id < FF_ARRAY_ELEMS(channel_names) && |
93 | 0 | channel_names[channel_id].name) |
94 | 0 | av_bprintf(bp, "%s", channel_names[channel_id].name); |
95 | 0 | else if (channel_id == AV_CHAN_NONE) |
96 | 0 | av_bprintf(bp, "NONE"); |
97 | 0 | else if (channel_id == AV_CHAN_UNKNOWN) |
98 | 0 | av_bprintf(bp, "UNK"); |
99 | 0 | else if (channel_id == AV_CHAN_UNUSED) |
100 | 0 | av_bprintf(bp, "UNSD"); |
101 | 0 | else |
102 | 0 | av_bprintf(bp, "USR%d", channel_id); |
103 | 0 | } |
104 | | |
105 | | int av_channel_name(char *buf, size_t buf_size, enum AVChannel channel_id) |
106 | 0 | { |
107 | 0 | AVBPrint bp; |
108 | |
|
109 | 0 | if (!buf && buf_size) |
110 | 0 | return AVERROR(EINVAL); |
111 | | |
112 | 0 | av_bprint_init_for_buffer(&bp, buf, buf_size); |
113 | 0 | av_channel_name_bprint(&bp, channel_id); |
114 | |
|
115 | 0 | if (bp.len >= INT_MAX) |
116 | 0 | return AVERROR(ERANGE); |
117 | 0 | return bp.len + 1; |
118 | 0 | } |
119 | | |
120 | | void av_channel_description_bprint(AVBPrint *bp, enum AVChannel channel_id) |
121 | 0 | { |
122 | 0 | if (channel_id >= AV_CHAN_AMBISONIC_BASE && |
123 | 0 | channel_id <= AV_CHAN_AMBISONIC_END) |
124 | 0 | av_bprintf(bp, "ambisonic ACN %d", channel_id - AV_CHAN_AMBISONIC_BASE); |
125 | 0 | else if ((unsigned)channel_id < FF_ARRAY_ELEMS(channel_names) && |
126 | 0 | channel_names[channel_id].description) |
127 | 0 | av_bprintf(bp, "%s", channel_names[channel_id].description); |
128 | 0 | else if (channel_id == AV_CHAN_NONE) |
129 | 0 | av_bprintf(bp, "none"); |
130 | 0 | else if (channel_id == AV_CHAN_UNKNOWN) |
131 | 0 | av_bprintf(bp, "unknown"); |
132 | 0 | else if (channel_id == AV_CHAN_UNUSED) |
133 | 0 | av_bprintf(bp, "unused"); |
134 | 0 | else |
135 | 0 | av_bprintf(bp, "user %d", channel_id); |
136 | 0 | } |
137 | | |
138 | | int av_channel_description(char *buf, size_t buf_size, enum AVChannel channel_id) |
139 | 0 | { |
140 | 0 | AVBPrint bp; |
141 | |
|
142 | 0 | if (!buf && buf_size) |
143 | 0 | return AVERROR(EINVAL); |
144 | | |
145 | 0 | av_bprint_init_for_buffer(&bp, buf, buf_size); |
146 | 0 | av_channel_description_bprint(&bp, channel_id); |
147 | |
|
148 | 0 | if (bp.len >= INT_MAX) |
149 | 0 | return AVERROR(ERANGE); |
150 | 0 | return bp.len + 1; |
151 | 0 | } |
152 | | |
153 | | enum AVChannel av_channel_from_string(const char *str) |
154 | 0 | { |
155 | 0 | int i; |
156 | 0 | char *endptr = (char *)str; |
157 | 0 | enum AVChannel id = AV_CHAN_NONE; |
158 | |
|
159 | 0 | if (!strncmp(str, "AMBI", 4)) { |
160 | 0 | i = strtol(str + 4, NULL, 0); |
161 | 0 | if (i < 0 || i > AV_CHAN_AMBISONIC_END - AV_CHAN_AMBISONIC_BASE) |
162 | 0 | return AV_CHAN_NONE; |
163 | 0 | return AV_CHAN_AMBISONIC_BASE + i; |
164 | 0 | } |
165 | | |
166 | 0 | for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++) { |
167 | 0 | if (channel_names[i].name && !strcmp(str, channel_names[i].name)) |
168 | 0 | return i; |
169 | 0 | } |
170 | 0 | if (!strcmp(str, "UNK")) |
171 | 0 | return AV_CHAN_UNKNOWN; |
172 | 0 | if (!strcmp(str, "UNSD")) |
173 | 0 | return AV_CHAN_UNUSED; |
174 | | |
175 | 0 | if (!strncmp(str, "USR", 3)) { |
176 | 0 | const char *p = str + 3; |
177 | 0 | id = strtol(p, &endptr, 0); |
178 | 0 | } |
179 | 0 | if (id >= 0 && !*endptr) |
180 | 0 | return id; |
181 | | |
182 | 0 | return AV_CHAN_NONE; |
183 | 0 | } |
184 | | |
185 | | struct channel_layout_name { |
186 | | const char *name; |
187 | | AVChannelLayout layout; |
188 | | }; |
189 | | |
190 | | static const struct channel_layout_name channel_layout_map[] = { |
191 | | { "mono", AV_CHANNEL_LAYOUT_MONO }, |
192 | | { "stereo", AV_CHANNEL_LAYOUT_STEREO }, |
193 | | { "2.1", AV_CHANNEL_LAYOUT_2POINT1 }, |
194 | | { "3.0", AV_CHANNEL_LAYOUT_SURROUND }, |
195 | | { "3.0(back)", AV_CHANNEL_LAYOUT_2_1 }, |
196 | | { "4.0", AV_CHANNEL_LAYOUT_4POINT0 }, |
197 | | { "quad", AV_CHANNEL_LAYOUT_QUAD }, |
198 | | { "quad(side)", AV_CHANNEL_LAYOUT_2_2 }, |
199 | | { "3.1", AV_CHANNEL_LAYOUT_3POINT1 }, |
200 | | { "5.0", AV_CHANNEL_LAYOUT_5POINT0_BACK }, |
201 | | { "5.0(side)", AV_CHANNEL_LAYOUT_5POINT0 }, |
202 | | { "4.1", AV_CHANNEL_LAYOUT_4POINT1 }, |
203 | | { "5.1", AV_CHANNEL_LAYOUT_5POINT1_BACK }, |
204 | | { "5.1(side)", AV_CHANNEL_LAYOUT_5POINT1 }, |
205 | | { "6.0", AV_CHANNEL_LAYOUT_6POINT0 }, |
206 | | { "6.0(front)", AV_CHANNEL_LAYOUT_6POINT0_FRONT }, |
207 | | { "3.1.2", AV_CHANNEL_LAYOUT_3POINT1POINT2 }, |
208 | | { "hexagonal", AV_CHANNEL_LAYOUT_HEXAGONAL }, |
209 | | { "6.1", AV_CHANNEL_LAYOUT_6POINT1 }, |
210 | | { "6.1(back)", AV_CHANNEL_LAYOUT_6POINT1_BACK }, |
211 | | { "6.1(front)", AV_CHANNEL_LAYOUT_6POINT1_FRONT }, |
212 | | { "7.0", AV_CHANNEL_LAYOUT_7POINT0 }, |
213 | | { "7.0(front)", AV_CHANNEL_LAYOUT_7POINT0_FRONT }, |
214 | | { "7.1", AV_CHANNEL_LAYOUT_7POINT1 }, |
215 | | { "7.1(wide)", AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK }, |
216 | | { "7.1(wide-side)", AV_CHANNEL_LAYOUT_7POINT1_WIDE }, |
217 | | { "5.1.2", AV_CHANNEL_LAYOUT_5POINT1POINT2 }, |
218 | | { "5.1.2(back)", AV_CHANNEL_LAYOUT_5POINT1POINT2_BACK }, |
219 | | { "octagonal", AV_CHANNEL_LAYOUT_OCTAGONAL }, |
220 | | { "cube", AV_CHANNEL_LAYOUT_CUBE }, |
221 | | { "5.1.4", AV_CHANNEL_LAYOUT_5POINT1POINT4 }, |
222 | | { "5.1.6", AV_CHANNEL_LAYOUT_5POINT1POINT6 }, |
223 | | { "7.1.2", AV_CHANNEL_LAYOUT_7POINT1POINT2 }, |
224 | | { "7.1.4", AV_CHANNEL_LAYOUT_7POINT1POINT4 }, |
225 | | { "7.1.6", AV_CHANNEL_LAYOUT_7POINT1POINT6 }, |
226 | | { "7.2.3", AV_CHANNEL_LAYOUT_7POINT2POINT3 }, |
227 | | { "9.1.4", AV_CHANNEL_LAYOUT_9POINT1POINT4 }, |
228 | | { "9.1.6", AV_CHANNEL_LAYOUT_9POINT1POINT6 }, |
229 | | { "hexadecagonal", AV_CHANNEL_LAYOUT_HEXADECAGONAL }, |
230 | | { "binaural", AV_CHANNEL_LAYOUT_BINAURAL }, |
231 | | { "downmix", AV_CHANNEL_LAYOUT_STEREO_DOWNMIX, }, |
232 | | { "22.2", AV_CHANNEL_LAYOUT_22POINT2, }, |
233 | | }; |
234 | | |
235 | | int av_channel_layout_custom_init(AVChannelLayout *channel_layout, int nb_channels) |
236 | 0 | { |
237 | 0 | AVChannelCustom *map; |
238 | |
|
239 | 0 | if (nb_channels <= 0) |
240 | 0 | return AVERROR(EINVAL); |
241 | | |
242 | 0 | map = av_calloc(nb_channels, sizeof(*channel_layout->u.map)); |
243 | 0 | if (!map) |
244 | 0 | return AVERROR(ENOMEM); |
245 | 0 | for (int i = 0; i < nb_channels; i++) |
246 | 0 | map[i].id = AV_CHAN_UNKNOWN; |
247 | |
|
248 | 0 | channel_layout->order = AV_CHANNEL_ORDER_CUSTOM; |
249 | 0 | channel_layout->nb_channels = nb_channels; |
250 | 0 | channel_layout->u.map = map; |
251 | |
|
252 | 0 | return 0; |
253 | 0 | } |
254 | | |
255 | | int av_channel_layout_from_mask(AVChannelLayout *channel_layout, |
256 | | uint64_t mask) |
257 | 0 | { |
258 | 0 | if (!mask) |
259 | 0 | return AVERROR(EINVAL); |
260 | | |
261 | 0 | channel_layout->order = AV_CHANNEL_ORDER_NATIVE; |
262 | 0 | channel_layout->nb_channels = av_popcount64(mask); |
263 | 0 | channel_layout->u.mask = mask; |
264 | |
|
265 | 0 | return 0; |
266 | 0 | } |
267 | | |
268 | | static int parse_channel_list(AVChannelLayout *ch_layout, const char *str) |
269 | 0 | { |
270 | 0 | int ret; |
271 | 0 | int nb_channels = 0; |
272 | 0 | AVChannelCustom *map = NULL; |
273 | 0 | AVChannelCustom custom = {0}; |
274 | |
|
275 | 0 | while (*str) { |
276 | 0 | char *channel, *chname; |
277 | 0 | ret = av_opt_get_key_value(&str, "@", "+", AV_OPT_FLAG_IMPLICIT_KEY, &channel, &chname); |
278 | 0 | if (ret < 0) { |
279 | 0 | av_freep(&map); |
280 | 0 | return ret; |
281 | 0 | } |
282 | 0 | if (*str) |
283 | 0 | str++; // skip separator |
284 | 0 | if (!channel) { |
285 | 0 | channel = chname; |
286 | 0 | chname = NULL; |
287 | 0 | } |
288 | 0 | av_strlcpy(custom.name, chname ? chname : "", sizeof(custom.name)); |
289 | 0 | custom.id = av_channel_from_string(channel); |
290 | 0 | av_free(channel); |
291 | 0 | av_free(chname); |
292 | 0 | if (custom.id == AV_CHAN_NONE) { |
293 | 0 | av_freep(&map); |
294 | 0 | return AVERROR(EINVAL); |
295 | 0 | } |
296 | | |
297 | 0 | av_dynarray2_add((void **)&map, &nb_channels, sizeof(custom), (void *)&custom); |
298 | 0 | if (!map) |
299 | 0 | return AVERROR(ENOMEM); |
300 | 0 | } |
301 | | |
302 | 0 | if (!nb_channels) |
303 | 0 | return AVERROR(EINVAL); |
304 | | |
305 | 0 | ch_layout->order = AV_CHANNEL_ORDER_CUSTOM; |
306 | 0 | ch_layout->u.map = map; |
307 | 0 | ch_layout->nb_channels = nb_channels; |
308 | |
|
309 | 0 | ret = av_channel_layout_retype(ch_layout, 0, AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL); |
310 | 0 | av_assert0(ret == 0); |
311 | | |
312 | 0 | return 0; |
313 | 0 | } |
314 | | |
315 | | int av_channel_layout_from_string(AVChannelLayout *channel_layout, |
316 | | const char *str) |
317 | 0 | { |
318 | 0 | int i, matches, ret; |
319 | 0 | int channels = 0, nb_channels = 0; |
320 | 0 | char *chlist, *end; |
321 | 0 | uint64_t mask = 0; |
322 | | |
323 | | /* channel layout names */ |
324 | 0 | for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++) { |
325 | 0 | if (channel_layout_map[i].name && !strcmp(str, channel_layout_map[i].name)) { |
326 | 0 | *channel_layout = channel_layout_map[i].layout; |
327 | 0 | return 0; |
328 | 0 | } |
329 | 0 | } |
330 | | |
331 | | /* This function is a channel layout initializer, so we have to |
332 | | * zero-initialize before we start setting fields individually. */ |
333 | 0 | memset(channel_layout, 0, sizeof(*channel_layout)); |
334 | | |
335 | | /* ambisonic */ |
336 | 0 | if (!strncmp(str, "ambisonic ", 10)) { |
337 | 0 | const char *p = str + 10; |
338 | 0 | char *endptr; |
339 | 0 | AVChannelLayout extra = {0}; |
340 | 0 | int order; |
341 | |
|
342 | 0 | order = strtol(p, &endptr, 0); |
343 | 0 | if (order < 0 || order + 1 > INT_MAX / (order + 1) || |
344 | 0 | (*endptr && *endptr != '+')) |
345 | 0 | return AVERROR(EINVAL); |
346 | | |
347 | 0 | channel_layout->order = AV_CHANNEL_ORDER_AMBISONIC; |
348 | 0 | channel_layout->nb_channels = (order + 1) * (order + 1); |
349 | |
|
350 | 0 | if (*endptr) { |
351 | 0 | ret = av_channel_layout_from_string(&extra, endptr + 1); |
352 | 0 | if (ret < 0) |
353 | 0 | return ret; |
354 | 0 | if (extra.nb_channels >= INT_MAX - channel_layout->nb_channels) { |
355 | 0 | av_channel_layout_uninit(&extra); |
356 | 0 | return AVERROR(EINVAL); |
357 | 0 | } |
358 | | |
359 | 0 | if (extra.order == AV_CHANNEL_ORDER_NATIVE) { |
360 | 0 | channel_layout->u.mask = extra.u.mask; |
361 | 0 | } else { |
362 | 0 | channel_layout->order = AV_CHANNEL_ORDER_CUSTOM; |
363 | 0 | channel_layout->u.map = |
364 | 0 | av_calloc(channel_layout->nb_channels + extra.nb_channels, |
365 | 0 | sizeof(*channel_layout->u.map)); |
366 | 0 | if (!channel_layout->u.map) { |
367 | 0 | av_channel_layout_uninit(&extra); |
368 | 0 | return AVERROR(ENOMEM); |
369 | 0 | } |
370 | | |
371 | 0 | for (i = 0; i < channel_layout->nb_channels; i++) |
372 | 0 | channel_layout->u.map[i].id = AV_CHAN_AMBISONIC_BASE + i; |
373 | 0 | for (i = 0; i < extra.nb_channels; i++) { |
374 | 0 | enum AVChannel ch = av_channel_layout_channel_from_index(&extra, i); |
375 | 0 | if (CHAN_IS_AMBI(ch)) { |
376 | 0 | av_channel_layout_uninit(channel_layout); |
377 | 0 | av_channel_layout_uninit(&extra); |
378 | 0 | return AVERROR(EINVAL); |
379 | 0 | } |
380 | 0 | channel_layout->u.map[channel_layout->nb_channels + i].id = ch; |
381 | 0 | if (extra.order == AV_CHANNEL_ORDER_CUSTOM && |
382 | 0 | extra.u.map[i].name[0]) |
383 | 0 | av_strlcpy(channel_layout->u.map[channel_layout->nb_channels + i].name, |
384 | 0 | extra.u.map[i].name, |
385 | 0 | sizeof(channel_layout->u.map[channel_layout->nb_channels + i].name)); |
386 | 0 | } |
387 | 0 | } |
388 | 0 | channel_layout->nb_channels += extra.nb_channels; |
389 | 0 | av_channel_layout_uninit(&extra); |
390 | 0 | } |
391 | | |
392 | 0 | return 0; |
393 | 0 | } |
394 | | |
395 | 0 | chlist = av_strdup(str); |
396 | 0 | if (!chlist) |
397 | 0 | return AVERROR(ENOMEM); |
398 | | |
399 | | /* channel names */ |
400 | 0 | matches = av_sscanf(str, "%d channels (%[^)]", &nb_channels, chlist); |
401 | 0 | ret = parse_channel_list(channel_layout, chlist); |
402 | 0 | av_freep(&chlist); |
403 | 0 | if (ret < 0 && ret != AVERROR(EINVAL)) |
404 | 0 | return ret; |
405 | | |
406 | 0 | if (ret >= 0) { |
407 | 0 | const char *end_par = strchr(str, ')'); |
408 | 0 | if (matches == 2 && (nb_channels != channel_layout->nb_channels || !end_par || *++end_par)) { |
409 | 0 | av_channel_layout_uninit(channel_layout); |
410 | 0 | return AVERROR(EINVAL); |
411 | 0 | } |
412 | 0 | return 0; |
413 | 0 | } |
414 | | |
415 | 0 | errno = 0; |
416 | 0 | mask = strtoull(str, &end, 0); |
417 | | |
418 | | /* channel layout mask */ |
419 | 0 | if (!errno && !*end && !strchr(str, '-') && mask) { |
420 | 0 | av_channel_layout_from_mask(channel_layout, mask); |
421 | 0 | return 0; |
422 | 0 | } |
423 | | |
424 | 0 | errno = 0; |
425 | 0 | channels = strtol(str, &end, 10); |
426 | | |
427 | | /* number of channels */ |
428 | 0 | if (!errno && !strcmp(end, "c") && channels > 0) { |
429 | 0 | av_channel_layout_default(channel_layout, channels); |
430 | 0 | if (channel_layout->order == AV_CHANNEL_ORDER_NATIVE) |
431 | 0 | return 0; |
432 | 0 | } |
433 | | |
434 | | /* number of unordered channels */ |
435 | 0 | if (!errno && (!strcmp(end, "C") || !strcmp(end, " channels")) |
436 | 0 | && channels > 0) { |
437 | 0 | channel_layout->order = AV_CHANNEL_ORDER_UNSPEC; |
438 | 0 | channel_layout->nb_channels = channels; |
439 | 0 | return 0; |
440 | 0 | } |
441 | | |
442 | 0 | return AVERROR(EINVAL); |
443 | 0 | } |
444 | | |
445 | | void av_channel_layout_uninit(AVChannelLayout *channel_layout) |
446 | 0 | { |
447 | 0 | if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) |
448 | 0 | av_freep(&channel_layout->u.map); |
449 | 0 | memset(channel_layout, 0, sizeof(*channel_layout)); |
450 | 0 | } |
451 | | |
452 | | int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src) |
453 | 0 | { |
454 | 0 | av_channel_layout_uninit(dst); |
455 | 0 | *dst = *src; |
456 | 0 | if (src->order == AV_CHANNEL_ORDER_CUSTOM) { |
457 | 0 | dst->u.map = av_malloc_array(src->nb_channels, sizeof(*dst->u.map)); |
458 | 0 | if (!dst->u.map) |
459 | 0 | return AVERROR(ENOMEM); |
460 | 0 | memcpy(dst->u.map, src->u.map, src->nb_channels * sizeof(*src->u.map)); |
461 | 0 | } |
462 | 0 | return 0; |
463 | 0 | } |
464 | | |
465 | | static int64_t masked_description(const AVChannelLayout *channel_layout, int start_channel) |
466 | 0 | { |
467 | 0 | uint64_t mask = 0; |
468 | 0 | for (int i = start_channel; i < channel_layout->nb_channels; i++) { |
469 | 0 | enum AVChannel ch = channel_layout->u.map[i].id; |
470 | 0 | if (ch >= 0 && ch < 63 && mask < (1ULL << ch)) |
471 | 0 | mask |= (1ULL << ch); |
472 | 0 | else |
473 | 0 | return AVERROR(EINVAL); |
474 | 0 | } |
475 | 0 | return mask; |
476 | 0 | } |
477 | | |
478 | | static int has_channel_names(const AVChannelLayout *channel_layout) |
479 | 0 | { |
480 | 0 | if (channel_layout->order != AV_CHANNEL_ORDER_CUSTOM) |
481 | 0 | return 0; |
482 | 0 | for (int i = 0; i < channel_layout->nb_channels; i++) |
483 | 0 | if (channel_layout->u.map[i].name[0]) |
484 | 0 | return 1; |
485 | 0 | return 0; |
486 | 0 | } |
487 | | |
488 | | int av_channel_layout_ambisonic_order(const AVChannelLayout *channel_layout) |
489 | 0 | { |
490 | 0 | int i, highest_ambi, order; |
491 | |
|
492 | 0 | if (channel_layout->order != AV_CHANNEL_ORDER_AMBISONIC && |
493 | 0 | channel_layout->order != AV_CHANNEL_ORDER_CUSTOM) |
494 | 0 | return AVERROR(EINVAL); |
495 | | |
496 | 0 | highest_ambi = -1; |
497 | 0 | if (channel_layout->order == AV_CHANNEL_ORDER_AMBISONIC) |
498 | 0 | highest_ambi = channel_layout->nb_channels - av_popcount64(channel_layout->u.mask) - 1; |
499 | 0 | else { |
500 | 0 | const AVChannelCustom *map = channel_layout->u.map; |
501 | 0 | av_assert0(channel_layout->order == AV_CHANNEL_ORDER_CUSTOM); |
502 | | |
503 | 0 | for (i = 0; i < channel_layout->nb_channels; i++) { |
504 | 0 | int is_ambi = CHAN_IS_AMBI(map[i].id); |
505 | | |
506 | | /* ambisonic following non-ambisonic */ |
507 | 0 | if (i > 0 && is_ambi && !CHAN_IS_AMBI(map[i - 1].id)) |
508 | 0 | return AVERROR(EINVAL); |
509 | | |
510 | | /* non-default ordering */ |
511 | 0 | if (is_ambi && map[i].id - AV_CHAN_AMBISONIC_BASE != i) |
512 | 0 | return AVERROR(EINVAL); |
513 | | |
514 | 0 | if (CHAN_IS_AMBI(map[i].id)) |
515 | 0 | highest_ambi = i; |
516 | 0 | } |
517 | 0 | } |
518 | | /* no ambisonic channels*/ |
519 | 0 | if (highest_ambi < 0) |
520 | 0 | return AVERROR(EINVAL); |
521 | | |
522 | 0 | order = floor(sqrt(highest_ambi)); |
523 | | /* incomplete order - some harmonics are missing */ |
524 | 0 | if ((order + 1) * (order + 1) != highest_ambi + 1) |
525 | 0 | return AVERROR(EINVAL); |
526 | | |
527 | 0 | return order; |
528 | 0 | } |
529 | | |
530 | | static enum AVChannelOrder canonical_order(AVChannelLayout *channel_layout) |
531 | 0 | { |
532 | 0 | int has_known_channel = 0; |
533 | 0 | int order; |
534 | |
|
535 | 0 | if (channel_layout->order != AV_CHANNEL_ORDER_CUSTOM) |
536 | 0 | return channel_layout->order; |
537 | | |
538 | 0 | if (has_channel_names(channel_layout)) |
539 | 0 | return AV_CHANNEL_ORDER_CUSTOM; |
540 | | |
541 | 0 | for (int i = 0; i < channel_layout->nb_channels && !has_known_channel; i++) |
542 | 0 | if (channel_layout->u.map[i].id != AV_CHAN_UNKNOWN) |
543 | 0 | has_known_channel = 1; |
544 | 0 | if (!has_known_channel) |
545 | 0 | return AV_CHANNEL_ORDER_UNSPEC; |
546 | | |
547 | 0 | if (masked_description(channel_layout, 0) > 0) |
548 | 0 | return AV_CHANNEL_ORDER_NATIVE; |
549 | | |
550 | 0 | order = av_channel_layout_ambisonic_order(channel_layout); |
551 | 0 | if (order >= 0 && masked_description(channel_layout, (order + 1) * (order + 1)) >= 0) |
552 | 0 | return AV_CHANNEL_ORDER_AMBISONIC; |
553 | | |
554 | 0 | return AV_CHANNEL_ORDER_CUSTOM; |
555 | 0 | } |
556 | | |
557 | | /** |
558 | | * If the custom layout is n-th order standard-order ambisonic, with optional |
559 | | * extra non-diegetic channels at the end, write its string description in bp. |
560 | | * Return a negative error code otherwise. |
561 | | */ |
562 | | static int try_describe_ambisonic(AVBPrint *bp, const AVChannelLayout *channel_layout) |
563 | 0 | { |
564 | 0 | int nb_ambi_channels; |
565 | 0 | int order = av_channel_layout_ambisonic_order(channel_layout); |
566 | 0 | if (order < 0) |
567 | 0 | return order; |
568 | | |
569 | 0 | av_bprintf(bp, "ambisonic %d", order); |
570 | | |
571 | | /* extra channels present */ |
572 | 0 | nb_ambi_channels = (order + 1) * (order + 1); |
573 | 0 | if (nb_ambi_channels < channel_layout->nb_channels) { |
574 | 0 | AVChannelLayout extra = { 0 }; |
575 | |
|
576 | 0 | if (channel_layout->order == AV_CHANNEL_ORDER_AMBISONIC) { |
577 | 0 | extra.order = AV_CHANNEL_ORDER_NATIVE; |
578 | 0 | extra.nb_channels = av_popcount64(channel_layout->u.mask); |
579 | 0 | extra.u.mask = channel_layout->u.mask; |
580 | 0 | } else { |
581 | 0 | int64_t mask; |
582 | 0 | if (!has_channel_names(channel_layout) && |
583 | 0 | (mask = masked_description(channel_layout, nb_ambi_channels)) > 0) { |
584 | 0 | extra.order = AV_CHANNEL_ORDER_NATIVE; |
585 | 0 | extra.nb_channels = av_popcount64(mask); |
586 | 0 | extra.u.mask = mask; |
587 | 0 | } else { |
588 | 0 | extra.order = AV_CHANNEL_ORDER_CUSTOM; |
589 | 0 | extra.nb_channels = channel_layout->nb_channels - nb_ambi_channels; |
590 | 0 | extra.u.map = channel_layout->u.map + nb_ambi_channels; |
591 | 0 | } |
592 | 0 | } |
593 | |
|
594 | 0 | av_bprint_chars(bp, '+', 1); |
595 | 0 | av_channel_layout_describe_bprint(&extra, bp); |
596 | | /* Not calling uninit here on extra because we don't own the u.map pointer */ |
597 | 0 | } |
598 | |
|
599 | 0 | return 0; |
600 | 0 | } |
601 | | |
602 | | int av_channel_layout_describe_bprint(const AVChannelLayout *channel_layout, |
603 | | AVBPrint *bp) |
604 | 0 | { |
605 | 0 | int i; |
606 | |
|
607 | 0 | switch (channel_layout->order) { |
608 | 0 | case AV_CHANNEL_ORDER_NATIVE: |
609 | 0 | for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++) |
610 | 0 | if (channel_layout->u.mask == channel_layout_map[i].layout.u.mask) { |
611 | 0 | av_bprintf(bp, "%s", channel_layout_map[i].name); |
612 | 0 | return 0; |
613 | 0 | } |
614 | 0 | av_fallthrough; |
615 | 0 | case AV_CHANNEL_ORDER_CUSTOM: |
616 | 0 | if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) { |
617 | 0 | int64_t mask; |
618 | 0 | int res = try_describe_ambisonic(bp, channel_layout); |
619 | 0 | if (res >= 0) |
620 | 0 | return 0; |
621 | 0 | if (!has_channel_names(channel_layout) && |
622 | 0 | (mask = masked_description(channel_layout, 0)) > 0) { |
623 | 0 | AVChannelLayout native = { .order = AV_CHANNEL_ORDER_NATIVE, |
624 | 0 | .nb_channels = av_popcount64(mask), |
625 | 0 | .u.mask = mask }; |
626 | 0 | return av_channel_layout_describe_bprint(&native, bp); |
627 | 0 | } |
628 | 0 | } |
629 | 0 | if (channel_layout->nb_channels) |
630 | 0 | av_bprintf(bp, "%d channels (", channel_layout->nb_channels); |
631 | 0 | for (i = 0; i < channel_layout->nb_channels; i++) { |
632 | 0 | enum AVChannel ch = av_channel_layout_channel_from_index(channel_layout, i); |
633 | |
|
634 | 0 | if (i) |
635 | 0 | av_bprintf(bp, "+"); |
636 | 0 | av_channel_name_bprint(bp, ch); |
637 | 0 | if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM && |
638 | 0 | channel_layout->u.map[i].name[0]) |
639 | 0 | av_bprintf(bp, "@%s", channel_layout->u.map[i].name); |
640 | 0 | } |
641 | 0 | if (channel_layout->nb_channels) { |
642 | 0 | av_bprintf(bp, ")"); |
643 | 0 | return 0; |
644 | 0 | } |
645 | 0 | av_fallthrough; |
646 | 0 | case AV_CHANNEL_ORDER_UNSPEC: |
647 | 0 | av_bprintf(bp, "%d channels", channel_layout->nb_channels); |
648 | 0 | return 0; |
649 | 0 | case AV_CHANNEL_ORDER_AMBISONIC: |
650 | 0 | return try_describe_ambisonic(bp, channel_layout); |
651 | 0 | default: |
652 | 0 | return AVERROR(EINVAL); |
653 | 0 | } |
654 | 0 | } |
655 | | |
656 | | int av_channel_layout_describe(const AVChannelLayout *channel_layout, |
657 | | char *buf, size_t buf_size) |
658 | 0 | { |
659 | 0 | AVBPrint bp; |
660 | 0 | int ret; |
661 | |
|
662 | 0 | if (!buf && buf_size) |
663 | 0 | return AVERROR(EINVAL); |
664 | | |
665 | 0 | av_bprint_init_for_buffer(&bp, buf, buf_size); |
666 | 0 | ret = av_channel_layout_describe_bprint(channel_layout, &bp); |
667 | 0 | if (ret < 0) |
668 | 0 | return ret; |
669 | | |
670 | 0 | if (bp.len >= INT_MAX) |
671 | 0 | return AVERROR(ERANGE); |
672 | 0 | return bp.len + 1; |
673 | 0 | } |
674 | | |
675 | | enum AVChannel |
676 | | av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout, |
677 | | unsigned int idx) |
678 | 0 | { |
679 | 0 | int i; |
680 | |
|
681 | 0 | if (idx >= channel_layout->nb_channels) |
682 | 0 | return AV_CHAN_NONE; |
683 | | |
684 | 0 | switch (channel_layout->order) { |
685 | 0 | case AV_CHANNEL_ORDER_CUSTOM: |
686 | 0 | return channel_layout->u.map[idx].id; |
687 | 0 | case AV_CHANNEL_ORDER_AMBISONIC: { |
688 | 0 | int ambi_channels = channel_layout->nb_channels - av_popcount64(channel_layout->u.mask); |
689 | 0 | if (idx < ambi_channels) |
690 | 0 | return AV_CHAN_AMBISONIC_BASE + idx; |
691 | 0 | idx -= ambi_channels; |
692 | 0 | } |
693 | 0 | av_fallthrough; |
694 | 0 | case AV_CHANNEL_ORDER_NATIVE: |
695 | 0 | for (i = 0; i < 64; i++) { |
696 | 0 | if ((1ULL << i) & channel_layout->u.mask && !idx--) |
697 | 0 | return i; |
698 | 0 | } |
699 | 0 | av_fallthrough; |
700 | 0 | default: |
701 | 0 | return AV_CHAN_NONE; |
702 | 0 | } |
703 | 0 | } |
704 | | |
705 | | enum AVChannel |
706 | | av_channel_layout_channel_from_string(const AVChannelLayout *channel_layout, |
707 | | const char *str) |
708 | 0 | { |
709 | 0 | int index = av_channel_layout_index_from_string(channel_layout, str); |
710 | |
|
711 | 0 | if (index < 0) |
712 | 0 | return AV_CHAN_NONE; |
713 | | |
714 | 0 | return av_channel_layout_channel_from_index(channel_layout, index); |
715 | 0 | } |
716 | | |
717 | | int av_channel_layout_index_from_channel(const AVChannelLayout *channel_layout, |
718 | | enum AVChannel channel) |
719 | 0 | { |
720 | 0 | int i; |
721 | |
|
722 | 0 | if (channel == AV_CHAN_NONE) |
723 | 0 | return AVERROR(EINVAL); |
724 | | |
725 | 0 | switch (channel_layout->order) { |
726 | 0 | case AV_CHANNEL_ORDER_CUSTOM: |
727 | 0 | for (i = 0; i < channel_layout->nb_channels; i++) |
728 | 0 | if (channel_layout->u.map[i].id == channel) |
729 | 0 | return i; |
730 | 0 | return AVERROR(EINVAL); |
731 | 0 | case AV_CHANNEL_ORDER_AMBISONIC: |
732 | 0 | case AV_CHANNEL_ORDER_NATIVE: { |
733 | 0 | uint64_t mask = channel_layout->u.mask; |
734 | 0 | int ambi_channels = channel_layout->nb_channels - av_popcount64(mask); |
735 | 0 | if (channel_layout->order == AV_CHANNEL_ORDER_AMBISONIC && |
736 | 0 | channel >= AV_CHAN_AMBISONIC_BASE) { |
737 | 0 | if (channel - AV_CHAN_AMBISONIC_BASE >= ambi_channels) |
738 | 0 | return AVERROR(EINVAL); |
739 | 0 | return channel - AV_CHAN_AMBISONIC_BASE; |
740 | 0 | } |
741 | 0 | if ((unsigned)channel > 63 || !(mask & (1ULL << channel))) |
742 | 0 | return AVERROR(EINVAL); |
743 | 0 | mask &= (1ULL << channel) - 1; |
744 | 0 | return av_popcount64(mask) + ambi_channels; |
745 | 0 | } |
746 | 0 | default: |
747 | 0 | return AVERROR(EINVAL); |
748 | 0 | } |
749 | 0 | } |
750 | | |
751 | | int av_channel_layout_index_from_string(const AVChannelLayout *channel_layout, |
752 | | const char *str) |
753 | 0 | { |
754 | 0 | const char *chname; |
755 | 0 | enum AVChannel ch = AV_CHAN_NONE; |
756 | |
|
757 | 0 | switch (channel_layout->order) { |
758 | 0 | case AV_CHANNEL_ORDER_CUSTOM: |
759 | 0 | chname = strstr(str, "@"); |
760 | 0 | if (chname) { |
761 | 0 | char buf[16]; |
762 | 0 | chname++; |
763 | 0 | av_strlcpy(buf, str, FFMIN(sizeof(buf), chname - str)); |
764 | 0 | if (!*chname) |
765 | 0 | chname = NULL; |
766 | 0 | ch = av_channel_from_string(buf); |
767 | 0 | if (ch == AV_CHAN_NONE && *buf) |
768 | 0 | return AVERROR(EINVAL); |
769 | 0 | } |
770 | 0 | for (int i = 0; chname && i < channel_layout->nb_channels; i++) { |
771 | 0 | if (!strcmp(chname, channel_layout->u.map[i].name) && |
772 | 0 | (ch == AV_CHAN_NONE || ch == channel_layout->u.map[i].id)) |
773 | 0 | return i; |
774 | 0 | } |
775 | 0 | av_fallthrough; |
776 | 0 | case AV_CHANNEL_ORDER_AMBISONIC: |
777 | 0 | case AV_CHANNEL_ORDER_NATIVE: |
778 | 0 | ch = av_channel_from_string(str); |
779 | 0 | if (ch == AV_CHAN_NONE) |
780 | 0 | return AVERROR(EINVAL); |
781 | 0 | return av_channel_layout_index_from_channel(channel_layout, ch); |
782 | 0 | } |
783 | | |
784 | 0 | return AVERROR(EINVAL); |
785 | 0 | } |
786 | | |
787 | | int av_channel_layout_check(const AVChannelLayout *channel_layout) |
788 | 0 | { |
789 | 0 | if (channel_layout->nb_channels <= 0) |
790 | 0 | return 0; |
791 | | |
792 | 0 | switch (channel_layout->order) { |
793 | 0 | case AV_CHANNEL_ORDER_NATIVE: |
794 | 0 | return av_popcount64(channel_layout->u.mask) == channel_layout->nb_channels; |
795 | 0 | case AV_CHANNEL_ORDER_CUSTOM: |
796 | 0 | if (!channel_layout->u.map) |
797 | 0 | return 0; |
798 | 0 | for (int i = 0; i < channel_layout->nb_channels; i++) { |
799 | 0 | if (channel_layout->u.map[i].id == AV_CHAN_NONE) |
800 | 0 | return 0; |
801 | 0 | } |
802 | 0 | return 1; |
803 | 0 | case AV_CHANNEL_ORDER_AMBISONIC: |
804 | | /* If non-diegetic channels are present, ensure they are taken into account */ |
805 | 0 | return av_popcount64(channel_layout->u.mask) < channel_layout->nb_channels; |
806 | 0 | case AV_CHANNEL_ORDER_UNSPEC: |
807 | 0 | return 1; |
808 | 0 | default: |
809 | 0 | return 0; |
810 | 0 | } |
811 | 0 | } |
812 | | |
813 | | int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1) |
814 | 0 | { |
815 | 0 | int i; |
816 | | |
817 | | /* different channel counts -> not equal */ |
818 | 0 | if (chl->nb_channels != chl1->nb_channels) |
819 | 0 | return 1; |
820 | | |
821 | | /* if only one is unspecified -> not equal */ |
822 | 0 | if ((chl->order == AV_CHANNEL_ORDER_UNSPEC) != |
823 | 0 | (chl1->order == AV_CHANNEL_ORDER_UNSPEC)) |
824 | 0 | return 1; |
825 | | /* both are unspecified -> equal */ |
826 | 0 | else if (chl->order == AV_CHANNEL_ORDER_UNSPEC) |
827 | 0 | return 0; |
828 | | |
829 | | /* can compare masks directly */ |
830 | 0 | if ((chl->order == AV_CHANNEL_ORDER_NATIVE || |
831 | 0 | chl->order == AV_CHANNEL_ORDER_AMBISONIC) && |
832 | 0 | chl->order == chl1->order) |
833 | 0 | return chl->u.mask != chl1->u.mask; |
834 | | |
835 | | /* compare channel by channel */ |
836 | 0 | for (i = 0; i < chl->nb_channels; i++) |
837 | 0 | if (av_channel_layout_channel_from_index(chl, i) != |
838 | 0 | av_channel_layout_channel_from_index(chl1, i)) |
839 | 0 | return 1; |
840 | 0 | return 0; |
841 | 0 | } |
842 | | |
843 | | void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels) |
844 | 0 | { |
845 | 0 | int i; |
846 | 0 | for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++) |
847 | 0 | if (nb_channels == channel_layout_map[i].layout.nb_channels) { |
848 | 0 | *ch_layout = channel_layout_map[i].layout; |
849 | 0 | return; |
850 | 0 | } |
851 | | |
852 | 0 | ch_layout->order = AV_CHANNEL_ORDER_UNSPEC; |
853 | 0 | ch_layout->nb_channels = nb_channels; |
854 | 0 | } |
855 | | |
856 | | const AVChannelLayout *av_channel_layout_standard(void **opaque) |
857 | 0 | { |
858 | 0 | uintptr_t i = (uintptr_t)*opaque; |
859 | 0 | const AVChannelLayout *ch_layout = NULL; |
860 | |
|
861 | 0 | if (i < FF_ARRAY_ELEMS(channel_layout_map)) { |
862 | 0 | ch_layout = &channel_layout_map[i].layout; |
863 | 0 | *opaque = (void*)(i + 1); |
864 | 0 | } |
865 | |
|
866 | 0 | return ch_layout; |
867 | 0 | } |
868 | | |
869 | | uint64_t av_channel_layout_subset(const AVChannelLayout *channel_layout, |
870 | | uint64_t mask) |
871 | 0 | { |
872 | 0 | uint64_t ret = 0; |
873 | 0 | int i; |
874 | |
|
875 | 0 | switch (channel_layout->order) { |
876 | 0 | case AV_CHANNEL_ORDER_NATIVE: |
877 | 0 | case AV_CHANNEL_ORDER_AMBISONIC: |
878 | 0 | return channel_layout->u.mask & mask; |
879 | 0 | case AV_CHANNEL_ORDER_CUSTOM: |
880 | 0 | for (i = 0; i < 64; i++) |
881 | 0 | if (mask & (1ULL << i) && av_channel_layout_index_from_channel(channel_layout, i) >= 0) |
882 | 0 | ret |= (1ULL << i); |
883 | 0 | break; |
884 | 0 | } |
885 | | |
886 | 0 | return ret; |
887 | 0 | } |
888 | | |
889 | | int av_channel_layout_retype(AVChannelLayout *channel_layout, enum AVChannelOrder order, int flags) |
890 | 0 | { |
891 | 0 | int allow_lossy = !(flags & AV_CHANNEL_LAYOUT_RETYPE_FLAG_LOSSLESS); |
892 | 0 | int lossy; |
893 | |
|
894 | 0 | if (!av_channel_layout_check(channel_layout)) |
895 | 0 | return AVERROR(EINVAL); |
896 | | |
897 | 0 | if (flags & AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL) |
898 | 0 | order = canonical_order(channel_layout); |
899 | |
|
900 | 0 | if (channel_layout->order == order) |
901 | 0 | return 0; |
902 | | |
903 | 0 | switch (order) { |
904 | 0 | case AV_CHANNEL_ORDER_UNSPEC: { |
905 | 0 | int nb_channels = channel_layout->nb_channels; |
906 | 0 | if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) { |
907 | 0 | lossy = 0; |
908 | 0 | for (int i = 0; i < nb_channels; i++) { |
909 | 0 | if (channel_layout->u.map[i].id != AV_CHAN_UNKNOWN || channel_layout->u.map[i].name[0]) { |
910 | 0 | lossy = 1; |
911 | 0 | break; |
912 | 0 | } |
913 | 0 | } |
914 | 0 | } else { |
915 | 0 | lossy = 1; |
916 | 0 | } |
917 | 0 | if (!lossy || allow_lossy) { |
918 | 0 | void *opaque = channel_layout->opaque; |
919 | 0 | av_channel_layout_uninit(channel_layout); |
920 | 0 | channel_layout->order = AV_CHANNEL_ORDER_UNSPEC; |
921 | 0 | channel_layout->nb_channels = nb_channels; |
922 | 0 | channel_layout->opaque = opaque; |
923 | 0 | return lossy; |
924 | 0 | } |
925 | 0 | return AVERROR(ENOSYS); |
926 | 0 | } |
927 | 0 | case AV_CHANNEL_ORDER_NATIVE: |
928 | 0 | if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) { |
929 | 0 | int64_t mask = masked_description(channel_layout, 0); |
930 | 0 | if (mask < 0) |
931 | 0 | return AVERROR(ENOSYS); |
932 | 0 | lossy = has_channel_names(channel_layout); |
933 | 0 | if (!lossy || allow_lossy) { |
934 | 0 | void *opaque = channel_layout->opaque; |
935 | 0 | av_channel_layout_uninit(channel_layout); |
936 | 0 | av_channel_layout_from_mask(channel_layout, mask); |
937 | 0 | channel_layout->opaque = opaque; |
938 | 0 | return lossy; |
939 | 0 | } |
940 | 0 | } |
941 | 0 | return AVERROR(ENOSYS); |
942 | 0 | case AV_CHANNEL_ORDER_CUSTOM: { |
943 | 0 | AVChannelLayout custom = { 0 }; |
944 | 0 | int ret = av_channel_layout_custom_init(&custom, channel_layout->nb_channels); |
945 | 0 | void *opaque = channel_layout->opaque; |
946 | 0 | if (ret < 0) |
947 | 0 | return ret; |
948 | 0 | if (channel_layout->order != AV_CHANNEL_ORDER_UNSPEC) |
949 | 0 | for (int i = 0; i < channel_layout->nb_channels; i++) |
950 | 0 | custom.u.map[i].id = av_channel_layout_channel_from_index(channel_layout, i); |
951 | 0 | av_channel_layout_uninit(channel_layout); |
952 | 0 | *channel_layout = custom; |
953 | 0 | channel_layout->opaque = opaque; |
954 | 0 | return 0; |
955 | 0 | } |
956 | 0 | case AV_CHANNEL_ORDER_AMBISONIC: |
957 | 0 | if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) { |
958 | 0 | int64_t mask; |
959 | 0 | int nb_channels = channel_layout->nb_channels; |
960 | 0 | int amb_order = av_channel_layout_ambisonic_order(channel_layout); |
961 | 0 | if (amb_order < 0) |
962 | 0 | return AVERROR(ENOSYS); |
963 | 0 | mask = masked_description(channel_layout, (amb_order + 1) * (amb_order + 1)); |
964 | 0 | if (mask < 0) |
965 | 0 | return AVERROR(ENOSYS); |
966 | 0 | lossy = has_channel_names(channel_layout); |
967 | 0 | if (!lossy || allow_lossy) { |
968 | 0 | void *opaque = channel_layout->opaque; |
969 | 0 | av_channel_layout_uninit(channel_layout); |
970 | 0 | channel_layout->order = AV_CHANNEL_ORDER_AMBISONIC; |
971 | 0 | channel_layout->nb_channels = nb_channels; |
972 | 0 | channel_layout->u.mask = mask; |
973 | 0 | channel_layout->opaque = opaque; |
974 | 0 | return lossy; |
975 | 0 | } |
976 | 0 | } |
977 | 0 | return AVERROR(ENOSYS); |
978 | 0 | default: |
979 | 0 | return AVERROR(EINVAL); |
980 | 0 | } |
981 | 0 | } |