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 386406 : 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 386406 : const Word16 cdbk_fix = 8; // 1.f / powf( 2, SNS_CDBKS_BITS_4_FRAC ) in Q15
60 386406 : move16();
61 386406 : const Word16 means_fix = 2; // 1.f / powf( 2, SNS_MEANS_BITS_4_FRAC ) in Q15
62 386406 : move16();
63 :
64 386406 : means = NULL;
65 386406 : SWITCH( L_frame )
66 : {
67 37706 : case L_FRAME16k:
68 37706 : means = &sns_1st_means_16k[core - 1][0]; /*Q0*/
69 37706 : BREAK;
70 119833 : case L_FRAME25_6k:
71 119833 : means = &sns_1st_means_25k6[core - 1][0]; /*Q0*/
72 119833 : BREAK;
73 228867 : case L_FRAME32k:
74 228867 : means = &sns_1st_means_32k[core - 1][0]; /*Q0*/
75 228867 : BREAK;
76 0 : default:
77 0 : assert( !"illegal frame length in sns_1st_cod" );
78 : }
79 :
80 :
81 386406 : p_dico = &sns_1st_cdbk[0][core - 1][0] + i_mult( index % 32, ( M / 2 ) ); /*Q0*/
82 :
83 3477654 : FOR( i = 0; i < M / 2; i++ )
84 : {
85 3091248 : snsq_fx[i] = L_add( L_mult( ( *p_dico++ ), cdbk_fix ), L_mult( means[i], means_fix ) ); // Q16
86 3091248 : move32();
87 : }
88 :
89 386406 : p_dico = &sns_1st_cdbk[1][sub( core, 1 )][0] + i_mult( shr( index, 5 ), ( M / 2 ) ); /*Q0*/
90 :
91 3477654 : FOR( i = M / 2; i < M; i++ )
92 : {
93 3091248 : snsq_fx[i] = L_add( L_mult( ( *p_dico++ ), cdbk_fix ), L_mult( means[i], means_fix ) ); /*Q16*/
94 3091248 : move32();
95 : }
96 :
97 386406 : return;
98 : }
99 : /*-------------------------------------------------------------------
100 : * sns_2st_dec_fx()
101 : *
102 : *
103 : *-------------------------------------------------------------------*/
104 :
105 570625 : 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 570625 : Word16 scale_fx = 13107; /*1.0/2.5f in Q15*/
114 570625 : move16();
115 : /* quantize */
116 570625 : AVQ_dec_lpc( indx, xq_fx, 2 );
117 570625 : Word32 tmp = 0;
118 570625 : move32();
119 570625 : Word16 snsq_dIFf = 0;
120 570625 : move16();
121 : Word32 tmp1;
122 :
123 9700625 : FOR( i = 0; i < M; i++ )
124 : {
125 9130000 : tmp = L_mult( scale_fx, xq_fx[i] ); // q16
126 9130000 : IF( GE_16( *q_snsq, 16 ) )
127 : {
128 9130000 : snsq_dIFf = sub( *q_snsq, 16 );
129 9130000 : tmp1 = L_shr( snsq_fx[i], snsq_dIFf ); /*Q16*/
130 9130000 : snsq_fx[i] = L_add( tmp1, tmp ); /*Q16*/
131 9130000 : move32();
132 9130000 : *q_snsq = 16;
133 9130000 : 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 570625 : return;
144 : }
145 : /*-------------------------------------------------------------------
146 : * sns_avq_dec_fx()
147 : *
148 : * Stereo noise-shaping AVQ decoder FOR 1 channel
149 : *-------------------------------------------------------------------*/
150 :
151 78184 : 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 78184 : last = sub( numlpc, 1 ); /*Q0*/
164 :
165 78184 : index++;
166 :
167 : /* Decode last LPC */
168 78184 : sns_1st_dec_fx( *index++, numlpc, L_frame, SNS_Q[last] ); // Q16
169 78184 : *q_snsq = 16;
170 78184 : move16();
171 78184 : sns_2st_dec_fx( SNS_Q[last], q_snsq, index );
172 :
173 78184 : nbi = add( add( 2, index[0] ), index[1] ); /*Q0*/
174 78184 : index += nbi;
175 :
176 : /* Decode intermediate LPC (512 framing) */
177 78184 : IF( EQ_16( numlpc, 2 ) )
178 : {
179 13986 : q_type = *index++; /*Q0*/
180 13986 : move16();
181 :
182 13986 : IF( q_type == 0 )
183 : {
184 5008 : sns_1st_dec_fx( *index++, numlpc, L_frame, SNS_Q[0] );
185 5008 : *q_snsq = 16;
186 5008 : move16();
187 5008 : sns_2st_dec_fx( SNS_Q[0], q_snsq, index );
188 : }
189 8978 : ELSE IF( EQ_16( q_type, 1 ) )
190 : {
191 152626 : FOR( i = 0; i < M; i++ )
192 : {
193 143648 : SNS_Q[0][i] = SNS_Q[1][i]; /*q_snsq*/
194 143648 : move32();
195 : }
196 8978 : sns_2st_dec_fx( SNS_Q[0], q_snsq, index );
197 : }
198 : }
199 :
200 78184 : return;
201 : }
202 :
203 : /*-------------------------------------------------------------------
204 : * sns_avq_dec_stereo_fx()
205 : *
206 : * Stereo noise-shaping AVQ decoder FOR 21 channels
207 : *-------------------------------------------------------------------*/
208 :
209 256410 : 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 256410 : stereo_mode = *indexl++; /*Q0*/
222 256410 : move16();
223 :
224 256410 : indexr++;
225 256410 : Word16 tmp1 = 0, tmp2 = 0, q_mid = 0, q_side = 0;
226 256410 : Word16 tmp_dIFf_q = 0;
227 256410 : move16();
228 256410 : move16();
229 256410 : move16();
230 256410 : move16();
231 256410 : move16();
232 :
233 256410 : IF( EQ_16( stereo_mode, 2 ) )
234 : {
235 : /* MS coding */
236 209606 : tmp1 = *indexl++; /*Q0*/
237 209606 : move16();
238 209606 : sns_1st_dec_fx( tmp1, TCX_20_CORE, L_frame, mid_q_fx );
239 209606 : q_mid = 16;
240 209606 : move16();
241 209606 : sns_2st_dec_fx( mid_q_fx, &q_mid, indexl );
242 :
243 3563302 : FOR( i = 0; i < M; i++ )
244 : {
245 3353696 : side_q_fx[i] = 0;
246 3353696 : move32();
247 3353696 : q_side = 31;
248 3353696 : move16();
249 : }
250 :
251 209606 : IF( EQ_16( *indexr++, -1 ) )
252 : {
253 175241 : sns_2st_dec_fx( side_q_fx, &q_side, indexr );
254 : }
255 :
256 209606 : Word32 tmp_c = 0;
257 209606 : move32();
258 3563302 : FOR( i = 0; i < M; i++ )
259 : {
260 3353696 : tmp_c = L_shr( side_q_fx[i], 1 ); // q_side
261 :
262 3353696 : IF( GE_16( q_mid, q_side ) )
263 : {
264 2803856 : tmp_dIFf_q = sub( q_mid, q_side );
265 2803856 : SNS_Ql[i] = L_add( L_shr( mid_q_fx[i], tmp_dIFf_q ), tmp_c ); // q_side
266 2803856 : move32();
267 2803856 : SNS_Qr[i] = L_sub( L_shr( mid_q_fx[i], tmp_dIFf_q ), tmp_c ); // q_side
268 2803856 : move32();
269 2803856 : *q_r = q_side;
270 2803856 : move16();
271 2803856 : *q_l = *q_r;
272 2803856 : move16();
273 : }
274 : ELSE
275 : {
276 549840 : tmp_dIFf_q = sub( q_side, q_mid );
277 549840 : SNS_Ql[i] = L_add( mid_q_fx[i], L_shr( tmp_c, tmp_dIFf_q ) ); // q_mid
278 549840 : move32();
279 549840 : SNS_Qr[i] = L_sub( mid_q_fx[i], L_shr( tmp_c, tmp_dIFf_q ) ); // q_mid
280 549840 : move32();
281 549840 : *q_r = q_mid;
282 549840 : move16();
283 549840 : *q_l = *q_r;
284 549840 : move16();
285 : }
286 : }
287 : }
288 : ELSE
289 : {
290 : /* LR decoding */
291 46804 : tmp1 = *indexl++; /*Q0*/
292 46804 : move16();
293 46804 : tmp2 = *indexr++; /*Q0*/
294 46804 : move16();
295 46804 : sns_1st_dec_fx( tmp1, TCX_20_CORE, L_frame, SNS_Ql );
296 46804 : *q_l = 16;
297 46804 : move16();
298 46804 : sns_2st_dec_fx( SNS_Ql, q_l, indexl );
299 :
300 46804 : sns_1st_dec_fx( tmp2, TCX_20_CORE, L_frame, SNS_Qr );
301 46804 : *q_r = 16;
302 46804 : move16();
303 46804 : sns_2st_dec_fx( SNS_Qr, q_r, indexr );
304 : }
305 :
306 256410 : return;
307 : }
308 :
309 : /*-------------------------------------------------------------------
310 : * dequantize_sns_fx()
311 : *
312 : * Dequantize SNS
313 : *-------------------------------------------------------------------*/
314 :
315 55797 : 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 55797 : sns_stereo_mode[0] = indices[0][0]; /*Q0*/
326 55797 : move16();
327 55797 : sns_stereo_mode[1] = indices[0][1]; /*Q0*/
328 55797 : move16();
329 55797 : zero_side_flag[0] = indices[0][2]; /*Q0*/
330 55797 : move16();
331 55797 : zero_side_flag[1] = indices[0][3]; /*Q0*/
332 55797 : move16();
333 :
334 167391 : FOR( ch = 0; ch < CPE_CHANNELS; ++ch )
335 : {
336 : Word16 idxIndices;
337 :
338 111594 : st = sts[ch];
339 111594 : nSubframes = NB_DIV;
340 111594 : move16();
341 111594 : if ( EQ_16( st->core, TCX_20_CORE ) )
342 : {
343 108540 : nSubframes = 1;
344 108540 : move16();
345 : }
346 :
347 111594 : idxIndices = 0;
348 111594 : move16();
349 :
350 226242 : FOR( k = 0; k < nSubframes; ++k )
351 : {
352 114648 : const Word16 is_side = EQ_16( ch, 1 ) && EQ_16( sns_stereo_mode[k], SNS_STEREO_MODE_MS );
353 :
354 114648 : const Word16 *const *cdbks_fx = EQ_16( nSubframes, 1 ) ? ivas_sns_cdbks_tcx20_fx : ivas_sns_cdbks_tcx10_fx;
355 :
356 114648 : Word16 nStages = SNS_MSVQ_NSTAGES_TCX10;
357 114648 : move16();
358 :
359 114648 : if ( EQ_16( nSubframes, 1 ) )
360 : {
361 108540 : nStages = SNS_MSVQ_NSTAGES_TCX20;
362 108540 : move16();
363 : }
364 :
365 114648 : Word32 *snsQ_fx = snsQ_out_fx[ch][k]; /*Q16*/
366 :
367 114648 : IF( is_side )
368 : {
369 52792 : 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 52792 : IF( zero_side_flag[k] )
372 : {
373 14738 : set32_fx( snsQ_fx, 0, M );
374 14738 : CONTINUE;
375 : }
376 :
377 38054 : nStages = SNS_MSVQ_NSTAGES_SIDE;
378 38054 : move16();
379 38054 : 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 61856 : 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 99910 : idxIndices = add( idxIndices, nStages ); /*Q0*/
386 : }
387 : }
388 :
389 55797 : IF( EQ_16( sns_stereo_mode[0], SNS_STEREO_MODE_MS ) || EQ_16( sns_stereo_mode[1], SNS_STEREO_MODE_MS ) )
390 : {
391 51627 : nSubframes = NB_DIV;
392 51627 : move16();
393 51627 : if ( EQ_16( sts[0]->core, TCX_20_CORE ) )
394 : {
395 50355 : nSubframes = 1;
396 50355 : move16();
397 : }
398 104526 : FOR( k = 0; k < nSubframes; ++k )
399 : {
400 52899 : IF( EQ_16( sns_stereo_mode[k], SNS_STEREO_MODE_MS ) )
401 : {
402 52792 : inverseMS_fx( M, snsQ_out_fx[0][k], snsQ_out_fx[1][k], ONE_IN_Q31 ); /*Q16*/
403 : }
404 : }
405 : }
406 :
407 55797 : return;
408 : }
|