Debian Codegraph @

openaptx.c

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