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 "ivas_cnst.h"
36 : #include "prot_fx.h"
37 : #include "wmc_auto.h"
38 : #include <assert.h>
39 : #include "stat_enc.h"
40 : #include "ivas_prot_fx.h"
41 :
42 : /*----------------------------------------------------------*
43 : * indexToChannelPair()
44 : *
45 : * get the index of each channel pair
46 : *----------------------------------------------------------*/
47 :
48 82161 : static void indexToChannelPair_fx(
49 : MCT_DEC_BLOCK_DATA_HANDLE hBlock,
50 : const Word16 nChannels,
51 : const Word16 pairIdx )
52 : {
53 : Word16 ch1, ch2;
54 82161 : Word16 tmpIdx = 0;
55 82161 : move16();
56 :
57 305743 : FOR( ch2 = 1; ch2 < nChannels; ch2++ )
58 : {
59 983389 : FOR( ch1 = 0; ch1 < ch2; ch1++ )
60 : {
61 759807 : IF( EQ_16( tmpIdx, pairIdx ) )
62 : {
63 82161 : hBlock->ch1 = ch1;
64 82161 : move16();
65 82161 : hBlock->ch2 = ch2;
66 82161 : move16();
67 :
68 82161 : return;
69 : }
70 : ELSE
71 : {
72 677646 : tmpIdx = add( tmpIdx, 1 );
73 : }
74 : }
75 : }
76 :
77 0 : return;
78 : }
79 :
80 :
81 : /*-------------------------------------------------------------------*
82 : * ivas_mct_dec_mct()
83 : *
84 : * decode core and mct information
85 : *-------------------------------------------------------------------*/
86 :
87 91423 : void ivas_mct_dec_mct_fx(
88 : MCT_DEC_HANDLE hMCT, /* i/o: MCT decoder structure */
89 : Decoder_State **sts, /* i/o: decoder state structure */
90 : const Word16 nchan /* i : number of channels */
91 : )
92 : {
93 : Word16 pair, ch, channelPairIndex;
94 : MCT_DEC_BLOCK_DATA_HANDLE hBlock;
95 : Decoder_State *p_st[CPE_CHANNELS];
96 91423 : Word16 nchan_active = 0;
97 91423 : move16();
98 :
99 91423 : hMCT->currBlockDataCnt = get_next_indice_fx( sts[0], MCT_NUM_BLOCK_DATA_BITS );
100 91423 : move16();
101 :
102 : /*first get core and overlap info for all channels*/
103 509763 : FOR( ch = 0; ch < nchan; ch++ )
104 : {
105 418340 : test();
106 418340 : IF( hMCT->currBlockDataCnt && NE_16( sts[ch]->mct_chan_mode, MCT_CHAN_MODE_IGNORE ) )
107 : {
108 247553 : hMCT->mc_global_ild[ch] = get_next_indice_fx( sts[0], SMDCT_GLOBAL_ILD_BITS );
109 247553 : move16();
110 : }
111 : ELSE
112 : {
113 170787 : hMCT->mc_global_ild[ch] = 0;
114 170787 : move16();
115 : }
116 : }
117 :
118 91423 : IF( hMCT->currBlockDataCnt )
119 : {
120 300797 : FOR( ch = 0; ch < nchan; ch++ )
121 : {
122 251203 : IF( NE_16( sts[ch]->mct_chan_mode, MCT_CHAN_MODE_IGNORE ) )
123 : {
124 247553 : hMCT->lowE_ch[ch] = get_next_indice_fx( sts[0], 1 );
125 247553 : move16();
126 : }
127 : }
128 : }
129 :
130 509763 : FOR( ch = 0; ch < nchan; ch++ )
131 : {
132 418340 : if ( NE_16( sts[ch]->mct_chan_mode, MCT_CHAN_MODE_IGNORE ) )
133 : {
134 407950 : nchan_active = add( nchan_active, 1 );
135 : }
136 : }
137 :
138 173584 : FOR( pair = hMCT->currBlockDataCnt - 1; pair >= 0; pair-- )
139 : {
140 82161 : assert( GE_16( nchan_active, 2 ) );
141 82161 : hBlock = hMCT->hBlockData[pair];
142 :
143 : /*get channel pair index from BS*/
144 82161 : channelPairIndex = get_next_indice_fx( sts[0], hMCT->bitsChannelPairIndex );
145 82161 : indexToChannelPair_fx( hBlock, nchan, channelPairIndex );
146 :
147 : /*point to decoder states of actual channels to read block pair bits*/
148 82161 : p_st[0] = sts[hBlock->ch1];
149 82161 : p_st[1] = sts[hBlock->ch2];
150 :
151 82161 : parse_stereo_from_bitstream( hBlock->hStereoMdct, p_st, 1, 0, sts[0], hBlock->mask );
152 : }
153 :
154 91423 : return;
155 : }
156 :
157 :
158 : /*----------------------------------------------------------*
159 : * applyGlobalILD()
160 : *
161 : * revert to initial channel energy levels using the ratios
162 : * sent from the encoder
163 : *----------------------------------------------------------*/
164 :
165 91423 : static void applyGlobalILD_fx(
166 : Decoder_State **sts,
167 : MCT_DEC_HANDLE hMCT,
168 : Word32 *x[MCT_MAX_CHANNELS][NB_DIV]
169 : #ifndef OPT_SBA_DEC_PATH
170 : ,
171 : Word16 q_x[MCT_MAX_CHANNELS]
172 : #endif /* OPT_SBA_DEC_PATH */
173 : )
174 : {
175 : Word16 ch, k;
176 : Word16 nSubframes, L_subframeTCX;
177 : Word32 qratio;
178 : Word16 q_qratio;
179 : Word16 tmp, tmp_e;
180 :
181 509763 : FOR( ch = 0; ch < ( hMCT->nchan_out_woLFE ); ch++ )
182 : {
183 418340 : IF( EQ_16( sts[ch]->core, TCX_20_CORE ) )
184 : {
185 409526 : nSubframes = 1;
186 409526 : move16();
187 : }
188 : ELSE
189 : {
190 8814 : nSubframes = NB_DIV;
191 8814 : move16();
192 : }
193 418340 : tmp = BASOP_Util_Divide1616_Scale( sts[ch]->hTcxDec->L_frameTCX, nSubframes, &tmp_e );
194 418340 : L_subframeTCX = shr( tmp, add( 15, negate( tmp_e ) ) );
195 :
196 418340 : IF( hMCT->mc_global_ild[ch] )
197 : {
198 164322 : IF( hMCT->lowE_ch[ch] )
199 : {
200 : /* qratio = (float) hMCT->mc_global_ild[ch] / SMDCT_ILD_RANGE; */
201 100862 : qratio = L_shl( hMCT->mc_global_ild[ch], Q26 - SMDCT_GLOBAL_ILD_BITS ); /* Q26 */
202 : }
203 : ELSE
204 : {
205 63460 : tmp = BASOP_Util_Divide3216_Scale( ( SMDCT_ILD_RANGE << Q26 ), hMCT->mc_global_ild[ch], &tmp_e );
206 63460 : qratio = L_shr( (Word32) tmp, negate( add( 1, tmp_e ) ) ); // Q26
207 : }
208 :
209 164322 : q_qratio = norm_l( qratio );
210 164322 : qratio = L_shl( qratio, q_qratio );
211 332042 : FOR( k = 0; k < nSubframes; k++ )
212 : {
213 167720 : v_multc_fixed( x[ch][k], qratio, x[ch][k], L_subframeTCX ); // Qx - 5 + q_qratio
214 : #ifdef OPT_SBA_DEC_PATH
215 167720 : scale_sig32( x[ch][k], L_subframeTCX, sub( 5, q_qratio ) );
216 : #endif /* OPT_SBA_DEC_PATH */
217 : }
218 : #ifndef OPT_SBA_DEC_PATH
219 : q_x[ch] = add( sub( q_x[ch], 5 ), q_qratio );
220 : move16();
221 : #endif /* OPT_SBA_DEC_PATH */
222 : }
223 : ELSE
224 : {
225 254018 : CONTINUE;
226 : }
227 : }
228 :
229 91423 : return;
230 : }
231 :
232 :
233 : /*----------------------------------------------------------*
234 : * apply_MCT_dec()
235 : *
236 : * main MCT decoding function
237 : *----------------------------------------------------------*/
238 :
239 91423 : void apply_MCT_dec_fx(
240 : MCT_DEC_HANDLE hMCT, /* i/o: MCT decoder structure */
241 : Decoder_State **sts, /* i/o: decoder state structure */
242 : Word32 *x[MCT_MAX_CHANNELS][NB_DIV] /* i/o: decoded and dequan. spect. input to MCT */
243 : #ifndef OPT_SBA_DEC_PATH
244 : ,
245 : Word16 q_x[MCT_MAX_CHANNELS]
246 : #endif /* OPT_SBA_DEC_PATH */
247 : )
248 : {
249 : Word16 pair;
250 : MCT_DEC_BLOCK_DATA_HANDLE hBlock;
251 :
252 173584 : FOR( pair = hMCT->currBlockDataCnt - 1; pair >= 0; pair-- )
253 : {
254 82161 : hBlock = hMCT->hBlockData[pair];
255 :
256 : #ifndef OPT_SBA_DEC_PATH
257 : stereo_decoder_tcx_fx( hBlock->hStereoMdct, hBlock->mask, &x[hBlock->ch2][0], &x[hBlock->ch1][0], &x[hBlock->ch2][0], hBlock->hStereoMdct->mdct_stereo_mode, sts[hBlock->ch1]->core, sts[hBlock->ch2]->core, sts[0]->igf, sts[0]->hTcxDec->L_frameTCX, sts[1]->hTcxDec->L_frameTCX, 1, TCX_20_CORE, TCX_20_CORE, 0, &q_x[hBlock->ch2], &q_x[hBlock->ch1] );
258 : #else /* OPT_SBA_DEC_PATH */
259 82161 : stereo_decoder_tcx_fx( hBlock->hStereoMdct, hBlock->mask, &x[hBlock->ch2][0], &x[hBlock->ch1][0], &x[hBlock->ch2][0], hBlock->hStereoMdct->mdct_stereo_mode, sts[hBlock->ch1]->core, sts[hBlock->ch2]->core, sts[0]->igf, sts[0]->hTcxDec->L_frameTCX, sts[1]->hTcxDec->L_frameTCX, 1, TCX_20_CORE, TCX_20_CORE, 0 );
260 : #endif /* OPT_SBA_DEC_PATH */
261 : }
262 :
263 : #ifndef OPT_SBA_DEC_PATH
264 : applyGlobalILD_fx( sts, hMCT, x, q_x );
265 : #else /* OPT_SBA_DEC_PATH */
266 91423 : applyGlobalILD_fx( sts, hMCT, x );
267 : #endif /* OPT_SBA_DEC_PATH */
268 :
269 91423 : return;
270 : }
271 :
272 : /*----------------------------------------------------------*
273 : * mctStereoIGF_dec()
274 : *
275 : * apply IGF to MCT stereo block pairs
276 : *----------------------------------------------------------*/
277 :
278 31321 : void mctStereoIGF_dec_fx(
279 : MCT_DEC_HANDLE hMCT, /* i/o: MCT decoder structure */
280 : Decoder_State **stm, /* i/o: decoder state structure */
281 : Word32 *x[MCT_MAX_CHANNELS][NB_DIV], /* i/o: decoded and dequantized spectrum i:Q12*/
282 : const Word16 bfi /* i : bad frame flag */
283 : )
284 : {
285 : Word16 b, core, nSubframes, k, ch, p_ch[CPE_CHANNELS], ch1, ch2;
286 31321 : Decoder_State *sts[CPE_CHANNELS], *st = NULL;
287 : Word16 tcx_offset[CPE_CHANNELS];
288 : Word16 tcx_offsetFB[CPE_CHANNELS];
289 : Word16 left_rect[CPE_CHANNELS];
290 : Word16 L_spec[CPE_CHANNELS];
291 : Word16 L_frame[CPE_CHANNELS];
292 : Word16 L_frameTCX[CPE_CHANNELS];
293 : Word32 *p_x[CPE_CHANNELS][NB_DIV]; // Q(31 - p_x_e)
294 : Word16 p_x_e[CPE_CHANNELS][NB_DIV];
295 : Word16 p_x_len[CPE_CHANNELS][NB_DIV];
296 : Word16 singleChEle[MCT_MAX_CHANNELS];
297 : Word16 tmp_e;
298 : Word16 L_frame_nSubframe, L_frameTCX_nSubframe, tmp;
299 :
300 :
301 31321 : set16_fx( singleChEle, 1, ( hMCT->nchan_out_woLFE ) );
302 :
303 85794 : FOR( b = 0; b < hMCT->currBlockDataCnt; b++ )
304 : {
305 54473 : ch1 = hMCT->hBlockData[b]->ch1;
306 54473 : move16();
307 54473 : ch2 = hMCT->hBlockData[b]->ch2;
308 54473 : move16();
309 :
310 54473 : sts[0] = stm[ch1];
311 54473 : sts[1] = stm[ch2];
312 54473 : core = sts[0]->core;
313 54473 : move16();
314 54473 : nSubframes = core;
315 54473 : move16();
316 54473 : p_ch[0] = ch1;
317 54473 : move16();
318 54473 : p_ch[1] = ch2;
319 54473 : move16();
320 54473 : singleChEle[hMCT->hBlockData[b]->ch1] = 0;
321 54473 : move16();
322 54473 : singleChEle[hMCT->hBlockData[b]->ch2] = 0;
323 54473 : move16();
324 :
325 : // Using input Q-factor as 12
326 54473 : set16_fx( p_x_e[0], 31 - Q12, CPE_CHANNELS ); // Q12
327 54473 : set16_fx( p_x_e[1], 31 - Q12, CPE_CHANNELS ); // Q12
328 :
329 110221 : FOR( k = 0; k < nSubframes; k++ )
330 : {
331 55748 : p_x[0][k] = x[ch1][k]; // Q12
332 55748 : p_x[1][k] = x[ch2][k]; // Q12
333 :
334 55748 : test();
335 55748 : IF( NE_16( hMCT->hBlockData[b]->hStereoMdct->IGFStereoMode[k], SMDCT_DUAL_MONO ) || NE_16( hMCT->hBlockData[b]->hStereoMdct->mdct_stereo_mode[k], SMDCT_DUAL_MONO ) )
336 55735 : {
337 : Word16 shr_div, shr_k;
338 :
339 55735 : assert( nSubframes == 1 || nSubframes == 2 );
340 : /* Note: nSubframes is in limited range [1, 2] for this function */
341 :
342 55735 : shr_div = sub( nSubframes, 1 ); /* 2 -> 1, 1 -> 0 */
343 55735 : L_spec[0] = shr( sts[0]->hTcxCfg->tcx_coded_lines, shr_div );
344 55735 : move16();
345 55735 : L_frame_nSubframe = shr( sts[0]->L_frame, shr_div );
346 55735 : L_frameTCX_nSubframe = shr( sts[0]->hTcxDec->L_frameTCX, shr_div );
347 :
348 55735 : init_tcx_info_fx( sts[0], L_frame_nSubframe, L_frameTCX_nSubframe, k, bfi, &tcx_offset[0], &tcx_offsetFB[0], &L_frame[0], &L_frameTCX[0], &left_rect[0], &L_spec[0] );
349 :
350 : /* stereo IGF decoding */
351 55735 : assert( ( EQ_16( sts[0]->core, sts[1]->core ) ) || ( ( hMCT->hBlockData[b]->hStereoMdct->mdct_stereo_mode[0] == SMDCT_DUAL_MONO ) ) );
352 :
353 55735 : decoder_tcx_IGF_stereo_fx( sts, hMCT->hBlockData[b]->hStereoMdct, hMCT->hBlockData[b]->mask, p_x, p_x_e, p_x_len, L_frame[0], left_rect[0], k, bfi, 1 /* MCT_flag */ );
354 :
355 : // Shifting output with variable exponent back to Q12
356 55735 : shr_k = sub( 31 - Q12, p_x_e[0][k] );
357 42541095 : FOR( Word16 i = 0; i < p_x_len[0][k]; i++ )
358 : {
359 42485360 : p_x[0][k][i] = L_shr( p_x[0][k][i], shr_k );
360 42485360 : move32();
361 : }
362 55735 : shr_k = sub( 31 - Q12, p_x_e[1][k] );
363 42541095 : FOR( Word16 i = 0; i < p_x_len[1][k]; i++ )
364 : {
365 42485360 : p_x[1][k][i] = L_shr( p_x[1][k][i], shr_k );
366 42485360 : move32();
367 : }
368 : }
369 : ELSE
370 : {
371 39 : FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
372 : {
373 : Word16 x_e, x_len;
374 :
375 26 : st = sts[ch];
376 26 : test();
377 26 : if ( bfi && ( st->core == ACELP_CORE ) ) /*no igf processing needed*/
378 : {
379 0 : CONTINUE;
380 : }
381 26 : tmp = BASOP_Util_Divide1616_Scale( st->hTcxCfg->tcx_coded_lines, nSubframes, &tmp_e );
382 26 : L_spec[ch] = shr( tmp, add( 15, negate( tmp_e ) ) );
383 26 : move32();
384 :
385 26 : tmp = BASOP_Util_Divide1616_Scale( st->L_frame, nSubframes, &tmp_e );
386 26 : L_frame_nSubframe = shr( tmp, add( 15, negate( tmp_e ) ) );
387 :
388 26 : tmp = BASOP_Util_Divide1616_Scale( st->hTcxDec->L_frameTCX, nSubframes, &tmp_e );
389 26 : L_frameTCX_nSubframe = shr( tmp, add( 15, negate( tmp_e ) ) );
390 :
391 26 : init_tcx_info_fx( st, L_frame_nSubframe, L_frameTCX_nSubframe, k, bfi, &tcx_offset[ch], &tcx_offsetFB[ch], &L_frame[ch], &L_frameTCX[ch], &left_rect[ch], &L_spec[ch] );
392 :
393 : /* mono or dual mono IGF decoding */
394 :
395 26 : x_e = 31 - Q12; // input q-factor of x[p_ch[ch]][k] is Q12
396 26 : move16();
397 :
398 26 : decoder_tcx_IGF_mono_fx( st, x[p_ch[ch]][k], &x_e, &x_len, L_frame[ch], left_rect[ch], bfi, k );
399 :
400 10426 : FOR( Word16 i = 0; i < x_len; i++ )
401 : {
402 : // Converting from variable exponent to Fixed q-factor (Q12)
403 10400 : x[p_ch[ch]][k][i] = L_shr( x[p_ch[ch]][k][i], sub( 31 - Q12, x_e ) );
404 10400 : move32();
405 : }
406 : }
407 : }
408 : }
409 : }
410 :
411 31321 : IF( sum16_fx( singleChEle, ( hMCT->nchan_out_woLFE ) ) != 0 )
412 : {
413 177634 : FOR( ch = 0; ch < ( hMCT->nchan_out_woLFE ); ch++ )
414 : {
415 146317 : IF( singleChEle[ch] )
416 : {
417 37387 : st = stm[ch];
418 37387 : test();
419 37387 : if ( bfi && ( st->core == ACELP_CORE ) ) /*no igf processing needed*/
420 : {
421 0 : CONTINUE;
422 : }
423 37387 : if ( EQ_16( st->mct_chan_mode, MCT_CHAN_MODE_IGNORE ) )
424 : {
425 1717 : CONTINUE;
426 : }
427 :
428 35670 : IF( EQ_16( st->core, TCX_10_CORE ) )
429 : {
430 1127 : nSubframes = NB_DIV;
431 : }
432 : ELSE
433 : {
434 34543 : nSubframes = 1;
435 : }
436 35670 : move16();
437 :
438 72467 : FOR( k = 0; k < nSubframes; k++ )
439 : {
440 : Word16 x_e, x_len;
441 :
442 36797 : tmp = BASOP_Util_Divide1616_Scale( st->hTcxCfg->tcx_coded_lines, nSubframes, &tmp_e );
443 36797 : L_spec[0] = shr( tmp, add( 15, negate( tmp_e ) ) );
444 36797 : move32();
445 :
446 36797 : tmp = BASOP_Util_Divide1616_Scale( st->L_frame, nSubframes, &tmp_e );
447 36797 : L_frame_nSubframe = shr( tmp, add( 15, negate( tmp_e ) ) );
448 :
449 36797 : tmp = BASOP_Util_Divide1616_Scale( st->hTcxDec->L_frameTCX, nSubframes, &tmp_e );
450 36797 : L_frameTCX_nSubframe = shr( tmp, add( 15, negate( tmp_e ) ) );
451 :
452 36797 : init_tcx_info_fx( st, L_frame_nSubframe, L_frameTCX_nSubframe, k, bfi, &tcx_offset[0], &tcx_offsetFB[0], &L_frame[0], &L_frameTCX[0], &left_rect[0], &L_spec[0] );
453 :
454 : /* mono or dual mono IGF decoding */
455 36797 : x_e = 31 - Q12; // Input Q-factor is Q12.
456 36797 : move16();
457 :
458 36797 : decoder_tcx_IGF_mono_fx( st, x[ch][k], &x_e, &x_len, L_frame[0], left_rect[0], bfi, k );
459 :
460 27614237 : FOR( Word16 i = 0; i < x_len; i++ )
461 : {
462 : // Converting from variable exponent to Fixed q-factor (Q12)
463 27577440 : x[ch][k][i] = L_shr( x[ch][k][i], sub( 31 - Q12, x_e ) );
464 27577440 : move32();
465 : }
466 : }
467 : }
468 : }
469 : }
470 :
471 31321 : return;
472 : }
|