/src/aac/libAACdec/src/usacdec_ace_d4t64.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* ----------------------------------------------------------------------------- |
2 | | Software License for The Fraunhofer FDK AAC Codec Library for Android |
3 | | |
4 | | © Copyright 1995 - 2018 Fraunhofer-Gesellschaft zur Förderung der angewandten |
5 | | Forschung e.V. All rights reserved. |
6 | | |
7 | | 1. INTRODUCTION |
8 | | The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software |
9 | | that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding |
10 | | scheme for digital audio. This FDK AAC Codec software is intended to be used on |
11 | | a wide variety of Android devices. |
12 | | |
13 | | AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient |
14 | | general perceptual audio codecs. AAC-ELD is considered the best-performing |
15 | | full-bandwidth communications codec by independent studies and is widely |
16 | | deployed. AAC has been standardized by ISO and IEC as part of the MPEG |
17 | | specifications. |
18 | | |
19 | | Patent licenses for necessary patent claims for the FDK AAC Codec (including |
20 | | those of Fraunhofer) may be obtained through Via Licensing |
21 | | (www.vialicensing.com) or through the respective patent owners individually for |
22 | | the purpose of encoding or decoding bit streams in products that are compliant |
23 | | with the ISO/IEC MPEG audio standards. Please note that most manufacturers of |
24 | | Android devices already license these patent claims through Via Licensing or |
25 | | directly from the patent owners, and therefore FDK AAC Codec software may |
26 | | already be covered under those patent licenses when it is used for those |
27 | | licensed purposes only. |
28 | | |
29 | | Commercially-licensed AAC software libraries, including floating-point versions |
30 | | with enhanced sound quality, are also available from Fraunhofer. Users are |
31 | | encouraged to check the Fraunhofer website for additional applications |
32 | | information and documentation. |
33 | | |
34 | | 2. COPYRIGHT LICENSE |
35 | | |
36 | | Redistribution and use in source and binary forms, with or without modification, |
37 | | are permitted without payment of copyright license fees provided that you |
38 | | satisfy the following conditions: |
39 | | |
40 | | You must retain the complete text of this software license in redistributions of |
41 | | the FDK AAC Codec or your modifications thereto in source code form. |
42 | | |
43 | | You must retain the complete text of this software license in the documentation |
44 | | and/or other materials provided with redistributions of the FDK AAC Codec or |
45 | | your modifications thereto in binary form. You must make available free of |
46 | | charge copies of the complete source code of the FDK AAC Codec and your |
47 | | modifications thereto to recipients of copies in binary form. |
48 | | |
49 | | The name of Fraunhofer may not be used to endorse or promote products derived |
50 | | from this library without prior written permission. |
51 | | |
52 | | You may not charge copyright license fees for anyone to use, copy or distribute |
53 | | the FDK AAC Codec software or your modifications thereto. |
54 | | |
55 | | Your modified versions of the FDK AAC Codec must carry prominent notices stating |
56 | | that you changed the software and the date of any change. For modified versions |
57 | | of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android" |
58 | | must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK |
59 | | AAC Codec Library for Android." |
60 | | |
61 | | 3. NO PATENT LICENSE |
62 | | |
63 | | NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without |
64 | | limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE. |
65 | | Fraunhofer provides no warranty of patent non-infringement with respect to this |
66 | | software. |
67 | | |
68 | | You may use this FDK AAC Codec software or modifications thereto only for |
69 | | purposes that are authorized by appropriate patent licenses. |
70 | | |
71 | | 4. DISCLAIMER |
72 | | |
73 | | This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright |
74 | | holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, |
75 | | including but not limited to the implied warranties of merchantability and |
76 | | fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
77 | | CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, |
78 | | or consequential damages, including but not limited to procurement of substitute |
79 | | goods or services; loss of use, data, or profits, or business interruption, |
80 | | however caused and on any theory of liability, whether in contract, strict |
81 | | liability, or tort (including negligence), arising in any way out of the use of |
82 | | this software, even if advised of the possibility of such damage. |
83 | | |
84 | | 5. CONTACT INFORMATION |
85 | | |
86 | | Fraunhofer Institute for Integrated Circuits IIS |
87 | | Attention: Audio and Multimedia Departments - FDK AAC LL |
88 | | Am Wolfsmantel 33 |
89 | | 91058 Erlangen, Germany |
90 | | |
91 | | www.iis.fraunhofer.de/amm |
92 | | amm-info@iis.fraunhofer.de |
93 | | ----------------------------------------------------------------------------- */ |
94 | | |
95 | | /**************************** AAC decoder library ****************************** |
96 | | |
97 | | Author(s): |
98 | | |
99 | | Description: ACELP |
100 | | |
101 | | *******************************************************************************/ |
102 | | |
103 | | #include "usacdec_ace_d4t64.h" |
104 | | |
105 | 387k | #define L_SUBFR 64 /* Subframe size */ |
106 | | |
107 | | /* |
108 | | * D_ACELP_add_pulse |
109 | | * |
110 | | * Parameters: |
111 | | * pos I: position of pulse |
112 | | * nb_pulse I: number of pulses |
113 | | * track I: track |
114 | | * code O: fixed codebook |
115 | | * |
116 | | * Function: |
117 | | * Add pulses to fixed codebook |
118 | | * |
119 | | * Returns: |
120 | | * void |
121 | | */ |
122 | | static void D_ACELP_add_pulse(SHORT pos[], SHORT nb_pulse, SHORT track, |
123 | 1.39M | FIXP_COD code[]) { |
124 | 1.39M | SHORT i, k; |
125 | 3.67M | for (k = 0; k < nb_pulse; k++) { |
126 | | /* i = ((pos[k] & (16-1))*NB_TRACK) + track; */ |
127 | 2.28M | i = ((pos[k] & (16 - 1)) << 2) + track; |
128 | 2.28M | if ((pos[k] & 16) == 0) { |
129 | 1.43M | code[i] = code[i] + (FIXP_COD)(512 << (COD_BITS - FRACT_BITS)); |
130 | 1.43M | } else { |
131 | 849k | code[i] = code[i] - (FIXP_COD)(512 << (COD_BITS - FRACT_BITS)); |
132 | 849k | } |
133 | 2.28M | } |
134 | 1.39M | return; |
135 | 1.39M | } |
136 | | /* |
137 | | * D_ACELP_decode_1p_N1 |
138 | | * |
139 | | * Parameters: |
140 | | * index I: pulse index |
141 | | * N I: number of bits for position |
142 | | * offset I: offset |
143 | | * pos O: position of the pulse |
144 | | * |
145 | | * Function: |
146 | | * Decode 1 pulse with N+1 bits |
147 | | * |
148 | | * Returns: |
149 | | * void |
150 | | */ |
151 | | static void D_ACELP_decode_1p_N1(LONG index, SHORT N, SHORT offset, |
152 | 1.04M | SHORT pos[]) { |
153 | 1.04M | SHORT pos1; |
154 | 1.04M | LONG i, mask; |
155 | | |
156 | 1.04M | mask = ((1 << N) - 1); |
157 | | /* |
158 | | * Decode 1 pulse with N+1 bits |
159 | | */ |
160 | 1.04M | pos1 = (SHORT)((index & mask) + offset); |
161 | 1.04M | i = ((index >> N) & 1); |
162 | 1.04M | if (i == 1) { |
163 | 365k | pos1 += 16; |
164 | 365k | } |
165 | 1.04M | pos[0] = pos1; |
166 | 1.04M | return; |
167 | 1.04M | } |
168 | | /* |
169 | | * D_ACELP_decode_2p_2N1 |
170 | | * |
171 | | * Parameters: |
172 | | * index I: pulse index |
173 | | * N I: number of bits for position |
174 | | * offset I: offset |
175 | | * pos O: position of the pulse |
176 | | * |
177 | | * Function: |
178 | | * Decode 2 pulses with 2*N+1 bits |
179 | | * |
180 | | * Returns: |
181 | | * void |
182 | | */ |
183 | | static void D_ACELP_decode_2p_2N1(LONG index, SHORT N, SHORT offset, |
184 | 618k | SHORT pos[]) { |
185 | 618k | SHORT pos1, pos2; |
186 | 618k | LONG mask, i; |
187 | 618k | mask = ((1 << N) - 1); |
188 | | /* |
189 | | * Decode 2 pulses with 2*N+1 bits |
190 | | */ |
191 | 618k | pos1 = (SHORT)(((index >> N) & mask) + offset); |
192 | 618k | i = (index >> (2 * N)) & 1; |
193 | 618k | pos2 = (SHORT)((index & mask) + offset); |
194 | 618k | if ((pos2 - pos1) < 0) { |
195 | 209k | if (i == 1) { |
196 | 99.6k | pos1 += 16; |
197 | 109k | } else { |
198 | 109k | pos2 += 16; |
199 | 109k | } |
200 | 409k | } else { |
201 | 409k | if (i == 1) { |
202 | 137k | pos1 += 16; |
203 | 137k | pos2 += 16; |
204 | 137k | } |
205 | 409k | } |
206 | 618k | pos[0] = pos1; |
207 | 618k | pos[1] = pos2; |
208 | 618k | return; |
209 | 618k | } |
210 | | /* |
211 | | * D_ACELP_decode_3p_3N1 |
212 | | * |
213 | | * Parameters: |
214 | | * index I: pulse index |
215 | | * N I: number of bits for position |
216 | | * offset I: offset |
217 | | * pos O: position of the pulse |
218 | | * |
219 | | * Function: |
220 | | * Decode 3 pulses with 3*N+1 bits |
221 | | * |
222 | | * Returns: |
223 | | * void |
224 | | */ |
225 | | static void D_ACELP_decode_3p_3N1(LONG index, SHORT N, SHORT offset, |
226 | 114k | SHORT pos[]) { |
227 | 114k | SHORT j; |
228 | 114k | LONG mask, idx; |
229 | | |
230 | | /* |
231 | | * Decode 3 pulses with 3*N+1 bits |
232 | | */ |
233 | 114k | mask = ((1 << ((2 * N) - 1)) - 1); |
234 | 114k | idx = index & mask; |
235 | 114k | j = offset; |
236 | 114k | if (((index >> ((2 * N) - 1)) & 1) == 1) { |
237 | 46.1k | j += (1 << (N - 1)); |
238 | 46.1k | } |
239 | 114k | D_ACELP_decode_2p_2N1(idx, N - 1, j, pos); |
240 | 114k | mask = ((1 << (N + 1)) - 1); |
241 | 114k | idx = (index >> (2 * N)) & mask; |
242 | 114k | D_ACELP_decode_1p_N1(idx, N, offset, pos + 2); |
243 | 114k | return; |
244 | 114k | } |
245 | | /* |
246 | | * D_ACELP_decode_4p_4N1 |
247 | | * |
248 | | * Parameters: |
249 | | * index I: pulse index |
250 | | * N I: number of bits for position |
251 | | * offset I: offset |
252 | | * pos O: position of the pulse |
253 | | * |
254 | | * Function: |
255 | | * Decode 4 pulses with 4*N+1 bits |
256 | | * |
257 | | * Returns: |
258 | | * void |
259 | | */ |
260 | | static void D_ACELP_decode_4p_4N1(LONG index, SHORT N, SHORT offset, |
261 | 78.3k | SHORT pos[]) { |
262 | 78.3k | SHORT j; |
263 | 78.3k | LONG mask, idx; |
264 | | /* |
265 | | * Decode 4 pulses with 4*N+1 bits |
266 | | */ |
267 | 78.3k | mask = ((1 << ((2 * N) - 1)) - 1); |
268 | 78.3k | idx = index & mask; |
269 | 78.3k | j = offset; |
270 | 78.3k | if (((index >> ((2 * N) - 1)) & 1) == 1) { |
271 | 26.0k | j += (1 << (N - 1)); |
272 | 26.0k | } |
273 | 78.3k | D_ACELP_decode_2p_2N1(idx, N - 1, j, pos); |
274 | 78.3k | mask = ((1 << ((2 * N) + 1)) - 1); |
275 | 78.3k | idx = (index >> (2 * N)) & mask; |
276 | 78.3k | D_ACELP_decode_2p_2N1(idx, N, offset, pos + 2); |
277 | 78.3k | return; |
278 | 78.3k | } |
279 | | /* |
280 | | * D_ACELP_decode_4p_4N |
281 | | * |
282 | | * Parameters: |
283 | | * index I: pulse index |
284 | | * N I: number of bits for position |
285 | | * offset I: offset |
286 | | * pos O: position of the pulse |
287 | | * |
288 | | * Function: |
289 | | * Decode 4 pulses with 4*N bits |
290 | | * |
291 | | * Returns: |
292 | | * void |
293 | | */ |
294 | | static void D_ACELP_decode_4p_4N(LONG index, SHORT N, SHORT offset, |
295 | 160k | SHORT pos[]) { |
296 | 160k | SHORT j, n_1; |
297 | | /* |
298 | | * Decode 4 pulses with 4*N bits |
299 | | */ |
300 | 160k | n_1 = N - 1; |
301 | 160k | j = offset + (1 << n_1); |
302 | 160k | switch ((index >> ((4 * N) - 2)) & 3) { |
303 | 78.3k | case 0: |
304 | 78.3k | if (((index >> ((4 * n_1) + 1)) & 1) == 0) { |
305 | 55.2k | D_ACELP_decode_4p_4N1(index, n_1, offset, pos); |
306 | 55.2k | } else { |
307 | 23.0k | D_ACELP_decode_4p_4N1(index, n_1, j, pos); |
308 | 23.0k | } |
309 | 78.3k | break; |
310 | 29.8k | case 1: |
311 | 29.8k | D_ACELP_decode_1p_N1((index >> ((3 * n_1) + 1)), n_1, offset, pos); |
312 | 29.8k | D_ACELP_decode_3p_3N1(index, n_1, j, pos + 1); |
313 | 29.8k | break; |
314 | 29.4k | case 2: |
315 | 29.4k | D_ACELP_decode_2p_2N1((index >> ((2 * n_1) + 1)), n_1, offset, pos); |
316 | 29.4k | D_ACELP_decode_2p_2N1(index, n_1, j, pos + 2); |
317 | 29.4k | break; |
318 | 22.8k | case 3: |
319 | 22.8k | D_ACELP_decode_3p_3N1((index >> (n_1 + 1)), n_1, offset, pos); |
320 | 22.8k | D_ACELP_decode_1p_N1(index, n_1, j, pos + 3); |
321 | 22.8k | break; |
322 | 160k | } |
323 | 160k | return; |
324 | 160k | } |
325 | | |
326 | | /* |
327 | | * D_ACELP_decode_4t |
328 | | * |
329 | | * Parameters: |
330 | | * index I: index |
331 | | * mode I: speech mode |
332 | | * code I: (Q9) algebraic (fixed) codebook excitation |
333 | | * |
334 | | * Function: |
335 | | * 20, 36, 44, 52, 64, 72, 88 bits algebraic codebook. |
336 | | * 4 tracks x 16 positions per track = 64 samples. |
337 | | * |
338 | | * 20 bits 5+5+5+5 --> 4 pulses in a frame of 64 samples. |
339 | | * 36 bits 9+9+9+9 --> 8 pulses in a frame of 64 samples. |
340 | | * 44 bits 13+9+13+9 --> 10 pulses in a frame of 64 samples. |
341 | | * 52 bits 13+13+13+13 --> 12 pulses in a frame of 64 samples. |
342 | | * 64 bits 2+2+2+2+14+14+14+14 --> 16 pulses in a frame of 64 samples. |
343 | | * 72 bits 10+2+10+2+10+14+10+14 --> 18 pulses in a frame of 64 samples. |
344 | | * 88 bits 11+11+11+11+11+11+11+11 --> 24 pulses in a frame of 64 samples. |
345 | | * |
346 | | * All pulses can have two (2) possible amplitudes: +1 or -1. |
347 | | * Each pulse can sixteen (16) possible positions. |
348 | | * |
349 | | * codevector length 64 |
350 | | * number of track 4 |
351 | | * number of position 16 |
352 | | * |
353 | | * Returns: |
354 | | * void |
355 | | */ |
356 | 387k | void D_ACELP_decode_4t64(SHORT index[], int nbits, FIXP_COD code[]) { |
357 | 387k | LONG L_index; |
358 | 387k | SHORT k, pos[6]; |
359 | | |
360 | 387k | FDKmemclear(code, L_SUBFR * sizeof(FIXP_COD)); |
361 | | |
362 | | /* decode the positions and signs of pulses and build the codeword */ |
363 | 387k | switch (nbits) { |
364 | 35.9k | case 12: |
365 | 107k | for (k = 0; k < 4; k += 2) { |
366 | 71.8k | L_index = index[2 * (k / 2) + 1]; |
367 | 71.8k | D_ACELP_decode_1p_N1(L_index, 4, 0, pos); |
368 | 71.8k | D_ACELP_add_pulse(pos, 1, 2 * (index[2 * (k / 2)]) + k / 2, code); |
369 | 71.8k | } |
370 | 35.9k | break; |
371 | 85.3k | case 16: { |
372 | 85.3k | int i = 0; |
373 | 85.3k | int offset = index[i++]; |
374 | 85.3k | offset = (offset == 0) ? 1 : 3; |
375 | 426k | for (k = 0; k < 4; k++) { |
376 | 341k | if (k != offset) { |
377 | 256k | L_index = index[i++]; |
378 | 256k | D_ACELP_decode_1p_N1(L_index, 4, 0, pos); |
379 | 256k | D_ACELP_add_pulse(pos, 1, k, code); |
380 | 256k | } |
381 | 341k | } |
382 | 85.3k | } break; |
383 | 124k | case 20: |
384 | 622k | for (k = 0; k < 4; k++) { |
385 | 498k | L_index = (LONG)index[k]; |
386 | 498k | D_ACELP_decode_1p_N1(L_index, 4, 0, pos); |
387 | 498k | D_ACELP_add_pulse(pos, 1, k, code); |
388 | 498k | } |
389 | 124k | break; |
390 | 27.6k | case 28: |
391 | 83.0k | for (k = 0; k < 4 - 2; k++) { |
392 | 55.3k | L_index = (LONG)index[k]; |
393 | 55.3k | D_ACELP_decode_2p_2N1(L_index, 4, 0, pos); |
394 | 55.3k | D_ACELP_add_pulse(pos, 2, k, code); |
395 | 55.3k | } |
396 | 83.0k | for (k = 2; k < 4; k++) { |
397 | 55.3k | L_index = (LONG)index[k]; |
398 | 55.3k | D_ACELP_decode_1p_N1(L_index, 4, 0, pos); |
399 | 55.3k | D_ACELP_add_pulse(pos, 1, k, code); |
400 | 55.3k | } |
401 | 27.6k | break; |
402 | 55.1k | case 36: |
403 | 275k | for (k = 0; k < 4; k++) { |
404 | 220k | L_index = (LONG)index[k]; |
405 | 220k | D_ACELP_decode_2p_2N1(L_index, 4, 0, pos); |
406 | 220k | D_ACELP_add_pulse(pos, 2, k, code); |
407 | 220k | } |
408 | 55.1k | break; |
409 | 6.46k | case 44: |
410 | 19.3k | for (k = 0; k < 4 - 2; k++) { |
411 | 12.9k | L_index = (LONG)index[k]; |
412 | 12.9k | D_ACELP_decode_3p_3N1(L_index, 4, 0, pos); |
413 | 12.9k | D_ACELP_add_pulse(pos, 3, k, code); |
414 | 12.9k | } |
415 | 19.3k | for (k = 2; k < 4; k++) { |
416 | 12.9k | L_index = (LONG)index[k]; |
417 | 12.9k | D_ACELP_decode_2p_2N1(L_index, 4, 0, pos); |
418 | 12.9k | D_ACELP_add_pulse(pos, 2, k, code); |
419 | 12.9k | } |
420 | 6.46k | break; |
421 | 12.1k | case 52: |
422 | 60.8k | for (k = 0; k < 4; k++) { |
423 | 48.6k | L_index = (LONG)index[k]; |
424 | 48.6k | D_ACELP_decode_3p_3N1(L_index, 4, 0, pos); |
425 | 48.6k | D_ACELP_add_pulse(pos, 3, k, code); |
426 | 48.6k | } |
427 | 12.1k | break; |
428 | 40.1k | case 64: |
429 | 200k | for (k = 0; k < 4; k++) { |
430 | 160k | L_index = (((LONG)index[k] << 14) + (LONG)index[k + 4]); |
431 | 160k | D_ACELP_decode_4p_4N(L_index, 4, 0, pos); |
432 | 160k | D_ACELP_add_pulse(pos, 4, k, code); |
433 | 160k | } |
434 | 40.1k | break; |
435 | 0 | default: |
436 | 0 | FDK_ASSERT(0); |
437 | 387k | } |
438 | 387k | return; |
439 | 387k | } |