Line data Source code
1 : /******************************************************************************
2 : * ETSI TS 103 634 V1.5.1 *
3 : * Low Complexity Communication Codec Plus (LC3plus) *
4 : * *
5 : * Copyright licence is solely granted through ETSI Intellectual Property *
6 : * Rights Policy, 3rd April 2019. No patent licence is granted by implication, *
7 : * estoppel or otherwise. *
8 : ******************************************************************************/
9 :
10 : #include "options.h"
11 : #include "wmc_auto.h"
12 : #include "functions.h"
13 :
14 : static void read_bit_fl(LC3_UINT8* ptr, LC3_INT* mask_side, LC3_INT* bp_side, LC3_INT* bit);
15 : static void read_uint_fl(LC3_INT nbits, LC3_UINT8* ptr, LC3_INT* mask_side, LC3_INT* bp_side, LC3_INT* val);
16 :
17 0 : void read_bit_fl(LC3_UINT8* ptr, LC3_INT* mask_side, LC3_INT* bp_side, LC3_INT* bit)
18 : {
19 0 : if (ptr[*bp_side] & *mask_side) {
20 0 : *bit = 1;
21 : } else {
22 0 : *bit = 0;
23 : }
24 :
25 0 : if (*mask_side == 128) {
26 0 : *mask_side = 1;
27 0 : *bp_side = *bp_side - 1;
28 : } else {
29 0 : *mask_side = *mask_side * 2;
30 : }
31 0 : }
32 :
33 0 : void read_uint_fl(LC3_INT nbits, LC3_UINT8* ptr, LC3_INT* mask_side, LC3_INT* bp_side, LC3_INT* val)
34 : {
35 : LC3_INT bit, i;
36 :
37 0 : read_bit_fl(ptr, mask_side, bp_side, val);
38 :
39 0 : for (i = 1; i < nbits; i++) {
40 0 : read_bit_fl(ptr, mask_side, bp_side, &bit);
41 0 : *val = *val + (bit << i);
42 : }
43 0 : }
44 :
45 : #ifdef ENABLE_PADDING
46 0 : LC3_INT paddingDec_fl(LC3_UINT8* bytes, LC3_INT nbbits, LC3_INT L_spec, LC3_INT bw_cutoff_bits, LC3_INT ep_enabled, LC3_INT* total_padding, LC3_INT *np_zero)
47 : {
48 : LC3_INT lastnz_threshold;
49 : LC3_INT val, padding_len_bits, padding_len;
50 : LC3_INT bp_side;
51 : LC3_INT mask_side;
52 0 : LC3_UINT8* ptr = bytes;
53 :
54 0 : LC3_INT nbbytes = nbbits >> 3;
55 : LC3_INT lastnz;
56 : LC3_INT bw_cutoff_idx;
57 0 : LC3_INT nbits = ceil(LC3_LOGTWO(L_spec / 2));
58 :
59 0 : if (nbits > nbbits)
60 : {
61 0 : return 1;
62 : }
63 :
64 0 : *np_zero = 0;
65 :
66 0 : *total_padding = 0;
67 :
68 0 : bp_side = (nbbits - 1) >> 3;
69 0 : mask_side = 1 << (8 - (nbbits - (bp_side << 3)));
70 :
71 0 : if (bp_side < 19 || bp_side >= LC3PLUS_MAX_BYTES) {
72 0 : return 1;
73 : }
74 :
75 0 : ptr = bytes;
76 :
77 0 : if (bw_cutoff_bits > 0) {
78 0 : read_uint_fl(bw_cutoff_bits, ptr, &mask_side, &bp_side, &bw_cutoff_idx);
79 : }
80 :
81 0 : read_uint_fl(nbits, ptr, &mask_side, &bp_side, &lastnz);
82 :
83 0 : lastnz_threshold = (1 << nbits) - 1 - 1;
84 :
85 0 : while (lastnz == lastnz_threshold) {
86 0 : padding_len_bits = 16 - nbits - bw_cutoff_bits - 4;
87 :
88 : /*Read padding length*/
89 0 : read_uint_fl(padding_len_bits, ptr, &mask_side, &bp_side, &padding_len);
90 :
91 : /* Read 4 reserved bits */
92 0 : read_uint_fl(4, ptr, &mask_side, &bp_side, &val);
93 :
94 0 : if (ep_enabled == 0)
95 : {
96 : /* Discard padding length bytes */
97 0 : bp_side = bp_side - padding_len;
98 0 : *total_padding = *total_padding + padding_len + 2;
99 : }
100 : else
101 : {
102 0 : *total_padding = *total_padding + 2;
103 0 : *np_zero = *np_zero + padding_len;
104 : }
105 :
106 : /* check if minimum payload size is reached */
107 0 : if ((nbbytes - (*total_padding + *np_zero)) < 20) {
108 0 : return 1;
109 : }
110 :
111 : /* Read bandwidth bits */
112 0 : if (bw_cutoff_bits > 0) {
113 0 : read_uint_fl(bw_cutoff_bits, ptr, &mask_side, &bp_side, &bw_cutoff_idx);
114 : }
115 :
116 0 : read_uint_fl(nbits, ptr, &mask_side, &bp_side, &lastnz);
117 : }
118 :
119 0 : if (ep_enabled != 0)
120 : {
121 0 : *total_padding = *total_padding + *np_zero;
122 : }
123 :
124 0 : return 0;
125 : }
126 : #endif
127 :
128 0 : void processDecoderEntropy_fl(LC3_UINT8* bytes, LC3_INT numbytes, LC3_INT* mask_side, LC3_INT* bp_side, LC3_INT N, LC3_INT fs_idx,
129 : LC3_INT bw_cutoff_bits, LC3_INT* bfi, LC3_INT* gg_idx, LC3_INT* scf_idx, LC3_INT* fac_ns_idx,
130 : LC3_INT* tns_numfilters, LC3_INT* tns_order, LC3_INT* ltpf_idx, LC3_INT* bw_cutoff_idx, LC3_INT* lastnz,
131 : LC3_INT* lsbMode, LC3_INT frame_dms)
132 : {
133 :
134 : LC3_INT plc_trigger_bw, plc_trigger_last_nz, plc_trigger_SNS1, plc_trigger_SNS2, tmp, bit,
135 : submodeMSB, i, ltpf_tmp[3], ind, submodeLSB, bp_side_local, mask_side_local;
136 : LC3_UINT8* ptr;
137 0 : LC3_INT gainMSBbits[4] = {1, 1, 2, 2};
138 :
139 0 : *bp_side = -1;
140 0 : bp_side_local = numbytes - 1; /* Matlab offset by 1 */
141 0 : mask_side_local = 1;
142 0 : *mask_side = -1;
143 0 : ptr = bytes;
144 0 : *lsbMode = -1;
145 0 : *lastnz = -1;
146 :
147 0 : plc_trigger_bw = 1; /* Bandwidth */
148 0 : plc_trigger_last_nz = 1; /* Last non-zero tuple */
149 0 : plc_trigger_SNS1 = 1; /* SNS-VQ 2nd stage MPVQ data (24-25 bits) */
150 0 : plc_trigger_SNS2 = 1; /* SNS-VQ 2nd stage MPVQ data (24-25 bits) */
151 :
152 :
153 :
154 : /* Bandwidth */
155 0 : if (bw_cutoff_bits > 0) {
156 0 : read_uint_fl(bw_cutoff_bits, ptr, &mask_side_local, &bp_side_local, bw_cutoff_idx);
157 :
158 0 : if (fs_idx < *bw_cutoff_idx) {
159 0 : *bfi = plc_trigger_bw;
160 :
161 0 : if (*bfi) {
162 0 : return;
163 : }
164 : }
165 : } else {
166 0 : *bw_cutoff_idx = fs_idx;
167 : }
168 :
169 : /* Number of TNS filters */
170 0 : if (*bw_cutoff_idx < 3 || frame_dms == 25) {
171 0 : *tns_numfilters = 1;
172 : } else {
173 0 : *tns_numfilters = 2;
174 : }
175 :
176 : /* Last non-zero tuple */
177 0 : read_uint_fl(ceil(LC3_LOGTWO(N >> 1)), ptr, &mask_side_local, &bp_side_local, lastnz);
178 0 : *lastnz = (*lastnz + 1) * 2;
179 :
180 0 : if (*lastnz > N) {
181 0 : *bfi = plc_trigger_last_nz;
182 0 : if (*bfi) {
183 0 : return;
184 : }
185 : }
186 :
187 : /* LSB mode bit */
188 0 : read_bit_fl(ptr, &mask_side_local, &bp_side_local, lsbMode);
189 :
190 : /* Global gain */
191 0 : read_uint_fl(8, ptr, &mask_side_local, &bp_side_local, gg_idx);
192 :
193 : /* TNS activation flag */
194 0 : for (i = 0; i < *tns_numfilters; i++) {
195 0 : read_bit_fl(ptr, &mask_side_local, &bp_side_local, &bit);
196 0 : tns_order[i] = bit;
197 : }
198 :
199 : /* LTPF activation flag */
200 0 : read_bit_fl(ptr, &mask_side_local, &bp_side_local, <pf_tmp[0]);
201 :
202 : /* SNS-VQ 1st stage */
203 0 : read_uint_fl(5, ptr, &mask_side_local, &bp_side_local, &scf_idx[0]);
204 0 : read_uint_fl(5, ptr, &mask_side_local, &bp_side_local, &scf_idx[1]);
205 :
206 : /* SNS-VQ 2nd stage side-info (3-4 bits) */
207 0 : read_bit_fl(ptr, &mask_side_local, &bp_side_local, &submodeMSB);
208 0 : scf_idx[2] = submodeMSB * 2;
209 :
210 0 : read_uint_fl(gainMSBbits[scf_idx[2]], ptr, &mask_side_local, &bp_side_local, &scf_idx[3]);
211 0 : read_bit_fl(ptr, &mask_side_local, &bp_side_local, &scf_idx[4]);
212 :
213 : /* SNS-VQ 2nd stage MPVQ data (24-25 bits) */
214 0 : if (submodeMSB == 0) {
215 0 : read_uint_fl(25, ptr, &mask_side_local, &bp_side_local, &tmp);
216 0 : if (tmp >= 33460056) {
217 0 : *bfi = plc_trigger_SNS1;
218 0 : if (*bfi) {
219 0 : return;
220 : }
221 : }
222 :
223 0 : ind = floor(tmp / 2390004);
224 0 : scf_idx[5] = tmp - ind * 2390004;
225 :
226 0 : if (ind < 2) {
227 0 : submodeLSB = 1;
228 0 : scf_idx[3] = scf_idx[3] * 2 + ind;
229 0 : scf_idx[6] = -2;
230 : } else {
231 0 : submodeLSB = 0;
232 0 : scf_idx[6] = ind - 2;
233 : }
234 :
235 : } else {
236 0 : read_uint_fl(24, ptr, &mask_side_local, &bp_side_local, &tmp);
237 :
238 0 : if (tmp >= 16708096) {
239 0 : *bfi = plc_trigger_SNS2;
240 0 : if (*bfi) {
241 0 : return;
242 : }
243 : }
244 :
245 0 : if (tmp >= 15158272) {
246 0 : submodeLSB = 1;
247 0 : scf_idx[3] = scf_idx[3] * 2 + ((tmp - 15158272) & 1);
248 0 : scf_idx[5] = floor((tmp - 15158272) / 2);
249 0 : scf_idx[6] = -2;
250 : } else {
251 0 : submodeLSB = 0;
252 0 : scf_idx[5] = tmp;
253 0 : scf_idx[6] = -1;
254 : }
255 : }
256 :
257 0 : scf_idx[2] = scf_idx[2] + submodeLSB;
258 :
259 : /* LTPF data */
260 0 : if (ltpf_tmp[0] == 1) {
261 0 : read_bit_fl(ptr, &mask_side_local, &bp_side_local, <pf_tmp[1]);
262 0 : read_uint_fl(9, ptr, &mask_side_local, &bp_side_local, <pf_tmp[2]);
263 : } else {
264 0 : ltpf_tmp[1] = 0;
265 0 : ltpf_tmp[2] = 0;
266 : }
267 :
268 0 : for (i = 0; i < 3; i++) {
269 0 : ltpf_idx[i] = ltpf_tmp[i];
270 : }
271 :
272 : /* Noise factor */
273 0 : read_uint_fl(3, ptr, &mask_side_local, &bp_side_local, fac_ns_idx);
274 :
275 0 : *bp_side = bp_side_local;
276 0 : *mask_side = mask_side_local;
277 : }
|