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 76379 : 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 445957 : IF( EQ_16( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->tcxMode, TCX_20 ) )
310 : {
311 436309 : length = st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX;
312 436309 : move16();
313 : }
314 : ELSE
315 : {
316 9648 : 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 445957 : IF( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->last_core == ACELP_CORE )
320 : {
321 0 : length = add( length, idiv1616( length, 4 ) );
322 : }
323 445957 : IF( hMCT->p_orig_spectrum_long_fx[cpe_id][ch] )
324 : {
325 445957 : norm = L_norm_arr( hMCT->p_orig_spectrum_long_fx[cpe_id][ch], L_FRAME48k );
326 445957 : 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 76379 : CONTINUE;
339 : }
340 445957 : IF( hMCT->p_orig_spectrum_long_fx[cpe_id][ch] )
341 : {
342 445957 : 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 445957 : hMCT->q_orig_spectrum_long_fx[cpe_id][ch] = q_origSpec;
344 445957 : 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 512682 : length = st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX;
358 512682 : move16();
359 : }
360 : ELSE
361 : {
362 9654 : 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 4 : 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 1054326 : FOR( Word16 k = 0; k <= ( ( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->core == TCX_20_CORE ) ? 1 : NB_DIV ) - 1; k++ )
373 : {
374 531990 : IF( hMCT->p_mdst_spectrum_long_fx[cpe_id][ch] )
375 : {
376 531990 : norm = L_norm_arr( &hMCT->p_mdst_spectrum_long_fx[cpe_id][ch][k * N_TCX10_MAX], length );
377 531990 : 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 512682 : length = st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX;
390 512682 : move16();
391 : }
392 : ELSE
393 : {
394 9654 : 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 4 : length = add( length, idiv1616( length, 4 ) );
408 : }
409 :
410 1054326 : FOR( Word16 k = 0; k <= ( ( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->core == TCX_20_CORE ) ? 1 : NB_DIV ) - 1; k++ )
411 : {
412 531990 : IF( hMCT->p_mdst_spectrum_long_fx[cpe_id][ch] )
413 : {
414 531990 : 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 531990 : hMCT->q_mdst_spectrum_long_fx[cpe_id][ch][k] = q_spec;
416 531990 : 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 512682 : length = st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->L_frameTCX;
432 512682 : move16();
433 : }
434 : ELSE
435 : {
436 9654 : 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 4 : length = add( length, shr( length, 2 ) );
442 : }
443 522336 : IF( EQ_16( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->core, TCX_20 ) )
444 : {
445 512682 : 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 512682 : 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 512682 : st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_e = e_spec;
448 512682 : move16();
449 : }
450 : ELSE
451 : {
452 9654 : 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 9654 : 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 9654 : 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 9654 : 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 9654 : st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->hTcxEnc->spectrum_long_e = e_spec;
457 9654 : 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 522336 : Copy32( hCPE->hCoreCoder[n]->input32_fx, hCPE->hCoreCoder[n]->old_input_signal32_fx, input_frame );
488 522336 : hCPE->hCoreCoder[n]->q_old_inp32 = hCPE->hCoreCoder[n]->q_inp32;
489 522336 : move16();
490 : /* common encoder updates */
491 :
492 522336 : updt_enc_common_ivas_fx( hCPE->hCoreCoder[n], Q_new_out[cpe_id][n] );
493 : }
494 : }
495 :
496 99969 : pop_wmops();
497 99969 : return error;
498 : }
499 :
500 : /*-------------------------------------------------------------------------
501 : * create_mct_enc()
502 : *
503 : * Create, allocate and initialize IVAS encoder MCT handle
504 : *-------------------------------------------------------------------------*/
505 :
506 897 : ivas_error create_mct_enc_fx(
507 : Encoder_Struct *st_ivas /* i/o: IVAS encoder structure */
508 : )
509 : {
510 : MCT_ENC_HANDLE hMCT;
511 : Word16 n;
512 : Word32 cp_bitrate;
513 : Word16 max_blocks;
514 : Word16 cpe_id;
515 : IVAS_FORMAT ivas_format;
516 : Word32 ivas_total_brate;
517 :
518 897 : ivas_format = st_ivas->hEncoderConfig->ivas_format;
519 897 : move16();
520 897 : ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
521 897 : move32();
522 :
523 : /*-----------------------------------------------------------------*
524 : * Allocate MCT handle
525 : *-----------------------------------------------------------------*/
526 :
527 897 : IF( ( hMCT = (MCT_ENC_HANDLE) malloc( sizeof( MCT_ENC_DATA ) ) ) == NULL )
528 : {
529 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MCT\n" ) );
530 : }
531 :
532 : /*-----------------------------------------------------------------*
533 : * Allocate and initialize MCT BlockData handles
534 : *-----------------------------------------------------------------*/
535 :
536 : /* Determine active channels */
537 897 : test();
538 897 : test();
539 897 : test();
540 897 : test();
541 897 : IF( EQ_16( ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_MCT ) )
542 : {
543 315 : hMCT->nchan_out_woLFE = sub( st_ivas->hEncoderConfig->nchan_inp, 1 ); /* LFE channel is coded separately */
544 315 : move16();
545 : }
546 582 : ELSE IF( EQ_16( ivas_format, SBA_FORMAT ) || EQ_16( ivas_format, SBA_ISM_FORMAT ) )
547 : {
548 559 : hMCT->nchan_out_woLFE = ivas_get_sba_num_TCs_fx( ivas_total_brate, st_ivas->sba_analysis_order );
549 559 : move16();
550 :
551 559 : test();
552 559 : IF( EQ_16( ivas_format, SBA_ISM_FORMAT ) && EQ_16( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
553 : {
554 84 : hMCT->nchan_out_woLFE = add( hMCT->nchan_out_woLFE, st_ivas->hEncoderConfig->nchan_ism );
555 84 : move16();
556 : }
557 : }
558 23 : ELSE IF( EQ_16( ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_PARAMMC ) )
559 : {
560 12 : hMCT->nchan_out_woLFE = ivas_param_mc_getNumTransportChannels_fx( ivas_total_brate, st_ivas->hEncoderConfig->mc_input_setup );
561 12 : move16();
562 : }
563 11 : ELSE IF( EQ_16( ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
564 : {
565 11 : hMCT->nchan_out_woLFE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS - 1;
566 11 : move16();
567 : }
568 :
569 : Word32 L_tmp;
570 897 : iDiv_and_mod_32( L_shl( ivas_total_brate, 1 ), hMCT->nchan_out_woLFE, &cp_bitrate, &L_tmp, 0 );
571 : // cp_bitrate = ivas_total_brate / hMCT->nchan_out_woLFE * CPE_CHANNELS;
572 897 : IF( EQ_16( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
573 : {
574 84 : iDiv_and_mod_32( L_shl( ivas_total_brate, 1 ), st_ivas->nchan_transport, &cp_bitrate, &L_tmp, 0 );
575 : // cp_bitrate = ivas_total_brate / st_ivas->nchan_transport * CPE_CHANNELS;
576 : }
577 :
578 3299 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
579 : {
580 7206 : FOR( n = 0; n < CPE_CHANNELS; n++ )
581 : {
582 4804 : st_ivas->hCPE[cpe_id]->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
583 : }
584 : }
585 :
586 : /* in case we have an uneven number of transport channels, indicate last channel ID as inactive */
587 897 : IF( hMCT->nchan_out_woLFE % 2 )
588 : {
589 690 : st_ivas->hCPE[st_ivas->nCPE - 1]->hCoreCoder[1]->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
590 690 : move16();
591 : }
592 :
593 : /*Initialize MCT block data */
594 897 : max_blocks = shr( hMCT->nchan_out_woLFE, 1 ); // hMCT->nchan_out_woLFE / CPE_CHANNELS
595 :
596 2609 : FOR( n = 0; n < max_blocks; n++ )
597 : {
598 1712 : assert( st_ivas->hEncoderConfig->element_mode_init == IVAS_CPE_MDCT && "MCT is not supported for other stereo modes" );
599 :
600 1712 : IF( ( hMCT->hBlockData[n] = (MCT_BLOCK_DATA_HANDLE) malloc( sizeof( MCT_BLOCK_DATA ) ) ) == NULL )
601 : {
602 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MCT block data structure\n" ) );
603 : }
604 :
605 : /*Initialize all parameters to zero*/
606 1712 : hMCT->hBlockData[n]->isActive = 0;
607 1712 : move16();
608 1712 : hMCT->hBlockData[n]->ch1 = 0;
609 1712 : move16();
610 1712 : hMCT->hBlockData[n]->ch2 = 0;
611 1712 : move16();
612 :
613 : /*-----------------------------------------------------------------*
614 : * MDCT stereo initialization
615 : *-----------------------------------------------------------------*/
616 :
617 1712 : IF( ( hMCT->hBlockData[n]->hStereoMdct = (STEREO_MDCT_ENC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_ENC_DATA ) ) ) == NULL )
618 : {
619 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
620 : }
621 :
622 : #ifdef DEBUGGING
623 : hMCT->hBlockData[n]->hStereoMdct->mdct_stereo_mode_cmdl = SMDCT_MS_DECISION;
624 : #endif
625 :
626 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 );
627 : }
628 :
629 4567 : FOR( ; n < MCT_MAX_BLOCKS; n++ )
630 : {
631 3670 : hMCT->hBlockData[n] = NULL;
632 : }
633 :
634 : /*-----------------------------------------------------------------*
635 : * Initializations
636 : *-----------------------------------------------------------------*/
637 :
638 897 : set_mct_enc_params_fx( hMCT, ivas_total_brate, ivas_format, 1 );
639 :
640 897 : st_ivas->hMCT = hMCT;
641 :
642 897 : return IVAS_ERR_OK;
643 : }
644 :
645 :
646 : /*-------------------------------------------------------------------------
647 : * mct_enc_reconfigure()
648 : *
649 : * Reconfigure IVAS encoder MCT handle
650 : *-------------------------------------------------------------------------*/
651 :
652 412 : ivas_error mct_enc_reconfigure_fx(
653 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
654 : const UWord16 b_nchan_change /* i : flag indicating different channel count */
655 : )
656 : {
657 : MCT_ENC_HANDLE hMCT;
658 : Encoder_State *st;
659 : Word16 n, cpe_id, max_blocks;
660 : Word32 ivas_total_brate, cp_bitrate;
661 : IVAS_FORMAT ivas_format;
662 : ivas_error error;
663 :
664 412 : error = IVAS_ERR_OK;
665 412 : move32();
666 :
667 412 : hMCT = st_ivas->hMCT;
668 412 : ivas_format = st_ivas->hEncoderConfig->ivas_format;
669 412 : move16();
670 412 : ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
671 412 : move32();
672 :
673 : /*-----------------------------------------------------------------*
674 : * Allocate and initialize MCT BlockData handles
675 : *-----------------------------------------------------------------*/
676 :
677 412 : IF( b_nchan_change )
678 : {
679 123 : test();
680 123 : test();
681 123 : test();
682 123 : test();
683 : /* Determine active channels */
684 123 : IF( EQ_16( ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_MCT ) )
685 : {
686 7 : hMCT->nchan_out_woLFE = sub( st_ivas->hEncoderConfig->nchan_inp, 1 ); /* LFE channel is coded separately */
687 : }
688 116 : ELSE IF( EQ_16( ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
689 : {
690 1 : hMCT->nchan_out_woLFE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS - 1;
691 : }
692 115 : ELSE IF( EQ_16( ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_PARAMMC ) )
693 : {
694 5 : hMCT->nchan_out_woLFE = ivas_param_mc_getNumTransportChannels_fx( ivas_total_brate, st_ivas->hEncoderConfig->mc_input_setup );
695 : }
696 110 : ELSE IF( EQ_16( ivas_format, SBA_FORMAT ) || EQ_16( ivas_format, SBA_ISM_FORMAT ) )
697 : {
698 110 : hMCT->nchan_out_woLFE = st_ivas->nchan_transport;
699 110 : move16();
700 110 : IF( EQ_16( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
701 : {
702 23 : hMCT->nchan_out_woLFE = add( hMCT->nchan_out_woLFE, st_ivas->hEncoderConfig->nchan_ism );
703 23 : move16();
704 : }
705 : }
706 : }
707 :
708 : Word32 L_tmp;
709 412 : iDiv_and_mod_32( L_shl( ivas_total_brate, 1 ), hMCT->nchan_out_woLFE, &cp_bitrate, &L_tmp, 0 );
710 : // cp_bitrate = ivas_total_brate / hMCT->nchan_out_woLFE * CPE_CHANNELS;
711 412 : IF( EQ_16( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
712 : {
713 33 : iDiv_and_mod_32( L_shl( ivas_total_brate, 1 ), st_ivas->nchan_transport, &cp_bitrate, &L_tmp, 0 );
714 : // cp_bitrate = ivas_total_brate / st_ivas->nchan_transport * CPE_CHANNELS;
715 : }
716 :
717 1492 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
718 : {
719 1080 : st_ivas->hCPE[cpe_id]->element_brate = cp_bitrate;
720 1080 : move32();
721 3240 : FOR( n = 0; n < CPE_CHANNELS; n++ )
722 : {
723 2160 : st_ivas->hCPE[cpe_id]->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
724 2160 : move16();
725 : }
726 : }
727 :
728 : /* in case we have an uneven number of transport channels, indicate last channel ID as inactive */
729 412 : IF( s_and( hMCT->nchan_out_woLFE, 1 ) )
730 : {
731 340 : st_ivas->hCPE[st_ivas->nCPE - 1]->hCoreCoder[1]->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
732 340 : move16();
733 : }
734 :
735 : /* set correct nominal bitrates and igf config already here, otherwise we
736 : * run into a number of problems; only do it when the bitrate changes */
737 412 : IF( NE_32( ivas_total_brate, st_ivas->hEncoderConfig->last_ivas_total_brate ) )
738 : {
739 1492 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
740 : {
741 3240 : FOR( n = 0; n < CPE_CHANNELS; n++ )
742 : {
743 2160 : st = st_ivas->hCPE[cpe_id]->hCoreCoder[n];
744 :
745 2160 : st->total_brate = st_ivas->hCPE[cpe_id]->element_brate;
746 2160 : move32();
747 :
748 2160 : IF( NE_16( st->mct_chan_mode, MCT_CHAN_MODE_IGNORE ) )
749 : {
750 1820 : st->bits_frame_nominal = extract_l( Mpy_32_32( st_ivas->hCPE[cpe_id]->element_brate, 42949673 ) /* 1/50 in Q31*/ );
751 1820 : move16();
752 1820 : st->igf = getIgfPresent_fx( st->element_mode, L_mult0( st->bits_frame_nominal, FRAMES_PER_SEC ), st->bwidth, st->rf_mode );
753 1820 : move16();
754 :
755 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 )
756 : {
757 0 : return error;
758 : }
759 : }
760 : }
761 : }
762 : }
763 :
764 : /* Initialize MCT block data */
765 412 : max_blocks = shr( hMCT->nchan_out_woLFE, 1 ); // hMCT->nchan_out_woLFE / CPE_CHANNELS
766 :
767 1152 : FOR( n = 0; n < max_blocks; n++ )
768 : {
769 740 : UWord16 mem_init = 0;
770 740 : move16();
771 740 : assert( st_ivas->hEncoderConfig->element_mode_init == IVAS_CPE_MDCT && "MCT is not supported for other stereo modes" );
772 :
773 740 : IF( b_nchan_change )
774 : {
775 246 : IF( hMCT->hBlockData[n] == NULL )
776 : {
777 117 : mem_init = 1;
778 117 : move16();
779 117 : IF( ( hMCT->hBlockData[n] = (MCT_BLOCK_DATA_HANDLE) malloc( sizeof( MCT_BLOCK_DATA ) ) ) == NULL )
780 : {
781 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MCT block data structure\n" ) );
782 : }
783 :
784 : /*Initialize all parameters to zero*/
785 117 : hMCT->hBlockData[n]->isActive = 0;
786 117 : move16();
787 117 : hMCT->hBlockData[n]->ch1 = 0;
788 117 : move16();
789 117 : hMCT->hBlockData[n]->ch2 = 0;
790 117 : move16();
791 :
792 : /*-----------------------------------------------------------------*
793 : * MDCT stereo initialization
794 : *-----------------------------------------------------------------*/
795 :
796 117 : IF( ( hMCT->hBlockData[n]->hStereoMdct = (STEREO_MDCT_ENC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_ENC_DATA ) ) ) == NULL )
797 : {
798 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
799 : }
800 : }
801 : }
802 :
803 : #ifdef DEBUGGING
804 : hMCT->hBlockData[n]->hStereoMdct->mdct_stereo_mode_cmdl = SMDCT_MS_DECISION;
805 : #endif
806 :
807 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 );
808 : }
809 :
810 2144 : FOR( ; n < MCT_MAX_BLOCKS; n++ )
811 : {
812 : /* deallocate no longer needed blocks */
813 1732 : IF( hMCT->hBlockData[n] != NULL )
814 : {
815 88 : IF( hMCT->hBlockData[n]->hStereoMdct != NULL )
816 : {
817 88 : free( hMCT->hBlockData[n]->hStereoMdct );
818 88 : hMCT->hBlockData[n]->hStereoMdct = NULL;
819 : }
820 :
821 88 : free( hMCT->hBlockData[n] );
822 88 : hMCT->hBlockData[n] = NULL;
823 : }
824 : }
825 :
826 : /*-----------------------------------------------------------------*
827 : * Initializations
828 : *-----------------------------------------------------------------*/
829 :
830 412 : set_mct_enc_params_fx( hMCT, ivas_total_brate, ivas_format, b_nchan_change );
831 :
832 412 : return error;
833 : }
834 :
835 :
836 : /*-------------------------------------------------------------------------
837 : * ivas_mct_enc_close()
838 : *
839 : * Close MCT
840 : *-------------------------------------------------------------------------*/
841 :
842 1345 : void ivas_mct_enc_close_fx(
843 : MCT_ENC_HANDLE *hMCT /* i/o: MCT encoder structure */
844 : )
845 : {
846 : Word16 n, maxBlocks;
847 :
848 1345 : test();
849 1345 : IF( hMCT == NULL || *hMCT == NULL )
850 : {
851 448 : return;
852 : }
853 :
854 897 : maxBlocks = shr( ( *hMCT )->nchan_out_woLFE, 1 );
855 :
856 2638 : FOR( n = 0; n < maxBlocks; n++ )
857 : {
858 1741 : IF( ( *hMCT )->hBlockData[n] != NULL )
859 : {
860 1741 : IF( ( *hMCT )->hBlockData[n]->hStereoMdct != NULL )
861 : {
862 1741 : free( ( *hMCT )->hBlockData[n]->hStereoMdct );
863 1741 : ( *hMCT )->hBlockData[n]->hStereoMdct = NULL;
864 : }
865 :
866 1741 : free( ( *hMCT )->hBlockData[n] );
867 1741 : ( *hMCT )->hBlockData[n] = NULL;
868 : }
869 : }
870 :
871 897 : free( ( *hMCT ) );
872 897 : ( *hMCT ) = NULL;
873 :
874 897 : return;
875 : }
876 :
877 :
878 : /*-------------------------------------------------------------------------
879 : * ivas_mc_enc_config()
880 : *
881 : * - select MC format mode
882 : * - write MC LS setup
883 : * - reconfigure the MC format encoder
884 : *-------------------------------------------------------------------------*/
885 :
886 47850 : ivas_error ivas_mc_enc_config_fx(
887 : Encoder_Struct *st_ivas /* i/o: IVAS encoder structure */
888 : )
889 : {
890 : Encoder_State *st0; /* used for bitstream handling */
891 : MC_MODE last_mc_mode;
892 : ivas_error error;
893 :
894 47850 : error = IVAS_ERR_OK;
895 47850 : move32();
896 :
897 47850 : last_mc_mode = st_ivas->mc_mode;
898 47850 : move32();
899 :
900 : /* select MC format mode */
901 47850 : st_ivas->mc_mode = ivas_mc_mode_select_fx( st_ivas->hEncoderConfig->mc_input_setup, st_ivas->hEncoderConfig->ivas_total_brate );
902 47850 : move32();
903 :
904 : /* write MC LS setup */
905 47850 : IF( st_ivas->nSCE > 0 )
906 : {
907 11065 : st0 = st_ivas->hSCE[0]->hCoreCoder[0];
908 : }
909 : ELSE
910 : {
911 36785 : st0 = st_ivas->hCPE[0]->hCoreCoder[0];
912 : }
913 :
914 47850 : push_indice( st0->hBstr, IND_SMODE, st_ivas->hEncoderConfig->mc_input_setup, MC_LS_SETUP_BITS );
915 :
916 : /* MC format switching */
917 47850 : test();
918 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 ) )
919 : {
920 990 : IF( NE_32( ( error = ivas_mc_enc_reconfig_fx( st_ivas, last_mc_mode ) ), IVAS_ERR_OK ) )
921 : {
922 0 : return error;
923 : }
924 : }
925 :
926 47850 : return error;
927 : }
928 :
929 :
930 : /*-------------------------------------------------------------------------
931 : * ivas_mc_enc_reconfig()
932 : *
933 : * Reconfigure the MC format encoder
934 : *-------------------------------------------------------------------------*/
935 :
936 990 : static ivas_error ivas_mc_enc_reconfig_fx(
937 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
938 : const Word16 last_mc_mode /* i : last frame mc mode */
939 : )
940 : {
941 : Word16 nchan_transport_old, nSCE_old, nCPE_old;
942 : ivas_error error;
943 : Word32 new_brate_SCE, new_brate_CPE;
944 :
945 990 : error = IVAS_ERR_OK;
946 990 : move32();
947 :
948 990 : nchan_transport_old = st_ivas->nchan_transport;
949 990 : move16();
950 990 : nSCE_old = st_ivas->nSCE;
951 990 : move16();
952 990 : nCPE_old = st_ivas->nCPE;
953 990 : move16();
954 :
955 : /*-----------------------------------------------------------------*
956 : * Reconfigure MC modules
957 : *-----------------------------------------------------------------*/
958 :
959 990 : IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) )
960 : {
961 428 : st_ivas->nSCE = 0;
962 428 : move16();
963 428 : st_ivas->nCPE = shr( st_ivas->hEncoderConfig->nchan_inp, 1 );
964 428 : move16();
965 :
966 428 : st_ivas->nchan_transport = ivas_mc_ls_setup_get_num_channels_fx( st_ivas->hEncoderConfig->mc_input_setup );
967 428 : move16();
968 :
969 428 : IF( NE_32( last_mc_mode, MC_MODE_MCT ) )
970 : {
971 286 : IF( st_ivas->hLFE != NULL )
972 : {
973 : /* LFE handle */
974 3 : ivas_lfe_enc_close_fx( &( st_ivas->hLFE ) );
975 : }
976 :
977 : /* create LFE handle */
978 286 : IF( NE_32( ( error = ivas_create_lfe_enc_fx( &st_ivas->hLFE, st_ivas->hEncoderConfig->input_Fs ) ), IVAS_ERR_OK ) )
979 : {
980 0 : return error;
981 : }
982 :
983 : /*De-allocate handles for other MC modes*/
984 286 : ivas_param_mc_enc_close_fx( &( st_ivas->hParamMC ), st_ivas->hEncoderConfig->input_Fs );
985 :
986 286 : ivas_mc_paramupmix_enc_close_fx( &( st_ivas->hMCParamUpmix ), st_ivas->hEncoderConfig->input_Fs );
987 :
988 : /* De-allocate McMasa-related handles */
989 286 : ivas_mcmasa_enc_close_fx( &( st_ivas->hMcMasa ), st_ivas->hEncoderConfig->input_Fs );
990 :
991 286 : ivas_masa_enc_close_fx( &( st_ivas->hMasa ) );
992 :
993 286 : ivas_qmetadata_close_fx( &st_ivas->hQMetaData );
994 : }
995 : }
996 562 : ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
997 : {
998 7 : st_ivas->nSCE = 0;
999 7 : move16();
1000 7 : st_ivas->nCPE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS >> 1;
1001 7 : move16();
1002 7 : st_ivas->nchan_transport = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
1003 7 : move16();
1004 :
1005 7 : IF( NE_32( last_mc_mode, MC_MODE_PARAMUPMIX ) )
1006 : {
1007 7 : IF( st_ivas->hLFE != NULL )
1008 : {
1009 : /* LFE handle */
1010 0 : ivas_lfe_enc_close_fx( &( st_ivas->hLFE ) );
1011 : }
1012 :
1013 : /* create LFE handle */
1014 7 : IF( NE_32( ( error = ivas_create_lfe_enc_fx( &st_ivas->hLFE, st_ivas->hEncoderConfig->input_Fs ) ), IVAS_ERR_OK ) )
1015 : {
1016 0 : return error;
1017 : }
1018 : }
1019 :
1020 7 : IF( NE_32( last_mc_mode, MC_MODE_PARAMUPMIX ) )
1021 : {
1022 7 : IF( NE_32( ( error = ivas_mc_paramupmix_enc_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
1023 : {
1024 0 : return error;
1025 : }
1026 : }
1027 :
1028 : /*De-allocate handles for other MC modes*/
1029 7 : ivas_param_mc_enc_close_fx( &st_ivas->hParamMC, st_ivas->hEncoderConfig->input_Fs );
1030 :
1031 : /* De-allocate McMasa-related handles */
1032 7 : ivas_mcmasa_enc_close_fx( &( st_ivas->hMcMasa ), st_ivas->hEncoderConfig->input_Fs );
1033 7 : ivas_masa_enc_close_fx( &( st_ivas->hMasa ) );
1034 :
1035 7 : ivas_qmetadata_close_fx( &st_ivas->hQMetaData );
1036 : }
1037 555 : ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) )
1038 : {
1039 308 : IF( NE_32( last_mc_mode, MC_MODE_PARAMMC ) )
1040 : {
1041 248 : IF( NE_32( ( error = ivas_param_mc_enc_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
1042 : {
1043 0 : return error;
1044 : }
1045 : }
1046 : ELSE
1047 : {
1048 60 : IF( NE_32( ( error = ivas_param_mc_enc_reconfig_fx( st_ivas ) ), IVAS_ERR_OK ) )
1049 : {
1050 0 : return error;
1051 : }
1052 : }
1053 :
1054 : /* De-allocate McMasa-related handles */
1055 308 : ivas_mcmasa_enc_close_fx( &( st_ivas->hMcMasa ), st_ivas->hEncoderConfig->input_Fs );
1056 :
1057 308 : IF( st_ivas->hMasa != NULL )
1058 : {
1059 89 : ivas_masa_enc_close_fx( &( st_ivas->hMasa ) );
1060 89 : st_ivas->hMasa = NULL;
1061 : }
1062 :
1063 308 : ivas_mc_paramupmix_enc_close_fx( &( st_ivas->hMCParamUpmix ), st_ivas->hEncoderConfig->input_Fs );
1064 :
1065 308 : test();
1066 308 : IF( EQ_32( last_mc_mode, MC_MODE_PARAMUPMIX ) && st_ivas->hLFE != NULL )
1067 : {
1068 : /* LFE handle */
1069 0 : ivas_lfe_enc_close_fx( &( st_ivas->hLFE ) );
1070 : }
1071 :
1072 308 : ivas_qmetadata_close_fx( &st_ivas->hQMetaData );
1073 :
1074 : /* De-allocate MCT handle if last mode was MCT */
1075 308 : test();
1076 308 : IF( EQ_32( last_mc_mode, MC_MODE_MCT ) && LE_16( st_ivas->nchan_transport, CPE_CHANNELS ) )
1077 : {
1078 154 : ivas_mct_enc_close_fx( &( st_ivas->hMCT ) );
1079 : }
1080 :
1081 308 : test();
1082 308 : IF( EQ_32( last_mc_mode, MC_MODE_MCT ) && st_ivas->hLFE != NULL )
1083 : {
1084 : /* LFE handle */
1085 159 : ivas_lfe_enc_close_fx( &( st_ivas->hLFE ) );
1086 : }
1087 : }
1088 247 : ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
1089 : {
1090 247 : IF( NE_32( last_mc_mode, MC_MODE_MCMASA ) )
1091 : {
1092 201 : ivas_mcmasa_setNumTransportChannels_fx( &( st_ivas->nchan_transport ), &( st_ivas->hEncoderConfig->element_mode_init ), st_ivas->hEncoderConfig->ivas_total_brate );
1093 :
1094 201 : IF( NE_32( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ), IVAS_ERR_OK ) )
1095 : {
1096 0 : return error;
1097 : }
1098 :
1099 201 : IF( NE_32( ( error = ivas_masa_enc_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
1100 : {
1101 0 : return error;
1102 : }
1103 201 : IF( NE_32( ( error = ivas_mcmasa_enc_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
1104 : {
1105 0 : return error;
1106 : }
1107 : }
1108 : ELSE
1109 : {
1110 : /* reconfigure McMASA instance */
1111 46 : IF( NE_32( ( error = ivas_mcmasa_enc_reconfig_fx( st_ivas ) ), IVAS_ERR_OK ) )
1112 : {
1113 0 : return error;
1114 : }
1115 : }
1116 :
1117 247 : ivas_param_mc_enc_close_fx( &( st_ivas->hParamMC ), st_ivas->hEncoderConfig->input_Fs );
1118 :
1119 247 : ivas_mc_paramupmix_enc_close_fx( &( st_ivas->hMCParamUpmix ), st_ivas->hEncoderConfig->input_Fs );
1120 :
1121 247 : test();
1122 247 : IF( EQ_32( last_mc_mode, MC_MODE_PARAMUPMIX ) && st_ivas->hLFE != NULL )
1123 : {
1124 : /* LFE handle */
1125 0 : ivas_lfe_enc_close_fx( &( st_ivas->hLFE ) );
1126 : }
1127 :
1128 247 : IF( EQ_32( last_mc_mode, MC_MODE_MCT ) )
1129 : {
1130 : /* LFE handle */
1131 116 : ivas_lfe_enc_close_fx( &( st_ivas->hLFE ) );
1132 :
1133 116 : ivas_mct_enc_close_fx( &( st_ivas->hMCT ) );
1134 : }
1135 : }
1136 :
1137 990 : IF( NE_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
1138 : {
1139 743 : IF( EQ_16( st_ivas->nchan_transport, 1 ) )
1140 : {
1141 0 : st_ivas->hEncoderConfig->element_mode_init = IVAS_SCE;
1142 0 : move16();
1143 : }
1144 : ELSE
1145 : {
1146 743 : st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
1147 743 : move16();
1148 : }
1149 : }
1150 :
1151 : /*-----------------------------------------------------------------*
1152 : * Reconfigure core coder
1153 : *-----------------------------------------------------------------*/
1154 :
1155 : /* special case: MCT->ParamMC with more than 2 TC, CPE 1 stays, but has the wrong mct_chan_mode in channel 1
1156 : and might have IGF and TranDet static memory not allocated and the bit stream index list not set,
1157 : set correct mct_chan_mode and init missing static mem
1158 : do it here since it is _very_ MC specific */
1159 990 : test();
1160 990 : test();
1161 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 ) )
1162 : {
1163 5 : Encoder_State *st = st_ivas->hCPE[1]->hCoreCoder[1];
1164 :
1165 5 : IF( EQ_16( st_ivas->nchan_transport, 3 ) )
1166 : {
1167 5 : st->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
1168 5 : move32();
1169 : }
1170 : ELSE
1171 : {
1172 0 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
1173 0 : move32();
1174 : }
1175 :
1176 5 : IF( st->hTranDet == NULL )
1177 : {
1178 0 : IF( ( st->hTranDet = (TRAN_DET_HANDLE) malloc( sizeof( TRAN_DET_DATA ) ) ) == NULL )
1179 : {
1180 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Transient Detection\n" ) );
1181 : }
1182 :
1183 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 );
1184 : }
1185 :
1186 5 : IF( st->hIGFEnc == NULL )
1187 : {
1188 0 : IF( ( st->hIGFEnc = (IGF_ENC_INSTANCE_HANDLE) malloc( sizeof( IGF_ENC_INSTANCE ) ) ) == NULL )
1189 : {
1190 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for hIGFEnc\n" ) );
1191 : }
1192 : }
1193 5 : st->igf = getIgfPresent_fx( st->element_mode, st->total_brate, st->bwidth, st->rf_mode );
1194 5 : move16();
1195 : /* set last core to TCX20 */
1196 5 : st->last_core = TCX_20_CORE;
1197 5 : move16();
1198 : }
1199 :
1200 990 : IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
1201 : {
1202 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 );
1203 : }
1204 743 : ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) )
1205 : {
1206 428 : new_brate_SCE = 0;
1207 428 : move32();
1208 : Word32 res_dec, res_frac;
1209 428 : iDiv_and_mod_32( st_ivas->hEncoderConfig->ivas_total_brate, sub( st_ivas->nchan_transport, 1 ), &res_dec, &res_frac, 0 );
1210 428 : new_brate_CPE = imult3216( res_dec, CPE_CHANNELS );
1211 : }
1212 315 : ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
1213 : {
1214 7 : new_brate_SCE = 0;
1215 7 : move32();
1216 : Word32 res_dec, res_frac;
1217 7 : iDiv_and_mod_32( st_ivas->hEncoderConfig->ivas_total_brate, sub( st_ivas->nchan_transport, 1 ), &res_dec, &res_frac, 0 );
1218 7 : new_brate_CPE = imult3216( res_dec, CPE_CHANNELS );
1219 : }
1220 : ELSE
1221 : {
1222 308 : new_brate_SCE = 0; /*st_ivas->hEncoderConfig->ivas_total_brate / st_ivas->nchan_transport;*/
1223 308 : move32();
1224 : Word32 res_dec, res_frac;
1225 308 : iDiv_and_mod_32( st_ivas->hEncoderConfig->ivas_total_brate, st_ivas->nchan_transport, &res_dec, &res_frac, 0 );
1226 308 : new_brate_CPE = imult3216( res_dec, CPE_CHANNELS );
1227 : }
1228 :
1229 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 ) )
1230 : {
1231 0 : return error;
1232 : }
1233 :
1234 990 : return error;
1235 : }
|