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 "functions.h"
13 :
14 0 : 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 0 : 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 0 : lastnz = 0, lsbMode = 0, nf_seed = 0, zero_frame = 0, residualPresent = 0, nbits_residual = 0, bitsRead = 0,
19 0 : i = 0, tns_order[2] = {0}, sqQdec[MAX_LEN] = {0};
20 : LC3_INT b_left;
21 0 : LC3_FLOAT stab_fac = 0;
22 :
23 0 : h_DecSetup = decoder->channel_setup[channel];
24 :
25 0 : memset(h_DecSetup->tns_idx, 0, sizeof(*h_DecSetup->tns_idx) * TNS_NUMFILTERS_MAX * MAXLAG);
26 :
27 0 : bfi = bfi_ext;
28 :
29 0 : decoder->rframe = 0;
30 0 : if (bfi == 3)
31 : {
32 0 : bfi = 2;
33 0 : decoder->rframe = 1;
34 : }
35 :
36 : /* Entropy decoding */
37 0 : if (bfi != 1) {
38 0 : processDecoderEntropy_fl(bs_in, h_DecSetup->targetBytes, &mask_side, &bp_side, decoder->yLen, decoder->fs_idx,
39 0 : decoder->BW_cutoff_bits, &bfi, &gg_idx, h_DecSetup->scf_idx, &fac_ns_idx,
40 0 : &tns_numfilters, tns_order, h_DecSetup->ltpf_param, &bw_cutoff_idx, &lastnz, &lsbMode, decoder->frame_dms
41 : );
42 0 : h_DecSetup->BW_cutoff_idx_nf = bw_cutoff_idx;
43 : }
44 :
45 : /* Arithmetic decoding */
46 0 : if (bfi != 1) {
47 0 : processAriDecoder_fl(bs_in, bp_side, mask_side, decoder->yLen, decoder->fs_idx,
48 0 : h_DecSetup->enable_lpc_weighting, tns_numfilters, lsbMode, lastnz, &bfi, tns_order, fac_ns_idx, gg_idx, h_DecSetup->resBits,
49 0 : sqQdec, &nf_seed, h_DecSetup->tns_idx, &zero_frame, h_DecSetup->targetBytes, &nbits_residual, &residualPresent, decoder->frame_dms,
50 0 : decoder->n_pc, decoder->be_bp_left, decoder->be_bp_right, 0, &b_left, &h_DecSetup->spec_inv_idx,
51 : decoder->hrmode
52 : );
53 :
54 0 : if (decoder->rframe == 1 && zero_frame == 0 && bfi != 1)
55 : {
56 0 : LC3_INT32 max_bw_stopband = BW_cutoff_bin_all[bw_cutoff_idx];
57 0 : bfi = 2;
58 0 : switch (decoder->frame_dms)
59 : {
60 0 : case 25:
61 0 : max_bw_stopband = max_bw_stopband >> 2;
62 0 : break;
63 0 : case 50:
64 0 : max_bw_stopband = max_bw_stopband >> 1;
65 0 : break;
66 0 : case 75:
67 0 : max_bw_stopband = 3 * (max_bw_stopband >> 2);
68 0 : break;
69 0 : case 100:
70 0 : break;
71 : }
72 :
73 0 : h_DecSetup->spec_inv_idx = MAX(lastnz, max_bw_stopband);
74 : }
75 :
76 : /* Cast from int to float */
77 0 : for (i = 0; i < decoder->yLen; i++) {
78 0 : h_DecSetup->sqQdec_fl[i] = (LC3_FLOAT)sqQdec[i];
79 : }
80 : }
81 :
82 0 : if (bfi != 1)
83 : {
84 : /* SNS Quantize Decoder */
85 0 : process_snsQuantizesScf_Dec(h_DecSetup->scf_idx, h_DecSetup->scf_q);
86 : }
87 0 : if (h_DecSetup->PlcAdvSetup)
88 : {
89 0 : processPlcComputeStabFacMain_fl(h_DecSetup->scf_q, h_DecSetup->PlcAdvSetup->scf_q_old, h_DecSetup->PlcAdvSetup->scf_q_old_old, bfi, h_DecSetup->PlcSetup.prevBfi,
90 0 : h_DecSetup->PlcSetup.prevprevBfi, &h_DecSetup->PlcAdvSetup->stabFac);
91 : }
92 :
93 0 : if ( bfi != 1 )
94 : {
95 0 : stab_fac = 1;
96 0 : if (h_DecSetup->PlcAdvSetup)
97 : {
98 0 : stab_fac = h_DecSetup->PlcAdvSetup->stabFac;
99 : }
100 :
101 : /* Partial Concealment */
102 0 : processPcMain_fl(&bfi, decoder, h_DecSetup->sqQdec_fl, h_DecSetup, h_DecSetup->ltpf_param[0], stab_fac, gg_idx, h_DecSetup->quantizedGainOff,
103 : fac_ns_idx, &h_DecSetup->statePC, h_DecSetup->spec_inv_idx, decoder->yLen);
104 : }
105 :
106 : /* Decoding only if no bit error detected */
107 0 : if (bfi != 1) {
108 : /* Residual decoding */
109 0 : if (residualPresent) {
110 0 : processResidualDecoding_fl(&bitsRead, h_DecSetup->sqQdec_fl, decoder->yLen, h_DecSetup->resBits,
111 : nbits_residual
112 : , decoder->hrmode
113 : );
114 : }
115 :
116 :
117 : /* Noise filling */
118 0 : if (zero_frame == 0) {
119 0 : 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);
120 : }
121 :
122 : /* Application of global gain */
123 0 : processApplyGlobalGain_fl(h_DecSetup->sqQdec_fl, decoder->yLen, gg_idx, h_DecSetup->quantizedGainOff);
124 :
125 : /* TNS decoder */
126 0 : processTnsDecoder_fl(h_DecSetup->sqQdec_fl, h_DecSetup->tns_idx, tns_order, tns_numfilters,
127 0 : decoder->cutoffBins[bw_cutoff_idx], h_DecSetup->N_red_tns, h_DecSetup->fs_red_tns);
128 :
129 : /* SNS interpolation */
130 0 : processSnsInterpolateScf_fl(h_DecSetup->scf_q, 0, decoder->bands_number, h_DecSetup->int_scf);
131 :
132 : /* MDCT shaping */
133 0 : processMdctShaping_fl(h_DecSetup->sqQdec_fl, h_DecSetup->int_scf, decoder->bands_offset, decoder->bands_number);
134 : }
135 :
136 : /* PLC */
137 0 : processPlcMain_fl(h_DecSetup->sqQdec_fl, h_DecSetup->x_fl, decoder, h_DecSetup, bfi, h_DecSetup->PlcAdvSetup, &h_DecSetup->PlcSetup,
138 : decoder->plcMeth, h_DecSetup->ltpf_mem_pitch, h_DecSetup->ltpf_mem_pitch_fr, decoder->tilt, decoder->bands_offset,
139 0 : decoder->bands_number, decoder->bands_offsetPLC, decoder->n_bandsPLC, decoder->hrmode, &h_DecSetup->statePC);
140 :
141 0 : processPlcDampingScramblingMain_fl(&h_DecSetup->PlcNsSetup.seed,
142 : &h_DecSetup->statePC.seed, h_DecSetup->statePC.ns_nbLostCmpt_pc,
143 0 : h_DecSetup->PlcSetup.nbLostCmpt, &h_DecSetup->PlcAdvSetup->stabFac,
144 0 : &h_DecSetup->PlcAdvSetup->cum_fading_slow, &h_DecSetup->PlcAdvSetup->cum_fading_fast,
145 0 : h_DecSetup->PlcSetup.q_d_prev, h_DecSetup->sqQdec_fl, h_DecSetup->spec_inv_idx, decoder->yLen, bfi,
146 : decoder->frame_dms, h_DecSetup->concealMethod, h_DecSetup->ltpf_mem_pitch, h_DecSetup->ltpf_param[0],
147 0 : &h_DecSetup->PlcAdvSetup->cum_fflcAtten
148 0 : , h_DecSetup->PlcAdvSetup->plc_fadeout_type
149 : );
150 :
151 : /* IMDCT */
152 0 : if (h_DecSetup->concealMethod == 4 || bfi != 1 )
153 : {
154 0 : ProcessingIMDCT_fl(h_DecSetup->sqQdec_fl, decoder->frame_length, decoder->imdct_win, decoder->imdct_winLen, decoder->imdct_laZeros,
155 0 : h_DecSetup->imdct_mem, h_DecSetup->x_fl, &h_DecSetup->dct4structImdct);
156 : }
157 :
158 0 : processPlcUpdate_fl(h_DecSetup->PlcAdvSetup
159 0 : , decoder->frame_length, h_DecSetup->x_fl, h_DecSetup->scf_q,
160 0 : &h_DecSetup->PlcSetup.nbLostCmpt, &h_DecSetup->PlcNsSetup.cum_alpha, bfi, &h_DecSetup->PlcSetup.prevBfi, &h_DecSetup->PlcSetup.prevprevBfi);
161 :
162 : /* LTPF decoder */
163 0 : process_ltpf_decoder_fl(h_DecSetup->x_fl, decoder->frame_length, h_DecSetup->x_fl, decoder->fs,
164 0 : h_DecSetup->ltpf_mem_x, h_DecSetup->ltpf_mem_y, &h_DecSetup->ltpf_mem_pitch,
165 : &h_DecSetup->ltpf_mem_pitch_fr, &h_DecSetup->ltpf_mem_gain, &h_DecSetup->ltpf_mem_beta_idx,
166 0 : bfi, h_DecSetup->ltpf_param, h_DecSetup->ltpf_param_mem, h_DecSetup->ltpf_conf_beta_idx,
167 : h_DecSetup->ltpf_conf_beta, h_DecSetup->concealMethod, h_DecSetup->alpha
168 : , &h_DecSetup->ltpf_mem_active
169 : , &h_DecSetup->rel_pitch_change, decoder->hrmode, decoder->frame_dms
170 : );
171 :
172 : {
173 : /* Round, scale and copy output to output buffer */
174 0 : if (bps == 16) {
175 0 : for (i = 0; i < decoder->frame_length; i++) {
176 0 : LC3_FLOAT tmp = round(h_DecSetup->x_fl[i]);
177 0 : ((int16_t*)s_out)[i] = (int16_t)fmaxf(fminf(tmp, 32767), -32768);
178 : }
179 : } else {
180 0 : for (i = 0; i < decoder->frame_length; i++) {
181 0 : LC3_FLOAT tmp = round(LC3_CONST_POW_2_23 * LC3_CONST_POW_2_M15 * h_DecSetup->x_fl[i]);
182 0 : ((int32_t*)s_out)[i] = (int32_t)fmaxf(fminf(tmp, LC3_CONST_POW_2_23_RED), LC3_CONST_POW_2_23_NEG);
183 : }
184 : }
185 : }
186 0 : return bfi;
187 : }
188 :
189 0 : LC3PLUS_Error Dec_LC3PLUS_fl(LC3PLUS_Dec* decoder, uint8_t* input, LC3_INT32 num_bytes, void** output, LC3_INT32 bps, LC3_INT32 bfi_ext)
190 : {
191 : LC3_INT32 ch, bfi, lc3_num_bytes;
192 : LC3PLUS_Error err;
193 : LC3_INT32 fec_num_bytes;
194 : LC3_INT32 lc3_channel_num_bytes;
195 : LC3_INT32 channel_bfi, out_bfi;
196 : LC3PLUS_EpModeRequest channel_epmr;
197 :
198 0 : bfi = bfi_ext;
199 0 : lc3_num_bytes = 0;
200 0 : err = LC3PLUS_OK;
201 :
202 0 : if (bfi == 0)
203 : {
204 0 : bfi = !num_bytes;
205 : }
206 :
207 0 : if (decoder->ep_enabled)
208 : {
209 0 : decoder->combined_channel_coding = decoder->channels > 1 && num_bytes <= 160;
210 :
211 0 : if (decoder->combined_channel_coding)
212 : {
213 0 : fec_num_bytes = num_bytes;
214 :
215 0 : decoder->error_report =
216 0 : fec_decoder(input, fec_num_bytes, &lc3_num_bytes, (LC3PLUS_EpModeRequest*)&decoder->epmr, decoder->combined_channel_coding,
217 : &decoder->n_pccw, &bfi, &decoder->be_bp_left, &decoder->be_bp_right, &decoder->n_pc, &decoder->m_fec);
218 :
219 0 : for (ch = 0; ch < decoder->channels; ch++)
220 : {
221 0 : lc3_channel_num_bytes = lc3_num_bytes / decoder->channels + (ch < (lc3_num_bytes % decoder->channels));
222 :
223 :
224 0 : if (bfi != 1 && lc3_channel_num_bytes != decoder->channel_setup[ch]->last_size)
225 : {
226 0 : err = update_dec_bitrate(decoder, ch, lc3_channel_num_bytes);
227 0 : if (err)
228 : {
229 0 : bfi = 1;
230 0 : decoder->last_error = err;
231 : }
232 : else
233 : {
234 0 : decoder->channel_setup[ch]->last_size = lc3_channel_num_bytes;
235 : }
236 : }
237 :
238 0 : bfi = Dec_LC3PLUS_Channel_fl(decoder, ch, input, output[ch], bps, bfi);
239 0 : if (input != NULL)
240 : {
241 0 : input += decoder->channel_setup[ch]->targetBytes;
242 : }
243 : }
244 : }
245 : else
246 : {
247 0 : decoder->epmr = LC3PLUS_EPMR_HIGH_NC;
248 0 : out_bfi = 0;
249 :
250 0 : for (ch = 0; ch < decoder->channels; ch++)
251 : {
252 0 : fec_num_bytes = num_bytes / decoder->channels + (ch < (num_bytes % decoder->channels));
253 :
254 0 : channel_bfi = bfi;
255 :
256 0 : decoder->error_report = fec_decoder(input, fec_num_bytes, &lc3_num_bytes, &channel_epmr,
257 0 : decoder->combined_channel_coding, &decoder->n_pccw, &channel_bfi,
258 : &decoder->be_bp_left, &decoder->be_bp_right, &decoder->n_pc, &decoder->m_fec);
259 :
260 0 : decoder->epmr = MIN((LC3PLUS_EpModeRequest) decoder->epmr, channel_epmr);
261 :
262 :
263 : #ifdef ENABLE_PADDING
264 0 : if (channel_bfi != 1)
265 : {
266 0 : LC3_INT32 padding_len = 0, np_zero = 0;
267 :
268 0 : if (paddingDec_fl(input, (lc3_num_bytes << 3), decoder->yLen, decoder->BW_cutoff_bits, decoder->ep_enabled, &padding_len, &np_zero))
269 : {
270 0 : channel_bfi = 1;
271 : }
272 :
273 0 : if (input != NULL)
274 : {
275 0 : input = input + np_zero;
276 : }
277 :
278 0 : decoder->n_pc = MAX(decoder->n_pc - (2 * np_zero), 0);
279 :
280 0 : if (channel_bfi == 2)
281 : {
282 0 : if (decoder->be_bp_right < (8 * np_zero))
283 : {
284 0 : channel_bfi = 0;
285 0 : decoder->be_bp_left = -1;
286 0 : decoder->be_bp_right = -1;
287 : }
288 : else
289 : {
290 0 : decoder->be_bp_right = decoder->be_bp_right - (8 * np_zero);
291 0 : decoder->be_bp_left = MAX(decoder->be_bp_left - (8 * np_zero), 0);
292 : }
293 : }
294 0 : lc3_num_bytes = lc3_num_bytes - padding_len;
295 : }
296 : #endif
297 :
298 0 : if (channel_bfi != 1 && lc3_num_bytes != decoder->channel_setup[ch]->last_size)
299 : {
300 0 : err = update_dec_bitrate(decoder, ch, lc3_num_bytes);
301 0 : if (err)
302 : {
303 0 : channel_bfi = 1;
304 0 : decoder->last_error = err;
305 : }
306 : else
307 : {
308 0 : decoder->channel_setup[ch]->last_size = lc3_num_bytes;
309 : }
310 : }
311 :
312 0 : channel_bfi = Dec_LC3PLUS_Channel_fl(decoder, ch, input, output[ch], bps, channel_bfi);
313 :
314 0 : out_bfi |= channel_bfi;
315 0 : if (input != NULL)
316 : {
317 0 : input += fec_num_bytes;
318 : }
319 : }
320 :
321 0 : bfi = out_bfi & 1;
322 : }
323 : }
324 : else
325 : {
326 0 : for (ch = 0; ch < decoder->channels; ch++)
327 : {
328 0 : lc3_num_bytes = num_bytes / decoder->channels + (ch < (num_bytes % decoder->channels));
329 :
330 : #ifdef ENABLE_PADDING
331 0 : if (bfi != 1)
332 : {
333 0 : LC3_INT32 padding_len = 0, np_zero = 0;
334 :
335 0 : if (paddingDec_fl(input, (lc3_num_bytes << 3), decoder->yLen, decoder->BW_cutoff_bits, decoder->ep_enabled, &padding_len, &np_zero))
336 : {
337 0 : bfi = 1;
338 0 : decoder->last_error = LC3PLUS_PADDING_ERROR;
339 : }
340 :
341 0 : lc3_num_bytes = lc3_num_bytes - padding_len;
342 0 : if (lc3_num_bytes < 20 || lc3_num_bytes > LC3PLUS_MAX_BYTES)
343 : {
344 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 */
345 0 : decoder->last_error = FRAMESIZE_ERROR;
346 : }
347 : }
348 : #endif
349 :
350 0 : if (bfi != 1 && lc3_num_bytes != decoder->channel_setup[ch]->last_size)
351 : {
352 0 : err = update_dec_bitrate(decoder, ch, lc3_num_bytes);
353 0 : if (err)
354 : {
355 0 : bfi = 1;
356 0 : decoder->last_error = err;
357 : }
358 : else
359 : {
360 0 : decoder->channel_setup[ch]->last_size = lc3_num_bytes;
361 : }
362 : }
363 :
364 0 : bfi = Dec_LC3PLUS_Channel_fl(decoder, ch, input, output[ch], bps, bfi);
365 0 : if (input != NULL)
366 : {
367 0 : input += decoder->channel_setup[ch]->targetBytes;
368 : }
369 : }
370 : }
371 :
372 0 : if ((decoder->last_error == LC3PLUS_OK) && bfi)
373 : {
374 0 : decoder->last_error = LC3PLUS_DECODE_ERROR;
375 : }
376 0 : return bfi == 1 ? LC3PLUS_DECODE_ERROR : LC3PLUS_OK;
377 : }
|