audio.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /****************************************************************************
  2. Copyright (c) 2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #pragma once
  22. // ----------------------------------------------------------------------------
  23. #include <stdint.h>
  24. #include <android/log.h>
  25. #include "audio/android/cutils/bitops.h"
  26. #define PROPERTY_VALUE_MAX 256
  27. #define CONSTEXPR constexpr
  28. #ifdef __cplusplus
  29. # define CC_LIKELY( exp ) (__builtin_expect( !!(exp), true ))
  30. # define CC_UNLIKELY( exp ) (__builtin_expect( !!(exp), false ))
  31. #else
  32. # define CC_LIKELY( exp ) (__builtin_expect( !!(exp), 1 ))
  33. # define CC_UNLIKELY( exp ) (__builtin_expect( !!(exp), 0 ))
  34. #endif
  35. /* special audio session values
  36. * (XXX: should this be living in the audio effects land?)
  37. */
  38. typedef enum {
  39. /* session for effects attached to a particular output stream
  40. * (value must be less than 0)
  41. */
  42. AUDIO_SESSION_OUTPUT_STAGE = -1,
  43. /* session for effects applied to output mix. These effects can
  44. * be moved by audio policy manager to another output stream
  45. * (value must be 0)
  46. */
  47. AUDIO_SESSION_OUTPUT_MIX = 0,
  48. /* application does not specify an explicit session ID to be used,
  49. * and requests a new session ID to be allocated
  50. * TODO use unique values for AUDIO_SESSION_OUTPUT_MIX and AUDIO_SESSION_ALLOCATE,
  51. * after all uses have been updated from 0 to the appropriate symbol, and have been tested.
  52. */
  53. AUDIO_SESSION_ALLOCATE = 0,
  54. } audio_session_t;
  55. /* Audio sub formats (see enum audio_format). */
  56. /* PCM sub formats */
  57. typedef enum {
  58. /* All of these are in native byte order */
  59. AUDIO_FORMAT_PCM_SUB_16_BIT = 0x1, /* DO NOT CHANGE - PCM signed 16 bits */
  60. AUDIO_FORMAT_PCM_SUB_8_BIT = 0x2, /* DO NOT CHANGE - PCM unsigned 8 bits */
  61. AUDIO_FORMAT_PCM_SUB_32_BIT = 0x3, /* PCM signed .31 fixed point */
  62. AUDIO_FORMAT_PCM_SUB_8_24_BIT = 0x4, /* PCM signed 8.23 fixed point */
  63. AUDIO_FORMAT_PCM_SUB_FLOAT = 0x5, /* PCM single-precision floating point */
  64. AUDIO_FORMAT_PCM_SUB_24_BIT_PACKED = 0x6, /* PCM signed .23 fixed point packed in 3 bytes */
  65. } audio_format_pcm_sub_fmt_t;
  66. /* The audio_format_*_sub_fmt_t declarations are not currently used */
  67. /* MP3 sub format field definition : can use 11 LSBs in the same way as MP3
  68. * frame header to specify bit rate, stereo mode, version...
  69. */
  70. typedef enum {
  71. AUDIO_FORMAT_MP3_SUB_NONE = 0x0,
  72. } audio_format_mp3_sub_fmt_t;
  73. /* AMR NB/WB sub format field definition: specify frame block interleaving,
  74. * bandwidth efficient or octet aligned, encoding mode for recording...
  75. */
  76. typedef enum {
  77. AUDIO_FORMAT_AMR_SUB_NONE = 0x0,
  78. } audio_format_amr_sub_fmt_t;
  79. /* AAC sub format field definition: specify profile or bitrate for recording... */
  80. typedef enum {
  81. AUDIO_FORMAT_AAC_SUB_MAIN = 0x1,
  82. AUDIO_FORMAT_AAC_SUB_LC = 0x2,
  83. AUDIO_FORMAT_AAC_SUB_SSR = 0x4,
  84. AUDIO_FORMAT_AAC_SUB_LTP = 0x8,
  85. AUDIO_FORMAT_AAC_SUB_HE_V1 = 0x10,
  86. AUDIO_FORMAT_AAC_SUB_SCALABLE = 0x20,
  87. AUDIO_FORMAT_AAC_SUB_ERLC = 0x40,
  88. AUDIO_FORMAT_AAC_SUB_LD = 0x80,
  89. AUDIO_FORMAT_AAC_SUB_HE_V2 = 0x100,
  90. AUDIO_FORMAT_AAC_SUB_ELD = 0x200,
  91. } audio_format_aac_sub_fmt_t;
  92. /* VORBIS sub format field definition: specify quality for recording... */
  93. typedef enum {
  94. AUDIO_FORMAT_VORBIS_SUB_NONE = 0x0,
  95. } audio_format_vorbis_sub_fmt_t;
  96. /* Audio format consists of a main format field (upper 8 bits) and a sub format
  97. * field (lower 24 bits).
  98. *
  99. * The main format indicates the main codec type. The sub format field
  100. * indicates options and parameters for each format. The sub format is mainly
  101. * used for record to indicate for instance the requested bitrate or profile.
  102. * It can also be used for certain formats to give informations not present in
  103. * the encoded audio stream (e.g. octet alignment for AMR).
  104. */
  105. typedef enum {
  106. AUDIO_FORMAT_INVALID = 0xFFFFFFFFUL,
  107. AUDIO_FORMAT_DEFAULT = 0,
  108. AUDIO_FORMAT_PCM = 0x00000000UL, /* DO NOT CHANGE */
  109. AUDIO_FORMAT_MP3 = 0x01000000UL,
  110. AUDIO_FORMAT_AMR_NB = 0x02000000UL,
  111. AUDIO_FORMAT_AMR_WB = 0x03000000UL,
  112. AUDIO_FORMAT_AAC = 0x04000000UL,
  113. AUDIO_FORMAT_HE_AAC_V1 = 0x05000000UL, /* Deprecated, Use AUDIO_FORMAT_AAC_HE_V1*/
  114. AUDIO_FORMAT_HE_AAC_V2 = 0x06000000UL, /* Deprecated, Use AUDIO_FORMAT_AAC_HE_V2*/
  115. AUDIO_FORMAT_VORBIS = 0x07000000UL,
  116. AUDIO_FORMAT_OPUS = 0x08000000UL,
  117. AUDIO_FORMAT_AC3 = 0x09000000UL,
  118. AUDIO_FORMAT_E_AC3 = 0x0A000000UL,
  119. AUDIO_FORMAT_DTS = 0x0B000000UL,
  120. AUDIO_FORMAT_DTS_HD = 0x0C000000UL,
  121. AUDIO_FORMAT_MAIN_MASK = 0xFF000000UL,
  122. AUDIO_FORMAT_SUB_MASK = 0x00FFFFFFUL,
  123. /* Aliases */
  124. /* note != AudioFormat.ENCODING_PCM_16BIT */
  125. AUDIO_FORMAT_PCM_16_BIT = (AUDIO_FORMAT_PCM |
  126. AUDIO_FORMAT_PCM_SUB_16_BIT),
  127. /* note != AudioFormat.ENCODING_PCM_8BIT */
  128. AUDIO_FORMAT_PCM_8_BIT = (AUDIO_FORMAT_PCM |
  129. AUDIO_FORMAT_PCM_SUB_8_BIT),
  130. AUDIO_FORMAT_PCM_32_BIT = (AUDIO_FORMAT_PCM |
  131. AUDIO_FORMAT_PCM_SUB_32_BIT),
  132. AUDIO_FORMAT_PCM_8_24_BIT = (AUDIO_FORMAT_PCM |
  133. AUDIO_FORMAT_PCM_SUB_8_24_BIT),
  134. AUDIO_FORMAT_PCM_FLOAT = (AUDIO_FORMAT_PCM |
  135. AUDIO_FORMAT_PCM_SUB_FLOAT),
  136. AUDIO_FORMAT_PCM_24_BIT_PACKED = (AUDIO_FORMAT_PCM |
  137. AUDIO_FORMAT_PCM_SUB_24_BIT_PACKED),
  138. AUDIO_FORMAT_AAC_MAIN = (AUDIO_FORMAT_AAC |
  139. AUDIO_FORMAT_AAC_SUB_MAIN),
  140. AUDIO_FORMAT_AAC_LC = (AUDIO_FORMAT_AAC |
  141. AUDIO_FORMAT_AAC_SUB_LC),
  142. AUDIO_FORMAT_AAC_SSR = (AUDIO_FORMAT_AAC |
  143. AUDIO_FORMAT_AAC_SUB_SSR),
  144. AUDIO_FORMAT_AAC_LTP = (AUDIO_FORMAT_AAC |
  145. AUDIO_FORMAT_AAC_SUB_LTP),
  146. AUDIO_FORMAT_AAC_HE_V1 = (AUDIO_FORMAT_AAC |
  147. AUDIO_FORMAT_AAC_SUB_HE_V1),
  148. AUDIO_FORMAT_AAC_SCALABLE = (AUDIO_FORMAT_AAC |
  149. AUDIO_FORMAT_AAC_SUB_SCALABLE),
  150. AUDIO_FORMAT_AAC_ERLC = (AUDIO_FORMAT_AAC |
  151. AUDIO_FORMAT_AAC_SUB_ERLC),
  152. AUDIO_FORMAT_AAC_LD = (AUDIO_FORMAT_AAC |
  153. AUDIO_FORMAT_AAC_SUB_LD),
  154. AUDIO_FORMAT_AAC_HE_V2 = (AUDIO_FORMAT_AAC |
  155. AUDIO_FORMAT_AAC_SUB_HE_V2),
  156. AUDIO_FORMAT_AAC_ELD = (AUDIO_FORMAT_AAC |
  157. AUDIO_FORMAT_AAC_SUB_ELD),
  158. } audio_format_t;
  159. /* For the channel mask for position assignment representation */
  160. enum {
  161. /* These can be a complete audio_channel_mask_t. */
  162. AUDIO_CHANNEL_NONE = 0x0,
  163. AUDIO_CHANNEL_INVALID = 0xC0000000,
  164. /* These can be the bits portion of an audio_channel_mask_t
  165. * with representation AUDIO_CHANNEL_REPRESENTATION_POSITION.
  166. * Using these bits as a complete audio_channel_mask_t is deprecated.
  167. */
  168. /* output channels */
  169. AUDIO_CHANNEL_OUT_FRONT_LEFT = 0x1,
  170. AUDIO_CHANNEL_OUT_FRONT_RIGHT = 0x2,
  171. AUDIO_CHANNEL_OUT_FRONT_CENTER = 0x4,
  172. AUDIO_CHANNEL_OUT_LOW_FREQUENCY = 0x8,
  173. AUDIO_CHANNEL_OUT_BACK_LEFT = 0x10,
  174. AUDIO_CHANNEL_OUT_BACK_RIGHT = 0x20,
  175. AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER = 0x40,
  176. AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER = 0x80,
  177. AUDIO_CHANNEL_OUT_BACK_CENTER = 0x100,
  178. AUDIO_CHANNEL_OUT_SIDE_LEFT = 0x200,
  179. AUDIO_CHANNEL_OUT_SIDE_RIGHT = 0x400,
  180. AUDIO_CHANNEL_OUT_TOP_CENTER = 0x800,
  181. AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT = 0x1000,
  182. AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER = 0x2000,
  183. AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT = 0x4000,
  184. AUDIO_CHANNEL_OUT_TOP_BACK_LEFT = 0x8000,
  185. AUDIO_CHANNEL_OUT_TOP_BACK_CENTER = 0x10000,
  186. AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT = 0x20000,
  187. /* TODO: should these be considered complete channel masks, or only bits? */
  188. AUDIO_CHANNEL_OUT_MONO = AUDIO_CHANNEL_OUT_FRONT_LEFT,
  189. AUDIO_CHANNEL_OUT_STEREO = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
  190. AUDIO_CHANNEL_OUT_FRONT_RIGHT),
  191. AUDIO_CHANNEL_OUT_QUAD = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
  192. AUDIO_CHANNEL_OUT_FRONT_RIGHT |
  193. AUDIO_CHANNEL_OUT_BACK_LEFT |
  194. AUDIO_CHANNEL_OUT_BACK_RIGHT),
  195. AUDIO_CHANNEL_OUT_QUAD_BACK = AUDIO_CHANNEL_OUT_QUAD,
  196. /* like AUDIO_CHANNEL_OUT_QUAD_BACK with *_SIDE_* instead of *_BACK_* */
  197. AUDIO_CHANNEL_OUT_QUAD_SIDE = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
  198. AUDIO_CHANNEL_OUT_FRONT_RIGHT |
  199. AUDIO_CHANNEL_OUT_SIDE_LEFT |
  200. AUDIO_CHANNEL_OUT_SIDE_RIGHT),
  201. AUDIO_CHANNEL_OUT_5POINT1 = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
  202. AUDIO_CHANNEL_OUT_FRONT_RIGHT |
  203. AUDIO_CHANNEL_OUT_FRONT_CENTER |
  204. AUDIO_CHANNEL_OUT_LOW_FREQUENCY |
  205. AUDIO_CHANNEL_OUT_BACK_LEFT |
  206. AUDIO_CHANNEL_OUT_BACK_RIGHT),
  207. AUDIO_CHANNEL_OUT_5POINT1_BACK = AUDIO_CHANNEL_OUT_5POINT1,
  208. /* like AUDIO_CHANNEL_OUT_5POINT1_BACK with *_SIDE_* instead of *_BACK_* */
  209. AUDIO_CHANNEL_OUT_5POINT1_SIDE = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
  210. AUDIO_CHANNEL_OUT_FRONT_RIGHT |
  211. AUDIO_CHANNEL_OUT_FRONT_CENTER |
  212. AUDIO_CHANNEL_OUT_LOW_FREQUENCY |
  213. AUDIO_CHANNEL_OUT_SIDE_LEFT |
  214. AUDIO_CHANNEL_OUT_SIDE_RIGHT),
  215. // matches the correct AudioFormat.CHANNEL_OUT_7POINT1_SURROUND definition for 7.1
  216. AUDIO_CHANNEL_OUT_7POINT1 = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
  217. AUDIO_CHANNEL_OUT_FRONT_RIGHT |
  218. AUDIO_CHANNEL_OUT_FRONT_CENTER |
  219. AUDIO_CHANNEL_OUT_LOW_FREQUENCY |
  220. AUDIO_CHANNEL_OUT_BACK_LEFT |
  221. AUDIO_CHANNEL_OUT_BACK_RIGHT |
  222. AUDIO_CHANNEL_OUT_SIDE_LEFT |
  223. AUDIO_CHANNEL_OUT_SIDE_RIGHT),
  224. AUDIO_CHANNEL_OUT_ALL = (AUDIO_CHANNEL_OUT_FRONT_LEFT |
  225. AUDIO_CHANNEL_OUT_FRONT_RIGHT |
  226. AUDIO_CHANNEL_OUT_FRONT_CENTER |
  227. AUDIO_CHANNEL_OUT_LOW_FREQUENCY |
  228. AUDIO_CHANNEL_OUT_BACK_LEFT |
  229. AUDIO_CHANNEL_OUT_BACK_RIGHT |
  230. AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER |
  231. AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER |
  232. AUDIO_CHANNEL_OUT_BACK_CENTER|
  233. AUDIO_CHANNEL_OUT_SIDE_LEFT|
  234. AUDIO_CHANNEL_OUT_SIDE_RIGHT|
  235. AUDIO_CHANNEL_OUT_TOP_CENTER|
  236. AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT|
  237. AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER|
  238. AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT|
  239. AUDIO_CHANNEL_OUT_TOP_BACK_LEFT|
  240. AUDIO_CHANNEL_OUT_TOP_BACK_CENTER|
  241. AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT),
  242. /* These are bits only, not complete values */
  243. /* input channels */
  244. AUDIO_CHANNEL_IN_LEFT = 0x4,
  245. AUDIO_CHANNEL_IN_RIGHT = 0x8,
  246. AUDIO_CHANNEL_IN_FRONT = 0x10,
  247. AUDIO_CHANNEL_IN_BACK = 0x20,
  248. AUDIO_CHANNEL_IN_LEFT_PROCESSED = 0x40,
  249. AUDIO_CHANNEL_IN_RIGHT_PROCESSED = 0x80,
  250. AUDIO_CHANNEL_IN_FRONT_PROCESSED = 0x100,
  251. AUDIO_CHANNEL_IN_BACK_PROCESSED = 0x200,
  252. AUDIO_CHANNEL_IN_PRESSURE = 0x400,
  253. AUDIO_CHANNEL_IN_X_AXIS = 0x800,
  254. AUDIO_CHANNEL_IN_Y_AXIS = 0x1000,
  255. AUDIO_CHANNEL_IN_Z_AXIS = 0x2000,
  256. AUDIO_CHANNEL_IN_VOICE_UPLINK = 0x4000,
  257. AUDIO_CHANNEL_IN_VOICE_DNLINK = 0x8000,
  258. /* TODO: should these be considered complete channel masks, or only bits, or deprecated? */
  259. AUDIO_CHANNEL_IN_MONO = AUDIO_CHANNEL_IN_FRONT,
  260. AUDIO_CHANNEL_IN_STEREO = (AUDIO_CHANNEL_IN_LEFT | AUDIO_CHANNEL_IN_RIGHT),
  261. AUDIO_CHANNEL_IN_FRONT_BACK = (AUDIO_CHANNEL_IN_FRONT | AUDIO_CHANNEL_IN_BACK),
  262. AUDIO_CHANNEL_IN_ALL = (AUDIO_CHANNEL_IN_LEFT |
  263. AUDIO_CHANNEL_IN_RIGHT |
  264. AUDIO_CHANNEL_IN_FRONT |
  265. AUDIO_CHANNEL_IN_BACK|
  266. AUDIO_CHANNEL_IN_LEFT_PROCESSED |
  267. AUDIO_CHANNEL_IN_RIGHT_PROCESSED |
  268. AUDIO_CHANNEL_IN_FRONT_PROCESSED |
  269. AUDIO_CHANNEL_IN_BACK_PROCESSED|
  270. AUDIO_CHANNEL_IN_PRESSURE |
  271. AUDIO_CHANNEL_IN_X_AXIS |
  272. AUDIO_CHANNEL_IN_Y_AXIS |
  273. AUDIO_CHANNEL_IN_Z_AXIS |
  274. AUDIO_CHANNEL_IN_VOICE_UPLINK |
  275. AUDIO_CHANNEL_IN_VOICE_DNLINK),
  276. };
  277. /* A channel mask per se only defines the presence or absence of a channel, not the order.
  278. * But see AUDIO_INTERLEAVE_* below for the platform convention of order.
  279. *
  280. * audio_channel_mask_t is an opaque type and its internal layout should not
  281. * be assumed as it may change in the future.
  282. * Instead, always use the functions declared in this header to examine.
  283. *
  284. * These are the current representations:
  285. *
  286. * AUDIO_CHANNEL_REPRESENTATION_POSITION
  287. * is a channel mask representation for position assignment.
  288. * Each low-order bit corresponds to the spatial position of a transducer (output),
  289. * or interpretation of channel (input).
  290. * The user of a channel mask needs to know the context of whether it is for output or input.
  291. * The constants AUDIO_CHANNEL_OUT_* or AUDIO_CHANNEL_IN_* apply to the bits portion.
  292. * It is not permitted for no bits to be set.
  293. *
  294. * AUDIO_CHANNEL_REPRESENTATION_INDEX
  295. * is a channel mask representation for index assignment.
  296. * Each low-order bit corresponds to a selected channel.
  297. * There is no platform interpretation of the various bits.
  298. * There is no concept of output or input.
  299. * It is not permitted for no bits to be set.
  300. *
  301. * All other representations are reserved for future use.
  302. *
  303. * Warning: current representation distinguishes between input and output, but this will not the be
  304. * case in future revisions of the platform. Wherever there is an ambiguity between input and output
  305. * that is currently resolved by checking the channel mask, the implementer should look for ways to
  306. * fix it with additional information outside of the mask.
  307. */
  308. typedef uint32_t audio_channel_mask_t;
  309. /* Maximum number of channels for all representations */
  310. #define AUDIO_CHANNEL_COUNT_MAX 30
  311. /* log(2) of maximum number of representations, not part of public API */
  312. #define AUDIO_CHANNEL_REPRESENTATION_LOG2 2
  313. /* Representations */
  314. typedef enum {
  315. AUDIO_CHANNEL_REPRESENTATION_POSITION = 0, // must be zero for compatibility
  316. // 1 is reserved for future use
  317. AUDIO_CHANNEL_REPRESENTATION_INDEX = 2,
  318. // 3 is reserved for future use
  319. } audio_channel_representation_t;
  320. /* The return value is undefined if the channel mask is invalid. */
  321. static inline uint32_t audio_channel_mask_get_bits(audio_channel_mask_t channel)
  322. {
  323. return channel & ((1 << AUDIO_CHANNEL_COUNT_MAX) - 1);
  324. }
  325. /* The return value is undefined if the channel mask is invalid. */
  326. static inline audio_channel_representation_t audio_channel_mask_get_representation(
  327. audio_channel_mask_t channel)
  328. {
  329. // The right shift should be sufficient, but also "and" for safety in case mask is not 32 bits
  330. return (audio_channel_representation_t)
  331. ((channel >> AUDIO_CHANNEL_COUNT_MAX) & ((1 << AUDIO_CHANNEL_REPRESENTATION_LOG2) - 1));
  332. }
  333. /* Returns the number of channels from an output channel mask,
  334. * used in the context of audio output or playback.
  335. * If a channel bit is set which could _not_ correspond to an output channel,
  336. * it is excluded from the count.
  337. * Returns zero if the representation is invalid.
  338. */
  339. static inline uint32_t audio_channel_count_from_out_mask(audio_channel_mask_t channel)
  340. {
  341. uint32_t bits = audio_channel_mask_get_bits(channel);
  342. switch (audio_channel_mask_get_representation(channel)) {
  343. case AUDIO_CHANNEL_REPRESENTATION_POSITION:
  344. // TODO: We can now merge with from_in_mask and remove anding
  345. bits &= AUDIO_CHANNEL_OUT_ALL;
  346. // fall through
  347. case AUDIO_CHANNEL_REPRESENTATION_INDEX:
  348. return popcount(bits);
  349. default:
  350. return 0;
  351. }
  352. }
  353. static inline bool audio_is_valid_format(audio_format_t format)
  354. {
  355. switch (format & AUDIO_FORMAT_MAIN_MASK) {
  356. case AUDIO_FORMAT_PCM:
  357. switch (format) {
  358. case AUDIO_FORMAT_PCM_16_BIT:
  359. case AUDIO_FORMAT_PCM_8_BIT:
  360. case AUDIO_FORMAT_PCM_32_BIT:
  361. case AUDIO_FORMAT_PCM_8_24_BIT:
  362. case AUDIO_FORMAT_PCM_FLOAT:
  363. case AUDIO_FORMAT_PCM_24_BIT_PACKED:
  364. return true;
  365. default:
  366. return false;
  367. }
  368. /* not reached */
  369. case AUDIO_FORMAT_MP3:
  370. case AUDIO_FORMAT_AMR_NB:
  371. case AUDIO_FORMAT_AMR_WB:
  372. case AUDIO_FORMAT_AAC:
  373. case AUDIO_FORMAT_HE_AAC_V1:
  374. case AUDIO_FORMAT_HE_AAC_V2:
  375. case AUDIO_FORMAT_VORBIS:
  376. case AUDIO_FORMAT_OPUS:
  377. case AUDIO_FORMAT_AC3:
  378. case AUDIO_FORMAT_E_AC3:
  379. case AUDIO_FORMAT_DTS:
  380. case AUDIO_FORMAT_DTS_HD:
  381. return true;
  382. default:
  383. return false;
  384. }
  385. }
  386. static inline bool audio_is_linear_pcm(audio_format_t format)
  387. {
  388. return ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM);
  389. }
  390. static inline size_t audio_bytes_per_sample(audio_format_t format)
  391. {
  392. size_t size = 0;
  393. switch (format) {
  394. case AUDIO_FORMAT_PCM_32_BIT:
  395. case AUDIO_FORMAT_PCM_8_24_BIT:
  396. size = sizeof(int32_t);
  397. break;
  398. case AUDIO_FORMAT_PCM_24_BIT_PACKED:
  399. size = sizeof(uint8_t) * 3;
  400. break;
  401. case AUDIO_FORMAT_PCM_16_BIT:
  402. size = sizeof(int16_t);
  403. break;
  404. case AUDIO_FORMAT_PCM_8_BIT:
  405. size = sizeof(uint8_t);
  406. break;
  407. case AUDIO_FORMAT_PCM_FLOAT:
  408. size = sizeof(float);
  409. break;
  410. default:
  411. break;
  412. }
  413. return size;
  414. }
  415. /* Not part of public API */
  416. static inline audio_channel_mask_t audio_channel_mask_from_representation_and_bits(
  417. audio_channel_representation_t representation, uint32_t bits)
  418. {
  419. return (audio_channel_mask_t) ((representation << AUDIO_CHANNEL_COUNT_MAX) | bits);
  420. }
  421. /* Derive an output channel mask for position assignment from a channel count.
  422. * This is to be used when the content channel mask is unknown. The 1, 2, 4, 5, 6, 7 and 8 channel
  423. * cases are mapped to the standard game/home-theater layouts, but note that 4 is mapped to quad,
  424. * and not stereo + FC + mono surround. A channel count of 3 is arbitrarily mapped to stereo + FC
  425. * for continuity with stereo.
  426. * Returns the matching channel mask,
  427. * or AUDIO_CHANNEL_NONE if the channel count is zero,
  428. * or AUDIO_CHANNEL_INVALID if the channel count exceeds that of the
  429. * configurations for which a default output channel mask is defined.
  430. */
  431. static inline audio_channel_mask_t audio_channel_out_mask_from_count(uint32_t channel_count)
  432. {
  433. uint32_t bits;
  434. switch (channel_count) {
  435. case 0:
  436. return AUDIO_CHANNEL_NONE;
  437. case 1:
  438. bits = AUDIO_CHANNEL_OUT_MONO;
  439. break;
  440. case 2:
  441. bits = AUDIO_CHANNEL_OUT_STEREO;
  442. break;
  443. case 3:
  444. bits = AUDIO_CHANNEL_OUT_STEREO | AUDIO_CHANNEL_OUT_FRONT_CENTER;
  445. break;
  446. case 4: // 4.0
  447. bits = AUDIO_CHANNEL_OUT_QUAD;
  448. break;
  449. case 5: // 5.0
  450. bits = AUDIO_CHANNEL_OUT_QUAD | AUDIO_CHANNEL_OUT_FRONT_CENTER;
  451. break;
  452. case 6: // 5.1
  453. bits = AUDIO_CHANNEL_OUT_5POINT1;
  454. break;
  455. case 7: // 6.1
  456. bits = AUDIO_CHANNEL_OUT_5POINT1 | AUDIO_CHANNEL_OUT_BACK_CENTER;
  457. break;
  458. case 8:
  459. bits = AUDIO_CHANNEL_OUT_7POINT1;
  460. break;
  461. // FIXME FCC_8
  462. default:
  463. return AUDIO_CHANNEL_INVALID;
  464. }
  465. return audio_channel_mask_from_representation_and_bits(
  466. AUDIO_CHANNEL_REPRESENTATION_POSITION, bits);
  467. }