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 "lc3plus.h"
13 : #include "defines.h"
14 : #include "functions.h"
15 : #include <stdio.h>
16 :
17 : #include "setup_dec_lc3plus.h"
18 : #include "setup_enc_lc3plus.h"
19 :
20 : #define RETURN_IF(cond, error) \
21 : if (cond) \
22 : return (error)
23 :
24 : /* ensure api header constants are up to date */
25 : STATIC_ASSERT(LC3PLUS_MAX_SAMPLES >= MAX_LEN);
26 : STATIC_ASSERT(LC3PLUS_MAX_CHANNELS >= MAX_CHANNELS);
27 : STATIC_ASSERT(LC3PLUS_MAX_BYTES >= BYTESBUFSIZE);
28 :
29 : /* misc functions ************************************************************/
30 :
31 0 : int lc3plus_version(void)
32 : {
33 0 : return LC3PLUS_VERSION;
34 : }
35 :
36 3938 : int lc3plus_channels_supported(int channels)
37 : {
38 3938 : return channels >= 1 && channels <= MAX_CHANNELS;
39 : }
40 :
41 3938 : int lc3plus_samplerate_supported(int samplerate)
42 : {
43 3938 : switch (samplerate)
44 : {
45 0 : case 8000: return 1;
46 0 : case 16000: return 1;
47 0 : case 24000: return 1;
48 0 : case 32000: return 1;
49 0 : case 44100: return 1;
50 3938 : case 48000: return 1;
51 0 : case 96000: return 1;
52 0 : default: break;
53 : }
54 0 : return 0;
55 : }
56 :
57 716 : static int lc3plus_plc_mode_supported(LC3PLUS_PlcMode plc_mode)
58 : {
59 716 : switch ((int)plc_mode)
60 : {
61 716 : case LC3PLUS_PLC_ADVANCED: /* fallthru */
62 716 : return 1;
63 0 : default: break;
64 : }
65 0 : return 0;
66 : }
67 :
68 1432 : static int lc3plus_frame_size_supported(LC3PLUS_FrameDuration frame_dms)
69 : {
70 1432 : switch (frame_dms)
71 : {
72 : #ifdef CR9_C_ADD_1p25MS
73 1432 : case LC3PLUS_FRAME_DURATION_1p25MS: /* fallthru */
74 : #endif
75 : case LC3PLUS_FRAME_DURATION_2p5MS: /* fallthru */
76 : case LC3PLUS_FRAME_DURATION_5MS: /* fallthru */
77 : case LC3PLUS_FRAME_DURATION_7p5MS: /* fallthru */
78 : case LC3PLUS_FRAME_DURATION_10MS:
79 1432 : return 1;
80 0 : default: break;
81 : }
82 :
83 0 : return 0;
84 : }
85 :
86 1181012 : static int null_in_list(void **list, int n)
87 : {
88 2362024 : while (--n >= 0)
89 1181012 : RETURN_IF(list[n] == NULL, 1);
90 1181012 : return 0;
91 : }
92 :
93 : /* return pointer to aligned base + base_size, *base_size += size + 4 bytes align */
94 42602 : void *balloc(void *base, size_t *base_size, size_t size)
95 : {
96 42602 : uintptr_t ptr = ((uintptr_t)base + *base_size + 3) & (uintptr_t)(~3);
97 42602 : assert((uintptr_t)base % 4 == 0); /* base must be 4-byte aligned */
98 42602 : *base_size = (*base_size + size + 3) & (uintptr_t)(~3);
99 42602 : return (void *)ptr;
100 : }
101 :
102 716 : int32_t lc3_enc_supported_lfe(void)
103 : {
104 716 : return 1;
105 : }
106 :
107 : /* encoder functions *********************************************************/
108 716 : LC3PLUS_Error lc3plus_enc_init(LC3PLUS_Enc *encoder, int samplerate, int channels, int hrmode, int32_t lfe_channel_array[])
109 : {
110 716 : int ch = 0;
111 716 : RETURN_IF(encoder == NULL, LC3PLUS_NULL_ERROR);
112 716 : RETURN_IF((uintptr_t)encoder % 4 != 0, LC3PLUS_ALIGN_ERROR);
113 716 : RETURN_IF(!lc3plus_samplerate_supported(samplerate), LC3PLUS_SAMPLERATE_ERROR);
114 716 : RETURN_IF(!lc3plus_channels_supported(channels), LC3PLUS_CHANNELS_ERROR);
115 716 : RETURN_IF(samplerate==96000 && hrmode == 0, LC3PLUS_HRMODE_ERROR);
116 :
117 716 : if (lfe_channel_array != NULL)
118 : {
119 1432 : for (ch = 0; ch < channels; ch++)
120 : {
121 716 : RETURN_IF(!lc3_enc_supported_lfe() && lfe_channel_array[ch], LC3PLUS_LFE_MODE_NOT_SUPPORTED);
122 : }
123 : }
124 :
125 716 : return FillEncSetup(encoder, samplerate, channels, hrmode, lfe_channel_array); /* real bitrate check happens here */
126 : }
127 :
128 1074 : int lc3plus_enc_get_size(int samplerate, int channels)
129 : {
130 1074 : RETURN_IF(!lc3plus_samplerate_supported(samplerate), 0);
131 1074 : RETURN_IF(!lc3plus_channels_supported(channels), 0);
132 1074 : return alloc_encoder(NULL, channels);
133 : }
134 :
135 : /* Dummy function for API alignment */
136 0 : int lc3plus_enc_get_scratch_size(const LC3PLUS_Enc *encoder)
137 : {
138 : UNUSED(encoder);
139 0 : return 0;
140 : }
141 :
142 0 : int lc3plus_enc_get_input_samples(const LC3PLUS_Enc *encoder)
143 : {
144 0 : RETURN_IF(encoder == NULL, 0);
145 0 : return encoder->frame_length;
146 : }
147 :
148 1523912 : int lc3plus_enc_get_num_bytes(const LC3PLUS_Enc *encoder)
149 : {
150 1523912 : RETURN_IF(encoder == NULL, 0);
151 :
152 1523912 : return encoder->bitrate * encoder->frame_length / (8 * encoder->fs_in);
153 : }
154 :
155 0 : int lc3plus_enc_get_real_bitrate(const LC3PLUS_Enc *encoder)
156 : {
157 0 : int ch = 0, totalBytes = 0;
158 : int bitrate;
159 : #ifdef CR9_C_ADD_1p25MS
160 : int frame_ns;
161 : #endif
162 0 : RETURN_IF(encoder == NULL, 0);
163 0 : RETURN_IF(!encoder->lc3_br_set, LC3PLUS_BITRATE_UNSET_ERROR);
164 :
165 0 : for (ch = 0; ch < encoder->channels; ch++)
166 : {
167 0 : totalBytes += encoder->channel_setup[ch]->targetBytes;
168 : }
169 : #ifdef CR9_C_ADD_1p25MS
170 0 : frame_ns = (int)(1250L * encoder->frame_dms);
171 0 : bitrate = (int) ((totalBytes*8L) * 1000000L + (frame_ns - 1) ) / (frame_ns);
172 : #else
173 : bitrate = (totalBytes * 80000.0 + encoder->frame_dms - 1) / encoder->frame_dms;
174 : #endif
175 0 : if (encoder->fs_in == 44100)
176 : {
177 0 : int rem = bitrate % 480;
178 0 : bitrate = ((bitrate - rem) / 480) * 441 + (rem * 441) / 480;
179 : }
180 :
181 0 : return bitrate;
182 : }
183 :
184 :
185 170476 : LC3PLUS_Error lc3plus_enc_set_bitrate(LC3PLUS_Enc *encoder, int bitrate)
186 : {
187 170476 : RETURN_IF(encoder == NULL, LC3PLUS_NULL_ERROR);
188 170476 : RETURN_IF(bitrate <= 0, LC3PLUS_BITRATE_ERROR);
189 : #ifndef STRIP_HR_MODE_API
190 170476 : RETURN_IF(encoder->fs_idx == 5 && encoder->hrmode == 0, LC3PLUS_HRMODE_ERROR);
191 : #endif
192 170476 : return update_enc_bitrate(encoder, bitrate);
193 : }
194 :
195 716 : int lc3plus_enc_get_delay(const LC3PLUS_Enc *encoder)
196 : {
197 716 : RETURN_IF(encoder == NULL, 0);
198 716 : return encoder->frame_length - 2 * encoder->la_zeroes;
199 : }
200 :
201 716 : LC3PLUS_Error lc3plus_enc_set_frame_dms(LC3PLUS_Enc *encoder, LC3PLUS_FrameDuration frame_dms)
202 : {
203 716 : RETURN_IF(encoder == NULL, LC3PLUS_NULL_ERROR);
204 716 : RETURN_IF(!lc3plus_frame_size_supported(frame_dms), LC3PLUS_FRAMEMS_ERROR);
205 716 : RETURN_IF(encoder->lc3_br_set, LC3PLUS_BITRATE_SET_ERROR);
206 : #ifdef CR9_C_ADD_1p25MS
207 716 : RETURN_IF(encoder->fs == 8000 && frame_dms == LC3PLUS_FRAME_DURATION_1p25MS, LC3PLUS_SAMPLERATE_ERROR);
208 : #endif
209 :
210 716 : encoder->frame_dms = frame_dms;
211 716 : encoder->frame_ms = frame_dms;
212 :
213 716 : set_enc_frame_params(encoder);
214 716 : return LC3PLUS_OK;
215 : }
216 :
217 0 : LC3PLUS_Error lc3plus_enc_set_bandwidth(LC3PLUS_Enc *encoder, int bandwidth)
218 : {
219 : LC3_INT effective_fs;
220 0 : RETURN_IF(encoder == NULL, LC3PLUS_NULL_ERROR);
221 : #ifdef ENABLE_HR_MODE_FL_FLAG
222 0 : RETURN_IF(encoder->hrmode == 1, LC3PLUS_HRMODE_BW_ERROR);
223 : #endif
224 0 : effective_fs = encoder->fs_in;
225 0 : if (encoder->bandwidth != bandwidth) {
226 0 : if (encoder->fs_in > 40000) {
227 0 : effective_fs = 40000;
228 : }
229 0 : if ((bandwidth * 2) > effective_fs) {
230 0 : return LC3PLUS_BW_WARNING;
231 : }
232 : else {
233 0 : encoder->bandwidth = bandwidth;
234 0 : encoder->bandwidth_preset = bandwidth;
235 0 : encoder->bw_ctrl_active = 1;
236 0 : update_enc_bitrate(encoder, encoder->bitrate);
237 : }
238 : }
239 0 : return LC3PLUS_OK;
240 : }
241 :
242 :
243 590506 : LC3PLUS_Error lc3plus_enc16(LC3PLUS_Enc* encoder, int16_t** input_samples, void* output_bytes, int* num_bytes
244 : , void *scratch
245 : )
246 : {
247 : UNUSED(scratch);
248 590506 : return lc3plus_enc_fl(encoder, (void**)input_samples, 16, output_bytes, num_bytes);
249 : }
250 :
251 0 : LC3PLUS_Error lc3plus_enc24(LC3PLUS_Enc* encoder, int32_t** input_samples, void* output_bytes, int* num_bytes
252 : , void *scratch
253 : )
254 : {
255 : UNUSED(scratch);
256 0 : return lc3plus_enc_fl(encoder, (void**)input_samples, 24, output_bytes, num_bytes);
257 : }
258 :
259 :
260 590506 : LC3PLUS_Error lc3plus_enc_fl(LC3PLUS_Enc* encoder, void** input_samples, int bitdepth, void* output_bytes, int* num_bytes)
261 : {
262 590506 : RETURN_IF(!encoder || !input_samples || !output_bytes || !num_bytes, LC3PLUS_NULL_ERROR);
263 590506 : RETURN_IF(null_in_list(input_samples, encoder->channels), LC3PLUS_NULL_ERROR);
264 590506 : RETURN_IF(bitdepth != 16 && bitdepth != 24, LC3PLUS_ERROR);
265 1181012 : *num_bytes = Enc_LC3PLUS_fl(encoder, input_samples, output_bytes, bitdepth
266 590506 : , *num_bytes == -1
267 : );
268 590506 : assert(*num_bytes == lc3plus_enc_get_num_bytes(encoder));
269 590506 : return LC3PLUS_OK;
270 : }
271 :
272 : /* decoder functions *********************************************************/
273 :
274 716 : LC3PLUS_Error lc3plus_dec_init(LC3PLUS_Dec* decoder, int samplerate, int channels, LC3PLUS_PlcMode plc_mode, int hrmode)
275 : {
276 716 : RETURN_IF(decoder == NULL, LC3PLUS_NULL_ERROR);
277 716 : RETURN_IF(!lc3plus_samplerate_supported(samplerate), LC3PLUS_SAMPLERATE_ERROR);
278 716 : RETURN_IF(!lc3plus_channels_supported(channels), LC3PLUS_CHANNELS_ERROR);
279 716 : RETURN_IF(!lc3plus_plc_mode_supported(plc_mode), LC3PLUS_PLCMODE_ERROR);
280 716 : RETURN_IF(samplerate==96000 && hrmode == 0, LC3PLUS_HRMODE_ERROR);
281 716 : return FillDecSetup(decoder, samplerate, channels, plc_mode, hrmode);
282 : }
283 :
284 1432 : int lc3plus_dec_get_size(int samplerate, int channels)
285 : {
286 1432 : RETURN_IF(!lc3plus_samplerate_supported(samplerate), 0);
287 1432 : RETURN_IF(!lc3plus_channels_supported(channels), 0);
288 1432 : return alloc_decoder(NULL, samplerate, channels);
289 : }
290 :
291 : /* Dummy function for API alignment */
292 0 : int lc3plus_dec_get_scratch_size(const LC3PLUS_Dec *decoder)
293 : {
294 : UNUSED(decoder);
295 0 : return 0;
296 : }
297 :
298 716 : LC3PLUS_Error lc3plus_dec_set_frame_dms(LC3PLUS_Dec *decoder, LC3PLUS_FrameDuration frame_dms)
299 : {
300 716 : RETURN_IF(decoder == NULL, LC3PLUS_NULL_ERROR);
301 716 : RETURN_IF(!lc3plus_frame_size_supported(frame_dms), LC3PLUS_FRAMEMS_ERROR);
302 716 : RETURN_IF(decoder->plcMeth == 2 && frame_dms != LC3PLUS_FRAME_DURATION_10MS, LC3PLUS_FRAMEMS_ERROR);
303 : #ifdef CR9_C_ADD_1p25MS
304 716 : RETURN_IF(decoder->fs == 8000 && frame_dms == LC3PLUS_FRAME_DURATION_1p25MS, LC3PLUS_SAMPLERATE_ERROR);
305 : #endif
306 :
307 716 : decoder->frame_dms = frame_dms;
308 716 : decoder->frame_ms = frame_dms;
309 :
310 716 : set_dec_frame_params(decoder);
311 716 : return LC3PLUS_OK;
312 : }
313 :
314 0 : int lc3plus_dec_get_output_samples(const LC3PLUS_Dec* decoder)
315 : {
316 0 : RETURN_IF(decoder == NULL, 0);
317 0 : return decoder->frame_length;
318 : }
319 :
320 716 : int lc3plus_dec_get_delay(const LC3PLUS_Dec* decoder)
321 : {
322 716 : RETURN_IF(decoder == NULL, 0);
323 716 : return decoder->frame_length - 2 * decoder->la_zeroes;
324 : }
325 :
326 590506 : LC3PLUS_Error lc3plus_dec_fl(LC3PLUS_Dec* decoder, void* input_bytes, int num_bytes, void** output_samples, int bps, int bfi_ext)
327 : {
328 590506 : if (bfi_ext == 1)
329 : {
330 0 : RETURN_IF(!decoder || !output_samples, LC3PLUS_NULL_ERROR);
331 : } else {
332 590506 : RETURN_IF(!decoder || !input_bytes || !output_samples, LC3PLUS_NULL_ERROR);
333 : }
334 :
335 590506 : RETURN_IF(null_in_list((void**)output_samples, decoder->channels), LC3PLUS_NULL_ERROR);
336 590506 : return Dec_LC3PLUS_fl(decoder, input_bytes, num_bytes, output_samples, bps, bfi_ext);
337 : }
338 :
339 590506 : LC3PLUS_Error lc3plus_dec16(LC3PLUS_Dec* decoder, void* input_bytes, int num_bytes, int16_t** output_samples,
340 : void* scratch,
341 : int bfi_ext)
342 : {
343 : UNUSED(scratch);
344 590506 : return lc3plus_dec_fl(decoder, input_bytes, num_bytes, (void**)output_samples, 16, bfi_ext);
345 : }
346 :
347 0 : LC3PLUS_Error lc3plus_dec24(LC3PLUS_Dec* decoder, void* input_bytes, int num_bytes, int32_t** output_samples,
348 : void* scratch,
349 : int bfi_ext)
350 : {
351 : UNUSED(scratch);
352 0 : return lc3plus_dec_fl(decoder, input_bytes, num_bytes, (void**)output_samples, 24, bfi_ext);
353 : }
354 :
355 : /* memory functions *********************************************************/
356 :
357 0 : LC3PLUS_Error lc3plus_enc_free_memory(LC3PLUS_Enc* encoder)
358 : {
359 0 : RETURN_IF(!encoder, LC3PLUS_NULL_ERROR);
360 :
361 0 : lc3plus_free_encoder_structs(encoder);
362 :
363 0 : return LC3PLUS_OK;
364 : }
365 :
366 0 : LC3PLUS_Error lc3plus_dec_free_memory(LC3PLUS_Dec* decoder)
367 : {
368 0 : RETURN_IF(!decoder, LC3PLUS_NULL_ERROR);
369 :
370 0 : lc3plus_free_decoder_structs(decoder);
371 :
372 0 : return LC3PLUS_OK;
373 : }
374 :
375 716 : LC3PLUS_Error lc3plus_free_encoder_structs(LC3PLUS_Enc* encoder)
376 : {
377 716 : int ch = 0;
378 716 : RETURN_IF(!encoder, LC3PLUS_NULL_ERROR);
379 :
380 1432 : for (ch = 0; ch < encoder->channels; ch++) {
381 716 : mdct_free(&encoder->channel_setup[ch]->mdctStruct);
382 716 : dct2_free(&encoder->channel_setup[ch]->dct2StructSNS);
383 : }
384 :
385 716 : return LC3PLUS_OK;
386 : }
387 :
388 716 : LC3PLUS_Error lc3plus_free_decoder_structs(LC3PLUS_Dec* decoder)
389 : {
390 716 : int ch = 0;
391 716 : RETURN_IF(!decoder, LC3PLUS_NULL_ERROR);
392 :
393 1432 : for (ch = 0; ch < decoder->channels; ch++) {
394 716 : dct4_free(&decoder->channel_setup[ch]->dct4structImdct);
395 716 : real_fft_free(&decoder->channel_setup[ch]->PlcAdvSetup->PlcPhEcuSetup.PhEcu_Fft);
396 716 : real_fft_free(&decoder->channel_setup[ch]->PlcAdvSetup->PlcPhEcuSetup.PhEcu_Ifft);
397 : }
398 :
399 716 : return LC3PLUS_OK;
400 : }
401 :
402 :
403 0 : LC3PLUS_EpModeRequest lc3plus_dec_get_ep_mode_request(const LC3PLUS_Dec *decoder)
404 : {
405 0 : RETURN_IF(decoder == NULL, LC3PLUS_EPMR_ZERO);
406 0 : return (LC3PLUS_EpModeRequest)decoder->epmr;
407 : }
408 :
409 0 : int lc3plus_dec_get_error_report(const LC3PLUS_Dec *decoder)
410 : {
411 0 : RETURN_IF(decoder == NULL, 0);
412 0 : return decoder->error_report == 2047 ? -1 : decoder->error_report & 0x07FF;
413 : }
414 :
415 0 : LC3PLUS_Error lc3plus_enc_set_ep_mode(LC3PLUS_Enc *encoder, LC3PLUS_EpMode epmode)
416 : {
417 : LC3PLUS_EpMode oldEpmode;
418 : LC3PLUS_Error error;
419 0 : RETURN_IF(encoder == NULL, LC3PLUS_NULL_ERROR);
420 0 : RETURN_IF((unsigned)epmode > LC3PLUS_EP_HIGH, LC3PLUS_EPMODE_ERROR);
421 0 : oldEpmode = encoder->epmode;
422 0 : encoder->epmode = epmode;
423 0 : error = encoder->lc3_br_set ? update_enc_bitrate(encoder, encoder->bitrate) : LC3PLUS_OK;
424 0 : if (error != LC3PLUS_OK)
425 : {
426 0 : encoder->epmode = oldEpmode; /* preserve old epmode in case of failure */
427 : }
428 0 : return error;
429 : }
430 :
431 0 : LC3PLUS_Error lc3plus_enc_set_ep_mode_request(LC3PLUS_Enc *encoder, LC3PLUS_EpModeRequest epmr)
432 : {
433 0 : RETURN_IF(encoder == NULL, LC3PLUS_NULL_ERROR);
434 0 : RETURN_IF((unsigned)epmr > LC3PLUS_EPMR_HIGH, LC3PLUS_EPMODE_ERROR);
435 0 : encoder->epmr = epmr;
436 0 : return LC3PLUS_OK;
437 : }
438 :
439 716 : LC3PLUS_Error lc3plus_dec_set_ep_enabled(LC3PLUS_Dec *decoder, int32_t ep_enabled)
440 : {
441 716 : RETURN_IF(decoder == NULL, LC3PLUS_NULL_ERROR);
442 716 : decoder->ep_enabled = ep_enabled != 0;
443 716 : decoder->epmr = LC3PLUS_EPMR_ZERO;
444 716 : return LC3PLUS_OK;
445 : }
446 :
447 0 : int lc3plus_dec_get_epok_flags(const LC3PLUS_Dec *decoder)
448 : {
449 0 : RETURN_IF(decoder == NULL, 0);
450 0 : return decoder->error_report >> 11;
451 : }
452 :
453 : #ifndef STRIP_ERROR_PROTECTION_API_FL
454 : #endif /* STRIP_ERROR_PROTECTION_API_FL */
455 :
456 : #ifndef STRIP_ERROR_PROTECTION_API_FL
457 : #endif /* STRIP_ERROR_PROTECTION_API_FL */
458 :
|