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 848 : 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 848 : set16_fx( difidx_org, 0, NB_SFM );
43 848 : difidx_flag = 0;
44 848 : 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 848 : IF( flag_pack == 0 )
56 : {
57 424 : test();
58 424 : 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 423 : hcode_l = 0;
134 423 : move16();
135 423 : prevj = add( difidx[0], OFFSET_NORM );
136 15234 : FOR( i = 1; i < num_sfm; i++ )
137 : {
138 14811 : j = difidx[i]; /* Q0 */
139 14811 : move16();
140 14811 : IF( GT_16( prevj, HTH_NORM ) )
141 : {
142 : /* above */
143 2525 : hcode_l = add( hcode_l, huffsizn_n[( 31 - j )] ); /* Q0 */
144 : }
145 : ELSE
146 : {
147 12286 : IF( LT_16( prevj, LTH_NORM ) )
148 : {
149 : /* less */
150 1457 : hcode_l = add( hcode_l, huffsizn_n[j] ); /* Q0 */
151 : }
152 : ELSE
153 : {
154 : /* equal */
155 10829 : hcode_l = add( hcode_l, huffsizn_e[j] ); /* Q0 */
156 : }
157 : }
158 14811 : prevj = j;
159 14811 : move16();
160 : }
161 :
162 423 : *LCmode = 0;
163 423 : move16();
164 :
165 : /* LR-MDCT core doesn't have coding mode 2 and 3 */
166 423 : IF( flag_HQ2 == NORMAL_HQ_CORE )
167 : {
168 : /* Check bits if LC mode == 1 -> Check bits if LC mode == 2 */
169 388 : bits = 0;
170 388 : move16();
171 14464 : FOR( i = 1; i < num_sfm; i++ )
172 : {
173 14076 : j = difidx[i]; /* Q0 */
174 14076 : move16();
175 14076 : 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 388 : IF( GT_16( hcode_l, bits ) )
183 : {
184 20 : *LCmode = 2;
185 20 : move16();
186 : }
187 388 : hcode_l = s_min( hcode_l, bits );
188 : }
189 :
190 : /* Check bits if LC mode == 2 -> Check bits if LC mode == 1 */
191 423 : bits = 0;
192 423 : move16();
193 423 : index_max = 0;
194 423 : move16();
195 423 : index_min = 31;
196 423 : move16();
197 15234 : FOR( i = 1; i < num_sfm; i++ )
198 : {
199 14811 : difidx_org[i] = difidx[i]; /* Q0 */
200 14811 : move16();
201 : }
202 :
203 423 : difidx_flag = 0;
204 423 : move16();
205 14811 : FOR( i = 2; i < num_sfm; i++ )
206 : {
207 14388 : IF( GT_16( difidx_org[i - 1], 17 ) )
208 : {
209 2176 : difidx[i] = add( difidx_org[i], s_min( sub( difidx_org[i - 1], 17 ), 3 ) ); /* Q0 */
210 2176 : move16();
211 2176 : IF( GT_16( difidx[i], 31 ) )
212 : {
213 0 : difidx_flag = 1;
214 0 : move16();
215 0 : BREAK;
216 : }
217 : }
218 :
219 14388 : IF( LT_16( difidx_org[i - 1], 13 ) )
220 : {
221 1445 : difidx[i] = add( difidx_org[i], s_max( sub( difidx_org[i - 1], 13 ), -3 ) ); /* Q0 */
222 1445 : move16();
223 1445 : IF( difidx[i] < 0 )
224 : {
225 0 : difidx_flag = 1;
226 0 : move16();
227 0 : BREAK;
228 : }
229 : }
230 : }
231 :
232 423 : index_rad = 0;
233 423 : move16();
234 423 : IF( difidx_flag == 0 )
235 : {
236 15234 : FOR( i = 1; i < num_sfm; i++ )
237 : {
238 14811 : index_max = s_max( index_max, difidx[i] );
239 14811 : index_min = s_min( index_min, difidx[i] );
240 : }
241 :
242 423 : index_rad = s_max( sub( 15, index_min ), sub( index_max, 15 ) );
243 :
244 423 : IF( LE_16( index_rad, HUFF_THR ) )
245 : {
246 13838 : FOR( i = 1; i < num_sfm; i++ )
247 : {
248 13451 : j = difidx[i]; /* Q0 */
249 13451 : move16();
250 13451 : 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 387 : IF( GT_16( hcode_l, bits ) )
258 : {
259 58 : *LCmode = 1;
260 58 : move16();
261 : }
262 387 : hcode_l = s_min( hcode_l, bits );
263 : }
264 : }
265 :
266 : /* LR-MDCT core doesn't have coding mode 2 and 3 */
267 423 : 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 388 : IF( GE_16( hcode_l, numnrmibits ) )
274 : {
275 0 : *LCmode = 3;
276 0 : move16();
277 : }
278 388 : hcode_l = s_min( hcode_l, numnrmibits );
279 : }
280 :
281 423 : test();
282 423 : test();
283 423 : IF( ( NE_16( *LCmode, 1 ) && flag_HQ2 == NORMAL_HQ_CORE ) || EQ_16( flag_HQ2, LOW_RATE_HQ_CORE ) )
284 : {
285 13121 : FOR( i = 2; i < num_sfm; i++ )
286 : {
287 12743 : difidx[i] = difidx_org[i]; /* Q0 */
288 12743 : move16();
289 : }
290 : }
291 : }
292 : }
293 : ELSE
294 : {
295 424 : test();
296 424 : IF( EQ_16( flag_HQ2, LOW_RATE_HQ_CORE_TRAN ) || EQ_16( flag_HQ2, LOW_RATE_HQ_CORE ) )
297 : {
298 36 : push_indice( hBstr, IND_HQ2_DENG_HMODE, *LCmode, BITS_DE_HMODE );
299 36 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, difidx[0], BITS_DE_FCOMP );
300 : }
301 : ELSE
302 : {
303 388 : push_indice( hBstr, IND_LC_MODE, *LCmode, 2 );
304 388 : push_indice( hBstr, IND_YNRM, difidx[0], NORM0_BITS );
305 : }
306 :
307 424 : test();
308 424 : 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 423 : hcode_l = 0;
382 423 : move16();
383 423 : IF( *LCmode == 0 )
384 : {
385 : /* LC mode 3 -> LC mode 0 */
386 346 : prevj = add( difidx[0], OFFSET_NORM );
387 12500 : FOR( i = 1; i < num_sfm; i++ )
388 : {
389 12154 : j = difidx[i]; /* Q0 */
390 12154 : move16();
391 :
392 12154 : IF( GT_16( prevj, HTH_NORM ) )
393 : {
394 : /* above */
395 2025 : r = huffsizn_n[( 31 - j )]; /* Q0 */
396 2025 : move16();
397 2025 : m = huffnorm_n[( 31 - j )]; /* Q0 */
398 2025 : move16();
399 : }
400 : ELSE
401 : {
402 10129 : IF( LT_16( prevj, LTH_NORM ) )
403 : {
404 : /* less */
405 1119 : r = huffsizn_n[j]; /* Q0 */
406 1119 : move16();
407 1119 : m = huffnorm_n[j]; /* Q0 */
408 1119 : move16();
409 : }
410 : ELSE
411 : {
412 : /* equal */
413 9010 : r = huffsizn_e[j]; /* Q0 */
414 9010 : move16();
415 9010 : m = huffnorm_e[j]; /* Q0 */
416 9010 : move16();
417 : }
418 : }
419 :
420 12154 : IF( EQ_16( flag_HQ2, LOW_RATE_HQ_CORE ) )
421 : {
422 462 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, m, r );
423 : }
424 : ELSE
425 : {
426 11692 : push_indice( hBstr, IND_YNRM, m, r );
427 : }
428 :
429 12154 : prevj = j;
430 12154 : move16();
431 : }
432 : }
433 77 : ELSE IF( EQ_16( *LCmode, 1 ) )
434 : {
435 58 : IF( EQ_16( flag_HQ2, 1 ) )
436 : {
437 13 : index_max = 0;
438 13 : move16();
439 13 : index_min = 31;
440 13 : move16();
441 286 : FOR( i = 1; i < num_sfm; i++ )
442 : {
443 273 : difidx_org[i] = difidx[i]; /* Q0 */
444 273 : move16();
445 : }
446 :
447 273 : FOR( i = 2; i < num_sfm; i++ )
448 : {
449 260 : IF( GT_16( difidx_org[i - 1], 17 ) )
450 : {
451 50 : difidx[i] = add( difidx_org[i], s_min( sub( difidx_org[i - 1], 17 ), 3 ) ); /* Q0 */
452 50 : move16();
453 50 : IF( GT_16( difidx[i], 31 ) )
454 : {
455 0 : difidx_flag = 1;
456 0 : move16();
457 0 : BREAK;
458 : }
459 : }
460 :
461 260 : IF( LT_16( difidx_org[i - 1], 13 ) )
462 : {
463 12 : difidx[i] = add( difidx_org[i], s_max( sub( difidx_org[i - 1], 13 ), -3 ) ); /* Q0 */
464 12 : move16();
465 12 : IF( difidx[i] < 0 )
466 : {
467 0 : difidx_flag = 1;
468 0 : move16();
469 0 : BREAK;
470 : }
471 : }
472 : }
473 :
474 13 : IF( difidx_flag == 0 )
475 : {
476 286 : FOR( i = 1; i < num_sfm; i++ )
477 : {
478 273 : index_max = s_max( index_max, difidx[i] );
479 273 : index_min = s_min( index_min, difidx[i] );
480 : }
481 :
482 13 : index_rad = s_max( sub( 15, index_min ), sub( index_max, 15 ) );
483 :
484 13 : IF( LE_16( index_rad, HUFF_THR ) )
485 : {
486 286 : FOR( i = 1; i < num_sfm; i++ )
487 : {
488 273 : j = difidx[i]; /* Q0 */
489 273 : move16();
490 : }
491 : }
492 : }
493 : }
494 :
495 : /* LC mode 2 -> LC mode 1 */
496 2021 : FOR( i = 1; i < num_sfm; i++ )
497 : {
498 1963 : j = difidx[i]; /* Q0 */
499 1963 : move16();
500 :
501 1963 : m = resize_huffnorm[j]; /* Q0 */
502 1963 : move16();
503 1963 : r = resize_huffsizn[j]; /* Q0 */
504 1963 : move16();
505 1963 : v = 0;
506 1963 : move16();
507 :
508 : /* Bit reverse */
509 8432 : FOR( k = 0; k < r; k++ )
510 : {
511 6469 : v = lshl( v, 1 ); /* Q0 */
512 6469 : v = s_or( v, s_and( m, 1 ) ); /* Q0 */
513 6469 : m = lshr( m, 1 ); /* Q0 */
514 : }
515 :
516 1963 : IF( flag_HQ2 == 0 )
517 : {
518 1690 : push_indice( hBstr, IND_YNRM, v, r );
519 : }
520 : ELSE
521 : {
522 273 : push_indice( hBstr, IND_HQ2_DIFF_ENERGY, v, r );
523 : }
524 : }
525 : }
526 19 : ELSE IF( EQ_16( *LCmode, 2 ) )
527 : {
528 : /* LC mode 1 -> LC mode 2 */
529 713 : FOR( i = 1; i < num_sfm; i++ )
530 : {
531 694 : j = difidx[i]; /* Q0 */
532 694 : move16();
533 :
534 694 : m = huffnorm[j]; /* Q0 */
535 694 : move16();
536 694 : r = huffsizn[j]; /* Q0 */
537 694 : move16();
538 :
539 694 : 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 848 : return hcode_l;
553 : }
554 :
555 15544 : 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 15544 : set16_fx( difidx_org, 0, NB_SFM );
578 15544 : difidx_flag = 0;
579 15544 : 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 15544 : IF( flag_pack == 0 )
591 : {
592 7772 : test();
593 7772 : 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 7772 : hcode_l = 0;
669 7772 : move16();
670 7772 : prevj = add( difidx[0], OFFSET_NORM );
671 224849 : FOR( i = 1; i < num_sfm; i++ )
672 : {
673 217077 : j = difidx[i]; /* Q0 */
674 217077 : move16();
675 217077 : IF( GT_16( prevj, HTH_NORM ) )
676 : {
677 : /* above */
678 47951 : hcode_l = add( hcode_l, huffsizn_n[( 31 - j )] ); /* Q0 */
679 : }
680 : ELSE
681 : {
682 169126 : IF( LT_16( prevj, LTH_NORM ) )
683 : {
684 : /* less */
685 29984 : hcode_l = add( hcode_l, huffsizn_n[j] ); /* Q0 */
686 : }
687 : ELSE
688 : {
689 : /* equal */
690 139142 : hcode_l = add( hcode_l, huffsizn_e[j] ); /* Q0 */
691 : }
692 : }
693 217077 : prevj = j;
694 217077 : move16();
695 : }
696 :
697 7772 : *LCmode = 0;
698 7772 : move16();
699 :
700 : /* LR-MDCT core doesn't have coding mode 2 and 3 */
701 7772 : IF( flag_HQ2 == NORMAL_HQ_CORE )
702 : {
703 : /* Check bits if LC mode == 1 -> Check bits if LC mode == 2 */
704 7772 : bits = 0;
705 7772 : move16();
706 224849 : FOR( i = 1; i < num_sfm; i++ )
707 : {
708 217077 : j = difidx[i]; /* Q0 */
709 217077 : move16();
710 217077 : 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 7772 : IF( GT_16( hcode_l, bits ) )
718 : {
719 1029 : *LCmode = 2;
720 1029 : move16();
721 : }
722 7772 : hcode_l = s_min( hcode_l, bits );
723 : }
724 :
725 : /* Check bits if LC mode == 2 -> Check bits if LC mode == 1 */
726 7772 : bits = 0;
727 7772 : move16();
728 7772 : index_max = 0;
729 7772 : move16();
730 7772 : index_min = 31;
731 7772 : move16();
732 224849 : FOR( i = 1; i < num_sfm; i++ )
733 : {
734 217077 : difidx_org[i] = difidx[i]; /* Q0 */
735 217077 : move16();
736 : }
737 :
738 7772 : difidx_flag = 0;
739 7772 : move16();
740 217077 : FOR( i = 2; i < num_sfm; i++ )
741 : {
742 209305 : IF( GT_16( difidx_org[i - 1], 17 ) )
743 : {
744 42325 : difidx[i] = add( difidx_org[i], s_min( sub( difidx_org[i - 1], 17 ), 3 ) ); /* Q0 */
745 42325 : move16();
746 42325 : IF( GT_16( difidx[i], 31 ) )
747 : {
748 0 : difidx_flag = 1;
749 0 : move16();
750 0 : BREAK;
751 : }
752 : }
753 :
754 209305 : IF( LT_16( difidx_org[i - 1], 13 ) )
755 : {
756 29917 : difidx[i] = add( difidx_org[i], s_max( sub( difidx_org[i - 1], 13 ), -3 ) ); /* Q0 */
757 29917 : move16();
758 29917 : IF( difidx[i] < 0 )
759 : {
760 0 : difidx_flag = 1;
761 0 : move16();
762 0 : BREAK;
763 : }
764 : }
765 : }
766 :
767 7772 : index_rad = 0;
768 7772 : move16();
769 7772 : IF( difidx_flag == 0 )
770 : {
771 224849 : FOR( i = 1; i < num_sfm; i++ )
772 : {
773 217077 : index_max = s_max( index_max, difidx[i] );
774 217077 : index_min = s_min( index_min, difidx[i] );
775 : }
776 :
777 7772 : index_rad = s_max( sub( 15, index_min ), sub( index_max, 15 ) );
778 :
779 7772 : IF( LE_16( index_rad, HUFF_THR ) )
780 : {
781 215096 : FOR( i = 1; i < num_sfm; i++ )
782 : {
783 207648 : j = difidx[i]; /* Q0 */
784 207648 : move16();
785 207648 : 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 7448 : IF( GT_16( hcode_l, bits ) )
793 : {
794 2312 : *LCmode = 1;
795 2312 : move16();
796 : }
797 7448 : hcode_l = s_min( hcode_l, bits );
798 : }
799 : }
800 :
801 : /* LR-MDCT core doesn't have coding mode 2 and 3 */
802 7772 : 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 7772 : IF( GE_16( hcode_l, numnrmibits ) )
809 : {
810 123 : *LCmode = 3;
811 123 : move16();
812 : }
813 7772 : hcode_l = s_min( hcode_l, numnrmibits );
814 : }
815 :
816 7772 : test();
817 7772 : test();
818 7772 : IF( ( NE_16( *LCmode, 1 ) && flag_HQ2 == NORMAL_HQ_CORE ) || EQ_16( flag_HQ2, LOW_RATE_HQ_CORE ) )
819 : {
820 151579 : FOR( i = 2; i < num_sfm; i++ )
821 : {
822 146119 : difidx[i] = difidx_org[i]; /* Q0 */
823 146119 : move16();
824 : }
825 : }
826 : }
827 : }
828 : ELSE
829 : {
830 7772 : test();
831 7772 : 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 7772 : push_indice( hBstr, IND_LC_MODE, *LCmode, 2 );
839 7772 : push_indice( hBstr, IND_YNRM, difidx[0], NORM0_BITS );
840 : }
841 :
842 7772 : test();
843 7772 : 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 7772 : hcode_l = 0;
917 7772 : move16();
918 7772 : IF( *LCmode == 0 )
919 : {
920 : /* LC mode 3 -> LC mode 0 */
921 4421 : prevj = add( difidx[0], OFFSET_NORM );
922 129051 : FOR( i = 1; i < num_sfm; i++ )
923 : {
924 124630 : j = difidx[i]; /* Q0 */
925 124630 : move16();
926 :
927 124630 : IF( GT_16( prevj, HTH_NORM ) )
928 : {
929 : /* above */
930 26960 : r = huffsizn_n[( 31 - j )]; /* Q0 */
931 26960 : move16();
932 26960 : m = huffnorm_n[( 31 - j )]; /* Q0 */
933 26960 : move16();
934 : }
935 : ELSE
936 : {
937 97670 : IF( LT_16( prevj, LTH_NORM ) )
938 : {
939 : /* less */
940 16657 : r = huffsizn_n[j]; /* Q0 */
941 16657 : move16();
942 16657 : m = huffnorm_n[j]; /* Q0 */
943 16657 : move16();
944 : }
945 : ELSE
946 : {
947 : /* equal */
948 81013 : r = huffsizn_e[j]; /* Q0 */
949 81013 : move16();
950 81013 : m = huffnorm_e[j]; /* Q0 */
951 81013 : move16();
952 : }
953 : }
954 :
955 124630 : 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 124630 : push_indice( hBstr, IND_YNRM, m, r );
962 : }
963 :
964 124630 : prevj = j;
965 124630 : move16();
966 : }
967 : }
968 3351 : ELSE IF( EQ_16( *LCmode, 1 ) )
969 : {
970 2312 : 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 67810 : FOR( i = 1; i < num_sfm; i++ )
1032 : {
1033 65498 : j = difidx[i]; /* Q0 */
1034 65498 : move16();
1035 :
1036 65498 : m = resize_huffnorm[j]; /* Q0 */
1037 65498 : move16();
1038 65498 : r = resize_huffsizn[j]; /* Q0 */
1039 65498 : move16();
1040 65498 : v = 0;
1041 65498 : move16();
1042 :
1043 : /* Bit reverse */
1044 277913 : FOR( k = 0; k < r; k++ )
1045 : {
1046 212415 : v = lshl( v, 1 ); /* Q0 */
1047 212415 : v = s_or( v, s_and( m, 1 ) ); /* Q0 */
1048 212415 : m = lshr( m, 1 ); /* Q0 */
1049 : }
1050 :
1051 65498 : IF( flag_HQ2 == 0 )
1052 : {
1053 65498 : 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 25175 : FOR( i = 1; i < num_sfm; i++ )
1065 : {
1066 24259 : j = difidx[i]; /* Q0 */
1067 24259 : move16();
1068 :
1069 24259 : m = huffnorm[j]; /* Q0 */
1070 24259 : move16();
1071 24259 : r = huffsizn[j]; /* Q0 */
1072 24259 : move16();
1073 :
1074 24259 : 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 15544 : return hcode_l;
1088 : }
1089 :
1090 : /*--------------------------------------------------------------------------*
1091 : * diff_envelope_coding_fx()
1092 : *
1093 : * Differential envelope coding
1094 : *--------------------------------------------------------------------------*/
1095 :
1096 8160 : 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 8160 : 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 7667 : diffcod_fx( num_env_bands, &ynrm[start_norm], &difidx[1] );
1122 7667 : difidx[0] = ynrm[start_norm]; /* Q0 */
1123 7667 : move16();
1124 :
1125 7667 : tmp = add( start_norm, num_env_bands );
1126 226051 : FOR( i = start_norm; i < tmp; i++ )
1127 : {
1128 218384 : normqlg2[i] = dicnlg2[ynrm[i]]; /* Q0 */
1129 218384 : move16();
1130 218384 : move16();
1131 : }
1132 : }
1133 :
1134 8160 : return;
1135 : }
|