You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1163 lines
46KB

  1. /*
  2. * Audio Processing Technology codec for Bluetooth (aptX)
  3. *
  4. * Copyright (C) 2017 Aurelien Jacobs <aurel@gnuage.org>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/intreadwrite.h"
  23. #include "avcodec.h"
  24. #include "internal.h"
  25. #include "mathops.h"
  26. #include "audio_frame_queue.h"
  27. enum channels {
  28. LEFT,
  29. RIGHT,
  30. NB_CHANNELS
  31. };
  32. enum subbands {
  33. LF, // Low Frequency (0-5.5 kHz)
  34. MLF, // Medium-Low Frequency (5.5-11kHz)
  35. MHF, // Medium-High Frequency (11-16.5kHz)
  36. HF, // High Frequency (16.5-22kHz)
  37. NB_SUBBANDS
  38. };
  39. #define NB_FILTERS 2
  40. #define FILTER_TAPS 16
  41. typedef struct {
  42. int pos;
  43. int32_t buffer[2*FILTER_TAPS];
  44. } FilterSignal;
  45. typedef struct {
  46. FilterSignal outer_filter_signal[NB_FILTERS];
  47. FilterSignal inner_filter_signal[NB_FILTERS][NB_FILTERS];
  48. } QMFAnalysis;
  49. typedef struct {
  50. int32_t quantized_sample;
  51. int32_t quantized_sample_parity_change;
  52. int32_t error;
  53. } Quantize;
  54. typedef struct {
  55. int32_t quantization_factor;
  56. int32_t factor_select;
  57. int32_t reconstructed_difference;
  58. } InvertQuantize;
  59. typedef struct {
  60. int32_t prev_sign[2];
  61. int32_t s_weight[2];
  62. int32_t d_weight[24];
  63. int32_t pos;
  64. int32_t reconstructed_differences[48];
  65. int32_t previous_reconstructed_sample;
  66. int32_t predicted_difference;
  67. int32_t predicted_sample;
  68. } Prediction;
  69. typedef struct {
  70. int32_t codeword_history;
  71. int32_t dither_parity;
  72. int32_t dither[NB_SUBBANDS];
  73. QMFAnalysis qmf;
  74. Quantize quantize[NB_SUBBANDS];
  75. InvertQuantize invert_quantize[NB_SUBBANDS];
  76. Prediction prediction[NB_SUBBANDS];
  77. } Channel;
  78. typedef struct {
  79. int hd;
  80. int block_size;
  81. int32_t sync_idx;
  82. Channel channels[NB_CHANNELS];
  83. AudioFrameQueue afq;
  84. } AptXContext;
  85. static const int32_t quantize_intervals_LF[65] = {
  86. -9948, 9948, 29860, 49808, 69822, 89926, 110144, 130502,
  87. 151026, 171738, 192666, 213832, 235264, 256982, 279014, 301384,
  88. 324118, 347244, 370790, 394782, 419250, 444226, 469742, 495832,
  89. 522536, 549890, 577936, 606720, 636290, 666700, 698006, 730270,
  90. 763562, 797958, 833538, 870398, 908640, 948376, 989740, 1032874,
  91. 1077948, 1125150, 1174700, 1226850, 1281900, 1340196, 1402156, 1468282,
  92. 1539182, 1615610, 1698514, 1789098, 1888944, 2000168, 2125700, 2269750,
  93. 2438670, 2642660, 2899462, 3243240, 3746078, 4535138, 5664098, 7102424,
  94. 8897462,
  95. };
  96. static const int32_t invert_quantize_dither_factors_LF[65] = {
  97. 9948, 9948, 9962, 9988, 10026, 10078, 10142, 10218,
  98. 10306, 10408, 10520, 10646, 10784, 10934, 11098, 11274,
  99. 11462, 11664, 11880, 12112, 12358, 12618, 12898, 13194,
  100. 13510, 13844, 14202, 14582, 14988, 15422, 15884, 16380,
  101. 16912, 17484, 18098, 18762, 19480, 20258, 21106, 22030,
  102. 23044, 24158, 25390, 26760, 28290, 30008, 31954, 34172,
  103. 36728, 39700, 43202, 47382, 52462, 58762, 66770, 77280,
  104. 91642, 112348, 144452, 199326, 303512, 485546, 643414, 794914,
  105. 1000124,
  106. };
  107. static const int32_t quantize_dither_factors_LF[65] = {
  108. 0, 4, 7, 10, 13, 16, 19, 22,
  109. 26, 28, 32, 35, 38, 41, 44, 47,
  110. 51, 54, 58, 62, 65, 70, 74, 79,
  111. 84, 90, 95, 102, 109, 116, 124, 133,
  112. 143, 154, 166, 180, 195, 212, 231, 254,
  113. 279, 308, 343, 383, 430, 487, 555, 639,
  114. 743, 876, 1045, 1270, 1575, 2002, 2628, 3591,
  115. 5177, 8026, 13719, 26047, 45509, 39467, 37875, 51303,
  116. 0,
  117. };
  118. static const int16_t quantize_factor_select_offset_LF[65] = {
  119. 0, -21, -19, -17, -15, -12, -10, -8,
  120. -6, -4, -1, 1, 3, 6, 8, 10,
  121. 13, 15, 18, 20, 23, 26, 29, 31,
  122. 34, 37, 40, 43, 47, 50, 53, 57,
  123. 60, 64, 68, 72, 76, 80, 85, 89,
  124. 94, 99, 105, 110, 116, 123, 129, 136,
  125. 144, 152, 161, 171, 182, 194, 207, 223,
  126. 241, 263, 291, 328, 382, 467, 522, 522,
  127. 522,
  128. };
  129. static const int32_t quantize_intervals_MLF[9] = {
  130. -89806, 89806, 278502, 494338, 759442, 1113112, 1652322, 2720256, 5190186,
  131. };
  132. static const int32_t invert_quantize_dither_factors_MLF[9] = {
  133. 89806, 89806, 98890, 116946, 148158, 205512, 333698, 734236, 1735696,
  134. };
  135. static const int32_t quantize_dither_factors_MLF[9] = {
  136. 0, 2271, 4514, 7803, 14339, 32047, 100135, 250365, 0,
  137. };
  138. static const int16_t quantize_factor_select_offset_MLF[9] = {
  139. 0, -14, 6, 29, 58, 96, 154, 270, 521,
  140. };
  141. static const int32_t quantize_intervals_MHF[3] = {
  142. -194080, 194080, 890562,
  143. };
  144. static const int32_t invert_quantize_dither_factors_MHF[3] = {
  145. 194080, 194080, 502402,
  146. };
  147. static const int32_t quantize_dither_factors_MHF[3] = {
  148. 0, 77081, 0,
  149. };
  150. static const int16_t quantize_factor_select_offset_MHF[3] = {
  151. 0, -33, 136,
  152. };
  153. static const int32_t quantize_intervals_HF[5] = {
  154. -163006, 163006, 542708, 1120554, 2669238,
  155. };
  156. static const int32_t invert_quantize_dither_factors_HF[5] = {
  157. 163006, 163006, 216698, 361148, 1187538,
  158. };
  159. static const int32_t quantize_dither_factors_HF[5] = {
  160. 0, 13423, 36113, 206598, 0,
  161. };
  162. static const int16_t quantize_factor_select_offset_HF[5] = {
  163. 0, -8, 33, 95, 262,
  164. };
  165. static const int32_t hd_quantize_intervals_LF[257] = {
  166. -2436, 2436, 7308, 12180, 17054, 21930, 26806, 31686,
  167. 36566, 41450, 46338, 51230, 56124, 61024, 65928, 70836,
  168. 75750, 80670, 85598, 90530, 95470, 100418, 105372, 110336,
  169. 115308, 120288, 125278, 130276, 135286, 140304, 145334, 150374,
  170. 155426, 160490, 165566, 170654, 175756, 180870, 185998, 191138,
  171. 196294, 201466, 206650, 211850, 217068, 222300, 227548, 232814,
  172. 238096, 243396, 248714, 254050, 259406, 264778, 270172, 275584,
  173. 281018, 286470, 291944, 297440, 302956, 308496, 314056, 319640,
  174. 325248, 330878, 336532, 342212, 347916, 353644, 359398, 365178,
  175. 370986, 376820, 382680, 388568, 394486, 400430, 406404, 412408,
  176. 418442, 424506, 430600, 436726, 442884, 449074, 455298, 461554,
  177. 467844, 474168, 480528, 486922, 493354, 499820, 506324, 512866,
  178. 519446, 526064, 532722, 539420, 546160, 552940, 559760, 566624,
  179. 573532, 580482, 587478, 594520, 601606, 608740, 615920, 623148,
  180. 630426, 637754, 645132, 652560, 660042, 667576, 675164, 682808,
  181. 690506, 698262, 706074, 713946, 721876, 729868, 737920, 746036,
  182. 754216, 762460, 770770, 779148, 787594, 796108, 804694, 813354,
  183. 822086, 830892, 839774, 848736, 857776, 866896, 876100, 885386,
  184. 894758, 904218, 913766, 923406, 933138, 942964, 952886, 962908,
  185. 973030, 983254, 993582, 1004020, 1014566, 1025224, 1035996, 1046886,
  186. 1057894, 1069026, 1080284, 1091670, 1103186, 1114838, 1126628, 1138558,
  187. 1150634, 1162858, 1175236, 1187768, 1200462, 1213320, 1226346, 1239548,
  188. 1252928, 1266490, 1280242, 1294188, 1308334, 1322688, 1337252, 1352034,
  189. 1367044, 1382284, 1397766, 1413494, 1429478, 1445728, 1462252, 1479058,
  190. 1496158, 1513562, 1531280, 1549326, 1567710, 1586446, 1605550, 1625034,
  191. 1644914, 1665208, 1685932, 1707108, 1728754, 1750890, 1773542, 1796732,
  192. 1820488, 1844840, 1869816, 1895452, 1921780, 1948842, 1976680, 2005338,
  193. 2034868, 2065322, 2096766, 2129260, 2162880, 2197708, 2233832, 2271352,
  194. 2310384, 2351050, 2393498, 2437886, 2484404, 2533262, 2584710, 2639036,
  195. 2696578, 2757738, 2822998, 2892940, 2968278, 3049896, 3138912, 3236760,
  196. 3345312, 3467068, 3605434, 3765154, 3952904, 4177962, 4452178, 4787134,
  197. 5187290, 5647128, 6159120, 6720518, 7332904, 8000032, 8726664, 9518152,
  198. 10380372,
  199. };
  200. static const int32_t hd_invert_quantize_dither_factors_LF[257] = {
  201. 2436, 2436, 2436, 2436, 2438, 2438, 2438, 2440,
  202. 2442, 2442, 2444, 2446, 2448, 2450, 2454, 2456,
  203. 2458, 2462, 2464, 2468, 2472, 2476, 2480, 2484,
  204. 2488, 2492, 2498, 2502, 2506, 2512, 2518, 2524,
  205. 2528, 2534, 2540, 2548, 2554, 2560, 2568, 2574,
  206. 2582, 2588, 2596, 2604, 2612, 2620, 2628, 2636,
  207. 2646, 2654, 2664, 2672, 2682, 2692, 2702, 2712,
  208. 2722, 2732, 2742, 2752, 2764, 2774, 2786, 2798,
  209. 2810, 2822, 2834, 2846, 2858, 2870, 2884, 2896,
  210. 2910, 2924, 2938, 2952, 2966, 2980, 2994, 3010,
  211. 3024, 3040, 3056, 3070, 3086, 3104, 3120, 3136,
  212. 3154, 3170, 3188, 3206, 3224, 3242, 3262, 3280,
  213. 3300, 3320, 3338, 3360, 3380, 3400, 3422, 3442,
  214. 3464, 3486, 3508, 3532, 3554, 3578, 3602, 3626,
  215. 3652, 3676, 3702, 3728, 3754, 3780, 3808, 3836,
  216. 3864, 3892, 3920, 3950, 3980, 4010, 4042, 4074,
  217. 4106, 4138, 4172, 4206, 4240, 4276, 4312, 4348,
  218. 4384, 4422, 4460, 4500, 4540, 4580, 4622, 4664,
  219. 4708, 4752, 4796, 4842, 4890, 4938, 4986, 5036,
  220. 5086, 5138, 5192, 5246, 5300, 5358, 5416, 5474,
  221. 5534, 5596, 5660, 5726, 5792, 5860, 5930, 6002,
  222. 6074, 6150, 6226, 6306, 6388, 6470, 6556, 6644,
  223. 6736, 6828, 6924, 7022, 7124, 7228, 7336, 7448,
  224. 7562, 7680, 7802, 7928, 8058, 8192, 8332, 8476,
  225. 8624, 8780, 8940, 9106, 9278, 9458, 9644, 9840,
  226. 10042, 10252, 10472, 10702, 10942, 11194, 11458, 11734,
  227. 12024, 12328, 12648, 12986, 13342, 13720, 14118, 14540,
  228. 14990, 15466, 15976, 16520, 17102, 17726, 18398, 19124,
  229. 19908, 20760, 21688, 22702, 23816, 25044, 26404, 27922,
  230. 29622, 31540, 33720, 36222, 39116, 42502, 46514, 51334,
  231. 57218, 64536, 73830, 85890, 101860, 123198, 151020, 183936,
  232. 216220, 243618, 268374, 293022, 319362, 347768, 378864, 412626, 449596,
  233. };
  234. static const int32_t hd_quantize_dither_factors_LF[256] = {
  235. 0, 0, 0, 1, 0, 0, 1, 1,
  236. 0, 1, 1, 1, 1, 1, 1, 1,
  237. 1, 1, 1, 1, 1, 1, 1, 1,
  238. 1, 2, 1, 1, 2, 2, 2, 1,
  239. 2, 2, 2, 2, 2, 2, 2, 2,
  240. 2, 2, 2, 2, 2, 2, 2, 3,
  241. 2, 3, 2, 3, 3, 3, 3, 3,
  242. 3, 3, 3, 3, 3, 3, 3, 3,
  243. 3, 3, 3, 3, 3, 4, 3, 4,
  244. 4, 4, 4, 4, 4, 4, 4, 4,
  245. 4, 4, 4, 4, 5, 4, 4, 5,
  246. 4, 5, 5, 5, 5, 5, 5, 5,
  247. 5, 5, 6, 5, 5, 6, 5, 6,
  248. 6, 6, 6, 6, 6, 6, 6, 7,
  249. 6, 7, 7, 7, 7, 7, 7, 7,
  250. 7, 7, 8, 8, 8, 8, 8, 8,
  251. 8, 9, 9, 9, 9, 9, 9, 9,
  252. 10, 10, 10, 10, 10, 11, 11, 11,
  253. 11, 11, 12, 12, 12, 12, 13, 13,
  254. 13, 14, 14, 14, 15, 15, 15, 15,
  255. 16, 16, 17, 17, 17, 18, 18, 18,
  256. 19, 19, 20, 21, 21, 22, 22, 23,
  257. 23, 24, 25, 26, 26, 27, 28, 29,
  258. 30, 31, 32, 33, 34, 35, 36, 37,
  259. 39, 40, 42, 43, 45, 47, 49, 51,
  260. 53, 55, 58, 60, 63, 66, 69, 73,
  261. 76, 80, 85, 89, 95, 100, 106, 113,
  262. 119, 128, 136, 146, 156, 168, 182, 196,
  263. 213, 232, 254, 279, 307, 340, 380, 425,
  264. 480, 545, 626, 724, 847, 1003, 1205, 1471,
  265. 1830, 2324, 3015, 3993, 5335, 6956, 8229, 8071,
  266. 6850, 6189, 6162, 6585, 7102, 7774, 8441, 9243,
  267. };
  268. static const int16_t hd_quantize_factor_select_offset_LF[257] = {
  269. 0, -22, -21, -21, -20, -20, -19, -19,
  270. -18, -18, -17, -17, -16, -16, -15, -14,
  271. -14, -13, -13, -12, -12, -11, -11, -10,
  272. -10, -9, -9, -8, -7, -7, -6, -6,
  273. -5, -5, -4, -4, -3, -3, -2, -1,
  274. -1, 0, 0, 1, 1, 2, 2, 3,
  275. 4, 4, 5, 5, 6, 6, 7, 8,
  276. 8, 9, 9, 10, 11, 11, 12, 12,
  277. 13, 14, 14, 15, 15, 16, 17, 17,
  278. 18, 19, 19, 20, 20, 21, 22, 22,
  279. 23, 24, 24, 25, 26, 26, 27, 28,
  280. 28, 29, 30, 30, 31, 32, 33, 33,
  281. 34, 35, 35, 36, 37, 38, 38, 39,
  282. 40, 41, 41, 42, 43, 44, 44, 45,
  283. 46, 47, 48, 48, 49, 50, 51, 52,
  284. 52, 53, 54, 55, 56, 57, 58, 58,
  285. 59, 60, 61, 62, 63, 64, 65, 66,
  286. 67, 68, 69, 69, 70, 71, 72, 73,
  287. 74, 75, 77, 78, 79, 80, 81, 82,
  288. 83, 84, 85, 86, 87, 89, 90, 91,
  289. 92, 93, 94, 96, 97, 98, 99, 101,
  290. 102, 103, 105, 106, 107, 109, 110, 112,
  291. 113, 115, 116, 118, 119, 121, 122, 124,
  292. 125, 127, 129, 130, 132, 134, 136, 137,
  293. 139, 141, 143, 145, 147, 149, 151, 153,
  294. 155, 158, 160, 162, 164, 167, 169, 172,
  295. 174, 177, 180, 182, 185, 188, 191, 194,
  296. 197, 201, 204, 208, 211, 215, 219, 223,
  297. 227, 232, 236, 241, 246, 251, 257, 263,
  298. 269, 275, 283, 290, 298, 307, 317, 327,
  299. 339, 352, 367, 384, 404, 429, 458, 494,
  300. 522, 522, 522, 522, 522, 522, 522, 522, 522,
  301. };
  302. static const int32_t hd_quantize_intervals_MLF[33] = {
  303. -21236, 21236, 63830, 106798, 150386, 194832, 240376, 287258,
  304. 335726, 386034, 438460, 493308, 550924, 611696, 676082, 744626,
  305. 817986, 896968, 982580, 1076118, 1179278, 1294344, 1424504, 1574386,
  306. 1751090, 1966260, 2240868, 2617662, 3196432, 4176450, 5658260, 7671068,
  307. 10380372,
  308. };
  309. static const int32_t hd_invert_quantize_dither_factors_MLF[33] = {
  310. 21236, 21236, 21360, 21608, 21978, 22468, 23076, 23806,
  311. 24660, 25648, 26778, 28070, 29544, 31228, 33158, 35386,
  312. 37974, 41008, 44606, 48934, 54226, 60840, 69320, 80564,
  313. 96140, 119032, 155576, 221218, 357552, 622468, 859344, 1153464, 1555840,
  314. };
  315. static const int32_t hd_quantize_dither_factors_MLF[32] = {
  316. 0, 31, 62, 93, 123, 152, 183, 214,
  317. 247, 283, 323, 369, 421, 483, 557, 647,
  318. 759, 900, 1082, 1323, 1654, 2120, 2811, 3894,
  319. 5723, 9136, 16411, 34084, 66229, 59219, 73530, 100594,
  320. };
  321. static const int16_t hd_quantize_factor_select_offset_MLF[33] = {
  322. 0, -21, -16, -12, -7, -2, 3, 8,
  323. 13, 19, 24, 30, 36, 43, 50, 57,
  324. 65, 74, 83, 93, 104, 117, 131, 147,
  325. 166, 189, 219, 259, 322, 427, 521, 521, 521,
  326. };
  327. static const int32_t hd_quantize_intervals_MHF[9] = {
  328. -95044, 95044, 295844, 528780, 821332, 1226438, 1890540, 3344850, 6450664,
  329. };
  330. static const int32_t hd_invert_quantize_dither_factors_MHF[9] = {
  331. 95044, 95044, 105754, 127180, 165372, 39736, 424366, 1029946, 2075866,
  332. };
  333. static const int32_t hd_quantize_dither_factors_MHF[8] = {
  334. 0, 2678, 5357, 9548, -31409, 96158, 151395, 261480,
  335. };
  336. static const int16_t hd_quantize_factor_select_offset_MHF[9] = {
  337. 0, -17, 5, 30, 62, 105, 177, 334, 518,
  338. };
  339. static const int32_t hd_quantize_intervals_HF[17] = {
  340. -45754, 45754, 138496, 234896, 337336, 448310, 570738, 708380,
  341. 866534, 1053262, 1281958, 1577438, 1993050, 2665984, 3900982, 5902844,
  342. 8897462,
  343. };
  344. static const int32_t hd_invert_quantize_dither_factors_HF[17] = {
  345. 45754, 45754, 46988, 49412, 53026, 57950, 64478, 73164,
  346. 84988, 101740, 126958, 168522, 247092, 425842, 809154, 1192708, 1801910,
  347. };
  348. static const int32_t hd_quantize_dither_factors_HF[16] = {
  349. 0, 309, 606, 904, 1231, 1632, 2172, 2956,
  350. 4188, 6305, 10391, 19643, 44688, 95828, 95889, 152301,
  351. };
  352. static const int16_t hd_quantize_factor_select_offset_HF[17] = {
  353. 0, -18, -8, 2, 13, 25, 38, 53,
  354. 70, 90, 115, 147, 192, 264, 398, 521, 521,
  355. };
  356. typedef const struct {
  357. const int32_t *quantize_intervals;
  358. const int32_t *invert_quantize_dither_factors;
  359. const int32_t *quantize_dither_factors;
  360. const int16_t *quantize_factor_select_offset;
  361. int tables_size;
  362. int32_t factor_max;
  363. int32_t prediction_order;
  364. } ConstTables;
  365. static ConstTables tables[2][NB_SUBBANDS] = {
  366. {
  367. [LF] = { quantize_intervals_LF,
  368. invert_quantize_dither_factors_LF,
  369. quantize_dither_factors_LF,
  370. quantize_factor_select_offset_LF,
  371. FF_ARRAY_ELEMS(quantize_intervals_LF),
  372. 0x11FF, 24 },
  373. [MLF] = { quantize_intervals_MLF,
  374. invert_quantize_dither_factors_MLF,
  375. quantize_dither_factors_MLF,
  376. quantize_factor_select_offset_MLF,
  377. FF_ARRAY_ELEMS(quantize_intervals_MLF),
  378. 0x14FF, 12 },
  379. [MHF] = { quantize_intervals_MHF,
  380. invert_quantize_dither_factors_MHF,
  381. quantize_dither_factors_MHF,
  382. quantize_factor_select_offset_MHF,
  383. FF_ARRAY_ELEMS(quantize_intervals_MHF),
  384. 0x16FF, 6 },
  385. [HF] = { quantize_intervals_HF,
  386. invert_quantize_dither_factors_HF,
  387. quantize_dither_factors_HF,
  388. quantize_factor_select_offset_HF,
  389. FF_ARRAY_ELEMS(quantize_intervals_HF),
  390. 0x15FF, 12 },
  391. },
  392. {
  393. [LF] = { hd_quantize_intervals_LF,
  394. hd_invert_quantize_dither_factors_LF,
  395. hd_quantize_dither_factors_LF,
  396. hd_quantize_factor_select_offset_LF,
  397. FF_ARRAY_ELEMS(hd_quantize_intervals_LF),
  398. 0x11FF, 24 },
  399. [MLF] = { hd_quantize_intervals_MLF,
  400. hd_invert_quantize_dither_factors_MLF,
  401. hd_quantize_dither_factors_MLF,
  402. hd_quantize_factor_select_offset_MLF,
  403. FF_ARRAY_ELEMS(hd_quantize_intervals_MLF),
  404. 0x14FF, 12 },
  405. [MHF] = { hd_quantize_intervals_MHF,
  406. hd_invert_quantize_dither_factors_MHF,
  407. hd_quantize_dither_factors_MHF,
  408. hd_quantize_factor_select_offset_MHF,
  409. FF_ARRAY_ELEMS(hd_quantize_intervals_MHF),
  410. 0x16FF, 6 },
  411. [HF] = { hd_quantize_intervals_HF,
  412. hd_invert_quantize_dither_factors_HF,
  413. hd_quantize_dither_factors_HF,
  414. hd_quantize_factor_select_offset_HF,
  415. FF_ARRAY_ELEMS(hd_quantize_intervals_HF),
  416. 0x15FF, 12 },
  417. }
  418. };
  419. static const int16_t quantization_factors[32] = {
  420. 2048, 2093, 2139, 2186, 2233, 2282, 2332, 2383,
  421. 2435, 2489, 2543, 2599, 2656, 2714, 2774, 2834,
  422. 2896, 2960, 3025, 3091, 3158, 3228, 3298, 3371,
  423. 3444, 3520, 3597, 3676, 3756, 3838, 3922, 4008,
  424. };
  425. /* Rounded right shift with optionnal clipping */
  426. #define RSHIFT_SIZE(size) \
  427. av_always_inline \
  428. static int##size##_t rshift##size(int##size##_t value, int shift) \
  429. { \
  430. int##size##_t rounding = (int##size##_t)1 << (shift - 1); \
  431. int##size##_t mask = ((int##size##_t)1 << (shift + 1)) - 1; \
  432. return ((value + rounding) >> shift) - ((value & mask) == rounding); \
  433. } \
  434. av_always_inline \
  435. static int##size##_t rshift##size##_clip24(int##size##_t value, int shift) \
  436. { \
  437. return av_clip_intp2(rshift##size(value, shift), 23); \
  438. }
  439. RSHIFT_SIZE(32)
  440. RSHIFT_SIZE(64)
  441. av_always_inline
  442. static void aptx_update_codeword_history(Channel *channel)
  443. {
  444. int32_t cw = ((channel->quantize[0].quantized_sample & 3) << 0) +
  445. ((channel->quantize[1].quantized_sample & 2) << 1) +
  446. ((channel->quantize[2].quantized_sample & 1) << 3);
  447. channel->codeword_history = (cw << 8) + (channel->codeword_history << 4);
  448. }
  449. static void aptx_generate_dither(Channel *channel)
  450. {
  451. int subband;
  452. int64_t m;
  453. int32_t d;
  454. aptx_update_codeword_history(channel);
  455. m = (int64_t)5184443 * (channel->codeword_history >> 7);
  456. d = (m << 2) + (m >> 22);
  457. for (subband = 0; subband < NB_SUBBANDS; subband++)
  458. channel->dither[subband] = d << (23 - 5*subband);
  459. channel->dither_parity = (d >> 25) & 1;
  460. }
  461. /*
  462. * Convolution filter coefficients for the outer QMF of the QMF tree.
  463. * The 2 sets are a mirror of each other.
  464. */
  465. static const int32_t aptx_qmf_outer_coeffs[NB_FILTERS][FILTER_TAPS] = {
  466. {
  467. 730, -413, -9611, 43626, -121026, 269973, -585547, 2801966,
  468. 697128, -160481, 27611, 8478, -10043, 3511, 688, -897,
  469. },
  470. {
  471. -897, 688, 3511, -10043, 8478, 27611, -160481, 697128,
  472. 2801966, -585547, 269973, -121026, 43626, -9611, -413, 730,
  473. },
  474. };
  475. /*
  476. * Convolution filter coefficients for the inner QMF of the QMF tree.
  477. * The 2 sets are a mirror of each other.
  478. */
  479. static const int32_t aptx_qmf_inner_coeffs[NB_FILTERS][FILTER_TAPS] = {
  480. {
  481. 1033, -584, -13592, 61697, -171156, 381799, -828088, 3962579,
  482. 985888, -226954, 39048, 11990, -14203, 4966, 973, -1268,
  483. },
  484. {
  485. -1268, 973, 4966, -14203, 11990, 39048, -226954, 985888,
  486. 3962579, -828088, 381799, -171156, 61697, -13592, -584, 1033,
  487. },
  488. };
  489. /*
  490. * Push one sample into a circular signal buffer.
  491. */
  492. av_always_inline
  493. static void aptx_qmf_filter_signal_push(FilterSignal *signal, int32_t sample)
  494. {
  495. signal->buffer[signal->pos ] = sample;
  496. signal->buffer[signal->pos+FILTER_TAPS] = sample;
  497. signal->pos = (signal->pos + 1) & (FILTER_TAPS - 1);
  498. }
  499. /*
  500. * Compute the convolution of the signal with the coefficients, and reduce
  501. * to 24 bits by applying the specified right shifting.
  502. */
  503. av_always_inline
  504. static int32_t aptx_qmf_convolution(FilterSignal *signal,
  505. const int32_t coeffs[FILTER_TAPS],
  506. int shift)
  507. {
  508. int32_t *sig = &signal->buffer[signal->pos];
  509. int64_t e = 0;
  510. int i;
  511. for (i = 0; i < FILTER_TAPS; i++)
  512. e += MUL64(sig[i], coeffs[i]);
  513. return rshift64_clip24(e, shift);
  514. }
  515. /*
  516. * Half-band QMF analysis filter realized with a polyphase FIR filter.
  517. * Split into 2 subbands and downsample by 2.
  518. * So for each pair of samples that goes in, one sample goes out,
  519. * split into 2 separate subbands.
  520. */
  521. av_always_inline
  522. static void aptx_qmf_polyphase_analysis(FilterSignal signal[NB_FILTERS],
  523. const int32_t coeffs[NB_FILTERS][FILTER_TAPS],
  524. int shift,
  525. int32_t samples[NB_FILTERS],
  526. int32_t *low_subband_output,
  527. int32_t *high_subband_output)
  528. {
  529. int32_t subbands[NB_FILTERS];
  530. int i;
  531. for (i = 0; i < NB_FILTERS; i++) {
  532. aptx_qmf_filter_signal_push(&signal[i], samples[NB_FILTERS-1-i]);
  533. subbands[i] = aptx_qmf_convolution(&signal[i], coeffs[i], shift);
  534. }
  535. *low_subband_output = av_clip_intp2(subbands[0] + subbands[1], 23);
  536. *high_subband_output = av_clip_intp2(subbands[0] - subbands[1], 23);
  537. }
  538. /*
  539. * Two stage QMF analysis tree.
  540. * Split 4 input samples into 4 subbands and downsample by 4.
  541. * So for each group of 4 samples that goes in, one sample goes out,
  542. * split into 4 separate subbands.
  543. */
  544. static void aptx_qmf_tree_analysis(QMFAnalysis *qmf,
  545. int32_t samples[4],
  546. int32_t subband_samples[4])
  547. {
  548. int32_t intermediate_samples[4];
  549. int i;
  550. /* Split 4 input samples into 2 intermediate subbands downsampled to 2 samples */
  551. for (i = 0; i < 2; i++)
  552. aptx_qmf_polyphase_analysis(qmf->outer_filter_signal,
  553. aptx_qmf_outer_coeffs, 23,
  554. &samples[2*i],
  555. &intermediate_samples[0+i],
  556. &intermediate_samples[2+i]);
  557. /* Split 2 intermediate subband samples into 4 final subbands downsampled to 1 sample */
  558. for (i = 0; i < 2; i++)
  559. aptx_qmf_polyphase_analysis(qmf->inner_filter_signal[i],
  560. aptx_qmf_inner_coeffs, 23,
  561. &intermediate_samples[2*i],
  562. &subband_samples[2*i+0],
  563. &subband_samples[2*i+1]);
  564. }
  565. /*
  566. * Half-band QMF synthesis filter realized with a polyphase FIR filter.
  567. * Join 2 subbands and upsample by 2.
  568. * So for each 2 subbands sample that goes in, a pair of samples goes out.
  569. */
  570. av_always_inline
  571. static void aptx_qmf_polyphase_synthesis(FilterSignal signal[NB_FILTERS],
  572. const int32_t coeffs[NB_FILTERS][FILTER_TAPS],
  573. int shift,
  574. int32_t low_subband_input,
  575. int32_t high_subband_input,
  576. int32_t samples[NB_FILTERS])
  577. {
  578. int32_t subbands[NB_FILTERS];
  579. int i;
  580. subbands[0] = low_subband_input + high_subband_input;
  581. subbands[1] = low_subband_input - high_subband_input;
  582. for (i = 0; i < NB_FILTERS; i++) {
  583. aptx_qmf_filter_signal_push(&signal[i], subbands[1-i]);
  584. samples[i] = aptx_qmf_convolution(&signal[i], coeffs[i], shift);
  585. }
  586. }
  587. /*
  588. * Two stage QMF synthesis tree.
  589. * Join 4 subbands and upsample by 4.
  590. * So for each 4 subbands sample that goes in, a group of 4 samples goes out.
  591. */
  592. static void aptx_qmf_tree_synthesis(QMFAnalysis *qmf,
  593. int32_t subband_samples[4],
  594. int32_t samples[4])
  595. {
  596. int32_t intermediate_samples[4];
  597. int i;
  598. /* Join 4 subbands into 2 intermediate subbands upsampled to 2 samples. */
  599. for (i = 0; i < 2; i++)
  600. aptx_qmf_polyphase_synthesis(qmf->inner_filter_signal[i],
  601. aptx_qmf_inner_coeffs, 22,
  602. subband_samples[2*i+0],
  603. subband_samples[2*i+1],
  604. &intermediate_samples[2*i]);
  605. /* Join 2 samples from intermediate subbands upsampled to 4 samples. */
  606. for (i = 0; i < 2; i++)
  607. aptx_qmf_polyphase_synthesis(qmf->outer_filter_signal,
  608. aptx_qmf_outer_coeffs, 21,
  609. intermediate_samples[0+i],
  610. intermediate_samples[2+i],
  611. &samples[2*i]);
  612. }
  613. av_always_inline
  614. static int32_t aptx_bin_search(int32_t value, int32_t factor,
  615. const int32_t *intervals, int32_t nb_intervals)
  616. {
  617. int32_t idx = 0;
  618. int i;
  619. for (i = nb_intervals >> 1; i > 0; i >>= 1)
  620. if (MUL64(factor, intervals[idx + i]) <= ((int64_t)value << 24))
  621. idx += i;
  622. return idx;
  623. }
  624. static void aptx_quantize_difference(Quantize *quantize,
  625. int32_t sample_difference,
  626. int32_t dither,
  627. int32_t quantization_factor,
  628. ConstTables *tables)
  629. {
  630. const int32_t *intervals = tables->quantize_intervals;
  631. int32_t quantized_sample, dithered_sample, parity_change;
  632. int32_t d, mean, interval, inv, sample_difference_abs;
  633. int64_t error;
  634. sample_difference_abs = FFABS(sample_difference);
  635. sample_difference_abs = FFMIN(sample_difference_abs, (1 << 23) - 1);
  636. quantized_sample = aptx_bin_search(sample_difference_abs >> 4,
  637. quantization_factor,
  638. intervals, tables->tables_size);
  639. d = rshift32_clip24(MULH(dither, dither), 7) - (1 << 23);
  640. d = rshift64(MUL64(d, tables->quantize_dither_factors[quantized_sample]), 23);
  641. intervals += quantized_sample;
  642. mean = (intervals[1] + intervals[0]) / 2;
  643. interval = (intervals[1] - intervals[0]) * (-(sample_difference < 0) | 1);
  644. dithered_sample = rshift64_clip24(MUL64(dither, interval) + ((int64_t)av_clip_intp2(mean + d, 23) << 32), 32);
  645. error = ((int64_t)sample_difference_abs << 20) - MUL64(dithered_sample, quantization_factor);
  646. quantize->error = FFABS(rshift64(error, 23));
  647. parity_change = quantized_sample;
  648. if (error < 0)
  649. quantized_sample--;
  650. else
  651. parity_change--;
  652. inv = -(sample_difference < 0);
  653. quantize->quantized_sample = quantized_sample ^ inv;
  654. quantize->quantized_sample_parity_change = parity_change ^ inv;
  655. }
  656. static void aptx_encode_channel(Channel *channel, int32_t samples[4], int hd)
  657. {
  658. int32_t subband_samples[4];
  659. int subband;
  660. aptx_qmf_tree_analysis(&channel->qmf, samples, subband_samples);
  661. aptx_generate_dither(channel);
  662. for (subband = 0; subband < NB_SUBBANDS; subband++) {
  663. int32_t diff = av_clip_intp2(subband_samples[subband] - channel->prediction[subband].predicted_sample, 23);
  664. aptx_quantize_difference(&channel->quantize[subband], diff,
  665. channel->dither[subband],
  666. channel->invert_quantize[subband].quantization_factor,
  667. &tables[hd][subband]);
  668. }
  669. }
  670. static void aptx_decode_channel(Channel *channel, int32_t samples[4])
  671. {
  672. int32_t subband_samples[4];
  673. int subband;
  674. for (subband = 0; subband < NB_SUBBANDS; subband++)
  675. subband_samples[subband] = channel->prediction[subband].previous_reconstructed_sample;
  676. aptx_qmf_tree_synthesis(&channel->qmf, subband_samples, samples);
  677. }
  678. static void aptx_invert_quantization(InvertQuantize *invert_quantize,
  679. int32_t quantized_sample, int32_t dither,
  680. ConstTables *tables)
  681. {
  682. int32_t qr, idx, shift, factor_select;
  683. idx = (quantized_sample ^ -(quantized_sample < 0)) + 1;
  684. qr = tables->quantize_intervals[idx] / 2;
  685. if (quantized_sample < 0)
  686. qr = -qr;
  687. qr = rshift64_clip24(((int64_t)qr<<32) + MUL64(dither, tables->invert_quantize_dither_factors[idx]), 32);
  688. invert_quantize->reconstructed_difference = MUL64(invert_quantize->quantization_factor, qr) >> 19;
  689. /* update factor_select */
  690. factor_select = 32620 * invert_quantize->factor_select;
  691. factor_select = rshift32(factor_select + (tables->quantize_factor_select_offset[idx] << 15), 15);
  692. invert_quantize->factor_select = av_clip(factor_select, 0, tables->factor_max);
  693. /* update quantization factor */
  694. idx = (invert_quantize->factor_select & 0xFF) >> 3;
  695. shift = (tables->factor_max - invert_quantize->factor_select) >> 8;
  696. invert_quantize->quantization_factor = (quantization_factors[idx] << 11) >> shift;
  697. }
  698. static int32_t *aptx_reconstructed_differences_update(Prediction *prediction,
  699. int32_t reconstructed_difference,
  700. int order)
  701. {
  702. int32_t *rd1 = prediction->reconstructed_differences, *rd2 = rd1 + order;
  703. int p = prediction->pos;
  704. rd1[p] = rd2[p];
  705. prediction->pos = p = (p + 1) % order;
  706. rd2[p] = reconstructed_difference;
  707. return &rd2[p];
  708. }
  709. static void aptx_prediction_filtering(Prediction *prediction,
  710. int32_t reconstructed_difference,
  711. int order)
  712. {
  713. int32_t reconstructed_sample, predictor, srd0;
  714. int32_t *reconstructed_differences;
  715. int64_t predicted_difference = 0;
  716. int i;
  717. reconstructed_sample = av_clip_intp2(reconstructed_difference + prediction->predicted_sample, 23);
  718. predictor = av_clip_intp2((MUL64(prediction->s_weight[0], prediction->previous_reconstructed_sample)
  719. + MUL64(prediction->s_weight[1], reconstructed_sample)) >> 22, 23);
  720. prediction->previous_reconstructed_sample = reconstructed_sample;
  721. reconstructed_differences = aptx_reconstructed_differences_update(prediction, reconstructed_difference, order);
  722. srd0 = FFDIFFSIGN(reconstructed_difference, 0) << 23;
  723. for (i = 0; i < order; i++) {
  724. int32_t srd = FF_SIGNBIT(reconstructed_differences[-i-1]) | 1;
  725. prediction->d_weight[i] -= rshift32(prediction->d_weight[i] - srd*srd0, 8);
  726. predicted_difference += MUL64(reconstructed_differences[-i], prediction->d_weight[i]);
  727. }
  728. prediction->predicted_difference = av_clip_intp2(predicted_difference >> 22, 23);
  729. prediction->predicted_sample = av_clip_intp2(predictor + prediction->predicted_difference, 23);
  730. }
  731. static void aptx_process_subband(InvertQuantize *invert_quantize,
  732. Prediction *prediction,
  733. int32_t quantized_sample, int32_t dither,
  734. ConstTables *tables)
  735. {
  736. int32_t sign, same_sign[2], weight[2], sw1, range;
  737. aptx_invert_quantization(invert_quantize, quantized_sample, dither, tables);
  738. sign = FFDIFFSIGN(invert_quantize->reconstructed_difference,
  739. -prediction->predicted_difference);
  740. same_sign[0] = sign * prediction->prev_sign[0];
  741. same_sign[1] = sign * prediction->prev_sign[1];
  742. prediction->prev_sign[0] = prediction->prev_sign[1];
  743. prediction->prev_sign[1] = sign | 1;
  744. range = 0x100000;
  745. sw1 = rshift32(-same_sign[1] * prediction->s_weight[1], 1);
  746. sw1 = (av_clip(sw1, -range, range) & ~0xF) << 4;
  747. range = 0x300000;
  748. weight[0] = 254 * prediction->s_weight[0] + 0x800000*same_sign[0] + sw1;
  749. prediction->s_weight[0] = av_clip(rshift32(weight[0], 8), -range, range);
  750. range = 0x3C0000 - prediction->s_weight[0];
  751. weight[1] = 255 * prediction->s_weight[1] + 0xC00000*same_sign[1];
  752. prediction->s_weight[1] = av_clip(rshift32(weight[1], 8), -range, range);
  753. aptx_prediction_filtering(prediction,
  754. invert_quantize->reconstructed_difference,
  755. tables->prediction_order);
  756. }
  757. static void aptx_invert_quantize_and_prediction(Channel *channel, int hd)
  758. {
  759. int subband;
  760. for (subband = 0; subband < NB_SUBBANDS; subband++)
  761. aptx_process_subband(&channel->invert_quantize[subband],
  762. &channel->prediction[subband],
  763. channel->quantize[subband].quantized_sample,
  764. channel->dither[subband],
  765. &tables[hd][subband]);
  766. }
  767. static int32_t aptx_quantized_parity(Channel *channel)
  768. {
  769. int32_t parity = channel->dither_parity;
  770. int subband;
  771. for (subband = 0; subband < NB_SUBBANDS; subband++)
  772. parity ^= channel->quantize[subband].quantized_sample;
  773. return parity & 1;
  774. }
  775. /* For each sample, ensure that the parity of all subbands of all channels
  776. * is 0 except once every 8 samples where the parity is forced to 1. */
  777. static int aptx_check_parity(Channel channels[NB_CHANNELS], int32_t *idx)
  778. {
  779. int32_t parity = aptx_quantized_parity(&channels[LEFT])
  780. ^ aptx_quantized_parity(&channels[RIGHT]);
  781. int eighth = *idx == 7;
  782. *idx = (*idx + 1) & 7;
  783. return parity ^ eighth;
  784. }
  785. static void aptx_insert_sync(Channel channels[NB_CHANNELS], int32_t *idx)
  786. {
  787. if (aptx_check_parity(channels, idx)) {
  788. int i;
  789. Channel *c;
  790. static const int map[] = { 1, 2, 0, 3 };
  791. Quantize *min = &channels[NB_CHANNELS-1].quantize[map[0]];
  792. for (c = &channels[NB_CHANNELS-1]; c >= channels; c--)
  793. for (i = 0; i < NB_SUBBANDS; i++)
  794. if (c->quantize[map[i]].error < min->error)
  795. min = &c->quantize[map[i]];
  796. /* Forcing the desired parity is done by offsetting by 1 the quantized
  797. * sample from the subband featuring the smallest quantization error. */
  798. min->quantized_sample = min->quantized_sample_parity_change;
  799. }
  800. }
  801. static uint16_t aptx_pack_codeword(Channel *channel)
  802. {
  803. int32_t parity = aptx_quantized_parity(channel);
  804. return (((channel->quantize[3].quantized_sample & 0x06) | parity) << 13)
  805. | (((channel->quantize[2].quantized_sample & 0x03) ) << 11)
  806. | (((channel->quantize[1].quantized_sample & 0x0F) ) << 7)
  807. | (((channel->quantize[0].quantized_sample & 0x7F) ) << 0);
  808. }
  809. static uint32_t aptxhd_pack_codeword(Channel *channel)
  810. {
  811. int32_t parity = aptx_quantized_parity(channel);
  812. return (((channel->quantize[3].quantized_sample & 0x01E) | parity) << 19)
  813. | (((channel->quantize[2].quantized_sample & 0x00F) ) << 15)
  814. | (((channel->quantize[1].quantized_sample & 0x03F) ) << 9)
  815. | (((channel->quantize[0].quantized_sample & 0x1FF) ) << 0);
  816. }
  817. static void aptx_unpack_codeword(Channel *channel, uint16_t codeword)
  818. {
  819. channel->quantize[0].quantized_sample = sign_extend(codeword >> 0, 7);
  820. channel->quantize[1].quantized_sample = sign_extend(codeword >> 7, 4);
  821. channel->quantize[2].quantized_sample = sign_extend(codeword >> 11, 2);
  822. channel->quantize[3].quantized_sample = sign_extend(codeword >> 13, 3);
  823. channel->quantize[3].quantized_sample = (channel->quantize[3].quantized_sample & ~1)
  824. | aptx_quantized_parity(channel);
  825. }
  826. static void aptxhd_unpack_codeword(Channel *channel, uint32_t codeword)
  827. {
  828. channel->quantize[0].quantized_sample = sign_extend(codeword >> 0, 9);
  829. channel->quantize[1].quantized_sample = sign_extend(codeword >> 9, 6);
  830. channel->quantize[2].quantized_sample = sign_extend(codeword >> 15, 4);
  831. channel->quantize[3].quantized_sample = sign_extend(codeword >> 19, 5);
  832. channel->quantize[3].quantized_sample = (channel->quantize[3].quantized_sample & ~1)
  833. | aptx_quantized_parity(channel);
  834. }
  835. static void aptx_encode_samples(AptXContext *ctx,
  836. int32_t samples[NB_CHANNELS][4],
  837. uint8_t *output)
  838. {
  839. int channel;
  840. for (channel = 0; channel < NB_CHANNELS; channel++)
  841. aptx_encode_channel(&ctx->channels[channel], samples[channel], ctx->hd);
  842. aptx_insert_sync(ctx->channels, &ctx->sync_idx);
  843. for (channel = 0; channel < NB_CHANNELS; channel++) {
  844. aptx_invert_quantize_and_prediction(&ctx->channels[channel], ctx->hd);
  845. if (ctx->hd)
  846. AV_WB24(output + 3*channel,
  847. aptxhd_pack_codeword(&ctx->channels[channel]));
  848. else
  849. AV_WB16(output + 2*channel,
  850. aptx_pack_codeword(&ctx->channels[channel]));
  851. }
  852. }
  853. static int aptx_decode_samples(AptXContext *ctx,
  854. const uint8_t *input,
  855. int32_t samples[NB_CHANNELS][4])
  856. {
  857. int channel, ret;
  858. for (channel = 0; channel < NB_CHANNELS; channel++) {
  859. aptx_generate_dither(&ctx->channels[channel]);
  860. if (ctx->hd)
  861. aptxhd_unpack_codeword(&ctx->channels[channel],
  862. AV_RB24(input + 3*channel));
  863. else
  864. aptx_unpack_codeword(&ctx->channels[channel],
  865. AV_RB16(input + 2*channel));
  866. aptx_invert_quantize_and_prediction(&ctx->channels[channel], ctx->hd);
  867. }
  868. ret = aptx_check_parity(ctx->channels, &ctx->sync_idx);
  869. for (channel = 0; channel < NB_CHANNELS; channel++)
  870. aptx_decode_channel(&ctx->channels[channel], samples[channel]);
  871. return ret;
  872. }
  873. static av_cold int aptx_init(AVCodecContext *avctx)
  874. {
  875. AptXContext *s = avctx->priv_data;
  876. int chan, subband;
  877. s->hd = avctx->codec->id == AV_CODEC_ID_APTX_HD;
  878. s->block_size = s->hd ? 6 : 4;
  879. if (avctx->frame_size == 0)
  880. avctx->frame_size = 256 * s->block_size;
  881. if (avctx->frame_size % s->block_size) {
  882. av_log(avctx, AV_LOG_ERROR,
  883. "Frame size must be a multiple of %d samples\n", s->block_size);
  884. return AVERROR(EINVAL);
  885. }
  886. for (chan = 0; chan < NB_CHANNELS; chan++) {
  887. Channel *channel = &s->channels[chan];
  888. for (subband = 0; subband < NB_SUBBANDS; subband++) {
  889. Prediction *prediction = &channel->prediction[subband];
  890. prediction->prev_sign[0] = 1;
  891. prediction->prev_sign[1] = 1;
  892. }
  893. }
  894. ff_af_queue_init(avctx, &s->afq);
  895. return 0;
  896. }
  897. static int aptx_decode_frame(AVCodecContext *avctx, void *data,
  898. int *got_frame_ptr, AVPacket *avpkt)
  899. {
  900. AptXContext *s = avctx->priv_data;
  901. AVFrame *frame = data;
  902. int pos, opos, channel, sample, ret;
  903. if (avpkt->size < s->block_size) {
  904. av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
  905. return AVERROR_INVALIDDATA;
  906. }
  907. /* get output buffer */
  908. frame->channels = NB_CHANNELS;
  909. frame->format = AV_SAMPLE_FMT_S32P;
  910. frame->nb_samples = 4 * avpkt->size / s->block_size;
  911. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  912. return ret;
  913. for (pos = 0, opos = 0; opos < frame->nb_samples; pos += s->block_size, opos += 4) {
  914. int32_t samples[NB_CHANNELS][4];
  915. if (aptx_decode_samples(s, &avpkt->data[pos], samples)) {
  916. av_log(avctx, AV_LOG_ERROR, "Synchronization error\n");
  917. return AVERROR_INVALIDDATA;
  918. }
  919. for (channel = 0; channel < NB_CHANNELS; channel++)
  920. for (sample = 0; sample < 4; sample++)
  921. AV_WN32A(&frame->data[channel][4*(opos+sample)],
  922. samples[channel][sample] << 8);
  923. }
  924. *got_frame_ptr = 1;
  925. return s->block_size * frame->nb_samples / 4;
  926. }
  927. static int aptx_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  928. const AVFrame *frame, int *got_packet_ptr)
  929. {
  930. AptXContext *s = avctx->priv_data;
  931. int pos, ipos, channel, sample, output_size, ret;
  932. if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
  933. return ret;
  934. output_size = s->block_size * frame->nb_samples/4;
  935. if ((ret = ff_alloc_packet2(avctx, avpkt, output_size, 0)) < 0)
  936. return ret;
  937. for (pos = 0, ipos = 0; pos < output_size; pos += s->block_size, ipos += 4) {
  938. int32_t samples[NB_CHANNELS][4];
  939. for (channel = 0; channel < NB_CHANNELS; channel++)
  940. for (sample = 0; sample < 4; sample++)
  941. samples[channel][sample] = (int32_t)AV_RN32A(&frame->data[channel][4*(ipos+sample)]) >> 8;
  942. aptx_encode_samples(s, samples, avpkt->data + pos);
  943. }
  944. ff_af_queue_remove(&s->afq, frame->nb_samples, &avpkt->pts, &avpkt->duration);
  945. *got_packet_ptr = 1;
  946. return 0;
  947. }
  948. static av_cold int aptx_close(AVCodecContext *avctx)
  949. {
  950. AptXContext *s = avctx->priv_data;
  951. ff_af_queue_close(&s->afq);
  952. return 0;
  953. }
  954. #if CONFIG_APTX_DECODER
  955. AVCodec ff_aptx_decoder = {
  956. .name = "aptx",
  957. .long_name = NULL_IF_CONFIG_SMALL("aptX (Audio Processing Technology for Bluetooth)"),
  958. .type = AVMEDIA_TYPE_AUDIO,
  959. .id = AV_CODEC_ID_APTX,
  960. .priv_data_size = sizeof(AptXContext),
  961. .init = aptx_init,
  962. .decode = aptx_decode_frame,
  963. .close = aptx_close,
  964. .capabilities = AV_CODEC_CAP_DR1,
  965. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  966. .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_STEREO, 0},
  967. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P,
  968. AV_SAMPLE_FMT_NONE },
  969. };
  970. #endif
  971. #if CONFIG_APTX_HD_DECODER
  972. AVCodec ff_aptx_hd_decoder = {
  973. .name = "aptx_hd",
  974. .long_name = NULL_IF_CONFIG_SMALL("aptX HD (Audio Processing Technology for Bluetooth)"),
  975. .type = AVMEDIA_TYPE_AUDIO,
  976. .id = AV_CODEC_ID_APTX_HD,
  977. .priv_data_size = sizeof(AptXContext),
  978. .init = aptx_init,
  979. .decode = aptx_decode_frame,
  980. .close = aptx_close,
  981. .capabilities = AV_CODEC_CAP_DR1,
  982. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  983. .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_STEREO, 0},
  984. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P,
  985. AV_SAMPLE_FMT_NONE },
  986. };
  987. #endif
  988. #if CONFIG_APTX_ENCODER
  989. AVCodec ff_aptx_encoder = {
  990. .name = "aptx",
  991. .long_name = NULL_IF_CONFIG_SMALL("aptX (Audio Processing Technology for Bluetooth)"),
  992. .type = AVMEDIA_TYPE_AUDIO,
  993. .id = AV_CODEC_ID_APTX,
  994. .priv_data_size = sizeof(AptXContext),
  995. .init = aptx_init,
  996. .encode2 = aptx_encode_frame,
  997. .close = aptx_close,
  998. .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
  999. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  1000. .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_STEREO, 0},
  1001. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P,
  1002. AV_SAMPLE_FMT_NONE },
  1003. .supported_samplerates = (const int[]) {8000, 16000, 24000, 32000, 44100, 48000, 0},
  1004. };
  1005. #endif
  1006. #if CONFIG_APTX_HD_ENCODER
  1007. AVCodec ff_aptx_hd_encoder = {
  1008. .name = "aptx_hd",
  1009. .long_name = NULL_IF_CONFIG_SMALL("aptX HD (Audio Processing Technology for Bluetooth)"),
  1010. .type = AVMEDIA_TYPE_AUDIO,
  1011. .id = AV_CODEC_ID_APTX_HD,
  1012. .priv_data_size = sizeof(AptXContext),
  1013. .init = aptx_init,
  1014. .encode2 = aptx_encode_frame,
  1015. .close = aptx_close,
  1016. .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
  1017. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  1018. .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_STEREO, 0},
  1019. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P,
  1020. AV_SAMPLE_FMT_NONE },
  1021. .supported_samplerates = (const int[]) {8000, 16000, 24000, 32000, 44100, 48000, 0},
  1022. };
  1023. #endif