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 <stdint.h>
34 : #include <math.h>
35 : #include "options.h"
36 : #include "cnst.h"
37 : #include "ivas_cnst.h"
38 : #include "rom_com.h"
39 : #include "prot_fx.h"
40 : #include "ivas_rom_com.h"
41 : #ifdef DEBUGGING
42 : #include "debug.h"
43 : #endif
44 : #include "wmc_auto.h"
45 : #include "ivas_prot_fx.h"
46 :
47 : /*-------------------------------------------------------------------*
48 : * ivas_enc()
49 : *
50 : * Principal IVAS encoder routine
51 : *-------------------------------------------------------------------*/
52 421288 : ivas_error ivas_enc_fx(
53 : Encoder_Struct *st_ivas, /* i/o: IVAS encoder structure */
54 : const Word16 *data, /* i : input signal Q0*/
55 : const Word16 n_samples /* i : number of input samples Q0*/
56 : )
57 : {
58 : Word16 i, n, input_frame, n_samples_chan, nchan_inp /*, scale*/;
59 : Word32 input_Fs;
60 : IVAS_FORMAT ivas_format;
61 : ENCODER_CONFIG_HANDLE hEncoderConfig;
62 : BSTR_ENC_HANDLE hMetaData;
63 : Word16 nb_bits_metadata[MAX_SCE + 1];
64 : Word32 *data_fx[MAX_INPUT_CHANNELS + MAX_NUM_OBJECTS];
65 : Word32 ivas_total_brate;
66 : ivas_error error;
67 421288 : error = IVAS_ERR_OK;
68 421288 : move32();
69 :
70 421288 : push_wmops( "ivas_enc" );
71 :
72 : /*------------------------------------------------------------------*
73 : * Initialization - general
74 : *-----------------------------------------------------------------*/
75 :
76 421288 : hEncoderConfig = st_ivas->hEncoderConfig;
77 :
78 421288 : input_Fs = hEncoderConfig->input_Fs; /* Q0 */
79 421288 : ivas_format = hEncoderConfig->ivas_format;
80 421288 : nchan_inp = hEncoderConfig->nchan_inp; /* Q0 */
81 421288 : ivas_total_brate = hEncoderConfig->ivas_total_brate; /* Q0 */
82 421288 : move32();
83 421288 : move32();
84 421288 : move16();
85 421288 : move32();
86 :
87 421288 : input_frame = extract_l( Mpy_32_16_1( input_Fs, INV_FRAME_PER_SEC_Q15 ) );
88 421288 : IF( NE_16( nchan_inp, 1 ) )
89 : {
90 387672 : n_samples_chan = div_l( L_deposit_l( n_samples ), nchan_inp ); /* Q0 */
91 387672 : n_samples_chan = shl( n_samples_chan, 1 ); /* Q0 */
92 : }
93 : ELSE
94 : {
95 33616 : n_samples_chan = n_samples; /* Q0 */
96 33616 : move16();
97 : }
98 421288 : set16_fx( nb_bits_metadata, 0, MAX_SCE + 1 );
99 :
100 : /*----------------------------------------------------------------*
101 : * convert 'Word16' input data to 'Word32' in Q11 format
102 : *----------------------------------------------------------------*/
103 :
104 8847048 : FOR( n = 0; n < MAX_INPUT_CHANNELS + MAX_NUM_OBJECTS; n++ )
105 : {
106 8425760 : data_fx[n] = st_ivas->p_data_fx[n];
107 8425760 : IF( data_fx[n] )
108 : {
109 2529310 : Scale_sig32( data_fx[n], input_frame, sub( Q11, st_ivas->q_data_fx ) ); // Q11
110 : }
111 : }
112 421288 : st_ivas->q_data_fx = Q11;
113 421288 : move16();
114 421288 : n = 0;
115 421288 : move16();
116 2938898 : WHILE( LT_16( n, nchan_inp ) )
117 : {
118 2063142250 : FOR( i = 0; i < n_samples_chan; i++ )
119 : {
120 2060624640 : data_fx[n][i] = L_mult0( data[i * nchan_inp + n], shl( 1, st_ivas->q_data_fx ) );
121 2060624640 : move32();
122 : }
123 2517610 : n = add( n, 1 );
124 : }
125 :
126 421288 : IF( LT_16( n_samples_chan, input_frame ) )
127 : {
128 0 : FOR( n = 0; n < nchan_inp; n++ )
129 : {
130 0 : set32_fx( data_fx[n] + n_samples_chan, 0, sub( input_frame, n_samples_chan ) );
131 : }
132 : }
133 :
134 421288 : IF( EQ_32( ivas_format, SBA_FORMAT ) )
135 : {
136 122500 : IF( NE_32( ( error = ivas_sba_enc_reconfigure_fx( st_ivas ) ), IVAS_ERR_OK ) )
137 : {
138 0 : return error;
139 : }
140 : }
141 298788 : ELSE IF( EQ_32( ivas_format, SBA_ISM_FORMAT ) )
142 : {
143 35000 : IF( ( error = ivas_osba_enc_reconfig( st_ivas ) ) != IVAS_ERR_OK )
144 : {
145 0 : return error;
146 : }
147 : }
148 :
149 : /*----------------------------------------------------------------*
150 : * HP filtering
151 : *----------------------------------------------------------------*/
152 :
153 421288 : n = getNumChanAnalysis_fx( st_ivas );
154 :
155 : /* bypass EVS coding in float precision, emulating EVS encoder/decoder delay */
156 2217898 : FOR( i = 0; i < n; i++ )
157 : {
158 1796610 : test();
159 1796610 : test();
160 1796610 : IF( ( EQ_32( ivas_format, SBA_FORMAT ) ) && !( GT_16( st_ivas->sba_analysis_order, 1 ) ) )
161 : {
162 443080 : hp20_fx_32( data_fx[HOA_keep_ind[st_ivas->hSpar->hMdEnc->HOA_md_ind[i]]], input_frame, st_ivas->mem_hp20_in_fx[i], input_Fs );
163 : }
164 1353530 : ELSE IF( !( EQ_32( ivas_format, MC_FORMAT ) && EQ_16( i, LFE_CHANNEL ) ) )
165 : {
166 1305680 : hp20_fx_32( data_fx[i], input_frame, st_ivas->mem_hp20_in_fx[i], input_Fs );
167 : }
168 : }
169 :
170 : /*----------------------------------------------------------------*
171 : * write IVAS format signaling
172 : *----------------------------------------------------------------*/
173 421288 : ivas_write_format_fx( st_ivas );
174 :
175 : /*----------------------------------------------------------------*
176 : * Encoding
177 : *----------------------------------------------------------------*/
178 421288 : test();
179 421288 : IF( EQ_32( ivas_format, STEREO_FORMAT ) )
180 : {
181 72486 : st_ivas->hCPE[0]->element_brate = ivas_total_brate; /* Q0 */
182 72486 : move32();
183 72486 : IF( ( error = ivas_cpe_enc_fx( st_ivas, 0, data_fx[0], data_fx[1], st_ivas->q_data_fx, input_frame, 0 /* no metadata */, NULL ) ) != IVAS_ERR_OK )
184 : {
185 0 : return error;
186 : }
187 : }
188 348802 : ELSE IF( EQ_32( ivas_format, ISM_FORMAT ) )
189 : {
190 : /* select ISM format mode; reconfigure the ISM format encoder */
191 103826 : IF( ( error = ivas_ism_enc_config( st_ivas ) ) != IVAS_ERR_OK )
192 : {
193 0 : return error;
194 : }
195 :
196 103826 : IF( EQ_32( st_ivas->ism_mode, ISM_MODE_PARAM ) )
197 : {
198 20720 : ivas_param_ism_enc_fx( st_ivas, data_fx, input_frame );
199 :
200 : /* Stereo DMX generation */
201 20720 : ivas_param_ism_stereo_dmx_fx( st_ivas, data_fx, input_frame );
202 :
203 : /* Core coding of Stereo DMX */
204 20720 : IF( ( error = ivas_ism_enc_fx( st_ivas, data_fx, st_ivas->q_data_fx, input_frame, nb_bits_metadata, 0 ) ) != IVAS_ERR_OK )
205 : {
206 0 : return error;
207 : }
208 : }
209 83106 : ELSE IF( EQ_32( st_ivas->ism_mode, ISM_MODE_DISC ) )
210 : {
211 : /* Analysis, decision about bitrates per channel & core coding */
212 83106 : IF( ( error = ivas_ism_enc_fx( st_ivas, data_fx, st_ivas->q_data_fx, input_frame, nb_bits_metadata, 0 ) ) != IVAS_ERR_OK )
213 : {
214 0 : return error;
215 : }
216 : }
217 : }
218 244976 : ELSE IF( EQ_32( ivas_format, SBA_FORMAT ) || EQ_32( ivas_format, MASA_FORMAT ) )
219 : {
220 : /* MASA configuration */
221 153126 : IF( EQ_32( ivas_format, MASA_FORMAT ) )
222 : {
223 30626 : ivas_masa_enc_reconfigure_fx( st_ivas );
224 : }
225 :
226 : /* SBA/MASA metadata encoding and SBA/MASA metadata bitstream writing */
227 : /* hMetaData = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[st_ivas->nSCE - 1]->hMetaData : st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData; */
228 153126 : IF( st_ivas->nSCE > 0 )
229 : {
230 51371 : hMetaData = st_ivas->hSCE[st_ivas->nSCE - 1]->hMetaData;
231 : }
232 : ELSE
233 : {
234 101755 : hMetaData = st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData;
235 : }
236 :
237 153126 : test();
238 153126 : IF( st_ivas->hQMetaData != NULL && EQ_32( ivas_format, MASA_FORMAT ) )
239 : {
240 30626 : ivas_masa_estimate_energy_fx( st_ivas->hMasa, data_fx, input_frame, st_ivas->nchan_transport, st_ivas->q_data_fx ); /* energy-estimation uses TF-resolution: 4x24 */
241 :
242 30626 : IF( ( error = ivas_masa_enc_config_fx( st_ivas ) ) != IVAS_ERR_OK )
243 : {
244 0 : return error;
245 : }
246 :
247 30626 : IF( EQ_16( st_ivas->nchan_transport, 2 ) )
248 : {
249 16536 : IF( ( error = ivas_masa_encode_fx( st_ivas->hMasa, st_ivas->hQMetaData, hMetaData, &nb_bits_metadata[0], st_ivas->nchan_transport, ivas_format, ivas_total_brate, hEncoderConfig->Opt_DTX_ON, st_ivas->hCPE[0]->element_mode,
250 : ISM_MODE_NONE, -1, NULL, -1, NULL, 0, 0 ) ) !=
251 : IVAS_ERR_OK )
252 : {
253 0 : return error;
254 : }
255 : }
256 : ELSE
257 : {
258 14090 : IF( ( error = ivas_masa_encode_fx( st_ivas->hMasa, st_ivas->hQMetaData, hMetaData, &nb_bits_metadata[0], st_ivas->nchan_transport, ivas_format, ivas_total_brate, hEncoderConfig->Opt_DTX_ON, -1,
259 : ISM_MODE_NONE, -1, NULL, -1, NULL, 0, 0 ) ) !=
260 : IVAS_ERR_OK )
261 : {
262 0 : return error;
263 : }
264 : }
265 : }
266 122500 : ELSE IF( EQ_32( ivas_format, SBA_FORMAT ) )
267 : {
268 122500 : IF( ( error = ivas_spar_enc_fx( st_ivas, data_fx, input_frame, nb_bits_metadata, hMetaData ) ) != IVAS_ERR_OK )
269 : {
270 0 : return error;
271 : }
272 : }
273 :
274 153126 : IF( EQ_32( ivas_format, SBA_FORMAT ) )
275 : {
276 122500 : ivas_sba_getTCs_fx( data_fx, st_ivas, input_frame );
277 : }
278 :
279 : /* core-coding of transport channels */
280 153126 : IF( EQ_16( st_ivas->nSCE, 1 ) )
281 : {
282 51371 : IF( ( error = ivas_sce_enc_fx( st_ivas, 0, data_fx[0], st_ivas->q_data_fx, input_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
283 : {
284 0 : return error;
285 : }
286 : }
287 101755 : ELSE IF( EQ_16( st_ivas->nCPE, 1 ) ) /* Stereo DMX */
288 : {
289 51516 : IF( ( error = ivas_cpe_enc_fx( st_ivas, 0, data_fx[0], data_fx[1], st_ivas->q_data_fx, input_frame, nb_bits_metadata[0], NULL ) ) != IVAS_ERR_OK )
290 : {
291 0 : return error;
292 : }
293 : }
294 50239 : ELSE IF( GT_16( st_ivas->nCPE, 1 ) ) /* FOA/HOA format */
295 : {
296 50239 : IF( ( error = ivas_mct_enc_fx( st_ivas, data_fx, st_ivas->q_data_fx, input_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
297 : {
298 0 : return error;
299 : }
300 : }
301 : }
302 91850 : ELSE IF( EQ_32( ivas_format, MASA_ISM_FORMAT ) )
303 : {
304 : Word32 *data_separated_object_fx;
305 : Word16 q_data_separated_object;
306 :
307 : Word16 idx_separated_object;
308 : Word16 flag_omasa_ener_brate;
309 :
310 9000 : flag_omasa_ener_brate = 0;
311 9000 : move16();
312 :
313 : /* Stereo transport is used also with monoMASA, duplicate mono if monoMASA */
314 9000 : IF( EQ_16( sub( st_ivas->hEncoderConfig->nchan_inp, hEncoderConfig->nchan_ism ), 1 ) )
315 : {
316 2700 : v_multc_fixed( data_fx[hEncoderConfig->nchan_ism], L_deposit_h( INV_SQRT2_FX_Q15 ) /* Q31 */, data_fx[hEncoderConfig->nchan_ism], input_frame );
317 2700 : Copy32( data_fx[hEncoderConfig->nchan_ism], data_fx[hEncoderConfig->nchan_ism + 1], input_frame ); /* st_ivas->q_data_fx */
318 : }
319 :
320 : /* Estimate TF-tile energy for the input MASA stream */
321 9000 : ivas_masa_estimate_energy_fx( st_ivas->hMasa, &( data_fx[hEncoderConfig->nchan_ism] ), input_frame, st_ivas->nchan_transport, st_ivas->q_data_fx );
322 :
323 9000 : IF( ( error = ivas_omasa_enc_config_fx( st_ivas ) ) != IVAS_ERR_OK )
324 : {
325 0 : return error;
326 : }
327 :
328 9000 : set16_fx( nb_bits_metadata, 0, MAX_SCE + 1 );
329 9000 : idx_separated_object = 0;
330 9000 : move16();
331 :
332 9000 : data_separated_object_fx = data_fx[hEncoderConfig->nchan_ism + CPE_CHANNELS]; // Q(st_ivas->q_data_fx)
333 9000 : q_data_separated_object = st_ivas->q_data_fx;
334 9000 : move16();
335 :
336 : /* put audio object data in SCE's */
337 9000 : IF( NE_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
338 : {
339 : /* Scaling data_fx */
340 : {
341 5858 : Word16 norm_data_in = MAX16B;
342 5858 : move16();
343 37014 : FOR( i = 0; i < hEncoderConfig->nchan_ism + st_ivas->nchan_transport; i++ )
344 : {
345 31156 : norm_data_in = s_min( norm_data_in, L_norm_arr( data_fx[i], input_frame ) );
346 : }
347 5858 : IF( LT_16( norm_data_in, 31 ) )
348 : {
349 5858 : norm_data_in = sub( norm_data_in, 7 ); /*guard bit is 4->to handle overflow in cldfbAnalysis*/
350 5858 : norm_data_in = s_min( norm_data_in, 20 ); // limit Q to 31 (11 + norm)
351 37014 : FOR( i = 0; i < hEncoderConfig->nchan_ism + st_ivas->nchan_transport; i++ )
352 : {
353 31156 : scale_sig32( data_fx[i], input_frame, norm_data_in ); /* st_ivas->q_data_fx + norm_data_in */
354 : }
355 5858 : st_ivas->q_data_fx = add( st_ivas->q_data_fx, norm_data_in );
356 5858 : move16();
357 : }
358 : ELSE
359 : {
360 0 : st_ivas->q_data_fx = 31;
361 0 : move16();
362 : }
363 : }
364 :
365 : /* Estimate MASA parameters for the objects */
366 5858 : ivas_omasa_enc_fx( st_ivas->hOMasa, st_ivas->hMasa, st_ivas->hIsmMetaData, data_fx, st_ivas->q_data_fx, input_frame, st_ivas->nchan_transport, hEncoderConfig->nchan_ism, st_ivas->ism_mode, data_separated_object_fx, &idx_separated_object );
367 :
368 5858 : q_data_separated_object = st_ivas->q_data_fx;
369 5858 : move16();
370 : }
371 :
372 : /* Encode ISMs transport channels */
373 9000 : n = 0;
374 9000 : move16();
375 9000 : IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_PARAM_ONE_OBJ ) )
376 : {
377 1862 : IF( NE_32( ( error = ivas_sce_enc_fx( st_ivas, 0, data_separated_object_fx, q_data_separated_object, input_frame, nb_bits_metadata[1] ) ), IVAS_ERR_OK ) ) /* there are no metadata bits in SCE in this mode */
378 : {
379 0 : return error;
380 : }
381 : }
382 7138 : ELSE IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) )
383 : {
384 2054 : IF( NE_32( ( error = ivas_ism_enc_fx( st_ivas, &data_separated_object_fx, q_data_separated_object, input_frame, &nb_bits_metadata[1], 0 ) ), IVAS_ERR_OK ) )
385 : {
386 0 : return error;
387 : }
388 : }
389 5084 : ELSE IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
390 : {
391 3142 : flag_omasa_ener_brate = ivas_omasa_ener_brate_fx( st_ivas->hEncoderConfig->nchan_ism, ivas_total_brate, data_fx, input_frame, sub( 31, st_ivas->q_data_fx ) ); /* Q0 */
392 :
393 : /* Analysis, decision about bitrates per channel & core coding */
394 3142 : IF( NE_32( ( error = ivas_ism_enc_fx( st_ivas, data_fx, st_ivas->q_data_fx, input_frame, &nb_bits_metadata[1], flag_omasa_ener_brate ) ), IVAS_ERR_OK ) )
395 : {
396 0 : return error;
397 : }
398 3142 : n = st_ivas->hEncoderConfig->nchan_ism; /* Q0 */
399 3142 : move16();
400 : }
401 :
402 9000 : hMetaData = st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData;
403 :
404 9000 : IF( st_ivas->nSCE > 0 )
405 : {
406 : /* update pointer to the buffer of indices (ISM indices were alredy written) */
407 7058 : hMetaData->ind_list = st_ivas->hSCE[st_ivas->nSCE - 1]->hMetaData->ind_list + st_ivas->hSCE[st_ivas->nSCE - 1]->hMetaData->nb_ind_tot;
408 7058 : st_ivas->hCPE[0]->hCoreCoder[0]->hBstr->ind_list = st_ivas->hSCE[st_ivas->nSCE - 1]->hCoreCoder[0]->hBstr->ind_list + st_ivas->hSCE[st_ivas->nSCE - 1]->hCoreCoder[0]->hBstr->nb_ind_tot;
409 : }
410 :
411 : /* Encode MASA parameters and write MASA metadata bitstream */
412 : Word16 element_mode;
413 9000 : if ( st_ivas->nchan_transport == 2 )
414 : {
415 9000 : element_mode = st_ivas->hCPE[0]->element_mode;
416 9000 : move16();
417 : }
418 : else
419 : {
420 0 : element_mode = -1;
421 0 : move16();
422 : }
423 :
424 9000 : IF( NE_32( ( error = ivas_masa_encode_fx( st_ivas->hMasa, st_ivas->hQMetaData, hMetaData, nb_bits_metadata, st_ivas->nchan_transport, ivas_format, ivas_total_brate, st_ivas->hEncoderConfig->Opt_DTX_ON, element_mode,
425 : st_ivas->ism_mode, hEncoderConfig->nchan_ism, st_ivas->hIsmMetaData, idx_separated_object, st_ivas->hOMasa, st_ivas->hIsmMetaData[0]->ism_imp, flag_omasa_ener_brate ) ),
426 : IVAS_ERR_OK ) )
427 : {
428 0 : return error;
429 : }
430 :
431 : /* Configuration of combined-format bit-budget distribution */
432 9000 : ivas_set_surplus_brate_enc( st_ivas
433 : #ifdef DEBUG_MODE_INFO
434 : ,
435 : nb_bits_metadata
436 : #endif
437 : );
438 :
439 : /* Encode MASA transport channels */
440 9000 : IF( ( ivas_cpe_enc_fx( st_ivas, 0, data_fx[n], data_fx[n + 1], st_ivas->q_data_fx, input_frame, nb_bits_metadata[0], NULL ) ) != IVAS_ERR_OK )
441 : {
442 0 : return error;
443 : }
444 : }
445 82850 : ELSE IF( EQ_32( ivas_format, SBA_ISM_FORMAT ) )
446 : {
447 : Word16 planar_sba_orig, nchan;
448 35000 : planar_sba_orig = hEncoderConfig->sba_planar;
449 35000 : move16();
450 :
451 : /* Analyze objects and determine needed audio signals */
452 35000 : ivas_osba_enc_fx( st_ivas->hOSba, st_ivas->hIsmMetaData, data_fx, input_frame, hEncoderConfig->nchan_ism, st_ivas->ism_mode, st_ivas->sba_analysis_order, hEncoderConfig->input_Fs, hEncoderConfig->sba_planar, &st_ivas->q_data_fx );
453 :
454 35000 : IF( st_ivas->ism_mode == ISM_MODE_NONE )
455 : {
456 18690 : nchan = imult1616( add( st_ivas->sba_analysis_order, 1 ), add( st_ivas->sba_analysis_order, 1 ) ); /* Q0 */
457 : }
458 : ELSE
459 : {
460 16310 : nchan = hEncoderConfig->nchan_ism; /* Q0 */
461 16310 : move16();
462 : }
463 161150 : FOR( n = 0; n < nchan; n++ )
464 : {
465 126150 : scale_sig32( data_fx[n], input_frame, sub( Q11, st_ivas->q_data_fx ) ); // Q(st_ivas->q_data_fx) -> Q11
466 : }
467 35000 : st_ivas->q_data_fx = Q11;
468 35000 : move16();
469 :
470 35000 : IF( st_ivas->ism_mode == ISM_MODE_NONE )
471 : {
472 : /*once SBA and ISM are combined into SBA signal then disable planar flag*/
473 18690 : hEncoderConfig->sba_planar = 0;
474 18690 : move16();
475 18690 : IF( EQ_16( st_ivas->nchan_transport, 1 ) )
476 : {
477 6610 : hMetaData = st_ivas->hSCE[st_ivas->nSCE - 1]->hMetaData;
478 : }
479 : ELSE
480 : {
481 12080 : hMetaData = st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData;
482 : }
483 :
484 : /* SBA metadata encoding and SBA metadata bitstream writing */
485 18690 : IF( ( error = ivas_spar_enc_fx( st_ivas, data_fx, input_frame, nb_bits_metadata, hMetaData ) ) != IVAS_ERR_OK )
486 : {
487 0 : return error;
488 : }
489 :
490 18690 : hEncoderConfig->sba_planar = planar_sba_orig; /* Q0 */
491 18690 : move16();
492 : }
493 : ELSE
494 : {
495 16310 : n = hEncoderConfig->nchan_ism; /* Q0 */
496 16310 : move16();
497 16310 : hMetaData = st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData;
498 :
499 16310 : IF( ( error = ivas_ism_metadata_enc_fx( &st_ivas->hEncoderConfig->ivas_total_brate, n, st_ivas->hEncoderConfig->nchan_ism, st_ivas->hIsmMetaData, NULL, hMetaData, &nb_bits_metadata[1], 0, st_ivas->ism_mode, NULL, st_ivas->hEncoderConfig->ism_extended_metadata_flag, -256 /* -1.0f in Q8 */, 0, NULL, st_ivas->hCPE[0]->hCoreCoder[0]->ini_frame ) ) != IVAS_ERR_OK )
500 : {
501 0 : return error;
502 : }
503 :
504 : /* SBA metadata encoding and SBA metadata bitstream writing */
505 16310 : IF( ( error = ivas_spar_enc_fx( st_ivas, &data_fx[n], input_frame, nb_bits_metadata, hMetaData ) ) != IVAS_ERR_OK )
506 : {
507 0 : return error;
508 : }
509 :
510 : /* get SBA TCs */
511 16310 : ivas_sba_getTCs_fx( &data_fx[n], st_ivas, input_frame );
512 : }
513 :
514 : /* core-coding of transport channels */
515 35000 : IF( EQ_16( st_ivas->nSCE, 1 ) )
516 : {
517 6610 : IF( ( error = ivas_sce_enc_fx( st_ivas, 0, data_fx[0], st_ivas->q_data_fx, input_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
518 : {
519 0 : return error;
520 : }
521 : }
522 28390 : ELSE IF( EQ_16( st_ivas->nCPE, 1 ) ) /* Stereo DMX */
523 : {
524 5190 : IF( ( error = ivas_cpe_enc_fx( st_ivas, 0, data_fx[0], data_fx[1], st_ivas->q_data_fx, input_frame, nb_bits_metadata[0], NULL ) ) != IVAS_ERR_OK )
525 : {
526 0 : return error;
527 : }
528 : }
529 23200 : ELSE IF( GT_16( st_ivas->nCPE, 1 ) ) /* FOA/HOA format */
530 : {
531 23200 : IF( ( error = ivas_mct_enc_fx( st_ivas, data_fx, st_ivas->q_data_fx, input_frame, nb_bits_metadata[0] ) ) != IVAS_ERR_OK )
532 : {
533 0 : return error;
534 : }
535 : }
536 : }
537 47850 : ELSE IF( EQ_32( ivas_format, MC_FORMAT ) )
538 : {
539 : /* select MC format mode; write MC LS setup; reconfigure the MC format encoder */
540 47850 : IF( ( error = ivas_mc_enc_config_fx( st_ivas ) ) != IVAS_ERR_OK )
541 : {
542 0 : return error;
543 : }
544 :
545 : /* hMetaData = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[st_ivas->nSCE - 1]->hMetaData : st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData; */
546 47850 : IF( st_ivas->nSCE > 0 )
547 : {
548 11050 : hMetaData = st_ivas->hSCE[st_ivas->nSCE - 1]->hMetaData;
549 : }
550 : ELSE
551 : {
552 36800 : hMetaData = st_ivas->hCPE[st_ivas->nCPE - 1]->hMetaData;
553 : }
554 :
555 : /* LFE low pass filter */
556 47850 : ivas_lfe_lpf_enc_apply_fx( st_ivas->hLfeLpf, data_fx[LFE_CHANNEL], input_frame );
557 :
558 : /* LFE channel encoder */
559 47850 : IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) )
560 : {
561 25630 : IF( st_ivas->nSCE > 0 )
562 : {
563 0 : st_ivas->hLFE->hBstr = st_ivas->hSCE[0]->hCoreCoder[0]->hBstr;
564 : }
565 : ELSE
566 : {
567 25630 : st_ivas->hLFE->hBstr = st_ivas->hCPE[0]->hCoreCoder[0]->hBstr;
568 : }
569 :
570 25630 : ivas_lfe_enc_fx( st_ivas->hLFE, data_fx[LFE_CHANNEL], st_ivas->q_data_fx, input_frame, st_ivas->hLFE->hBstr );
571 : }
572 :
573 47850 : IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCT ) )
574 : {
575 25630 : IF( NE_32( ( error = ivas_mct_enc_fx( st_ivas, data_fx, st_ivas->q_data_fx, input_frame, 0 ) ), IVAS_ERR_OK ) )
576 : {
577 0 : return error;
578 : }
579 : }
580 22220 : ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMUPMIX ) )
581 : {
582 : /* encode MC ParamUpmix parameters and write bitstream */
583 790 : ivas_mc_paramupmix_enc_fx( st_ivas, hMetaData, data_fx, input_frame );
584 :
585 : /* st_ivas->hLFE->hBstr = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[0]->hCoreCoder[0]->hBstr : st_ivas->hCPE[0]->hCoreCoder[0]->hBstr; */
586 790 : IF( st_ivas->nSCE > 0 )
587 : {
588 0 : st_ivas->hLFE->hBstr = st_ivas->hSCE[0]->hCoreCoder[0]->hBstr;
589 : }
590 : ELSE
591 : {
592 790 : st_ivas->hLFE->hBstr = st_ivas->hCPE[0]->hCoreCoder[0]->hBstr;
593 : }
594 :
595 790 : ivas_lfe_enc_fx( st_ivas->hLFE, data_fx[LFE_CHANNEL], st_ivas->q_data_fx, input_frame, st_ivas->hLFE->hBstr );
596 :
597 790 : IF( ( error = ivas_mct_enc_fx( st_ivas, data_fx, st_ivas->q_data_fx, input_frame, hMetaData->nb_bits_tot ) ) != IVAS_ERR_OK )
598 : {
599 0 : return error;
600 : }
601 : }
602 21430 : ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) )
603 : {
604 : /* encode Parametric MC parameters and write bitstream */
605 9960 : ivas_param_mc_enc_fx( st_ivas, hMetaData, data_fx, input_frame );
606 :
607 9960 : IF( EQ_16( st_ivas->nCPE, 1 ) ) /* Stereo DMX */
608 : {
609 9850 : IF( ( error = ivas_cpe_enc_fx( st_ivas, 0, data_fx[0], data_fx[1], st_ivas->q_data_fx, input_frame, hMetaData->nb_bits_tot, NULL ) ) != IVAS_ERR_OK )
610 : {
611 0 : return error;
612 : }
613 : }
614 110 : ELSE IF( GT_16( st_ivas->nCPE, 1 ) )
615 : {
616 110 : IF( ( error = ivas_mct_enc_fx( st_ivas, data_fx, st_ivas->q_data_fx, input_frame, hMetaData->nb_bits_tot ) ) != IVAS_ERR_OK )
617 : {
618 0 : return error;
619 : }
620 : }
621 : }
622 11470 : ELSE IF( EQ_32( st_ivas->mc_mode, MC_MODE_MCMASA ) )
623 : {
624 11470 : if ( st_ivas->hMcMasa->separateChannelEnabled )
625 : {
626 625 : hMetaData = st_ivas->hCPE[0]->hMetaData; /* Metadata is always with CPE in the case of separated channel */
627 : }
628 :
629 88310 : FOR( i = 0; i < nchan_inp; i++ )
630 : {
631 76840 : scale_sig32( data_fx[i], input_frame, sub( Q8, st_ivas->q_data_fx ) ); // st_ivas->q_data_fx -> Q8
632 : }
633 11470 : st_ivas->q_data_fx = Q8;
634 11470 : move16();
635 :
636 11470 : ivas_mcmasa_enc_fx( st_ivas->hMcMasa, st_ivas->hQMetaData, st_ivas->hMasa, data_fx, input_frame, st_ivas->nchan_transport, nchan_inp, st_ivas->q_data_fx );
637 :
638 88310 : FOR( i = 0; i < nchan_inp; i++ )
639 : {
640 76840 : scale_sig32( data_fx[i], input_frame, sub( Q11, st_ivas->q_data_fx ) ); // st_ivas->q_data_fx -> Q11
641 : }
642 11470 : st_ivas->q_data_fx = Q11;
643 11470 : move16();
644 :
645 : /* Scaling down the Q-factor of energy_ratio_fx from Q31 to Q30 */
646 68820 : FOR( i = 0; i < st_ivas->hMcMasa->nbands; i++ )
647 : {
648 57350 : scale_sig32( st_ivas->hQMetaData->q_direction[0].band_data[i].energy_ratio_fx, MAX_PARAM_SPATIAL_SUBFRAMES, -1 ); // Q31 -> Q30
649 : }
650 :
651 11470 : IF( ( error = ivas_masa_encode_fx( st_ivas->hMasa, st_ivas->hQMetaData, hMetaData, &nb_bits_metadata[0], st_ivas->nchan_transport, ivas_format, ivas_total_brate, 0, -1,
652 : ISM_MODE_NONE, -1, NULL, -1, NULL, 0, 0 ) ) !=
653 : IVAS_ERR_OK )
654 : {
655 0 : return error;
656 : }
657 :
658 11470 : IF( st_ivas->hMcMasa->separateChannelEnabled )
659 : {
660 625 : IF( ( error = ivas_sce_enc_fx( st_ivas, 0, data_fx[2], st_ivas->q_data_fx, input_frame, 0 ) ) != IVAS_ERR_OK )
661 : {
662 0 : return error;
663 : }
664 :
665 625 : st_ivas->hCPE[0]->hCoreCoder[0]->hBstr->ind_list = st_ivas->hSCE[0]->hCoreCoder[0]->hBstr->ind_list + st_ivas->hSCE[0]->hCoreCoder[0]->hBstr->nb_ind_tot;
666 :
667 625 : IF( ( error = ivas_cpe_enc_fx( st_ivas, 0, data_fx[0], data_fx[1], st_ivas->q_data_fx, input_frame, hMetaData->nb_bits_tot, NULL ) ) != IVAS_ERR_OK )
668 : {
669 0 : return error;
670 : }
671 : }
672 : ELSE
673 : {
674 10845 : IF( EQ_16( st_ivas->nSCE, 1 ) )
675 : {
676 10425 : IF( ( error = ivas_sce_enc_fx( st_ivas, 0, data_fx[0], st_ivas->q_data_fx, input_frame, hMetaData->nb_bits_tot ) ) != IVAS_ERR_OK )
677 : {
678 0 : return error;
679 : }
680 : }
681 420 : ELSE IF( EQ_16( st_ivas->nCPE, 1 ) ) /* Stereo DMX */
682 : {
683 420 : IF( ( error = ivas_cpe_enc_fx( st_ivas, 0, data_fx[0], data_fx[1], st_ivas->q_data_fx, input_frame, hMetaData->nb_bits_tot, NULL ) ) != IVAS_ERR_OK )
684 : {
685 0 : return error;
686 : }
687 : }
688 : }
689 : }
690 : }
691 :
692 : /*----------------------------------------------------------------*
693 : * Common updates
694 : *----------------------------------------------------------------*/
695 :
696 421288 : hEncoderConfig->last_ivas_total_brate = ivas_total_brate;
697 421288 : move32();
698 :
699 : #ifdef DEBUG_MODE_INFO
700 : {
701 : float tmpF = ivas_total_brate / 1000.0f;
702 : dbgwrite( &tmpF, sizeof( float ), 1, input_frame, "res/ivas_total_brate" );
703 : }
704 : #endif
705 :
706 421288 : pop_wmops();
707 421288 : return error;
708 : }
|