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.h"
39 : #include "prot.h"
40 : #include "ivas_rom_com.h"
41 : #include "ivas_rom_enc.h"
42 : #include "wmc_auto.h"
43 : #include "stat_enc.h"
44 : #ifdef DEBUG_PLOT
45 : #include "deb_out.h"
46 : #endif
47 :
48 :
49 : /*-------------------------------------------------------------------*
50 : * Local constants
51 : *-------------------------------------------------------------------*/
52 :
53 : #define OFFSET_BITS_TCX20 126
54 : #define OFFSET_BITS_TCX10 222
55 :
56 :
57 : /*-------------------------------------------------------------------*
58 : * Local function prototypes
59 : *-------------------------------------------------------------------*/
60 :
61 : static void convertToBwMS( const int16_t startLine, const int16_t stopLine, float x0[], float x1[], const float norm_fac );
62 :
63 : void convertToMS( const int16_t L_frame, float x0[], float x1[], const float norm_fac );
64 :
65 : static float GetChannelEnergyRatio( Encoder_State **st, const int16_t iFirstSubframe, const int16_t iLastSubframe, const uint8_t ratioInRmsDomain );
66 :
67 : static void MsStereoDecision( STEREO_MDCT_BAND_PARAMETERS *sfbParam, float *specL, float *specR, float *specM, float *specS, int16_t *mdct_stereo_mode, int16_t *msMask, const int16_t nBitsAvailable );
68 :
69 :
70 : /*-------------------------------------------------------------------*
71 : * dft_ana_init()
72 : *
73 : * Initialization function for dft analysis handle within
74 : * MDCT-stereo
75 : *-------------------------------------------------------------------*/
76 :
77 151 : static void dft_ana_init(
78 : DFT_ANA_HANDLE hDft_ana, /*i : DFT analysis handle */
79 : const int32_t input_Fs /*i : Input sampling frequency */
80 : )
81 : {
82 151 : hDft_ana->N = (int16_t) ( STEREO_DFT_HOP_MAX_ENC * input_Fs / 48000 );
83 151 : hDft_ana->NFFT = (int16_t) ( STEREO_DFT_N_MAX_ENC * input_Fs / 48000 );
84 151 : hDft_ana->dft_ovl = (int16_t) ( STEREO_DFT_OVL_MAX * input_Fs / 48000 );
85 151 : hDft_ana->dft_zp = (int16_t) ( STEREO_DFT_ZP_MAX_ENC * input_Fs / 48000 );
86 :
87 151 : hDft_ana->dft_trigo_32k = dft_trigo_32k;
88 :
89 151 : if ( input_Fs == 16000 )
90 : {
91 5 : hDft_ana->dft_trigo = dft_trigo_32k;
92 5 : hDft_ana->dft_trigo_step = STEREO_DFT_TRIGO_SRATE_16k_STEP;
93 5 : hDft_ana->win_ana = win_ana_16k;
94 : }
95 146 : else if ( input_Fs == 32000 )
96 : {
97 59 : hDft_ana->dft_trigo = dft_trigo_32k;
98 59 : hDft_ana->dft_trigo_step = STEREO_DFT_TRIGO_SRATE_32k_STEP;
99 59 : hDft_ana->win_ana = win_ana_32k;
100 : }
101 : else
102 : {
103 87 : assert( input_Fs == 48000 );
104 87 : hDft_ana->dft_trigo = dft_trigo_48k;
105 87 : hDft_ana->dft_trigo_step = STEREO_DFT_TRIGO_SRATE_48k_STEP;
106 87 : hDft_ana->win_ana = win_ana_48k;
107 : }
108 :
109 151 : return;
110 : }
111 :
112 :
113 : /*-------------------------------------------------------------------*
114 : * write_itd_data()
115 : *
116 : * Bitstream writing of ITDs
117 : *-------------------------------------------------------------------*/
118 :
119 11754 : static void write_itd_data(
120 : ITD_DATA_HANDLE hItd, /* i : ITD data handle */
121 : BSTR_ENC_HANDLE hBstr /* i/o: bitstream handle */
122 : )
123 : {
124 : int16_t k_offset;
125 : int16_t itd;
126 :
127 11754 : k_offset = 1;
128 :
129 11754 : push_next_indice( hBstr, ( hItd->itd[k_offset] != 0 ), STEREO_DFT_ITD_MODE_NBITS );
130 :
131 11754 : if ( hItd->itd[k_offset] )
132 : {
133 3533 : itd = hItd->itd_index[k_offset];
134 3533 : if ( itd > 255 )
135 : {
136 2395 : itd -= 256;
137 :
138 2395 : if ( itd < 20 )
139 : {
140 1604 : push_next_indice( hBstr, 1, 1 ); /* use Huffman*/
141 1604 : push_next_indice( hBstr, 1, 1 ); /* negative */
142 1604 : push_next_indice( hBstr, dft_code_itd[itd], dft_len_itd[itd] );
143 : }
144 : else
145 : {
146 791 : push_next_indice( hBstr, 0, 1 ); /* don't use Huffman */
147 791 : push_next_indice( hBstr, 1, 1 ); /* negative */
148 791 : push_next_indice( hBstr, itd, STEREO_DFT_ITD_NBITS - 1 );
149 : }
150 : }
151 : else
152 : {
153 1138 : if ( itd < 20 )
154 : {
155 866 : push_next_indice( hBstr, 1, 1 ); /* use Huffman*/
156 866 : push_next_indice( hBstr, 0, 1 ); /* positive */
157 866 : push_next_indice( hBstr, dft_code_itd[itd], dft_len_itd[itd] );
158 : }
159 : else
160 : {
161 : /* don't use Huffman and positive*/
162 272 : push_next_indice( hBstr, itd, STEREO_DFT_ITD_NBITS + 1 );
163 : }
164 : }
165 : }
166 11754 : return;
167 : }
168 :
169 :
170 : /*-------------------------------------------------------------------*
171 : * stereo_coder_tcx()
172 : *
173 : *
174 : *-------------------------------------------------------------------*/
175 :
176 140333 : void stereo_coder_tcx(
177 : STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct, /* i/o: Stereo MDCT encoder structure */
178 : Encoder_State **sts, /* i/o: encoder state structure */
179 : int16_t ms_mask[NB_DIV][MAX_SFB], /* i : bandwise MS mask */
180 : float *mdst_spectrum[CPE_CHANNELS][NB_DIV], /* i/o: MDST spectrum */
181 : float *inv_spectrum[CPE_CHANNELS][NB_DIV], /* i/o: inverse spectrum */
182 : float *inv_mdst_spectrum[CPE_CHANNELS][NB_DIV], /* i/o: inverse MDST spectrum */
183 : const int16_t mct_on /* i : flag mct block (1) or stereo (0) */
184 : )
185 : {
186 140333 : STEREO_MDCT_BAND_PARAMETERS *sfbConf = NULL;
187 : float nrgRatio[CPE_CHANNELS];
188 : float nonQNrgRatio[CPE_CHANNELS];
189 : int16_t k;
190 : int16_t nSubframes, L_frameTCX;
191 : int16_t nAvailBitsMS[NB_DIV];
192 140333 : push_wmops( "stereo_coder_tcx" );
193 :
194 140333 : set_s( nAvailBitsMS, 0, NB_DIV );
195 :
196 140333 : nSubframes = ( sts[0]->core == TCX_20_CORE && sts[1]->core == TCX_20_CORE ) ? 1 : NB_DIV;
197 140333 : L_frameTCX = sts[0]->hTcxEnc->L_frameTCX / nSubframes;
198 :
199 140333 : set_s( &ms_mask[0][0], 0, MAX_SFB );
200 140333 : set_s( &ms_mask[1][0], 0, MAX_SFB );
201 :
202 140333 : if ( !mct_on )
203 : {
204 42789 : if ( sts[0]->core == sts[1]->core )
205 : {
206 86203 : for ( k = 0; k < nSubframes; k++ )
207 : {
208 43676 : nonQNrgRatio[k] = GetChannelEnergyRatio( sts, k, k, 1 );
209 :
210 43676 : hStereoMdct->global_ild[k] = max( 1, min( SMDCT_ILD_RANGE - 1, (int16_t) ( SMDCT_ILD_RANGE * nonQNrgRatio[k] + 0.5f ) ) );
211 43676 : nrgRatio[k] = (float) SMDCT_ILD_RANGE / hStereoMdct->global_ild[k] - 1; /* nrgRatio = nrg[1]/nrg[0] */
212 :
213 43676 : nonQNrgRatio[k] = max( 0.5f / SMDCT_ILD_RANGE, min( ( SMDCT_ILD_RANGE - 0.5f ) / SMDCT_ILD_RANGE, nonQNrgRatio[k] ) );
214 43676 : nonQNrgRatio[k] = 1.0f / nonQNrgRatio[k] - 1.0f;
215 : }
216 : #ifdef DEBUG_MODE_MDCT
217 : {
218 : float tmp;
219 : for ( k = 0; k < 2; k++ )
220 : {
221 : tmp = GetChannelEnergyRatio( sts, k, k, 1 );
222 : if ( nSubframes == 1 && k == 1 )
223 : {
224 : tmp = 0;
225 : }
226 : dbgwrite( &tmp, sizeof( float ), 1, 1, "./res/ild" );
227 : }
228 : }
229 : #endif
230 : }
231 : else
232 : {
233 262 : nonQNrgRatio[0] = nonQNrgRatio[1] = GetChannelEnergyRatio( sts, 0, nSubframes - 1,
234 : 1 );
235 262 : hStereoMdct->global_ild[0] = max( 1, min( SMDCT_ILD_RANGE - 1, (int16_t) ( SMDCT_ILD_RANGE * nonQNrgRatio[0] + 0.5f ) ) );
236 262 : nrgRatio[0] = nrgRatio[1] = (float) SMDCT_ILD_RANGE / hStereoMdct->global_ild[0] - 1; /* nrgRatio = nrg[1]/nrg[0] */
237 262 : nonQNrgRatio[0] = nonQNrgRatio[1] = max( 0.5f / SMDCT_ILD_RANGE, min( ( SMDCT_ILD_RANGE - 0.5f ) / SMDCT_ILD_RANGE, nonQNrgRatio[0] ) );
238 262 : nonQNrgRatio[0] = nonQNrgRatio[1] = 1.0f / nonQNrgRatio[0] - 1.0f;
239 : #ifdef DEBUG_MODE_MDCT
240 : {
241 : float tmp;
242 : for ( k = 0; k < 2; k++ )
243 : {
244 : tmp = GetChannelEnergyRatio( sts, 0, nSubframes - 1, 1 );
245 : if ( nSubframes == 1 && k == 1 )
246 : {
247 : tmp = 0;
248 : }
249 : dbgwrite( &tmp, sizeof( float ), 1, 1, "./res/ild" );
250 : }
251 : }
252 : #endif
253 : }
254 :
255 86989 : for ( k = 0; k < nSubframes; k++ )
256 : {
257 44200 : if ( ( nrgRatio[k] > 1.0f ) && ( k < ( ( sts[1]->core == TCX_20_CORE ) ? 1 : NB_DIV ) ) )
258 : {
259 21449 : L_frameTCX = sts[1]->hTcxEnc->L_frameTCX + ( ( sts[1]->last_core == 0 ) ? sts[1]->hTcxEnc->L_frameTCX / 4 : 0 );
260 21449 : L_frameTCX /= ( ( sts[1]->core == TCX_20_CORE ) ? 1 : NB_DIV );
261 :
262 21449 : v_multc( sts[1]->hTcxEnc->spectrum[k], 1.0f / nrgRatio[k], sts[1]->hTcxEnc->spectrum[k], L_frameTCX );
263 21449 : v_multc( mdst_spectrum[1][k], 1.0f / nrgRatio[k], mdst_spectrum[1][k], L_frameTCX );
264 : }
265 22751 : else if ( ( nrgRatio[k] < 1.0f ) && k < ( ( sts[0]->core == TCX_20_CORE ) ? 1 : NB_DIV ) )
266 : {
267 9562 : L_frameTCX = sts[0]->hTcxEnc->L_frameTCX + ( ( sts[0]->last_core == 0 ) ? sts[0]->hTcxEnc->L_frameTCX / 4 : 0 );
268 9562 : L_frameTCX /= ( ( sts[0]->core == TCX_20_CORE ) ? 1 : NB_DIV );
269 :
270 9562 : v_multc( sts[0]->hTcxEnc->spectrum[k], nrgRatio[k], sts[0]->hTcxEnc->spectrum[k], L_frameTCX );
271 9562 : v_multc( mdst_spectrum[0][k], nrgRatio[k], mdst_spectrum[0][k], L_frameTCX );
272 : }
273 : }
274 : }
275 :
276 140333 : if (
277 : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
278 : hStereoMdct->fDualMono ||
279 : #endif
280 140333 : ( sts[0]->hTcxEnc->transform_type[0] != sts[1]->hTcxEnc->transform_type[0] ) || ( sts[0]->hTcxEnc->transform_type[1] != sts[1]->hTcxEnc->transform_type[1] ) || ( sts[0]->last_core != sts[1]->last_core && ( sts[0]->last_core == ACELP_CORE || sts[1]->last_core == ACELP_CORE ) ) || sts[0]->last_core == ACELP_CORE || sts[1]->last_core == ACELP_CORE )
281 : {
282 934 : hStereoMdct->mdct_stereo_mode[0] = SMDCT_DUAL_MONO;
283 934 : hStereoMdct->mdct_stereo_mode[1] = SMDCT_DUAL_MONO;
284 :
285 : #ifdef DEBUG_MODE_MDCT
286 : for ( k = 0; k < 2; k++ )
287 : {
288 : nAvailBitsMS[k] = -1;
289 : dbgwrite( &nAvailBitsMS[k], sizeof( int16_t ), 1, 1, "./res/nAvailBitsMS" );
290 : }
291 : #endif
292 :
293 934 : if ( sts[0]->igf )
294 : {
295 664 : hStereoMdct->IGFStereoMode[0] = SMDCT_DUAL_MONO;
296 664 : hStereoMdct->IGFStereoMode[1] = SMDCT_DUAL_MONO;
297 : }
298 : #ifdef DEBUG_MODE_MDCT
299 : /* MDCT stereo data */
300 : {
301 : float Em[2];
302 : int16_t ch;
303 : getChannelEnergies( sts, Em, 2 );
304 : dbgwrite( Em, sizeof( float ), 2, 1, "./res/Ech" );
305 : for ( k = 0; k < 2; k++ )
306 : {
307 : dbgwrite( &hStereoMdct->global_ild[k], sizeof( int16_t ), 1, 1, "./res/ild_q" );
308 : dbgwrite( &hStereoMdct->mdct_stereo_mode[k], sizeof( int16_t ), 1, 1, "./res/stereo_mode" );
309 : dbgwrite( &hStereoMdct->IGFStereoMode[k], sizeof( int16_t ), 1, 1, "./res/stereo_mode_ifg" );
310 : dbgwrite( ms_mask[k], sizeof( int16_t ), 70, 1, "./res/ms_mask" );
311 : }
312 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
313 : {
314 : for ( k = 0; k < 2; k++ )
315 : {
316 : dbgwrite( sts[ch]->hTcxEnc->spectrum[k], sizeof( float ), 640, 1, "./res/MDCT_spec_after_stereo" );
317 : dbgwrite( mdst_spectrum[ch][k], sizeof( float ), 640, 1, "./res/MDST_spec_after_stereo" );
318 : }
319 : }
320 : }
321 : #endif
322 934 : hStereoMdct->sw_uncorr = 1;
323 :
324 934 : pop_wmops();
325 934 : return;
326 : }
327 : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
328 : else if ( hStereoMdct->fMSstereo )
329 : {
330 : hStereoMdct->mdct_stereo_mode[0] = SMDCT_MS_FULL;
331 : hStereoMdct->mdct_stereo_mode[1] = SMDCT_MS_FULL;
332 : if ( sts[0]->igf )
333 : {
334 : hStereoMdct->IGFStereoMode[0] = SMDCT_MS_FULL;
335 : hStereoMdct->IGFStereoMode[1] = SMDCT_MS_FULL;
336 : }
337 : for ( k = 0; k < nSubframes; k++ )
338 : {
339 : convertToMS( L_frameTCX, sts[0]->hTcxEnc->spectrum[k], sts[1]->hTcxEnc->spectrum[k], SQRT2_OVER_2 );
340 :
341 : /* Make sure that the MDST is processed in the correct way also */
342 : set_s( &ms_mask[k][0], 1, MAX_SFB );
343 : }
344 :
345 : pop_wmops();
346 : return;
347 : }
348 : #endif
349 : else /* decide based on signal */
350 : {
351 281969 : for ( k = 0; k < nSubframes; k++ )
352 : {
353 142570 : sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
354 142570 : if ( sts[0]->last_core == ACELP_CORE )
355 : {
356 0 : sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
357 : }
358 142570 : if ( ( sts[0]->element_brate < IVAS_80k ) && ( sts[0]->core == sts[1]->core ) && !mct_on ) /* band-wise HF ILD alignment to increase channel compaction */
359 : {
360 25298 : int16_t sfb = 1;
361 :
362 417842 : while ( ( sfb < sfbConf->nBandsStereoCore ) && ( sfbConf->sfbOffset[sfb + 1] - sfbConf->sfbOffset[sfb] < 12 ) )
363 : {
364 392544 : sfb++; /* find start offset */
365 : }
366 :
367 420696 : for ( sfb--; sfb < sfbConf->nBandsStereoCore; sfb++ ) /* start one SFB early for the fade-in */
368 : {
369 395398 : const int16_t startLine = sfbConf->sfbOffset[sfb];
370 395398 : const int16_t endLine = sfbConf->sfbOffset[sfb + 1];
371 395398 : const int16_t sfbWidth = endLine - startLine;
372 :
373 395398 : nrgRatio[0] = sum2_f( &sts[0]->hTcxEnc->spectrum[k][startLine], sfbWidth );
374 395398 : nrgRatio[1] = sum2_f( &sts[1]->hTcxEnc->spectrum[k][startLine], sfbWidth );
375 :
376 395398 : if ( !sts[0]->hTcxEnc->fUseTns[k] && !sts[1]->hTcxEnc->fUseTns[k] ) /* no TNS in either ch */
377 : {
378 331658 : nrgRatio[0] += sum2_f( &mdst_spectrum[0][k][startLine], sfbWidth );
379 331658 : nrgRatio[1] += sum2_f( &mdst_spectrum[1][k][startLine], sfbWidth );
380 : }
381 395398 : if ( ( nrgRatio[0] > 0.f ) && ( nrgRatio[1] > 0.f ) && ( nrgRatio[0] != nrgRatio[1] ) )
382 : {
383 395068 : float fTemp = 0.5f * ( nrgRatio[0] + nrgRatio[1] );
384 395068 : nrgRatio[0] = sqrtf( fTemp / nrgRatio[0] );
385 395068 : nrgRatio[1] = sqrtf( fTemp / nrgRatio[1] );
386 :
387 395068 : nrgRatio[0] = max( 0.25f, min( 4.f, nrgRatio[0] ) );
388 395068 : nrgRatio[1] = max( 0.25f, min( 4.f, nrgRatio[1] ) );
389 :
390 395068 : if ( ( sfbWidth < 12 && sfb + 1 < sfbConf->nBandsStereoCore ) || ( sts[0]->element_brate > IVAS_48k ) ) /* attenuate ILD alignment in the first SFB or at 64k */
391 : {
392 208776 : nrgRatio[0] = powf( nrgRatio[0], 0.25f );
393 208776 : nrgRatio[1] = powf( nrgRatio[1], 0.25f );
394 : }
395 : else
396 : {
397 186292 : nrgRatio[0] = sqrtf( nrgRatio[0] );
398 186292 : nrgRatio[1] = sqrtf( nrgRatio[1] );
399 : }
400 395068 : v_multc( &sts[0]->hTcxEnc->spectrum[k][startLine], nrgRatio[0], &sts[0]->hTcxEnc->spectrum[k][startLine], sfbWidth );
401 395068 : v_multc( &mdst_spectrum[0][k][startLine], nrgRatio[0], &mdst_spectrum[0][k][startLine], sfbWidth );
402 :
403 395068 : v_multc( &sts[1]->hTcxEnc->spectrum[k][startLine], nrgRatio[1], &sts[1]->hTcxEnc->spectrum[k][startLine], sfbWidth );
404 395068 : v_multc( &mdst_spectrum[1][k][startLine], nrgRatio[1], &mdst_spectrum[1][k][startLine], sfbWidth );
405 : }
406 : }
407 : }
408 :
409 : /* set mask to zero */
410 142570 : set_s( &ms_mask[k][0], 0, MAX_SFB );
411 :
412 142570 : nAvailBitsMS[k] = ( ( mct_on ? 2 * sts[0]->bits_frame_channel : sts[0]->bits_frame_nominal ) - sts[0]->side_bits_frame_channel - sts[1]->side_bits_frame_channel - ( nSubframes == 2 ? OFFSET_BITS_TCX10 : OFFSET_BITS_TCX20 ) ) / nSubframes;
413 :
414 142570 : if ( mct_on && nAvailBitsMS[k] <= 0 ) /*Force M/S when bit-budget is low for MCT*/
415 : {
416 0 : hStereoMdct->mdct_stereo_mode[k] = 1;
417 0 : hStereoMdct->IGFStereoMode[k] = 1;
418 0 : set_s( ms_mask[k], 1, sfbConf->sfbCnt );
419 : }
420 : else
421 : {
422 142570 : MsStereoDecision( sfbConf, sts[0]->hTcxEnc->spectrum[k], sts[1]->hTcxEnc->spectrum[k], inv_spectrum[0][k], inv_spectrum[1][k], &hStereoMdct->mdct_stereo_mode[k], &ms_mask[k][0], nAvailBitsMS[k] );
423 :
424 142570 : if ( sts[0]->igf )
425 : {
426 93247 : IGFEncStereoEncoder( sfbConf, sts[0]->hIGFEnc, sts[0]->hTcxEnc->spectrum[k], sts[1]->hTcxEnc->spectrum[k], &ms_mask[k][0],
427 93247 : &hStereoMdct->IGFStereoMode[k], hStereoMdct->mdct_stereo_mode[k], sts[0]->core == TCX_20_CORE, sts[0]->last_core == ACELP_CORE );
428 : }
429 : else
430 : {
431 49323 : hStereoMdct->IGFStereoMode[k] = hStereoMdct->mdct_stereo_mode[k];
432 : }
433 : }
434 :
435 142570 : if ( hStereoMdct->mdct_stereo_mode[k] != SMDCT_DUAL_MONO || hStereoMdct->IGFStereoMode[k] != SMDCT_DUAL_MONO )
436 : {
437 135131 : ms_inv_mask_processing( hStereoMdct, sts, ms_mask, k, sts[0]->hTcxEnc->spectrum[k], sts[1]->hTcxEnc->spectrum[k], inv_spectrum[0][k], inv_spectrum[1][k], sfbConf->sfbCnt );
438 135131 : ms_processing( hStereoMdct, sts, ms_mask, k, sts[0]->hTcxEnc->spectrum[k], sts[1]->hTcxEnc->spectrum[k], sfbConf->sfbCnt );
439 :
440 135131 : if ( !sts[0]->hTcxEnc->fUseTns[k] && !sts[1]->hTcxEnc->fUseTns[k] )
441 : {
442 112243 : sts[0]->hTcxEnc->tns_ms_flag[k] = 1;
443 112243 : sts[1]->hTcxEnc->tns_ms_flag[k] = 1;
444 112243 : ms_inv_mask_processing( hStereoMdct, sts, ms_mask, k, mdst_spectrum[0][k], mdst_spectrum[1][k], inv_mdst_spectrum[0][k], inv_mdst_spectrum[1][k], -1 );
445 112243 : ms_processing( hStereoMdct, sts, ms_mask, k, mdst_spectrum[0][k], mdst_spectrum[1][k], sfbConf->sfbCnt );
446 : }
447 : }
448 : } /* for k */
449 :
450 : #ifdef DEBUG_MODE_MDCT
451 : for ( k = 0; k < 2; k++ )
452 : {
453 : dbgwrite( &nAvailBitsMS[k], sizeof( int16_t ), 1, 1, "./res/nAvailBitsMS" );
454 : }
455 : #endif
456 : }
457 : /* for bitrate switching determine correlation depending on m/s decision */
458 : {
459 : int16_t ms_bands[2];
460 : float sw_uncorr[2], sw_uncorr_mean;
461 281969 : for ( k = 0; k < nSubframes; k++ )
462 : {
463 142570 : sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
464 142570 : ms_bands[k] = sum_s( ms_mask[k], sfbConf->nBandsStereoCore );
465 142570 : sw_uncorr[k] = ( 1.0f - (float) ms_bands[k] / sfbConf->nBandsStereoCore );
466 : }
467 139399 : if ( sts[0]->core == TCX_20_CORE )
468 : {
469 136228 : sw_uncorr_mean = sw_uncorr[0];
470 : }
471 : else
472 : {
473 3171 : sw_uncorr_mean = ( sw_uncorr[0] + sw_uncorr[1] ) * 0.5f;
474 : }
475 139399 : hStereoMdct->sw_uncorr = ( sw_uncorr_mean > 0.6f );
476 : }
477 :
478 139399 : pop_wmops();
479 :
480 : #ifdef DEBUG_MODE_MDCT
481 : /* MDCT stereo data */
482 : {
483 : float Em[2];
484 : int16_t ch;
485 : getChannelEnergies( sts, Em, 2 );
486 : dbgwrite( Em, sizeof( float ), 2, 1, "./res/Ech" );
487 : for ( k = 0; k < 2; k++ )
488 : {
489 : dbgwrite( &hStereoMdct->global_ild[k], sizeof( int16_t ), 1, 1, "./res/ild_q" );
490 : dbgwrite( &hStereoMdct->mdct_stereo_mode[k], sizeof( int16_t ), 1, 1, "./res/stereo_mode" );
491 : dbgwrite( &hStereoMdct->IGFStereoMode[k], sizeof( int16_t ), 1, 1, "./res/stereo_mode_ifg" );
492 : dbgwrite( ms_mask[k], sizeof( int16_t ), 70, 1, "./res/ms_mask" );
493 : }
494 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
495 : {
496 : for ( k = 0; k < 2; k++ )
497 : {
498 : dbgwrite( sts[ch]->hTcxEnc->spectrum[k], sizeof( float ), 640, 1, "./res/MDCT_spec_after_stereo" );
499 : dbgwrite( mdst_spectrum[ch][k], sizeof( float ), 640, 1, "./res/MDST_spec_after_stereo" );
500 : }
501 : }
502 : }
503 : #endif
504 :
505 139399 : return;
506 : }
507 :
508 :
509 : /*-------------------------------------------------------------------*
510 : * ms_processing()
511 : *
512 : *
513 : *-------------------------------------------------------------------*/
514 :
515 247374 : void ms_processing(
516 : STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct, /* i/o: Stereo MDCT encoder structure */
517 : Encoder_State **sts, /* i/o: Encoder state structure */
518 : int16_t ms_mask[NB_DIV][MAX_SFB], /* i : bandwise MS mask */
519 : const int16_t iSubframe, /* i : subframe number */
520 : float x_0[], /* i/o: spectrum 1 */
521 : float x_1[], /* i/o: spectrum 1 */
522 : int16_t maxSfb /* i : number of stereo frequency bands*/
523 : )
524 : {
525 : int16_t sfb;
526 : STEREO_MDCT_BAND_PARAMETERS *sfbConf;
527 :
528 247374 : sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
529 :
530 247374 : if ( sts[0]->last_core == ACELP_CORE )
531 : {
532 0 : assert( sts[1]->last_core == ACELP_CORE );
533 0 : sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
534 : }
535 :
536 247374 : if ( maxSfb == -1 )
537 : {
538 0 : maxSfb = sfbConf->sfbCnt;
539 : }
540 :
541 10878509 : for ( sfb = 0; sfb < maxSfb; sfb++ )
542 : {
543 10631135 : if ( ms_mask[iSubframe][sfb] )
544 : {
545 8715630 : convertToBwMS( sfbConf->sfbOffset[sfb], sfbConf->sfbOffset[sfb + 1], x_0, x_1, SQRT2_OVER_2 );
546 : }
547 : }
548 :
549 247374 : return;
550 : }
551 :
552 :
553 : /*-------------------------------------------------------------------*
554 : * ms_inv_mask_processing()
555 : *
556 : *
557 : *-------------------------------------------------------------------*/
558 :
559 247374 : void ms_inv_mask_processing(
560 : STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct, /* i/o: Stereo MDCT encoder structure */
561 : Encoder_State **sts, /* i/o: Encoder state structure */
562 : int16_t ms_mask[NB_DIV][MAX_SFB], /* i : bandwise MS mask */
563 : const int16_t iSubframe, /* i : subframe number */
564 : const float x_0[], /* i : spectrum 1 */
565 : const float x_1[], /* i : spectrum 2 */
566 : float x_inv_0[], /* o : inverse spectrum 1 */
567 : float x_inv_1[], /* o : inverse spectrum 2 */
568 : int16_t maxSfb /* i : number of stereo frequency bands*/
569 : )
570 : {
571 : int16_t sfb;
572 : STEREO_MDCT_BAND_PARAMETERS *sfbConf;
573 : int16_t nSubframes, L_subframeTCX;
574 :
575 247374 : nSubframes = ( sts[0]->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
576 247374 : L_subframeTCX = sts[0]->hTcxEnc->L_frameTCX / nSubframes;
577 247374 : sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
578 :
579 247374 : if ( sts[0]->last_core == ACELP_CORE )
580 : {
581 0 : assert( sts[1]->last_core == ACELP_CORE );
582 0 : sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
583 : }
584 :
585 247374 : if ( maxSfb == -1 )
586 : {
587 112243 : maxSfb = sfbConf->sfbCnt;
588 : }
589 :
590 10878509 : for ( sfb = 0; sfb < maxSfb; sfb++ )
591 : {
592 10631135 : mvr2r( &x_0[sfbConf->sfbOffset[sfb]], &x_inv_0[sfbConf->sfbOffset[sfb]], sfbConf->sfbOffset[sfb + 1] - sfbConf->sfbOffset[sfb] );
593 10631135 : mvr2r( &x_1[sfbConf->sfbOffset[sfb]], &x_inv_1[sfbConf->sfbOffset[sfb]], sfbConf->sfbOffset[sfb + 1] - sfbConf->sfbOffset[sfb] );
594 :
595 10631135 : if ( ms_mask[iSubframe][sfb] == 0 )
596 : {
597 1915505 : convertToBwMS( sfbConf->sfbOffset[sfb], sfbConf->sfbOffset[sfb + 1], x_inv_0, x_inv_1, SQRT2_OVER_2 );
598 : }
599 : }
600 :
601 : /* set rest of inverse spectrum to zero */
602 247374 : if ( L_subframeTCX > sfbConf->sfbOffset[maxSfb] )
603 : {
604 147667 : set_zero( &x_inv_0[sfbConf->sfbOffset[maxSfb]], L_subframeTCX - sfbConf->sfbOffset[maxSfb] );
605 147667 : set_zero( &x_inv_1[sfbConf->sfbOffset[maxSfb]], L_subframeTCX - sfbConf->sfbOffset[maxSfb] );
606 : }
607 :
608 247374 : return;
609 : }
610 :
611 :
612 : /*-------------------------------------------------------------------*
613 : * write_stereo_to_bitstream()
614 : *
615 : *
616 : *-------------------------------------------------------------------*/
617 :
618 135415 : int16_t write_stereo_to_bitstream(
619 : STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct, /* i/o: Stereo MDCT encoder structure */
620 : Encoder_State **sts, /* i/o: Encoder state structure */
621 : int16_t ms_mask[NB_DIV][MAX_SFB], /* i : bandwise MS mask */
622 : const int16_t mct_on, /* i : flag mct block (1) or stereo (0)*/
623 : BSTR_ENC_HANDLE hBstr /* i/o: bitstream handle */
624 : )
625 : {
626 : int16_t i, k, nSubframes;
627 : uint16_t mdct_stereo_mode, stereo_mode_bits;
628 : STEREO_MDCT_BAND_PARAMETERS *sfbConf;
629 135415 : int16_t start_bits = hBstr->nb_bits_tot;
630 :
631 135415 : if ( !mct_on )
632 : {
633 42789 : assert( ( ( sts[0]->hTcxEnc->transform_type[0] == sts[1]->hTcxEnc->transform_type[0] ) && ( sts[0]->hTcxEnc->transform_type[1] == sts[1]->hTcxEnc->transform_type[1] ) ) || ( ( hStereoMdct->mdct_stereo_mode[0] == SMDCT_DUAL_MONO ) && ( hStereoMdct->mdct_stereo_mode[1] == SMDCT_DUAL_MONO ) ) );
634 : }
635 :
636 135415 : nSubframes = ( sts[0]->core == TCX_10_CORE || ( sts[0]->core != sts[1]->core ) ) ? NB_DIV : 1;
637 135415 : sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
638 :
639 135415 : if ( sts[0]->last_core == ACELP_CORE )
640 : {
641 654 : assert( sts[1]->ini_frame == 0 || sts[1]->last_core == ACELP_CORE );
642 654 : sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
643 : }
644 :
645 135415 : if ( hStereoMdct->hItd != NULL )
646 : {
647 11754 : write_itd_data( hStereoMdct->hItd, hBstr );
648 : }
649 :
650 274254 : for ( k = 0; k < nSubframes; k++ )
651 : {
652 138839 : mdct_stereo_mode = 0;
653 138839 : stereo_mode_bits = 1;
654 :
655 138839 : switch ( hStereoMdct->mdct_stereo_mode[k] )
656 : {
657 3799 : case SMDCT_DUAL_MONO:
658 3799 : mdct_stereo_mode = 0;
659 3799 : stereo_mode_bits = 1;
660 3799 : break;
661 83558 : case SMDCT_MS_FULL:
662 83558 : mdct_stereo_mode = 2;
663 83558 : stereo_mode_bits = 2;
664 83558 : break;
665 51482 : case SMDCT_BW_MS:
666 51482 : mdct_stereo_mode = 3;
667 51482 : stereo_mode_bits = 2;
668 51482 : break;
669 0 : default:
670 0 : assert( !"Not supported MDCT stereo mode\n" );
671 : }
672 :
673 138839 : push_next_indice( hBstr, mdct_stereo_mode, stereo_mode_bits );
674 :
675 138839 : if ( !mct_on )
676 : {
677 44200 : if ( ( sts[0]->core == sts[1]->core ) || ( k == 0 ) )
678 : {
679 43938 : push_next_indice( hBstr, hStereoMdct->global_ild[k], SMDCT_GLOBAL_ILD_BITS );
680 : }
681 : }
682 :
683 138839 : if ( hStereoMdct->mdct_stereo_mode[k] == SMDCT_BW_MS )
684 : {
685 2135495 : for ( i = 0; i < sfbConf->nBandsStereoCore; i++ )
686 : {
687 2084013 : push_next_indice( hBstr, ms_mask[k][i] ? 1 : 0, 1 );
688 : }
689 : }
690 :
691 138839 : if ( sts[0]->igf )
692 : {
693 92601 : mdct_stereo_mode = 0;
694 92601 : stereo_mode_bits = 1;
695 92601 : switch ( hStereoMdct->IGFStereoMode[k] )
696 : {
697 37765 : case SMDCT_DUAL_MONO:
698 37765 : mdct_stereo_mode = 0;
699 37765 : stereo_mode_bits = 1;
700 37765 : break;
701 40527 : case SMDCT_MS_FULL:
702 40527 : mdct_stereo_mode = 2;
703 40527 : stereo_mode_bits = 2;
704 40527 : break;
705 14309 : case SMDCT_BW_MS:
706 14309 : mdct_stereo_mode = 3;
707 14309 : stereo_mode_bits = 2;
708 14309 : break;
709 0 : default:
710 0 : assert( !"Not supported MDCT stereo mode\n" );
711 : }
712 :
713 92601 : push_next_indice( hBstr, mdct_stereo_mode, stereo_mode_bits );
714 :
715 :
716 92601 : if ( hStereoMdct->IGFStereoMode[k] == SMDCT_BW_MS )
717 : {
718 90352 : for ( i = sfbConf->nBandsStereoCore; i < sfbConf->sfbCnt; i++ )
719 : {
720 76043 : push_next_indice( hBstr, ms_mask[k][i] ? 1 : 0, 1 );
721 : }
722 : }
723 : }
724 : }
725 :
726 135415 : return ( hBstr->nb_bits_tot - start_bits );
727 : }
728 :
729 : /*-------------------------------------------------------------------*
730 : * Band-wise M/S stereo processing
731 : *
732 : *
733 : *-------------------------------------------------------------------*/
734 :
735 10723329 : static void convertToBwMS(
736 : const int16_t startLine, /* i : start line of sfb */
737 : const int16_t stopLine, /* i : stop line of sfb */
738 : float x0[], /* i/o: mid/left channel coefficients */
739 : float x1[], /* i/o: side/right channel coefficients */
740 : const float norm_fac /* i : normalization factor */
741 : )
742 : {
743 : int16_t j;
744 : float tmpValue;
745 :
746 198947731 : for ( j = startLine; j < stopLine; j++ )
747 : {
748 188224402 : tmpValue = x0[j];
749 188224402 : x0[j] = ( x0[j] + x1[j] ) * norm_fac;
750 188224402 : x1[j] = ( tmpValue - x1[j] ) * norm_fac;
751 : }
752 :
753 10723329 : return;
754 : }
755 :
756 : /*-------------------------------------------------------------------*
757 : * convertToMS()
758 : *
759 : *
760 : *-------------------------------------------------------------------*/
761 :
762 92194 : void convertToMS(
763 : const int16_t L_frame, /* i : frame length */
764 : float x0[], /* i/o: mid/left channel coefficients */
765 : float x1[], /* i/o: side/right channel coefficients */
766 : const float norm_fac /* i : normalization factor */
767 : )
768 : {
769 92194 : convertToBwMS( 0, L_frame, x0, x1, norm_fac );
770 :
771 92194 : return;
772 : }
773 :
774 : /*-------------------------------------------------------------------*
775 : * SQ_gain_estimate_stereo()
776 : *
777 : *
778 : *-------------------------------------------------------------------*/
779 :
780 : /*! r: SQ gain */
781 142570 : static float SQ_gain_estimate_stereo(
782 : float xL[], /* i : L vector to quantize */
783 : float xR[], /* i : R vector to quantize */
784 : const int16_t nbitsSQ, /* i : number of bits targeted */
785 : const int16_t lg /* i : vector size (2048 max) */
786 : )
787 : {
788 : int16_t i, q, iter;
789 : float ener, tmp, target, fac, offset;
790 : float en[N_MAX / 2];
791 : int16_t lg2, lg_4, lg2_4;
792 :
793 142570 : lg_4 = lg >> 2;
794 142570 : lg2_4 = 2 * lg_4;
795 142570 : lg2 = lg2_4 << 2;
796 142570 : i = 0;
797 :
798 142570 : set_f( en, 0.01f, N_MAX / 2 );
799 :
800 : /* energy of quadruples with 9dB offset */
801 : /* 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()*/
802 20376920 : for ( q = 0; q < lg_4; q++ )
803 : {
804 20234350 : ener = 0.01f + xL[i] * xL[i] + xL[i + 1] * xL[i + 1] + xL[i + 2] * xL[i + 2] + xL[i + 3] * xL[i + 3];
805 20234350 : en[q] = log10f( ener ); /* saves a MAC */
806 20234350 : i += 4;
807 : }
808 142570 : i = 0;
809 20376920 : for ( ; q < lg2_4; q++ )
810 : {
811 20234350 : ener = 0.01f + xR[i] * xR[i] + xR[i + 1] * xR[i + 1] + xR[i + 2] * xR[i + 2] + xR[i + 3] * xR[i + 3];
812 20234350 : en[q] = log10f( ener ); /* saves a MAC */
813 20234350 : i += 4;
814 : }
815 :
816 : /* SQ scale: 4 bits / 6 dB per quadruple */
817 142570 : target = 0.15f * (float) ( nbitsSQ - ( lg2 >> 4 ) );
818 142570 : fac = 12.8f;
819 142570 : offset = fac;
820 :
821 : /* find offset (0 to 128 dB with step of 0.125dB) */
822 1568270 : for ( iter = 0; iter < 10; iter++ )
823 : {
824 1425700 : fac *= 0.5f;
825 1425700 : offset -= fac;
826 1425700 : ener = 0.0f;
827 :
828 367544769 : for ( i = 0; i < lg2_4; i++ )
829 : {
830 366703713 : tmp = en[i] - offset;
831 :
832 : /* avoid SV with 1 bin of amp < 0.5f */
833 366703713 : if ( tmp > 0.3f )
834 : {
835 202687374 : ener += tmp;
836 :
837 : /* if ener is above target -> break and increase offset */
838 202687374 : if ( ener > target )
839 : {
840 584644 : offset += fac;
841 584644 : break;
842 : }
843 : }
844 : }
845 : }
846 :
847 : /* return gain */
848 142570 : return powf( 10.0f, 0.45f + 0.5f * offset );
849 : }
850 :
851 : /*-------------------------------------------------------------------*
852 : * QuantSpecEstimateBits()
853 : *
854 : *
855 : *-------------------------------------------------------------------*/
856 :
857 570280 : static int16_t QuantSpecEstimateBits(
858 : float *spec,
859 : float G,
860 : const int16_t length,
861 : const int16_t nBitsAvailable,
862 : int16_t sqQ[] )
863 : {
864 : int16_t stop, sqBits, nEncoded;
865 : int16_t lastnz;
866 :
867 570280 : tcx_scalar_quantization( spec, sqQ, length, G, 0.5f, NULL, 1 );
868 :
869 570280 : stop = 0;
870 :
871 570280 : sqBits = RCcontextMapping_encode2_estimate_no_mem_s17_LCS( sqQ, length, &lastnz, &nEncoded, nBitsAvailable, &stop, 0, NULL );
872 :
873 570280 : if ( stop != 0 )
874 : {
875 79410 : sqBits = stop;
876 : }
877 :
878 570280 : return sqBits;
879 : }
880 :
881 : /*-------------------------------------------------------------------*
882 : * context_update()
883 : *
884 : *
885 : *-------------------------------------------------------------------*/
886 :
887 11338570 : static void context_update(
888 : HANDLE_RC_CONTEXT_MEM ctxSrc,
889 : HANDLE_RC_CONTEXT_MEM ctxTarget,
890 : const int16_t endLine )
891 : {
892 : int16_t last_nz;
893 :
894 : /* check if last_nz of target is smaller than endLine, save and update */
895 11338570 : last_nz = max( ctxTarget->lastnz, endLine );
896 :
897 11338570 : mvc2c( (uint8_t *) ctxSrc, (uint8_t *) ctxTarget, sizeof( RC_CONTEXT_MEM ) );
898 11338570 : ctxTarget->lastnz = last_nz;
899 :
900 11338570 : return;
901 : }
902 :
903 : /*-------------------------------------------------------------------*
904 : * GetChannelEnergyRatio()
905 : *
906 : *
907 : *-------------------------------------------------------------------*/
908 :
909 126579 : static float GetChannelEnergyRatio(
910 : Encoder_State **sts, /* i/o: Encoder state structure */
911 : const int16_t iFirstSubframe,
912 : const int16_t iLastSubframe,
913 : const uint8_t ratioInRmsDomain )
914 : {
915 : int16_t ch, n, i;
916 : float nrg[2];
917 :
918 : /* Calculate energies per channel */
919 379737 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
920 : {
921 253158 : const Encoder_State *st = sts[ch];
922 253158 : const int16_t nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
923 253158 : int16_t L_subframeTCX = st->hTcxEnc->L_frameTCX / nSubframes;
924 :
925 253158 : if ( st->last_core == ACELP_CORE )
926 : {
927 2668 : L_subframeTCX += L_subframeTCX / 4;
928 : }
929 253158 : assert( iFirstSubframe >= 0 && iFirstSubframe <= iLastSubframe );
930 :
931 253158 : nrg[ch] = 0;
932 510743 : for ( n = iFirstSubframe; n <= min( nSubframes - 1, iLastSubframe ); n++ )
933 : {
934 194717105 : for ( i = 0; i < L_subframeTCX; i++ )
935 : {
936 194459520 : nrg[ch] += st->hTcxEnc->spectrum[n][i] * st->hTcxEnc->spectrum[n][i];
937 : }
938 : }
939 253158 : if ( ratioInRmsDomain )
940 : {
941 242908 : nrg[ch] = sqrtf( nrg[ch] );
942 : }
943 : }
944 :
945 126579 : return ( nrg[0] + nrg[1] ) > 0 ? nrg[0] / ( nrg[0] + nrg[1] ) : -1.0f;
946 : }
947 :
948 : /*-------------------------------------------------------------------*
949 : * FindSplitRatio()
950 : *
951 : *
952 : *-------------------------------------------------------------------*/
953 :
954 82641 : void FindSplitRatio(
955 : CPE_ENC_HANDLE hCPE, /* i/o: CPE encoder structure */
956 : Encoder_State **sts /* i/o: Encoder state structure */
957 : )
958 : {
959 82641 : const uint8_t highRateMdctStereo = ( sts[0]->element_brate < IVAS_80k && sts[0]->core == sts[1]->core && sts[0]->element_mode == IVAS_CPE_MDCT && sts[0]->hTcxEnc->enc_ste_pre_corr_past ? 0 : 1 );
960 : float ratio;
961 :
962 : /* Calculate split ratio and quantize it */
963 82641 : hCPE->hStereoMdct->split_ratio = SMDCT_EQUAL_RATIO_RANGE; /* Equal bits to both channels */
964 :
965 82641 : ratio = GetChannelEnergyRatio( sts, 0, 1, highRateMdctStereo );
966 :
967 82641 : if ( ratio >= 0 )
968 : {
969 82620 : hCPE->hStereoMdct->split_ratio = (uint16_t) ( SMDCT_BITRATE_RATIO_RANGE * ratio + 0.5f );
970 : /* Tuning to get closer to the optimal split ratio */
971 82620 : if ( ratio < 8.0f / 9.0f && hCPE->hStereoMdct->split_ratio > SMDCT_EQUAL_RATIO_RANGE + ( SMDCT_BITRATE_RATIO_RANGE >> 4 ) )
972 : {
973 62908 : hCPE->hStereoMdct->split_ratio -= SMDCT_BITRATE_RATIO_RANGE >> 3;
974 : }
975 82620 : if ( ratio > 1.0f / 9.0f && hCPE->hStereoMdct->split_ratio < SMDCT_EQUAL_RATIO_RANGE - ( SMDCT_BITRATE_RATIO_RANGE >> 4 ) )
976 : {
977 1121 : hCPE->hStereoMdct->split_ratio += SMDCT_BITRATE_RATIO_RANGE >> 3;
978 : }
979 :
980 82620 : hCPE->hStereoMdct->split_ratio = min( SMDCT_BITRATE_RATIO_RANGE - 1, max( 1, hCPE->hStereoMdct->split_ratio ) );
981 : }
982 :
983 82641 : return;
984 : }
985 :
986 : /*-------------------------------------------------------------------*
987 : * MsStereoDecision()
988 : *
989 : *
990 : *-------------------------------------------------------------------*/
991 :
992 142570 : static void MsStereoDecision(
993 : STEREO_MDCT_BAND_PARAMETERS *sfbParam,
994 : float *specL,
995 : float *specR,
996 : float *specM, /* scratch buffer for M, use buffer for inverse MS mask spectrum */
997 : float *specS, /* scratch buffer for M, use buffer for inverse MS mask spectrum */
998 : int16_t *mdct_stereo_mode, /* output */
999 : int16_t *msMask, /* output */
1000 : const int16_t nBitsAvailable )
1001 : {
1002 142570 : int16_t length = sfbParam->sfbOffset[sfbParam->nBandsStereoCore];
1003 :
1004 : int16_t bitsL, bitsR, bitsM, bitsS;
1005 : int16_t bitsBW, bitsLR, bitsMS;
1006 : float G;
1007 : float GLR;
1008 : int16_t sfb, i;
1009 : int16_t nMSOn; /* Number of MS active bands */
1010 : int16_t quantSpecL[N_MAX];
1011 : int16_t quantSpecR[N_MAX];
1012 : int16_t quantSpecM[N_MAX];
1013 : int16_t quantSpecS[N_MAX];
1014 : RC_CONTEXT_MEM ctxMem[4];
1015 : HANDLE_RC_CONTEXT_MEM ctxL, ctxR, ctxM, ctxS;
1016 :
1017 142570 : set_s( quantSpecL, 0, N_MAX );
1018 142570 : set_s( quantSpecR, 0, N_MAX );
1019 142570 : set_s( quantSpecM, 0, N_MAX );
1020 142570 : set_s( quantSpecS, 0, N_MAX );
1021 :
1022 142570 : assert( nBitsAvailable > 0 );
1023 :
1024 142570 : ctxL = &ctxMem[0];
1025 142570 : ctxR = &ctxMem[1];
1026 142570 : ctxM = &ctxMem[2];
1027 142570 : ctxS = &ctxMem[3];
1028 :
1029 142570 : GLR = SQ_gain_estimate_stereo( specL, specR, nBitsAvailable, length );
1030 :
1031 81079970 : for ( i = 0; i < length; i++ )
1032 : {
1033 80937400 : specM[i] = ( specL[i] + specR[i] ) * SQRT2_OVER_2;
1034 80937400 : specS[i] = ( specL[i] - specR[i] ) * SQRT2_OVER_2;
1035 : }
1036 :
1037 142570 : G = 0.5f * GLR; /* seems to be favourable to underestimate a bit */
1038 :
1039 : /* do the full spectrum estimates already here, as side effect we get the quantized spectra... */
1040 142570 : bitsLR = QuantSpecEstimateBits( specL, G, length, nBitsAvailable, quantSpecL ) + QuantSpecEstimateBits( specR, G, length, nBitsAvailable, quantSpecR );
1041 142570 : bitsMS = QuantSpecEstimateBits( specM, G, length, nBitsAvailable, quantSpecM ) + QuantSpecEstimateBits( specS, G, length, nBitsAvailable, quantSpecS );
1042 :
1043 : /* clean-up MS scratch buffers */
1044 142570 : set_zero( specM, length );
1045 142570 : set_zero( specS, length );
1046 :
1047 142570 : nMSOn = 0;
1048 142570 : bitsBW = 0;
1049 :
1050 142570 : RCcontextMapping_encode2_estimate_bandWise_start( quantSpecL, length, nBitsAvailable, ctxL );
1051 142570 : RCcontextMapping_encode2_estimate_bandWise_start( quantSpecR, length, nBitsAvailable, ctxR );
1052 :
1053 142570 : bitsBW += RCcontextMapping_encode2_estimate_bandWise_start( quantSpecM, length, nBitsAvailable, ctxM );
1054 142570 : bitsBW += RCcontextMapping_encode2_estimate_bandWise_start( quantSpecS, length, nBitsAvailable, ctxS );
1055 :
1056 : /*find_max_lastnz(ctxL,ctxR,ctxM,ctxS);*/
1057 :
1058 5811855 : for ( sfb = 0; sfb < sfbParam->nBandsStereoCore; sfb++ )
1059 : {
1060 5669285 : const int16_t startline = sfbParam->sfbOffset[sfb];
1061 5669285 : const int16_t endline = sfbParam->sfbOffset[sfb + 1];
1062 :
1063 5669285 : bitsL = RCcontextMapping_encode2_estimate_bandWise( quantSpecL, startline, endline, ctxL );
1064 5669285 : bitsR = RCcontextMapping_encode2_estimate_bandWise( quantSpecR, startline, endline, ctxR );
1065 5669285 : bitsM = RCcontextMapping_encode2_estimate_bandWise( quantSpecM, startline, endline, ctxM );
1066 5669285 : bitsS = RCcontextMapping_encode2_estimate_bandWise( quantSpecS, startline, endline, ctxS );
1067 :
1068 5669285 : if ( bitsM + bitsS <= bitsL + bitsR )
1069 : {
1070 4254530 : msMask[sfb] = 1;
1071 4254530 : ++nMSOn;
1072 4254530 : context_update( ctxM, ctxL, endline );
1073 4254530 : context_update( ctxS, ctxR, endline );
1074 4254530 : bitsBW += bitsM + bitsS;
1075 : }
1076 : else
1077 : {
1078 1414755 : msMask[sfb] = 0;
1079 1414755 : context_update( ctxL, ctxM, endline );
1080 1414755 : context_update( ctxR, ctxS, endline );
1081 1414755 : bitsBW += bitsL + bitsR;
1082 : }
1083 : #ifdef DEBUG_MODE_MDCT
1084 : dbgwrite( &bitsL, sizeof( int16_t ), 1, 1, "./res/bitsL" );
1085 : dbgwrite( &bitsR, sizeof( int16_t ), 1, 1, "./res/bitsR" );
1086 : dbgwrite( &bitsM, sizeof( int16_t ), 1, 1, "./res/bitsM" );
1087 : dbgwrite( &bitsS, sizeof( int16_t ), 1, 1, "./res/bitsS" );
1088 : #endif
1089 : }
1090 :
1091 142570 : bitsBW += sfbParam->nBandsStereoCore; /* Signaling bits */
1092 :
1093 142570 : if ( bitsLR < bitsBW )
1094 : {
1095 8744 : nMSOn = 0;
1096 8744 : set_s( msMask, 0, sfbParam->sfbCnt );
1097 8744 : bitsBW = bitsLR;
1098 : }
1099 :
1100 142570 : if ( bitsMS < bitsBW )
1101 : {
1102 83558 : nMSOn = sfbParam->nBandsStereoCore;
1103 83558 : set_s( msMask, 1, sfbParam->sfbCnt );
1104 : }
1105 :
1106 142570 : *mdct_stereo_mode = SMDCT_BW_MS;
1107 142570 : if ( nMSOn == sfbParam->nBandsStereoCore )
1108 : {
1109 83558 : *mdct_stereo_mode = SMDCT_MS_FULL;
1110 : }
1111 59012 : else if ( nMSOn == 0 )
1112 : {
1113 7530 : *mdct_stereo_mode = SMDCT_DUAL_MONO;
1114 : }
1115 :
1116 142570 : return;
1117 : }
1118 :
1119 :
1120 : /*-----------------------------------------------------------------------*
1121 : * initMdctStereoEncData()
1122 : *
1123 : * initialize encoder mdct stereo structure
1124 : *-----------------------------------------------------------------------*/
1125 :
1126 75162 : void initMdctStereoEncData(
1127 : STEREO_MDCT_ENC_DATA *hStereoMdct, /* i/o: mdct stereo parameters structure */
1128 : const IVAS_FORMAT ivas_format, /* i : IVAS format */
1129 : const int16_t element_mode, /* i : element mode */
1130 : const int32_t element_brate, /* i : element bitrate */
1131 : const int16_t bwidth, /* i : bandwidth */
1132 : const int16_t igf, /* i : flag indicating IGF activity */
1133 : const H_IGF_GRID hIgfGrid, /* i : IGF grid setup */
1134 : const int16_t mem_init /* i : initialize memory after malloc */
1135 : )
1136 : {
1137 : int16_t tcx_coded_lines;
1138 :
1139 75162 : tcx_coded_lines = getNumTcxCodedLines( bwidth );
1140 :
1141 : /*initialize mdct stereo mode*/
1142 75162 : set_s( hStereoMdct->mdct_stereo_mode, -1, 2 );
1143 :
1144 : /*Initialize sfb parameteres for TCX20 */
1145 75162 : stereo_mdct_init_bands( tcx_coded_lines, TCX_20_CORE, element_brate, igf, igf ? &hIgfGrid[IGF_GRID_LB_NORM] : NULL, &hStereoMdct->stbParamsTCX20.sfbOffset[0], &hStereoMdct->stbParamsTCX20.sfbCnt );
1146 :
1147 : /*Initialize sfb parameteres for TCX10 */
1148 75162 : stereo_mdct_init_bands( tcx_coded_lines, TCX_10_CORE, element_brate, igf, igf ? &hIgfGrid[IGF_GRID_LB_SHORT] : NULL,
1149 : &hStereoMdct->stbParamsTCX10.sfbOffset[0], &hStereoMdct->stbParamsTCX10.sfbCnt );
1150 :
1151 : /*Initialize sfb parameteres for transitions */
1152 75162 : stereo_mdct_init_bands( tcx_coded_lines, -1, element_brate, igf, igf ? &hIgfGrid[IGF_GRID_LB_TRAN] : NULL,
1153 : &hStereoMdct->stbParamsTCX20afterACELP.sfbOffset[0], &hStereoMdct->stbParamsTCX20afterACELP.sfbCnt );
1154 :
1155 75162 : set_s( hStereoMdct->IGFStereoMode, -1, 2 );
1156 :
1157 : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
1158 : /*set all other members to defined states */
1159 : hStereoMdct->fDualMono = 0;
1160 : hStereoMdct->fMSstereo = 0;
1161 :
1162 : if ( hStereoMdct->mdct_stereo_mode_cmdl == SMDCT_FORCE_LR )
1163 : {
1164 : hStereoMdct->fDualMono = 1;
1165 : }
1166 : else if ( hStereoMdct->mdct_stereo_mode_cmdl == SMDCT_FORCE_MS )
1167 : {
1168 : hStereoMdct->fMSstereo = 1;
1169 : }
1170 : #endif
1171 :
1172 75162 : hStereoMdct->split_ratio = SMDCT_EQUAL_RATIO_RANGE;
1173 75162 : set_s( hStereoMdct->global_ild, SMDCT_ILD_RANGE >> 1, 2 );
1174 :
1175 75162 : if ( mem_init )
1176 : {
1177 3551 : hStereoMdct->hItd = NULL;
1178 3551 : hStereoMdct->hDft_ana = NULL;
1179 : }
1180 :
1181 75162 : if ( !( element_mode == IVAS_CPE_MDCT && element_brate <= MAX_MDCT_ITD_BRATE && ivas_format == STEREO_FORMAT ) )
1182 : {
1183 25748 : if ( hStereoMdct->hDft_ana != NULL )
1184 : {
1185 25 : free( hStereoMdct->hDft_ana );
1186 25 : hStereoMdct->hDft_ana = NULL;
1187 : }
1188 :
1189 25748 : if ( hStereoMdct->hItd != NULL )
1190 : {
1191 25 : free( hStereoMdct->hItd );
1192 25 : hStereoMdct->hItd = NULL;
1193 : }
1194 : }
1195 :
1196 75162 : return;
1197 : }
1198 :
1199 : /*-----------------------------------------------------------------------*
1200 : * initMdctItdHandling()
1201 : *
1202 : * initialize encoder mdct ITD handling structures
1203 : *-----------------------------------------------------------------------*/
1204 :
1205 151 : ivas_error initMdctItdHandling(
1206 : STEREO_MDCT_ENC_DATA *hStereoMdct, /* i/o: mdct stereo parameters structure */
1207 : const int32_t input_Fs /* i : input sampling rate */
1208 : )
1209 : {
1210 151 : if ( hStereoMdct->hItd == NULL )
1211 : {
1212 67 : if ( ( hStereoMdct->hItd = (ITD_DATA_HANDLE) malloc( sizeof( ITD_DATA ) ) ) == NULL )
1213 : {
1214 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for ITD data\n" ) );
1215 : }
1216 : }
1217 :
1218 151 : if ( hStereoMdct->hDft_ana == NULL )
1219 : {
1220 67 : if ( ( hStereoMdct->hDft_ana = (DFT_ANA_HANDLE) malloc( sizeof( DFT_ANA ) ) ) == NULL )
1221 : {
1222 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for ITD data\n" ) );
1223 : }
1224 : }
1225 :
1226 : /*Initialize ITD parameters*/
1227 151 : stereo_enc_itd_init( hStereoMdct->hItd );
1228 :
1229 : /*Initialize DFT analysis parameters*/
1230 151 : dft_ana_init( hStereoMdct->hDft_ana, input_Fs );
1231 :
1232 151 : return IVAS_ERR_OK;
1233 : }
1234 :
1235 : /*-------------------------------------------------------------------------
1236 : * stereo_mdct_enc_destroy()
1237 : *
1238 : * destroy MDCT stereo handle
1239 : *-------------------------------------------------------------------------*/
1240 :
1241 1123 : void stereo_mdct_enc_destroy(
1242 : STEREO_MDCT_ENC_DATA_HANDLE *hStereoMdct /* i/o: encoder MDCT stereo handle */
1243 : )
1244 : {
1245 1123 : if ( ( *hStereoMdct )->hDft_ana != NULL )
1246 : {
1247 42 : free( ( *hStereoMdct )->hDft_ana );
1248 42 : ( *hStereoMdct )->hDft_ana = NULL;
1249 : }
1250 :
1251 1123 : if ( ( *hStereoMdct )->hItd != NULL )
1252 : {
1253 42 : free( ( *hStereoMdct )->hItd );
1254 42 : ( *hStereoMdct )->hItd = NULL;
1255 : }
1256 :
1257 1123 : free( *hStereoMdct );
1258 1123 : *hStereoMdct = NULL;
1259 :
1260 1123 : return;
1261 : }
|