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 : #include <stdint.h>
34 : #include "options.h"
35 : #include "prot_fx.h"
36 : #include "rom_com.h"
37 : #include "ivas_rom_com.h"
38 : #include "ivas_cnst.h"
39 : #include <math.h>
40 : #include "wmc_auto.h"
41 : #include "ivas_prot_fx.h"
42 :
43 : /*-------------------------------------------------------------------
44 : * sns_1st_dec_fx()
45 : *
46 : *
47 : *-------------------------------------------------------------------*/
48 :
49 355008 : static void sns_1st_dec_fx(
50 : const Word16 index, /* i : codebook index */
51 : const Word16 core,
52 : const Word16 L_frame,
53 : Word32 *snsq_fx /* i/o: i:prediction o:quantized sns Q16*/
54 : )
55 : {
56 : Word16 i;
57 : const Word16 *p_dico, *means;
58 :
59 355008 : const Word16 cdbk_fix = 8; // 1.f / powf( 2, SNS_CDBKS_BITS_4_FRAC ) in Q15
60 355008 : move16();
61 355008 : const Word16 means_fix = 2; // 1.f / powf( 2, SNS_MEANS_BITS_4_FRAC ) in Q15
62 355008 : move16();
63 :
64 355008 : means = NULL;
65 355008 : SWITCH( L_frame )
66 : {
67 36810 : case L_FRAME16k:
68 36810 : means = &sns_1st_means_16k[core - 1][0]; /*Q0*/
69 36810 : BREAK;
70 108460 : case L_FRAME25_6k:
71 108460 : means = &sns_1st_means_25k6[core - 1][0]; /*Q0*/
72 108460 : BREAK;
73 209738 : case L_FRAME32k:
74 209738 : means = &sns_1st_means_32k[core - 1][0]; /*Q0*/
75 209738 : BREAK;
76 0 : default:
77 0 : assert( !"illegal frame length in sns_1st_cod" );
78 : }
79 :
80 :
81 355008 : p_dico = &sns_1st_cdbk[0][core - 1][0] + i_mult( index % 32, ( M / 2 ) ); /*Q0*/
82 :
83 3195072 : FOR( i = 0; i < M / 2; i++ )
84 : {
85 2840064 : snsq_fx[i] = L_add( L_mult( ( *p_dico++ ), cdbk_fix ), L_mult( means[i], means_fix ) ); // Q16
86 2840064 : move32();
87 : }
88 :
89 355008 : p_dico = &sns_1st_cdbk[1][sub( core, 1 )][0] + i_mult( shr( index, 5 ), ( M / 2 ) ); /*Q0*/
90 :
91 3195072 : FOR( i = M / 2; i < M; i++ )
92 : {
93 2840064 : snsq_fx[i] = L_add( L_mult( ( *p_dico++ ), cdbk_fix ), L_mult( means[i], means_fix ) ); /*Q16*/
94 2840064 : move32();
95 : }
96 :
97 355008 : return;
98 : }
99 : /*-------------------------------------------------------------------
100 : * sns_2st_dec_fx()
101 : *
102 : *
103 : *-------------------------------------------------------------------*/
104 :
105 526863 : static void sns_2st_dec_fx(
106 : Word32 *snsq_fx, /* i/o: i:1st stage o:1st+2nd stage q_snsq*/
107 : Word16 *q_snsq,
108 : Word16 *indx /* i : index[] (4 bits per words) Q0*/
109 : )
110 : {
111 : Word16 i;
112 : Word16 xq_fx[M];
113 526863 : Word16 scale_fx = 13107; /*1.0/2.5f in Q15*/
114 526863 : move16();
115 : /* quantize */
116 526863 : AVQ_dec_lpc( indx, xq_fx, 2 );
117 526863 : Word32 tmp = 0;
118 526863 : move32();
119 526863 : Word16 snsq_dIFf = 0;
120 526863 : move16();
121 : Word32 tmp1;
122 :
123 8956671 : FOR( i = 0; i < M; i++ )
124 : {
125 8429808 : tmp = L_mult( scale_fx, xq_fx[i] ); // q16
126 8429808 : IF( GE_16( *q_snsq, 16 ) )
127 : {
128 8429808 : snsq_dIFf = sub( *q_snsq, 16 );
129 8429808 : tmp1 = L_shr( snsq_fx[i], snsq_dIFf ); /*Q16*/
130 8429808 : snsq_fx[i] = L_add( tmp1, tmp ); /*Q16*/
131 8429808 : move32();
132 8429808 : *q_snsq = 16;
133 8429808 : move16();
134 : }
135 : ELSE
136 : {
137 0 : snsq_dIFf = sub( 16, *q_snsq );
138 0 : tmp1 = L_shr( tmp, snsq_dIFf ); /*q_snsq*/
139 0 : snsq_fx[i] = L_add( snsq_fx[i], tmp1 ); /*q_snsq*/
140 0 : move32();
141 : }
142 : }
143 526863 : return;
144 : }
145 : /*-------------------------------------------------------------------
146 : * sns_avq_dec_fx()
147 : *
148 : * Stereo noise-shaping AVQ decoder FOR 1 channel
149 : *-------------------------------------------------------------------*/
150 :
151 75092 : void sns_avq_dec_fx(
152 : Word16 *index, /* i : Quantization indices Q0*/
153 : Word32 SNS_Q[NB_DIV][M], /* o : Quantized SNS vectors q_snsq*/
154 : Word16 *q_snsq,
155 : const Word16 L_frame, /* i : frame length Q0*/
156 : const Word16 numlpc /* i : Number of sets of lpc Q0*/
157 : )
158 : {
159 : Word16 i, nbi, last;
160 : Word16 q_type;
161 :
162 : /* go from one-based indexing to zero-based indexing */
163 75092 : last = sub( numlpc, 1 ); /*Q0*/
164 :
165 75092 : index++;
166 :
167 : /* Decode last LPC */
168 75092 : sns_1st_dec_fx( *index++, numlpc, L_frame, SNS_Q[last] ); // Q16
169 75092 : *q_snsq = 16;
170 75092 : move16();
171 75092 : sns_2st_dec_fx( SNS_Q[last], q_snsq, index );
172 :
173 75092 : nbi = add( add( 2, index[0] ), index[1] ); /*Q0*/
174 75092 : index += nbi;
175 :
176 : /* Decode intermediate LPC (512 framing) */
177 75092 : IF( EQ_16( numlpc, 2 ) )
178 : {
179 12115 : q_type = *index++; /*Q0*/
180 12115 : move16();
181 :
182 12115 : IF( q_type == 0 )
183 : {
184 4490 : sns_1st_dec_fx( *index++, numlpc, L_frame, SNS_Q[0] );
185 4490 : *q_snsq = 16;
186 4490 : move16();
187 4490 : sns_2st_dec_fx( SNS_Q[0], q_snsq, index );
188 : }
189 7625 : ELSE IF( EQ_16( q_type, 1 ) )
190 : {
191 129625 : FOR( i = 0; i < M; i++ )
192 : {
193 122000 : SNS_Q[0][i] = SNS_Q[1][i]; /*q_snsq*/
194 122000 : move32();
195 : }
196 7625 : sns_2st_dec_fx( SNS_Q[0], q_snsq, index );
197 : }
198 : }
199 :
200 75092 : return;
201 : }
202 :
203 : /*-------------------------------------------------------------------
204 : * sns_avq_dec_stereo_fx()
205 : *
206 : * Stereo noise-shaping AVQ decoder FOR 21 channels
207 : *-------------------------------------------------------------------*/
208 :
209 229881 : void sns_avq_dec_stereo_fx(
210 : Word16 *indexl, /* i : Quantization indices (left channel) Q0*/
211 : Word16 *indexr, /* i : Quantization indices (right channe) Q0*/
212 : const Word16 L_frame, /* i : frame length Q0*/
213 : Word32 *SNS_Ql, /* o : Quantized SNS vectors (left channel) q_l*/
214 : Word16 *q_l,
215 : Word32 *SNS_Qr, /* o : Quantized SNS vectors (right channe) q_r*/
216 : Word16 *q_r )
217 : {
218 : Word16 i, stereo_mode;
219 :
220 : Word32 mid_q_fx[M], side_q_fx[M];
221 229881 : stereo_mode = *indexl++; /*Q0*/
222 229881 : move16();
223 :
224 229881 : indexr++;
225 229881 : Word16 tmp1 = 0, tmp2 = 0, q_mid = 0, q_side = 0;
226 229881 : Word16 tmp_dIFf_q = 0;
227 229881 : move16();
228 229881 : move16();
229 229881 : move16();
230 229881 : move16();
231 229881 : move16();
232 :
233 229881 : IF( EQ_16( stereo_mode, 2 ) )
234 : {
235 : /* MS coding */
236 184336 : tmp1 = *indexl++; /*Q0*/
237 184336 : move16();
238 184336 : sns_1st_dec_fx( tmp1, TCX_20_CORE, L_frame, mid_q_fx );
239 184336 : q_mid = 16;
240 184336 : move16();
241 184336 : sns_2st_dec_fx( mid_q_fx, &q_mid, indexl );
242 :
243 3133712 : FOR( i = 0; i < M; i++ )
244 : {
245 2949376 : side_q_fx[i] = 0;
246 2949376 : move32();
247 2949376 : q_side = 31;
248 2949376 : move16();
249 : }
250 :
251 184336 : IF( EQ_16( *indexr++, -1 ) )
252 : {
253 164230 : sns_2st_dec_fx( side_q_fx, &q_side, indexr );
254 : }
255 :
256 184336 : Word32 tmp_c = 0;
257 184336 : move32();
258 3133712 : FOR( i = 0; i < M; i++ )
259 : {
260 2949376 : tmp_c = L_shr( side_q_fx[i], 1 ); // q_side
261 :
262 2949376 : IF( GE_16( q_mid, q_side ) )
263 : {
264 2627680 : tmp_dIFf_q = sub( q_mid, q_side );
265 2627680 : SNS_Ql[i] = L_add( L_shr( mid_q_fx[i], tmp_dIFf_q ), tmp_c ); // q_side
266 2627680 : move32();
267 2627680 : SNS_Qr[i] = L_sub( L_shr( mid_q_fx[i], tmp_dIFf_q ), tmp_c ); // q_side
268 2627680 : move32();
269 2627680 : *q_r = q_side;
270 2627680 : move16();
271 2627680 : *q_l = *q_r;
272 2627680 : move16();
273 : }
274 : ELSE
275 : {
276 321696 : tmp_dIFf_q = sub( q_side, q_mid );
277 321696 : SNS_Ql[i] = L_add( mid_q_fx[i], L_shr( tmp_c, tmp_dIFf_q ) ); // q_mid
278 321696 : move32();
279 321696 : SNS_Qr[i] = L_sub( mid_q_fx[i], L_shr( tmp_c, tmp_dIFf_q ) ); // q_mid
280 321696 : move32();
281 321696 : *q_r = q_mid;
282 321696 : move16();
283 321696 : *q_l = *q_r;
284 321696 : move16();
285 : }
286 : }
287 : }
288 : ELSE
289 : {
290 : /* LR decoding */
291 45545 : tmp1 = *indexl++; /*Q0*/
292 45545 : move16();
293 45545 : tmp2 = *indexr++; /*Q0*/
294 45545 : move16();
295 45545 : sns_1st_dec_fx( tmp1, TCX_20_CORE, L_frame, SNS_Ql );
296 45545 : *q_l = 16;
297 45545 : move16();
298 45545 : sns_2st_dec_fx( SNS_Ql, q_l, indexl );
299 :
300 45545 : sns_1st_dec_fx( tmp2, TCX_20_CORE, L_frame, SNS_Qr );
301 45545 : *q_r = 16;
302 45545 : move16();
303 45545 : sns_2st_dec_fx( SNS_Qr, q_r, indexr );
304 : }
305 :
306 229881 : return;
307 : }
308 :
309 : /*-------------------------------------------------------------------
310 : * dequantize_sns_fx()
311 : *
312 : * Dequantize SNS
313 : *-------------------------------------------------------------------*/
314 :
315 47542 : void dequantize_sns_fx(
316 : Word16 indices[CPE_CHANNELS][NPRM_LPC_NEW], /*Q0*/
317 : Word32 snsQ_out_fx[CPE_CHANNELS][NB_DIV][M], /*Q16*/
318 : Decoder_State **sts )
319 : {
320 : Word16 nSubframes, k, ch;
321 : Word16 sns_stereo_mode[NB_DIV];
322 : Word16 zero_side_flag[NB_DIV];
323 : Decoder_State *st;
324 :
325 47542 : sns_stereo_mode[0] = indices[0][0]; /*Q0*/
326 47542 : move16();
327 47542 : sns_stereo_mode[1] = indices[0][1]; /*Q0*/
328 47542 : move16();
329 47542 : zero_side_flag[0] = indices[0][2]; /*Q0*/
330 47542 : move16();
331 47542 : zero_side_flag[1] = indices[0][3]; /*Q0*/
332 47542 : move16();
333 :
334 142626 : FOR( ch = 0; ch < CPE_CHANNELS; ++ch )
335 : {
336 : Word16 idxIndices;
337 :
338 95084 : st = sts[ch];
339 95084 : nSubframes = NB_DIV;
340 95084 : move16();
341 95084 : if ( EQ_16( st->core, TCX_20_CORE ) )
342 : {
343 92758 : nSubframes = 1;
344 92758 : move16();
345 : }
346 :
347 95084 : idxIndices = 0;
348 95084 : move16();
349 :
350 192494 : FOR( k = 0; k < nSubframes; ++k )
351 : {
352 97410 : const Word16 is_side = EQ_16( ch, 1 ) && EQ_16( sns_stereo_mode[k], SNS_STEREO_MODE_MS );
353 :
354 97410 : const Word16 *const *cdbks_fx = EQ_16( nSubframes, 1 ) ? ivas_sns_cdbks_tcx20_fx : ivas_sns_cdbks_tcx10_fx;
355 :
356 97410 : Word16 nStages = SNS_MSVQ_NSTAGES_TCX10;
357 97410 : move16();
358 :
359 97410 : if ( EQ_16( nSubframes, 1 ) )
360 : {
361 92758 : nStages = SNS_MSVQ_NSTAGES_TCX20;
362 92758 : move16();
363 : }
364 :
365 97410 : Word32 *snsQ_fx = snsQ_out_fx[ch][k]; /*Q16*/
366 :
367 97410 : IF( is_side )
368 : {
369 44906 : const Word16 *const *side_cdbks_fx = EQ_16( st->core, TCX_20_CORE ) ? ivas_sns_cdbks_side_tcx20_fx : ivas_sns_cdbks_side_tcx10_fx;
370 :
371 44906 : IF( zero_side_flag[k] )
372 : {
373 9434 : set32_fx( snsQ_fx, 0, M );
374 9434 : CONTINUE;
375 : }
376 :
377 35472 : nStages = SNS_MSVQ_NSTAGES_SIDE;
378 35472 : move16();
379 35472 : msvq_dec_fx( side_cdbks_fx, NULL, NULL, nStages, M, M, &indices[ch][( idxIndices + SNS_STEREO_MODE_OFFSET_INDICES )], 0, NULL, snsQ_fx, NULL, 15 ); // always 15
380 : }
381 : ELSE
382 : {
383 52504 : msvq_dec_fx( cdbks_fx, NULL, NULL, nStages, M, M, &indices[ch][( idxIndices + SNS_STEREO_MODE_OFFSET_INDICES )], 0, NULL, snsQ_fx, NULL, 12 );
384 : }
385 87976 : idxIndices = add( idxIndices, nStages ); /*Q0*/
386 : }
387 : }
388 :
389 47542 : IF( EQ_16( sns_stereo_mode[0], SNS_STEREO_MODE_MS ) || EQ_16( sns_stereo_mode[1], SNS_STEREO_MODE_MS ) )
390 : {
391 44051 : nSubframes = NB_DIV;
392 44051 : move16();
393 44051 : if ( EQ_16( sts[0]->core, TCX_20_CORE ) )
394 : {
395 43100 : nSubframes = 1;
396 43100 : move16();
397 : }
398 89053 : FOR( k = 0; k < nSubframes; ++k )
399 : {
400 45002 : IF( EQ_16( sns_stereo_mode[k], SNS_STEREO_MODE_MS ) )
401 : {
402 44906 : inverseMS_fx( M, snsQ_out_fx[0][k], snsQ_out_fx[1][k], ONE_IN_Q31 ); /*Q16*/
403 : }
404 : }
405 : }
406 :
407 47542 : return;
408 : }
|