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 <assert.h>
38 : #include <stdint.h>
39 : #include "options.h"
40 : #include "cnst.h"
41 : #include "prot_fx.h"
42 : #include "stat_enc.h"
43 : #include "stat_dec.h"
44 : #include "rom_com.h"
45 : #include "mime.h"
46 : #include "ivas_cnst.h"
47 : #include "ivas_rom_com.h"
48 : #include "wmc_auto.h"
49 : #include "ivas_prot_fx.h"
50 : #include "prot_fx_enc.h"
51 :
52 :
53 : #define STEP_MAX_NUM_INDICES 100 /* increase the maximum number of allowed indices in the list by this amount */
54 :
55 :
56 : /*-------------------------------------------------------------------*
57 : * rate2AMRWB_IOmode()
58 : *
59 : * lookup AMRWB IO mode
60 : *-------------------------------------------------------------------*/
61 :
62 3100 : static Word16 rate2AMRWB_IOmode(
63 : Word32 brate /* i : bitrate */
64 : )
65 : {
66 3100 : switch ( brate )
67 : {
68 : /* EVS AMR-WB IO modes */
69 0 : case SID_1k75:
70 0 : return AMRWB_IO_SID;
71 0 : case ACELP_6k60:
72 0 : return AMRWB_IO_6600;
73 0 : case ACELP_8k85:
74 0 : return AMRWB_IO_8850;
75 0 : case ACELP_12k65:
76 0 : return AMRWB_IO_1265;
77 0 : case ACELP_14k25:
78 0 : return AMRWB_IO_1425;
79 0 : case ACELP_15k85:
80 0 : return AMRWB_IO_1585;
81 0 : case ACELP_18k25:
82 0 : return AMRWB_IO_1825;
83 0 : case ACELP_19k85:
84 0 : return AMRWB_IO_1985;
85 0 : case ACELP_23k05:
86 0 : return AMRWB_IO_2305;
87 0 : case ACELP_23k85:
88 0 : return AMRWB_IO_2385;
89 3100 : default:
90 3100 : break;
91 : }
92 :
93 3100 : return -1;
94 : }
95 :
96 : /*-------------------------------------------------------------------*
97 : * rate2EVSmode()
98 : *
99 : * lookup EVS mode
100 : *-------------------------------------------------------------------*/
101 3100 : Word16 rate2EVSmode_float(
102 : const Word32 brate, /* i : bitrate */
103 : int16_t *is_amr_wb /* o : (flag) does the bitrate belong to AMR-WB? Can be NULL */
104 : )
105 : {
106 3100 : if ( is_amr_wb != NULL )
107 : {
108 0 : *is_amr_wb = 0;
109 : }
110 :
111 3100 : switch ( brate )
112 : {
113 : /* EVS Primary modes */
114 0 : case FRAME_NO_DATA:
115 0 : return NO_DATA_TYPE;
116 0 : case SID_2k40:
117 0 : return PRIMARY_SID;
118 0 : case PPP_NELP_2k80:
119 0 : return PRIMARY_2800;
120 0 : case ACELP_7k20:
121 0 : return PRIMARY_7200;
122 0 : case ACELP_8k00:
123 0 : return PRIMARY_8000;
124 0 : case ACELP_9k60:
125 0 : return PRIMARY_9600;
126 1050 : case ACELP_13k20:
127 1050 : return PRIMARY_13200;
128 0 : case ACELP_16k40:
129 0 : return PRIMARY_16400;
130 1050 : case ACELP_24k40:
131 1050 : return PRIMARY_24400;
132 0 : case ACELP_32k:
133 0 : return PRIMARY_32000;
134 0 : case ACELP_48k:
135 0 : return PRIMARY_48000;
136 1000 : case ACELP_64k:
137 1000 : return PRIMARY_64000;
138 0 : case HQ_96k:
139 0 : return PRIMARY_96000;
140 0 : case HQ_128k:
141 0 : return PRIMARY_128000;
142 0 : default:
143 0 : break;
144 : }
145 :
146 0 : if ( is_amr_wb != NULL )
147 : {
148 0 : *is_amr_wb = 1;
149 : }
150 :
151 0 : return rate2AMRWB_IOmode( brate );
152 : }
153 :
154 : /*-------------------------------------------------------------------*
155 : * ind_list_realloc()
156 : *
157 : * Re-allocate the list of indices
158 : *-------------------------------------------------------------------*/
159 :
160 0 : ivas_error ind_list_realloc(
161 : INDICE_HANDLE old_ind_list, /* i : pointer to the beginning of the old buffer of indices */
162 : const Word16 max_num_indices, /* i : new maximum number of allowed indices in the list */
163 : Encoder_Struct *st_ivas /* i : IVAS encoder structure */
164 : )
165 : {
166 : Word16 i, n, ch, n_channels, ind_list_pos, is_metadata, ivas_max_num_indices;
167 : INDICE_HANDLE new_ind_list;
168 : BSTR_ENC_HANDLE hBstr;
169 :
170 0 : IF( st_ivas == NULL )
171 : {
172 0 : return IVAS_ERR_OK;
173 : }
174 :
175 : /* get the pointer to the beginning of the old buffer of indices (either metadata or core coders) */
176 0 : IF( old_ind_list == st_ivas->ind_list_metadata )
177 : {
178 0 : is_metadata = 1;
179 0 : ivas_max_num_indices = st_ivas->ivas_max_num_indices_metadata;
180 : }
181 : ELSE
182 : {
183 0 : is_metadata = 0;
184 0 : ivas_max_num_indices = st_ivas->ivas_max_num_indices;
185 : }
186 0 : move16();
187 0 : move16();
188 :
189 : /* allocate new buffer of indices */
190 0 : IF( ( new_ind_list = (INDICE_HANDLE) malloc( max_num_indices * sizeof( Indice ) ) ) == NULL )
191 : {
192 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for buffer of indices!\n" ) );
193 : }
194 :
195 : /* move indices from the old list to the new list */
196 0 : FOR( i = 0; i < s_min( max_num_indices, ivas_max_num_indices ); i++ )
197 : {
198 0 : IF( GT_16( old_ind_list[i].nb_bits, -1 ) )
199 : {
200 0 : new_ind_list[i].id = old_ind_list[i].id;
201 0 : new_ind_list[i].value = old_ind_list[i].value;
202 0 : move16();
203 0 : move16();
204 : }
205 0 : new_ind_list[i].nb_bits = old_ind_list[i].nb_bits;
206 0 : move16();
207 : }
208 :
209 : /* reset nb_bits of all other indices to -1 */
210 0 : FOR( ; i < max_num_indices; i++ )
211 : {
212 0 : new_ind_list[i].nb_bits = -1;
213 0 : move16();
214 : }
215 :
216 : /* update parameters in all SCE elements */
217 0 : FOR( n = 0; n < st_ivas->nSCE; n++ )
218 : {
219 : /* get the pointer to hBstr */
220 0 : IF( is_metadata )
221 : {
222 0 : hBstr = st_ivas->hSCE[n]->hMetaData;
223 : }
224 : ELSE
225 : {
226 0 : hBstr = st_ivas->hSCE[n]->hCoreCoder[0]->hBstr;
227 : }
228 :
229 0 : IF( hBstr != NULL )
230 : {
231 : /* get the current position inside the old list */
232 0 : ind_list_pos = (Word16) ( hBstr->ind_list - old_ind_list );
233 :
234 : /* set pointers in the new list */
235 0 : *( hBstr->ivas_ind_list_zero ) = new_ind_list;
236 0 : hBstr->ind_list = &new_ind_list[ind_list_pos];
237 :
238 : /* set the new maximum number of indices */
239 0 : *( hBstr->ivas_max_num_indices ) = max_num_indices;
240 0 : move16();
241 : }
242 : }
243 :
244 : /* update parameters in all CPE elements */
245 0 : FOR( n = 0; n < st_ivas->nCPE; n++ )
246 : {
247 : /* get the pointer to hBstr */
248 0 : IF( is_metadata )
249 : {
250 0 : n_channels = 1;
251 : }
252 : ELSE
253 : {
254 0 : n_channels = CPE_CHANNELS;
255 : }
256 0 : move16();
257 :
258 0 : FOR( ch = 0; ch < n_channels; ch++ )
259 : {
260 0 : IF( is_metadata )
261 : {
262 0 : hBstr = st_ivas->hCPE[n]->hMetaData;
263 : }
264 : ELSE
265 : {
266 0 : hBstr = st_ivas->hCPE[n]->hCoreCoder[ch]->hBstr;
267 : }
268 :
269 0 : IF( hBstr != NULL )
270 : {
271 : /* get the current position inside the old list */
272 0 : ind_list_pos = (Word16) ( hBstr->ind_list - old_ind_list );
273 :
274 : /* set pointers in the new list */
275 0 : *( hBstr->ivas_ind_list_zero ) = new_ind_list;
276 0 : hBstr->ind_list = &new_ind_list[ind_list_pos];
277 :
278 : /* set the new maximum number of indices */
279 0 : *( hBstr->ivas_max_num_indices ) = max_num_indices;
280 0 : move16();
281 : }
282 : }
283 : }
284 :
285 : /* free the old list */
286 0 : free( old_ind_list );
287 :
288 0 : return IVAS_ERR_OK;
289 : }
290 :
291 :
292 : /*-----------------------------------------------------------------------*
293 : * get_ivas_max_num_indices()
294 : *
295 : * Get the maximum allowed number of indices in the encoder
296 : *-----------------------------------------------------------------------*/
297 :
298 : /*! r: maximum number of indices */
299 0 : Word16 get_ivas_max_num_indices_fx(
300 : const IVAS_FORMAT ivas_format, /* i : IVAS format */
301 : const Word32 ivas_total_brate /* i : IVAS total bitrate */
302 : )
303 : {
304 0 : test();
305 0 : test();
306 0 : IF( EQ_16( ivas_format, STEREO_FORMAT ) )
307 : {
308 0 : IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
309 : {
310 0 : return 300;
311 : }
312 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_24k4 ) )
313 : {
314 0 : return 400;
315 : }
316 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
317 : {
318 0 : return 450;
319 : }
320 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_48k ) )
321 : {
322 0 : return 650;
323 : }
324 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_80k ) )
325 : {
326 0 : return 750;
327 : }
328 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_128k ) )
329 : {
330 0 : return 850;
331 : }
332 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_192k ) )
333 : {
334 0 : return 950;
335 : }
336 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_256k ) )
337 : {
338 0 : return 1350;
339 : }
340 : ELSE
341 : {
342 0 : return 1650;
343 : }
344 : }
345 0 : ELSE IF( EQ_16( ivas_format, ISM_FORMAT ) || EQ_16( ivas_format, MONO_FORMAT ) )
346 : {
347 0 : IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
348 : {
349 0 : return 250;
350 : }
351 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_24k4 ) )
352 : {
353 0 : return 350;
354 : }
355 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
356 : {
357 0 : return 450;
358 : }
359 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_48k ) )
360 : {
361 0 : return 550;
362 : }
363 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_64k ) )
364 : {
365 0 : return 620;
366 : }
367 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_80k ) )
368 : {
369 0 : return 670;
370 : }
371 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_96k ) )
372 : {
373 0 : return 780;
374 : }
375 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_128k ) )
376 : {
377 0 : return 880;
378 : }
379 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_192k ) )
380 : {
381 0 : return 950;
382 : }
383 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_256k ) )
384 : {
385 0 : return 1100;
386 : }
387 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_384k ) )
388 : {
389 0 : return 1300;
390 : }
391 : ELSE
392 : {
393 0 : return 1650;
394 : }
395 : }
396 0 : ELSE IF( EQ_16( ivas_format, SBA_FORMAT ) || EQ_16( ivas_format, SBA_ISM_FORMAT ) )
397 : {
398 0 : IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
399 : {
400 0 : return 250;
401 : }
402 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_24k4 ) )
403 : {
404 0 : return 350;
405 : }
406 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
407 : {
408 0 : return 400;
409 : }
410 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_48k ) )
411 : {
412 0 : return 650;
413 : }
414 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_80k ) )
415 : {
416 0 : return 750;
417 : }
418 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_128k ) )
419 : {
420 0 : return 1020;
421 : }
422 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_160k ) )
423 : {
424 0 : return 1160;
425 : }
426 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_192k ) )
427 : {
428 0 : return 1220;
429 : }
430 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_256k ) )
431 : {
432 0 : return 1300;
433 : }
434 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_384k ) )
435 : {
436 0 : return 1720;
437 : }
438 : ELSE
439 : {
440 0 : return 2000;
441 : }
442 : }
443 0 : ELSE IF( EQ_16( ivas_format, MASA_FORMAT ) )
444 : {
445 0 : IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
446 : {
447 0 : return 300;
448 : }
449 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
450 : {
451 0 : return 400;
452 : }
453 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_48k ) )
454 : {
455 0 : return 650;
456 : }
457 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_80k ) )
458 : {
459 0 : return 750;
460 : }
461 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_160k ) )
462 : {
463 0 : return 850;
464 : }
465 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_192k ) )
466 : {
467 0 : return 950;
468 : }
469 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_256k ) )
470 : {
471 0 : return 1150;
472 : }
473 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_384k ) )
474 : {
475 0 : return 1450;
476 : }
477 : ELSE
478 : {
479 0 : return 1650;
480 : }
481 : }
482 0 : ELSE IF( EQ_16( ivas_format, MASA_ISM_FORMAT ) )
483 : {
484 0 : IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
485 : {
486 0 : return 300;
487 : }
488 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
489 : {
490 0 : return 400;
491 : }
492 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_48k ) )
493 : {
494 0 : return 650;
495 : }
496 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_80k ) )
497 : {
498 0 : return 750;
499 : }
500 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_160k ) )
501 : {
502 0 : return 1150;
503 : }
504 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_192k ) )
505 : {
506 0 : return 1250;
507 : }
508 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_256k ) )
509 : {
510 0 : return 1400;
511 : }
512 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_384k ) )
513 : {
514 0 : return 1650;
515 : }
516 : ELSE
517 : {
518 0 : return 1850;
519 : }
520 : }
521 0 : ELSE IF( EQ_16( ivas_format, MC_FORMAT ) )
522 : {
523 0 : IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
524 : {
525 0 : return 250;
526 : }
527 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_24k4 ) )
528 : {
529 0 : return 350;
530 : }
531 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
532 : {
533 0 : return 400;
534 : }
535 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_48k ) )
536 : {
537 0 : return 650;
538 : }
539 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_64k ) )
540 : {
541 0 : return 750;
542 : }
543 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_80k ) )
544 : {
545 0 : return 850;
546 : }
547 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_128k ) )
548 : {
549 0 : return 1150;
550 : }
551 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_160k ) )
552 : {
553 0 : return 1420;
554 : }
555 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_256k ) )
556 : {
557 0 : return 2120;
558 : }
559 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_384k ) )
560 : {
561 0 : return 2250;
562 : }
563 : ELSE
564 : {
565 0 : return 2450;
566 : }
567 : }
568 :
569 0 : return 2450;
570 : }
571 : /*-----------------------------------------------------------------------*
572 : * get_BWE_max_num_indices()
573 : *
574 : * Get the maximum number of indices in the BWE
575 : *-----------------------------------------------------------------------*/
576 :
577 : /*! r: maximum number of indices */
578 0 : int16_t get_BWE_max_num_indices(
579 : const int32_t extl_brate /* i : extensiona layer bitrate */
580 : )
581 : {
582 : /* set the maximum number of indices in the BWE */
583 0 : if ( extl_brate < SWB_BWE_16k )
584 : {
585 0 : return 30;
586 : }
587 : else
588 : {
589 0 : return 150;
590 : }
591 : }
592 :
593 :
594 : /*-----------------------------------------------------------------------*
595 : * get_ivas_max_num_indices_metadata()
596 : *
597 : * Set the maximum allowed number of metadata indices in the list
598 : *-----------------------------------------------------------------------*/
599 :
600 : /*! r: maximum number of indices */
601 0 : Word16 get_ivas_max_num_indices_metadata_fx(
602 : const IVAS_FORMAT ivas_format, /* i : IVAS format */
603 : const Word32 ivas_total_brate /* i : IVAS total bitrate */
604 : )
605 : {
606 : /* set the maximum required number of metadata indices */
607 0 : test();
608 0 : IF( EQ_16( ivas_format, MONO_FORMAT ) )
609 : {
610 0 : return 0;
611 : }
612 0 : ELSE IF( EQ_16( ivas_format, STEREO_FORMAT ) )
613 : {
614 0 : IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
615 : {
616 0 : return 60;
617 : }
618 : ELSE
619 : {
620 0 : return 80;
621 : }
622 : }
623 0 : ELSE IF( EQ_16( ivas_format, ISM_FORMAT ) )
624 : {
625 0 : IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
626 : {
627 0 : return 20;
628 : }
629 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
630 : {
631 0 : return 65;
632 : }
633 : ELSE
634 : {
635 0 : return 80;
636 : }
637 : }
638 0 : ELSE IF( EQ_16( ivas_format, SBA_FORMAT ) || EQ_16( ivas_format, SBA_ISM_FORMAT ) )
639 : {
640 0 : IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
641 : {
642 0 : return 100;
643 : }
644 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_24k4 ) )
645 : {
646 0 : return 200;
647 : }
648 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
649 : {
650 0 : return 300;
651 : }
652 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_192k ) )
653 : {
654 0 : return 500;
655 : }
656 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_256k ) )
657 : {
658 0 : return 1050;
659 : }
660 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_384k ) )
661 : {
662 0 : return 2000;
663 : }
664 : ELSE
665 : {
666 0 : return 2500;
667 : }
668 : }
669 0 : ELSE IF( EQ_16( ivas_format, MASA_FORMAT ) )
670 : {
671 0 : IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
672 : {
673 0 : return 80;
674 : }
675 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
676 : {
677 0 : return 125;
678 : }
679 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_48k ) )
680 : {
681 0 : return 205;
682 : }
683 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_96k ) )
684 : {
685 0 : return 240;
686 : }
687 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_128k ) )
688 : {
689 0 : return 305;
690 : }
691 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_160k ) )
692 : {
693 0 : return 425;
694 : }
695 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_192k ) )
696 : {
697 0 : return 630;
698 : }
699 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_256k ) )
700 : {
701 0 : return 850;
702 : }
703 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_384k ) )
704 : {
705 0 : return 1000;
706 : }
707 : ELSE
708 : {
709 0 : return 1750;
710 : }
711 : }
712 0 : ELSE IF( EQ_16( ivas_format, MASA_ISM_FORMAT ) )
713 : {
714 0 : IF( LE_32( ivas_total_brate, IVAS_16k4 ) )
715 : {
716 0 : return 80;
717 : }
718 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_32k ) )
719 : {
720 0 : return 125 + 100;
721 : }
722 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_48k ) )
723 : {
724 0 : return 205 + 100;
725 : }
726 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_96k ) )
727 : {
728 0 : return 240 + 150;
729 : }
730 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_128k ) )
731 : {
732 0 : return 305 + 30;
733 : }
734 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_160k ) )
735 : {
736 0 : return 425 + 30;
737 : }
738 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_192k ) )
739 : {
740 0 : return 630 + 30;
741 : }
742 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_256k ) )
743 : {
744 0 : return 850 + 30;
745 : }
746 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_384k ) )
747 : {
748 0 : return 1000 + 30;
749 : }
750 : ELSE
751 : {
752 0 : return 1750 + 30;
753 : }
754 : }
755 0 : ELSE IF( EQ_16( ivas_format, MC_FORMAT ) )
756 : {
757 0 : IF( LE_32( ivas_total_brate, IVAS_13k2 ) )
758 : {
759 0 : return 80;
760 : }
761 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_24k4 ) )
762 : {
763 0 : return 100;
764 : }
765 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_64k ) )
766 : {
767 0 : return 210;
768 : }
769 0 : ELSE IF( LE_32( ivas_total_brate, IVAS_96k ) )
770 : {
771 0 : return 220;
772 : }
773 : ELSE
774 : {
775 0 : return 300;
776 : }
777 : }
778 :
779 0 : return 50;
780 : }
781 : /*-------------------------------------------------------------------*
782 : * move_indices()
783 : *
784 : * Move indices inside the buffer or among two buffers
785 : *-------------------------------------------------------------------*/
786 :
787 0 : void move_indices(
788 : INDICE_HANDLE old_ind_list, /* i/o: old location of indices */
789 : INDICE_HANDLE new_ind_list, /* i/o: new location of indices */
790 : const int16_t nb_indices /* i : number of moved indices */
791 : )
792 : {
793 : int16_t i;
794 :
795 0 : if ( new_ind_list < old_ind_list )
796 : {
797 0 : for ( i = 0; i < nb_indices; i++ )
798 : {
799 0 : new_ind_list[i].id = old_ind_list[i].id;
800 0 : new_ind_list[i].value = old_ind_list[i].value;
801 0 : new_ind_list[i].nb_bits = old_ind_list[i].nb_bits;
802 :
803 0 : old_ind_list[i].nb_bits = -1;
804 : }
805 : }
806 0 : else if ( new_ind_list > old_ind_list )
807 : {
808 0 : for ( i = nb_indices - 1; i >= 0; i-- )
809 : {
810 0 : new_ind_list[i].id = old_ind_list[i].id;
811 0 : new_ind_list[i].value = old_ind_list[i].value;
812 0 : new_ind_list[i].nb_bits = old_ind_list[i].nb_bits;
813 :
814 0 : old_ind_list[i].nb_bits = -1;
815 : }
816 : }
817 :
818 0 : return;
819 : }
820 :
821 :
822 : /*-------------------------------------------------------------------*
823 : * check_ind_list_limits()
824 : *
825 : * Check, if the maximum number of indices has been reached -> reallocate
826 : * Check, if we will not overwrite an existing indice -> adjust the location
827 : *-------------------------------------------------------------------*/
828 :
829 0 : ivas_error check_ind_list_limits(
830 : BSTR_ENC_HANDLE hBstr /* i/o: encoder bitstream handle */
831 : )
832 : {
833 : Indice *ivas_ind_list_zero, *ivas_ind_list_last;
834 : ivas_error error;
835 :
836 0 : error = IVAS_ERR_OK;
837 0 : move32();
838 0 : ivas_ind_list_zero = *( hBstr->ivas_ind_list_zero );
839 :
840 : /* check, if the maximum number of indices has been reached and re-allocate the buffer */
841 : /* the re-allocation can be avoided by increasing the limits in get_ivas_max_num_indices() or get_ivas_max_num_indices_metadata() */
842 0 : IF( GE_16( (Word16) ( &hBstr->ind_list[hBstr->nb_ind_tot] - ivas_ind_list_zero ), *( hBstr->ivas_max_num_indices ) ) )
843 : {
844 :
845 : /* reallocate the buffer of indices with increased limit */
846 0 : IF( NE_32( ( error = ind_list_realloc( *hBstr->ivas_ind_list_zero, *( hBstr->ivas_max_num_indices ) + STEP_MAX_NUM_INDICES, hBstr->st_ivas ) ), IVAS_ERR_OK ) )
847 : {
848 0 : return error;
849 : }
850 : }
851 :
852 : /* check, if we will not overwrite an existing indice */
853 0 : IF( hBstr->ind_list[hBstr->nb_ind_tot].nb_bits > 0 )
854 : {
855 0 : IF( hBstr->nb_ind_tot == 0 )
856 : {
857 : /* move the pointer to the next available empty slot */
858 0 : ivas_ind_list_last = &ivas_ind_list_zero[*( hBstr->ivas_max_num_indices )];
859 0 : WHILE( hBstr->ind_list[0].nb_bits > 0 && hBstr->ind_list < ivas_ind_list_last )
860 : {
861 0 : hBstr->ind_list++;
862 : }
863 :
864 0 : IF( hBstr->ind_list >= ivas_ind_list_last )
865 : {
866 :
867 : /* no available empty slot -> need to re-allocate the buffer */
868 0 : IF( NE_32( ( error = ind_list_realloc( *hBstr->ivas_ind_list_zero, *( hBstr->ivas_max_num_indices ) + STEP_MAX_NUM_INDICES, hBstr->st_ivas ) ), IVAS_ERR_OK ) )
869 : {
870 0 : return error;
871 : }
872 : }
873 : }
874 : ELSE
875 : {
876 0 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Buffer of indices corrupted in frame %d! Attempt to overwrite indice ID = %d (value: %d, bits: %d)!\n", hBstr->ind_list[hBstr->nb_ind_tot].id, hBstr->ind_list[hBstr->nb_ind_tot].value, hBstr->ind_list[hBstr->nb_ind_tot].nb_bits );
877 : }
878 : }
879 :
880 0 : return error;
881 : }
882 :
883 :
884 : /*-------------------------------------------------------------------*
885 : * push_indice()
886 : *
887 : * Push a new indice into the buffer
888 : *-------------------------------------------------------------------*/
889 :
890 0 : ivas_error push_indice(
891 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
892 : Word16 id, /* i : ID of the indice */
893 : UWord16 value, /* i : value of the quantized indice */
894 : Word16 nb_bits /* i : number of bits used to quantize the indice */
895 : )
896 : {
897 : Word16 i;
898 : Word16 j;
899 : ivas_error error;
900 :
901 0 : error = IVAS_ERR_OK;
902 0 : move32();
903 :
904 : /* check the limits of the list of indices */
905 0 : IF( NE_32( ( error = check_ind_list_limits( hBstr ) ), IVAS_ERR_OK ) )
906 : {
907 0 : return IVAS_ERROR( error, "Error occured in push_indice() while re-allocating the list of indices (frame %d) !\n" );
908 : }
909 :
910 : /* find the location in the list of indices based on ID */
911 0 : i = hBstr->nb_ind_tot;
912 0 : move16();
913 0 : test();
914 0 : WHILE( i > 0 && ( id < hBstr->ind_list[i - 1].id ) )
915 : {
916 0 : test();
917 0 : i = sub( i, 1 );
918 : }
919 :
920 : /* shift indices, if the new ID is to be written somewhere inside the list */
921 0 : IF( i < hBstr->nb_ind_tot )
922 : {
923 0 : FOR( j = hBstr->nb_ind_tot; j > i; j-- )
924 : {
925 0 : hBstr->ind_list[j].id = hBstr->ind_list[j - 1].id;
926 0 : hBstr->ind_list[j].nb_bits = hBstr->ind_list[j - 1].nb_bits;
927 0 : hBstr->ind_list[j].value = hBstr->ind_list[j - 1].value;
928 0 : move16();
929 0 : move16();
930 0 : move16();
931 : }
932 : }
933 :
934 :
935 : /* store the new indice in the list */
936 0 : hBstr->ind_list[i].id = id;
937 0 : hBstr->ind_list[i].value = value;
938 0 : hBstr->ind_list[i].nb_bits = nb_bits;
939 0 : move16();
940 0 : move16();
941 0 : move16();
942 :
943 : /* updates */
944 0 : hBstr->nb_ind_tot = add( hBstr->nb_ind_tot, 1 );
945 0 : hBstr->nb_bits_tot = add( hBstr->nb_bits_tot, nb_bits );
946 0 : move16();
947 0 : move16();
948 :
949 0 : return error;
950 : }
951 :
952 : /*-------------------------------------------------------------------*
953 : * push_next_indice()
954 : *
955 : * Push a new indice into the buffer at the next position
956 : *-------------------------------------------------------------------*/
957 0 : ivas_error push_next_indice(
958 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
959 : UWord16 value, /* i : value of the quantized indice */
960 : Word16 nb_bits /* i : number of bits used to quantize the indice */
961 : )
962 : {
963 : Word16 prev_id;
964 : ivas_error error;
965 :
966 0 : error = IVAS_ERR_OK;
967 0 : move32();
968 :
969 :
970 : /* check the limits of the list of indices */
971 0 : IF( NE_32( ( error = check_ind_list_limits( hBstr ) ), IVAS_ERR_OK ) )
972 : {
973 0 : return error;
974 : }
975 :
976 : /* get the id of the previous indice -> it will be re-used */
977 0 : IF( hBstr->nb_ind_tot > 0 )
978 : {
979 0 : prev_id = hBstr->ind_list[hBstr->nb_ind_tot - 1].id;
980 : }
981 : ELSE
982 : {
983 0 : prev_id = 0;
984 : }
985 0 : move16();
986 :
987 : /* store the values in the list */
988 0 : hBstr->ind_list[hBstr->nb_ind_tot].id = prev_id;
989 0 : hBstr->ind_list[hBstr->nb_ind_tot].value = value;
990 0 : hBstr->ind_list[hBstr->nb_ind_tot].nb_bits = nb_bits;
991 0 : move16();
992 0 : move16();
993 0 : move16();
994 :
995 : /* updates */
996 0 : hBstr->nb_ind_tot = add( hBstr->nb_ind_tot, 1 );
997 0 : hBstr->nb_bits_tot = add( hBstr->nb_bits_tot, nb_bits );
998 0 : move16();
999 0 : move16();
1000 :
1001 0 : return error;
1002 : }
1003 :
1004 : /*-------------------------------------------------------------------*
1005 : * push_next_bits()
1006 : * Push a bit buffer into the buffer at the next position
1007 : *-------------------------------------------------------------------*/
1008 0 : ivas_error push_next_bits(
1009 : BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
1010 : const UWord16 bits[], /* i : bit buffer to pack, sequence of single bits */
1011 : const Word16 nb_bits /* i : number of bits to pack */
1012 : )
1013 : {
1014 : UWord16 code;
1015 : Word16 i, nb_bits_m15;
1016 : Indice *ptr;
1017 : Word16 prev_id;
1018 : ivas_error error;
1019 :
1020 0 : error = IVAS_ERR_OK;
1021 0 : move32();
1022 :
1023 0 : ptr = &hBstr->ind_list[hBstr->nb_ind_tot];
1024 :
1025 : /* get the id of the previous indice -> will be re-used */
1026 0 : IF( hBstr->nb_ind_tot > 0 )
1027 : {
1028 0 : prev_id = hBstr->ind_list[hBstr->nb_ind_tot - 1].id;
1029 0 : move16();
1030 : }
1031 : ELSE
1032 : {
1033 0 : prev_id = 0;
1034 0 : move16();
1035 : }
1036 0 : nb_bits_m15 = sub( nb_bits, 15 );
1037 :
1038 0 : FOR( i = 0; i < nb_bits_m15; i += 16 )
1039 : {
1040 0 : code = (UWord16) L_or( L_shl( bits[i], 15 ), L_or( L_shl( bits[i + 1], 14 ), L_or( L_shl( bits[i + 2], 13 ), L_or( L_shl( bits[i + 3], 12 ), L_or( L_shl( bits[i + 4], 11 ), L_or( L_shl( bits[i + 5], 10 ), L_or( L_shl( bits[i + 6], 9 ), L_or( L_shl( bits[i + 7], 8 ), L_or( L_shl( bits[i + 8], 7 ), L_or( L_shl( bits[i + 9], 6 ), L_or( L_shl( bits[i + 10], 5 ), L_or( L_shl( bits[i + 11], 4 ), L_or( L_shl( bits[i + 12], 3 ), L_or( L_shl( bits[i + 13], 2 ), L_or( L_shl( bits[i + 14], 1 ), bits[i + 15] ) ) ) ) ) ) ) ) ) ) ) ) ) ) );
1041 :
1042 : /* check the limits of the list of indices */
1043 0 : IF( NE_32( ( error = check_ind_list_limits( hBstr ) ), IVAS_ERR_OK ) )
1044 : {
1045 0 : return IVAS_ERROR( error, "Error occured in push_next_bits() while re-allocating the list of indices (frame %d) !\n" );
1046 : }
1047 0 : ptr = &hBstr->ind_list[hBstr->nb_ind_tot];
1048 :
1049 0 : ptr->value = code;
1050 0 : ptr->nb_bits = 16;
1051 0 : ptr->id = prev_id;
1052 0 : hBstr->nb_ind_tot = add( hBstr->nb_ind_tot, 1 );
1053 0 : move16();
1054 0 : move16();
1055 0 : move16();
1056 0 : move16();
1057 :
1058 0 : ++ptr;
1059 : }
1060 :
1061 0 : FOR( ; i < nb_bits; ++i )
1062 : {
1063 : /* check the limits of the list of indices */
1064 0 : IF( NE_32( ( error = check_ind_list_limits( hBstr ) ), IVAS_ERR_OK ) )
1065 : {
1066 0 : return IVAS_ERROR( error, "Error occured in push_next_bits() while re-allocating the list of indices (frame %d) !\n" );
1067 : }
1068 0 : ptr = &hBstr->ind_list[hBstr->nb_ind_tot];
1069 :
1070 0 : ptr->value = bits[i];
1071 0 : ptr->nb_bits = 1;
1072 0 : ptr->id = prev_id;
1073 0 : hBstr->nb_ind_tot = add( hBstr->nb_ind_tot, 1 );
1074 0 : move16();
1075 0 : move16();
1076 0 : move16();
1077 0 : move16();
1078 :
1079 0 : ++ptr;
1080 : }
1081 :
1082 0 : hBstr->nb_bits_tot = add( hBstr->nb_bits_tot, nb_bits );
1083 0 : move16();
1084 :
1085 0 : return error;
1086 : }
1087 :
1088 : /*-------------------------------------------------------------------*
1089 : * find_indice()
1090 : *
1091 : * Find indice based on its id
1092 : *-------------------------------------------------------------------*/
1093 :
1094 : /*! r: result: index of the indice in the list, -1 if not found */
1095 0 : int16_t find_indice(
1096 : BSTR_ENC_HANDLE hBstr, /* i : encoder bitstream handle */
1097 : const int16_t id, /* i : ID of the indice */
1098 : uint16_t *value, /* o : value of the quantized indice */
1099 : int16_t *nb_bits /* o : number of bits used to quantize the indice */
1100 : )
1101 : {
1102 : int16_t i;
1103 :
1104 0 : for ( i = 0; i < hBstr->nb_ind_tot; i++ )
1105 : {
1106 0 : if ( hBstr->ind_list[i].id == id && hBstr->ind_list[i].nb_bits > 0 )
1107 : {
1108 0 : *value = hBstr->ind_list[i].value;
1109 0 : *nb_bits = hBstr->ind_list[i].nb_bits;
1110 0 : return i;
1111 : }
1112 : }
1113 :
1114 0 : return -1;
1115 : }
1116 :
1117 :
1118 : /*-------------------------------------------------------------------*
1119 : * delete_indice()
1120 : *
1121 : * Delete indice based on its id (note, that nb_ind_tot and nb_bits_tot are updated)
1122 : *-------------------------------------------------------------------*/
1123 :
1124 : /*! r: number of deleted indices */
1125 0 : uint16_t delete_indice(
1126 : BSTR_ENC_HANDLE hBstr, /* i : encoder bitstream handle */
1127 : const int16_t id /* i : ID of the indice */
1128 : )
1129 : {
1130 : int16_t i, j;
1131 :
1132 0 : j = 0;
1133 0 : for ( i = 0; i < hBstr->nb_ind_tot; i++ )
1134 : {
1135 0 : if ( hBstr->ind_list[i].id == id )
1136 : {
1137 0 : hBstr->nb_bits_tot -= hBstr->ind_list[i].nb_bits;
1138 0 : continue;
1139 : }
1140 :
1141 0 : if ( j < i )
1142 : {
1143 : /* shift the indice left */
1144 0 : hBstr->ind_list[j].id = hBstr->ind_list[i].id;
1145 0 : hBstr->ind_list[j].value = hBstr->ind_list[i].value;
1146 0 : hBstr->ind_list[j].nb_bits = hBstr->ind_list[i].nb_bits;
1147 : }
1148 :
1149 0 : j++;
1150 : }
1151 :
1152 0 : hBstr->nb_ind_tot = j;
1153 :
1154 0 : for ( ; j < i; j++ )
1155 : {
1156 : /* reset the shifted indices at the end of the list */
1157 0 : hBstr->ind_list[j].nb_bits = -1;
1158 : }
1159 :
1160 0 : return i - j;
1161 : }
1162 :
1163 :
1164 : /*-------------------------------------------------------------------*
1165 : * get_next_indice()
1166 : *
1167 : * Get the next indice from the buffer
1168 : *-------------------------------------------------------------------*/
1169 :
1170 : /*! r: value of the indice */
1171 3046 : uint16_t get_next_indice(
1172 : Decoder_State *st, /* i/o: decoder state structure */
1173 : int16_t nb_bits /* i : number of bits that were used to quantize the indice */
1174 : )
1175 : {
1176 : uint16_t value;
1177 : int16_t i;
1178 : int32_t nbits_total;
1179 :
1180 3046 : assert( nb_bits <= 16 );
1181 :
1182 3046 : nbits_total = st->total_brate / FRAMES_PER_SEC;
1183 : /* detect corrupted bitstream */
1184 3046 : if ( st->next_bit_pos + nb_bits > nbits_total )
1185 : {
1186 0 : st->BER_detect = 1;
1187 0 : return ( 0 );
1188 : }
1189 :
1190 3046 : value = 0;
1191 7504 : for ( i = 0; i < nb_bits; i++ )
1192 : {
1193 4458 : value <<= 1;
1194 4458 : value += st->bit_stream[st->next_bit_pos + i];
1195 : }
1196 :
1197 : /* update the position in the bitstream */
1198 3046 : st->next_bit_pos += nb_bits;
1199 :
1200 3046 : return value;
1201 : }
1202 :
1203 : /*-------------------------------------------------------------------*
1204 : * get_next_indice_1()
1205 : *
1206 : * Get the next 1-bit indice from the buffer
1207 : *-------------------------------------------------------------------*/
1208 :
1209 : /*! r: value of the indice */
1210 0 : uint16_t get_next_indice_1(
1211 : Decoder_State *st /* i/o: decoder state structure */
1212 : )
1213 : {
1214 : int32_t nbits_total;
1215 0 : nbits_total = st->total_brate / FRAMES_PER_SEC;
1216 : /* detect corrupted bitstream */
1217 0 : if ( ( st->next_bit_pos + 1 > nbits_total && st->codec_mode == MODE1 ) ||
1218 0 : ( ( st->next_bit_pos + 1 > nbits_total + ( 2 * 8 ) ) && st->codec_mode == MODE2 ) /* add two zero bytes for arithmetic coder flush */
1219 : )
1220 : {
1221 0 : st->BER_detect = 1;
1222 0 : return ( 0 );
1223 : }
1224 :
1225 0 : return st->bit_stream[st->next_bit_pos++];
1226 : }
1227 :
1228 : /*-------------------------------------------------------------------*
1229 : * get_next_indice_tmp()
1230 : *
1231 : * update the total number of bits and the position in the bitstream
1232 : *-------------------------------------------------------------------*/
1233 :
1234 560908 : void get_next_indice_tmp(
1235 : Decoder_State *st, /* o : decoder state structure */
1236 : int16_t nb_bits /* i : number of bits that were used to quantize the indice */
1237 : )
1238 : {
1239 : /* update the position in the bitstream */
1240 560908 : st->next_bit_pos += nb_bits;
1241 :
1242 560908 : return;
1243 : }
1244 :
1245 : /*-------------------------------------------------------------------*
1246 : * get_indice()
1247 : *
1248 : * Get indice at specific position in the buffer
1249 : *-------------------------------------------------------------------*/
1250 :
1251 : /*! r: value of the indice */
1252 536584 : uint16_t get_indice(
1253 : Decoder_State *st, /* i/o: decoder state structure */
1254 : int16_t pos, /* i : absolute position in the bitstream (update after the read) */
1255 : int16_t nb_bits /* i : number of bits that were used to quantize the indice */
1256 : )
1257 : {
1258 : uint16_t value;
1259 : int16_t i;
1260 : int32_t nbits_total;
1261 :
1262 536584 : assert( nb_bits <= 16 );
1263 :
1264 536584 : nbits_total = st->total_brate / FRAMES_PER_SEC;
1265 :
1266 : /* detect corrupted bitstream */
1267 536584 : if ( pos + nb_bits > nbits_total )
1268 : {
1269 0 : st->BER_detect = 1;
1270 0 : return ( 0 );
1271 : }
1272 :
1273 536584 : value = 0;
1274 3395718 : for ( i = 0; i < nb_bits; i++ )
1275 : {
1276 2859134 : value <<= 1;
1277 2859134 : value += st->bit_stream[pos + i];
1278 : }
1279 536584 : return value;
1280 : }
1281 :
1282 : /*-------------------------------------------------------------------*
1283 : * get_indice_st()
1284 : *
1285 : * Get indice at specific position in the buffer
1286 : *-------------------------------------------------------------------*/
1287 :
1288 : /*! r: value of the indice */
1289 19352 : UWord16 get_indice_st(
1290 : Decoder_State *st, /* i/o: decoder state structure */
1291 : const Word32 element_brate, /* i : element bitrate */
1292 : const Word16 pos, /* i : absolute position in the bitstream */
1293 : const Word16 nb_bits /* i : number of bits to quantize the indice */
1294 : )
1295 : {
1296 : UWord16 value;
1297 : Word16 i;
1298 :
1299 19352 : assert( nb_bits <= 16 );
1300 :
1301 : /* detect corrupted bitstream */
1302 19352 : IF( GT_32( L_deposit_l( add( pos, nb_bits ) ), Mpy_32_32( element_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) ) )
1303 : {
1304 0 : st->BER_detect = 1;
1305 0 : move16();
1306 0 : return ( 0 );
1307 : }
1308 :
1309 19352 : value = 0;
1310 19352 : move16();
1311 62880 : FOR( i = 0; i < nb_bits; i++ )
1312 : {
1313 43528 : value = shl( value, 1 );
1314 43528 : value = add( value, st->bit_stream[pos + i] );
1315 : }
1316 :
1317 19352 : return value;
1318 : }
1319 : #define WMC_TOOL_SKIP
1320 :
1321 : /*-------------------------------------------------------------------*
1322 : * reset_indices_dec()
1323 : *
1324 : * Reset the buffer of decoder indices
1325 : *-------------------------------------------------------------------*/
1326 2162322 : void reset_indices_dec(
1327 : Decoder_State *st )
1328 : {
1329 2162322 : st->next_bit_pos = 0;
1330 2162322 : move16();
1331 :
1332 2162322 : return;
1333 : }
1334 : /*-------------------------------------------------------------------*
1335 : * write_indices_to_stream()
1336 : *
1337 : * writing forward or backward to a serial stream
1338 : *-------------------------------------------------------------------*/
1339 0 : static Word16 write_indices_to_stream_fx(
1340 : Indice *ind_list,
1341 : UWord16 **pt_stream,
1342 : const Word16 inc,
1343 : const Word16 num_indices )
1344 : {
1345 : Word16 i, k;
1346 : Word16 value, nb_bits;
1347 : UWord16 mask;
1348 :
1349 0 : FOR( i = 0; i < num_indices; i++ )
1350 : {
1351 0 : value = ind_list[i].value;
1352 0 : nb_bits = ind_list[i].nb_bits;
1353 0 : move16();
1354 0 : move16();
1355 :
1356 0 : IF( nb_bits > 0 )
1357 : {
1358 : /* mask from MSB to LSB */
1359 0 : mask = (UWord16) L_shl( 1, sub( nb_bits, 1 ) );
1360 :
1361 : /* write bit by bit */
1362 0 : FOR( k = 0; k < nb_bits; k++ )
1363 : {
1364 0 : IF( L_and( value, mask ) )
1365 : {
1366 0 : **pt_stream = 1;
1367 0 : move16();
1368 0 : *pt_stream += inc;
1369 : }
1370 : ELSE
1371 : {
1372 0 : **pt_stream = 0;
1373 0 : move16();
1374 0 : *pt_stream += inc;
1375 : }
1376 :
1377 0 : mask = (UWord16) L_shr( mask, 1 );
1378 : }
1379 : }
1380 : }
1381 0 : return 0;
1382 : }
1383 :
1384 : /*-------------------------------------------------------------------*
1385 : * write_indices_element()
1386 : *
1387 : * Bitstream writing function of one element (one SCE or one CPE)
1388 : *-------------------------------------------------------------------*/
1389 0 : static ivas_error write_indices_element_fx(
1390 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
1391 : UWord16 **pt_stream, /* i : pointer to bitstream buffer */
1392 : const Word16 is_SCE, /* i : flag to distingusih SCE and CPE */
1393 : const Word16 element_id /* i : id of the SCE or CPE */
1394 : )
1395 : {
1396 : Word16 ch;
1397 0 : Encoder_State **sts = NULL;
1398 : UWord16 *pt_stream_loc;
1399 : UWord16 *pt_stream_backup;
1400 : UWord16 *pt_stream_end;
1401 : Word16 nb_bits_tot_metadata;
1402 : Word16 nb_ind_tot_metadata;
1403 :
1404 : Indice *ind_list_metadata;
1405 : Word16 n, n_channels;
1406 : ivas_error error;
1407 :
1408 0 : error = IVAS_ERR_OK;
1409 0 : move32();
1410 :
1411 0 : ind_list_metadata = NULL;
1412 0 : nb_ind_tot_metadata = 0;
1413 0 : move16();
1414 :
1415 0 : IF( EQ_32( st_ivas->hEncoderConfig->ivas_format, MONO_FORMAT ) )
1416 : {
1417 0 : sts = st_ivas->hSCE[0]->hCoreCoder;
1418 0 : nb_bits_tot_metadata = 0;
1419 0 : move16();
1420 : }
1421 : ELSE
1422 : {
1423 0 : nb_bits_tot_metadata = 0;
1424 0 : move16();
1425 0 : test();
1426 0 : test();
1427 0 : IF( is_SCE && st_ivas->hSCE[element_id] != NULL )
1428 : {
1429 0 : sts = st_ivas->hSCE[element_id]->hCoreCoder;
1430 :
1431 0 : IF( st_ivas->hSCE[element_id]->hMetaData != NULL )
1432 : {
1433 0 : nb_bits_tot_metadata = st_ivas->hSCE[element_id]->hMetaData->nb_bits_tot;
1434 0 : ind_list_metadata = st_ivas->hSCE[element_id]->hMetaData->ind_list;
1435 0 : nb_ind_tot_metadata = st_ivas->hSCE[element_id]->hMetaData->nb_ind_tot;
1436 0 : move16();
1437 0 : move16();
1438 : }
1439 : }
1440 0 : ELSE IF( !is_SCE && st_ivas->hCPE[element_id] != NULL )
1441 : {
1442 0 : sts = st_ivas->hCPE[element_id]->hCoreCoder;
1443 :
1444 0 : IF( st_ivas->hCPE[element_id]->hMetaData != NULL )
1445 : {
1446 0 : nb_bits_tot_metadata = st_ivas->hCPE[element_id]->hMetaData->nb_bits_tot;
1447 0 : ind_list_metadata = st_ivas->hCPE[element_id]->hMetaData->ind_list;
1448 0 : nb_ind_tot_metadata = st_ivas->hCPE[element_id]->hMetaData->nb_ind_tot;
1449 0 : move16();
1450 0 : move16();
1451 : }
1452 : }
1453 : }
1454 :
1455 0 : n_channels = 1;
1456 0 : move16();
1457 0 : if ( GT_16( sts[0]->element_mode, IVAS_CPE_DFT ) )
1458 : {
1459 0 : n_channels = CPE_CHANNELS;
1460 0 : move16();
1461 : }
1462 :
1463 : /*----------------------------------------------------------------*
1464 : * Bitstream packing (conversion of individual indices into a serial stream)
1465 : *----------------------------------------------------------------*/
1466 :
1467 0 : pt_stream_loc = *pt_stream;
1468 0 : pt_stream_end = pt_stream_loc;
1469 :
1470 0 : FOR( n = 0; n < n_channels; n++ )
1471 : {
1472 : /* write the metadata buffer */
1473 0 : test();
1474 0 : IF( n == 0 && nb_bits_tot_metadata != 0 )
1475 : {
1476 0 : pt_stream_backup = pt_stream_loc;
1477 :
1478 0 : FOR( ch = 0; ch < n_channels; ch++ )
1479 : {
1480 0 : pt_stream_loc += sts[ch]->hBstr->nb_bits_tot;
1481 : }
1482 0 : pt_stream_loc += nb_bits_tot_metadata - 1;
1483 0 : pt_stream_end = pt_stream_loc + 1;
1484 :
1485 0 : write_indices_to_stream_fx( ind_list_metadata, &pt_stream_loc, -1,
1486 : nb_ind_tot_metadata );
1487 :
1488 : /* restore previous pointer position */
1489 0 : pt_stream_loc = pt_stream_backup;
1490 : }
1491 0 : write_indices_to_stream_fx( sts[n]->hBstr->ind_list, &pt_stream_loc, 1,
1492 0 : sts[n]->hBstr->nb_ind_tot );
1493 :
1494 0 : if ( pt_stream_loc > pt_stream_end )
1495 : {
1496 0 : pt_stream_end = pt_stream_loc;
1497 : }
1498 : }
1499 :
1500 : /*----------------------------------------------------------------*
1501 : * Clearing of indices
1502 : * Reset index pointers
1503 : *----------------------------------------------------------------*/
1504 :
1505 0 : IF( is_SCE ) /* EVS and SCE */
1506 : {
1507 0 : IF( st_ivas->hSCE[element_id]->hMetaData != NULL )
1508 : {
1509 0 : reset_indices_enc_fx( st_ivas->hSCE[element_id]->hMetaData, st_ivas->hSCE[element_id]->hMetaData->nb_ind_tot );
1510 : }
1511 :
1512 0 : reset_indices_enc_fx( sts[0]->hBstr, sts[0]->hBstr->nb_ind_tot );
1513 : }
1514 : ELSE
1515 : {
1516 0 : IF( st_ivas->hCPE[element_id]->hMetaData != NULL )
1517 : {
1518 0 : reset_indices_enc_fx( st_ivas->hCPE[element_id]->hMetaData, st_ivas->hCPE[element_id]->hMetaData->nb_ind_tot );
1519 : }
1520 :
1521 0 : FOR( n = 0; n < n_channels; n++ )
1522 : {
1523 0 : reset_indices_enc_fx( sts[n]->hBstr, sts[n]->hBstr->nb_ind_tot );
1524 : }
1525 : }
1526 :
1527 : /* update pointer */
1528 0 : *pt_stream = pt_stream_end;
1529 :
1530 0 : return error;
1531 : }
1532 :
1533 : /*-------------------------------------------------------------------*
1534 : * write_indices_ivas()
1535 : *
1536 : * Write the buffer of indices to a serial bitstream buffer,
1537 : * each bit represented as a uint16_t of value 0 or 1
1538 : *-------------------------------------------------------------------*/
1539 0 : ivas_error write_indices_ivas_fx(
1540 : Encoder_Struct *st_ivas, /* i/o: encoder state structure */
1541 : UWord16 *bit_stream, /* i/o: output bitstream */
1542 : UWord16 *num_bits /* i : number of indices written to output */
1543 : )
1544 : {
1545 : Word16 i, n;
1546 : UWord16 *pt_stream;
1547 : ivas_error error;
1548 :
1549 0 : error = IVAS_ERR_OK;
1550 0 : move32();
1551 :
1552 0 : pt_stream = bit_stream;
1553 0 : FOR( i = 0; i < MAX_BITS_PER_FRAME; ++i )
1554 : {
1555 0 : bit_stream[i] = 0;
1556 0 : move16();
1557 : }
1558 :
1559 :
1560 : /*-----------------------------------------------------------------*
1561 : * Encode Payload
1562 : *-----------------------------------------------------------------*/
1563 :
1564 0 : FOR( n = 0; n < st_ivas->nSCE; n++ )
1565 : {
1566 0 : write_indices_element_fx( st_ivas, &pt_stream, 1, n );
1567 : }
1568 :
1569 0 : FOR( n = 0; n < st_ivas->nCPE; n++ )
1570 : {
1571 0 : write_indices_element_fx( st_ivas, &pt_stream, 0, n );
1572 : }
1573 :
1574 0 : *num_bits = (UWord16) ( pt_stream - bit_stream );
1575 0 : move16();
1576 :
1577 0 : return error;
1578 : }
1579 :
1580 : /*-------------------------------------------------------------------*
1581 : * decoder_selectCodec()
1582 : *
1583 : *
1584 : *-------------------------------------------------------------------*/
1585 :
1586 3100 : static void decoder_selectCodec(
1587 : Decoder_State *st, /* i/o: decoder state structure */
1588 : const int32_t total_brate, /* i : total bitrate */
1589 : const int16_t bit0 /* i : first bit */
1590 : )
1591 : {
1592 : /* set the AMR-WB IO flag */
1593 3100 : if ( rate2AMRWB_IOmode( total_brate ) != -1 )
1594 : {
1595 0 : st->Opt_AMR_WB = 1;
1596 : }
1597 3100 : else if ( total_brate != FRAME_NO_DATA )
1598 : {
1599 3100 : st->Opt_AMR_WB = 0;
1600 : }
1601 :
1602 3100 : if ( st->Opt_AMR_WB )
1603 : {
1604 0 : st->codec_mode = MODE1;
1605 : }
1606 : else
1607 : {
1608 3100 : st->codec_mode = get_codec_mode( total_brate );
1609 :
1610 3100 : if ( st->codec_mode == -1 )
1611 : {
1612 0 : switch ( total_brate )
1613 : {
1614 0 : case 0:
1615 0 : st->codec_mode = st->last_codec_mode;
1616 0 : break;
1617 0 : case 2400:
1618 0 : st->codec_mode = st->last_codec_mode;
1619 0 : break;
1620 0 : case 2800:
1621 0 : st->codec_mode = MODE1;
1622 0 : break;
1623 0 : default: /* validate that total_brate (derived from RTP packet or a file header) is one of the defined bitrates */
1624 0 : st->codec_mode = st->last_codec_mode;
1625 0 : st->bfi = 1;
1626 0 : break;
1627 : }
1628 3100 : }
1629 : }
1630 :
1631 3100 : if ( st->ini_frame == 0 )
1632 : {
1633 3 : if ( st->codec_mode == -1 )
1634 : {
1635 0 : st->codec_mode = MODE1;
1636 : }
1637 3 : st->last_codec_mode = st->codec_mode;
1638 : }
1639 :
1640 : /* set SID/CNG type */
1641 3100 : if ( total_brate == SID_2k40 )
1642 : {
1643 0 : if ( bit0 == 0 )
1644 : {
1645 0 : st->cng_type = LP_CNG;
1646 :
1647 : /* force MODE1 when selecting LP_CNG */
1648 0 : st->codec_mode = MODE1;
1649 : }
1650 0 : else if ( bit0 == 1 )
1651 : {
1652 0 : st->cng_type = FD_CNG;
1653 0 : if ( st->last_codec_mode == MODE2 && st->last_total_brate == ACELP_13k20 )
1654 : {
1655 0 : st->codec_mode = MODE1;
1656 : }
1657 : }
1658 : }
1659 :
1660 3100 : return;
1661 : }
1662 :
1663 :
1664 : /*-------------------------------------------------------------------*
1665 : * reset_elements()
1666 : *
1667 : * Simulate packet losses by reading FEC pattern from external file
1668 : *-------------------------------------------------------------------*/
1669 :
1670 788299 : Decoder_State **reset_elements(
1671 : Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
1672 : )
1673 : {
1674 : Word16 k, n;
1675 788299 : Decoder_State **sts = NULL; /* to avoid compilation warning */
1676 :
1677 1417174 : FOR( k = 0; k < st_ivas->nSCE; k++ )
1678 : {
1679 628875 : sts = st_ivas->hSCE[k]->hCoreCoder;
1680 :
1681 628875 : sts[0]->bfi = 0;
1682 628875 : sts[0]->BER_detect = 0;
1683 628875 : sts[0]->mdct_sw_enable = 0;
1684 628875 : sts[0]->mdct_sw = 0;
1685 :
1686 628875 : move16();
1687 628875 : move16();
1688 628875 : move16();
1689 628875 : move16();
1690 :
1691 628875 : reset_indices_dec( sts[0] );
1692 : }
1693 :
1694 1554221 : FOR( k = 0; k < st_ivas->nCPE; k++ )
1695 : {
1696 765922 : sts = st_ivas->hCPE[k]->hCoreCoder;
1697 :
1698 2297766 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1699 : {
1700 1531844 : sts[n]->bfi = 0;
1701 1531844 : sts[n]->BER_detect = 0;
1702 1531844 : sts[n]->mdct_sw_enable = 0;
1703 1531844 : sts[n]->mdct_sw = 0;
1704 :
1705 1531844 : move16();
1706 1531844 : move16();
1707 1531844 : move16();
1708 1531844 : move16();
1709 :
1710 1531844 : reset_indices_dec( sts[n] );
1711 : }
1712 : }
1713 :
1714 788299 : return sts; /* return last decoder state */
1715 : }
1716 :
1717 : /*-------------------------------------------------------------------*
1718 : * ivas_set_bitstream_pointers()
1719 : *
1720 : * Set bitstream pointers for every SCE/CPE Core-Decoder
1721 : *-------------------------------------------------------------------*/
1722 :
1723 391414 : void ivas_set_bitstream_pointers(
1724 : Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
1725 : )
1726 : {
1727 : int16_t k, num_bits;
1728 : Decoder_State **sts;
1729 :
1730 391414 : num_bits = 0;
1731 :
1732 : /* set bitstream pointers for SCEs */
1733 705105 : for ( k = 0; k < st_ivas->nSCE; k++ )
1734 : {
1735 313691 : sts = st_ivas->hSCE[k]->hCoreCoder;
1736 313691 : sts[0]->bit_stream = st_ivas->bit_stream + num_bits;
1737 313691 : num_bits += (int16_t) ( st_ivas->hSCE[k]->element_brate / FRAMES_PER_SEC );
1738 : }
1739 :
1740 : /* set bitstream pointers for CPEs */
1741 771479 : for ( k = 0; k < st_ivas->nCPE; k++ )
1742 : {
1743 380065 : sts = st_ivas->hCPE[k]->hCoreCoder;
1744 380065 : sts[0]->bit_stream = st_ivas->bit_stream + num_bits;
1745 380065 : num_bits += (int16_t) ( st_ivas->hCPE[k]->element_brate / FRAMES_PER_SEC );
1746 : }
1747 :
1748 391414 : return;
1749 : }
1750 :
1751 :
1752 : /*-------------------------------------------------------------------*
1753 : * read_indices()
1754 : *
1755 : * Read indices from an ITU-T G.192 bitstream to the buffer
1756 : * Simulate packet losses by inserting frame erasures
1757 : *-------------------------------------------------------------------*/
1758 :
1759 : /*! r: 1 = reading OK, 0 = problem */
1760 399985 : ivas_error read_indices_fx(
1761 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
1762 : UWord16 bit_stream[], /* i : bitstream buffer */
1763 : UWord16 num_bits, /* i : number of bits in bitstream */
1764 : Word16 *prev_ft_speech,
1765 : Word16 *CNG,
1766 : Word16 bfi /* i : bad frame indicator */
1767 : )
1768 : {
1769 : Word16 k;
1770 : Decoder_State **sts;
1771 399985 : Word32 total_brate = 0;
1772 399985 : move32();
1773 : Word16 curr_ft_good_sp, curr_ft_bad_sp;
1774 : Word16 g192_sid_first, sid_upd_bad, sid_update;
1775 : Word16 speech_bad, speech_lost;
1776 : Word16 n;
1777 : ivas_error error;
1778 :
1779 399985 : error = IVAS_ERR_OK;
1780 399985 : move32();
1781 :
1782 399985 : st_ivas->BER_detect = 0;
1783 399985 : move16();
1784 399985 : st_ivas->num_bits = num_bits;
1785 399985 : move16();
1786 399985 : sts = reset_elements( st_ivas );
1787 :
1788 399985 : st_ivas->bfi = bfi;
1789 399985 : move16();
1790 :
1791 : /* convert the frame length to total bitrate */
1792 399985 : total_brate = imult3216( num_bits, FRAMES_PER_SEC );
1793 399985 : move32();
1794 :
1795 : /* verify that a valid num bits value is present in the G.192 file */
1796 : /* only AMRWB, EVS or IVAS bitrates or 0(NO DATA) are allowed in G.192 file frame reading */
1797 399985 : IF( NE_32( st_ivas->ivas_format, MONO_FORMAT ) )
1798 : {
1799 396885 : k = 0;
1800 396885 : move16();
1801 :
1802 396885 : test();
1803 3338374 : WHILE( LT_16( k, SIZE_IVAS_BRATE_TBL ) && NE_32( total_brate, ivas_brate_tbl[k] ) )
1804 : {
1805 2941489 : k = add( k, 1 );
1806 : }
1807 :
1808 396885 : test();
1809 396885 : test();
1810 396885 : test();
1811 396885 : test();
1812 396885 : IF( EQ_32( st_ivas->ivas_format, ISM_FORMAT ) && ( LT_16( k, SIZE_IVAS_BRATE_TBL ) || LE_32( total_brate, SID_2k40 ) ) )
1813 : {
1814 93770 : st_ivas->element_mode_init = IVAS_SCE;
1815 93770 : move16();
1816 : }
1817 303115 : ELSE IF( ( EQ_32( st_ivas->ivas_format, SBA_FORMAT ) || EQ_32( st_ivas->ivas_format, MASA_FORMAT ) ) && LE_32( total_brate, SID_2k40 ) )
1818 : {
1819 3901 : st_ivas->element_mode_init = IVAS_SCE;
1820 3901 : move16();
1821 : }
1822 299214 : ELSE IF( EQ_16( k, SIZE_IVAS_BRATE_TBL ) )
1823 : {
1824 0 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error, illegal bitrate (%d) in the G.192 frame ! Exiting ! \n", total_brate );
1825 : }
1826 : ELSE
1827 : {
1828 299214 : st_ivas->element_mode_init = -1;
1829 299214 : move16();
1830 : }
1831 : }
1832 : ELSE /* AMRWB or EVS */
1833 : {
1834 3100 : st_ivas->element_mode_init = EVS_MONO;
1835 3100 : move16();
1836 :
1837 3100 : IF( rate2EVSmode_float( total_brate, NULL ) < 0 ) /* negative value means that a valid rate was not found */
1838 : {
1839 0 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error, illegal bitrate (%d) in the G.192 frame ! Exiting ! \n", total_brate );
1840 : }
1841 : }
1842 :
1843 : /* G.192 RX DTX handler*/
1844 : /* handle SID_FIRST, SID_BAD, SPEECH_LOST, NO_DATA as properly as possible for the ITU-T G.192 format */
1845 : /* (total_brate, bfi , st_CNG) = rx_handler(received frame type, [previous frame type], past CNG state, past core) */
1846 399985 : curr_ft_good_sp = 0;
1847 399985 : move16();
1848 399985 : curr_ft_bad_sp = 0;
1849 399985 : move16();
1850 :
1851 399985 : IF( is_DTXrate( total_brate ) == 0 )
1852 : {
1853 380176 : IF( st_ivas->bfi == 0 )
1854 : {
1855 372669 : curr_ft_good_sp = 1;
1856 372669 : move16();
1857 : }
1858 : ELSE
1859 : {
1860 7507 : curr_ft_bad_sp = 1;
1861 7507 : move16();
1862 : }
1863 : }
1864 :
1865 399985 : sid_update = 0;
1866 399985 : move16();
1867 399985 : sid_upd_bad = 0;
1868 399985 : move16();
1869 399985 : IF( EQ_16( is_SIDrate( total_brate ), 1 ) )
1870 : {
1871 2980 : IF( st_ivas->bfi == 0 )
1872 : {
1873 2907 : sid_update = 1;
1874 2907 : move16();
1875 : }
1876 : ELSE
1877 : {
1878 73 : sid_upd_bad = 1; /* this frame type may happen in ETSI/3GPP CS cases, a corrupt SID frames */
1879 73 : move16();
1880 : }
1881 : }
1882 :
1883 : /* all zero indices/bits iSP AMRWB SID_update results in a valid LP filter with extremely high LP-filter-gain */
1884 : /* all zero indices/bits may be a result of CS bit errors and/or erroneously injected by gateways or by a bad dejitter handlers */
1885 399985 : test();
1886 399985 : IF( EQ_32( total_brate, SID_1k75 ) && EQ_16( sid_update, 1 ) )
1887 : {
1888 : /* valid sid_update received, check for very risky but formally valid content */
1889 0 : Word16 sum = 0;
1890 0 : move16();
1891 0 : FOR( k = 0; k < num_bits; ++k )
1892 : {
1893 0 : sum = add( sum, extract_l( EQ_32( bit_stream[k], 1 ) ) ); /* check of 35 zeroes */
1894 : }
1895 0 : if ( sum == 0 )
1896 : { /* all zeros */
1897 0 : sid_upd_bad = 1; /* initial signal as corrupt (BER likely) */
1898 0 : move16();
1899 : }
1900 : }
1901 :
1902 : /* AMRWB 26.173 G.192 file reader (read_serial) does not declare/use SID_BAD ft,
1903 : it declares every bad synch marked frame initially as a lost_speech frame,
1904 : and then the RXDTX handler CNG state decides the decoding mode CNG/SPEECH.
1905 : While In the AMRWB ETSI/3GPP format eid a CRC error in a detected SID_UPDATE frames triggers SID_BAD.
1906 :
1907 : Here we inhibit use of the SID-length info, even though it is available in the G.192 file format after STL/EID-XOR .
1908 : */
1909 399985 : IF( sid_upd_bad )
1910 : {
1911 73 : sid_upd_bad = 0;
1912 73 : move16();
1913 73 : total_brate = FRAME_NO_DATA; /* treat SID_BAD as a stolen signaling frame --> SPEECH LOST */
1914 73 : move32();
1915 : }
1916 :
1917 399985 : g192_sid_first = 0;
1918 399985 : move16();
1919 :
1920 399985 : test();
1921 399985 : test();
1922 399985 : test();
1923 399985 : test();
1924 399985 : if ( EQ_32( st_ivas->ivas_format, MONO_FORMAT ) && EQ_16( sts[0]->core, AMR_WB_CORE ) && *prev_ft_speech && total_brate == FRAME_NO_DATA && st_ivas->bfi == 0 )
1925 : {
1926 0 : g192_sid_first = 1; /* SID_FIRST detected for previous AMRWB/AMRWBIO active frames only */
1927 : /* It is not possible to perfectly simulate rate switching conditions EVS->AMRWBIO where:
1928 : the very first SID_FIRST detection is based on a past EVS active frame
1929 : and a good length 0 "SID_FIRST"(NO_DATA) frame is sent in AMRWBIO,
1930 : due to the one frame state memory in the AMRWB legacy G.192 SID_FIRST encoding
1931 : */
1932 0 : move16();
1933 : }
1934 :
1935 399985 : speech_bad = 0;
1936 399985 : move16();
1937 :
1938 399985 : test();
1939 399985 : if ( st_ivas->bfi != 0 && ( is_DTXrate( total_brate ) == 0 ) )
1940 : {
1941 7507 : speech_bad = 1; /* initial ft assumption, CNG_state decides what to do */
1942 7507 : move16();
1943 : }
1944 :
1945 399985 : speech_lost = 0;
1946 399985 : move16();
1947 :
1948 399985 : test();
1949 399985 : if ( total_brate == FRAME_NO_DATA && st_ivas->bfi != 0 ) /* unsent NO_DATA or stolen NO_DATA/signaling frame */
1950 : {
1951 2457 : speech_lost = 1; /* initial ft assumption, CNG_state decides what to do */
1952 2457 : move16();
1953 : }
1954 :
1955 : /* Do not allow decoder to enter CNG-synthesis for any instantly received GOOD+LENGTH==0 frame
1956 : as this frame was never transmitted, one can not know it is good and has a a length of zero ) */
1957 399985 : IF( *CNG != 0 )
1958 : {
1959 : /* We were in CNG synthesis */
1960 18675 : if ( curr_ft_good_sp != 0 )
1961 : {
1962 : /* only a good speech frame makes you leave CNG synthesis */
1963 934 : *CNG = 0;
1964 934 : move16();
1965 : }
1966 : }
1967 : ELSE
1968 : {
1969 : /* We were in SPEECH synthesis */
1970 : /* only a received/detected SID frame can make the decoder enter into CNG synthsis */
1971 381310 : test();
1972 381310 : test();
1973 381310 : if ( g192_sid_first || sid_update || sid_upd_bad )
1974 : {
1975 1010 : *CNG = 1;
1976 1010 : move16();
1977 : }
1978 : }
1979 :
1980 : /* set bfi, total_brate pair for proper decoding */
1981 : /* handle the G.192 _simulated_ untransmitted NO_DATA frame, setting for decoder SPEECH synthesis */
1982 399985 : test();
1983 399985 : test();
1984 399985 : if ( *CNG == 0 && total_brate == FRAME_NO_DATA && st_ivas->bfi == 0 )
1985 : {
1986 80 : st_ivas->bfi = 1; /* SPEECH PLC code will now become active as in a real system */
1987 : /* total_brate= 0 */
1988 80 : move16();
1989 : }
1990 :
1991 : /* handle bad/lost speech frame(and CS bad SID frame) in the decoders CNG synthesis settings pair (total_brate, bfi) */
1992 399985 : test();
1993 399985 : test();
1994 399985 : test();
1995 399985 : test();
1996 399985 : IF( (
1997 : NE_16( bfi, FRAMEMODE_FUTURE ) &&
1998 : ( *CNG != 0 ) && ( ( speech_bad != 0 ) || ( speech_lost != 0 ) ) ) || /* SP_BAD or SPEECH_LOST) --> stay in CNG */
1999 : ( sid_upd_bad != 0 ) ) /* SID_UPD_BAD --> start CNG */
2000 : {
2001 1479 : st_ivas->bfi = 0; /* bfi=0 needed to activate CNG code */
2002 1479 : move16();
2003 1479 : total_brate = FRAME_NO_DATA;
2004 1479 : move32();
2005 : }
2006 :
2007 : /* update for next frame's G.192 file format's odd SID_FIRST detection (primarily for AMRWBIO) */
2008 399985 : test();
2009 399985 : *prev_ft_speech = ( ( curr_ft_good_sp != 0 ) || ( curr_ft_bad_sp != 0 ) );
2010 399985 : move16();
2011 :
2012 : /* st->total brate= total_brate; updated in a good frame below */
2013 :
2014 785842 : FOR( k = 0; k < st_ivas->nCPE; k++ )
2015 : {
2016 385857 : sts = st_ivas->hCPE[k]->hCoreCoder;
2017 1157571 : FOR( n = 0; n < CPE_CHANNELS; n++ )
2018 : {
2019 771714 : sts[n]->bfi = st_ivas->bfi;
2020 771714 : move16();
2021 : }
2022 : }
2023 :
2024 718269 : FOR( k = 0; k < st_ivas->nSCE; k++ )
2025 : {
2026 318284 : sts = st_ivas->hSCE[k]->hCoreCoder;
2027 318284 : sts[0]->bfi = st_ivas->bfi;
2028 318284 : move16();
2029 : }
2030 :
2031 399985 : IF( st_ivas->bfi == 0 )
2032 : {
2033 : /* select Mode 1 or Mode 2 */
2034 391420 : IF( EQ_32( st_ivas->ivas_format, MONO_FORMAT ) ) /* EVS mono */
2035 : {
2036 3100 : decoder_selectCodec( sts[0], total_brate, bit_stream[0] );
2037 3100 : st_ivas->hDecoderConfig->Opt_AMR_WB = sts[0]->Opt_AMR_WB;
2038 3100 : move16();
2039 : }
2040 : ELSE /* IVAS */
2041 : {
2042 388320 : st_ivas->codec_mode = MODE1;
2043 388320 : move16();
2044 388320 : st_ivas->hDecoderConfig->Opt_AMR_WB = 0;
2045 388320 : move16();
2046 : }
2047 : }
2048 :
2049 : /* GOOD frame */
2050 399985 : test();
2051 399985 : if ( st_ivas->bfi == 0 || EQ_16( st_ivas->bfi, FRAMEMODE_FUTURE ) )
2052 : {
2053 : /* GOOD frame - convert ITU-T G.192 words to short values */
2054 391420 : st_ivas->hDecoderConfig->ivas_total_brate = total_brate;
2055 391420 : move32();
2056 : }
2057 :
2058 399985 : st_ivas->bit_stream = bit_stream;
2059 :
2060 399985 : IF( EQ_32( st_ivas->ivas_format, MONO_FORMAT ) )
2061 : {
2062 3100 : ivas_set_bitstream_pointers( st_ivas );
2063 : }
2064 :
2065 399985 : return error;
2066 : }
2067 : #undef WMC_TOOL_SKIP
|