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 <assert.h>
34 : #include <stdint.h>
35 : #include "options.h"
36 : #include <math.h>
37 : #include "ivas_cnst.h"
38 : #include "ivas_prot_fx.h"
39 : #include "prot_fx.h"
40 : #include "prot_fx_enc.h"
41 : #include "ivas_rom_com.h"
42 : #include "ivas_rom_com_fx.h"
43 : #include "ivas_rom_enc.h"
44 : #include "wmc_auto.h"
45 : #include "stat_enc.h"
46 :
47 :
48 : /*-------------------------------------------------------------------*
49 : * Local constants
50 : *-------------------------------------------------------------------*/
51 :
52 : #define OFFSET_BITS_TCX20 126
53 : #define OFFSET_BITS_TCX10 222
54 :
55 :
56 : /*-------------------------------------------------------------------*
57 : * Local function prototypes
58 : *-------------------------------------------------------------------*/
59 :
60 : static void convertToBwMS_fx( const Word16 startLine, const Word16 stopLine, Word32 x0[], Word32 x1[], const Word32 norm_fac );
61 :
62 : void convertToMS_fx( const Word16 L_frame, Word32 x0_fx[], Word32 x1_fx[], const Word32 norm_fac );
63 :
64 : static Word16 GetChannelEnergyRatio_fx( Encoder_State **st, const Word16 iFirstSubframe, const Word16 iLastSubframe, const UWord8 ratioInRmsDomain );
65 :
66 : static void MsStereoDecision_fx( STEREO_MDCT_BAND_PARAMETERS *sfbParam, Word32 *specL_fx, Word32 *specR_fx, Word32 *specM_fx, Word32 *specS_fx, Word16 q_spec, Word16 *mdct_stereo_mode, Word16 *msMask, const Word16 nBitsAvailable );
67 :
68 :
69 : /*-------------------------------------------------------------------*
70 : * dft_ana_init()
71 : *
72 : * Initialization function for dft analysis handle within
73 : * MDCT-stereo
74 : *-------------------------------------------------------------------*/
75 :
76 151 : static void dft_ana_init_fx(
77 : DFT_ANA_HANDLE hDft_ana, /*i : DFT analysis handle */
78 : const Word32 input_Fs /*i : Input sampling frequency Q0*/
79 : )
80 : {
81 151 : hDft_ana->N = extract_l( Mpy_32_16_1( input_Fs, 656 /* STEREO_DFT_HOP_MAX_ENC / 48000 in Q15 */ ) );
82 151 : move16();
83 151 : hDft_ana->NFFT = extract_l( Mpy_32_16_1( input_Fs, 1311 /* STEREO_DFT_N_MAX_ENC / 48000 in Q15 */ ) );
84 151 : move16();
85 151 : hDft_ana->dft_ovl = extract_l( Mpy_32_16_1( input_Fs, 287 /* STEREO_DFT_OVL_MAX / 48000 in Q15 */ ) );
86 151 : move16();
87 151 : hDft_ana->dft_zp = extract_l( Mpy_32_16_1( input_Fs, 185 /* STEREO_DFT_ZP_MAX_ENC / 48000 in Q15 */ ) );
88 151 : move16();
89 :
90 151 : hDft_ana->dft_trigo_32k_fx = dft_trigo_32k_fx; /* Q15 */
91 :
92 151 : IF( EQ_32( input_Fs, 16000 ) )
93 : {
94 5 : hDft_ana->dft_trigo_fx = dft_trigo_32k_fx; /* Q15 */
95 5 : hDft_ana->dft_trigo_step = STEREO_DFT_TRIGO_SRATE_16k_STEP;
96 5 : move16();
97 5 : hDft_ana->win_ana_fx = win_ana_16k_fx; /* Q15 */
98 : }
99 146 : ELSE IF( EQ_32( input_Fs, 32000 ) )
100 : {
101 59 : hDft_ana->dft_trigo_fx = dft_trigo_32k_fx; /* Q15 */
102 59 : hDft_ana->dft_trigo_step = STEREO_DFT_TRIGO_SRATE_32k_STEP;
103 59 : move16();
104 59 : hDft_ana->win_ana_fx = win_ana_32k_fx; /* Q15 */
105 : }
106 : ELSE
107 : {
108 87 : assert( input_Fs == 48000 );
109 87 : hDft_ana->dft_trigo_fx = dft_trigo_48k_fx; /* Q15 */
110 87 : hDft_ana->dft_trigo_step = STEREO_DFT_TRIGO_SRATE_48k_STEP;
111 87 : move16();
112 87 : hDft_ana->win_ana_fx = win_ana_48k_fx; /* Q15 */
113 : }
114 :
115 151 : return;
116 : }
117 :
118 :
119 : /*-------------------------------------------------------------------*
120 : * write_itd_data()
121 : *
122 : * Bitstream writing of ITDs
123 : *-------------------------------------------------------------------*/
124 :
125 11753 : static void write_itd_data_fx(
126 : ITD_DATA_HANDLE hItd, /* i : ITD data handle */
127 : BSTR_ENC_HANDLE hBstr /* i/o: bitstream handle */
128 : )
129 : {
130 : Word16 k_offset;
131 : Word16 itd;
132 :
133 11753 : k_offset = 1;
134 11753 : move16();
135 :
136 11753 : push_next_indice( hBstr, ( hItd->itd_fx[k_offset] != 0 ), STEREO_DFT_ITD_MODE_NBITS );
137 :
138 11753 : IF( hItd->itd_fx[k_offset] != 0 )
139 : {
140 3517 : itd = hItd->itd_index[k_offset]; /* Q0 */
141 3517 : IF( GT_16( itd, 255 ) )
142 : {
143 2375 : itd = sub( itd, 256 );
144 :
145 2375 : IF( LT_16( itd, 20 ) )
146 : {
147 1604 : push_next_indice( hBstr, 1, 1 ); /* use Huffman*/
148 1604 : push_next_indice( hBstr, 1, 1 ); /* negative */
149 1604 : push_next_indice( hBstr, dft_code_itd[itd], dft_len_itd[itd] );
150 : }
151 : ELSE
152 : {
153 771 : push_next_indice( hBstr, 0, 1 ); /* don't use Huffman */
154 771 : push_next_indice( hBstr, 1, 1 ); /* negative */
155 771 : push_next_indice( hBstr, itd, STEREO_DFT_ITD_NBITS - 1 );
156 : }
157 : }
158 : ELSE
159 : {
160 1142 : IF( LT_16( itd, 20 ) )
161 : {
162 870 : push_next_indice( hBstr, 1, 1 ); /* use Huffman*/
163 870 : push_next_indice( hBstr, 0, 1 ); /* positive */
164 870 : push_next_indice( hBstr, dft_code_itd[itd], dft_len_itd[itd] );
165 : }
166 : ELSE
167 : {
168 : /* don't use Huffman and positive*/
169 272 : push_next_indice( hBstr, itd, STEREO_DFT_ITD_NBITS + 1 );
170 : }
171 : }
172 : }
173 11753 : return;
174 : }
175 :
176 : /*-------------------------------------------------------------------*
177 : * stereo_coder_tcx()
178 : *
179 : *
180 : *-------------------------------------------------------------------*/
181 :
182 136188 : void stereo_coder_tcx_fx(
183 : STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct, /* i/o: Stereo MDCT encoder structure */
184 : Encoder_State **sts, /* i/o: encoder state structure */
185 : Word16 ms_mask[NB_DIV][MAX_SFB], /* i : bandwise MS mask Q0*/
186 : Word32 *mdst_spectrum_fx[CPE_CHANNELS][NB_DIV],
187 : /* i/o: MDST spectrum */ // Q( q_spec ) (same as spectrum buffer in hTcxEnc)
188 : Word32 *inv_spectrum_fx[CPE_CHANNELS][NB_DIV],
189 : /* i/o: inverse spectrum */ // Q( q_spec ) (same as spectrum buffer in hTcxEnc)
190 : Word32 *inv_mdst_spectrum_fx[CPE_CHANNELS][NB_DIV],
191 : /* i/o: inverse MDST spectrum */ // Q( q_spec ) (same as spectrum buffer in hTcxEnc)
192 : const Word16 mct_on, /* i : flag mct block (1) or stereo (0) Q0*/
193 : Word16 exp_spec,
194 : Word16 exp_mdct_spec )
195 : {
196 136188 : STEREO_MDCT_BAND_PARAMETERS *sfbConf = NULL;
197 : Word32 nrgRatio_fx[CPE_CHANNELS];
198 : Word16 nrgRatio_e[CPE_CHANNELS];
199 : Word16 nonQNrgRatio_fx[CPE_CHANNELS]; // Q15
200 : Word16 k;
201 : Word16 nSubframes, L_frameTCX;
202 : Word16 nAvailBitsMS[NB_DIV];
203 : Word16 tmp, e_tmp;
204 136188 : push_wmops( "stereo_coder_tcx" );
205 :
206 :
207 136188 : set16_fx( nAvailBitsMS, 0, NB_DIV );
208 :
209 136188 : nSubframes = ( sts[0]->core == TCX_20_CORE && sts[1]->core == TCX_20_CORE ) ? 1 : NB_DIV;
210 136188 : L_frameTCX = idiv1616( sts[0]->hTcxEnc->L_frameTCX, nSubframes );
211 :
212 136188 : set16_fx( &ms_mask[0][0], 0, MAX_SFB );
213 136188 : set16_fx( &ms_mask[1][0], 0, MAX_SFB );
214 :
215 136188 : IF( !mct_on )
216 : {
217 42498 : IF( EQ_16( sts[0]->core, sts[1]->core ) )
218 : {
219 85600 : FOR( k = 0; k < nSubframes; k++ )
220 : {
221 43364 : nonQNrgRatio_fx[k] = GetChannelEnergyRatio_fx( sts, k, k, 1 ); // Q15
222 43364 : move16();
223 :
224 43364 : hStereoMdct->global_ild[k] = s_max( 1, s_min( SMDCT_ILD_RANGE - 1, round_fx( L_mult( SMDCT_ILD_RANGE, nonQNrgRatio_fx[k] ) ) ) );
225 43364 : move16();
226 43364 : nrgRatio_fx[k] = L_deposit_h( BASOP_Util_Divide1616_Scale( sub( SMDCT_ILD_RANGE, hStereoMdct->global_ild[k] ), hStereoMdct->global_ild[k], &nrgRatio_e[k] ) ); /* Q31-nrgRatio_e[k] */
227 43364 : move32();
228 : /*nonQNrgRatio[k] = max( 0.5f / SMDCT_ILD_RANGE, min( ( SMDCT_ILD_RANGE - 0.5f ) / SMDCT_ILD_RANGE, nonQNrgRatio[k] ) );
229 : nonQNrgRatio[k] = 1.0f / nonQNrgRatio[k] - 1.0f;*/
230 : // Not getting used further
231 : }
232 : }
233 : ELSE
234 : {
235 262 : nonQNrgRatio_fx[0] = nonQNrgRatio_fx[1] = GetChannelEnergyRatio_fx( sts, 0, nSubframes - 1, 1 ); /* Q15 */
236 262 : move16();
237 262 : hStereoMdct->global_ild[0] = s_max( 1, s_min( SMDCT_ILD_RANGE - 1, round_fx( L_mult( SMDCT_ILD_RANGE, nonQNrgRatio_fx[0] ) ) ) );
238 262 : move16();
239 262 : nrgRatio_fx[0] = nrgRatio_fx[1] = L_deposit_h( BASOP_Util_Divide1616_Scale( sub( SMDCT_ILD_RANGE, hStereoMdct->global_ild[0] ), hStereoMdct->global_ild[0], &nrgRatio_e[0] ) ); /* nrgRatio = nrg[1]/nrg[0] (Q31-nrgRatio_e[0])*/
240 262 : move32();
241 262 : nrgRatio_e[1] = nrgRatio_e[0];
242 262 : move16();
243 : /*nonQNrgRatio[0] = nonQNrgRatio[1] = max( 0.5f / SMDCT_ILD_RANGE, min( ( SMDCT_ILD_RANGE - 0.5f ) / SMDCT_ILD_RANGE, nonQNrgRatio[0] ) );
244 : nonQNrgRatio[0] = nonQNrgRatio[1] = 1.0f / nonQNrgRatio[0] - 1.0f;*/
245 : // Not getting used further
246 : }
247 :
248 86386 : FOR( k = 0; k < nSubframes; k++ )
249 : {
250 43888 : IF( EQ_16( BASOP_Util_Cmp_Mant32Exp( nrgRatio_fx[k], nrgRatio_e[k], ONE_IN_Q27, 4 ), 1 ) && LT_16( k, ( EQ_16( sts[1]->core, TCX_20_CORE ) ? 1 : NB_DIV ) ) )
251 : {
252 21322 : L_frameTCX = add( sts[1]->hTcxEnc->L_frameTCX, ( ( sts[1]->last_core == 0 ) ? shr( sts[1]->hTcxEnc->L_frameTCX, 2 ) : 0 ) ); /* Q0 */
253 21322 : L_frameTCX = idiv1616( L_frameTCX, ( sts[1]->core == TCX_20_CORE ) ? 1 : NB_DIV ); /* Q0 */
254 :
255 21322 : e_tmp = nrgRatio_e[k];
256 21322 : tmp = Inv16( extract_h( nrgRatio_fx[k] ), &e_tmp ); /* Q15-e_tmp */
257 21322 : tmp = shl( tmp, e_tmp ); /* Q15 */
258 :
259 21322 : v_multc_fixed_16( sts[1]->hTcxEnc->spectrum_fx[k], tmp, sts[1]->hTcxEnc->spectrum_fx[k], L_frameTCX ); /* exp: exp_spec */
260 21322 : v_multc_fixed_16( mdst_spectrum_fx[1][k], tmp, mdst_spectrum_fx[1][k], L_frameTCX ); /* exp: exp_mdct_spec */
261 : }
262 22566 : ELSE IF( EQ_16( BASOP_Util_Cmp_Mant32Exp( nrgRatio_fx[k], nrgRatio_e[k], ONE_IN_Q27, 4 ), -1 ) && LT_16( k, EQ_16( sts[0]->core, TCX_20_CORE ) ? 1 : NB_DIV ) )
263 : {
264 9502 : L_frameTCX = add( sts[0]->hTcxEnc->L_frameTCX, EQ_16( sts[0]->last_core, 0 ) ? shr( sts[0]->hTcxEnc->L_frameTCX, 2 ) : 0 ); /* Q0 */
265 9502 : L_frameTCX = idiv1616( L_frameTCX, ( sts[0]->core == TCX_20_CORE ) ? 1 : NB_DIV ); /* Q0 */
266 :
267 : /* This operation is resulting in some high MLDs in fixed point. */
268 9502 : v_multc_fixed_16( sts[0]->hTcxEnc->spectrum_fx[k], extract_l( L_shr( nrgRatio_fx[k], 16 - nrgRatio_e[k] ) ), sts[0]->hTcxEnc->spectrum_fx[k], L_frameTCX ); /* exp: exp_spec */
269 9502 : v_multc_fixed_16( mdst_spectrum_fx[0][k], extract_l( L_shr( nrgRatio_fx[k], 16 - nrgRatio_e[k] ) ), mdst_spectrum_fx[0][k], L_frameTCX ); /* exp: exp_mdct_spec */
270 : }
271 : }
272 : }
273 :
274 : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
275 : IF( hStereoMdct->fDualMono ||
276 : ( NE_16( sts[0]->hTcxEnc->transform_type[0], sts[1]->hTcxEnc->transform_type[0] ) ) || NE_16( sts[0]->hTcxEnc->transform_type[1], sts[1]->hTcxEnc->transform_type[1] ) || ( NE_16( sts[0]->last_core, sts[1]->last_core ) && ( EQ_16( sts[0]->last_core, ACELP_CORE ) || EQ_16( sts[1]->last_core, ACELP_CORE ) ) ) || EQ_16( sts[0]->last_core, ACELP_CORE ) || EQ_16( sts[1]->last_core, ACELP_CORE ) )
277 : #else
278 136188 : IF( ( NE_16( sts[0]->hTcxEnc->transform_type[0], sts[1]->hTcxEnc->transform_type[0] ) ) || NE_16( sts[0]->hTcxEnc->transform_type[1], sts[1]->hTcxEnc->transform_type[1] ) || ( NE_16( sts[0]->last_core, sts[1]->last_core ) && ( EQ_16( sts[0]->last_core, ACELP_CORE ) || EQ_16( sts[1]->last_core, ACELP_CORE ) ) ) || EQ_16( sts[0]->last_core, ACELP_CORE ) || EQ_16( sts[1]->last_core, ACELP_CORE ) )
279 : #endif
280 : {
281 927 : hStereoMdct->mdct_stereo_mode[0] = SMDCT_DUAL_MONO;
282 927 : move16();
283 927 : hStereoMdct->mdct_stereo_mode[1] = SMDCT_DUAL_MONO;
284 927 : move16();
285 :
286 :
287 927 : IF( sts[0]->igf )
288 : {
289 658 : hStereoMdct->IGFStereoMode[0] = SMDCT_DUAL_MONO;
290 658 : move16();
291 658 : hStereoMdct->IGFStereoMode[1] = SMDCT_DUAL_MONO;
292 658 : move16();
293 : }
294 927 : hStereoMdct->sw_uncorr = 1;
295 927 : move16();
296 :
297 927 : pop_wmops();
298 927 : return;
299 : }
300 : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
301 : else if ( hStereoMdct->fMSstereo )
302 : {
303 : hStereoMdct->mdct_stereo_mode[0] = SMDCT_MS_FULL;
304 : hStereoMdct->mdct_stereo_mode[1] = SMDCT_MS_FULL;
305 :
306 : if ( sts[0]->igf )
307 : {
308 : hStereoMdct->IGFStereoMode[0] = SMDCT_MS_FULL;
309 : hStereoMdct->IGFStereoMode[1] = SMDCT_MS_FULL;
310 : }
311 : for ( k = 0; k < nSubframes; k++ )
312 : {
313 : convertToMS_fx( L_frameTCX, sts[0]->hTcxEnc->spectrum_fx[k], sts[1]->hTcxEnc->spectrum_fx[k], SQRT2_OVER_2_FIXED );
314 :
315 : /* Make sure that the MDST is processed in the correct way also */
316 : set_s( &ms_mask[k][0], 1, MAX_SFB );
317 : }
318 :
319 : pop_wmops();
320 : return;
321 : }
322 : #endif
323 : ELSE /* decide based on signal */
324 : {
325 273538 : FOR( k = 0; k < nSubframes; k++ )
326 : {
327 138277 : IF( EQ_16( sts[0]->core, TCX_20_CORE ) )
328 : {
329 132245 : sfbConf = &hStereoMdct->stbParamsTCX20;
330 : }
331 : ELSE
332 : {
333 6032 : sfbConf = &hStereoMdct->stbParamsTCX10;
334 : }
335 138277 : IF( EQ_16( sts[0]->last_core, ACELP_CORE ) )
336 : {
337 0 : sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
338 : }
339 :
340 138277 : IF( LT_32( sts[0]->element_brate, IVAS_80k ) && EQ_16( sts[0]->core, sts[1]->core ) && !mct_on ) /* band-wise HF ILD alignment to increase channel compaction */
341 : {
342 25123 : Word16 sfb = 1;
343 25123 : move16();
344 :
345 25123 : test();
346 415123 : WHILE( LT_16( sfb, sfbConf->nBandsStereoCore ) && LT_16( sub( sfbConf->sfbOffset[sfb + 1], sfbConf->sfbOffset[sfb] ), 12 ) )
347 : {
348 390000 : test();
349 390000 : sfb = add( sfb, 1 ); /* find start offset */
350 : }
351 :
352 417973 : FOR( sfb--; sfb < sfbConf->nBandsStereoCore; sfb++ ) /* start one SFB early for the fade-in */
353 : {
354 392850 : const Word16 startLine = sfbConf->sfbOffset[sfb]; /* Q0 */
355 392850 : move16();
356 392850 : const Word16 endLine = sfbConf->sfbOffset[sfb + 1]; /* Q0 */
357 392850 : move16();
358 392850 : const Word16 sfbWidth = sub( endLine, startLine );
359 :
360 392850 : nrgRatio_e[0] = nrgRatio_e[1] = exp_spec;
361 392850 : move16();
362 392850 : move16();
363 :
364 392850 : nrgRatio_fx[0] = sum2_32_fx( &sts[0]->hTcxEnc->spectrum_fx[k][startLine], sfbWidth, &nrgRatio_e[0] ); /* Q31-nrgRatio_e[0] */
365 392850 : move32();
366 392850 : nrgRatio_fx[1] = sum2_32_fx( &sts[1]->hTcxEnc->spectrum_fx[k][startLine], sfbWidth, &nrgRatio_e[1] ); /* Q31-nrgRatio_e[1] */
367 392850 : move32();
368 :
369 392850 : test();
370 392850 : IF( !sts[0]->hTcxEnc->fUseTns[k] && !sts[1]->hTcxEnc->fUseTns[k] ) /* no TNS in either ch */
371 : {
372 : Word32 tmp_fx;
373 : Word16 tmp_e;
374 329255 : tmp_e = exp_mdct_spec;
375 329255 : move16();
376 329255 : tmp_fx = sum2_32_fx( &mdst_spectrum_fx[0][k][startLine], sfbWidth, &tmp_e ); /* Q31-tmp_e */
377 329255 : nrgRatio_fx[0] = BASOP_Util_Add_Mant32Exp( nrgRatio_fx[0], nrgRatio_e[0], tmp_fx, tmp_e, &nrgRatio_e[0] ); /* Q31-nrgRatio_e[0] */
378 329255 : tmp_e = exp_mdct_spec;
379 329255 : move16();
380 329255 : tmp_fx = sum2_32_fx( &mdst_spectrum_fx[1][k][startLine], sfbWidth, &tmp_e ); /* Q31-tmp_e */
381 329255 : nrgRatio_fx[1] = BASOP_Util_Add_Mant32Exp( nrgRatio_fx[1], nrgRatio_e[1], tmp_fx, tmp_e, &nrgRatio_e[1] ); /* Q31-nrgRatio_e[1] */
382 : }
383 392850 : IF( ( nrgRatio_fx[0] > 0 ) && ( nrgRatio_fx[1] > 0 ) && BASOP_Util_Cmp_Mant32Exp( nrgRatio_fx[0], nrgRatio_e[0], nrgRatio_fx[1], nrgRatio_e[1] ) )
384 : {
385 : Word16 fTemp_e, tmp_e;
386 392520 : Word32 fTemp_fx = BASOP_Util_Add_Mant32Exp( nrgRatio_fx[0], nrgRatio_e[0], nrgRatio_fx[1], nrgRatio_e[1], &fTemp_e ); /* Q31-fTemp_e */
387 392520 : fTemp_e = sub( fTemp_e, 1 );
388 392520 : nrgRatio_fx[0] = BASOP_Util_Divide3232_Scale_newton( fTemp_fx, nrgRatio_fx[0], &tmp_e ); /* Q31 - tmp_e - fTemp_e + nrgRatio_e[0] */
389 392520 : move32();
390 392520 : nrgRatio_e[0] = add( tmp_e, sub( fTemp_e, nrgRatio_e[0] ) );
391 392520 : move16();
392 392520 : nrgRatio_fx[0] = Sqrt32( nrgRatio_fx[0], &nrgRatio_e[0] ); /* Q31-nrgRatio_e[0] */
393 392520 : move32();
394 :
395 392520 : nrgRatio_fx[1] = BASOP_Util_Divide3232_Scale_newton( fTemp_fx, nrgRatio_fx[1], &tmp_e ); /* Q31 - tmp_e - fTemp_e + nrgRatio_e[1] */
396 392520 : move32();
397 392520 : nrgRatio_e[1] = add( tmp_e, sub( fTemp_e, nrgRatio_e[1] ) );
398 392520 : move16();
399 392520 : nrgRatio_fx[1] = Sqrt32( nrgRatio_fx[1], &nrgRatio_e[1] ); /* Q31-nrgRatio_e[1] */
400 392520 : move32();
401 :
402 392520 : nrgRatio_fx[0] = L_max( ONE_IN_Q27, L_shl_sat( nrgRatio_fx[0], sub( nrgRatio_e[0], 2 ) ) ); // max( 0.25f, min( 4.f, nrgRatio[0] ) );
403 392520 : move32();
404 392520 : nrgRatio_fx[1] = L_max( ONE_IN_Q27, L_shl_sat( nrgRatio_fx[1], sub( nrgRatio_e[1], 2 ) ) ); // Q29
405 392520 : move32();
406 392520 : nrgRatio_e[0] = Q2;
407 392520 : move16();
408 392520 : nrgRatio_e[1] = Q2;
409 392520 : move16();
410 :
411 392520 : IF( ( LT_16( sfbWidth, 12 ) && LT_16( add( sfb, 1 ), sfbConf->nBandsStereoCore ) ) || GT_32( sts[0]->element_brate, IVAS_48k ) ) /* attenuate ILD alignment in the first SFB or at 64k */
412 : {
413 207287 : nrgRatio_fx[0] = BASOP_Util_fPow( nrgRatio_fx[0], nrgRatio_e[0], ONE_IN_Q29, 0, &nrgRatio_e[0] ); /* Q31-nrgRatio_e[0] */
414 207287 : move32();
415 207287 : nrgRatio_fx[1] = BASOP_Util_fPow( nrgRatio_fx[1], nrgRatio_e[1], ONE_IN_Q29, 0, &nrgRatio_e[1] ); /* Q31-nrgRatio_e[1] */
416 207287 : move32();
417 : }
418 : ELSE
419 : {
420 185233 : nrgRatio_fx[0] = Sqrt32( nrgRatio_fx[0], &nrgRatio_e[0] ); /* Q31-nrgRatio_e[0] */
421 185233 : move32();
422 185233 : nrgRatio_fx[1] = Sqrt32( nrgRatio_fx[1], &nrgRatio_e[1] ); /* Q31-nrgRatio_e[1] */
423 185233 : move32();
424 : }
425 :
426 392520 : nrgRatio_fx[0] = L_shl( nrgRatio_fx[0], sub( nrgRatio_e[0], 1 ) ); // Q30
427 392520 : move32();
428 392520 : nrgRatio_fx[1] = L_shl( nrgRatio_fx[1], sub( nrgRatio_e[1], 1 ) ); // Q30
429 392520 : move32();
430 :
431 :
432 392520 : v_multc_fixed( &sts[0]->hTcxEnc->spectrum_fx[k][startLine], nrgRatio_fx[0], &sts[0]->hTcxEnc->spectrum_fx[k][startLine], sfbWidth ); /* exp: exp_spec+1 */
433 392520 : v_multc_fixed( &mdst_spectrum_fx[0][k][startLine], nrgRatio_fx[0], &mdst_spectrum_fx[0][k][startLine], sfbWidth ); /* exp: exp_mdct_spec+1 */
434 392520 : scale_sig32( &sts[0]->hTcxEnc->spectrum_fx[k][startLine], sfbWidth, Q1 ); // exp: exp_spec
435 392520 : scale_sig32( &mdst_spectrum_fx[0][k][startLine], sfbWidth, Q1 ); // exp: exp_mdct_spec
436 :
437 392520 : v_multc_fixed( &sts[1]->hTcxEnc->spectrum_fx[k][startLine], nrgRatio_fx[1], &sts[1]->hTcxEnc->spectrum_fx[k][startLine], sfbWidth ); /* exp: exp_spec+1 */
438 392520 : v_multc_fixed( &mdst_spectrum_fx[1][k][startLine], nrgRatio_fx[1], &mdst_spectrum_fx[1][k][startLine], sfbWidth ); /* exp: exp_mdct_spec+1 */
439 392520 : scale_sig32( &sts[1]->hTcxEnc->spectrum_fx[k][startLine], sfbWidth, Q1 ); // exp: exp_spec
440 392520 : scale_sig32( &mdst_spectrum_fx[1][k][startLine], sfbWidth, Q1 ); // exp: exp_mdct_spec
441 : }
442 : }
443 : }
444 :
445 : /* set mask to zero */
446 138277 : set16_fx( &ms_mask[k][0], 0, MAX_SFB );
447 138277 : IF( mct_on )
448 : {
449 95584 : nAvailBitsMS[k] = shl( sts[0]->bits_frame_channel, 1 ); /* Q0 */
450 95584 : move16();
451 : }
452 : ELSE
453 : {
454 42693 : nAvailBitsMS[k] = sts[0]->bits_frame_nominal; /* Q0 */
455 42693 : move16();
456 : }
457 :
458 138277 : nAvailBitsMS[k] = sub( nAvailBitsMS[k], add( sts[0]->side_bits_frame_channel, sts[1]->side_bits_frame_channel ) ); /* Q0 */
459 138277 : move16();
460 :
461 138277 : IF( EQ_16( nSubframes, 2 ) )
462 : {
463 6032 : nAvailBitsMS[k] = sub( nAvailBitsMS[k], OFFSET_BITS_TCX10 ); /* Q0 */
464 6032 : move16();
465 : }
466 : ELSE
467 : {
468 132245 : nAvailBitsMS[k] = sub( nAvailBitsMS[k], OFFSET_BITS_TCX20 ); /* Q0 */
469 132245 : move16();
470 : }
471 :
472 138277 : nAvailBitsMS[k] = idiv1616( nAvailBitsMS[k], nSubframes ); /* Q0 */
473 138277 : move16();
474 :
475 138277 : MsStereoDecision_fx( sfbConf, sts[0]->hTcxEnc->spectrum_fx[k], sts[1]->hTcxEnc->spectrum_fx[k], inv_spectrum_fx[0][k], inv_spectrum_fx[1][k], sub( Q31, exp_spec ), &hStereoMdct->mdct_stereo_mode[k], &ms_mask[k][0], nAvailBitsMS[k] );
476 :
477 138277 : IF( sts[0]->igf )
478 : {
479 90473 : IGFEncStereoEncoder_fx( sfbConf, sts[0]->hIGFEnc, sts[0]->hTcxEnc->spectrum_fx[k], sts[1]->hTcxEnc->spectrum_fx[k], sub( Q31, exp_spec ), &ms_mask[k][0],
480 90473 : &hStereoMdct->IGFStereoMode[k], hStereoMdct->mdct_stereo_mode[k], (Word16) EQ_16( sts[0]->core, TCX_20_CORE ), (Word16) EQ_16( sts[0]->last_core, ACELP_CORE ) );
481 : }
482 : ELSE
483 : {
484 47804 : hStereoMdct->IGFStereoMode[k] = hStereoMdct->mdct_stereo_mode[k];
485 47804 : move16();
486 : }
487 :
488 :
489 138277 : IF( NE_16( hStereoMdct->mdct_stereo_mode[k], SMDCT_DUAL_MONO ) || NE_16( hStereoMdct->IGFStereoMode[k], SMDCT_DUAL_MONO ) )
490 : {
491 :
492 131695 : ms_inv_mask_processing_fx( hStereoMdct, sts, ms_mask, k, sts[0]->hTcxEnc->spectrum_fx[k], sts[1]->hTcxEnc->spectrum_fx[k], inv_spectrum_fx[0][k], inv_spectrum_fx[1][k], sfbConf->sfbCnt );
493 :
494 131695 : ms_processing_fx( hStereoMdct, sts, ms_mask, k, sts[0]->hTcxEnc->spectrum_fx[k], sts[1]->hTcxEnc->spectrum_fx[k], sfbConf->sfbCnt );
495 :
496 131695 : IF( !sts[0]->hTcxEnc->fUseTns[k] && !sts[1]->hTcxEnc->fUseTns[k] )
497 : {
498 109234 : sts[0]->hTcxEnc->tns_ms_flag[k] = 1;
499 109234 : move16();
500 109234 : sts[1]->hTcxEnc->tns_ms_flag[k] = 1;
501 109234 : move16();
502 109234 : ms_inv_mask_processing_fx( hStereoMdct, sts, ms_mask, k, mdst_spectrum_fx[0][k], mdst_spectrum_fx[1][k], inv_mdst_spectrum_fx[0][k], inv_mdst_spectrum_fx[1][k], -1 );
503 :
504 109234 : ms_processing_fx( hStereoMdct, sts, ms_mask, k, mdst_spectrum_fx[0][k], mdst_spectrum_fx[1][k], sfbConf->sfbCnt );
505 : }
506 : }
507 : } /* for k */
508 : }
509 : /* for bitrate switching determine correlation depending on m/s decision */
510 : {
511 : Word16 ms_bands[2];
512 : Word16 sw_uncorr[2], sw_uncorr_mean;
513 273538 : FOR( k = 0; k < nSubframes; k++ )
514 : {
515 138277 : sfbConf = EQ_16( sts[0]->core, TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
516 138277 : ms_bands[k] = sum_s( ms_mask[k], sfbConf->nBandsStereoCore ); /* Q0 */
517 138277 : move16();
518 138277 : sw_uncorr[k] = sub( 32767 /*Q15*/, div_s( ms_bands[k], sfbConf->nBandsStereoCore ) ); /* Q15 */
519 138277 : move16();
520 : }
521 135261 : IF( EQ_16( sts[0]->core, TCX_20_CORE ) )
522 : {
523 132245 : sw_uncorr_mean = sw_uncorr[0]; /* Q15 */
524 132245 : move16();
525 : }
526 : ELSE
527 : {
528 3016 : sw_uncorr_mean = add( shr( sw_uncorr[0], 1 ), shr( sw_uncorr[1], 1 ) ); /* Q15 */
529 : }
530 135261 : hStereoMdct->sw_uncorr = extract_l( GT_16( sw_uncorr_mean, 19661 /*0.6 in Q15*/ ) );
531 135261 : move16();
532 : }
533 :
534 135261 : pop_wmops();
535 135261 : return;
536 : }
537 :
538 : /*-------------------------------------------------------------------*
539 : * ms_processing()
540 : *
541 : *
542 : *-------------------------------------------------------------------*/
543 :
544 240929 : void ms_processing_fx(
545 : STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct, /* i/o: Stereo MDCT encoder structure */
546 : Encoder_State **sts, /* i/o: Encoder state structure */
547 : Word16 ms_mask[NB_DIV][MAX_SFB], /* i : bandwise MS mask Q0*/
548 : const Word16 iSubframe, /* i : subframe number Q0*/
549 : Word32 x_0_fx[],
550 : /* i/o: spectrum 1 */ // Q( q_x )
551 : Word32 x_1_fx[],
552 : /* i/o: spectrum 1 */ // Q( q_x )
553 : Word16 maxSfb /* i : number of stereo frequency bands Q0*/
554 : )
555 : {
556 : Word16 sfb;
557 : STEREO_MDCT_BAND_PARAMETERS *sfbConf;
558 :
559 240929 : sfbConf = EQ_16( sts[0]->core, TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
560 :
561 240929 : IF( sts[0]->last_core == ACELP_CORE )
562 : {
563 0 : assert( EQ_16( sts[1]->last_core, ACELP_CORE ) );
564 0 : sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
565 : }
566 :
567 240929 : if ( EQ_16( maxSfb, -1 ) )
568 : {
569 0 : maxSfb = sfbConf->sfbCnt; /* Q0 */
570 0 : move16();
571 : }
572 :
573 10595183 : FOR( sfb = 0; sfb < maxSfb; sfb++ )
574 : {
575 10354254 : IF( NE_16( ms_mask[iSubframe][sfb], 0 ) )
576 : {
577 8516931 : convertToBwMS_fx( sfbConf->sfbOffset[sfb], sfbConf->sfbOffset[sfb + 1], x_0_fx, x_1_fx, SQRT2_OVER_2_FIXED );
578 : }
579 : }
580 :
581 240929 : return;
582 : }
583 :
584 : /*-------------------------------------------------------------------*
585 : * ms_inv_mask_processing()
586 : *
587 : *
588 : *-------------------------------------------------------------------*/
589 :
590 240929 : void ms_inv_mask_processing_fx(
591 : STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct, /* i/o: Stereo MDCT encoder structure */
592 : Encoder_State **sts, /* i/o: Encoder state structure */
593 : Word16 ms_mask[NB_DIV][MAX_SFB], /* i : bandwise MS mask Q0*/
594 : const Word16 iSubframe, /* i : subframe number Q0*/
595 : const Word32 x_0_fx[],
596 : /* i : spectrum 1 */ // Q( q_x )
597 : const Word32 x_1_fx[],
598 : /* i : spectrum 2 */ // Q( q_x )
599 : Word32 x_inv_0_fx[],
600 : /* o : inverse spectrum 1 */ // Q( q_x )
601 : Word32 x_inv_1_fx[],
602 : /* o : inverse spectrum 2 */ // Q( q_x )
603 : Word16 maxSfb /* i : number of stereo frequency bands Q0*/
604 : )
605 : {
606 : Word16 sfb;
607 : STEREO_MDCT_BAND_PARAMETERS *sfbConf;
608 : Word16 nSubframes, L_subframeTCX;
609 :
610 240929 : nSubframes = ( sts[0]->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
611 240929 : L_subframeTCX = idiv1616( sts[0]->hTcxEnc->L_frameTCX, nSubframes ); /* Q0 */
612 240929 : sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
613 :
614 240929 : IF( EQ_16( sts[0]->last_core, ACELP_CORE ) )
615 : {
616 0 : assert( EQ_16( sts[1]->last_core, ACELP_CORE ) );
617 0 : sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
618 : }
619 :
620 240929 : IF( EQ_16( maxSfb, -1 ) )
621 : {
622 109234 : maxSfb = sfbConf->sfbCnt; /* Q0 */
623 109234 : move16();
624 : }
625 :
626 10595183 : FOR( sfb = 0; sfb < maxSfb; sfb++ )
627 : {
628 10354254 : Copy32( &x_0_fx[sfbConf->sfbOffset[sfb]], &x_inv_0_fx[sfbConf->sfbOffset[sfb]], sfbConf->sfbOffset[sfb + 1] - sfbConf->sfbOffset[sfb] ); /* q_x */
629 10354254 : Copy32( &x_1_fx[sfbConf->sfbOffset[sfb]], &x_inv_1_fx[sfbConf->sfbOffset[sfb]], sfbConf->sfbOffset[sfb + 1] - sfbConf->sfbOffset[sfb] ); /* q_x */
630 :
631 10354254 : IF( EQ_16( ms_mask[iSubframe][sfb], 0 ) )
632 : {
633 1837323 : convertToBwMS_fx( sfbConf->sfbOffset[sfb], sfbConf->sfbOffset[sfb + 1], x_inv_0_fx, x_inv_1_fx, SQRT2_OVER_2_FIXED );
634 : }
635 : }
636 :
637 : /* set rest of inverse spectrum to zero */
638 240929 : IF( GT_16( L_subframeTCX, sfbConf->sfbOffset[maxSfb] ) )
639 : {
640 141841 : set_zero_fx( &x_inv_0_fx[sfbConf->sfbOffset[maxSfb]], sub( L_subframeTCX, sfbConf->sfbOffset[maxSfb] ) );
641 141841 : set_zero_fx( &x_inv_1_fx[sfbConf->sfbOffset[maxSfb]], sub( L_subframeTCX, sfbConf->sfbOffset[maxSfb] ) );
642 : }
643 :
644 240929 : return;
645 : }
646 :
647 :
648 : /*-------------------------------------------------------------------*
649 : * write_stereo_to_bitstream()
650 : *
651 : *
652 : *-------------------------------------------------------------------*/
653 :
654 132111 : Word16 write_stereo_to_bitstream_fx(
655 : STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct, /* i/o: Stereo MDCT encoder structure */
656 : Encoder_State **sts, /* i/o: Encoder state structure */
657 : Word16 ms_mask[NB_DIV][MAX_SFB], /* i : bandwise MS mask Q0*/
658 : const Word16 mct_on, /* i : flag mct block (1) or stereo (0) Q0*/
659 : BSTR_ENC_HANDLE hBstr /* i/o: bitstream handle */
660 : )
661 : {
662 : Word16 i, k, nSubframes;
663 : UWord16 mdct_stereo_mode, stereo_mode_bits;
664 : STEREO_MDCT_BAND_PARAMETERS *sfbConf;
665 132111 : Word16 start_bits = hBstr->nb_bits_tot;
666 132111 : move16();
667 :
668 132111 : if ( !mct_on )
669 : {
670 42498 : assert( ( EQ_16( sts[0]->hTcxEnc->transform_type[0], sts[1]->hTcxEnc->transform_type[0] ) && EQ_16( sts[0]->hTcxEnc->transform_type[1], sts[1]->hTcxEnc->transform_type[1] ) ) || ( EQ_16( hStereoMdct->mdct_stereo_mode[0], SMDCT_DUAL_MONO ) && EQ_16( hStereoMdct->mdct_stereo_mode[1], SMDCT_DUAL_MONO ) ) );
671 : }
672 :
673 132111 : nSubframes = ( EQ_16( sts[0]->core, TCX_10_CORE ) || NE_16( sts[0]->core, sts[1]->core ) ) ? NB_DIV : 1;
674 132111 : move16();
675 132111 : sfbConf = EQ_16( sts[0]->core, TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
676 132111 : move16();
677 :
678 132111 : IF( EQ_16( sts[0]->last_core, ACELP_CORE ) )
679 : {
680 649 : assert( EQ_16( sts[1]->ini_frame, 0 ) || EQ_16( sts[1]->last_core, ACELP_CORE ) );
681 649 : sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
682 : }
683 :
684 132111 : IF( hStereoMdct->hItd != NULL )
685 : {
686 11753 : write_itd_data_fx( hStereoMdct->hItd, hBstr );
687 : }
688 :
689 267504 : FOR( k = 0; k < nSubframes; k++ )
690 : {
691 135393 : mdct_stereo_mode = 0;
692 135393 : move16();
693 135393 : stereo_mode_bits = 1;
694 135393 : move16();
695 :
696 135393 : SWITCH( hStereoMdct->mdct_stereo_mode[k] )
697 : {
698 3753 : case SMDCT_DUAL_MONO:
699 3753 : mdct_stereo_mode = 0;
700 3753 : move16();
701 3753 : stereo_mode_bits = 1;
702 3753 : move16();
703 3753 : BREAK;
704 81907 : case SMDCT_MS_FULL:
705 81907 : mdct_stereo_mode = 2;
706 81907 : move16();
707 81907 : stereo_mode_bits = 2;
708 81907 : move16();
709 81907 : BREAK;
710 49733 : case SMDCT_BW_MS:
711 49733 : mdct_stereo_mode = 3;
712 49733 : move16();
713 49733 : stereo_mode_bits = 2;
714 49733 : move16();
715 49733 : BREAK;
716 0 : default:
717 0 : assert( !"Not supported MDCT stereo mode\n" );
718 : }
719 :
720 135393 : push_next_indice( hBstr, mdct_stereo_mode, stereo_mode_bits );
721 :
722 135393 : IF( !mct_on )
723 : {
724 43888 : IF( EQ_16( sts[0]->core, sts[1]->core ) || ( k == 0 ) )
725 : {
726 43626 : push_next_indice( hBstr, hStereoMdct->global_ild[k], SMDCT_GLOBAL_ILD_BITS );
727 : }
728 : }
729 :
730 135393 : IF( EQ_16( hStereoMdct->mdct_stereo_mode[k], SMDCT_BW_MS ) )
731 : {
732 2059362 : FOR( i = 0; i < sfbConf->nBandsStereoCore; i++ )
733 : {
734 2009629 : push_next_indice( hBstr, ms_mask[k][i] ? 1 : 0, 1 );
735 : }
736 : }
737 :
738 135393 : IF( sts[0]->igf )
739 : {
740 90166 : mdct_stereo_mode = 0;
741 90166 : move16();
742 90166 : stereo_mode_bits = 1;
743 90166 : move16();
744 90166 : SWITCH( hStereoMdct->IGFStereoMode[k] )
745 : {
746 36339 : case SMDCT_DUAL_MONO:
747 36339 : mdct_stereo_mode = 0;
748 36339 : move16();
749 36339 : stereo_mode_bits = 1;
750 36339 : move16();
751 36339 : BREAK;
752 39679 : case SMDCT_MS_FULL:
753 39679 : mdct_stereo_mode = 2;
754 39679 : move16();
755 39679 : stereo_mode_bits = 2;
756 39679 : move16();
757 39679 : BREAK;
758 14148 : case SMDCT_BW_MS:
759 14148 : mdct_stereo_mode = 3;
760 14148 : move16();
761 14148 : stereo_mode_bits = 2;
762 14148 : move16();
763 14148 : BREAK;
764 0 : default:
765 0 : assert( !"Not supported MDCT stereo mode\n" );
766 : }
767 :
768 90166 : push_next_indice( hBstr, mdct_stereo_mode, stereo_mode_bits );
769 :
770 :
771 90166 : IF( EQ_16( hStereoMdct->IGFStereoMode[k], SMDCT_BW_MS ) )
772 : {
773 89863 : FOR( i = sfbConf->nBandsStereoCore; i < sfbConf->sfbCnt; i++ )
774 : {
775 75715 : push_next_indice( hBstr, ms_mask[k][i] ? 1 : 0, 1 );
776 : }
777 : }
778 : }
779 : }
780 :
781 132111 : return sub( hBstr->nb_bits_tot, start_bits );
782 : }
783 :
784 : /*-------------------------------------------------------------------*
785 : * Band-wise M/S stereo processing
786 : *
787 : *
788 : *-------------------------------------------------------------------*/
789 :
790 10446240 : static void convertToBwMS_fx(
791 : const Word16 startLine, /* i : start line of sfb Q0*/
792 : const Word16 stopLine, /* i : stop line of sfb Q0*/
793 : Word32 x0[],
794 : /* i/o: mid/left channel coefficients */ /* Q_x */
795 : Word32 x1[],
796 : /* i/o: side/right channel coefficients */ /* Q_x */
797 : const Word32 norm_fac /* i : normalization factor */ /* Q31 */
798 : )
799 : {
800 : Word16 j;
801 : Word32 tmpValue_fx;
802 :
803 194393970 : FOR( j = startLine; j < stopLine; j++ )
804 : {
805 183947730 : tmpValue_fx = x0[j]; /*Q_x*/
806 183947730 : move32();
807 183947730 : x0[j] = Mpy_32_32( L_add( x0[j], x1[j] ), norm_fac ); /* Q_x */
808 183947730 : move32();
809 183947730 : x1[j] = Mpy_32_32( L_sub( tmpValue_fx, x1[j] ), norm_fac ); /* Q_x */
810 183947730 : move32();
811 : }
812 :
813 10446240 : return;
814 : }
815 :
816 : /*-------------------------------------------------------------------*
817 : * convertToMS()
818 : *
819 : *
820 : *-------------------------------------------------------------------*/
821 :
822 91986 : void convertToMS_fx(
823 : const Word16 L_frame, /* i : frame length Q0*/
824 : Word32 x0[], /* i/o: mid/left channel coefficients Q(q_x)*/
825 : Word32 x1[], /* i/o: side/right channel coefficients Q(q_x)*/
826 : const Word32 norm_fac /* i : normalization factor Q31*/
827 : )
828 : {
829 91986 : convertToBwMS_fx( 0, L_frame, x0, x1, norm_fac );
830 :
831 91986 : return;
832 : }
833 :
834 : /*-------------------------------------------------------------------*
835 : * SQ_gain_estimate_stereo()
836 : *
837 : *
838 : *-------------------------------------------------------------------*/
839 :
840 : /*! r: SQ gain */
841 138277 : static Word32 SQ_gain_estimate_stereo_fx( // e_res
842 : const Word32 xL_fx[], /* i : L vector to quantize Q31-e_xL*/
843 : const Word16 e_xL,
844 : const Word32 xR_fx[], /* i : R vector to quantize Q31-e_xR*/
845 : const Word16 e_xR,
846 : const Word16 nbitsSQ, /* i : number of bits targeted Q0*/
847 : const Word16 lg, /* i : vector size (2048 max) Q0*/
848 : Word16 *e_res )
849 : {
850 : Word16 i, q, iter, e_ener, e_xL_2, e_xR_2, s;
851 : Word32 ener_fx, tmp_32, target_fx, fac_fx, offset_fx;
852 : Word32 en_fx[N_MAX / 2]; // Q25
853 : Word16 lg2, lg_4, lg2_4;
854 : Word64 W_tmp, _0_01;
855 :
856 138277 : lg_4 = shr( lg, 2 ); /* Q0 */
857 138277 : lg2_4 = shl( lg_4, 1 ); /* Q0 */
858 138277 : lg2 = shl( lg2_4, 2 ); /* Q0 */
859 138277 : i = 0;
860 138277 : move16();
861 138277 : e_xL_2 = shl( e_xL, 1 );
862 138277 : e_xR_2 = shl( e_xR, 1 );
863 138277 : _0_01 = W_shr( 21474836 /* 0.01 in Q31 */, sub( e_xL_2, 32 ) ); // 0.01 in 2*(Q of specL/R) + 1
864 :
865 138277 : set32_fx( en_fx, 335544 /* 0.01 in Q25 */, ( N_MAX / 2 ) );
866 :
867 : /* energy of quadruples with 9dB offset */
868 : /* ignore that we may take no all lines into account, max. 3 lines at the upper end of the spectrum can be missed (if lg is not a multiple of 4, happens also in SQGain()*/
869 :
870 19877051 : FOR( q = 0; q < lg_4; q++ )
871 : {
872 19738774 : W_tmp = W_mac_32_32( _0_01, xL_fx[i], xL_fx[i] ); // 2 * e_xL
873 19738774 : W_tmp = W_mac_32_32( W_tmp, xL_fx[i + 1], xL_fx[i + 1] ); // 2 * e_xL
874 19738774 : W_tmp = W_mac_32_32( W_tmp, xL_fx[i + 2], xL_fx[i + 2] ); // 2 * e_xL
875 19738774 : W_tmp = W_mac_32_32( W_tmp, xL_fx[i + 3], xL_fx[i + 3] ); // 2 * e_xL
876 :
877 19738774 : s = W_norm( W_tmp );
878 19738774 : ener_fx = W_extract_h( W_shl( W_tmp, s ) );
879 19738774 : e_ener = sub( e_xL_2, s );
880 :
881 19738774 : en_fx[q] = BASOP_Util_Log2( ener_fx ); /* saves a MAC */
882 19738774 : move32();
883 19738774 : en_fx[q] = Mpy_32_16_1( L_add( L_shl( e_ener, Q25 ), en_fx[q] ), 9864 /* log10(2) in Q15 */ ); // Q(25)
884 19738774 : move32();
885 19738774 : i = add( i, 4 );
886 : }
887 138277 : i = 0;
888 138277 : move16();
889 19877051 : FOR( ; q < lg2_4; q++ )
890 : {
891 19738774 : W_tmp = W_mac_32_32( _0_01, xR_fx[i], xR_fx[i] ); // 2 * e_xR
892 19738774 : W_tmp = W_mac_32_32( W_tmp, xR_fx[i + 1], xR_fx[i + 1] ); // 2 * e_xR
893 19738774 : W_tmp = W_mac_32_32( W_tmp, xR_fx[i + 2], xR_fx[i + 2] ); // 2 * e_xR
894 19738774 : W_tmp = W_mac_32_32( W_tmp, xR_fx[i + 3], xR_fx[i + 3] ); // 2 * e_xR
895 :
896 19738774 : s = W_norm( W_tmp );
897 19738774 : ener_fx = W_extract_h( W_shl( W_tmp, s ) );
898 19738774 : e_ener = sub( e_xR_2, s );
899 :
900 19738774 : en_fx[q] = BASOP_Util_Log2( ener_fx ); /* saves a MAC */
901 19738774 : move32();
902 19738774 : en_fx[q] = Mpy_32_16_1( L_add( L_shl( e_ener, Q25 ), en_fx[q] ), 9864 /* log10(2) in Q15 */ ); // Q(25)
903 19738774 : move32();
904 19738774 : i = add( i, 4 );
905 : }
906 :
907 : /* SQ scale: 4 bits / 6 dB per quadruple */
908 138277 : target_fx = L_mult( 19660 /* 0.15 in Q17 */, sub( nbitsSQ, shr( lg2, 4 ) ) ); // Q(18)
909 138277 : fac_fx = 429496729; /* 12.8 in Q25 */
910 138277 : move32();
911 138277 : offset_fx = fac_fx; /* Q25 */
912 138277 : move32();
913 :
914 : /* find offset (0 to 128 dB with step of 0.125dB) */
915 1521047 : FOR( iter = 0; iter < 10; iter++ )
916 : {
917 1382770 : fac_fx = L_shr( fac_fx, 1 ); /* Q25 */
918 1382770 : offset_fx = L_sub( offset_fx, fac_fx ); /* Q25 */
919 1382770 : W_tmp = 0;
920 1382770 : move64();
921 :
922 359326556 : FOR( i = 0; i < lg2_4; i++ )
923 : {
924 358510092 : tmp_32 = L_sub( en_fx[i], offset_fx ); /* Q25 */
925 :
926 : /* avoid SV with 1 bin of amp < 0.5f */
927 358510092 : IF( GT_32( tmp_32, 10066329 /*0.3 Q25*/ ) )
928 : {
929 200684411 : W_tmp = W_add( W_tmp, W_deposit32_l( tmp_32 ) ); /* Q25 */
930 :
931 : /* if ener is above target -> break and increase offset */
932 200684411 : IF( GT_64( W_tmp, W_shl( W_deposit32_l( target_fx ), 7 ) ) )
933 : {
934 566306 : offset_fx = L_add( offset_fx, fac_fx ); /* Q25 */
935 566306 : BREAK;
936 : }
937 : }
938 : }
939 : }
940 :
941 : /* return gain */
942 138277 : tmp_32 = L_add( 12539858, Mpy_32_16_1( offset_fx, 13606 ) ); // Q23
943 :
944 138277 : return BASOP_util_Pow2( tmp_32, Q8, e_res ); /* Q31-e_res */
945 : }
946 :
947 : /*-------------------------------------------------------------------*
948 : * QuantSpecEstimateBits()
949 : *
950 : *
951 : *-------------------------------------------------------------------*/
952 :
953 553108 : static Word16 QuantSpecEstimateBits_fx(
954 : Word32 *spec_fx, /* Q15-spec_e */
955 : Word16 spec_e,
956 : Word16 G_fx, /* Q15-G_e */
957 : Word16 G_e,
958 : const Word16 length, /* Q0 */
959 : const Word16 nBitsAvailable, /* Q0 */
960 : Word16 sqQ[] /* Q0 */ )
961 : {
962 : Word16 stop, sqBits, nEncoded;
963 : Word16 lastnz;
964 :
965 553108 : tcx_scalar_quantization_ivas_fx( spec_fx, spec_e, sqQ, length, G_fx, G_e, 16384 /*Q15*/, NULL, 1 );
966 :
967 553108 : stop = 0;
968 553108 : move16();
969 :
970 553108 : sqBits = RCcontextMapping_encode2_estimate_no_mem_s17_LCS_fx( sqQ, length, &lastnz, &nEncoded, nBitsAvailable, &stop, 0, NULL ); /*Q0*/
971 :
972 553108 : if ( stop != 0 )
973 : {
974 67214 : sqBits = stop;
975 67214 : move16();
976 : }
977 :
978 553108 : return sqBits;
979 : }
980 :
981 : /*-------------------------------------------------------------------*
982 : * context_update()
983 : *
984 : *
985 : *-------------------------------------------------------------------*/
986 :
987 10976094 : static void context_update_fx(
988 : HANDLE_RC_CONTEXT_MEM ctxSrc,
989 : HANDLE_RC_CONTEXT_MEM ctxTarget,
990 : const Word16 endLine /*Q0*/ )
991 : {
992 : Word16 last_nz;
993 :
994 : /* check if last_nz of target is smaller than endLine, save and update */
995 10976094 : last_nz = s_max( ctxTarget->lastnz, endLine ); /* Q0 */
996 :
997 10976094 : mvc2c( (UWord8 *) ctxSrc, (UWord8 *) ctxTarget, sizeof( RC_CONTEXT_MEM ) );
998 10976094 : ctxTarget->lastnz = last_nz;
999 10976094 : move16();
1000 :
1001 10976094 : return;
1002 : }
1003 :
1004 : /*-------------------------------------------------------------------*
1005 : * GetChannelEnergyRatio()
1006 : *
1007 : *
1008 : *-------------------------------------------------------------------*/
1009 :
1010 125507 : static Word16 GetChannelEnergyRatio_fx( // Q15
1011 : Encoder_State **sts, /* i/o: Encoder state structure */
1012 : const Word16 iFirstSubframe,
1013 : const Word16 iLastSubframe,
1014 : const UWord8 ratioInRmsDomain /* Q0 */ )
1015 : {
1016 : Word16 ch, n, i;
1017 : Word32 nrg_fx[2];
1018 : Word16 nrg_e[2];
1019 :
1020 : /* Calculate energies per channel */
1021 376521 : FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
1022 : {
1023 251014 : const Encoder_State *st = sts[ch];
1024 :
1025 251014 : Word16 nSubframes = NB_DIV;
1026 251014 : move16();
1027 :
1028 251014 : if ( EQ_16( st->hTcxEnc->tcxMode, TCX_20 ) )
1029 : {
1030 242143 : nSubframes = 1;
1031 242143 : move16();
1032 : }
1033 :
1034 251014 : Word16 L_subframeTCX = shr( st->hTcxEnc->L_frameTCX, sub( nSubframes, 1 ) ); /* Q0 */
1035 :
1036 251014 : if ( st->last_core == ACELP_CORE )
1037 : {
1038 2628 : L_subframeTCX = add( L_subframeTCX, shr( L_subframeTCX, 2 ) ); /* Q0 */
1039 : }
1040 251014 : assert( iFirstSubframe >= 0 && ( iFirstSubframe <= iLastSubframe ) );
1041 :
1042 251014 : nrg_fx[ch] = 0;
1043 251014 : move32();
1044 251014 : nrg_e[ch] = 0;
1045 251014 : move16();
1046 :
1047 506387 : FOR( n = iFirstSubframe; n <= s_min( nSubframes - 1, iLastSubframe ); n++ )
1048 : {
1049 192602413 : FOR( i = 0; i < L_subframeTCX; i++ )
1050 : {
1051 192347040 : nrg_fx[ch] = BASOP_Util_Add_Mant32Exp( nrg_fx[ch], nrg_e[ch], Mpy_32_32( st->hTcxEnc->spectrum_fx[n][i], st->hTcxEnc->spectrum_fx[n][i] ), shl( st->hTcxEnc->spectrum_e[n], 1 ), &nrg_e[ch] ); /* Q31-nrg_e */
1052 192347040 : move32();
1053 : }
1054 : }
1055 251014 : IF( ratioInRmsDomain && nrg_fx[ch] )
1056 : {
1057 242221 : nrg_fx[ch] = Sqrt32( nrg_fx[ch], &nrg_e[ch] ); /* Q31-nrg_e */
1058 242221 : move32();
1059 : }
1060 : }
1061 :
1062 125507 : IF( L_add_sat( nrg_fx[0], nrg_fx[1] ) )
1063 : {
1064 125478 : nrg_fx[1] = BASOP_Util_Add_Mant32Exp( nrg_fx[0], nrg_e[0], nrg_fx[1], nrg_e[1], &nrg_e[1] ); /* Q31-nrg_e[1] */
1065 125478 : move32();
1066 :
1067 125478 : IF( NE_16( nrg_e[1], nrg_e[0] ) )
1068 : {
1069 89079 : nrg_fx[0] = L_shr( nrg_fx[0], sub( nrg_e[1], nrg_e[0] ) ); /* Q31-nrg_e[1] */
1070 89079 : move32();
1071 : }
1072 125478 : return divide3232( nrg_fx[0], nrg_fx[1] ); /* Q15 */
1073 : }
1074 :
1075 29 : return MIN_16;
1076 : }
1077 :
1078 : /*-------------------------------------------------------------------*
1079 : * FindSplitRatio()
1080 : *
1081 : *
1082 : *-------------------------------------------------------------------*/
1083 :
1084 81881 : void FindSplitRatio_fx(
1085 : CPE_ENC_HANDLE hCPE, /* i/o: CPE encoder structure */
1086 : Encoder_State **sts /* i/o: Encoder state structure */
1087 : )
1088 : {
1089 81881 : const UWord8 highRateMdctStereo = ( LT_32( sts[0]->element_brate, IVAS_80k ) && EQ_16( sts[0]->core, sts[1]->core ) && EQ_16( sts[0]->element_mode, IVAS_CPE_MDCT ) && sts[0]->hTcxEnc->enc_ste_pre_corr_past ? 0 : 1 );
1090 81881 : move16();
1091 : Word16 ratio_fx;
1092 :
1093 : /* Calculate split ratio and quantize it */
1094 81881 : hCPE->hStereoMdct->split_ratio = SMDCT_EQUAL_RATIO_RANGE; /* Equal bits to both channels */
1095 81881 : move16();
1096 :
1097 81881 : ratio_fx = GetChannelEnergyRatio_fx( sts, 0, 1, highRateMdctStereo ); // Q15
1098 :
1099 81881 : IF( ratio_fx >= 0 )
1100 : {
1101 81852 : hCPE->hStereoMdct->split_ratio = round_fx( L_mult( SMDCT_BITRATE_RATIO_RANGE, ratio_fx ) ); /* Q0 */
1102 81852 : move16();
1103 : /* Tuning to get closer to the optimal split ratio */
1104 81852 : IF( LT_32( L_mult0( 9, ratio_fx ), 8 * ONE_IN_Q15 ) && GT_16( hCPE->hStereoMdct->split_ratio, SMDCT_EQUAL_RATIO_RANGE + ( SMDCT_BITRATE_RATIO_RANGE >> 4 ) ) )
1105 : {
1106 62744 : hCPE->hStereoMdct->split_ratio = sub( hCPE->hStereoMdct->split_ratio, SMDCT_BITRATE_RATIO_RANGE >> 3 ); /* Q0 */
1107 62744 : move16();
1108 : }
1109 81852 : IF( GT_32( L_mult0( 9, ratio_fx ), 1 * ONE_IN_Q15 ) && LT_16( hCPE->hStereoMdct->split_ratio, SMDCT_EQUAL_RATIO_RANGE - ( SMDCT_BITRATE_RATIO_RANGE >> 4 ) ) )
1110 : {
1111 1160 : hCPE->hStereoMdct->split_ratio = add( hCPE->hStereoMdct->split_ratio, SMDCT_BITRATE_RATIO_RANGE >> 3 ); /* Q0 */
1112 1160 : move16();
1113 : }
1114 :
1115 81852 : hCPE->hStereoMdct->split_ratio = s_min( SMDCT_BITRATE_RATIO_RANGE - 1, s_max( 1, hCPE->hStereoMdct->split_ratio ) ); /* Q0 */
1116 81852 : move16();
1117 : }
1118 :
1119 81881 : return;
1120 : }
1121 :
1122 : /*-------------------------------------------------------------------*
1123 : * MsStereoDecision()
1124 : *
1125 : *
1126 : *-------------------------------------------------------------------*/
1127 :
1128 138277 : static void MsStereoDecision_fx(
1129 : STEREO_MDCT_BAND_PARAMETERS *sfbParam,
1130 : Word32 *specL_fx, /* Q( q_spec ) */
1131 : Word32 *specR_fx, /* Q( q_spec ) */
1132 : Word32 *specM_fx,
1133 : /* scratch buffer for M, use buffer for inverse MS mask spectrum */ /* Q( q_spec ) */
1134 : Word32 *specS_fx,
1135 : /* scratch buffer for M, use buffer for inverse MS mask spectrum */ /* Q( q_spec ) */
1136 : Word16 q_spec,
1137 : Word16 *mdct_stereo_mode, /* output Q0*/
1138 : Word16 *msMask, /* output Q0*/
1139 : const Word16 nBitsAvailable /*Q0*/ )
1140 : {
1141 138277 : Word16 length = sfbParam->sfbOffset[sfbParam->nBandsStereoCore]; /* Q0 */
1142 :
1143 : Word16 bitsL, bitsR, bitsM, bitsS;
1144 : Word16 bitsBW, bitsLR, bitsMS;
1145 : Word16 sfb, i;
1146 : Word16 nMSOn; /* Number of MS active bands */
1147 : Word16 quantSpecL[N_MAX];
1148 : Word16 quantSpecR[N_MAX];
1149 : Word16 quantSpecM[N_MAX];
1150 : Word16 quantSpecS[N_MAX];
1151 : RC_CONTEXT_MEM ctxMem[4];
1152 : HANDLE_RC_CONTEXT_MEM ctxL, ctxR, ctxM, ctxS;
1153 :
1154 138277 : set16_fx( quantSpecL, 0, N_MAX );
1155 138277 : set16_fx( quantSpecR, 0, N_MAX );
1156 138277 : set16_fx( quantSpecM, 0, N_MAX );
1157 138277 : set16_fx( quantSpecS, 0, N_MAX );
1158 :
1159 138277 : assert( GT_16( nBitsAvailable, 0 ) );
1160 :
1161 138277 : ctxL = &ctxMem[0];
1162 138277 : ctxR = &ctxMem[1];
1163 138277 : ctxM = &ctxMem[2];
1164 138277 : ctxS = &ctxMem[3];
1165 : Word16 specL_e, specR_e, specM_e, specS_e, G_fx, G_e;
1166 : Word16 e_GLR;
1167 : Word32 GLR_fx;
1168 :
1169 138277 : specL_e = sub( Q31, q_spec );
1170 138277 : specR_e = sub( Q31, q_spec );
1171 138277 : specM_e = sub( Q31, q_spec );
1172 138277 : specS_e = sub( Q31, q_spec );
1173 :
1174 138277 : GLR_fx = SQ_gain_estimate_stereo_fx( specL_fx, specL_e, specR_fx, specR_e, nBitsAvailable, length, &e_GLR ); /* Q31-e_GLR */
1175 :
1176 79093373 : FOR( i = 0; i < length; i++ )
1177 : {
1178 78955096 : specM_fx[i] = Mpy_32_32( L_add( specL_fx[i], specR_fx[i] ), SQRT2_OVER_2_FIXED ); // Q( q_spec )
1179 78955096 : move32();
1180 78955096 : specS_fx[i] = Mpy_32_32( L_sub( specL_fx[i], specR_fx[i] ), SQRT2_OVER_2_FIXED ); // Q( q_spec )
1181 78955096 : move32();
1182 : }
1183 :
1184 138277 : G_fx = extract_h( GLR_fx ); /* seems to be favourable to underestimate a bit Q15-e_GLR*/
1185 138277 : G_e = sub( e_GLR, 1 );
1186 :
1187 : /* do the full spectrum estimates already here, as side effect we get the quantized spectra... */
1188 138277 : bitsLR = add( QuantSpecEstimateBits_fx( specL_fx, specL_e, G_fx, G_e, length, nBitsAvailable, quantSpecL ), QuantSpecEstimateBits_fx( specR_fx, specR_e, G_fx, G_e, length, nBitsAvailable, quantSpecR ) ); /* Q0 */
1189 138277 : bitsMS = add( QuantSpecEstimateBits_fx( specM_fx, specM_e, G_fx, G_e, length, nBitsAvailable, quantSpecM ), QuantSpecEstimateBits_fx( specS_fx, specS_e, G_fx, G_e, length, nBitsAvailable, quantSpecS ) ); /* Q0 */
1190 :
1191 : /* clean-up MS scratch buffers */
1192 138277 : set32_fx( specM_fx, 0, length );
1193 138277 : set32_fx( specS_fx, 0, length );
1194 :
1195 138277 : nMSOn = 0;
1196 138277 : move16();
1197 138277 : bitsBW = 0;
1198 138277 : move16();
1199 :
1200 138277 : RCcontextMapping_encode2_estimate_bandWise_start_fx( quantSpecL, length, nBitsAvailable, ctxL );
1201 138277 : RCcontextMapping_encode2_estimate_bandWise_start_fx( quantSpecR, length, nBitsAvailable, ctxR );
1202 :
1203 138277 : bitsBW = add( bitsBW, RCcontextMapping_encode2_estimate_bandWise_start_fx( quantSpecM, length, nBitsAvailable, ctxM ) ); /* Q0 */
1204 138277 : bitsBW = add( bitsBW, RCcontextMapping_encode2_estimate_bandWise_start_fx( quantSpecS, length, nBitsAvailable, ctxS ) ); /* Q0 */
1205 :
1206 : /*find_max_lastnz(ctxL,ctxR,ctxM,ctxS);*/
1207 :
1208 5626324 : FOR( sfb = 0; sfb < sfbParam->nBandsStereoCore; sfb++ )
1209 : {
1210 5488047 : const Word16 startline = sfbParam->sfbOffset[sfb];
1211 5488047 : move16();
1212 5488047 : const Word16 endline = sfbParam->sfbOffset[sfb + 1];
1213 5488047 : move16();
1214 :
1215 5488047 : bitsL = RCcontextMapping_encode2_estimate_bandWise_fx( quantSpecL, startline, endline, ctxL );
1216 5488047 : bitsR = RCcontextMapping_encode2_estimate_bandWise_fx( quantSpecR, startline, endline, ctxR );
1217 5488047 : bitsM = RCcontextMapping_encode2_estimate_bandWise_fx( quantSpecM, startline, endline, ctxM );
1218 5488047 : bitsS = RCcontextMapping_encode2_estimate_bandWise_fx( quantSpecS, startline, endline, ctxS );
1219 :
1220 5488047 : IF( LE_16( add( bitsM, bitsS ), add( bitsL, bitsR ) ) )
1221 : {
1222 4138178 : msMask[sfb] = 1;
1223 4138178 : move16();
1224 4138178 : nMSOn = add( nMSOn, 1 );
1225 4138178 : context_update_fx( ctxM, ctxL, endline );
1226 4138178 : context_update_fx( ctxS, ctxR, endline );
1227 4138178 : bitsBW = add( bitsBW, add( bitsM, bitsS ) ); /* Q0 */
1228 : }
1229 : ELSE
1230 : {
1231 1349869 : msMask[sfb] = 0;
1232 1349869 : move16();
1233 :
1234 1349869 : context_update_fx( ctxL, ctxM, endline );
1235 1349869 : context_update_fx( ctxR, ctxS, endline );
1236 1349869 : bitsBW = add( bitsBW, add( bitsL, bitsR ) ); /* Q0 */
1237 : }
1238 : }
1239 :
1240 138277 : bitsBW = add( bitsBW, sfbParam->nBandsStereoCore ); /* Signaling bits Q0*/
1241 :
1242 138277 : IF( LT_16( bitsLR, bitsBW ) )
1243 : {
1244 7778 : nMSOn = 0;
1245 7778 : move16();
1246 7778 : set16_fx( msMask, 0, sfbParam->sfbCnt );
1247 7778 : bitsBW = bitsLR;
1248 7778 : move16();
1249 : }
1250 :
1251 138277 : IF( LT_16( bitsMS, bitsBW ) )
1252 : {
1253 81907 : nMSOn = sfbParam->nBandsStereoCore;
1254 81907 : move16();
1255 81907 : set16_fx( msMask, 1, sfbParam->sfbCnt );
1256 : }
1257 :
1258 138277 : *mdct_stereo_mode = SMDCT_BW_MS;
1259 138277 : move16();
1260 138277 : IF( EQ_16( nMSOn, sfbParam->nBandsStereoCore ) )
1261 : {
1262 81907 : *mdct_stereo_mode = SMDCT_MS_FULL;
1263 81907 : move16();
1264 : }
1265 56370 : ELSE IF( nMSOn == 0 )
1266 : {
1267 6637 : *mdct_stereo_mode = SMDCT_DUAL_MONO;
1268 6637 : move16();
1269 : }
1270 :
1271 138277 : return;
1272 : }
1273 :
1274 :
1275 : /*-----------------------------------------------------------------------*
1276 : * initMdctStereoEncData()
1277 : *
1278 : * initialize encoder mdct stereo structure
1279 : *-----------------------------------------------------------------------*/
1280 :
1281 73784 : void initMdctStereoEncData_fx(
1282 : STEREO_MDCT_ENC_DATA *hStereoMdct, /* i/o: mdct stereo parameters structure */
1283 : const IVAS_FORMAT ivas_format, /* i : IVAS format */
1284 : const Word16 element_mode, /* i : element mode Q0*/
1285 : const Word32 element_brate, /* i : element bitrate Q0*/
1286 : const Word16 bwidth, /* i : bandwidth Q0*/
1287 : const Word16 igf, /* i : flag indicating IGF activity Q0*/
1288 : const H_IGF_GRID hIgfGrid, /* i : IGF grid setup */
1289 : const Word16 mem_init /* i : initialize memory after malloc Q0*/
1290 : )
1291 : {
1292 : Word16 tcx_coded_lines;
1293 :
1294 73784 : tcx_coded_lines = getNumTcxCodedLines( bwidth ); /* Q0 */
1295 :
1296 : /*initialize mdct stereo mode*/
1297 73784 : set16_fx( hStereoMdct->mdct_stereo_mode, -1, 2 );
1298 :
1299 : /*Initialize sfb parameteres for TCX20 */
1300 73784 : stereo_mdct_init_bands_fx( tcx_coded_lines, TCX_20_CORE, element_brate, igf, igf ? &hIgfGrid[IGF_GRID_LB_NORM] : NULL, &hStereoMdct->stbParamsTCX20.sfbOffset[0], &hStereoMdct->stbParamsTCX20.sfbCnt );
1301 :
1302 : /*Initialize sfb parameteres for TCX10 */
1303 73784 : stereo_mdct_init_bands_fx( tcx_coded_lines, TCX_10_CORE, element_brate, igf, igf ? &hIgfGrid[IGF_GRID_LB_SHORT] : NULL,
1304 : &hStereoMdct->stbParamsTCX10.sfbOffset[0], &hStereoMdct->stbParamsTCX10.sfbCnt );
1305 :
1306 : /*Initialize sfb parameteres for transitions */
1307 73784 : stereo_mdct_init_bands_fx( tcx_coded_lines, -1, element_brate, igf, igf ? &hIgfGrid[IGF_GRID_LB_TRAN] : NULL,
1308 : &hStereoMdct->stbParamsTCX20afterACELP.sfbOffset[0], &hStereoMdct->stbParamsTCX20afterACELP.sfbCnt );
1309 :
1310 73784 : set16_fx( hStereoMdct->IGFStereoMode, -1, 2 );
1311 :
1312 73784 : hStereoMdct->split_ratio = SMDCT_EQUAL_RATIO_RANGE; /* Q0 */
1313 73784 : move16();
1314 73784 : set16_fx( hStereoMdct->global_ild, SMDCT_ILD_RANGE >> 1, 2 );
1315 :
1316 73784 : IF( mem_init )
1317 : {
1318 3242 : hStereoMdct->hItd = NULL;
1319 3242 : hStereoMdct->hDft_ana = NULL;
1320 : }
1321 :
1322 73784 : test();
1323 73784 : test();
1324 73784 : IF( !( EQ_16( element_mode, IVAS_CPE_MDCT ) && LE_32( element_brate, MAX_MDCT_ITD_BRATE ) && EQ_32( ivas_format, STEREO_FORMAT ) ) )
1325 : {
1326 24458 : IF( hStereoMdct->hDft_ana != NULL )
1327 : {
1328 25 : free( hStereoMdct->hDft_ana );
1329 25 : hStereoMdct->hDft_ana = NULL;
1330 : }
1331 :
1332 24458 : IF( hStereoMdct->hItd != NULL )
1333 : {
1334 25 : free( hStereoMdct->hItd );
1335 25 : hStereoMdct->hItd = NULL;
1336 : }
1337 : }
1338 :
1339 73784 : return;
1340 : }
1341 :
1342 :
1343 : /*-----------------------------------------------------------------------*
1344 : * initMdctItdHandling()
1345 : *
1346 : * initialize encoder mdct ITD handling structures
1347 : *-----------------------------------------------------------------------*/
1348 :
1349 151 : ivas_error initMdctItdHandling_fx(
1350 : STEREO_MDCT_ENC_DATA *hStereoMdct, /* i/o: mdct stereo parameters structure */
1351 : const Word32 input_Fs /* i : input sampling rate Q0*/
1352 : )
1353 : {
1354 151 : test();
1355 151 : test();
1356 151 : IF( hStereoMdct->hItd == NULL )
1357 : {
1358 67 : IF( ( hStereoMdct->hItd = (ITD_DATA_HANDLE) malloc( sizeof( ITD_DATA ) ) ) == NULL )
1359 : {
1360 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for ITD data\n" ) );
1361 : }
1362 : }
1363 :
1364 151 : test();
1365 151 : test();
1366 151 : IF( hStereoMdct->hDft_ana == NULL )
1367 : {
1368 67 : IF( ( hStereoMdct->hDft_ana = (DFT_ANA_HANDLE) malloc( sizeof( DFT_ANA ) ) ) == NULL )
1369 : {
1370 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for ITD data\n" ) );
1371 : }
1372 : }
1373 :
1374 : /*Initialize ITD parameters*/
1375 151 : stereo_enc_itd_init_fx( hStereoMdct->hItd );
1376 :
1377 : /*Initialize DFT analysis parameters*/
1378 151 : dft_ana_init_fx( hStereoMdct->hDft_ana, input_Fs );
1379 :
1380 151 : return IVAS_ERR_OK;
1381 : }
1382 :
1383 : /*-------------------------------------------------------------------------
1384 : * stereo_mdct_enc_destroy()
1385 : *
1386 : * destroy MDCT stereo handle
1387 : *-------------------------------------------------------------------------*/
1388 :
1389 1105 : void stereo_mdct_enc_destroy_fx(
1390 : STEREO_MDCT_ENC_DATA_HANDLE *hStereoMdct /* i/o: encoder MDCT stereo handle */
1391 : )
1392 : {
1393 1105 : IF( ( *hStereoMdct )->hDft_ana != NULL )
1394 : {
1395 42 : free( ( *hStereoMdct )->hDft_ana );
1396 42 : ( *hStereoMdct )->hDft_ana = NULL;
1397 : }
1398 :
1399 1105 : IF( ( *hStereoMdct )->hItd != NULL )
1400 : {
1401 42 : free( ( *hStereoMdct )->hItd );
1402 42 : ( *hStereoMdct )->hItd = NULL;
1403 : }
1404 :
1405 1105 : free( *hStereoMdct );
1406 1105 : *hStereoMdct = NULL;
1407 :
1408 1105 : return;
1409 : }
|