xref: /btstack/3rd-party/lc3-google/include/lc3_private.h (revision 6897da5c53aac5b1f90f41b5b15d0bd43d61dfff)
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 /**
27*6897da5cSDirk Helbig  * Characteristics
28*6897da5cSDirk Helbig  *
29*6897da5cSDirk Helbig  * - The number of samples within a frame
30*6897da5cSDirk Helbig  *
31*6897da5cSDirk Helbig  * - The number of MDCT delayed samples, sum of half a frame and
32*6897da5cSDirk Helbig  *   an ovelap of future by 1.25 ms (2.5ms, 5ms and 10ms frame durations)
33*6897da5cSDirk Helbig  *   or 2 ms (7.5ms frame duration).
34*6897da5cSDirk Helbig  *
35*6897da5cSDirk Helbig  * - For decoding, keep 18 ms of history, aligned on a frame
36*6897da5cSDirk Helbig  *
37*6897da5cSDirk Helbig  * - For encoding, keep 1.25 ms of temporal previous samples
389a19cd78SMatthias Ringwald  */
399a19cd78SMatthias Ringwald 
40*6897da5cSDirk Helbig #define LC3_NS(dt_us, sr_hz) \
41*6897da5cSDirk Helbig     ( (dt_us) * (sr_hz) / 1000 / 1000 )
429a19cd78SMatthias Ringwald 
43*6897da5cSDirk Helbig #define LC3_ND(dt_us, sr_hz) \
44*6897da5cSDirk Helbig     ( LC3_NS(dt_us, sr_hz) / 2 + \
45*6897da5cSDirk Helbig       LC3_NS((dt_us) == 7500 ? 2000 : 1250, sr_hz) )
469a19cd78SMatthias Ringwald 
47*6897da5cSDirk Helbig #define LC3_NH(dt_us, sr_hz) \
48*6897da5cSDirk Helbig     ( (sr_hz) > 48000 ? 0 : ( LC3_NS(18000, sr_hz) + \
49*6897da5cSDirk Helbig       LC3_NS(dt_us, sr_hz) - (LC3_NS(18000, sr_hz) % LC3_NS(dt_us, sr_hz)) ) )
504930cef6SMatthias Ringwald 
51*6897da5cSDirk Helbig #define LC3_NT(sr_hz) \
52*6897da5cSDirk Helbig     ( LC3_NS(1250, sr_hz) )
539a19cd78SMatthias Ringwald 
549a19cd78SMatthias Ringwald 
559a19cd78SMatthias Ringwald /**
56*6897da5cSDirk Helbig  * Frame duration
579a19cd78SMatthias Ringwald  */
589a19cd78SMatthias Ringwald 
599a19cd78SMatthias Ringwald enum lc3_dt {
60*6897da5cSDirk Helbig     LC3_DT_2M5 = 0,
61*6897da5cSDirk Helbig     LC3_DT_5M  = 1,
62*6897da5cSDirk Helbig     LC3_DT_7M5 = 2,
63*6897da5cSDirk Helbig     LC3_DT_10M = 3,
649a19cd78SMatthias Ringwald 
659a19cd78SMatthias Ringwald     LC3_NUM_DT
669a19cd78SMatthias Ringwald };
679a19cd78SMatthias Ringwald 
68*6897da5cSDirk Helbig 
699a19cd78SMatthias Ringwald /**
70*6897da5cSDirk Helbig  * Sampling frequency and high-resolution mode
719a19cd78SMatthias Ringwald  */
729a19cd78SMatthias Ringwald 
739a19cd78SMatthias Ringwald enum lc3_srate {
749a19cd78SMatthias Ringwald     LC3_SRATE_8K,
759a19cd78SMatthias Ringwald     LC3_SRATE_16K,
769a19cd78SMatthias Ringwald     LC3_SRATE_24K,
779a19cd78SMatthias Ringwald     LC3_SRATE_32K,
789a19cd78SMatthias Ringwald     LC3_SRATE_48K,
79*6897da5cSDirk Helbig     LC3_SRATE_48K_HR,
80*6897da5cSDirk Helbig     LC3_SRATE_96K_HR,
819a19cd78SMatthias Ringwald 
82*6897da5cSDirk Helbig     LC3_NUM_SRATE
839a19cd78SMatthias Ringwald };
849a19cd78SMatthias Ringwald 
859a19cd78SMatthias Ringwald 
869a19cd78SMatthias Ringwald /**
879a19cd78SMatthias Ringwald  * Encoder state and memory
889a19cd78SMatthias Ringwald  */
899a19cd78SMatthias Ringwald 
909a19cd78SMatthias Ringwald typedef struct lc3_attdet_analysis {
914930cef6SMatthias Ringwald     int32_t en1, an1;
929a19cd78SMatthias Ringwald     int p_att;
939a19cd78SMatthias Ringwald } lc3_attdet_analysis_t;
949a19cd78SMatthias Ringwald 
959a19cd78SMatthias Ringwald struct lc3_ltpf_hp50_state {
964930cef6SMatthias Ringwald     int64_t s1, s2;
979a19cd78SMatthias Ringwald };
989a19cd78SMatthias Ringwald 
999a19cd78SMatthias Ringwald typedef struct lc3_ltpf_analysis {
1009a19cd78SMatthias Ringwald     bool active;
1019a19cd78SMatthias Ringwald     int pitch;
1029a19cd78SMatthias Ringwald     float nc[2];
1039a19cd78SMatthias Ringwald 
1049a19cd78SMatthias Ringwald     struct lc3_ltpf_hp50_state hp50;
1054930cef6SMatthias Ringwald     int16_t x_12k8[384];
1064930cef6SMatthias Ringwald     int16_t x_6k4[178];
1079a19cd78SMatthias Ringwald     int tc;
1089a19cd78SMatthias Ringwald } lc3_ltpf_analysis_t;
1099a19cd78SMatthias Ringwald 
1109a19cd78SMatthias Ringwald typedef struct lc3_spec_analysis {
1119a19cd78SMatthias Ringwald     float nbits_off;
1129a19cd78SMatthias Ringwald     int nbits_spare;
1139a19cd78SMatthias Ringwald } lc3_spec_analysis_t;
1149a19cd78SMatthias Ringwald 
1159a19cd78SMatthias Ringwald struct lc3_encoder {
1164c4eb519SMatthias Ringwald     enum lc3_dt dt;
1174c4eb519SMatthias Ringwald     enum lc3_srate sr, sr_pcm;
1184c4eb519SMatthias Ringwald 
1194c4eb519SMatthias Ringwald     lc3_attdet_analysis_t attdet;
1204c4eb519SMatthias Ringwald     lc3_ltpf_analysis_t ltpf;
1214c4eb519SMatthias Ringwald     lc3_spec_analysis_t spec;
1224c4eb519SMatthias Ringwald 
123*6897da5cSDirk Helbig     int xt_off, xs_off, xd_off;
124*6897da5cSDirk Helbig     float x[1];
1259a19cd78SMatthias Ringwald };
1269a19cd78SMatthias Ringwald 
1279a19cd78SMatthias Ringwald #define LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz) \
128*6897da5cSDirk Helbig     ( ( LC3_NS(dt_us, sr_hz) + LC3_NT(sr_hz) ) / 2 + \
129*6897da5cSDirk Helbig         LC3_NS(dt_us, sr_hz) + LC3_ND(dt_us, sr_hz) )
1309a19cd78SMatthias Ringwald 
1319a19cd78SMatthias Ringwald #define LC3_ENCODER_MEM_T(dt_us, sr_hz) \
1329a19cd78SMatthias Ringwald     struct { \
1334c4eb519SMatthias Ringwald         struct lc3_encoder __e; \
134*6897da5cSDirk Helbig         float __x[LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz)-1]; \
1359a19cd78SMatthias Ringwald     }
1369a19cd78SMatthias Ringwald 
1379a19cd78SMatthias Ringwald 
1389a19cd78SMatthias Ringwald /**
1399a19cd78SMatthias Ringwald  * Decoder state and memory
1409a19cd78SMatthias Ringwald  */
1419a19cd78SMatthias Ringwald 
1429a19cd78SMatthias Ringwald typedef struct lc3_ltpf_synthesis {
1439a19cd78SMatthias Ringwald     bool active;
1449a19cd78SMatthias Ringwald     int pitch;
1454930cef6SMatthias Ringwald     float c[2*12], x[12];
1469a19cd78SMatthias Ringwald } lc3_ltpf_synthesis_t;
1479a19cd78SMatthias Ringwald 
1489a19cd78SMatthias Ringwald typedef struct lc3_plc_state {
1499a19cd78SMatthias Ringwald     uint16_t seed;
1509a19cd78SMatthias Ringwald     int count;
1519a19cd78SMatthias Ringwald     float alpha;
1529a19cd78SMatthias Ringwald } lc3_plc_state_t;
1539a19cd78SMatthias Ringwald 
1549a19cd78SMatthias Ringwald struct lc3_decoder {
1554c4eb519SMatthias Ringwald     enum lc3_dt dt;
1564c4eb519SMatthias Ringwald     enum lc3_srate sr, sr_pcm;
1574c4eb519SMatthias Ringwald 
1584c4eb519SMatthias Ringwald     lc3_ltpf_synthesis_t ltpf;
1594c4eb519SMatthias Ringwald     lc3_plc_state_t plc;
1604c4eb519SMatthias Ringwald 
161*6897da5cSDirk Helbig     int xh_off, xs_off, xd_off, xg_off;
162*6897da5cSDirk Helbig     float x[1];
1639a19cd78SMatthias Ringwald };
1649a19cd78SMatthias Ringwald 
1659a19cd78SMatthias Ringwald #define LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz) \
166*6897da5cSDirk Helbig     ( LC3_NH(dt_us, sr_hz) + LC3_NS(dt_us, sr_hz) + \
167*6897da5cSDirk Helbig       LC3_ND(dt_us, sr_hz) + LC3_NS(dt_us, sr_hz)   )
1689a19cd78SMatthias Ringwald 
1699a19cd78SMatthias Ringwald #define LC3_DECODER_MEM_T(dt_us, sr_hz) \
1709a19cd78SMatthias Ringwald     struct { \
1714c4eb519SMatthias Ringwald         struct lc3_decoder __d; \
172*6897da5cSDirk Helbig         float __x[LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz)-1]; \
1739a19cd78SMatthias Ringwald     }
1749a19cd78SMatthias Ringwald 
1759a19cd78SMatthias Ringwald 
176*6897da5cSDirk Helbig /**
177*6897da5cSDirk Helbig  * Change the visibility of interface functions
178*6897da5cSDirk Helbig  */
179*6897da5cSDirk Helbig 
180*6897da5cSDirk Helbig #ifdef _WIN32
181*6897da5cSDirk Helbig #define LC3_EXPORT __declspec(dllexport)
182*6897da5cSDirk Helbig #else
183*6897da5cSDirk Helbig #define LC3_EXPORT __attribute__((visibility ("default")))
184*6897da5cSDirk Helbig #endif
185*6897da5cSDirk Helbig 
186*6897da5cSDirk Helbig 
1879a19cd78SMatthias Ringwald #endif /* __LC3_PRIVATE_H */
188