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 1574 : 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 1574 : IF( b_nchan_change )
69 : {
70 1240 : hMCT->currBlockDataCnt = 0;
71 1240 : move16();
72 :
73 : /*Initialize bits required to signal channel-pair index*/
74 1240 : 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 1240 : set16_fx( hMCT->lowE_ch, 0, MCT_MAX_CHANNELS );
77 :
78 14880 : FOR( n = 0; n < MCT_MAX_CHANNELS; n++ )
79 : {
80 13640 : set32_fx( hMCT->lastxCorrMatrix_fx[n], 0, MCT_MAX_CHANNELS ); // hMCT->lastxCorrMatrix_e
81 : }
82 1240 : hMCT->lastxCorrMatrix_e = 0;
83 1240 : move16();
84 : }
85 :
86 1574 : hMCT->hbr_mct = 0;
87 1574 : move16();
88 :
89 1574 : test();
90 1574 : IF( EQ_16( ivas_format, SBA_FORMAT ) && GE_32( ivas_total_brate, IVAS_256k ) )
91 : {
92 237 : hMCT->hbr_mct = 1;
93 237 : move16();
94 : }
95 :
96 1574 : 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 103369 : 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 103369 : nchan_transport = st_ivas->nchan_transport;
120 103369 : test();
121 103369 : IF( EQ_32( st_ivas->hEncoderConfig->ivas_format, SBA_ISM_FORMAT ) && EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
122 : {
123 22480 : nchan_transport = add( nchan_transport, st_ivas->hEncoderConfig->nchan_ism );
124 : }
125 103369 : i = 0;
126 103369 : move16();
127 :
128 310107 : FOR( n = 0; n < LFE_CHANNEL - 1; n++ )
129 : {
130 206738 : pdata[i] = data[n]; // Qx
131 206738 : i = add( i, 1 );
132 : }
133 103369 : test();
134 103369 : test();
135 103369 : 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 96740 : FOR( n = LFE_CHANNEL + 1; n < nchan_transport; n++ )
138 : {
139 69360 : pdata[i] = data[n]; // Qx
140 69360 : i = add( i, 1 );
141 : }
142 27380 : pdata[i] = data[LFE_CHANNEL - 1];
143 : }
144 : ELSE
145 : {
146 263728 : FOR( ; n < nchan_transport; n++ )
147 : {
148 187739 : pdata[i] = data[n]; // Qx
149 187739 : i = add( i, 1 );
150 : }
151 : }
152 :
153 : /* odd channel CPE*/
154 103369 : test();
155 103369 : test();
156 103369 : test();
157 103369 : 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 60839 : pdata[st_ivas->nCPE * CPE_CHANNELS - 1] = NULL;
160 : }
161 :
162 103369 : return;
163 : }
164 :
165 :
166 : /*-------------------------------------------------------------------*
167 : * ivas_mct_enc()
168 : *
169 : * Multi-Channel Coding Tool (MCT) encoding routine
170 : *-------------------------------------------------------------------*/
171 103369 : 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 103369 : error = IVAS_ERR_OK;
191 103369 : move32();
192 :
193 103369 : push_wmops( "ivas_mct_enc" );
194 :
195 : /* Initialization */
196 103369 : hMCT = st_ivas->hMCT;
197 103369 : hMCT->hBstr = st_ivas->hCPE[0]->hCoreCoder[0]->hBstr; /* pointer to write MCT side bits */
198 :
199 103369 : ivas_format = st_ivas->hEncoderConfig->ivas_format;
200 103369 : ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
201 103369 : move32();
202 103369 : move16();
203 103369 : move32();
204 :
205 379397 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
206 : {
207 828084 : FOR( n = 0; n < CPE_CHANNELS; n++ )
208 : {
209 552056 : hCPE = st_ivas->hCPE[cpe_id];
210 552056 : set32_fx( orig_spectrum_long_fx[cpe_id][n], 0, L_FRAME48k );
211 552056 : set32_fx( mdst_spectrum_long_fx[cpe_id][n], 0, L_FRAME48k );
212 552056 : set16_fx( hMCT->q_mdst_spectrum_long_fx[cpe_id][n], Q31, CPE_CHANNELS );
213 552056 : set16_fx( hMCT->q_orig_spectrum_long_fx[cpe_id], Q31, CPE_CHANNELS );
214 :
215 552056 : hMCT->p_mdst_spectrum_long_fx[cpe_id][n] = mdst_spectrum_long_fx[cpe_id][n];
216 552056 : hMCT->p_orig_spectrum_long_fx[cpe_id][n] = orig_spectrum_long_fx[cpe_id][n];
217 :
218 552056 : hCPE->hCoreCoder[n]->input_bwidth = hCPE->hCoreCoder[n]->last_input_bwidth; /* updated in BWD */
219 552056 : hCPE->hCoreCoder[n]->bwidth = hCPE->hCoreCoder[n]->last_bwidth; /* updated in BWD */
220 552056 : move16();
221 552056 : move16();
222 : /* reset channel mode from previous state*/
223 552056 : IF( ( hCPE->cpe_id * CPE_CHANNELS + n ) >= hMCT->nchan_out_woLFE )
224 : {
225 60839 : hCPE->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
226 60839 : move32();
227 : }
228 : ELSE
229 : {
230 491217 : hCPE->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
231 491217 : move32();
232 : }
233 : }
234 : }
235 :
236 : /* reconfiguration in case of bitrate switching */
237 103369 : 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 1429 : 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 1429 : 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 291 : 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 1429 : IF( st_ivas->hCPE[0]->hCoreCoder[0]->igf )
250 : {
251 2834 : FOR( n = 0; n < shr( hMCT->nchan_out_woLFE, 1 ); n++ )
252 : {
253 1851 : 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 1509 : FOR( n = 0; n < shr( hMCT->nchan_out_woLFE, 1 ); n++ )
259 : {
260 1063 : 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 103369 : 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 103369 : 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 379397 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
274 : {
275 276028 : 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 103369 : Word16 q_spec = Q31;
282 103369 : Word16 q_origSpec = Q31;
283 103369 : move16();
284 103369 : move16();
285 : Word16 length, ch, nCPE;
286 103369 : IF( ( hMCT->nchan_out_woLFE % 2 ) == 0 )
287 : {
288 42530 : nCPE = shr( hMCT->nchan_out_woLFE, 1 );
289 : }
290 : ELSE
291 : {
292 60839 : nCPE = add( shr( hMCT->nchan_out_woLFE, 1 ), 1 );
293 : }
294 :
295 379397 : FOR( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
296 : {
297 828084 : FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
298 : {
299 : Word16 norm;
300 552056 : IF( EQ_32( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->mct_chan_mode, MCT_CHAN_MODE_IGNORE ) )
301 : {
302 69532 : 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 482524 : IF( EQ_16( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->tcxMode, TCX_20 ) )
308 : {
309 471251 : length = st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX;
310 471251 : move16();
311 : }
312 : ELSE
313 : {
314 11273 : 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 482524 : IF( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->last_core == ACELP_CORE )
318 : {
319 0 : length = add( length, shr( length, 2 ) );
320 : }
321 482524 : IF( hMCT->p_orig_spectrum_long_fx[cpe_id][ch] )
322 : {
323 482524 : norm = L_norm_arr( hMCT->p_orig_spectrum_long_fx[cpe_id][ch], L_FRAME48k );
324 482524 : q_origSpec = s_min( q_origSpec, add( norm, hMCT->q_orig_spectrum_long_fx[cpe_id][ch] ) );
325 : }
326 : }
327 : }
328 103369 : q_origSpec = sub( q_origSpec, 1 );
329 :
330 379397 : FOR( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
331 : {
332 828084 : FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
333 : {
334 552056 : IF( EQ_32( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->mct_chan_mode, MCT_CHAN_MODE_IGNORE ) )
335 : {
336 69532 : CONTINUE;
337 : }
338 482524 : IF( hMCT->p_orig_spectrum_long_fx[cpe_id][ch] )
339 : {
340 482524 : 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 482524 : hMCT->q_orig_spectrum_long_fx[cpe_id][ch] = q_origSpec;
342 482524 : move16();
343 : }
344 : }
345 : }
346 103369 : hMCT->q_orig_spectrum_long_com = q_origSpec;
347 103369 : move16();
348 :
349 379397 : FOR( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
350 : {
351 828084 : FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
352 : {
353 552056 : IF( EQ_16( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->core, TCX_20 ) )
354 : {
355 540776 : length = st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX;
356 540776 : move16();
357 : }
358 : ELSE
359 : {
360 11280 : length = shr( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX, NB_DIV_LOG2 );
361 : }
362 552056 : IF( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->last_core == ACELP_CORE )
363 : {
364 0 : length = add( length, shr( length, 2 ) );
365 : }
366 552056 : Word16 norm = L_norm_arr( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_fx, N_MAX );
367 552056 : q_spec = s_min( q_spec, add( sub( 31, st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_e ), norm ) );
368 552056 : q_spec = s_min( q_spec, add( sub( 31, st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_e ), norm ) );
369 :
370 1115392 : FOR( Word16 k = 0; k <= ( ( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->core == TCX_20_CORE ) ? 1 : NB_DIV ) - 1; k++ )
371 : {
372 563336 : IF( hMCT->p_mdst_spectrum_long_fx[cpe_id][ch] )
373 : {
374 563336 : norm = L_norm_arr( &hMCT->p_mdst_spectrum_long_fx[cpe_id][ch][k * N_TCX10_MAX], length );
375 563336 : q_spec = s_min( q_spec, add( norm, hMCT->q_mdst_spectrum_long_fx[cpe_id][ch][k] ) );
376 : }
377 : }
378 : }
379 : }
380 :
381 379397 : FOR( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
382 : {
383 828084 : FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
384 : {
385 552056 : IF( EQ_16( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->core, TCX_20 ) )
386 : {
387 540776 : length = st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX;
388 540776 : move16();
389 : }
390 : ELSE
391 : {
392 11280 : length = shr( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX, NB_DIV_LOG2 );
393 : }
394 552056 : 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 552056 : st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_e[0] = sub( 31, q_spec );
397 552056 : move16();
398 552056 : st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_e[1] = sub( 31, q_spec );
399 552056 : move16();
400 552056 : st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_e = sub( 31, q_spec );
401 552056 : move16();
402 :
403 552056 : IF( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->last_core == ACELP_CORE )
404 : {
405 0 : length = add( length, shr( length, 2 ) );
406 : }
407 :
408 1115392 : FOR( Word16 k = 0; k <= ( ( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->core == TCX_20_CORE ) ? 1 : NB_DIV ) - 1; k++ )
409 : {
410 563336 : IF( hMCT->p_mdst_spectrum_long_fx[cpe_id][ch] )
411 : {
412 563336 : 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 563336 : hMCT->q_mdst_spectrum_long_fx[cpe_id][ch][k] = q_spec;
414 563336 : move16();
415 : }
416 : }
417 : }
418 : }
419 :
420 : /* joint MCT encoding */
421 103369 : 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 379397 : FOR( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
424 : {
425 828084 : FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
426 : {
427 552056 : IF( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->core == TCX_20 )
428 : {
429 540776 : length = st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX;
430 540776 : move16();
431 : }
432 : ELSE
433 : {
434 11280 : length = shr( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX, NB_DIV_LOG2 );
435 : }
436 552056 : 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 552056 : IF( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->last_core == ACELP_CORE )
438 : {
439 0 : length = add( length, shr( length, 2 ) );
440 : }
441 552056 : IF( EQ_16( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->core, TCX_20 ) )
442 : {
443 540776 : 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 540776 : 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 540776 : st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_e = e_spec;
446 540776 : move16();
447 : }
448 : ELSE
449 : {
450 11280 : 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 11280 : 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 11280 : 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 11280 : 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 11280 : st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_e = e_spec;
455 11280 : move16();
456 : }
457 552056 : st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_e[0] = st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_e;
458 552056 : move16();
459 552056 : st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_e[1] = st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_e;
460 552056 : move16();
461 : }
462 : }
463 :
464 103369 : q_spec = 31;
465 103369 : move16();
466 :
467 : /* Spectrum quantization and coding */
468 379397 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
469 : {
470 276028 : hCPE = st_ivas->hCPE[cpe_id];
471 :
472 276028 : IF( cpe_id > 0 )
473 : {
474 172659 : 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 276028 : 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 828084 : FOR( n = 0; n < CPE_CHANNELS; n++ )
481 : {
482 552056 : Copy( hCPE->hCoreCoder[n]->input_fx, hCPE->hCoreCoder[n]->old_input_signal_fx, input_frame );
483 552056 : hCPE->hCoreCoder[n]->q_old_inp = hCPE->hCoreCoder[n]->q_inp;
484 552056 : move16();
485 552056 : Copy32( hCPE->hCoreCoder[n]->input32_fx, hCPE->hCoreCoder[n]->old_input_signal32_fx, input_frame );
486 552056 : hCPE->hCoreCoder[n]->q_old_inp32 = hCPE->hCoreCoder[n]->q_inp32;
487 552056 : move16();
488 : /* common encoder updates */
489 :
490 552056 : updt_enc_common_ivas_fx( hCPE->hCoreCoder[n], Q_new_out[cpe_id][n] );
491 : }
492 : }
493 :
494 103369 : pop_wmops();
495 103369 : return error;
496 : }
497 :
498 : /*-------------------------------------------------------------------------
499 : * create_mct_enc()
500 : *
501 : * Create, allocate and initialize IVAS encoder MCT handle
502 : *-------------------------------------------------------------------------*/
503 :
504 1066 : 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 1066 : ivas_format = st_ivas->hEncoderConfig->ivas_format;
517 1066 : move16();
518 1066 : ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
519 1066 : move32();
520 :
521 : /*-----------------------------------------------------------------*
522 : * Allocate MCT handle
523 : *-----------------------------------------------------------------*/
524 :
525 1066 : 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 1066 : test();
536 1066 : test();
537 1066 : test();
538 1066 : test();
539 1066 : IF( EQ_16( ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_MCT ) )
540 : {
541 399 : hMCT->nchan_out_woLFE = sub( st_ivas->hEncoderConfig->nchan_inp, 1 ); /* LFE channel is coded separately */
542 399 : move16();
543 : }
544 667 : ELSE IF( EQ_16( ivas_format, SBA_FORMAT ) || EQ_16( ivas_format, SBA_ISM_FORMAT ) )
545 : {
546 644 : hMCT->nchan_out_woLFE = ivas_get_sba_num_TCs_fx( ivas_total_brate, st_ivas->sba_analysis_order );
547 644 : move16();
548 :
549 644 : test();
550 644 : IF( EQ_16( ivas_format, SBA_ISM_FORMAT ) && EQ_16( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
551 : {
552 212 : hMCT->nchan_out_woLFE = add( hMCT->nchan_out_woLFE, st_ivas->hEncoderConfig->nchan_ism );
553 212 : 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 1066 : 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 1066 : IF( EQ_16( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
571 : {
572 212 : 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 4069 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
577 : {
578 9009 : FOR( n = 0; n < CPE_CHANNELS; n++ )
579 : {
580 6006 : 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 1066 : IF( hMCT->nchan_out_woLFE % 2 )
586 : {
587 783 : st_ivas->hCPE[st_ivas->nCPE - 1]->hCoreCoder[1]->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
588 783 : move16();
589 : }
590 :
591 : /*Initialize MCT block data */
592 1066 : max_blocks = shr( hMCT->nchan_out_woLFE, 1 ); // hMCT->nchan_out_woLFE / CPE_CHANNELS
593 :
594 3286 : FOR( n = 0; n < max_blocks; n++ )
595 : {
596 2220 : assert( st_ivas->hEncoderConfig->element_mode_init == IVAS_CPE_MDCT && "MCT is not supported for other stereo modes" );
597 :
598 2220 : 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 2220 : hMCT->hBlockData[n]->isActive = 0;
605 2220 : move16();
606 2220 : hMCT->hBlockData[n]->ch1 = 0;
607 2220 : move16();
608 2220 : hMCT->hBlockData[n]->ch2 = 0;
609 2220 : move16();
610 :
611 : /*-----------------------------------------------------------------*
612 : * MDCT stereo initialization
613 : *-----------------------------------------------------------------*/
614 :
615 2220 : 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 2220 : 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 5242 : FOR( ; n < MCT_MAX_BLOCKS; n++ )
633 : {
634 4176 : hMCT->hBlockData[n] = NULL;
635 : }
636 :
637 : /*-----------------------------------------------------------------*
638 : * Initializations
639 : *-----------------------------------------------------------------*/
640 :
641 1066 : set_mct_enc_params_fx( hMCT, ivas_total_brate, ivas_format, 1 );
642 :
643 1066 : st_ivas->hMCT = hMCT;
644 :
645 1066 : return IVAS_ERR_OK;
646 : }
647 :
648 :
649 : /*-------------------------------------------------------------------------
650 : * mct_enc_reconfigure()
651 : *
652 : * Reconfigure IVAS encoder MCT handle
653 : *-------------------------------------------------------------------------*/
654 :
655 508 : 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 508 : error = IVAS_ERR_OK;
668 508 : move32();
669 :
670 508 : hMCT = st_ivas->hMCT;
671 508 : ivas_format = st_ivas->hEncoderConfig->ivas_format;
672 508 : move16();
673 508 : ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
674 508 : move32();
675 :
676 : /*-----------------------------------------------------------------*
677 : * Allocate and initialize MCT BlockData handles
678 : *-----------------------------------------------------------------*/
679 :
680 508 : IF( b_nchan_change )
681 : {
682 174 : test();
683 174 : test();
684 174 : test();
685 174 : test();
686 : /* Determine active channels */
687 174 : 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 167 : 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 166 : 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 161 : ELSE IF( EQ_16( ivas_format, SBA_FORMAT ) || EQ_16( ivas_format, SBA_ISM_FORMAT ) )
700 : {
701 161 : hMCT->nchan_out_woLFE = st_ivas->nchan_transport;
702 161 : move16();
703 161 : IF( EQ_16( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
704 : {
705 59 : hMCT->nchan_out_woLFE = add( hMCT->nchan_out_woLFE, st_ivas->hEncoderConfig->nchan_ism );
706 59 : move16();
707 : }
708 : }
709 : }
710 :
711 : Word32 L_tmp;
712 508 : 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 508 : IF( EQ_16( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
715 : {
716 98 : 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 1920 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
721 : {
722 1412 : st_ivas->hCPE[cpe_id]->element_brate = cp_bitrate;
723 1412 : move32();
724 4236 : FOR( n = 0; n < CPE_CHANNELS; n++ )
725 : {
726 2824 : st_ivas->hCPE[cpe_id]->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
727 2824 : move16();
728 : }
729 : }
730 :
731 : /* in case we have an uneven number of transport channels, indicate last channel ID as inactive */
732 508 : IF( s_and( hMCT->nchan_out_woLFE, 1 ) )
733 : {
734 400 : st_ivas->hCPE[st_ivas->nCPE - 1]->hCoreCoder[1]->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
735 400 : 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 508 : IF( NE_32( ivas_total_brate, st_ivas->hEncoderConfig->last_ivas_total_brate ) )
741 : {
742 1920 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
743 : {
744 4236 : FOR( n = 0; n < CPE_CHANNELS; n++ )
745 : {
746 2824 : st = st_ivas->hCPE[cpe_id]->hCoreCoder[n];
747 :
748 2824 : st->total_brate = st_ivas->hCPE[cpe_id]->element_brate;
749 2824 : move32();
750 :
751 2824 : IF( NE_16( st->mct_chan_mode, MCT_CHAN_MODE_IGNORE ) )
752 : {
753 2424 : st->bits_frame_nominal = extract_l( Mpy_32_32( st_ivas->hCPE[cpe_id]->element_brate, 42949673 ) /* 1/50 in Q31*/ );
754 2424 : move16();
755 2424 : st->igf = getIgfPresent_fx( st->element_mode, L_mult0( st->bits_frame_nominal, FRAMES_PER_SEC ), st->bwidth, st->rf_mode );
756 2424 : move16();
757 :
758 2424 : 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 508 : max_blocks = shr( hMCT->nchan_out_woLFE, 1 ); // hMCT->nchan_out_woLFE / CPE_CHANNELS
769 :
770 1520 : FOR( n = 0; n < max_blocks; n++ )
771 : {
772 1012 : UWord16 mem_init = 0;
773 1012 : move16();
774 1012 : assert( st_ivas->hEncoderConfig->element_mode_init == IVAS_CPE_MDCT && "MCT is not supported for other stereo modes" );
775 :
776 1012 : IF( b_nchan_change )
777 : {
778 367 : IF( hMCT->hBlockData[n] == NULL )
779 : {
780 117 : mem_init = 1;
781 117 : move16();
782 117 : 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 117 : hMCT->hBlockData[n]->isActive = 0;
789 117 : move16();
790 117 : hMCT->hBlockData[n]->ch1 = 0;
791 117 : move16();
792 117 : hMCT->hBlockData[n]->ch2 = 0;
793 117 : move16();
794 :
795 : /*-----------------------------------------------------------------*
796 : * MDCT stereo initialization
797 : *-----------------------------------------------------------------*/
798 :
799 117 : 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 1012 : 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 2544 : FOR( ; n < MCT_MAX_BLOCKS; n++ )
819 : {
820 : /* deallocate no longer needed blocks */
821 2036 : IF( hMCT->hBlockData[n] != NULL )
822 : {
823 108 : IF( hMCT->hBlockData[n]->hStereoMdct != NULL )
824 : {
825 108 : free( hMCT->hBlockData[n]->hStereoMdct );
826 108 : hMCT->hBlockData[n]->hStereoMdct = NULL;
827 : }
828 :
829 108 : free( hMCT->hBlockData[n] );
830 108 : hMCT->hBlockData[n] = NULL;
831 : }
832 : }
833 :
834 : /*-----------------------------------------------------------------*
835 : * Initializations
836 : *-----------------------------------------------------------------*/
837 :
838 508 : set_mct_enc_params_fx( hMCT, ivas_total_brate, ivas_format, b_nchan_change );
839 :
840 508 : return error;
841 : }
842 :
843 :
844 : /*-------------------------------------------------------------------------
845 : * ivas_mct_enc_close()
846 : *
847 : * Close MCT
848 : *-------------------------------------------------------------------------*/
849 :
850 1526 : void ivas_mct_enc_close_fx(
851 : MCT_ENC_HANDLE *hMCT /* i/o: MCT encoder structure */
852 : )
853 : {
854 : Word16 n, maxBlocks;
855 :
856 1526 : test();
857 1526 : IF( hMCT == NULL || *hMCT == NULL )
858 : {
859 460 : return;
860 : }
861 :
862 1066 : maxBlocks = shr( ( *hMCT )->nchan_out_woLFE, 1 );
863 :
864 3295 : FOR( n = 0; n < maxBlocks; n++ )
865 : {
866 2229 : IF( ( *hMCT )->hBlockData[n] != NULL )
867 : {
868 2229 : IF( ( *hMCT )->hBlockData[n]->hStereoMdct != NULL )
869 : {
870 2229 : free( ( *hMCT )->hBlockData[n]->hStereoMdct );
871 2229 : ( *hMCT )->hBlockData[n]->hStereoMdct = NULL;
872 : }
873 :
874 2229 : free( ( *hMCT )->hBlockData[n] );
875 2229 : ( *hMCT )->hBlockData[n] = NULL;
876 : }
877 : }
878 :
879 1066 : free( ( *hMCT ) );
880 1066 : ( *hMCT ) = NULL;
881 :
882 1066 : 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 49850 : 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 49850 : error = IVAS_ERR_OK;
903 49850 : move32();
904 :
905 49850 : last_mc_mode = st_ivas->mc_mode;
906 49850 : move32();
907 :
908 : /* select MC format mode */
909 49850 : st_ivas->mc_mode = ivas_mc_mode_select_fx( st_ivas->hEncoderConfig->mc_input_setup, st_ivas->hEncoderConfig->ivas_total_brate );
910 49850 : move32();
911 :
912 : /* write MC LS setup */
913 49850 : IF( st_ivas->nSCE > 0 )
914 : {
915 11477 : st0 = st_ivas->hSCE[0]->hCoreCoder[0];
916 : }
917 : ELSE
918 : {
919 38373 : st0 = st_ivas->hCPE[0]->hCoreCoder[0];
920 : }
921 :
922 49850 : push_indice( st0->hBstr, IND_SMODE, st_ivas->hEncoderConfig->mc_input_setup, MC_LS_SETUP_BITS );
923 :
924 : /* MC format switching */
925 49850 : test();
926 49850 : 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 1277 : 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 49850 : return error;
935 : }
936 :
937 :
938 : /*-------------------------------------------------------------------------
939 : * ivas_mc_enc_reconfig()
940 : *
941 : * Reconfigure the MC format encoder
942 : *-------------------------------------------------------------------------*/
943 :
944 1277 : 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 1277 : error = IVAS_ERR_OK;
954 1277 : move32();
955 :
956 1277 : nchan_transport_old = st_ivas->nchan_transport;
957 1277 : move16();
958 1277 : nSCE_old = st_ivas->nSCE;
959 1277 : move16();
960 1277 : nCPE_old = st_ivas->nCPE;
961 1277 : move16();
962 :
963 : /*-----------------------------------------------------------------*
964 : * Reconfigure MC modules
965 : *-----------------------------------------------------------------*/
966 :
967 1277 : IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) )
968 : {
969 563 : st_ivas->nSCE = 0;
970 563 : move16();
971 563 : st_ivas->nCPE = shr( st_ivas->hEncoderConfig->nchan_inp, 1 );
972 563 : move16();
973 :
974 563 : st_ivas->nchan_transport = ivas_mc_ls_setup_get_num_channels_fx( st_ivas->hEncoderConfig->mc_input_setup );
975 563 : move16();
976 :
977 563 : IF( NE_32( last_mc_mode, MC_MODE_MCT ) )
978 : {
979 370 : 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 370 : 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 370 : ivas_param_mc_enc_close_fx( &( st_ivas->hParamMC ), st_ivas->hEncoderConfig->input_Fs );
993 :
994 370 : ivas_mc_paramupmix_enc_close_fx( &( st_ivas->hMCParamUpmix ), st_ivas->hEncoderConfig->input_Fs );
995 :
996 : /* De-allocate McMasa-related handles */
997 370 : ivas_mcmasa_enc_close_fx( &( st_ivas->hMcMasa ), st_ivas->hEncoderConfig->input_Fs );
998 :
999 370 : ivas_masa_enc_close_fx( &( st_ivas->hMasa ) );
1000 :
1001 370 : ivas_qmetadata_close_fx( &st_ivas->hQMetaData );
1002 : }
1003 : }
1004 714 : 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 707 : ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) )
1046 : {
1047 402 : IF( NE_32( last_mc_mode, MC_MODE_PARAMMC ) )
1048 : {
1049 323 : 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 79 : 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 402 : ivas_mcmasa_enc_close_fx( &( st_ivas->hMcMasa ), st_ivas->hEncoderConfig->input_Fs );
1064 :
1065 402 : IF( st_ivas->hMasa != NULL )
1066 : {
1067 111 : ivas_masa_enc_close_fx( &( st_ivas->hMasa ) );
1068 111 : st_ivas->hMasa = NULL;
1069 : }
1070 :
1071 402 : ivas_mc_paramupmix_enc_close_fx( &( st_ivas->hMCParamUpmix ), st_ivas->hEncoderConfig->input_Fs );
1072 :
1073 402 : test();
1074 402 : 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 402 : ivas_qmetadata_close_fx( &st_ivas->hQMetaData );
1081 :
1082 : /* De-allocate MCT handle if last mode was MCT */
1083 402 : test();
1084 402 : IF( EQ_32( last_mc_mode, MC_MODE_MCT ) && LE_16( st_ivas->nchan_transport, CPE_CHANNELS ) )
1085 : {
1086 207 : ivas_mct_enc_close_fx( &( st_ivas->hMCT ) );
1087 : }
1088 :
1089 402 : test();
1090 402 : IF( EQ_32( last_mc_mode, MC_MODE_MCT ) && st_ivas->hLFE != NULL )
1091 : {
1092 : /* LFE handle */
1093 212 : ivas_lfe_enc_close_fx( &( st_ivas->hLFE ) );
1094 : }
1095 : }
1096 305 : ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
1097 : {
1098 305 : IF( NE_32( last_mc_mode, MC_MODE_MCMASA ) )
1099 : {
1100 257 : ivas_mcmasa_setNumTransportChannels_fx( &( st_ivas->nchan_transport ), &( st_ivas->hEncoderConfig->element_mode_init ), st_ivas->hEncoderConfig->ivas_total_brate );
1101 :
1102 257 : IF( NE_32( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ), IVAS_ERR_OK ) )
1103 : {
1104 0 : return error;
1105 : }
1106 :
1107 257 : IF( NE_32( ( error = ivas_masa_enc_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
1108 : {
1109 0 : return error;
1110 : }
1111 257 : 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 305 : ivas_param_mc_enc_close_fx( &( st_ivas->hParamMC ), st_ivas->hEncoderConfig->input_Fs );
1126 :
1127 305 : ivas_mc_paramupmix_enc_close_fx( &( st_ivas->hMCParamUpmix ), st_ivas->hEncoderConfig->input_Fs );
1128 :
1129 305 : test();
1130 305 : 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 305 : IF( EQ_32( last_mc_mode, MC_MODE_MCT ) )
1137 : {
1138 : /* LFE handle */
1139 146 : ivas_lfe_enc_close_fx( &( st_ivas->hLFE ) );
1140 :
1141 146 : ivas_mct_enc_close_fx( &( st_ivas->hMCT ) );
1142 : }
1143 : }
1144 :
1145 1277 : IF( NE_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
1146 : {
1147 972 : 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 972 : st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
1155 972 : 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 1277 : test();
1168 1277 : test();
1169 1277 : 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( extract_l( Mult_32_16( st->input_Fs, INV_FRAME_PER_SEC_Q15 ) ), 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 1277 : IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
1209 : {
1210 305 : 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 972 : ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) )
1213 : {
1214 563 : new_brate_SCE = 0;
1215 563 : move32();
1216 : Word32 res_dec, res_frac;
1217 563 : iDiv_and_mod_32( st_ivas->hEncoderConfig->ivas_total_brate, sub( st_ivas->nchan_transport, 1 ), &res_dec, &res_frac, 0 );
1218 563 : new_brate_CPE = imult3216( res_dec, CPE_CHANNELS );
1219 : }
1220 409 : 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 402 : new_brate_SCE = 0; /*st_ivas->hEncoderConfig->ivas_total_brate / st_ivas->nchan_transport;*/
1231 402 : move32();
1232 : Word32 res_dec, res_frac;
1233 402 : iDiv_and_mod_32( st_ivas->hEncoderConfig->ivas_total_brate, st_ivas->nchan_transport, &res_dec, &res_frac, 0 );
1234 402 : new_brate_CPE = imult3216( res_dec, CPE_CHANNELS );
1235 : }
1236 :
1237 1277 : 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 1277 : return error;
1243 : }
|