Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <stdlib.h>
6 : #include "options.h" /* Compilation switches */
7 : // #include "prot_fx.h" /* Function prototypes */
8 : #include "rom_enc.h"
9 : #include "rom_com.h"
10 : #include "stl.h"
11 : #include "prot_fx.h" /* Function prototypes */
12 : #include "prot_fx_enc.h" /* Function prototypes */
13 :
14 : /*--------------------------------------------------------------------------------------*
15 : * encode_envelope_indices_fx()
16 : *
17 : * Encode envelope indices
18 : *--------------------------------------------------------------------------------------*/
19 :
20 814 : Word16 encode_envelope_indices_fx( /* o : Number of bits if flag_pack=0,0 if flag_pack=1 Q0 */
21 : BSTR_ENC_HANDLE hBstr, /* i : handle to the bitstream Q0 */
22 : const Word16 num_sfm, /* i : Number of subbands Q0 */
23 : const Word16 numnrmibits, /* i : Bitrate of fall-back coding mode Q0 */
24 : Word16 *difidx, /* i/o: Diff indices/encoded diff indices Q0 */
25 : Word16 *LCmode, /* o : Coding mode if flag_pack=0, i : if flag_pack=1 Q0 */
26 : const Word16 flag_pack, /* i : indicator of packing or estimating bits Q0 */
27 : const Word16 flag_HQ2, /* i : indicator of HQ2 core Q0 */
28 : const Word16 is_transient /* i : indicator of HQ_TRANSIENT Q0 */
29 : )
30 : {
31 : Word16 bits;
32 : Word16 prevj;
33 : Word16 hcode_l;
34 : Word16 i, j;
35 : Word16 difidx_flag;
36 : Word16 index_max, index_min, index_rad;
37 : Word16 difidx_org[NB_SFM]; /* length of this buffer is max(BANDS_MAX,NB_SFM) */
38 : Word16 m, r;
39 : Word16 v, k;
40 :
41 :
42 814 : set16_fx( difidx_org, 0, NB_SFM );
43 814 : difidx_flag = 0;
44 814 : move16();
45 :
46 : /*------------------------------------------------------------------*
47 : * Check Huffman encoding for QNorm indices
48 : *------------------------------------------------------------------*/
49 :
50 : /* LC mode index is changed to synchronize LR_MDCT signaling */
51 : /* LC mode 0 = Context based coding */
52 : /* LC mode 1 = resized huffman coding */
53 : /* LC mode 2 = normal Huffman Coding */
54 : /* LC mode 3 = bit packing */
55 814 : IF( flag_pack == 0 )
56 : {
57 407 : test();
58 407 : IF( is_transient && EQ_16( flag_HQ2, LOW_RATE_HQ_CORE_TRAN ) )
59 : {
60 1 : bits = 0;
61 1 : move16();
62 1 : index_max = 0;
63 1 : move16();
64 1 : index_min = 31;
65 1 : move16();
66 33 : FOR( i = 0; i < num_sfm; i++ )
67 : {
68 32 : IF( GT_16( difidx[i], index_max ) )
69 : {
70 3 : index_max = difidx[i]; /* Q0 */
71 3 : move16();
72 : }
73 32 : IF( LT_16( difidx[i], index_min ) )
74 : {
75 1 : index_min = difidx[i]; /* Q0 */
76 1 : move16();
77 : }
78 : }
79 1 : test();
80 1 : IF( GT_16( index_min, 10 ) && LT_16( index_max, 22 ) )
81 : {
82 32 : FOR( i = 1; i < num_sfm; i++ )
83 : {
84 31 : j = difidx[i]; /* Q0 */
85 31 : move16();
86 31 : bits = add( bits, huffsizn_tran[j] );
87 : }
88 : }
89 1 : hcode_l = 0;
90 1 : move16();
91 1 : *LCmode = 0;
92 1 : move16();
93 1 : prevj = add( difidx[0], OFFSET_NORM );
94 : /* LC mode 0 = Context based coding */
95 32 : FOR( i = 1; i < num_sfm; i++ )
96 : {
97 31 : j = difidx[i]; /* Q0 */
98 31 : move16();
99 31 : IF( GT_16( prevj, HTH_NORM ) )
100 : {
101 : /* above */
102 0 : hcode_l = add( hcode_l, huffsizn_n[31 - j] ); /* Q0 */
103 : }
104 : ELSE
105 : {
106 31 : IF( LT_16( prevj, LTH_NORM ) )
107 : {
108 : /* less */
109 0 : hcode_l = add( hcode_l, huffsizn_n[j] ); /* Q0 */
110 : }
111 : ELSE
112 : {
113 : /* equal */
114 31 : hcode_l = add( hcode_l, huffsizn_e[j] ); /* Q0 */
115 : }
116 : }
117 31 : prevj = j;
118 31 : move16();
119 : }
120 1 : test();
121 1 : IF( GE_16( hcode_l, bits ) && bits != 0 )
122 : {
123 : /* LC mode 1 Transient Huffman Coding */
124 1 : *LCmode = 1;
125 1 : move16();
126 1 : hcode_l = bits;
127 1 : move16();
128 : }
129 : }
130 : ELSE
131 : {
132 : /* Check bits if LC mode == 3 -> Check bits if LC mode == 0 */
133 406 : hcode_l = 0;
134 406 : move16();
135 406 : prevj = add( difidx[0], OFFSET_NORM );
136 14742 : FOR( i = 1; i < num_sfm; i++ )
137 : {
138 14336 : j = difidx[i]; /* Q0 */
139 14336 : move16();
140 14336 : IF( GT_16( prevj, HTH_NORM ) )
141 : {
142 : /* above */
143 2453 : hcode_l = add( hcode_l, huffsizn_n[( 31 - j )] ); /* Q0 */
144 : }
145 : ELSE
146 : {
147 11883 : IF( LT_16( prevj, LTH_NORM ) )
148 : {
149 : /* less */
150 1420 : hcode_l = add( hcode_l, huffsizn_n[j] ); /* Q0 */
151 : }
152 : ELSE
153 : {
154 : /* equal */
155 10463 : hcode_l = add( hcode_l, huffsizn_e[j] ); /* Q0 */
156 : }
157 : }
158 14336 : prevj = j;
159 14336 : move16();
160 : }
161 :
162 406 : *LCmode = 0;
163 406 : move16();
164 :
165 : /* LR-MDCT core doesn't have coding mode 2 and 3 */
166 406 : IF( flag_HQ2 == NORMAL_HQ_CORE )
167 : {
168 : /* Check bits if LC mode == 1 -> Check bits if LC mode == 2 */
169 370 : bits = 0;
170 370 : move16();
171 13950 : FOR( i = 1; i < num_sfm; i++ )
172 : {
173 13580 : j = difidx[i]; /* Q0 */
174 13580 : move16();
175 13580 : bits = add( bits, huffsizn[j] ); /* Q0 */
176 : }
177 :
178 : /*------------------------------------------------------------------------------*
179 : * comparing bit expenses of coding mode 2 with that of the optimal coding mode
180 : *------------------------------------------------------------------------------*/
181 :
182 370 : IF( GT_16( hcode_l, bits ) )
183 : {
184 19 : *LCmode = 2;
185 19 : move16();
186 : }
187 370 : hcode_l = s_min( hcode_l, bits );
188 : }
189 :
190 : /* Check bits if LC mode == 2 -> Check bits if LC mode == 1 */
191 406 : bits = 0;
192 406 : move16();
193 406 : index_max = 0;
194 406 : move16();
195 406 : index_min = 31;
196 406 : move16();
197 14742 : FOR( i = 1; i < num_sfm; i++ )
198 : {
199 14336 : difidx_org[i] = difidx[i]; /* Q0 */
200 14336 : move16();
201 : }
202 :
203 406 : difidx_flag = 0;
204 406 : move16();
205 14336 : FOR( i = 2; i < num_sfm; i++ )
206 : {
207 13930 : IF( GT_16( difidx_org[i - 1], 17 ) )
208 : {
209 2121 : difidx[i] = add( difidx_org[i], s_min( sub( difidx_org[i - 1], 17 ), 3 ) ); /* Q0 */
210 2121 : move16();
211 2121 : IF( GT_16( difidx[i], 31 ) )
212 : {
213 0 : difidx_flag = 1;
214 0 : move16();
215 0 : BREAK;
216 : }
217 : }
218 :
219 13930 : IF( LT_16( difidx_org[i - 1], 13 ) )
220 : {
221 1405 : difidx[i] = add( difidx_org[i], s_max( sub( difidx_org[i - 1], 13 ), -3 ) ); /* Q0 */
222 1405 : move16();
223 1405 : IF( difidx[i] < 0 )
224 : {
225 0 : difidx_flag = 1;
226 0 : move16();
227 0 : BREAK;
228 : }
229 : }
230 : }
231 :
232 406 : index_rad = 0;
233 406 : move16();
234 406 : IF( difidx_flag == 0 )
235 : {
236 14742 : FOR( i = 1; i < num_sfm; i++ )
237 : {
238 14336 : index_max = s_max( index_max, difidx[i] );
239 14336 : index_min = s_min( index_min, difidx[i] );
240 : }
241 :
242 406 : index_rad = s_max( sub( 15, index_min ), sub( index_max, 15 ) );
243 :
244 406 : IF( LE_16( index_rad, HUFF_THR ) )
245 : {
246 13377 : FOR( i = 1; i < num_sfm; i++ )
247 : {
248 13006 : j = difidx[i]; /* Q0 */
249 13006 : move16();
250 13006 : bits = add( bits, resize_huffsizn[j] );
251 : }
252 :
253 : /*------------------------------------------------------------------*
254 : * comparing bit expenses of coding mode 1 with that of coding mode 0
255 : *------------------------------------------------------------------*/
256 :
257 371 : IF( GT_16( hcode_l, bits ) )
258 : {
259 61 : *LCmode = 1;
260 61 : move16();
261 : }
262 371 : hcode_l = s_min( hcode_l, bits );
263 : }
264 : }
265 :
266 : /* LR-MDCT core doesn't have coding mode 2 and 3 */
267 406 : IF( flag_HQ2 == NORMAL_HQ_CORE )
268 : {
269 : /*------------------------------------------------------------------------------*
270 : * comparing bit expenses of coding mode 3 with that of the optimal coding mode
271 : *------------------------------------------------------------------------------*/
272 :
273 370 : IF( GE_16( hcode_l, numnrmibits ) )
274 : {
275 0 : *LCmode = 3;
276 0 : move16();
277 : }
278 370 : hcode_l = s_min( hcode_l, numnrmibits );
279 : }
280 :
281 406 : test();
282 406 : test();
283 406 : IF( ( NE_16( *LCmode, 1 ) && flag_HQ2 == NORMAL_HQ_CORE ) || EQ_16( flag_HQ2, LOW_RATE_HQ_CORE ) )
284 : {
285 12616 : FOR( i = 2; i < num_sfm; i++ )
286 : {
287 12256 : difidx[i] = difidx_org[i]; /* Q0 */
288 12256 : move16();
289 : }
290 : }
291 : }
292 : }
293 : ELSE
294 : {
295 407 : test();
296 407 : IF( EQ_16( flag_HQ2, LOW_RATE_HQ_CORE_TRAN ) || EQ_16( flag_HQ2, LOW_RATE_HQ_CORE ) )
297 : {
298 37 : push_indice( hBstr, IND_HQ2_DENG_HMODE, *LCmode, BITS_DE_HMODE );
299 37 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, difidx[0], BITS_DE_FCOMP );
300 : }
301 : ELSE
302 : {
303 370 : push_indice( hBstr, IND_LC_MODE, *LCmode, 2 );
304 370 : push_indice( hBstr, IND_YNRM, difidx[0], NORM0_BITS );
305 : }
306 :
307 407 : test();
308 407 : IF( is_transient && EQ_16( flag_HQ2, LOW_RATE_HQ_CORE_TRAN ) )
309 : {
310 1 : hcode_l = 0;
311 1 : move16();
312 1 : IF( EQ_16( *LCmode, 1 ) )
313 : {
314 : /* LC mode 0 Transient Huffman Coding */
315 32 : FOR( i = 1; i < num_sfm; i++ )
316 : {
317 31 : j = difidx[i]; /* Q0 */
318 31 : move16();
319 31 : m = huffnorm_tran[j]; /* Q0 */
320 31 : move16();
321 31 : r = huffsizn_tran[j]; /* Q0 */
322 31 : move16();
323 31 : v = 0;
324 31 : move16();
325 :
326 : /* Bit reverse */
327 93 : FOR( k = 0; k < r; k++ )
328 : {
329 62 : v = lshl( v, 1 ); /* Q0 */
330 62 : v = s_or( v, s_and( m, 1 ) ); /* Q0 */
331 62 : m = lshr( m, 1 ); /* Q0 */
332 : }
333 :
334 31 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, v, r );
335 : }
336 : }
337 : ELSE
338 : {
339 : /* LC mode 1 context based Coding */
340 0 : prevj = add( difidx[0], OFFSET_NORM );
341 0 : FOR( i = 1; i < num_sfm; i++ )
342 : {
343 0 : j = difidx[i]; /* Q0 */
344 0 : move16();
345 :
346 0 : IF( GT_16( prevj, HTH_NORM ) )
347 : {
348 : /* above */
349 0 : r = huffsizn_n[( 31 - j )]; /* Q0 */
350 0 : move16();
351 0 : m = huffnorm_n[( 31 - j )]; /* Q0 */
352 0 : move16();
353 : }
354 : ELSE
355 : {
356 0 : IF( LT_16( prevj, LTH_NORM ) )
357 : {
358 : /* less */
359 0 : r = huffsizn_n[j]; /* Q0 */
360 0 : move16();
361 0 : m = huffnorm_n[j]; /* Q0 */
362 0 : move16();
363 : }
364 : ELSE
365 : {
366 : /* equal */
367 0 : r = huffsizn_e[j]; /* Q0 */
368 0 : move16();
369 0 : m = huffnorm_e[j]; /* Q0 */
370 0 : move16();
371 : }
372 : }
373 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, m, r );
374 0 : prevj = j;
375 0 : move16();
376 : }
377 : }
378 : }
379 : ELSE
380 : {
381 406 : hcode_l = 0;
382 406 : move16();
383 406 : IF( *LCmode == 0 )
384 : {
385 : /* LC mode 3 -> LC mode 0 */
386 327 : prevj = add( difidx[0], OFFSET_NORM );
387 11964 : FOR( i = 1; i < num_sfm; i++ )
388 : {
389 11637 : j = difidx[i]; /* Q0 */
390 11637 : move16();
391 :
392 11637 : IF( GT_16( prevj, HTH_NORM ) )
393 : {
394 : /* above */
395 1951 : r = huffsizn_n[( 31 - j )]; /* Q0 */
396 1951 : move16();
397 1951 : m = huffnorm_n[( 31 - j )]; /* Q0 */
398 1951 : move16();
399 : }
400 : ELSE
401 : {
402 9686 : IF( LT_16( prevj, LTH_NORM ) )
403 : {
404 : /* less */
405 1080 : r = huffsizn_n[j]; /* Q0 */
406 1080 : move16();
407 1080 : m = huffnorm_n[j]; /* Q0 */
408 1080 : move16();
409 : }
410 : ELSE
411 : {
412 : /* equal */
413 8606 : r = huffsizn_e[j]; /* Q0 */
414 8606 : move16();
415 8606 : m = huffnorm_e[j]; /* Q0 */
416 8606 : move16();
417 : }
418 : }
419 :
420 11637 : IF( EQ_16( flag_HQ2, LOW_RATE_HQ_CORE ) )
421 : {
422 441 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, m, r );
423 : }
424 : ELSE
425 : {
426 11196 : push_indice( hBstr, IND_YNRM, m, r );
427 : }
428 :
429 11637 : prevj = j;
430 11637 : move16();
431 : }
432 : }
433 79 : ELSE IF( EQ_16( *LCmode, 1 ) )
434 : {
435 61 : IF( EQ_16( flag_HQ2, 1 ) )
436 : {
437 15 : index_max = 0;
438 15 : move16();
439 15 : index_min = 31;
440 15 : move16();
441 330 : FOR( i = 1; i < num_sfm; i++ )
442 : {
443 315 : difidx_org[i] = difidx[i]; /* Q0 */
444 315 : move16();
445 : }
446 :
447 315 : FOR( i = 2; i < num_sfm; i++ )
448 : {
449 300 : IF( GT_16( difidx_org[i - 1], 17 ) )
450 : {
451 51 : difidx[i] = add( difidx_org[i], s_min( sub( difidx_org[i - 1], 17 ), 3 ) ); /* Q0 */
452 51 : move16();
453 51 : IF( GT_16( difidx[i], 31 ) )
454 : {
455 0 : difidx_flag = 1;
456 0 : move16();
457 0 : BREAK;
458 : }
459 : }
460 :
461 300 : IF( LT_16( difidx_org[i - 1], 13 ) )
462 : {
463 13 : difidx[i] = add( difidx_org[i], s_max( sub( difidx_org[i - 1], 13 ), -3 ) ); /* Q0 */
464 13 : move16();
465 13 : IF( difidx[i] < 0 )
466 : {
467 0 : difidx_flag = 1;
468 0 : move16();
469 0 : BREAK;
470 : }
471 : }
472 : }
473 :
474 15 : IF( difidx_flag == 0 )
475 : {
476 330 : FOR( i = 1; i < num_sfm; i++ )
477 : {
478 315 : index_max = s_max( index_max, difidx[i] );
479 315 : index_min = s_min( index_min, difidx[i] );
480 : }
481 :
482 15 : index_rad = s_max( sub( 15, index_min ), sub( index_max, 15 ) );
483 :
484 15 : IF( LE_16( index_rad, HUFF_THR ) )
485 : {
486 330 : FOR( i = 1; i < num_sfm; i++ )
487 : {
488 315 : j = difidx[i]; /* Q0 */
489 315 : move16();
490 : }
491 : }
492 : }
493 : }
494 :
495 : /* LC mode 2 -> LC mode 1 */
496 2096 : FOR( i = 1; i < num_sfm; i++ )
497 : {
498 2035 : j = difidx[i]; /* Q0 */
499 2035 : move16();
500 :
501 2035 : m = resize_huffnorm[j]; /* Q0 */
502 2035 : move16();
503 2035 : r = resize_huffsizn[j]; /* Q0 */
504 2035 : move16();
505 2035 : v = 0;
506 2035 : move16();
507 :
508 : /* Bit reverse */
509 8697 : FOR( k = 0; k < r; k++ )
510 : {
511 6662 : v = lshl( v, 1 ); /* Q0 */
512 6662 : v = s_or( v, s_and( m, 1 ) ); /* Q0 */
513 6662 : m = lshr( m, 1 ); /* Q0 */
514 : }
515 :
516 2035 : IF( flag_HQ2 == 0 )
517 : {
518 1720 : push_indice( hBstr, IND_YNRM, v, r );
519 : }
520 : ELSE
521 : {
522 315 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, v, r );
523 : }
524 : }
525 : }
526 18 : ELSE IF( EQ_16( *LCmode, 2 ) )
527 : {
528 : /* LC mode 1 -> LC mode 2 */
529 682 : FOR( i = 1; i < num_sfm; i++ )
530 : {
531 664 : j = difidx[i]; /* Q0 */
532 664 : move16();
533 :
534 664 : m = huffnorm[j]; /* Q0 */
535 664 : move16();
536 664 : r = huffsizn[j]; /* Q0 */
537 664 : move16();
538 :
539 664 : push_indice( hBstr, IND_YNRM, m, r );
540 : }
541 : }
542 : ELSE
543 : {
544 0 : FOR( i = 1; i < num_sfm; i++ )
545 : {
546 0 : push_indice( hBstr, IND_YNRM, difidx[i], NORMI_BITS );
547 : }
548 : }
549 : }
550 : }
551 :
552 814 : return hcode_l;
553 : }
554 :
555 15630 : Word16 encode_envelope_indices_ivas_fx( /* o : Number of bits if flag_pack=0,0 if flag_pack=1 Q0 */
556 : BSTR_ENC_HANDLE hBstr, /* i : handle to the bitstream Q0 */
557 : const Word16 num_sfm, /* i : Number of subbands Q0 */
558 : const Word16 numnrmibits, /* i : Bitrate of fall-back coding mode Q0 */
559 : Word16 *difidx, /* i/o: Diff indices/encoded diff indices Q0 */
560 : Word16 *LCmode, /* o : Coding mode if flag_pack=0, i : if flag_pack=1 Q0 */
561 : const Word16 flag_pack, /* i : indicator of packing or estimating bits Q0 */
562 : const Word16 flag_HQ2, /* i : indicator of HQ2 core Q0 */
563 : const Word16 is_transient /* i : indicator of HQ_TRANSIENT Q0 */
564 : )
565 : {
566 : Word16 bits;
567 : Word16 prevj;
568 : Word16 hcode_l;
569 : Word16 i, j;
570 : Word16 difidx_flag;
571 : Word16 index_max, index_min, index_rad;
572 : Word16 difidx_org[NB_SFM]; /* length of this buffer is max(BANDS_MAX,NB_SFM) */
573 : Word16 m, r;
574 : Word16 v, k;
575 :
576 :
577 15630 : set16_fx( difidx_org, 0, NB_SFM );
578 15630 : difidx_flag = 0;
579 15630 : move16();
580 :
581 : /*------------------------------------------------------------------*
582 : * Check Huffman encoding for QNorm indices
583 : *------------------------------------------------------------------*/
584 :
585 : /* LC mode index is changed to synchronize LR_MDCT signaling */
586 : /* LC mode 0 = Context based coding */
587 : /* LC mode 1 = resized huffman coding */
588 : /* LC mode 2 = normal Huffman Coding */
589 : /* LC mode 3 = bit packing */
590 15630 : IF( flag_pack == 0 )
591 : {
592 7815 : test();
593 7815 : IF( is_transient && EQ_16( flag_HQ2, LOW_RATE_HQ_CORE_TRAN ) )
594 : {
595 0 : bits = 0;
596 0 : move16();
597 0 : index_max = 0;
598 0 : move16();
599 0 : index_min = 31;
600 0 : move16();
601 0 : FOR( i = 0; i < num_sfm; i++ )
602 : {
603 0 : IF( GT_16( difidx[i], index_max ) )
604 : {
605 0 : index_max = difidx[i]; /* Q0 */
606 0 : move16();
607 : }
608 0 : IF( LT_16( difidx[i], index_min ) )
609 : {
610 0 : index_min = difidx[i]; /* Q0 */
611 0 : move16();
612 : }
613 : }
614 0 : test();
615 0 : IF( GT_16( index_min, 10 ) && LT_16( index_max, 22 ) )
616 : {
617 0 : FOR( i = 1; i < num_sfm; i++ )
618 : {
619 0 : j = difidx[i]; /* Q0 */
620 0 : move16();
621 0 : bits = add( bits, huffsizn_tran[j] );
622 : }
623 : }
624 0 : hcode_l = 0;
625 0 : move16();
626 0 : *LCmode = 0;
627 0 : move16();
628 0 : prevj = add( difidx[0], OFFSET_NORM );
629 : /* LC mode 0 = Context based coding */
630 0 : FOR( i = 1; i < num_sfm; i++ )
631 : {
632 0 : j = difidx[i]; /* Q0 */
633 0 : move16();
634 0 : IF( GT_16( prevj, HTH_NORM ) )
635 : {
636 : /* above */
637 0 : hcode_l = add( hcode_l, huffsizn_n[31 - j] ); /* Q0 */
638 : }
639 : ELSE
640 : {
641 0 : IF( LT_16( prevj, LTH_NORM ) )
642 : {
643 : /* less */
644 0 : hcode_l = add( hcode_l, huffsizn_n[j] ); /* Q0 */
645 : }
646 : ELSE
647 : {
648 : /* equal */
649 0 : hcode_l = add( hcode_l, huffsizn_e[j] ); /* Q0 */
650 : }
651 : }
652 0 : prevj = j;
653 0 : move16();
654 : }
655 0 : test();
656 0 : IF( GE_16( hcode_l, bits ) && bits != 0 )
657 : {
658 : /* LC mode 1 Transient Huffman Coding */
659 0 : *LCmode = 1;
660 0 : move16();
661 0 : hcode_l = bits;
662 0 : move16();
663 : }
664 : }
665 : ELSE
666 : {
667 : /* Check bits if LC mode == 3 -> Check bits if LC mode == 0 */
668 7815 : hcode_l = 0;
669 7815 : move16();
670 7815 : prevj = add( difidx[0], OFFSET_NORM );
671 226494 : FOR( i = 1; i < num_sfm; i++ )
672 : {
673 218679 : j = difidx[i]; /* Q0 */
674 218679 : move16();
675 218679 : IF( GT_16( prevj, HTH_NORM ) )
676 : {
677 : /* above */
678 48124 : hcode_l = add( hcode_l, huffsizn_n[( 31 - j )] ); /* Q0 */
679 : }
680 : ELSE
681 : {
682 170555 : IF( LT_16( prevj, LTH_NORM ) )
683 : {
684 : /* less */
685 30038 : hcode_l = add( hcode_l, huffsizn_n[j] ); /* Q0 */
686 : }
687 : ELSE
688 : {
689 : /* equal */
690 140517 : hcode_l = add( hcode_l, huffsizn_e[j] ); /* Q0 */
691 : }
692 : }
693 218679 : prevj = j;
694 218679 : move16();
695 : }
696 :
697 7815 : *LCmode = 0;
698 7815 : move16();
699 :
700 : /* LR-MDCT core doesn't have coding mode 2 and 3 */
701 7815 : IF( flag_HQ2 == NORMAL_HQ_CORE )
702 : {
703 : /* Check bits if LC mode == 1 -> Check bits if LC mode == 2 */
704 7815 : bits = 0;
705 7815 : move16();
706 226494 : FOR( i = 1; i < num_sfm; i++ )
707 : {
708 218679 : j = difidx[i]; /* Q0 */
709 218679 : move16();
710 218679 : bits = add( bits, huffsizn[j] );
711 : }
712 :
713 : /*------------------------------------------------------------------------------*
714 : * comparing bit expenses of coding mode 2 with that of the optimal coding mode
715 : *------------------------------------------------------------------------------*/
716 :
717 7815 : IF( GT_16( hcode_l, bits ) )
718 : {
719 1029 : *LCmode = 2;
720 1029 : move16();
721 : }
722 7815 : hcode_l = s_min( hcode_l, bits );
723 : }
724 :
725 : /* Check bits if LC mode == 2 -> Check bits if LC mode == 1 */
726 7815 : bits = 0;
727 7815 : move16();
728 7815 : index_max = 0;
729 7815 : move16();
730 7815 : index_min = 31;
731 7815 : move16();
732 226494 : FOR( i = 1; i < num_sfm; i++ )
733 : {
734 218679 : difidx_org[i] = difidx[i]; /* Q0 */
735 218679 : move16();
736 : }
737 :
738 7815 : difidx_flag = 0;
739 7815 : move16();
740 218679 : FOR( i = 2; i < num_sfm; i++ )
741 : {
742 210864 : IF( GT_16( difidx_org[i - 1], 17 ) )
743 : {
744 42491 : difidx[i] = add( difidx_org[i], s_min( sub( difidx_org[i - 1], 17 ), 3 ) ); /* Q0 */
745 42491 : move16();
746 42491 : IF( GT_16( difidx[i], 31 ) )
747 : {
748 0 : difidx_flag = 1;
749 0 : move16();
750 0 : BREAK;
751 : }
752 : }
753 :
754 210864 : IF( LT_16( difidx_org[i - 1], 13 ) )
755 : {
756 29969 : difidx[i] = add( difidx_org[i], s_max( sub( difidx_org[i - 1], 13 ), -3 ) ); /* Q0 */
757 29969 : move16();
758 29969 : IF( difidx[i] < 0 )
759 : {
760 0 : difidx_flag = 1;
761 0 : move16();
762 0 : BREAK;
763 : }
764 : }
765 : }
766 :
767 7815 : index_rad = 0;
768 7815 : move16();
769 7815 : IF( difidx_flag == 0 )
770 : {
771 226494 : FOR( i = 1; i < num_sfm; i++ )
772 : {
773 218679 : index_max = s_max( index_max, difidx[i] );
774 218679 : index_min = s_min( index_min, difidx[i] );
775 : }
776 :
777 7815 : index_rad = s_max( sub( 15, index_min ), sub( index_max, 15 ) );
778 :
779 7815 : IF( LE_16( index_rad, HUFF_THR ) )
780 : {
781 216715 : FOR( i = 1; i < num_sfm; i++ )
782 : {
783 209225 : j = difidx[i]; /* Q0 */
784 209225 : move16();
785 209225 : bits = add( bits, resize_huffsizn[j] );
786 : }
787 :
788 : /*------------------------------------------------------------------*
789 : * comparing bit expenses of coding mode 1 with that of coding mode 0
790 : *------------------------------------------------------------------*/
791 :
792 7490 : IF( GT_16( hcode_l, bits ) )
793 : {
794 2308 : *LCmode = 1;
795 2308 : move16();
796 : }
797 7490 : hcode_l = s_min( hcode_l, bits );
798 : }
799 : }
800 :
801 : /* LR-MDCT core doesn't have coding mode 2 and 3 */
802 7815 : IF( flag_HQ2 == NORMAL_HQ_CORE )
803 : {
804 : /*------------------------------------------------------------------------------*
805 : * comparing bit expenses of coding mode 3 with that of the optimal coding mode
806 : *------------------------------------------------------------------------------*/
807 :
808 7815 : IF( GE_16( hcode_l, numnrmibits ) )
809 : {
810 123 : *LCmode = 3;
811 123 : move16();
812 : }
813 7815 : hcode_l = s_min( hcode_l, numnrmibits );
814 : }
815 :
816 7815 : test();
817 7815 : test();
818 7815 : IF( ( NE_16( *LCmode, 1 ) && flag_HQ2 == NORMAL_HQ_CORE ) || EQ_16( flag_HQ2, LOW_RATE_HQ_CORE ) )
819 : {
820 153297 : FOR( i = 2; i < num_sfm; i++ )
821 : {
822 147790 : difidx[i] = difidx_org[i]; /* Q0 */
823 147790 : move16();
824 : }
825 : }
826 : }
827 : }
828 : ELSE
829 : {
830 7815 : test();
831 7815 : IF( EQ_16( flag_HQ2, LOW_RATE_HQ_CORE_TRAN ) || EQ_16( flag_HQ2, LOW_RATE_HQ_CORE ) )
832 : {
833 0 : push_indice( hBstr, IND_HQ2_DENG_HMODE, *LCmode, BITS_DE_HMODE );
834 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, difidx[0], BITS_DE_FCOMP );
835 : }
836 : ELSE
837 : {
838 7815 : push_indice( hBstr, IND_LC_MODE, *LCmode, 2 );
839 7815 : push_indice( hBstr, IND_YNRM, difidx[0], NORM0_BITS );
840 : }
841 :
842 7815 : test();
843 7815 : IF( is_transient && EQ_16( flag_HQ2, LOW_RATE_HQ_CORE_TRAN ) )
844 : {
845 0 : hcode_l = 0;
846 0 : move16();
847 0 : IF( EQ_16( *LCmode, 1 ) )
848 : {
849 : /* LC mode 0 Transient Huffman Coding */
850 0 : FOR( i = 1; i < num_sfm; i++ )
851 : {
852 0 : j = difidx[i]; /* Q0 */
853 0 : move16();
854 0 : m = huffnorm_tran[j]; /* Q0 */
855 0 : move16();
856 0 : r = huffsizn_tran[j]; /* Q0 */
857 0 : move16();
858 0 : v = 0;
859 0 : move16();
860 :
861 : /* Bit reverse */
862 0 : FOR( k = 0; k < r; k++ )
863 : {
864 0 : v = lshl( v, 1 ); /* Q0 */
865 0 : v = s_or( v, s_and( m, 1 ) ); /* Q0 */
866 0 : m = lshr( m, 1 ); /* Q0 */
867 : }
868 :
869 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, v, r );
870 : }
871 : }
872 : ELSE
873 : {
874 : /* LC mode 1 context based Coding */
875 0 : prevj = add( difidx[0], OFFSET_NORM );
876 0 : FOR( i = 1; i < num_sfm; i++ )
877 : {
878 0 : j = difidx[i]; /* Q0 */
879 0 : move16();
880 :
881 0 : IF( GT_16( prevj, HTH_NORM ) )
882 : {
883 : /* above */
884 0 : r = huffsizn_n[( 31 - j )]; /* Q0 */
885 0 : move16();
886 0 : m = huffnorm_n[( 31 - j )]; /* Q0 */
887 0 : move16();
888 : }
889 : ELSE
890 : {
891 0 : IF( LT_16( prevj, LTH_NORM ) )
892 : {
893 : /* less */
894 0 : r = huffsizn_n[j]; /* Q0 */
895 0 : move16();
896 0 : m = huffnorm_n[j]; /* Q0 */
897 0 : move16();
898 : }
899 : ELSE
900 : {
901 : /* equal */
902 0 : r = huffsizn_e[j]; /* Q0 */
903 0 : move16();
904 0 : m = huffnorm_e[j]; /* Q0 */
905 0 : move16();
906 : }
907 : }
908 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, m, r );
909 0 : prevj = j;
910 0 : move16();
911 : }
912 : }
913 : }
914 : ELSE
915 : {
916 7815 : hcode_l = 0;
917 7815 : move16();
918 7815 : IF( *LCmode == 0 )
919 : {
920 : /* LC mode 3 -> LC mode 0 */
921 4468 : prevj = add( difidx[0], OFFSET_NORM );
922 130804 : FOR( i = 1; i < num_sfm; i++ )
923 : {
924 126336 : j = difidx[i]; /* Q0 */
925 126336 : move16();
926 :
927 126336 : IF( GT_16( prevj, HTH_NORM ) )
928 : {
929 : /* above */
930 27184 : r = huffsizn_n[( 31 - j )]; /* Q0 */
931 27184 : move16();
932 27184 : m = huffnorm_n[( 31 - j )]; /* Q0 */
933 27184 : move16();
934 : }
935 : ELSE
936 : {
937 99152 : IF( LT_16( prevj, LTH_NORM ) )
938 : {
939 : /* less */
940 16765 : r = huffsizn_n[j]; /* Q0 */
941 16765 : move16();
942 16765 : m = huffnorm_n[j]; /* Q0 */
943 16765 : move16();
944 : }
945 : ELSE
946 : {
947 : /* equal */
948 82387 : r = huffsizn_e[j]; /* Q0 */
949 82387 : move16();
950 82387 : m = huffnorm_e[j]; /* Q0 */
951 82387 : move16();
952 : }
953 : }
954 :
955 126336 : IF( EQ_16( flag_HQ2, LOW_RATE_HQ_CORE ) )
956 : {
957 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, m, r );
958 : }
959 : ELSE
960 : {
961 126336 : push_indice( hBstr, IND_YNRM, m, r );
962 : }
963 :
964 126336 : prevj = j;
965 126336 : move16();
966 : }
967 : }
968 3347 : ELSE IF( EQ_16( *LCmode, 1 ) )
969 : {
970 2308 : IF( EQ_16( flag_HQ2, 1 ) )
971 : {
972 0 : index_max = 0;
973 0 : move16();
974 0 : index_min = 31;
975 0 : move16();
976 0 : FOR( i = 1; i < num_sfm; i++ )
977 : {
978 0 : difidx_org[i] = difidx[i]; /* Q0 */
979 0 : move16();
980 : }
981 :
982 0 : FOR( i = 2; i < num_sfm; i++ )
983 : {
984 0 : IF( GT_16( difidx_org[i - 1], 17 ) )
985 : {
986 0 : difidx[i] = add( difidx_org[i], s_min( sub( difidx_org[i - 1], 17 ), 3 ) ); /* Q0 */
987 0 : move16();
988 0 : IF( GT_16( difidx[i], 31 ) )
989 : {
990 0 : difidx_flag = 1;
991 0 : move16();
992 0 : BREAK;
993 : }
994 : }
995 :
996 0 : IF( LT_16( difidx_org[i - 1], 13 ) )
997 : {
998 0 : difidx[i] = add( difidx_org[i], s_max( sub( difidx_org[i - 1], 13 ), -3 ) ); /* Q0 */
999 0 : move16();
1000 0 : IF( difidx[i] < 0 )
1001 : {
1002 0 : difidx_flag = 1;
1003 0 : move16();
1004 0 : BREAK;
1005 : }
1006 : }
1007 : }
1008 :
1009 0 : IF( difidx_flag == 0 )
1010 : {
1011 0 : FOR( i = 1; i < num_sfm; i++ )
1012 : {
1013 0 : index_max = s_max( index_max, difidx[i] );
1014 0 : index_min = s_min( index_min, difidx[i] );
1015 : }
1016 :
1017 0 : index_rad = s_max( sub( 15, index_min ), sub( index_max, 15 ) );
1018 :
1019 0 : IF( LE_16( index_rad, HUFF_THR ) )
1020 : {
1021 0 : FOR( i = 1; i < num_sfm; i++ )
1022 : {
1023 0 : j = difidx[i]; /* Q0 */
1024 0 : move16();
1025 : }
1026 : }
1027 : }
1028 : }
1029 :
1030 : /* LC mode 2 -> LC mode 1 */
1031 67690 : FOR( i = 1; i < num_sfm; i++ )
1032 : {
1033 65382 : j = difidx[i]; /* Q0 */
1034 65382 : move16();
1035 :
1036 65382 : m = resize_huffnorm[j]; /* Q0 */
1037 65382 : move16();
1038 65382 : r = resize_huffsizn[j]; /* Q0 */
1039 65382 : move16();
1040 65382 : v = 0;
1041 65382 : move16();
1042 :
1043 : /* Bit reverse */
1044 277183 : FOR( k = 0; k < r; k++ )
1045 : {
1046 211801 : v = lshl( v, 1 ); /* Q0 */
1047 211801 : v = s_or( v, s_and( m, 1 ) ); /* Q0 */
1048 211801 : m = lshr( m, 1 ); /* Q0 */
1049 : }
1050 :
1051 65382 : IF( flag_HQ2 == 0 )
1052 : {
1053 65382 : push_indice( hBstr, IND_YNRM, v, r );
1054 : }
1055 : ELSE
1056 : {
1057 0 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, v, r );
1058 : }
1059 : }
1060 : }
1061 1039 : ELSE IF( EQ_16( *LCmode, 2 ) )
1062 : {
1063 : /* LC mode 1 -> LC mode 2 */
1064 25187 : FOR( i = 1; i < num_sfm; i++ )
1065 : {
1066 24271 : j = difidx[i]; /* Q0 */
1067 24271 : move16();
1068 :
1069 24271 : m = huffnorm[j]; /* Q0 */
1070 24271 : move16();
1071 24271 : r = huffsizn[j]; /* Q0 */
1072 24271 : move16();
1073 :
1074 24271 : push_indice( hBstr, IND_YNRM, m, r );
1075 : }
1076 : }
1077 : ELSE
1078 : {
1079 2813 : FOR( i = 1; i < num_sfm; i++ )
1080 : {
1081 2690 : push_indice( hBstr, IND_YNRM, difidx[i], NORMI_BITS );
1082 : }
1083 : }
1084 : }
1085 : }
1086 :
1087 15630 : return hcode_l;
1088 : }
1089 :
1090 : /*--------------------------------------------------------------------------*
1091 : * diff_envelope_coding_fx()
1092 : *
1093 : * Differential envelope coding
1094 : *--------------------------------------------------------------------------*/
1095 :
1096 8185 : void diff_envelope_coding_fx(
1097 : const Word16 is_transient, /* i : transient indicator Q0 */
1098 : const Word16 num_env_bands, /* i : number of envelope bands to code Q0 */
1099 : const Word16 start_norm, /* i : start of envelope coding Q0 */
1100 : Word16 *ynrm, /* i/o: quantization indices for norms Q0 */
1101 : Word16 *normqlg2, /* i/o: quantized norms Q0 */
1102 : Word16 *difidx /* o : differential code Q0 */
1103 : )
1104 : {
1105 : Word16 i, tmp;
1106 : Word16 idxbuf[NB_SFM];
1107 : Word16 normbuf[NB_SFM];
1108 :
1109 : /* Differential coding for indices of quantized norms */
1110 8185 : IF( is_transient != 0 )
1111 : {
1112 : /* Reorder quantization indices and quantized norms */
1113 493 : reordernorm_fx( ynrm, normqlg2, idxbuf, normbuf, num_env_bands );
1114 493 : diffcod_fx( num_env_bands, idxbuf, &difidx[1] );
1115 493 : difidx[0] = idxbuf[0]; /* Q0 */
1116 493 : move16();
1117 493 : recovernorm_fx( idxbuf, ynrm, normqlg2, num_env_bands );
1118 : }
1119 : ELSE
1120 : {
1121 7692 : diffcod_fx( num_env_bands, &ynrm[start_norm], &difidx[1] );
1122 7692 : difidx[0] = ynrm[start_norm]; /* Q0 */
1123 7692 : move16();
1124 :
1125 7692 : tmp = add( start_norm, num_env_bands );
1126 227207 : FOR( i = start_norm; i < tmp; i++ )
1127 : {
1128 219515 : normqlg2[i] = dicnlg2[ynrm[i]]; /* Q0 */
1129 219515 : move16();
1130 219515 : move16();
1131 : }
1132 : }
1133 :
1134 8185 : return;
1135 : }
|