xref: /btstack/3rd-party/lc3-google/include/lc3_private.h (revision 4c4eb519208b4224604d94b3ed1931841ddd93bb)
19a19cd78SMatthias Ringwald /******************************************************************************
29a19cd78SMatthias Ringwald  *
34930cef6SMatthias Ringwald  *  Copyright 2022 Google LLC
49a19cd78SMatthias Ringwald  *
59a19cd78SMatthias Ringwald  *  Licensed under the Apache License, Version 2.0 (the "License");
69a19cd78SMatthias Ringwald  *  you may not use this file except in compliance with the License.
79a19cd78SMatthias Ringwald  *  You may obtain a copy of the License at:
89a19cd78SMatthias Ringwald  *
99a19cd78SMatthias Ringwald  *  http://www.apache.org/licenses/LICENSE-2.0
109a19cd78SMatthias Ringwald  *
119a19cd78SMatthias Ringwald  *  Unless required by applicable law or agreed to in writing, software
129a19cd78SMatthias Ringwald  *  distributed under the License is distributed on an "AS IS" BASIS,
139a19cd78SMatthias Ringwald  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
149a19cd78SMatthias Ringwald  *  See the License for the specific language governing permissions and
159a19cd78SMatthias Ringwald  *  limitations under the License.
169a19cd78SMatthias Ringwald  *
179a19cd78SMatthias Ringwald  ******************************************************************************/
189a19cd78SMatthias Ringwald 
199a19cd78SMatthias Ringwald #ifndef __LC3_PRIVATE_H
209a19cd78SMatthias Ringwald #define __LC3_PRIVATE_H
219a19cd78SMatthias Ringwald 
229a19cd78SMatthias Ringwald #include <stdint.h>
239a19cd78SMatthias Ringwald #include <stdbool.h>
249a19cd78SMatthias Ringwald 
259a19cd78SMatthias Ringwald 
269a19cd78SMatthias Ringwald /**
279a19cd78SMatthias Ringwald  * Return number of samples, delayed samples and
289a19cd78SMatthias Ringwald  * encoded spectrum coefficients within a frame
294930cef6SMatthias Ringwald  * - For encoding, keep 1.25 ms of temporal winodw
304930cef6SMatthias Ringwald  * - For decoding, keep 18 ms of history, aligned on frames, and a frame
319a19cd78SMatthias Ringwald  */
329a19cd78SMatthias Ringwald 
339a19cd78SMatthias Ringwald #define __LC3_NS(dt_us, sr_hz) \
349a19cd78SMatthias Ringwald     ( (dt_us * sr_hz) / 1000 / 1000 )
359a19cd78SMatthias Ringwald 
369a19cd78SMatthias Ringwald #define __LC3_ND(dt_us, sr_hz) \
379a19cd78SMatthias Ringwald     ( (dt_us) == 7500 ? 23 * __LC3_NS(dt_us, sr_hz) / 30 \
389a19cd78SMatthias Ringwald                       :  5 * __LC3_NS(dt_us, sr_hz) /  8 )
399a19cd78SMatthias Ringwald 
404930cef6SMatthias Ringwald #define __LC3_NT(sr_hz) \
414930cef6SMatthias Ringwald     ( (5 * sr_hz) / 4000 )
424930cef6SMatthias Ringwald 
434930cef6SMatthias Ringwald #define __LC3_NH(dt_us, sr_hz) \
444930cef6SMatthias Ringwald     ( ((3 - ((dt_us) >= 10000)) + 1) * __LC3_NS(dt_us, sr_hz) )
459a19cd78SMatthias Ringwald 
469a19cd78SMatthias Ringwald 
479a19cd78SMatthias Ringwald /**
489a19cd78SMatthias Ringwald  * Frame duration 7.5ms or 10ms
499a19cd78SMatthias Ringwald  */
509a19cd78SMatthias Ringwald 
519a19cd78SMatthias Ringwald enum lc3_dt {
529a19cd78SMatthias Ringwald     LC3_DT_7M5,
539a19cd78SMatthias Ringwald     LC3_DT_10M,
549a19cd78SMatthias Ringwald 
559a19cd78SMatthias Ringwald     LC3_NUM_DT
569a19cd78SMatthias Ringwald };
579a19cd78SMatthias Ringwald 
589a19cd78SMatthias Ringwald /**
599a19cd78SMatthias Ringwald  * Sampling frequency
609a19cd78SMatthias Ringwald  */
619a19cd78SMatthias Ringwald 
629a19cd78SMatthias Ringwald enum lc3_srate {
639a19cd78SMatthias Ringwald     LC3_SRATE_8K,
649a19cd78SMatthias Ringwald     LC3_SRATE_16K,
659a19cd78SMatthias Ringwald     LC3_SRATE_24K,
669a19cd78SMatthias Ringwald     LC3_SRATE_32K,
679a19cd78SMatthias Ringwald     LC3_SRATE_48K,
689a19cd78SMatthias Ringwald 
699a19cd78SMatthias Ringwald     LC3_NUM_SRATE,
709a19cd78SMatthias Ringwald };
719a19cd78SMatthias Ringwald 
729a19cd78SMatthias Ringwald 
739a19cd78SMatthias Ringwald /**
749a19cd78SMatthias Ringwald  * Encoder state and memory
759a19cd78SMatthias Ringwald  */
769a19cd78SMatthias Ringwald 
779a19cd78SMatthias Ringwald typedef struct lc3_attdet_analysis {
784930cef6SMatthias Ringwald     int32_t en1, an1;
799a19cd78SMatthias Ringwald     int p_att;
809a19cd78SMatthias Ringwald } lc3_attdet_analysis_t;
819a19cd78SMatthias Ringwald 
829a19cd78SMatthias Ringwald struct lc3_ltpf_hp50_state {
834930cef6SMatthias Ringwald     int64_t s1, s2;
849a19cd78SMatthias Ringwald };
859a19cd78SMatthias Ringwald 
869a19cd78SMatthias Ringwald typedef struct lc3_ltpf_analysis {
879a19cd78SMatthias Ringwald     bool active;
889a19cd78SMatthias Ringwald     int pitch;
899a19cd78SMatthias Ringwald     float nc[2];
909a19cd78SMatthias Ringwald 
919a19cd78SMatthias Ringwald     struct lc3_ltpf_hp50_state hp50;
924930cef6SMatthias Ringwald     int16_t x_12k8[384];
934930cef6SMatthias Ringwald     int16_t x_6k4[178];
949a19cd78SMatthias Ringwald     int tc;
959a19cd78SMatthias Ringwald } lc3_ltpf_analysis_t;
969a19cd78SMatthias Ringwald 
979a19cd78SMatthias Ringwald typedef struct lc3_spec_analysis {
989a19cd78SMatthias Ringwald     float nbits_off;
999a19cd78SMatthias Ringwald     int nbits_spare;
1009a19cd78SMatthias Ringwald } lc3_spec_analysis_t;
1019a19cd78SMatthias Ringwald 
1029a19cd78SMatthias Ringwald struct lc3_encoder {
103*4c4eb519SMatthias Ringwald     enum lc3_dt dt;
104*4c4eb519SMatthias Ringwald     enum lc3_srate sr, sr_pcm;
105*4c4eb519SMatthias Ringwald 
106*4c4eb519SMatthias Ringwald     lc3_attdet_analysis_t attdet;
107*4c4eb519SMatthias Ringwald     lc3_ltpf_analysis_t ltpf;
108*4c4eb519SMatthias Ringwald     lc3_spec_analysis_t spec;
109*4c4eb519SMatthias Ringwald 
110*4c4eb519SMatthias Ringwald     int16_t *xt;
111*4c4eb519SMatthias Ringwald     float *xs, *xd, s[1];
1129a19cd78SMatthias Ringwald };
1139a19cd78SMatthias Ringwald 
1149a19cd78SMatthias Ringwald #define LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz) \
1154930cef6SMatthias Ringwald     ( ( __LC3_NS(dt_us, sr_hz) + __LC3_NT(sr_hz) ) / 2 + \
1164930cef6SMatthias Ringwald         __LC3_NS(dt_us, sr_hz) + __LC3_ND(dt_us, sr_hz) )
1179a19cd78SMatthias Ringwald 
1189a19cd78SMatthias Ringwald #define LC3_ENCODER_MEM_T(dt_us, sr_hz) \
1199a19cd78SMatthias Ringwald     struct { \
120*4c4eb519SMatthias Ringwald         struct lc3_encoder __e; \
121*4c4eb519SMatthias Ringwald         float __s[LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz)-1]; \
1229a19cd78SMatthias Ringwald     }
1239a19cd78SMatthias Ringwald 
1249a19cd78SMatthias Ringwald 
1259a19cd78SMatthias Ringwald /**
1269a19cd78SMatthias Ringwald  * Decoder state and memory
1279a19cd78SMatthias Ringwald  */
1289a19cd78SMatthias Ringwald 
1299a19cd78SMatthias Ringwald typedef struct lc3_ltpf_synthesis {
1309a19cd78SMatthias Ringwald     bool active;
1319a19cd78SMatthias Ringwald     int pitch;
1324930cef6SMatthias Ringwald     float c[2*12], x[12];
1339a19cd78SMatthias Ringwald } lc3_ltpf_synthesis_t;
1349a19cd78SMatthias Ringwald 
1359a19cd78SMatthias Ringwald typedef struct lc3_plc_state {
1369a19cd78SMatthias Ringwald     uint16_t seed;
1379a19cd78SMatthias Ringwald     int count;
1389a19cd78SMatthias Ringwald     float alpha;
1399a19cd78SMatthias Ringwald } lc3_plc_state_t;
1409a19cd78SMatthias Ringwald 
1419a19cd78SMatthias Ringwald struct lc3_decoder {
142*4c4eb519SMatthias Ringwald     enum lc3_dt dt;
143*4c4eb519SMatthias Ringwald     enum lc3_srate sr, sr_pcm;
144*4c4eb519SMatthias Ringwald 
145*4c4eb519SMatthias Ringwald     lc3_ltpf_synthesis_t ltpf;
146*4c4eb519SMatthias Ringwald     lc3_plc_state_t plc;
147*4c4eb519SMatthias Ringwald 
148*4c4eb519SMatthias Ringwald     float *xh, *xs, *xd, *xg, s[1];
1499a19cd78SMatthias Ringwald };
1509a19cd78SMatthias Ringwald 
1519a19cd78SMatthias Ringwald #define LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz) \
1524930cef6SMatthias Ringwald     ( __LC3_NH(dt_us, sr_hz) + __LC3_ND(dt_us, sr_hz) + \
1534930cef6SMatthias Ringwald       __LC3_NS(dt_us, sr_hz) )
1549a19cd78SMatthias Ringwald 
1559a19cd78SMatthias Ringwald #define LC3_DECODER_MEM_T(dt_us, sr_hz) \
1569a19cd78SMatthias Ringwald     struct { \
157*4c4eb519SMatthias Ringwald         struct lc3_decoder __d; \
158*4c4eb519SMatthias Ringwald         float __s[LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz)-1]; \
1599a19cd78SMatthias Ringwald     }
1609a19cd78SMatthias Ringwald 
1619a19cd78SMatthias Ringwald 
1629a19cd78SMatthias Ringwald #endif /* __LC3_PRIVATE_H */
163