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 : Word16 q_x[MCT_MAX_CHANNELS] )
170 : {
171 : Word16 ch, k;
172 : Word16 nSubframes, L_subframeTCX;
173 : Word32 qratio;
174 : Word16 q_qratio;
175 : Word16 tmp, tmp_e;
176 :
177 509763 : FOR( ch = 0; ch < ( hMCT->nchan_out_woLFE ); ch++ )
178 : {
179 418340 : IF( EQ_16( sts[ch]->core, TCX_20_CORE ) )
180 : {
181 409526 : nSubframes = 1;
182 409526 : move16();
183 : }
184 : ELSE
185 : {
186 8814 : nSubframes = NB_DIV;
187 8814 : move16();
188 : }
189 418340 : tmp = BASOP_Util_Divide1616_Scale( sts[ch]->hTcxDec->L_frameTCX, nSubframes, &tmp_e );
190 418340 : L_subframeTCX = shr( tmp, add( 15, negate( tmp_e ) ) );
191 :
192 418340 : IF( hMCT->mc_global_ild[ch] )
193 : {
194 164322 : IF( hMCT->lowE_ch[ch] )
195 : {
196 : /* qratio = (float) hMCT->mc_global_ild[ch] / SMDCT_ILD_RANGE; */
197 100862 : qratio = L_shl( hMCT->mc_global_ild[ch], Q26 - SMDCT_GLOBAL_ILD_BITS ); /* Q26 */
198 : }
199 : ELSE
200 : {
201 63460 : tmp = BASOP_Util_Divide3216_Scale( ( SMDCT_ILD_RANGE << Q26 ), hMCT->mc_global_ild[ch], &tmp_e );
202 63460 : qratio = L_shr( (Word32) tmp, negate( add( 1, tmp_e ) ) ); // Q26
203 : }
204 :
205 164322 : q_qratio = norm_l( qratio );
206 164322 : qratio = L_shl( qratio, q_qratio );
207 332042 : FOR( k = 0; k < nSubframes; k++ )
208 : {
209 167720 : v_multc_fixed( x[ch][k], qratio, x[ch][k], L_subframeTCX ); // Qx - 5 + q_qratio
210 : }
211 164322 : q_x[ch] = add( sub( q_x[ch], 5 ), q_qratio );
212 164322 : move16();
213 : }
214 : ELSE
215 : {
216 254018 : CONTINUE;
217 : }
218 : }
219 :
220 91423 : return;
221 : }
222 :
223 :
224 : /*----------------------------------------------------------*
225 : * apply_MCT_dec()
226 : *
227 : * main MCT decoding function
228 : *----------------------------------------------------------*/
229 :
230 91423 : void apply_MCT_dec_fx(
231 : MCT_DEC_HANDLE hMCT, /* i/o: MCT decoder structure */
232 : Decoder_State **sts, /* i/o: decoder state structure */
233 : Word32 *x[MCT_MAX_CHANNELS][NB_DIV], /* i/o: decoded and dequan. spect. input to MCT */
234 : Word16 q_x[MCT_MAX_CHANNELS] )
235 : {
236 : Word16 pair;
237 : MCT_DEC_BLOCK_DATA_HANDLE hBlock;
238 :
239 173584 : FOR( pair = hMCT->currBlockDataCnt - 1; pair >= 0; pair-- )
240 : {
241 82161 : hBlock = hMCT->hBlockData[pair];
242 :
243 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, &q_x[hBlock->ch2], &q_x[hBlock->ch1] );
244 : }
245 :
246 91423 : applyGlobalILD_fx( sts, hMCT, x, q_x );
247 :
248 91423 : return;
249 : }
250 :
251 : /*----------------------------------------------------------*
252 : * mctStereoIGF_dec()
253 : *
254 : * apply IGF to MCT stereo block pairs
255 : *----------------------------------------------------------*/
256 :
257 31321 : void mctStereoIGF_dec_fx(
258 : MCT_DEC_HANDLE hMCT, /* i/o: MCT decoder structure */
259 : Decoder_State **stm, /* i/o: decoder state structure */
260 : Word32 *x[MCT_MAX_CHANNELS][NB_DIV], /* i/o: decoded and dequantized spectrum i:Q12*/
261 : const Word16 bfi /* i : bad frame flag */
262 : )
263 : {
264 : Word16 b, core, nSubframes, k, ch, p_ch[CPE_CHANNELS], ch1, ch2;
265 31321 : Decoder_State *sts[CPE_CHANNELS], *st = NULL;
266 : Word16 tcx_offset[CPE_CHANNELS];
267 : Word16 tcx_offsetFB[CPE_CHANNELS];
268 : Word16 left_rect[CPE_CHANNELS];
269 : Word16 L_spec[CPE_CHANNELS];
270 : Word16 L_frame[CPE_CHANNELS];
271 : Word16 L_frameTCX[CPE_CHANNELS];
272 : Word32 *p_x[CPE_CHANNELS][NB_DIV]; // Q(31 - p_x_e)
273 : Word16 p_x_e[CPE_CHANNELS][NB_DIV];
274 : Word16 p_x_len[CPE_CHANNELS][NB_DIV];
275 : Word16 singleChEle[MCT_MAX_CHANNELS];
276 : Word16 tmp_e;
277 : Word16 L_frame_nSubframe, L_frameTCX_nSubframe, tmp;
278 :
279 :
280 31321 : set16_fx( singleChEle, 1, ( hMCT->nchan_out_woLFE ) );
281 :
282 85794 : FOR( b = 0; b < hMCT->currBlockDataCnt; b++ )
283 : {
284 54473 : ch1 = hMCT->hBlockData[b]->ch1;
285 54473 : move16();
286 54473 : ch2 = hMCT->hBlockData[b]->ch2;
287 54473 : move16();
288 :
289 54473 : sts[0] = stm[ch1];
290 54473 : sts[1] = stm[ch2];
291 54473 : core = sts[0]->core;
292 54473 : move16();
293 54473 : nSubframes = core;
294 54473 : move16();
295 54473 : p_ch[0] = ch1;
296 54473 : move16();
297 54473 : p_ch[1] = ch2;
298 54473 : move16();
299 54473 : singleChEle[hMCT->hBlockData[b]->ch1] = 0;
300 54473 : move16();
301 54473 : singleChEle[hMCT->hBlockData[b]->ch2] = 0;
302 54473 : move16();
303 :
304 : // Using input Q-factor as 12
305 54473 : set16_fx( p_x_e[0], 31 - Q12, CPE_CHANNELS ); // Q12
306 54473 : set16_fx( p_x_e[1], 31 - Q12, CPE_CHANNELS ); // Q12
307 :
308 110221 : FOR( k = 0; k < nSubframes; k++ )
309 : {
310 55748 : p_x[0][k] = x[ch1][k]; // Q12
311 55748 : p_x[1][k] = x[ch2][k]; // Q12
312 :
313 55748 : test();
314 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 ) )
315 55735 : {
316 : Word16 shr_div, shr_k;
317 :
318 55735 : assert( nSubframes == 1 || nSubframes == 2 );
319 : /* Note: nSubframes is in limited range [1, 2] for this function */
320 :
321 55735 : shr_div = sub( nSubframes, 1 ); /* 2 -> 1, 1 -> 0 */
322 55735 : L_spec[0] = shr( sts[0]->hTcxCfg->tcx_coded_lines, shr_div );
323 55735 : move16();
324 55735 : L_frame_nSubframe = shr( sts[0]->L_frame, shr_div );
325 55735 : L_frameTCX_nSubframe = shr( sts[0]->hTcxDec->L_frameTCX, shr_div );
326 :
327 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] );
328 :
329 : /* stereo IGF decoding */
330 55735 : assert( ( EQ_16( sts[0]->core, sts[1]->core ) ) || ( ( hMCT->hBlockData[b]->hStereoMdct->mdct_stereo_mode[0] == SMDCT_DUAL_MONO ) ) );
331 :
332 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 */ );
333 :
334 : // Shifting output with variable exponent back to Q12
335 55735 : shr_k = sub( 31 - Q12, p_x_e[0][k] );
336 42541095 : FOR( Word16 i = 0; i < p_x_len[0][k]; i++ )
337 : {
338 42485360 : p_x[0][k][i] = L_shr( p_x[0][k][i], shr_k );
339 42485360 : move32();
340 : }
341 55735 : shr_k = sub( 31 - Q12, p_x_e[1][k] );
342 42541095 : FOR( Word16 i = 0; i < p_x_len[1][k]; i++ )
343 : {
344 42485360 : p_x[1][k][i] = L_shr( p_x[1][k][i], shr_k );
345 42485360 : move32();
346 : }
347 : }
348 : ELSE
349 : {
350 39 : FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
351 : {
352 : Word16 x_e, x_len;
353 :
354 26 : st = sts[ch];
355 26 : test();
356 26 : if ( bfi && ( st->core == ACELP_CORE ) ) /*no igf processing needed*/
357 : {
358 0 : CONTINUE;
359 : }
360 26 : tmp = BASOP_Util_Divide1616_Scale( st->hTcxCfg->tcx_coded_lines, nSubframes, &tmp_e );
361 26 : L_spec[ch] = shr( tmp, add( 15, negate( tmp_e ) ) );
362 26 : move32();
363 :
364 26 : tmp = BASOP_Util_Divide1616_Scale( st->L_frame, nSubframes, &tmp_e );
365 26 : L_frame_nSubframe = shr( tmp, add( 15, negate( tmp_e ) ) );
366 :
367 26 : tmp = BASOP_Util_Divide1616_Scale( st->hTcxDec->L_frameTCX, nSubframes, &tmp_e );
368 26 : L_frameTCX_nSubframe = shr( tmp, add( 15, negate( tmp_e ) ) );
369 :
370 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] );
371 :
372 : /* mono or dual mono IGF decoding */
373 :
374 26 : x_e = 31 - Q12; // input q-factor of x[p_ch[ch]][k] is Q12
375 26 : move16();
376 :
377 26 : decoder_tcx_IGF_mono_fx( st, x[p_ch[ch]][k], &x_e, &x_len, L_frame[ch], left_rect[ch], bfi, k );
378 :
379 10426 : FOR( Word16 i = 0; i < x_len; i++ )
380 : {
381 : // Converting from variable exponent to Fixed q-factor (Q12)
382 10400 : x[p_ch[ch]][k][i] = L_shr( x[p_ch[ch]][k][i], sub( 31 - Q12, x_e ) );
383 10400 : move32();
384 : }
385 : }
386 : }
387 : }
388 : }
389 :
390 31321 : IF( sum16_fx( singleChEle, ( hMCT->nchan_out_woLFE ) ) != 0 )
391 : {
392 177634 : FOR( ch = 0; ch < ( hMCT->nchan_out_woLFE ); ch++ )
393 : {
394 146317 : IF( singleChEle[ch] )
395 : {
396 37387 : st = stm[ch];
397 37387 : test();
398 37387 : if ( bfi && ( st->core == ACELP_CORE ) ) /*no igf processing needed*/
399 : {
400 0 : CONTINUE;
401 : }
402 37387 : if ( EQ_16( st->mct_chan_mode, MCT_CHAN_MODE_IGNORE ) )
403 : {
404 1717 : CONTINUE;
405 : }
406 :
407 35670 : IF( EQ_16( st->core, TCX_10_CORE ) )
408 : {
409 1127 : nSubframes = NB_DIV;
410 : }
411 : ELSE
412 : {
413 34543 : nSubframes = 1;
414 : }
415 35670 : move16();
416 :
417 72467 : FOR( k = 0; k < nSubframes; k++ )
418 : {
419 : Word16 x_e, x_len;
420 :
421 36797 : tmp = BASOP_Util_Divide1616_Scale( st->hTcxCfg->tcx_coded_lines, nSubframes, &tmp_e );
422 36797 : L_spec[0] = shr( tmp, add( 15, negate( tmp_e ) ) );
423 36797 : move32();
424 :
425 36797 : tmp = BASOP_Util_Divide1616_Scale( st->L_frame, nSubframes, &tmp_e );
426 36797 : L_frame_nSubframe = shr( tmp, add( 15, negate( tmp_e ) ) );
427 :
428 36797 : tmp = BASOP_Util_Divide1616_Scale( st->hTcxDec->L_frameTCX, nSubframes, &tmp_e );
429 36797 : L_frameTCX_nSubframe = shr( tmp, add( 15, negate( tmp_e ) ) );
430 :
431 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] );
432 :
433 : /* mono or dual mono IGF decoding */
434 36797 : x_e = 31 - Q12; // Input Q-factor is Q12.
435 36797 : move16();
436 :
437 36797 : decoder_tcx_IGF_mono_fx( st, x[ch][k], &x_e, &x_len, L_frame[0], left_rect[0], bfi, k );
438 :
439 27614237 : FOR( Word16 i = 0; i < x_len; i++ )
440 : {
441 : // Converting from variable exponent to Fixed q-factor (Q12)
442 27577440 : x[ch][k][i] = L_shr( x[ch][k][i], sub( 31 - Q12, x_e ) );
443 27577440 : move32();
444 : }
445 : }
446 : }
447 : }
448 : }
449 :
450 31321 : return;
451 : }
|