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 139797 : 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 139797 : 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 139797 : push_wmops( "stereo_coder_tcx" );
193 :
194 139797 : set_s( nAvailBitsMS, 0, NB_DIV );
195 :
196 139797 : nSubframes = ( sts[0]->core == TCX_20_CORE && sts[1]->core == TCX_20_CORE ) ? 1 : NB_DIV;
197 139797 : L_frameTCX = sts[0]->hTcxEnc->L_frameTCX / nSubframes;
198 :
199 139797 : set_s( &ms_mask[0][0], 0, MAX_SFB );
200 139797 : set_s( &ms_mask[1][0], 0, MAX_SFB );
201 :
202 139797 : 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 139797 : if (
277 : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
278 : hStereoMdct->fDualMono ||
279 : #endif
280 139797 : ( 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 933 : hStereoMdct->mdct_stereo_mode[0] = SMDCT_DUAL_MONO;
283 933 : 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 933 : if ( sts[0]->igf )
294 : {
295 663 : hStereoMdct->IGFStereoMode[0] = SMDCT_DUAL_MONO;
296 663 : 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 933 : hStereoMdct->sw_uncorr = 1;
323 :
324 933 : pop_wmops();
325 933 : 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 280893 : for ( k = 0; k < nSubframes; k++ )
352 : {
353 142029 : sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
354 142029 : if ( sts[0]->last_core == ACELP_CORE )
355 : {
356 0 : sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
357 : }
358 142029 : 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 142029 : set_s( &ms_mask[k][0], 0, MAX_SFB );
411 :
412 142029 : 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 142029 : 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] );
415 :
416 142029 : if ( sts[0]->igf )
417 : {
418 92706 : IGFEncStereoEncoder( sfbConf, sts[0]->hIGFEnc, sts[0]->hTcxEnc->spectrum[k], sts[1]->hTcxEnc->spectrum[k], &ms_mask[k][0],
419 92706 : &hStereoMdct->IGFStereoMode[k], hStereoMdct->mdct_stereo_mode[k], sts[0]->core == TCX_20_CORE, sts[0]->last_core == ACELP_CORE );
420 : }
421 : else
422 : {
423 49323 : hStereoMdct->IGFStereoMode[k] = hStereoMdct->mdct_stereo_mode[k];
424 : }
425 :
426 142029 : if ( hStereoMdct->mdct_stereo_mode[k] != SMDCT_DUAL_MONO || hStereoMdct->IGFStereoMode[k] != SMDCT_DUAL_MONO )
427 : {
428 134600 : 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 );
429 134600 : ms_processing( hStereoMdct, sts, ms_mask, k, sts[0]->hTcxEnc->spectrum[k], sts[1]->hTcxEnc->spectrum[k], sfbConf->sfbCnt );
430 :
431 134600 : if ( !sts[0]->hTcxEnc->fUseTns[k] && !sts[1]->hTcxEnc->fUseTns[k] )
432 : {
433 111788 : sts[0]->hTcxEnc->tns_ms_flag[k] = 1;
434 111788 : sts[1]->hTcxEnc->tns_ms_flag[k] = 1;
435 111788 : 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 );
436 111788 : ms_processing( hStereoMdct, sts, ms_mask, k, mdst_spectrum[0][k], mdst_spectrum[1][k], sfbConf->sfbCnt );
437 : }
438 : }
439 : } /* for k */
440 :
441 : #ifdef DEBUG_MODE_MDCT
442 : for ( k = 0; k < 2; k++ )
443 : {
444 : dbgwrite( &nAvailBitsMS[k], sizeof( int16_t ), 1, 1, "./res/nAvailBitsMS" );
445 : }
446 : #endif
447 : }
448 : /* for bitrate switching determine correlation depending on m/s decision */
449 : {
450 : int16_t ms_bands[2];
451 : float sw_uncorr[2], sw_uncorr_mean;
452 280893 : for ( k = 0; k < nSubframes; k++ )
453 : {
454 142029 : sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
455 142029 : ms_bands[k] = sum_s( ms_mask[k], sfbConf->nBandsStereoCore );
456 142029 : sw_uncorr[k] = ( 1.0f - (float) ms_bands[k] / sfbConf->nBandsStereoCore );
457 : }
458 138864 : if ( sts[0]->core == TCX_20_CORE )
459 : {
460 135699 : sw_uncorr_mean = sw_uncorr[0];
461 : }
462 : else
463 : {
464 3165 : sw_uncorr_mean = ( sw_uncorr[0] + sw_uncorr[1] ) * 0.5f;
465 : }
466 138864 : hStereoMdct->sw_uncorr = ( sw_uncorr_mean > 0.6f );
467 : }
468 :
469 138864 : pop_wmops();
470 :
471 : #ifdef DEBUG_MODE_MDCT
472 : /* MDCT stereo data */
473 : {
474 : float Em[2];
475 : int16_t ch;
476 : getChannelEnergies( sts, Em, 2 );
477 : dbgwrite( Em, sizeof( float ), 2, 1, "./res/Ech" );
478 : for ( k = 0; k < 2; k++ )
479 : {
480 : dbgwrite( &hStereoMdct->global_ild[k], sizeof( int16_t ), 1, 1, "./res/ild_q" );
481 : dbgwrite( &hStereoMdct->mdct_stereo_mode[k], sizeof( int16_t ), 1, 1, "./res/stereo_mode" );
482 : dbgwrite( &hStereoMdct->IGFStereoMode[k], sizeof( int16_t ), 1, 1, "./res/stereo_mode_ifg" );
483 : dbgwrite( ms_mask[k], sizeof( int16_t ), 70, 1, "./res/ms_mask" );
484 : }
485 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
486 : {
487 : for ( k = 0; k < 2; k++ )
488 : {
489 : dbgwrite( sts[ch]->hTcxEnc->spectrum[k], sizeof( float ), 640, 1, "./res/MDCT_spec_after_stereo" );
490 : dbgwrite( mdst_spectrum[ch][k], sizeof( float ), 640, 1, "./res/MDST_spec_after_stereo" );
491 : }
492 : }
493 : }
494 : #endif
495 :
496 138864 : return;
497 : }
498 :
499 :
500 : /*-------------------------------------------------------------------*
501 : * ms_processing()
502 : *
503 : *
504 : *-------------------------------------------------------------------*/
505 :
506 246388 : void ms_processing(
507 : STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct, /* i/o: Stereo MDCT encoder structure */
508 : Encoder_State **sts, /* i/o: Encoder state structure */
509 : int16_t ms_mask[NB_DIV][MAX_SFB], /* i : bandwise MS mask */
510 : const int16_t iSubframe, /* i : subframe number */
511 : float x_0[], /* i/o: spectrum 1 */
512 : float x_1[], /* i/o: spectrum 1 */
513 : int16_t maxSfb /* i : number of stereo frequency bands*/
514 : )
515 : {
516 : int16_t sfb;
517 : STEREO_MDCT_BAND_PARAMETERS *sfbConf;
518 :
519 246388 : sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
520 :
521 246388 : if ( sts[0]->last_core == ACELP_CORE )
522 : {
523 0 : assert( sts[1]->last_core == ACELP_CORE );
524 0 : sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
525 : }
526 :
527 246388 : if ( maxSfb == -1 )
528 : {
529 0 : maxSfb = sfbConf->sfbCnt;
530 : }
531 :
532 10835317 : for ( sfb = 0; sfb < maxSfb; sfb++ )
533 : {
534 10588929 : if ( ms_mask[iSubframe][sfb] )
535 : {
536 8690091 : convertToBwMS( sfbConf->sfbOffset[sfb], sfbConf->sfbOffset[sfb + 1], x_0, x_1, SQRT2_OVER_2 );
537 : }
538 : }
539 :
540 246388 : return;
541 : }
542 :
543 :
544 : /*-------------------------------------------------------------------*
545 : * ms_inv_mask_processing()
546 : *
547 : *
548 : *-------------------------------------------------------------------*/
549 :
550 246388 : void ms_inv_mask_processing(
551 : STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct, /* i/o: Stereo MDCT encoder structure */
552 : Encoder_State **sts, /* i/o: Encoder state structure */
553 : int16_t ms_mask[NB_DIV][MAX_SFB], /* i : bandwise MS mask */
554 : const int16_t iSubframe, /* i : subframe number */
555 : const float x_0[], /* i : spectrum 1 */
556 : const float x_1[], /* i : spectrum 2 */
557 : float x_inv_0[], /* o : inverse spectrum 1 */
558 : float x_inv_1[], /* o : inverse spectrum 2 */
559 : int16_t maxSfb /* i : number of stereo frequency bands*/
560 : )
561 : {
562 : int16_t sfb;
563 : STEREO_MDCT_BAND_PARAMETERS *sfbConf;
564 : int16_t nSubframes, L_subframeTCX;
565 :
566 246388 : nSubframes = ( sts[0]->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
567 246388 : L_subframeTCX = sts[0]->hTcxEnc->L_frameTCX / nSubframes;
568 246388 : sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
569 :
570 246388 : if ( sts[0]->last_core == ACELP_CORE )
571 : {
572 0 : assert( sts[1]->last_core == ACELP_CORE );
573 0 : sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
574 : }
575 :
576 246388 : if ( maxSfb == -1 )
577 : {
578 111788 : maxSfb = sfbConf->sfbCnt;
579 : }
580 :
581 10835317 : for ( sfb = 0; sfb < maxSfb; sfb++ )
582 : {
583 10588929 : mvr2r( &x_0[sfbConf->sfbOffset[sfb]], &x_inv_0[sfbConf->sfbOffset[sfb]], sfbConf->sfbOffset[sfb + 1] - sfbConf->sfbOffset[sfb] );
584 10588929 : mvr2r( &x_1[sfbConf->sfbOffset[sfb]], &x_inv_1[sfbConf->sfbOffset[sfb]], sfbConf->sfbOffset[sfb + 1] - sfbConf->sfbOffset[sfb] );
585 :
586 10588929 : if ( ms_mask[iSubframe][sfb] == 0 )
587 : {
588 1898838 : convertToBwMS( sfbConf->sfbOffset[sfb], sfbConf->sfbOffset[sfb + 1], x_inv_0, x_inv_1, SQRT2_OVER_2 );
589 : }
590 : }
591 :
592 : /* set rest of inverse spectrum to zero */
593 246388 : if ( L_subframeTCX > sfbConf->sfbOffset[maxSfb] )
594 : {
595 146681 : set_zero( &x_inv_0[sfbConf->sfbOffset[maxSfb]], L_subframeTCX - sfbConf->sfbOffset[maxSfb] );
596 146681 : set_zero( &x_inv_1[sfbConf->sfbOffset[maxSfb]], L_subframeTCX - sfbConf->sfbOffset[maxSfb] );
597 : }
598 :
599 246388 : return;
600 : }
601 :
602 :
603 : /*-------------------------------------------------------------------*
604 : * write_stereo_to_bitstream()
605 : *
606 : *
607 : *-------------------------------------------------------------------*/
608 :
609 134891 : int16_t write_stereo_to_bitstream(
610 : STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct, /* i/o: Stereo MDCT encoder structure */
611 : Encoder_State **sts, /* i/o: Encoder state structure */
612 : int16_t ms_mask[NB_DIV][MAX_SFB], /* i : bandwise MS mask */
613 : const int16_t mct_on, /* i : flag mct block (1) or stereo (0)*/
614 : BSTR_ENC_HANDLE hBstr /* i/o: bitstream handle */
615 : )
616 : {
617 : int16_t i, k, nSubframes;
618 : uint16_t mdct_stereo_mode, stereo_mode_bits;
619 : STEREO_MDCT_BAND_PARAMETERS *sfbConf;
620 134891 : int16_t start_bits = hBstr->nb_bits_tot;
621 :
622 134891 : if ( !mct_on )
623 : {
624 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 ) ) );
625 : }
626 :
627 134891 : nSubframes = ( sts[0]->core == TCX_10_CORE || ( sts[0]->core != sts[1]->core ) ) ? NB_DIV : 1;
628 134891 : sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
629 :
630 134891 : if ( sts[0]->last_core == ACELP_CORE )
631 : {
632 654 : assert( sts[1]->ini_frame == 0 || sts[1]->last_core == ACELP_CORE );
633 654 : sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
634 : }
635 :
636 134891 : if ( hStereoMdct->hItd != NULL )
637 : {
638 11754 : write_itd_data( hStereoMdct->hItd, hBstr );
639 : }
640 :
641 273200 : for ( k = 0; k < nSubframes; k++ )
642 : {
643 138309 : mdct_stereo_mode = 0;
644 138309 : stereo_mode_bits = 1;
645 :
646 138309 : switch ( hStereoMdct->mdct_stereo_mode[k] )
647 : {
648 3799 : case SMDCT_DUAL_MONO:
649 3799 : mdct_stereo_mode = 0;
650 3799 : stereo_mode_bits = 1;
651 3799 : break;
652 83450 : case SMDCT_MS_FULL:
653 83450 : mdct_stereo_mode = 2;
654 83450 : stereo_mode_bits = 2;
655 83450 : break;
656 51060 : case SMDCT_BW_MS:
657 51060 : mdct_stereo_mode = 3;
658 51060 : stereo_mode_bits = 2;
659 51060 : break;
660 0 : default:
661 0 : assert( !"Not supported MDCT stereo mode\n" );
662 : }
663 :
664 138309 : push_next_indice( hBstr, mdct_stereo_mode, stereo_mode_bits );
665 :
666 138309 : if ( !mct_on )
667 : {
668 44200 : if ( ( sts[0]->core == sts[1]->core ) || ( k == 0 ) )
669 : {
670 43938 : push_next_indice( hBstr, hStereoMdct->global_ild[k], SMDCT_GLOBAL_ILD_BITS );
671 : }
672 : }
673 :
674 138309 : if ( hStereoMdct->mdct_stereo_mode[k] == SMDCT_BW_MS )
675 : {
676 2119109 : for ( i = 0; i < sfbConf->nBandsStereoCore; i++ )
677 : {
678 2068049 : push_next_indice( hBstr, ms_mask[k][i] ? 1 : 0, 1 );
679 : }
680 : }
681 :
682 138309 : if ( sts[0]->igf )
683 : {
684 92071 : mdct_stereo_mode = 0;
685 92071 : stereo_mode_bits = 1;
686 92071 : switch ( hStereoMdct->IGFStereoMode[k] )
687 : {
688 37325 : case SMDCT_DUAL_MONO:
689 37325 : mdct_stereo_mode = 0;
690 37325 : stereo_mode_bits = 1;
691 37325 : break;
692 40496 : case SMDCT_MS_FULL:
693 40496 : mdct_stereo_mode = 2;
694 40496 : stereo_mode_bits = 2;
695 40496 : break;
696 14250 : case SMDCT_BW_MS:
697 14250 : mdct_stereo_mode = 3;
698 14250 : stereo_mode_bits = 2;
699 14250 : break;
700 0 : default:
701 0 : assert( !"Not supported MDCT stereo mode\n" );
702 : }
703 :
704 92071 : push_next_indice( hBstr, mdct_stereo_mode, stereo_mode_bits );
705 :
706 :
707 92071 : if ( hStereoMdct->IGFStereoMode[k] == SMDCT_BW_MS )
708 : {
709 89998 : for ( i = sfbConf->nBandsStereoCore; i < sfbConf->sfbCnt; i++ )
710 : {
711 75748 : push_next_indice( hBstr, ms_mask[k][i] ? 1 : 0, 1 );
712 : }
713 : }
714 : }
715 : }
716 :
717 134891 : return ( hBstr->nb_bits_tot - start_bits );
718 : }
719 :
720 : /*-------------------------------------------------------------------*
721 : * Band-wise M/S stereo processing
722 : *
723 : *
724 : *-------------------------------------------------------------------*/
725 :
726 10681123 : static void convertToBwMS(
727 : const int16_t startLine, /* i : start line of sfb */
728 : const int16_t stopLine, /* i : stop line of sfb */
729 : float x0[], /* i/o: mid/left channel coefficients */
730 : float x1[], /* i/o: side/right channel coefficients */
731 : const float norm_fac /* i : normalization factor */
732 : )
733 : {
734 : int16_t j;
735 : float tmpValue;
736 :
737 198123125 : for ( j = startLine; j < stopLine; j++ )
738 : {
739 187442002 : tmpValue = x0[j];
740 187442002 : x0[j] = ( x0[j] + x1[j] ) * norm_fac;
741 187442002 : x1[j] = ( tmpValue - x1[j] ) * norm_fac;
742 : }
743 :
744 10681123 : return;
745 : }
746 :
747 : /*-------------------------------------------------------------------*
748 : * convertToMS()
749 : *
750 : *
751 : *-------------------------------------------------------------------*/
752 :
753 92194 : void convertToMS(
754 : const int16_t L_frame, /* i : frame length */
755 : float x0[], /* i/o: mid/left channel coefficients */
756 : float x1[], /* i/o: side/right channel coefficients */
757 : const float norm_fac /* i : normalization factor */
758 : )
759 : {
760 92194 : convertToBwMS( 0, L_frame, x0, x1, norm_fac );
761 :
762 92194 : return;
763 : }
764 :
765 : /*-------------------------------------------------------------------*
766 : * SQ_gain_estimate_stereo()
767 : *
768 : *
769 : *-------------------------------------------------------------------*/
770 :
771 : /*! r: SQ gain */
772 142029 : static float SQ_gain_estimate_stereo(
773 : float xL[], /* i : L vector to quantize */
774 : float xR[], /* i : R vector to quantize */
775 : const int16_t nbitsSQ, /* i : number of bits targeted */
776 : const int16_t lg /* i : vector size (2048 max) */
777 : )
778 : {
779 : int16_t i, q, iter;
780 : float ener, tmp, target, fac, offset;
781 : float en[N_MAX / 2];
782 : int16_t lg2, lg_4, lg2_4;
783 :
784 142029 : lg_4 = lg >> 2;
785 142029 : lg2_4 = 2 * lg_4;
786 142029 : lg2 = lg2_4 << 2;
787 142029 : i = 0;
788 :
789 142029 : set_f( en, 0.01f, N_MAX / 2 );
790 :
791 : /* energy of quadruples with 9dB offset */
792 : /* 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()*/
793 20307899 : for ( q = 0; q < lg_4; q++ )
794 : {
795 20165870 : 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];
796 20165870 : en[q] = log10f( ener ); /* saves a MAC */
797 20165870 : i += 4;
798 : }
799 142029 : i = 0;
800 20307899 : for ( ; q < lg2_4; q++ )
801 : {
802 20165870 : 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];
803 20165870 : en[q] = log10f( ener ); /* saves a MAC */
804 20165870 : i += 4;
805 : }
806 :
807 : /* SQ scale: 4 bits / 6 dB per quadruple */
808 142029 : target = 0.15f * (float) ( nbitsSQ - ( lg2 >> 4 ) );
809 142029 : fac = 12.8f;
810 142029 : offset = fac;
811 :
812 : /* find offset (0 to 128 dB with step of 0.125dB) */
813 1562319 : for ( iter = 0; iter < 10; iter++ )
814 : {
815 1420290 : fac *= 0.5f;
816 1420290 : offset -= fac;
817 1420290 : ener = 0.0f;
818 :
819 366289466 : for ( i = 0; i < lg2_4; i++ )
820 : {
821 365451654 : tmp = en[i] - offset;
822 :
823 : /* avoid SV with 1 bin of amp < 0.5f */
824 365451654 : if ( tmp > 0.3f )
825 : {
826 202028061 : ener += tmp;
827 :
828 : /* if ener is above target -> break and increase offset */
829 202028061 : if ( ener > target )
830 : {
831 582478 : offset += fac;
832 582478 : break;
833 : }
834 : }
835 : }
836 : }
837 :
838 : /* return gain */
839 142029 : return powf( 10.0f, 0.45f + 0.5f * offset );
840 : }
841 :
842 : /*-------------------------------------------------------------------*
843 : * QuantSpecEstimateBits()
844 : *
845 : *
846 : *-------------------------------------------------------------------*/
847 :
848 568116 : static int16_t QuantSpecEstimateBits(
849 : float *spec,
850 : float G,
851 : const int16_t length,
852 : const int16_t nBitsAvailable,
853 : int16_t sqQ[] )
854 : {
855 : int16_t stop, sqBits, nEncoded;
856 : int16_t lastnz;
857 :
858 568116 : tcx_scalar_quantization( spec, sqQ, length, G, 0.5f, NULL, 1 );
859 :
860 568116 : stop = 0;
861 :
862 568116 : sqBits = RCcontextMapping_encode2_estimate_no_mem_s17_LCS( sqQ, length, &lastnz, &nEncoded, nBitsAvailable, &stop, 0, NULL );
863 :
864 568116 : if ( stop != 0 )
865 : {
866 79384 : sqBits = stop;
867 : }
868 :
869 568116 : return sqBits;
870 : }
871 :
872 : /*-------------------------------------------------------------------*
873 : * context_update()
874 : *
875 : *
876 : *-------------------------------------------------------------------*/
877 :
878 11297742 : static void context_update(
879 : HANDLE_RC_CONTEXT_MEM ctxSrc,
880 : HANDLE_RC_CONTEXT_MEM ctxTarget,
881 : const int16_t endLine )
882 : {
883 : int16_t last_nz;
884 :
885 : /* check if last_nz of target is smaller than endLine, save and update */
886 11297742 : last_nz = max( ctxTarget->lastnz, endLine );
887 :
888 11297742 : mvc2c( (uint8_t *) ctxSrc, (uint8_t *) ctxTarget, sizeof( RC_CONTEXT_MEM ) );
889 11297742 : ctxTarget->lastnz = last_nz;
890 :
891 11297742 : return;
892 : }
893 :
894 : /*-------------------------------------------------------------------*
895 : * GetChannelEnergyRatio()
896 : *
897 : *
898 : *-------------------------------------------------------------------*/
899 :
900 126579 : static float GetChannelEnergyRatio(
901 : Encoder_State **sts, /* i/o: Encoder state structure */
902 : const int16_t iFirstSubframe,
903 : const int16_t iLastSubframe,
904 : const uint8_t ratioInRmsDomain )
905 : {
906 : int16_t ch, n, i;
907 : float nrg[2];
908 :
909 : /* Calculate energies per channel */
910 379737 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
911 : {
912 253158 : const Encoder_State *st = sts[ch];
913 253158 : const int16_t nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
914 253158 : int16_t L_subframeTCX = st->hTcxEnc->L_frameTCX / nSubframes;
915 :
916 253158 : if ( st->last_core == ACELP_CORE )
917 : {
918 2668 : L_subframeTCX += L_subframeTCX / 4;
919 : }
920 253158 : assert( iFirstSubframe >= 0 && iFirstSubframe <= iLastSubframe );
921 :
922 253158 : nrg[ch] = 0;
923 510743 : for ( n = iFirstSubframe; n <= min( nSubframes - 1, iLastSubframe ); n++ )
924 : {
925 194717105 : for ( i = 0; i < L_subframeTCX; i++ )
926 : {
927 194459520 : nrg[ch] += st->hTcxEnc->spectrum[n][i] * st->hTcxEnc->spectrum[n][i];
928 : }
929 : }
930 253158 : if ( ratioInRmsDomain )
931 : {
932 242908 : nrg[ch] = sqrtf( nrg[ch] );
933 : }
934 : }
935 :
936 126579 : return ( nrg[0] + nrg[1] ) > 0 ? nrg[0] / ( nrg[0] + nrg[1] ) : -1.0f;
937 : }
938 :
939 : /*-------------------------------------------------------------------*
940 : * FindSplitRatio()
941 : *
942 : *
943 : *-------------------------------------------------------------------*/
944 :
945 82641 : void FindSplitRatio(
946 : CPE_ENC_HANDLE hCPE, /* i/o: CPE encoder structure */
947 : Encoder_State **sts /* i/o: Encoder state structure */
948 : )
949 : {
950 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 );
951 : float ratio;
952 :
953 : /* Calculate split ratio and quantize it */
954 82641 : hCPE->hStereoMdct->split_ratio = SMDCT_EQUAL_RATIO_RANGE; /* Equal bits to both channels */
955 :
956 82641 : ratio = GetChannelEnergyRatio( sts, 0, 1, highRateMdctStereo );
957 :
958 82641 : if ( ratio >= 0 )
959 : {
960 82620 : hCPE->hStereoMdct->split_ratio = (uint16_t) ( SMDCT_BITRATE_RATIO_RANGE * ratio + 0.5f );
961 : /* Tuning to get closer to the optimal split ratio */
962 82620 : if ( ratio < 8.0f / 9.0f && hCPE->hStereoMdct->split_ratio > SMDCT_EQUAL_RATIO_RANGE + ( SMDCT_BITRATE_RATIO_RANGE >> 4 ) )
963 : {
964 62908 : hCPE->hStereoMdct->split_ratio -= SMDCT_BITRATE_RATIO_RANGE >> 3;
965 : }
966 82620 : if ( ratio > 1.0f / 9.0f && hCPE->hStereoMdct->split_ratio < SMDCT_EQUAL_RATIO_RANGE - ( SMDCT_BITRATE_RATIO_RANGE >> 4 ) )
967 : {
968 1121 : hCPE->hStereoMdct->split_ratio += SMDCT_BITRATE_RATIO_RANGE >> 3;
969 : }
970 :
971 82620 : hCPE->hStereoMdct->split_ratio = min( SMDCT_BITRATE_RATIO_RANGE - 1, max( 1, hCPE->hStereoMdct->split_ratio ) );
972 : }
973 :
974 82641 : return;
975 : }
976 :
977 : /*-------------------------------------------------------------------*
978 : * MsStereoDecision()
979 : *
980 : *
981 : *-------------------------------------------------------------------*/
982 :
983 142029 : static void MsStereoDecision(
984 : STEREO_MDCT_BAND_PARAMETERS *sfbParam,
985 : float *specL,
986 : float *specR,
987 : float *specM, /* scratch buffer for M, use buffer for inverse MS mask spectrum */
988 : float *specS, /* scratch buffer for M, use buffer for inverse MS mask spectrum */
989 : int16_t *mdct_stereo_mode, /* output */
990 : int16_t *msMask, /* output */
991 : const int16_t nBitsAvailable )
992 : {
993 142029 : int16_t length = sfbParam->sfbOffset[sfbParam->nBandsStereoCore];
994 :
995 : int16_t bitsL, bitsR, bitsM, bitsS;
996 : int16_t bitsBW, bitsLR, bitsMS;
997 : float G;
998 : float GLR;
999 : int16_t sfb, i;
1000 : int16_t nMSOn; /* Number of MS active bands */
1001 : int16_t quantSpecL[N_MAX];
1002 : int16_t quantSpecR[N_MAX];
1003 : int16_t quantSpecM[N_MAX];
1004 : int16_t quantSpecS[N_MAX];
1005 : RC_CONTEXT_MEM ctxMem[4];
1006 : HANDLE_RC_CONTEXT_MEM ctxL, ctxR, ctxM, ctxS;
1007 :
1008 142029 : set_s( quantSpecL, 0, N_MAX );
1009 142029 : set_s( quantSpecR, 0, N_MAX );
1010 142029 : set_s( quantSpecM, 0, N_MAX );
1011 142029 : set_s( quantSpecS, 0, N_MAX );
1012 :
1013 142029 : assert( nBitsAvailable > 0 );
1014 :
1015 142029 : ctxL = &ctxMem[0];
1016 142029 : ctxR = &ctxMem[1];
1017 142029 : ctxM = &ctxMem[2];
1018 142029 : ctxS = &ctxMem[3];
1019 :
1020 142029 : GLR = SQ_gain_estimate_stereo( specL, specR, nBitsAvailable, length );
1021 :
1022 80805509 : for ( i = 0; i < length; i++ )
1023 : {
1024 80663480 : specM[i] = ( specL[i] + specR[i] ) * SQRT2_OVER_2;
1025 80663480 : specS[i] = ( specL[i] - specR[i] ) * SQRT2_OVER_2;
1026 : }
1027 :
1028 142029 : G = 0.5f * GLR; /* seems to be favourable to underestimate a bit */
1029 :
1030 : /* do the full spectrum estimates already here, as side effect we get the quantized spectra... */
1031 142029 : bitsLR = QuantSpecEstimateBits( specL, G, length, nBitsAvailable, quantSpecL ) + QuantSpecEstimateBits( specR, G, length, nBitsAvailable, quantSpecR );
1032 142029 : bitsMS = QuantSpecEstimateBits( specM, G, length, nBitsAvailable, quantSpecM ) + QuantSpecEstimateBits( specS, G, length, nBitsAvailable, quantSpecS );
1033 :
1034 : /* clean-up MS scratch buffers */
1035 142029 : set_zero( specM, length );
1036 142029 : set_zero( specS, length );
1037 :
1038 142029 : nMSOn = 0;
1039 142029 : bitsBW = 0;
1040 :
1041 142029 : RCcontextMapping_encode2_estimate_bandWise_start( quantSpecL, length, nBitsAvailable, ctxL );
1042 142029 : RCcontextMapping_encode2_estimate_bandWise_start( quantSpecR, length, nBitsAvailable, ctxR );
1043 :
1044 142029 : bitsBW += RCcontextMapping_encode2_estimate_bandWise_start( quantSpecM, length, nBitsAvailable, ctxM );
1045 142029 : bitsBW += RCcontextMapping_encode2_estimate_bandWise_start( quantSpecS, length, nBitsAvailable, ctxS );
1046 :
1047 : /*find_max_lastnz(ctxL,ctxR,ctxM,ctxS);*/
1048 :
1049 5790900 : for ( sfb = 0; sfb < sfbParam->nBandsStereoCore; sfb++ )
1050 : {
1051 5648871 : const int16_t startline = sfbParam->sfbOffset[sfb];
1052 5648871 : const int16_t endline = sfbParam->sfbOffset[sfb + 1];
1053 :
1054 5648871 : bitsL = RCcontextMapping_encode2_estimate_bandWise( quantSpecL, startline, endline, ctxL );
1055 5648871 : bitsR = RCcontextMapping_encode2_estimate_bandWise( quantSpecR, startline, endline, ctxR );
1056 5648871 : bitsM = RCcontextMapping_encode2_estimate_bandWise( quantSpecM, startline, endline, ctxM );
1057 5648871 : bitsS = RCcontextMapping_encode2_estimate_bandWise( quantSpecS, startline, endline, ctxS );
1058 :
1059 5648871 : if ( bitsM + bitsS <= bitsL + bitsR )
1060 : {
1061 4241894 : msMask[sfb] = 1;
1062 4241894 : ++nMSOn;
1063 4241894 : context_update( ctxM, ctxL, endline );
1064 4241894 : context_update( ctxS, ctxR, endline );
1065 4241894 : bitsBW += bitsM + bitsS;
1066 : }
1067 : else
1068 : {
1069 1406977 : msMask[sfb] = 0;
1070 1406977 : context_update( ctxL, ctxM, endline );
1071 1406977 : context_update( ctxR, ctxS, endline );
1072 1406977 : bitsBW += bitsL + bitsR;
1073 : }
1074 : #ifdef DEBUG_MODE_MDCT
1075 : dbgwrite( &bitsL, sizeof( int16_t ), 1, 1, "./res/bitsL" );
1076 : dbgwrite( &bitsR, sizeof( int16_t ), 1, 1, "./res/bitsR" );
1077 : dbgwrite( &bitsM, sizeof( int16_t ), 1, 1, "./res/bitsM" );
1078 : dbgwrite( &bitsS, sizeof( int16_t ), 1, 1, "./res/bitsS" );
1079 : #endif
1080 : }
1081 :
1082 142029 : bitsBW += sfbParam->nBandsStereoCore; /* Signaling bits */
1083 :
1084 142029 : if ( bitsLR < bitsBW )
1085 : {
1086 8733 : nMSOn = 0;
1087 8733 : set_s( msMask, 0, sfbParam->sfbCnt );
1088 8733 : bitsBW = bitsLR;
1089 : }
1090 :
1091 142029 : if ( bitsMS < bitsBW )
1092 : {
1093 83450 : nMSOn = sfbParam->nBandsStereoCore;
1094 83450 : set_s( msMask, 1, sfbParam->sfbCnt );
1095 : }
1096 :
1097 142029 : *mdct_stereo_mode = SMDCT_BW_MS;
1098 142029 : if ( nMSOn == sfbParam->nBandsStereoCore )
1099 : {
1100 83450 : *mdct_stereo_mode = SMDCT_MS_FULL;
1101 : }
1102 58579 : else if ( nMSOn == 0 )
1103 : {
1104 7519 : *mdct_stereo_mode = SMDCT_DUAL_MONO;
1105 : }
1106 :
1107 142029 : return;
1108 : }
1109 :
1110 :
1111 : /*-----------------------------------------------------------------------*
1112 : * initMdctStereoEncData()
1113 : *
1114 : * initialize encoder mdct stereo structure
1115 : *-----------------------------------------------------------------------*/
1116 :
1117 75161 : void initMdctStereoEncData(
1118 : STEREO_MDCT_ENC_DATA *hStereoMdct, /* i/o: mdct stereo parameters structure */
1119 : const IVAS_FORMAT ivas_format, /* i : IVAS format */
1120 : const int16_t element_mode, /* i : element mode */
1121 : const int32_t element_brate, /* i : element bitrate */
1122 : const int16_t bwidth, /* i : bandwidth */
1123 : const int16_t igf, /* i : flag indicating IGF activity */
1124 : const H_IGF_GRID hIgfGrid, /* i : IGF grid setup */
1125 : const int16_t mem_init /* i : initialize memory after malloc */
1126 : )
1127 : {
1128 : int16_t tcx_coded_lines;
1129 :
1130 75161 : tcx_coded_lines = getNumTcxCodedLines( bwidth );
1131 :
1132 : /*initialize mdct stereo mode*/
1133 75161 : set_s( hStereoMdct->mdct_stereo_mode, -1, 2 );
1134 :
1135 : /*Initialize sfb parameteres for TCX20 */
1136 75161 : 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 );
1137 :
1138 : /*Initialize sfb parameteres for TCX10 */
1139 75161 : stereo_mdct_init_bands( tcx_coded_lines, TCX_10_CORE, element_brate, igf, igf ? &hIgfGrid[IGF_GRID_LB_SHORT] : NULL,
1140 : &hStereoMdct->stbParamsTCX10.sfbOffset[0], &hStereoMdct->stbParamsTCX10.sfbCnt );
1141 :
1142 : /*Initialize sfb parameteres for transitions */
1143 75161 : stereo_mdct_init_bands( tcx_coded_lines, -1, element_brate, igf, igf ? &hIgfGrid[IGF_GRID_LB_TRAN] : NULL,
1144 : &hStereoMdct->stbParamsTCX20afterACELP.sfbOffset[0], &hStereoMdct->stbParamsTCX20afterACELP.sfbCnt );
1145 :
1146 75161 : set_s( hStereoMdct->IGFStereoMode, -1, 2 );
1147 :
1148 : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
1149 : /*set all other members to defined states */
1150 : hStereoMdct->fDualMono = 0;
1151 : hStereoMdct->fMSstereo = 0;
1152 :
1153 : if ( hStereoMdct->mdct_stereo_mode_cmdl == SMDCT_FORCE_LR )
1154 : {
1155 : hStereoMdct->fDualMono = 1;
1156 : }
1157 : else if ( hStereoMdct->mdct_stereo_mode_cmdl == SMDCT_FORCE_MS )
1158 : {
1159 : hStereoMdct->fMSstereo = 1;
1160 : }
1161 : #endif
1162 :
1163 75161 : hStereoMdct->split_ratio = SMDCT_EQUAL_RATIO_RANGE;
1164 75161 : set_s( hStereoMdct->global_ild, SMDCT_ILD_RANGE >> 1, 2 );
1165 :
1166 75161 : if ( mem_init )
1167 : {
1168 3550 : hStereoMdct->hItd = NULL;
1169 3550 : hStereoMdct->hDft_ana = NULL;
1170 : }
1171 :
1172 75161 : if ( !( element_mode == IVAS_CPE_MDCT && element_brate <= MAX_MDCT_ITD_BRATE && ivas_format == STEREO_FORMAT ) )
1173 : {
1174 25747 : if ( hStereoMdct->hDft_ana != NULL )
1175 : {
1176 25 : free( hStereoMdct->hDft_ana );
1177 25 : hStereoMdct->hDft_ana = NULL;
1178 : }
1179 :
1180 25747 : if ( hStereoMdct->hItd != NULL )
1181 : {
1182 25 : free( hStereoMdct->hItd );
1183 25 : hStereoMdct->hItd = NULL;
1184 : }
1185 : }
1186 :
1187 75161 : return;
1188 : }
1189 :
1190 : /*-----------------------------------------------------------------------*
1191 : * initMdctItdHandling()
1192 : *
1193 : * initialize encoder mdct ITD handling structures
1194 : *-----------------------------------------------------------------------*/
1195 :
1196 151 : ivas_error initMdctItdHandling(
1197 : STEREO_MDCT_ENC_DATA *hStereoMdct, /* i/o: mdct stereo parameters structure */
1198 : const int32_t input_Fs /* i : input sampling rate */
1199 : )
1200 : {
1201 151 : if ( hStereoMdct->hItd == NULL )
1202 : {
1203 67 : if ( ( hStereoMdct->hItd = (ITD_DATA_HANDLE) malloc( sizeof( ITD_DATA ) ) ) == NULL )
1204 : {
1205 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for ITD data\n" ) );
1206 : }
1207 : }
1208 :
1209 151 : if ( hStereoMdct->hDft_ana == NULL )
1210 : {
1211 67 : if ( ( hStereoMdct->hDft_ana = (DFT_ANA_HANDLE) malloc( sizeof( DFT_ANA ) ) ) == NULL )
1212 : {
1213 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for ITD data\n" ) );
1214 : }
1215 : }
1216 :
1217 : /*Initialize ITD parameters*/
1218 151 : stereo_enc_itd_init( hStereoMdct->hItd );
1219 :
1220 : /*Initialize DFT analysis parameters*/
1221 151 : dft_ana_init( hStereoMdct->hDft_ana, input_Fs );
1222 :
1223 151 : return IVAS_ERR_OK;
1224 : }
1225 :
1226 : /*-------------------------------------------------------------------------
1227 : * stereo_mdct_enc_destroy()
1228 : *
1229 : * destroy MDCT stereo handle
1230 : *-------------------------------------------------------------------------*/
1231 :
1232 1123 : void stereo_mdct_enc_destroy(
1233 : STEREO_MDCT_ENC_DATA_HANDLE *hStereoMdct /* i/o: encoder MDCT stereo handle */
1234 : )
1235 : {
1236 1123 : if ( ( *hStereoMdct )->hDft_ana != NULL )
1237 : {
1238 42 : free( ( *hStereoMdct )->hDft_ana );
1239 42 : ( *hStereoMdct )->hDft_ana = NULL;
1240 : }
1241 :
1242 1123 : if ( ( *hStereoMdct )->hItd != NULL )
1243 : {
1244 42 : free( ( *hStereoMdct )->hItd );
1245 42 : ( *hStereoMdct )->hItd = NULL;
1246 : }
1247 :
1248 1123 : free( *hStereoMdct );
1249 1123 : *hStereoMdct = NULL;
1250 :
1251 1123 : return;
1252 : }
|