Line data Source code
1 : /******************************************************************************
2 : * ETSI TS 103 634 V1.6.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 "functions.h"
13 :
14 1186529 : static int Dec_LC3PLUS_Channel_fl(LC3PLUS_Dec* decoder, int channel, uint8_t* bs_in, void* s_out, int bps, int bfi_ext)
15 : {
16 : DecSetup* h_DecSetup;
17 1186529 : LC3_INT mask_side = 0, bp_side = 0, bfi = 0, gg_idx = 0, fac_ns_idx = 0, tns_numfilters = 0, bw_cutoff_idx = 0,
18 1186529 : lastnz = 0, lsbMode = 0, nf_seed = 0, zero_frame = 0, residualPresent = 0, nbits_residual = 0, bitsRead = 0,
19 1186529 : i = 0, tns_order[2] = {0}, sqQdec[MAX_LEN] = {0};
20 : LC3_INT b_left;
21 1186529 : LC3_FLOAT stab_fac = 0;
22 : #ifdef CR9_C_ADD_1p25MS_LRSNS
23 : LC3_INT32 pitch_rx;
24 : LC3_INT32 ltpf_rx;
25 : #endif
26 :
27 1186529 : h_DecSetup = decoder->channel_setup[channel];
28 :
29 1186529 : memset(h_DecSetup->tns_idx, 0, sizeof(*h_DecSetup->tns_idx) * TNS_NUMFILTERS_MAX * MAXLAG);
30 :
31 1186529 : bfi = bfi_ext;
32 :
33 1186529 : decoder->rframe = 0;
34 1186529 : if (bfi == 3)
35 : {
36 0 : bfi = 2;
37 0 : decoder->rframe = 1;
38 : }
39 :
40 : #ifdef NEW_SIGNALLING_SCHEME_1p25
41 1186529 : h_DecSetup->ltpfinfo_frame_cntr = MIN(32767, h_DecSetup->ltpfinfo_frame_cntr + 1); /*increased always, also for bfi==1 */ /* set or reset inside processDecoderEntropy_fl */
42 : #endif
43 :
44 : /* Entropy decoding */
45 1186529 : if (bfi != 1) {
46 1178242 : processDecoderEntropy_fl(bs_in, h_DecSetup->targetBytes, &mask_side, &bp_side, decoder->yLen, decoder->fs_idx,
47 1178242 : decoder->BW_cutoff_bits, &bfi, &gg_idx, h_DecSetup->scf_idx, &fac_ns_idx,
48 1178242 : &tns_numfilters, tns_order, h_DecSetup->ltpf_param, &bw_cutoff_idx, &lastnz, &lsbMode, decoder->frame_dms
49 :
50 :
51 : #ifdef CR9_C_ADD_1p25MS
52 : #ifdef FIX_TX_RX_STRUCT_STEREO
53 1178242 : , h_DecSetup->ltpf_rx_status, &h_DecSetup->ltpf_mem_continuation
54 : #else
55 : , decoder->ltpf_rx_status, &h_DecSetup->ltpf_mem_continuation
56 : #endif
57 : #ifdef NEW_SIGNALLING_SCHEME_1p25
58 : , &h_DecSetup->ltpfinfo_frame_cntr /* set here, but also increased outside during/when bfi for the channel */
59 : #endif
60 : #endif
61 : );
62 1178242 : h_DecSetup->BW_cutoff_idx_nf = bw_cutoff_idx;
63 : }
64 :
65 : /* Arithmetic decoding */
66 1186529 : if (bfi != 1) {
67 1178242 : processAriDecoder_fl(bs_in, bp_side, mask_side, decoder->yLen, decoder->fs_idx,
68 1178242 : h_DecSetup->enable_lpc_weighting, tns_numfilters, lsbMode, lastnz, &bfi, tns_order, fac_ns_idx, gg_idx, h_DecSetup->resBits,
69 1178242 : sqQdec, &nf_seed, h_DecSetup->tns_idx, &zero_frame, h_DecSetup->targetBytes, &nbits_residual, &residualPresent, decoder->frame_dms,
70 1178242 : decoder->n_pc, decoder->be_bp_left, decoder->be_bp_right, 0, &b_left, &h_DecSetup->spec_inv_idx,
71 : decoder->hrmode
72 : );
73 :
74 1178242 : if (decoder->rframe == 1 && zero_frame == 0 && bfi != 1)
75 : {
76 0 : LC3_INT32 max_bw_stopband = BW_cutoff_bin_all[bw_cutoff_idx];
77 0 : bfi = 2;
78 0 : switch (decoder->frame_dms)
79 : {
80 : #ifdef CR9_C_ADD_1p25MS
81 0 : case LC3PLUS_FRAME_DURATION_1p25MS:
82 0 : max_bw_stopband = max_bw_stopband >> 3;
83 0 : break;
84 : #endif
85 0 : case LC3PLUS_FRAME_DURATION_2p5MS:
86 0 : max_bw_stopband = max_bw_stopband >> 2;
87 0 : break;
88 0 : case LC3PLUS_FRAME_DURATION_5MS:
89 0 : max_bw_stopband = max_bw_stopband >> 1;
90 0 : break;
91 0 : case LC3PLUS_FRAME_DURATION_7p5MS:
92 0 : max_bw_stopband = 3 * (max_bw_stopband >> 2);
93 0 : break;
94 0 : case LC3PLUS_FRAME_DURATION_10MS:
95 0 : break;
96 0 : case LC3PLUS_FRAME_DURATION_UNDEFINED:
97 0 : assert(0);
98 : }
99 :
100 0 : h_DecSetup->spec_inv_idx = MAX(lastnz, max_bw_stopband);
101 : }
102 :
103 : /* Cast from int to float */
104 246806962 : for (i = 0; i < decoder->yLen; i++) {
105 245628720 : h_DecSetup->sqQdec_fl[i] = (LC3_FLOAT)sqQdec[i];
106 : }
107 : }
108 :
109 1186529 : if (bfi != 1)
110 : {
111 : /* SNS Quantize Decoder */
112 : #ifdef CR9_C_ADD_1p25MS_LRSNS
113 1178242 : if (decoder->frame_dms == LC3PLUS_FRAME_DURATION_1p25MS) /* 9,10,29,30 bit low rate SNS VQ */
114 : {
115 0 : pitch_rx = ( h_DecSetup->ltpf_param[0] != 0 );
116 0 : ltpf_rx = ( h_DecSetup->ltpf_param[1] != 0 ) ;
117 :
118 0 : snsQuantScfDecLR(h_DecSetup->scf_idx, h_DecSetup->scf_q, pitch_rx, ltpf_rx); /* 9,12,29,30, bits decoding including pitch_rx, ltpf_rx info */
119 : }
120 : else
121 : {
122 1178242 : process_snsQuantizesScf_Dec(h_DecSetup->scf_idx, h_DecSetup->scf_q); /* 38 bits decoded */
123 : }
124 : #else
125 : /* SNS Quantize Decoder */
126 : process_snsQuantizesScf_Dec(h_DecSetup->scf_idx, h_DecSetup->scf_q);
127 : #endif
128 : }
129 1186529 : if (h_DecSetup->PlcAdvSetup)
130 : {
131 1186529 : processPlcComputeStabFacMain_fl(h_DecSetup->scf_q, h_DecSetup->PlcAdvSetup->scf_q_old, h_DecSetup->PlcAdvSetup->scf_q_old_old, bfi, h_DecSetup->PlcSetup.prevBfi,
132 1186529 : h_DecSetup->PlcSetup.prevprevBfi, &h_DecSetup->PlcAdvSetup->stabFac);
133 : }
134 :
135 1186529 : if ( bfi != 1 )
136 : {
137 1178242 : stab_fac = 1;
138 1178242 : if (h_DecSetup->PlcAdvSetup)
139 : {
140 1178242 : stab_fac = h_DecSetup->PlcAdvSetup->stabFac;
141 : }
142 :
143 : /* Partial Concealment */
144 1178242 : processPcMain_fl(&bfi, decoder, h_DecSetup->sqQdec_fl, h_DecSetup, h_DecSetup->ltpf_param[0], stab_fac, gg_idx, h_DecSetup->quantizedGainOff,
145 : fac_ns_idx, &h_DecSetup->statePC, h_DecSetup->spec_inv_idx, decoder->yLen);
146 : }
147 :
148 : #ifdef CR9_C_ADD_1p25MS
149 1186529 : if ( bfi == 1 )
150 : {
151 : #ifdef FIX_TX_RX_STRUCT_STEREO
152 8287 : h_DecSetup->ltpf_rx_status[0] = 0;
153 8287 : h_DecSetup->ltpf_rx_status[1] = 0;
154 : #else
155 : decoder->ltpf_rx_status[0] = 0;
156 : decoder->ltpf_rx_status[1] = 0;
157 : #endif
158 : }
159 : #endif
160 :
161 : /* Decoding only if no bit error detected */
162 1186529 : if (bfi != 1) {
163 : /* Residual decoding */
164 1178242 : if (residualPresent) {
165 1160429 : processResidualDecoding_fl(&bitsRead, h_DecSetup->sqQdec_fl, decoder->yLen, h_DecSetup->resBits, nbits_residual, decoder->hrmode
166 : #ifdef ENABLE_12p5_DMS_MODE
167 : , decoder->frame_dms
168 : #endif
169 : );
170 : }
171 :
172 : /* Noise filling */
173 : #ifdef CR9_C_ADD_1p25MS
174 1178242 : if (zero_frame == 0 && decoder->cutoffBins != NULL)
175 : #else
176 : if (zero_frame == 0)
177 : #endif
178 : {
179 1166730 : processNoiseFilling_fl(h_DecSetup->sqQdec_fl, nf_seed, fac_ns_idx, decoder->cutoffBins[h_DecSetup->BW_cutoff_idx_nf], decoder->frame_dms, h_DecSetup->prev_fac_ns, h_DecSetup->spec_inv_idx);
180 : }
181 :
182 : /* Application of global gain */
183 1178242 : processApplyGlobalGain_fl(h_DecSetup->sqQdec_fl, decoder->yLen, gg_idx, h_DecSetup->quantizedGainOff);
184 :
185 : /* TNS decoder */
186 : #ifdef CR9_C_ADD_1p25MS
187 1178242 : if (tns_numfilters > 0) {
188 : #endif
189 1178242 : processTnsDecoder_fl(h_DecSetup->sqQdec_fl, h_DecSetup->tns_idx, tns_order, tns_numfilters,
190 1178242 : decoder->cutoffBins[bw_cutoff_idx], h_DecSetup->N_red_tns, h_DecSetup->fs_red_tns);
191 : #ifdef CR9_C_ADD_1p25MS
192 : }
193 : #endif
194 :
195 : /* SNS interpolation */
196 1178242 : processSnsInterpolateScf_fl(h_DecSetup->scf_q, 0, decoder->bands_number, h_DecSetup->int_scf);
197 :
198 : /* MDCT shaping */
199 1178242 : processMdctShaping_fl(h_DecSetup->sqQdec_fl, h_DecSetup->int_scf, decoder->bands_offset, decoder->bands_number);
200 : }
201 :
202 : /* PLC */
203 1186529 : processPlcMain_fl(h_DecSetup->sqQdec_fl, h_DecSetup->x_fl, decoder, h_DecSetup, bfi, h_DecSetup->PlcAdvSetup, &h_DecSetup->PlcSetup,
204 : decoder->plcMeth, h_DecSetup->ltpf_mem_pitch, h_DecSetup->ltpf_mem_pitch_fr, decoder->tilt, decoder->bands_offset,
205 1186529 : decoder->bands_number, decoder->bands_offsetPLC, decoder->n_bandsPLC, decoder->hrmode, &h_DecSetup->statePC);
206 :
207 1186529 : processPlcDampingScramblingMain_fl(&h_DecSetup->PlcNsSetup.seed,
208 : &h_DecSetup->statePC.seed, h_DecSetup->statePC.ns_nbLostCmpt_pc,
209 1186529 : h_DecSetup->PlcSetup.nbLostCmpt, &h_DecSetup->PlcAdvSetup->stabFac,
210 1186529 : &h_DecSetup->PlcAdvSetup->cum_fading_slow, &h_DecSetup->PlcAdvSetup->cum_fading_fast,
211 1186529 : h_DecSetup->PlcSetup.q_d_prev, h_DecSetup->sqQdec_fl, h_DecSetup->spec_inv_idx, decoder->yLen, bfi,
212 : decoder->frame_dms, h_DecSetup->concealMethod, h_DecSetup->ltpf_mem_pitch, h_DecSetup->ltpf_param[0],
213 1186529 : &h_DecSetup->PlcAdvSetup->cum_fflcAtten
214 1186529 : , h_DecSetup->PlcAdvSetup->plc_fadeout_type
215 : );
216 :
217 : /* IMDCT */
218 1186529 : if (h_DecSetup->concealMethod == 4 || bfi != 1 )
219 : {
220 1179617 : ProcessingIMDCT_fl(h_DecSetup->sqQdec_fl, decoder->frame_length, decoder->imdct_win, decoder->imdct_winLen, decoder->imdct_laZeros,
221 1179617 : h_DecSetup->imdct_mem, h_DecSetup->x_fl, &h_DecSetup->dct4structImdct);
222 : }
223 :
224 1186529 : processPlcUpdate_fl(h_DecSetup->PlcAdvSetup
225 1186529 : , decoder->frame_length, h_DecSetup->x_fl, h_DecSetup->scf_q,
226 1186529 : &h_DecSetup->PlcSetup.nbLostCmpt, &h_DecSetup->PlcNsSetup.cum_alpha, bfi, &h_DecSetup->PlcSetup.prevBfi, &h_DecSetup->PlcSetup.prevprevBfi);
227 :
228 : /* LTPF decoder */
229 1186529 : process_ltpf_decoder_fl(h_DecSetup->x_fl, decoder->frame_length, h_DecSetup->x_fl, decoder->fs,
230 1186529 : h_DecSetup->ltpf_mem_x, h_DecSetup->ltpf_mem_y, &h_DecSetup->ltpf_mem_pitch,
231 : &h_DecSetup->ltpf_mem_pitch_fr, &h_DecSetup->ltpf_mem_gain, &h_DecSetup->ltpf_mem_beta_idx,
232 1186529 : bfi, h_DecSetup->ltpf_param, h_DecSetup->ltpf_param_mem, h_DecSetup->ltpf_conf_beta_idx,
233 : &h_DecSetup->ltpf_conf_beta, h_DecSetup->concealMethod, h_DecSetup->alpha
234 : , &h_DecSetup->ltpf_mem_active
235 : , &h_DecSetup->rel_pitch_change, decoder->hrmode, decoder->frame_dms
236 : #ifdef CR9_C_ADD_1p25MS
237 1186529 : , &h_DecSetup->ltpf_mem_continuation, h_DecSetup->ltpf_param_mem_prev,
238 : &h_DecSetup->ltpf_mem_pitch_prev, &h_DecSetup->ltpf_mem_pitch_fr_prev, &h_DecSetup->ltpf_mem_beta_idx_prev, &h_DecSetup->ltpf_mem_gain_prev,
239 : &h_DecSetup->ltpf_pitch_stability_counter, &h_DecSetup->ltpf_gain_step, h_DecSetup->ltpf_conf_beta_max
240 : #endif
241 : );
242 :
243 : {
244 : /* Round, scale and copy output to output buffer */
245 1186529 : if (bps == 16) {
246 297935249 : for (i = 0; i < decoder->frame_length; i++) {
247 296748720 : LC3_FLOAT tmp = round(h_DecSetup->x_fl[i]);
248 296748720 : ((int16_t*)s_out)[i] = (int16_t)fmaxf(fminf(tmp, 32767), -32768);
249 : }
250 : } else {
251 0 : for (i = 0; i < decoder->frame_length; i++) {
252 0 : LC3_FLOAT tmp = round(LC3_CONST_POW_2_23 * LC3_CONST_POW_2_M15 * h_DecSetup->x_fl[i]);
253 0 : ((int32_t*)s_out)[i] = (int32_t)fmaxf(fminf(tmp, LC3_CONST_POW_2_23_RED), LC3_CONST_POW_2_23_NEG);
254 : }
255 : }
256 : }
257 1186529 : return bfi;
258 : }
259 :
260 1186529 : LC3PLUS_Error Dec_LC3PLUS_fl(LC3PLUS_Dec* decoder, uint8_t* input, LC3_INT32 num_bytes, void** output, LC3_INT32 bps, LC3_INT32 bfi_ext)
261 : {
262 : LC3_INT32 ch, bfi, lc3_num_bytes;
263 : LC3PLUS_Error err;
264 : LC3_INT32 fec_num_bytes;
265 : LC3_INT32 lc3_channel_num_bytes;
266 : LC3_INT32 channel_bfi, out_bfi;
267 : LC3_INT32 chan_error_report;
268 : LC3PLUS_EpModeRequest channel_epmr;
269 :
270 1186529 : bfi = bfi_ext;
271 1186529 : lc3_num_bytes = 0;
272 1186529 : err = LC3PLUS_OK;
273 :
274 1186529 : if (bfi == 0)
275 : {
276 1178242 : bfi = !num_bytes;
277 : }
278 :
279 1186529 : if (decoder->ep_enabled)
280 : {
281 0 : decoder->combined_channel_coding = decoder->channels > 1 && num_bytes <= 160;
282 :
283 0 : if (decoder->combined_channel_coding)
284 : {
285 0 : fec_num_bytes = num_bytes;
286 :
287 0 : decoder->error_report =
288 0 : fec_decoder(input, fec_num_bytes, &lc3_num_bytes, (LC3PLUS_EpModeRequest*)&decoder->epmr, decoder->combined_channel_coding,
289 : &decoder->n_pccw, &bfi, &decoder->be_bp_left, &decoder->be_bp_right, &decoder->n_pc, &decoder->m_fec);
290 :
291 0 : for (ch = 0; ch < decoder->channels; ch++)
292 : {
293 0 : lc3_channel_num_bytes = lc3_num_bytes / decoder->channels + (ch < (lc3_num_bytes % decoder->channels));
294 :
295 :
296 0 : if (bfi != 1 && lc3_channel_num_bytes != decoder->channel_setup[ch]->last_size)
297 : {
298 0 : err = update_dec_bitrate(decoder, ch, lc3_channel_num_bytes);
299 0 : if (err)
300 : {
301 0 : bfi = 1;
302 0 : decoder->last_error = err;
303 : }
304 : else
305 : {
306 0 : decoder->channel_setup[ch]->last_size = lc3_channel_num_bytes;
307 : }
308 : }
309 :
310 0 : bfi = Dec_LC3PLUS_Channel_fl(decoder, ch, input, output[ch], bps, bfi);
311 0 : if (input != NULL)
312 : {
313 0 : input += decoder->channel_setup[ch]->targetBytes;
314 : }
315 : }
316 : }
317 : else
318 : {
319 0 : decoder->epmr = LC3PLUS_EPMR_HIGH_NC;
320 0 : out_bfi = 0;
321 0 : decoder->error_report = 0;
322 :
323 0 : for (ch = 0; ch < decoder->channels; ch++)
324 : {
325 0 : fec_num_bytes = num_bytes / decoder->channels + (ch < (num_bytes % decoder->channels));
326 :
327 0 : channel_bfi = bfi;
328 :
329 0 : chan_error_report = fec_decoder(input, fec_num_bytes, &lc3_num_bytes, &channel_epmr,
330 0 : decoder->combined_channel_coding, &decoder->n_pccw, &channel_bfi,
331 : &decoder->be_bp_left, &decoder->be_bp_right, &decoder->n_pc, &decoder->m_fec);
332 :
333 0 : if (chan_error_report < 0 || decoder->error_report < 0) {
334 0 : decoder->error_report = -1;
335 : } else {
336 0 : decoder->error_report += chan_error_report;
337 : }
338 :
339 0 : decoder->epmr = MIN((LC3PLUS_EpModeRequest) decoder->epmr, channel_epmr);
340 :
341 :
342 : #ifdef ENABLE_PADDING
343 0 : if (channel_bfi != 1)
344 : {
345 0 : LC3_INT32 padding_len = 0, np_zero = 0;
346 :
347 0 : if (paddingDec_fl(input, (lc3_num_bytes << 3), decoder->yLen, decoder->BW_cutoff_bits, decoder->ep_enabled, &padding_len, &np_zero))
348 : {
349 0 : channel_bfi = 1;
350 : }
351 :
352 0 : if (input != NULL)
353 : {
354 0 : input = input + np_zero;
355 : }
356 :
357 0 : decoder->n_pc = MAX(decoder->n_pc - (2 * np_zero), 0);
358 :
359 0 : if (channel_bfi == 2)
360 : {
361 0 : if (decoder->be_bp_right < (8 * np_zero))
362 : {
363 0 : channel_bfi = 0;
364 0 : decoder->be_bp_left = -1;
365 0 : decoder->be_bp_right = -1;
366 : }
367 : else
368 : {
369 0 : decoder->be_bp_right = decoder->be_bp_right - (8 * np_zero);
370 0 : decoder->be_bp_left = MAX(decoder->be_bp_left - (8 * np_zero), 0);
371 : }
372 : }
373 0 : lc3_num_bytes = lc3_num_bytes - padding_len;
374 : }
375 : #endif
376 :
377 0 : if (channel_bfi != 1 && lc3_num_bytes != decoder->channel_setup[ch]->last_size)
378 : {
379 0 : err = update_dec_bitrate(decoder, ch, lc3_num_bytes);
380 0 : if (err)
381 : {
382 0 : channel_bfi = 1;
383 0 : decoder->last_error = err;
384 : }
385 : else
386 : {
387 0 : decoder->channel_setup[ch]->last_size = lc3_num_bytes;
388 : }
389 : }
390 :
391 0 : channel_bfi = Dec_LC3PLUS_Channel_fl(decoder, ch, input, output[ch], bps, channel_bfi);
392 :
393 0 : out_bfi |= channel_bfi;
394 0 : if (input != NULL)
395 : {
396 0 : input += fec_num_bytes;
397 : }
398 : }
399 :
400 0 : bfi = out_bfi & 1;
401 : }
402 : }
403 : else
404 : {
405 2373058 : for (ch = 0; ch < decoder->channels; ch++)
406 : {
407 1186529 : lc3_num_bytes = num_bytes / decoder->channels + (ch < (num_bytes % decoder->channels));
408 :
409 : #ifdef ENABLE_PADDING
410 1186529 : if (bfi != 1)
411 : {
412 1178242 : LC3_INT32 padding_len = 0, np_zero = 0;
413 :
414 1178242 : if (paddingDec_fl(input, (lc3_num_bytes << 3), decoder->yLen, decoder->BW_cutoff_bits, decoder->ep_enabled, &padding_len, &np_zero))
415 : {
416 0 : bfi = 1;
417 0 : decoder->last_error = LC3PLUS_PADDING_ERROR;
418 : }
419 :
420 1178242 : lc3_num_bytes = lc3_num_bytes - padding_len;
421 1178242 : if (lc3_num_bytes < 20 || lc3_num_bytes > LC3PLUS_MAX_BYTES)
422 : {
423 0 : bfi = 1; /* mark frame as broken if frame size is below the minimum of 20 bytes or above the maximum of LC3PLUS_MAX_BYTES */
424 0 : decoder->last_error = FRAMESIZE_ERROR;
425 : }
426 : }
427 : #endif
428 :
429 1186529 : if (bfi != 1 && lc3_num_bytes != decoder->channel_setup[ch]->last_size)
430 : {
431 62823 : err = update_dec_bitrate(decoder, ch, lc3_num_bytes);
432 62823 : if (err)
433 : {
434 0 : bfi = 1;
435 0 : decoder->last_error = err;
436 : }
437 : else
438 : {
439 62823 : decoder->channel_setup[ch]->last_size = lc3_num_bytes;
440 : }
441 : }
442 :
443 1186529 : bfi = Dec_LC3PLUS_Channel_fl(decoder, ch, input, output[ch], bps, bfi);
444 1186529 : if (input != NULL)
445 : {
446 1186529 : input += decoder->channel_setup[ch]->targetBytes;
447 : }
448 : }
449 : }
450 :
451 1186529 : if ((decoder->last_error == LC3PLUS_OK) && bfi)
452 : {
453 91 : decoder->last_error = LC3PLUS_DECODE_ERROR;
454 : }
455 1186529 : return bfi == 1 ? LC3PLUS_DECODE_ERROR : LC3PLUS_OK;
456 : }
|