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 "cnst.h"
38 : #include "ivas_cnst.h"
39 : #include "prot_fx.h"
40 : #include "wmc_auto.h"
41 : #include "ivas_prot_fx.h"
42 : #include "prot_fx_enc.h"
43 : #include "rom_com.h"
44 :
45 :
46 : /*-----------------------------------------------------------------------*
47 : * Local function prototypes
48 : *-----------------------------------------------------------------------*/
49 :
50 : static ivas_error ivas_mc_enc_reconfig_fx( Encoder_Struct *st_ivas, const Word16 last_mc_mode );
51 :
52 :
53 : /*-------------------------------------------------------------------*
54 : * set_mct_enc_params()
55 : *
56 : * Set hMCT handle parameters
57 : *-------------------------------------------------------------------*/
58 :
59 1394 : static void set_mct_enc_params_fx(
60 : MCT_ENC_HANDLE hMCT, /* i/o: MCT encoder structure */
61 : const Word32 ivas_total_brate, /* i : IVAS total bitrate */
62 : const IVAS_FORMAT ivas_format, /* i : IVAS_format */
63 : const UWord16 b_nchan_change /* i : flag indicating different channel count */
64 : )
65 : {
66 : Word16 n;
67 :
68 1394 : IF( b_nchan_change )
69 : {
70 1105 : hMCT->currBlockDataCnt = 0;
71 1105 : move16();
72 :
73 : /*Initialize bits required to signal channel-pair index*/
74 1105 : hMCT->bitsChannelPairIndex = s_max( 1, add( sub( 14, norm_s( sub( shr( i_mult( hMCT->nchan_out_woLFE, sub( hMCT->nchan_out_woLFE, 1 ) ), 1 ), 1 ) ) ), 1 ) );
75 :
76 1105 : set16_fx( hMCT->lowE_ch, 0, MCT_MAX_CHANNELS );
77 :
78 14365 : FOR( n = 0; n < MCT_MAX_CHANNELS; n++ )
79 : {
80 13260 : set32_fx( hMCT->lastxCorrMatrix_fx[n], 0, MCT_MAX_CHANNELS ); // hMCT->lastxCorrMatrix_e
81 : }
82 1105 : hMCT->lastxCorrMatrix_e = 0;
83 1105 : move16();
84 : }
85 :
86 1394 : hMCT->hbr_mct = 0;
87 1394 : move16();
88 :
89 1394 : test();
90 1394 : IF( EQ_16( ivas_format, SBA_FORMAT ) && GE_32( ivas_total_brate, IVAS_256k ) )
91 : {
92 219 : hMCT->hbr_mct = 1;
93 219 : move16();
94 : }
95 :
96 1394 : return;
97 : }
98 :
99 : /*-------------------------------------------------------------------*
100 : * map_input_to_cpe_channels()
101 : *
102 : * for MC_MODE map input channels to cpe channels
103 : * mid channel (ch==2) is mapped to last odd-channel CPE
104 : * all channels after LFE are mapped to cpe_id=1 and onwards
105 : * E.g. for 5_1 and for 3 CPEs:
106 : * cpe_id 0: L=data[0] R=data[1]
107 : * cpe_id 1: L=data[4] R=data[5]
108 : * cpe_id 2: L=data[2] (mid) R=NULL
109 : *-------------------------------------------------------------------*/
110 100939 : static void map_input_to_cpe_channels_ivas_fx(
111 : const Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
112 : Word32 *pdata[MAX_INPUT_CHANNELS], /* o : mapped input pointers */
113 : Word32 *data[MCT_MAX_CHANNELS] /* i : input channel data Qx */
114 : )
115 : {
116 : Word16 i, n;
117 : Word16 nchan_transport;
118 :
119 100939 : nchan_transport = st_ivas->nchan_transport;
120 100939 : test();
121 100939 : IF( EQ_32( st_ivas->hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) && EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
122 : {
123 22080 : nchan_transport = add( nchan_transport, st_ivas->hEncoderConfig->nchan_ism );
124 : }
125 100939 : i = 0;
126 100939 : move16();
127 :
128 302817 : FOR( n = 0; n < LFE_CHANNEL - 1; n++ )
129 : {
130 201878 : pdata[i] = data[n]; // Qx
131 201878 : i = add( i, 1 );
132 : }
133 100939 : test();
134 100939 : test();
135 100939 : IF( EQ_32( st_ivas->hEncoderConfig->ivas_format, MC_FORMAT ) && ( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) || EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) ) )
136 : {
137 95360 : FOR( n = LFE_CHANNEL + 1; n < nchan_transport; n++ )
138 : {
139 68440 : pdata[i] = data[n]; // Qx
140 68440 : i = add( i, 1 );
141 : }
142 26920 : pdata[i] = data[LFE_CHANNEL - 1];
143 : }
144 : ELSE
145 : {
146 258568 : FOR( ; n < nchan_transport; n++ )
147 : {
148 184549 : pdata[i] = data[n]; // Qx
149 184549 : i = add( i, 1 );
150 : }
151 : }
152 :
153 : /* odd channel CPE*/
154 100939 : test();
155 100939 : test();
156 100939 : test();
157 100939 : IF( ( LT_16( nchan_transport, i_mult( st_ivas->nCPE, CPE_CHANNELS ) ) ) || ( ( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) || EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) ) && LT_16( st_ivas->hMCT->nchan_out_woLFE, i_mult( st_ivas->nCPE, CPE_CHANNELS ) ) ) )
158 : {
159 58829 : pdata[st_ivas->nCPE * CPE_CHANNELS - 1] = NULL;
160 : }
161 :
162 100939 : return;
163 : }
164 :
165 :
166 : /*-------------------------------------------------------------------*
167 : * ivas_mct_enc()
168 : *
169 : * Multi-Channel Coding Tool (MCT) encoding routine
170 : *-------------------------------------------------------------------*/
171 100939 : ivas_error ivas_mct_enc_fx(
172 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
173 : Word32 *data_fx[MCT_MAX_CHANNELS], /* i : input signal buffers Q(q_data_fx) */
174 : Word16 q_data_fx, /* i : Q-factor of input signal */
175 : const Word16 input_frame, /* i : input frame length per channel */
176 : const Word16 nb_bits_metadata /* i : number of metadata bits */
177 : )
178 : {
179 : Word16 n, cpe_id;
180 : MCT_ENC_HANDLE hMCT;
181 : CPE_ENC_HANDLE hCPE;
182 : Word32 mdst_spectrum_long_fx[MCT_MAX_BLOCKS][CPE_CHANNELS][L_FRAME48k];
183 : Word32 orig_spectrum_long_fx[MCT_MAX_BLOCKS][CPE_CHANNELS][L_FRAME48k];
184 : Word16 switch_bw;
185 : IVAS_FORMAT ivas_format;
186 : Word32 ivas_total_brate;
187 : ivas_error error;
188 : Word32 *pdata_fx[MAX_INPUT_CHANNELS];
189 :
190 100939 : error = IVAS_ERR_OK;
191 100939 : move32();
192 :
193 100939 : push_wmops( "ivas_mct_enc" );
194 :
195 : /* Initialization */
196 100939 : hMCT = st_ivas->hMCT;
197 100939 : hMCT->hBstr = st_ivas->hCPE[0]->hCoreCoder[0]->hBstr; /* pointer to write MCT side bits */
198 :
199 100939 : ivas_format = st_ivas->hEncoderConfig->ivas_format;
200 100939 : ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
201 100939 : move32();
202 100939 : move16();
203 100939 : move32();
204 :
205 371247 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
206 : {
207 810924 : FOR( n = 0; n < CPE_CHANNELS; n++ )
208 : {
209 540616 : hCPE = st_ivas->hCPE[cpe_id];
210 540616 : set32_fx( orig_spectrum_long_fx[cpe_id][n], 0, L_FRAME48k );
211 540616 : set32_fx( mdst_spectrum_long_fx[cpe_id][n], 0, L_FRAME48k );
212 540616 : set16_fx( hMCT->q_mdst_spectrum_long_fx[cpe_id][n], Q31, CPE_CHANNELS );
213 540616 : set16_fx( hMCT->q_orig_spectrum_long_fx[cpe_id], Q31, CPE_CHANNELS );
214 :
215 540616 : hMCT->p_mdst_spectrum_long_fx[cpe_id][n] = mdst_spectrum_long_fx[cpe_id][n];
216 540616 : hMCT->p_orig_spectrum_long_fx[cpe_id][n] = orig_spectrum_long_fx[cpe_id][n];
217 :
218 540616 : hCPE->hCoreCoder[n]->input_bwidth = hCPE->hCoreCoder[n]->last_input_bwidth; /* updated in BWD */
219 540616 : hCPE->hCoreCoder[n]->bwidth = hCPE->hCoreCoder[n]->last_bwidth; /* updated in BWD */
220 540616 : move16();
221 540616 : move16();
222 : /* reset channel mode from previous state*/
223 540616 : IF( ( hCPE->cpe_id * CPE_CHANNELS + n ) >= hMCT->nchan_out_woLFE )
224 : {
225 58829 : hCPE->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
226 58829 : move32();
227 : }
228 : ELSE
229 : {
230 481787 : hCPE->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
231 481787 : move32();
232 : }
233 : }
234 : }
235 :
236 : /* reconfiguration in case of bitrate switching */
237 100939 : IF( NE_32( ivas_total_brate, st_ivas->hEncoderConfig->last_ivas_total_brate ) )
238 : {
239 : Word32 cp_bitrate;
240 : // cp_bitrate = ivas_total_brate / hMCT->nchan_out_woLFE * CPE_CHANNELS;
241 1250 : cp_bitrate = L_shl( L_deposit_l( div_l( ivas_total_brate, hMCT->nchan_out_woLFE ) ), 2 ); // a/b => div_l(a, b/2) or (2 * div_l(a, b))
242 :
243 1250 : IF( EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
244 : {
245 : // cp_bitrate = ivas_total_brate / st_ivas->nchan_transport * CPE_CHANNELS;
246 255 : cp_bitrate = L_shl( L_deposit_l( div_l( ivas_total_brate, st_ivas->nchan_transport ) ), 2 ); // a/b => div_l(a, b/2) or (2 * div_l(a, b))
247 : }
248 :
249 1250 : IF( st_ivas->hCPE[0]->hCoreCoder[0]->igf )
250 : {
251 2458 : FOR( n = 0; n < shr( hMCT->nchan_out_woLFE, 1 ); n++ )
252 : {
253 1610 : initMdctStereoEncData_fx( hMCT->hBlockData[n]->hStereoMdct, ivas_format, IVAS_CPE_MDCT, cp_bitrate, st_ivas->hCPE[0]->hCoreCoder[0]->bwidth, st_ivas->hCPE[0]->hCoreCoder[0]->igf, st_ivas->hCPE[0]->hCoreCoder[0]->hIGFEnc->igfData.igfInfo.grid, 0 );
254 : }
255 : }
256 : ELSE
257 : {
258 1363 : FOR( n = 0; n < shr( hMCT->nchan_out_woLFE, 1 ); n++ )
259 : {
260 961 : initMdctStereoEncData_fx( hMCT->hBlockData[n]->hStereoMdct, ivas_format, IVAS_CPE_MDCT, cp_bitrate, st_ivas->hCPE[0]->hCoreCoder[0]->bwidth, st_ivas->hCPE[0]->hCoreCoder[0]->igf, NULL, 0 );
261 : }
262 : }
263 : }
264 :
265 : /* set coded audio band-width */
266 100939 : switch_bw = set_bw_mct_fx( st_ivas->hCPE, st_ivas->nCPE );
267 :
268 : /*for MC and MCT remove pointer to LFE input that has been processed separately */
269 100939 : map_input_to_cpe_channels_ivas_fx( st_ivas, pdata_fx, data_fx );
270 :
271 : /* pre-processing */
272 : Word16 Q_new_out[MCT_MAX_BLOCKS][CPE_CHANNELS];
273 371247 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
274 : {
275 270308 : IF( ( error = ivas_cpe_enc_fx( st_ivas, cpe_id, pdata_fx[cpe_id * CPE_CHANNELS], pdata_fx[cpe_id * CPE_CHANNELS + 1], q_data_fx, input_frame, nb_bits_metadata, Q_new_out[cpe_id] ) ) != IVAS_ERR_OK )
276 : {
277 0 : return error;
278 : }
279 : }
280 :
281 100939 : Word16 q_spec = Q31;
282 100939 : Word16 q_origSpec = Q31;
283 100939 : move16();
284 100939 : move16();
285 : Word16 length, ch, nCPE;
286 100939 : IF( ( hMCT->nchan_out_woLFE % 2 ) == 0 )
287 : {
288 42110 : nCPE = shr( hMCT->nchan_out_woLFE, 1 );
289 : }
290 : ELSE
291 : {
292 58829 : nCPE = add( shr( hMCT->nchan_out_woLFE, 1 ), 1 );
293 : }
294 :
295 371247 : FOR( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
296 : {
297 810924 : FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
298 : {
299 : Word16 norm;
300 540616 : IF( EQ_32( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->mct_chan_mode, MCT_CHAN_MODE_IGNORE ) )
301 : {
302 67365 : CONTINUE;
303 : }
304 :
305 : // length = st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX / ( ( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV );
306 :
307 473251 : IF( EQ_16( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->tcxMode, TCX_20 ) )
308 : {
309 462623 : length = st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX;
310 462623 : move16();
311 : }
312 : ELSE
313 : {
314 10628 : length = shr( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX, NB_DIV_LOG2 ); // st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX / NB_DIV
315 : }
316 :
317 473251 : IF( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->last_core == ACELP_CORE )
318 : {
319 0 : length = add( length, shr( length, 2 ) );
320 : }
321 473251 : IF( hMCT->p_orig_spectrum_long_fx[cpe_id][ch] )
322 : {
323 473251 : norm = L_norm_arr( hMCT->p_orig_spectrum_long_fx[cpe_id][ch], L_FRAME48k );
324 473251 : q_origSpec = s_min( q_origSpec, add( norm, hMCT->q_orig_spectrum_long_fx[cpe_id][ch] ) );
325 : }
326 : }
327 : }
328 100939 : q_origSpec = sub( q_origSpec, 1 );
329 :
330 371247 : FOR( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
331 : {
332 810924 : FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
333 : {
334 540616 : IF( EQ_32( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->mct_chan_mode, MCT_CHAN_MODE_IGNORE ) )
335 : {
336 67365 : CONTINUE;
337 : }
338 473251 : IF( hMCT->p_orig_spectrum_long_fx[cpe_id][ch] )
339 : {
340 473251 : Scale_sig32( hMCT->p_orig_spectrum_long_fx[cpe_id][ch], L_FRAME48k, sub( q_origSpec, hMCT->q_orig_spectrum_long_fx[cpe_id][ch] ) ); // q_origSpec
341 473251 : hMCT->q_orig_spectrum_long_fx[cpe_id][ch] = q_origSpec;
342 473251 : move16();
343 : }
344 : }
345 : }
346 100939 : hMCT->q_orig_spectrum_long_com = q_origSpec;
347 100939 : move16();
348 :
349 371247 : FOR( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
350 : {
351 810924 : FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
352 : {
353 540616 : IF( EQ_16( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->core, TCX_20 ) )
354 : {
355 529982 : length = st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX;
356 529982 : move16();
357 : }
358 : ELSE
359 : {
360 10634 : length = shr( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX, NB_DIV_LOG2 );
361 : }
362 540616 : IF( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->last_core == ACELP_CORE )
363 : {
364 4 : length = add( length, shr( length, 2 ) );
365 : }
366 540616 : Word16 norm = L_norm_arr( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_fx, N_MAX );
367 540616 : q_spec = s_min( q_spec, add( sub( 31, st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_e ), norm ) );
368 540616 : q_spec = s_min( q_spec, add( sub( 31, st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_e ), norm ) );
369 :
370 1091866 : FOR( Word16 k = 0; k <= ( ( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->core == TCX_20_CORE ) ? 1 : NB_DIV ) - 1; k++ )
371 : {
372 551250 : IF( hMCT->p_mdst_spectrum_long_fx[cpe_id][ch] )
373 : {
374 551250 : norm = L_norm_arr( &hMCT->p_mdst_spectrum_long_fx[cpe_id][ch][k * N_TCX10_MAX], length );
375 551250 : q_spec = s_min( q_spec, add( norm, hMCT->q_mdst_spectrum_long_fx[cpe_id][ch][k] ) );
376 : }
377 : }
378 : }
379 : }
380 :
381 371247 : FOR( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
382 : {
383 810924 : FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
384 : {
385 540616 : IF( EQ_16( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->core, TCX_20 ) )
386 : {
387 529982 : length = st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX;
388 529982 : move16();
389 : }
390 : ELSE
391 : {
392 10634 : length = shr( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX, NB_DIV_LOG2 );
393 : }
394 540616 : scale_sig32( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_fx, N_MAX, sub( q_spec, sub( 31, st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_e ) ) ); // q_spec
395 :
396 540616 : st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_e[0] = sub( 31, q_spec );
397 540616 : move16();
398 540616 : st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_e[1] = sub( 31, q_spec );
399 540616 : move16();
400 540616 : st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_e = sub( 31, q_spec );
401 540616 : move16();
402 :
403 540616 : IF( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->last_core == ACELP_CORE )
404 : {
405 4 : length = add( length, shr( length, 2 ) );
406 : }
407 :
408 1091866 : FOR( Word16 k = 0; k <= ( ( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->core == TCX_20_CORE ) ? 1 : NB_DIV ) - 1; k++ )
409 : {
410 551250 : IF( hMCT->p_mdst_spectrum_long_fx[cpe_id][ch] )
411 : {
412 551250 : Scale_sig32( &hMCT->p_mdst_spectrum_long_fx[cpe_id][ch][k * N_TCX10_MAX], length, sub( q_spec, hMCT->q_mdst_spectrum_long_fx[cpe_id][ch][k] ) ); // q_spec
413 551250 : hMCT->q_mdst_spectrum_long_fx[cpe_id][ch][k] = q_spec;
414 551250 : move16();
415 : }
416 : }
417 : }
418 : }
419 :
420 : /* joint MCT encoding */
421 100939 : ivas_mct_core_enc_fx( ivas_format, hMCT, st_ivas->hCPE, hMCT->nchan_out_woLFE, ivas_total_brate, switch_bw, ( ivas_format == MC_FORMAT && ( st_ivas->mc_mode == MC_MODE_MCT || st_ivas->mc_mode == MC_MODE_PARAMUPMIX ) ) ? (Word16) st_ivas->hLFE->lfe_bits : 0, st_ivas->hEncoderConfig->sba_order );
422 :
423 371247 : FOR( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
424 : {
425 810924 : FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
426 : {
427 540616 : IF( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->core == TCX_20 )
428 : {
429 529982 : length = st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX;
430 529982 : move16();
431 : }
432 : ELSE
433 : {
434 10634 : length = shr( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX, NB_DIV_LOG2 );
435 : }
436 540616 : Word16 e_spec = sub( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_e[0], L_norm_arr( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_fx, N_MAX ) );
437 540616 : IF( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->last_core == ACELP_CORE )
438 : {
439 4 : length = add( length, shr( length, 2 ) );
440 : }
441 540616 : IF( EQ_16( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->core, TCX_20 ) )
442 : {
443 529982 : scale_sig32( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_fx[0], length, sub( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_e[0], e_spec ) );
444 529982 : scale_sig32( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_fx + length, sub( N_MAX, length ), sub( sub( 31, q_spec ), e_spec ) );
445 529982 : st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_e = e_spec;
446 529982 : move16();
447 : }
448 : ELSE
449 : {
450 10634 : scale_sig32( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_fx[0], length, sub( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_e[0], e_spec ) ); // st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_e[0]
451 10634 : scale_sig32( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_fx[0] + length, sub( N_TCX10_MAX, length ), sub( sub( 31, q_spec ), e_spec ) ); // 31 - q_spec
452 10634 : scale_sig32( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_fx[1], length, sub( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_e[0], e_spec ) ); // st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_e[0]
453 10634 : scale_sig32( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_fx[1] + length, sub( N_MAX - N_TCX10_MAX, length ), sub( sub( 31, q_spec ), e_spec ) ); // 31 - q_spec
454 10634 : st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_e = e_spec;
455 10634 : move16();
456 : }
457 540616 : st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_e[0] = st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_e;
458 540616 : move16();
459 540616 : st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_e[1] = st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_e;
460 540616 : move16();
461 : }
462 : }
463 :
464 100939 : q_spec = 31;
465 100939 : move16();
466 :
467 : /* Spectrum quantization and coding */
468 371247 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
469 : {
470 270308 : hCPE = st_ivas->hCPE[cpe_id];
471 :
472 270308 : IF( cpe_id > 0 )
473 : {
474 169369 : hCPE->hCoreCoder[0]->hBstr->ind_list = st_ivas->hCPE[cpe_id - 1]->hCoreCoder[1]->hBstr->ind_list + st_ivas->hCPE[cpe_id - 1]->hCoreCoder[1]->hBstr->nb_ind_tot;
475 : }
476 :
477 270308 : ivas_mdct_quant_coder_fx( hCPE, hMCT->tnsBits[cpe_id], hMCT->tnsSize[cpe_id], hMCT->p_param[cpe_id], 1 );
478 :
479 : /* update input samples buffer (as done in ivas_cpe_enc() for other than MCT coding) */
480 810924 : FOR( n = 0; n < CPE_CHANNELS; n++ )
481 : {
482 540616 : Copy( hCPE->hCoreCoder[n]->input_fx, hCPE->hCoreCoder[n]->old_input_signal_fx, input_frame );
483 540616 : hCPE->hCoreCoder[n]->q_old_inp = hCPE->hCoreCoder[n]->q_inp;
484 540616 : move16();
485 540616 : Copy32( hCPE->hCoreCoder[n]->input32_fx, hCPE->hCoreCoder[n]->old_input_signal32_fx, input_frame );
486 540616 : hCPE->hCoreCoder[n]->q_old_inp32 = hCPE->hCoreCoder[n]->q_inp32;
487 540616 : move16();
488 : /* common encoder updates */
489 :
490 540616 : updt_enc_common_ivas_fx( hCPE->hCoreCoder[n], Q_new_out[cpe_id][n] );
491 : }
492 : }
493 :
494 100939 : pop_wmops();
495 100939 : return error;
496 : }
497 :
498 : /*-------------------------------------------------------------------------
499 : * create_mct_enc()
500 : *
501 : * Create, allocate and initialize IVAS encoder MCT handle
502 : *-------------------------------------------------------------------------*/
503 :
504 948 : ivas_error create_mct_enc_fx(
505 : Encoder_Struct *st_ivas /* i/o: IVAS encoder structure */
506 : )
507 : {
508 : MCT_ENC_HANDLE hMCT;
509 : Word16 n;
510 : Word32 cp_bitrate;
511 : Word16 max_blocks;
512 : Word16 cpe_id;
513 : IVAS_FORMAT ivas_format;
514 : Word32 ivas_total_brate;
515 :
516 948 : ivas_format = st_ivas->hEncoderConfig->ivas_format;
517 948 : move16();
518 948 : ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
519 948 : move32();
520 :
521 : /*-----------------------------------------------------------------*
522 : * Allocate MCT handle
523 : *-----------------------------------------------------------------*/
524 :
525 948 : IF( ( hMCT = (MCT_ENC_HANDLE) malloc( sizeof( MCT_ENC_DATA ) ) ) == NULL )
526 : {
527 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MCT\n" ) );
528 : }
529 :
530 : /*-----------------------------------------------------------------*
531 : * Allocate and initialize MCT BlockData handles
532 : *-----------------------------------------------------------------*/
533 :
534 : /* Determine active channels */
535 948 : test();
536 948 : test();
537 948 : test();
538 948 : test();
539 948 : IF( EQ_16( ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_MCT ) )
540 : {
541 339 : hMCT->nchan_out_woLFE = sub( st_ivas->hEncoderConfig->nchan_inp, 1 ); /* LFE channel is coded separately */
542 339 : move16();
543 : }
544 609 : ELSE IF( EQ_16( ivas_format, SBA_FORMAT ) || EQ_16( ivas_format, SBA_ISM_FORMAT ) )
545 : {
546 586 : hMCT->nchan_out_woLFE = ivas_get_sba_num_TCs_fx( ivas_total_brate, st_ivas->sba_analysis_order );
547 586 : move16();
548 :
549 586 : test();
550 586 : IF( EQ_16( ivas_format, SBA_ISM_FORMAT ) && EQ_16( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
551 : {
552 188 : hMCT->nchan_out_woLFE = add( hMCT->nchan_out_woLFE, st_ivas->hEncoderConfig->nchan_ism );
553 188 : move16();
554 : }
555 : }
556 23 : ELSE IF( EQ_16( ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_PARAMMC ) )
557 : {
558 12 : hMCT->nchan_out_woLFE = ivas_param_mc_getNumTransportChannels_fx( ivas_total_brate, st_ivas->hEncoderConfig->mc_input_setup );
559 12 : move16();
560 : }
561 11 : ELSE IF( EQ_16( ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
562 : {
563 11 : hMCT->nchan_out_woLFE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS - 1;
564 11 : move16();
565 : }
566 :
567 : Word32 L_tmp;
568 948 : iDiv_and_mod_32( L_shl( ivas_total_brate, 1 ), hMCT->nchan_out_woLFE, &cp_bitrate, &L_tmp, 0 );
569 : // cp_bitrate = ivas_total_brate / hMCT->nchan_out_woLFE * CPE_CHANNELS;
570 948 : IF( EQ_16( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
571 : {
572 188 : iDiv_and_mod_32( L_shl( ivas_total_brate, 1 ), st_ivas->nchan_transport, &cp_bitrate, &L_tmp, 0 );
573 : // cp_bitrate = ivas_total_brate / st_ivas->nchan_transport * CPE_CHANNELS;
574 : }
575 :
576 3631 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
577 : {
578 8049 : FOR( n = 0; n < CPE_CHANNELS; n++ )
579 : {
580 5366 : st_ivas->hCPE[cpe_id]->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
581 : }
582 : }
583 :
584 : /* in case we have an uneven number of transport channels, indicate last channel ID as inactive */
585 948 : IF( hMCT->nchan_out_woLFE % 2 )
586 : {
587 689 : st_ivas->hCPE[st_ivas->nCPE - 1]->hCoreCoder[1]->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
588 689 : move16();
589 : }
590 :
591 : /*Initialize MCT block data */
592 948 : max_blocks = shr( hMCT->nchan_out_woLFE, 1 ); // hMCT->nchan_out_woLFE / CPE_CHANNELS
593 :
594 2942 : FOR( n = 0; n < max_blocks; n++ )
595 : {
596 1994 : assert( st_ivas->hEncoderConfig->element_mode_init == IVAS_CPE_MDCT && "MCT is not supported for other stereo modes" );
597 :
598 1994 : IF( ( hMCT->hBlockData[n] = (MCT_BLOCK_DATA_HANDLE) malloc( sizeof( MCT_BLOCK_DATA ) ) ) == NULL )
599 : {
600 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MCT block data structure\n" ) );
601 : }
602 :
603 : /*Initialize all parameters to zero*/
604 1994 : hMCT->hBlockData[n]->isActive = 0;
605 1994 : move16();
606 1994 : hMCT->hBlockData[n]->ch1 = 0;
607 1994 : move16();
608 1994 : hMCT->hBlockData[n]->ch2 = 0;
609 1994 : move16();
610 :
611 : /*-----------------------------------------------------------------*
612 : * MDCT stereo initialization
613 : *-----------------------------------------------------------------*/
614 :
615 1994 : IF( ( hMCT->hBlockData[n]->hStereoMdct = (STEREO_MDCT_ENC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_ENC_DATA ) ) ) == NULL )
616 : {
617 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
618 : }
619 :
620 : #ifdef DEBUGGING
621 : hMCT->hBlockData[n]->hStereoMdct->mdct_stereo_mode_cmdl = SMDCT_MS_DECISION;
622 : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
623 : /*set all other members to defined states */
624 : hMCT->hBlockData[n]->hStereoMdct->fDualMono = 0;
625 : hMCT->hBlockData[n]->hStereoMdct->fMSstereo = 0;
626 : #endif
627 : #endif
628 :
629 1994 : initMdctStereoEncData_fx( hMCT->hBlockData[n]->hStereoMdct, ivas_format, IVAS_CPE_MDCT, cp_bitrate, st_ivas->hEncoderConfig->max_bwidth, st_ivas->hCPE[0]->hCoreCoder[0]->igf, st_ivas->hCPE[0]->hCoreCoder[0]->igf ? st_ivas->hCPE[0]->hCoreCoder[0]->hIGFEnc->igfData.igfInfo.grid : NULL, 1 );
630 : }
631 :
632 4642 : FOR( ; n < MCT_MAX_BLOCKS; n++ )
633 : {
634 3694 : hMCT->hBlockData[n] = NULL;
635 : }
636 :
637 : /*-----------------------------------------------------------------*
638 : * Initializations
639 : *-----------------------------------------------------------------*/
640 :
641 948 : set_mct_enc_params_fx( hMCT, ivas_total_brate, ivas_format, 1 );
642 :
643 948 : st_ivas->hMCT = hMCT;
644 :
645 948 : return IVAS_ERR_OK;
646 : }
647 :
648 :
649 : /*-------------------------------------------------------------------------
650 : * mct_enc_reconfigure()
651 : *
652 : * Reconfigure IVAS encoder MCT handle
653 : *-------------------------------------------------------------------------*/
654 :
655 446 : ivas_error mct_enc_reconfigure_fx(
656 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
657 : const UWord16 b_nchan_change /* i : flag indicating different channel count */
658 : )
659 : {
660 : MCT_ENC_HANDLE hMCT;
661 : Encoder_State *st;
662 : Word16 n, cpe_id, max_blocks;
663 : Word32 ivas_total_brate, cp_bitrate;
664 : IVAS_FORMAT ivas_format;
665 : ivas_error error;
666 :
667 446 : error = IVAS_ERR_OK;
668 446 : move32();
669 :
670 446 : hMCT = st_ivas->hMCT;
671 446 : ivas_format = st_ivas->hEncoderConfig->ivas_format;
672 446 : move16();
673 446 : ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
674 446 : move32();
675 :
676 : /*-----------------------------------------------------------------*
677 : * Allocate and initialize MCT BlockData handles
678 : *-----------------------------------------------------------------*/
679 :
680 446 : IF( b_nchan_change )
681 : {
682 157 : test();
683 157 : test();
684 157 : test();
685 157 : test();
686 : /* Determine active channels */
687 157 : IF( EQ_16( ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_MCT ) )
688 : {
689 7 : hMCT->nchan_out_woLFE = sub( st_ivas->hEncoderConfig->nchan_inp, 1 ); /* LFE channel is coded separately */
690 : }
691 150 : ELSE IF( EQ_16( ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
692 : {
693 1 : hMCT->nchan_out_woLFE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS - 1;
694 : }
695 149 : ELSE IF( EQ_16( ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_PARAMMC ) )
696 : {
697 5 : hMCT->nchan_out_woLFE = ivas_param_mc_getNumTransportChannels_fx( ivas_total_brate, st_ivas->hEncoderConfig->mc_input_setup );
698 : }
699 144 : ELSE IF( EQ_16( ivas_format, SBA_FORMAT ) || EQ_16( ivas_format, SBA_ISM_FORMAT ) )
700 : {
701 144 : hMCT->nchan_out_woLFE = st_ivas->nchan_transport;
702 144 : move16();
703 144 : IF( EQ_16( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
704 : {
705 52 : hMCT->nchan_out_woLFE = add( hMCT->nchan_out_woLFE, st_ivas->hEncoderConfig->nchan_ism );
706 52 : move16();
707 : }
708 : }
709 : }
710 :
711 : Word32 L_tmp;
712 446 : iDiv_and_mod_32( L_shl( ivas_total_brate, 1 ), hMCT->nchan_out_woLFE, &cp_bitrate, &L_tmp, 0 );
713 : // cp_bitrate = ivas_total_brate / hMCT->nchan_out_woLFE * CPE_CHANNELS;
714 446 : IF( EQ_16( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
715 : {
716 86 : iDiv_and_mod_32( L_shl( ivas_total_brate, 1 ), st_ivas->nchan_transport, &cp_bitrate, &L_tmp, 0 );
717 : // cp_bitrate = ivas_total_brate / st_ivas->nchan_transport * CPE_CHANNELS;
718 : }
719 :
720 1690 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
721 : {
722 1244 : st_ivas->hCPE[cpe_id]->element_brate = cp_bitrate;
723 1244 : move32();
724 3732 : FOR( n = 0; n < CPE_CHANNELS; n++ )
725 : {
726 2488 : st_ivas->hCPE[cpe_id]->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
727 2488 : move16();
728 : }
729 : }
730 :
731 : /* in case we have an uneven number of transport channels, indicate last channel ID as inactive */
732 446 : IF( s_and( hMCT->nchan_out_woLFE, 1 ) )
733 : {
734 350 : st_ivas->hCPE[st_ivas->nCPE - 1]->hCoreCoder[1]->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
735 350 : move16();
736 : }
737 :
738 : /* set correct nominal bitrates and igf config already here, otherwise we
739 : * run into a number of problems; only do it when the bitrate changes */
740 446 : IF( NE_32( ivas_total_brate, st_ivas->hEncoderConfig->last_ivas_total_brate ) )
741 : {
742 1690 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
743 : {
744 3732 : FOR( n = 0; n < CPE_CHANNELS; n++ )
745 : {
746 2488 : st = st_ivas->hCPE[cpe_id]->hCoreCoder[n];
747 :
748 2488 : st->total_brate = st_ivas->hCPE[cpe_id]->element_brate;
749 2488 : move32();
750 :
751 2488 : IF( NE_16( st->mct_chan_mode, MCT_CHAN_MODE_IGNORE ) )
752 : {
753 2138 : st->bits_frame_nominal = extract_l( Mpy_32_32( st_ivas->hCPE[cpe_id]->element_brate, 42949673 ) /* 1/50 in Q31*/ );
754 2138 : move16();
755 2138 : st->igf = getIgfPresent_fx( st->element_mode, L_mult0( st->bits_frame_nominal, FRAMES_PER_SEC ), st->bwidth, st->rf_mode );
756 2138 : move16();
757 :
758 2138 : IF( ( error = IGF_Reconfig( &st->hIGFEnc, st->igf, 1, st_ivas->hCPE[cpe_id]->element_brate, st->bwidth, st->element_mode, st->rf_mode ) ) != IVAS_ERR_OK )
759 : {
760 0 : return error;
761 : }
762 : }
763 : }
764 : }
765 : }
766 :
767 : /* Initialize MCT block data */
768 446 : max_blocks = shr( hMCT->nchan_out_woLFE, 1 ); // hMCT->nchan_out_woLFE / CPE_CHANNELS
769 :
770 1340 : FOR( n = 0; n < max_blocks; n++ )
771 : {
772 894 : UWord16 mem_init = 0;
773 894 : move16();
774 894 : assert( st_ivas->hEncoderConfig->element_mode_init == IVAS_CPE_MDCT && "MCT is not supported for other stereo modes" );
775 :
776 894 : IF( b_nchan_change )
777 : {
778 335 : IF( hMCT->hBlockData[n] == NULL )
779 : {
780 107 : mem_init = 1;
781 107 : move16();
782 107 : IF( ( hMCT->hBlockData[n] = (MCT_BLOCK_DATA_HANDLE) malloc( sizeof( MCT_BLOCK_DATA ) ) ) == NULL )
783 : {
784 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MCT block data structure\n" ) );
785 : }
786 :
787 : /*Initialize all parameters to zero*/
788 107 : hMCT->hBlockData[n]->isActive = 0;
789 107 : move16();
790 107 : hMCT->hBlockData[n]->ch1 = 0;
791 107 : move16();
792 107 : hMCT->hBlockData[n]->ch2 = 0;
793 107 : move16();
794 :
795 : /*-----------------------------------------------------------------*
796 : * MDCT stereo initialization
797 : *-----------------------------------------------------------------*/
798 :
799 107 : IF( ( hMCT->hBlockData[n]->hStereoMdct = (STEREO_MDCT_ENC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_ENC_DATA ) ) ) == NULL )
800 : {
801 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
802 : }
803 : }
804 : }
805 :
806 : #ifdef DEBUGGING
807 : hMCT->hBlockData[n]->hStereoMdct->mdct_stereo_mode_cmdl = SMDCT_MS_DECISION;
808 : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
809 : /*set all other members to defined states */
810 : hMCT->hBlockData[n]->hStereoMdct->fDualMono = 0;
811 : hMCT->hBlockData[n]->hStereoMdct->fMSstereo = 0;
812 : #endif
813 : #endif
814 :
815 894 : initMdctStereoEncData_fx( hMCT->hBlockData[n]->hStereoMdct, ivas_format, IVAS_CPE_MDCT, cp_bitrate, st_ivas->hEncoderConfig->max_bwidth, st_ivas->hCPE[0]->hCoreCoder[0]->igf, st_ivas->hCPE[0]->hCoreCoder[0]->igf ? st_ivas->hCPE[0]->hCoreCoder[0]->hIGFEnc->igfData.igfInfo.grid : NULL, mem_init );
816 : }
817 :
818 2228 : FOR( ; n < MCT_MAX_BLOCKS; n++ )
819 : {
820 : /* deallocate no longer needed blocks */
821 1782 : IF( hMCT->hBlockData[n] != NULL )
822 : {
823 99 : IF( hMCT->hBlockData[n]->hStereoMdct != NULL )
824 : {
825 99 : free( hMCT->hBlockData[n]->hStereoMdct );
826 99 : hMCT->hBlockData[n]->hStereoMdct = NULL;
827 : }
828 :
829 99 : free( hMCT->hBlockData[n] );
830 99 : hMCT->hBlockData[n] = NULL;
831 : }
832 : }
833 :
834 : /*-----------------------------------------------------------------*
835 : * Initializations
836 : *-----------------------------------------------------------------*/
837 :
838 446 : set_mct_enc_params_fx( hMCT, ivas_total_brate, ivas_format, b_nchan_change );
839 :
840 446 : return error;
841 : }
842 :
843 :
844 : /*-------------------------------------------------------------------------
845 : * ivas_mct_enc_close()
846 : *
847 : * Close MCT
848 : *-------------------------------------------------------------------------*/
849 :
850 1398 : void ivas_mct_enc_close_fx(
851 : MCT_ENC_HANDLE *hMCT /* i/o: MCT encoder structure */
852 : )
853 : {
854 : Word16 n, maxBlocks;
855 :
856 1398 : test();
857 1398 : IF( hMCT == NULL || *hMCT == NULL )
858 : {
859 450 : return;
860 : }
861 :
862 948 : maxBlocks = shr( ( *hMCT )->nchan_out_woLFE, 1 );
863 :
864 2950 : FOR( n = 0; n < maxBlocks; n++ )
865 : {
866 2002 : IF( ( *hMCT )->hBlockData[n] != NULL )
867 : {
868 2002 : IF( ( *hMCT )->hBlockData[n]->hStereoMdct != NULL )
869 : {
870 2002 : free( ( *hMCT )->hBlockData[n]->hStereoMdct );
871 2002 : ( *hMCT )->hBlockData[n]->hStereoMdct = NULL;
872 : }
873 :
874 2002 : free( ( *hMCT )->hBlockData[n] );
875 2002 : ( *hMCT )->hBlockData[n] = NULL;
876 : }
877 : }
878 :
879 948 : free( ( *hMCT ) );
880 948 : ( *hMCT ) = NULL;
881 :
882 948 : return;
883 : }
884 :
885 :
886 : /*-------------------------------------------------------------------------
887 : * ivas_mc_enc_config()
888 : *
889 : * - select MC format mode
890 : * - write MC LS setup
891 : * - reconfigure the MC format encoder
892 : *-------------------------------------------------------------------------*/
893 :
894 48850 : ivas_error ivas_mc_enc_config_fx(
895 : Encoder_Struct *st_ivas /* i/o: IVAS encoder structure */
896 : )
897 : {
898 : Encoder_State *st0; /* used for bitstream handling */
899 : MC_MODE last_mc_mode;
900 : ivas_error error;
901 :
902 48850 : error = IVAS_ERR_OK;
903 48850 : move32();
904 :
905 48850 : last_mc_mode = st_ivas->mc_mode;
906 48850 : move32();
907 :
908 : /* select MC format mode */
909 48850 : st_ivas->mc_mode = ivas_mc_mode_select_fx( st_ivas->hEncoderConfig->mc_input_setup, st_ivas->hEncoderConfig->ivas_total_brate );
910 48850 : move32();
911 :
912 : /* write MC LS setup */
913 48850 : IF( st_ivas->nSCE > 0 )
914 : {
915 11276 : st0 = st_ivas->hSCE[0]->hCoreCoder[0];
916 : }
917 : ELSE
918 : {
919 37574 : st0 = st_ivas->hCPE[0]->hCoreCoder[0];
920 : }
921 :
922 48850 : push_indice( st0->hBstr, IND_SMODE, st_ivas->hEncoderConfig->mc_input_setup, MC_LS_SETUP_BITS );
923 :
924 : /* MC format switching */
925 48850 : test();
926 48850 : IF( NE_32( st_ivas->hEncoderConfig->last_ivas_total_brate, st_ivas->hEncoderConfig->ivas_total_brate ) || NE_32( st_ivas->mc_mode, last_mc_mode ) )
927 : {
928 1078 : IF( NE_32( ( error = ivas_mc_enc_reconfig_fx( st_ivas, last_mc_mode ) ), IVAS_ERR_OK ) )
929 : {
930 0 : return error;
931 : }
932 : }
933 :
934 48850 : return error;
935 : }
936 :
937 :
938 : /*-------------------------------------------------------------------------
939 : * ivas_mc_enc_reconfig()
940 : *
941 : * Reconfigure the MC format encoder
942 : *-------------------------------------------------------------------------*/
943 :
944 1078 : static ivas_error ivas_mc_enc_reconfig_fx(
945 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
946 : const Word16 last_mc_mode /* i : last frame mc mode */
947 : )
948 : {
949 : Word16 nchan_transport_old, nSCE_old, nCPE_old;
950 : ivas_error error;
951 : Word32 new_brate_SCE, new_brate_CPE;
952 :
953 1078 : error = IVAS_ERR_OK;
954 1078 : move32();
955 :
956 1078 : nchan_transport_old = st_ivas->nchan_transport;
957 1078 : move16();
958 1078 : nSCE_old = st_ivas->nSCE;
959 1078 : move16();
960 1078 : nCPE_old = st_ivas->nCPE;
961 1078 : move16();
962 :
963 : /*-----------------------------------------------------------------*
964 : * Reconfigure MC modules
965 : *-----------------------------------------------------------------*/
966 :
967 1078 : IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) )
968 : {
969 471 : st_ivas->nSCE = 0;
970 471 : move16();
971 471 : st_ivas->nCPE = shr( st_ivas->hEncoderConfig->nchan_inp, 1 );
972 471 : move16();
973 :
974 471 : st_ivas->nchan_transport = ivas_mc_ls_setup_get_num_channels_fx( st_ivas->hEncoderConfig->mc_input_setup );
975 471 : move16();
976 :
977 471 : IF( NE_32( last_mc_mode, MC_MODE_MCT ) )
978 : {
979 310 : IF( st_ivas->hLFE != NULL )
980 : {
981 : /* LFE handle */
982 3 : ivas_lfe_enc_close_fx( &( st_ivas->hLFE ) );
983 : }
984 :
985 : /* create LFE handle */
986 310 : IF( NE_32( ( error = ivas_create_lfe_enc_fx( &st_ivas->hLFE, st_ivas->hEncoderConfig->input_Fs ) ), IVAS_ERR_OK ) )
987 : {
988 0 : return error;
989 : }
990 :
991 : /*De-allocate handles for other MC modes*/
992 310 : ivas_param_mc_enc_close_fx( &( st_ivas->hParamMC ), st_ivas->hEncoderConfig->input_Fs );
993 :
994 310 : ivas_mc_paramupmix_enc_close_fx( &( st_ivas->hMCParamUpmix ), st_ivas->hEncoderConfig->input_Fs );
995 :
996 : /* De-allocate McMasa-related handles */
997 310 : ivas_mcmasa_enc_close_fx( &( st_ivas->hMcMasa ), st_ivas->hEncoderConfig->input_Fs );
998 :
999 310 : ivas_masa_enc_close_fx( &( st_ivas->hMasa ) );
1000 :
1001 310 : ivas_qmetadata_close_fx( &st_ivas->hQMetaData );
1002 : }
1003 : }
1004 607 : ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
1005 : {
1006 7 : st_ivas->nSCE = 0;
1007 7 : move16();
1008 7 : st_ivas->nCPE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS >> 1;
1009 7 : move16();
1010 7 : st_ivas->nchan_transport = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
1011 7 : move16();
1012 :
1013 7 : IF( NE_32( last_mc_mode, MC_MODE_PARAMUPMIX ) )
1014 : {
1015 7 : IF( st_ivas->hLFE != NULL )
1016 : {
1017 : /* LFE handle */
1018 0 : ivas_lfe_enc_close_fx( &( st_ivas->hLFE ) );
1019 : }
1020 :
1021 : /* create LFE handle */
1022 7 : IF( NE_32( ( error = ivas_create_lfe_enc_fx( &st_ivas->hLFE, st_ivas->hEncoderConfig->input_Fs ) ), IVAS_ERR_OK ) )
1023 : {
1024 0 : return error;
1025 : }
1026 : }
1027 :
1028 7 : IF( NE_32( last_mc_mode, MC_MODE_PARAMUPMIX ) )
1029 : {
1030 7 : IF( NE_32( ( error = ivas_mc_paramupmix_enc_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
1031 : {
1032 0 : return error;
1033 : }
1034 : }
1035 :
1036 : /*De-allocate handles for other MC modes*/
1037 7 : ivas_param_mc_enc_close_fx( &st_ivas->hParamMC, st_ivas->hEncoderConfig->input_Fs );
1038 :
1039 : /* De-allocate McMasa-related handles */
1040 7 : ivas_mcmasa_enc_close_fx( &( st_ivas->hMcMasa ), st_ivas->hEncoderConfig->input_Fs );
1041 7 : ivas_masa_enc_close_fx( &( st_ivas->hMasa ) );
1042 :
1043 7 : ivas_qmetadata_close_fx( &st_ivas->hQMetaData );
1044 : }
1045 600 : ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) )
1046 : {
1047 334 : IF( NE_32( last_mc_mode, MC_MODE_PARAMMC ) )
1048 : {
1049 269 : IF( NE_32( ( error = ivas_param_mc_enc_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
1050 : {
1051 0 : return error;
1052 : }
1053 : }
1054 : ELSE
1055 : {
1056 65 : IF( NE_32( ( error = ivas_param_mc_enc_reconfig_fx( st_ivas ) ), IVAS_ERR_OK ) )
1057 : {
1058 0 : return error;
1059 : }
1060 : }
1061 :
1062 : /* De-allocate McMasa-related handles */
1063 334 : ivas_mcmasa_enc_close_fx( &( st_ivas->hMcMasa ), st_ivas->hEncoderConfig->input_Fs );
1064 :
1065 334 : IF( st_ivas->hMasa != NULL )
1066 : {
1067 94 : ivas_masa_enc_close_fx( &( st_ivas->hMasa ) );
1068 94 : st_ivas->hMasa = NULL;
1069 : }
1070 :
1071 334 : ivas_mc_paramupmix_enc_close_fx( &( st_ivas->hMCParamUpmix ), st_ivas->hEncoderConfig->input_Fs );
1072 :
1073 334 : test();
1074 334 : IF( EQ_32( last_mc_mode, MC_MODE_PARAMUPMIX ) && st_ivas->hLFE != NULL )
1075 : {
1076 : /* LFE handle */
1077 0 : ivas_lfe_enc_close_fx( &( st_ivas->hLFE ) );
1078 : }
1079 :
1080 334 : ivas_qmetadata_close_fx( &st_ivas->hQMetaData );
1081 :
1082 : /* De-allocate MCT handle if last mode was MCT */
1083 334 : test();
1084 334 : IF( EQ_32( last_mc_mode, MC_MODE_MCT ) && LE_16( st_ivas->nchan_transport, CPE_CHANNELS ) )
1085 : {
1086 170 : ivas_mct_enc_close_fx( &( st_ivas->hMCT ) );
1087 : }
1088 :
1089 334 : test();
1090 334 : IF( EQ_32( last_mc_mode, MC_MODE_MCT ) && st_ivas->hLFE != NULL )
1091 : {
1092 : /* LFE handle */
1093 175 : ivas_lfe_enc_close_fx( &( st_ivas->hLFE ) );
1094 : }
1095 : }
1096 266 : ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
1097 : {
1098 266 : IF( NE_32( last_mc_mode, MC_MODE_MCMASA ) )
1099 : {
1100 218 : ivas_mcmasa_setNumTransportChannels_fx( &( st_ivas->nchan_transport ), &( st_ivas->hEncoderConfig->element_mode_init ), st_ivas->hEncoderConfig->ivas_total_brate );
1101 :
1102 218 : IF( NE_32( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ), IVAS_ERR_OK ) )
1103 : {
1104 0 : return error;
1105 : }
1106 :
1107 218 : IF( NE_32( ( error = ivas_masa_enc_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
1108 : {
1109 0 : return error;
1110 : }
1111 218 : IF( NE_32( ( error = ivas_mcmasa_enc_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
1112 : {
1113 0 : return error;
1114 : }
1115 : }
1116 : ELSE
1117 : {
1118 : /* reconfigure McMASA instance */
1119 48 : IF( NE_32( ( error = ivas_mcmasa_enc_reconfig_fx( st_ivas ) ), IVAS_ERR_OK ) )
1120 : {
1121 0 : return error;
1122 : }
1123 : }
1124 :
1125 266 : ivas_param_mc_enc_close_fx( &( st_ivas->hParamMC ), st_ivas->hEncoderConfig->input_Fs );
1126 :
1127 266 : ivas_mc_paramupmix_enc_close_fx( &( st_ivas->hMCParamUpmix ), st_ivas->hEncoderConfig->input_Fs );
1128 :
1129 266 : test();
1130 266 : IF( EQ_32( last_mc_mode, MC_MODE_PARAMUPMIX ) && st_ivas->hLFE != NULL )
1131 : {
1132 : /* LFE handle */
1133 0 : ivas_lfe_enc_close_fx( &( st_ivas->hLFE ) );
1134 : }
1135 :
1136 266 : IF( EQ_32( last_mc_mode, MC_MODE_MCT ) )
1137 : {
1138 : /* LFE handle */
1139 124 : ivas_lfe_enc_close_fx( &( st_ivas->hLFE ) );
1140 :
1141 124 : ivas_mct_enc_close_fx( &( st_ivas->hMCT ) );
1142 : }
1143 : }
1144 :
1145 1078 : IF( NE_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
1146 : {
1147 812 : IF( EQ_16( st_ivas->nchan_transport, 1 ) )
1148 : {
1149 0 : st_ivas->hEncoderConfig->element_mode_init = IVAS_SCE;
1150 0 : move16();
1151 : }
1152 : ELSE
1153 : {
1154 812 : st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
1155 812 : move16();
1156 : }
1157 : }
1158 :
1159 : /*-----------------------------------------------------------------*
1160 : * Reconfigure core coder
1161 : *-----------------------------------------------------------------*/
1162 :
1163 : /* special case: MCT->ParamMC with more than 2 TC, CPE 1 stays, but has the wrong mct_chan_mode in channel 1
1164 : and might have IGF and TranDet static memory not allocated and the bit stream index list not set,
1165 : set correct mct_chan_mode and init missing static mem
1166 : do it here since it is _very_ MC specific */
1167 1078 : test();
1168 1078 : test();
1169 1078 : IF( EQ_32( last_mc_mode, MC_MODE_MCT ) && EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) && GT_16( st_ivas->nchan_transport, CPE_CHANNELS ) )
1170 : {
1171 5 : Encoder_State *st = st_ivas->hCPE[1]->hCoreCoder[1];
1172 :
1173 5 : IF( EQ_16( st_ivas->nchan_transport, 3 ) )
1174 : {
1175 5 : st->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
1176 5 : move32();
1177 : }
1178 : ELSE
1179 : {
1180 0 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
1181 0 : move32();
1182 : }
1183 :
1184 5 : IF( st->hTranDet == NULL )
1185 : {
1186 0 : IF( ( st->hTranDet = (TRAN_DET_HANDLE) malloc( sizeof( TRAN_DET_DATA ) ) ) == NULL )
1187 : {
1188 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Transient Detection\n" ) );
1189 : }
1190 :
1191 0 : InitTransientDetection_ivas_fx( shl( div_l( st->input_Fs, FRAMES_PER_SEC ), 1 ), NS2SA_FX2( st->input_Fs, DELAY_FIR_RESAMPL_NS ), st->hTranDet, 0 );
1192 : }
1193 :
1194 5 : IF( st->hIGFEnc == NULL )
1195 : {
1196 0 : IF( ( st->hIGFEnc = (IGF_ENC_INSTANCE_HANDLE) malloc( sizeof( IGF_ENC_INSTANCE ) ) ) == NULL )
1197 : {
1198 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for hIGFEnc\n" ) );
1199 : }
1200 : }
1201 5 : st->igf = getIgfPresent_fx( st->element_mode, st->total_brate, st->bwidth, st->rf_mode );
1202 5 : move16();
1203 : /* set last core to TCX20 */
1204 5 : st->last_core = TCX_20_CORE;
1205 5 : move16();
1206 : }
1207 :
1208 1078 : IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
1209 : {
1210 266 : ivas_mcmasa_split_brate_fx( st_ivas->hMcMasa->separateChannelEnabled, st_ivas->hEncoderConfig->ivas_total_brate, st_ivas->nSCE, st_ivas->nCPE, &new_brate_SCE, &new_brate_CPE );
1211 : }
1212 812 : ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) )
1213 : {
1214 471 : new_brate_SCE = 0;
1215 471 : move32();
1216 : Word32 res_dec, res_frac;
1217 471 : iDiv_and_mod_32( st_ivas->hEncoderConfig->ivas_total_brate, sub( st_ivas->nchan_transport, 1 ), &res_dec, &res_frac, 0 );
1218 471 : new_brate_CPE = imult3216( res_dec, CPE_CHANNELS );
1219 : }
1220 341 : ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
1221 : {
1222 7 : new_brate_SCE = 0;
1223 7 : move32();
1224 : Word32 res_dec, res_frac;
1225 7 : iDiv_and_mod_32( st_ivas->hEncoderConfig->ivas_total_brate, sub( st_ivas->nchan_transport, 1 ), &res_dec, &res_frac, 0 );
1226 7 : new_brate_CPE = imult3216( res_dec, CPE_CHANNELS );
1227 : }
1228 : ELSE
1229 : {
1230 334 : new_brate_SCE = 0; /*st_ivas->hEncoderConfig->ivas_total_brate / st_ivas->nchan_transport;*/
1231 334 : move32();
1232 : Word32 res_dec, res_frac;
1233 334 : iDiv_and_mod_32( st_ivas->hEncoderConfig->ivas_total_brate, st_ivas->nchan_transport, &res_dec, &res_frac, 0 );
1234 334 : new_brate_CPE = imult3216( res_dec, CPE_CHANNELS );
1235 : }
1236 :
1237 1078 : IF( NE_32( ( error = ivas_corecoder_enc_reconfig_fx( st_ivas, nSCE_old, nCPE_old, nchan_transport_old, new_brate_SCE, new_brate_CPE, last_mc_mode ) ), IVAS_ERR_OK ) )
1238 : {
1239 0 : return error;
1240 : }
1241 :
1242 1078 : return error;
1243 : }
|