blob: ab13bd809adf3df74c53b8bbc7878d2e1e96aea3
1 | /* |
2 | *=================================================================== |
3 | * 3GPP AMR Wideband Floating-point Speech Codec |
4 | *=================================================================== |
5 | */ |
6 | //#include <memory.h> |
7 | #include <stdio.h> |
8 | #include <stdlib.h> |
9 | #include <string.h> |
10 | #include <fcntl.h> |
11 | #include "typedef.h" |
12 | #include "dec_util.h" |
13 | |
14 | #define L_SUBFR 64 /* Subframe size */ |
15 | #define PRED_ORDER 4 |
16 | #define MEAN_ENER 30 /* average innovation energy */ |
17 | extern const Word16 D_ROM_ph_imp_low[]; |
18 | extern const Word16 D_ROM_ph_imp_mid[]; |
19 | |
20 | |
21 | /* |
22 | * D_ACELP_add_pulse |
23 | * |
24 | * Parameters: |
25 | * pos I: position of pulse |
26 | * nb_pulse I: number of pulses |
27 | * track I: track |
28 | * code O: fixed codebook |
29 | * |
30 | * Function: |
31 | * Add pulses to fixed codebook |
32 | * |
33 | * Returns: |
34 | * void |
35 | */ |
36 | static void D_ACELP_add_pulse(Word32 pos[], Word32 nb_pulse, |
37 | Word32 track, Word16 code[]) |
38 | { |
39 | Word32 i, k; |
40 | |
41 | for (k = 0; k < nb_pulse; k++) { |
42 | /* i = ((pos[k] & (16-1))*NB_TRACK) + track; */ |
43 | i = ((pos[k] & (16 - 1)) << 2) + track; |
44 | |
45 | if ((pos[k] & 16) == 0) { |
46 | code[i] = (Word16)(code[i] + 512); |
47 | } else { |
48 | code[i] = (Word16)(code[i] - 512); |
49 | } |
50 | } |
51 | |
52 | return; |
53 | } |
54 | |
55 | |
56 | /* |
57 | * D_ACELP_decode_1p_N1 |
58 | * |
59 | * Parameters: |
60 | * index I: pulse index |
61 | * N I: number of bits for position |
62 | * offset I: offset |
63 | * pos O: position of the pulse |
64 | |
65 | * |
66 | * Function: |
67 | * Decode 1 pulse with N+1 bits |
68 | * |
69 | * Returns: |
70 | * void |
71 | */ |
72 | static void D_ACELP_decode_1p_N1(Word32 index, Word32 N, |
73 | Word32 offset, Word32 pos[]) |
74 | { |
75 | Word32 i, pos1, mask; |
76 | |
77 | mask = ((1 << N) - 1); |
78 | |
79 | /* |
80 | * Decode 1 pulse with N+1 bits |
81 | */ |
82 | pos1 = ((index & mask) + offset); |
83 | i = ((index >> N) & 1); |
84 | |
85 | if (i == 1) { |
86 | pos1 += 16; |
87 | } |
88 | |
89 | pos[0] = pos1; |
90 | |
91 | return; |
92 | } |
93 | |
94 | |
95 | /* |
96 | * D_ACELP_decode_2p_2N1 |
97 | * |
98 | * Parameters: |
99 | * index I: pulse index |
100 | * N I: number of bits for position |
101 | * offset I: offset |
102 | * pos O: position of the pulse |
103 | * |
104 | * Function: |
105 | * Decode 2 pulses with 2*N+1 bits |
106 | * |
107 | * Returns: |
108 | * void |
109 | */ |
110 | static void D_ACELP_decode_2p_2N1(Word32 index, Word32 N, |
111 | Word32 offset, Word32 pos[]) |
112 | { |
113 | Word32 i, pos1, pos2; |
114 | Word32 mask; |
115 | |
116 | mask = ((1 << N) - 1); |
117 | |
118 | /* |
119 | * Decode 2 pulses with 2*N+1 bits |
120 | */ |
121 | pos1 = (((index >> N) & mask) + offset); |
122 | i = (index >> (2 * N)) & 1; |
123 | pos2 = ((index & mask) + offset); |
124 | |
125 | if ((pos2 - pos1) < 0) { |
126 | if (i == 1) { |
127 | pos1 += 16; |
128 | } else { |
129 | pos2 += 16; |
130 | } |
131 | } else { |
132 | if (i == 1) { |
133 | pos1 += 16; |
134 | pos2 += 16; |
135 | } |
136 | } |
137 | |
138 | pos[0] = pos1; |
139 | pos[1] = pos2; |
140 | |
141 | return; |
142 | } |
143 | |
144 | |
145 | /* |
146 | * D_ACELP_decode_3p_3N1 |
147 | * |
148 | * Parameters: |
149 | * index I: pulse index |
150 | * N I: number of bits for position |
151 | * offset I: offset |
152 | * pos O: position of the pulse |
153 | * |
154 | * Function: |
155 | * Decode 3 pulses with 3*N+1 bits |
156 | * |
157 | * Returns: |
158 | * void |
159 | */ |
160 | static void D_ACELP_decode_3p_3N1(Word32 index, Word32 N, |
161 | Word32 offset, Word32 pos[]) |
162 | { |
163 | Word32 j, mask, idx; |
164 | |
165 | /* |
166 | * Decode 3 pulses with 3*N+1 bits |
167 | */ |
168 | mask = ((1 << ((2 * N) - 1)) - 1); |
169 | idx = index & mask; |
170 | j = offset; |
171 | |
172 | if (((index >> ((2 * N) - 1)) & 1) == 1) { |
173 | j += (1 << (N - 1)); |
174 | } |
175 | |
176 | D_ACELP_decode_2p_2N1(idx, N - 1, j, pos); |
177 | mask = ((1 << (N + 1)) - 1); |
178 | idx = (index >> (2 * N)) & mask; |
179 | D_ACELP_decode_1p_N1(idx, N, offset, pos + 2); |
180 | |
181 | return; |
182 | } |
183 | |
184 | |
185 | /* |
186 | * D_ACELP_decode_4p_4N1 |
187 | * |
188 | * Parameters: |
189 | * index I: pulse index |
190 | * N I: number of bits for position |
191 | * offset I: offset |
192 | * pos O: position of the pulse |
193 | * |
194 | * Function: |
195 | * Decode 4 pulses with 4*N+1 bits |
196 | * |
197 | * Returns: |
198 | * void |
199 | */ |
200 | static void D_ACELP_decode_4p_4N1(Word32 index, Word32 N, |
201 | Word32 offset, Word32 pos[]) |
202 | { |
203 | Word32 j, mask, idx; |
204 | |
205 | /* |
206 | * Decode 4 pulses with 4*N+1 bits |
207 | */ |
208 | mask = ((1 << ((2 * N) - 1)) - 1); |
209 | idx = index & mask; |
210 | j = offset; |
211 | |
212 | if (((index >> ((2 * N) - 1)) & 1) == 1) { |
213 | j += (1 << (N - 1)); |
214 | } |
215 | |
216 | D_ACELP_decode_2p_2N1(idx, N - 1, j, pos); |
217 | mask = ((1 << ((2 * N) + 1)) - 1); |
218 | idx = (index >> (2 * N)) & mask; |
219 | D_ACELP_decode_2p_2N1(idx, N, offset, pos + 2); |
220 | |
221 | return; |
222 | } |
223 | |
224 | |
225 | /* |
226 | * D_ACELP_decode_4p_4N |
227 | * |
228 | * Parameters: |
229 | * index I: pulse index |
230 | * N I: number of bits for position |
231 | * offset I: offset |
232 | * pos O: position of the pulse |
233 | * |
234 | * Function: |
235 | * Decode 4 pulses with 4*N bits |
236 | * |
237 | * Returns: |
238 | * void |
239 | */ |
240 | static void D_ACELP_decode_4p_4N(Word32 index, Word32 N, |
241 | Word32 offset, Word32 pos[]) |
242 | { |
243 | Word32 j, n_1; |
244 | |
245 | /* |
246 | * Decode 4 pulses with 4*N bits |
247 | */ |
248 | n_1 = N - 1; |
249 | j = offset + (1 << n_1); |
250 | |
251 | switch ((index >> ((4 * N) - 2)) & 3) { |
252 | case 0: |
253 | if (((index >> ((4 * n_1) + 1)) & 1) == 0) { |
254 | D_ACELP_decode_4p_4N1(index, n_1, offset, pos); |
255 | } else { |
256 | D_ACELP_decode_4p_4N1(index, n_1, j, pos); |
257 | } |
258 | break; |
259 | |
260 | case 1: |
261 | D_ACELP_decode_1p_N1((index >> ((3 * n_1) + 1)), n_1, offset, pos); |
262 | D_ACELP_decode_3p_3N1(index, n_1, j, pos + 1); |
263 | break; |
264 | |
265 | case 2: |
266 | D_ACELP_decode_2p_2N1((index >> ((2 * n_1) + 1)), n_1, offset, pos); |
267 | D_ACELP_decode_2p_2N1(index, n_1, j, pos + 2); |
268 | break; |
269 | |
270 | case 3: |
271 | D_ACELP_decode_3p_3N1((index >> (n_1 + 1)), n_1, offset, pos); |
272 | D_ACELP_decode_1p_N1(index, n_1, j, pos + 3); |
273 | break; |
274 | } |
275 | |
276 | return; |
277 | } |
278 | |
279 | |
280 | /* |
281 | * D_ACELP_decode_5p_5N |
282 | * |
283 | * Parameters: |
284 | * index I: pulse index |
285 | * N I: number of bits for position |
286 | * offset I: offset |
287 | * pos O: position of the pulse |
288 | * |
289 | * Function: |
290 | * Decode 5 pulses with 5*N bits |
291 | * |
292 | * Returns: |
293 | * void |
294 | */ |
295 | static void D_ACELP_decode_5p_5N(Word32 index, Word32 N, |
296 | Word32 offset, Word32 pos[]) |
297 | { |
298 | Word32 j, n_1; |
299 | Word32 idx; |
300 | |
301 | /* |
302 | * Decode 5 pulses with 5*N bits |
303 | */ |
304 | n_1 = N - 1; |
305 | j = offset + (1 << n_1); |
306 | idx = (index >> ((2 * N) + 1)); |
307 | |
308 | if (((index >> ((5 * N) - 1)) & 1) == 0) { |
309 | D_ACELP_decode_3p_3N1(idx, n_1, offset, pos); |
310 | D_ACELP_decode_2p_2N1(index, N, offset, pos + 3); |
311 | } else { |
312 | D_ACELP_decode_3p_3N1(idx, n_1, j, pos); |
313 | D_ACELP_decode_2p_2N1(index, N, offset, pos + 3); |
314 | } |
315 | |
316 | return; |
317 | } |
318 | |
319 | |
320 | /* |
321 | * D_ACELP_decode_6p_6N_2 |
322 | * |
323 | * Parameters: |
324 | * index I: pulse index |
325 | * N I: number of bits for position |
326 | * offset I: offset |
327 | * pos O: position of the pulse |
328 | * |
329 | * Function: |
330 | * Decode 6 pulses with 6*N-2 bits |
331 | * |
332 | * Returns: |
333 | * void |
334 | */ |
335 | static void D_ACELP_decode_6p_6N_2(Word32 index, Word32 N, |
336 | Word32 offset, Word32 pos[]) |
337 | { |
338 | Word32 j, n_1, offsetA, offsetB; |
339 | |
340 | n_1 = N - 1; |
341 | j = offset + (1 << n_1); |
342 | offsetA = offsetB = j; |
343 | |
344 | if (((index >> ((6 * N) - 5)) & 1) == 0) { |
345 | offsetA = offset; |
346 | } else { |
347 | offsetB = offset; |
348 | } |
349 | |
350 | switch ((index >> ((6 * N) - 4)) & 3) { |
351 | case 0: |
352 | D_ACELP_decode_5p_5N(index >> N, n_1, offsetA, pos); |
353 | D_ACELP_decode_1p_N1(index, n_1, offsetA, pos + 5); |
354 | break; |
355 | |
356 | case 1: |
357 | D_ACELP_decode_5p_5N(index >> N, n_1, offsetA, pos); |
358 | D_ACELP_decode_1p_N1(index, n_1, offsetB, pos + 5); |
359 | break; |
360 | |
361 | case 2: |
362 | D_ACELP_decode_4p_4N(index >> ((2 * n_1) + 1), n_1, offsetA, pos); |
363 | D_ACELP_decode_2p_2N1(index, n_1, offsetB, pos + 4); |
364 | break; |
365 | |
366 | case 3: |
367 | D_ACELP_decode_3p_3N1(index >> ((3 * n_1) + 1), n_1, offset, pos); |
368 | D_ACELP_decode_3p_3N1(index, n_1, j, pos + 3); |
369 | break; |
370 | } |
371 | |
372 | return; |
373 | } |
374 | |
375 | |
376 | /* |
377 | * D_ACELP_decode_2t |
378 | * |
379 | * Parameters: |
380 | * index I: 12 bits index |
381 | * code O: (Q9) algebraic (fixed) codebook excitation |
382 | * |
383 | * Function: |
384 | * 12 bits algebraic codebook decoder. |
385 | * 2 tracks x 32 positions per track = 64 samples. |
386 | * |
387 | * 12 bits --> 2 pulses in a frame of 64 samples. |
388 | * |
389 | * All pulses can have two (2) possible amplitudes: +1 or -1. |
390 | * Each pulse can have 32 possible positions. |
391 | * |
392 | * codevector length 64 |
393 | * number of track 2 |
394 | * number of position 32 |
395 | * |
396 | * Returns: |
397 | * void |
398 | */ |
399 | void D_ACELP_decode_2t(Word16 index, Word16 code[]) |
400 | { |
401 | Word32 i0, i1; |
402 | |
403 | memset(code, 0, 64 * sizeof(Word16)); |
404 | |
405 | /* decode the positions and signs of pulses and build the codeword */ |
406 | i0 = (index >> 5) & 0x0000003E; |
407 | i1 = ((index & 0x0000001F) << 1) + 1; |
408 | |
409 | if (((index >> 6) & 32) == 0) { |
410 | code[i0] = 512; |
411 | } else { |
412 | code[i0] = -512; |
413 | } |
414 | |
415 | if ((index & 32) == 0) { |
416 | code[i1] = 512; |
417 | } else { |
418 | code[i1] = -512; |
419 | } |
420 | |
421 | return; |
422 | } |
423 | |
424 | |
425 | /* |
426 | * D_ACELP_decode_4t |
427 | * |
428 | * Parameters: |
429 | * index I: index |
430 | * mode I: speech mode |
431 | * code I: (Q9) algebraic (fixed) codebook excitation |
432 | * |
433 | * Function: |
434 | * 20, 36, 44, 52, 64, 72, 88 bits algebraic codebook. |
435 | * 4 tracks x 16 positions per track = 64 samples. |
436 | * |
437 | * 20 bits 5+5+5+5 --> 4 pulses in a frame of 64 samples. |
438 | * 36 bits 9+9+9+9 --> 8 pulses in a frame of 64 samples. |
439 | * 44 bits 13+9+13+9 --> 10 pulses in a frame of 64 samples. |
440 | * 52 bits 13+13+13+13 --> 12 pulses in a frame of 64 samples. |
441 | * 64 bits 2+2+2+2+14+14+14+14 --> 16 pulses in a frame of 64 samples. |
442 | * 72 bits 10+2+10+2+10+14+10+14 --> 18 pulses in a frame of 64 samples. |
443 | * 88 bits 11+11+11+11+11+11+11+11 --> 24 pulses in a frame of 64 samples. |
444 | * |
445 | * All pulses can have two (2) possible amplitudes: +1 or -1. |
446 | * Each pulse can sixteen (16) possible positions. |
447 | * |
448 | * codevector length 64 |
449 | * number of track 4 |
450 | * number of position 16 |
451 | * |
452 | * Returns: |
453 | * void |
454 | */ |
455 | void D_ACELP_decode_4t(Word16 index[], Word16 nbbits, Word16 code[]) |
456 | { |
457 | Word32 k, L_index, pos[6]; |
458 | |
459 | memset(code, 0, 64 * sizeof(Word16)); |
460 | |
461 | /* decode the positions and signs of pulses and build the codeword */ |
462 | if (nbbits == 20) { |
463 | for (k = 0; k < 4; k++) { |
464 | L_index = index[k]; |
465 | D_ACELP_decode_1p_N1(L_index, 4, 0, pos); |
466 | D_ACELP_add_pulse(pos, 1, k, code); |
467 | } |
468 | } else if (nbbits == 36) { |
469 | for (k = 0; k < 4; k++) { |
470 | L_index = index[k]; |
471 | D_ACELP_decode_2p_2N1(L_index, 4, 0, pos); |
472 | D_ACELP_add_pulse(pos, 2, k, code); |
473 | } |
474 | } else if (nbbits == 44) { |
475 | for (k = 0; k < 4 - 2; k++) { |
476 | L_index = index[k]; |
477 | D_ACELP_decode_3p_3N1(L_index, 4, 0, pos); |
478 | D_ACELP_add_pulse(pos, 3, k, code); |
479 | } |
480 | |
481 | for (k = 2; k < 4; k++) { |
482 | L_index = index[k]; |
483 | D_ACELP_decode_2p_2N1(L_index, 4, 0, pos); |
484 | D_ACELP_add_pulse(pos, 2, k, code); |
485 | } |
486 | } else if (nbbits == 52) { |
487 | for (k = 0; k < 4; k++) { |
488 | L_index = index[k]; |
489 | D_ACELP_decode_3p_3N1(L_index, 4, 0, pos); |
490 | D_ACELP_add_pulse(pos, 3, k, code); |
491 | } |
492 | } else if (nbbits == 64) { |
493 | for (k = 0; k < 4; k++) { |
494 | L_index = ((index[k] << 14) + index[k + 4]); |
495 | D_ACELP_decode_4p_4N(L_index, 4, 0, pos); |
496 | D_ACELP_add_pulse(pos, 4, k, code); |
497 | } |
498 | } else if (nbbits == 72) { |
499 | for (k = 0; k < 4 - 2; k++) { |
500 | L_index = ((index[k] << 10) + index[k + 4]); |
501 | D_ACELP_decode_5p_5N(L_index, 4, 0, pos); |
502 | D_ACELP_add_pulse(pos, 5, k, code); |
503 | } |
504 | |
505 | for (k = 2; k < 4; k++) { |
506 | L_index = ((index[k] << 14) + index[k + 4]); |
507 | D_ACELP_decode_4p_4N(L_index, 4, 0, pos); |
508 | D_ACELP_add_pulse(pos, 4, k, code); |
509 | } |
510 | } else if (nbbits == 88) { |
511 | for (k = 0; k < 4; k++) { |
512 | L_index = ((index[k] << 11) + index[k + 4]); |
513 | D_ACELP_decode_6p_6N_2(L_index, 4, 0, pos); |
514 | D_ACELP_add_pulse(pos, 6, k, code); |
515 | } |
516 | } |
517 | return; |
518 | } |
519 | |
520 | |
521 | /* |
522 | * D_ACELP_phase_dispersion |
523 | * |
524 | * Parameters: |
525 | * gain_code I: (Q0) gain of code |
526 | * gain_pit I: (Q14) gain of pitch |
527 | * code I/O: code vector |
528 | * mode I: level, 0=hi, 1=lo, 2=off |
529 | * disp_mem I/O: static memory (size = 8) |
530 | * |
531 | * Function: |
532 | * An adaptive anti-sparseness post-processing procedure is |
533 | * applied to the fixed codebook vector in order to |
534 | * reduce perceptual artifacts arising from the sparseness |
535 | * of the algebraic fixed codebook vectors with only |
536 | * a few non-zero samples per subframe. |
537 | * |
538 | * Returns: |
539 | * void |
540 | */ |
541 | void D_ACELP_phase_dispersion(Word16 gain_code, Word16 gain_pit, Word16 code[], |
542 | Word16 mode, Word16 disp_mem[]) |
543 | { |
544 | Word32 code2[2 * L_SUBFR] = {0}; |
545 | Word32 i, j, state; |
546 | Word16 *prev_gain_pit, *prev_gain_code, *prev_state; |
547 | |
548 | prev_state = disp_mem; |
549 | prev_gain_code = disp_mem + 1; |
550 | prev_gain_pit = disp_mem + 2; |
551 | |
552 | if (gain_pit < 9830) { /* 0.6 in Q14 */ |
553 | state = 0; |
554 | } else if (gain_pit < 14746) { /* 0.9 in Q14 */ |
555 | state = 1; |
556 | } else { |
557 | state = 2; |
558 | } |
559 | |
560 | for (i = 5; i > 0; i--) { |
561 | prev_gain_pit[i] = prev_gain_pit[i - 1]; |
562 | } |
563 | prev_gain_pit[0] = gain_pit; |
564 | |
565 | if ((gain_code - *prev_gain_code) > (*prev_gain_code << 1)) { |
566 | /* onset */ |
567 | if (state < 2) { |
568 | state = state + 1; |
569 | } |
570 | } else { |
571 | j = 0; |
572 | |
573 | for (i = 0; i < 6; i++) { |
574 | if (prev_gain_pit[i] < 9830) { /* 0.6 in Q14 */ |
575 | j = (j + 1); |
576 | } |
577 | } |
578 | |
579 | if (j > 2) { |
580 | state = 0; |
581 | } |
582 | |
583 | if ((state - *prev_state) > 1) { |
584 | state = state - 1; |
585 | } |
586 | } |
587 | *prev_gain_code = gain_code; |
588 | *prev_state = (Word16)state; |
589 | |
590 | /* circular convolution */ |
591 | state = state + mode; /* level of dispersion */ |
592 | |
593 | if (state == 0) { |
594 | for (i = 0; i < L_SUBFR; i++) { |
595 | if (code[i] != 0) { |
596 | for (j = 0; j < L_SUBFR; j++) { |
597 | code2[i + j] = code2[i + j] + |
598 | (((code[i] * D_ROM_ph_imp_low[j]) + 0x4000) >> 15); |
599 | } |
600 | } |
601 | } |
602 | } else if (state == 1) { |
603 | for (i = 0; i < L_SUBFR; i++) { |
604 | if (code[i] != 0) { |
605 | for (j = 0; j < L_SUBFR; j++) { |
606 | code2[i + j] = code2[i + j] + |
607 | (((code[i] * D_ROM_ph_imp_mid[j]) + 0x4000) >> 15); |
608 | } |
609 | } |
610 | } |
611 | } |
612 | |
613 | if (state < 2) { |
614 | for (i = 0; i < L_SUBFR; i++) { |
615 | code[i] = (Word16)(code2[i] + code2[i + L_SUBFR]); |
616 | } |
617 | } |
618 | |
619 | return; |
620 | } |
621 |