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 1662 : 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 1662 : hDft_ana->N = (int16_t) ( STEREO_DFT_HOP_MAX_ENC * input_Fs / 48000 );
83 1662 : hDft_ana->NFFT = (int16_t) ( STEREO_DFT_N_MAX_ENC * input_Fs / 48000 );
84 1662 : hDft_ana->dft_ovl = (int16_t) ( STEREO_DFT_OVL_MAX * input_Fs / 48000 );
85 1662 : hDft_ana->dft_zp = (int16_t) ( STEREO_DFT_ZP_MAX_ENC * input_Fs / 48000 );
86 :
87 1662 : hDft_ana->dft_trigo_32k = dft_trigo_32k;
88 :
89 1662 : if ( input_Fs == 16000 )
90 : {
91 16 : hDft_ana->dft_trigo = dft_trigo_32k;
92 16 : hDft_ana->dft_trigo_step = STEREO_DFT_TRIGO_SRATE_16k_STEP;
93 16 : hDft_ana->win_ana = win_ana_16k;
94 : }
95 1646 : else if ( input_Fs == 32000 )
96 : {
97 623 : hDft_ana->dft_trigo = dft_trigo_32k;
98 623 : hDft_ana->dft_trigo_step = STEREO_DFT_TRIGO_SRATE_32k_STEP;
99 623 : hDft_ana->win_ana = win_ana_32k;
100 : }
101 : else
102 : {
103 1023 : assert( input_Fs == 48000 );
104 1023 : hDft_ana->dft_trigo = dft_trigo_48k;
105 1023 : hDft_ana->dft_trigo_step = STEREO_DFT_TRIGO_SRATE_48k_STEP;
106 1023 : hDft_ana->win_ana = win_ana_48k;
107 : }
108 :
109 1662 : return;
110 : }
111 :
112 :
113 : /*-------------------------------------------------------------------*
114 : * write_itd_data()
115 : *
116 : * Bitstream writing of ITDs
117 : *-------------------------------------------------------------------*/
118 :
119 147220 : 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 147220 : k_offset = 1;
128 :
129 147220 : push_next_indice( hBstr, ( hItd->itd[k_offset] != 0 ), STEREO_DFT_ITD_MODE_NBITS );
130 :
131 147220 : if ( hItd->itd[k_offset] )
132 : {
133 30134 : itd = hItd->itd_index[k_offset];
134 30134 : if ( itd > 255 )
135 : {
136 12989 : itd -= 256;
137 :
138 12989 : if ( itd < 20 )
139 : {
140 9579 : push_next_indice( hBstr, 1, 1 ); /* use Huffman*/
141 9579 : push_next_indice( hBstr, 1, 1 ); /* negative */
142 9579 : push_next_indice( hBstr, dft_code_itd[itd], dft_len_itd[itd] );
143 : }
144 : else
145 : {
146 3410 : push_next_indice( hBstr, 0, 1 ); /* don't use Huffman */
147 3410 : push_next_indice( hBstr, 1, 1 ); /* negative */
148 3410 : push_next_indice( hBstr, itd, STEREO_DFT_ITD_NBITS - 1 );
149 : }
150 : }
151 : else
152 : {
153 17145 : if ( itd < 20 )
154 : {
155 13941 : push_next_indice( hBstr, 1, 1 ); /* use Huffman*/
156 13941 : push_next_indice( hBstr, 0, 1 ); /* positive */
157 13941 : push_next_indice( hBstr, dft_code_itd[itd], dft_len_itd[itd] );
158 : }
159 : else
160 : {
161 : /* don't use Huffman and positive*/
162 3204 : push_next_indice( hBstr, itd, STEREO_DFT_ITD_NBITS + 1 );
163 : }
164 : }
165 : }
166 147220 : return;
167 : }
168 :
169 :
170 : /*-------------------------------------------------------------------*
171 : * stereo_coder_tcx()
172 : *
173 : *
174 : *-------------------------------------------------------------------*/
175 :
176 2343693 : 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 2343693 : 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 2343693 : push_wmops( "stereo_coder_tcx" );
193 :
194 2343693 : set_s( nAvailBitsMS, 0, NB_DIV );
195 :
196 2343693 : nSubframes = ( sts[0]->core == TCX_20_CORE && sts[1]->core == TCX_20_CORE ) ? 1 : NB_DIV;
197 2343693 : L_frameTCX = sts[0]->hTcxEnc->L_frameTCX / nSubframes;
198 :
199 2343693 : set_s( &ms_mask[0][0], 0, MAX_SFB );
200 2343693 : set_s( &ms_mask[1][0], 0, MAX_SFB );
201 :
202 2343693 : if ( !mct_on )
203 : {
204 786993 : if ( sts[0]->core == sts[1]->core )
205 : {
206 1583828 : for ( k = 0; k < nSubframes; k++ )
207 : {
208 801813 : nonQNrgRatio[k] = GetChannelEnergyRatio( sts, k, k, 1 );
209 :
210 801813 : hStereoMdct->global_ild[k] = max( 1, min( SMDCT_ILD_RANGE - 1, (int16_t) ( SMDCT_ILD_RANGE * nonQNrgRatio[k] + 0.5f ) ) );
211 801813 : nrgRatio[k] = (float) SMDCT_ILD_RANGE / hStereoMdct->global_ild[k] - 1; /* nrgRatio = nrg[1]/nrg[0] */
212 :
213 801813 : nonQNrgRatio[k] = max( 0.5f / SMDCT_ILD_RANGE, min( ( SMDCT_ILD_RANGE - 0.5f ) / SMDCT_ILD_RANGE, nonQNrgRatio[k] ) );
214 801813 : 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 4978 : nonQNrgRatio[0] = nonQNrgRatio[1] = GetChannelEnergyRatio( sts, 0, nSubframes - 1,
234 : 1 );
235 4978 : hStereoMdct->global_ild[0] = max( 1, min( SMDCT_ILD_RANGE - 1, (int16_t) ( SMDCT_ILD_RANGE * nonQNrgRatio[0] + 0.5f ) ) );
236 4978 : nrgRatio[0] = nrgRatio[1] = (float) SMDCT_ILD_RANGE / hStereoMdct->global_ild[0] - 1; /* nrgRatio = nrg[1]/nrg[0] */
237 4978 : nonQNrgRatio[0] = nonQNrgRatio[1] = max( 0.5f / SMDCT_ILD_RANGE, min( ( SMDCT_ILD_RANGE - 0.5f ) / SMDCT_ILD_RANGE, nonQNrgRatio[0] ) );
238 4978 : 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 1598762 : for ( k = 0; k < nSubframes; k++ )
256 : {
257 811769 : if ( ( nrgRatio[k] > 1.0f ) && ( k < ( ( sts[1]->core == TCX_20_CORE ) ? 1 : NB_DIV ) ) )
258 : {
259 264720 : L_frameTCX = sts[1]->hTcxEnc->L_frameTCX + ( ( sts[1]->last_core == 0 ) ? sts[1]->hTcxEnc->L_frameTCX / 4 : 0 );
260 264720 : L_frameTCX /= ( ( sts[1]->core == TCX_20_CORE ) ? 1 : NB_DIV );
261 :
262 264720 : v_multc( sts[1]->hTcxEnc->spectrum[k], 1.0f / nrgRatio[k], sts[1]->hTcxEnc->spectrum[k], L_frameTCX );
263 264720 : v_multc( mdst_spectrum[1][k], 1.0f / nrgRatio[k], mdst_spectrum[1][k], L_frameTCX );
264 : }
265 547049 : else if ( ( nrgRatio[k] < 1.0f ) && k < ( ( sts[0]->core == TCX_20_CORE ) ? 1 : NB_DIV ) )
266 : {
267 206124 : L_frameTCX = sts[0]->hTcxEnc->L_frameTCX + ( ( sts[0]->last_core == 0 ) ? sts[0]->hTcxEnc->L_frameTCX / 4 : 0 );
268 206124 : L_frameTCX /= ( ( sts[0]->core == TCX_20_CORE ) ? 1 : NB_DIV );
269 :
270 206124 : v_multc( sts[0]->hTcxEnc->spectrum[k], nrgRatio[k], sts[0]->hTcxEnc->spectrum[k], L_frameTCX );
271 206124 : v_multc( mdst_spectrum[0][k], nrgRatio[k], mdst_spectrum[0][k], L_frameTCX );
272 : }
273 : }
274 : }
275 :
276 2343693 : if (
277 : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
278 : hStereoMdct->fDualMono ||
279 : #endif
280 2343693 : ( 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 15967 : hStereoMdct->mdct_stereo_mode[0] = SMDCT_DUAL_MONO;
283 15967 : 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 15967 : if ( sts[0]->igf )
294 : {
295 11118 : hStereoMdct->IGFStereoMode[0] = SMDCT_DUAL_MONO;
296 11118 : 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 15967 : hStereoMdct->sw_uncorr = 1;
323 :
324 15967 : pop_wmops();
325 15967 : 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 4710674 : for ( k = 0; k < nSubframes; k++ )
352 : {
353 2382948 : sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
354 2382948 : if ( sts[0]->last_core == ACELP_CORE )
355 : {
356 0 : sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
357 : }
358 2382948 : 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 432273 : int16_t sfb = 1;
361 :
362 7150897 : while ( ( sfb < sfbConf->nBandsStereoCore ) && ( sfbConf->sfbOffset[sfb + 1] - sfbConf->sfbOffset[sfb] < 12 ) )
363 : {
364 6718624 : sfb++; /* find start offset */
365 : }
366 :
367 7284094 : for ( sfb--; sfb < sfbConf->nBandsStereoCore; sfb++ ) /* start one SFB early for the fade-in */
368 : {
369 6851821 : const int16_t startLine = sfbConf->sfbOffset[sfb];
370 6851821 : const int16_t endLine = sfbConf->sfbOffset[sfb + 1];
371 6851821 : const int16_t sfbWidth = endLine - startLine;
372 :
373 6851821 : nrgRatio[0] = sum2_f( &sts[0]->hTcxEnc->spectrum[k][startLine], sfbWidth );
374 6851821 : nrgRatio[1] = sum2_f( &sts[1]->hTcxEnc->spectrum[k][startLine], sfbWidth );
375 :
376 6851821 : if ( !sts[0]->hTcxEnc->fUseTns[k] && !sts[1]->hTcxEnc->fUseTns[k] ) /* no TNS in either ch */
377 : {
378 5761009 : nrgRatio[0] += sum2_f( &mdst_spectrum[0][k][startLine], sfbWidth );
379 5761009 : nrgRatio[1] += sum2_f( &mdst_spectrum[1][k][startLine], sfbWidth );
380 : }
381 6851821 : if ( ( nrgRatio[0] > 0.f ) && ( nrgRatio[1] > 0.f ) && ( nrgRatio[0] != nrgRatio[1] ) )
382 : {
383 6468142 : float fTemp = 0.5f * ( nrgRatio[0] + nrgRatio[1] );
384 6468142 : nrgRatio[0] = sqrtf( fTemp / nrgRatio[0] );
385 6468142 : nrgRatio[1] = sqrtf( fTemp / nrgRatio[1] );
386 :
387 6468142 : nrgRatio[0] = max( 0.25f, min( 4.f, nrgRatio[0] ) );
388 6468142 : nrgRatio[1] = max( 0.25f, min( 4.f, nrgRatio[1] ) );
389 :
390 6468142 : 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 3505941 : nrgRatio[0] = powf( nrgRatio[0], 0.25f );
393 3505941 : nrgRatio[1] = powf( nrgRatio[1], 0.25f );
394 : }
395 : else
396 : {
397 2962201 : nrgRatio[0] = sqrtf( nrgRatio[0] );
398 2962201 : nrgRatio[1] = sqrtf( nrgRatio[1] );
399 : }
400 6468142 : v_multc( &sts[0]->hTcxEnc->spectrum[k][startLine], nrgRatio[0], &sts[0]->hTcxEnc->spectrum[k][startLine], sfbWidth );
401 6468142 : v_multc( &mdst_spectrum[0][k][startLine], nrgRatio[0], &mdst_spectrum[0][k][startLine], sfbWidth );
402 :
403 6468142 : v_multc( &sts[1]->hTcxEnc->spectrum[k][startLine], nrgRatio[1], &sts[1]->hTcxEnc->spectrum[k][startLine], sfbWidth );
404 6468142 : 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 2382948 : set_s( &ms_mask[k][0], 0, MAX_SFB );
411 :
412 2382948 : 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 2382948 : 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 2382948 : 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 2382948 : if ( sts[0]->igf )
425 : {
426 1699962 : IGFEncStereoEncoder( sfbConf, sts[0]->hIGFEnc, sts[0]->hTcxEnc->spectrum[k], sts[1]->hTcxEnc->spectrum[k], &ms_mask[k][0],
427 1699962 : &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 682986 : hStereoMdct->IGFStereoMode[k] = hStereoMdct->mdct_stereo_mode[k];
432 : }
433 : }
434 :
435 2382948 : if ( hStereoMdct->mdct_stereo_mode[k] != SMDCT_DUAL_MONO || hStereoMdct->IGFStereoMode[k] != SMDCT_DUAL_MONO )
436 : {
437 2266241 : 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 2266241 : ms_processing( hStereoMdct, sts, ms_mask, k, sts[0]->hTcxEnc->spectrum[k], sts[1]->hTcxEnc->spectrum[k], sfbConf->sfbCnt );
439 :
440 2266241 : if ( !sts[0]->hTcxEnc->fUseTns[k] && !sts[1]->hTcxEnc->fUseTns[k] )
441 : {
442 1952048 : sts[0]->hTcxEnc->tns_ms_flag[k] = 1;
443 1952048 : sts[1]->hTcxEnc->tns_ms_flag[k] = 1;
444 1952048 : 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 1952048 : 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 4710674 : for ( k = 0; k < nSubframes; k++ )
462 : {
463 2382948 : sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
464 2382948 : ms_bands[k] = sum_s( ms_mask[k], sfbConf->nBandsStereoCore );
465 2382948 : sw_uncorr[k] = ( 1.0f - (float) ms_bands[k] / sfbConf->nBandsStereoCore );
466 : }
467 2327726 : if ( sts[0]->core == TCX_20_CORE )
468 : {
469 2272504 : sw_uncorr_mean = sw_uncorr[0];
470 : }
471 : else
472 : {
473 55222 : sw_uncorr_mean = ( sw_uncorr[0] + sw_uncorr[1] ) * 0.5f;
474 : }
475 2327726 : hStereoMdct->sw_uncorr = ( sw_uncorr_mean > 0.6f );
476 : }
477 :
478 2327726 : 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 2327726 : return;
506 : }
507 :
508 :
509 : /*-------------------------------------------------------------------*
510 : * ms_processing()
511 : *
512 : *
513 : *-------------------------------------------------------------------*/
514 :
515 4218289 : 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 4218289 : sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
529 :
530 4218289 : 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 4218289 : if ( maxSfb == -1 )
537 : {
538 0 : maxSfb = sfbConf->sfbCnt;
539 : }
540 :
541 183626135 : for ( sfb = 0; sfb < maxSfb; sfb++ )
542 : {
543 179407846 : if ( ms_mask[iSubframe][sfb] )
544 : {
545 155638810 : convertToBwMS( sfbConf->sfbOffset[sfb], sfbConf->sfbOffset[sfb + 1], x_0, x_1, SQRT2_OVER_2 );
546 : }
547 : }
548 :
549 4218289 : return;
550 : }
551 :
552 :
553 : /*-------------------------------------------------------------------*
554 : * ms_inv_mask_processing()
555 : *
556 : *
557 : *-------------------------------------------------------------------*/
558 :
559 4218289 : 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 4218289 : nSubframes = ( sts[0]->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
576 4218289 : L_subframeTCX = sts[0]->hTcxEnc->L_frameTCX / nSubframes;
577 4218289 : sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
578 :
579 4218289 : 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 4218289 : if ( maxSfb == -1 )
586 : {
587 1952048 : maxSfb = sfbConf->sfbCnt;
588 : }
589 :
590 183626135 : for ( sfb = 0; sfb < maxSfb; sfb++ )
591 : {
592 179407846 : mvr2r( &x_0[sfbConf->sfbOffset[sfb]], &x_inv_0[sfbConf->sfbOffset[sfb]], sfbConf->sfbOffset[sfb + 1] - sfbConf->sfbOffset[sfb] );
593 179407846 : mvr2r( &x_1[sfbConf->sfbOffset[sfb]], &x_inv_1[sfbConf->sfbOffset[sfb]], sfbConf->sfbOffset[sfb + 1] - sfbConf->sfbOffset[sfb] );
594 :
595 179407846 : if ( ms_mask[iSubframe][sfb] == 0 )
596 : {
597 23769036 : 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 4218289 : if ( L_subframeTCX > sfbConf->sfbOffset[maxSfb] )
603 : {
604 2934875 : set_zero( &x_inv_0[sfbConf->sfbOffset[maxSfb]], L_subframeTCX - sfbConf->sfbOffset[maxSfb] );
605 2934875 : set_zero( &x_inv_1[sfbConf->sfbOffset[maxSfb]], L_subframeTCX - sfbConf->sfbOffset[maxSfb] );
606 : }
607 :
608 4218289 : return;
609 : }
610 :
611 :
612 : /*-------------------------------------------------------------------*
613 : * write_stereo_to_bitstream()
614 : *
615 : *
616 : *-------------------------------------------------------------------*/
617 :
618 2306323 : 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 2306323 : int16_t start_bits = hBstr->nb_bits_tot;
630 :
631 2306323 : if ( !mct_on )
632 : {
633 786993 : 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 2306323 : nSubframes = ( sts[0]->core == TCX_10_CORE || ( sts[0]->core != sts[1]->core ) ) ? NB_DIV : 1;
637 2306323 : sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
638 :
639 2306323 : if ( sts[0]->last_core == ACELP_CORE )
640 : {
641 10464 : assert( sts[1]->ini_frame == 0 || sts[1]->last_core == ACELP_CORE );
642 10464 : sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
643 : }
644 :
645 2306323 : if ( hStereoMdct->hItd != NULL )
646 : {
647 147220 : write_itd_data( hStereoMdct->hItd, hBstr );
648 : }
649 :
650 4672890 : for ( k = 0; k < nSubframes; k++ )
651 : {
652 2366567 : mdct_stereo_mode = 0;
653 2366567 : stereo_mode_bits = 1;
654 :
655 2366567 : switch ( hStereoMdct->mdct_stereo_mode[k] )
656 : {
657 103916 : case SMDCT_DUAL_MONO:
658 103916 : mdct_stereo_mode = 0;
659 103916 : stereo_mode_bits = 1;
660 103916 : break;
661 1638695 : case SMDCT_MS_FULL:
662 1638695 : mdct_stereo_mode = 2;
663 1638695 : stereo_mode_bits = 2;
664 1638695 : break;
665 623956 : case SMDCT_BW_MS:
666 623956 : mdct_stereo_mode = 3;
667 623956 : stereo_mode_bits = 2;
668 623956 : break;
669 0 : default:
670 0 : assert( !"Not supported MDCT stereo mode\n" );
671 : }
672 :
673 2366567 : push_next_indice( hBstr, mdct_stereo_mode, stereo_mode_bits );
674 :
675 2366567 : if ( !mct_on )
676 : {
677 811769 : if ( ( sts[0]->core == sts[1]->core ) || ( k == 0 ) )
678 : {
679 806791 : push_next_indice( hBstr, hStereoMdct->global_ild[k], SMDCT_GLOBAL_ILD_BITS );
680 : }
681 : }
682 :
683 2366567 : if ( hStereoMdct->mdct_stereo_mode[k] == SMDCT_BW_MS )
684 : {
685 25199721 : for ( i = 0; i < sfbConf->nBandsStereoCore; i++ )
686 : {
687 24575765 : push_next_indice( hBstr, ms_mask[k][i] ? 1 : 0, 1 );
688 : }
689 : }
690 :
691 2366567 : if ( sts[0]->igf )
692 : {
693 1701693 : mdct_stereo_mode = 0;
694 1701693 : stereo_mode_bits = 1;
695 1701693 : switch ( hStereoMdct->IGFStereoMode[k] )
696 : {
697 568450 : case SMDCT_DUAL_MONO:
698 568450 : mdct_stereo_mode = 0;
699 568450 : stereo_mode_bits = 1;
700 568450 : break;
701 766065 : case SMDCT_MS_FULL:
702 766065 : mdct_stereo_mode = 2;
703 766065 : stereo_mode_bits = 2;
704 766065 : break;
705 367178 : case SMDCT_BW_MS:
706 367178 : mdct_stereo_mode = 3;
707 367178 : stereo_mode_bits = 2;
708 367178 : break;
709 0 : default:
710 0 : assert( !"Not supported MDCT stereo mode\n" );
711 : }
712 :
713 1701693 : push_next_indice( hBstr, mdct_stereo_mode, stereo_mode_bits );
714 :
715 :
716 1701693 : if ( hStereoMdct->IGFStereoMode[k] == SMDCT_BW_MS )
717 : {
718 2482418 : for ( i = sfbConf->nBandsStereoCore; i < sfbConf->sfbCnt; i++ )
719 : {
720 2115240 : push_next_indice( hBstr, ms_mask[k][i] ? 1 : 0, 1 );
721 : }
722 : }
723 : }
724 : }
725 :
726 2306323 : return ( hBstr->nb_bits_tot - start_bits );
727 : }
728 :
729 : /*-------------------------------------------------------------------*
730 : * Band-wise M/S stereo processing
731 : *
732 : *
733 : *-------------------------------------------------------------------*/
734 :
735 180700186 : 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 3355698414 : for ( j = startLine; j < stopLine; j++ )
747 : {
748 3174998228 : tmpValue = x0[j];
749 3174998228 : x0[j] = ( x0[j] + x1[j] ) * norm_fac;
750 3174998228 : x1[j] = ( tmpValue - x1[j] ) * norm_fac;
751 : }
752 :
753 180700186 : return;
754 : }
755 :
756 : /*-------------------------------------------------------------------*
757 : * convertToMS()
758 : *
759 : *
760 : *-------------------------------------------------------------------*/
761 :
762 1292340 : 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 1292340 : convertToBwMS( 0, L_frame, x0, x1, norm_fac );
770 :
771 1292340 : return;
772 : }
773 :
774 : /*-------------------------------------------------------------------*
775 : * SQ_gain_estimate_stereo()
776 : *
777 : *
778 : *-------------------------------------------------------------------*/
779 :
780 : /*! r: SQ gain */
781 2382948 : 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 2382948 : lg_4 = lg >> 2;
794 2382948 : lg2_4 = 2 * lg_4;
795 2382948 : lg2 = lg2_4 << 2;
796 2382948 : i = 0;
797 :
798 2382948 : 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 319506920 : for ( q = 0; q < lg_4; q++ )
803 : {
804 317123972 : 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 317123972 : en[q] = log10f( ener ); /* saves a MAC */
806 317123972 : i += 4;
807 : }
808 2382948 : i = 0;
809 319506920 : for ( ; q < lg2_4; q++ )
810 : {
811 317123972 : 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 317123972 : en[q] = log10f( ener ); /* saves a MAC */
813 317123972 : i += 4;
814 : }
815 :
816 : /* SQ scale: 4 bits / 6 dB per quadruple */
817 2382948 : target = 0.15f * (float) ( nbitsSQ - ( lg2 >> 4 ) );
818 2382948 : fac = 12.8f;
819 2382948 : offset = fac;
820 :
821 : /* find offset (0 to 128 dB with step of 0.125dB) */
822 26212428 : for ( iter = 0; iter < 10; iter++ )
823 : {
824 23829480 : fac *= 0.5f;
825 23829480 : offset -= fac;
826 23829480 : ener = 0.0f;
827 :
828 5763855405 : for ( i = 0; i < lg2_4; i++ )
829 : {
830 5749809315 : tmp = en[i] - offset;
831 :
832 : /* avoid SV with 1 bin of amp < 0.5f */
833 5749809315 : if ( tmp > 0.3f )
834 : {
835 3237275297 : ener += tmp;
836 :
837 : /* if ener is above target -> break and increase offset */
838 3237275297 : if ( ener > target )
839 : {
840 9783390 : offset += fac;
841 9783390 : break;
842 : }
843 : }
844 : }
845 : }
846 :
847 : /* return gain */
848 2382948 : return powf( 10.0f, 0.45f + 0.5f * offset );
849 : }
850 :
851 : /*-------------------------------------------------------------------*
852 : * QuantSpecEstimateBits()
853 : *
854 : *
855 : *-------------------------------------------------------------------*/
856 :
857 9531792 : 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 9531792 : tcx_scalar_quantization( spec, sqQ, length, G, 0.5f, NULL, 1 );
868 :
869 9531792 : stop = 0;
870 :
871 9531792 : sqBits = RCcontextMapping_encode2_estimate_no_mem_s17_LCS( sqQ, length, &lastnz, &nEncoded, nBitsAvailable, &stop, 0, NULL );
872 :
873 9531792 : if ( stop != 0 )
874 : {
875 1267814 : sqBits = stop;
876 : }
877 :
878 9531792 : return sqBits;
879 : }
880 :
881 : /*-------------------------------------------------------------------*
882 : * context_update()
883 : *
884 : *
885 : *-------------------------------------------------------------------*/
886 :
887 183815692 : 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 183815692 : last_nz = max( ctxTarget->lastnz, endLine );
896 :
897 183815692 : mvc2c( (uint8_t *) ctxSrc, (uint8_t *) ctxTarget, sizeof( RC_CONTEXT_MEM ) );
898 183815692 : ctxTarget->lastnz = last_nz;
899 :
900 183815692 : return;
901 : }
902 :
903 : /*-------------------------------------------------------------------*
904 : * GetChannelEnergyRatio()
905 : *
906 : *
907 : *-------------------------------------------------------------------*/
908 :
909 2230408 : 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 6691224 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
920 : {
921 4460816 : const Encoder_State *st = sts[ch];
922 4460816 : const int16_t nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
923 4460816 : int16_t L_subframeTCX = st->hTcxEnc->L_frameTCX / nSubframes;
924 :
925 4460816 : if ( st->last_core == ACELP_CORE )
926 : {
927 42152 : L_subframeTCX += L_subframeTCX / 4;
928 : }
929 4460816 : assert( iFirstSubframe >= 0 && iFirstSubframe <= iLastSubframe );
930 :
931 4460816 : nrg[ch] = 0;
932 9000374 : for ( n = iFirstSubframe; n <= min( nSubframes - 1, iLastSubframe ); n++ )
933 : {
934 3566301798 : for ( i = 0; i < L_subframeTCX; i++ )
935 : {
936 3561762240 : nrg[ch] += st->hTcxEnc->spectrum[n][i] * st->hTcxEnc->spectrum[n][i];
937 : }
938 : }
939 4460816 : if ( ratioInRmsDomain )
940 : {
941 4211964 : nrg[ch] = sqrtf( nrg[ch] );
942 : }
943 : }
944 :
945 2230408 : return ( nrg[0] + nrg[1] ) > 0 ? nrg[0] / ( nrg[0] + nrg[1] ) : -1.0f;
946 : }
947 :
948 : /*-------------------------------------------------------------------*
949 : * FindSplitRatio()
950 : *
951 : *
952 : *-------------------------------------------------------------------*/
953 :
954 1423617 : void FindSplitRatio(
955 : CPE_ENC_HANDLE hCPE, /* i/o: CPE encoder structure */
956 : Encoder_State **sts /* i/o: Encoder state structure */
957 : )
958 : {
959 1423617 : 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 1423617 : hCPE->hStereoMdct->split_ratio = SMDCT_EQUAL_RATIO_RANGE; /* Equal bits to both channels */
964 :
965 1423617 : ratio = GetChannelEnergyRatio( sts, 0, 1, highRateMdctStereo );
966 :
967 1423617 : if ( ratio >= 0 )
968 : {
969 1408424 : hCPE->hStereoMdct->split_ratio = (uint16_t) ( SMDCT_BITRATE_RATIO_RANGE * ratio + 0.5f );
970 : /* Tuning to get closer to the optimal split ratio */
971 1408424 : if ( ratio < 8.0f / 9.0f && hCPE->hStereoMdct->split_ratio > SMDCT_EQUAL_RATIO_RANGE + ( SMDCT_BITRATE_RATIO_RANGE >> 4 ) )
972 : {
973 993052 : hCPE->hStereoMdct->split_ratio -= SMDCT_BITRATE_RATIO_RANGE >> 3;
974 : }
975 1408424 : if ( ratio > 1.0f / 9.0f && hCPE->hStereoMdct->split_ratio < SMDCT_EQUAL_RATIO_RANGE - ( SMDCT_BITRATE_RATIO_RANGE >> 4 ) )
976 : {
977 12327 : hCPE->hStereoMdct->split_ratio += SMDCT_BITRATE_RATIO_RANGE >> 3;
978 : }
979 :
980 1408424 : hCPE->hStereoMdct->split_ratio = min( SMDCT_BITRATE_RATIO_RANGE - 1, max( 1, hCPE->hStereoMdct->split_ratio ) );
981 : }
982 :
983 1423617 : return;
984 : }
985 :
986 : /*-------------------------------------------------------------------*
987 : * MsStereoDecision()
988 : *
989 : *
990 : *-------------------------------------------------------------------*/
991 :
992 2382948 : 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 2382948 : 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 2382948 : set_s( quantSpecL, 0, N_MAX );
1018 2382948 : set_s( quantSpecR, 0, N_MAX );
1019 2382948 : set_s( quantSpecM, 0, N_MAX );
1020 2382948 : set_s( quantSpecS, 0, N_MAX );
1021 :
1022 2382948 : assert( nBitsAvailable > 0 );
1023 :
1024 2382948 : ctxL = &ctxMem[0];
1025 2382948 : ctxR = &ctxMem[1];
1026 2382948 : ctxM = &ctxMem[2];
1027 2382948 : ctxS = &ctxMem[3];
1028 :
1029 2382948 : GLR = SQ_gain_estimate_stereo( specL, specR, nBitsAvailable, length );
1030 :
1031 1270878836 : for ( i = 0; i < length; i++ )
1032 : {
1033 1268495888 : specM[i] = ( specL[i] + specR[i] ) * SQRT2_OVER_2;
1034 1268495888 : specS[i] = ( specL[i] - specR[i] ) * SQRT2_OVER_2;
1035 : }
1036 :
1037 2382948 : 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 2382948 : bitsLR = QuantSpecEstimateBits( specL, G, length, nBitsAvailable, quantSpecL ) + QuantSpecEstimateBits( specR, G, length, nBitsAvailable, quantSpecR );
1041 2382948 : bitsMS = QuantSpecEstimateBits( specM, G, length, nBitsAvailable, quantSpecM ) + QuantSpecEstimateBits( specS, G, length, nBitsAvailable, quantSpecS );
1042 :
1043 : /* clean-up MS scratch buffers */
1044 2382948 : set_zero( specM, length );
1045 2382948 : set_zero( specS, length );
1046 :
1047 2382948 : nMSOn = 0;
1048 2382948 : bitsBW = 0;
1049 :
1050 2382948 : RCcontextMapping_encode2_estimate_bandWise_start( quantSpecL, length, nBitsAvailable, ctxL );
1051 2382948 : RCcontextMapping_encode2_estimate_bandWise_start( quantSpecR, length, nBitsAvailable, ctxR );
1052 :
1053 2382948 : bitsBW += RCcontextMapping_encode2_estimate_bandWise_start( quantSpecM, length, nBitsAvailable, ctxM );
1054 2382948 : bitsBW += RCcontextMapping_encode2_estimate_bandWise_start( quantSpecS, length, nBitsAvailable, ctxS );
1055 :
1056 : /*find_max_lastnz(ctxL,ctxR,ctxM,ctxS);*/
1057 :
1058 94290794 : for ( sfb = 0; sfb < sfbParam->nBandsStereoCore; sfb++ )
1059 : {
1060 91907846 : const int16_t startline = sfbParam->sfbOffset[sfb];
1061 91907846 : const int16_t endline = sfbParam->sfbOffset[sfb + 1];
1062 :
1063 91907846 : bitsL = RCcontextMapping_encode2_estimate_bandWise( quantSpecL, startline, endline, ctxL );
1064 91907846 : bitsR = RCcontextMapping_encode2_estimate_bandWise( quantSpecR, startline, endline, ctxR );
1065 91907846 : bitsM = RCcontextMapping_encode2_estimate_bandWise( quantSpecM, startline, endline, ctxM );
1066 91907846 : bitsS = RCcontextMapping_encode2_estimate_bandWise( quantSpecS, startline, endline, ctxS );
1067 :
1068 91907846 : if ( bitsM + bitsS <= bitsL + bitsR )
1069 : {
1070 72752103 : msMask[sfb] = 1;
1071 72752103 : ++nMSOn;
1072 72752103 : context_update( ctxM, ctxL, endline );
1073 72752103 : context_update( ctxS, ctxR, endline );
1074 72752103 : bitsBW += bitsM + bitsS;
1075 : }
1076 : else
1077 : {
1078 19155743 : msMask[sfb] = 0;
1079 19155743 : context_update( ctxL, ctxM, endline );
1080 19155743 : context_update( ctxR, ctxS, endline );
1081 19155743 : 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 2382948 : bitsBW += sfbParam->nBandsStereoCore; /* Signaling bits */
1092 :
1093 2382948 : if ( bitsLR < bitsBW )
1094 : {
1095 135492 : nMSOn = 0;
1096 135492 : set_s( msMask, 0, sfbParam->sfbCnt );
1097 135492 : bitsBW = bitsLR;
1098 : }
1099 :
1100 2382948 : if ( bitsMS < bitsBW )
1101 : {
1102 1638695 : nMSOn = sfbParam->nBandsStereoCore;
1103 1638695 : set_s( msMask, 1, sfbParam->sfbCnt );
1104 : }
1105 :
1106 2382948 : *mdct_stereo_mode = SMDCT_BW_MS;
1107 2382948 : if ( nMSOn == sfbParam->nBandsStereoCore )
1108 : {
1109 1638695 : *mdct_stereo_mode = SMDCT_MS_FULL;
1110 : }
1111 744253 : else if ( nMSOn == 0 )
1112 : {
1113 120297 : *mdct_stereo_mode = SMDCT_DUAL_MONO;
1114 : }
1115 :
1116 2382948 : return;
1117 : }
1118 :
1119 :
1120 : /*-----------------------------------------------------------------------*
1121 : * initMdctStereoEncData()
1122 : *
1123 : * initialize encoder mdct stereo structure
1124 : *-----------------------------------------------------------------------*/
1125 :
1126 1199361 : 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 1199361 : tcx_coded_lines = getNumTcxCodedLines( bwidth );
1140 :
1141 : /*initialize mdct stereo mode*/
1142 1199361 : set_s( hStereoMdct->mdct_stereo_mode, -1, 2 );
1143 :
1144 : /*Initialize sfb parameteres for TCX20 */
1145 1199361 : 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 1199361 : 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 1199361 : 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 1199361 : 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 1199361 : hStereoMdct->split_ratio = SMDCT_EQUAL_RATIO_RANGE;
1173 1199361 : set_s( hStereoMdct->global_ild, SMDCT_ILD_RANGE >> 1, 2 );
1174 :
1175 1199361 : if ( mem_init )
1176 : {
1177 55733 : hStereoMdct->hItd = NULL;
1178 55733 : hStereoMdct->hDft_ana = NULL;
1179 : }
1180 :
1181 1199361 : if ( !( element_mode == IVAS_CPE_MDCT && element_brate <= MAX_MDCT_ITD_BRATE && ivas_format == STEREO_FORMAT ) )
1182 : {
1183 406037 : if ( hStereoMdct->hDft_ana != NULL )
1184 : {
1185 278 : free( hStereoMdct->hDft_ana );
1186 278 : hStereoMdct->hDft_ana = NULL;
1187 : }
1188 :
1189 406037 : if ( hStereoMdct->hItd != NULL )
1190 : {
1191 278 : free( hStereoMdct->hItd );
1192 278 : hStereoMdct->hItd = NULL;
1193 : }
1194 : }
1195 :
1196 1199361 : return;
1197 : }
1198 :
1199 : /*-----------------------------------------------------------------------*
1200 : * initMdctItdHandling()
1201 : *
1202 : * initialize encoder mdct ITD handling structures
1203 : *-----------------------------------------------------------------------*/
1204 :
1205 1662 : 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 1662 : if ( hStereoMdct->hItd == NULL )
1211 : {
1212 635 : 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 1662 : if ( hStereoMdct->hDft_ana == NULL )
1219 : {
1220 635 : 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 1662 : stereo_enc_itd_init( hStereoMdct->hItd );
1228 :
1229 : /*Initialize DFT analysis parameters*/
1230 1662 : dft_ana_init( hStereoMdct->hDft_ana, input_Fs );
1231 :
1232 1662 : return IVAS_ERR_OK;
1233 : }
1234 :
1235 : /*-------------------------------------------------------------------------
1236 : * stereo_mdct_enc_destroy()
1237 : *
1238 : * destroy MDCT stereo handle
1239 : *-------------------------------------------------------------------------*/
1240 :
1241 21586 : void stereo_mdct_enc_destroy(
1242 : STEREO_MDCT_ENC_DATA_HANDLE *hStereoMdct /* i/o: encoder MDCT stereo handle */
1243 : )
1244 : {
1245 21586 : if ( ( *hStereoMdct )->hDft_ana != NULL )
1246 : {
1247 357 : free( ( *hStereoMdct )->hDft_ana );
1248 357 : ( *hStereoMdct )->hDft_ana = NULL;
1249 : }
1250 :
1251 21586 : if ( ( *hStereoMdct )->hItd != NULL )
1252 : {
1253 357 : free( ( *hStereoMdct )->hItd );
1254 357 : ( *hStereoMdct )->hItd = NULL;
1255 : }
1256 :
1257 21586 : free( *hStereoMdct );
1258 21586 : *hStereoMdct = NULL;
1259 :
1260 21586 : return;
1261 : }
|