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 // BK: to avoid C2229 on MSVC due to zero-sized array not being the last element in struct when 103 // LC3_ENCODER_MEM_T is used, we define the struct with the help of a macro 104 105 #define LC3_ENCODER_FIELDS(SAMPLES) \ 106 enum lc3_dt dt; \ 107 enum lc3_srate sr, sr_pcm; \ 108 \ 109 lc3_attdet_analysis_t attdet; \ 110 lc3_ltpf_analysis_t ltpf; \ 111 lc3_spec_analysis_t spec; \ 112 \ 113 int16_t *xt; \ 114 float *xs, *xd, s[SAMPLES]; 115 116 struct lc3_encoder { 117 LC3_ENCODER_FIELDS(0) 118 }; 119 120 #define LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz) \ 121 ( ( __LC3_NS(dt_us, sr_hz) + __LC3_NT(sr_hz) ) / 2 + \ 122 __LC3_NS(dt_us, sr_hz) + __LC3_ND(dt_us, sr_hz) ) 123 124 #define LC3_ENCODER_MEM_T(dt_us, sr_hz) \ 125 struct { \ 126 LC3_ENCODER_FIELDS( LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz) ) \ 127 } 128 129 130 /** 131 * Decoder state and memory 132 */ 133 134 typedef struct lc3_ltpf_synthesis { 135 bool active; 136 int pitch; 137 float c[2*12], x[12]; 138 } lc3_ltpf_synthesis_t; 139 140 typedef struct lc3_plc_state { 141 uint16_t seed; 142 int count; 143 float alpha; 144 } lc3_plc_state_t; 145 146 // BK: to avoid C2229 on MSVC due to zero-sized array not being the last element in struct when 147 // LC3_ENCODER_MEM_T is used, we define the struct with the help of a macro 148 149 #define LC3_DECODER_FIELDS(SAMPLES) \ 150 enum lc3_dt dt; \ 151 enum lc3_srate sr, sr_pcm; \ 152 \ 153 lc3_ltpf_synthesis_t ltpf; \ 154 lc3_plc_state_t plc; \ 155 \ 156 float *xh, *xs, *xd, *xg, s[SAMPLES]; 157 158 struct lc3_decoder { 159 LC3_DECODER_FIELDS(0) 160 }; 161 162 #define LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz) \ 163 ( __LC3_NH(dt_us, sr_hz) + __LC3_ND(dt_us, sr_hz) + \ 164 __LC3_NS(dt_us, sr_hz) ) 165 166 #define LC3_DECODER_MEM_T(dt_us, sr_hz) \ 167 struct { \ 168 LC3_DECODER_FIELDS(LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz)) \ 169 } 170 171 172 #endif /* __LC3_PRIVATE_H */ 173