Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 : #include <stdint.h>
5 : #include "options.h" /* Compilation switches */
6 : #include "rom_com.h"
7 :
8 : #include "prot_fx.h" /* Function prototypes */
9 : #include "cnst.h" /* Common constants */
10 :
11 : /*--------------------------------------------------------------------------*/
12 : /* Function hvq_pvq_bitalloc */
13 : /* ~~~~~~~~~~~~~~~~~~~~~~~~ */
14 : /* */
15 : /* Calculate the number of PVQ bands to code and allocate bits based on */
16 : /* the number of available bits. */
17 : /*--------------------------------------------------------------------------*/
18 2695 : Word16 hvq_pvq_bitalloc_fx(
19 : Word16 num_bits, /* i/o: Number of available bits (including gain bits) */
20 : const Word32 core_brate, /* i : core bitrate */
21 : const Word16 bwidth_fx, /* i : Encoded bandwidth */
22 : const Word16 *ynrm, /* i : Envelope coefficients */
23 : const Word32 manE_peak, /* i : Peak energy mantissa */
24 : const Word16 expE_peak, /* i : Peak energy exponent */
25 : Word16 *Rk, /* Q3 o : bit allocation for concatenated vector */
26 : Word16 *R, /* Q0 i/o: Global bit allocation */
27 : Word16 *sel_bands, /* Q0 o : Selected bands for encoding */
28 : Word16 *n_sel_bands /* Q0 o : No. of selected bands for encoding */
29 : )
30 : {
31 : Word16 num_bands, band_max_bits;
32 : Word16 one_over_band_max_bits;
33 : Word16 k;
34 : Word16 reciprocal, envSum, expo, align, m, n, indx;
35 : Word16 k_max;
36 : Word16 k_start;
37 : Word32 E_max, E_max5;
38 : Word32 tmp, acc;
39 : Word32 env_mean;
40 : UWord16 lsb;
41 : Word16 num_sfm;
42 :
43 2695 : IF( EQ_16( bwidth_fx, FB ) )
44 : {
45 2282 : num_sfm = SFM_N_HARM_FB;
46 : }
47 : ELSE
48 : {
49 413 : num_sfm = SFM_N_HARM;
50 : }
51 2695 : move16();
52 :
53 2695 : IF( LT_32( core_brate, HQ_BWE_CROSSOVER_BRATE ) )
54 : {
55 1561 : band_max_bits = HVQ_BAND_MAX_BITS_24k; /*Q0*/
56 1561 : move16();
57 1561 : one_over_band_max_bits = ONE_OVER_HVQ_BAND_MAX_BITS_24k_FX; /*Q15*/
58 1561 : move16();
59 1561 : k_start = HVQ_THRES_SFM_24k; /*Q0*/
60 1561 : move16();
61 1561 : IF( EQ_16( bwidth_fx, FB ) )
62 : {
63 1162 : reciprocal = 2731; /* Q15, 1/(SFM_N_HARM_FB + 1 - k_start) */
64 1162 : move16();
65 : }
66 : ELSE
67 : {
68 399 : reciprocal = 3277; /* Q15, 1/(SFM_N_HARM + 1 - k_start) */
69 399 : move16();
70 : }
71 : }
72 : ELSE
73 : {
74 1134 : band_max_bits = HVQ_BAND_MAX_BITS_32k; /*Q0*/
75 1134 : move16();
76 1134 : one_over_band_max_bits = ONE_OVER_HVQ_BAND_MAX_BITS_32k_FX; /*Q15*/
77 1134 : move16();
78 1134 : k_start = HVQ_THRES_SFM_32k; /*Q0*/
79 1134 : move16();
80 1134 : IF( EQ_16( bwidth_fx, FB ) )
81 : {
82 1120 : reciprocal = 3641; /* Q15, 1/(SFM_N_HARM_FB + 1 - k_start) */
83 1120 : move16();
84 : }
85 : ELSE
86 : {
87 14 : reciprocal = 4681; /* Q15, 1/(SFM_N_HARM + 1 - k_start) */
88 14 : move16();
89 : }
90 : }
91 :
92 2695 : num_bands = mult( num_bits, one_over_band_max_bits ); /* Q0 */
93 2695 : num_bits = sub( num_bits, i_mult( num_bands, band_max_bits ) ); /* Q0 */
94 :
95 2695 : IF( GE_16( num_bits, HVQ_NEW_BAND_BIT_THR ) )
96 : {
97 1466 : num_bands = add( num_bands, 1 );
98 : }
99 : ELSE
100 : {
101 1229 : num_bits = add( num_bits, band_max_bits );
102 : }
103 :
104 : /* safety check in case of bit errors */
105 2695 : IF( LT_16( num_bands, 1 ) )
106 : {
107 14 : return 0;
108 : }
109 :
110 2681 : *n_sel_bands = 0;
111 2681 : move16();
112 2681 : envSum = 0;
113 2681 : move16();
114 2681 : E_max = L_deposit_l( 0 );
115 2681 : k_max = k_start;
116 2681 : move16();
117 27944 : FOR( k = k_start; k < num_sfm; k++ )
118 : {
119 25263 : indx = ynrm[k];
120 25263 : move16();
121 25263 : tmp = dicn_fx[indx]; /* Q14 */
122 25263 : move32();
123 25263 : envSum = add( envSum, indx ); /* Since the size of dicn_fx = 40, ynrm[k] must be less than 41. 16 bits are enough for envSum.*/
124 25263 : IF( GT_32( tmp, E_max ) )
125 : {
126 5158 : E_max = tmp;
127 5158 : move32();
128 5158 : k_max = k;
129 5158 : move16();
130 : }
131 : }
132 2681 : env_mean = L_mult( envSum, reciprocal ); /* env_mean in Q16 */
133 2681 : IF( GT_32( L_sub( env_mean, L_deposit_h( ynrm[k_max] ) ), 0x30000L ) ) /* condition: env_mean - ynrm[k_max] > 3 */
134 : {
135 1644 : expo = norm_l( E_max );
136 1644 : E_max = L_shl( E_max, expo );
137 1644 : Mpy_32_16_ss( E_max, 0x7a12, &E_max5, &lsb ); /* NB: 5.0e5 = 0x7a12(Q15) x 2^19. */
138 : /* True floating point value of E_max*5e5 = E_max5 x 2^(19 - expo - 14).
139 : * In this context, the 32-bit E_max5 is in Q0, and
140 : * -14 is due to Emax in Q14.
141 : * True floating point value of E_peak = manE_peak x 2^(31 - expE_peak - 2*12). See peak_vq_enc_fx().
142 : */
143 :
144 : /* Align the Q-points of the floating point Emax*5e5 and E_peak. */
145 1644 : align = sub( expo, expE_peak );
146 1644 : align = add( align, ( 19 - 14 ) - ( 31 - 2 * 12 ) );
147 1644 : IF( align < 0 )
148 : {
149 1644 : acc = L_sub( E_max5, L_shl( manE_peak, align ) );
150 : }
151 : ELSE
152 : {
153 0 : acc = L_sub( L_shr( E_max5, align ), manE_peak );
154 : }
155 :
156 1644 : IF( acc > 0 ) /* condition: E_max*5.e5 > E_peak */
157 : {
158 1644 : IF( EQ_16( band_len_harm[k_max], 96 ) )
159 : {
160 0 : n = 61;
161 0 : move16();
162 : }
163 : ELSE
164 : {
165 1644 : QuantaPerDsDirac_fx( band_len_harm[k_max], 1, hBitsN, &n );
166 : }
167 1644 : m = shl( sub( num_bits, HVQ_PVQ_GAIN_BITS ), 3 ); /*Q3*/
168 1644 : IF( GE_16( m, n ) )
169 : {
170 1644 : IF( GT_16( num_bands, 1 ) ) /* condition: num_bands > 1 */
171 : {
172 694 : sel_bands[*n_sel_bands] = k_max; /*Q0*/
173 694 : move16();
174 694 : *n_sel_bands = add( *n_sel_bands, 1 ); /*Q0*/
175 694 : move16();
176 694 : R[k_max] = 1; /* Mark that the band has been encoded for fill_spectrum */
177 694 : move16();
178 : }
179 : }
180 : }
181 : }
182 :
183 : /* Allocate bits */
184 2681 : tmp = sub( num_bands, 1 );
185 5283 : FOR( k = 0; k < tmp; k++ )
186 : {
187 2602 : Rk[k] = shl( sub( band_max_bits, HVQ_PVQ_GAIN_BITS ), 3 ); /*Q3*/
188 2602 : move16();
189 : }
190 : /* NB: When it exits the above loop, k = num_bands - 1. */
191 2681 : Rk[k] = shl( sub( num_bits, HVQ_PVQ_GAIN_BITS ), 3 ); /*Q3*/
192 2681 : move16();
193 :
194 2681 : return num_bands;
195 : }
|