channel_layout.h 32 KB

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