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 "options.h"
34 : #include "ivas_cnst.h"
35 : #include "ivas_prot_fx.h"
36 : #include "prot_fx.h"
37 : #include <stdint.h>
38 : #include "wmc_auto.h"
39 :
40 :
41 : /*-------------------------------------------------------------------*
42 : * ivas_corecoder_dec_reconfig()
43 : *
44 : * Allocate, initialize, and configure SCE/CPE/MCT handles in case of bitrate switching
45 : *-------------------------------------------------------------------*/
46 :
47 4858 : ivas_error ivas_corecoder_dec_reconfig_fx(
48 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
49 : const Word16 nSCE_old, /* i : number of SCEs in previous frame */
50 : Word16 nCPE_old, /* i : number of CPEs in previous frame */
51 : const Word16 nchan_transport_old, /* i : number of TCs in previous frame */
52 : const Word16 sba_dirac_stereo_flag_old, /* i : signal stereo rendering using DFT upmix in previous frame */
53 : const Word32 brate_SCE, /* i : bitrate to be set for the SCEs */
54 : const Word32 brate_CPE /* i : bitrate to be set for the CPEs */
55 : )
56 : {
57 : Word16 n, sce_id, cpe_id, output_frame;
58 : Word16 nSCE_existing, nCPE_existing;
59 : Word32 ivas_total_brate;
60 : Word16 nchan_transport_real;
61 : MC_MODE last_mc_mode;
62 : DECODER_CONFIG_HANDLE hDecoderConfig;
63 : ivas_error error;
64 : Word16 prev_bfi;
65 : Decoder_State *st0;
66 :
67 : /*-----------------------------------------------------------------*
68 : * Initialization
69 : *-----------------------------------------------------------------*/
70 :
71 4858 : hDecoderConfig = st_ivas->hDecoderConfig;
72 4858 : ivas_total_brate = hDecoderConfig->ivas_total_brate;
73 4858 : move32();
74 4858 : output_frame = extract_l( Mpy_32_16_1( hDecoderConfig->output_Fs, INV_FRAME_PER_SEC_Q15 ) );
75 4858 : error = IVAS_ERR_OK;
76 4858 : move32();
77 :
78 4858 : IF( EQ_16( st_ivas->ivas_format, MC_FORMAT ) )
79 : {
80 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 */
81 : }
82 : ELSE
83 : {
84 3964 : last_mc_mode = MC_MODE_NONE;
85 3964 : move32();
86 : }
87 :
88 4858 : nchan_transport_real = st_ivas->nchan_transport;
89 4858 : move16();
90 :
91 4858 : IF( EQ_16( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
92 : {
93 84 : nchan_transport_real = add( nchan_transport_real, st_ivas->nchan_ism );
94 : }
95 : /*-----------------------------------------------------------------*
96 : * Allocate, initialize, and configure SCE/CPE/MCT handles
97 : *-----------------------------------------------------------------*/
98 :
99 : /* remove dummy CPE element for DFT stereo-like upmix */
100 4858 : test();
101 4858 : test();
102 4858 : test();
103 4858 : test();
104 4858 : test();
105 4858 : test();
106 4858 : test();
107 4858 : test();
108 4858 : test();
109 4858 : test();
110 4858 : IF( ( EQ_16( st_ivas->ivas_format, SBA_FORMAT ) && sba_dirac_stereo_flag_old && EQ_16( nchan_transport_old, 1 ) && ( !st_ivas->sba_dirac_stereo_flag || GT_16( st_ivas->nchan_transport, 1 ) ) ) ||
111 : ( EQ_16( st_ivas->ivas_format, MC_FORMAT ) && EQ_16( last_mc_mode, MC_MODE_MCMASA ) && sba_dirac_stereo_flag_old && EQ_16( nchan_transport_old, 1 ) && ( !st_ivas->sba_dirac_stereo_flag || GT_16( st_ivas->nchan_transport, 1 ) ) ) )
112 : {
113 57 : st_ivas->hCPE[0]->hCoreCoder[0] = NULL;
114 57 : st_ivas->hCPE[0]->hCoreCoder[1] = NULL;
115 57 : destroy_cpe_dec( st_ivas->hCPE[0] );
116 57 : st_ivas->hCPE[0] = NULL;
117 :
118 57 : IF( st_ivas->hSCE[0] != NULL )
119 : {
120 57 : free( st_ivas->hSCE[0]->save_synth_fx );
121 57 : st_ivas->hSCE[0]->save_synth_fx = NULL;
122 :
123 57 : free( st_ivas->hSCE[0]->save_hb_synth_fx );
124 57 : st_ivas->hSCE[0]->save_hb_synth_fx = NULL;
125 : }
126 : }
127 :
128 4858 : test();
129 4858 : test();
130 4858 : IF( EQ_16( nchan_transport_real, nchan_transport_old ) && EQ_16( st_ivas->nSCE, nSCE_old ) && EQ_16( st_ivas->nCPE, nCPE_old ) )
131 : {
132 4004 : FOR( sce_id = 0; sce_id < st_ivas->nSCE; sce_id++ )
133 : {
134 2576 : st_ivas->hSCE[sce_id]->element_brate = brate_SCE;
135 2576 : move32();
136 2576 : st_ivas->hSCE[sce_id]->hCoreCoder[0]->total_brate = st_ivas->hSCE[sce_id]->element_brate; /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
137 2576 : move32();
138 : }
139 :
140 2442 : FOR( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
141 : {
142 1014 : st_ivas->hCPE[cpe_id]->element_brate = brate_CPE;
143 1014 : move32();
144 :
145 : /* prepare bitstream buffers */
146 3042 : FOR( n = 0; n < CPE_CHANNELS; n++ )
147 : {
148 2028 : IF( GT_16( st_ivas->nCPE, 1 ) )
149 : {
150 1384 : st_ivas->hCPE[cpe_id]->hCoreCoder[n]->total_brate = st_ivas->hCPE[cpe_id]->element_brate; /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
151 1384 : move32();
152 : }
153 : ELSE
154 : {
155 644 : st_ivas->hCPE[cpe_id]->hCoreCoder[n]->total_brate = L_shr( st_ivas->hCPE[cpe_id]->element_brate, 1 ); /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
156 644 : move32();
157 : }
158 : }
159 : }
160 :
161 1428 : IF( GT_16( st_ivas->nCPE, 1 ) )
162 : {
163 270 : IF( NE_32( ( error = mct_dec_reconfigure_fx( st_ivas, 0 ) ), IVAS_ERR_OK ) )
164 : {
165 0 : return error;
166 : }
167 : }
168 : }
169 : ELSE
170 : {
171 3430 : IF( nSCE_old > 0 )
172 : {
173 1776 : st0 = st_ivas->hSCE[0]->hCoreCoder[0];
174 : }
175 : ELSE
176 : {
177 1654 : st0 = st_ivas->hCPE[0]->hCoreCoder[0];
178 : }
179 3430 : prev_bfi = st0->prev_bfi;
180 3430 : move16();
181 3430 : nSCE_existing = s_min( nSCE_old, st_ivas->nSCE );
182 3430 : nCPE_existing = s_min( nCPE_old, st_ivas->nCPE );
183 3430 : st_ivas->nCPE_old = nCPE_existing;
184 3430 : move16();
185 :
186 : /* destroy superfluous core coder elements */
187 5488 : FOR( sce_id = st_ivas->nSCE; sce_id < nSCE_old; sce_id++ )
188 : {
189 2058 : destroy_sce_dec( st_ivas->hSCE[sce_id] );
190 2058 : st_ivas->hSCE[sce_id] = NULL;
191 : }
192 :
193 3430 : test();
194 3430 : test();
195 3430 : IF( sba_dirac_stereo_flag_old && ( nCPE_old == 0 ) && st_ivas->hCPE[0] )
196 : {
197 27 : st_ivas->hCPE[0]->hCoreCoder[0] = NULL; /* this has been deallocated as part of the SCE it actually belongs to */
198 27 : destroy_cpe_dec( st_ivas->hCPE[0] );
199 27 : st_ivas->hCPE[0] = NULL;
200 : }
201 :
202 5122 : FOR( cpe_id = st_ivas->nCPE; cpe_id < nCPE_old; cpe_id++ )
203 : {
204 : /* don't deallocate first CPE in case of mono/stereo output of 1 TC SBA, only deallocate core coder */
205 1692 : test();
206 1692 : test();
207 1692 : IF( ( cpe_id == 0 ) && st_ivas->sba_dirac_stereo_flag && sba_dirac_stereo_flag_old )
208 : {
209 156 : FOR( n = 0; n < CPE_CHANNELS; n++ )
210 : {
211 104 : IF( st_ivas->hCPE[cpe_id]->hCoreCoder[n] != NULL )
212 : {
213 104 : destroy_core_dec_fx( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
214 :
215 104 : free( st_ivas->hCPE[cpe_id]->hCoreCoder[n] );
216 104 : st_ivas->hCPE[cpe_id]->hCoreCoder[n] = NULL;
217 : }
218 : }
219 52 : CONTINUE;
220 : }
221 1640 : destroy_cpe_dec( st_ivas->hCPE[cpe_id] );
222 1640 : st_ivas->hCPE[cpe_id] = NULL;
223 : }
224 :
225 : /* the CPE-internal settings depend from ivas_format and mc_mode, so clean-up when switching between mc_modes */
226 3430 : test();
227 3430 : test();
228 3430 : test();
229 3430 : IF( EQ_16( st_ivas->ivas_format, MC_FORMAT ) && NE_16( st_ivas->mc_mode, last_mc_mode ) && ( EQ_16( st_ivas->mc_mode, MC_MODE_MCMASA ) || EQ_16( last_mc_mode, MC_MODE_MCMASA ) ) )
230 : {
231 395 : FOR( cpe_id = 0; cpe_id < nCPE_existing; cpe_id++ )
232 : {
233 26 : destroy_cpe_dec( st_ivas->hCPE[cpe_id] );
234 26 : st_ivas->hCPE[cpe_id] = NULL;
235 : }
236 369 : nCPE_old = 0;
237 369 : move16();
238 369 : nCPE_existing = s_min( nCPE_old, st_ivas->nCPE );
239 369 : st_ivas->nCPE_old = nCPE_existing;
240 369 : move16();
241 : }
242 3430 : test();
243 3430 : IF( LE_16( st_ivas->nCPE, 1 ) && st_ivas->hMCT != NULL )
244 : {
245 419 : ivas_mct_dec_close( &st_ivas->hMCT );
246 : }
247 :
248 : /* special case, if we have MCT now and had a single CPE before, remove the MDCT Stereo handles from the first CPE*/
249 3430 : test();
250 3430 : IF( GT_16( st_ivas->nCPE, 1 ) && EQ_16( nCPE_old, 1 ) )
251 : {
252 271 : free( st_ivas->hCPE[0]->hStereoMdct );
253 271 : st_ivas->hCPE[0]->hStereoMdct = NULL;
254 : }
255 :
256 4512 : FOR( sce_id = 0; sce_id < nSCE_existing; sce_id++ )
257 : {
258 1082 : st_ivas->hSCE[sce_id]->element_brate = brate_SCE;
259 1082 : move32();
260 1082 : st_ivas->hSCE[sce_id]->hCoreCoder[0]->total_brate = st_ivas->hSCE[sce_id]->element_brate; /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
261 1082 : move32();
262 : }
263 5479 : FOR( ; sce_id < st_ivas->nSCE; sce_id++ )
264 : {
265 2049 : IF( NE_32( ( error = create_sce_dec( st_ivas, sce_id, brate_SCE ) ), IVAS_ERR_OK ) )
266 : {
267 0 : return error;
268 : }
269 2049 : st_ivas->hSCE[sce_id]->hCoreCoder[0]->prev_bfi = prev_bfi;
270 2049 : move16();
271 : }
272 3430 : test();
273 3430 : test();
274 3430 : test();
275 3430 : IF( st_ivas->sba_dirac_stereo_flag && sba_dirac_stereo_flag_old && EQ_16( st_ivas->nchan_transport, 1 ) && ( nSCE_old == 0 ) )
276 : {
277 52 : st_ivas->hCPE[0]->hCoreCoder[0] = st_ivas->hSCE[0]->hCoreCoder[0]; /* don't allocate unnecessary core coder, simply point to core coder of SCE element */
278 52 : st_ivas->hCPE[0]->hCoreCoder[1] = NULL;
279 : }
280 :
281 5451 : FOR( cpe_id = 0; cpe_id < nCPE_existing; cpe_id++ )
282 : {
283 2021 : st_ivas->hCPE[cpe_id]->element_brate = brate_CPE;
284 2021 : move32();
285 :
286 : /* prepare bitstream buffers */
287 6063 : FOR( n = 0; n < CPE_CHANNELS; n++ )
288 : {
289 4042 : IF( GT_16( st_ivas->nCPE, 1 ) )
290 : {
291 994 : st_ivas->hCPE[cpe_id]->hCoreCoder[n]->total_brate = st_ivas->hCPE[cpe_id]->element_brate; /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
292 994 : move32();
293 : }
294 : ELSE
295 : {
296 3048 : st_ivas->hCPE[cpe_id]->hCoreCoder[n]->total_brate = L_shr( st_ivas->hCPE[cpe_id]->element_brate, 1 ); /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
297 3048 : move32();
298 : }
299 : }
300 : }
301 5222 : FOR( ; cpe_id < st_ivas->nCPE; cpe_id++ )
302 : {
303 1792 : test();
304 1792 : IF( EQ_16( st_ivas->mc_mode, MC_MODE_MCMASA ) && st_ivas->hOutSetup.separateChannelEnabled )
305 : {
306 12 : st_ivas->element_mode_init = IVAS_CPE_MDCT;
307 12 : move16();
308 : }
309 :
310 1792 : IF( NE_32( ( error = create_cpe_dec( st_ivas, cpe_id, brate_CPE ) ), IVAS_ERR_OK ) )
311 : {
312 0 : return error;
313 : }
314 1792 : st_ivas->hCPE[cpe_id]->hCoreCoder[0]->prev_bfi = prev_bfi;
315 1792 : move16();
316 1792 : st_ivas->hCPE[cpe_id]->hCoreCoder[1]->prev_bfi = prev_bfi;
317 1792 : move16();
318 : }
319 :
320 3430 : test();
321 3430 : test();
322 3430 : IF( GT_16( st_ivas->nCPE, 1 ) && LE_16( nCPE_old, 1 ) )
323 : {
324 698 : IF( EQ_16( nCPE_old, 1 ) )
325 : {
326 : /* set correct nominal bitrates and igf config already here, needed for the correct init of the MDCT Stereo handles for MCT */
327 813 : FOR( n = 0; n < CPE_CHANNELS; n++ )
328 : {
329 542 : st_ivas->hCPE[0]->hCoreCoder[n]->total_brate = st_ivas->hCPE[0]->element_brate;
330 542 : move32();
331 542 : st_ivas->hCPE[0]->hCoreCoder[n]->bits_frame_nominal = extract_l( Mpy_32_16_1( st_ivas->hCPE[0]->element_brate, INV_FRAME_PER_SEC_Q15 ) );
332 542 : move16();
333 542 : st_ivas->hCPE[0]->hCoreCoder[n]->igf = 0;
334 542 : move16();
335 : }
336 : }
337 :
338 698 : IF( NE_32( ( error = create_mct_dec_fx( st_ivas ) ), IVAS_ERR_OK ) )
339 : {
340 0 : return error;
341 : }
342 : }
343 2732 : ELSE IF( st_ivas->hMCT != NULL && GT_16( st_ivas->nCPE, 1 ) )
344 : {
345 111 : IF( NE_32( ( error = mct_dec_reconfigure_fx( st_ivas, (UWord16) NE_16( st_ivas->nchan_transport, nchan_transport_old ) ) ), IVAS_ERR_OK ) )
346 : {
347 0 : return error;
348 : }
349 : }
350 :
351 : /* special case, if we have a single CPE and had MCT before we need to init the MDCT stereo handles here */
352 3430 : test();
353 3430 : IF( EQ_16( st_ivas->nCPE, 1 ) && GT_16( nCPE_old, 1 ) )
354 : {
355 279 : IF( ( st_ivas->hCPE[st_ivas->nCPE - 1]->hStereoMdct = (STEREO_MDCT_DEC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_DEC_DATA ) ) ) == NULL )
356 : {
357 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
358 : }
359 :
360 279 : st_ivas->hCPE[st_ivas->nCPE - 1]->hStereoMdct->use_itd = 0;
361 279 : move16();
362 279 : st_ivas->hCPE[st_ivas->nCPE - 1]->hStereoMdct->reverse_dmx = 0;
363 279 : move16();
364 279 : st_ivas->hCPE[st_ivas->nCPE - 1]->hStereoMdct->smooth_ratio_fx = ONE_IN_Q26; /* Q26 */
365 279 : move32();
366 279 : st_ivas->hCPE[st_ivas->nCPE - 1]->hStereoMdct->lastCoh_fx = ONE_IN_Q14; /* Q14 */
367 279 : move16();
368 279 : set16_fx( st_ivas->hCPE[st_ivas->nCPE - 1]->hStereoMdct->prev_ms_mask[0], 0, MAX_SFB );
369 279 : set16_fx( st_ivas->hCPE[st_ivas->nCPE - 1]->hStereoMdct->prev_ms_mask[1], 0, MAX_SFB );
370 279 : st_ivas->hCPE[st_ivas->nCPE - 1]->hStereoMdct->mdct_stereo_mode[0] = SMDCT_DUAL_MONO;
371 279 : move16();
372 279 : st_ivas->hCPE[st_ivas->nCPE - 1]->hStereoMdct->mdct_stereo_mode[1] = SMDCT_DUAL_MONO;
373 279 : move16();
374 279 : st_ivas->hCPE[st_ivas->nCPE - 1]->hStereoMdct->IGFStereoMode[0] = -1;
375 279 : move16();
376 279 : st_ivas->hCPE[st_ivas->nCPE - 1]->hStereoMdct->IGFStereoMode[1] = -1;
377 279 : move16();
378 837 : FOR( n = 0; n < CPE_CHANNELS; n++ )
379 : {
380 : /* reset mct_chan_mode */
381 558 : st_ivas->hCPE[0]->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
382 558 : move32();
383 : }
384 : }
385 : }
386 :
387 : /* create dummy CPE element for DFT stereo-like upmix */
388 4858 : test();
389 4858 : test();
390 4858 : test();
391 4858 : test();
392 4858 : test();
393 4858 : test();
394 4858 : IF( ( EQ_16( st_ivas->ivas_format, SBA_FORMAT ) && st_ivas->sba_dirac_stereo_flag && !sba_dirac_stereo_flag_old ) ||
395 : ( EQ_16( st_ivas->ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_MCMASA ) && st_ivas->sba_dirac_stereo_flag && !sba_dirac_stereo_flag_old ) )
396 : {
397 : /* if at least one CPE is already available, only allocate DFT Stereo struct */
398 28 : IF( ( st_ivas->nCPE > 0 ) )
399 : {
400 0 : IF( NE_32( ( error = stereo_dft_dec_create_fx( &( st_ivas->hCPE[0]->hStereoDft ), st_ivas->hCPE[0]->element_brate, st_ivas->hDecoderConfig->output_Fs, st_ivas->sba_dirac_stereo_flag, st_ivas->nchan_transport ) ), IVAS_ERR_OK ) )
401 : {
402 0 : return error;
403 : }
404 : }
405 : /* otherwise create extra dummy CPE */
406 : ELSE
407 : {
408 : Word32 quo, rem;
409 28 : iDiv_and_mod_32( ivas_total_brate, add( st_ivas->nSCE, st_ivas->nCPE ), &quo, &rem, 0 );
410 28 : IF( NE_32( ( error = create_cpe_dec( st_ivas, 0, quo ) ), IVAS_ERR_OK ) )
411 : {
412 0 : return error;
413 : }
414 : }
415 :
416 28 : set32_fx( st_ivas->hCPE[0]->hStereoDft->buff_LBTCX_mem_fx, 0, NS2SA( 16000, STEREO_DFT32MS_OVL_NS ) );
417 :
418 28 : st_ivas->hCPE[0]->hCoreCoder[0] = st_ivas->hSCE[0]->hCoreCoder[0]; /* don't allocate unnecessary core coder, simply point to core coder of SCE element */
419 28 : st_ivas->hCPE[0]->hCoreCoder[1] = NULL;
420 :
421 28 : IF( st_ivas->hSCE[0]->save_synth_fx == NULL )
422 : {
423 2 : IF( ( st_ivas->hSCE[0]->save_synth_fx = (Word32 *) malloc( sizeof( *( st_ivas->hSCE[0]->save_synth_fx ) ) * output_frame ) ) == NULL )
424 : {
425 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for stereo output\n" ) );
426 : }
427 2 : set32_fx( st_ivas->hSCE[0]->save_synth_fx, 0, output_frame );
428 2 : st_ivas->hSCE[0]->q_save_synth_fx = 0;
429 2 : move16();
430 : }
431 :
432 28 : IF( st_ivas->hSCE[0]->save_hb_synth_fx == NULL )
433 : {
434 2 : IF( ( st_ivas->hSCE[0]->save_hb_synth_fx = (Word32 *) malloc( sizeof( *( st_ivas->hSCE[0]->save_hb_synth_fx ) ) * output_frame ) ) == NULL )
435 : {
436 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate HB memory for stereo output\n" ) );
437 : }
438 2 : set32_fx( st_ivas->hSCE[0]->save_hb_synth_fx, 0, output_frame );
439 : }
440 : }
441 :
442 : /*-----------------------------------------------------------------*
443 : * Set CNA/CNG flags
444 : *-----------------------------------------------------------------*/
445 :
446 4858 : test();
447 4858 : IF( EQ_16( st_ivas->ivas_format, SBA_FORMAT ) || EQ_16( st_ivas->ivas_format, MASA_FORMAT ) )
448 : {
449 1414 : ivas_sba_set_cna_cng_flag( st_ivas );
450 : }
451 :
452 : /* special case, if the decoder goes from 1TC DTX to 2TC active frame (in case the bitstream started with an SBA SID frame), allocate DTX memories */
453 4858 : test();
454 4858 : IF( LE_32( hDecoderConfig->last_ivas_total_brate, IVAS_SID_5k2 ) && GE_16( st_ivas->nCPE, 1 ) )
455 : {
456 6 : IF( NE_32( ( error = initMdctStereoDtxData_fx( st_ivas->hCPE[0] ) ), IVAS_ERR_OK ) )
457 : {
458 0 : return error;
459 : }
460 : }
461 :
462 4858 : return error;
463 : }
464 :
465 :
466 : /*-------------------------------------------------------------------*
467 : * ivas_hp20_dec_reconfig()
468 : *
469 : * Allocate, initialize, and configure HP20 memory handles in case of bitrate switching
470 : *-------------------------------------------------------------------*/
471 :
472 4858 : ivas_error ivas_hp20_dec_reconfig_fx(
473 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
474 : const Word16 nchan_hp20_old /* i : number of HP20 filters in previous frame */
475 : )
476 : {
477 : Word16 i, nchan_hp20;
478 : Word32 **old_mem_hp20_out_fx;
479 : ivas_error error;
480 :
481 4858 : error = IVAS_ERR_OK;
482 4858 : move32();
483 :
484 : /*-----------------------------------------------------------------*
485 : * HP20 memories
486 : *-----------------------------------------------------------------*/
487 :
488 4858 : nchan_hp20 = getNumChanSynthesis( st_ivas );
489 :
490 4858 : IF( GT_16( nchan_hp20, nchan_hp20_old ) )
491 : {
492 : /* save old mem_hp_20 pointer */
493 1652 : old_mem_hp20_out_fx = st_ivas->mem_hp20_out_fx;
494 1652 : st_ivas->mem_hp20_out_fx = NULL;
495 :
496 1652 : IF( ( st_ivas->mem_hp20_out_fx = (Word32 **) malloc( nchan_hp20 * sizeof( Word32 * ) ) ) == NULL )
497 : {
498 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
499 : }
500 :
501 4730 : FOR( i = 0; i < nchan_hp20_old; i++ )
502 : {
503 3078 : st_ivas->mem_hp20_out_fx[i] = old_mem_hp20_out_fx[i];
504 3078 : move32();
505 3078 : old_mem_hp20_out_fx[i] = NULL;
506 : }
507 : /* create additional hp20 memories */
508 5592 : FOR( ; i < nchan_hp20; i++ )
509 : {
510 3940 : IF( ( st_ivas->mem_hp20_out_fx[i] = (Word32 *) malloc( ( L_HP20_MEM + 2 ) * sizeof( Word32 ) ) ) == NULL )
511 : {
512 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
513 : }
514 :
515 3940 : set32_fx( st_ivas->mem_hp20_out_fx[i], 0, L_HP20_MEM + 2 );
516 : }
517 :
518 1652 : free( old_mem_hp20_out_fx );
519 1652 : old_mem_hp20_out_fx = NULL;
520 : }
521 3206 : ELSE IF( LT_16( nchan_hp20, nchan_hp20_old ) )
522 : {
523 : /* save old mem_hp_20 pointer */
524 1611 : old_mem_hp20_out_fx = st_ivas->mem_hp20_out_fx;
525 1611 : st_ivas->mem_hp20_out_fx = NULL;
526 :
527 1611 : IF( ( st_ivas->mem_hp20_out_fx = (Word32 **) malloc( nchan_hp20 * sizeof( Word32 * ) ) ) == NULL )
528 : {
529 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
530 : }
531 :
532 4601 : FOR( i = 0; i < nchan_hp20; i++ )
533 : {
534 2990 : st_ivas->mem_hp20_out_fx[i] = old_mem_hp20_out_fx[i];
535 2990 : move32();
536 2990 : old_mem_hp20_out_fx[i] = NULL;
537 : }
538 : /* remove superfluous hp20 memories */
539 5428 : FOR( ; i < nchan_hp20_old; i++ )
540 : {
541 3817 : free( old_mem_hp20_out_fx[i] );
542 3817 : old_mem_hp20_out_fx[i] = NULL;
543 : }
544 :
545 1611 : free( old_mem_hp20_out_fx );
546 1611 : old_mem_hp20_out_fx = NULL;
547 : }
548 :
549 4858 : return error;
550 : }
551 :
552 : /*-------------------------------------------------------------------*
553 : * ivas_cldfb_dec_reconfig()
554 : *
555 : * Allocate, initialize, and configure CLDFB handles in case of bitrate switching
556 : *-------------------------------------------------------------------*/
557 6175 : ivas_error ivas_cldfb_dec_reconfig_fx(
558 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
559 : const Word16 nchan_transport_old, /* i : number of TCs in previous frame */
560 : Word16 numCldfbAnalyses_old, /* i : number of CLDFB analysis instances in previous frame */
561 : const Word16 numCldfbSyntheses_old /* i : number of CLDFB synthesis instances in previous frame */
562 : )
563 : {
564 : Word16 i, numCldfbAnalyses, numCldfbSyntheses;
565 : DECODER_CONFIG_HANDLE hDecoderConfig;
566 : ivas_error error;
567 :
568 6175 : hDecoderConfig = st_ivas->hDecoderConfig;
569 :
570 6175 : ivas_init_dec_get_num_cldfb_instances_ivas_fx( st_ivas, &numCldfbAnalyses, &numCldfbSyntheses );
571 :
572 : /* special case, if there was one transport channel in the previous frame and more than one in the current frame,
573 : remove the second CLDFB here, it was for CNA/CNG */
574 6175 : test();
575 6175 : test();
576 6175 : test();
577 6175 : IF( EQ_16( st_ivas->ivas_format, SBA_FORMAT ) && EQ_16( nchan_transport_old, 1 ) && EQ_16( numCldfbAnalyses_old, 2 ) && GT_16( st_ivas->nchan_transport, 1 ) )
578 : {
579 0 : deleteCldfb_ivas_fx( &( st_ivas->cldfbAnaDec[1] ) );
580 0 : numCldfbAnalyses_old = sub( numCldfbAnalyses_old, 1 );
581 : }
582 : /* resample CLDFB analysis instances */
583 17077 : FOR( i = 0; i < s_min( numCldfbAnalyses, numCldfbAnalyses_old ); i++ )
584 : {
585 10902 : IF( NE_32( L_mult0( extract_l( L_mult0( st_ivas->cldfbAnaDec[i]->no_channels, st_ivas->cldfbAnaDec[i]->no_col ) ), FRAMES_PER_SEC ), hDecoderConfig->output_Fs ) )
586 : {
587 492 : resampleCldfb_ivas_fx( st_ivas->cldfbAnaDec[i], hDecoderConfig->output_Fs );
588 : }
589 : }
590 :
591 : /* Analysis*/
592 6175 : IF( GT_16( numCldfbAnalyses_old, numCldfbAnalyses ) )
593 : {
594 : /* delete superfluous CLDFB synthesis instances */
595 4078 : FOR( i = numCldfbAnalyses; i < numCldfbAnalyses_old; i++ )
596 : {
597 2909 : deleteCldfb_ivas_fx( &( st_ivas->cldfbAnaDec[i] ) );
598 : }
599 : }
600 5006 : ELSE IF( LT_16( numCldfbAnalyses_old, numCldfbAnalyses ) )
601 : {
602 : /* create additional CLDFB synthesis instances */
603 3868 : FOR( i = numCldfbAnalyses_old; i < numCldfbAnalyses; i++ )
604 : {
605 2706 : IF( NE_32( ( error = openCldfb_ivas_fx( &( st_ivas->cldfbAnaDec[i] ), CLDFB_ANALYSIS, hDecoderConfig->output_Fs, CLDFB_PROTOTYPE_5_00MS, DEC ) ), IVAS_ERR_OK ) )
606 : {
607 0 : return error;
608 : }
609 : }
610 : }
611 : /* Synthesis */
612 6175 : IF( GT_16( numCldfbSyntheses_old, numCldfbSyntheses ) )
613 : {
614 : /* delete superfluous CLDFB synthesis instances */
615 4342 : FOR( i = numCldfbSyntheses; i < numCldfbSyntheses_old; i++ )
616 : {
617 3739 : deleteCldfb_ivas_fx( &( st_ivas->cldfbSynDec[i] ) );
618 : }
619 : }
620 5572 : ELSE IF( LT_16( numCldfbSyntheses_old, numCldfbSyntheses ) )
621 : {
622 : /* create additional CLDFB synthesis instances */
623 4346 : FOR( i = numCldfbSyntheses_old; i < numCldfbSyntheses; i++ )
624 : {
625 3746 : IF( NE_32( ( error = openCldfb_ivas_fx( &( st_ivas->cldfbSynDec[i] ), CLDFB_SYNTHESIS, hDecoderConfig->output_Fs, CLDFB_PROTOTYPE_5_00MS, DEC ) ), IVAS_ERR_OK ) )
626 : {
627 0 : return error;
628 : }
629 : }
630 : }
631 : /* CLDFB Interpolation weights */
632 6175 : test();
633 6175 : test();
634 6175 : test();
635 6175 : test();
636 6175 : test();
637 6175 : IF( EQ_16( st_ivas->ivas_format, SBA_FORMAT ) && ( NE_16( numCldfbAnalyses_old, numCldfbAnalyses ) || NE_16( numCldfbSyntheses_old, numCldfbSyntheses ) || NE_16( nchan_transport_old, st_ivas->nchan_transport ) ) && ( numCldfbAnalyses != 0 ) && ( numCldfbSyntheses != 0 ) )
638 : {
639 659 : ivas_spar_get_cldfb_gains_fx( st_ivas->hSpar, st_ivas->cldfbAnaDec[0], st_ivas->cldfbSynDec[0], hDecoderConfig );
640 325559 : FOR( i = 0; i < st_ivas->cldfbAnaDec[0]->cldfb_state_length; i++ )
641 : {
642 324900 : st_ivas->cldfbAnaDec[0]->cldfb_state_fx[i] = L_shr( st_ivas->cldfbAnaDec[0]->cldfb_state_fx[i], ( Q27 - Q11 ) ); // Scaling down from 27 to 11
643 324900 : move32();
644 : }
645 659 : st_ivas->cldfbAnaDec[0]->Q_cldfb_state = Q11;
646 659 : move16();
647 361659 : FOR( i = 0; i < st_ivas->cldfbSynDec[0]->cldfb_state_length; i++ )
648 : {
649 361000 : st_ivas->cldfbSynDec[0]->cldfb_state_fx[i] = L_shr( st_ivas->cldfbSynDec[0]->cldfb_state_fx[i], ( Q21 - Q11 ) ); // Scaling down from 21 to 11
650 361000 : move32();
651 : }
652 659 : st_ivas->cldfbSynDec[0]->Q_cldfb_state = Q11;
653 659 : move16();
654 : }
655 6175 : return IVAS_ERR_OK;
656 : }
|