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  * hrmode          High-resolution mode indication
57  * return          Frame duration identifier, or LC3_NUM_DT
58  */
resolve_dt(int us,bool hrmode)59 static enum lc3_dt resolve_dt(int us, bool hrmode)
60 {
61     return LC3_PLUS && us ==  2500 ? LC3_DT_2M5 :
62            LC3_PLUS && us ==  5000 ? LC3_DT_5M  :
63            !hrmode  && us ==  7500 ? LC3_DT_7M5 :
64                        us == 10000 ? LC3_DT_10M : LC3_NUM_DT;
65 }
66 
67 /**
68  * Resolve samplerate in Hz
69  * hz              Samplerate in Hz
70  * hrmode          High-resolution mode indication
71  * return          Sample rate identifier, or LC3_NUM_SRATE
72  */
resolve_srate(int hz,bool hrmode)73 static enum lc3_srate resolve_srate(int hz, bool hrmode)
74 {
75     hrmode = LC3_PLUS_HR && hrmode;
76 
77     return !hrmode && hz ==  8000 ? LC3_SRATE_8K     :
78            !hrmode && hz == 16000 ? LC3_SRATE_16K    :
79            !hrmode && hz == 24000 ? LC3_SRATE_24K    :
80            !hrmode && hz == 32000 ? LC3_SRATE_32K    :
81            !hrmode && hz == 48000 ? LC3_SRATE_48K    :
82             hrmode && hz == 48000 ? LC3_SRATE_48K_HR :
83             hrmode && hz == 96000 ? LC3_SRATE_96K_HR : LC3_NUM_SRATE;
84 }
85 
86 /**
87  * Return the number of PCM samples in a frame
88  */
lc3_hr_frame_samples(bool hrmode,int dt_us,int sr_hz)89 LC3_EXPORT int lc3_hr_frame_samples(bool hrmode, int dt_us, int sr_hz)
90 {
91     enum lc3_dt dt = resolve_dt(dt_us, hrmode);
92     enum lc3_srate sr = resolve_srate(sr_hz, hrmode);
93 
94     if (dt >= LC3_NUM_DT || sr >= LC3_NUM_SRATE)
95         return -1;
96 
97     return lc3_ns(dt, sr);
98 }
99 
lc3_frame_samples(int dt_us,int sr_hz)100 LC3_EXPORT int lc3_frame_samples(int dt_us, int sr_hz)
101 {
102     return lc3_hr_frame_samples(false, dt_us, sr_hz);
103 }
104 
105 /**
106  * Return the size of frames or frame blocks, from bitrate
107  */
lc3_hr_frame_block_bytes(bool hrmode,int dt_us,int sr_hz,int nchannels,int bitrate)108 LC3_EXPORT int lc3_hr_frame_block_bytes(
109     bool hrmode, int dt_us, int sr_hz, int nchannels, int bitrate)
110 {
111     enum lc3_dt dt = resolve_dt(dt_us, hrmode);
112     enum lc3_srate sr = resolve_srate(sr_hz, hrmode);
113 
114     if (dt >= LC3_NUM_DT || sr >= LC3_NUM_SRATE
115             || nchannels < 1 || nchannels > 8 || bitrate < 0)
116         return -1;
117 
118     bitrate = LC3_CLIP(bitrate, 0, 8*LC3_HR_MAX_BITRATE);
119 
120     return LC3_CLIP((bitrate * (int)(1 + dt)) / 3200,
121         nchannels * lc3_min_frame_bytes(dt, sr),
122         nchannels * lc3_max_frame_bytes(dt, sr) );
123 }
124 
lc3_frame_bock_bytes(int dt_us,int nchannels,int bitrate)125 LC3_EXPORT int lc3_frame_bock_bytes(int dt_us, int nchannels, int bitrate)
126 {
127     return lc3_hr_frame_block_bytes(false, dt_us, 8000, nchannels, bitrate);
128 }
129 
lc3_hr_frame_bytes(bool hrmode,int dt_us,int sr_hz,int bitrate)130 LC3_EXPORT int lc3_hr_frame_bytes(
131     bool hrmode, int dt_us, int sr_hz, int bitrate)
132 {
133     return lc3_hr_frame_block_bytes(hrmode, dt_us, sr_hz, 1, bitrate);
134 }
135 
lc3_frame_bytes(int dt_us,int bitrate)136 LC3_EXPORT int lc3_frame_bytes(int dt_us, int bitrate)
137 {
138     return lc3_hr_frame_bytes(false, dt_us, 8000, bitrate);
139 }
140 
141 /**
142  * Resolve the bitrate, from the size of frames or frame blocks
143  */
lc3_hr_resolve_bitrate(bool hrmode,int dt_us,int sr_hz,int nbytes)144 LC3_EXPORT int lc3_hr_resolve_bitrate(
145     bool hrmode, int dt_us, int sr_hz, int nbytes)
146 {
147     enum lc3_dt dt = resolve_dt(dt_us, hrmode);
148     enum lc3_srate sr = resolve_srate(sr_hz, hrmode);
149 
150     if (dt >= LC3_NUM_DT || sr >= LC3_NUM_SRATE || nbytes < 0)
151         return -1;
152 
153     return LC3_MIN(((int64_t)nbytes * 3200 + dt) / (1 + dt), INT_MAX);
154 }
155 
lc3_resolve_bitrate(int dt_us,int nbytes)156 LC3_EXPORT int lc3_resolve_bitrate(int dt_us, int nbytes)
157 {
158     return lc3_hr_resolve_bitrate(false, dt_us, 8000, nbytes);
159 }
160 
161 /**
162  * Return algorithmic delay, as a number of samples
163  */
lc3_hr_delay_samples(bool hrmode,int dt_us,int sr_hz)164 LC3_EXPORT int lc3_hr_delay_samples(bool hrmode, int dt_us, int sr_hz)
165 {
166     enum lc3_dt dt = resolve_dt(dt_us, hrmode);
167     enum lc3_srate sr = resolve_srate(sr_hz, hrmode);
168 
169     if (dt >= LC3_NUM_DT || sr >= LC3_NUM_SRATE)
170         return -1;
171 
172     return 2 * lc3_nd(dt, sr) - lc3_ns(dt, sr);
173 }
174 
lc3_delay_samples(int dt_us,int sr_hz)175 LC3_EXPORT int lc3_delay_samples(int dt_us, int sr_hz)
176 {
177     return lc3_hr_delay_samples(false, dt_us, sr_hz);
178 }
179 
180 
181 /* ----------------------------------------------------------------------------
182  *  Encoder
183  * -------------------------------------------------------------------------- */
184 
185 /**
186  * Input PCM Samples from signed 16 bits
187  * encoder         Encoder state
188  * pcm, stride     Input PCM samples, and count between two consecutives
189  */
load_s16(struct lc3_encoder * encoder,const void * _pcm,int stride)190 static void load_s16(
191     struct lc3_encoder *encoder, const void *_pcm, int stride)
192 {
193     const int16_t *pcm = _pcm;
194 
195     enum lc3_dt dt = encoder->dt;
196     enum lc3_srate sr = encoder->sr_pcm;
197 
198     int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
199     float *xs = encoder->x + encoder->xs_off;
200     int ns = lc3_ns(dt, sr);
201 
202     for (int i = 0; i < ns; i++, pcm += stride)
203         xt[i] = *pcm, xs[i] = *pcm;
204 }
205 
206 /**
207  * Input PCM Samples from signed 24 bits
208  * encoder         Encoder state
209  * pcm, stride     Input PCM samples, and count between two consecutives
210  */
load_s24(struct lc3_encoder * encoder,const void * _pcm,int stride)211 static void load_s24(
212     struct lc3_encoder *encoder, const void *_pcm, int stride)
213 {
214     const int32_t *pcm = _pcm;
215 
216     enum lc3_dt dt = encoder->dt;
217     enum lc3_srate sr = encoder->sr_pcm;
218 
219     int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
220     float *xs = encoder->x + encoder->xs_off;
221     int ns = lc3_ns(dt, sr);
222 
223     for (int i = 0; i < ns; i++, pcm += stride) {
224         xt[i] = *pcm >> 8;
225         xs[i] = lc3_ldexpf(*pcm, -8);
226     }
227 }
228 
229 /**
230  * Input PCM Samples from signed 24 bits packed
231  * encoder         Encoder state
232  * pcm, stride     Input PCM samples, and count between two consecutives
233  */
load_s24_3le(struct lc3_encoder * encoder,const void * _pcm,int stride)234 static void load_s24_3le(
235     struct lc3_encoder *encoder, const void *_pcm, int stride)
236 {
237     const uint8_t *pcm = _pcm;
238 
239     enum lc3_dt dt = encoder->dt;
240     enum lc3_srate sr = encoder->sr_pcm;
241 
242     int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
243     float *xs = encoder->x + encoder->xs_off;
244     int ns = lc3_ns(dt, sr);
245 
246     for (int i = 0; i < ns; i++, pcm += 3*stride) {
247         int32_t in = ((uint32_t)pcm[0] <<  8) |
248                      ((uint32_t)pcm[1] << 16) |
249                      ((uint32_t)pcm[2] << 24)  ;
250 
251         xt[i] = in >> 16;
252         xs[i] = lc3_ldexpf(in, -16);
253     }
254 }
255 
256 /**
257  * Input PCM Samples from float 32 bits
258  * encoder         Encoder state
259  * pcm, stride     Input PCM samples, and count between two consecutives
260  */
load_float(struct lc3_encoder * encoder,const void * _pcm,int stride)261 static void load_float(
262     struct lc3_encoder *encoder, const void *_pcm, int stride)
263 {
264     const float *pcm = _pcm;
265 
266     enum lc3_dt dt = encoder->dt;
267     enum lc3_srate sr = encoder->sr_pcm;
268 
269     int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
270     float *xs = encoder->x + encoder->xs_off;
271     int ns = lc3_ns(dt, sr);
272 
273     for (int i = 0; i < ns; i++, pcm += stride) {
274         xs[i] = lc3_ldexpf(*pcm, 15);
275         xt[i] = LC3_SAT16((int32_t)xs[i]);
276     }
277 }
278 
279 /**
280  * Frame Analysis
281  * encoder         Encoder state
282  * nbytes          Size in bytes of the frame
283  * side            Return frame data
284  */
analyze(struct lc3_encoder * encoder,int nbytes,struct side_data * side)285 static void analyze(struct lc3_encoder *encoder,
286     int nbytes, struct side_data *side)
287 {
288     enum lc3_dt dt = encoder->dt;
289     enum lc3_srate sr = encoder->sr;
290     enum lc3_srate sr_pcm = encoder->sr_pcm;
291 
292     int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
293     float *xs = encoder->x + encoder->xs_off;
294     int ns = lc3_ns(dt, sr_pcm);
295     int nt = lc3_nt(sr_pcm);
296 
297     float *xd = encoder->x + encoder->xd_off;
298     float *xf = xs;
299 
300     /* --- Temporal --- */
301 
302     bool att = lc3_attdet_run(dt, sr_pcm, nbytes, &encoder->attdet, xt);
303 
304     side->pitch_present =
305         lc3_ltpf_analyse(dt, sr_pcm, &encoder->ltpf, xt, &side->ltpf);
306 
307     memmove(xt - nt, xt + (ns-nt), nt * sizeof(*xt));
308 
309     /* --- Spectral --- */
310 
311     float e[LC3_MAX_BANDS];
312 
313     lc3_mdct_forward(dt, sr_pcm, sr, xs, xd, xf);
314 
315     bool nn_flag = lc3_energy_compute(dt, sr, xf, e);
316     if (nn_flag)
317         lc3_ltpf_disable(&side->ltpf);
318 
319     side->bw = lc3_bwdet_run(dt, sr, e);
320 
321     lc3_sns_analyze(dt, sr, nbytes, e, att, &side->sns, xf, xf);
322 
323     lc3_tns_analyze(dt, side->bw, nn_flag, nbytes, &side->tns, xf);
324 
325     lc3_spec_analyze(dt, sr,
326         nbytes, side->pitch_present, &side->tns,
327         &encoder->spec, xf, &side->spec);
328 }
329 
330 /**
331  * Encode bitstream
332  * encoder         Encoder state
333  * side            The frame data
334  * nbytes          Target size of the frame (20 to 400)
335  * buffer          Output bitstream buffer of `nbytes` size
336  */
encode(struct lc3_encoder * encoder,const struct side_data * side,int nbytes,void * buffer)337 static void encode(struct lc3_encoder *encoder,
338     const struct side_data *side, int nbytes, void *buffer)
339 {
340     enum lc3_dt dt = encoder->dt;
341     enum lc3_srate sr = encoder->sr;
342 
343     float *xf = encoder->x + encoder->xs_off;
344     enum lc3_bandwidth bw = side->bw;
345 
346     lc3_bits_t bits;
347 
348     lc3_setup_bits(&bits, LC3_BITS_MODE_WRITE, buffer, nbytes);
349 
350     lc3_bwdet_put_bw(&bits, sr, bw);
351 
352     lc3_spec_put_side(&bits, dt, sr, &side->spec);
353 
354     lc3_tns_put_data(&bits, &side->tns);
355 
356     lc3_put_bit(&bits, side->pitch_present);
357 
358     lc3_sns_put_data(&bits, &side->sns);
359 
360     if (side->pitch_present)
361         lc3_ltpf_put_data(&bits, &side->ltpf);
362 
363     lc3_spec_encode(&bits, dt, sr, bw, nbytes, &side->spec, xf);
364 
365     lc3_flush_bits(&bits);
366 }
367 
368 /**
369  * Return size needed for an encoder
370  */
lc3_hr_encoder_size(bool hrmode,int dt_us,int sr_hz)371 LC3_EXPORT unsigned lc3_hr_encoder_size(bool hrmode, int dt_us, int sr_hz)
372 {
373     if (resolve_dt(dt_us, hrmode) >= LC3_NUM_DT ||
374         resolve_srate(sr_hz, hrmode) >= LC3_NUM_SRATE)
375         return 0;
376 
377     return sizeof(struct lc3_encoder) +
378         (LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz)-1) * sizeof(float);
379 }
380 
lc3_encoder_size(int dt_us,int sr_hz)381 LC3_EXPORT unsigned lc3_encoder_size(int dt_us, int sr_hz)
382 {
383     return lc3_hr_encoder_size(false, dt_us, sr_hz);
384 }
385 
386 /**
387  * Setup encoder
388  */
lc3_hr_setup_encoder(bool hrmode,int dt_us,int sr_hz,int sr_pcm_hz,void * mem)389 LC3_EXPORT struct lc3_encoder *lc3_hr_setup_encoder(
390     bool hrmode, int dt_us, int sr_hz, int sr_pcm_hz, void *mem)
391 {
392     if (sr_pcm_hz <= 0)
393         sr_pcm_hz = sr_hz;
394 
395     enum lc3_dt dt = resolve_dt(dt_us, hrmode);
396     enum lc3_srate sr = resolve_srate(sr_hz, hrmode);
397     enum lc3_srate sr_pcm = resolve_srate(sr_pcm_hz, hrmode);
398 
399     if (dt >= LC3_NUM_DT || sr_pcm >= LC3_NUM_SRATE || sr > sr_pcm || !mem)
400         return NULL;
401 
402     struct lc3_encoder *encoder = mem;
403     int ns = lc3_ns(dt, sr_pcm);
404     int nt = lc3_nt(sr_pcm);
405 
406     *encoder = (struct lc3_encoder){
407         .dt = dt, .sr = sr,
408         .sr_pcm = sr_pcm,
409 
410         .xt_off = nt,
411         .xs_off = (nt + ns) / 2,
412         .xd_off = (nt + ns) / 2 + ns,
413     };
414 
415     memset(encoder->x, 0,
416         LC3_ENCODER_BUFFER_COUNT(dt_us, sr_pcm_hz) * sizeof(float));
417 
418     return encoder;
419 }
420 
lc3_setup_encoder(int dt_us,int sr_hz,int sr_pcm_hz,void * mem)421 LC3_EXPORT struct lc3_encoder *lc3_setup_encoder(
422     int dt_us, int sr_hz, int sr_pcm_hz, void *mem)
423 {
424     return lc3_hr_setup_encoder(false, dt_us, sr_hz, sr_pcm_hz, mem);
425 }
426 
427 /**
428  * Encode a frame
429  */
lc3_encode(struct lc3_encoder * encoder,enum lc3_pcm_format fmt,const void * pcm,int stride,int nbytes,void * out)430 LC3_EXPORT int lc3_encode(struct lc3_encoder *encoder,
431     enum lc3_pcm_format fmt, const void *pcm, int stride, int nbytes, void *out)
432 {
433     static void (* const load[])(struct lc3_encoder *, const void *, int) = {
434         [LC3_PCM_FORMAT_S16    ] = load_s16,
435         [LC3_PCM_FORMAT_S24    ] = load_s24,
436         [LC3_PCM_FORMAT_S24_3LE] = load_s24_3le,
437         [LC3_PCM_FORMAT_FLOAT  ] = load_float,
438     };
439 
440     /* --- Check parameters --- */
441 
442     if (!encoder || nbytes < lc3_min_frame_bytes(encoder->dt, encoder->sr)
443                  || nbytes > lc3_max_frame_bytes(encoder->dt, encoder->sr))
444         return -1;
445 
446     /* --- Processing --- */
447 
448     struct side_data side;
449 
450     load[fmt](encoder, pcm, stride);
451 
452     analyze(encoder, nbytes, &side);
453 
454     encode(encoder, &side, nbytes, out);
455 
456     return 0;
457 }
458 
459 
460 /* ----------------------------------------------------------------------------
461  *  Decoder
462  * -------------------------------------------------------------------------- */
463 
464 /**
465  * Output PCM Samples to signed 16 bits
466  * decoder         Decoder state
467  * pcm, stride     Output PCM samples, and count between two consecutives
468  */
store_s16(struct lc3_decoder * decoder,void * _pcm,int stride)469 static void store_s16(
470     struct lc3_decoder *decoder, void *_pcm, int stride)
471 {
472     int16_t *pcm = _pcm;
473 
474     enum lc3_dt dt = decoder->dt;
475     enum lc3_srate sr = decoder->sr_pcm;
476 
477     float *xs = decoder->x + decoder->xs_off;
478     int ns = lc3_ns(dt, sr);
479 
480     for ( ; ns > 0; ns--, xs++, pcm += stride) {
481         int32_t s = *xs >= 0 ? (int)(*xs + 0.5f) : (int)(*xs - 0.5f);
482         *pcm = LC3_SAT16(s);
483     }
484 }
485 
486 /**
487  * Output PCM Samples to signed 24 bits
488  * decoder         Decoder state
489  * pcm, stride     Output PCM samples, and count between two consecutives
490  */
store_s24(struct lc3_decoder * decoder,void * _pcm,int stride)491 static void store_s24(
492     struct lc3_decoder *decoder, void *_pcm, int stride)
493 {
494     int32_t *pcm = _pcm;
495 
496     enum lc3_dt dt = decoder->dt;
497     enum lc3_srate sr = decoder->sr_pcm;
498 
499     float *xs = decoder->x + decoder->xs_off;
500     int ns = lc3_ns(dt, sr);
501 
502     for ( ; ns > 0; ns--, xs++, pcm += stride) {
503         int32_t s = *xs >= 0 ? (int32_t)(lc3_ldexpf(*xs, 8) + 0.5f)
504                              : (int32_t)(lc3_ldexpf(*xs, 8) - 0.5f);
505         *pcm = LC3_SAT24(s);
506     }
507 }
508 
509 /**
510  * Output PCM Samples to signed 24 bits packed
511  * decoder         Decoder state
512  * pcm, stride     Output PCM samples, and count between two consecutives
513  */
store_s24_3le(struct lc3_decoder * decoder,void * _pcm,int stride)514 static void store_s24_3le(
515     struct lc3_decoder *decoder, void *_pcm, int stride)
516 {
517     uint8_t *pcm = _pcm;
518 
519     enum lc3_dt dt = decoder->dt;
520     enum lc3_srate sr = decoder->sr_pcm;
521 
522     float *xs = decoder->x + decoder->xs_off;
523     int ns = lc3_ns(dt, sr);
524 
525     for ( ; ns > 0; ns--, xs++, pcm += 3*stride) {
526         int32_t s = *xs >= 0 ? (int32_t)(lc3_ldexpf(*xs, 8) + 0.5f)
527                              : (int32_t)(lc3_ldexpf(*xs, 8) - 0.5f);
528 
529         s = LC3_SAT24(s);
530         pcm[0] = (s >>  0) & 0xff;
531         pcm[1] = (s >>  8) & 0xff;
532         pcm[2] = (s >> 16) & 0xff;
533     }
534 }
535 
536 /**
537  * Output PCM Samples to float 32 bits
538  * decoder         Decoder state
539  * pcm, stride     Output PCM samples, and count between two consecutives
540  */
store_float(struct lc3_decoder * decoder,void * _pcm,int stride)541 static void store_float(
542     struct lc3_decoder *decoder, void *_pcm, int stride)
543 {
544     float *pcm = _pcm;
545 
546     enum lc3_dt dt = decoder->dt;
547     enum lc3_srate sr = decoder->sr_pcm;
548 
549     float *xs = decoder->x + decoder->xs_off;
550     int ns = lc3_ns(dt, sr);
551 
552     for ( ; ns > 0; ns--, xs++, pcm += stride) {
553         float s = lc3_ldexpf(*xs, -15);
554         *pcm = fminf(fmaxf(s, -1.f), 1.f);
555     }
556 }
557 
558 /**
559  * Decode bitstream
560  * decoder         Decoder state
561  * data, nbytes    Input bitstream buffer
562  * side            Return the side data
563  * return          0: Ok  < 0: Bitsream error detected
564  */
decode(struct lc3_decoder * decoder,const void * data,int nbytes,struct side_data * side)565 static int decode(struct lc3_decoder *decoder,
566     const void *data, int nbytes, struct side_data *side)
567 {
568     enum lc3_dt dt = decoder->dt;
569     enum lc3_srate sr = decoder->sr;
570 
571     float *xf = decoder->x + decoder->xs_off;
572     int ns = lc3_ns(dt, sr);
573     int ne = lc3_ne(dt, sr);
574 
575     lc3_bits_t bits;
576     int ret = 0;
577 
578     lc3_setup_bits(&bits, LC3_BITS_MODE_READ, (void *)data, nbytes);
579 
580     if ((ret = lc3_bwdet_get_bw(&bits, sr, &side->bw)) < 0)
581         return ret;
582 
583     if ((ret = lc3_spec_get_side(&bits, dt, sr, &side->spec)) < 0)
584         return ret;
585 
586     if ((ret = lc3_tns_get_data(&bits, dt, side->bw, nbytes, &side->tns)) < 0)
587         return ret;
588 
589     side->pitch_present = lc3_get_bit(&bits);
590 
591     if ((ret = lc3_sns_get_data(&bits, &side->sns)) < 0)
592         return ret;
593 
594     if (side->pitch_present)
595       lc3_ltpf_get_data(&bits, &side->ltpf);
596 
597     if ((ret = lc3_spec_decode(&bits, dt, sr,
598                     side->bw, nbytes, &side->spec, xf)) < 0)
599         return ret;
600 
601     memset(xf + ne, 0, (ns - ne) * sizeof(float));
602 
603     return lc3_check_bits(&bits);
604 }
605 
606 /**
607  * Frame synthesis
608  * decoder         Decoder state
609  * side            Frame data, NULL performs PLC
610  * nbytes          Size in bytes of the frame
611  */
synthesize(struct lc3_decoder * decoder,const struct side_data * side,int nbytes)612 static void synthesize(struct lc3_decoder *decoder,
613     const struct side_data *side, int nbytes)
614 {
615     enum lc3_dt dt = decoder->dt;
616     enum lc3_srate sr = decoder->sr;
617     enum lc3_srate sr_pcm = decoder->sr_pcm;
618 
619     float *xf = decoder->x + decoder->xs_off;
620     int ns = lc3_ns(dt, sr_pcm);
621     int ne = lc3_ne(dt, sr);
622 
623     float *xg = decoder->x + decoder->xg_off;
624     float *xs = xf;
625 
626     float *xd = decoder->x + decoder->xd_off;
627     float *xh = decoder->x + decoder->xh_off;
628 
629     if (side) {
630         enum lc3_bandwidth bw = side->bw;
631 
632         lc3_plc_suspend(&decoder->plc);
633 
634         lc3_tns_synthesize(dt, bw, &side->tns, xf);
635 
636         lc3_sns_synthesize(dt, sr, &side->sns, xf, xg);
637 
638         lc3_mdct_inverse(dt, sr_pcm, sr, xg, xd, xs);
639 
640     } else {
641         lc3_plc_synthesize(dt, sr, &decoder->plc, xg, xf);
642 
643         memset(xf + ne, 0, (ns - ne) * sizeof(float));
644 
645         lc3_mdct_inverse(dt, sr_pcm, sr, xf, xd, xs);
646     }
647 
648     if (!lc3_hr(sr))
649         lc3_ltpf_synthesize(dt, sr_pcm, nbytes, &decoder->ltpf,
650             side && side->pitch_present ? &side->ltpf : NULL, xh, xs);
651 }
652 
653 /**
654  * Update decoder state on decoding completion
655  * decoder         Decoder state
656  */
complete(struct lc3_decoder * decoder)657 static void complete(struct lc3_decoder *decoder)
658 {
659     enum lc3_dt dt = decoder->dt;
660     enum lc3_srate sr_pcm = decoder->sr_pcm;
661     int nh = lc3_nh(dt, sr_pcm);
662     int ns = lc3_ns(dt, sr_pcm);
663 
664     decoder->xs_off = decoder->xs_off - decoder->xh_off < nh ?
665         decoder->xs_off + ns : decoder->xh_off;
666 }
667 
668 /**
669  * Return size needed for a decoder
670  */
lc3_hr_decoder_size(bool hrmode,int dt_us,int sr_hz)671 LC3_EXPORT unsigned lc3_hr_decoder_size(bool hrmode, int dt_us, int sr_hz)
672 {
673     if (resolve_dt(dt_us, hrmode) >= LC3_NUM_DT ||
674         resolve_srate(sr_hz, hrmode) >= LC3_NUM_SRATE)
675         return 0;
676 
677     return sizeof(struct lc3_decoder) +
678         (LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz)-1) * sizeof(float);
679 }
680 
lc3_decoder_size(int dt_us,int sr_hz)681 LC3_EXPORT unsigned lc3_decoder_size(int dt_us, int sr_hz)
682 {
683     return lc3_hr_decoder_size(false, dt_us, sr_hz);
684 }
685 
686 /**
687  * Setup decoder
688  */
lc3_hr_setup_decoder(bool hrmode,int dt_us,int sr_hz,int sr_pcm_hz,void * mem)689 LC3_EXPORT struct lc3_decoder *lc3_hr_setup_decoder(
690     bool hrmode, int dt_us, int sr_hz, int sr_pcm_hz, void *mem)
691 {
692     if (sr_pcm_hz <= 0)
693         sr_pcm_hz = sr_hz;
694 
695     enum lc3_dt dt = resolve_dt(dt_us, hrmode);
696     enum lc3_srate sr = resolve_srate(sr_hz, hrmode);
697     enum lc3_srate sr_pcm = resolve_srate(sr_pcm_hz, hrmode);
698 
699     if (dt >= LC3_NUM_DT || sr_pcm >= LC3_NUM_SRATE || sr > sr_pcm || !mem)
700         return NULL;
701 
702     struct lc3_decoder *decoder = mem;
703     int nh = lc3_nh(dt, sr_pcm);
704     int ns = lc3_ns(dt, sr_pcm);
705     int nd = lc3_nd(dt, sr_pcm);
706 
707     *decoder = (struct lc3_decoder){
708         .dt = dt, .sr = sr,
709         .sr_pcm = sr_pcm,
710 
711         .xh_off = 0,
712         .xs_off = nh,
713         .xd_off = nh + ns,
714         .xg_off = nh + ns + nd,
715     };
716 
717     lc3_plc_reset(&decoder->plc);
718 
719     memset(decoder->x, 0,
720         LC3_DECODER_BUFFER_COUNT(dt_us, sr_pcm_hz) * sizeof(float));
721 
722     return decoder;
723 }
724 
lc3_setup_decoder(int dt_us,int sr_hz,int sr_pcm_hz,void * mem)725 LC3_EXPORT struct lc3_decoder *lc3_setup_decoder(
726     int dt_us, int sr_hz, int sr_pcm_hz, void *mem)
727 {
728     return lc3_hr_setup_decoder(false, dt_us, sr_hz, sr_pcm_hz, mem);
729 }
730 
731 /**
732  * Decode a frame
733  */
lc3_decode(struct lc3_decoder * decoder,const void * in,int nbytes,enum lc3_pcm_format fmt,void * pcm,int stride)734 LC3_EXPORT int lc3_decode(struct lc3_decoder *decoder,
735     const void *in, int nbytes, enum lc3_pcm_format fmt, void *pcm, int stride)
736 {
737     static void (* const store[])(struct lc3_decoder *, void *, int) = {
738         [LC3_PCM_FORMAT_S16    ] = store_s16,
739         [LC3_PCM_FORMAT_S24    ] = store_s24,
740         [LC3_PCM_FORMAT_S24_3LE] = store_s24_3le,
741         [LC3_PCM_FORMAT_FLOAT  ] = store_float,
742     };
743 
744     /* --- Check parameters --- */
745 
746     if (!decoder)
747         return -1;
748 
749     if (in && (nbytes < LC3_MIN_FRAME_BYTES ||
750                nbytes > lc3_max_frame_bytes(decoder->dt, decoder->sr) ))
751         return -1;
752 
753     /* --- Processing --- */
754 
755     struct side_data side;
756 
757     int ret = !in || (decode(decoder, in, nbytes, &side) < 0);
758 
759     synthesize(decoder, ret ? NULL : &side, nbytes);
760 
761     store[fmt](decoder, pcm, stride);
762 
763     complete(decoder);
764 
765     return ret;
766 }
767