Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <stdint.h>
38 : #include "options.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include <math.h>
43 : #include "cnst.h"
44 : #include "prot.h"
45 : #include "rom_com.h"
46 : #include "wmc_auto.h"
47 :
48 : /*-----------------------------------------------------------------*
49 : * transf_cdbk_dec()
50 : *
51 : * Transform domain contribution decoding
52 : *-----------------------------------------------------------------*/
53 :
54 218955 : void transf_cdbk_dec(
55 : Decoder_State *st, /* i/o: decoder state structure */
56 : const int16_t harm_flag_acelp, /* i : harmonic flag for higher rates ACELP */
57 : const int16_t i_subfr, /* i : subframe index */
58 : const float Es_pred, /* i : predicited scaled innovation energy */
59 : const float gain_code, /* i : innovative excitation gain */
60 : float *gain_preQ, /* o : prequantizer excitation gain */
61 : float *norm_gain_preQ, /* o : normalized prequantizer excitation gain */
62 : float code_preQ[], /* o : prequantizer excitation */
63 : int16_t *unbits /* o : number of AVQ unused bits */
64 : )
65 : {
66 : int16_t i, index, nBits, Nsv;
67 : int16_t x_norm[L_SUBFR];
68 : float Ecode;
69 : int16_t nq[L_SUBFR / WIDTH_BAND];
70 : int16_t avq_bit_sFlag;
71 : int16_t trgtSvPos;
72 :
73 218955 : avq_bit_sFlag = 0;
74 218955 : if ( st->element_mode > EVS_MONO )
75 : {
76 208920 : avq_bit_sFlag = 1;
77 : }
78 :
79 : /*--------------------------------------------------------------*
80 : * Set bit-allocation
81 : *--------------------------------------------------------------*/
82 :
83 218955 : Nsv = 8;
84 218955 : nBits = st->acelp_cfg.AVQ_cdk_bits[i_subfr / L_SUBFR];
85 :
86 : /* increase # of AVQ allocated bits by unused bits from the previous subframe */
87 218955 : nBits += ( *unbits );
88 :
89 : /*--------------------------------------------------------------*
90 : * Dequantize prequantizer excitation gain
91 : *--------------------------------------------------------------*/
92 :
93 218955 : index = get_next_indice( st, G_AVQ_BITS );
94 :
95 218955 : if ( st->coder_type == INACTIVE )
96 : {
97 38610 : if ( st->core_brate > 56000 )
98 : {
99 0 : *gain_preQ = usdequant( index, G_AVQ_MIN_INACT_64k, G_AVQ_DELTA_INACT_64k );
100 : }
101 38610 : else if ( st->core_brate > 42000 )
102 : {
103 4635 : *gain_preQ = usdequant( index, G_AVQ_MIN_INACT_48k, G_AVQ_DELTA_INACT_48k );
104 : }
105 : else
106 : {
107 33975 : *gain_preQ = usdequant( index, G_AVQ_MIN_INACT, G_AVQ_DELTA_INACT );
108 : }
109 :
110 38610 : *gain_preQ *= gain_code;
111 : }
112 : else
113 : {
114 180345 : if ( st->core_brate > ACELP_24k40 && st->core_brate <= 42000 )
115 : {
116 153630 : *gain_preQ = gain_dequant( index, 0.1f * G_AVQ_MIN, G_AVQ_MAX, G_AVQ_BITS );
117 : }
118 : else
119 : {
120 26715 : *gain_preQ = gain_dequant( index, G_AVQ_MIN, G_AVQ_MAX, G_AVQ_BITS );
121 : }
122 180345 : if ( Es_pred < 0 )
123 : {
124 1080 : *gain_preQ *= (float) ( 0.25f * fabs( Es_pred ) );
125 : }
126 : else
127 : {
128 179265 : *gain_preQ *= Es_pred;
129 : }
130 : }
131 :
132 218955 : trgtSvPos = Nsv - 1;
133 218955 : if ( avq_bit_sFlag && nBits > 85 && !harm_flag_acelp && ( st->coder_type == GENERIC || st->coder_type == TRANSITION || st->coder_type == INACTIVE ) )
134 : {
135 59265 : trgtSvPos = 2;
136 59265 : avq_bit_sFlag = 2;
137 : }
138 :
139 : /*--------------------------------------------------------------*
140 : * Encode and multiplex subvectors into bitstream
141 : *--------------------------------------------------------------*/
142 :
143 218955 : AVQ_demuxdec( st, x_norm, &nBits, Nsv, nq, avq_bit_sFlag, trgtSvPos );
144 :
145 : /* save # of AVQ unused bits for next subframe */
146 218955 : *unbits = nBits;
147 :
148 : /*--------------------------------------------------------------*
149 : * iDCT transform
150 : *--------------------------------------------------------------*/
151 :
152 218955 : set_f( code_preQ, 0.0f, L_SUBFR );
153 14232075 : for ( i = 0; i < Nsv * WIDTH_BAND; i++ )
154 : {
155 14013120 : code_preQ[i] = (float) ( x_norm[i] );
156 : }
157 :
158 218955 : if ( st->coder_type == INACTIVE || st->core_brate > MAX_BRATE_AVQ_EXC_TD || harm_flag_acelp )
159 : {
160 96090 : edct2( L_SUBFR, 1, code_preQ, code_preQ, ip_edct2_64, w_edct2_64 );
161 : }
162 :
163 : /*--------------------------------------------------------------*
164 : * Preemphasise
165 : *--------------------------------------------------------------*/
166 :
167 : /* in extreme cases at subframe boundaries, lower the preemphasis memory to avoid a saturation */
168 218955 : if ( ( nq[7] != 0 ) && ( st->last_nq_preQ - nq[0] > 7 ) )
169 : {
170 0 : st->mem_preemp_preQ /= 16;
171 : }
172 :
173 218955 : st->last_nq_preQ = nq[7];
174 :
175 : /* TD pre-quantizer: in extreme cases at subframe boundaries, lower the preemphasis memory to avoid a saturation */
176 218955 : if ( st->element_mode > EVS_MONO && st->coder_type != INACTIVE && st->core_brate >= MIN_BRATE_AVQ_EXC && st->core_brate <= MAX_BRATE_AVQ_EXC_TD && !harm_flag_acelp && code_preQ[0] != 0 )
177 : {
178 53529 : if ( fabs( (float) ( st->last_code_preq ) ) > 16.0f * (float) fabs( code_preQ[0] ) )
179 : {
180 0 : st->mem_preemp_preQ /= 16;
181 : }
182 53529 : else if ( fabs( (float) ( st->last_code_preq ) ) > 8.0f * (float) fabs( code_preQ[0] ) )
183 : {
184 12 : st->mem_preemp_preQ /= 8;
185 : }
186 : }
187 :
188 218955 : st->last_code_preq = (int16_t) code_preQ[L_SUBFR - 1];
189 :
190 218955 : preemph( code_preQ, FAC_PRE_AVQ, L_SUBFR, &st->mem_preemp_preQ );
191 :
192 : /*--------------------------------------------------------------*
193 : * Compute normalized prequantizer excitation gain for FEC
194 : *--------------------------------------------------------------*/
195 :
196 218955 : Ecode = ( sum2_f( code_preQ, L_SUBFR ) + 0.01f ) / L_SUBFR;
197 :
198 : /* somewhat attenuate pre-quantizer normalized gain for FEC */
199 218955 : *norm_gain_preQ = 0.8f * ( *gain_preQ ) * (float) sqrt( Ecode );
200 :
201 218955 : st->use_acelp_preq = 1;
202 :
203 218955 : return;
204 : }
205 :
206 : /*-----------------------------------------------------------*
207 : * gain_dequant()
208 : *
209 : * Returns decoded gain quantized between the specified
210 : * range using the specified number of levels.
211 : *-----------------------------------------------------------*/
212 :
213 : /*! r: decoded gain */
214 337254 : float gain_dequant(
215 : int16_t index, /* i : quantization index */
216 : const float min_val, /* i : value of lower limit */
217 : const float max_val, /* i : value of upper limit */
218 : const int16_t bits /* i : number of bits to dequantize */
219 : )
220 : {
221 : float gain, c_min, c_mult;
222 : int16_t levels;
223 :
224 337254 : levels = 1 << bits;
225 :
226 337254 : c_min = (float) log10( min_val );
227 337254 : c_mult = (float) ( ( levels - 1 ) / ( log10( max_val ) - c_min ) );
228 :
229 337254 : gain = (float) pow( 10.0, ( ( (float) index ) / c_mult ) + c_min );
230 :
231 337254 : return ( gain );
232 : }
|