FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/channel_layout.c
Date: 2026-08-31 23:16:59
Exec Total Coverage
Lines: 483 531 91.0%
Functions: 28 28 100.0%
Branches: 355 434 81.8%

Line Branch Exec 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 #define CHAN_IS_AMBI(x) ((x) >= AV_CHAN_AMBISONIC_BASE &&\
41 (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 17184 void av_channel_name_bprint(AVBPrint *bp, enum AVChannel channel_id)
88 {
89
3/4
✓ Branch 0 taken 579 times.
✓ Branch 1 taken 16605 times.
✓ Branch 2 taken 579 times.
✗ Branch 3 not taken.
17184 if (channel_id >= AV_CHAN_AMBISONIC_BASE &&
90 channel_id <= AV_CHAN_AMBISONIC_END)
91 579 av_bprintf(bp, "AMBI%d", channel_id - AV_CHAN_AMBISONIC_BASE);
92
2/2
✓ Branch 0 taken 16508 times.
✓ Branch 1 taken 97 times.
16605 else if ((unsigned)channel_id < FF_ARRAY_ELEMS(channel_names) &&
93
1/2
✓ Branch 0 taken 16508 times.
✗ Branch 1 not taken.
16508 channel_names[channel_id].name)
94 16508 av_bprintf(bp, "%s", channel_names[channel_id].name);
95
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 33 times.
97 else if (channel_id == AV_CHAN_NONE)
96 64 av_bprintf(bp, "NONE");
97
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 22 times.
33 else if (channel_id == AV_CHAN_UNKNOWN)
98 11 av_bprintf(bp, "UNK");
99
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 9 times.
22 else if (channel_id == AV_CHAN_UNUSED)
100 13 av_bprintf(bp, "UNSD");
101 else
102 9 av_bprintf(bp, "USR%d", channel_id);
103 17184 }
104
105 15061 int av_channel_name(char *buf, size_t buf_size, enum AVChannel channel_id)
106 {
107 AVBPrint bp;
108
109
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 15056 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
15061 if (!buf && buf_size)
110 return AVERROR(EINVAL);
111
112 15061 av_bprint_init_for_buffer(&bp, buf, buf_size);
113 15061 av_channel_name_bprint(&bp, channel_id);
114
115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15061 times.
15061 if (bp.len >= INT_MAX)
116 return AVERROR(ERANGE);
117 15061 return bp.len + 1;
118 }
119
120 15 void av_channel_description_bprint(AVBPrint *bp, enum AVChannel channel_id)
121 {
122
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
15 if (channel_id >= AV_CHAN_AMBISONIC_BASE &&
123 channel_id <= AV_CHAN_AMBISONIC_END)
124 6 av_bprintf(bp, "ambisonic ACN %d", channel_id - AV_CHAN_AMBISONIC_BASE);
125
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 else if ((unsigned)channel_id < FF_ARRAY_ELEMS(channel_names) &&
126
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 channel_names[channel_id].description)
127 6 av_bprintf(bp, "%s", channel_names[channel_id].description);
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 else if (channel_id == AV_CHAN_NONE)
129 av_bprintf(bp, "none");
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 else if (channel_id == AV_CHAN_UNKNOWN)
131 av_bprintf(bp, "unknown");
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 else if (channel_id == AV_CHAN_UNUSED)
133 av_bprintf(bp, "unused");
134 else
135 3 av_bprintf(bp, "user %d", channel_id);
136 15 }
137
138 10 int av_channel_description(char *buf, size_t buf_size, enum AVChannel channel_id)
139 {
140 AVBPrint bp;
141
142
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
10 if (!buf && buf_size)
143 return AVERROR(EINVAL);
144
145 10 av_bprint_init_for_buffer(&bp, buf, buf_size);
146 10 av_channel_description_bprint(&bp, channel_id);
147
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (bp.len >= INT_MAX)
149 return AVERROR(ERANGE);
150 10 return bp.len + 1;
151 }
152
153 776 enum AVChannel av_channel_from_string(const char *str)
154 {
155 int i;
156 776 char *endptr = (char *)str;
157 776 enum AVChannel id = AV_CHAN_NONE;
158
159
2/2
✓ Branch 0 taken 260 times.
✓ Branch 1 taken 516 times.
776 if (!strncmp(str, "AMBI", 4)) {
160 260 i = strtol(str + 4, NULL, 0);
161
3/4
✓ Branch 0 taken 260 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 259 times.
260 if (i < 0 || i > AV_CHAN_AMBISONIC_END - AV_CHAN_AMBISONIC_BASE)
162 1 return AV_CHAN_NONE;
163 259 return AV_CHAN_AMBISONIC_BASE + i;
164 }
165
166
2/2
✓ Branch 0 taken 9616 times.
✓ Branch 1 taken 128 times.
9744 for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++) {
167
4/4
✓ Branch 0 taken 6116 times.
✓ Branch 1 taken 3500 times.
✓ Branch 2 taken 388 times.
✓ Branch 3 taken 5728 times.
9616 if (channel_names[i].name && !strcmp(str, channel_names[i].name))
168 388 return i;
169 }
170
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 126 times.
128 if (!strcmp(str, "UNK"))
171 2 return AV_CHAN_UNKNOWN;
172
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 121 times.
126 if (!strcmp(str, "UNSD"))
173 5 return AV_CHAN_UNUSED;
174
175
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 114 times.
121 if (!strncmp(str, "USR", 3)) {
176 7 const char *p = str + 3;
177 7 id = strtol(p, &endptr, 0);
178 }
179
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 114 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
121 if (id >= 0 && !*endptr)
180 7 return id;
181
182 114 return AV_CHAN_NONE;
183 }
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 { "7.1.2", AV_CHANNEL_LAYOUT_7POINT1POINT2 },
223 { "7.1.4", AV_CHANNEL_LAYOUT_7POINT1POINT4 },
224 { "7.2.3", AV_CHANNEL_LAYOUT_7POINT2POINT3 },
225 { "9.1.4", AV_CHANNEL_LAYOUT_9POINT1POINT4 },
226 { "9.1.6", AV_CHANNEL_LAYOUT_9POINT1POINT6 },
227 { "hexadecagonal", AV_CHANNEL_LAYOUT_HEXADECAGONAL },
228 { "binaural", AV_CHANNEL_LAYOUT_BINAURAL },
229 { "downmix", AV_CHANNEL_LAYOUT_STEREO_DOWNMIX, },
230 { "22.2", AV_CHANNEL_LAYOUT_22POINT2, },
231 };
232
233 1309 int av_channel_layout_custom_init(AVChannelLayout *channel_layout, int nb_channels)
234 {
235 AVChannelCustom *map;
236
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1309 times.
1309 if (nb_channels <= 0)
238 return AVERROR(EINVAL);
239
240 1309 map = av_calloc(nb_channels, sizeof(*channel_layout->u.map));
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1309 times.
1309 if (!map)
242 return AVERROR(ENOMEM);
243
2/2
✓ Branch 0 taken 9967 times.
✓ Branch 1 taken 1309 times.
11276 for (int i = 0; i < nb_channels; i++)
244 9967 map[i].id = AV_CHAN_UNKNOWN;
245
246 1309 channel_layout->order = AV_CHANNEL_ORDER_CUSTOM;
247 1309 channel_layout->nb_channels = nb_channels;
248 1309 channel_layout->u.map = map;
249
250 1309 return 0;
251 }
252
253 28438 int av_channel_layout_from_mask(AVChannelLayout *channel_layout,
254 uint64_t mask)
255 {
256
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 28397 times.
28438 if (!mask)
257 41 return AVERROR(EINVAL);
258
259 28397 channel_layout->order = AV_CHANNEL_ORDER_NATIVE;
260 28397 channel_layout->nb_channels = av_popcount64(mask);
261 28397 channel_layout->u.mask = mask;
262
263 28397 return 0;
264 }
265
266 196 static int parse_channel_list(AVChannelLayout *ch_layout, const char *str)
267 {
268 int ret;
269 196 int nb_channels = 0;
270 196 AVChannelCustom *map = NULL;
271 196 AVChannelCustom custom = {0};
272
273
2/2
✓ Branch 0 taken 371 times.
✓ Branch 1 taken 95 times.
466 while (*str) {
274 char *channel, *chname;
275 371 ret = av_opt_get_key_value(&str, "@", "+", AV_OPT_FLAG_IMPLICIT_KEY, &channel, &chname);
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 371 times.
371 if (ret < 0) {
277 av_freep(&map);
278 101 return ret;
279 }
280
2/2
✓ Branch 0 taken 177 times.
✓ Branch 1 taken 194 times.
371 if (*str)
281 177 str++; // skip separator
282
2/2
✓ Branch 0 taken 357 times.
✓ Branch 1 taken 14 times.
371 if (!channel) {
283 357 channel = chname;
284 357 chname = NULL;
285 }
286
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 357 times.
371 av_strlcpy(custom.name, chname ? chname : "", sizeof(custom.name));
287 371 custom.id = av_channel_from_string(channel);
288 371 av_free(channel);
289 371 av_free(chname);
290
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 270 times.
371 if (custom.id == AV_CHAN_NONE) {
291 101 av_freep(&map);
292 101 return AVERROR(EINVAL);
293 }
294
295 270 av_dynarray2_add((void **)&map, &nb_channels, sizeof(custom), (void *)&custom);
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 270 times.
270 if (!map)
297 return AVERROR(ENOMEM);
298 }
299
300
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 94 times.
95 if (!nb_channels)
301 1 return AVERROR(EINVAL);
302
303 94 ch_layout->order = AV_CHANNEL_ORDER_CUSTOM;
304 94 ch_layout->u.map = map;
305 94 ch_layout->nb_channels = nb_channels;
306
307 94 ret = av_channel_layout_retype(ch_layout, 0, AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL);
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 94 times.
94 av_assert0(ret == 0);
309
310 94 return 0;
311 }
312
313 2576 int av_channel_layout_from_string(AVChannelLayout *channel_layout,
314 const char *str)
315 {
316 int i, matches, ret;
317 2576 int channels = 0, nb_channels = 0;
318 char *chlist, *end;
319 2576 uint64_t mask = 0;
320
321 /* channel layout names */
322
2/2
✓ Branch 0 taken 22488 times.
✓ Branch 1 taken 211 times.
22699 for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++) {
323
3/4
✓ Branch 0 taken 22488 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2365 times.
✓ Branch 3 taken 20123 times.
22488 if (channel_layout_map[i].name && !strcmp(str, channel_layout_map[i].name)) {
324 2365 *channel_layout = channel_layout_map[i].layout;
325 2365 return 0;
326 }
327 }
328
329 /* This function is a channel layout initializer, so we have to
330 * zero-initialize before we start setting fields individually. */
331 211 memset(channel_layout, 0, sizeof(*channel_layout));
332
333 /* ambisonic */
334
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 196 times.
211 if (!strncmp(str, "ambisonic ", 10)) {
335 15 const char *p = str + 10;
336 char *endptr;
337 15 AVChannelLayout extra = {0};
338 int order;
339
340 15 order = strtol(p, &endptr, 0);
341
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
15 if (order < 0 || order + 1 > INT_MAX / (order + 1) ||
342
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
15 (*endptr && *endptr != '+'))
343 return AVERROR(EINVAL);
344
345 15 channel_layout->order = AV_CHANNEL_ORDER_AMBISONIC;
346 15 channel_layout->nb_channels = (order + 1) * (order + 1);
347
348
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5 times.
15 if (*endptr) {
349 10 ret = av_channel_layout_from_string(&extra, endptr + 1);
350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (ret < 0)
351 return ret;
352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (extra.nb_channels >= INT_MAX - channel_layout->nb_channels) {
353 av_channel_layout_uninit(&extra);
354 return AVERROR(EINVAL);
355 }
356
357
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 if (extra.order == AV_CHANNEL_ORDER_NATIVE) {
358 8 channel_layout->u.mask = extra.u.mask;
359 } else {
360 2 channel_layout->order = AV_CHANNEL_ORDER_CUSTOM;
361 2 channel_layout->u.map =
362 2 av_calloc(channel_layout->nb_channels + extra.nb_channels,
363 sizeof(*channel_layout->u.map));
364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!channel_layout->u.map) {
365 av_channel_layout_uninit(&extra);
366 return AVERROR(ENOMEM);
367 }
368
369
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 2 times.
15 for (i = 0; i < channel_layout->nb_channels; i++)
370 13 channel_layout->u.map[i].id = AV_CHAN_AMBISONIC_BASE + i;
371
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 for (i = 0; i < extra.nb_channels; i++) {
372 3 enum AVChannel ch = av_channel_layout_channel_from_index(&extra, i);
373
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 if (CHAN_IS_AMBI(ch)) {
374 av_channel_layout_uninit(channel_layout);
375 av_channel_layout_uninit(&extra);
376 return AVERROR(EINVAL);
377 }
378 3 channel_layout->u.map[channel_layout->nb_channels + i].id = ch;
379
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (extra.order == AV_CHANNEL_ORDER_CUSTOM &&
380
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 extra.u.map[i].name[0])
381 1 av_strlcpy(channel_layout->u.map[channel_layout->nb_channels + i].name,
382 1 extra.u.map[i].name,
383 sizeof(channel_layout->u.map[channel_layout->nb_channels + i].name));
384 }
385 }
386 10 channel_layout->nb_channels += extra.nb_channels;
387 10 av_channel_layout_uninit(&extra);
388 }
389
390 15 return 0;
391 }
392
393 196 chlist = av_strdup(str);
394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 196 times.
196 if (!chlist)
395 return AVERROR(ENOMEM);
396
397 /* channel names */
398 196 matches = av_sscanf(str, "%d channels (%[^)]", &nb_channels, chlist);
399 196 ret = parse_channel_list(channel_layout, chlist);
400 196 av_freep(&chlist);
401
3/4
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 94 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 102 times.
196 if (ret < 0 && ret != AVERROR(EINVAL))
402 return ret;
403
404
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 102 times.
196 if (ret >= 0) {
405 94 const char *end_par = strchr(str, ')');
406
7/8
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 47 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 46 times.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 46 times.
94 if (matches == 2 && (nb_channels != channel_layout->nb_channels || !end_par || *++end_par)) {
407 3 av_channel_layout_uninit(channel_layout);
408 3 return AVERROR(EINVAL);
409 }
410 91 return 0;
411 }
412
413 102 errno = 0;
414 102 mask = strtoull(str, &end, 0);
415
416 /* channel layout mask */
417
7/8
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 49 times.
✓ Branch 3 taken 53 times.
✓ Branch 4 taken 48 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 47 times.
✓ Branch 7 taken 1 times.
102 if (!errno && !*end && !strchr(str, '-') && mask) {
418 47 av_channel_layout_from_mask(channel_layout, mask);
419 47 return 0;
420 }
421
422 55 errno = 0;
423 55 channels = strtol(str, &end, 10);
424
425 /* number of channels */
426
5/6
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 43 times.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 1 times.
55 if (!errno && !strcmp(end, "c") && channels > 0) {
427 11 av_channel_layout_default(channel_layout, channels);
428
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2 times.
11 if (channel_layout->order == AV_CHANNEL_ORDER_NATIVE)
429 9 return 0;
430 }
431
432 /* number of unordered channels */
433
5/6
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 21 times.
46 if (!errno && (!strcmp(end, "C") || !strcmp(end, " channels"))
434
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 && channels > 0) {
435 25 channel_layout->order = AV_CHANNEL_ORDER_UNSPEC;
436 25 channel_layout->nb_channels = channels;
437 25 return 0;
438 }
439
440 21 return AVERROR(EINVAL);
441 }
442
443 12096750 void av_channel_layout_uninit(AVChannelLayout *channel_layout)
444 {
445
2/2
✓ Branch 0 taken 14324 times.
✓ Branch 1 taken 12082426 times.
12096750 if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM)
446 14324 av_freep(&channel_layout->u.map);
447 12096750 memset(channel_layout, 0, sizeof(*channel_layout));
448 12096750 }
449
450 2705311 int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
451 {
452 2705311 av_channel_layout_uninit(dst);
453 2705311 *dst = *src;
454
2/2
✓ Branch 0 taken 12851 times.
✓ Branch 1 taken 2692460 times.
2705311 if (src->order == AV_CHANNEL_ORDER_CUSTOM) {
455 12851 dst->u.map = av_malloc_array(src->nb_channels, sizeof(*dst->u.map));
456
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12851 times.
12851 if (!dst->u.map)
457 return AVERROR(ENOMEM);
458 12851 memcpy(dst->u.map, src->u.map, src->nb_channels * sizeof(*src->u.map));
459 }
460 2705311 return 0;
461 }
462
463 911 static int64_t masked_description(const AVChannelLayout *channel_layout, int start_channel)
464 {
465 911 uint64_t mask = 0;
466
2/2
✓ Branch 0 taken 1794 times.
✓ Branch 1 taken 298 times.
2092 for (int i = start_channel; i < channel_layout->nb_channels; i++) {
467 1794 enum AVChannel ch = channel_layout->u.map[i].id;
468
5/6
✓ Branch 0 taken 1794 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1333 times.
✓ Branch 3 taken 461 times.
✓ Branch 4 taken 1181 times.
✓ Branch 5 taken 152 times.
1794 if (ch >= 0 && ch < 63 && mask < (1ULL << ch))
469 1181 mask |= (1ULL << ch);
470 else
471 613 return AVERROR(EINVAL);
472 }
473 298 return mask;
474 }
475
476 884 static int has_channel_names(const AVChannelLayout *channel_layout)
477 {
478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 884 times.
884 if (channel_layout->order != AV_CHANNEL_ORDER_CUSTOM)
479 return 0;
480
2/2
✓ Branch 0 taken 1995 times.
✓ Branch 1 taken 865 times.
2860 for (int i = 0; i < channel_layout->nb_channels; i++)
481
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1976 times.
1995 if (channel_layout->u.map[i].name[0])
482 19 return 1;
483 865 return 0;
484 }
485
486 627 int av_channel_layout_ambisonic_order(const AVChannelLayout *channel_layout)
487 {
488 int i, highest_ambi, order;
489
490
2/2
✓ Branch 0 taken 592 times.
✓ Branch 1 taken 35 times.
627 if (channel_layout->order != AV_CHANNEL_ORDER_AMBISONIC &&
491
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 592 times.
592 channel_layout->order != AV_CHANNEL_ORDER_CUSTOM)
492 return AVERROR(EINVAL);
493
494 627 highest_ambi = -1;
495
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 592 times.
627 if (channel_layout->order == AV_CHANNEL_ORDER_AMBISONIC)
496 35 highest_ambi = channel_layout->nb_channels - av_popcount64(channel_layout->u.mask) - 1;
497 else {
498 592 const AVChannelCustom *map = channel_layout->u.map;
499
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 592 times.
592 av_assert0(channel_layout->order == AV_CHANNEL_ORDER_CUSTOM);
500
501
2/2
✓ Branch 0 taken 1356 times.
✓ Branch 1 taken 147 times.
1503 for (i = 0; i < channel_layout->nb_channels; i++) {
502
3/4
✓ Branch 0 taken 511 times.
✓ Branch 1 taken 845 times.
✓ Branch 2 taken 511 times.
✗ Branch 3 not taken.
1356 int is_ambi = CHAN_IS_AMBI(map[i].id);
503
504 /* ambisonic following non-ambisonic */
505
6/8
✓ Branch 0 taken 764 times.
✓ Branch 1 taken 592 times.
✓ Branch 2 taken 53 times.
✓ Branch 3 taken 711 times.
✓ Branch 4 taken 53 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 53 times.
1356 if (i > 0 && is_ambi && !CHAN_IS_AMBI(map[i - 1].id))
506 return AVERROR(EINVAL);
507
508 /* non-default ordering */
509
4/4
✓ Branch 0 taken 511 times.
✓ Branch 1 taken 845 times.
✓ Branch 2 taken 445 times.
✓ Branch 3 taken 66 times.
1356 if (is_ambi && map[i].id - AV_CHAN_AMBISONIC_BASE != i)
510 445 return AVERROR(EINVAL);
511
512
3/4
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 845 times.
✓ Branch 2 taken 66 times.
✗ Branch 3 not taken.
911 if (CHAN_IS_AMBI(map[i].id))
513 66 highest_ambi = i;
514 }
515 }
516 /* no ambisonic channels*/
517
2/2
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 48 times.
182 if (highest_ambi < 0)
518 134 return AVERROR(EINVAL);
519
520 48 order = floor(sqrt(highest_ambi));
521 /* incomplete order - some harmonics are missing */
522
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if ((order + 1) * (order + 1) != highest_ambi + 1)
523 return AVERROR(EINVAL);
524
525 48 return order;
526 }
527
528 337 static enum AVChannelOrder canonical_order(AVChannelLayout *channel_layout)
529 {
530 337 int has_known_channel = 0;
531 int order;
532
533
2/2
✓ Branch 0 taken 117 times.
✓ Branch 1 taken 220 times.
337 if (channel_layout->order != AV_CHANNEL_ORDER_CUSTOM)
534 117 return channel_layout->order;
535
536
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 216 times.
220 if (has_channel_names(channel_layout))
537 4 return AV_CHANNEL_ORDER_CUSTOM;
538
539
4/4
✓ Branch 0 taken 319 times.
✓ Branch 1 taken 114 times.
✓ Branch 2 taken 217 times.
✓ Branch 3 taken 102 times.
433 for (int i = 0; i < channel_layout->nb_channels && !has_known_channel; i++)
540
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 1 times.
217 if (channel_layout->u.map[i].id != AV_CHAN_UNKNOWN)
541 216 has_known_channel = 1;
542
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
216 if (!has_known_channel)
543 return AV_CHANNEL_ORDER_UNSPEC;
544
545
2/2
✓ Branch 1 taken 132 times.
✓ Branch 2 taken 84 times.
216 if (masked_description(channel_layout, 0) > 0)
546 132 return AV_CHANNEL_ORDER_NATIVE;
547
548 84 order = av_channel_layout_ambisonic_order(channel_layout);
549
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 83 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
84 if (order >= 0 && masked_description(channel_layout, (order + 1) * (order + 1)) >= 0)
550 1 return AV_CHANNEL_ORDER_AMBISONIC;
551
552 83 return AV_CHANNEL_ORDER_CUSTOM;
553 }
554
555 /**
556 * If the custom layout is n-th order standard-order ambisonic, with optional
557 * extra non-diegetic channels at the end, write its string description in bp.
558 * Return a negative error code otherwise.
559 */
560 536 static int try_describe_ambisonic(AVBPrint *bp, const AVChannelLayout *channel_layout)
561 {
562 int nb_ambi_channels;
563 536 int order = av_channel_layout_ambisonic_order(channel_layout);
564
2/2
✓ Branch 0 taken 494 times.
✓ Branch 1 taken 42 times.
536 if (order < 0)
565 494 return order;
566
567 42 av_bprintf(bp, "ambisonic %d", order);
568
569 /* extra channels present */
570 42 nb_ambi_channels = (order + 1) * (order + 1);
571
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 30 times.
42 if (nb_ambi_channels < channel_layout->nb_channels) {
572 12 AVChannelLayout extra = { 0 };
573
574
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 7 times.
12 if (channel_layout->order == AV_CHANNEL_ORDER_AMBISONIC) {
575 5 extra.order = AV_CHANNEL_ORDER_NATIVE;
576 5 extra.nb_channels = av_popcount64(channel_layout->u.mask);
577 5 extra.u.mask = channel_layout->u.mask;
578 } else {
579 int64_t mask;
580
4/4
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 3 times.
11 if (!has_channel_names(channel_layout) &&
581 4 (mask = masked_description(channel_layout, nb_ambi_channels)) > 0) {
582 1 extra.order = AV_CHANNEL_ORDER_NATIVE;
583 1 extra.nb_channels = av_popcount64(mask);
584 1 extra.u.mask = mask;
585 } else {
586 6 extra.order = AV_CHANNEL_ORDER_CUSTOM;
587 6 extra.nb_channels = channel_layout->nb_channels - nb_ambi_channels;
588 6 extra.u.map = channel_layout->u.map + nb_ambi_channels;
589 }
590 }
591
592 12 av_bprint_chars(bp, '+', 1);
593 12 av_channel_layout_describe_bprint(&extra, bp);
594 /* Not calling uninit here on extra because we don't own the u.map pointer */
595 }
596
597 42 return 0;
598 }
599
600 18557 int av_channel_layout_describe_bprint(const AVChannelLayout *channel_layout,
601 AVBPrint *bp)
602 {
603 int i;
604
605
4/5
✓ Branch 0 taken 17856 times.
✓ Branch 1 taken 501 times.
✓ Branch 2 taken 165 times.
✓ Branch 3 taken 35 times.
✗ Branch 4 not taken.
18557 switch (channel_layout->order) {
606 17856 case AV_CHANNEL_ORDER_NATIVE:
607
2/2
✓ Branch 0 taken 66611 times.
✓ Branch 1 taken 215 times.
66826 for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
608
2/2
✓ Branch 0 taken 17641 times.
✓ Branch 1 taken 48970 times.
66611 if (channel_layout->u.mask == channel_layout_map[i].layout.u.mask) {
609 17641 av_bprintf(bp, "%s", channel_layout_map[i].name);
610 17641 return 0;
611 }
612 av_fallthrough;
613 case AV_CHANNEL_ORDER_CUSTOM:
614
2/2
✓ Branch 0 taken 501 times.
✓ Branch 1 taken 215 times.
716 if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) {
615 int64_t mask;
616 501 int res = try_describe_ambisonic(bp, channel_layout);
617
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 494 times.
501 if (res >= 0)
618 7 return 0;
619
4/4
✓ Branch 1 taken 483 times.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 482 times.
977 if (!has_channel_names(channel_layout) &&
620 483 (mask = masked_description(channel_layout, 0)) > 0) {
621 1 AVChannelLayout native = { .order = AV_CHANNEL_ORDER_NATIVE,
622 1 .nb_channels = av_popcount64(mask),
623 .u.mask = mask };
624 1 return av_channel_layout_describe_bprint(&native, bp);
625 }
626 }
627
1/2
✓ Branch 0 taken 708 times.
✗ Branch 1 not taken.
708 if (channel_layout->nb_channels)
628 708 av_bprintf(bp, "%d channels (", channel_layout->nb_channels);
629
2/2
✓ Branch 0 taken 1830 times.
✓ Branch 1 taken 708 times.
2538 for (i = 0; i < channel_layout->nb_channels; i++) {
630 1830 enum AVChannel ch = av_channel_layout_channel_from_index(channel_layout, i);
631
632
2/2
✓ Branch 0 taken 1122 times.
✓ Branch 1 taken 708 times.
1830 if (i)
633 1122 av_bprintf(bp, "+");
634 1830 av_channel_name_bprint(bp, ch);
635
2/2
✓ Branch 0 taken 1133 times.
✓ Branch 1 taken 697 times.
1830 if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM &&
636
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 1116 times.
1133 channel_layout->u.map[i].name[0])
637 17 av_bprintf(bp, "@%s", channel_layout->u.map[i].name);
638 }
639
1/2
✓ Branch 0 taken 708 times.
✗ Branch 1 not taken.
708 if (channel_layout->nb_channels) {
640 708 av_bprintf(bp, ")");
641 708 return 0;
642 }
643 av_fallthrough;
644 case AV_CHANNEL_ORDER_UNSPEC:
645 165 av_bprintf(bp, "%d channels", channel_layout->nb_channels);
646 165 return 0;
647 35 case AV_CHANNEL_ORDER_AMBISONIC:
648 35 return try_describe_ambisonic(bp, channel_layout);
649 default:
650 return AVERROR(EINVAL);
651 }
652 }
653
654 12377 int av_channel_layout_describe(const AVChannelLayout *channel_layout,
655 char *buf, size_t buf_size)
656 {
657 AVBPrint bp;
658 int ret;
659
660
3/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 12355 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
12377 if (!buf && buf_size)
661 return AVERROR(EINVAL);
662
663 12377 av_bprint_init_for_buffer(&bp, buf, buf_size);
664 12377 ret = av_channel_layout_describe_bprint(channel_layout, &bp);
665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12377 times.
12377 if (ret < 0)
666 return ret;
667
668
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12377 times.
12377 if (bp.len >= INT_MAX)
669 return AVERROR(ERANGE);
670 12377 return bp.len + 1;
671 }
672
673 enum AVChannel
674 183267 av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout,
675 unsigned int idx)
676 {
677 int i;
678
679
2/2
✓ Branch 0 taken 21985 times.
✓ Branch 1 taken 161282 times.
183267 if (idx >= channel_layout->nb_channels)
680 21985 return AV_CHAN_NONE;
681
682
4/4
✓ Branch 0 taken 118157 times.
✓ Branch 1 taken 344 times.
✓ Branch 2 taken 42715 times.
✓ Branch 3 taken 66 times.
161282 switch (channel_layout->order) {
683 118157 case AV_CHANNEL_ORDER_CUSTOM:
684 118157 return channel_layout->u.map[idx].id;
685 344 case AV_CHANNEL_ORDER_AMBISONIC: {
686 344 int ambi_channels = channel_layout->nb_channels - av_popcount64(channel_layout->u.mask);
687
2/2
✓ Branch 0 taken 330 times.
✓ Branch 1 taken 14 times.
344 if (idx < ambi_channels)
688 330 return AV_CHAN_AMBISONIC_BASE + idx;
689 14 idx -= ambi_channels;
690 }
691 av_fallthrough;
692 42729 case AV_CHANNEL_ORDER_NATIVE:
693
1/2
✓ Branch 0 taken 442207 times.
✗ Branch 1 not taken.
442207 for (i = 0; i < 64; i++) {
694
4/4
✓ Branch 0 taken 322990 times.
✓ Branch 1 taken 119217 times.
✓ Branch 2 taken 42729 times.
✓ Branch 3 taken 280261 times.
442207 if ((1ULL << i) & channel_layout->u.mask && !idx--)
695 42729 return i;
696 }
697 av_fallthrough;
698 default:
699 66 return AV_CHAN_NONE;
700 }
701 }
702
703 enum AVChannel
704 19 av_channel_layout_channel_from_string(const AVChannelLayout *channel_layout,
705 const char *str)
706 {
707 19 int index = av_channel_layout_index_from_string(channel_layout, str);
708
709
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 12 times.
19 if (index < 0)
710 7 return AV_CHAN_NONE;
711
712 12 return av_channel_layout_channel_from_index(channel_layout, index);
713 }
714
715 47999 int av_channel_layout_index_from_channel(const AVChannelLayout *channel_layout,
716 enum AVChannel channel)
717 {
718 int i;
719
720
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47999 times.
47999 if (channel == AV_CHAN_NONE)
721 return AVERROR(EINVAL);
722
723
3/3
✓ Branch 0 taken 2647 times.
✓ Branch 1 taken 45336 times.
✓ Branch 2 taken 16 times.
47999 switch (channel_layout->order) {
724 2647 case AV_CHANNEL_ORDER_CUSTOM:
725
2/2
✓ Branch 0 taken 17087 times.
✓ Branch 1 taken 2365 times.
19452 for (i = 0; i < channel_layout->nb_channels; i++)
726
2/2
✓ Branch 0 taken 282 times.
✓ Branch 1 taken 16805 times.
17087 if (channel_layout->u.map[i].id == channel)
727 282 return i;
728 2365 return AVERROR(EINVAL);
729 45336 case AV_CHANNEL_ORDER_AMBISONIC:
730 case AV_CHANNEL_ORDER_NATIVE: {
731 45336 uint64_t mask = channel_layout->u.mask;
732 45336 int ambi_channels = channel_layout->nb_channels - av_popcount64(mask);
733
4/4
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 45134 times.
✓ Branch 2 taken 193 times.
✓ Branch 3 taken 9 times.
45336 if (channel_layout->order == AV_CHANNEL_ORDER_AMBISONIC &&
734 channel >= AV_CHAN_AMBISONIC_BASE) {
735
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 193 times.
193 if (channel - AV_CHAN_AMBISONIC_BASE >= ambi_channels)
736 return AVERROR(EINVAL);
737 193 return channel - AV_CHAN_AMBISONIC_BASE;
738 }
739
3/4
✓ Branch 0 taken 45143 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31700 times.
✓ Branch 3 taken 13443 times.
45143 if ((unsigned)channel > 63 || !(mask & (1ULL << channel)))
740 31700 return AVERROR(EINVAL);
741 13443 mask &= (1ULL << channel) - 1;
742 13443 return av_popcount64(mask) + ambi_channels;
743 }
744 16 default:
745 16 return AVERROR(EINVAL);
746 }
747 }
748
749 35 int av_channel_layout_index_from_string(const AVChannelLayout *channel_layout,
750 const char *str)
751 {
752 const char *chname;
753 35 enum AVChannel ch = AV_CHAN_NONE;
754
755
2/3
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
35 switch (channel_layout->order) {
756 18 case AV_CHANNEL_ORDER_CUSTOM:
757 18 chname = strstr(str, "@");
758
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
18 if (chname) {
759 char buf[16];
760 8 chname++;
761 8 av_strlcpy(buf, str, FFMIN(sizeof(buf), chname - str));
762
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!*chname)
763 chname = NULL;
764 8 ch = av_channel_from_string(buf);
765
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
8 if (ch == AV_CHAN_NONE && *buf)
766 return AVERROR(EINVAL);
767 }
768
4/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 2 times.
32 for (int i = 0; chname && i < channel_layout->nb_channels; i++) {
769
4/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 2 times.
20 if (!strcmp(chname, channel_layout->u.map[i].name) &&
770
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
10 (ch == AV_CHAN_NONE || ch == channel_layout->u.map[i].id))
771 6 return i;
772 }
773 av_fallthrough;
774 case AV_CHANNEL_ORDER_AMBISONIC:
775 case AV_CHANNEL_ORDER_NATIVE:
776 29 ch = av_channel_from_string(str);
777
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 22 times.
29 if (ch == AV_CHAN_NONE)
778 7 return AVERROR(EINVAL);
779 22 return av_channel_layout_index_from_channel(channel_layout, ch);
780 }
781
782 return AVERROR(EINVAL);
783 }
784
785 357921 int av_channel_layout_check(const AVChannelLayout *channel_layout)
786 {
787
2/2
✓ Branch 0 taken 6900 times.
✓ Branch 1 taken 351021 times.
357921 if (channel_layout->nb_channels <= 0)
788 6900 return 0;
789
790
4/5
✓ Branch 0 taken 310932 times.
✓ Branch 1 taken 1822 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 38255 times.
✗ Branch 4 not taken.
351021 switch (channel_layout->order) {
791 310932 case AV_CHANNEL_ORDER_NATIVE:
792 310932 return av_popcount64(channel_layout->u.mask) == channel_layout->nb_channels;
793 1822 case AV_CHANNEL_ORDER_CUSTOM:
794
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1822 times.
1822 if (!channel_layout->u.map)
795 return 0;
796
2/2
✓ Branch 0 taken 10782 times.
✓ Branch 1 taken 1822 times.
12604 for (int i = 0; i < channel_layout->nb_channels; i++) {
797
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10782 times.
10782 if (channel_layout->u.map[i].id == AV_CHAN_NONE)
798 return 0;
799 }
800 1822 return 1;
801 12 case AV_CHANNEL_ORDER_AMBISONIC:
802 /* If non-diegetic channels are present, ensure they are taken into account */
803 12 return av_popcount64(channel_layout->u.mask) < channel_layout->nb_channels;
804 38255 case AV_CHANNEL_ORDER_UNSPEC:
805 38255 return 1;
806 default:
807 return 0;
808 }
809 }
810
811 2041506 int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
812 {
813 int i;
814
815 /* different channel counts -> not equal */
816
2/2
✓ Branch 0 taken 12277 times.
✓ Branch 1 taken 2029229 times.
2041506 if (chl->nb_channels != chl1->nb_channels)
817 12277 return 1;
818
819 /* if only one is unspecified -> not equal */
820 2029229 if ((chl->order == AV_CHANNEL_ORDER_UNSPEC) !=
821
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 2029161 times.
2029229 (chl1->order == AV_CHANNEL_ORDER_UNSPEC))
822 68 return 1;
823 /* both are unspecified -> equal */
824
2/2
✓ Branch 0 taken 244742 times.
✓ Branch 1 taken 1784419 times.
2029161 else if (chl->order == AV_CHANNEL_ORDER_UNSPEC)
825 244742 return 0;
826
827 /* can compare masks directly */
828
2/2
✓ Branch 0 taken 7812 times.
✓ Branch 1 taken 1776607 times.
1784419 if ((chl->order == AV_CHANNEL_ORDER_NATIVE ||
829
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 7798 times.
7812 chl->order == AV_CHANNEL_ORDER_AMBISONIC) &&
830
1/2
✓ Branch 0 taken 1776621 times.
✗ Branch 1 not taken.
1776621 chl->order == chl1->order)
831 1776621 return chl->u.mask != chl1->u.mask;
832
833 /* compare channel by channel */
834
2/2
✓ Branch 0 taken 58365 times.
✓ Branch 1 taken 7795 times.
66160 for (i = 0; i < chl->nb_channels; i++)
835
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 58362 times.
116730 if (av_channel_layout_channel_from_index(chl, i) !=
836 58365 av_channel_layout_channel_from_index(chl1, i))
837 3 return 1;
838 7795 return 0;
839 }
840
841 33866 void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
842 {
843 int i;
844
2/2
✓ Branch 0 taken 57362 times.
✓ Branch 1 taken 15 times.
57377 for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
845
2/2
✓ Branch 0 taken 33851 times.
✓ Branch 1 taken 23511 times.
57362 if (nb_channels == channel_layout_map[i].layout.nb_channels) {
846 33851 *ch_layout = channel_layout_map[i].layout;
847 33851 return;
848 }
849
850 15 ch_layout->order = AV_CHANNEL_ORDER_UNSPEC;
851 15 ch_layout->nb_channels = nb_channels;
852 }
853
854 41 const AVChannelLayout *av_channel_layout_standard(void **opaque)
855 {
856 41 uintptr_t i = (uintptr_t)*opaque;
857 41 const AVChannelLayout *ch_layout = NULL;
858
859
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 1 times.
41 if (i < FF_ARRAY_ELEMS(channel_layout_map)) {
860 40 ch_layout = &channel_layout_map[i].layout;
861 40 *opaque = (void*)(i + 1);
862 }
863
864 41 return ch_layout;
865 }
866
867 2388 uint64_t av_channel_layout_subset(const AVChannelLayout *channel_layout,
868 uint64_t mask)
869 {
870 2388 uint64_t ret = 0;
871 int i;
872
873
3/3
✓ Branch 0 taken 1981 times.
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 368 times.
2388 switch (channel_layout->order) {
874 1981 case AV_CHANNEL_ORDER_NATIVE:
875 case AV_CHANNEL_ORDER_AMBISONIC:
876 1981 return channel_layout->u.mask & mask;
877 39 case AV_CHANNEL_ORDER_CUSTOM:
878
2/2
✓ Branch 0 taken 2496 times.
✓ Branch 1 taken 39 times.
2535 for (i = 0; i < 64; i++)
879
4/4
✓ Branch 0 taken 2312 times.
✓ Branch 1 taken 184 times.
✓ Branch 3 taken 250 times.
✓ Branch 4 taken 2062 times.
2496 if (mask & (1ULL << i) && av_channel_layout_index_from_channel(channel_layout, i) >= 0)
880 250 ret |= (1ULL << i);
881 39 break;
882 }
883
884 407 return ret;
885 }
886
887 429 int av_channel_layout_retype(AVChannelLayout *channel_layout, enum AVChannelOrder order, int flags)
888 {
889 429 int allow_lossy = !(flags & AV_CHANNEL_LAYOUT_RETYPE_FLAG_LOSSLESS);
890 int lossy;
891
892
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 429 times.
429 if (!av_channel_layout_check(channel_layout))
893 return AVERROR(EINVAL);
894
895
2/2
✓ Branch 0 taken 337 times.
✓ Branch 1 taken 92 times.
429 if (flags & AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL)
896 337 order = canonical_order(channel_layout);
897
898
2/2
✓ Branch 0 taken 209 times.
✓ Branch 1 taken 220 times.
429 if (channel_layout->order == order)
899 209 return 0;
900
901
4/5
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 204 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
220 switch (order) {
902 4 case AV_CHANNEL_ORDER_UNSPEC: {
903 4 int nb_channels = channel_layout->nb_channels;
904
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) {
905 2 lossy = 0;
906
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 for (int i = 0; i < nb_channels; i++) {
907
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (channel_layout->u.map[i].id != AV_CHAN_UNKNOWN || channel_layout->u.map[i].name[0]) {
908 2 lossy = 1;
909 2 break;
910 }
911 }
912 } else {
913 2 lossy = 1;
914 }
915
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 if (!lossy || allow_lossy) {
916 4 void *opaque = channel_layout->opaque;
917 4 av_channel_layout_uninit(channel_layout);
918 4 channel_layout->order = AV_CHANNEL_ORDER_UNSPEC;
919 4 channel_layout->nb_channels = nb_channels;
920 4 channel_layout->opaque = opaque;
921 4 return lossy;
922 }
923 return AVERROR(ENOSYS);
924 }
925 204 case AV_CHANNEL_ORDER_NATIVE:
926
2/2
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 2 times.
204 if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) {
927 202 int64_t mask = masked_description(channel_layout, 0);
928
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 158 times.
202 if (mask < 0)
929 44 return AVERROR(ENOSYS);
930 158 lossy = has_channel_names(channel_layout);
931
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 157 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
158 if (!lossy || allow_lossy) {
932 158 void *opaque = channel_layout->opaque;
933 158 av_channel_layout_uninit(channel_layout);
934 158 av_channel_layout_from_mask(channel_layout, mask);
935 158 channel_layout->opaque = opaque;
936 158 return lossy;
937 }
938 }
939 2 return AVERROR(ENOSYS);
940 3 case AV_CHANNEL_ORDER_CUSTOM: {
941 3 AVChannelLayout custom = { 0 };
942 3 int ret = av_channel_layout_custom_init(&custom, channel_layout->nb_channels);
943 3 void *opaque = channel_layout->opaque;
944
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
945 return ret;
946
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (channel_layout->order != AV_CHANNEL_ORDER_UNSPEC)
947
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 2 times.
15 for (int i = 0; i < channel_layout->nb_channels; i++)
948 13 custom.u.map[i].id = av_channel_layout_channel_from_index(channel_layout, i);
949 3 av_channel_layout_uninit(channel_layout);
950 3 *channel_layout = custom;
951 3 channel_layout->opaque = opaque;
952 3 return 0;
953 }
954 9 case AV_CHANNEL_ORDER_AMBISONIC:
955
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
9 if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) {
956 int64_t mask;
957 7 int nb_channels = channel_layout->nb_channels;
958 7 int amb_order = av_channel_layout_ambisonic_order(channel_layout);
959
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (amb_order < 0)
960 2 return AVERROR(ENOSYS);
961 5 mask = masked_description(channel_layout, (amb_order + 1) * (amb_order + 1));
962
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (mask < 0)
963 return AVERROR(ENOSYS);
964 5 lossy = has_channel_names(channel_layout);
965
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5 if (!lossy || allow_lossy) {
966 5 void *opaque = channel_layout->opaque;
967 5 av_channel_layout_uninit(channel_layout);
968 5 channel_layout->order = AV_CHANNEL_ORDER_AMBISONIC;
969 5 channel_layout->nb_channels = nb_channels;
970 5 channel_layout->u.mask = mask;
971 5 channel_layout->opaque = opaque;
972 5 return lossy;
973 }
974 }
975 2 return AVERROR(ENOSYS);
976 default:
977 return AVERROR(EINVAL);
978 }
979 }
980