Coverage Report

Created: 2026-04-29 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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_BACK  },
222
    { "7.1.2",          AV_CHANNEL_LAYOUT_7POINT1POINT2       },
223
    { "7.1.4",          AV_CHANNEL_LAYOUT_7POINT1POINT4_BACK  },
224
    { "7.2.3",          AV_CHANNEL_LAYOUT_7POINT2POINT3       },
225
    { "9.1.4",          AV_CHANNEL_LAYOUT_9POINT1POINT4_BACK  },
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
int av_channel_layout_custom_init(AVChannelLayout *channel_layout, int nb_channels)
234
0
{
235
0
    AVChannelCustom *map;
236
237
0
    if (nb_channels <= 0)
238
0
        return AVERROR(EINVAL);
239
240
0
    map = av_calloc(nb_channels, sizeof(*channel_layout->u.map));
241
0
    if (!map)
242
0
        return AVERROR(ENOMEM);
243
0
    for (int i = 0; i < nb_channels; i++)
244
0
        map[i].id = AV_CHAN_UNKNOWN;
245
246
0
    channel_layout->order       = AV_CHANNEL_ORDER_CUSTOM;
247
0
    channel_layout->nb_channels = nb_channels;
248
0
    channel_layout->u.map       = map;
249
250
0
    return 0;
251
0
}
252
253
int av_channel_layout_from_mask(AVChannelLayout *channel_layout,
254
                                uint64_t mask)
255
0
{
256
0
    if (!mask)
257
0
        return AVERROR(EINVAL);
258
259
0
    channel_layout->order       = AV_CHANNEL_ORDER_NATIVE;
260
0
    channel_layout->nb_channels = av_popcount64(mask);
261
0
    channel_layout->u.mask      = mask;
262
263
0
    return 0;
264
0
}
265
266
static int parse_channel_list(AVChannelLayout *ch_layout, const char *str)
267
0
{
268
0
    int ret;
269
0
    int nb_channels = 0;
270
0
    AVChannelCustom *map = NULL;
271
0
    AVChannelCustom custom = {0};
272
273
0
    while (*str) {
274
0
        char *channel, *chname;
275
0
        ret = av_opt_get_key_value(&str, "@", "+", AV_OPT_FLAG_IMPLICIT_KEY, &channel, &chname);
276
0
        if (ret < 0) {
277
0
            av_freep(&map);
278
0
            return ret;
279
0
        }
280
0
        if (*str)
281
0
            str++; // skip separator
282
0
        if (!channel) {
283
0
            channel = chname;
284
0
            chname = NULL;
285
0
        }
286
0
        av_strlcpy(custom.name, chname ? chname : "", sizeof(custom.name));
287
0
        custom.id = av_channel_from_string(channel);
288
0
        av_free(channel);
289
0
        av_free(chname);
290
0
        if (custom.id == AV_CHAN_NONE) {
291
0
            av_freep(&map);
292
0
            return AVERROR(EINVAL);
293
0
        }
294
295
0
        av_dynarray2_add((void **)&map, &nb_channels, sizeof(custom), (void *)&custom);
296
0
        if (!map)
297
0
            return AVERROR(ENOMEM);
298
0
    }
299
300
0
    if (!nb_channels)
301
0
        return AVERROR(EINVAL);
302
303
0
    ch_layout->order = AV_CHANNEL_ORDER_CUSTOM;
304
0
    ch_layout->u.map = map;
305
0
    ch_layout->nb_channels = nb_channels;
306
307
0
    ret = av_channel_layout_retype(ch_layout, 0, AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL);
308
0
    av_assert0(ret == 0);
309
310
0
    return 0;
311
0
}
312
313
int av_channel_layout_from_string(AVChannelLayout *channel_layout,
314
                                  const char *str)
315
0
{
316
0
    int i, matches, ret;
317
0
    int channels = 0, nb_channels = 0;
318
0
    char *chlist, *end;
319
0
    uint64_t mask = 0;
320
321
    /* channel layout names */
322
0
    for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++) {
323
0
        if (channel_layout_map[i].name && !strcmp(str, channel_layout_map[i].name)) {
324
0
            *channel_layout = channel_layout_map[i].layout;
325
0
            return 0;
326
0
        }
327
0
    }
328
329
    /* This function is a channel layout initializer, so we have to
330
     * zero-initialize before we start setting fields individually. */
331
0
    memset(channel_layout, 0, sizeof(*channel_layout));
332
333
    /* ambisonic */
334
0
    if (!strncmp(str, "ambisonic ", 10)) {
335
0
        const char *p = str + 10;
336
0
        char *endptr;
337
0
        AVChannelLayout extra = {0};
338
0
        int order;
339
340
0
        order = strtol(p, &endptr, 0);
341
0
        if (order < 0 || order + 1  > INT_MAX / (order + 1) ||
342
0
            (*endptr && *endptr != '+'))
343
0
            return AVERROR(EINVAL);
344
345
0
        channel_layout->order       = AV_CHANNEL_ORDER_AMBISONIC;
346
0
        channel_layout->nb_channels = (order + 1) * (order + 1);
347
348
0
        if (*endptr) {
349
0
            ret = av_channel_layout_from_string(&extra, endptr + 1);
350
0
            if (ret < 0)
351
0
                return ret;
352
0
            if (extra.nb_channels >= INT_MAX - channel_layout->nb_channels) {
353
0
                av_channel_layout_uninit(&extra);
354
0
                return AVERROR(EINVAL);
355
0
            }
356
357
0
            if (extra.order == AV_CHANNEL_ORDER_NATIVE) {
358
0
                channel_layout->u.mask = extra.u.mask;
359
0
            } else {
360
0
                channel_layout->order = AV_CHANNEL_ORDER_CUSTOM;
361
0
                channel_layout->u.map =
362
0
                    av_calloc(channel_layout->nb_channels + extra.nb_channels,
363
0
                              sizeof(*channel_layout->u.map));
364
0
                if (!channel_layout->u.map) {
365
0
                    av_channel_layout_uninit(&extra);
366
0
                    return AVERROR(ENOMEM);
367
0
                }
368
369
0
                for (i = 0; i < channel_layout->nb_channels; i++)
370
0
                    channel_layout->u.map[i].id = AV_CHAN_AMBISONIC_BASE + i;
371
0
                for (i = 0; i < extra.nb_channels; i++) {
372
0
                    enum AVChannel ch = av_channel_layout_channel_from_index(&extra, i);
373
0
                    if (CHAN_IS_AMBI(ch)) {
374
0
                        av_channel_layout_uninit(channel_layout);
375
0
                        av_channel_layout_uninit(&extra);
376
0
                        return AVERROR(EINVAL);
377
0
                    }
378
0
                    channel_layout->u.map[channel_layout->nb_channels + i].id = ch;
379
0
                    if (extra.order == AV_CHANNEL_ORDER_CUSTOM &&
380
0
                        extra.u.map[i].name[0])
381
0
                        av_strlcpy(channel_layout->u.map[channel_layout->nb_channels + i].name,
382
0
                                   extra.u.map[i].name,
383
0
                                   sizeof(channel_layout->u.map[channel_layout->nb_channels + i].name));
384
0
                }
385
0
            }
386
0
            channel_layout->nb_channels += extra.nb_channels;
387
0
            av_channel_layout_uninit(&extra);
388
0
        }
389
390
0
        return 0;
391
0
    }
392
393
0
    chlist = av_strdup(str);
394
0
    if (!chlist)
395
0
        return AVERROR(ENOMEM);
396
397
    /* channel names */
398
0
    matches = av_sscanf(str, "%d channels (%[^)]", &nb_channels, chlist);
399
0
    ret = parse_channel_list(channel_layout, chlist);
400
0
    av_freep(&chlist);
401
0
    if (ret < 0 && ret != AVERROR(EINVAL))
402
0
        return ret;
403
404
0
    if (ret >= 0) {
405
0
        end = strchr(str, ')');
406
0
        if (matches == 2 && (nb_channels != channel_layout->nb_channels || !end || *++end)) {
407
0
            av_channel_layout_uninit(channel_layout);
408
0
            return AVERROR(EINVAL);
409
0
        }
410
0
        return 0;
411
0
    }
412
413
0
    errno = 0;
414
0
    mask = strtoull(str, &end, 0);
415
416
    /* channel layout mask */
417
0
    if (!errno && !*end && !strchr(str, '-') && mask) {
418
0
        av_channel_layout_from_mask(channel_layout, mask);
419
0
        return 0;
420
0
    }
421
422
0
    errno = 0;
423
0
    channels = strtol(str, &end, 10);
424
425
    /* number of channels */
426
0
    if (!errno && !strcmp(end, "c") && channels > 0) {
427
0
        av_channel_layout_default(channel_layout, channels);
428
0
        if (channel_layout->order == AV_CHANNEL_ORDER_NATIVE)
429
0
            return 0;
430
0
    }
431
432
    /* number of unordered channels */
433
0
    if (!errno && (!strcmp(end, "C") || !strcmp(end, " channels"))
434
0
        && channels > 0) {
435
0
        channel_layout->order = AV_CHANNEL_ORDER_UNSPEC;
436
0
        channel_layout->nb_channels = channels;
437
0
        return 0;
438
0
    }
439
440
0
    return AVERROR(EINVAL);
441
0
}
442
443
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
444
0
{
445
0
    if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM)
446
0
        av_freep(&channel_layout->u.map);
447
0
    memset(channel_layout, 0, sizeof(*channel_layout));
448
0
}
449
450
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
451
0
{
452
0
    av_channel_layout_uninit(dst);
453
0
    *dst = *src;
454
0
    if (src->order == AV_CHANNEL_ORDER_CUSTOM) {
455
0
        dst->u.map = av_malloc_array(src->nb_channels, sizeof(*dst->u.map));
456
0
        if (!dst->u.map)
457
0
            return AVERROR(ENOMEM);
458
0
        memcpy(dst->u.map, src->u.map, src->nb_channels * sizeof(*src->u.map));
459
0
    }
460
0
    return 0;
461
0
}
462
463
static int64_t masked_description(const AVChannelLayout *channel_layout, int start_channel)
464
0
{
465
0
    uint64_t mask = 0;
466
0
    for (int i = start_channel; i < channel_layout->nb_channels; i++) {
467
0
        enum AVChannel ch = channel_layout->u.map[i].id;
468
0
        if (ch >= 0 && ch < 63 && mask < (1ULL << ch))
469
0
            mask |= (1ULL << ch);
470
0
        else
471
0
            return AVERROR(EINVAL);
472
0
    }
473
0
    return mask;
474
0
}
475
476
static int has_channel_names(const AVChannelLayout *channel_layout)
477
0
{
478
0
    if (channel_layout->order != AV_CHANNEL_ORDER_CUSTOM)
479
0
        return 0;
480
0
    for (int i = 0; i < channel_layout->nb_channels; i++)
481
0
        if (channel_layout->u.map[i].name[0])
482
0
            return 1;
483
0
    return 0;
484
0
}
485
486
int av_channel_layout_ambisonic_order(const AVChannelLayout *channel_layout)
487
0
{
488
0
    int i, highest_ambi, order;
489
490
0
    if (channel_layout->order != AV_CHANNEL_ORDER_AMBISONIC &&
491
0
        channel_layout->order != AV_CHANNEL_ORDER_CUSTOM)
492
0
        return AVERROR(EINVAL);
493
494
0
    highest_ambi = -1;
495
0
    if (channel_layout->order == AV_CHANNEL_ORDER_AMBISONIC)
496
0
        highest_ambi = channel_layout->nb_channels - av_popcount64(channel_layout->u.mask) - 1;
497
0
    else {
498
0
        const AVChannelCustom *map = channel_layout->u.map;
499
0
        av_assert0(channel_layout->order == AV_CHANNEL_ORDER_CUSTOM);
500
501
0
        for (i = 0; i < channel_layout->nb_channels; i++) {
502
0
            int is_ambi = CHAN_IS_AMBI(map[i].id);
503
504
            /* ambisonic following non-ambisonic */
505
0
            if (i > 0 && is_ambi && !CHAN_IS_AMBI(map[i - 1].id))
506
0
                return AVERROR(EINVAL);
507
508
            /* non-default ordering */
509
0
            if (is_ambi && map[i].id - AV_CHAN_AMBISONIC_BASE != i)
510
0
                return AVERROR(EINVAL);
511
512
0
            if (CHAN_IS_AMBI(map[i].id))
513
0
                highest_ambi = i;
514
0
        }
515
0
    }
516
    /* no ambisonic channels*/
517
0
    if (highest_ambi < 0)
518
0
        return AVERROR(EINVAL);
519
520
0
    order = floor(sqrt(highest_ambi));
521
    /* incomplete order - some harmonics are missing */
522
0
    if ((order + 1) * (order + 1) != highest_ambi + 1)
523
0
        return AVERROR(EINVAL);
524
525
0
    return order;
526
0
}
527
528
static enum AVChannelOrder canonical_order(AVChannelLayout *channel_layout)
529
0
{
530
0
    int has_known_channel = 0;
531
0
    int order;
532
533
0
    if (channel_layout->order != AV_CHANNEL_ORDER_CUSTOM)
534
0
        return channel_layout->order;
535
536
0
    if (has_channel_names(channel_layout))
537
0
        return AV_CHANNEL_ORDER_CUSTOM;
538
539
0
    for (int i = 0; i < channel_layout->nb_channels && !has_known_channel; i++)
540
0
        if (channel_layout->u.map[i].id != AV_CHAN_UNKNOWN)
541
0
            has_known_channel = 1;
542
0
    if (!has_known_channel)
543
0
        return AV_CHANNEL_ORDER_UNSPEC;
544
545
0
    if (masked_description(channel_layout, 0) > 0)
546
0
        return AV_CHANNEL_ORDER_NATIVE;
547
548
0
    order = av_channel_layout_ambisonic_order(channel_layout);
549
0
    if (order >= 0 && masked_description(channel_layout, (order + 1) * (order + 1)) >= 0)
550
0
        return AV_CHANNEL_ORDER_AMBISONIC;
551
552
0
    return AV_CHANNEL_ORDER_CUSTOM;
553
0
}
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
static int try_describe_ambisonic(AVBPrint *bp, const AVChannelLayout *channel_layout)
561
0
{
562
0
    int nb_ambi_channels;
563
0
    int order = av_channel_layout_ambisonic_order(channel_layout);
564
0
    if (order < 0)
565
0
        return order;
566
567
0
    av_bprintf(bp, "ambisonic %d", order);
568
569
    /* extra channels present */
570
0
    nb_ambi_channels = (order + 1) * (order + 1);
571
0
    if (nb_ambi_channels < channel_layout->nb_channels) {
572
0
        AVChannelLayout extra = { 0 };
573
574
0
        if (channel_layout->order == AV_CHANNEL_ORDER_AMBISONIC) {
575
0
            extra.order       = AV_CHANNEL_ORDER_NATIVE;
576
0
            extra.nb_channels = av_popcount64(channel_layout->u.mask);
577
0
            extra.u.mask      = channel_layout->u.mask;
578
0
        } else {
579
0
            int64_t mask;
580
0
            if (!has_channel_names(channel_layout) &&
581
0
                (mask = masked_description(channel_layout, nb_ambi_channels)) > 0) {
582
0
                extra.order       = AV_CHANNEL_ORDER_NATIVE;
583
0
                extra.nb_channels = av_popcount64(mask);
584
0
                extra.u.mask      = mask;
585
0
            } else {
586
0
                extra.order       = AV_CHANNEL_ORDER_CUSTOM;
587
0
                extra.nb_channels = channel_layout->nb_channels - nb_ambi_channels;
588
0
                extra.u.map       = channel_layout->u.map + nb_ambi_channels;
589
0
            }
590
0
        }
591
592
0
        av_bprint_chars(bp, '+', 1);
593
0
        av_channel_layout_describe_bprint(&extra, bp);
594
        /* Not calling uninit here on extra because we don't own the u.map pointer */
595
0
    }
596
597
0
    return 0;
598
0
}
599
600
int av_channel_layout_describe_bprint(const AVChannelLayout *channel_layout,
601
                                      AVBPrint *bp)
602
0
{
603
0
    int i;
604
605
0
    switch (channel_layout->order) {
606
0
    case AV_CHANNEL_ORDER_NATIVE:
607
0
        for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
608
0
            if (channel_layout->u.mask == channel_layout_map[i].layout.u.mask) {
609
0
                av_bprintf(bp, "%s", channel_layout_map[i].name);
610
0
                return 0;
611
0
            }
612
0
        av_fallthrough;
613
0
    case AV_CHANNEL_ORDER_CUSTOM:
614
0
        if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) {
615
0
            int64_t mask;
616
0
            int res = try_describe_ambisonic(bp, channel_layout);
617
0
            if (res >= 0)
618
0
                return 0;
619
0
            if (!has_channel_names(channel_layout) &&
620
0
                (mask = masked_description(channel_layout, 0)) > 0) {
621
0
                AVChannelLayout native = { .order       = AV_CHANNEL_ORDER_NATIVE,
622
0
                                           .nb_channels = av_popcount64(mask),
623
0
                                           .u.mask      = mask };
624
0
                return av_channel_layout_describe_bprint(&native, bp);
625
0
            }
626
0
        }
627
0
        if (channel_layout->nb_channels)
628
0
            av_bprintf(bp, "%d channels (", channel_layout->nb_channels);
629
0
        for (i = 0; i < channel_layout->nb_channels; i++) {
630
0
            enum AVChannel ch = av_channel_layout_channel_from_index(channel_layout, i);
631
632
0
            if (i)
633
0
                av_bprintf(bp, "+");
634
0
            av_channel_name_bprint(bp, ch);
635
0
            if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM &&
636
0
                channel_layout->u.map[i].name[0])
637
0
                av_bprintf(bp, "@%s", channel_layout->u.map[i].name);
638
0
        }
639
0
        if (channel_layout->nb_channels) {
640
0
            av_bprintf(bp, ")");
641
0
            return 0;
642
0
        }
643
0
        av_fallthrough;
644
0
    case AV_CHANNEL_ORDER_UNSPEC:
645
0
        av_bprintf(bp, "%d channels", channel_layout->nb_channels);
646
0
        return 0;
647
0
    case AV_CHANNEL_ORDER_AMBISONIC:
648
0
        return try_describe_ambisonic(bp, channel_layout);
649
0
    default:
650
0
        return AVERROR(EINVAL);
651
0
    }
652
0
}
653
654
int av_channel_layout_describe(const AVChannelLayout *channel_layout,
655
                               char *buf, size_t buf_size)
656
0
{
657
0
    AVBPrint bp;
658
0
    int ret;
659
660
0
    if (!buf && buf_size)
661
0
        return AVERROR(EINVAL);
662
663
0
    av_bprint_init_for_buffer(&bp, buf, buf_size);
664
0
    ret = av_channel_layout_describe_bprint(channel_layout, &bp);
665
0
    if (ret < 0)
666
0
        return ret;
667
668
0
    if (bp.len >= INT_MAX)
669
0
        return AVERROR(ERANGE);
670
0
    return bp.len + 1;
671
0
}
672
673
enum AVChannel
674
av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout,
675
                                     unsigned int idx)
676
0
{
677
0
    int i;
678
679
0
    if (idx >= channel_layout->nb_channels)
680
0
        return AV_CHAN_NONE;
681
682
0
    switch (channel_layout->order) {
683
0
    case AV_CHANNEL_ORDER_CUSTOM:
684
0
        return channel_layout->u.map[idx].id;
685
0
    case AV_CHANNEL_ORDER_AMBISONIC: {
686
0
        int ambi_channels = channel_layout->nb_channels - av_popcount64(channel_layout->u.mask);
687
0
        if (idx < ambi_channels)
688
0
            return AV_CHAN_AMBISONIC_BASE + idx;
689
0
        idx -= ambi_channels;
690
0
        }
691
0
        av_fallthrough;
692
0
    case AV_CHANNEL_ORDER_NATIVE:
693
0
        for (i = 0; i < 64; i++) {
694
0
            if ((1ULL << i) & channel_layout->u.mask && !idx--)
695
0
                return i;
696
0
        }
697
0
        av_fallthrough;
698
0
    default:
699
0
        return AV_CHAN_NONE;
700
0
    }
701
0
}
702
703
enum AVChannel
704
av_channel_layout_channel_from_string(const AVChannelLayout *channel_layout,
705
                                      const char *str)
706
0
{
707
0
    int index = av_channel_layout_index_from_string(channel_layout, str);
708
709
0
    if (index < 0)
710
0
        return AV_CHAN_NONE;
711
712
0
    return av_channel_layout_channel_from_index(channel_layout, index);
713
0
}
714
715
int av_channel_layout_index_from_channel(const AVChannelLayout *channel_layout,
716
                                         enum AVChannel channel)
717
0
{
718
0
    int i;
719
720
0
    if (channel == AV_CHAN_NONE)
721
0
        return AVERROR(EINVAL);
722
723
0
    switch (channel_layout->order) {
724
0
    case AV_CHANNEL_ORDER_CUSTOM:
725
0
        for (i = 0; i < channel_layout->nb_channels; i++)
726
0
            if (channel_layout->u.map[i].id == channel)
727
0
                return i;
728
0
        return AVERROR(EINVAL);
729
0
    case AV_CHANNEL_ORDER_AMBISONIC:
730
0
    case AV_CHANNEL_ORDER_NATIVE: {
731
0
        uint64_t mask = channel_layout->u.mask;
732
0
        int ambi_channels = channel_layout->nb_channels - av_popcount64(mask);
733
0
        if (channel_layout->order == AV_CHANNEL_ORDER_AMBISONIC &&
734
0
            channel >= AV_CHAN_AMBISONIC_BASE) {
735
0
            if (channel - AV_CHAN_AMBISONIC_BASE >= ambi_channels)
736
0
                return AVERROR(EINVAL);
737
0
            return channel - AV_CHAN_AMBISONIC_BASE;
738
0
        }
739
0
        if ((unsigned)channel > 63 || !(mask & (1ULL << channel)))
740
0
            return AVERROR(EINVAL);
741
0
        mask &= (1ULL << channel) - 1;
742
0
        return av_popcount64(mask) + ambi_channels;
743
0
        }
744
0
    default:
745
0
        return AVERROR(EINVAL);
746
0
    }
747
0
}
748
749
int av_channel_layout_index_from_string(const AVChannelLayout *channel_layout,
750
                                        const char *str)
751
0
{
752
0
    char *chname;
753
0
    enum AVChannel ch = AV_CHAN_NONE;
754
755
0
    switch (channel_layout->order) {
756
0
    case AV_CHANNEL_ORDER_CUSTOM:
757
0
        chname = strstr(str, "@");
758
0
        if (chname) {
759
0
            char buf[16];
760
0
            chname++;
761
0
            av_strlcpy(buf, str, FFMIN(sizeof(buf), chname - str));
762
0
            if (!*chname)
763
0
                chname = NULL;
764
0
            ch = av_channel_from_string(buf);
765
0
            if (ch == AV_CHAN_NONE && *buf)
766
0
                return AVERROR(EINVAL);
767
0
        }
768
0
        for (int i = 0; chname && i < channel_layout->nb_channels; i++) {
769
0
            if (!strcmp(chname, channel_layout->u.map[i].name) &&
770
0
                (ch == AV_CHAN_NONE || ch == channel_layout->u.map[i].id))
771
0
                return i;
772
0
        }
773
0
        av_fallthrough;
774
0
    case AV_CHANNEL_ORDER_AMBISONIC:
775
0
    case AV_CHANNEL_ORDER_NATIVE:
776
0
        ch = av_channel_from_string(str);
777
0
        if (ch == AV_CHAN_NONE)
778
0
            return AVERROR(EINVAL);
779
0
        return av_channel_layout_index_from_channel(channel_layout, ch);
780
0
    }
781
782
0
    return AVERROR(EINVAL);
783
0
}
784
785
int av_channel_layout_check(const AVChannelLayout *channel_layout)
786
0
{
787
0
    if (channel_layout->nb_channels <= 0)
788
0
        return 0;
789
790
0
    switch (channel_layout->order) {
791
0
    case AV_CHANNEL_ORDER_NATIVE:
792
0
        return av_popcount64(channel_layout->u.mask) == channel_layout->nb_channels;
793
0
    case AV_CHANNEL_ORDER_CUSTOM:
794
0
        if (!channel_layout->u.map)
795
0
            return 0;
796
0
        for (int i = 0; i < channel_layout->nb_channels; i++) {
797
0
            if (channel_layout->u.map[i].id == AV_CHAN_NONE)
798
0
                return 0;
799
0
        }
800
0
        return 1;
801
0
    case AV_CHANNEL_ORDER_AMBISONIC:
802
        /* If non-diegetic channels are present, ensure they are taken into account */
803
0
        return av_popcount64(channel_layout->u.mask) < channel_layout->nb_channels;
804
0
    case AV_CHANNEL_ORDER_UNSPEC:
805
0
        return 1;
806
0
    default:
807
0
        return 0;
808
0
    }
809
0
}
810
811
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
812
0
{
813
0
    int i;
814
815
    /* different channel counts -> not equal */
816
0
    if (chl->nb_channels != chl1->nb_channels)
817
0
        return 1;
818
819
    /* if only one is unspecified -> not equal */
820
0
    if ((chl->order  == AV_CHANNEL_ORDER_UNSPEC) !=
821
0
        (chl1->order == AV_CHANNEL_ORDER_UNSPEC))
822
0
        return 1;
823
    /* both are unspecified -> equal */
824
0
    else if (chl->order == AV_CHANNEL_ORDER_UNSPEC)
825
0
        return 0;
826
827
    /* can compare masks directly */
828
0
    if ((chl->order == AV_CHANNEL_ORDER_NATIVE ||
829
0
         chl->order == AV_CHANNEL_ORDER_AMBISONIC) &&
830
0
        chl->order == chl1->order)
831
0
        return chl->u.mask != chl1->u.mask;
832
833
    /* compare channel by channel */
834
0
    for (i = 0; i < chl->nb_channels; i++)
835
0
        if (av_channel_layout_channel_from_index(chl,  i) !=
836
0
            av_channel_layout_channel_from_index(chl1, i))
837
0
            return 1;
838
0
    return 0;
839
0
}
840
841
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
842
0
{
843
0
    int i;
844
0
    for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
845
0
        if (nb_channels == channel_layout_map[i].layout.nb_channels) {
846
0
            *ch_layout = channel_layout_map[i].layout;
847
0
            return;
848
0
        }
849
850
0
    ch_layout->order       = AV_CHANNEL_ORDER_UNSPEC;
851
0
    ch_layout->nb_channels = nb_channels;
852
0
}
853
854
const AVChannelLayout *av_channel_layout_standard(void **opaque)
855
0
{
856
0
    uintptr_t i = (uintptr_t)*opaque;
857
0
    const AVChannelLayout *ch_layout = NULL;
858
859
0
    if (i < FF_ARRAY_ELEMS(channel_layout_map)) {
860
0
        ch_layout = &channel_layout_map[i].layout;
861
0
        *opaque = (void*)(i + 1);
862
0
    }
863
864
0
    return ch_layout;
865
0
}
866
867
uint64_t av_channel_layout_subset(const AVChannelLayout *channel_layout,
868
                                  uint64_t mask)
869
0
{
870
0
    uint64_t ret = 0;
871
0
    int i;
872
873
0
    switch (channel_layout->order) {
874
0
    case AV_CHANNEL_ORDER_NATIVE:
875
0
    case AV_CHANNEL_ORDER_AMBISONIC:
876
0
        return channel_layout->u.mask & mask;
877
0
    case AV_CHANNEL_ORDER_CUSTOM:
878
0
        for (i = 0; i < 64; i++)
879
0
            if (mask & (1ULL << i) && av_channel_layout_index_from_channel(channel_layout, i) >= 0)
880
0
                ret |= (1ULL << i);
881
0
        break;
882
0
    }
883
884
0
    return ret;
885
0
}
886
887
int av_channel_layout_retype(AVChannelLayout *channel_layout, enum AVChannelOrder order, int flags)
888
0
{
889
0
    int allow_lossy = !(flags & AV_CHANNEL_LAYOUT_RETYPE_FLAG_LOSSLESS);
890
0
    int lossy;
891
892
0
    if (!av_channel_layout_check(channel_layout))
893
0
        return AVERROR(EINVAL);
894
895
0
    if (flags & AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL)
896
0
        order = canonical_order(channel_layout);
897
898
0
    if (channel_layout->order == order)
899
0
        return 0;
900
901
0
    switch (order) {
902
0
    case AV_CHANNEL_ORDER_UNSPEC: {
903
0
        int nb_channels = channel_layout->nb_channels;
904
0
        if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) {
905
0
            lossy = 0;
906
0
            for (int i = 0; i < nb_channels; i++) {
907
0
                if (channel_layout->u.map[i].id != AV_CHAN_UNKNOWN || channel_layout->u.map[i].name[0]) {
908
0
                    lossy = 1;
909
0
                    break;
910
0
                }
911
0
            }
912
0
        } else {
913
0
            lossy = 1;
914
0
        }
915
0
        if (!lossy || allow_lossy) {
916
0
            void *opaque = channel_layout->opaque;
917
0
            av_channel_layout_uninit(channel_layout);
918
0
            channel_layout->order       = AV_CHANNEL_ORDER_UNSPEC;
919
0
            channel_layout->nb_channels = nb_channels;
920
0
            channel_layout->opaque      = opaque;
921
0
            return lossy;
922
0
        }
923
0
        return AVERROR(ENOSYS);
924
0
        }
925
0
    case AV_CHANNEL_ORDER_NATIVE:
926
0
        if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) {
927
0
            int64_t mask = masked_description(channel_layout, 0);
928
0
            if (mask < 0)
929
0
                return AVERROR(ENOSYS);
930
0
            lossy = has_channel_names(channel_layout);
931
0
            if (!lossy || allow_lossy) {
932
0
                void *opaque = channel_layout->opaque;
933
0
                av_channel_layout_uninit(channel_layout);
934
0
                av_channel_layout_from_mask(channel_layout, mask);
935
0
                channel_layout->opaque = opaque;
936
0
                return lossy;
937
0
            }
938
0
        }
939
0
        return AVERROR(ENOSYS);
940
0
    case AV_CHANNEL_ORDER_CUSTOM: {
941
0
        AVChannelLayout custom = { 0 };
942
0
        int ret = av_channel_layout_custom_init(&custom, channel_layout->nb_channels);
943
0
        void *opaque = channel_layout->opaque;
944
0
        if (ret < 0)
945
0
            return ret;
946
0
        if (channel_layout->order != AV_CHANNEL_ORDER_UNSPEC)
947
0
            for (int i = 0; i < channel_layout->nb_channels; i++)
948
0
                custom.u.map[i].id = av_channel_layout_channel_from_index(channel_layout, i);
949
0
        av_channel_layout_uninit(channel_layout);
950
0
        *channel_layout = custom;
951
0
        channel_layout->opaque = opaque;
952
0
        return 0;
953
0
        }
954
0
    case AV_CHANNEL_ORDER_AMBISONIC:
955
0
        if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) {
956
0
            int64_t mask;
957
0
            int nb_channels = channel_layout->nb_channels;
958
0
            int amb_order = av_channel_layout_ambisonic_order(channel_layout);
959
0
            if (amb_order < 0)
960
0
                return AVERROR(ENOSYS);
961
0
            mask = masked_description(channel_layout, (amb_order + 1) * (amb_order + 1));
962
0
            if (mask < 0)
963
0
                return AVERROR(ENOSYS);
964
0
            lossy = has_channel_names(channel_layout);
965
0
            if (!lossy || allow_lossy) {
966
0
                void *opaque = channel_layout->opaque;
967
0
                av_channel_layout_uninit(channel_layout);
968
0
                channel_layout->order       = AV_CHANNEL_ORDER_AMBISONIC;
969
0
                channel_layout->nb_channels = nb_channels;
970
0
                channel_layout->u.mask      = mask;
971
0
                channel_layout->opaque      = opaque;
972
0
                return lossy;
973
0
            }
974
0
        }
975
0
        return AVERROR(ENOSYS);
976
0
    default:
977
0
        return AVERROR(EINVAL);
978
0
    }
979
0
}