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 #ifndef __LC3_PRIVATE_H
20 #define __LC3_PRIVATE_H
21 
22 #include <stdint.h>
23 #include <stdbool.h>
24 
25 
26 /**
27  * Characteristics
28  *
29  * - The number of samples within a frame
30  *
31  * - The number of MDCT delayed samples, sum of half a frame and
32  *   an ovelap of future by 1.25 ms (2.5ms, 5ms and 10ms frame durations)
33  *   or 2 ms (7.5ms frame duration).
34  *
35  * - For decoding, keep 18 ms of history, aligned on a frame
36  *
37  * - For encoding, keep 1.25 ms of temporal previous samples
38  */
39 
40 #define LC3_NS(dt_us, sr_hz) \
41     ( (dt_us) * (sr_hz) / 1000 / 1000 )
42 
43 #define LC3_ND(dt_us, sr_hz) \
44     ( LC3_NS(dt_us, sr_hz) / 2 + \
45       LC3_NS((dt_us) == 7500 ? 2000 : 1250, sr_hz) )
46 
47 #define LC3_NH(dt_us, sr_hz) \
48     ( (sr_hz) > 48000 ? 0 : ( LC3_NS(18000, sr_hz) + \
49       LC3_NS(dt_us, sr_hz) - (LC3_NS(18000, sr_hz) % LC3_NS(dt_us, sr_hz)) ) )
50 
51 #define LC3_NT(sr_hz) \
52     ( LC3_NS(1250, sr_hz) )
53 
54 
55 /**
56  * Frame duration
57  */
58 
59 enum lc3_dt {
60     LC3_DT_2M5 = 0,
61     LC3_DT_5M  = 1,
62     LC3_DT_7M5 = 2,
63     LC3_DT_10M = 3,
64 
65     LC3_NUM_DT
66 };
67 
68 
69 /**
70  * Sampling frequency and high-resolution mode
71  */
72 
73 enum lc3_srate {
74     LC3_SRATE_8K,
75     LC3_SRATE_16K,
76     LC3_SRATE_24K,
77     LC3_SRATE_32K,
78     LC3_SRATE_48K,
79     LC3_SRATE_48K_HR,
80     LC3_SRATE_96K_HR,
81 
82     LC3_NUM_SRATE
83 };
84 
85 
86 /**
87  * Encoder state and memory
88  */
89 
90 typedef struct lc3_attdet_analysis {
91     int32_t en1, an1;
92     int p_att;
93 } lc3_attdet_analysis_t;
94 
95 struct lc3_ltpf_hp50_state {
96     int64_t s1, s2;
97 };
98 
99 typedef struct lc3_ltpf_analysis {
100     bool active;
101     int pitch;
102     float nc[2];
103 
104     struct lc3_ltpf_hp50_state hp50;
105     int16_t x_12k8[384];
106     int16_t x_6k4[178];
107     int tc;
108 } lc3_ltpf_analysis_t;
109 
110 typedef struct lc3_spec_analysis {
111     float nbits_off;
112     int nbits_spare;
113 } lc3_spec_analysis_t;
114 
115 struct lc3_encoder {
116     enum lc3_dt dt;
117     enum lc3_srate sr, sr_pcm;
118 
119     lc3_attdet_analysis_t attdet;
120     lc3_ltpf_analysis_t ltpf;
121     lc3_spec_analysis_t spec;
122 
123     int xt_off, xs_off, xd_off;
124     float x[1];
125 };
126 
127 #define LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz) \
128     ( ( LC3_NS(dt_us, sr_hz) + LC3_NT(sr_hz) ) / 2 + \
129         LC3_NS(dt_us, sr_hz) + LC3_ND(dt_us, sr_hz) )
130 
131 #define LC3_ENCODER_MEM_T(dt_us, sr_hz) \
132     struct { \
133         struct lc3_encoder __e; \
134         float __x[LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz)-1]; \
135     }
136 
137 
138 /**
139  * Decoder state and memory
140  */
141 
142 typedef struct lc3_ltpf_synthesis {
143     bool active;
144     int pitch;
145     float c[2*12], x[12];
146 } lc3_ltpf_synthesis_t;
147 
148 typedef struct lc3_plc_state {
149     uint16_t seed;
150     int count;
151     float alpha;
152 } lc3_plc_state_t;
153 
154 struct lc3_decoder {
155     enum lc3_dt dt;
156     enum lc3_srate sr, sr_pcm;
157 
158     lc3_ltpf_synthesis_t ltpf;
159     lc3_plc_state_t plc;
160 
161     int xh_off, xs_off, xd_off, xg_off;
162     float x[1];
163 };
164 
165 #define LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz) \
166     ( LC3_NH(dt_us, sr_hz) + LC3_NS(dt_us, sr_hz) + \
167       LC3_ND(dt_us, sr_hz) + LC3_NS(dt_us, sr_hz)   )
168 
169 #define LC3_DECODER_MEM_T(dt_us, sr_hz) \
170     struct { \
171         struct lc3_decoder __d; \
172         float __x[LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz)-1]; \
173     }
174 
175 
176 /**
177  * Change the visibility of interface functions
178  */
179 
180 #ifdef _WIN32
181 #define LC3_EXPORT __declspec(dllexport)
182 #else
183 #define LC3_EXPORT __attribute__((visibility ("default")))
184 #endif
185 
186 
187 #endif /* __LC3_PRIVATE_H */
188