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  * Return number of samples, delayed samples and
28  * encoded spectrum coefficients within a frame
29  * - For encoding, keep 1.25 ms of temporal winodw
30  * - For decoding, keep 18 ms of history, aligned on frames, and a frame
31  */
32 
33 #define __LC3_NS(dt_us, sr_hz) \
34     ( (dt_us * sr_hz) / 1000 / 1000 )
35 
36 #define __LC3_ND(dt_us, sr_hz) \
37     ( (dt_us) == 7500 ? 23 * __LC3_NS(dt_us, sr_hz) / 30 \
38                       :  5 * __LC3_NS(dt_us, sr_hz) /  8 )
39 
40 #define __LC3_NT(sr_hz) \
41     ( (5 * sr_hz) / 4000 )
42 
43 #define __LC3_NH(dt_us, sr_hz) \
44     ( ((3 - ((dt_us) >= 10000)) + 1) * __LC3_NS(dt_us, sr_hz) )
45 
46 
47 /**
48  * Frame duration 7.5ms or 10ms
49  */
50 
51 enum lc3_dt {
52     LC3_DT_7M5,
53     LC3_DT_10M,
54 
55     LC3_NUM_DT
56 };
57 
58 /**
59  * Sampling frequency
60  */
61 
62 enum lc3_srate {
63     LC3_SRATE_8K,
64     LC3_SRATE_16K,
65     LC3_SRATE_24K,
66     LC3_SRATE_32K,
67     LC3_SRATE_48K,
68 
69     LC3_NUM_SRATE,
70 };
71 
72 
73 /**
74  * Encoder state and memory
75  */
76 
77 typedef struct lc3_attdet_analysis {
78     int32_t en1, an1;
79     int p_att;
80 } lc3_attdet_analysis_t;
81 
82 struct lc3_ltpf_hp50_state {
83     int64_t s1, s2;
84 };
85 
86 typedef struct lc3_ltpf_analysis {
87     bool active;
88     int pitch;
89     float nc[2];
90 
91     struct lc3_ltpf_hp50_state hp50;
92     int16_t x_12k8[384];
93     int16_t x_6k4[178];
94     int tc;
95 } lc3_ltpf_analysis_t;
96 
97 typedef struct lc3_spec_analysis {
98     float nbits_off;
99     int nbits_spare;
100 } lc3_spec_analysis_t;
101 
102 struct lc3_encoder {
103     enum lc3_dt dt;
104     enum lc3_srate sr, sr_pcm;
105 
106     lc3_attdet_analysis_t attdet;
107     lc3_ltpf_analysis_t ltpf;
108     lc3_spec_analysis_t spec;
109 
110     int16_t *xt;
111     float *xs, *xd, s[0];
112 };
113 
114 #define LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz) \
115     ( ( __LC3_NS(dt_us, sr_hz) + __LC3_NT(sr_hz) ) / 2 + \
116         __LC3_NS(dt_us, sr_hz) + __LC3_ND(dt_us, sr_hz) )
117 
118 #define LC3_ENCODER_MEM_T(dt_us, sr_hz) \
119     struct { \
120         struct lc3_encoder __e; \
121         float __s[LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz)]; \
122     }
123 
124 
125 /**
126  * Decoder state and memory
127  */
128 
129 typedef struct lc3_ltpf_synthesis {
130     bool active;
131     int pitch;
132     float c[2*12], x[12];
133 } lc3_ltpf_synthesis_t;
134 
135 typedef struct lc3_plc_state {
136     uint16_t seed;
137     int count;
138     float alpha;
139 } lc3_plc_state_t;
140 
141 struct lc3_decoder {
142     enum lc3_dt dt;
143     enum lc3_srate sr, sr_pcm;
144 
145     lc3_ltpf_synthesis_t ltpf;
146     lc3_plc_state_t plc;
147 
148     float *xh, *xs, *xd, *xg, s[0];
149 };
150 
151 #define LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz) \
152     ( __LC3_NH(dt_us, sr_hz) + __LC3_ND(dt_us, sr_hz) + \
153       __LC3_NS(dt_us, sr_hz) )
154 
155 #define LC3_DECODER_MEM_T(dt_us, sr_hz) \
156     struct { \
157         struct lc3_decoder __d; \
158         float __s[LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz)]; \
159     }
160 
161 
162 #endif /* __LC3_PRIVATE_H */
163