Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include "cnst.h"
43 : #include "prot.h"
44 : #include "ivas_prot.h"
45 : #include "rom_com.h"
46 : #include "wmc_auto.h"
47 :
48 : /*-------------------------------------------------------------------*
49 : * Local function prototypes
50 : *-------------------------------------------------------------------*/
51 :
52 : static void add_pulses( const int16_t pos[], const int16_t nb_pulse, const int16_t track, float code[] );
53 : static void dec_1p_N1( const int32_t index, const int16_t N, const int16_t offset, int16_t pos[] );
54 : static void dec_2p_2N1( const int32_t index, const int16_t N, const int16_t offset, int16_t pos[] );
55 : static void dec_3p_3N1( const int32_t index, const int16_t N, const int16_t offset, int16_t pos[] );
56 : static void dec_4p_4N1( const int32_t index, const int16_t N, const int16_t offset, int16_t pos[] );
57 : static void dec_4p_4N( const int32_t index, const int16_t N, const int16_t offset, int16_t pos[] );
58 : static void dec_5p_5N( const int32_t index, const int16_t N, const int16_t offset, int16_t pos[] );
59 : static void dec_6p_6N2( const int32_t index, const int16_t N, const int16_t offset, int16_t pos[] );
60 : static void fcb_decode_PI( int32_t code_index, int16_t sector_6p[], const int16_t pulse_num );
61 :
62 :
63 : /*----------------------------------------------------------------------------------*
64 : * dec_acelp_4t64()
65 : *
66 : * 20, 36 bits algebraic codebook decoder.
67 : * 4 tracks x 16 positions per track = 64 samples.
68 : *
69 : * 20 bits --> 4 pulses in a frame of 64 samples.
70 : * 36 bits --> 8 pulses in a frame of 64 samples.
71 : *
72 : * All pulses can have two (2) possible amplitudes: +1 or -1.
73 : * Each pulse can have sixteen (16) possible positions.
74 : *
75 : * See cod4t64.c for more details of the algebraic code.
76 : *----------------------------------------------------------------------------------*/
77 :
78 1384729 : void dec_acelp_4t64(
79 : Decoder_State *st, /* i/o: decoder state structure */
80 : int16_t nbbits, /* i : number of bits per codebook */
81 : float code[], /* o : algebraic (fixed) codebook excitation */
82 : const int16_t Opt_AMR_WB /* i : flag indicating AMR-WB IO mode */
83 : )
84 : {
85 : int16_t i, k, pos[7];
86 : int32_t L_index;
87 : int16_t ind1[NB_TRACK_FCB_4T], ind2[NB_TRACK_FCB_4T];
88 : PulseConfig config;
89 : int16_t indexing_indices[6], wordcnt, bitcnt;
90 :
91 1384729 : if ( !Opt_AMR_WB )
92 : {
93 1375909 : switch ( nbbits )
94 : {
95 13737 : case 20:
96 13737 : config.nb_pulse = 4;
97 13737 : break;
98 :
99 60945 : case 28:
100 60945 : config.nb_pulse = 6;
101 60945 : break;
102 :
103 1298816 : case 36:
104 1298816 : config.nb_pulse = 8;
105 1298816 : break;
106 :
107 1988 : case 43:
108 1988 : config.nb_pulse = 10;
109 1988 : break;
110 :
111 0 : case 50:
112 0 : config.nb_pulse = 12;
113 0 : break;
114 :
115 422 : case 62:
116 422 : config.nb_pulse = 16;
117 422 : break;
118 :
119 :
120 1 : case 87:
121 1 : config.nb_pulse = 26;
122 1 : break;
123 : }
124 1375909 : config.bits = nbbits;
125 1375909 : config.codetrackpos = TRACKPOS_FIXED_FIRST;
126 :
127 :
128 1375909 : wordcnt = nbbits >> 4;
129 1375909 : bitcnt = nbbits & 15;
130 4053470 : for ( i = 0; i < wordcnt; i++ )
131 : {
132 2677561 : indexing_indices[i] = get_next_indice( st, 16 );
133 : }
134 1375909 : if ( bitcnt )
135 : {
136 1375909 : indexing_indices[i] = get_next_indice( st, bitcnt );
137 : }
138 1375909 : D_ACELP_indexing( code, config, NB_TRACK_FCB_4T, indexing_indices, &st->BER_detect );
139 : }
140 : else
141 : {
142 573300 : for ( i = 0; i < L_SUBFR; i++ )
143 : {
144 564480 : code[i] = 0.0f;
145 : }
146 :
147 8820 : if ( nbbits == 20 )
148 : {
149 600 : for ( k = 0; k < NB_TRACK_FCB_4T; k++ )
150 : {
151 480 : L_index = get_next_indice( st, 5 );
152 480 : dec_1p_N1( L_index, 4, 0, pos );
153 480 : add_pulses( pos, 1, k, code );
154 : }
155 : }
156 8700 : else if ( nbbits == 36 )
157 : {
158 11060 : for ( k = 0; k < NB_TRACK_FCB_4T; k++ )
159 : {
160 8848 : L_index = get_next_indice( st, 9 );
161 8848 : dec_2p_2N1( L_index, 4, 0, pos );
162 8848 : add_pulses( pos, 2, k, code );
163 : }
164 : }
165 6488 : else if ( nbbits == 44 ) /* AMR-WB pulse indexing */
166 : {
167 0 : for ( k = 0; k < NB_TRACK_FCB_4T - 2; k++ )
168 : {
169 0 : L_index = get_next_indice( st, 13 );
170 0 : dec_3p_3N1( L_index, 4, 0, pos );
171 0 : add_pulses( pos, 3, k, code );
172 : }
173 :
174 0 : for ( k = 2; k < NB_TRACK_FCB_4T; k++ )
175 : {
176 0 : L_index = get_next_indice( st, 9 );
177 0 : dec_2p_2N1( L_index, 4, 0, pos );
178 0 : add_pulses( pos, 2, k, code );
179 : }
180 : }
181 6488 : else if ( nbbits == 52 ) /* AMR-WB pulse indexing */
182 : {
183 0 : for ( k = 0; k < NB_TRACK_FCB_4T; k++ )
184 : {
185 0 : L_index = get_next_indice( st, 13 );
186 0 : dec_3p_3N1( L_index, 4, 0, pos );
187 0 : add_pulses( pos, 3, k, code );
188 : }
189 : }
190 6488 : else if ( nbbits == 64 ) /* AMR-WB pulse indexing */
191 : {
192 0 : for ( k = 0; k < NB_TRACK_FCB_4T; k++ )
193 : {
194 0 : ind1[k] = get_next_indice( st, 2 );
195 : }
196 :
197 0 : for ( k = 0; k < NB_TRACK_FCB_4T; k++ )
198 : {
199 0 : ind2[k] = get_next_indice( st, 14 );
200 : }
201 :
202 0 : for ( k = 0; k < NB_TRACK_FCB_4T; k++ )
203 : {
204 0 : L_index = ( ( ind1[k] << 14 ) + ind2[k] );
205 0 : dec_4p_4N( L_index, 4, 0, pos );
206 0 : add_pulses( pos, 4, k, code );
207 : }
208 : }
209 6488 : else if ( nbbits == 72 )
210 : {
211 13032 : for ( k = 0; k < NB_TRACK_FCB_4T - 2; k++ )
212 : {
213 8688 : ind1[k] = get_next_indice( st, 10 );
214 : }
215 :
216 13032 : for ( k = 2; k < NB_TRACK_FCB_4T; k++ )
217 : {
218 8688 : ind1[k] = get_next_indice( st, 2 );
219 : }
220 :
221 13032 : for ( k = 0; k < NB_TRACK_FCB_4T - 2; k++ )
222 : {
223 8688 : ind2[k] = get_next_indice( st, 10 );
224 : }
225 :
226 13032 : for ( k = 2; k < NB_TRACK_FCB_4T; k++ )
227 : {
228 8688 : ind2[k] = get_next_indice( st, 14 );
229 : }
230 :
231 13032 : for ( k = 0; k < NB_TRACK_FCB_4T - 2; k++ )
232 : {
233 8688 : L_index = ( ( ind1[k] << 10 ) + ind2[k] );
234 8688 : dec_5p_5N( L_index, 4, 0, pos );
235 8688 : add_pulses( pos, 5, k, code );
236 : }
237 :
238 13032 : for ( k = 2; k < NB_TRACK_FCB_4T; k++ )
239 : {
240 8688 : L_index = ( ( ind1[k] << 14 ) + ind2[k] );
241 8688 : dec_4p_4N( L_index, 4, 0, pos );
242 8688 : add_pulses( pos, 4, k, code );
243 : }
244 : }
245 2144 : else if ( nbbits == 88 )
246 : {
247 10720 : for ( k = 0; k < NB_TRACK_FCB_4T; k++ )
248 : {
249 8576 : ind1[k] = get_next_indice( st, 11 );
250 : }
251 :
252 10720 : for ( k = 0; k < NB_TRACK_FCB_4T; k++ )
253 : {
254 8576 : ind2[k] = get_next_indice( st, 11 );
255 : }
256 :
257 10720 : for ( k = 0; k < NB_TRACK_FCB_4T; k++ )
258 : {
259 8576 : L_index = ( ( ind1[k] << 11 ) + ind2[k] );
260 8576 : dec_6p_6N2( L_index, 4, 0, pos );
261 8576 : add_pulses( pos, 6, k, code );
262 : }
263 : }
264 : }
265 :
266 1384729 : return;
267 : }
268 :
269 : /*-------------------------------------------------------*
270 : * add_pulses()
271 : *
272 : * Add decoded pulses to the codeword
273 : *-------------------------------------------------------*/
274 :
275 3239675 : static void add_pulses(
276 : const int16_t pos[], /* i : pulse position */
277 : const int16_t nb_pulse, /* i : nb. of pulses */
278 : const int16_t track, /* i : no. of the tracks */
279 : float code[] /* i/o: decoded codevector */
280 : )
281 : {
282 : int16_t i, k;
283 :
284 10101867 : for ( k = 0; k < nb_pulse; k++ )
285 : {
286 6862192 : i = ( ( pos[k] & ( NB_POS_FCB_4T - 1 ) ) * NB_TRACK_FCB_4T ) + track;
287 6862192 : if ( ( pos[k] & NB_POS_FCB_4T ) == 0 )
288 : {
289 3411580 : code[i] += 1.0f;
290 : }
291 : else
292 : {
293 3450612 : code[i] -= 1.0f;
294 : }
295 : }
296 :
297 3239675 : return;
298 : }
299 :
300 : /*-------------------------------------------------------*
301 : * dec_1p_N1()
302 : *
303 : * Decode 1 pulse with N+1 bits
304 : *-------------------------------------------------------*/
305 :
306 885984 : void dec_1p_N1(
307 : const int32_t index, /* i : quantization index */
308 : const int16_t N, /* i : nb. of bits */
309 : const int16_t offset, /* i : pulse position offset */
310 : int16_t pos[] /* o : pulse position */
311 : )
312 : {
313 : int16_t i, pos1;
314 : int32_t mask;
315 :
316 885984 : mask = ( ( 1 << N ) - 1 );
317 :
318 885984 : pos1 = (int16_t) ( index & mask ) + offset;
319 :
320 885984 : i = (int16_t) ( index >> N ) & 1;
321 :
322 885984 : if ( i == 1 )
323 : {
324 447760 : pos1 += NB_POS_FCB_4T;
325 : }
326 :
327 885984 : pos[0] = pos1;
328 :
329 885984 : return;
330 : }
331 :
332 : /*-------------------------------------------------------*
333 : * dec_2p_2N1()
334 : *
335 : * Decode 2 pulses with 2*N+1 bits:
336 : *-------------------------------------------------------*/
337 :
338 1247372 : void dec_2p_2N1(
339 : const int32_t index, /* i : quantization index */
340 : const int16_t N, /* i : nb. of bits */
341 : const int16_t offset, /* i : pulse position offset */
342 : int16_t pos[] /* o : pulse position */
343 : )
344 : {
345 : int16_t i, pos1, pos2;
346 : int32_t mask;
347 :
348 1247372 : mask = ( ( 1 << N ) - 1 );
349 :
350 1247372 : pos1 = (int16_t) ( ( index >> N ) & mask ) + offset;
351 1247372 : i = (int16_t) ( index >> ( 2 * N ) ) & 1;
352 1247372 : pos2 = (int16_t) ( index & mask ) + offset;
353 1247372 : if ( ( pos2 - pos1 ) < 0 )
354 : {
355 601349 : if ( i == 1 )
356 : {
357 297242 : pos1 += NB_POS_FCB_4T;
358 : }
359 : else
360 : {
361 304107 : pos2 += NB_POS_FCB_4T;
362 : }
363 : }
364 : else
365 : {
366 646023 : if ( i == 1 )
367 : {
368 323721 : pos1 += NB_POS_FCB_4T;
369 323721 : pos2 += NB_POS_FCB_4T;
370 : }
371 : }
372 :
373 1247372 : pos[0] = pos1;
374 1247372 : pos[1] = pos2;
375 :
376 1247372 : return;
377 : }
378 :
379 : /*-------------------------------------------------------*
380 : * dec_3p_3N1()
381 : *
382 : * Decode 3 pulses with 3*N+1 bits:
383 : *-------------------------------------------------------*/
384 :
385 22508 : static void dec_3p_3N1(
386 : const int32_t index, /* i : quantization index */
387 : const int16_t N, /* i : nb. of bits */
388 : const int16_t offset, /* i : pulse position offset */
389 : int16_t pos[] /* o : pulse position */
390 : )
391 : {
392 : int16_t j;
393 : int32_t idx, mask;
394 :
395 22508 : mask = ( ( 1 << ( ( 2 * N ) - 1 ) ) - 1 );
396 22508 : idx = index & mask;
397 22508 : j = offset;
398 :
399 22508 : if ( ( ( index >> ( ( 2 * N ) - 1 ) ) & 1 ) == 1 )
400 : {
401 11255 : j += ( 1 << ( N - 1 ) );
402 : }
403 :
404 22508 : dec_2p_2N1( idx, N - 1, j, pos );
405 22508 : mask = ( ( 1 << ( N + 1 ) ) - 1 );
406 22508 : idx = ( index >> ( 2 * N ) ) & mask;
407 22508 : dec_1p_N1( idx, N, offset, pos + 2 );
408 :
409 22508 : return;
410 : }
411 :
412 : /*-------------------------------------------------------*
413 : * dec_4p_4N1()
414 : *
415 : * Decode 4 pulses with 4*N+1 bits:
416 : *-------------------------------------------------------*/
417 :
418 1348 : static void dec_4p_4N1(
419 : const int32_t index, /* i : quantization index */
420 : const int16_t N, /* i : nb. of bits */
421 : const int16_t offset, /* i : pulse position offset */
422 : int16_t pos[] /* o : pulse position */
423 : )
424 : {
425 : int16_t j;
426 : int32_t mask, idx;
427 :
428 1348 : mask = ( ( 1 << ( ( 2 * N ) - 1 ) ) - 1 );
429 1348 : idx = index & mask;
430 1348 : j = offset;
431 :
432 1348 : if ( ( ( index >> ( ( 2 * N ) - 1 ) ) & 1 ) == 1 )
433 : {
434 654 : j += ( 1 << ( N - 1 ) );
435 : }
436 :
437 1348 : dec_2p_2N1( idx, N - 1, j, pos );
438 1348 : mask = ( ( 1 << ( ( 2 * N ) + 1 ) ) - 1 );
439 1348 : idx = ( index >> ( 2 * N ) ) & mask;
440 1348 : dec_2p_2N1( idx, N, offset, pos + 2 );
441 :
442 1348 : return;
443 : }
444 :
445 : /*-------------------------------------------------------*
446 : * dec_4p_4N()
447 : *
448 : * Decode 4 pulses with 4*N bits:
449 : *-------------------------------------------------------*/
450 :
451 12710 : static void dec_4p_4N(
452 : const int32_t index, /* i : quantization index */
453 : const int16_t N, /* i : nb. of bits */
454 : const int16_t offset, /* i : pulse position offset */
455 : int16_t pos[] /* o : pulse position */
456 : )
457 : {
458 : int16_t j, n_1;
459 :
460 12710 : n_1 = N - 1;
461 12710 : j = offset + ( 1 << n_1 );
462 12710 : switch ( ( index >> ( ( 4 * N ) - 2 ) ) & 3 )
463 : {
464 1348 : case 0:
465 : {
466 1348 : if ( ( ( index >> ( ( 4 * n_1 ) + 1 ) ) & 1 ) == 0 )
467 : {
468 661 : dec_4p_4N1( index, n_1, offset, pos );
469 : }
470 : else
471 : {
472 687 : dec_4p_4N1( index, n_1, j, pos );
473 : }
474 1348 : break;
475 : }
476 3143 : case 1:
477 : {
478 3143 : dec_1p_N1( ( index >> ( ( 3 * n_1 ) + 1 ) ), n_1, offset, pos );
479 3143 : dec_3p_3N1( index, n_1, j, pos + 1 );
480 3143 : break;
481 : }
482 4963 : case 2:
483 : {
484 4963 : dec_2p_2N1( ( index >> ( ( 2 * n_1 ) + 1 ) ), n_1, offset, pos );
485 4963 : dec_2p_2N1( index, n_1, j, pos + 2 );
486 4963 : break;
487 : }
488 3256 : case 3:
489 : {
490 3256 : dec_3p_3N1( ( index >> ( n_1 + 1 ) ), n_1, offset, pos );
491 3256 : dec_1p_N1( index, n_1, j, pos + 3 );
492 3256 : break;
493 : }
494 : }
495 :
496 12710 : return;
497 : }
498 :
499 : /*-------------------------------------------------------*
500 : * dec_5p_5N()
501 : *
502 : * Decode 5 pulses with 5*N bits:
503 : *-------------------------------------------------------*/
504 :
505 10375 : static void dec_5p_5N(
506 : const int32_t index, /* i : quantization index */
507 : const int16_t N, /* i : nb. of bits */
508 : const int16_t offset, /* i : pulse position offset */
509 : int16_t pos[] /* o : pulse position */
510 : )
511 : {
512 : int16_t j, n_1;
513 : int32_t idx;
514 :
515 10375 : n_1 = N - 1;
516 10375 : j = offset + ( 1 << n_1 );
517 10375 : idx = ( index >> ( ( 2 * N ) + 1 ) );
518 :
519 10375 : if ( ( ( index >> ( ( 5 * N ) - 1 ) ) & 1 ) == 0 )
520 : {
521 5307 : dec_3p_3N1( idx, n_1, offset, pos );
522 5307 : dec_2p_2N1( index, N, offset, pos + 3 );
523 : }
524 : else
525 : {
526 5068 : dec_3p_3N1( idx, n_1, j, pos );
527 5068 : dec_2p_2N1( index, N, offset, pos + 3 );
528 : }
529 :
530 10375 : return;
531 : }
532 :
533 : /*-------------------------------------------------------*
534 : * dec_6p_6N2()
535 : *
536 : * Decode 6 pulses with 6*N+2 bits:
537 : *-------------------------------------------------------*/
538 :
539 8576 : static void dec_6p_6N2(
540 : const int32_t index, /* i : quantization index */
541 : const int16_t N, /* i : nb. of bits */
542 : const int16_t offset, /* i : pulse position offset */
543 : int16_t pos[] /* o : pulse position */
544 : )
545 : {
546 : int16_t j, n_1, offsetA, offsetB;
547 :
548 8576 : n_1 = N - 1;
549 8576 : j = offset + ( 1 << n_1 );
550 8576 : offsetA = offsetB = j;
551 8576 : if ( ( ( index >> ( ( 6 * N ) - 5 ) ) & 1 ) == 0 )
552 : {
553 4311 : offsetA = offset;
554 : }
555 : else
556 : {
557 4265 : offsetB = offset;
558 : }
559 :
560 8576 : switch ( ( index >> ( ( 6 * N ) - 4 ) ) & 3 )
561 : {
562 228 : case 0:
563 : {
564 228 : dec_5p_5N( index >> N, n_1, offsetA, pos );
565 228 : dec_1p_N1( index, n_1, offsetA, pos + 5 );
566 228 : break;
567 : }
568 :
569 1459 : case 1:
570 : {
571 1459 : dec_5p_5N( index >> N, n_1, offsetA, pos );
572 1459 : dec_1p_N1( index, n_1, offsetB, pos + 5 );
573 1459 : break;
574 : }
575 :
576 4022 : case 2:
577 : {
578 4022 : dec_4p_4N( index >> ( ( 2 * n_1 ) + 1 ), n_1, offsetA, pos );
579 4022 : dec_2p_2N1( index, n_1, offsetB, pos + 4 );
580 4022 : break;
581 : }
582 :
583 2867 : case 3:
584 : {
585 2867 : dec_3p_3N1( index >> ( ( 3 * n_1 ) + 1 ), n_1, offset, pos );
586 2867 : dec_3p_3N1( index, n_1, j, pos + 3 );
587 2867 : break;
588 : }
589 : }
590 :
591 8576 : return;
592 : }
593 :
594 : /*---------------------------------------------------------------------*
595 : * fcb_decode_class_all_p()
596 : *
597 : * Get the position number and the pulse number on every position
598 : *---------------------------------------------------------------------*/
599 :
600 : /*! r: The index of pulse positions */
601 1160488 : static int32_t fcb_decode_class_all_p(
602 : int32_t *code_index, /* i : fcb index information */
603 : int16_t sector_6p_num[], /* o : Number of pulses for each position */
604 : const int16_t pulse_num, /* i : Number of pulses on a track. */
605 : int16_t *pos_num /* o : Number of positions which have pulses on the track.*/
606 : )
607 : {
608 : int16_t i, j, k;
609 : int32_t mn9;
610 : int16_t pulse_pos_num;
611 2491901 : for ( i = 1; i <= pulse_num; i++ )
612 : {
613 2471993 : if ( ( *code_index ) < PI_offset[pulse_num][i] )
614 : {
615 1140580 : break;
616 : }
617 : }
618 :
619 1160488 : ( *code_index ) -= PI_offset[pulse_num][i - 1];
620 :
621 1160488 : pulse_pos_num = pulse_num - i + 2;
622 1160488 : j = (int16_t) ( ( *code_index ) >> pulse_pos_num );
623 :
624 1160488 : k = (int16_t) ( j / PI_select_table[16][pulse_pos_num] );
625 1160488 : mn9 = j - k * PI_select_table[16][pulse_pos_num];
626 1160488 : if ( ( pulse_pos_num < pulse_num ) && ( pulse_pos_num > 1 ) )
627 : {
628 393327 : for ( i = 0; i < pulse_pos_num; i++ )
629 : {
630 262218 : sector_6p_num[i] = 1;
631 : }
632 131109 : sector_6p_num[k]++;
633 : }
634 : else
635 : {
636 1029379 : if ( pulse_pos_num == 1 )
637 : {
638 19908 : sector_6p_num[0] = (int16_t) pulse_num;
639 : }
640 : else
641 : {
642 4037884 : for ( i = 0; i < pulse_num; i++ )
643 : {
644 3028413 : sector_6p_num[i] = 1;
645 : }
646 : }
647 : }
648 :
649 1160488 : *pos_num = pulse_pos_num;
650 :
651 1160488 : return mn9;
652 : }
653 :
654 : /*---------------------------------------------------------------------*
655 : * fcb_decode_position()
656 : *
657 : * Decode the pulse position not same to the others
658 : *---------------------------------------------------------------------*/
659 :
660 1160488 : static void fcb_decode_position(
661 : int32_t index, /* i : position index information */
662 : int16_t pos_vector[], /* o : the positon vector */
663 : const int16_t pos_num /* i : number of positions */
664 : )
665 : {
666 : int16_t i, l;
667 : int32_t k;
668 : int16_t temp;
669 :
670 1160488 : k = index;
671 1160488 : l = 0;
672 1160488 : temp = pos_num;
673 3310539 : for ( i = 0; i < pos_num - 1; i++ )
674 : {
675 2150051 : k = PI_select_table[16 - l][temp] - k;
676 :
677 7413025 : for ( ; PI_select_table[16 - l][temp] >= k; l += 2 )
678 : {
679 : ;
680 : }
681 :
682 2150051 : if ( k > PI_select_table[17 - l][temp] )
683 : {
684 1205066 : l--;
685 : }
686 :
687 2150051 : k = PI_select_table[17 - l][temp--] - k;
688 2150051 : pos_vector[i] = (int16_t) ( l - 1 );
689 : }
690 1160488 : pos_vector[i] = (int16_t) ( l + k );
691 :
692 1160488 : return;
693 : }
694 :
695 : /*---------------------------------------------------------------------*
696 : * fcb_decode_PI()
697 : *
698 : * decode fcb pulse index
699 : *---------------------------------------------------------------------*/
700 :
701 : /*! r: return pulse position number */
702 1160488 : static void fcb_decode_PI(
703 : int32_t code_index, /* i : fcb index information */
704 : int16_t sector_6p[], /* o : decoded pulse position */
705 : const int16_t pulse_num /* i : pulse number for the track */
706 : )
707 : {
708 : int16_t i, l;
709 : int32_t mn9;
710 : int16_t pulse_pos_num;
711 : int16_t sector_6p_temp[7], sector_6p_num_temp[7];
712 : int16_t *sector_6p_ptr0;
713 : int16_t *sector_6p_ptr1;
714 :
715 : /*get the position number and the pulse number on every position */
716 1160488 : mn9 = fcb_decode_class_all_p( &code_index, sector_6p_num_temp, pulse_num, &pulse_pos_num );
717 :
718 : /* rebuild the vector*/
719 : /* decode the pulse position not same to the others*/
720 1160488 : fcb_decode_position( mn9, sector_6p_temp, pulse_pos_num );
721 4471027 : for ( i = pulse_pos_num - 1; i >= 0; i-- )
722 : {
723 3310539 : sector_6p_temp[i] += ( ( code_index & 0x1 ) << 4 );
724 3310539 : code_index = code_index >> 1;
725 : }
726 :
727 : /* decode the pulse position maybe some pulse position same to other pulse */
728 1160488 : sector_6p_ptr0 = §or_6p[pulse_num];
729 1160488 : sector_6p_ptr1 = §or_6p_temp[pulse_pos_num];
730 4471027 : for ( i = 0; i < pulse_pos_num; i++ )
731 : {
732 3310539 : sector_6p_ptr1--;
733 6792003 : for ( l = 0; l < sector_6p_num_temp[pulse_pos_num - 1 - i]; l++ )
734 : {
735 3481464 : *--sector_6p_ptr0 = *sector_6p_ptr1;
736 : }
737 : }
738 :
739 1160488 : return;
740 : }
741 :
742 : /*---------------------------------------------------------------------*
743 : * Read FCB index *
744 : *---------------------------------------------------------------------*/
745 :
746 580244 : void D_ACELP_decode_43bit(
747 : uint16_t idxs[],
748 : float code[],
749 : int16_t *pulsestrack )
750 : {
751 : int32_t ps[8];
752 : int16_t pos[7];
753 : int32_t joint_index;
754 580244 : int32_t joint_offset = 3611648; /*offset for 3 pulses per track*/
755 :
756 580244 : set_f( code, 0.0f, L_SUBFR );
757 :
758 580244 : ps[3] = idxs[0] & 0x1ff;
759 580244 : ps[2] = ( ( idxs[1] & 3 ) << 7 ) + ( idxs[0] >> 9 );
760 580244 : joint_index = ( ( idxs[2] << 16 ) + idxs[1] ) >> 2;
761 :
762 580244 : if ( joint_index >= joint_offset )
763 : {
764 487613 : joint_index = joint_index - joint_offset;
765 : }
766 :
767 580244 : ps[0] = joint_index / 5472;
768 580244 : ps[1] = joint_index - ps[0] * 5472;
769 580244 : fcb_decode_PI( ps[0], pos, 3 );
770 580244 : add_pulses( pos, pulsestrack[0], 0, code );
771 580244 : fcb_decode_PI( ps[1], pos, 3 );
772 580244 : add_pulses( pos, pulsestrack[1], 1, code );
773 :
774 580244 : dec_2p_2N1( ps[2], 4, 0, pos );
775 580244 : add_pulses( pos, pulsestrack[2], 2, code );
776 580244 : dec_2p_2N1( ps[3], 4, 0, pos );
777 580244 : add_pulses( pos, pulsestrack[3], 3, code );
778 :
779 580244 : return;
780 : }
781 :
782 : /*-------------------------------------------------------*
783 : * dec_1p_N1()
784 : *
785 : * Decode 1 pulse with N+1 bits
786 : *-------------------------------------------------------*/
787 :
788 252 : static void dec_1p_N1_L_subfr(
789 : const int32_t index, /* i : quantization index */
790 : const int16_t nb_pos, /* i : number of positions */
791 : const int16_t N, /* i : nb. of bits */
792 : int16_t pos[] /* o : pulse position */
793 : )
794 : {
795 : int16_t i, pos1;
796 : int32_t mask;
797 :
798 252 : mask = ( ( 1 << N ) - 1 );
799 252 : pos1 = (int16_t) ( index & mask );
800 252 : i = (int16_t) ( index >> N ) & 1;
801 :
802 252 : if ( i == 1 )
803 : {
804 123 : pos1 += nb_pos;
805 : }
806 :
807 252 : pos[0] = pos1;
808 :
809 252 : return;
810 : }
811 :
812 : /*-------------------------------------------------------*
813 : * add_pulses()
814 : *
815 : * Add decoded pulses to the codeword
816 : *-------------------------------------------------------*/
817 :
818 252 : static void add_pulses_L_subfr(
819 : const int16_t nb_pos, /* i : number of positions */
820 : const int16_t pos[], /* i : pulse position */
821 : const int16_t nb_pulse, /* i : nb. of pulses */
822 : const int16_t track, /* i : no. of the tracks */
823 : float code[] /* i/o: decoded codevector */
824 : )
825 : {
826 : int16_t i, k;
827 :
828 504 : for ( k = 0; k < nb_pulse; k++ )
829 : {
830 252 : i = ( ( pos[k] & ( nb_pos - 1 ) ) * NB_TRACK_FCB_4T ) + track;
831 252 : if ( ( pos[k] & nb_pos ) == 0 )
832 : {
833 129 : code[i] += 1.0f;
834 : }
835 : else
836 : {
837 123 : code[i] -= 1.0f;
838 : }
839 : }
840 :
841 252 : return;
842 : }
843 :
844 : /*----------------------------------------------------------------------------------*
845 : * dec_acelp_fast()
846 : *
847 : * fast algebraic codebook decoder
848 : *----------------------------------------------------------------------------------*/
849 :
850 429762 : void dec_acelp_fast(
851 : Decoder_State *st, /* i/o: decoder state structure */
852 : const int16_t cdk_index, /* i : codebook index */
853 : float code[], /* o : algebraic (fixed) codebook excitation */
854 : const int16_t L_subfr /* i : subframe length */
855 : )
856 : {
857 : int16_t k, pos[7], skip_track;
858 : int32_t L_index;
859 : PulseConfig config;
860 :
861 429762 : skip_track = -1;
862 429762 : set_f( code, 0.0f, L_subfr );
863 :
864 429762 : if ( L_subfr == L_SUBFR )
865 : {
866 358488 : config = PulseConfTable[cdk_index];
867 :
868 358488 : if ( cdk_index == 2 )
869 : {
870 77928 : dec_acelp_2t32( st, code );
871 : }
872 280560 : else if ( config.nb_pulse == 2 )
873 : {
874 : /* 10 bits, 2 pulses, 4 tracks 1010 (used only even tracks) */
875 20385 : for ( k = 0; k < NB_TRACK_FCB_4T - 1; k += 2 )
876 : {
877 13590 : L_index = get_next_indice( st, 5 );
878 13590 : dec_1p_N1( L_index, 4, 0, pos );
879 13590 : add_pulses( pos, 1, k, code );
880 : }
881 : }
882 273765 : else if ( config.nb_pulse == 3 )
883 : {
884 225231 : if ( config.codetrackpos == TRACKPOS_FIXED_FIRST )
885 : {
886 : /* 15 bits, 3 pulses, 4 tracks 1110 fixed track to first ? */
887 857328 : for ( k = 0; k < NB_TRACK_FCB_4T - 1; k++ )
888 : {
889 642996 : L_index = get_next_indice( st, 5 );
890 642996 : dec_1p_N1( L_index, 4, 0, pos );
891 642996 : add_pulses( pos, 1, k, code );
892 : }
893 : }
894 10899 : else if ( config.codetrackpos == TRACKPOS_FREE_THREE )
895 : {
896 : /* 17 bits, 3 pulses, 4 tracks (used all tracks) - 1110, 1101, 1011, 0111 */
897 10899 : skip_track = get_next_indice( st, 2 );
898 54495 : for ( k = 0; k < NB_TRACK_FCB_4T; k++ )
899 : {
900 43596 : if ( k != skip_track )
901 : {
902 32697 : L_index = get_next_indice( st, 5 );
903 32697 : dec_1p_N1( L_index, 4, 0, pos );
904 32697 : add_pulses( pos, 1, k, code );
905 : }
906 : }
907 : }
908 : }
909 : else
910 : {
911 48534 : if ( config.bits == 20 )
912 : {
913 20025 : skip_track = -1;
914 : }
915 28509 : else if ( config.codetrackpos == TRACKPOS_FIXED_FIRST )
916 : {
917 14505 : skip_track = 0;
918 : }
919 14004 : else if ( config.codetrackpos == TRACKPOS_FREE_ONE )
920 : {
921 14004 : skip_track = get_next_indice( st, 2 );
922 : }
923 :
924 242670 : for ( k = 0; k < NB_TRACK_FCB_4T; k++ )
925 : {
926 194136 : if ( k == skip_track )
927 : {
928 28509 : L_index = get_next_indice( st, 9 );
929 28509 : dec_2p_2N1( L_index, 4, 0, pos );
930 28509 : add_pulses( pos, 2, k, code );
931 : }
932 : else
933 : {
934 165627 : L_index = get_next_indice( st, 5 );
935 165627 : dec_1p_N1( L_index, 4, 0, pos );
936 165627 : add_pulses( pos, 1, k, code );
937 : }
938 : }
939 : }
940 : }
941 : else /* L_subfr == 2*L_SUBFR */
942 : {
943 71274 : config.bits = cdk_index;
944 :
945 71274 : if ( cdk_index == 14 )
946 : {
947 : /* 14 bits, 2 pulses, 2 tracks: 11 (used all tracks) */
948 : int16_t index, i0, i1;
949 :
950 71190 : index = get_next_indice( st, 14 );
951 :
952 71190 : i0 = ( ( index >> 7 ) & ( NB_POS_FCB_2T_128 - 1 ) ) * NB_TRACK_FCB_2T;
953 71190 : i1 = ( ( index & ( NB_POS_FCB_2T_128 - 1 ) ) * NB_TRACK_FCB_2T ) + 1;
954 :
955 71190 : code[i0] = -1.0f;
956 71190 : if ( ( index & 0x2000 ) == 0 )
957 : {
958 35652 : code[i0] = 1.0f;
959 : }
960 :
961 71190 : code[i1] = -1.0f;
962 71190 : if ( ( index & 0x40 ) == 0 )
963 : {
964 35823 : code[i1] = 1.0f;
965 : }
966 : }
967 84 : else if ( cdk_index == 12 )
968 : {
969 : /* 12 bits, 2 pulses, 4 tracks: 1010 (used only even tracks) */
970 0 : for ( k = 0; k < NB_TRACK_FCB_4T - 1; k += 2 )
971 : {
972 0 : L_index = get_next_indice( st, 6 );
973 0 : dec_1p_N1_L_subfr( L_index, NB_POS_FCB_4T_128, 5, pos );
974 0 : add_pulses_L_subfr( NB_POS_FCB_4T_128, pos, 1, k, code );
975 : }
976 : }
977 84 : else if ( cdk_index == 18 )
978 : {
979 : /* 18 bits, 3 pulses, 4 tracks: 1110 (used first three tracks) */
980 24 : for ( k = 0; k < NB_TRACK_FCB_4T - 1; k++ )
981 : {
982 18 : L_index = get_next_indice( st, 6 );
983 18 : dec_1p_N1_L_subfr( L_index, NB_POS_FCB_4T_128, 5, pos );
984 18 : add_pulses_L_subfr( NB_POS_FCB_4T_128, pos, 1, k, code );
985 : }
986 : }
987 78 : else if ( cdk_index == 20 )
988 : {
989 : /* 20 bits, 3 pulses, 4 tracks (used all tracks): 1110, 1101, 1011, 0111 */
990 78 : skip_track = get_next_indice( st, 2 );
991 390 : for ( k = 0; k < NB_TRACK_FCB_4T; k++ )
992 : {
993 312 : if ( k != skip_track )
994 : {
995 234 : L_index = get_next_indice( st, 6 );
996 234 : dec_1p_N1_L_subfr( L_index, NB_POS_FCB_4T_128, 5, pos );
997 234 : add_pulses_L_subfr( NB_POS_FCB_4T_128, pos, 1, k, code );
998 : }
999 : }
1000 : }
1001 0 : else if ( cdk_index == 24 )
1002 : {
1003 : /* 24 bits, 4 pulses, 4 tracks: 1111 */
1004 0 : for ( k = 0; k < NB_TRACK_FCB_4T; k++ )
1005 : {
1006 0 : L_index = get_next_indice( st, 6 );
1007 0 : dec_1p_N1_L_subfr( L_index, NB_POS_FCB_4T_128, 5, pos );
1008 0 : add_pulses_L_subfr( NB_POS_FCB_4T_128, pos, 1, k, code );
1009 : }
1010 : }
1011 : }
1012 :
1013 429762 : return;
1014 : }
|