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.h"
36 : #include "wmc_auto.h"
37 : #include "ivas_prot.h"
38 : #include "ivas_rom_com.h"
39 : #include "rom_dec.h"
40 : #ifdef DEBUGGING
41 : #include "debug.h"
42 : #endif
43 :
44 :
45 : /*---------------------------------------------------------------------*
46 : * read_GR2()
47 : *
48 : * reading a bitstream of data encoded with GR of order 2
49 : *---------------------------------------------------------------------*/
50 :
51 : /*! r: number of bits read */
52 8157 : static int16_t read_GR2(
53 : const uint16_t *bit_stream, /* i : bitstream to be read */
54 : int16_t *ind, /* o : parameters read */
55 : const int16_t len /* i : number of params to be read */
56 : )
57 : {
58 : int16_t i;
59 : uint16_t nb, ready, temp, b;
60 : const uint16_t *p;
61 :
62 8157 : p = bit_stream;
63 8157 : nb = 0;
64 :
65 75039 : for ( i = 0; i < len; i++ )
66 : {
67 66882 : ready = 0;
68 66882 : temp = 0;
69 : do
70 : {
71 124713 : b = *p++;
72 124713 : if ( b == 0 )
73 : {
74 66882 : ready = 1;
75 : }
76 : else
77 : {
78 57831 : temp += 1;
79 : }
80 124713 : } while ( ready == 0 );
81 66882 : b = *p++;
82 66882 : b = 2 * b + *p++;
83 66882 : ind[i] = 4 * temp + b;
84 66882 : nb += temp + 3;
85 : }
86 :
87 8157 : return nb;
88 : }
89 :
90 :
91 : /*---------------------------------------------------------------------*
92 : * read_GR1()
93 : *
94 : * reading a bitstream of data encoded with GR of order 1
95 : *---------------------------------------------------------------------*/
96 :
97 : /*! r: number of bits read */
98 199947 : static int16_t read_GR1(
99 : const uint16_t *bit_stream, /* i : bitstream to be read */
100 : int16_t *ind, /* o : parameters read */
101 : const int16_t len /* i : number of params to be read */
102 : )
103 : {
104 : int16_t i;
105 : uint16_t nb, ready, temp, b;
106 : const uint16_t *p;
107 :
108 199947 : p = bit_stream;
109 199947 : nb = 0;
110 :
111 954564 : for ( i = 0; i < len; i++ )
112 : {
113 754617 : ready = 0;
114 754617 : temp = 0;
115 : do
116 : {
117 1511748 : b = *p++;
118 1511748 : if ( b == 0 )
119 : {
120 754617 : ready = 1;
121 : }
122 : else
123 : {
124 757131 : temp += 1;
125 : }
126 1511748 : } while ( ready == 0 );
127 :
128 754617 : b = *p++;
129 754617 : ind[i] = 2 * temp + b;
130 754617 : nb +=
131 : temp + 2;
132 : }
133 :
134 199947 : return nb;
135 : }
136 :
137 :
138 : /*---------------------------------------------------------------------*
139 : * read_GR0()
140 : *
141 : * reading a bitstream of data encoded with GR of order 0
142 : *---------------------------------------------------------------------*/
143 :
144 : /*! r: number of bits read */
145 180375 : int16_t read_GR0(
146 : const uint16_t *bit_stream, /* i : bitstream to be read */
147 : int16_t *ind, /* o : parameters read */
148 : const int16_t len /* i : number of params to be read */
149 : )
150 : {
151 : int16_t i;
152 : uint16_t nb, ready, b, temp;
153 : const uint16_t *p;
154 :
155 180375 : p = bit_stream;
156 180375 : nb = 0;
157 :
158 1377312 : for ( i = 0; i < len; i++ )
159 : {
160 1196937 : ready = 0;
161 1196937 : temp = 0;
162 : do
163 : {
164 1955577 : b = *p++;
165 1955577 : if ( b == 0 )
166 : {
167 1196937 : ready = 1;
168 : }
169 : else
170 : {
171 758640 : temp += 1;
172 : }
173 1955577 : } while ( ready == 0 );
174 1196937 : ind[i] = temp;
175 1196937 : nb += temp + 1;
176 : }
177 :
178 180375 : return nb;
179 : }
180 :
181 :
182 : /*---------------------------------------------------------------------*
183 : * find_map()
184 : *
185 : * find the position of the value 'val' in the array 'map'
186 : *---------------------------------------------------------------------*/
187 :
188 : /*! r: index in array */
189 1133592 : static ivas_error find_map(
190 : int16_t *map_idx,
191 : const int16_t *map, /* i : array to look into */
192 : const int16_t val, /* i : value to look for */
193 : const int16_t len /* i : length of array */
194 : )
195 : {
196 1133592 : *map_idx = 0;
197 :
198 15532236 : while ( ( map[*map_idx] != val ) && ( *map_idx < len ) )
199 : {
200 14398644 : ( *map_idx )++;
201 : }
202 :
203 : #ifdef DEBUGGING
204 : if ( *map_idx == len )
205 : {
206 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "find_map(map, %d) : value not found!!", val );
207 : }
208 : #endif
209 :
210 1133592 : return IVAS_ERR_OK;
211 : }
212 :
213 :
214 : /*---------------------------------------------------------------------*
215 : * decode_adapt_GR_indices1()
216 : *
217 : * decodes input array using the adaptive GR procedure
218 : *---------------------------------------------------------------------*/
219 :
220 123879 : static void decode_adapt_GR_indices1(
221 : const int16_t *ind, /* i : array of input encoded symbols */
222 : const int16_t len, /* i : number of parameters to decode */
223 : const int16_t no_symb, /* i : number of possible symbols in GR coding */
224 : int16_t *out, /* o : array of decoded parameters */
225 : const int16_t *map0 /* i : initial mapping array for the adaptive GR */
226 : )
227 : {
228 : const int16_t *map;
229 : int16_t map_symb, i;
230 :
231 123879 : if ( no_symb == NO_SYMB_GR_SIDE_G )
232 : {
233 102252 : map = &map0[15 * no_symb];
234 : }
235 : else
236 : {
237 21627 : map = &( map0[8 * no_symb] );
238 : }
239 :
240 1257471 : for ( i = 0; i < len; i++ )
241 : {
242 1133592 : map_symb = ind[i];
243 1133592 : find_map( out + i, map, map_symb, no_symb );
244 1133592 : map = &( map0[out[i] * no_symb] );
245 : }
246 :
247 123879 : return;
248 : }
249 :
250 :
251 : /*---------------------------------------------------------------------*
252 : * get_value()
253 : *
254 : * reads a value on 'nbits' from a bitstream
255 : *---------------------------------------------------------------------*/
256 :
257 : /*! r: read value */
258 88515 : int16_t get_value(
259 : const uint16_t *bit_stream, /* i : bitstream */
260 : const int16_t nbits /* i : number of bits to be read */
261 : )
262 : {
263 : int16_t i;
264 88515 : uint16_t mask = 1, val = 0;
265 :
266 400413 : for ( i = nbits - 1; i >= 0; i-- )
267 : {
268 311898 : val += mask * bit_stream[i];
269 311898 : mask <<= 1;
270 : }
271 :
272 88515 : return val;
273 : }
274 :
275 :
276 : /*---------------------------------------------------------------------*
277 : * read_BS_GR()
278 : *
279 : * decode simple GR code from a bitstream
280 : *---------------------------------------------------------------------*/
281 :
282 : /*! r: number of bits read */
283 114255 : int16_t read_BS_GR(
284 : const uint16_t *bit_stream, /* i : bitstream to be read */
285 : const int16_t nb, /* i : starting point in bitstream */
286 : int16_t *ind1, /* o : data array read */
287 : const int16_t len, /* i : number of params to be read */
288 : int16_t *GR_ord /* o : GR order to be used */
289 : )
290 : {
291 : int16_t b, ind1_tmp[STEREO_DFT_BAND_MAX], tmp, i;
292 :
293 114255 : *GR_ord = bit_stream[nb];
294 114255 : b = 1;
295 :
296 114255 : if ( *GR_ord == 0 )
297 : {
298 112272 : b += read_GR0( &bit_stream[nb + b], ind1_tmp, len );
299 : }
300 : else
301 : {
302 1983 : b += read_GR1( &bit_stream[nb + b], ind1_tmp, len );
303 : }
304 :
305 972633 : for ( i = 0; i < len; i++ )
306 : {
307 858378 : tmp = ind1_tmp[i] + 1;
308 858378 : if ( tmp & 1 ) /* if odd number */
309 : {
310 696609 : ind1[i] = -( ind1_tmp[i] >> 1 );
311 : }
312 : else
313 : {
314 161769 : ind1[i] = tmp >> 1;
315 : }
316 : }
317 :
318 114255 : return b;
319 : }
320 :
321 :
322 : /*---------------------------------------------------------------------*
323 : * read_BS_adapt_GR_sg()
324 : *
325 : * read and decode with adaptive GR the bitstream containing side gains values
326 : *---------------------------------------------------------------------*/
327 :
328 : /*! r: number of bits read */
329 102252 : int16_t read_BS_adapt_GR_sg(
330 : const uint16_t *bit_stream, /* i : bitstream to be read */
331 : const int16_t nb, /* i : starting position in bitstream */
332 : int16_t *ind1, /* o : decoded side gain values */
333 : const int16_t len, /* i : number of params to be read */
334 : int16_t *GR_ord, /* o : GR order used (read from bitstream)*/
335 : const int16_t *map0 /* i : initial map */
336 : )
337 : {
338 : int16_t b, ind1_tmp[STEREO_DFT_BAND_MAX], ord;
339 :
340 : /* read first component */
341 102252 : b = 0;
342 102252 : b += read_GR1( &bit_stream[nb], ind1_tmp, 1 );
343 : /* read GR ord */
344 102252 : ord = bit_stream[nb + b];
345 102252 : b += 1;
346 :
347 102252 : if ( ord == 0 )
348 : {
349 67536 : *GR_ord = 1;
350 67536 : b += read_GR1( &bit_stream[nb + b], &ind1_tmp[1], len - 1 );
351 : }
352 : else
353 : {
354 34716 : ord = bit_stream[nb + b];
355 34716 : b += 1;
356 34716 : if ( ord == 0 )
357 : {
358 26559 : *GR_ord = 0;
359 :
360 26559 : b += read_GR0( &bit_stream[nb + b], &ind1_tmp[1], len - 1 );
361 : }
362 : else
363 : {
364 8157 : *GR_ord = 2;
365 8157 : b += read_GR2( &bit_stream[nb + b], &ind1_tmp[1], len - 1 );
366 : }
367 : }
368 :
369 102252 : decode_adapt_GR_indices1( ind1_tmp, len, NO_SYMB_GR_SIDE_G, ind1, map0 );
370 :
371 102252 : return b;
372 : }
373 :
374 :
375 : /*---------------------------------------------------------------------*
376 : * read_itd()
377 : *
378 : * read and decode ITD in DFT mode
379 : *---------------------------------------------------------------------*/
380 :
381 : /*! r: number of bits read */
382 87054 : int16_t read_itd(
383 : Decoder_State *st, /* i : Decoder state */
384 : int16_t *pI /* o : ITD value */
385 : )
386 : {
387 87054 : int16_t huff_flag, sign_flag, I, i, nb = 0, ready;
388 :
389 87054 : huff_flag = get_next_indice( st, 1 );
390 87054 : sign_flag = get_next_indice( st, 1 );
391 87054 : nb += 2;
392 :
393 87054 : if ( huff_flag == 1 )
394 : {
395 47568 : ready = 0;
396 47568 : I = 0;
397 :
398 333177 : while ( ( ready == 0 ) && ( nb < 10 ) )
399 : {
400 285609 : I = 2 * I + get_next_indice( st, 1 );
401 285609 : nb += 1;
402 :
403 5652009 : for ( i = 0; i < 20; i++ )
404 : {
405 5413968 : if ( ( I == dft_code_itd[i] ) && ( dft_len_itd[i] == ( nb - 2 ) ) )
406 : {
407 47568 : I = i;
408 47568 : ready = 1;
409 47568 : break;
410 : }
411 : }
412 : }
413 47568 : if ( ready == 0 )
414 : {
415 0 : printf( "Error reading Huffman code for ITD: \n" );
416 : }
417 : }
418 : else
419 : {
420 39486 : I = get_next_indice( st, STEREO_DFT_ITD_NBITS - 1 );
421 39486 : nb += STEREO_DFT_ITD_NBITS - 1;
422 : }
423 :
424 87054 : I += 256 * sign_flag;
425 87054 : *pI = I;
426 :
427 87054 : return nb;
428 : }
429 :
430 :
431 : /*---------------------------------------------------------------------*
432 : * read_BS_adapt_GR_rpg()
433 : *
434 : * read and decode residual prediction gain values using adaptive GR
435 : *---------------------------------------------------------------------*/
436 :
437 : /*! r: number of bits read */
438 24609 : int16_t read_BS_adapt_GR_rpg(
439 : const uint16_t *bit_stream, /* i : bitstream to be read */
440 : const int16_t nb, /* i : starting point in bitstream */
441 : int16_t *ind1_pred, /* o : decoded res pred gains */
442 : const int16_t start, /* i : starting subband */
443 : const int16_t total_no, /* i : number of params to be read */
444 : int16_t *GR_ord /* o : GR order - read */
445 : )
446 : {
447 : int16_t b, ind1_tmp[STEREO_DFT_BAND_MAX], i, len;
448 24609 : len = total_no - start;
449 :
450 : /* read first band */
451 24609 : b = read_GR1( &bit_stream[nb], ind1_tmp, 1 );
452 :
453 24609 : if ( ind1_tmp[0] == dft_maps_rpg[8 * NO_SYMB_GR_PRED_G] )
454 : {
455 21294 : for ( i = start; i < total_no; i++ )
456 : {
457 18312 : ind1_pred[i] = 0;
458 : }
459 : }
460 : else
461 : {
462 21627 : *GR_ord = bit_stream[nb + b]; /* GR order */
463 21627 : b += 1;
464 21627 : if ( *GR_ord == 0 )
465 : {
466 18060 : b += read_GR0( &bit_stream[nb + b], &ind1_tmp[1], len - 1 );
467 : }
468 : else
469 : {
470 : /* GR ord 1 */
471 3567 : b += read_GR1( &bit_stream[nb + b], &ind1_tmp[1], len - 1 );
472 : }
473 21627 : decode_adapt_GR_indices1( ind1_tmp, total_no - start, NO_SYMB_GR_PRED_G, &ind1_pred[start], dft_maps_rpg );
474 : }
475 :
476 24609 : return b;
477 : }
478 :
479 :
480 : /*---------------------------------------------------------------------*
481 : * read_flag_EC_DFT()
482 : *
483 : * read flag differentiating between: CBR/EC with adaptive GR/GR for differential coding
484 : *---------------------------------------------------------------------*/
485 :
486 : /*! r: number of bits read */
487 253875 : int16_t read_flag_EC_DFT(
488 : const uint16_t *bit_stream, /* i : bitstream */
489 : int16_t *flag /* o : flag value */
490 : )
491 : {
492 : int16_t flg;
493 :
494 253875 : flg = bit_stream[0];
495 253875 : if ( flg == 0 )
496 : {
497 126861 : *flag = flg;
498 126861 : return 1;
499 : }
500 : else
501 : {
502 127014 : *flag = 2 + bit_stream[1];
503 127014 : return 2;
504 : }
505 : }
|