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 "rom_com.h"
40 : #include "prot_fx.h"
41 : #include "ivas_prot_rend_fx.h"
42 : #include "ivas_rom_com.h"
43 : #include "wmc_auto.h"
44 : #include "ivas_prot_fx.h"
45 :
46 :
47 : /*-----------------------------------------------------------------------*
48 : * Local function prototypes
49 : *-----------------------------------------------------------------------*/
50 :
51 : static ivas_error ivas_mc_dec_reconfig_fx( Decoder_Struct *st_ivas, UWord16 *nSamplesRendered, Word16 *data );
52 :
53 :
54 : /*--------------------------------------------------------------------------*
55 : * ivas_mct_dec()
56 : *
57 : * Multi-channel Coding Tool (MCT) decoding routine
58 : *--------------------------------------------------------------------------*/
59 :
60 93116 : ivas_error ivas_mct_dec_fx(
61 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
62 : Word32 *output_fx[], /* o : output synthesis signal Q11*/
63 : const Word16 output_frame, /* i : output frame length per channel */
64 : const Word16 nb_bits_metadata /* i : number of metadata bits */
65 : )
66 : {
67 : Word16 ch, nCPE, cpe_id;
68 : MCT_DEC_HANDLE hMCT;
69 : CPE_DEC_HANDLE hCPE;
70 : Word16 param[MCT_MAX_BLOCKS][CPE_CHANNELS][DEC_NPRM_DIV * NB_DIV];
71 : Word16 param_lpc[MCT_MAX_BLOCKS][CPE_CHANNELS][NPRM_LPC_NEW];
72 : Word16 p_param[MCT_MAX_BLOCKS][CPE_CHANNELS][NB_DIV];
73 : Word16 nTnsBitsTCX10[MCT_MAX_BLOCKS][CPE_CHANNELS][NB_DIV];
74 : Word16 fUseTns[MCT_MAX_BLOCKS][CPE_CHANNELS][NB_DIV];
75 : STnsData tnsData[MCT_MAX_BLOCKS][CPE_CHANNELS][NB_DIV];
76 : Word16 Aq_fx[MCT_MAX_BLOCKS][CPE_CHANNELS][( NB_SUBFR16k + 1 ) * ( M + 1 )]; // Q12
77 : Word32 output_lfe_ch_fx[L_FRAME48k];
78 93116 : Word16 q_output = 11;
79 : Word16 n, k, i;
80 :
81 : Word32 *x_fx[CPE_CHANNELS][NB_DIV]; //(Q(31 - x_e)
82 : Word16 x_e[MAX_CICP_CHANNELS][NB_DIV];
83 : Word16 x_len[CPE_CHANNELS][NB_DIV];
84 93116 : set16_fx( x_len[0], 0, NB_DIV );
85 93116 : set16_fx( x_len[1], 0, NB_DIV );
86 : Decoder_State **sts;
87 : Word32 *p_output_orig_fx[2];
88 : Word32 synth_32_fx[CPE_CHANNELS][L_FRAME_PLUS];
89 : Word16 synth_fx[CPE_CHANNELS][L_FRAME_PLUS]; //(Q_synth)
90 : Word32 ivas_total_brate;
91 : ivas_error error;
92 93116 : Word16 e_sig[CPE_CHANNELS] = { 15, 15 };
93 93116 : move16();
94 93116 : move16();
95 :
96 93116 : push_wmops( "ivas_mct_dec" );
97 :
98 93116 : error = IVAS_ERR_OK;
99 93116 : move32();
100 93116 : nCPE = st_ivas->nCPE;
101 93116 : move16();
102 93116 : hMCT = st_ivas->hMCT;
103 :
104 93116 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
105 93116 : move32();
106 :
107 93116 : test();
108 93116 : test();
109 93116 : test();
110 93116 : test();
111 93116 : IF( EQ_16( st_ivas->ivas_format, MC_FORMAT ) && ( EQ_16( st_ivas->mc_mode, MC_MODE_MCT ) || EQ_16( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) ) )
112 : {
113 : /* save LFE channel */
114 25087 : Copy32( output_fx[LFE_CHANNEL], output_lfe_ch_fx, output_frame ); // Q11
115 : }
116 :
117 93116 : IF( EQ_16( st_ivas->ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_MCT ) && ( st_ivas->bfi == 0 ) )
118 : {
119 : /* get the number of channels from the signalled MC LS setup */
120 24181 : n = ivas_mc_ls_setup_get_num_channels_fx( ivas_mc_map_output_config_to_mc_ls_setup_fx( st_ivas->transport_config ) );
121 :
122 24181 : IF( NE_16( n, st_ivas->nchan_transport ) )
123 : {
124 : /* IVAS_fmToDo: more work needed for switching the number of transport channels */
125 0 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: wrong number of transport channels signalled in MC format!" );
126 : }
127 : }
128 :
129 334254 : FOR( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
130 : {
131 : /*initialize param_lpc buffer*/
132 723414 : FOR( n = 0; n < CPE_CHANNELS; n++ )
133 : {
134 482276 : set16_fx( param_lpc[cpe_id][n], 0, NPRM_LPC_NEW );
135 : }
136 :
137 241138 : set32_fx( &output_fx[0][0], 0, L_FRAME48k );
138 241138 : set32_fx( &output_fx[1][0], 0, L_FRAME48k );
139 241138 : IF( NE_32( ( error = ivas_cpe_dec_fx( st_ivas, cpe_id, &output_fx[0], output_frame, 0 ) ), IVAS_ERR_OK ) )
140 : {
141 0 : return error;
142 : }
143 :
144 241138 : if ( cpe_id == 0 )
145 : {
146 93116 : st_ivas->hCPE[0]->hCoreCoder[0]->total_brate = ivas_total_brate; /* set high enough to read the whole side-info; total_brate is rewritten later in ivas_mdct_core_invQ() */
147 93116 : move32();
148 : }
149 :
150 241138 : IF( st_ivas->bfi == 0 )
151 : {
152 237593 : ivas_mdct_dec_side_bits_frame_channel_fx( st_ivas->hCPE[cpe_id], param_lpc[cpe_id], p_param[cpe_id], st_ivas->hCPE[0]->hCoreCoder[0], nTnsBitsTCX10[cpe_id], param[cpe_id], 1,
153 237593 : (Word16) GT_16( i_mult( add( cpe_id, 1 ), CPE_CHANNELS ), hMCT->nchan_out_woLFE ) );
154 :
155 237593 : st_ivas->BER_detect = s_or( st_ivas->BER_detect, st_ivas->hCPE[cpe_id]->hCoreCoder[0]->BER_detect );
156 237593 : test();
157 237593 : st_ivas->BER_detect = s_or( st_ivas->BER_detect, st_ivas->hCPE[cpe_id]->hCoreCoder[1]->BER_detect );
158 237593 : test();
159 : }
160 : }
161 :
162 : /* MCT side bits decoder */
163 93116 : ivas_mct_side_bits_fx( hMCT, st_ivas->hCPE, nCPE, st_ivas->hCPE[0]->hCoreCoder[0], st_ivas->bfi, st_ivas->hCPE[0]->hCoreCoder[0]->bit_stream, ivas_total_brate, nb_bits_metadata );
164 :
165 : /* in case of switching from an SID frame (with ACELP core) to MCT, buffer of L_FRAME_PLUS samples is needed -> use synth[] as a temporary buffer */
166 93116 : IF( st_ivas->hCPE[0]->hCoreCoder[0]->last_core == ACELP_CORE )
167 : {
168 9 : FOR( n = 0; n < CPE_CHANNELS; n++ )
169 : {
170 6 : p_output_orig_fx[n] = output_fx[n];
171 6 : output_fx[n] = synth_32_fx[n];
172 : }
173 : }
174 :
175 334254 : FOR( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
176 : {
177 241138 : st_ivas->hCPE[cpe_id]->hCoreCoder[0]->BER_detect = s_or( st_ivas->hCPE[cpe_id]->hCoreCoder[0]->BER_detect, st_ivas->BER_detect );
178 241138 : move16();
179 241138 : st_ivas->hCPE[cpe_id]->hCoreCoder[1]->BER_detect = s_or( st_ivas->hCPE[cpe_id]->hCoreCoder[1]->BER_detect, st_ivas->BER_detect );
180 241138 : move16();
181 :
182 723414 : FOR( n = 0; n < CPE_CHANNELS; n++ )
183 : {
184 482276 : x_fx[n][0] = output_fx[n + ( cpe_id * CPE_CHANNELS )]; // Q11
185 482276 : x_e[n][0] = 20;
186 482276 : move16();
187 482276 : move16();
188 482276 : x_fx[n][1] = output_fx[n + ( cpe_id * CPE_CHANNELS )] + ( L_FRAME48k / 2 ); // Q11
189 482276 : x_e[n][1] = 20;
190 482276 : move16();
191 482276 : move16();
192 :
193 482276 : set32_fx( x_fx[n][0], 0, L_FRAME48k / 2 );
194 482276 : set32_fx( x_fx[n][1], 0, L_FRAME48k / 2 );
195 : }
196 :
197 241138 : ivas_mdct_core_invQ_fx( st_ivas->hCPE[cpe_id], nTnsBitsTCX10[cpe_id], p_param[cpe_id], param_lpc[cpe_id], param[cpe_id],
198 241138 : fUseTns[cpe_id], tnsData[cpe_id], x_fx, x_e, x_fx, x_e, x_len, Aq_fx[cpe_id], NULL, 1 );
199 :
200 723414 : FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
201 : {
202 : Word16 subFrames;
203 482276 : IF( EQ_16( st_ivas->hCPE[cpe_id]->hCoreCoder[ch]->core, TCX_10_CORE ) )
204 : {
205 8837 : subFrames = NB_DIV;
206 : }
207 : ELSE
208 : {
209 473439 : subFrames = 1;
210 : }
211 482276 : move16();
212 973389 : FOR( k = 0; k < subFrames; ++k )
213 : {
214 491113 : Scale_sig32( x_fx[ch][k], shr( L_FRAME48k, sub( subFrames, 1 ) ), sub( x_e[ch][k], 20 ) ); // Scaling back to Q11
215 : }
216 : }
217 :
218 241138 : st_ivas->BER_detect = s_or( st_ivas->BER_detect, st_ivas->hCPE[cpe_id]->hCoreCoder[0]->BER_detect );
219 241138 : move16();
220 241138 : st_ivas->BER_detect = s_or( st_ivas->BER_detect, st_ivas->hCPE[cpe_id]->hCoreCoder[1]->BER_detect );
221 241138 : move16();
222 : }
223 :
224 :
225 : /* MCT core decoder */
226 : Word16 q_x[MAX_TRANSPORT_CHANNELS];
227 93116 : set16_fx( q_x, Q12, MAX_TRANSPORT_CHANNELS );
228 : // Scaling output buffer to q_x
229 517605 : FOR( i = 0; i < hMCT->nchan_out_woLFE; ++i )
230 : {
231 424489 : Scale_sig32( output_fx[i], L_FRAME48k, sub( q_x[i], Q11 ) ); // Q11 -> Q12
232 : }
233 93116 : ivas_mct_core_dec( hMCT, st_ivas->hCPE, nCPE, output_fx, q_x );
234 : // Scaling output buffer back to Q11
235 517605 : FOR( i = 0; i < hMCT->nchan_out_woLFE; ++i )
236 : {
237 424489 : Scale_sig32( output_fx[i], L_FRAME48k, sub( Q11, q_x[i] ) ); // Q12 -> Q11
238 : }
239 :
240 :
241 : /* for sba to stereo output disable any further processing for TCs > 2 as it is not needed*/
242 93116 : test();
243 93116 : IF( ( st_ivas->sba_dirac_stereo_flag != 0 ) && NE_16( st_ivas->ivas_format, SBA_ISM_FORMAT ) )
244 : {
245 4940 : FOR( cpe_id = 1; cpe_id < nCPE; cpe_id++ )
246 : {
247 7410 : FOR( n = 0; n < CPE_CHANNELS; n++ )
248 : {
249 4940 : st_ivas->hCPE[cpe_id]->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
250 4940 : move16();
251 : }
252 : }
253 : }
254 :
255 : Word32 Aq_fx_32[6][2][102];
256 : /* MCT reconstruction and CoreCoder updates */
257 334254 : FOR( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
258 : {
259 241138 : hCPE = st_ivas->hCPE[cpe_id];
260 :
261 723414 : FOR( n = 0; n < CPE_CHANNELS; n++ )
262 : {
263 482276 : Copy_Scale_sig_16_32_DEPREC( Aq_fx[cpe_id][n], Aq_fx_32[cpe_id][n], 102, Q16 - Q12 ); // Q16
264 482276 : x_fx[n][0] = output_fx[n + ( cpe_id * CPE_CHANNELS )]; // Q11
265 482276 : x_fx[n][1] = output_fx[n + ( cpe_id * CPE_CHANNELS )] + ( L_FRAME48k / 2 ); // Q11
266 482276 : x_e[n][0] = 20;
267 482276 : move16();
268 482276 : move16();
269 482276 : move16();
270 482276 : x_e[n][1] = 20;
271 482276 : move16();
272 : }
273 :
274 241138 : ivas_mdct_core_tns_ns_fx( hCPE, fUseTns[cpe_id], tnsData[cpe_id], x_fx, Aq_fx_32[cpe_id], 1, x_e );
275 :
276 723414 : FOR( Word16 ind = 0; ind < 2; ind++ )
277 : {
278 : Word16 nSubFrames;
279 482276 : IF( EQ_16( hCPE->hCoreCoder[ind]->core, TCX_20_CORE ) )
280 : {
281 470907 : nSubFrames = 1;
282 : }
283 : ELSE
284 : {
285 11369 : nSubFrames = NB_DIV;
286 : }
287 482276 : move16();
288 482276 : Scale_sig32( x_fx[ind][0], shr( L_FRAME48k, sub( nSubFrames, 1 ) ), sub( x_e[ind][0], 20 ) ); // Q11
289 482276 : IF( EQ_16( nSubFrames, 2 ) )
290 : {
291 11369 : Scale_sig32( x_fx[ind][1], shr( L_FRAME48k, 1 ), sub( x_e[ind][1], 20 ) ); // Q11
292 : }
293 : }
294 : }
295 :
296 :
297 93116 : test();
298 93116 : test();
299 93116 : IF( EQ_16( st_ivas->renderer_type, RENDERER_MC ) )
300 : {
301 : /* Equalization in MDCT Domain */
302 6504 : ivas_ls_setup_conversion_process_mdct_fx( st_ivas, output_fx );
303 : }
304 86612 : ELSE IF( EQ_16( st_ivas->renderer_type, RENDERER_MC_PARAMMC ) && ( EQ_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_MONO ) || EQ_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_STEREO ) ) )
305 : {
306 : Word16 x_all_e[MAX_CICP_CHANNELS][NB_DIV];
307 340 : FOR( i = 0; i < MAX_CICP_CHANNELS; i++ )
308 : {
309 320 : set16_fx( x_all_e[i], 0, NB_DIV );
310 : }
311 : Word32 *x_all_fx[MAX_CICP_CHANNELS][NB_DIV]; //(Q(31 - x_all_e)
312 60 : FOR( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
313 : {
314 120 : FOR( n = 0; n < CPE_CHANNELS; n++ )
315 : {
316 80 : x_all_fx[n + ( cpe_id * CPE_CHANNELS )][0] = output_fx[n + ( cpe_id * CPE_CHANNELS )]; // Q11
317 80 : x_all_fx[n + ( cpe_id * CPE_CHANNELS )][1] = output_fx[n + ( cpe_id * CPE_CHANNELS )] + ( L_FRAME48k / 2 ); // Q11
318 80 : x_all_e[n + ( cpe_id * CPE_CHANNELS )][1] = 20;
319 80 : move16();
320 80 : x_all_e[n + ( cpe_id * CPE_CHANNELS )][0] = 20;
321 80 : move16();
322 : }
323 : }
324 :
325 20 : ivas_ls_setup_conversion_process_mdct_param_mc_fx( st_ivas, x_all_fx, x_all_e );
326 :
327 60 : FOR( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
328 : {
329 120 : FOR( n = 0; n < CPE_CHANNELS; n++ )
330 : {
331 80 : Scale_sig32( x_all_fx[n + ( cpe_id * CPE_CHANNELS )][0], ( L_FRAME48k / 2 ), sub( x_all_e[n + ( cpe_id * CPE_CHANNELS )][0], 20 ) ); // Q11
332 80 : Scale_sig32( x_all_fx[n + ( cpe_id * CPE_CHANNELS )][1], ( L_FRAME48k / 2 ), sub( x_all_e[n + ( cpe_id * CPE_CHANNELS )][1], 20 ) ); // Q11
333 : }
334 : }
335 : }
336 :
337 334254 : FOR( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
338 : {
339 241138 : hCPE = st_ivas->hCPE[cpe_id];
340 241138 : sts = hCPE->hCoreCoder;
341 :
342 723414 : FOR( n = 0; n < CPE_CHANNELS; n++ )
343 : {
344 482276 : x_fx[n][0] = output_fx[n + ( cpe_id * CPE_CHANNELS )]; // Q11
345 482276 : x_fx[n][1] = output_fx[n + ( cpe_id * CPE_CHANNELS )] + ( L_FRAME48k / 2 ); // Q11
346 : }
347 241138 : Copy_Scale_sig_16_32_no_sat( hCPE->hCoreCoder[0]->old_Aq_12_8_fx, hCPE->hCoreCoder[0]->old_Aq_12_8_fx_32, add( M, 1 ), sub( 28, sub( 15, norm_s( sub( hCPE->hCoreCoder[0]->old_Aq_12_8_fx[0], 1 ) ) ) ) );
348 241138 : Copy_Scale_sig_16_32_no_sat( hCPE->hCoreCoder[1]->old_Aq_12_8_fx, hCPE->hCoreCoder[1]->old_Aq_12_8_fx_32, add( M, 1 ), sub( 28, sub( 15, norm_s( sub( hCPE->hCoreCoder[1]->old_Aq_12_8_fx[0], 1 ) ) ) ) );
349 241138 : ivas_mdct_core_reconstruct_fx( hCPE, x_fx, synth_fx, fUseTns[cpe_id], 1, q_output, e_sig );
350 :
351 : Word16 hdrm, sh;
352 241138 : hdrm = getScaleFactor16( synth_fx[0], hCPE->hCoreCoder[0]->hTcxDec->L_frameTCX );
353 241138 : IF( hdrm != 0 )
354 : {
355 228901 : sh = s_min( sub( e_sig[0], 16 ), hdrm );
356 228901 : Scale_sig( synth_fx[0], hCPE->hCoreCoder[0]->hTcxDec->L_frameTCX, sh );
357 228901 : e_sig[0] = sub( e_sig[0], sh );
358 228901 : move16();
359 : }
360 241138 : hdrm = getScaleFactor16( synth_fx[1], hCPE->hCoreCoder[0]->hTcxDec->L_frameTCX );
361 241138 : IF( hdrm != 0 )
362 : {
363 178287 : sh = s_min( sub( e_sig[1], 16 ), hdrm );
364 178287 : Scale_sig( synth_fx[1], hCPE->hCoreCoder[1]->hTcxDec->L_frameTCX, sh );
365 178287 : e_sig[1] = sub( e_sig[1], sh );
366 178287 : move16();
367 : }
368 :
369 723414 : FOR( n = 0; n < CPE_CHANNELS; n++ )
370 : {
371 482276 : IF( hCPE->hCoreCoder[n]->hBPF )
372 : {
373 0 : hCPE->hCoreCoder[n]->hBPF->pst_mem_deemp_err_fx = extract_l( L_shr( hCPE->hCoreCoder[n]->mem_error, sub( Q16, sub( hCPE->hCoreCoder[n]->Q_syn2, 1 ) ) ) );
374 0 : Scale_sig( hCPE->hCoreCoder[n]->hBPF->pst_old_syn_fx, NBPSF_PIT_MAX, sub( hCPE->hCoreCoder[n]->Q_syn2, 1 ) );
375 0 : move16();
376 : }
377 : }
378 :
379 : /* set pointers back */
380 241138 : test();
381 241138 : IF( cpe_id == 0 && st_ivas->hCPE[0]->hCoreCoder[0]->last_core == ACELP_CORE )
382 : {
383 9 : FOR( n = 0; n < CPE_CHANNELS; n++ )
384 : {
385 6 : output_fx[n] = p_output_orig_fx[n];
386 : }
387 : }
388 :
389 :
390 : /*----------------------------------------------------------------*
391 : * CoreCoder Post-processing and updates
392 : *----------------------------------------------------------------*/
393 : Word32 synth_fx_32[CPE_CHANNELS][L_FRAME48k];
394 :
395 723414 : FOR( n = 0; n < CPE_CHANNELS; n++ )
396 : {
397 482276 : test();
398 482276 : test();
399 482276 : IF( ( st_ivas->sba_dirac_stereo_flag != 0 ) && ( NE_16( st_ivas->ivas_format, SBA_ISM_FORMAT ) || GE_16( cpe_id, sub( nCPE, 2 ) ) ) )
400 : {
401 19764 : Copy_Scale_sig_16_32_no_sat( synth_fx[n], synth_fx_32[n], output_frame, sub( Q11, ( sub( 15, e_sig[n] ) ) ) ); // Q11
402 19764 : Copy_Scale_sig_16_32_no_sat( hCPE->hCoreCoder[n]->hHQ_core->old_out_fx, hCPE->hCoreCoder[n]->hHQ_core->old_out_fx32, output_frame, sub( Q11, hCPE->hCoreCoder[n]->hHQ_core->Q_old_wtda ) );
403 19764 : ivas_post_proc_fx( NULL, hCPE, n, synth_fx_32[n], NULL, output_frame, 1, Q11 );
404 19764 : Copy_Scale_sig_32_16( synth_fx_32[n], synth_fx[n], output_frame, sub( sub( 15, e_sig[n] ), Q11 ) ); // Q0
405 : }
406 :
407 : /* Postprocessing for ACELP/MDCT core switching and synchronization */
408 : Word16 output_mem_fx[L_FRAME48k];
409 482276 : IF( hCPE->output_mem_fx[1] != NULL )
410 : {
411 24604 : Copy_Scale_sig_32_16( hCPE->output_mem_fx[1], output_mem_fx, NS2SA_FX2( sts[n]->output_Fs, 3125000 ), -Q11 );
412 : }
413 : ELSE
414 : {
415 457672 : set16_fx( output_mem_fx, 0, NS2SA_FX2( sts[n]->output_Fs, 3125000 ) );
416 : }
417 :
418 482276 : Word16 Q_synth = sub( 15, e_sig[n] );
419 482276 : move16();
420 :
421 : Word16 dirac_stereo_flag;
422 482276 : test();
423 482276 : IF( NE_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) || GE_16( cpe_id, sub( nCPE, 2 ) ) )
424 : {
425 436096 : dirac_stereo_flag = st_ivas->sba_dirac_stereo_flag;
426 : }
427 : ELSE
428 : {
429 46180 : dirac_stereo_flag = 0;
430 : }
431 482276 : IF( NE_32( ( error = core_switching_post_dec_ivas_fx( sts[n], synth_fx[n], output_fx[( cpe_id * CPE_CHANNELS ) + n], output_mem_fx, 0, output_frame, 0 /*core_switching_flag*/, dirac_stereo_flag, -1, hCPE->last_element_mode, &Q_synth ) ), IVAS_ERR_OK ) )
432 : {
433 0 : return error;
434 : }
435 482276 : Copy_Scale_sig_16_32_no_sat( synth_fx[n], output_fx[( cpe_id * CPE_CHANNELS ) + n], output_frame, sub( Q11, Q_synth ) ); // Q11
436 : /* Save synthesis for HQ FEC */
437 : Word32 output_fx_[L_FRAME48k];
438 482276 : Copy32( output_fx[( cpe_id * CPE_CHANNELS ) + n], output_fx_, L_FRAME48k ); // Q11
439 482276 : Scale_sig32( output_fx_, L_FRAME48k, Q16 - Q11 ); // Q11 -> Q16
440 482276 : Copy_Scale_sig32_16( sts[n]->prev_synth_buffer32_fx, sts[n]->prev_synth_buffer_fx, NS2SA( 48000, DELAY_BWE_TOTAL_NS - DELAY_CLDFB_NS ), -Q11 ); // Q11 -> Q0
441 482276 : sts[n]->q_prev_synth_buffer_fx = 0;
442 482276 : move16();
443 :
444 482276 : save_synthesis_hq_fec_fx( sts[n], NULL, output_fx_, output_frame, 0, hCPE );
445 :
446 : /* CoreCoder common updates */
447 482276 : ivas_updt_dec_common_fx( hCPE->hCoreCoder[n], NORMAL_HQ_CORE, -1, output_fx[( cpe_id * CPE_CHANNELS ) + n], 11 );
448 :
449 : } /* n_channels loop */
450 :
451 723414 : FOR( n = 0; n < 2; n++ )
452 : {
453 482276 : IF( hCPE->hCoreCoder[n] )
454 : {
455 482276 : Copy_Scale_sig_16_32_no_sat( hCPE->hCoreCoder[n]->delay_buf_out_fx, hCPE->hCoreCoder[n]->delay_buf_out32_fx, HQ_DELTA_MAX * HQ_DELAY_COMP, Q11 ); // Q0 -> Q11
456 : }
457 : }
458 :
459 : /* synthesis synchronization between stereo modes */
460 241138 : test();
461 241138 : test();
462 241138 : IF( ( st_ivas->sba_dirac_stereo_flag == 0 ) || ( EQ_16( st_ivas->ivas_format, SBA_ISM_FORMAT ) && LT_16( cpe_id, sub( nCPE, 2 ) ) ) )
463 : {
464 231256 : synchro_synthesis_fx( ivas_total_brate, hCPE, output_fx + imult1616( cpe_id, CPE_CHANNELS ), output_frame, 0, Q11 );
465 : }
466 : }
467 :
468 : /* move channels after LFE to correct output for multi-channel MCT */
469 93116 : test();
470 93116 : test();
471 93116 : IF( EQ_16( st_ivas->ivas_format, MC_FORMAT ) && ( EQ_16( st_ivas->mc_mode, MC_MODE_MCT ) || EQ_16( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) ) )
472 : {
473 : Word32 tmp[L_FRAME48k];
474 :
475 : /*save center channel output*/
476 25087 : Copy32( output_fx[hMCT->nchan_out_woLFE - 1], tmp, output_frame ); // Q11
477 :
478 88719 : FOR( n = hMCT->nchan_out_woLFE - 1; n >= LFE_CHANNEL; n-- )
479 : {
480 63632 : Copy32( output_fx[n - 1], output_fx[n + 1], output_frame ); // Q11
481 : }
482 25087 : Copy32( tmp, output_fx[LFE_CHANNEL - 1], output_frame ); // Q11
483 :
484 : /* save LFE channel */
485 25087 : Copy32( output_lfe_ch_fx, output_fx[LFE_CHANNEL], output_frame ); // Q11
486 : }
487 :
488 93116 : pop_wmops();
489 93116 : return error;
490 : }
491 :
492 :
493 : /*-------------------------------------------------------------------------
494 : * create_mct_dec()
495 : *
496 : * Create, allocate and initialize IVAS decoder MCT handle
497 : *-------------------------------------------------------------------------*/
498 836 : ivas_error create_mct_dec_fx(
499 : Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
500 : )
501 : {
502 : MCT_DEC_HANDLE hMCT;
503 : Word16 n;
504 : Word32 cp_bitrate;
505 : Word16 max_blocks;
506 : Word16 cpe_id;
507 :
508 : /*--------------------------------------------------------- --------*
509 : * Allocate MCT handle
510 : *-----------------------------------------------------------------*/
511 :
512 836 : IF( ( hMCT = (MCT_DEC_HANDLE) malloc( sizeof( MCT_DEC_DATA ) ) ) == NULL )
513 : {
514 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CPE\n" ) );
515 : }
516 :
517 : /*-----------------------------------------------------------------*
518 : * Allocate MCT BlockData handles
519 : *-----------------------------------------------------------------*/
520 :
521 : /* Determine active channels */
522 836 : test();
523 836 : test();
524 836 : test();
525 836 : test();
526 836 : IF( ( EQ_16( st_ivas->ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_PARAMMC ) ) || EQ_16( st_ivas->ivas_format, SBA_FORMAT ) || EQ_16( st_ivas->ivas_format, SBA_ISM_FORMAT ) )
527 : {
528 535 : hMCT->nchan_out_woLFE = st_ivas->nchan_transport;
529 535 : move16();
530 535 : if ( EQ_16( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
531 : {
532 69 : hMCT->nchan_out_woLFE = add( hMCT->nchan_out_woLFE, st_ivas->nchan_ism );
533 69 : move16();
534 : }
535 : }
536 301 : ELSE IF( EQ_16( st_ivas->mc_mode, MC_MODE_MCT ) )
537 : {
538 292 : hMCT->nchan_out_woLFE = sub( st_ivas->nchan_transport, st_ivas->hTransSetup.num_lfe );
539 292 : move16();
540 : }
541 9 : ELSE IF( EQ_16( st_ivas->ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
542 : {
543 9 : hMCT->nchan_out_woLFE = sub( st_ivas->nchan_transport, st_ivas->hTransSetup.num_lfe );
544 9 : move16();
545 : }
546 : ELSE
547 : {
548 0 : assert( !"IVAS format currently not supported for MCT" );
549 : }
550 :
551 836 : cp_bitrate = L_shl( Mult_32_16( st_ivas->hDecoderConfig->ivas_total_brate, div_s( 1, hMCT->nchan_out_woLFE ) ), 1 );
552 :
553 836 : IF( EQ_16( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
554 : {
555 69 : cp_bitrate = L_shl( Mult_32_16( st_ivas->hDecoderConfig->ivas_total_brate, div_s( 1, st_ivas->nchan_transport ) ), 1 );
556 : }
557 :
558 : /* indicate LFE for appropriate core-coder channel */
559 3019 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
560 : {
561 6549 : FOR( n = 0; n < CPE_CHANNELS; n++ )
562 : {
563 4366 : st_ivas->hCPE[cpe_id]->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
564 4366 : move32();
565 : }
566 : }
567 :
568 : /* in case we have an uneven number of transport channels, indicate last channel ID as inactive */
569 836 : if ( hMCT->nchan_out_woLFE % 2 )
570 : {
571 631 : st_ivas->hCPE[st_ivas->nCPE - 1]->hCoreCoder[1]->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
572 631 : move32();
573 : }
574 :
575 : /*Initialize MCT block data */
576 836 : max_blocks = shr( hMCT->nchan_out_woLFE, 1 );
577 :
578 2388 : FOR( n = 0; n < max_blocks; n++ )
579 : {
580 1552 : IF( ( hMCT->hBlockData[n] = (MCT_DEC_BLOCK_DATA_HANDLE) malloc( sizeof( MCT_DEC_BLOCK_DATA ) ) ) == NULL )
581 : {
582 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MCT block data structure\n" ) );
583 : }
584 :
585 : /*Initialize all parameters to zero*/
586 1552 : hMCT->hBlockData[n]->ch1 = 0;
587 1552 : hMCT->hBlockData[n]->ch2 = 0;
588 1552 : move16();
589 1552 : move16();
590 : /*-----------------------------------------------------------------*
591 : * MDCT stereo initialization
592 : *-----------------------------------------------------------------*/
593 :
594 1552 : IF( ( hMCT->hBlockData[n]->hStereoMdct = (STEREO_MDCT_DEC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_DEC_DATA ) ) ) == NULL )
595 : {
596 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
597 : }
598 :
599 1552 : initMdctStereoDecData_fx( hMCT->hBlockData[n]->hStereoMdct, st_ivas->hCPE[0]->hCoreCoder[0]->igf, st_ivas->hCPE[0]->hCoreCoder[0]->hIGFDec->igfData.igfInfo.grid, cp_bitrate, SWB );
600 1552 : hMCT->hBlockData[n]->hStereoMdct->use_itd = 0;
601 1552 : hMCT->hBlockData[n]->hStereoMdct->reverse_dmx = 0;
602 1552 : move16();
603 1552 : move16();
604 1552 : hMCT->hBlockData[n]->hStereoMdct->smooth_ratio_fx = ONE_IN_Q26; // Q26
605 1552 : move32();
606 : }
607 4300 : FOR( ; n < MCT_MAX_BLOCKS; n++ )
608 : {
609 3464 : hMCT->hBlockData[n] = NULL;
610 : }
611 :
612 : /*-----------------------------------------------------------------*
613 : * Initializations
614 : *-----------------------------------------------------------------*/
615 :
616 836 : hMCT->currBlockDataCnt = 0;
617 836 : move16();
618 :
619 : /*Initialize bits required to signal channel-pair index*/
620 : // hMCT->bitsChannelPairIndex = max( 1, (Word16) ( floorf( ( logf( (float) hMCT->nchan_out_woLFE * ( hMCT->nchan_out_woLFE - 1 ) / 2 - 1 ) * INV_LOG_2 ) ) + 1 ) );
621 836 : hMCT->bitsChannelPairIndex = s_max( 1, add( floor_log_2( sub( shr( imult1616( hMCT->nchan_out_woLFE, sub( hMCT->nchan_out_woLFE, 1 ) ), 1 ), 1 ) ), 1 ) );
622 :
623 836 : set16_fx( hMCT->chBitRatios, 0, MCT_MAX_CHANNELS );
624 836 : set16_fx( hMCT->lowE_ch, 0, MCT_MAX_CHANNELS );
625 :
626 836 : st_ivas->hMCT = hMCT;
627 :
628 836 : return IVAS_ERR_OK;
629 : }
630 :
631 : /*-------------------------------------------------------------------------
632 : * mct_dec_reconfigure()
633 : *
634 : * Reconfigure IVAS decoder MCT handle
635 : *-------------------------------------------------------------------------*/
636 :
637 381 : ivas_error mct_dec_reconfigure_fx(
638 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
639 : const UWord16 b_nchan_change /* i : flag indicating different channel count */
640 : )
641 : {
642 : MCT_DEC_HANDLE hMCT;
643 : Decoder_State *st;
644 : Word16 n, cpe_id, max_blocks;
645 : Word32 cp_bitrate, L_tmp;
646 : Word16 tmp_exp, tmp;
647 :
648 381 : hMCT = st_ivas->hMCT;
649 :
650 : /*-----------------------------------------------------------------*
651 : * Allocate and initialize MCT BlockData handles
652 : *-----------------------------------------------------------------*/
653 381 : IF( b_nchan_change )
654 : {
655 : /* Determine active channels */
656 111 : test();
657 111 : test();
658 111 : test();
659 111 : test();
660 111 : IF( ( EQ_32( st_ivas->ivas_format, MC_FORMAT ) && EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) ) || EQ_32( st_ivas->ivas_format, SBA_FORMAT ) || EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) )
661 : {
662 105 : hMCT->nchan_out_woLFE = st_ivas->nchan_transport;
663 105 : move16();
664 105 : IF( EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
665 : {
666 19 : hMCT->nchan_out_woLFE = add( hMCT->nchan_out_woLFE, st_ivas->nchan_ism );
667 19 : move16();
668 : }
669 : }
670 6 : ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) )
671 : {
672 5 : hMCT->nchan_out_woLFE = sub( st_ivas->nchan_transport, st_ivas->hTransSetup.num_lfe );
673 5 : move16();
674 : }
675 1 : ELSE IF( EQ_32( st_ivas->ivas_format, MC_FORMAT ) && EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
676 : {
677 1 : hMCT->nchan_out_woLFE = sub( st_ivas->nchan_transport, st_ivas->hTransSetup.num_lfe );
678 1 : move16();
679 : }
680 : ELSE
681 : {
682 0 : assert( !"IVAS format currently not supported for MCT" );
683 : }
684 : }
685 :
686 : /* indicate LFE for appropriate core-coder channel */
687 1352 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
688 : {
689 2913 : FOR( n = 0; n < CPE_CHANNELS; n++ )
690 : {
691 1942 : st_ivas->hCPE[cpe_id]->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
692 1942 : move32();
693 : }
694 : }
695 :
696 : /* in case we have an uneven number of transport channels, indicate last channel ID as inactive */
697 381 : IF( hMCT->nchan_out_woLFE % 2 )
698 : {
699 309 : st_ivas->hCPE[st_ivas->nCPE - 1]->hCoreCoder[1]->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
700 309 : move32();
701 : }
702 :
703 381 : tmp = BASOP_Util_Divide3216_Scale( st_ivas->hDecoderConfig->ivas_total_brate, hMCT->nchan_out_woLFE, &tmp_exp );
704 381 : cp_bitrate = L_shl( tmp, add( tmp_exp, 2 ) );
705 :
706 381 : IF( EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
707 : {
708 27 : tmp = BASOP_Util_Divide3216_Scale( st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->nchan_transport, &tmp_exp );
709 27 : cp_bitrate = L_shl( tmp, add( tmp_exp, 2 ) );
710 : }
711 :
712 : /* set correct nominal bitrates and igf config already here, otherwise we
713 : * run into a number of problems */
714 1352 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
715 : {
716 971 : st_ivas->hCPE[cpe_id]->element_brate = cp_bitrate;
717 971 : move32();
718 2913 : FOR( n = 0; n < CPE_CHANNELS; n++ )
719 : {
720 1942 : st = st_ivas->hCPE[cpe_id]->hCoreCoder[n];
721 :
722 1942 : st->total_brate = st_ivas->hCPE[cpe_id]->element_brate;
723 1942 : move32();
724 :
725 1942 : IF( NE_32( st->mct_chan_mode, MCT_CHAN_MODE_IGNORE ) )
726 : {
727 : /* st->bits_frame_nominal = (int16_t) ( st_ivas->hCPE[cpe_id]->element_brate / FRAMES_PER_SEC ); */
728 1633 : st->bits_frame_nominal = extract_l( Mpy_32_32( st_ivas->hCPE[cpe_id]->element_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );
729 1633 : move16();
730 1633 : st->igf = getIgfPresent_fx( st->element_mode, L_mult0( st->bits_frame_nominal, FRAMES_PER_SEC ), st->bwidth, st->rf_flag ); // no floating point so directly pluggable
731 1633 : move16();
732 :
733 1633 : IF( st->igf )
734 : {
735 974 : IGFDecSetMode_ivas_fx( st->hIGFDec, st_ivas->hCPE[cpe_id]->element_brate, st->bwidth, st->element_mode, -1, -1, st->rf_flag );
736 : }
737 : }
738 : }
739 : }
740 :
741 : /*Initialize MCT block data */
742 : /* max_blocks = hMCT->nchan_out_woLFE / 2; */
743 381 : max_blocks = shr( hMCT->nchan_out_woLFE, 1 );
744 :
745 1043 : FOR( n = 0; n < max_blocks; n++ )
746 : {
747 662 : IF( b_nchan_change )
748 : {
749 218 : IF( hMCT->hBlockData[n] == NULL )
750 : {
751 103 : IF( ( hMCT->hBlockData[n] = (MCT_DEC_BLOCK_DATA_HANDLE) malloc( sizeof( MCT_BLOCK_DATA ) ) ) == NULL )
752 : {
753 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MCT block data structure\n" ) );
754 : }
755 :
756 : /*Initialize all parameters to zero*/
757 103 : hMCT->hBlockData[n]->ch1 = 0;
758 103 : move16();
759 103 : hMCT->hBlockData[n]->ch2 = 0;
760 103 : move16();
761 :
762 : /* MDCT stereo initialization */
763 103 : IF( ( hMCT->hBlockData[n]->hStereoMdct = (STEREO_MDCT_DEC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_ENC_DATA ) ) ) == NULL )
764 : {
765 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
766 : }
767 : }
768 : }
769 :
770 662 : initMdctStereoDecData_fx( hMCT->hBlockData[n]->hStereoMdct, st_ivas->hCPE[0]->hCoreCoder[0]->igf, st_ivas->hCPE[0]->hCoreCoder[0]->hIGFDec->igfData.igfInfo.grid, cp_bitrate, st_ivas->hCPE[0]->hCoreCoder[0]->bwidth );
771 :
772 662 : hMCT->hBlockData[n]->hStereoMdct->use_itd = 0;
773 662 : move16();
774 : }
775 :
776 2005 : FOR( ; n < MCT_MAX_BLOCKS; n++ )
777 : {
778 : /* deallocate no longer needed blocks */
779 1624 : IF( hMCT->hBlockData[n] != NULL )
780 : {
781 74 : IF( hMCT->hBlockData[n]->hStereoMdct != NULL )
782 : {
783 74 : free( hMCT->hBlockData[n]->hStereoMdct );
784 74 : hMCT->hBlockData[n]->hStereoMdct = NULL;
785 : }
786 :
787 74 : free( hMCT->hBlockData[n] );
788 74 : hMCT->hBlockData[n] = NULL;
789 : }
790 : }
791 :
792 : /*-----------------------------------------------------------------*
793 : * Initializations
794 : *-----------------------------------------------------------------*/
795 :
796 381 : IF( b_nchan_change )
797 : {
798 111 : hMCT->currBlockDataCnt = 0;
799 111 : move16();
800 :
801 : /*Initialize bits required to signal channel-pair index*/
802 :
803 : Word32 log_tmp;
804 111 : L_tmp = L_sub( L_shr( L_mult0( hMCT->nchan_out_woLFE, sub( hMCT->nchan_out_woLFE, 1 ) ), 1 ), 1 );
805 111 : tmp_exp = norm_l( L_tmp );
806 111 : L_tmp = L_shl( L_tmp, tmp_exp );
807 111 : log_tmp = BASOP_Util_Log2( L_tmp ); // ( 31 - tmp_exp )
808 111 : log_tmp = L_add( log_tmp, L_shl( sub( Q31, tmp_exp ), Q25 ) ); // Q25
809 : // scale down from Q25 to Q0
810 :
811 111 : tmp = extract_l( L_shr( log_tmp, Q25 ) );
812 111 : tmp = add( tmp, 1 );
813 111 : hMCT->bitsChannelPairIndex = s_max( 1, tmp );
814 111 : move16();
815 :
816 111 : set16_fx( hMCT->chBitRatios, 0, MCT_MAX_CHANNELS );
817 111 : set16_fx( hMCT->lowE_ch, 0, MCT_MAX_CHANNELS );
818 : }
819 :
820 381 : return IVAS_ERR_OK;
821 : }
822 :
823 :
824 : /*-------------------------------------------------------------------------
825 : * create_mct_dec_close()
826 : *
827 : * Close IVAS decoder MCT handle
828 : *-------------------------------------------------------------------------*/
829 :
830 1277 : void ivas_mct_dec_close(
831 : MCT_DEC_HANDLE *hMCT /* i/o: MCT decoder structure */
832 : )
833 : {
834 : Word16 n, maxBlocks;
835 :
836 1277 : test();
837 1277 : IF( hMCT == NULL || *hMCT == NULL )
838 : {
839 441 : return;
840 : }
841 :
842 836 : maxBlocks = shr( ( *hMCT )->nchan_out_woLFE, 1 );
843 :
844 2417 : FOR( n = 0; n < maxBlocks; n++ )
845 : {
846 1581 : IF( ( *hMCT )->hBlockData[n] != NULL )
847 : {
848 1581 : IF( ( *hMCT )->hBlockData[n]->hStereoMdct != NULL )
849 : {
850 1581 : free( ( *hMCT )->hBlockData[n]->hStereoMdct );
851 1581 : ( *hMCT )->hBlockData[n]->hStereoMdct = NULL;
852 : }
853 :
854 1581 : free( ( *hMCT )->hBlockData[n] );
855 1581 : ( *hMCT )->hBlockData[n] = NULL;
856 : }
857 : }
858 :
859 836 : free( *hMCT );
860 836 : *hMCT = NULL;
861 :
862 836 : return;
863 : }
864 :
865 :
866 : /*-------------------------------------------------------------------------
867 : * ivas_mc_dec_config()
868 : *
869 : * - read transported MC LS setup
870 : * - select MC format mode
871 : * - reconfigure the MC format decoder
872 : *-------------------------------------------------------------------------*/
873 :
874 : /*! r : MC format mode */
875 43754 : ivas_error ivas_mc_dec_config_fx(
876 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
877 : const Word16 idx, /* i : LS config. index */
878 : UWord16 *nSamplesRendered, /* o : samples flushed from last frame (JBM) */
879 : Word16 *data /* o : output synthesis signal */
880 : )
881 : {
882 : AUDIO_CONFIG signaled_config;
883 : MC_MODE last_mc_mode;
884 : ivas_error error;
885 :
886 : /* store last frame MC mode */
887 43754 : last_mc_mode = st_ivas->mc_mode;
888 43754 : move16();
889 :
890 43754 : IF( st_ivas->bfi == 0 )
891 : {
892 : /* set transported MC LS setup */
893 43754 : signaled_config = ivas_mc_map_ls_setup_to_output_config_fx( idx );
894 :
895 43754 : if ( st_ivas->ini_frame == 0 )
896 : {
897 76 : st_ivas->transport_config = signaled_config;
898 76 : move32();
899 : }
900 :
901 : /* select MC format mode */
902 43754 : st_ivas->mc_mode = ivas_mc_mode_select_fx( ivas_mc_map_output_config_to_mc_ls_setup_fx( signaled_config ), st_ivas->hDecoderConfig->ivas_total_brate );
903 43754 : move32();
904 :
905 : /* MC format switching */
906 43754 : IF( st_ivas->ini_frame != 0 )
907 : {
908 43678 : test();
909 43678 : IF( NE_32( st_ivas->hDecoderConfig->last_ivas_total_brate, st_ivas->hDecoderConfig->ivas_total_brate ) || st_ivas->transport_config != signaled_config || last_mc_mode != st_ivas->mc_mode )
910 : {
911 894 : IF( st_ivas->hRenderConfig )
912 : {
913 2265 : FOR( Word16 i = 0; i < 4; i++ )
914 : {
915 1812 : st_ivas->hRenderConfig->directivity_fx[( i * 3 ) + 2] = shl_sat( st_ivas->hRenderConfig->directivity_fx[( i * 3 ) + 2], 9 ); // Q15
916 1812 : move16();
917 : }
918 : }
919 894 : IF( NE_32( ( error = ivas_mc_dec_reconfig_fx( st_ivas, nSamplesRendered, data ) ), IVAS_ERR_OK ) )
920 : {
921 0 : return error;
922 : }
923 894 : IF( st_ivas->hRenderConfig )
924 : {
925 2265 : FOR( Word16 i = 0; i < 4; i++ )
926 : {
927 1812 : st_ivas->hRenderConfig->directivity_fx[( i * 3 ) + 2] = shr( st_ivas->hRenderConfig->directivity_fx[( i * 3 ) + 2], 9 ); // Q15
928 1812 : move16();
929 : }
930 : }
931 : }
932 : }
933 :
934 43754 : st_ivas->transport_config = signaled_config;
935 43754 : move32();
936 : }
937 :
938 43754 : return IVAS_ERR_OK;
939 : }
940 :
941 :
942 : /*-------------------------------------------------------------------------
943 : * ivas_mc_dec_reconfig()
944 : *
945 : * reconfigure the MC format decoder
946 : *-------------------------------------------------------------------------*/
947 :
948 894 : static ivas_error ivas_mc_dec_reconfig_fx(
949 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
950 : UWord16 *nSamplesRendered, /* o : number of samples flushed from the last frame (JBM) */
951 : Word16 *data /* o : output synthesis signal */
952 : )
953 : {
954 : Word16 nchan_transport_old, nSCE_old, nCPE_old, sba_dirac_stereo_flag_old, nchan_hp20_old;
955 : Word16 numCldfbAnalyses_old, numCldfbSyntheses_old;
956 : Word32 new_brate_SCE, new_brate_CPE, ivas_total_brate;
957 : RENDERER_TYPE renderer_type_old;
958 : Decoder_State *st;
959 : ivas_error error;
960 : MC_MODE mc_mode, last_mc_mode;
961 : TC_BUFFER_MODE tc_buffer_mode_new;
962 : Word16 tc_nchan_tc_new;
963 : Word16 tc_nchan_allocate_new;
964 : Word16 tc_granularity_new;
965 : AUDIO_CONFIG intern_config_old;
966 : IVAS_OUTPUT_SETUP hIntSetupOld;
967 : Word16 nchan_out_buff_old, nchan_out_buff;
968 :
969 894 : error = IVAS_ERR_OK;
970 894 : move32();
971 894 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
972 894 : move32();
973 894 : nchan_transport_old = st_ivas->nchan_transport;
974 894 : move16();
975 894 : nchan_out_buff_old = ivas_get_nchan_buffers_dec( st_ivas, -1, -1 );
976 894 : last_mc_mode = ivas_mc_mode_select_fx( ivas_mc_map_output_config_to_mc_ls_setup_fx( st_ivas->transport_config ), st_ivas->hDecoderConfig->last_ivas_total_brate ); /* NB: this assumes that LS config remains the same between frames */
977 :
978 : /* temporally set the current mc_mode back to the previous one to make sure the following call to
979 : ivas_init_dec_get_num_cldfb_instances() returns the correct counts */
980 894 : mc_mode = st_ivas->mc_mode;
981 894 : move32();
982 894 : st_ivas->mc_mode = last_mc_mode;
983 894 : move32();
984 894 : ivas_init_dec_get_num_cldfb_instances_ivas_fx( st_ivas, &numCldfbAnalyses_old, &numCldfbSyntheses_old );
985 :
986 894 : st_ivas->mc_mode = mc_mode;
987 894 : move32();
988 :
989 894 : nSCE_old = st_ivas->nSCE;
990 894 : move16();
991 894 : nCPE_old = st_ivas->nCPE;
992 894 : move16();
993 894 : sba_dirac_stereo_flag_old = st_ivas->sba_dirac_stereo_flag;
994 894 : move16();
995 :
996 : /* special handling needed for the hp20 buffers for McMASA */
997 894 : IF( EQ_16( last_mc_mode, MC_MODE_MCMASA ) )
998 : {
999 217 : nchan_hp20_old = getNumChanSynthesis( st_ivas );
1000 : }
1001 : ELSE
1002 : {
1003 677 : nchan_hp20_old = nchan_transport_old;
1004 677 : move16();
1005 : }
1006 894 : st_ivas->sba_dirac_stereo_flag = ivas_get_sba_dirac_stereo_flag( st_ivas );
1007 894 : move16();
1008 :
1009 : /* save old IntSetup, might be needed for JBM flushing...*/
1010 894 : intern_config_old = st_ivas->intern_config;
1011 894 : move32();
1012 894 : hIntSetupOld = st_ivas->hIntSetup;
1013 894 : tc_granularity_new = 1;
1014 894 : move16();
1015 :
1016 : /* renderer might have changed, reselect */
1017 894 : renderer_type_old = st_ivas->renderer_type;
1018 894 : move32();
1019 894 : ivas_renderer_select( st_ivas );
1020 :
1021 : /* side effect of the renderer selection can be a changed internal config */
1022 894 : ivas_output_init( &( st_ivas->hIntSetup ), st_ivas->intern_config );
1023 :
1024 : /* transfer subframe info from DirAC or ParamMC to central tc buffer */
1025 894 : test();
1026 894 : IF( EQ_16( last_mc_mode, MC_MODE_PARAMMC ) )
1027 : {
1028 288 : st_ivas->hTcBuffer->nb_subframes = st_ivas->hParamMC->nb_subframes;
1029 288 : move16();
1030 288 : st_ivas->hTcBuffer->subframes_rendered = st_ivas->hParamMC->subframes_rendered;
1031 288 : move16();
1032 288 : st_ivas->hTcBuffer->num_slots = st_ivas->hParamMC->num_slots;
1033 288 : move16();
1034 288 : st_ivas->hTcBuffer->slots_rendered = st_ivas->hParamMC->slots_rendered;
1035 288 : move16();
1036 288 : Copy( st_ivas->hParamMC->subframe_nbslots, st_ivas->hTcBuffer->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
1037 : }
1038 606 : ELSE IF( EQ_16( last_mc_mode, MC_MODE_MCMASA ) && st_ivas->hSpatParamRendCom != NULL )
1039 : {
1040 208 : st_ivas->hTcBuffer->nb_subframes = st_ivas->hSpatParamRendCom->nb_subframes;
1041 208 : move16();
1042 208 : st_ivas->hTcBuffer->subframes_rendered = st_ivas->hSpatParamRendCom->subframes_rendered;
1043 208 : move16();
1044 208 : st_ivas->hTcBuffer->num_slots = st_ivas->hSpatParamRendCom->num_slots;
1045 208 : move16();
1046 208 : st_ivas->hTcBuffer->slots_rendered = st_ivas->hSpatParamRendCom->slots_rendered;
1047 208 : move16();
1048 208 : Copy( st_ivas->hSpatParamRendCom->subframe_nbslots, st_ivas->hTcBuffer->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
1049 : }
1050 :
1051 : /* JBM: when granularity goes down (e.g. MCT with CREND -> ParamMC with binaural fastconv
1052 : render what still fits in the new granularity */
1053 894 : tc_granularity_new = ivas_jbm_dec_get_render_granularity( st_ivas->renderer_type, st_ivas->ivas_format, st_ivas->mc_mode, st_ivas->hDecoderConfig->output_Fs );
1054 894 : move16();
1055 894 : IF( LT_16( tc_granularity_new, st_ivas->hTcBuffer->n_samples_granularity ) )
1056 : {
1057 130 : IF( NE_32( ( error = ivas_jbm_dec_flush_renderer_fx( st_ivas, tc_granularity_new, renderer_type_old, intern_config_old, &hIntSetupOld, last_mc_mode, ISM_MODE_NONE, nSamplesRendered, data ) ), IVAS_ERR_OK ) )
1058 : {
1059 0 : return error;
1060 : }
1061 : }
1062 : /* JBM: when granularity goes up set samples to discard at the beginning of the frame */
1063 764 : ELSE IF( GT_16( tc_granularity_new, st_ivas->hTcBuffer->n_samples_granularity ) )
1064 : {
1065 134 : IF( NE_32( ( error = ivas_jbm_dec_set_discard_samples( st_ivas ) ), IVAS_ERR_OK ) )
1066 : {
1067 0 : return error;
1068 : }
1069 : }
1070 894 : IF( EQ_16( st_ivas->mc_mode, MC_MODE_MCT ) )
1071 : {
1072 393 : st_ivas->nchan_transport = ivas_mc_ls_setup_get_num_channels_fx( ivas_mc_map_output_config_to_mc_ls_setup_fx( st_ivas->transport_config ) );
1073 393 : move16();
1074 393 : st_ivas->nSCE = 0;
1075 393 : move16();
1076 393 : st_ivas->nCPE = shr( st_ivas->nchan_transport, 1 );
1077 393 : move16();
1078 :
1079 393 : IF( NE_16( last_mc_mode, MC_MODE_MCT ) )
1080 : {
1081 : /*De-allocate handles for other MC modes*/
1082 262 : IF( st_ivas->hParamMC != NULL )
1083 : {
1084 155 : ivas_param_mc_dec_close_fx( &st_ivas->hParamMC );
1085 :
1086 : /* remove ls conversion if it was allocated by ParamMC */
1087 155 : ivas_ls_setup_conversion_close_fx( &st_ivas->hLsSetUpConversion );
1088 : }
1089 :
1090 262 : IF( EQ_16( last_mc_mode, MC_MODE_PARAMUPMIX ) )
1091 : {
1092 2 : ivas_mc_paramupmix_dec_close( &( st_ivas->hMCParamUpmix ) );
1093 2 : ivas_ls_setup_conversion_close_fx( &( st_ivas->hLsSetUpConversion ) );
1094 : }
1095 :
1096 : /* De-allocate McMasa-related handles */
1097 262 : ivas_masa_dec_close_fx( &( st_ivas->hMasa ) );
1098 :
1099 262 : ivas_qmetadata_close_fx( &st_ivas->hQMetaData );
1100 262 : IF( st_ivas->hDirAC != NULL )
1101 : {
1102 106 : ivas_dirac_rend_close_fx( &( st_ivas->hDirACRend ) );
1103 106 : ivas_spat_hSpatParamRendCom_close_fx( &( st_ivas->hSpatParamRendCom ) );
1104 106 : ivas_dirac_dec_close_fx( &( st_ivas->hDirAC ) );
1105 106 : vbap_free_data_fx( &( st_ivas->hVBAPdata ) );
1106 : }
1107 :
1108 : /* init LS conversion if the renderer type asks for it */
1109 262 : test();
1110 262 : IF( EQ_16( st_ivas->renderer_type, RENDERER_MC ) && st_ivas->hLsSetUpConversion == NULL )
1111 : {
1112 62 : IF( NE_32( ( error = ivas_ls_setup_conversion_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
1113 : {
1114 0 : return error;
1115 : }
1116 : }
1117 : }
1118 : }
1119 501 : ELSE IF( EQ_16( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
1120 : {
1121 5 : st_ivas->nSCE = 0;
1122 5 : move16();
1123 5 : st_ivas->nCPE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS / 2;
1124 5 : move16();
1125 5 : st_ivas->nchan_transport = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
1126 5 : move16();
1127 :
1128 5 : IF( NE_16( last_mc_mode, MC_MODE_PARAMUPMIX ) )
1129 : {
1130 : /*De-allocate handles for other MC modes*/
1131 5 : IF( st_ivas->hParamMC != NULL )
1132 : {
1133 1 : ivas_param_mc_dec_close_fx( &st_ivas->hParamMC );
1134 :
1135 : /* remove ls conversion if it was allocated by ParamMC */
1136 1 : ivas_ls_setup_conversion_close_fx( &st_ivas->hLsSetUpConversion );
1137 : }
1138 :
1139 5 : ivas_masa_dec_close_fx( &( st_ivas->hMasa ) );
1140 5 : ivas_qmetadata_close_fx( &st_ivas->hQMetaData );
1141 :
1142 : /* init LS conversion if the renderer type asks for it */
1143 5 : test();
1144 5 : IF( ( EQ_16( st_ivas->renderer_type, RENDERER_MC ) ) && st_ivas->hLsSetUpConversion == NULL )
1145 : {
1146 1 : IF( ( error = ivas_ls_setup_conversion_open_fx( st_ivas ) ) != IVAS_ERR_OK )
1147 : {
1148 0 : return error;
1149 : }
1150 : }
1151 :
1152 5 : IF( ( error = ivas_mc_paramupmix_dec_open( st_ivas ) ) != IVAS_ERR_OK )
1153 : {
1154 0 : return error;
1155 : }
1156 : }
1157 : }
1158 496 : ELSE IF( EQ_16( st_ivas->mc_mode, MC_MODE_PARAMMC ) )
1159 : {
1160 288 : IF( NE_16( last_mc_mode, MC_MODE_PARAMMC ) )
1161 : {
1162 : /* remove old ls conversion for MCT if open, gets reopened correctly within ivas_param_mc_dec_open when needed */
1163 232 : test();
1164 232 : IF( EQ_16( renderer_type_old, RENDERER_MC ) && st_ivas->hLsSetUpConversion != NULL )
1165 : {
1166 37 : ivas_ls_setup_conversion_close_fx( &st_ivas->hLsSetUpConversion );
1167 : }
1168 :
1169 232 : IF( NE_32( ( error = ivas_param_mc_dec_open_fx( st_ivas ) ), IVAS_ERR_OK ) )
1170 : {
1171 0 : return error;
1172 : }
1173 : }
1174 : ELSE
1175 : {
1176 56 : IF( NE_32( ( error = ivas_param_mc_dec_reconfig_fx( st_ivas ) ), IVAS_ERR_OK ) )
1177 : {
1178 0 : return error;
1179 : }
1180 : }
1181 :
1182 : /* De-allocate McMasa-related handles */
1183 288 : ivas_masa_dec_close_fx( &( st_ivas->hMasa ) );
1184 288 : ivas_qmetadata_close_fx( &st_ivas->hQMetaData );
1185 :
1186 288 : IF( st_ivas->hDirAC != NULL )
1187 : {
1188 78 : ivas_dirac_rend_close_fx( &( st_ivas->hDirACRend ) );
1189 78 : ivas_spat_hSpatParamRendCom_close_fx( &( st_ivas->hSpatParamRendCom ) );
1190 78 : ivas_dirac_dec_close_fx( &( st_ivas->hDirAC ) );
1191 78 : vbap_free_data_fx( &( st_ivas->hVBAPdata ) );
1192 : }
1193 :
1194 288 : IF( EQ_16( last_mc_mode, MC_MODE_MCT ) )
1195 : {
1196 152 : test();
1197 152 : IF( st_ivas->hMCT != NULL && LE_16( st_ivas->nchan_transport, CPE_CHANNELS ) )
1198 : {
1199 150 : ivas_mct_dec_close( &st_ivas->hMCT );
1200 : }
1201 : }
1202 136 : ELSE IF( EQ_16( last_mc_mode, MC_MODE_PARAMUPMIX ) )
1203 : {
1204 0 : ivas_mc_paramupmix_dec_close( &( st_ivas->hMCParamUpmix ) );
1205 : }
1206 :
1207 : /* LFE handle */
1208 288 : ivas_lfe_dec_close_fx( &( st_ivas->hLFE ) );
1209 : }
1210 208 : ELSE IF( EQ_16( st_ivas->mc_mode, MC_MODE_MCMASA ) )
1211 : {
1212 208 : ivas_mcmasa_setNumTransportChannels_fx( &( st_ivas->nchan_transport ), &( st_ivas->element_mode_init ), ivas_total_brate );
1213 :
1214 208 : IF( NE_16( last_mc_mode, MC_MODE_MCMASA ) )
1215 : {
1216 180 : IF( NE_32( ( error = ivas_qmetadata_open_fx( &( st_ivas->hQMetaData ) ) ), IVAS_ERR_OK ) )
1217 : {
1218 0 : return error;
1219 : }
1220 : }
1221 :
1222 208 : IF( NE_32( ( error = ivas_mcmasa_dec_reconfig( st_ivas ) ), IVAS_ERR_OK ) )
1223 : {
1224 0 : return error;
1225 : }
1226 :
1227 : /* LS conversion */
1228 208 : IF( st_ivas->hLsSetUpConversion != NULL )
1229 : {
1230 41 : ivas_ls_setup_conversion_close_fx( &st_ivas->hLsSetUpConversion );
1231 : }
1232 :
1233 208 : ivas_mc_paramupmix_dec_close( &( st_ivas->hMCParamUpmix ) );
1234 :
1235 208 : IF( st_ivas->hParamMC != NULL )
1236 : {
1237 76 : ivas_param_mc_dec_close_fx( &st_ivas->hParamMC );
1238 76 : st_ivas->hParamMC = NULL;
1239 : }
1240 :
1241 208 : IF( EQ_16( last_mc_mode, MC_MODE_MCT ) )
1242 : {
1243 104 : ivas_mct_dec_close( &st_ivas->hMCT );
1244 : }
1245 :
1246 : /* LFE handle */
1247 208 : ivas_lfe_dec_close_fx( &( st_ivas->hLFE ) );
1248 : }
1249 :
1250 894 : IF( NE_16( st_ivas->mc_mode, MC_MODE_MCMASA ) )
1251 : {
1252 686 : IF( EQ_16( st_ivas->nchan_transport, 1 ) )
1253 : {
1254 0 : st_ivas->element_mode_init = IVAS_SCE;
1255 : }
1256 : ELSE
1257 : {
1258 686 : st_ivas->element_mode_init = IVAS_CPE_MDCT;
1259 : }
1260 686 : move16();
1261 : }
1262 :
1263 : /*-----------------------------------------------------------------*
1264 : * Reconfigure core coder
1265 : *-----------------------------------------------------------------*/
1266 :
1267 : /* special case: MCT->ParamMC with more than 2 TC, CPE 1 stays, but has the wrong mct_chan_mode in channel 1
1268 : and might have IGF static memory not allocated and the bit stream index list not set,
1269 : set correct mct_chan_mode and init missing static mem (IGF, HQ) and some config (TNS) do it here since it is _very_ MC specific */
1270 894 : test();
1271 894 : test();
1272 894 : IF( EQ_16( last_mc_mode, MC_MODE_MCT ) && EQ_16( st_ivas->mc_mode, MC_MODE_PARAMMC ) && GT_16( st_ivas->nchan_transport, CPE_CHANNELS ) )
1273 : {
1274 2 : st = st_ivas->hCPE[1]->hCoreCoder[1];
1275 :
1276 2 : IF( EQ_16( st_ivas->nchan_transport, 3 ) )
1277 : {
1278 2 : st->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
1279 : }
1280 : ELSE
1281 : {
1282 0 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
1283 : }
1284 2 : move32();
1285 :
1286 2 : IF( st->hIGFDec == NULL )
1287 : {
1288 0 : IF( ( st->hIGFDec = (IGF_DEC_INSTANCE_HANDLE) malloc( sizeof( IGFDEC_INSTANCE ) ) ) == NULL )
1289 : {
1290 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for IGF\n" ) );
1291 : }
1292 :
1293 0 : st->igf = 0;
1294 0 : move16();
1295 0 : init_igf_dec( st->hIGFDec );
1296 : }
1297 :
1298 2 : IF( st->hHQ_core == NULL )
1299 : {
1300 0 : IF( ( st->hHQ_core = (HQ_DEC_HANDLE) malloc( sizeof( HQ_DEC_DATA ) ) ) == NULL )
1301 : {
1302 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HQ core\n" ) );
1303 : }
1304 :
1305 : /* HQ core initialization */
1306 0 : HQ_core_dec_init_fx( st->hHQ_core );
1307 : }
1308 :
1309 : /* check if we have a doubly used hTxcCfg, if so, allocate a distint one for the old MCT LFE channel */
1310 2 : IF( st->hTcxCfg == st_ivas->hCPE[1]->hCoreCoder[0]->hTcxCfg )
1311 : {
1312 0 : IF( ( st->hTcxCfg = (TCX_CONFIG_HANDLE) malloc( sizeof( TCX_config ) ) ) == NULL )
1313 : {
1314 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for hTcxCfg\n" ) );
1315 : }
1316 : }
1317 :
1318 2 : st->hTcxCfg->fIsTNSAllowed = getTnsAllowed( ivas_total_brate, st->igf, st->element_mode );
1319 2 : move16();
1320 : }
1321 894 : IF( EQ_16( st_ivas->mc_mode, MC_MODE_MCMASA ) )
1322 : {
1323 : UWord8 separateChannelEnabled;
1324 : Word16 separateChannelIndex;
1325 208 : ivas_mcmasa_set_separate_channel_mode_fx( &separateChannelEnabled, &separateChannelIndex, ivas_total_brate );
1326 208 : ivas_mcmasa_split_brate_fx( separateChannelEnabled, ivas_total_brate, st_ivas->nSCE, st_ivas->nCPE, &new_brate_SCE, &new_brate_CPE );
1327 : }
1328 686 : ELSE IF( EQ_16( st_ivas->mc_mode, MC_MODE_MCT ) )
1329 : {
1330 393 : new_brate_SCE = 0;
1331 393 : move32();
1332 : Word16 temp_e;
1333 393 : Word32 L_temp = BASOP_Util_Divide3232_Scale_newton( ivas_total_brate, sub( st_ivas->nchan_transport, 1 ), &temp_e );
1334 393 : L_temp = L_shr( L_temp, sub( 31, temp_e ) );
1335 393 : new_brate_CPE = L_shl( L_temp, 1 ) /*CPE_CHANNELS*/;
1336 : }
1337 293 : ELSE IF( EQ_16( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
1338 : {
1339 5 : new_brate_SCE = 0;
1340 5 : move32();
1341 : Word16 temp_e;
1342 5 : Word32 L_temp = BASOP_Util_Divide3232_Scale_newton( ivas_total_brate, sub( st_ivas->nchan_transport, 1 ), &temp_e );
1343 5 : L_temp = L_shr( L_temp, sub( 31, temp_e ) );
1344 5 : new_brate_CPE = L_shl( L_temp, 1 ) /*CPE_CHANNELS*/;
1345 : }
1346 : ELSE
1347 : {
1348 288 : new_brate_SCE = 0; /* ivas_total_brate / st_ivas->nchan_transport;*/
1349 288 : move32();
1350 : Word16 temp_e;
1351 288 : Word32 L_temp = BASOP_Util_Divide3232_Scale_newton( ivas_total_brate, st_ivas->nchan_transport, &temp_e );
1352 288 : L_temp = L_shr( L_temp, sub( 31, temp_e ) );
1353 288 : new_brate_CPE = L_shl( L_temp, 1 ) /*CPE_CHANNELS*/;
1354 : }
1355 :
1356 894 : test();
1357 894 : test();
1358 894 : IF( NE_32( ( error = ivas_corecoder_dec_reconfig_fx( st_ivas, nSCE_old, nCPE_old, nchan_transport_old, sba_dirac_stereo_flag_old, new_brate_SCE, new_brate_CPE ) ), IVAS_ERR_OK ) )
1359 : {
1360 0 : return error;
1361 : }
1362 894 : IF( EQ_16( last_mc_mode, MC_MODE_MCT ) && EQ_16( st_ivas->mc_mode, MC_MODE_PARAMMC ) && GT_16( st_ivas->nchan_transport, CPE_CHANNELS ) )
1363 : {
1364 2 : st = st_ivas->hCPE[1]->hCoreCoder[1];
1365 :
1366 : /* TCX-LTP */
1367 2 : IF( st->hTcxLtpDec == NULL )
1368 : {
1369 0 : IF( ( st->hTcxLtpDec = (TCX_LTP_DEC_HANDLE) malloc( sizeof( TCX_LTP_DEC_DATA ) ) ) == NULL )
1370 : {
1371 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TCX-LTP handle\n" ) );
1372 : }
1373 0 : tcxltp_dec_init_fx( st->hTcxLtpDec, 0, st->last_codec_mode, st->element_mode, st->pit_max, st->sr_core );
1374 : }
1375 : }
1376 :
1377 : /*-----------------------------------------------------------------*
1378 : * re-configure HP20 memories
1379 : *-----------------------------------------------------------------*/
1380 894 : IF( NE_32( ( error = ivas_hp20_dec_reconfig_fx( st_ivas, nchan_hp20_old ) ), IVAS_ERR_OK ) )
1381 : {
1382 0 : return error;
1383 : }
1384 : /*-----------------------------------------------------------------*
1385 : * Allocate the LFE handle that is coded separately after the allocation of the core coders
1386 : *-----------------------------------------------------------------*/
1387 :
1388 894 : test();
1389 894 : test();
1390 894 : IF( ( EQ_16( st_ivas->mc_mode, MC_MODE_MCT ) || EQ_16( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) ) && st_ivas->hLFE == NULL )
1391 : {
1392 265 : Word32 binauralization_delay_ns = st_ivas->binaural_latency_ns;
1393 265 : move32();
1394 265 : IF( st_ivas->hBinRenderer != NULL )
1395 : {
1396 79 : IF( st_ivas->hBinRenderer->render_lfe )
1397 : {
1398 : /* Account for filterbank delay */
1399 77 : binauralization_delay_ns = L_add( binauralization_delay_ns, IVAS_FB_DEC_DELAY_NS );
1400 : }
1401 : ELSE
1402 : {
1403 2 : binauralization_delay_ns = 0;
1404 2 : move32();
1405 : }
1406 : }
1407 :
1408 265 : IF( NE_32( ( error = ivas_create_lfe_dec_fx( &st_ivas->hLFE, st_ivas->hDecoderConfig->output_Fs, binauralization_delay_ns ) ), IVAS_ERR_OK ) )
1409 : {
1410 0 : return error;
1411 : }
1412 :
1413 265 : set32_fx( st_ivas->hLFE->prevsynth_buf_fx, 0, LFE_PLC_BUFLEN );
1414 265 : set32_fx( st_ivas->hLFE->prior_out_buffer_fx, 0, L_FRAME48k );
1415 : }
1416 :
1417 : /*-----------------------------------------------------------------*
1418 : * Reconfigure renderers
1419 : *-----------------------------------------------------------------*/
1420 :
1421 894 : IF( EQ_16( st_ivas->mc_mode, MC_MODE_MCMASA ) )
1422 : {
1423 208 : test();
1424 208 : test();
1425 208 : IF( ( NE_16( st_ivas->renderer_type, RENDERER_DISABLE ) ) && ( NE_16( st_ivas->renderer_type, RENDERER_MCMASA_MONO_STEREO ) ) )
1426 : {
1427 200 : IF( st_ivas->hDirAC != NULL )
1428 : {
1429 : /* reconfigure existing DirAC dec */
1430 200 : IF( NE_32( ( error = ivas_dirac_dec_config_fx( st_ivas, DIRAC_RECONFIGURE ) ), IVAS_ERR_OK ) )
1431 : {
1432 0 : return error;
1433 : }
1434 : }
1435 : ELSE
1436 : {
1437 : /* init a new DirAC dec */
1438 0 : IF( NE_32( ( error = ivas_dirac_dec_config_fx( st_ivas, DIRAC_OPEN ) ), IVAS_ERR_OK ) )
1439 : {
1440 0 : return error;
1441 : }
1442 : }
1443 : }
1444 8 : ELSE IF( EQ_16( st_ivas->renderer_type, RENDERER_DISABLE ) && st_ivas->hDirAC != NULL )
1445 : {
1446 0 : ivas_dirac_rend_close_fx( &( st_ivas->hDirACRend ) );
1447 0 : ivas_spat_hSpatParamRendCom_close_fx( &( st_ivas->hSpatParamRendCom ) );
1448 0 : ivas_dirac_dec_close_fx( &( st_ivas->hDirAC ) );
1449 0 : vbap_free_data_fx( &( st_ivas->hVBAPdata ) );
1450 : }
1451 : }
1452 :
1453 894 : IF( NE_16( renderer_type_old, st_ivas->renderer_type ) )
1454 : {
1455 : AUDIO_CONFIG output_config;
1456 :
1457 511 : output_config = st_ivas->hDecoderConfig->output_config;
1458 511 : move32();
1459 :
1460 : /* binaural renderers*/
1461 511 : test();
1462 511 : test();
1463 511 : test();
1464 511 : test();
1465 511 : test();
1466 511 : test();
1467 511 : test();
1468 511 : test();
1469 511 : IF( EQ_16( output_config, IVAS_AUDIO_CONFIG_BINAURAL ) || EQ_16( output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR ) || EQ_16( output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) )
1470 : {
1471 : /* remove unneeded binaural renderers */
1472 346 : test();
1473 346 : test();
1474 346 : IF( st_ivas->hBinRenderer != NULL && ( NE_16( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV ) && NE_16( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV_ROOM ) ) )
1475 : {
1476 119 : ivas_binRenderer_close_fx( &st_ivas->hBinRenderer );
1477 : }
1478 :
1479 346 : test();
1480 346 : test();
1481 346 : test();
1482 346 : test();
1483 346 : test();
1484 346 : IF( ( st_ivas->hCrendWrapper != NULL ) && ( st_ivas->hCrendWrapper->hCrend != NULL ) && ( NE_16( st_ivas->renderer_type, RENDERER_BINAURAL_MIXER_CONV ) && NE_16( st_ivas->renderer_type, RENDERER_BINAURAL_MIXER_CONV_ROOM ) && ( NE_16( st_ivas->renderer_type, RENDERER_BINAURAL_OBJECTS_TD ) || NE_16( st_ivas->hIntSetup.output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) ) ) )
1485 : {
1486 :
1487 127 : ivas_rend_closeCrend( &( st_ivas->hCrendWrapper ) );
1488 : }
1489 :
1490 346 : test();
1491 346 : IF( st_ivas->hBinRendererTd != NULL && ( NE_16( st_ivas->renderer_type, RENDERER_BINAURAL_OBJECTS_TD ) ) )
1492 : {
1493 6 : IF( EQ_16( st_ivas->hBinRendererTd->HrFiltSet_p->ModelParams.modelROM, TRUE ) )
1494 : {
1495 0 : ivas_td_binaural_close_fx( &st_ivas->hBinRendererTd );
1496 0 : st_ivas->hHrtfTD = NULL;
1497 : }
1498 : }
1499 :
1500 346 : IF( st_ivas->hDiracDecBin != NULL )
1501 : {
1502 189 : test();
1503 189 : test();
1504 189 : IF( NE_16( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) && NE_16( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) && NE_16( st_ivas->renderer_type, RENDERER_STEREO_PARAMETRIC ) )
1505 : {
1506 97 : ivas_dirac_dec_close_binaural_data( &st_ivas->hDiracDecBin );
1507 : }
1508 : }
1509 :
1510 : /* init necessary new renderers */
1511 346 : test();
1512 346 : test();
1513 346 : test();
1514 346 : test();
1515 346 : test();
1516 346 : IF( st_ivas->hBinRenderer == NULL && ( EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV ) || EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV_ROOM ) ) )
1517 : {
1518 120 : IF( ( error = ivas_binRenderer_open_fx( st_ivas ) ) != IVAS_ERR_OK )
1519 : {
1520 0 : return error;
1521 : }
1522 : }
1523 226 : ELSE IF( st_ivas->hBinRendererTd == NULL && EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_OBJECTS_TD ) )
1524 : {
1525 1 : IF( ( error = ivas_td_binaural_open_fx( st_ivas, &st_ivas->SrcInd[0], &st_ivas->num_src ) ) != IVAS_ERR_OK )
1526 : {
1527 0 : return error;
1528 : }
1529 1 : IF( EQ_16( st_ivas->hIntSetup.output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) )
1530 : {
1531 0 : IF( ( error = ivas_rend_initCrendWrapper( &st_ivas->hCrendWrapper ) ) != IVAS_ERR_OK )
1532 : {
1533 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Crend Wrapper\n" );
1534 : }
1535 :
1536 0 : st_ivas->hCrendWrapper->hCrend = NULL;
1537 0 : st_ivas->hCrendWrapper->hHrtfCrend = NULL;
1538 0 : IF( ( st_ivas->hCrendWrapper->hCrend = (CREND_HANDLE) malloc( sizeof( CREND_DATA ) ) ) == NULL )
1539 : {
1540 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Crend\n" );
1541 : }
1542 : }
1543 : }
1544 225 : ELSE IF( st_ivas->hCrendWrapper == NULL && ( EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_MIXER_CONV ) || EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_MIXER_CONV_ROOM ) ) )
1545 : {
1546 130 : IF( ( error = ivas_rend_openCrend( &( st_ivas->hCrendWrapper ), st_ivas->intern_config, st_ivas->hDecoderConfig->output_config, st_ivas->hRenderConfig, st_ivas->hSetOfHRTF, st_ivas->hDecoderConfig->output_Fs ) ) != IVAS_ERR_OK )
1547 : {
1548 0 : return error;
1549 : }
1550 130 : st_ivas->binaural_latency_ns = st_ivas->hCrendWrapper->binaural_latency_ns;
1551 130 : move32();
1552 : }
1553 : }
1554 : }
1555 : /*-----------------------------------------------------------------*
1556 : * CLDFB instances
1557 : *-----------------------------------------------------------------*/
1558 :
1559 894 : IF( NE_32( ( error = ivas_cldfb_dec_reconfig_fx( st_ivas, nchan_transport_old, numCldfbAnalyses_old, numCldfbSyntheses_old ) ), IVAS_ERR_OK ) )
1560 : {
1561 0 : return error;
1562 : }
1563 :
1564 : /*-----------------------------------------------------------------*
1565 : * JBM TC buffers
1566 : *-----------------------------------------------------------------*/
1567 :
1568 : {
1569 : Word16 tc_nchan_full_new;
1570 : DECODER_TC_BUFFER_HANDLE hTcBuffer;
1571 :
1572 894 : hTcBuffer = st_ivas->hTcBuffer;
1573 894 : tc_buffer_mode_new = ivas_jbm_dec_get_tc_buffer_mode( st_ivas );
1574 894 : tc_nchan_tc_new = ivas_jbm_dec_get_num_tc_channels_fx( st_ivas );
1575 894 : tc_nchan_allocate_new = tc_nchan_tc_new;
1576 894 : move16();
1577 894 : tc_nchan_full_new = tc_nchan_tc_new;
1578 894 : move16();
1579 :
1580 894 : test();
1581 894 : test();
1582 894 : IF( EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) || EQ_16( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) || EQ_16( st_ivas->renderer_type, RENDERER_STEREO_PARAMETRIC ) )
1583 : {
1584 105 : tc_nchan_allocate_new = BINAURAL_CHANNELS * 2;
1585 105 : move16();
1586 105 : tc_nchan_full_new = tc_nchan_allocate_new;
1587 105 : move16();
1588 : }
1589 :
1590 894 : test();
1591 894 : test();
1592 894 : IF( EQ_16( st_ivas->mc_mode, MC_MODE_PARAMMC ) && NE_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_MONO ) && NE_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_STEREO ) )
1593 : {
1594 286 : tc_nchan_full_new = 0;
1595 286 : move16();
1596 : }
1597 608 : ELSE IF( EQ_16( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
1598 : {
1599 5 : tc_nchan_allocate_new = MC_PARAMUPMIX_MAX_INPUT_CHANS;
1600 5 : move16();
1601 5 : tc_buffer_mode_new = TC_BUFFER_MODE_RENDERER;
1602 5 : move32();
1603 5 : test();
1604 5 : IF( EQ_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_STEREO ) || EQ_16( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_MONO ) )
1605 : {
1606 1 : tc_buffer_mode_new = TC_BUFFER_MODE_BUFFER;
1607 1 : move32();
1608 1 : tc_nchan_tc_new = st_ivas->hDecoderConfig->nchan_out;
1609 1 : move16();
1610 1 : tc_nchan_allocate_new = tc_nchan_tc_new;
1611 1 : move16();
1612 : }
1613 5 : tc_nchan_full_new = tc_nchan_allocate_new;
1614 5 : move16();
1615 : }
1616 :
1617 : /* reconfigure buffer */
1618 894 : test();
1619 894 : test();
1620 894 : test();
1621 894 : test();
1622 894 : IF( NE_16( hTcBuffer->tc_buffer_mode, tc_buffer_mode_new ) || NE_16( hTcBuffer->nchan_transport_jbm, tc_nchan_tc_new ) ||
1623 : NE_16( hTcBuffer->nchan_buffer_full, tc_nchan_full_new ) || NE_16( hTcBuffer->nchan_transport_internal, tc_nchan_allocate_new ) ||
1624 : NE_16( tc_granularity_new, hTcBuffer->n_samples_granularity ) )
1625 : {
1626 690 : IF( NE_32( ( error = ivas_jbm_dec_tc_buffer_reconfigure_fx( st_ivas, tc_buffer_mode_new, tc_nchan_tc_new, tc_nchan_allocate_new, tc_nchan_full_new, tc_granularity_new ) ), IVAS_ERR_OK ) )
1627 : {
1628 0 : return error;
1629 : }
1630 : }
1631 : /* transfer subframe info from central tc buffer to ParamMC or McMASA (DirAC) */
1632 894 : IF( st_ivas->hSpatParamRendCom != NULL )
1633 : {
1634 204 : st_ivas->hSpatParamRendCom->nb_subframes = st_ivas->hTcBuffer->nb_subframes;
1635 204 : move16();
1636 204 : st_ivas->hSpatParamRendCom->subframes_rendered = st_ivas->hTcBuffer->subframes_rendered;
1637 204 : move16();
1638 204 : st_ivas->hSpatParamRendCom->num_slots = st_ivas->hTcBuffer->num_slots;
1639 204 : move16();
1640 204 : st_ivas->hSpatParamRendCom->slots_rendered = st_ivas->hTcBuffer->slots_rendered;
1641 204 : move16();
1642 204 : Copy( st_ivas->hTcBuffer->subframe_nbslots, st_ivas->hSpatParamRendCom->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
1643 : }
1644 690 : ELSE IF( st_ivas->hParamMC != NULL )
1645 : {
1646 288 : st_ivas->hParamMC->nb_subframes = st_ivas->hTcBuffer->nb_subframes;
1647 288 : move16();
1648 288 : st_ivas->hParamMC->subframes_rendered = st_ivas->hTcBuffer->subframes_rendered;
1649 288 : move16();
1650 288 : st_ivas->hParamMC->num_slots = st_ivas->hTcBuffer->num_slots;
1651 288 : move16();
1652 288 : st_ivas->hParamMC->slots_rendered = st_ivas->hTcBuffer->slots_rendered;
1653 288 : move16();
1654 288 : Copy( st_ivas->hTcBuffer->subframe_nbslots, st_ivas->hParamMC->subframe_nbslots, MAX_JBM_SUBFRAMES_5MS );
1655 : }
1656 : }
1657 :
1658 :
1659 : /*-----------------------------------------------------------------*
1660 : * floating-point output audio buffers
1661 : *-----------------------------------------------------------------*/
1662 :
1663 894 : nchan_out_buff = ivas_get_nchan_buffers_dec_ivas_fx( st_ivas, -1, -1 );
1664 :
1665 894 : IF( NE_32( ( error = ivas_output_buff_dec_fx( st_ivas->p_output_fx, nchan_out_buff_old, nchan_out_buff ) ), IVAS_ERR_OK ) )
1666 : {
1667 0 : return error;
1668 : }
1669 894 : return error;
1670 : }
|