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" /* Compilation switches */
7 : #include "rom_com.h" /* Static table prototypes */
8 : #include "prot_fx.h" /* Function prototypes */
9 : #include "ivas_prot_fx.h"
10 :
11 :
12 : /*--------------------------------------------------------------------------
13 : * subband_gain_bits()
14 : *
15 : * HQ core encoder
16 : *--------------------------------------------------------------------------*/
17 767 : static void subband_gain_bits_fx(
18 : const Word16 *Rk, /* i : bit allocation per band Q3 */
19 : const Word16 N, /* i : number of bands */
20 : Word16 *bits, /* o : gain bits per band */
21 : const Word16 *sfmsize /* i : Size of bands */
22 : )
23 : {
24 : Word16 i, b, tot;
25 : Word16 bps;
26 :
27 767 : tot = 0;
28 767 : move16();
29 :
30 30424 : FOR( i = 0; i < N; i++ )
31 : {
32 : /*bps = (short)(Rk[i]*((word16)min(32767, ceil(32767.0f/sfmsize[i]); inexact C-integer division approx. */
33 29657 : bps = extract_l( L_shr( L_mult0( Rk[i], inv_tbl_fx[sfmsize[i]] ), 18 ) ); /* 3+15 */
34 29657 : if ( EQ_32( L_shl( L_mult0( sfmsize[i], add( bps, 1 ) ), 3 ), Rk[i] ) )
35 : {
36 7091 : bps = add( bps, 1 );
37 : }
38 :
39 29657 : bps = s_min( 7, bps );
40 29657 : b = fine_gain_bits[bps];
41 29657 : move16();
42 29657 : bits[i] = b;
43 29657 : move16();
44 29657 : tot = add( tot, b );
45 : }
46 :
47 767 : if ( tot == 0 )
48 : {
49 : /* If no gain bits were assigned, use one bit anyway for potential PVQ overage */
50 18 : bits[0] = 1;
51 18 : move16();
52 : }
53 :
54 767 : return;
55 : }
56 :
57 : /*--------------------------------------------------------------------------*
58 : * assign_gain_bits()
59 : *
60 : * Assign gain adjustment bits and update bit budget
61 : *--------------------------------------------------------------------------*/
62 :
63 21334 : Word16 assign_gain_bits_fx( /* o : Number of assigned gain bits */
64 : const Word16 core, /* i : HQ core */
65 : const Word16 BANDS, /* i : Number of bands */
66 : const Word16 *band_width, /* i : Sub band bandwidth */
67 : Word16 *Rk, /* i/o: Bit allocation/Adjusted bit alloc. Q3 */
68 : Word16 *gain_bits_array, /* o : Assigned gain bits */
69 : Word16 *Rcalc /* o : Bit budget for shape quantizer Q3 */
70 : )
71 : {
72 : Word16 gain_bits_tot;
73 : Word16 i;
74 :
75 : /* Allocate gain bits for every subband used, based on bit rate and bandwidth */
76 21334 : IF( EQ_16( core, HQ_CORE ) )
77 : {
78 767 : subband_gain_bits_fx( Rk, BANDS, gain_bits_array, band_width );
79 : }
80 : ELSE
81 : {
82 20567 : set16_fx( gain_bits_array, 0, BANDS );
83 : }
84 :
85 : /* Re-adjust bit budget for gain quantization */
86 21334 : gain_bits_tot = 0;
87 21334 : move16();
88 21334 : *Rcalc = 0;
89 21334 : move16();
90 197799 : FOR( i = 0; i < BANDS; i++ )
91 : {
92 176465 : IF( Rk[i] > 0 )
93 : {
94 153268 : Rk[i] = sub( Rk[i], shl( gain_bits_array[i], 3 ) );
95 153268 : move16();
96 153268 : gain_bits_tot = add( gain_bits_tot, gain_bits_array[i] );
97 153268 : *Rcalc = add( *Rcalc, Rk[i] );
98 153268 : move16();
99 : }
100 : }
101 :
102 21334 : return gain_bits_tot;
103 : }
104 :
105 12442 : static void ivas_subband_gain_bits_fx(
106 : const Word16 *Rk, /* i : bit allocation per band Q3 */
107 : const Word16 N, /* i : number of bands */
108 : Word16 *bits, /* o : gain bits per band */
109 : const Word16 *sfmsize /* i : Size of bands */
110 : )
111 : {
112 : Word16 i, b, tot;
113 : Word16 bps;
114 :
115 12442 : tot = 0;
116 12442 : move16();
117 :
118 514234 : FOR( i = 0; i < N; i++ )
119 : {
120 : /*bps = (short)(Rk[i]*((word16)min(32767, ceil(32767.0f/sfmsize[i]); inexact C-integer division approx. */
121 501792 : bps = extract_l( L_shr( L_mult0( Rk[i], fg_inv_tbl_fx[sfmsize[i] >> 3] ), 18 ) ); /* 3+15 */
122 501792 : if ( EQ_32( L_shl( L_mult0( sfmsize[i], add( bps, 1 ) ), 3 ), Rk[i] ) )
123 : {
124 0 : bps = add( bps, 1 );
125 : }
126 :
127 501792 : bps = s_min( 7, bps );
128 501792 : b = fine_gain_bits[bps];
129 501792 : move16();
130 501792 : bits[i] = b;
131 501792 : move16();
132 501792 : tot = add( tot, b );
133 : }
134 :
135 12442 : if ( tot == 0 )
136 : {
137 : /* If no gain bits were assigned, use one bit anyway for potential PVQ overage */
138 2499 : bits[0] = 1;
139 2499 : move16();
140 : }
141 :
142 12442 : return;
143 : }
144 :
145 35149 : Word16 ivas_assign_gain_bits_fx( /* o : Number of assigned gain bits */
146 : const Word16 core, /* i : HQ core */
147 : const Word16 BANDS, /* i : Number of bands */
148 : const Word16 *band_width, /* i : Sub band bandwidth */
149 : Word16 *Rk, /* i/o: Bit allocation/Adjusted bit alloc. Q3 */
150 : Word16 *gain_bits_array, /* o : Assigned gain bits */
151 : Word16 *Rcalc /* o : Bit budget for shape quantizer Q3 */
152 : )
153 : {
154 : Word16 gain_bits_tot;
155 : Word16 i;
156 :
157 : /* Allocate gain bits for every subband used, based on bit rate and bandwidth */
158 35149 : IF( EQ_16( core, HQ_CORE ) )
159 : {
160 12442 : ivas_subband_gain_bits_fx( Rk, BANDS, gain_bits_array, band_width );
161 : }
162 : ELSE
163 : {
164 22707 : set16_fx( gain_bits_array, 0, BANDS );
165 : }
166 :
167 : /* Re-adjust bit budget for gain quantization */
168 35149 : gain_bits_tot = 0;
169 35149 : move16();
170 35149 : *Rcalc = 0;
171 35149 : move16();
172 699803 : FOR( i = 0; i < BANDS; i++ )
173 : {
174 664654 : IF( Rk[i] > 0 )
175 : {
176 403564 : Rk[i] = sub( Rk[i], shl( gain_bits_array[i], 3 ) );
177 403564 : move16();
178 403564 : gain_bits_tot = add( gain_bits_tot, gain_bits_array[i] );
179 403564 : *Rcalc = add( *Rcalc, Rk[i] );
180 403564 : move16();
181 : }
182 : }
183 :
184 35149 : return gain_bits_tot;
185 : }
|