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 : #include <stdint.h>
34 : #include "options.h"
35 : #include "prot_fx.h"
36 : #include "wmc_auto.h"
37 : #include "ivas_rom_com.h"
38 : #include "rom_dec.h"
39 : #include "ivas_prot_fx.h"
40 :
41 : /*---------------------------------------------------------------------*
42 : * read_GR2()
43 : *
44 : * reading a bitstream of data encoded with GR of order 2
45 : *---------------------------------------------------------------------*/
46 :
47 : /*! r: number of bits read */
48 2721 : static Word16 read_GR2(
49 : const UWord16 *bit_stream, /* i : bitstream to be read Q0*/
50 : Word16 *ind, /* o : parameters read Q0*/
51 : const Word16 len /* i : number of params to be read Q0*/
52 : )
53 : {
54 : Word16 i;
55 : UWord16 nb, ready, temp, b;
56 : const UWord16 *p;
57 :
58 2721 : p = bit_stream;
59 2721 : nb = 0;
60 2721 : move16();
61 :
62 24932 : FOR( i = 0; i < len; i++ )
63 : {
64 22211 : ready = 0;
65 22211 : move16();
66 22211 : temp = 0;
67 22211 : move16();
68 : DO
69 : {
70 41321 : b = *p++;
71 41321 : move16();
72 41321 : IF( b == 0 )
73 : {
74 22211 : ready = 1;
75 22211 : move16();
76 : }
77 : ELSE
78 : {
79 19110 : temp = (UWord16) ( L_add( temp, 1 ) );
80 19110 : move16();
81 : }
82 : }
83 41321 : WHILE( ready == 0 );
84 22211 : b = *p++;
85 22211 : move16();
86 22211 : b = (UWord16) L_add( L_shl( b, 1 ), *p++ );
87 22211 : move16();
88 22211 : ind[i] = extract_l( L_add( L_shl( temp, 2 ), b ) );
89 22211 : move16();
90 22211 : nb = (UWord16) L_add( nb, L_add( temp, 3 ) );
91 22211 : move16();
92 : }
93 :
94 2721 : return nb;
95 : }
96 :
97 :
98 : /*---------------------------------------------------------------------*
99 : * read_GR1()
100 : *
101 : * reading a bitstream of data encoded with GR of order 1
102 : *---------------------------------------------------------------------*/
103 :
104 : /*! r: number of bits read */
105 66798 : static Word16 read_GR1(
106 : const UWord16 *bit_stream, /* i : bitstream to be read Q0*/
107 : Word16 *ind, /* o : parameters read Q0*/
108 : const Word16 len /* i : number of params to be read Q0*/
109 : )
110 : {
111 : Word16 i;
112 : UWord16 nb, ready, temp, b;
113 : const UWord16 *p;
114 :
115 66798 : p = bit_stream;
116 66798 : nb = 0;
117 66798 : move16();
118 :
119 319091 : FOR( i = 0; i < len; i++ )
120 : {
121 252293 : ready = 0;
122 252293 : move16();
123 252293 : temp = 0;
124 252293 : move16();
125 : DO
126 : {
127 506369 : b = *p++;
128 506369 : move16();
129 506369 : IF( b == 0 )
130 : {
131 252293 : ready = 1;
132 252293 : move16();
133 : }
134 : ELSE
135 : {
136 254076 : temp = (UWord16) ( L_add( temp, 1 ) );
137 254076 : move16();
138 : }
139 : }
140 506369 : WHILE( ready == 0 );
141 :
142 252293 : b = *p++;
143 252293 : move16();
144 252293 : ind[i] = extract_l( L_add( L_shl( temp, 1 ), b ) ); /* Q0 */
145 252293 : move16();
146 252293 : nb = (UWord16) L_add( nb, L_add( temp, 2 ) );
147 252293 : move16();
148 : }
149 :
150 66798 : return nb;
151 : }
152 :
153 :
154 : /*---------------------------------------------------------------------*
155 : * read_GR0()
156 : *
157 : * reading a bitstream of data encoded with GR of order 0
158 : *---------------------------------------------------------------------*/
159 :
160 : /*! r: number of bits read */
161 60523 : Word16 read_GR0(
162 : const UWord16 *bit_stream, /* i : bitstream to be read Q0*/
163 : Word16 *ind, /* o : parameters read Q0*/
164 : const Word16 len /* i : number of params to be read Q0*/
165 : )
166 : {
167 : Word16 i;
168 : UWord16 nb, ready, b, temp;
169 : const UWord16 *p;
170 :
171 60523 : p = bit_stream;
172 60523 : nb = 0;
173 60523 : move16();
174 :
175 459359 : FOR( i = 0; i < len; i++ )
176 : {
177 398836 : ready = 0;
178 398836 : move16();
179 398836 : temp = 0;
180 398836 : move16();
181 : DO
182 : {
183 651848 : b = *p++;
184 651848 : move16();
185 651848 : IF( b == 0 )
186 : {
187 398836 : ready = 1;
188 398836 : move16();
189 : }
190 : ELSE
191 : {
192 253012 : temp = (UWord16) L_add( temp, 1 );
193 253012 : move16();
194 : }
195 : }
196 651848 : WHILE( ready == 0 );
197 398836 : ind[i] = temp;
198 398836 : move16();
199 398836 : nb = (UWord16) L_add( nb, L_add( temp, 1 ) );
200 398836 : move16();
201 : }
202 :
203 60523 : return nb;
204 : }
205 :
206 :
207 : /*---------------------------------------------------------------------*
208 : * find_map()
209 : *
210 : * find the position of the value 'val' in the array 'map'
211 : *---------------------------------------------------------------------*/
212 :
213 : /*! r: index in array */
214 378826 : static ivas_error find_map(
215 : Word16 *map_idx, /* Q0 */
216 : const Word16 *map, /* i : array to look into Q0*/
217 : const Word16 val, /* i : value to look for Q0*/
218 : const Word16 len /* i : length of array Q0*/
219 : )
220 : {
221 378826 : *map_idx = 0;
222 378826 : move16();
223 :
224 5184937 : WHILE( ( NE_16( map[*map_idx], val ) ) && ( NE_16( *map_idx, len ) ) )
225 : {
226 4806111 : test();
227 4806111 : *map_idx = add( *map_idx, 1 ); /* Q0 */
228 4806111 : move16();
229 : }
230 :
231 :
232 378826 : return IVAS_ERR_OK;
233 : }
234 :
235 :
236 : /*---------------------------------------------------------------------*
237 : * decode_adapt_GR_indices1()
238 : *
239 : * decodes input array using the adaptive GR procedure
240 : *---------------------------------------------------------------------*/
241 :
242 41430 : static void decode_adapt_GR_indices1(
243 : const Word16 *ind, /* i : array of input encoded symbols Q0*/
244 : const Word16 len, /* i : number of parameters to decode Q0*/
245 : const Word16 no_symb, /* i : number of possible symbols in GR coding Q0*/
246 : Word16 *out, /* o : array of decoded parameters Q0*/
247 : const Word16 *map0 /* i : initial mapping array for the adaptive GR Q0*/
248 : )
249 : {
250 : const Word16 *map;
251 : Word16 map_symb, i;
252 :
253 41430 : IF( EQ_16( no_symb, NO_SYMB_GR_SIDE_G ) )
254 : {
255 34133 : map = &map0[15 * no_symb];
256 : }
257 : ELSE
258 : {
259 7297 : map = &( map0[8 * no_symb] );
260 : }
261 :
262 420256 : FOR( i = 0; i < len; i++ )
263 : {
264 378826 : map_symb = ind[i];
265 378826 : move16();
266 378826 : find_map( out + i, map, map_symb, no_symb );
267 378826 : map = &( map0[out[i] * no_symb] );
268 : }
269 :
270 41430 : return;
271 : }
272 :
273 :
274 : /*---------------------------------------------------------------------*
275 : * get_value()
276 : *
277 : * reads a value on 'nbits' from a bitstream
278 : *---------------------------------------------------------------------*/
279 :
280 : /*! r: read value */
281 29448 : Word16 get_value(
282 : const UWord16 *bit_stream, /* i : bitstream Q0*/
283 : const Word16 nbits /* i : number of bits to be read Q0*/
284 : )
285 : {
286 : Word16 i;
287 29448 : UWord16 mask = 0, val = 0;
288 29448 : move16();
289 29448 : move16();
290 :
291 133208 : FOR( i = nbits - 1; i >= 0; i-- )
292 : {
293 103760 : val = (UWord16) L_add( val, L_shl( bit_stream[i], mask ) );
294 103760 : mask = (UWord16) L_add( mask, 1 );
295 103760 : move16();
296 103760 : move16();
297 : }
298 :
299 29448 : return val;
300 : }
301 :
302 :
303 : /*---------------------------------------------------------------------*
304 : * read_BS_GR()
305 : *
306 : * decode simple GR code from a bitstream
307 : *---------------------------------------------------------------------*/
308 :
309 : /*! r: number of bits read */
310 38042 : Word16 read_BS_GR(
311 : const UWord16 *bit_stream, /* i : bitstream to be read Q0*/
312 : const Word16 nb, /* i : starting point in bitstream Q0*/
313 : Word16 *ind1, /* o : data array read Q0*/
314 : const Word16 len, /* i : number of params to be read Q0*/
315 : Word16 *GR_ord /* o : GR order to be used Q0*/
316 : )
317 : {
318 : Word16 b, ind1_tmp[STEREO_DFT_BAND_MAX], tmp, i;
319 :
320 38042 : *GR_ord = bit_stream[nb];
321 38042 : move16();
322 38042 : b = 1;
323 38042 : move16();
324 :
325 38042 : IF( *GR_ord == 0 )
326 : {
327 37378 : b = add( b, read_GR0( &bit_stream[nb + b], ind1_tmp, len ) ); /* Q0 */
328 : }
329 : ELSE
330 : {
331 664 : b = add( b, read_GR1( &bit_stream[nb + b], ind1_tmp, len ) ); /* Q0 */
332 : }
333 :
334 323416 : FOR( i = 0; i < len; i++ )
335 : {
336 285374 : tmp = add( ind1_tmp[i], 1 );
337 285374 : IF( s_and( tmp, 1 ) ) /* if odd number */
338 : {
339 231827 : ind1[i] = negate( shr( ind1_tmp[i], 1 ) ); /* Q0 */
340 231827 : move16();
341 : }
342 : ELSE
343 : {
344 53547 : ind1[i] = shr( tmp, 1 ); /* Q0 */
345 53547 : move16();
346 : }
347 : }
348 :
349 38042 : return b;
350 : }
351 :
352 :
353 : /*---------------------------------------------------------------------*
354 : * read_BS_adapt_GR_sg()
355 : *
356 : * read and decode with adaptive GR the bitstream containing side gains values
357 : *---------------------------------------------------------------------*/
358 :
359 : /*! r: number of bits read */
360 34133 : Word16 read_BS_adapt_GR_sg(
361 : const UWord16 *bit_stream, /* i : bitstream to be read Q0*/
362 : const Word16 nb, /* i : starting position in bitstream Q0*/
363 : Word16 *ind1, /* o : decoded side gain values Q0*/
364 : const Word16 len, /* i : number of params to be read Q0*/
365 : Word16 *GR_ord, /* o : GR order used (read from bitstream) Q0*/
366 : const Word16 *map0 /* i : initial map Q0*/
367 : )
368 : {
369 : Word16 b, ind1_tmp[STEREO_DFT_BAND_MAX], ord;
370 :
371 : /* read first component */
372 34133 : b = 0;
373 34133 : move16();
374 34133 : b = add( b, read_GR1( &bit_stream[nb], ind1_tmp, 1 ) );
375 : /* read GR ord */
376 34133 : ord = bit_stream[nb + b];
377 34133 : move16();
378 34133 : b = add( b, 1 );
379 :
380 34133 : IF( ord == 0 )
381 : {
382 22582 : *GR_ord = 1;
383 22582 : move16();
384 22582 : b = add( b, read_GR1( &bit_stream[nb + b], &ind1_tmp[1], sub( len, 1 ) ) ); /* Q0 */
385 : }
386 : ELSE
387 : {
388 11551 : ord = bit_stream[nb + b];
389 11551 : move16();
390 11551 : b = b + 1;
391 11551 : IF( ord == 0 )
392 : {
393 8830 : *GR_ord = 0;
394 8830 : move16();
395 :
396 8830 : b = add( b, read_GR0( &bit_stream[nb + b], &ind1_tmp[1], sub( len, 1 ) ) ); /* Q0 */
397 : }
398 : ELSE
399 : {
400 2721 : *GR_ord = 2;
401 2721 : move16();
402 2721 : b = add( b, read_GR2( &bit_stream[nb + b], &ind1_tmp[1], sub( len, 1 ) ) ); /* Q0 */
403 : }
404 : }
405 :
406 34133 : decode_adapt_GR_indices1( ind1_tmp, len, NO_SYMB_GR_SIDE_G, ind1, map0 );
407 :
408 34133 : return b;
409 : }
410 :
411 :
412 : /*---------------------------------------------------------------------*
413 : * read_itd()
414 : *
415 : * read and decode ITD in DFT mode
416 : *---------------------------------------------------------------------*/
417 :
418 : /*! r: number of bits read */
419 28927 : Word16 read_itd(
420 : Decoder_State *st, /* i : Decoder state */
421 : Word16 *pI /* o : ITD value Q0*/
422 : )
423 : {
424 28927 : Word16 huff_flag, sign_flag, I, i, nb = 0, ready;
425 28927 : move16();
426 :
427 28927 : huff_flag = extract_l( get_next_indice_fx( st, 1 ) );
428 28927 : sign_flag = extract_l( get_next_indice_fx( st, 1 ) );
429 28927 : nb = add( nb, 2 );
430 :
431 28927 : IF( EQ_16( huff_flag, 1 ) )
432 : {
433 15700 : ready = 0;
434 15700 : move16();
435 15700 : I = 0;
436 15700 : move16();
437 :
438 110012 : WHILE( ( ready == 0 ) && ( LT_16( nb, 10 ) ) )
439 : {
440 94312 : test();
441 94312 : I = add( shl( I, 1 ), extract_l( get_next_indice_fx( st, 1 ) ) );
442 94312 : nb = add( nb, 1 );
443 :
444 1866865 : FOR( i = 0; i < 20; i++ )
445 : {
446 1788253 : test();
447 1788253 : IF( ( EQ_16( I, dft_code_itd[i] ) ) && ( EQ_16( dft_len_itd[i], ( sub( nb, 2 ) ) ) ) )
448 : {
449 15700 : I = i;
450 15700 : move16();
451 15700 : ready = 1;
452 15700 : move16();
453 15700 : BREAK;
454 : }
455 : }
456 : }
457 15700 : IF( ready == 0 )
458 : {
459 0 : printf( "Error reading Huffman code for ITD: \n" );
460 : }
461 : }
462 : ELSE
463 : {
464 13227 : I = extract_l( get_next_indice_fx( st, STEREO_DFT_ITD_NBITS - 1 ) );
465 13227 : nb = add( nb, STEREO_DFT_ITD_NBITS - 1 );
466 : }
467 :
468 28927 : I = add( I, shl( sign_flag, 8 ) );
469 28927 : *pI = I;
470 28927 : move16();
471 :
472 28927 : return nb;
473 : }
474 :
475 :
476 : /*---------------------------------------------------------------------*
477 : * read_BS_adapt_GR_rpg()
478 : *
479 : * read and decode residual prediction gain values using adaptive GR
480 : *---------------------------------------------------------------------*/
481 :
482 : /*! r: number of bits read */
483 8245 : Word16 read_BS_adapt_GR_rpg(
484 : const UWord16 *bit_stream, /* i : bitstream to be read Q0*/
485 : const Word16 nb, /* i : starting point in bitstream Q0*/
486 : Word16 *ind1_pred, /* o : decoded res pred gains Q0*/
487 : const Word16 start, /* i : starting subband Q0*/
488 : const Word16 total_no, /* i : number of params to be read Q0*/
489 : Word16 *GR_ord /* o : GR order - read Q0*/
490 : )
491 : {
492 : Word16 b, ind1_tmp[STEREO_DFT_BAND_MAX], i, len;
493 8245 : len = sub( total_no, start );
494 :
495 : /* read first band */
496 8245 : b = read_GR1( &bit_stream[nb], ind1_tmp, 1 ); /* Q0 */
497 :
498 8245 : IF( EQ_16( ind1_tmp[0], dft_maps_rpg[8 * NO_SYMB_GR_PRED_G] ) )
499 : {
500 6642 : FOR( i = start; i < total_no; i++ )
501 : {
502 5694 : ind1_pred[i] = 0;
503 5694 : move16();
504 : }
505 : }
506 : ELSE
507 : {
508 7297 : *GR_ord = bit_stream[nb + b]; /* GR order */
509 7297 : move16();
510 7297 : b = add( b, 1 );
511 7297 : IF( *GR_ord == 0 )
512 : {
513 6123 : b = add( b, read_GR0( &bit_stream[nb + b], &ind1_tmp[1], sub( len, 1 ) ) ); /* Q0 */
514 : }
515 : ELSE
516 : {
517 : /* GR ord 1 */
518 1174 : b = add( b, read_GR1( &bit_stream[nb + b], &ind1_tmp[1], sub( len, 1 ) ) ); /* Q0 */
519 : }
520 7297 : decode_adapt_GR_indices1( ind1_tmp, sub( total_no, start ), NO_SYMB_GR_PRED_G, &ind1_pred[start], dft_maps_rpg );
521 : }
522 :
523 8245 : return b;
524 : }
525 :
526 :
527 : /*---------------------------------------------------------------------*
528 : * read_flag_EC_DFT()
529 : *
530 : * read flag differentiating between: CBR/EC with adaptive GR/GR for differential coding
531 : *---------------------------------------------------------------------*/
532 :
533 : /*! r: number of bits read */
534 84660 : Word16 read_flag_EC_DFT(
535 : const UWord16 *bit_stream, /* i : bitstream Q0*/
536 : Word16 *flag /* o : flag value Q0*/
537 : )
538 : {
539 : Word16 flg;
540 :
541 84660 : flg = bit_stream[0];
542 84660 : move16();
543 84660 : IF( flg == 0 )
544 : {
545 42378 : *flag = flg;
546 42378 : move16();
547 42378 : return 1;
548 : }
549 : ELSE
550 : {
551 42282 : *flag = add( 2, bit_stream[1] );
552 42282 : move16();
553 42282 : return 2;
554 : }
555 : }
|