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 : #ifdef FIX_BOTH_1p25_ALLOC_SPECTRUM
15 : #include <assert.h>
16 : #endif
17 :
18 1189311 : static void Enc_LC3PLUS_Channel_fl(LC3PLUS_Enc* encoder, int channel, int32_t* s_in, uint8_t* bytes, int bps
19 : , LC3_INT32 bfi_ext
20 : )
21 : {
22 : EncSetup* h_EncSetup;
23 :
24 1189311 : LC3_INT s_12k8_len = 0, T0_out = 0, ltpfBits = 0, BW_cutoff_idx = 0, tns_numfilters = 0, quantizedGain = 0,
25 1189311 : quantizedGainMin = 0, nbits = 0, nbits2 = 0, lastnz = 0, lsbMode = 0, gainChange = 0, bp_side = 0,
26 1189311 : mask_side = 0, fac_ns_idx = 0, numResBits = 0, tns_order[2] = {0}, i = 0;
27 1189311 : LC3_FLOAT normcorr = 0, gain = 0;
28 :
29 1189311 : LC3_FLOAT d_fl[MAX_LEN] = {0};
30 1189311 : LC3_INT q_d[MAX_LEN] = {0};
31 1189311 : LC3_INT indexes[TNS_NUMFILTERS_MAX * MAXLAG] = {0};
32 :
33 : #ifdef CR9_C_ADD_1p25MS_LRSNS
34 1189311 : LC3_INT16 envelope_bits = -1;
35 1189311 : LC3_INT32 pitch_rx = -1; /* pitch_rx status flag */
36 1189311 : LC3_INT32 ltpf_rx = -1;
37 : #endif
38 :
39 1189311 : h_EncSetup = encoder->channel_setup[channel];
40 1189311 : memset(bytes, 0, sizeof(uint8_t) * h_EncSetup->targetBytes);
41 :
42 : #ifdef FIX_BOTH_1p25_ALLOC_SPECTRUM
43 1189311 : if (encoder->frame_dms == LC3PLUS_FRAME_DURATION_1p25MS)
44 : {
45 0 : assert(MAX_LEN >= (GG_1p25_MAX_TUPLES - 1) + (encoder->yLen));
46 : /*make sure that there extra tail coeffs in d_fl for Global gain estimation routine */
47 : }
48 : #endif
49 :
50 1189311 : if (bps == 24) {
51 0 : for (i = 0; i < encoder->frame_length; i++) {
52 0 : int32_t tmp = ((int32_t*)s_in)[i];
53 :
54 0 : if (tmp >= 0)
55 : {
56 0 : tmp = tmp & 0x007fffff;
57 : }
58 : else
59 : {
60 0 : tmp = tmp | (int32_t)0xff800000;
61 : }
62 :
63 0 : h_EncSetup->s_in_scaled[i] = ((LC3_FLOAT) tmp / (float) LC3_POW(2, 8));
64 : }
65 1189311 : } else if (bps == 16) {
66 298613391 : for (i = 0; i < encoder->frame_length; i++) {
67 297424080 : h_EncSetup->s_in_scaled[i] = (LC3_FLOAT)((int16_t*)s_in)[i];
68 : }
69 : }
70 :
71 : /* MDCT */
72 1189311 : processMdct_fl(h_EncSetup->s_in_scaled, d_fl, &h_EncSetup->mdctStruct);
73 :
74 : /* 12.8 kHz resampler */
75 1189311 : process_resamp12k8_fl(h_EncSetup->s_in_scaled, encoder->frame_length, h_EncSetup->r12k8_mem_in,
76 1189311 : encoder->r12k8_mem_in_len, h_EncSetup->r12k8_mem_50, h_EncSetup->r12k8_mem_out,
77 1189311 : encoder->r12k8_mem_out_len, h_EncSetup->s_12k8, &s_12k8_len, encoder->fs_idx,
78 : encoder->frame_dms, encoder->fs);
79 :
80 : /* Pitch estimation */
81 1189311 : processOlpa_fl(h_EncSetup->s_12k8, h_EncSetup->olpa_mem_s12k8, h_EncSetup->olpa_mem_s6k4,
82 : &h_EncSetup->olpa_mem_pitch,
83 : &h_EncSetup->pitch_flag,
84 : &T0_out, &normcorr, s_12k8_len, encoder->frame_dms);
85 :
86 : /* LTPF encoder */
87 1189311 : process_ltpf_coder_fl(h_EncSetup->s_12k8, s_12k8_len + 1, h_EncSetup->ltpf_enable, T0_out, normcorr,
88 1189311 : encoder->frame_dms, h_EncSetup->ltpf_mem_in, encoder->ltpf_mem_in_len,
89 : #ifdef CR9_C_ADD_1p25MS
90 1189311 : h_EncSetup->ltpf_mem_normcorr,
91 : #else
92 : &h_EncSetup->ltpf_mem_normcorr,
93 : #endif
94 : &h_EncSetup->ltpf_mem_ltpf_on,
95 1189311 : &h_EncSetup->ltpf_mem_pitch, h_EncSetup->ltpf_param, &h_EncSetup->ltpf_mem_mem_normcorr,
96 : <pfBits
97 1189311 : , encoder->hrmode
98 : #ifdef CR9_C_ADD_1p25MS
99 : #ifdef FIX_TX_RX_STRUCT_STEREO
100 : #ifdef NEW_SIGNALLING_SCHEME_1p25
101 : ,&h_EncSetup->Tx_ltpf
102 : #else
103 : ,h_EncSetup->Tx_ltpf
104 : #endif
105 : #else
106 : , encoder->Tx_ltpf
107 : #endif
108 : #endif
109 : );
110 :
111 : /* Attack detector */
112 1189311 : attack_detector_fl(h_EncSetup->s_in_scaled, encoder->frame_length, encoder->fs, &h_EncSetup->attdec_position,
113 1189311 : &h_EncSetup->attdec_acc_energy, &h_EncSetup->attdec_detected, h_EncSetup->attdec_filter_mem,
114 : h_EncSetup->attack_handling, encoder->attdec_nblocks, encoder->attdec_hangover_thresh);
115 :
116 : /* Per-band energy */
117 1189311 : processPerBandEnergy_fl(encoder->bands_number, encoder->bands_offset, encoder->hrmode, encoder->frame_dms, h_EncSetup->ener, d_fl);
118 : /* Near Nyquist detector */
119 1189311 : processNearNyquistdetector_fl(&encoder->near_nyquist_flag, encoder->fs_idx, encoder->near_nyquist_index, encoder->bands_number, h_EncSetup->ener
120 1189311 : , encoder->frame_dms, encoder->hrmode );
121 : /* Disable LTPF if nyquist detector triggers or -lfe mode is active*/
122 1189311 : if (encoder->near_nyquist_flag != 0 || h_EncSetup->lfe == 1)
123 : {
124 0 : h_EncSetup->ltpf_mem_ltpf_on = 0;
125 0 : h_EncSetup->ltpf_param[1] = 0;
126 : }
127 :
128 : /* Bandwidth cut-off detection */
129 1189311 : if (h_EncSetup->lfe == 0) {
130 : /* No BW Cutoff for 8 kHz and 96 kHz. No detection if bandwidth controller is active */
131 1189311 : if (encoder->fs_idx > 0 && encoder->hrmode == 0 && encoder->bw_ctrl_active == 0) {
132 1189311 : processDetectCutoffWarped_fl(h_EncSetup->ener, encoder->fs_idx, encoder->frame_dms, &BW_cutoff_idx);
133 : } else {
134 0 : BW_cutoff_idx = encoder->fs_idx;
135 : }
136 : } else {
137 0 : BW_cutoff_idx = 0;
138 : }
139 :
140 1189311 : processSnsComputeScf_fl(h_EncSetup->ener, encoder->bands_number, h_EncSetup->scf,
141 : h_EncSetup->attdec_detected, encoder->sns_damping, encoder->attdec_damping, encoder->fs_idx
142 : #ifdef CR9_C_ADD_1p25MS
143 : , encoder->frame_dms, &encoder->long_term_norm_corr, normcorr
144 : #endif
145 : );
146 :
147 : /* SNS Quantizer */
148 : #ifdef CR9_C_ADD_1p25MS_LRSNS
149 1189311 : if (encoder->frame_dms == LC3PLUS_FRAME_DURATION_1p25MS)
150 : {
151 : /* 9,10, 29,30 bit LRSNS quantizer */
152 0 : pitch_rx = h_EncSetup->ltpf_param[0]; /* pitch_rx status flag */
153 0 : ltpf_rx = h_EncSetup->ltpf_param[1]; /* ltpf active flag, LTPF lag data information is split over two 1.25 ms frames */
154 0 : assert(ltpfBits == 2 || ltpfBits == 6 || ltpfBits == 7);
155 0 : assert((pitch_rx != 0 && ltpf_rx == 0) || (pitch_rx != 0 && ltpf_rx != 0) || (pitch_rx == 0 && ltpf_rx == 0));
156 :
157 0 : envelope_bits = snsQuantScfEncLR(h_EncSetup->scf, h_EncSetup->L_scf_idx, h_EncSetup->scf_q, h_EncSetup->dct2StructSNS, pitch_rx, ltpf_rx);
158 : }
159 : else
160 : { /* legacy 38 bit SNS VQ */
161 1189311 : assert(ltpfBits == 1 || ltpfBits == 11);
162 1189311 : process_snsQuantizesScf_Enc(h_EncSetup->scf, h_EncSetup->L_scf_idx, h_EncSetup->scf_q, h_EncSetup->dct2StructSNS);
163 1189311 : envelope_bits = 38;
164 : }
165 : #else
166 : process_snsQuantizesScf_Enc(h_EncSetup->scf, h_EncSetup->L_scf_idx, h_EncSetup->scf_q, h_EncSetup->dct2StructSNS);
167 : #endif
168 :
169 : /* SNS Interpolation */
170 1189311 : processSnsInterpolateScf_fl(h_EncSetup->scf_q, 1, encoder->bands_number, h_EncSetup->int_scf);
171 :
172 : /* MDCT shaping */
173 1189311 : processMdctShaping_fl(d_fl, h_EncSetup->int_scf, encoder->bands_offset, encoder->bands_number);
174 :
175 : /* Bandwidth controller */
176 1189311 : if (encoder->bandwidth < encoder->fs / 2) {
177 0 : process_cutoff_bandwidth(d_fl, encoder->yLen, encoder->bw_ctrl_cutoff_bin);
178 0 : BW_cutoff_idx = MIN(BW_cutoff_idx, encoder->bw_index);
179 : }
180 :
181 : /* TNS encoder */
182 : #ifdef CR9_C_ADD_1p25MS
183 1189311 : if (h_EncSetup->lfe == 0 && encoder->frame_dms > LC3PLUS_FRAME_DURATION_1p25MS)
184 : #else
185 : if (h_EncSetup->lfe == 0)
186 : #endif
187 : {
188 1189311 : processTnsCoder_fl(d_fl, BW_cutoff_idx, encoder->cutoffBins[BW_cutoff_idx], encoder->fs, encoder->frame_length,
189 : encoder->frame_dms, h_EncSetup->total_bits, tns_order, indexes, &tns_numfilters,
190 : &(h_EncSetup->tns_bits)
191 1189311 : , encoder->near_nyquist_flag
192 : );
193 : }
194 : else
195 : {
196 0 : tns_numfilters = 1;
197 : #ifdef CR9_C_ADD_1p25MS
198 0 : if (encoder->frame_dms == LC3PLUS_FRAME_DURATION_1p25MS) {
199 0 : tns_numfilters = 0;
200 : }
201 : #endif
202 0 : tns_order[0] = 0;
203 0 : h_EncSetup->tns_bits = tns_numfilters;
204 : }
205 : /* Global Gain Estimation */
206 1189311 : h_EncSetup->targetBitsQuant = h_EncSetup->targetBitsInit - (h_EncSetup->tns_bits + ltpfBits);
207 :
208 : #ifdef CR9_C_ADD_1p25MS_LRSNS
209 1189311 : if ( encoder->frame_dms == LC3PLUS_FRAME_DURATION_1p25MS)
210 : {
211 0 : h_EncSetup->targetBitsQuant += 38; /* 38 was already subtracted in h_EncSetup->targetBitsInit setup */
212 0 : assert(envelope_bits >= 9 && envelope_bits <= 30);
213 0 : h_EncSetup->targetBitsQuant -= envelope_bits; /* 9,10, 29,30 */
214 : }
215 : #endif
216 :
217 1189311 : processEstimateGlobalGain_fl(d_fl, encoder->yLen, h_EncSetup->targetBitsQuant, &gain, &quantizedGain,
218 : &quantizedGainMin, h_EncSetup->quantizedGainOff, &h_EncSetup->targetBitsOff,
219 : &h_EncSetup->mem_targetBits, h_EncSetup->mem_specBits
220 : , encoder->hrmode, h_EncSetup->regBits, encoder->frame_ms
221 :
222 : );
223 :
224 : /* 1. Quantization */
225 1189311 : processQuantizeSpec_fl(d_fl, gain, q_d, encoder->yLen, h_EncSetup->total_bits, &nbits, &nbits2, encoder->fs,
226 1189311 : &lastnz, h_EncSetup->codingdata, &lsbMode, -1, h_EncSetup->targetBitsQuant, encoder->hrmode
227 : );
228 :
229 1189311 : h_EncSetup->mem_specBits = nbits;
230 :
231 : /* Global Gain Adjustment */
232 1189311 : processAdjustGlobalGain_fl(&quantizedGain, quantizedGainMin, h_EncSetup->quantizedGainOff, &gain,
233 : h_EncSetup->targetBitsQuant, h_EncSetup->mem_specBits, &gainChange, encoder->fs_idx
234 1189311 : , encoder->hrmode, encoder->frame_dms
235 : );
236 :
237 : /* 2. Quantization */
238 1189311 : if (gainChange) {
239 540702 : processQuantizeSpec_fl(d_fl, gain, q_d, encoder->yLen, h_EncSetup->total_bits, &nbits, &nbits2, encoder->fs,
240 : &lastnz,
241 540702 : h_EncSetup->codingdata,
242 : &lsbMode, 0, h_EncSetup->targetBitsQuant
243 : , encoder->hrmode
244 : );
245 : }
246 :
247 : /* Noise factor */
248 1189311 : if (h_EncSetup->lfe == 0)
249 : {
250 1189311 : processNoiseFactor_fl(&fac_ns_idx, d_fl, q_d, gain, encoder->cutoffBins[BW_cutoff_idx], encoder->frame_dms,
251 : h_EncSetup->targetBytes);
252 : }
253 : else
254 : {
255 0 : fac_ns_idx = 7;
256 : }
257 : /* Residual Coding */
258 1189311 : if (lsbMode == 0) {
259 1171359 : processResidualCoding_fl(d_fl, q_d, gain, encoder->yLen, h_EncSetup->targetBitsQuant, nbits2, h_EncSetup->resBits, &numResBits, encoder->hrmode
260 : #ifdef ENABLE_12p5_DMS_MODE
261 : , encoder->frame_dms
262 : #endif
263 : );
264 : } else {
265 17952 : numResBits = 0;
266 : }
267 :
268 : /* Entropy encoding */
269 1189311 : processEncoderEntropy_fl(bytes, &bp_side, &mask_side, h_EncSetup->targetBytes, encoder->BW_cutoff_bits,
270 : BW_cutoff_idx, lastnz, encoder->yLen, lsbMode, quantizedGain, tns_numfilters, tns_order,
271 1189311 : h_EncSetup->ltpf_param, h_EncSetup->L_scf_idx, fac_ns_idx,
272 : bfi_ext, encoder->fs_idx
273 : #ifdef CR9_C_ADD_1p25MS
274 : #ifdef FIX_TX_RX_STRUCT_STEREO
275 : , encoder->frame_dms, &h_EncSetup->Tx_ltpf
276 : #else
277 : , encoder->frame_dms, &encoder->Tx_ltpf
278 : #endif
279 : #endif
280 : );
281 :
282 : /* Artithmetic encoding */
283 1189311 : processAriEncoder_fl(bytes, bp_side, mask_side, q_d, tns_order, tns_numfilters, indexes, lastnz,
284 1189311 : h_EncSetup->codingdata,
285 1189311 : h_EncSetup->resBits, numResBits, lsbMode, h_EncSetup->targetBitsAri,
286 : h_EncSetup->enable_lpc_weighting);
287 :
288 1189311 : if (encoder->combined_channel_coding == 0 && h_EncSetup->n_pc > 0)
289 : {
290 0 : LC3_INT32 xbuf[MAX_LEN] = {0}, nf_seed, tns_idx[M], zero_frame, nbits_residual, residualPresent, b_left, spec_inv_idx;
291 :
292 0 : memset(h_EncSetup->resBits, 0, sizeof(*(h_EncSetup->resBits)) * MAX_RESBITS_LEN);
293 :
294 0 : processAriDecoder_fl(bytes, bp_side, mask_side, encoder->yLen, encoder->fs_idx, h_EncSetup->enable_lpc_weighting,
295 : tns_numfilters, lsbMode, lastnz, &bfi_ext, tns_order, fac_ns_idx, quantizedGain,
296 0 : h_EncSetup->resBits, xbuf, &nf_seed, tns_idx, &zero_frame, h_EncSetup->targetBytes, &nbits_residual,
297 0 : &residualPresent, encoder->frame_dms, h_EncSetup->n_pc, 0, h_EncSetup->total_bits >> 3, 1, &b_left,
298 : &spec_inv_idx, encoder->hrmode);
299 :
300 0 : processReorderBitstream_fl(bytes, h_EncSetup->n_pccw, h_EncSetup->n_pc, b_left, h_EncSetup->targetBytes);
301 : }
302 :
303 1189311 : }
304 :
305 1189311 : int Enc_LC3PLUS_fl(LC3PLUS_Enc* encoder, void** input, uint8_t* output, int bps
306 : , LC3_INT32 bfi_ext
307 : )
308 : {
309 1189311 : int ch = 0, output_size = 0;
310 1189311 : uint8_t* lc3buf = output;
311 :
312 : LC3_INT32 totalBytes;
313 : LC3_INT32 output_size2, input_size;
314 :
315 1189311 : totalBytes = encoder->bitrate * encoder->frame_length / (8 * encoder->fs_in);
316 :
317 2378622 : for (ch = 0; ch < encoder->channels; ch++)
318 : {
319 1189311 : Enc_LC3PLUS_Channel_fl(encoder, ch, input[ch], lc3buf, bps, bfi_ext);
320 1189311 : if (encoder->epmode && encoder->combined_channel_coding == 0)
321 : {
322 0 : output_size2 = totalBytes / encoder->channels + (ch < (totalBytes % encoder->channels));
323 :
324 0 : fec_encoder(encoder->epmode, encoder->epmr, lc3buf, encoder->channel_setup[ch]->targetBytes, output_size2,
325 0 : encoder->channel_setup[ch]->n_pccw);
326 :
327 0 : lc3buf += output_size2;
328 0 : output_size += output_size2;
329 : }
330 : else
331 : {
332 1189311 : lc3buf += encoder->channel_setup[ch]->targetBytes;
333 1189311 : output_size += encoder->channel_setup[ch]->targetBytes;
334 : }
335 : }
336 :
337 1189311 : if (encoder->epmode > 0 && encoder->combined_channel_coding)
338 : {
339 0 : input_size = output_size;
340 0 : output_size = encoder->bitrate * encoder->frame_length / (8 * encoder->fs_in);
341 :
342 0 : fec_encoder(encoder->epmode, encoder->epmr, output, input_size, output_size, encoder->channel_setup[0]->n_pccw);
343 : }
344 :
345 1189311 : return output_size;
346 : }
|