a2dp-codecs.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* SPDX-License-Identifier: LGPL-2.1-or-later */
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2006-2010 Nokia Corporation
  7. * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
  8. * Copyright (C) 2018 Pali Rohár <pali.rohar@gmail.com>
  9. *
  10. *
  11. */
  12. #include <endian.h>
  13. #include <stdint.h>
  14. #define A2DP_CODEC_SBC 0x00
  15. #define A2DP_CODEC_MPEG12 0x01
  16. #define A2DP_CODEC_MPEG24 0x02
  17. #define A2DP_CODEC_ATRAC 0x04
  18. #define A2DP_CODEC_VENDOR 0xFF
  19. #define SBC_SAMPLING_FREQ_16000 (1 << 3)
  20. #define SBC_SAMPLING_FREQ_32000 (1 << 2)
  21. #define SBC_SAMPLING_FREQ_44100 (1 << 1)
  22. #define SBC_SAMPLING_FREQ_48000 1
  23. #define SBC_CHANNEL_MODE_MONO (1 << 3)
  24. #define SBC_CHANNEL_MODE_DUAL_CHANNEL (1 << 2)
  25. #define SBC_CHANNEL_MODE_STEREO (1 << 1)
  26. #define SBC_CHANNEL_MODE_JOINT_STEREO 1
  27. #define SBC_BLOCK_LENGTH_4 (1 << 3)
  28. #define SBC_BLOCK_LENGTH_8 (1 << 2)
  29. #define SBC_BLOCK_LENGTH_12 (1 << 1)
  30. #define SBC_BLOCK_LENGTH_16 1
  31. #define SBC_SUBBANDS_4 (1 << 1)
  32. #define SBC_SUBBANDS_8 1
  33. #define SBC_ALLOCATION_SNR (1 << 1)
  34. #define SBC_ALLOCATION_LOUDNESS 1
  35. #define SBC_MIN_BITPOOL 2
  36. #define SBC_MAX_BITPOOL 250
  37. /* Other settings:
  38. * Block length = 16
  39. * Allocation method = Loudness
  40. * Subbands = 8
  41. */
  42. #define SBC_BITPOOL_MQ_MONO_44100 19
  43. #define SBC_BITPOOL_MQ_MONO_48000 18
  44. #define SBC_BITPOOL_MQ_JOINT_STEREO_44100 35
  45. #define SBC_BITPOOL_MQ_JOINT_STEREO_48000 33
  46. #define SBC_BITPOOL_HQ_MONO_44100 31
  47. #define SBC_BITPOOL_HQ_MONO_48000 29
  48. #define SBC_BITPOOL_HQ_JOINT_STEREO_44100 53
  49. #define SBC_BITPOOL_HQ_JOINT_STEREO_48000 51
  50. #define MPEG_CHANNEL_MODE_MONO (1 << 3)
  51. #define MPEG_CHANNEL_MODE_DUAL_CHANNEL (1 << 2)
  52. #define MPEG_CHANNEL_MODE_STEREO (1 << 1)
  53. #define MPEG_CHANNEL_MODE_JOINT_STEREO 1
  54. #define MPEG_LAYER_MP1 (1 << 2)
  55. #define MPEG_LAYER_MP2 (1 << 1)
  56. #define MPEG_LAYER_MP3 1
  57. #define MPEG_SAMPLING_FREQ_16000 (1 << 5)
  58. #define MPEG_SAMPLING_FREQ_22050 (1 << 4)
  59. #define MPEG_SAMPLING_FREQ_24000 (1 << 3)
  60. #define MPEG_SAMPLING_FREQ_32000 (1 << 2)
  61. #define MPEG_SAMPLING_FREQ_44100 (1 << 1)
  62. #define MPEG_SAMPLING_FREQ_48000 1
  63. #define MPEG_BIT_RATE_INDEX_0 (1 << 0)
  64. #define MPEG_BIT_RATE_INDEX_1 (1 << 1)
  65. #define MPEG_BIT_RATE_INDEX_2 (1 << 2)
  66. #define MPEG_BIT_RATE_INDEX_3 (1 << 3)
  67. #define MPEG_BIT_RATE_INDEX_4 (1 << 4)
  68. #define MPEG_BIT_RATE_INDEX_5 (1 << 5)
  69. #define MPEG_BIT_RATE_INDEX_6 (1 << 6)
  70. #define MPEG_BIT_RATE_INDEX_7 (1 << 7)
  71. #define MPEG_BIT_RATE_INDEX_8 (1 << 8)
  72. #define MPEG_BIT_RATE_INDEX_9 (1 << 9)
  73. #define MPEG_BIT_RATE_INDEX_10 (1 << 10)
  74. #define MPEG_BIT_RATE_INDEX_11 (1 << 11)
  75. #define MPEG_BIT_RATE_INDEX_12 (1 << 12)
  76. #define MPEG_BIT_RATE_INDEX_13 (1 << 13)
  77. #define MPEG_BIT_RATE_INDEX_14 (1 << 14)
  78. #define MPEG_MP1_BIT_RATE_32000 MPEG_BIT_RATE_INDEX_1
  79. #define MPEG_MP1_BIT_RATE_64000 MPEG_BIT_RATE_INDEX_2
  80. #define MPEG_MP1_BIT_RATE_96000 MPEG_BIT_RATE_INDEX_3
  81. #define MPEG_MP1_BIT_RATE_128000 MPEG_BIT_RATE_INDEX_4
  82. #define MPEG_MP1_BIT_RATE_160000 MPEG_BIT_RATE_INDEX_5
  83. #define MPEG_MP1_BIT_RATE_192000 MPEG_BIT_RATE_INDEX_6
  84. #define MPEG_MP1_BIT_RATE_224000 MPEG_BIT_RATE_INDEX_7
  85. #define MPEG_MP1_BIT_RATE_256000 MPEG_BIT_RATE_INDEX_8
  86. #define MPEG_MP1_BIT_RATE_288000 MPEG_BIT_RATE_INDEX_9
  87. #define MPEG_MP1_BIT_RATE_320000 MPEG_BIT_RATE_INDEX_10
  88. #define MPEG_MP1_BIT_RATE_352000 MPEG_BIT_RATE_INDEX_11
  89. #define MPEG_MP1_BIT_RATE_384000 MPEG_BIT_RATE_INDEX_12
  90. #define MPEG_MP1_BIT_RATE_416000 MPEG_BIT_RATE_INDEX_13
  91. #define MPEG_MP1_BIT_RATE_448000 MPEG_BIT_RATE_INDEX_14
  92. #define MPEG_MP2_BIT_RATE_32000 MPEG_BIT_RATE_INDEX_1
  93. #define MPEG_MP2_BIT_RATE_48000 MPEG_BIT_RATE_INDEX_2
  94. #define MPEG_MP2_BIT_RATE_56000 MPEG_BIT_RATE_INDEX_3
  95. #define MPEG_MP2_BIT_RATE_64000 MPEG_BIT_RATE_INDEX_4
  96. #define MPEG_MP2_BIT_RATE_80000 MPEG_BIT_RATE_INDEX_5
  97. #define MPEG_MP2_BIT_RATE_96000 MPEG_BIT_RATE_INDEX_6
  98. #define MPEG_MP2_BIT_RATE_112000 MPEG_BIT_RATE_INDEX_7
  99. #define MPEG_MP2_BIT_RATE_128000 MPEG_BIT_RATE_INDEX_8
  100. #define MPEG_MP2_BIT_RATE_160000 MPEG_BIT_RATE_INDEX_9
  101. #define MPEG_MP2_BIT_RATE_192000 MPEG_BIT_RATE_INDEX_10
  102. #define MPEG_MP2_BIT_RATE_224000 MPEG_BIT_RATE_INDEX_11
  103. #define MPEG_MP2_BIT_RATE_256000 MPEG_BIT_RATE_INDEX_12
  104. #define MPEG_MP2_BIT_RATE_320000 MPEG_BIT_RATE_INDEX_13
  105. #define MPEG_MP2_BIT_RATE_384000 MPEG_BIT_RATE_INDEX_14
  106. #define MPEG_MP3_BIT_RATE_32000 MPEG_BIT_RATE_INDEX_1
  107. #define MPEG_MP3_BIT_RATE_40000 MPEG_BIT_RATE_INDEX_2
  108. #define MPEG_MP3_BIT_RATE_48000 MPEG_BIT_RATE_INDEX_3
  109. #define MPEG_MP3_BIT_RATE_56000 MPEG_BIT_RATE_INDEX_4
  110. #define MPEG_MP3_BIT_RATE_64000 MPEG_BIT_RATE_INDEX_5
  111. #define MPEG_MP3_BIT_RATE_80000 MPEG_BIT_RATE_INDEX_6
  112. #define MPEG_MP3_BIT_RATE_96000 MPEG_BIT_RATE_INDEX_7
  113. #define MPEG_MP3_BIT_RATE_112000 MPEG_BIT_RATE_INDEX_8
  114. #define MPEG_MP3_BIT_RATE_128000 MPEG_BIT_RATE_INDEX_9
  115. #define MPEG_MP3_BIT_RATE_160000 MPEG_BIT_RATE_INDEX_10
  116. #define MPEG_MP3_BIT_RATE_192000 MPEG_BIT_RATE_INDEX_11
  117. #define MPEG_MP3_BIT_RATE_224000 MPEG_BIT_RATE_INDEX_12
  118. #define MPEG_MP3_BIT_RATE_256000 MPEG_BIT_RATE_INDEX_13
  119. #define MPEG_MP3_BIT_RATE_320000 MPEG_BIT_RATE_INDEX_14
  120. #define MPEG_BIT_RATE_FREE MPEG_BIT_RATE_INDEX_0
  121. #define MPEG_GET_BITRATE(a) ((uint16_t)(a).bitrate1 << 8 | (a).bitrate2)
  122. #define MPEG_SET_BITRATE(a, b) \
  123. do { \
  124. (a).bitrate1 = ((b) >> 8) & 0x7f; \
  125. (a).bitrate2 = (b) & 0xff; \
  126. } while (0)
  127. #define AAC_OBJECT_TYPE_MPEG2_AAC_LC 0x80
  128. #define AAC_OBJECT_TYPE_MPEG4_AAC_LC 0x40
  129. #define AAC_OBJECT_TYPE_MPEG4_AAC_LTP 0x20
  130. #define AAC_OBJECT_TYPE_MPEG4_AAC_SCA 0x10
  131. #define AAC_SAMPLING_FREQ_8000 0x0800
  132. #define AAC_SAMPLING_FREQ_11025 0x0400
  133. #define AAC_SAMPLING_FREQ_12000 0x0200
  134. #define AAC_SAMPLING_FREQ_16000 0x0100
  135. #define AAC_SAMPLING_FREQ_22050 0x0080
  136. #define AAC_SAMPLING_FREQ_24000 0x0040
  137. #define AAC_SAMPLING_FREQ_32000 0x0020
  138. #define AAC_SAMPLING_FREQ_44100 0x0010
  139. #define AAC_SAMPLING_FREQ_48000 0x0008
  140. #define AAC_SAMPLING_FREQ_64000 0x0004
  141. #define AAC_SAMPLING_FREQ_88200 0x0002
  142. #define AAC_SAMPLING_FREQ_96000 0x0001
  143. #define AAC_CHANNELS_1 0x02
  144. #define AAC_CHANNELS_2 0x01
  145. #define AAC_GET_BITRATE(a) ((a).bitrate1 << 16 | \
  146. (a).bitrate2 << 8 | (a).bitrate3)
  147. #define AAC_GET_FREQUENCY(a) ((a).frequency1 << 4 | (a).frequency2)
  148. #define AAC_SET_BITRATE(a, b) \
  149. do { \
  150. (a).bitrate1 = (b >> 16) & 0x7f; \
  151. (a).bitrate2 = (b >> 8) & 0xff; \
  152. (a).bitrate3 = b & 0xff; \
  153. } while (0)
  154. #define AAC_SET_FREQUENCY(a, f) \
  155. do { \
  156. (a).frequency1 = (f >> 4) & 0xff; \
  157. (a).frequency2 = f & 0x0f; \
  158. } while (0)
  159. #define AAC_INIT_BITRATE(b) \
  160. .bitrate1 = (b >> 16) & 0x7f, \
  161. .bitrate2 = (b >> 8) & 0xff, \
  162. .bitrate3 = b & 0xff,
  163. #define AAC_INIT_FREQUENCY(f) \
  164. .frequency1 = (f >> 4) & 0xff, \
  165. .frequency2 = f & 0x0f,
  166. #define APTX_VENDOR_ID 0x0000004f
  167. #define APTX_CODEC_ID 0x0001
  168. #define APTX_CHANNEL_MODE_MONO 0x01
  169. #define APTX_CHANNEL_MODE_STEREO 0x02
  170. #define APTX_SAMPLING_FREQ_16000 0x08
  171. #define APTX_SAMPLING_FREQ_32000 0x04
  172. #define APTX_SAMPLING_FREQ_44100 0x02
  173. #define APTX_SAMPLING_FREQ_48000 0x01
  174. #define FASTSTREAM_VENDOR_ID 0x0000000a
  175. #define FASTSTREAM_CODEC_ID 0x0001
  176. #define FASTSTREAM_DIRECTION_SINK 0x1
  177. #define FASTSTREAM_DIRECTION_SOURCE 0x2
  178. #define FASTSTREAM_SINK_SAMPLING_FREQ_44100 0x2
  179. #define FASTSTREAM_SINK_SAMPLING_FREQ_48000 0x1
  180. #define FASTSTREAM_SOURCE_SAMPLING_FREQ_16000 0x2
  181. #define APTX_LL_VENDOR_ID 0x0000000a
  182. #define APTX_LL_CODEC_ID 0x0002
  183. /* Default parameters for aptX Low Latency encoder */
  184. /* Target codec buffer level = 180 */
  185. #define APTX_LL_TARGET_LEVEL2 0xb4
  186. #define APTX_LL_TARGET_LEVEL1 0x00
  187. /* Initial codec buffer level = 360 */
  188. #define APTX_LL_INITIAL_LEVEL2 0x68
  189. #define APTX_LL_INITIAL_LEVEL1 0x01
  190. /* SRA max rate 0.005 * 10000 = 50 */
  191. #define APTX_LL_SRA_MAX_RATE 0x32
  192. /* SRA averaging time = 1s */
  193. #define APTX_LL_SRA_AVG_TIME 0x01
  194. /* Good working codec buffer level = 180 */
  195. #define APTX_LL_GOOD_WORKING_LEVEL2 0xB4
  196. #define APTX_LL_GOOD_WORKING_LEVEL1 0x00
  197. #define APTX_HD_VENDOR_ID 0x000000D7
  198. #define APTX_HD_CODEC_ID 0x0024
  199. #define LDAC_VENDOR_ID 0x0000012d
  200. #define LDAC_CODEC_ID 0x00aa
  201. #define LDAC_SAMPLING_FREQ_44100 0x20
  202. #define LDAC_SAMPLING_FREQ_48000 0x10
  203. #define LDAC_SAMPLING_FREQ_88200 0x08
  204. #define LDAC_SAMPLING_FREQ_96000 0x04
  205. #define LDAC_SAMPLING_FREQ_176400 0x02
  206. #define LDAC_SAMPLING_FREQ_192000 0x01
  207. #define LDAC_CHANNEL_MODE_MONO 0x04
  208. #define LDAC_CHANNEL_MODE_DUAL 0x02
  209. #define LDAC_CHANNEL_MODE_STEREO 0x01
  210. typedef struct {
  211. uint8_t vendor_id4;
  212. uint8_t vendor_id3;
  213. uint8_t vendor_id2;
  214. uint8_t vendor_id1;
  215. uint8_t codec_id2;
  216. uint8_t codec_id1;
  217. } __attribute__ ((packed)) a2dp_vendor_codec_t;
  218. #define A2DP_GET_VENDOR_ID(a) ( \
  219. (((uint32_t)(a).vendor_id4) << 0) | \
  220. (((uint32_t)(a).vendor_id3) << 8) | \
  221. (((uint32_t)(a).vendor_id2) << 16) | \
  222. (((uint32_t)(a).vendor_id1) << 24) \
  223. )
  224. #define A2DP_GET_CODEC_ID(a) ((a).codec_id2 | (((uint16_t)(a).codec_id1) << 8))
  225. #define A2DP_SET_VENDOR_ID_CODEC_ID(v, c) ((a2dp_vendor_codec_t){ \
  226. .vendor_id4 = (((v) >> 0) & 0xff), \
  227. .vendor_id3 = (((v) >> 8) & 0xff), \
  228. .vendor_id2 = (((v) >> 16) & 0xff), \
  229. .vendor_id1 = (((v) >> 24) & 0xff), \
  230. .codec_id2 = (((c) >> 0) & 0xff), \
  231. .codec_id1 = (((c) >> 8) & 0xff), \
  232. })
  233. typedef struct {
  234. uint8_t reserved;
  235. uint8_t target_level2;
  236. uint8_t target_level1;
  237. uint8_t initial_level2;
  238. uint8_t initial_level1;
  239. uint8_t sra_max_rate;
  240. uint8_t sra_avg_time;
  241. uint8_t good_working_level2;
  242. uint8_t good_working_level1;
  243. } __attribute__ ((packed)) a2dp_aptx_ll_new_caps_t;
  244. typedef struct {
  245. a2dp_vendor_codec_t info;
  246. uint8_t frequency;
  247. uint8_t channel_mode;
  248. } __attribute__ ((packed)) a2dp_ldac_t;
  249. #if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
  250. __BYTE_ORDER == __LITTLE_ENDIAN
  251. typedef struct {
  252. uint8_t channel_mode:4;
  253. uint8_t frequency:4;
  254. uint8_t allocation_method:2;
  255. uint8_t subbands:2;
  256. uint8_t block_length:4;
  257. uint8_t min_bitpool;
  258. uint8_t max_bitpool;
  259. } __attribute__ ((packed)) a2dp_sbc_t;
  260. typedef struct {
  261. uint8_t channel_mode:4;
  262. uint8_t crc:1;
  263. uint8_t layer:3;
  264. uint8_t frequency:6;
  265. uint8_t mpf:1;
  266. uint8_t rfa:1;
  267. uint8_t bitrate1:7;
  268. uint8_t vbr:1;
  269. uint8_t bitrate2;
  270. } __attribute__ ((packed)) a2dp_mpeg_t;
  271. typedef struct {
  272. uint8_t object_type;
  273. uint8_t frequency1;
  274. uint8_t rfa:2;
  275. uint8_t channels:2;
  276. uint8_t frequency2:4;
  277. uint8_t bitrate1:7;
  278. uint8_t vbr:1;
  279. uint8_t bitrate2;
  280. uint8_t bitrate3;
  281. } __attribute__ ((packed)) a2dp_aac_t;
  282. typedef struct {
  283. a2dp_vendor_codec_t info;
  284. uint8_t channel_mode:4;
  285. uint8_t frequency:4;
  286. } __attribute__ ((packed)) a2dp_aptx_t;
  287. typedef struct {
  288. a2dp_vendor_codec_t info;
  289. uint8_t direction;
  290. uint8_t sink_frequency:4;
  291. uint8_t source_frequency:4;
  292. } __attribute__ ((packed)) a2dp_faststream_t;
  293. typedef struct {
  294. a2dp_aptx_t aptx;
  295. uint8_t bidirect_link:1;
  296. uint8_t has_new_caps:1;
  297. uint8_t reserved:6;
  298. a2dp_aptx_ll_new_caps_t new_caps[0];
  299. } __attribute__ ((packed)) a2dp_aptx_ll_t;
  300. #elif defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
  301. __BYTE_ORDER == __BIG_ENDIAN
  302. typedef struct {
  303. uint8_t frequency:4;
  304. uint8_t channel_mode:4;
  305. uint8_t block_length:4;
  306. uint8_t subbands:2;
  307. uint8_t allocation_method:2;
  308. uint8_t min_bitpool;
  309. uint8_t max_bitpool;
  310. } __attribute__ ((packed)) a2dp_sbc_t;
  311. typedef struct {
  312. uint8_t layer:3;
  313. uint8_t crc:1;
  314. uint8_t channel_mode:4;
  315. uint8_t rfa:1;
  316. uint8_t mpf:1;
  317. uint8_t frequency:6;
  318. uint8_t vbr:1;
  319. uint8_t bitrate1:7;
  320. uint8_t bitrate2;
  321. } __attribute__ ((packed)) a2dp_mpeg_t;
  322. typedef struct {
  323. uint8_t object_type;
  324. uint8_t frequency1;
  325. uint8_t frequency2:4;
  326. uint8_t channels:2;
  327. uint8_t rfa:2;
  328. uint8_t vbr:1;
  329. uint8_t bitrate1:7;
  330. uint8_t bitrate2;
  331. uint8_t bitrate3;
  332. } __attribute__ ((packed)) a2dp_aac_t;
  333. typedef struct {
  334. a2dp_vendor_codec_t info;
  335. uint8_t frequency:4;
  336. uint8_t channel_mode:4;
  337. } __attribute__ ((packed)) a2dp_aptx_t;
  338. typedef struct {
  339. a2dp_vendor_codec_t info;
  340. uint8_t direction;
  341. uint8_t source_frequency:4;
  342. uint8_t sink_frequency:4;
  343. } __attribute__ ((packed)) a2dp_faststream_t;
  344. typedef struct {
  345. a2dp_aptx_t aptx;
  346. uint8_t reserved:6;
  347. uint8_t has_new_caps:1;
  348. uint8_t bidirect_link:1;
  349. a2dp_aptx_ll_new_caps_t new_caps[0];
  350. } __attribute__ ((packed)) a2dp_aptx_ll_t;
  351. #else
  352. #error "Unknown byte order"
  353. #endif
  354. typedef struct {
  355. a2dp_aptx_t aptx;
  356. uint8_t reserved0;
  357. uint8_t reserved1;
  358. uint8_t reserved2;
  359. uint8_t reserved3;
  360. } __attribute__ ((packed)) a2dp_aptx_hd_t;