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 41254 : 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 41254 : p = bit_stream;
63 41254 : nb = 0;
64 :
65 423261 : for ( i = 0; i < len; i++ )
66 : {
67 382007 : ready = 0;
68 382007 : temp = 0;
69 : do
70 : {
71 739462 : b = *p++;
72 739462 : if ( b == 0 )
73 : {
74 382007 : ready = 1;
75 : }
76 : else
77 : {
78 357455 : temp += 1;
79 : }
80 739462 : } while ( ready == 0 );
81 382007 : b = *p++;
82 382007 : b = 2 * b + *p++;
83 382007 : ind[i] = 4 * temp + b;
84 382007 : nb += temp + 3;
85 : }
86 :
87 41254 : 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 1135983 : 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 1135983 : p = bit_stream;
109 1135983 : nb = 0;
110 :
111 4890129 : for ( i = 0; i < len; i++ )
112 : {
113 3754146 : ready = 0;
114 3754146 : temp = 0;
115 : do
116 : {
117 7881052 : b = *p++;
118 7881052 : if ( b == 0 )
119 : {
120 3754146 : ready = 1;
121 : }
122 : else
123 : {
124 4126906 : temp += 1;
125 : }
126 7881052 : } while ( ready == 0 );
127 :
128 3754146 : b = *p++;
129 3754146 : ind[i] = 2 * temp + b;
130 3754146 : nb +=
131 : temp + 2;
132 : }
133 :
134 1135983 : 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 1102478 : 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 1102478 : p = bit_stream;
156 1102478 : nb = 0;
157 :
158 9815250 : for ( i = 0; i < len; i++ )
159 : {
160 8712772 : ready = 0;
161 8712772 : temp = 0;
162 : do
163 : {
164 13440542 : b = *p++;
165 13440542 : if ( b == 0 )
166 : {
167 8712772 : ready = 1;
168 : }
169 : else
170 : {
171 4727770 : temp += 1;
172 : }
173 13440542 : } while ( ready == 0 );
174 8712772 : ind[i] = temp;
175 8712772 : nb += temp + 1;
176 : }
177 :
178 1102478 : 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 6535285 : 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 6535285 : *map_idx = 0;
197 :
198 94084476 : while ( ( map[*map_idx] != val ) && ( *map_idx < len ) )
199 : {
200 87549191 : ( *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 6535285 : 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 685226 : 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 685226 : if ( no_symb == NO_SYMB_GR_SIDE_G )
232 : {
233 571926 : map = &map0[15 * no_symb];
234 : }
235 : else
236 : {
237 113300 : map = &( map0[8 * no_symb] );
238 : }
239 :
240 7220511 : for ( i = 0; i < len; i++ )
241 : {
242 6535285 : map_symb = ind[i];
243 6535285 : find_map( out + i, map, map_symb, no_symb );
244 6535285 : map = &( map0[out[i] * no_symb] );
245 : }
246 :
247 685226 : 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 438051 : 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 438051 : uint16_t mask = 1, val = 0;
265 :
266 1872936 : for ( i = nbits - 1; i >= 0; i-- )
267 : {
268 1434885 : val += mask * bit_stream[i];
269 1434885 : mask <<= 1;
270 : }
271 :
272 438051 : 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 756342 : 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 756342 : *GR_ord = bit_stream[nb];
294 756342 : b = 1;
295 :
296 756342 : if ( *GR_ord == 0 )
297 : {
298 742674 : b += read_GR0( &bit_stream[nb + b], ind1_tmp, len );
299 : }
300 : else
301 : {
302 13668 : b += read_GR1( &bit_stream[nb + b], ind1_tmp, len );
303 : }
304 :
305 6917061 : for ( i = 0; i < len; i++ )
306 : {
307 6160719 : tmp = ind1_tmp[i] + 1;
308 6160719 : if ( tmp & 1 ) /* if odd number */
309 : {
310 5112135 : ind1[i] = -( ind1_tmp[i] >> 1 );
311 : }
312 : else
313 : {
314 1048584 : ind1[i] = tmp >> 1;
315 : }
316 : }
317 :
318 756342 : 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 571926 : 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 571926 : b = 0;
342 571926 : b += read_GR1( &bit_stream[nb], ind1_tmp, 1 );
343 : /* read GR ord */
344 571926 : ord = bit_stream[nb + b];
345 571926 : b += 1;
346 :
347 571926 : if ( ord == 0 )
348 : {
349 311952 : *GR_ord = 1;
350 311952 : b += read_GR1( &bit_stream[nb + b], &ind1_tmp[1], len - 1 );
351 : }
352 : else
353 : {
354 259974 : ord = bit_stream[nb + b];
355 259974 : b += 1;
356 259974 : if ( ord == 0 )
357 : {
358 218720 : *GR_ord = 0;
359 :
360 218720 : b += read_GR0( &bit_stream[nb + b], &ind1_tmp[1], len - 1 );
361 : }
362 : else
363 : {
364 41254 : *GR_ord = 2;
365 41254 : b += read_GR2( &bit_stream[nb + b], &ind1_tmp[1], len - 1 );
366 : }
367 : }
368 :
369 571926 : decode_adapt_GR_indices1( ind1_tmp, len, NO_SYMB_GR_SIDE_G, ind1, map0 );
370 :
371 571926 : 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 333772 : int16_t read_itd(
383 : Decoder_State *st, /* i : Decoder state */
384 : int16_t *pI /* o : ITD value */
385 : )
386 : {
387 333772 : int16_t huff_flag, sign_flag, I, i, nb = 0, ready;
388 :
389 333772 : huff_flag = get_next_indice( st, 1 );
390 333772 : sign_flag = get_next_indice( st, 1 );
391 333772 : nb += 2;
392 :
393 333772 : if ( huff_flag == 1 )
394 : {
395 235895 : ready = 0;
396 235895 : I = 0;
397 :
398 1318038 : while ( ( ready == 0 ) && ( nb < 10 ) )
399 : {
400 1082143 : I = 2 * I + get_next_indice( st, 1 );
401 1082143 : nb += 1;
402 :
403 19704577 : for ( i = 0; i < 20; i++ )
404 : {
405 18858329 : if ( ( I == dft_code_itd[i] ) && ( dft_len_itd[i] == ( nb - 2 ) ) )
406 : {
407 235895 : I = i;
408 235895 : ready = 1;
409 235895 : break;
410 : }
411 : }
412 : }
413 235895 : if ( ready == 0 )
414 : {
415 0 : printf( "Error reading Huffman code for ITD: \n" );
416 : }
417 : }
418 : else
419 : {
420 97877 : I = get_next_indice( st, STEREO_DFT_ITD_NBITS - 1 );
421 97877 : nb += STEREO_DFT_ITD_NBITS - 1;
422 : }
423 :
424 333772 : I += 256 * sign_flag;
425 333772 : *pI = I;
426 :
427 333772 : 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 220286 : 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 220286 : len = total_no - start;
449 :
450 : /* read first band */
451 220286 : b = read_GR1( &bit_stream[nb], ind1_tmp, 1 );
452 :
453 220286 : if ( ind1_tmp[0] == dft_maps_rpg[8 * NO_SYMB_GR_PRED_G] )
454 : {
455 872848 : for ( i = start; i < total_no; i++ )
456 : {
457 765862 : ind1_pred[i] = 0;
458 : }
459 : }
460 : else
461 : {
462 113300 : *GR_ord = bit_stream[nb + b]; /* GR order */
463 113300 : b += 1;
464 113300 : if ( *GR_ord == 0 )
465 : {
466 95149 : b += read_GR0( &bit_stream[nb + b], &ind1_tmp[1], len - 1 );
467 : }
468 : else
469 : {
470 : /* GR ord 1 */
471 18151 : b += read_GR1( &bit_stream[nb + b], &ind1_tmp[1], len - 1 );
472 : }
473 113300 : decode_adapt_GR_indices1( ind1_tmp, total_no - start, NO_SYMB_GR_PRED_G, &ind1_pred[start], dft_maps_rpg );
474 : }
475 :
476 220286 : 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 1607982 : 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 1607982 : flg = bit_stream[0];
495 1607982 : if ( flg == 0 )
496 : {
497 792212 : *flag = flg;
498 792212 : return 1;
499 : }
500 : else
501 : {
502 815770 : *flag = 2 + bit_stream[1];
503 815770 : return 2;
504 : }
505 : }
|