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