Coverage Report

Created: 2026-01-25 07:18

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