Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <stdint.h>
6 : #include "options.h"
7 : #include "cnst.h"
8 : #include "options.h"
9 : //#include "prot_fx.h"
10 : #include "prot_fx.h" /* Function prototypes */
11 : #include "prot_fx_enc.h" /* Function prototypes */
12 : #include "basop_util.h"
13 :
14 : /*------------------------------------------------------------------*
15 : * vlpc_2st_cod_fx()
16 : *
17 : *
18 : *------------------------------------------------------------------*/
19 :
20 111007 : Word16 vlpc_2st_cod_fx( /* output: number of allocated bits */
21 : const Word16 *lsf, /* i : normalized vector to quantize (14Q1*1.28) */
22 : Word16 *lsfq, /* i/o: i:1st stage o:1st+2nd stage (14Q1*1.28) */
23 : Word16 *indx, /* o : index[] (4 bits per words) Q0 */
24 : const Word16 mode, /* i : 0=abs, >0=rel */
25 : const Word32 sr_core /* i : internal sampling rate */
26 : )
27 : {
28 : Word16 i, nbits;
29 : Word16 w[M], x[M];
30 : Word16 nq, xq[M];
31 : Word32 L_tmp;
32 : Word16 gap;
33 :
34 : /* 0 bit with true weighting: save 0.5 bit */
35 111007 : lsf_weight_2st( lsf, w, 1 ); /*w:0Q15*1.28*/
36 :
37 1776112 : FOR( i = 1; i < M; i++ )
38 : {
39 1665105 : x[i] = divide1616( shr( sub( lsf[i], lsfq[i] ), 2 ), w[i] ); /*5Q10*/
40 1665105 : move16();
41 : }
42 111007 : i = shr( sub( lsf[0], lsfq[0] ), 2 ); /*5Q10*/
43 111007 : IF( w[0] != 0 )
44 : {
45 111007 : i = shl( mult_r( i, 20972 ), 1 ); /*5Q10*1.28*/
46 111007 : i = divide1616( i, w[0] ); /*5Q10*/
47 : }
48 111007 : x[0] = i; /*5Q10*/
49 111007 : move16();
50 :
51 111007 : L_tmp = L_mult( x[0], x[0] ); /*10Q21*/
52 : BASOP_SATURATE_WARNING_OFF_EVS /* Allow saturate because we only need to know if the result is smaller than 8.0f */
53 1776112 : FOR( i = 1; i < M; i++ )
54 : {
55 1665105 : L_tmp = L_mac_sat( L_tmp, x[i], x[i] ); /*10Q21*/
56 : }
57 : BASOP_SATURATE_WARNING_ON_EVS
58 :
59 111007 : IF( LT_32( L_tmp, 16777216l /*8.0f Q21*/ ) ) /*tmp40 < 8.0f */
60 : {
61 14 : indx[0] = 0;
62 14 : move16();
63 14 : indx[1] = 0;
64 14 : move16();
65 :
66 : /* the following assignments are excluded from the complexity count
67 : due to temporaray change of the type of the return value
68 : (nbits -> wops) */
69 14 : nbits = 6; /* 2*(2+1) */
70 14 : move16();
71 14 : test();
72 14 : IF( ( mode == 0 ) || ( EQ_16( mode, 3 ) ) )
73 : {
74 14 : nbits = 10; /* 2*(2+3) */
75 14 : move16();
76 : }
77 0 : ELSE IF( EQ_16( mode, 1 ) )
78 : {
79 0 : nbits = 2; /* 2*1 */
80 0 : move16();
81 : }
82 :
83 14 : return nbits;
84 : }
85 :
86 : /* weighting from the 1st stage */
87 110993 : lsf_weight_2st( lsfq, w, mode );
88 :
89 : /* find lsf and scale the residual */
90 1886881 : FOR( i = 0; i < M; i++ )
91 : {
92 : /* limit to range [-32767+1024+2048;32767-1024-2048] to guarantee enough headroom in quantizer */
93 1775888 : x[i] = s_max( s_min( divide1616( shr( sub( lsf[i], lsfq[i] ), 2 ), w[i] ), 32767 - 1024 - 2048 ), -32767 + 1024 + 2048 ); /*5Q10*/
94 1775888 : move16();
95 : }
96 :
97 : /* quantize */
98 110993 : AVQ_cod_lpc_fx( x, xq, indx, 2 );
99 :
100 : /* quantized lsf */
101 1886881 : FOR( i = 0; i < M; i++ )
102 : {
103 : /*lsfq[i] += (w[i]*(float)xq[i]);*/
104 1775888 : lsfq[i] = add( lsfq[i], shl( mult_r( w[i], xq[i] ), 2 ) ); /*14Q1*1.28*/
105 1775888 : move16();
106 : }
107 :
108 : /* total number of bits using entropic code to index the quantizer number */
109 110993 : nbits = 0;
110 110993 : move16();
111 332979 : FOR( i = 0; i < 2; i++ )
112 : {
113 221986 : nq = indx[i];
114 221986 : move16();
115 :
116 : /*nbits += (2+(nq*4));*/
117 221986 : nbits = add( nbits, add( 2, shl( nq, 2 ) ) ); /* 2 bits to specify Q2,Q3,Q4,ext; nbits += (2+(nq*4)); */
118 221986 : IF( GT_16( nq, 6 ) )
119 : {
120 : /*nbits += nq-3;*/
121 73762 : nbits = add( nbits, sub( nq, 3 ) ); /* unary code (Q7=1110, ...) */
122 : }
123 148224 : ELSE IF( GT_16( nq, 4 ) )
124 : {
125 : /*nbits += nq-4;*/
126 96600 : nbits = add( nbits, sub( nq, 4 ) ); /* Q5=0, Q6=10 */
127 : }
128 51624 : ELSE IF( nq == 0 )
129 : {
130 : /*nbits += 3;*/
131 1892 : nbits = add( nbits, 3 ); /* Q0=110 */
132 : }
133 : } /*FOR (i=0; i<2; i++)*/
134 :
135 : /* reorder */
136 110993 : sort_fx( lsfq, 0, M - 1 );
137 110993 : IF( EQ_32( sr_core, 16000 ) )
138 : {
139 528 : gap = 102;
140 : }
141 110465 : ELSE IF( EQ_32( sr_core, 25600 ) )
142 : {
143 7286 : gap = 64;
144 : }
145 103179 : ELSE IF( EQ_32( sr_core, 32000 ) )
146 : {
147 103179 : gap = 51;
148 : }
149 : ELSE
150 : {
151 0 : gap = 34;
152 : }
153 110993 : move16();
154 110993 : reorder_lsf_fx( lsfq, gap, M, INT_FS_FX );
155 :
156 :
157 110993 : return nbits;
158 : }
|