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 "cnst.h"
37 : #include "prot.h"
38 : #include "ivas_prot.h"
39 : #include "rom_com.h"
40 : #ifdef DEBUGGING
41 : #include "debug.h"
42 : #endif
43 : #include "wmc_auto.h"
44 :
45 :
46 : /*----------------------------------------------------------*
47 : * FindChannelRatio()
48 : *
49 : * find ratio of channel energy to total energy for
50 : * bit distribution
51 : *----------------------------------------------------------*/
52 :
53 1461203 : static void FindChannelRatio(
54 : Encoder_State **sts, /* i/o: encoder state structure */
55 : int16_t chBitRatios[MCT_MAX_CHANNELS], /* o : bit-disctribution channel ratios */
56 : const int16_t nChannels /* i : number of channels to be coded */
57 : )
58 : {
59 : float sum_nrg, chRatio;
60 : int16_t i;
61 : float nrg[MCT_MAX_CHANNELS];
62 :
63 1461203 : set_f( nrg, 0, MCT_MAX_CHANNELS );
64 :
65 1461203 : getChannelEnergies( sts, nrg, nChannels );
66 :
67 : /*calculate total energy without LFE*/
68 1461203 : sum_nrg = 0;
69 8759520 : for ( i = 0; i < nChannels; i++ )
70 : {
71 7298317 : if ( sts[i]->mct_chan_mode != MCT_CHAN_MODE_IGNORE )
72 : {
73 7143418 : sum_nrg += nrg[i];
74 : }
75 : }
76 1461203 : sum_nrg = 1.0f / max( sum_nrg, EPSILON );
77 :
78 8759520 : for ( i = 0; i < nChannels; i++ )
79 : {
80 7298317 : if ( sts[i]->mct_chan_mode != MCT_CHAN_MODE_IGNORE )
81 : {
82 7143418 : chRatio = nrg[i] * sum_nrg;
83 7143418 : chBitRatios[i] = min( BITRATE_MCT_RATIO_RANGE - 1, max( 1, (uint16_t) ( BITRATE_MCT_RATIO_RANGE * chRatio + 0.5f ) ) );
84 : }
85 154899 : else if ( sts[i]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
86 : {
87 154899 : chBitRatios[i] = 0;
88 : }
89 : }
90 :
91 : #ifdef DEBUG_MODE_MDCT
92 : for ( i = 0; i < nChannels; i++ )
93 : {
94 : dbgwrite( &chBitRatios[i], sizeof( int16_t ), 1, 1, "./res/chBitRatio" );
95 : /* dbgwrite(&chRatio[i], sizeof(float),1,1,"./res/chRatio");*/
96 : }
97 : #endif
98 :
99 1461203 : return;
100 : }
101 :
102 :
103 : /*----------------------------------------------------------*
104 : * AdjustChannelRatios()
105 : *
106 : * adjust ratio of channels for bit distribution
107 : *----------------------------------------------------------*/
108 :
109 413043 : static void AdjustChannelRatios(
110 : int16_t chBitRatios[MCT_MAX_CHANNELS], /* o : bit-disctribution channel ratios */
111 : const int16_t nChannels, /* i/o: number of channels */
112 : const int32_t ivas_total_brate, /* i : IVAS total bitrate */
113 : const int16_t nAvailBits, /* i : number of available bits */
114 : const int16_t sba_order /* i : Ambisonic (SBA) order */
115 : )
116 : {
117 : int16_t force_ch_bit_ratios[IVAS_SPAR_MAX_DMX_CHS];
118 : int32_t temp_brs[IVAS_SPAR_MAX_DMX_CHS];
119 : float cur_ratio, tar_ratio, sum_ratio, sum_tar_ratio;
120 : int16_t ratio_diff, i;
121 :
122 413043 : ivas_spar_bitrate_dist( temp_brs, nAvailBits, ivas_total_brate, sba_order, (int16_t) FB );
123 :
124 413043 : sum_ratio = 0.0f;
125 2065215 : for ( i = 0; i < nChannels; i++ )
126 : {
127 1652172 : sum_ratio += (float) temp_brs[i];
128 : }
129 2065215 : for ( i = 0; i < nChannels; i++ )
130 : {
131 1652172 : cur_ratio = temp_brs[i] / sum_ratio;
132 1652172 : force_ch_bit_ratios[i] = min( BITRATE_MCT_RATIO_RANGE - 1, max( 1, (uint16_t) ( BITRATE_MCT_RATIO_RANGE * cur_ratio + 0.5f ) ) );
133 : }
134 :
135 : /* adjust the ratios further based on received chBitRatios[]*/
136 413043 : ratio_diff = 0;
137 413043 : sum_ratio = 0.0f;
138 413043 : sum_tar_ratio = 0.0f;
139 2065215 : for ( i = 0; i < nChannels; i++ )
140 : {
141 1652172 : sum_ratio += (float) chBitRatios[i];
142 1652172 : sum_tar_ratio += (float) force_ch_bit_ratios[i];
143 : }
144 826086 : for ( i = 3; i < nChannels; i++ )
145 : {
146 413043 : cur_ratio = (float) chBitRatios[i] / ( sum_ratio + FLT_MIN );
147 413043 : tar_ratio = (float) force_ch_bit_ratios[i] / ( sum_tar_ratio + FLT_MIN );
148 :
149 413043 : tar_ratio = min( tar_ratio, cur_ratio );
150 413043 : chBitRatios[i] = (int16_t) ( tar_ratio * sum_tar_ratio );
151 413043 : chBitRatios[i] = max( 1, chBitRatios[i] );
152 413043 : assert( chBitRatios[i] < ( BITRATE_MCT_RATIO_RANGE - 1 ) );
153 413043 : ratio_diff += force_ch_bit_ratios[i] - chBitRatios[i];
154 : }
155 :
156 1652172 : for ( i = 0; i < min( 3, nChannels ); i++ )
157 : {
158 1239129 : assert( force_ch_bit_ratios[i] >= 0 );
159 1239129 : chBitRatios[i] = min( BITRATE_MCT_RATIO_RANGE - 1, force_ch_bit_ratios[i] );
160 : }
161 413043 : chBitRatios[1] += ratio_diff;
162 :
163 : /* make sure final ratios are within range*/
164 413043 : sum_ratio = 0.0f;
165 2065215 : for ( i = 0; i < nChannels; i++ )
166 : {
167 1652172 : sum_ratio += (float) chBitRatios[i];
168 : }
169 2065215 : for ( i = 0; i < nChannels; i++ )
170 : {
171 1652172 : cur_ratio = chBitRatios[i] / sum_ratio;
172 1652172 : chBitRatios[i] = min( BITRATE_MCT_RATIO_RANGE - 1, max( 1, (uint16_t) ( BITRATE_MCT_RATIO_RANGE * cur_ratio + 0.5f ) ) );
173 : }
174 :
175 413043 : return;
176 : }
177 :
178 :
179 : /*-------------------------------------------------------------------*
180 : * ivas_mct_core_enc()
181 : *
182 : * joint mct encoding
183 : *-------------------------------------------------------------------*/
184 :
185 1461203 : void ivas_mct_core_enc(
186 : const IVAS_FORMAT ivas_format, /* i : IVAS format */
187 : MCT_ENC_HANDLE hMCT, /* i/o: MCT encoder structure */
188 : CPE_ENC_HANDLE hCPE[MCT_MAX_BLOCKS], /* i/o: CPE encoder structures */
189 : const int16_t nChannels, /* i : number of channels to be coded */
190 : const int32_t ivas_total_brate, /* i : IVAS total bitrate */
191 : const int16_t switch_bw, /* i : flag bandwidth switch occurance */
192 : const int16_t lfe_bits, /* i : bits spent for LFE */
193 : const int16_t sba_order /* i : Ambisonic (SBA) order */
194 : )
195 : {
196 : int16_t ch, ch_core, nSubframes, L_subframeTCX;
197 : int16_t i, cpe_id, n, nAvailBits;
198 : int16_t nCPE;
199 : float *orig_spectrum[MCT_MAX_CHANNELS][2]; /* Pointers to MDCT output for a short block (L/R) */
200 : float powerSpecMsInv_long_cpe0[CPE_CHANNELS][L_FRAME_PLUS];
201 : float inv_spectrum_long_cpe0[CPE_CHANNELS][L_FRAME_PLUS];
202 : float powerSpec_long_cpe0[CPE_CHANNELS][L_FRAME_PLUS];
203 : float powerSpec_long[MCT_MAX_CHANNELS - CPE_CHANNELS][L_FRAME48k];
204 : float inv_spectrum_long[MCT_MAX_CHANNELS - CPE_CHANNELS][L_FRAME48k]; /* quantized MDCT spectrum, inv ms mask mdst spectrum, scratch for MS spectra in the MS decision */
205 : float powerSpecMsInv_long[MCT_MAX_CHANNELS - CPE_CHANNELS][L_FRAME48k]; /* MS inv power spectrum, also inverse MDST spectrum */
206 : float *powerSpec[MCT_MAX_CHANNELS];
207 : float *powerSpecMsInv[MCT_MAX_CHANNELS][2];
208 : float *inv_mdst_spectrum[MCT_MAX_CHANNELS][2];
209 : float *inv_spectrum[MCT_MAX_CHANNELS][2];
210 : float *mdst_spectrum[MCT_MAX_CHANNELS][2];
211 : int16_t total_side_bits;
212 : int16_t chBitRatios[MCT_MAX_CHANNELS];
213 : Encoder_State *sts[MCT_MAX_CHANNELS];
214 : Encoder_State *st;
215 : int16_t sp_aud_decision0[MCT_MAX_CHANNELS];
216 : BSTR_ENC_HANDLE hBstr;
217 : float mdst;
218 : #ifdef DEBUGGING
219 : int32_t total_brate = 0;
220 : int16_t sba_meta = 0;
221 : int16_t format_bits = 0;
222 : int16_t mct_bits = 0;
223 : #endif
224 :
225 : #if defined( DEBUG_MODE_MDCT ) && defined( DEBUG_PLOT_BITS )
226 : static FILE *f_bit_split = 0;
227 : #endif
228 :
229 1461203 : push_wmops( "mct_encoding" );
230 :
231 : /*--------------------------------------------------------------*
232 : * Initialization
233 : *---------------------------------------------------------------*/
234 :
235 1461203 : total_side_bits = 0;
236 :
237 1461203 : nCPE = nChannels / CPE_CHANNELS;
238 :
239 : /*in case of odd number of channels*/
240 1461203 : if ( ( nCPE * CPE_CHANNELS ) != nChannels )
241 : {
242 876995 : nCPE++;
243 : }
244 :
245 : /* point first CPE channels to longer buffers where switching from ACELP to TCX may occur in SBA with DTX (total memory saving)*/
246 4383609 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
247 : {
248 2922406 : inv_mdst_spectrum[ch][0] = powerSpecMsInv[ch][0] = powerSpecMsInv_long_cpe0[ch];
249 2922406 : inv_mdst_spectrum[ch][1] = powerSpecMsInv[ch][1] = powerSpecMsInv_long_cpe0[ch] + N_TCX10_MAX;
250 2922406 : inv_spectrum[ch][0] = inv_spectrum_long_cpe0[ch];
251 2922406 : inv_spectrum[ch][1] = inv_spectrum_long_cpe0[ch] + N_TCX10_MAX;
252 2922406 : powerSpec[ch] = powerSpec_long_cpe0[ch];
253 : }
254 :
255 5837114 : for ( ch = CPE_CHANNELS; ch < nChannels; ch++ )
256 : {
257 4375911 : inv_mdst_spectrum[ch][0] = powerSpecMsInv[ch][0] = powerSpecMsInv_long[ch - CPE_CHANNELS];
258 4375911 : inv_mdst_spectrum[ch][1] = powerSpecMsInv[ch][1] = powerSpecMsInv_long[ch - CPE_CHANNELS] + N_TCX10_MAX;
259 4375911 : inv_spectrum[ch][0] = inv_spectrum_long[ch - CPE_CHANNELS];
260 4375911 : inv_spectrum[ch][1] = inv_spectrum_long[ch - CPE_CHANNELS] + N_TCX10_MAX;
261 4375911 : powerSpec[ch] = powerSpec_long[ch - CPE_CHANNELS];
262 : }
263 :
264 5548859 : for ( cpe_id = 0, i = 0; cpe_id < nCPE; cpe_id++ )
265 : {
266 12262968 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
267 : {
268 8175312 : if ( cpe_id * CPE_CHANNELS + ch < nChannels )
269 : {
270 7298317 : sts[i] = hCPE[cpe_id]->hCoreCoder[ch];
271 : }
272 :
273 8175312 : if ( hCPE[cpe_id]->hCoreCoder[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
274 : {
275 1031894 : i++;
276 1031894 : continue;
277 : }
278 :
279 7143418 : mdst_spectrum[i][0] = hMCT->p_mdst_spectrum_long[cpe_id][ch];
280 7143418 : mdst_spectrum[i][1] = hMCT->p_mdst_spectrum_long[cpe_id][ch] + N_TCX10_MAX;
281 7143418 : orig_spectrum[i][0] = hMCT->p_orig_spectrum_long[cpe_id][ch];
282 7143418 : orig_spectrum[i][1] = hMCT->p_orig_spectrum_long[cpe_id][ch] + N_TCX10_MAX;
283 :
284 7143418 : sp_aud_decision0[i] = hCPE[cpe_id]->hCoreCoder[0]->sp_aud_decision0;
285 :
286 7143418 : sts[i]->hTcxEnc->tns_ms_flag[0] = 0;
287 7143418 : sts[i]->hTcxEnc->tns_ms_flag[1] = 0;
288 :
289 7143418 : i++;
290 : }
291 : }
292 :
293 1461203 : hBstr = sts[0]->hBstr;
294 :
295 4671864 : for ( ch = 0; ch < (int16_t) ( hMCT->nchan_out_woLFE * 0.5 ); ch++ )
296 : {
297 3210661 : ch_core = ch * CPE_CHANNELS;
298 :
299 3210661 : if ( switch_bw )
300 : {
301 : H_IGF_GRID igf_grid;
302 :
303 6322 : igf_grid = sts[ch_core]->igf ? sts[ch_core]->hIGFEnc->igfData.igfInfo.grid : NULL;
304 6322 : initMdctStereoEncData( hMCT->hBlockData[ch]->hStereoMdct, ivas_format, sts[ch_core]->element_mode, sts[ch_core]->element_brate, sts[ch_core]->bwidth, sts[ch_core]->igf, igf_grid, 0 );
305 : }
306 :
307 3210661 : if ( sts[ch_core]->igf )
308 : {
309 : /* calculate the igf start band from the igf start line */
310 1730999 : stereo_mdct_init_igf_start_band( &hMCT->hBlockData[ch]->hStereoMdct->stbParamsTCX20, 1.0f, sts[ch_core]->bwidth, sts[ch_core]->total_brate );
311 :
312 1730999 : stereo_mdct_init_igf_start_band( &hMCT->hBlockData[ch]->hStereoMdct->stbParamsTCX10, 0.5f, sts[ch_core]->bwidth, sts[ch_core]->total_brate );
313 : }
314 : else
315 : {
316 1479662 : hMCT->hBlockData[ch]->hStereoMdct->stbParamsTCX20.sfbIgfStart = -1;
317 1479662 : hMCT->hBlockData[ch]->hStereoMdct->stbParamsTCX10.sfbIgfStart = -1;
318 1479662 : hMCT->hBlockData[ch]->hStereoMdct->stbParamsTCX10.nBandsStereoCore = hMCT->hBlockData[ch]->hStereoMdct->stbParamsTCX10.sfbCnt;
319 1479662 : hMCT->hBlockData[ch]->hStereoMdct->stbParamsTCX20.nBandsStereoCore = hMCT->hBlockData[ch]->hStereoMdct->stbParamsTCX20.sfbCnt;
320 : }
321 : }
322 :
323 8759520 : for ( ch = 0; ch < nChannels; ch++ )
324 : {
325 7298317 : st = sts[ch];
326 :
327 7298317 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
328 : {
329 154899 : continue;
330 : }
331 :
332 7143418 : nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
333 :
334 14445792 : for ( n = 0; n < nSubframes; n++ )
335 : {
336 7302374 : st->side_bits_frame_channel += NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL;
337 : }
338 :
339 : /* Add estimated stereo bits */
340 7143418 : st->side_bits_frame_channel += NBBITS_MCT_RATIO;
341 : }
342 :
343 : /*--------------------------------------------------------------*
344 : * MCT algorithm
345 : *---------------------------------------------------------------*/
346 :
347 1461203 : apply_MCT_enc( hMCT, sts, mdst_spectrum, inv_spectrum, inv_mdst_spectrum, nChannels );
348 :
349 : /*--------------------------------------------------------------*
350 : * Power spectrum calculation for Noise Estimation
351 : *---------------------------------------------------------------*/
352 :
353 8759520 : for ( ch = 0; ch < nChannels; ch++ )
354 : {
355 7298317 : if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
356 : {
357 154899 : continue;
358 : }
359 7143418 : nSubframes = ( sts[ch]->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
360 7143418 : L_subframeTCX = sts[ch]->hTcxEnc->L_frameTCX / nSubframes;
361 : /* in MCT only relevant for bitrate switching from non-MCT bitrates */
362 7143418 : if ( sts[ch]->last_core == ACELP_CORE )
363 : {
364 3 : L_subframeTCX += L_subframeTCX / 4;
365 : }
366 :
367 14445792 : for ( n = 0; n < nSubframes; n++ )
368 : {
369 7302374 : if ( sts[ch]->hTcxEnc->tns_ms_flag[n] )
370 : {
371 : /* power spectrum: MDCT^2 + MDST^2 */
372 2468427444 : for ( i = 0; i < L_subframeTCX; i++ )
373 : {
374 2465706240 : powerSpec[ch][i + n * L_subframeTCX] = mdst_spectrum[ch][n][i] * mdst_spectrum[ch][n][i] + sts[ch]->hTcxEnc->spectrum[n][i] * sts[ch]->hTcxEnc->spectrum[n][i];
375 2465706240 : powerSpecMsInv[ch][n][i] = inv_mdst_spectrum[ch][n][i] * inv_mdst_spectrum[ch][n][i] + inv_spectrum[ch][n][i] * inv_spectrum[ch][n][i];
376 : }
377 : }
378 : else
379 : {
380 4581170 : if ( hMCT->currBlockDataCnt > 0 )
381 : {
382 : /* power spectrum: MDCT^2 + MDST^2 */
383 1639071 : powerSpecMsInv[ch][n][0] = inv_spectrum[ch][n][0] * inv_spectrum[ch][n][0];
384 :
385 1390623969 : for ( i = 1; i < L_subframeTCX - 1; i++ )
386 : {
387 1388984898 : mdst = ( inv_spectrum[ch][n][i + 1] - inv_spectrum[ch][n][i - 1] ); /* An MDST estimate */
388 1388984898 : powerSpecMsInv[ch][n][i] = mdst * mdst + inv_spectrum[ch][n][i] * inv_spectrum[ch][n][i];
389 : }
390 :
391 1639071 : powerSpecMsInv[ch][n][L_subframeTCX - 1] = inv_spectrum[ch][n][L_subframeTCX - 1] * inv_spectrum[ch][n][L_subframeTCX - 1];
392 : }
393 :
394 : /* power spectrum: MDCT^2 + MDST^2 */
395 4581170 : powerSpec[ch][n * L_subframeTCX] = sts[ch]->hTcxEnc->spectrum[n][0] * sts[ch]->hTcxEnc->spectrum[n][0];
396 :
397 3663663950 : for ( i = 1; i < L_subframeTCX - 1; i++ )
398 : {
399 3659082780 : mdst = ( sts[ch]->hTcxEnc->spectrum[n][i + 1] - sts[ch]->hTcxEnc->spectrum[n][i - 1] ); /* An MDST estimate */
400 3659082780 : powerSpec[ch][i + n * L_subframeTCX] = mdst * mdst + sts[ch]->hTcxEnc->spectrum[n][i] * sts[ch]->hTcxEnc->spectrum[n][i];
401 : }
402 4581170 : powerSpec[ch][L_subframeTCX - 1 + n * L_subframeTCX] = sts[ch]->hTcxEnc->spectrum[n][L_subframeTCX - 1] * sts[ch]->hTcxEnc->spectrum[n][L_subframeTCX - 1];
403 : }
404 : }
405 : }
406 :
407 8759520 : for ( ch = 0; ch < nChannels; ch++ )
408 : {
409 7298317 : st = sts[ch];
410 7298317 : if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
411 : {
412 154899 : continue;
413 : }
414 7143418 : nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
415 7143418 : L_subframeTCX = st->hTcxEnc->L_frameTCX / nSubframes;
416 : /* in MCT only relevant for bitrate switching from non-MCT bitrates */
417 7143418 : if ( st->last_core == ACELP_CORE )
418 : {
419 3 : L_subframeTCX += L_subframeTCX / 4;
420 : }
421 :
422 7143418 : if ( ( st->hTcxEnc->tcxMode == TCX_20 ) && ( st->total_brate < HQ_96k || st->igf ) )
423 : {
424 : /*-----------------------------------------------------------*
425 : * Compute noise-measure flags for spectrum filling *
426 : * and quantization (0: tonal, 1: noise-like). *
427 : * Detect low pass if present. *
428 : *-----------------------------------------------------------*/
429 :
430 4180553 : ComputeSpectrumNoiseMeasure( powerSpec[ch], st->hTcxEnc->L_frameTCX, st->hTcxEnc->nmStartLine, ( st->L_frame * st->last_sr_core != st->L_frame_past * st->sr_core ) || ( st->last_core != TCX_20_CORE ), st->hTcxEnc->memQuantZeros, st->hTcxEnc->L_frameTCX );
431 : }
432 :
433 7143418 : st->hTcxEnc->measuredBwRatio = 1.f; /* No bandwidth limit for the noise filling */
434 : }
435 :
436 1461203 : if ( sts[0]->igf )
437 : {
438 778511 : if ( hMCT->currBlockDataCnt > 0 )
439 : {
440 495569 : mctStereoIGF_enc( hMCT, sts, orig_spectrum, powerSpec, powerSpecMsInv, inv_spectrum, sp_aud_decision0 );
441 : }
442 : else
443 : {
444 1374526 : for ( ch = 0; ch < nChannels; ch++ )
445 : {
446 1091584 : st = sts[ch];
447 :
448 : /* update the pointer to the buffer of indices of the second channel */
449 1091584 : if ( ch > 0 )
450 : {
451 808642 : st->hBstr->ind_list = sts[0]->hBstr->ind_list + sts[0]->hBstr->nb_ind_tot;
452 : }
453 1091584 : if (
454 1091584 : sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
455 : {
456 22114 : continue;
457 : }
458 1069470 : nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
459 1069470 : L_subframeTCX = st->hTcxEnc->L_frameTCX / nSubframes;
460 :
461 1069470 : if ( st->igf )
462 : {
463 2163796 : for ( n = 0; n < nSubframes; n++ )
464 : {
465 1094326 : ProcessIGF( st, st->hTcxEnc->spectrum[n], orig_spectrum[ch][n], &powerSpec[ch][n * L_subframeTCX], st->core == TCX_20_CORE, n, sp_aud_decision0[ch], 0 );
466 : }
467 : }
468 : }
469 : }
470 :
471 : /*write IGF data to bitstream*/
472 4868996 : for ( ch = 0; ch < nChannels; ch++ )
473 : {
474 4090485 : st = sts[ch];
475 4090485 : if (
476 4090485 : sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
477 : {
478 32749 : continue;
479 : }
480 4057736 : enc_prm_igf_mdct( st, hBstr );
481 : }
482 : }
483 :
484 : /* correct side bits per channel*/
485 8759520 : for ( ch = 0; ch < nChannels; ch++ )
486 : {
487 7298317 : st = sts[ch];
488 :
489 7298317 : if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
490 : {
491 154899 : continue;
492 : }
493 :
494 7143418 : st->side_bits_frame_channel -= NBBITS_MCT_RATIO; /* Subtract estimated stereo bits */
495 7143418 : total_side_bits += st->side_bits_frame_channel;
496 : }
497 :
498 : /*--------------------------------------------------------------*
499 : * Bitrate distribution among channels and MCT bitstream writing
500 : *---------------------------------------------------------------*/
501 :
502 1461203 : write_mct_bitstream( sts, hMCT, nChannels );
503 :
504 1461203 : FindChannelRatio( sts, chBitRatios, nChannels );
505 :
506 1461203 : nAvailBits = (int16_t) ( ( ivas_total_brate / FRAMES_PER_SEC ) - NBITS_BWIDTH - hMCT->nBitsMCT - lfe_bits );
507 :
508 :
509 : /* subtract IVAS format signaling bits */
510 1461203 : if ( ivas_format == MC_FORMAT )
511 : {
512 430182 : nAvailBits -= IVAS_FORMAT_SIGNALING_NBITS;
513 430182 : nAvailBits -= MC_LS_SETUP_BITS;
514 : }
515 1031021 : else if ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT )
516 : {
517 1031021 : nAvailBits -= IVAS_FORMAT_SIGNALING_NBITS_EXTENDED;
518 1031021 : nAvailBits -= SBA_ORDER_BITS + SBA_PLANAR_BITS;
519 :
520 : /*MCT is used at bitrates > 80 kbps and additional 1 bit is present at these bitrates*/
521 1031021 : if ( ivas_format == SBA_ISM_FORMAT )
522 : {
523 242279 : nAvailBits -= IVAS_COMBINED_FORMAT_SIGNALLING_BITS;
524 : }
525 : }
526 :
527 5548859 : for ( cpe_id = 0, i = 0; cpe_id < nCPE; cpe_id++ )
528 : {
529 4087656 : if ( hCPE[cpe_id]->hMetaData != NULL )
530 : {
531 1461203 : nAvailBits -= hCPE[cpe_id]->hMetaData->nb_bits_tot;
532 : }
533 : }
534 :
535 : /*substract bits needed for the bitrate ratios */
536 8759520 : for ( ch = 0; ch < nChannels; ch++ )
537 : {
538 7298317 : if (
539 7298317 : sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
540 : {
541 154899 : continue;
542 : }
543 7143418 : nAvailBits -= NBBITS_MCT_RATIO;
544 : }
545 1461203 : nAvailBits -= total_side_bits + hMCT->nchan_out_woLFE; /* if MC 1 extra bit that was initially send to signal LFE_off */
546 :
547 : #ifdef DEBUG_MODE_MDCT
548 : dbgwrite( &nAvailBits, sizeof( int16_t ), 1, 1, "./res/availBits" );
549 : #endif
550 :
551 1461203 : if ( hMCT->hbr_mct )
552 : {
553 413043 : assert( ivas_total_brate >= IVAS_256k );
554 413043 : AdjustChannelRatios( chBitRatios, nChannels, ivas_total_brate, nAvailBits, sba_order );
555 : }
556 :
557 8759520 : for ( ch = 0; ch < nChannels; ch++ )
558 : {
559 7298317 : if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
560 : {
561 154899 : continue;
562 : }
563 :
564 7143418 : push_next_indice( hBstr, chBitRatios[ch], NBBITS_MCT_RATIO );
565 : #ifdef DEBUGGING
566 : mct_bits += NBBITS_MCT_RATIO;
567 : #endif
568 : }
569 :
570 : /*distribute bits amongst channels*/
571 1461203 : splitAvailableBitsMCT( (void **) sts, nAvailBits, chBitRatios, ENC, nChannels );
572 :
573 5548859 : for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
574 : {
575 12262968 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
576 : {
577 8175312 : st = hCPE[cpe_id]->hCoreCoder[ch];
578 8175312 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
579 : {
580 1030548 : continue;
581 : }
582 7144764 : st->total_brate = ( st->bits_frame_channel + st->side_bits_frame_channel ) * FRAMES_PER_SEC;
583 :
584 : #ifdef DEBUGGING
585 : total_brate += st->total_brate;
586 : #endif
587 : }
588 : #ifdef DEBUGGING
589 : if ( hCPE[cpe_id]->hMetaData != NULL )
590 : {
591 : sba_meta += hCPE[cpe_id]->hMetaData->nb_bits_tot;
592 : }
593 : #endif
594 : }
595 :
596 : #ifdef DEBUGGING
597 : format_bits = ( ivas_format == MC_FORMAT ? IVAS_FORMAT_SIGNALING_NBITS + MC_LS_SETUP_BITS : IVAS_FORMAT_SIGNALING_NBITS_EXTENDED + SBA_ORDER_BITS + SBA_PLANAR_BITS );
598 :
599 : format_bits += ( ivas_format == SBA_ISM_FORMAT );
600 :
601 : mct_bits += hMCT->nBitsMCT + hMCT->nchan_out_woLFE;
602 : assert( ( total_brate + ( NBITS_BWIDTH + format_bits + mct_bits + sba_meta + lfe_bits ) * FRAMES_PER_SEC ) == ivas_total_brate );
603 : #endif
604 :
605 1461203 : pop_wmops();
606 :
607 1461203 : return;
608 : }
|