1/*
2 * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2008 Peter Ross
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef AVUTIL_CHANNEL_LAYOUT_H
23#define AVUTIL_CHANNEL_LAYOUT_H
24
25#include <stdint.h>
26#include <stdlib.h>
27
28#include "version.h"
29#include "attributes.h"
30
31/**
32 * @file
33 * @ingroup lavu_audio_channels
34 * Public libavutil channel layout APIs header.
35 */
36
37
38/**
39 * @defgroup lavu_audio_channels Audio channels
40 * @ingroup lavu_audio
41 *
42 * Audio channel layout utility functions
43 *
44 * @{
45 */
46
47enum AVChannel {
48 ///< Invalid channel index
49 AV_CHAN_NONE = -1,
50 AV_CHAN_FRONT_LEFT,
51 AV_CHAN_FRONT_RIGHT,
52 AV_CHAN_FRONT_CENTER,
53 AV_CHAN_LOW_FREQUENCY,
54 AV_CHAN_BACK_LEFT,
55 AV_CHAN_BACK_RIGHT,
56 AV_CHAN_FRONT_LEFT_OF_CENTER,
57 AV_CHAN_FRONT_RIGHT_OF_CENTER,
58 AV_CHAN_BACK_CENTER,
59 AV_CHAN_SIDE_LEFT,
60 AV_CHAN_SIDE_RIGHT,
61 AV_CHAN_TOP_CENTER,
62 AV_CHAN_TOP_FRONT_LEFT,
63 AV_CHAN_TOP_FRONT_CENTER,
64 AV_CHAN_TOP_FRONT_RIGHT,
65 AV_CHAN_TOP_BACK_LEFT,
66 AV_CHAN_TOP_BACK_CENTER,
67 AV_CHAN_TOP_BACK_RIGHT,
68 /** Stereo downmix. */
69 AV_CHAN_STEREO_LEFT = 29,
70 /** See above. */
71 AV_CHAN_STEREO_RIGHT,
72 AV_CHAN_WIDE_LEFT,
73 AV_CHAN_WIDE_RIGHT,
74 AV_CHAN_SURROUND_DIRECT_LEFT,
75 AV_CHAN_SURROUND_DIRECT_RIGHT,
76 AV_CHAN_LOW_FREQUENCY_2,
77 AV_CHAN_TOP_SIDE_LEFT,
78 AV_CHAN_TOP_SIDE_RIGHT,
79 AV_CHAN_BOTTOM_FRONT_CENTER,
80 AV_CHAN_BOTTOM_FRONT_LEFT,
81 AV_CHAN_BOTTOM_FRONT_RIGHT,
82
83 /** Channel is empty can be safely skipped. */
84 AV_CHAN_UNUSED = 0x200,
85
86 /** Channel contains data, but its position is unknown. */
87 AV_CHAN_UNKNOWN = 0x300,
88
89 /**
90 * Range of channels between AV_CHAN_AMBISONIC_BASE and
91 * AV_CHAN_AMBISONIC_END represent Ambisonic components using the ACN system.
92 *
93 * Given a channel id `<i>` between AV_CHAN_AMBISONIC_BASE and
94 * AV_CHAN_AMBISONIC_END (inclusive), the ACN index of the channel `<n>` is
95 * `<n> = <i> - AV_CHAN_AMBISONIC_BASE`.
96 *
97 * @note these values are only used for AV_CHANNEL_ORDER_CUSTOM channel
98 * orderings, the AV_CHANNEL_ORDER_AMBISONIC ordering orders the channels
99 * implicitly by their position in the stream.
100 */
101 AV_CHAN_AMBISONIC_BASE = 0x400,
102 // leave space for 1024 ids, which correspond to maximum order-32 harmonics,
103 // which should be enough for the foreseeable use cases
104 AV_CHAN_AMBISONIC_END = 0x7ff,
105};
106
107enum AVChannelOrder {
108 /**
109 * Only the channel count is specified, without any further information
110 * about the channel order.
111 */
112 AV_CHANNEL_ORDER_UNSPEC,
113 /**
114 * The native channel order, i.e. the channels are in the same order in
115 * which they are defined in the AVChannel enum. This supports up to 63
116 * different channels.
117 */
118 AV_CHANNEL_ORDER_NATIVE,
119 /**
120 * The channel order does not correspond to any other predefined order and
121 * is stored as an explicit map. For example, this could be used to support
122 * layouts with 64 or more channels, or with empty/skipped (AV_CHAN_UNUSED)
123 * channels at arbitrary positions.
124 */
125 AV_CHANNEL_ORDER_CUSTOM,
126 /**
127 * The audio is represented as the decomposition of the sound field into
128 * spherical harmonics. Each channel corresponds to a single expansion
129 * component. Channels are ordered according to ACN (Ambisonic Channel
130 * Number).
131 *
132 * The channel with the index n in the stream contains the spherical
133 * harmonic of degree l and order m given by
134 * @code{.unparsed}
135 * l = floor(sqrt(n)),
136 * m = n - l * (l + 1).
137 * @endcode
138 *
139 * Conversely given a spherical harmonic of degree l and order m, the
140 * corresponding channel index n is given by
141 * @code{.unparsed}
142 * n = l * (l + 1) + m.
143 * @endcode
144 *
145 * Normalization is assumed to be SN3D (Schmidt Semi-Normalization)
146 * as defined in AmbiX format $ 2.1.
147 */
148 AV_CHANNEL_ORDER_AMBISONIC,
149 /**
150 * Number of channel orders, not part of ABI/API
151 */
152 FF_CHANNEL_ORDER_NB
153};
154
155
156/**
157 * @defgroup channel_masks Audio channel masks
158 *
159 * A channel layout is a 64-bits integer with a bit set for every channel.
160 * The number of bits set must be equal to the number of channels.
161 * The value 0 means that the channel layout is not known.
162 * @note this data structure is not powerful enough to handle channels
163 * combinations that have the same channel multiple times, such as
164 * dual-mono.
165 *
166 * @{
167 */
168#define AV_CH_FRONT_LEFT (1ULL << AV_CHAN_FRONT_LEFT )
169#define AV_CH_FRONT_RIGHT (1ULL << AV_CHAN_FRONT_RIGHT )
170#define AV_CH_FRONT_CENTER (1ULL << AV_CHAN_FRONT_CENTER )
171#define AV_CH_LOW_FREQUENCY (1ULL << AV_CHAN_LOW_FREQUENCY )
172#define AV_CH_BACK_LEFT (1ULL << AV_CHAN_BACK_LEFT )
173#define AV_CH_BACK_RIGHT (1ULL << AV_CHAN_BACK_RIGHT )
174#define AV_CH_FRONT_LEFT_OF_CENTER (1ULL << AV_CHAN_FRONT_LEFT_OF_CENTER )
175#define AV_CH_FRONT_RIGHT_OF_CENTER (1ULL << AV_CHAN_FRONT_RIGHT_OF_CENTER)
176#define AV_CH_BACK_CENTER (1ULL << AV_CHAN_BACK_CENTER )
177#define AV_CH_SIDE_LEFT (1ULL << AV_CHAN_SIDE_LEFT )
178#define AV_CH_SIDE_RIGHT (1ULL << AV_CHAN_SIDE_RIGHT )
179#define AV_CH_TOP_CENTER (1ULL << AV_CHAN_TOP_CENTER )
180#define AV_CH_TOP_FRONT_LEFT (1ULL << AV_CHAN_TOP_FRONT_LEFT )
181#define AV_CH_TOP_FRONT_CENTER (1ULL << AV_CHAN_TOP_FRONT_CENTER )
182#define AV_CH_TOP_FRONT_RIGHT (1ULL << AV_CHAN_TOP_FRONT_RIGHT )
183#define AV_CH_TOP_BACK_LEFT (1ULL << AV_CHAN_TOP_BACK_LEFT )
184#define AV_CH_TOP_BACK_CENTER (1ULL << AV_CHAN_TOP_BACK_CENTER )
185#define AV_CH_TOP_BACK_RIGHT (1ULL << AV_CHAN_TOP_BACK_RIGHT )
186#define AV_CH_STEREO_LEFT (1ULL << AV_CHAN_STEREO_LEFT )
187#define AV_CH_STEREO_RIGHT (1ULL << AV_CHAN_STEREO_RIGHT )
188#define AV_CH_WIDE_LEFT (1ULL << AV_CHAN_WIDE_LEFT )
189#define AV_CH_WIDE_RIGHT (1ULL << AV_CHAN_WIDE_RIGHT )
190#define AV_CH_SURROUND_DIRECT_LEFT (1ULL << AV_CHAN_SURROUND_DIRECT_LEFT )
191#define AV_CH_SURROUND_DIRECT_RIGHT (1ULL << AV_CHAN_SURROUND_DIRECT_RIGHT)
192#define AV_CH_LOW_FREQUENCY_2 (1ULL << AV_CHAN_LOW_FREQUENCY_2 )
193#define AV_CH_TOP_SIDE_LEFT (1ULL << AV_CHAN_TOP_SIDE_LEFT )
194#define AV_CH_TOP_SIDE_RIGHT (1ULL << AV_CHAN_TOP_SIDE_RIGHT )
195#define AV_CH_BOTTOM_FRONT_CENTER (1ULL << AV_CHAN_BOTTOM_FRONT_CENTER )
196#define AV_CH_BOTTOM_FRONT_LEFT (1ULL << AV_CHAN_BOTTOM_FRONT_LEFT )
197#define AV_CH_BOTTOM_FRONT_RIGHT (1ULL << AV_CHAN_BOTTOM_FRONT_RIGHT )
198
199/**
200 * @}
201 * @defgroup channel_mask_c Audio channel layouts
202 * @{
203 * */
204#define AV_CH_LAYOUT_MONO (AV_CH_FRONT_CENTER)
205#define AV_CH_LAYOUT_STEREO (AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT)
206#define AV_CH_LAYOUT_2POINT1 (AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY)
207#define AV_CH_LAYOUT_2_1 (AV_CH_LAYOUT_STEREO|AV_CH_BACK_CENTER)
208#define AV_CH_LAYOUT_SURROUND (AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER)
209#define AV_CH_LAYOUT_3POINT1 (AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY)
210#define AV_CH_LAYOUT_4POINT0 (AV_CH_LAYOUT_SURROUND|AV_CH_BACK_CENTER)
211#define AV_CH_LAYOUT_4POINT1 (AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY)
212#define AV_CH_LAYOUT_2_2 (AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT)
213#define AV_CH_LAYOUT_QUAD (AV_CH_LAYOUT_STEREO|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT)
214#define AV_CH_LAYOUT_5POINT0 (AV_CH_LAYOUT_SURROUND|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT)
215#define AV_CH_LAYOUT_5POINT1 (AV_CH_LAYOUT_5POINT0|AV_CH_LOW_FREQUENCY)
216#define AV_CH_LAYOUT_5POINT0_BACK (AV_CH_LAYOUT_SURROUND|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT)
217#define AV_CH_LAYOUT_5POINT1_BACK (AV_CH_LAYOUT_5POINT0_BACK|AV_CH_LOW_FREQUENCY)
218#define AV_CH_LAYOUT_6POINT0 (AV_CH_LAYOUT_5POINT0|AV_CH_BACK_CENTER)
219#define AV_CH_LAYOUT_6POINT0_FRONT (AV_CH_LAYOUT_2_2|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER)
220#define AV_CH_LAYOUT_HEXAGONAL (AV_CH_LAYOUT_5POINT0_BACK|AV_CH_BACK_CENTER)
221#define AV_CH_LAYOUT_3POINT1POINT2 (AV_CH_LAYOUT_3POINT1|AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_FRONT_RIGHT)
222#define AV_CH_LAYOUT_6POINT1 (AV_CH_LAYOUT_5POINT1|AV_CH_BACK_CENTER)
223#define AV_CH_LAYOUT_6POINT1_BACK (AV_CH_LAYOUT_5POINT1_BACK|AV_CH_BACK_CENTER)
224#define AV_CH_LAYOUT_6POINT1_FRONT (AV_CH_LAYOUT_6POINT0_FRONT|AV_CH_LOW_FREQUENCY)
225#define AV_CH_LAYOUT_7POINT0 (AV_CH_LAYOUT_5POINT0|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT)
226#define AV_CH_LAYOUT_7POINT0_FRONT (AV_CH_LAYOUT_5POINT0|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER)
227#define AV_CH_LAYOUT_7POINT1 (AV_CH_LAYOUT_5POINT1|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT)
228#define AV_CH_LAYOUT_7POINT1_WIDE (AV_CH_LAYOUT_5POINT1|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER)
229#define AV_CH_LAYOUT_7POINT1_WIDE_BACK (AV_CH_LAYOUT_5POINT1_BACK|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER)
230#define AV_CH_LAYOUT_5POINT1POINT2_BACK (AV_CH_LAYOUT_5POINT1_BACK|AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_FRONT_RIGHT)
231#define AV_CH_LAYOUT_OCTAGONAL (AV_CH_LAYOUT_5POINT0|AV_CH_BACK_LEFT|AV_CH_BACK_CENTER|AV_CH_BACK_RIGHT)
232#define AV_CH_LAYOUT_CUBE (AV_CH_LAYOUT_QUAD|AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_FRONT_RIGHT|AV_CH_TOP_BACK_LEFT|AV_CH_TOP_BACK_RIGHT)
233#define AV_CH_LAYOUT_5POINT1POINT4_BACK (AV_CH_LAYOUT_5POINT1POINT2_BACK|AV_CH_TOP_BACK_LEFT|AV_CH_TOP_BACK_RIGHT)
234#define AV_CH_LAYOUT_7POINT1POINT2 (AV_CH_LAYOUT_7POINT1|AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_FRONT_RIGHT)
235#define AV_CH_LAYOUT_7POINT1POINT4_BACK (AV_CH_LAYOUT_7POINT1POINT2|AV_CH_TOP_BACK_LEFT|AV_CH_TOP_BACK_RIGHT)
236#define AV_CH_LAYOUT_7POINT2POINT3 (AV_CH_LAYOUT_7POINT1POINT2|AV_CH_TOP_BACK_CENTER|AV_CH_LOW_FREQUENCY_2)
237#define AV_CH_LAYOUT_9POINT1POINT4_BACK (AV_CH_LAYOUT_7POINT1POINT4_BACK|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER)
238#define AV_CH_LAYOUT_HEXADECAGONAL (AV_CH_LAYOUT_OCTAGONAL|AV_CH_WIDE_LEFT|AV_CH_WIDE_RIGHT|AV_CH_TOP_BACK_LEFT|AV_CH_TOP_BACK_RIGHT|AV_CH_TOP_BACK_CENTER|AV_CH_TOP_FRONT_CENTER|AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_FRONT_RIGHT)
239#define AV_CH_LAYOUT_STEREO_DOWNMIX (AV_CH_STEREO_LEFT|AV_CH_STEREO_RIGHT)
240#define AV_CH_LAYOUT_22POINT2 (AV_CH_LAYOUT_7POINT1POINT4_BACK|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER|AV_CH_BACK_CENTER|AV_CH_LOW_FREQUENCY_2|AV_CH_TOP_FRONT_CENTER|AV_CH_TOP_CENTER|AV_CH_TOP_SIDE_LEFT|AV_CH_TOP_SIDE_RIGHT|AV_CH_TOP_BACK_CENTER|AV_CH_BOTTOM_FRONT_CENTER|AV_CH_BOTTOM_FRONT_LEFT|AV_CH_BOTTOM_FRONT_RIGHT)
241
242#define AV_CH_LAYOUT_7POINT1_TOP_BACK AV_CH_LAYOUT_5POINT1POINT2_BACK
243
244enum AVMatrixEncoding {
245 AV_MATRIX_ENCODING_NONE,
246 AV_MATRIX_ENCODING_DOLBY,
247 AV_MATRIX_ENCODING_DPLII,
248 AV_MATRIX_ENCODING_DPLIIX,
249 AV_MATRIX_ENCODING_DPLIIZ,
250 AV_MATRIX_ENCODING_DOLBYEX,
251 AV_MATRIX_ENCODING_DOLBYHEADPHONE,
252 AV_MATRIX_ENCODING_NB
253};
254
255/**
256 * @}
257 */
258
259/**
260 * An AVChannelCustom defines a single channel within a custom order layout
261 *
262 * Unlike most structures in FFmpeg, sizeof(AVChannelCustom) is a part of the
263 * public ABI.
264 *
265 * No new fields may be added to it without a major version bump.
266 */
267typedef struct AVChannelCustom {
268 enum AVChannel id;
269 char name[16];
270 void *opaque;
271} AVChannelCustom;
272
273/**
274 * An AVChannelLayout holds information about the channel layout of audio data.
275 *
276 * A channel layout here is defined as a set of channels ordered in a specific
277 * way (unless the channel order is AV_CHANNEL_ORDER_UNSPEC, in which case an
278 * AVChannelLayout carries only the channel count).
279 * All orders may be treated as if they were AV_CHANNEL_ORDER_UNSPEC by
280 * ignoring everything but the channel count, as long as av_channel_layout_check()
281 * considers they are valid.
282 *
283 * Unlike most structures in FFmpeg, sizeof(AVChannelLayout) is a part of the
284 * public ABI and may be used by the caller. E.g. it may be allocated on stack
285 * or embedded in caller-defined structs.
286 *
287 * AVChannelLayout can be initialized as follows:
288 * - default initialization with {0}, followed by setting all used fields
289 * correctly;
290 * - by assigning one of the predefined AV_CHANNEL_LAYOUT_* initializers;
291 * - with a constructor function, such as av_channel_layout_default(),
292 * av_channel_layout_from_mask() or av_channel_layout_from_string().
293 *
294 * The channel layout must be unitialized with av_channel_layout_uninit()
295 *
296 * Copying an AVChannelLayout via assigning is forbidden,
297 * av_channel_layout_copy() must be used instead (and its return value should
298 * be checked)
299 *
300 * No new fields may be added to it without a major version bump, except for
301 * new elements of the union fitting in sizeof(uint64_t).
302 */
303typedef struct AVChannelLayout {
304 /**
305 * Channel order used in this layout.
306 * This is a mandatory field.
307 */
308 enum AVChannelOrder order;
309
310 /**
311 * Number of channels in this layout. Mandatory field.
312 */
313 int nb_channels;
314
315 /**
316 * Details about which channels are present in this layout.
317 * For AV_CHANNEL_ORDER_UNSPEC, this field is undefined and must not be
318 * used.
319 */
320 union {
321 /**
322 * This member must be used for AV_CHANNEL_ORDER_NATIVE, and may be used
323 * for AV_CHANNEL_ORDER_AMBISONIC to signal non-diegetic channels.
324 * It is a bitmask, where the position of each set bit means that the
325 * AVChannel with the corresponding value is present.
326 *
327 * I.e. when (mask & (1 << AV_CHAN_FOO)) is non-zero, then AV_CHAN_FOO
328 * is present in the layout. Otherwise it is not present.
329 *
330 * @note when a channel layout using a bitmask is constructed or
331 * modified manually (i.e. not using any of the av_channel_layout_*
332 * functions), the code doing it must ensure that the number of set bits
333 * is equal to nb_channels.
334 */
335 uint64_t mask;
336 /**
337 * This member must be used when the channel order is
338 * AV_CHANNEL_ORDER_CUSTOM. It is a nb_channels-sized array, with each
339 * element signalling the presence of the AVChannel with the
340 * corresponding value in map[i].id.
341 *
342 * I.e. when map[i].id is equal to AV_CHAN_FOO, then AV_CH_FOO is the
343 * i-th channel in the audio data.
344 *
345 * When map[i].id is in the range between AV_CHAN_AMBISONIC_BASE and
346 * AV_CHAN_AMBISONIC_END (inclusive), the channel contains an ambisonic
347 * component with ACN index (as defined above)
348 * n = map[i].id - AV_CHAN_AMBISONIC_BASE.
349 *
350 * map[i].name may be filled with a 0-terminated string, in which case
351 * it will be used for the purpose of identifying the channel with the
352 * convenience functions below. Otherise it must be zeroed.
353 */
354 AVChannelCustom *map;
355 } u;
356
357 /**
358 * For some private data of the user.
359 */
360 void *opaque;
361} AVChannelLayout;
362
363/**
364 * Macro to define native channel layouts
365 *
366 * @note This doesn't use designated initializers for compatibility with C++ 17 and older.
367 */
368#define AV_CHANNEL_LAYOUT_MASK(nb, m) \
369 { /* .order */ AV_CHANNEL_ORDER_NATIVE, \
370 /* .nb_channels */ (nb), \
371 /* .u.mask */ { m }, \
372 /* .opaque */ NULL }
373
374/**
375 * @name Common pre-defined channel layouts
376 * @{
377 */
378#define AV_CHANNEL_LAYOUT_MONO AV_CHANNEL_LAYOUT_MASK(1, AV_CH_LAYOUT_MONO)
379#define AV_CHANNEL_LAYOUT_STEREO AV_CHANNEL_LAYOUT_MASK(2, AV_CH_LAYOUT_STEREO)
380#define AV_CHANNEL_LAYOUT_2POINT1 AV_CHANNEL_LAYOUT_MASK(3, AV_CH_LAYOUT_2POINT1)
381#define AV_CHANNEL_LAYOUT_2_1 AV_CHANNEL_LAYOUT_MASK(3, AV_CH_LAYOUT_2_1)
382#define AV_CHANNEL_LAYOUT_SURROUND AV_CHANNEL_LAYOUT_MASK(3, AV_CH_LAYOUT_SURROUND)
383#define AV_CHANNEL_LAYOUT_3POINT1 AV_CHANNEL_LAYOUT_MASK(4, AV_CH_LAYOUT_3POINT1)
384#define AV_CHANNEL_LAYOUT_4POINT0 AV_CHANNEL_LAYOUT_MASK(4, AV_CH_LAYOUT_4POINT0)
385#define AV_CHANNEL_LAYOUT_4POINT1 AV_CHANNEL_LAYOUT_MASK(5, AV_CH_LAYOUT_4POINT1)
386#define AV_CHANNEL_LAYOUT_2_2 AV_CHANNEL_LAYOUT_MASK(4, AV_CH_LAYOUT_2_2)
387#define AV_CHANNEL_LAYOUT_QUAD AV_CHANNEL_LAYOUT_MASK(4, AV_CH_LAYOUT_QUAD)
388#define AV_CHANNEL_LAYOUT_5POINT0 AV_CHANNEL_LAYOUT_MASK(5, AV_CH_LAYOUT_5POINT0)
389#define AV_CHANNEL_LAYOUT_5POINT1 AV_CHANNEL_LAYOUT_MASK(6, AV_CH_LAYOUT_5POINT1)
390#define AV_CHANNEL_LAYOUT_5POINT0_BACK AV_CHANNEL_LAYOUT_MASK(5, AV_CH_LAYOUT_5POINT0_BACK)
391#define AV_CHANNEL_LAYOUT_5POINT1_BACK AV_CHANNEL_LAYOUT_MASK(6, AV_CH_LAYOUT_5POINT1_BACK)
392#define AV_CHANNEL_LAYOUT_6POINT0 AV_CHANNEL_LAYOUT_MASK(6, AV_CH_LAYOUT_6POINT0)
393#define AV_CHANNEL_LAYOUT_6POINT0_FRONT AV_CHANNEL_LAYOUT_MASK(6, AV_CH_LAYOUT_6POINT0_FRONT)
394#define AV_CHANNEL_LAYOUT_3POINT1POINT2 AV_CHANNEL_LAYOUT_MASK(6, AV_CH_LAYOUT_3POINT1POINT2)
395#define AV_CHANNEL_LAYOUT_HEXAGONAL AV_CHANNEL_LAYOUT_MASK(6, AV_CH_LAYOUT_HEXAGONAL)
396#define AV_CHANNEL_LAYOUT_6POINT1 AV_CHANNEL_LAYOUT_MASK(7, AV_CH_LAYOUT_6POINT1)
397#define AV_CHANNEL_LAYOUT_6POINT1_BACK AV_CHANNEL_LAYOUT_MASK(7, AV_CH_LAYOUT_6POINT1_BACK)
398#define AV_CHANNEL_LAYOUT_6POINT1_FRONT AV_CHANNEL_LAYOUT_MASK(7, AV_CH_LAYOUT_6POINT1_FRONT)
399#define AV_CHANNEL_LAYOUT_7POINT0 AV_CHANNEL_LAYOUT_MASK(7, AV_CH_LAYOUT_7POINT0)
400#define AV_CHANNEL_LAYOUT_7POINT0_FRONT AV_CHANNEL_LAYOUT_MASK(7, AV_CH_LAYOUT_7POINT0_FRONT)
401#define AV_CHANNEL_LAYOUT_7POINT1 AV_CHANNEL_LAYOUT_MASK(8, AV_CH_LAYOUT_7POINT1)
402#define AV_CHANNEL_LAYOUT_7POINT1_WIDE AV_CHANNEL_LAYOUT_MASK(8, AV_CH_LAYOUT_7POINT1_WIDE)
403#define AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK AV_CHANNEL_LAYOUT_MASK(8, AV_CH_LAYOUT_7POINT1_WIDE_BACK)
404#define AV_CHANNEL_LAYOUT_5POINT1POINT2_BACK AV_CHANNEL_LAYOUT_MASK(8, AV_CH_LAYOUT_5POINT1POINT2_BACK)
405#define AV_CHANNEL_LAYOUT_OCTAGONAL AV_CHANNEL_LAYOUT_MASK(8, AV_CH_LAYOUT_OCTAGONAL)
406#define AV_CHANNEL_LAYOUT_CUBE AV_CHANNEL_LAYOUT_MASK(8, AV_CH_LAYOUT_CUBE)
407#define AV_CHANNEL_LAYOUT_5POINT1POINT4_BACK AV_CHANNEL_LAYOUT_MASK(10, AV_CH_LAYOUT_5POINT1POINT4_BACK)
408#define AV_CHANNEL_LAYOUT_7POINT1POINT2 AV_CHANNEL_LAYOUT_MASK(10, AV_CH_LAYOUT_7POINT1POINT2)
409#define AV_CHANNEL_LAYOUT_7POINT1POINT4_BACK AV_CHANNEL_LAYOUT_MASK(12, AV_CH_LAYOUT_7POINT1POINT4_BACK)
410#define AV_CHANNEL_LAYOUT_7POINT2POINT3 AV_CHANNEL_LAYOUT_MASK(12, AV_CH_LAYOUT_7POINT2POINT3)
411#define AV_CHANNEL_LAYOUT_9POINT1POINT4_BACK AV_CHANNEL_LAYOUT_MASK(14, AV_CH_LAYOUT_9POINT1POINT4_BACK)
412#define AV_CHANNEL_LAYOUT_HEXADECAGONAL AV_CHANNEL_LAYOUT_MASK(16, AV_CH_LAYOUT_HEXADECAGONAL)
413#define AV_CHANNEL_LAYOUT_STEREO_DOWNMIX AV_CHANNEL_LAYOUT_MASK(2, AV_CH_LAYOUT_STEREO_DOWNMIX)
414#define AV_CHANNEL_LAYOUT_22POINT2 AV_CHANNEL_LAYOUT_MASK(24, AV_CH_LAYOUT_22POINT2)
415
416#define AV_CHANNEL_LAYOUT_7POINT1_TOP_BACK AV_CHANNEL_LAYOUT_5POINT1POINT2_BACK
417
418#define AV_CHANNEL_LAYOUT_AMBISONIC_FIRST_ORDER \
419 { /* .order */ AV_CHANNEL_ORDER_AMBISONIC, \
420 /* .nb_channels */ 4, \
421 /* .u.mask */ { 0 }, \
422 /* .opaque */ NULL }
423/** @} */
424
425struct AVBPrint;
426
427/**
428 * Get a human readable string in an abbreviated form describing a given channel.
429 * This is the inverse function of @ref av_channel_from_string().
430 *
431 * @param buf pre-allocated buffer where to put the generated string
432 * @param buf_size size in bytes of the buffer.
433 * @param channel the AVChannel whose name to get
434 * @return amount of bytes needed to hold the output string, or a negative AVERROR
435 * on failure. If the returned value is bigger than buf_size, then the
436 * string was truncated.
437 */
438int av_channel_name(char *buf, size_t buf_size, enum AVChannel channel);
439
440/**
441 * bprint variant of av_channel_name().
442 *
443 * @note the string will be appended to the bprint buffer.
444 */
445void av_channel_name_bprint(struct AVBPrint *bp, enum AVChannel channel_id);
446
447/**
448 * Get a human readable string describing a given channel.
449 *
450 * @param buf pre-allocated buffer where to put the generated string
451 * @param buf_size size in bytes of the buffer.
452 * @param channel the AVChannel whose description to get
453 * @return amount of bytes needed to hold the output string, or a negative AVERROR
454 * on failure. If the returned value is bigger than buf_size, then the
455 * string was truncated.
456 */
457int av_channel_description(char *buf, size_t buf_size, enum AVChannel channel);
458
459/**
460 * bprint variant of av_channel_description().
461 *
462 * @note the string will be appended to the bprint buffer.
463 */
464void av_channel_description_bprint(struct AVBPrint *bp, enum AVChannel channel_id);
465
466/**
467 * This is the inverse function of @ref av_channel_name().
468 *
469 * @return the channel with the given name
470 * AV_CHAN_NONE when name does not identify a known channel
471 */
472enum AVChannel av_channel_from_string(const char *name);
473
474/**
475 * Initialize a custom channel layout with the specified number of channels.
476 * The channel map will be allocated and the designation of all channels will
477 * be set to AV_CHAN_UNKNOWN.
478 *
479 * This is only a convenience helper function, a custom channel layout can also
480 * be constructed without using this.
481 *
482 * @param channel_layout the layout structure to be initialized
483 * @param nb_channels the number of channels
484 *
485 * @return 0 on success
486 * AVERROR(EINVAL) if the number of channels <= 0
487 * AVERROR(ENOMEM) if the channel map could not be allocated
488 */
489int av_channel_layout_custom_init(AVChannelLayout *channel_layout, int nb_channels);
490
491/**
492 * Initialize a native channel layout from a bitmask indicating which channels
493 * are present.
494 *
495 * @param channel_layout the layout structure to be initialized
496 * @param mask bitmask describing the channel layout
497 *
498 * @return 0 on success
499 * AVERROR(EINVAL) for invalid mask values
500 */
501int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask);
502
503/**
504 * Initialize a channel layout from a given string description.
505 * The input string can be represented by:
506 * - the formal channel layout name (returned by av_channel_layout_describe())
507 * - single or multiple channel names (returned by av_channel_name(), eg. "FL",
508 * or concatenated with "+", each optionally containing a custom name after
509 * a "@", eg. "FL@Left+FR@Right+LFE")
510 * - a decimal or hexadecimal value of a native channel layout (eg. "4" or "0x4")
511 * - the number of channels with default layout (eg. "4c")
512 * - the number of unordered channels (eg. "4C" or "4 channels")
513 * - the ambisonic order followed by optional non-diegetic channels (eg.
514 * "ambisonic 2+stereo")
515 * On error, the channel layout will remain uninitialized, but not necessarily
516 * untouched.
517 *
518 * @param channel_layout uninitialized channel layout for the result
519 * @param str string describing the channel layout
520 * @return 0 on success parsing the channel layout
521 * AVERROR(EINVAL) if an invalid channel layout string was provided
522 * AVERROR(ENOMEM) if there was not enough memory
523 */
524int av_channel_layout_from_string(AVChannelLayout *channel_layout,
525 const char *str);
526
527/**
528 * Get the default channel layout for a given number of channels.
529 *
530 * @param ch_layout the layout structure to be initialized
531 * @param nb_channels number of channels
532 */
533void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels);
534
535/**
536 * Iterate over all standard channel layouts.
537 *
538 * @param opaque a pointer where libavutil will store the iteration state. Must
539 * point to NULL to start the iteration.
540 *
541 * @return the standard channel layout or NULL when the iteration is
542 * finished
543 */
544const AVChannelLayout *av_channel_layout_standard(void **opaque);
545
546/**
547 * Free any allocated data in the channel layout and reset the channel
548 * count to 0.
549 *
550 * @param channel_layout the layout structure to be uninitialized
551 */
552void av_channel_layout_uninit(AVChannelLayout *channel_layout);
553
554/**
555 * Make a copy of a channel layout. This differs from just assigning src to dst
556 * in that it allocates and copies the map for AV_CHANNEL_ORDER_CUSTOM.
557 *
558 * @note the destination channel_layout will be always uninitialized before copy.
559 *
560 * @param dst destination channel layout
561 * @param src source channel layout
562 * @return 0 on success, a negative AVERROR on error.
563 */
564int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src);
565
566/**
567 * Get a human-readable string describing the channel layout properties.
568 * The string will be in the same format that is accepted by
569 * @ref av_channel_layout_from_string(), allowing to rebuild the same
570 * channel layout, except for opaque pointers.
571 *
572 * @param channel_layout channel layout to be described
573 * @param buf pre-allocated buffer where to put the generated string
574 * @param buf_size size in bytes of the buffer.
575 * @return amount of bytes needed to hold the output string, or a negative AVERROR
576 * on failure. If the returned value is bigger than buf_size, then the
577 * string was truncated.
578 */
579int av_channel_layout_describe(const AVChannelLayout *channel_layout,
580 char *buf, size_t buf_size);
581
582/**
583 * bprint variant of av_channel_layout_describe().
584 *
585 * @note the string will be appended to the bprint buffer.
586 * @return 0 on success, or a negative AVERROR value on failure.
587 */
588int av_channel_layout_describe_bprint(const AVChannelLayout *channel_layout,
589 struct AVBPrint *bp);
590
591/**
592 * Get the channel with the given index in a channel layout.
593 *
594 * @param channel_layout input channel layout
595 * @param idx index of the channel
596 * @return channel with the index idx in channel_layout on success or
597 * AV_CHAN_NONE on failure (if idx is not valid or the channel order is
598 * unspecified)
599 */
600enum AVChannel
601av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout, unsigned int idx);
602
603/**
604 * Get the index of a given channel in a channel layout. In case multiple
605 * channels are found, only the first match will be returned.
606 *
607 * @param channel_layout input channel layout
608 * @param channel the channel whose index to obtain
609 * @return index of channel in channel_layout on success or a negative number if
610 * channel is not present in channel_layout.
611 */
612int av_channel_layout_index_from_channel(const AVChannelLayout *channel_layout,
613 enum AVChannel channel);
614
615/**
616 * Get the index in a channel layout of a channel described by the given string.
617 * In case multiple channels are found, only the first match will be returned.
618 *
619 * This function accepts channel names in the same format as
620 * @ref av_channel_from_string().
621 *
622 * @param channel_layout input channel layout
623 * @param name string describing the channel whose index to obtain
624 * @return a channel index described by the given string, or a negative AVERROR
625 * value.
626 */
627int av_channel_layout_index_from_string(const AVChannelLayout *channel_layout,
628 const char *name);
629
630/**
631 * Get a channel described by the given string.
632 *
633 * This function accepts channel names in the same format as
634 * @ref av_channel_from_string().
635 *
636 * @param channel_layout input channel layout
637 * @param name string describing the channel to obtain
638 * @return a channel described by the given string in channel_layout on success
639 * or AV_CHAN_NONE on failure (if the string is not valid or the channel
640 * order is unspecified)
641 */
642enum AVChannel
643av_channel_layout_channel_from_string(const AVChannelLayout *channel_layout,
644 const char *name);
645
646/**
647 * Find out what channels from a given set are present in a channel layout,
648 * without regard for their positions.
649 *
650 * @param channel_layout input channel layout
651 * @param mask a combination of AV_CH_* representing a set of channels
652 * @return a bitfield representing all the channels from mask that are present
653 * in channel_layout
654 */
655uint64_t av_channel_layout_subset(const AVChannelLayout *channel_layout,
656 uint64_t mask);
657
658/**
659 * Check whether a channel layout is valid, i.e. can possibly describe audio
660 * data.
661 *
662 * @param channel_layout input channel layout
663 * @return 1 if channel_layout is valid, 0 otherwise.
664 */
665int av_channel_layout_check(const AVChannelLayout *channel_layout);
666
667/**
668 * Check whether two channel layouts are semantically the same, i.e. the same
669 * channels are present on the same positions in both.
670 *
671 * If one of the channel layouts is AV_CHANNEL_ORDER_UNSPEC, while the other is
672 * not, they are considered to be unequal. If both are AV_CHANNEL_ORDER_UNSPEC,
673 * they are considered equal iff the channel counts are the same in both.
674 *
675 * @param chl input channel layout
676 * @param chl1 input channel layout
677 * @return 0 if chl and chl1 are equal, 1 if they are not equal. A negative
678 * AVERROR code if one or both are invalid.
679 */
680int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1);
681
682/**
683 * The conversion must be lossless.
684 */
685#define AV_CHANNEL_LAYOUT_RETYPE_FLAG_LOSSLESS (1 << 0)
686
687/**
688 * The specified retype target order is ignored and the simplest possible
689 * (canonical) order is used for which the input layout can be losslessy
690 * represented.
691 */
692#define AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL (1 << 1)
693
694/**
695 * Change the AVChannelOrder of a channel layout.
696 *
697 * Change of AVChannelOrder can be either lossless or lossy. In case of a
698 * lossless conversion all the channel designations and the associated channel
699 * names (if any) are kept. On a lossy conversion the channel names and channel
700 * designations might be lost depending on the capabilities of the desired
701 * AVChannelOrder. Note that some conversions are simply not possible in which
702 * case this function returns AVERROR(ENOSYS).
703 *
704 * The following conversions are supported:
705 *
706 * Any -> Custom : Always possible, always lossless.
707 * Any -> Unspecified: Always possible, lossless if channel designations
708 * are all unknown and channel names are not used, lossy otherwise.
709 * Custom -> Ambisonic : Possible if it contains ambisonic channels with
710 * optional non-diegetic channels in the end. Lossy if the channels have
711 * custom names, lossless otherwise.
712 * Custom -> Native : Possible if it contains native channels in native
713 * order. Lossy if the channels have custom names, lossless otherwise.
714 *
715 * On error this function keeps the original channel layout untouched.
716 *
717 * @param channel_layout channel layout which will be changed
718 * @param order the desired channel layout order
719 * @param flags a combination of AV_CHANNEL_LAYOUT_RETYPE_FLAG_* constants
720 * @return 0 if the conversion was successful and lossless or if the channel
721 * layout was already in the desired order
722 * >0 if the conversion was successful but lossy
723 * AVERROR(ENOSYS) if the conversion was not possible (or would be
724 * lossy and AV_CHANNEL_LAYOUT_RETYPE_FLAG_LOSSLESS was specified)
725 * AVERROR(EINVAL), AVERROR(ENOMEM) on error
726 */
727int av_channel_layout_retype(AVChannelLayout *channel_layout, enum AVChannelOrder order, int flags);
728
729/**
730 * @}
731 */
732
733#endif /* AVUTIL_CHANNEL_LAYOUT_H */
734