Line data Source code
1 : /******************************************************************************
2 : * ETSI TS 103 634 V1.5.1 *
3 : * Low Complexity Communication Codec Plus (LC3plus) *
4 : * *
5 : * Copyright licence is solely granted through ETSI Intellectual Property *
6 : * Rights Policy, 3rd April 2019. No patent licence is granted by implication, *
7 : * estoppel or otherwise. *
8 : ******************************************************************************/
9 :
10 : #include "options.h"
11 : #include "wmc_auto.h"
12 : #include "setup_dec_lc3plus.h"
13 : #include "functions.h"
14 : #include <stdio.h>
15 : #include <assert.h>
16 :
17 : /* if decoder is null only size is reported */
18 : #include "fft/iis_fft.h"
19 :
20 6594 : int alloc_decoder(LC3PLUS_Dec* decoder, int samplerate, int channels)
21 : {
22 6594 : int ch = 0;
23 6594 : size_t size = sizeof(LC3PLUS_Dec);
24 6594 : size_t frame_len = DYN_MAX_LEN_EXT(samplerate);
25 :
26 6594 : void *PlcAdvSetup = NULL;
27 : LC3_FLOAT *pcmbufHist, *harmonicBuf;
28 : LC3_FLOAT *PhECU_oold_grp_shape, *PhECU_old_grp_shape;
29 : LC3_FLOAT *PhECU_xfp;
30 : Complex *PhECU_X_sav_m;
31 : LC3_INT32 *PhECU_plocs;
32 : LC3_FLOAT *PhECU_f0est, *PhECU_mag_chg_1st, *PhECU_Xavg;
33 : LC3_FLOAT *sine_table1_phecu, *sine_table2_phecu;
34 : HANDLE_IIS_FFT handle_fft_phaseecu;
35 : HANDLE_IIS_FFT handle_ifft_phaseecu;
36 : LC3_FLOAT *q_old_res;
37 :
38 6594 : LC3_INT32 * plc_longterm_advc_tdc = NULL, *plc_longterm_advc_ns = NULL;
39 6594 : LC3_INT16 longterm_analysis_counter_max = 0, longterm_analysis_counter_max_bytebuffer = 0;
40 :
41 13188 : for (ch = 0; ch < channels; ch++) {
42 6594 : DecSetup* setup = balloc(decoder, &size, sizeof(DecSetup));
43 :
44 6594 : size_t max_pitch = ceilf(228.0 * CODEC_FS(samplerate) / 12800.0);
45 6594 : size_t pcm_plc_len = max_pitch + frame_len;
46 6594 : pcmbufHist = balloc(decoder, &size, sizeof(LC3_FLOAT) * pcm_plc_len);
47 6594 : harmonicBuf = balloc(decoder, &size, sizeof(LC3_FLOAT) * max_pitch);
48 6594 : PlcAdvSetup = balloc(decoder, &size, sizeof(*setup->PlcAdvSetup));
49 6594 : PhECU_oold_grp_shape = balloc(decoder, &size, sizeof(LC3_FLOAT) *MAX_LGW); /* BASOP Word16 PhECU_oold_grp_shape_fx[MAX_LGW]; */
50 6594 : PhECU_old_grp_shape = balloc(decoder, &size, sizeof(LC3_FLOAT) *MAX_LGW); /* BASOP Word16 PhECU_old_grp_shape_fx[MAX_LGW] ; */
51 6594 : PhECU_xfp = balloc(decoder, &size, sizeof(LC3_FLOAT) *(frame_len * 16 / 10));
52 6594 : PhECU_X_sav_m = balloc(decoder, &size, sizeof(Complex) *(((frame_len * 16 / 10) / 2) + 1));/*MAX_PLC_LMSPEC*/
53 6594 : PhECU_plocs = balloc(decoder, &size, sizeof(LC3_INT32) * (((frame_len * 16 / 10) / 4) + 1 + 1)); /* BASOP Word16 *PhECU_plocs; */
54 :
55 6594 : handle_fft_phaseecu = balloc(decoder, &size, sizeof(IIS_FFT) * 1);
56 6594 : handle_ifft_phaseecu = balloc(decoder, &size, sizeof(IIS_FFT) * 1);
57 6594 : PhECU_f0est = balloc(decoder, &size, sizeof(LC3_FLOAT) * (((frame_len * 16 / 10) / 4) + 1)); /*BASOP Word32 *PhECU_f0est;*/
58 6594 : PhECU_mag_chg_1st = balloc(decoder, &size, sizeof(LC3_FLOAT) *MAX_LGW); /* BASOP Word16 PhECU_mag_chg_1st[MAX_LGW];*/
59 6594 : PhECU_Xavg = balloc(decoder, &size, sizeof(LC3_FLOAT) * MAX_LGW); /* BASOP Word16 PhECU_Xavg[MAX_LGW] ; */
60 :
61 6594 : sine_table1_phecu = balloc(decoder, &size, sizeof(LC3_FLOAT) * (((CODEC_FS(samplerate) * 16) / 1000) / 2 + 1));
62 6594 : sine_table2_phecu = balloc(decoder, &size, sizeof(LC3_FLOAT) * (((CODEC_FS(samplerate) * 16) / 1000) / 2 + 1));
63 :
64 6594 : longterm_analysis_counter_max = plc_fadeout_param_maxlen[0];
65 6594 : longterm_analysis_counter_max_bytebuffer = plc_fadeout_param_maxbytes[0];
66 :
67 6594 : plc_longterm_advc_tdc = balloc(decoder, &size, sizeof(LC3_INT32) * longterm_analysis_counter_max_bytebuffer);
68 6594 : plc_longterm_advc_ns = balloc(decoder, &size, sizeof(LC3_INT32) * longterm_analysis_counter_max_bytebuffer);
69 :
70 6594 : q_old_res = balloc(decoder, &size, sizeof(LC3_FLOAT) * frame_len);
71 :
72 6594 : if (decoder) {
73 2198 : decoder->channel_setup[ch] = setup;
74 :
75 2198 : setup->PlcAdvSetup = PlcAdvSetup;
76 :
77 2198 : setup->PlcAdvSetup->pcmbufHist = pcmbufHist;
78 2198 : setup->PlcAdvSetup->PlcTdcSetup.harmonicBuf = harmonicBuf;
79 2198 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_oold_grp_shape = PhECU_oold_grp_shape;
80 2198 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_old_grp_shape = PhECU_old_grp_shape;
81 2198 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_xfp = PhECU_xfp;
82 2198 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_X_sav_m = PhECU_X_sav_m;
83 2198 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_plocs = PhECU_plocs;
84 2198 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_f0est = PhECU_f0est;
85 2198 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_mag_chg_1st = PhECU_mag_chg_1st;
86 2198 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_Xavg = PhECU_Xavg;
87 2198 : setup->PlcAdvSetup->PlcPhEcuSetup.handle_fft_phaseecu = handle_fft_phaseecu;
88 2198 : setup->PlcAdvSetup->PlcPhEcuSetup.handle_ifft_phaseecu = handle_ifft_phaseecu;
89 :
90 2198 : setup->PlcAdvSetup->PlcPhEcuSetup.handle_fft_phaseecu->sine_table = sine_table1_phecu;
91 2198 : setup->PlcAdvSetup->PlcPhEcuSetup.handle_ifft_phaseecu->sine_table = sine_table2_phecu;
92 :
93 2198 : setup->PlcAdvSetup->longterm_analysis_counter_max = longterm_analysis_counter_max;
94 2198 : setup->PlcAdvSetup->longterm_analysis_counter_max_bytebuffer = longterm_analysis_counter_max_bytebuffer;
95 :
96 2198 : setup->PlcAdvSetup->plc_longterm_advc_tdc = plc_longterm_advc_tdc;
97 2198 : setup->PlcAdvSetup->plc_longterm_advc_ns = plc_longterm_advc_ns;
98 :
99 2198 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_Lprot = (CODEC_FS(samplerate) * 16) / 1000;
100 2198 : real_fft_init(&(setup->PlcAdvSetup->PlcPhEcuSetup.PhEcu_Fft), setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_Lprot, &(setup->PlcAdvSetup->PlcPhEcuSetup.handle_fft_phaseecu));
101 2198 : real_ifft_init(&(setup->PlcAdvSetup->PlcPhEcuSetup.PhEcu_Ifft), setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_Lprot, &(setup->PlcAdvSetup->PlcPhEcuSetup.handle_ifft_phaseecu));
102 2198 : setup->statePC.q_old_res = q_old_res;
103 : }
104 : }
105 :
106 6594 : return (int)size;
107 : }
108 :
109 2198 : LC3PLUS_Error FillDecSetup(LC3PLUS_Dec* decoder, int samplerate, int channels, LC3PLUS_PlcMode plc_mode
110 : , int hrmode
111 : )
112 : {
113 2198 : memset(decoder, 0, lc3plus_dec_get_size(samplerate, channels));
114 2198 : alloc_decoder(decoder, samplerate, channels);
115 :
116 2198 : decoder->fs = CODEC_FS(samplerate);
117 2198 : decoder->fs_out = samplerate;
118 2198 : decoder->fs_idx = FS2FS_IDX(decoder->fs);
119 2198 : decoder->plcMeth = plc_mode;
120 :
121 2198 : decoder->hrmode = hrmode != 0;
122 :
123 2198 : decoder->channels = channels;
124 2198 : decoder->frame_ms = 10;
125 2198 : decoder->frame_dms = 100;
126 2198 : decoder->BW_cutoff_bits = BW_cutoff_bits_all[decoder->fs_idx];
127 :
128 2198 : if (decoder->fs == 8000) {
129 0 : decoder->tilt = 14;
130 2198 : } else if (decoder->fs == 16000) {
131 2 : decoder->tilt = 18;
132 2196 : } else if (decoder->fs == 24000) {
133 0 : decoder->tilt = 22;
134 2196 : } else if (decoder->fs == 32000) {
135 2 : decoder->tilt = 26;
136 2194 : } else if (decoder->fs == 48000) {
137 2194 : decoder->tilt = 30;
138 : }
139 0 : else if (decoder->fs == 96000) {
140 0 : decoder->tilt = 34;
141 : }
142 :
143 2198 : set_dec_frame_params(decoder);
144 :
145 2198 : lc3plus_dec_set_ep_enabled(decoder, 0);
146 :
147 2198 : return LC3PLUS_OK;
148 : }
149 :
150 : /* set frame config params */
151 4395 : void set_dec_frame_params(LC3PLUS_Dec* decoder)
152 : {
153 4395 : int ch = 0;
154 : int n;
155 :
156 4395 : if (decoder->fs_idx == 5)
157 : {
158 0 : decoder->hrmode = 1;
159 : }
160 :
161 4395 : decoder->frame_length = ceil(decoder->fs * 10 / 1000); /* fs * 0.01*2^6 */
162 4395 : if (decoder->hrmode == 1)
163 : {
164 0 : decoder->yLen = decoder->frame_length;
165 : }
166 : else
167 : {
168 4395 : decoder->yLen = MIN(MAX_BW, decoder->frame_length);
169 : }
170 :
171 4395 : decoder->bands_number = 64;
172 4395 : if (decoder->frame_ms == 2.5)
173 : {
174 2 : decoder->frame_length = decoder->frame_length >> 2;
175 2 : decoder->yLen /= 4;
176 2 : if (decoder->hrmode)
177 : {
178 0 : decoder->bands_number = bands_number_2_5ms_HR[decoder->fs_idx];
179 : }
180 : else
181 : {
182 2 : decoder->bands_number = bands_number_2_5ms[decoder->fs_idx];
183 : }
184 : }
185 4395 : if (decoder->frame_ms == 5)
186 : {
187 2014 : decoder->frame_length = decoder->frame_length >> 1;
188 2014 : decoder->yLen /= 2;
189 2014 : decoder->bands_number = bands_number_5ms[decoder->fs_idx];
190 : }
191 4395 : if (decoder->frame_ms == 7.5)
192 : {
193 0 : decoder->frame_length = (decoder->frame_length >> 2) * 3;
194 0 : decoder->yLen = (decoder->yLen / 4) * 3;
195 0 : if (decoder->hrmode)
196 : {
197 0 : decoder->bands_number = bands_number_7_5ms_HR[decoder->fs_idx];
198 : }
199 : else
200 : {
201 0 : decoder->bands_number = bands_number_7_5ms[decoder->fs_idx];
202 : }
203 : }
204 :
205 4395 : if (decoder->hrmode)
206 : {
207 0 : decoder->BW_cutoff_bits = 0;
208 : }
209 : else
210 : {
211 4395 : decoder->BW_cutoff_bits = BW_cutoff_bits_all[decoder->fs_idx];
212 : }
213 :
214 4395 : if (decoder->frame_ms == 10)
215 : {
216 2379 : if (decoder->hrmode)
217 : {
218 0 : decoder->bands_offset = ACC_COEFF_PER_BAND_HR[decoder->fs_idx];
219 : }
220 : else
221 : {
222 2379 : decoder->bands_offset = ACC_COEFF_PER_BAND[decoder->fs_idx];
223 : }
224 2379 : decoder->cutoffBins = BW_cutoff_bin_all;
225 : }
226 2016 : else if (decoder->frame_ms == 2.5)
227 : {
228 2 : if (decoder->hrmode)
229 : {
230 0 : decoder->bands_offset = ACC_COEFF_PER_BAND_2_5ms_HR[decoder->fs_idx];
231 : }
232 : else
233 : {
234 2 : decoder->bands_offset = ACC_COEFF_PER_BAND_2_5ms[decoder->fs_idx];
235 : }
236 2 : decoder->cutoffBins = BW_cutoff_bin_all_2_5ms;
237 : }
238 2014 : else if (decoder->frame_ms == 5)
239 : {
240 2014 : if (decoder->hrmode)
241 : {
242 0 : decoder->bands_offset = ACC_COEFF_PER_BAND_5ms_HR[decoder->fs_idx];
243 : }
244 : else
245 : {
246 2014 : decoder->bands_offset = ACC_COEFF_PER_BAND_5ms[decoder->fs_idx];
247 : }
248 2014 : decoder->cutoffBins = BW_cutoff_bin_all_5ms;
249 : }
250 0 : else if (decoder->frame_ms == 7.5)
251 : {
252 0 : if (decoder->hrmode)
253 : {
254 0 : decoder->bands_offset = ACC_COEFF_PER_BAND_7_5ms_HR[decoder->fs_idx];
255 : }
256 : else
257 : {
258 0 : decoder->bands_offset = ACC_COEFF_PER_BAND_7_5ms[decoder->fs_idx];
259 : }
260 0 : decoder->cutoffBins = BW_cutoff_bin_all_7_5ms;
261 : }
262 :
263 4395 : decoder->n_bandsPLC = MIN(decoder->frame_length, 80);
264 :
265 4395 : if (decoder->frame_ms == 10)
266 : {
267 2379 : decoder->bands_offsetPLC = ACC_COEFF_PER_BAND_PLC[decoder->fs_idx];
268 : }
269 2016 : else if (decoder->frame_ms == 5)
270 : {
271 2014 : decoder->bands_offsetPLC = ACC_COEFF_PER_BAND_PLC_5ms[decoder->fs_idx];
272 :
273 2014 : if (decoder->fs == 24000)
274 : {
275 0 : decoder->n_bandsPLC = 40;
276 : }
277 : }
278 2 : else if (decoder->frame_ms == 2.5)
279 : {
280 2 : decoder->bands_offsetPLC = ACC_COEFF_PER_BAND_PLC_2_5ms[decoder->fs_idx];
281 :
282 2 : if (decoder->fs == 48000)
283 : {
284 2 : decoder->n_bandsPLC = 60;
285 : }
286 : }
287 0 : else if (decoder->frame_ms == 7.5)
288 : {
289 0 : decoder->bands_offsetPLC = ACC_COEFF_PER_BAND_PLC_7_5ms[decoder->fs_idx];
290 :
291 0 : if (decoder->fs != 32000 && decoder->fs != 96000)
292 : {
293 0 : decoder->n_bandsPLC = 60;
294 : }
295 : }
296 :
297 4395 : assert(decoder->bands_offsetPLC);
298 :
299 4395 : if (decoder->frame_ms == 10) {
300 2379 : decoder->imdct_win = MDCT_WINS_10ms[decoder->hrmode][decoder->fs_idx];
301 2379 : decoder->imdct_laZeros = MDCT_la_zeroes[decoder->fs_idx];
302 2379 : decoder->imdct_winLen = MDCT_WINDOWS_LENGTHS_10ms[decoder->fs_idx];
303 : }
304 2016 : else if (decoder->frame_ms == 2.5) {
305 2 : decoder->imdct_win = MDCT_WINS_2_5ms[decoder->hrmode][decoder->fs_idx];
306 2 : decoder->imdct_laZeros = MDCT_la_zeroes_2_5ms[decoder->fs_idx];
307 2 : decoder->imdct_winLen = MDCT_WINDOWS_LENGTHS_2_5ms[decoder->fs_idx];
308 : }
309 2014 : else if (decoder->frame_ms == 5) {
310 2014 : decoder->imdct_win = MDCT_WINS_5ms[decoder->hrmode][decoder->fs_idx];
311 2014 : decoder->imdct_laZeros = MDCT_la_zeroes_5ms[decoder->fs_idx];
312 2014 : decoder->imdct_winLen = MDCT_WINDOWS_LENGTHS_5ms[decoder->fs_idx];
313 : }
314 0 : else if (decoder->frame_ms == 7.5) {
315 0 : decoder->imdct_win = MDCT_WINS_7_5ms[decoder->hrmode][decoder->fs_idx];
316 0 : decoder->imdct_laZeros = MDCT_la_zeroes_7_5ms[decoder->fs_idx];
317 0 : decoder->imdct_winLen = MDCT_WINDOWS_LENGTHS_7_5ms[decoder->fs_idx];
318 : }
319 :
320 4395 : decoder->la_zeroes = decoder->imdct_laZeros;
321 :
322 4395 : decoder->imdct_memLen = decoder->frame_length - decoder->imdct_laZeros;
323 :
324 8790 : for (ch = 0; ch < decoder->channels; ch++) {
325 4395 : DecSetup* setup = decoder->channel_setup[ch];
326 :
327 4395 : setup->ltpf_mem_beta_idx = -1;
328 :
329 4395 : setup->statePC.seed = 24607;
330 :
331 4395 : if (decoder) {
332 : /* Init DCT4 structs */
333 4395 : if (setup->dct4structImdct.length != 0) {
334 2197 : dct4_free(&setup->dct4structImdct);
335 2197 : dct4_init(&setup->dct4structImdct, decoder->frame_length);
336 : } else {
337 2198 : dct4_init(&setup->dct4structImdct, decoder->frame_length);
338 : }
339 :
340 4395 : setup->PlcNsSetup.cum_alpha = 1;
341 4395 : setup->PlcNsSetup.seed = 24607;
342 4395 : setup->alpha = 1;
343 4395 : if (setup->PlcAdvSetup)
344 : {
345 4395 : LC3_INT32 pitch_max = 0, pitch_ana_len = 0, tdc_synt_len = 0;
346 4395 : pitch_max = ceil(228.0 * (LC3_FLOAT) decoder->fs / 12800.0);
347 4395 : pitch_ana_len = pitch_max + decoder->frame_length * (LC3_FLOAT) 100 / decoder->frame_dms;
348 4395 : tdc_synt_len = 16 + 1 + pitch_max + ceil(decoder->frame_length / 2);
349 4395 : setup->PlcAdvSetup->max_len_pcm_plc = MAX(pitch_ana_len, tdc_synt_len);
350 4395 : setup->PlcAdvSetup->PlcTdcSetup.preemphFac = plc_preemph_fac[decoder->fs_idx];
351 4395 : setup->PlcAdvSetup->PlcTdcSetup.seed = 24607;
352 4395 : setup->PlcAdvSetup->PlcTdcSetup.lpcorder = 16;
353 :
354 4395 : if (decoder->fs_idx == 0 && decoder->frame_dms == 25)
355 : {
356 0 : setup->PlcAdvSetup->PlcTdcSetup.lpcorder = 8;
357 : }
358 :
359 4395 : setup->PlcAdvSetup->stabFac = 1;
360 4395 : setup->PlcAdvSetup->cum_fading_fast = 1;
361 4395 : setup->PlcAdvSetup->cum_fading_slow = 1;
362 4395 : setup->PlcAdvSetup->cum_fflcAtten = 1;
363 :
364 4395 : setup->PlcAdvSetup->longterm_analysis_counter_max = plc_fadeout_param_maxlen[(decoder->frame_dms / 25) - 1];
365 4395 : setup->PlcAdvSetup->longterm_analysis_counter_max_bytebuffer = plc_fadeout_param_maxbytes[(decoder->frame_dms / 25) - 1];
366 :
367 4395 : if (decoder->fs_idx <= 4 && decoder->frame_dms == 100)
368 : {
369 2379 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_Lprot = (decoder->fs * 16) / 1000; /* 16 ms of samples at fs*/
370 :
371 2379 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_f0hzLtpBin = 0;
372 2379 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_norm_corr = 0;
373 :
374 2379 : set_vec(PHECU_GRP_SHAPE_INIT, setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_oold_grp_shape, MAX_LGW);
375 2379 : set_vec(PHECU_GRP_SHAPE_INIT, setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_old_grp_shape, MAX_LGW);
376 :
377 2379 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_L_oold_xfp_w_E = (LC3_FLOAT)PHECU_LTOT_MIN_MAN * LC3_POW(2.0, PHECU_LTOT_MIN_EXP - 31);
378 2379 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_L_old_xfp_w_E = (LC3_FLOAT)PHECU_LTOT_MIN_MAN * LC3_POW(2.0, PHECU_LTOT_MIN_EXP - 31);
379 :
380 : /* CFL uses separate buffers for pcmHist, xfp and Xsav and q_d , BASOP uses an optimized joint buffer*/
381 2379 : zero_float(setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_xfp, setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_Lprot);
382 2379 : zero_cmplx(setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_X_sav_m, (setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_Lprot/2 + 1));
383 :
384 2379 : set_vec(POS_ONE_Q15, setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_mag_chg_1st, MAX_LGW);
385 :
386 2379 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_beta_mute = (16384.0/32768.0);
387 2379 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_seed = 21845;
388 :
389 2379 : assert(decoder->frame_dms == 100);
390 2379 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_LDWIN_OLAP = (decoder->frame_length / 4 ); /* 2.5 ms for regular 10 ms MDCT */
391 :
392 2379 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_t_adv = (
393 2379 : decoder->frame_length
394 2379 : + setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_Lprot
395 2379 : + setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_LDWIN_OLAP )/ 2;
396 : }
397 4395 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_short_flag_prev = 0; /* fullband transient */
398 4395 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_num_plocs = 0;
399 4395 : setup->PlcAdvSetup->PlcPhEcuSetup.PhECU_nonpure_tone_flag = -1; /* nonpure tone flag, -1==new calc., 0==pure, 1==nonpure */
400 : }
401 : }
402 : }
403 132695 : for (n=0; n < LC3_ROUND(PLC_FADEOUT_TYPE_1_IN_MS*10/decoder->frame_dms);n++){
404 128300 : decoder->alpha_type_2_table[n] = type_2_fadeout(n, decoder->frame_dms);
405 : }
406 4395 : }
407 :
408 62841 : LC3PLUS_Error update_dec_bitrate(LC3PLUS_Dec* decoder, int ch, int nBytes)
409 : {
410 62841 : int totalBits = 0, bitsTmp = 0, channel_bytes = 0, maxBytes = 0, minBytes = 0;
411 : DecSetup* setup;
412 :
413 62841 : if (decoder->hrmode)
414 : {
415 0 : switch (decoder->frame_dms)
416 : {
417 0 : case 25:
418 0 : maxBytes = 210;
419 0 : minBytes = MIN_NBYTES;
420 0 : break;
421 0 : case 50:
422 0 : maxBytes = 375;
423 0 : minBytes = MIN_NBYTES;
424 0 : break;
425 0 : case 75:
426 0 : maxBytes = 625;
427 0 : minBytes = MIN_NBYTES;
428 0 : break;
429 0 : case 100:
430 0 : maxBytes = 625;
431 0 : minBytes = MIN_NBYTES;
432 0 : break;
433 0 : default:
434 0 : return LC3PLUS_HRMODE_ERROR;
435 : }
436 : }
437 : else
438 : {
439 62841 : minBytes = MIN_NBYTES;
440 62841 : maxBytes = MAX_NBYTES_100; /* for backward compatibility, MAX_NBYTES_100 is used for all frame lengths */
441 : }
442 :
443 62841 : channel_bytes = nBytes;
444 :
445 62841 : setup = decoder->channel_setup[ch];
446 :
447 62841 : if (channel_bytes < minBytes || channel_bytes > maxBytes)
448 : {
449 0 : return LC3PLUS_NUMBYTES_ERROR;
450 : }
451 :
452 62841 : setup->targetBytes = channel_bytes;
453 62841 : setup->total_bits = setup->targetBytes << 3;
454 62841 : setup->enable_lpc_weighting = (setup->total_bits < 480);
455 62841 : setup->quantizedGainOff =
456 62841 : -(MIN(115, setup->total_bits / (10 * (decoder->fs_idx + 1))) + 105 + 5 * (decoder->fs_idx + 1));
457 :
458 62841 : if (decoder->hrmode && decoder->fs_idx == 5)
459 : {
460 0 : setup->quantizedGainOff = MAX(setup->quantizedGainOff, -181);
461 : }
462 :
463 62841 : totalBits = setup->total_bits;
464 :
465 62841 : if (decoder->frame_ms == 2.5) {
466 2 : setup->enable_lpc_weighting = setup->total_bits < 120;
467 2 : totalBits = setup->total_bits * 4.0 * (1.0 - 0.4);
468 : }
469 62841 : if (decoder->frame_ms == 5) {
470 57180 : setup->enable_lpc_weighting = (setup->total_bits < 240);
471 57180 : totalBits = setup->total_bits * 2 - 160;
472 : }
473 62841 : if (decoder->frame_ms == 7.5) {
474 0 : setup->enable_lpc_weighting = (setup->total_bits < 360);
475 0 : totalBits = round(setup->total_bits * 10 / 7.5);
476 : }
477 :
478 62841 : if (decoder->frame_length > 40 * ((LC3_FLOAT) (decoder->frame_dms) / 10.0)) {
479 62837 : setup->N_red_tns = 40 * ((LC3_FLOAT) (decoder->frame_dms) / 10.0);
480 62837 : setup->fs_red_tns = 40000;
481 : } else {
482 4 : setup->N_red_tns = decoder->frame_length;
483 4 : setup->fs_red_tns = decoder->fs;
484 : }
485 :
486 62841 : bitsTmp = totalBits;
487 :
488 62841 : if (bitsTmp < 400 + (decoder->fs_idx - 1) * 80) {
489 0 : setup->ltpf_conf_beta = 0.4;
490 0 : setup->ltpf_conf_beta_idx = 0;
491 62841 : } else if (bitsTmp < 480 + (decoder->fs_idx - 1) * 80) {
492 2 : setup->ltpf_conf_beta = 0.35;
493 2 : setup->ltpf_conf_beta_idx = 1;
494 62839 : } else if (bitsTmp < 560 + (decoder->fs_idx - 1) * 80) {
495 0 : setup->ltpf_conf_beta = 0.3;
496 0 : setup->ltpf_conf_beta_idx = 2;
497 62839 : } else if (bitsTmp < 640 + (decoder->fs_idx - 1) * 80) {
498 2 : setup->ltpf_conf_beta = 0.25;
499 2 : setup->ltpf_conf_beta_idx = 3;
500 : } else {
501 62837 : setup->ltpf_conf_beta = 0;
502 62837 : setup->ltpf_conf_beta_idx = -1;
503 : }
504 :
505 : /* No LTPF in hrmode */
506 62841 : if (decoder->hrmode == 1) {
507 0 : setup->ltpf_conf_beta = 0;
508 0 : setup->ltpf_conf_beta_idx = -1;
509 : }
510 :
511 62841 : return LC3PLUS_OK;
512 : }
|