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 void Enc_LC3PLUS_Channel_fl(LC3PLUS_Enc* encoder, int channel, int32_t* s_in, uint8_t* bytes, int bps
15 : , LC3_INT32 bfi_ext
16 : )
17 : {
18 : EncSetup* h_EncSetup;
19 :
20 0 : LC3_INT s_12k8_len = 0, T0_out = 0, ltpfBits = 0, BW_cutoff_idx = 0, tns_numfilters = 0, quantizedGain = 0,
21 0 : quantizedGainMin = 0, nbits = 0, nbits2 = 0, lastnz = 0, lsbMode = 0, gainChange = 0, bp_side = 0,
22 0 : mask_side = 0, fac_ns_idx = 0, numResBits = 0, tns_order[2] = {0}, i = 0;
23 0 : LC3_FLOAT normcorr = 0, gain = 0;
24 :
25 :
26 0 : LC3_FLOAT d_fl[MAX_LEN] = {0};
27 0 : LC3_INT q_d[MAX_LEN] = {0};
28 0 : LC3_INT indexes[TNS_NUMFILTERS_MAX * MAXLAG] = {0};
29 :
30 0 : h_EncSetup = encoder->channel_setup[channel];
31 0 : memset(bytes, 0, sizeof(uint8_t) * h_EncSetup->targetBytes);
32 :
33 0 : if (bps == 24) {
34 0 : for (i = 0; i < encoder->frame_length; i++) {
35 0 : int32_t tmp = ((int32_t*)s_in)[i];
36 :
37 0 : if (tmp >= 0)
38 : {
39 0 : tmp = tmp & 0x007fffff;
40 : }
41 : else
42 : {
43 0 : tmp = tmp | (int32_t)0xff800000;
44 : }
45 :
46 0 : h_EncSetup->s_in_scaled[i] = ((LC3_FLOAT) tmp / (float) LC3_POW(2, 8));
47 : }
48 0 : } else if (bps == 16) {
49 0 : for (i = 0; i < encoder->frame_length; i++) {
50 0 : h_EncSetup->s_in_scaled[i] = (LC3_FLOAT)((int16_t*)s_in)[i];
51 : }
52 : }
53 :
54 : /* MDCT */
55 0 : processMdct_fl(h_EncSetup->s_in_scaled, d_fl, &h_EncSetup->mdctStruct);
56 :
57 : /* 12.8 kHz resampler */
58 0 : process_resamp12k8_fl(h_EncSetup->s_in_scaled, encoder->frame_length, h_EncSetup->r12k8_mem_in,
59 0 : encoder->r12k8_mem_in_len, h_EncSetup->r12k8_mem_50, h_EncSetup->r12k8_mem_out,
60 0 : encoder->r12k8_mem_out_len, h_EncSetup->s_12k8, &s_12k8_len, encoder->fs_idx,
61 : encoder->frame_dms, encoder->fs);
62 :
63 : /* Pitch estimation */
64 0 : processOlpa_fl(h_EncSetup->s_12k8, h_EncSetup->olpa_mem_s12k8, h_EncSetup->olpa_mem_s6k4,
65 : &h_EncSetup->olpa_mem_pitch,
66 : &h_EncSetup->pitch_flag,
67 : &T0_out, &normcorr, s_12k8_len, encoder->frame_dms);
68 :
69 : /* LTPF encoder */
70 0 : process_ltpf_coder_fl(h_EncSetup->s_12k8, s_12k8_len + 1, h_EncSetup->ltpf_enable, T0_out, normcorr,
71 0 : encoder->frame_dms, h_EncSetup->ltpf_mem_in, encoder->ltpf_mem_in_len,
72 : &h_EncSetup->ltpf_mem_normcorr, &h_EncSetup->ltpf_mem_ltpf_on,
73 0 : &h_EncSetup->ltpf_mem_pitch, h_EncSetup->ltpf_param, &h_EncSetup->ltpf_mem_mem_normcorr,
74 : <pfBits
75 0 : , encoder->hrmode
76 : );
77 :
78 : /* Attack detector */
79 0 : attack_detector_fl(h_EncSetup->s_in_scaled, encoder->frame_length, encoder->fs, &h_EncSetup->attdec_position,
80 0 : &h_EncSetup->attdec_acc_energy, &h_EncSetup->attdec_detected, h_EncSetup->attdec_filter_mem,
81 : h_EncSetup->attack_handling, encoder->attdec_nblocks, encoder->attdec_hangover_thresh);
82 :
83 : /* Per-band energy */
84 0 : processPerBandEnergy_fl(encoder->bands_number, encoder->bands_offset, encoder->hrmode, encoder->frame_dms, h_EncSetup->ener, d_fl);
85 : /* Near Nyquist detector */
86 0 : processNearNyquistdetector_fl(&encoder->near_nyquist_flag, encoder->fs_idx, encoder->near_nyquist_index, encoder->bands_number, h_EncSetup->ener
87 0 : , encoder->frame_dms, encoder->hrmode );
88 : /* Disable LTPF if nyquist detector triggers or -lfe mode is active*/
89 0 : if (encoder->near_nyquist_flag != 0 || h_EncSetup->lfe == 1)
90 : {
91 0 : h_EncSetup->ltpf_mem_ltpf_on = 0;
92 0 : h_EncSetup->ltpf_param[1] = 0;
93 : }
94 :
95 : /* Bandwidth cut-off detection */
96 0 : if (h_EncSetup->lfe == 0) {
97 : /* No BW Cutoff for 8 kHz and 96 kHz. No detection if bandwidth controller is active */
98 0 : if (encoder->fs_idx > 0 && encoder->hrmode == 0 && encoder->bw_ctrl_active == 0) {
99 0 : processDetectCutoffWarped_fl(h_EncSetup->ener, encoder->fs_idx, encoder->frame_dms, &BW_cutoff_idx);
100 : } else {
101 0 : BW_cutoff_idx = encoder->fs_idx;
102 : }
103 : } else {
104 0 : BW_cutoff_idx = 0;
105 : }
106 :
107 0 : processSnsComputeScf_fl(h_EncSetup->ener, encoder->bands_number, h_EncSetup->scf,
108 : h_EncSetup->attdec_detected, encoder->sns_damping, encoder->attdec_damping, encoder->fs_idx);
109 :
110 : /* SNS Quantizer */
111 0 : process_snsQuantizesScf_Enc(h_EncSetup->scf, h_EncSetup->L_scf_idx, h_EncSetup->scf_q, h_EncSetup->dct2StructSNS);
112 :
113 : /* SNS Interpolation */
114 0 : processSnsInterpolateScf_fl(h_EncSetup->scf_q, 1, encoder->bands_number, h_EncSetup->int_scf);
115 :
116 : /* MDCT shaping */
117 0 : processMdctShaping_fl(d_fl, h_EncSetup->int_scf, encoder->bands_offset, encoder->bands_number);
118 :
119 : /* Bandwidth controller */
120 0 : if (encoder->bandwidth < encoder->fs / 2) {
121 0 : process_cutoff_bandwidth(d_fl, encoder->yLen, encoder->bw_ctrl_cutoff_bin);
122 0 : BW_cutoff_idx = MIN(BW_cutoff_idx, encoder->bw_index);
123 : }
124 :
125 : /* TNS encoder */
126 0 : if (h_EncSetup->lfe == 0)
127 : {
128 0 : processTnsCoder_fl(d_fl, BW_cutoff_idx, encoder->cutoffBins[BW_cutoff_idx], encoder->fs, encoder->frame_length,
129 : encoder->frame_dms, h_EncSetup->total_bits, tns_order, indexes, &tns_numfilters,
130 : &(h_EncSetup->tns_bits)
131 0 : , encoder->near_nyquist_flag
132 : );
133 : }
134 : else
135 : {
136 0 : tns_numfilters = 1;
137 0 : tns_order[0] = 0;
138 0 : h_EncSetup->tns_bits = tns_numfilters;
139 : }
140 : /* Global Gain Estimation */
141 0 : h_EncSetup->targetBitsQuant = h_EncSetup->targetBitsInit - (h_EncSetup->tns_bits + ltpfBits);
142 :
143 0 : if (h_EncSetup->targetBitsQuant < 0 && ltpfBits > 1)
144 : {
145 : /* Disable LTPF */
146 0 : h_EncSetup->ltpf_mem_ltpf_on = 0;
147 0 : h_EncSetup->ltpf_param[1] = 0;
148 0 : ltpfBits = 1;
149 0 : h_EncSetup->targetBitsQuant = h_EncSetup->targetBitsInit - (h_EncSetup->tns_bits + ltpfBits);
150 : }
151 :
152 0 : processEstimateGlobalGain_fl(d_fl, encoder->yLen, h_EncSetup->targetBitsQuant, &gain, &quantizedGain,
153 : &quantizedGainMin, h_EncSetup->quantizedGainOff, &h_EncSetup->targetBitsOff,
154 : &h_EncSetup->mem_targetBits, h_EncSetup->mem_specBits
155 : , encoder->hrmode, h_EncSetup->regBits, encoder->frame_ms
156 :
157 : );
158 :
159 : /* 1. Quantization */
160 0 : processQuantizeSpec_fl(d_fl, gain, q_d, encoder->yLen, h_EncSetup->total_bits, &nbits, &nbits2, encoder->fs,
161 0 : &lastnz, h_EncSetup->codingdata, &lsbMode, -1, h_EncSetup->targetBitsQuant, encoder->hrmode
162 : );
163 :
164 0 : h_EncSetup->mem_specBits = nbits;
165 :
166 : /* Global Gain Adjustment */
167 0 : processAdjustGlobalGain_fl(&quantizedGain, quantizedGainMin, h_EncSetup->quantizedGainOff, &gain,
168 : h_EncSetup->targetBitsQuant, h_EncSetup->mem_specBits, &gainChange, encoder->fs_idx
169 0 : , encoder->hrmode, encoder->frame_dms
170 : );
171 :
172 : /* 2. Quantization */
173 0 : if (gainChange) {
174 0 : processQuantizeSpec_fl(d_fl, gain, q_d, encoder->yLen, h_EncSetup->total_bits, &nbits, &nbits2, encoder->fs,
175 : &lastnz,
176 0 : h_EncSetup->codingdata,
177 : &lsbMode, 0, h_EncSetup->targetBitsQuant
178 : , encoder->hrmode
179 : );
180 : }
181 :
182 : /* Noise factor */
183 0 : if (h_EncSetup->lfe == 0)
184 : {
185 0 : processNoiseFactor_fl(&fac_ns_idx, d_fl, q_d, gain, encoder->cutoffBins[BW_cutoff_idx], encoder->frame_dms,
186 : h_EncSetup->targetBytes
187 : );
188 : }
189 : else
190 : {
191 0 : fac_ns_idx = 7;
192 : }
193 : /* Residual Coding */
194 0 : if (lsbMode == 0) {
195 0 : processResidualCoding_fl(d_fl, q_d, gain, encoder->yLen, h_EncSetup->targetBitsQuant, nbits2,
196 0 : h_EncSetup->resBits, &numResBits
197 : , encoder->hrmode
198 : );
199 : } else {
200 0 : numResBits = 0;
201 : }
202 :
203 : /* Entropy encoding */
204 0 : processEncoderEntropy_fl(bytes, &bp_side, &mask_side, h_EncSetup->targetBytes, encoder->BW_cutoff_bits,
205 : BW_cutoff_idx, lastnz, encoder->yLen, lsbMode, quantizedGain, tns_numfilters, tns_order,
206 0 : h_EncSetup->ltpf_param, h_EncSetup->L_scf_idx, fac_ns_idx
207 : , bfi_ext, encoder->fs_idx
208 : );
209 :
210 : /* Artithmetic encoding */
211 0 : processAriEncoder_fl(bytes, bp_side, mask_side, q_d, tns_order, tns_numfilters, indexes, lastnz,
212 0 : h_EncSetup->codingdata,
213 0 : h_EncSetup->resBits, numResBits, lsbMode, h_EncSetup->targetBitsAri,
214 : h_EncSetup->enable_lpc_weighting);
215 :
216 0 : if (encoder->combined_channel_coding == 0 && h_EncSetup->n_pc > 0)
217 : {
218 0 : LC3_INT32 xbuf[MAX_LEN] = {0}, nf_seed, tns_idx[M], zero_frame, nbits_residual, residualPresent, b_left, spec_inv_idx;
219 :
220 0 : memset(h_EncSetup->resBits, 0, sizeof(*(h_EncSetup->resBits)) * MAX_RESBITS_LEN);
221 :
222 0 : processAriDecoder_fl(bytes, bp_side, mask_side, encoder->yLen, encoder->fs_idx, h_EncSetup->enable_lpc_weighting,
223 : tns_numfilters, lsbMode, lastnz, &bfi_ext, tns_order, fac_ns_idx, quantizedGain,
224 0 : h_EncSetup->resBits, xbuf, &nf_seed, tns_idx, &zero_frame, h_EncSetup->targetBytes, &nbits_residual,
225 0 : &residualPresent, encoder->frame_dms, h_EncSetup->n_pc, 0, h_EncSetup->total_bits >> 3, 1, &b_left,
226 : &spec_inv_idx, encoder->hrmode);
227 :
228 0 : processReorderBitstream_fl(bytes, h_EncSetup->n_pccw, h_EncSetup->n_pc, b_left, h_EncSetup->targetBytes);
229 : }
230 :
231 0 : }
232 :
233 0 : int Enc_LC3PLUS_fl(LC3PLUS_Enc* encoder, void** input, uint8_t* output, int bps
234 : , LC3_INT32 bfi_ext
235 : )
236 : {
237 0 : int ch = 0, output_size = 0;
238 0 : uint8_t* lc3buf = output;
239 :
240 : LC3_INT32 totalBytes;
241 : LC3_INT32 output_size2, input_size;
242 :
243 0 : totalBytes = encoder->bitrate * encoder->frame_length / (8 * encoder->fs_in);
244 :
245 0 : for (ch = 0; ch < encoder->channels; ch++)
246 : {
247 0 : Enc_LC3PLUS_Channel_fl(encoder, ch, input[ch], lc3buf, bps, bfi_ext);
248 0 : if (encoder->epmode && encoder->combined_channel_coding == 0)
249 : {
250 0 : output_size2 = totalBytes / encoder->channels + (ch < (totalBytes % encoder->channels));
251 :
252 0 : fec_encoder(encoder->epmode, encoder->epmr, lc3buf, encoder->channel_setup[ch]->targetBytes, output_size2,
253 0 : encoder->channel_setup[ch]->n_pccw);
254 :
255 0 : lc3buf += output_size2;
256 0 : output_size += output_size2;
257 : }
258 : else
259 : {
260 0 : lc3buf += encoder->channel_setup[ch]->targetBytes;
261 0 : output_size += encoder->channel_setup[ch]->targetBytes;
262 : }
263 : }
264 :
265 0 : if (encoder->epmode > 0 && encoder->combined_channel_coding)
266 : {
267 0 : input_size = output_size;
268 0 : output_size = encoder->bitrate * encoder->frame_length / (8 * encoder->fs_in);
269 :
270 0 : fec_encoder(encoder->epmode, encoder->epmr, output, input_size, output_size, encoder->channel_setup[0]->n_pccw);
271 : }
272 :
273 0 : return output_size;
274 : }
|