Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : #include <assert.h>
34 : #include <stdint.h>
35 : #include "options.h"
36 : #include <math.h>
37 : #include "cnst.h"
38 : #include "ivas_cnst.h"
39 : #include "prot.h"
40 : #include "ivas_prot.h"
41 : #ifdef DEBUGGING
42 : #include "debug.h"
43 : #endif
44 : #include "wmc_auto.h"
45 :
46 :
47 : /*-----------------------------------------------------------------------*
48 : * Local function prototypes
49 : *-----------------------------------------------------------------------*/
50 :
51 : static ivas_error ivas_mc_enc_reconfig( Encoder_Struct *st_ivas, const int16_t last_mc_mode );
52 :
53 :
54 : /*-------------------------------------------------------------------*
55 : * set_mct_enc_params()
56 : *
57 : * Set hMCT handle parameters
58 : *-------------------------------------------------------------------*/
59 :
60 1394 : static void set_mct_enc_params(
61 : MCT_ENC_HANDLE hMCT, /* i/o: MCT encoder structure */
62 : const int32_t ivas_total_brate, /* i : IVAS total bitrate */
63 : const IVAS_FORMAT ivas_format, /* i : IVAS_format */
64 : const uint16_t b_nchan_change /* i : flag indicating different channel count */
65 : )
66 : {
67 : int16_t n;
68 :
69 1394 : if ( b_nchan_change )
70 : {
71 1105 : hMCT->currBlockDataCnt = 0;
72 :
73 : /*Initialize bits required to signal channel-pair index*/
74 1105 : hMCT->bitsChannelPairIndex = max( 1, (int16_t) ( floorf( ( logf( (float) hMCT->nchan_out_woLFE * ( hMCT->nchan_out_woLFE - 1 ) / 2 - 1 ) * INV_LOG_2 ) ) + 1 ) );
75 :
76 1105 : set_s( hMCT->lowE_ch, 0, MCT_MAX_CHANNELS );
77 :
78 13260 : for ( n = 0; n < MCT_MAX_CHANNELS; n++ )
79 : {
80 12155 : set_f( hMCT->lastxCorrMatrix[n], 0, MCT_MAX_CHANNELS );
81 : }
82 : }
83 :
84 1394 : hMCT->hbr_mct = 0;
85 1394 : if ( ivas_format == SBA_FORMAT && ivas_total_brate >= IVAS_256k )
86 : {
87 219 : hMCT->hbr_mct = 1;
88 : }
89 :
90 1394 : return;
91 : }
92 :
93 : /*-------------------------------------------------------------------*
94 : * map_input_to_cpe_channels()
95 : *
96 : * for MC_MODE map input channels to cpe channels
97 : * mid channel (ch==2) is mapped to last odd-channel CPE
98 : * all channels after LFE are mapped to cpe_id=1 and onwards
99 : * E.g. for 5_1 and for 3 CPEs:
100 : * cpe_id 0: L=data[0] R=data[1]
101 : * cpe_id 1: L=data[4] R=data[5]
102 : * cpe_id 2: L=data[2] (mid) R=NULL
103 : *-------------------------------------------------------------------*/
104 :
105 100939 : static void map_input_to_cpe_channels(
106 : const Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
107 : float *pdata[MAX_INPUT_CHANNELS], /* o : mapped input pointers */
108 : float *data[MCT_MAX_CHANNELS] /* i : input channel data */
109 : )
110 : {
111 : int16_t i, n;
112 : int16_t nchan_transport;
113 :
114 100939 : nchan_transport = st_ivas->nchan_transport;
115 100939 : if ( st_ivas->hEncoderConfig->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC )
116 : {
117 22080 : nchan_transport += st_ivas->hEncoderConfig->nchan_ism;
118 : }
119 100939 : i = 0;
120 :
121 302817 : for ( n = 0; n < LFE_CHANNEL - 1; n++ )
122 : {
123 201878 : pdata[i] = data[n];
124 201878 : i++;
125 : }
126 100939 : if ( st_ivas->hEncoderConfig->ivas_format == MC_FORMAT && ( st_ivas->mc_mode == MC_MODE_MCT || st_ivas->mc_mode == MC_MODE_PARAMUPMIX ) )
127 : {
128 95360 : for ( n = LFE_CHANNEL + 1; n < nchan_transport; n++ )
129 : {
130 68440 : pdata[i] = data[n];
131 68440 : i++;
132 : }
133 26920 : pdata[i] = data[LFE_CHANNEL - 1];
134 : }
135 : else
136 : {
137 258568 : for ( ; n < nchan_transport; n++ )
138 : {
139 184549 : pdata[i] = data[n];
140 184549 : i++;
141 : }
142 : }
143 :
144 : /* odd channel CPE*/
145 100939 : if ( ( nchan_transport < st_ivas->nCPE * CPE_CHANNELS ) || ( ( st_ivas->mc_mode == MC_MODE_MCT || st_ivas->mc_mode == MC_MODE_PARAMUPMIX ) && st_ivas->hMCT->nchan_out_woLFE < st_ivas->nCPE * CPE_CHANNELS ) )
146 :
147 : {
148 58829 : pdata[st_ivas->nCPE * CPE_CHANNELS - 1] = NULL;
149 : }
150 :
151 100939 : return;
152 : }
153 :
154 :
155 : /*-------------------------------------------------------------------*
156 : * ivas_mct_enc()
157 : *
158 : * Multi-Channel Coding Tool (MCT) encoding routine
159 : *-------------------------------------------------------------------*/
160 :
161 100939 : ivas_error ivas_mct_enc(
162 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
163 : float *data[MCT_MAX_CHANNELS], /* i : input signal buffers */
164 : const int16_t input_frame, /* i : input frame length per channel */
165 : const int16_t nb_bits_metadata /* i : number of metadata bits */
166 : )
167 : {
168 : int16_t n, cpe_id;
169 : MCT_ENC_HANDLE hMCT;
170 : CPE_ENC_HANDLE hCPE;
171 : float mdst_spectrum_long[MCT_MAX_BLOCKS][CPE_CHANNELS][L_FRAME48k];
172 : float orig_spectrum_long[MCT_MAX_BLOCKS][CPE_CHANNELS][L_FRAME48k];
173 : int16_t switch_bw;
174 : IVAS_FORMAT ivas_format;
175 : int32_t ivas_total_brate;
176 : ivas_error error;
177 : float *pdata[MAX_INPUT_CHANNELS];
178 :
179 100939 : error = IVAS_ERR_OK;
180 :
181 100939 : push_wmops( "ivas_mct_enc" );
182 :
183 : /* Initialization */
184 100939 : hMCT = st_ivas->hMCT;
185 100939 : hMCT->hBstr = st_ivas->hCPE[0]->hCoreCoder[0]->hBstr; /* pointer to write MCT side bits */
186 :
187 100939 : ivas_format = st_ivas->hEncoderConfig->ivas_format;
188 100939 : ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
189 :
190 : #ifdef DEBUG_FORCE_MCT_CP
191 : if ( ivas_format == MC_FORMAT )
192 : {
193 : assert( !"Debugging switch works only with SBA modes" );
194 : }
195 : #endif
196 :
197 371247 : for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
198 : {
199 810924 : for ( n = 0; n < CPE_CHANNELS; n++ )
200 : {
201 540616 : hCPE = st_ivas->hCPE[cpe_id];
202 540616 : hMCT->p_mdst_spectrum_long[cpe_id][n] = mdst_spectrum_long[cpe_id][n];
203 540616 : hMCT->p_orig_spectrum_long[cpe_id][n] = orig_spectrum_long[cpe_id][n];
204 540616 : hCPE->hCoreCoder[n]->input_bwidth = hCPE->hCoreCoder[n]->last_input_bwidth; /* updated in BWD */
205 540616 : hCPE->hCoreCoder[n]->bwidth = hCPE->hCoreCoder[n]->last_bwidth; /* updated in BWD */
206 : /* reset channel mode from previous state*/
207 540616 : if ( ( hCPE->cpe_id * CPE_CHANNELS + n ) >= hMCT->nchan_out_woLFE )
208 : {
209 58829 : hCPE->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
210 : }
211 : else
212 : {
213 481787 : hCPE->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
214 : }
215 : }
216 : }
217 :
218 : /* reconfiguration in case of bitrate switching */
219 100939 : if ( ivas_total_brate != st_ivas->hEncoderConfig->last_ivas_total_brate )
220 : {
221 : int32_t cp_bitrate;
222 1250 : cp_bitrate = ivas_total_brate / hMCT->nchan_out_woLFE * CPE_CHANNELS;
223 :
224 1250 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
225 : {
226 255 : cp_bitrate = ivas_total_brate / st_ivas->nchan_transport * CPE_CHANNELS;
227 : }
228 :
229 3821 : for ( n = 0; n < (int16_t) ( hMCT->nchan_out_woLFE * 0.5 ); n++ )
230 : {
231 2571 : initMdctStereoEncData( hMCT->hBlockData[n]->hStereoMdct, ivas_format, IVAS_CPE_MDCT, cp_bitrate, st_ivas->hCPE[0]->hCoreCoder[0]->bwidth, st_ivas->hCPE[0]->hCoreCoder[0]->igf, st_ivas->hCPE[0]->hCoreCoder[0]->igf ? st_ivas->hCPE[0]->hCoreCoder[0]->hIGFEnc->igfData.igfInfo.grid : NULL, 0 );
232 : }
233 : }
234 :
235 : /* set coded audio band-width */
236 100939 : switch_bw = set_bw_mct( st_ivas->hCPE, st_ivas->nCPE );
237 :
238 : /*for MC and MCT remove pointer to LFE input that has been processed separately */
239 100939 : map_input_to_cpe_channels( st_ivas, pdata, data );
240 :
241 : /* pre-processing */
242 371247 : for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
243 : {
244 270308 : if ( ( error = ivas_cpe_enc( st_ivas, cpe_id, pdata[cpe_id * CPE_CHANNELS], pdata[cpe_id * CPE_CHANNELS + 1], input_frame, nb_bits_metadata ) ) != IVAS_ERR_OK )
245 : {
246 0 : return error;
247 : }
248 : }
249 :
250 : /* joint MCT encoding */
251 100939 : ivas_mct_core_enc( ivas_format, hMCT, st_ivas->hCPE, hMCT->nchan_out_woLFE, ivas_total_brate, switch_bw, ( ivas_format == MC_FORMAT && ( st_ivas->mc_mode == MC_MODE_MCT || st_ivas->mc_mode == MC_MODE_PARAMUPMIX ) ) ? (int16_t) st_ivas->hLFE->lfe_bits : 0, st_ivas->hEncoderConfig->sba_order );
252 :
253 : /* Spectrum quantization and coding */
254 371247 : for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
255 : {
256 270308 : hCPE = st_ivas->hCPE[cpe_id];
257 :
258 270308 : if ( cpe_id > 0 )
259 : {
260 169369 : hCPE->hCoreCoder[0]->hBstr->ind_list = st_ivas->hCPE[cpe_id - 1]->hCoreCoder[1]->hBstr->ind_list + st_ivas->hCPE[cpe_id - 1]->hCoreCoder[1]->hBstr->nb_ind_tot;
261 : }
262 :
263 270308 : ivas_mdct_quant_coder( hCPE, hMCT->tnsBits[cpe_id], hMCT->tnsSize[cpe_id], hMCT->p_param[cpe_id], 1 );
264 :
265 : /* update input samples buffer (as done in ivas_cpe_enc() for other than MCT coding) */
266 810924 : for ( n = 0; n < CPE_CHANNELS; n++ )
267 : {
268 540616 : mvr2r( hCPE->hCoreCoder[n]->input, hCPE->hCoreCoder[n]->old_input_signal, input_frame );
269 :
270 : /* common encoder updates */
271 540616 : updt_enc_common( hCPE->hCoreCoder[n] );
272 : }
273 : }
274 :
275 100939 : pop_wmops();
276 100939 : return error;
277 : }
278 :
279 :
280 : /*-------------------------------------------------------------------------
281 : * create_mct_enc()
282 : *
283 : * Create, allocate and initialize IVAS encoder MCT handle
284 : *-------------------------------------------------------------------------*/
285 :
286 948 : ivas_error create_mct_enc(
287 : Encoder_Struct *st_ivas /* i/o: IVAS encoder structure */
288 : )
289 : {
290 : MCT_ENC_HANDLE hMCT;
291 : int16_t n;
292 : int32_t cp_bitrate;
293 : int16_t max_blocks;
294 : int16_t cpe_id;
295 : IVAS_FORMAT ivas_format;
296 : int32_t ivas_total_brate;
297 :
298 948 : ivas_format = st_ivas->hEncoderConfig->ivas_format;
299 948 : ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
300 :
301 : /*-----------------------------------------------------------------*
302 : * Allocate MCT handle
303 : *-----------------------------------------------------------------*/
304 :
305 948 : if ( ( hMCT = (MCT_ENC_HANDLE) malloc( sizeof( MCT_ENC_DATA ) ) ) == NULL )
306 : {
307 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MCT\n" ) );
308 : }
309 :
310 : /*-----------------------------------------------------------------*
311 : * Allocate and initialize MCT BlockData handles
312 : *-----------------------------------------------------------------*/
313 :
314 : /* Determine active channels */
315 948 : if ( ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCT )
316 : {
317 339 : hMCT->nchan_out_woLFE = st_ivas->hEncoderConfig->nchan_inp - 1; /* LFE channel is coded separately */
318 : }
319 609 : else if ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT )
320 : {
321 586 : hMCT->nchan_out_woLFE = ivas_get_sba_num_TCs( ivas_total_brate, st_ivas->sba_analysis_order );
322 586 : if ( ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC )
323 : {
324 188 : hMCT->nchan_out_woLFE += st_ivas->hEncoderConfig->nchan_ism;
325 : }
326 : }
327 23 : else if ( ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_PARAMMC )
328 : {
329 12 : hMCT->nchan_out_woLFE = ivas_param_mc_getNumTransportChannels( ivas_total_brate, st_ivas->hEncoderConfig->mc_input_setup );
330 : }
331 11 : else if ( ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
332 : {
333 11 : hMCT->nchan_out_woLFE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS - 1;
334 : }
335 : #ifdef DEBUGGING
336 : else
337 : {
338 : assert( !"IVAS format currently not supported for MCT" );
339 : }
340 : #endif
341 :
342 948 : cp_bitrate = ivas_total_brate / hMCT->nchan_out_woLFE * CPE_CHANNELS;
343 948 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
344 : {
345 188 : cp_bitrate = ivas_total_brate / st_ivas->nchan_transport * CPE_CHANNELS;
346 : }
347 :
348 3631 : for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
349 : {
350 8049 : for ( n = 0; n < CPE_CHANNELS; n++ )
351 : {
352 5366 : st_ivas->hCPE[cpe_id]->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
353 : }
354 : }
355 :
356 : /* in case we have an uneven number of transport channels, indicate last channel ID as inactive */
357 948 : if ( hMCT->nchan_out_woLFE % 2 )
358 : {
359 689 : st_ivas->hCPE[st_ivas->nCPE - 1]->hCoreCoder[1]->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
360 : }
361 :
362 : /*Initialize MCT block data */
363 948 : max_blocks = hMCT->nchan_out_woLFE / CPE_CHANNELS;
364 :
365 2942 : for ( n = 0; n < max_blocks; n++ )
366 : {
367 1994 : assert( st_ivas->hEncoderConfig->element_mode_init == IVAS_CPE_MDCT && "MCT is not supported for other stereo modes" );
368 :
369 1994 : if ( ( hMCT->hBlockData[n] = (MCT_BLOCK_DATA_HANDLE) malloc( sizeof( MCT_BLOCK_DATA ) ) ) == NULL )
370 : {
371 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MCT block data structure\n" ) );
372 : }
373 :
374 : /*Initialize all parameters to zero*/
375 1994 : hMCT->hBlockData[n]->isActive = 0;
376 1994 : hMCT->hBlockData[n]->ch1 = 0;
377 1994 : hMCT->hBlockData[n]->ch2 = 0;
378 :
379 : /*-----------------------------------------------------------------*
380 : * MDCT stereo initialization
381 : *-----------------------------------------------------------------*/
382 :
383 1994 : if ( ( hMCT->hBlockData[n]->hStereoMdct = (STEREO_MDCT_ENC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_ENC_DATA ) ) ) == NULL )
384 : {
385 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
386 : }
387 :
388 : #ifdef DEBUGGING
389 : hMCT->hBlockData[n]->hStereoMdct->mdct_stereo_mode_cmdl = SMDCT_MS_DECISION;
390 : #endif
391 1994 : initMdctStereoEncData( hMCT->hBlockData[n]->hStereoMdct, ivas_format, IVAS_CPE_MDCT, cp_bitrate, st_ivas->hEncoderConfig->max_bwidth, st_ivas->hCPE[0]->hCoreCoder[0]->igf, st_ivas->hCPE[0]->hCoreCoder[0]->igf ? st_ivas->hCPE[0]->hCoreCoder[0]->hIGFEnc->igfData.igfInfo.grid : NULL, 1 );
392 : }
393 :
394 4642 : for ( ; n < MCT_MAX_BLOCKS; n++ )
395 : {
396 3694 : hMCT->hBlockData[n] = NULL;
397 : }
398 :
399 : /*-----------------------------------------------------------------*
400 : * Initializations
401 : *-----------------------------------------------------------------*/
402 :
403 948 : set_mct_enc_params( hMCT, ivas_total_brate, ivas_format, 1 );
404 :
405 948 : st_ivas->hMCT = hMCT;
406 :
407 948 : return IVAS_ERR_OK;
408 : }
409 :
410 :
411 : /*-------------------------------------------------------------------------
412 : * mct_enc_reconfigure()
413 : *
414 : * Reconfigure IVAS encoder MCT handle
415 : *-------------------------------------------------------------------------*/
416 :
417 446 : ivas_error mct_enc_reconfigure(
418 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
419 : const uint16_t b_nchan_change /* i : flag indicating different channel count */
420 : )
421 : {
422 : MCT_ENC_HANDLE hMCT;
423 : Encoder_State *st;
424 : int16_t n, cpe_id, max_blocks;
425 : int32_t ivas_total_brate, cp_bitrate;
426 : IVAS_FORMAT ivas_format;
427 : ivas_error error;
428 :
429 446 : error = IVAS_ERR_OK;
430 :
431 446 : hMCT = st_ivas->hMCT;
432 446 : ivas_format = st_ivas->hEncoderConfig->ivas_format;
433 446 : ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
434 :
435 : /*-----------------------------------------------------------------*
436 : * Allocate and initialize MCT BlockData handles
437 : *-----------------------------------------------------------------*/
438 :
439 446 : if ( b_nchan_change )
440 : {
441 : /* Determine active channels */
442 157 : if ( ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCT )
443 : {
444 7 : hMCT->nchan_out_woLFE = st_ivas->hEncoderConfig->nchan_inp - 1; /* LFE channel is coded separately */
445 : }
446 150 : else if ( ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
447 : {
448 1 : hMCT->nchan_out_woLFE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS - 1;
449 : }
450 149 : else if ( ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_PARAMMC )
451 : {
452 5 : hMCT->nchan_out_woLFE = ivas_param_mc_getNumTransportChannels( ivas_total_brate, st_ivas->hEncoderConfig->mc_input_setup );
453 : }
454 144 : else if ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT )
455 : {
456 144 : hMCT->nchan_out_woLFE = st_ivas->nchan_transport;
457 144 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
458 : {
459 52 : hMCT->nchan_out_woLFE += st_ivas->hEncoderConfig->nchan_ism;
460 : }
461 : }
462 : #ifdef DEBUGGING
463 : else
464 : {
465 : assert( !"IVAS format currently not supported for MCT" );
466 : }
467 : #endif
468 : }
469 :
470 446 : cp_bitrate = ivas_total_brate / hMCT->nchan_out_woLFE * CPE_CHANNELS;
471 446 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
472 : {
473 86 : cp_bitrate = ivas_total_brate / st_ivas->nchan_transport * CPE_CHANNELS;
474 : }
475 :
476 1690 : for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
477 : {
478 1244 : st_ivas->hCPE[cpe_id]->element_brate = cp_bitrate;
479 3732 : for ( n = 0; n < CPE_CHANNELS; n++ )
480 : {
481 2488 : st_ivas->hCPE[cpe_id]->hCoreCoder[n]->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
482 : }
483 : }
484 :
485 : /* in case we have an uneven number of transport channels, indicate last channel ID as inactive */
486 446 : if ( hMCT->nchan_out_woLFE % 2 )
487 : {
488 350 : st_ivas->hCPE[st_ivas->nCPE - 1]->hCoreCoder[1]->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
489 : }
490 :
491 : /* set correct nominal bitrates and igf config already here, otherwise we
492 : * run into a number of problems; only do it when the bitrate changes */
493 446 : if ( ivas_total_brate != st_ivas->hEncoderConfig->last_ivas_total_brate )
494 : {
495 1690 : for ( cpe_id = 0; cpe_id < st_ivas->nCPE; cpe_id++ )
496 : {
497 3732 : for ( n = 0; n < CPE_CHANNELS; n++ )
498 : {
499 2488 : st = st_ivas->hCPE[cpe_id]->hCoreCoder[n];
500 :
501 2488 : st->total_brate = st_ivas->hCPE[cpe_id]->element_brate;
502 :
503 2488 : if ( st->mct_chan_mode != MCT_CHAN_MODE_IGNORE )
504 : {
505 2138 : st->bits_frame_nominal = (int16_t) ( st_ivas->hCPE[cpe_id]->element_brate / FRAMES_PER_SEC );
506 2138 : st->igf = getIgfPresent( st->element_mode, st->bits_frame_nominal * FRAMES_PER_SEC, st->bwidth, st->rf_mode );
507 :
508 2138 : if ( ( error = IGF_Reconfig( &st->hIGFEnc, st->igf, 1, st_ivas->hCPE[cpe_id]->element_brate, st->bwidth, st->element_mode, st->rf_mode ) ) != IVAS_ERR_OK )
509 : {
510 0 : return error;
511 : }
512 : }
513 : }
514 : }
515 : }
516 :
517 : /* Initialize MCT block data */
518 446 : max_blocks = hMCT->nchan_out_woLFE / CPE_CHANNELS;
519 :
520 1340 : for ( n = 0; n < max_blocks; n++ )
521 : {
522 894 : uint16_t mem_init = 0;
523 894 : assert( st_ivas->hEncoderConfig->element_mode_init == IVAS_CPE_MDCT && "MCT is not supported for other stereo modes" );
524 :
525 894 : if ( b_nchan_change )
526 : {
527 335 : if ( hMCT->hBlockData[n] == NULL )
528 : {
529 107 : mem_init = 1;
530 107 : if ( ( hMCT->hBlockData[n] = (MCT_BLOCK_DATA_HANDLE) malloc( sizeof( MCT_BLOCK_DATA ) ) ) == NULL )
531 : {
532 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MCT block data structure\n" ) );
533 : }
534 :
535 : /*Initialize all parameters to zero*/
536 107 : hMCT->hBlockData[n]->isActive = 0;
537 107 : hMCT->hBlockData[n]->ch1 = 0;
538 107 : hMCT->hBlockData[n]->ch2 = 0;
539 :
540 : /*-----------------------------------------------------------------*
541 : * MDCT stereo initialization
542 : *-----------------------------------------------------------------*/
543 :
544 107 : if ( ( hMCT->hBlockData[n]->hStereoMdct = (STEREO_MDCT_ENC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_ENC_DATA ) ) ) == NULL )
545 : {
546 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
547 : }
548 : }
549 : }
550 :
551 : #ifdef DEBUGGING
552 : hMCT->hBlockData[n]->hStereoMdct->mdct_stereo_mode_cmdl = SMDCT_MS_DECISION;
553 : #endif
554 894 : initMdctStereoEncData( hMCT->hBlockData[n]->hStereoMdct, ivas_format, IVAS_CPE_MDCT, cp_bitrate, st_ivas->hEncoderConfig->max_bwidth, st_ivas->hCPE[0]->hCoreCoder[0]->igf, st_ivas->hCPE[0]->hCoreCoder[0]->igf ? st_ivas->hCPE[0]->hCoreCoder[0]->hIGFEnc->igfData.igfInfo.grid : NULL, mem_init );
555 : }
556 :
557 2228 : for ( ; n < MCT_MAX_BLOCKS; n++ )
558 : {
559 : /* deallocate no longer needed blocks */
560 1782 : if ( hMCT->hBlockData[n] != NULL )
561 : {
562 99 : if ( hMCT->hBlockData[n]->hStereoMdct != NULL )
563 : {
564 99 : free( hMCT->hBlockData[n]->hStereoMdct );
565 99 : hMCT->hBlockData[n]->hStereoMdct = NULL;
566 : }
567 :
568 99 : free( hMCT->hBlockData[n] );
569 99 : hMCT->hBlockData[n] = NULL;
570 : }
571 : }
572 :
573 : /*-----------------------------------------------------------------*
574 : * Initializations
575 : *-----------------------------------------------------------------*/
576 :
577 446 : set_mct_enc_params( hMCT, ivas_total_brate, ivas_format, b_nchan_change );
578 :
579 446 : return error;
580 : }
581 :
582 :
583 : /*-------------------------------------------------------------------------
584 : * ivas_mct_enc_close()
585 : *
586 : * Close MCT
587 : *-------------------------------------------------------------------------*/
588 :
589 1398 : void ivas_mct_enc_close(
590 : MCT_ENC_HANDLE *hMCT /* i/o: MCT encoder structure */
591 : )
592 : {
593 : int16_t n, maxBlocks;
594 :
595 1398 : if ( hMCT == NULL || *hMCT == NULL )
596 : {
597 450 : return;
598 : }
599 :
600 948 : maxBlocks = ( *hMCT )->nchan_out_woLFE / 2;
601 :
602 2950 : for ( n = 0; n < maxBlocks; n++ )
603 : {
604 2002 : if ( ( *hMCT )->hBlockData[n] != NULL )
605 : {
606 2002 : if ( ( *hMCT )->hBlockData[n]->hStereoMdct != NULL )
607 : {
608 2002 : free( ( *hMCT )->hBlockData[n]->hStereoMdct );
609 2002 : ( *hMCT )->hBlockData[n]->hStereoMdct = NULL;
610 : }
611 :
612 2002 : free( ( *hMCT )->hBlockData[n] );
613 2002 : ( *hMCT )->hBlockData[n] = NULL;
614 : }
615 : }
616 :
617 948 : free( ( *hMCT ) );
618 948 : ( *hMCT ) = NULL;
619 :
620 948 : return;
621 : }
622 :
623 :
624 : /*-------------------------------------------------------------------------
625 : * ivas_mc_enc_config()
626 : *
627 : * - select MC format mode
628 : * - write MC LS setup
629 : * - reconfigure the MC format encoder
630 : *-------------------------------------------------------------------------*/
631 :
632 48850 : ivas_error ivas_mc_enc_config(
633 : Encoder_Struct *st_ivas /* i/o: IVAS encoder structure */
634 : )
635 : {
636 : Encoder_State *st0; /* used for bitstream handling */
637 : MC_MODE last_mc_mode;
638 : ivas_error error;
639 :
640 48850 : error = IVAS_ERR_OK;
641 :
642 48850 : last_mc_mode = st_ivas->mc_mode;
643 :
644 : /* select MC format mode */
645 48850 : st_ivas->mc_mode = ivas_mc_mode_select( st_ivas->hEncoderConfig->mc_input_setup, st_ivas->hEncoderConfig->ivas_total_brate );
646 :
647 : /* write MC LS setup */
648 48850 : st0 = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[0]->hCoreCoder[0] : st_ivas->hCPE[0]->hCoreCoder[0];
649 48850 : push_indice( st0->hBstr, IND_SMODE, st_ivas->hEncoderConfig->mc_input_setup, MC_LS_SETUP_BITS );
650 :
651 : /* MC format switching */
652 48850 : if ( st_ivas->hEncoderConfig->last_ivas_total_brate != st_ivas->hEncoderConfig->ivas_total_brate || st_ivas->mc_mode != last_mc_mode )
653 : {
654 1078 : if ( ( error = ivas_mc_enc_reconfig( st_ivas, last_mc_mode ) ) != IVAS_ERR_OK )
655 : {
656 0 : return error;
657 : }
658 : }
659 :
660 48850 : return error;
661 : }
662 :
663 :
664 : /*-------------------------------------------------------------------------
665 : * ivas_mc_enc_reconfig()
666 : *
667 : * Reconfigure the MC format encoder
668 : *-------------------------------------------------------------------------*/
669 :
670 1078 : static ivas_error ivas_mc_enc_reconfig(
671 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
672 : const int16_t last_mc_mode /* i : last frame mc mode */
673 : )
674 : {
675 : int16_t nchan_transport_old, nSCE_old, nCPE_old;
676 : ivas_error error;
677 : int32_t new_brate_SCE, new_brate_CPE;
678 :
679 1078 : error = IVAS_ERR_OK;
680 :
681 1078 : nchan_transport_old = st_ivas->nchan_transport;
682 1078 : nSCE_old = st_ivas->nSCE;
683 1078 : nCPE_old = st_ivas->nCPE;
684 :
685 : /*-----------------------------------------------------------------*
686 : * Reconfigure MC modules
687 : *-----------------------------------------------------------------*/
688 :
689 1078 : if ( st_ivas->mc_mode == MC_MODE_MCT )
690 : {
691 471 : st_ivas->nSCE = 0;
692 471 : st_ivas->nCPE = st_ivas->hEncoderConfig->nchan_inp / 2;
693 :
694 471 : st_ivas->nchan_transport = ivas_mc_ls_setup_get_num_channels( st_ivas->hEncoderConfig->mc_input_setup );
695 :
696 471 : if ( last_mc_mode != MC_MODE_MCT )
697 : {
698 310 : if ( st_ivas->hLFE != NULL )
699 : {
700 : /* LFE handle */
701 3 : ivas_lfe_enc_close( &( st_ivas->hLFE ) );
702 : }
703 :
704 : /* create LFE handle */
705 310 : if ( ( error = ivas_create_lfe_enc( &st_ivas->hLFE, st_ivas->hEncoderConfig->input_Fs ) ) != IVAS_ERR_OK )
706 : {
707 0 : return error;
708 : }
709 :
710 : /*De-allocate handles for other MC modes*/
711 310 : ivas_param_mc_enc_close( &( st_ivas->hParamMC ), st_ivas->hEncoderConfig->input_Fs );
712 :
713 310 : ivas_mc_paramupmix_enc_close( &( st_ivas->hMCParamUpmix ), st_ivas->hEncoderConfig->input_Fs );
714 :
715 : /* De-allocate McMasa-related handles */
716 310 : ivas_mcmasa_enc_close( &( st_ivas->hMcMasa ), st_ivas->hEncoderConfig->input_Fs );
717 :
718 310 : ivas_masa_enc_close( &( st_ivas->hMasa ) );
719 :
720 310 : ivas_qmetadata_close( &st_ivas->hQMetaData );
721 : }
722 : }
723 607 : else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
724 : {
725 7 : st_ivas->nSCE = 0;
726 7 : st_ivas->nCPE = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS / 2;
727 7 : st_ivas->nchan_transport = MC_PARAMUPMIX_MAX_TRANSPORT_CHANS;
728 :
729 7 : if ( last_mc_mode != MC_MODE_PARAMUPMIX )
730 : {
731 7 : if ( st_ivas->hLFE != NULL )
732 : {
733 : /* LFE handle */
734 0 : ivas_lfe_enc_close( &( st_ivas->hLFE ) );
735 : }
736 :
737 : /* create LFE handle */
738 7 : if ( ( error = ivas_create_lfe_enc( &st_ivas->hLFE, st_ivas->hEncoderConfig->input_Fs ) ) != IVAS_ERR_OK )
739 : {
740 0 : return error;
741 : }
742 : }
743 :
744 7 : if ( last_mc_mode != MC_MODE_PARAMUPMIX )
745 : {
746 7 : if ( ( error = ivas_mc_paramupmix_enc_open( st_ivas ) ) != IVAS_ERR_OK )
747 : {
748 0 : return error;
749 : }
750 : }
751 : #ifdef DEBUGGING
752 : else
753 : {
754 : assert( 0 );
755 : }
756 : #endif
757 :
758 : /*De-allocate handles for other MC modes*/
759 7 : ivas_param_mc_enc_close( &st_ivas->hParamMC, st_ivas->hEncoderConfig->input_Fs );
760 :
761 : /* De-allocate McMasa-related handles */
762 7 : ivas_mcmasa_enc_close( &( st_ivas->hMcMasa ), st_ivas->hEncoderConfig->input_Fs );
763 :
764 7 : ivas_masa_enc_close( &( st_ivas->hMasa ) );
765 :
766 7 : ivas_qmetadata_close( &st_ivas->hQMetaData );
767 : }
768 600 : else if ( st_ivas->mc_mode == MC_MODE_PARAMMC )
769 : {
770 334 : if ( last_mc_mode != MC_MODE_PARAMMC )
771 : {
772 269 : if ( ( error = ivas_param_mc_enc_open( st_ivas ) ) != IVAS_ERR_OK )
773 : {
774 0 : return error;
775 : }
776 : }
777 : else
778 : {
779 65 : if ( ( error = ivas_param_mc_enc_reconfig( st_ivas ) ) != IVAS_ERR_OK )
780 : {
781 0 : return error;
782 : }
783 : }
784 :
785 : /* De-allocate McMasa-related handles */
786 334 : ivas_mcmasa_enc_close( &( st_ivas->hMcMasa ), st_ivas->hEncoderConfig->input_Fs );
787 :
788 334 : if ( st_ivas->hMasa != NULL )
789 : {
790 94 : ivas_masa_enc_close( &( st_ivas->hMasa ) );
791 94 : st_ivas->hMasa = NULL;
792 : }
793 :
794 334 : ivas_mc_paramupmix_enc_close( &( st_ivas->hMCParamUpmix ), st_ivas->hEncoderConfig->input_Fs );
795 :
796 334 : if ( last_mc_mode == MC_MODE_PARAMUPMIX && st_ivas->hLFE != NULL )
797 : {
798 : /* LFE handle */
799 0 : ivas_lfe_enc_close( &( st_ivas->hLFE ) );
800 : }
801 :
802 334 : ivas_qmetadata_close( &st_ivas->hQMetaData );
803 :
804 : /* De-allocate MCT handle if last mode was MCT */
805 334 : if ( last_mc_mode == MC_MODE_MCT && st_ivas->nchan_transport <= CPE_CHANNELS )
806 : {
807 170 : ivas_mct_enc_close( &( st_ivas->hMCT ) );
808 : }
809 :
810 334 : if ( last_mc_mode == MC_MODE_MCT && st_ivas->hLFE != NULL )
811 : {
812 : /* LFE handle */
813 175 : ivas_lfe_enc_close( &( st_ivas->hLFE ) );
814 : }
815 : }
816 266 : else if ( st_ivas->mc_mode == MC_MODE_MCMASA )
817 : {
818 266 : if ( last_mc_mode != MC_MODE_MCMASA )
819 : {
820 218 : ivas_mcmasa_setNumTransportChannels( &( st_ivas->nchan_transport ), &( st_ivas->hEncoderConfig->element_mode_init ), st_ivas->hEncoderConfig->ivas_total_brate );
821 :
822 218 : if ( ( error = ivas_qmetadata_open( &( st_ivas->hQMetaData ) ) ) != IVAS_ERR_OK )
823 : {
824 0 : return error;
825 : }
826 :
827 218 : if ( ( error = ivas_masa_enc_open( st_ivas ) ) != IVAS_ERR_OK )
828 : {
829 0 : return error;
830 : }
831 :
832 218 : if ( ( error = ivas_mcmasa_enc_open( st_ivas ) ) != IVAS_ERR_OK )
833 : {
834 0 : return error;
835 : }
836 : }
837 : else
838 : {
839 : /* reconfigure McMASA instance */
840 48 : if ( ( error = ivas_mcmasa_enc_reconfig( st_ivas ) ) != IVAS_ERR_OK )
841 : {
842 0 : return error;
843 : }
844 : }
845 :
846 266 : ivas_param_mc_enc_close( &( st_ivas->hParamMC ), st_ivas->hEncoderConfig->input_Fs );
847 :
848 266 : ivas_mc_paramupmix_enc_close( &( st_ivas->hMCParamUpmix ), st_ivas->hEncoderConfig->input_Fs );
849 :
850 266 : if ( last_mc_mode == MC_MODE_PARAMUPMIX && st_ivas->hLFE != NULL )
851 : {
852 : /* LFE handle */
853 0 : ivas_lfe_enc_close( &( st_ivas->hLFE ) );
854 : }
855 :
856 266 : if ( last_mc_mode == MC_MODE_MCT )
857 : {
858 : /* LFE handle */
859 124 : ivas_lfe_enc_close( &( st_ivas->hLFE ) );
860 :
861 124 : ivas_mct_enc_close( &( st_ivas->hMCT ) );
862 : }
863 : }
864 :
865 1078 : if ( st_ivas->mc_mode != MC_MODE_MCMASA )
866 : {
867 812 : if ( st_ivas->nchan_transport == 1 )
868 : {
869 0 : st_ivas->hEncoderConfig->element_mode_init = IVAS_SCE;
870 : }
871 : else
872 : {
873 812 : st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
874 : }
875 : }
876 :
877 : /*-----------------------------------------------------------------*
878 : * Reconfigure core coder
879 : *-----------------------------------------------------------------*/
880 :
881 : /* special case: MCT->ParamMC with more than 2 TC, CPE 1 stays, but has the wrong mct_chan_mode in channel 1
882 : and might have IGF and TranDet static memory not allocated and the bit stream index list not set,
883 : set correct mct_chan_mode and init missing static mem
884 : do it here since it is _very_ MC specific */
885 1078 : if ( last_mc_mode == MC_MODE_MCT && st_ivas->mc_mode == MC_MODE_PARAMMC && st_ivas->nchan_transport > CPE_CHANNELS )
886 : {
887 5 : Encoder_State *st = st_ivas->hCPE[1]->hCoreCoder[1];
888 :
889 5 : if ( st_ivas->nchan_transport == 3 )
890 : {
891 5 : st->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
892 : }
893 : else
894 : {
895 0 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
896 : }
897 :
898 5 : if ( st->hTranDet == NULL )
899 : {
900 0 : if ( ( st->hTranDet = (TRAN_DET_HANDLE) malloc( sizeof( TRAN_DET_DATA ) ) ) == NULL )
901 : {
902 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Transient Detection\n" ) );
903 : }
904 :
905 0 : InitTransientDetection( (int16_t) ( st->input_Fs / FRAMES_PER_SEC ), NS2SA( st->input_Fs, DELAY_FIR_RESAMPL_NS ), st->hTranDet, 0 );
906 : }
907 :
908 5 : if ( st->hIGFEnc == NULL )
909 : {
910 0 : if ( ( st->hIGFEnc = (IGF_ENC_INSTANCE_HANDLE) malloc( sizeof( IGF_ENC_INSTANCE ) ) ) == NULL )
911 : {
912 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for hIGFEnc\n" ) );
913 : }
914 : }
915 :
916 5 : st->igf = getIgfPresent( st->element_mode, st->total_brate, st->bwidth, st->rf_mode );
917 :
918 : /* set last core to TCX20 */
919 5 : st->last_core = TCX_20_CORE;
920 : }
921 :
922 1078 : if ( st_ivas->mc_mode == MC_MODE_MCMASA )
923 : {
924 266 : ivas_mcmasa_split_brate( st_ivas->hMcMasa->separateChannelEnabled, st_ivas->hEncoderConfig->ivas_total_brate, st_ivas->nSCE, st_ivas->nCPE, &new_brate_SCE, &new_brate_CPE );
925 : }
926 812 : else if ( st_ivas->mc_mode == MC_MODE_MCT )
927 : {
928 471 : new_brate_SCE = 0;
929 471 : new_brate_CPE = ( st_ivas->hEncoderConfig->ivas_total_brate / ( st_ivas->nchan_transport - 1 ) ) * CPE_CHANNELS;
930 : }
931 341 : else if ( st_ivas->mc_mode == MC_MODE_PARAMUPMIX )
932 : {
933 7 : new_brate_SCE = 0;
934 7 : new_brate_CPE = ( st_ivas->hEncoderConfig->ivas_total_brate / ( st_ivas->nchan_transport - 1 ) ) * CPE_CHANNELS;
935 : }
936 : else
937 : {
938 334 : new_brate_SCE = 0; /*st_ivas->hEncoderConfig->ivas_total_brate / st_ivas->nchan_transport;*/
939 334 : new_brate_CPE = ( st_ivas->hEncoderConfig->ivas_total_brate / st_ivas->nchan_transport ) * CPE_CHANNELS;
940 : }
941 :
942 1078 : if ( ( error = ivas_corecoder_enc_reconfig( st_ivas, nSCE_old, nCPE_old, nchan_transport_old, new_brate_SCE, new_brate_CPE, last_mc_mode ) ) != IVAS_ERR_OK )
943 : {
944 0 : return error;
945 : }
946 :
947 1078 : return error;
948 : }
|