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 <assert.h>
38 : #include <stdint.h>
39 : #include "options.h"
40 : #ifdef DEBUGGING
41 : #include "debug.h"
42 : #endif
43 : #include "cnst.h"
44 : #include "prot.h"
45 : #include "rom_com.h"
46 : #include "wmc_auto.h"
47 :
48 : /*---------------------------------------------------------------
49 : * tcx_decoder_memory_update()
50 : *
51 : *
52 : *--------------------------------------------------------------*/
53 :
54 2805831 : void tcx_decoder_memory_update(
55 : Decoder_State *st, /* i/o: decoder memory state */
56 : const float *xn_buf, /* i/o: mdct output buffer used also as temporary buffer */
57 : float *synthout, /* o : synth */
58 : const float *A /* i : Quantized LPC coefficients */
59 : )
60 : {
61 : int16_t L_frame_glob;
62 : float tmp;
63 : float *synth;
64 : float buf[1 + M + L_FRAME_PLUS];
65 2805831 : float preemph_f = st->preemph_fac;
66 :
67 2805831 : L_frame_glob = st->L_frame;
68 :
69 : /*TCX must be aligned with ACELP*/
70 2805831 : assert( st->hTcxCfg->lfacNext <= 0 );
71 :
72 : /* Output synth */
73 2805831 : mvr2r( xn_buf, synthout, L_frame_glob );
74 :
75 : /* Update synth */
76 2805831 : synth = buf + 1 + M;
77 2805831 : mvr2r( st->syn, buf, 1 + M );
78 2805831 : mvr2r( xn_buf, synth, L_frame_glob );
79 2805831 : mvr2r( synth + L_frame_glob - M - 1, st->syn, 1 + M );
80 :
81 : /* Emphasis of synth -> synth_pe */
82 2805831 : tmp = synth[-M - 1];
83 2805831 : preemph( synth - M, preemph_f, M + L_frame_glob, &tmp );
84 :
85 2805831 : mvr2r( synth + L_frame_glob - M, st->mem_syn2, M );
86 2805831 : mvr2r( synth + L_frame_glob - L_SYN_MEM, st->mem_syn_r, L_SYN_MEM );
87 :
88 2805831 : if ( st->element_mode != IVAS_CPE_MDCT )
89 : {
90 701457 : if ( !st->tcxonly || L_frame_glob == L_FRAME16k )
91 : {
92 : /* Update excitation */
93 383574 : assert( L_frame_glob < L_EXC_MEM_DEC );
94 383574 : mvr2r( st->old_exc + ( L_frame_glob ), st->old_exc, L_EXC_MEM_DEC - ( L_frame_glob ) );
95 383574 : residu( A, M, synth, st->old_exc + L_EXC_MEM_DEC - ( L_frame_glob ), ( L_frame_glob ) );
96 : }
97 :
98 : /* Update old_Aq */
99 701457 : mvr2r( A, st->old_Aq_12_8, M + 1 );
100 : }
101 :
102 2805831 : return;
103 : }
104 :
105 : /*---------------------------------------------------------------
106 : * tcx_ari_res_invQ_spec()
107 : *
108 : * Residual Quantization
109 : *--------------------------------------------------------------*/
110 :
111 : /*! r: number of bits used (including "bits") */
112 50958 : int16_t tcx_ari_res_invQ_spec(
113 : float x_Q[], /* i/o: quantized spectrum */
114 : const int16_t L_frame, /* i : number of lines */
115 : const int16_t prm[], /* i : bitstream */
116 : int16_t target_bits, /* i : number of bits available */
117 : int16_t bits, /* i : number of bits used so far */
118 : const float deadzone, /* i : quantizer deadzone */
119 : const float x_fac[] /* i : spectrum post-quantization factors */
120 : )
121 : {
122 : int16_t i, j, num_zeros;
123 : int16_t zeros[L_FRAME_PLUS];
124 : float fac_m, fac_p, sign;
125 :
126 : /* Limit the number of residual bits */
127 50958 : target_bits = min( target_bits, NPRM_RESQ );
128 :
129 : /* Requantize the spectrum line-by-line */
130 50958 : fac_m = deadzone * 0.5f;
131 50958 : num_zeros = 0;
132 2205522 : for ( i = 0; i < L_frame; ++i )
133 : {
134 2202168 : if ( bits >= target_bits )
135 : {
136 : /* no bits left */
137 47604 : break;
138 : }
139 2154564 : if ( x_Q[i] != 0 )
140 : {
141 45372 : if ( x_Q[i] > 0 )
142 : {
143 22695 : sign = x_fac[i];
144 : }
145 : else
146 : {
147 22677 : sign = -x_fac[i];
148 : }
149 :
150 45372 : x_Q[i] += sign * ( prm[bits++] * 0.5f - fac_m );
151 : }
152 : else
153 : {
154 2109192 : zeros[num_zeros++] = i;
155 : }
156 : }
157 :
158 : /* Requantize zeroed-lines of the spectrum */
159 50958 : fac_p = ( 1.0f - deadzone ) * 0.33f * 2.0f;
160 50958 : --target_bits; /* reserve 1 bit for the check below */
161 156270 : for ( j = 0; j < num_zeros; ++j )
162 : {
163 110121 : if ( bits >= target_bits )
164 : {
165 : /* 1 or 0 bits left */
166 4809 : break;
167 : }
168 :
169 105312 : i = zeros[j];
170 :
171 105312 : if ( prm[bits++] != 0 )
172 : {
173 30678 : x_Q[i] = ( 2 * prm[bits++] - 1 ) * fac_p * x_fac[i];
174 : }
175 : }
176 :
177 50958 : return bits;
178 : }
179 :
180 :
181 : /*---------------------------------------------------------------
182 : * tcx_res_invQ_gain()
183 : *
184 : *
185 : *--------------------------------------------------------------*/
186 :
187 1093812 : int16_t tcx_res_invQ_gain(
188 : float *gain_tcx,
189 : const int16_t *prm,
190 : const int16_t resQBits )
191 : {
192 : int16_t bits;
193 :
194 : /*Refine the gain quantization*/
195 3880791 : for ( bits = 0; bits < min( resQBits, TCX_RES_Q_BITS_GAIN ); bits++ )
196 : {
197 2786979 : if ( prm[bits] == 0 )
198 : {
199 1409874 : *gain_tcx = ( *gain_tcx ) * gain_corr_inv_fac[bits];
200 : }
201 : else
202 : {
203 1377105 : *gain_tcx = ( *gain_tcx ) * gain_corr_fac[bits];
204 : }
205 : }
206 :
207 1093812 : return ( bits );
208 : }
209 :
210 :
211 : /*---------------------------------------------------------------
212 : * tcx_res_invQ_spec()
213 : *
214 : *
215 : *--------------------------------------------------------------*/
216 :
217 1093812 : int16_t tcx_res_invQ_spec(
218 : float *x,
219 : const int16_t L_frame,
220 : const int16_t *prm,
221 : int16_t resQBits,
222 : int16_t bits,
223 : const float sq_round,
224 : const float lf_deemph_factors[] )
225 : {
226 : int16_t i;
227 : float fac_m, fac_p, thres;
228 :
229 : /* Limit the number of residual bits */
230 1093812 : resQBits = min( resQBits, NPRM_RESQ );
231 :
232 : /* Requantize the spectrum line-by-line */
233 1093812 : fac_p = 0.5f - sq_round * 0.5f;
234 1093812 : fac_m = sq_round * 0.5f;
235 1093812 : if ( !lf_deemph_factors )
236 : {
237 20417028 : for ( i = 0; ( i < L_frame ) && ( bits < resQBits ); i++ )
238 : {
239 : /* bits < resQBits */
240 19648614 : if ( x[i] != 0.0f )
241 : {
242 5177463 : if ( prm[bits++] == 0 )
243 : {
244 2591640 : x[i] -= ( x[i] > 0.0f ) ? fac_m : fac_p;
245 : }
246 : else
247 : {
248 2585823 : x[i] += ( x[i] > 0.0f ) ? fac_p : fac_m;
249 : }
250 : }
251 : }
252 768414 : resQBits--; /* Quantize zeroed lines of the spectrum */
253 1171509 : for ( i = 0; ( i < L_frame ) && ( bits < resQBits ); i++ )
254 : {
255 403095 : if ( x[i] == 0.0f )
256 : {
257 267138 : if ( prm[bits++] != 0 )
258 : {
259 118785 : x[i] = ( prm[bits++] * 2.64f - 1.32f ) * fac_p;
260 : }
261 : }
262 : }
263 :
264 768414 : return bits;
265 : }
266 17955486 : for ( i = 0; i < L_frame; i++ )
267 : {
268 17935023 : if ( bits >= resQBits )
269 : {
270 304935 : break;
271 : }
272 17630088 : if ( x[i] != 0.f && lf_deemph_factors[i] > 0.5f )
273 : {
274 1402377 : if ( prm[bits++] == 0 )
275 : {
276 699990 : x[i] -= ( x[i] > 0 ) ? fac_m * lf_deemph_factors[i] : fac_p * lf_deemph_factors[i];
277 : }
278 : else
279 : {
280 702387 : x[i] += ( x[i] > 0 ) ? fac_p * lf_deemph_factors[i] : fac_m * lf_deemph_factors[i];
281 : }
282 : }
283 : }
284 :
285 : /*Quantize zeroed-line of the spectrum*/
286 1551693 : for ( i = 0; i < L_frame; i++ )
287 : {
288 1551693 : if ( bits >= ( resQBits - 1 ) ) /*need at least two bits*/
289 : {
290 325398 : break;
291 : }
292 1226295 : if ( x[i] == 0.f && lf_deemph_factors[i] > 0.5f )
293 : {
294 990552 : if ( prm[bits++] == 1 )
295 : {
296 307977 : thres = ( 1 - sq_round ) * 0.66f * lf_deemph_factors[i];
297 307977 : x[i] = ( prm[bits++] * 2.f - 1.f ) * thres;
298 : }
299 : }
300 : }
301 :
302 325398 : return bits;
303 : }
|