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 3247 : 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 3247 : p = bit_stream;
59 3247 : nb = 0;
60 3247 : move16();
61 :
62 31834 : FOR( i = 0; i < len; i++ )
63 : {
64 28587 : ready = 0;
65 28587 : move16();
66 28587 : temp = 0;
67 28587 : move16();
68 : DO
69 : {
70 54564 : b = *p++;
71 54564 : move16();
72 54564 : IF( b == 0 )
73 : {
74 28587 : ready = 1;
75 28587 : move16();
76 : }
77 : ELSE
78 : {
79 25977 : temp = (UWord16) ( L_add( temp, 1 ) );
80 25977 : move16();
81 : }
82 : }
83 54564 : WHILE( ready == 0 );
84 28587 : b = *p++;
85 28587 : move16();
86 28587 : b = (UWord16) L_add( L_shl( b, 1 ), *p++ );
87 28587 : move16();
88 28587 : ind[i] = extract_l( L_add( L_shl( temp, 2 ), b ) );
89 28587 : move16();
90 28587 : nb = (UWord16) L_add( nb, L_add( temp, 3 ) );
91 28587 : move16();
92 : }
93 :
94 3247 : 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 81637 : 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 81637 : p = bit_stream;
116 81637 : nb = 0;
117 81637 : move16();
118 :
119 372700 : FOR( i = 0; i < len; i++ )
120 : {
121 291063 : ready = 0;
122 291063 : move16();
123 291063 : temp = 0;
124 291063 : move16();
125 : DO
126 : {
127 586485 : b = *p++;
128 586485 : move16();
129 586485 : IF( b == 0 )
130 : {
131 291063 : ready = 1;
132 291063 : move16();
133 : }
134 : ELSE
135 : {
136 295422 : temp = (UWord16) ( L_add( temp, 1 ) );
137 295422 : move16();
138 : }
139 : }
140 586485 : WHILE( ready == 0 );
141 :
142 291063 : b = *p++;
143 291063 : move16();
144 291063 : ind[i] = extract_l( L_add( L_shl( temp, 1 ), b ) ); /* Q0 */
145 291063 : move16();
146 291063 : nb = (UWord16) L_add( nb, L_add( temp, 2 ) );
147 291063 : move16();
148 : }
149 :
150 81637 : 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 80132 : 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 80132 : p = bit_stream;
172 80132 : nb = 0;
173 80132 : move16();
174 :
175 692250 : FOR( i = 0; i < len; i++ )
176 : {
177 612118 : ready = 0;
178 612118 : move16();
179 612118 : temp = 0;
180 612118 : move16();
181 : DO
182 : {
183 985011 : b = *p++;
184 985011 : move16();
185 985011 : IF( b == 0 )
186 : {
187 612118 : ready = 1;
188 612118 : move16();
189 : }
190 : ELSE
191 : {
192 372893 : temp = (UWord16) L_add( temp, 1 );
193 372893 : move16();
194 : }
195 : }
196 985011 : WHILE( ready == 0 );
197 612118 : ind[i] = temp;
198 612118 : move16();
199 612118 : nb = (UWord16) L_add( nb, L_add( temp, 1 ) );
200 612118 : move16();
201 : }
202 :
203 80132 : 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 493276 : 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 493276 : *map_idx = 0;
222 493276 : move16();
223 :
224 6841338 : WHILE( ( NE_16( map[*map_idx], val ) ) && ( NE_16( *map_idx, len ) ) )
225 : {
226 6348062 : test();
227 6348062 : *map_idx = add( *map_idx, 1 ); /* Q0 */
228 6348062 : move16();
229 : }
230 :
231 :
232 493276 : 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 51244 : 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 51244 : IF( EQ_16( no_symb, NO_SYMB_GR_SIDE_G ) )
254 : {
255 41404 : map = &map0[15 * no_symb];
256 : }
257 : ELSE
258 : {
259 9840 : map = &( map0[8 * no_symb] );
260 : }
261 :
262 544520 : FOR( i = 0; i < len; i++ )
263 : {
264 493276 : map_symb = ind[i];
265 493276 : move16();
266 493276 : find_map( out + i, map, map_symb, no_symb );
267 493276 : map = &( map0[out[i] * no_symb] );
268 : }
269 :
270 51244 : 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 41145 : 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 41145 : UWord16 mask = 0, val = 0;
288 41145 : move16();
289 41145 : move16();
290 :
291 181807 : FOR( i = nbits - 1; i >= 0; i-- )
292 : {
293 140662 : val = (UWord16) L_add( val, L_shl( bit_stream[i], mask ) );
294 140662 : mask = (UWord16) L_add( mask, 1 );
295 140662 : move16();
296 140662 : move16();
297 : }
298 :
299 41145 : 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 51047 : 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 51047 : *GR_ord = bit_stream[nb];
321 51047 : move16();
322 51047 : b = 1;
323 51047 : move16();
324 :
325 51047 : IF( *GR_ord == 0 )
326 : {
327 49856 : b = add( b, read_GR0( &bit_stream[nb + b], ind1_tmp, len ) ); /* Q0 */
328 : }
329 : ELSE
330 : {
331 1191 : b = add( b, read_GR1( &bit_stream[nb + b], ind1_tmp, len ) ); /* Q0 */
332 : }
333 :
334 478058 : FOR( i = 0; i < len; i++ )
335 : {
336 427011 : tmp = add( ind1_tmp[i], 1 );
337 427011 : IF( s_and( tmp, 1 ) ) /* if odd number */
338 : {
339 346663 : ind1[i] = negate( shr( ind1_tmp[i], 1 ) ); /* Q0 */
340 346663 : move16();
341 : }
342 : ELSE
343 : {
344 80348 : ind1[i] = shr( tmp, 1 ); /* Q0 */
345 80348 : move16();
346 : }
347 : }
348 :
349 51047 : 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 41404 : 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 41404 : b = 0;
373 41404 : move16();
374 41404 : b = add( b, read_GR1( &bit_stream[nb], ind1_tmp, 1 ) );
375 : /* read GR ord */
376 41404 : ord = bit_stream[nb + b];
377 41404 : move16();
378 41404 : b = add( b, 1 );
379 :
380 41404 : IF( ord == 0 )
381 : {
382 24235 : *GR_ord = 1;
383 24235 : move16();
384 24235 : b = add( b, read_GR1( &bit_stream[nb + b], &ind1_tmp[1], sub( len, 1 ) ) ); /* Q0 */
385 : }
386 : ELSE
387 : {
388 17169 : ord = bit_stream[nb + b];
389 17169 : move16();
390 17169 : b = b + 1;
391 17169 : IF( ord == 0 )
392 : {
393 13922 : *GR_ord = 0;
394 13922 : move16();
395 :
396 13922 : b = add( b, read_GR0( &bit_stream[nb + b], &ind1_tmp[1], sub( len, 1 ) ) ); /* Q0 */
397 : }
398 : ELSE
399 : {
400 3247 : *GR_ord = 2;
401 3247 : move16();
402 3247 : b = add( b, read_GR2( &bit_stream[nb + b], &ind1_tmp[1], sub( len, 1 ) ) ); /* Q0 */
403 : }
404 : }
405 :
406 41404 : decode_adapt_GR_indices1( ind1_tmp, len, NO_SYMB_GR_SIDE_G, ind1, map0 );
407 :
408 41404 : 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 30238 : Word16 read_itd(
420 : Decoder_State *st, /* i : Decoder state */
421 : Word16 *pI /* o : ITD value Q0*/
422 : )
423 : {
424 30238 : Word16 huff_flag, sign_flag, I, i, nb = 0, ready;
425 30238 : move16();
426 :
427 30238 : huff_flag = extract_l( get_next_indice_fx( st, 1 ) );
428 30238 : sign_flag = extract_l( get_next_indice_fx( st, 1 ) );
429 30238 : nb = add( nb, 2 );
430 :
431 30238 : IF( EQ_16( huff_flag, 1 ) )
432 : {
433 16964 : ready = 0;
434 16964 : move16();
435 16964 : I = 0;
436 16964 : move16();
437 :
438 115648 : WHILE( ( ready == 0 ) && ( LT_16( nb, 10 ) ) )
439 : {
440 98684 : test();
441 98684 : I = add( shl( I, 1 ), extract_l( get_next_indice_fx( st, 1 ) ) );
442 98684 : nb = add( nb, 1 );
443 :
444 1930353 : FOR( i = 0; i < 20; i++ )
445 : {
446 1848633 : test();
447 1848633 : IF( ( EQ_16( I, dft_code_itd[i] ) ) && ( EQ_16( dft_len_itd[i], ( sub( nb, 2 ) ) ) ) )
448 : {
449 16964 : I = i;
450 16964 : move16();
451 16964 : ready = 1;
452 16964 : move16();
453 16964 : BREAK;
454 : }
455 : }
456 : }
457 16964 : IF( ready == 0 )
458 : {
459 0 : printf( "Error reading Huffman code for ITD: \n" );
460 : }
461 : }
462 : ELSE
463 : {
464 13274 : I = extract_l( get_next_indice_fx( st, STEREO_DFT_ITD_NBITS - 1 ) );
465 13274 : nb = add( nb, STEREO_DFT_ITD_NBITS - 1 );
466 : }
467 :
468 30238 : I = add( I, shl( sign_flag, 8 ) );
469 30238 : *pI = I;
470 30238 : move16();
471 :
472 30238 : 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 13409 : 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 13409 : len = sub( total_no, start );
494 :
495 : /* read first band */
496 13409 : b = read_GR1( &bit_stream[nb], ind1_tmp, 1 ); /* Q0 */
497 :
498 13409 : IF( EQ_16( ind1_tmp[0], dft_maps_rpg[8 * NO_SYMB_GR_PRED_G] ) )
499 : {
500 31558 : FOR( i = start; i < total_no; i++ )
501 : {
502 27989 : ind1_pred[i] = 0;
503 27989 : move16();
504 : }
505 : }
506 : ELSE
507 : {
508 9840 : *GR_ord = bit_stream[nb + b]; /* GR order */
509 9840 : move16();
510 9840 : b = add( b, 1 );
511 9840 : IF( *GR_ord == 0 )
512 : {
513 8442 : 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 1398 : b = add( b, read_GR1( &bit_stream[nb + b], &ind1_tmp[1], sub( len, 1 ) ) ); /* Q0 */
519 : }
520 9840 : decode_adapt_GR_indices1( ind1_tmp, sub( total_no, start ), NO_SYMB_GR_PRED_G, &ind1_pred[start], dft_maps_rpg );
521 : }
522 :
523 13409 : 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 111244 : 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 111244 : flg = bit_stream[0];
542 111244 : move16();
543 111244 : IF( flg == 0 )
544 : {
545 54813 : *flag = flg;
546 54813 : move16();
547 54813 : return 1;
548 : }
549 : ELSE
550 : {
551 56431 : *flag = add( 2, bit_stream[1] );
552 56431 : move16();
553 56431 : return 2;
554 : }
555 : }
|