1 /******************************************************************************
2 *
3 * Copyright 2022 Google LLC
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include <lc3.h>
20
21 #include "common.h"
22 #include "bits.h"
23
24 #include "attdet.h"
25 #include "bwdet.h"
26 #include "ltpf.h"
27 #include "mdct.h"
28 #include "energy.h"
29 #include "sns.h"
30 #include "tns.h"
31 #include "spec.h"
32 #include "plc.h"
33
34
35 /**
36 * Frame side data
37 */
38
39 struct side_data {
40 enum lc3_bandwidth bw;
41 bool pitch_present;
42 lc3_ltpf_data_t ltpf;
43 lc3_sns_data_t sns;
44 lc3_tns_data_t tns;
45 lc3_spec_side_t spec;
46 };
47
48
49 /* ----------------------------------------------------------------------------
50 * General
51 * -------------------------------------------------------------------------- */
52
53 /**
54 * Resolve frame duration in us
55 * us Frame duration in us
56 * return Frame duration identifier, or LC3_NUM_DT
57 */
resolve_dt(int us)58 static enum lc3_dt resolve_dt(int us)
59 {
60 return us == 7500 ? LC3_DT_7M5 :
61 us == 10000 ? LC3_DT_10M : LC3_NUM_DT;
62 }
63
64 /**
65 * Resolve samplerate in Hz
66 * hz Samplerate in Hz
67 * return Sample rate identifier, or LC3_NUM_SRATE
68 */
resolve_sr(int hz)69 static enum lc3_srate resolve_sr(int hz)
70 {
71 return hz == 8000 ? LC3_SRATE_8K : hz == 16000 ? LC3_SRATE_16K :
72 hz == 24000 ? LC3_SRATE_24K : hz == 32000 ? LC3_SRATE_32K :
73 hz == 48000 ? LC3_SRATE_48K : LC3_NUM_SRATE;
74 }
75
76 /**
77 * Return the number of PCM samples in a frame
78 */
lc3_frame_samples(int dt_us,int sr_hz)79 int lc3_frame_samples(int dt_us, int sr_hz)
80 {
81 enum lc3_dt dt = resolve_dt(dt_us);
82 enum lc3_srate sr = resolve_sr(sr_hz);
83
84 if (dt >= LC3_NUM_DT || sr >= LC3_NUM_SRATE)
85 return -1;
86
87 return LC3_NS(dt, sr);
88 }
89
90 /**
91 * Return the size of frames, from bitrate
92 */
lc3_frame_bytes(int dt_us,int bitrate)93 int lc3_frame_bytes(int dt_us, int bitrate)
94 {
95 if (resolve_dt(dt_us) >= LC3_NUM_DT)
96 return -1;
97
98 if (bitrate < LC3_MIN_BITRATE)
99 return LC3_MIN_FRAME_BYTES;
100
101 if (bitrate > LC3_MAX_BITRATE)
102 return LC3_MAX_FRAME_BYTES;
103
104 int nbytes = ((unsigned)bitrate * dt_us) / (1000*1000*8);
105
106 return LC3_CLIP(nbytes, LC3_MIN_FRAME_BYTES, LC3_MAX_FRAME_BYTES);
107 }
108
109 /**
110 * Resolve the bitrate, from the size of frames
111 */
lc3_resolve_bitrate(int dt_us,int nbytes)112 int lc3_resolve_bitrate(int dt_us, int nbytes)
113 {
114 if (resolve_dt(dt_us) >= LC3_NUM_DT)
115 return -1;
116
117 if (nbytes < LC3_MIN_FRAME_BYTES)
118 return LC3_MIN_BITRATE;
119
120 if (nbytes > LC3_MAX_FRAME_BYTES)
121 return LC3_MAX_BITRATE;
122
123 int bitrate = ((unsigned)nbytes * (1000*1000*8) + dt_us/2) / dt_us;
124
125 return LC3_CLIP(bitrate, LC3_MIN_BITRATE, LC3_MAX_BITRATE);
126 }
127
128 /**
129 * Return algorithmic delay, as a number of samples
130 */
lc3_delay_samples(int dt_us,int sr_hz)131 int lc3_delay_samples(int dt_us, int sr_hz)
132 {
133 enum lc3_dt dt = resolve_dt(dt_us);
134 enum lc3_srate sr = resolve_sr(sr_hz);
135
136 if (dt >= LC3_NUM_DT || sr >= LC3_NUM_SRATE)
137 return -1;
138
139 return (dt == LC3_DT_7M5 ? 8 : 5) * (LC3_SRATE_KHZ(sr) / 2);
140 }
141
142
143 /* ----------------------------------------------------------------------------
144 * Encoder
145 * -------------------------------------------------------------------------- */
146
147 /**
148 * Input PCM Samples from signed 16 bits
149 * encoder Encoder state
150 * pcm, stride Input PCM samples, and count between two consecutives
151 */
load_s16(struct lc3_encoder * encoder,const void * _pcm,int stride)152 static void load_s16(
153 struct lc3_encoder *encoder, const void *_pcm, int stride)
154 {
155 const int16_t *pcm = _pcm;
156
157 enum lc3_dt dt = encoder->dt;
158 enum lc3_srate sr = encoder->sr_pcm;
159
160 int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
161 float *xs = encoder->x + encoder->xs_off;
162 int ns = LC3_NS(dt, sr);
163
164 for (int i = 0; i < ns; i++, pcm += stride)
165 xt[i] = *pcm, xs[i] = *pcm;
166 }
167
168 /**
169 * Input PCM Samples from signed 24 bits
170 * encoder Encoder state
171 * pcm, stride Input PCM samples, and count between two consecutives
172 */
load_s24(struct lc3_encoder * encoder,const void * _pcm,int stride)173 static void load_s24(
174 struct lc3_encoder *encoder, const void *_pcm, int stride)
175 {
176 const int32_t *pcm = _pcm;
177
178 enum lc3_dt dt = encoder->dt;
179 enum lc3_srate sr = encoder->sr_pcm;
180
181 int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
182 float *xs = encoder->x + encoder->xs_off;
183 int ns = LC3_NS(dt, sr);
184
185 for (int i = 0; i < ns; i++, pcm += stride) {
186 xt[i] = *pcm >> 8;
187 xs[i] = ldexpf(*pcm, -8);
188 }
189 }
190
191 /**
192 * Input PCM Samples from signed 24 bits packed
193 * encoder Encoder state
194 * pcm, stride Input PCM samples, and count between two consecutives
195 */
load_s24_3le(struct lc3_encoder * encoder,const void * _pcm,int stride)196 static void load_s24_3le(
197 struct lc3_encoder *encoder, const void *_pcm, int stride)
198 {
199 const uint8_t *pcm = _pcm;
200
201 enum lc3_dt dt = encoder->dt;
202 enum lc3_srate sr = encoder->sr_pcm;
203
204 int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
205 float *xs = encoder->x + encoder->xs_off;
206 int ns = LC3_NS(dt, sr);
207
208 for (int i = 0; i < ns; i++, pcm += 3*stride) {
209 int32_t in = ((uint32_t)pcm[0] << 8) |
210 ((uint32_t)pcm[1] << 16) |
211 ((uint32_t)pcm[2] << 24) ;
212
213 xt[i] = in >> 16;
214 xs[i] = ldexpf(in, -16);
215 }
216 }
217
218 /**
219 * Input PCM Samples from float 32 bits
220 * encoder Encoder state
221 * pcm, stride Input PCM samples, and count between two consecutives
222 */
load_float(struct lc3_encoder * encoder,const void * _pcm,int stride)223 static void load_float(
224 struct lc3_encoder *encoder, const void *_pcm, int stride)
225 {
226 const float *pcm = _pcm;
227
228 enum lc3_dt dt = encoder->dt;
229 enum lc3_srate sr = encoder->sr_pcm;
230
231 int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
232 float *xs = encoder->x + encoder->xs_off;
233 int ns = LC3_NS(dt, sr);
234
235 for (int i = 0; i < ns; i++, pcm += stride) {
236 xs[i] = ldexpf(*pcm, 15);
237 xt[i] = LC3_SAT16((int32_t)xs[i]);
238 }
239 }
240
241 /**
242 * Frame Analysis
243 * encoder Encoder state
244 * nbytes Size in bytes of the frame
245 * side, xq Return frame data
246 */
analyze(struct lc3_encoder * encoder,int nbytes,struct side_data * side,uint16_t * xq)247 static void analyze(struct lc3_encoder *encoder,
248 int nbytes, struct side_data *side, uint16_t *xq)
249 {
250 enum lc3_dt dt = encoder->dt;
251 enum lc3_srate sr = encoder->sr;
252 enum lc3_srate sr_pcm = encoder->sr_pcm;
253 int ns = LC3_NS(dt, sr_pcm);
254 int nt = LC3_NT(sr_pcm);
255
256 int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
257 float *xs = encoder->x + encoder->xs_off;
258 float *xd = encoder->x + encoder->xd_off;
259 float *xf = xs;
260
261 /* --- Temporal --- */
262
263 bool att = lc3_attdet_run(dt, sr_pcm, nbytes, &encoder->attdet, xt);
264
265 side->pitch_present =
266 lc3_ltpf_analyse(dt, sr_pcm, &encoder->ltpf, xt, &side->ltpf);
267
268 memmove(xt - nt, xt + (ns-nt), nt * sizeof(*xt));
269
270 /* --- Spectral --- */
271
272 float e[LC3_NUM_BANDS];
273
274 lc3_mdct_forward(dt, sr_pcm, sr, xs, xd, xf);
275
276 bool nn_flag = lc3_energy_compute(dt, sr, xf, e);
277 if (nn_flag)
278 lc3_ltpf_disable(&side->ltpf);
279
280 side->bw = lc3_bwdet_run(dt, sr, e);
281
282 lc3_sns_analyze(dt, sr, e, att, &side->sns, xf, xf);
283
284 lc3_tns_analyze(dt, side->bw, nn_flag, nbytes, &side->tns, xf);
285
286 lc3_spec_analyze(dt, sr,
287 nbytes, side->pitch_present, &side->tns,
288 &encoder->spec, xf, xq, &side->spec);
289 }
290
291 /**
292 * Encode bitstream
293 * encoder Encoder state
294 * side, xq The frame data
295 * nbytes Target size of the frame (20 to 400)
296 * buffer Output bitstream buffer of `nbytes` size
297 */
encode(struct lc3_encoder * encoder,const struct side_data * side,uint16_t * xq,int nbytes,void * buffer)298 static void encode(struct lc3_encoder *encoder,
299 const struct side_data *side, uint16_t *xq, int nbytes, void *buffer)
300 {
301 enum lc3_dt dt = encoder->dt;
302 enum lc3_srate sr = encoder->sr;
303 enum lc3_bandwidth bw = side->bw;
304 float *xf = encoder->x + encoder->xs_off;
305
306 lc3_bits_t bits;
307
308 lc3_setup_bits(&bits, LC3_BITS_MODE_WRITE, buffer, nbytes);
309
310 lc3_bwdet_put_bw(&bits, sr, bw);
311
312 lc3_spec_put_side(&bits, dt, sr, &side->spec);
313
314 lc3_tns_put_data(&bits, &side->tns);
315
316 lc3_put_bit(&bits, side->pitch_present);
317
318 lc3_sns_put_data(&bits, &side->sns);
319
320 if (side->pitch_present)
321 lc3_ltpf_put_data(&bits, &side->ltpf);
322
323 lc3_spec_encode(&bits,
324 dt, sr, bw, nbytes, xq, &side->spec, xf);
325
326 lc3_flush_bits(&bits);
327 }
328
329 /**
330 * Return size needed for an encoder
331 */
lc3_encoder_size(int dt_us,int sr_hz)332 unsigned lc3_encoder_size(int dt_us, int sr_hz)
333 {
334 if (resolve_dt(dt_us) >= LC3_NUM_DT ||
335 resolve_sr(sr_hz) >= LC3_NUM_SRATE)
336 return 0;
337
338 return sizeof(struct lc3_encoder) +
339 (LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz)-1) * sizeof(float);
340 }
341
342 /**
343 * Setup encoder
344 */
lc3_setup_encoder(int dt_us,int sr_hz,int sr_pcm_hz,void * mem)345 struct lc3_encoder *lc3_setup_encoder(
346 int dt_us, int sr_hz, int sr_pcm_hz, void *mem)
347 {
348 if (sr_pcm_hz <= 0)
349 sr_pcm_hz = sr_hz;
350
351 enum lc3_dt dt = resolve_dt(dt_us);
352 enum lc3_srate sr = resolve_sr(sr_hz);
353 enum lc3_srate sr_pcm = resolve_sr(sr_pcm_hz);
354
355 if (dt >= LC3_NUM_DT || sr_pcm >= LC3_NUM_SRATE || sr > sr_pcm || !mem)
356 return NULL;
357
358 struct lc3_encoder *encoder = mem;
359 int ns = LC3_NS(dt, sr_pcm);
360 int nt = LC3_NT(sr_pcm);
361
362 *encoder = (struct lc3_encoder){
363 .dt = dt, .sr = sr,
364 .sr_pcm = sr_pcm,
365
366 .xt_off = nt,
367 .xs_off = (nt + ns) / 2,
368 .xd_off = (nt + ns) / 2 + ns,
369 };
370
371 memset(encoder->x, 0,
372 LC3_ENCODER_BUFFER_COUNT(dt_us, sr_pcm_hz) * sizeof(float));
373
374 return encoder;
375 }
376
377 /**
378 * Encode a frame
379 */
lc3_encode(struct lc3_encoder * encoder,enum lc3_pcm_format fmt,const void * pcm,int stride,int nbytes,void * out)380 int lc3_encode(struct lc3_encoder *encoder, enum lc3_pcm_format fmt,
381 const void *pcm, int stride, int nbytes, void *out)
382 {
383 static void (* const load[])(struct lc3_encoder *, const void *, int) = {
384 [LC3_PCM_FORMAT_S16 ] = load_s16,
385 [LC3_PCM_FORMAT_S24 ] = load_s24,
386 [LC3_PCM_FORMAT_S24_3LE] = load_s24_3le,
387 [LC3_PCM_FORMAT_FLOAT ] = load_float,
388 };
389
390 /* --- Check parameters --- */
391
392 if (!encoder || nbytes < LC3_MIN_FRAME_BYTES
393 || nbytes > LC3_MAX_FRAME_BYTES)
394 return -1;
395
396 /* --- Processing --- */
397
398 struct side_data side;
399 uint16_t xq[LC3_MAX_NE];
400
401 load[fmt](encoder, pcm, stride);
402
403 analyze(encoder, nbytes, &side, xq);
404
405 encode(encoder, &side, xq, nbytes, out);
406
407 return 0;
408 }
409
410
411 /* ----------------------------------------------------------------------------
412 * Decoder
413 * -------------------------------------------------------------------------- */
414
415 /**
416 * Output PCM Samples to signed 16 bits
417 * decoder Decoder state
418 * pcm, stride Output PCM samples, and count between two consecutives
419 */
store_s16(struct lc3_decoder * decoder,void * _pcm,int stride)420 static void store_s16(
421 struct lc3_decoder *decoder, void *_pcm, int stride)
422 {
423 int16_t *pcm = _pcm;
424
425 enum lc3_dt dt = decoder->dt;
426 enum lc3_srate sr = decoder->sr_pcm;
427
428 float *xs = decoder->x + decoder->xs_off;
429 int ns = LC3_NS(dt, sr);
430
431 for ( ; ns > 0; ns--, xs++, pcm += stride) {
432 int32_t s = *xs >= 0 ? (int)(*xs + 0.5f) : (int)(*xs - 0.5f);
433 *pcm = LC3_SAT16(s);
434 }
435 }
436
437 /**
438 * Output PCM Samples to signed 24 bits
439 * decoder Decoder state
440 * pcm, stride Output PCM samples, and count between two consecutives
441 */
store_s24(struct lc3_decoder * decoder,void * _pcm,int stride)442 static void store_s24(
443 struct lc3_decoder *decoder, void *_pcm, int stride)
444 {
445 int32_t *pcm = _pcm;
446
447 enum lc3_dt dt = decoder->dt;
448 enum lc3_srate sr = decoder->sr_pcm;
449
450 float *xs = decoder->x + decoder->xs_off;
451 int ns = LC3_NS(dt, sr);
452
453 for ( ; ns > 0; ns--, xs++, pcm += stride) {
454 int32_t s = *xs >= 0 ? (int32_t)(ldexpf(*xs, 8) + 0.5f)
455 : (int32_t)(ldexpf(*xs, 8) - 0.5f);
456 *pcm = LC3_SAT24(s);
457 }
458 }
459
460 /**
461 * Output PCM Samples to signed 24 bits packed
462 * decoder Decoder state
463 * pcm, stride Output PCM samples, and count between two consecutives
464 */
store_s24_3le(struct lc3_decoder * decoder,void * _pcm,int stride)465 static void store_s24_3le(
466 struct lc3_decoder *decoder, void *_pcm, int stride)
467 {
468 uint8_t *pcm = _pcm;
469
470 enum lc3_dt dt = decoder->dt;
471 enum lc3_srate sr = decoder->sr_pcm;
472
473 float *xs = decoder->x + decoder->xs_off;
474 int ns = LC3_NS(dt, sr);
475
476 for ( ; ns > 0; ns--, xs++, pcm += 3*stride) {
477 int32_t s = *xs >= 0 ? (int32_t)(ldexpf(*xs, 8) + 0.5f)
478 : (int32_t)(ldexpf(*xs, 8) - 0.5f);
479
480 s = LC3_SAT24(s);
481 pcm[0] = (s >> 0) & 0xff;
482 pcm[1] = (s >> 8) & 0xff;
483 pcm[2] = (s >> 16) & 0xff;
484 }
485 }
486
487 /**
488 * Output PCM Samples to float 32 bits
489 * decoder Decoder state
490 * pcm, stride Output PCM samples, and count between two consecutives
491 */
store_float(struct lc3_decoder * decoder,void * _pcm,int stride)492 static void store_float(
493 struct lc3_decoder *decoder, void *_pcm, int stride)
494 {
495 float *pcm = _pcm;
496
497 enum lc3_dt dt = decoder->dt;
498 enum lc3_srate sr = decoder->sr_pcm;
499
500 float *xs = decoder->x + decoder->xs_off;
501 int ns = LC3_NS(dt, sr);
502
503 for ( ; ns > 0; ns--, xs++, pcm += stride) {
504 float s = ldexpf(*xs, -15);
505 *pcm = fminf(fmaxf(s, -1.f), 1.f);
506 }
507 }
508
509 /**
510 * Decode bitstream
511 * decoder Decoder state
512 * data, nbytes Input bitstream buffer
513 * side Return the side data
514 * return 0: Ok < 0: Bitsream error detected
515 */
decode(struct lc3_decoder * decoder,const void * data,int nbytes,struct side_data * side)516 static int decode(struct lc3_decoder *decoder,
517 const void *data, int nbytes, struct side_data *side)
518 {
519 enum lc3_dt dt = decoder->dt;
520 enum lc3_srate sr = decoder->sr;
521
522 float *xf = decoder->x + decoder->xs_off;
523 int ns = LC3_NS(dt, sr);
524 int ne = LC3_NE(dt, sr);
525
526 lc3_bits_t bits;
527 int ret = 0;
528
529 lc3_setup_bits(&bits, LC3_BITS_MODE_READ, (void *)data, nbytes);
530
531 if ((ret = lc3_bwdet_get_bw(&bits, sr, &side->bw)) < 0)
532 return ret;
533
534 if ((ret = lc3_spec_get_side(&bits, dt, sr, &side->spec)) < 0)
535 return ret;
536
537 lc3_tns_get_data(&bits, dt, side->bw, nbytes, &side->tns);
538
539 side->pitch_present = lc3_get_bit(&bits);
540
541 if ((ret = lc3_sns_get_data(&bits, &side->sns)) < 0)
542 return ret;
543
544 if (side->pitch_present)
545 lc3_ltpf_get_data(&bits, &side->ltpf);
546
547 if ((ret = lc3_spec_decode(&bits, dt, sr,
548 side->bw, nbytes, &side->spec, xf)) < 0)
549 return ret;
550
551 memset(xf + ne, 0, (ns - ne) * sizeof(float));
552
553 return lc3_check_bits(&bits);
554 }
555
556 /**
557 * Frame synthesis
558 * decoder Decoder state
559 * side Frame data, NULL performs PLC
560 * nbytes Size in bytes of the frame
561 */
synthesize(struct lc3_decoder * decoder,const struct side_data * side,int nbytes)562 static void synthesize(struct lc3_decoder *decoder,
563 const struct side_data *side, int nbytes)
564 {
565 enum lc3_dt dt = decoder->dt;
566 enum lc3_srate sr = decoder->sr;
567 enum lc3_srate sr_pcm = decoder->sr_pcm;
568
569 float *xf = decoder->x + decoder->xs_off;
570 int ns = LC3_NS(dt, sr_pcm);
571 int ne = LC3_NE(dt, sr);
572
573 float *xg = decoder->x + decoder->xg_off;
574 float *xs = xf;
575
576 float *xd = decoder->x + decoder->xd_off;
577 float *xh = decoder->x + decoder->xh_off;
578
579 if (side) {
580 enum lc3_bandwidth bw = side->bw;
581
582 lc3_plc_suspend(&decoder->plc);
583
584 lc3_tns_synthesize(dt, bw, &side->tns, xf);
585
586 lc3_sns_synthesize(dt, sr, &side->sns, xf, xg);
587
588 lc3_mdct_inverse(dt, sr_pcm, sr, xg, xd, xs);
589
590 } else {
591 lc3_plc_synthesize(dt, sr, &decoder->plc, xg, xf);
592
593 memset(xf + ne, 0, (ns - ne) * sizeof(float));
594
595 lc3_mdct_inverse(dt, sr_pcm, sr, xf, xd, xs);
596 }
597
598 lc3_ltpf_synthesize(dt, sr_pcm, nbytes, &decoder->ltpf,
599 side && side->pitch_present ? &side->ltpf : NULL, xh, xs);
600 }
601
602 /**
603 * Update decoder state on decoding completion
604 * decoder Decoder state
605 */
complete(struct lc3_decoder * decoder)606 static void complete(struct lc3_decoder *decoder)
607 {
608 enum lc3_dt dt = decoder->dt;
609 enum lc3_srate sr_pcm = decoder->sr_pcm;
610 int nh = LC3_NH(dt, sr_pcm);
611 int ns = LC3_NS(dt, sr_pcm);
612
613 decoder->xs_off = decoder->xs_off - decoder->xh_off < nh - ns ?
614 decoder->xs_off + ns : decoder->xh_off;
615 }
616
617 /**
618 * Return size needed for a decoder
619 */
lc3_decoder_size(int dt_us,int sr_hz)620 unsigned lc3_decoder_size(int dt_us, int sr_hz)
621 {
622 if (resolve_dt(dt_us) >= LC3_NUM_DT ||
623 resolve_sr(sr_hz) >= LC3_NUM_SRATE)
624 return 0;
625
626 return sizeof(struct lc3_decoder) +
627 (LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz)-1) * sizeof(float);
628 }
629
630 /**
631 * Setup decoder
632 */
lc3_setup_decoder(int dt_us,int sr_hz,int sr_pcm_hz,void * mem)633 struct lc3_decoder *lc3_setup_decoder(
634 int dt_us, int sr_hz, int sr_pcm_hz, void *mem)
635 {
636 if (sr_pcm_hz <= 0)
637 sr_pcm_hz = sr_hz;
638
639 enum lc3_dt dt = resolve_dt(dt_us);
640 enum lc3_srate sr = resolve_sr(sr_hz);
641 enum lc3_srate sr_pcm = resolve_sr(sr_pcm_hz);
642
643 if (dt >= LC3_NUM_DT || sr_pcm >= LC3_NUM_SRATE || sr > sr_pcm || !mem)
644 return NULL;
645
646 struct lc3_decoder *decoder = mem;
647 int nh = LC3_NH(dt, sr_pcm);
648 int ns = LC3_NS(dt, sr_pcm);
649 int nd = LC3_ND(dt, sr_pcm);
650
651 *decoder = (struct lc3_decoder){
652 .dt = dt, .sr = sr,
653 .sr_pcm = sr_pcm,
654
655 .xh_off = 0,
656 .xs_off = nh - ns,
657 .xd_off = nh,
658 .xg_off = nh + nd,
659 };
660
661 lc3_plc_reset(&decoder->plc);
662
663 memset(decoder->x, 0,
664 LC3_DECODER_BUFFER_COUNT(dt_us, sr_pcm_hz) * sizeof(float));
665
666 return decoder;
667 }
668
669 /**
670 * Decode a frame
671 */
lc3_decode(struct lc3_decoder * decoder,const void * in,int nbytes,enum lc3_pcm_format fmt,void * pcm,int stride)672 int lc3_decode(struct lc3_decoder *decoder, const void *in, int nbytes,
673 enum lc3_pcm_format fmt, void *pcm, int stride)
674 {
675 static void (* const store[])(struct lc3_decoder *, void *, int) = {
676 [LC3_PCM_FORMAT_S16 ] = store_s16,
677 [LC3_PCM_FORMAT_S24 ] = store_s24,
678 [LC3_PCM_FORMAT_S24_3LE] = store_s24_3le,
679 [LC3_PCM_FORMAT_FLOAT ] = store_float,
680 };
681
682 /* --- Check parameters --- */
683
684 if (!decoder)
685 return -1;
686
687 if (in && (nbytes < LC3_MIN_FRAME_BYTES ||
688 nbytes > LC3_MAX_FRAME_BYTES ))
689 return -1;
690
691 /* --- Processing --- */
692
693 struct side_data side;
694
695 int ret = !in || (decode(decoder, in, nbytes, &side) < 0);
696
697 synthesize(decoder, ret ? NULL : &side, nbytes);
698
699 store[fmt](decoder, pcm, stride);
700
701 complete(decoder);
702
703 return ret;
704 }
705