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 142983096 : void read_bit_fl(LC3_UINT8* ptr, LC3_INT* mask_side, LC3_INT* bp_side, LC3_INT* bit)
18 : {
19 142983096 : if (ptr[*bp_side] & *mask_side) {
20 68085386 : *bit = 1;
21 : } else {
22 74897710 : *bit = 0;
23 : }
24 :
25 142983096 : if (*mask_side == 128) {
26 16908198 : *mask_side = 1;
27 16908198 : *bp_side = *bp_side - 1;
28 : } else {
29 126074898 : *mask_side = *mask_side * 2;
30 : }
31 142983096 : }
32 :
33 19059330 : 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 19059330 : read_bit_fl(ptr, mask_side, bp_side, val);
38 :
39 131016918 : for (i = 1; i < nbits; i++) {
40 111957588 : read_bit_fl(ptr, mask_side, bp_side, &bit);
41 111957588 : *val = *val + (bit << i);
42 : }
43 19059330 : }
44 :
45 : #ifdef ENABLE_PADDING
46 1768748 : 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 1768748 : LC3_UINT8* ptr = bytes;
53 :
54 1768748 : LC3_INT nbbytes = nbbits >> 3;
55 : LC3_INT lastnz;
56 : LC3_INT bw_cutoff_idx;
57 1768748 : LC3_INT nbits = ceil(LC3_LOGTWO(L_spec / 2));
58 :
59 1768748 : if (nbits > nbbits)
60 : {
61 0 : return 1;
62 : }
63 :
64 1768748 : *np_zero = 0;
65 :
66 1768748 : *total_padding = 0;
67 :
68 1768748 : bp_side = (nbbits - 1) >> 3;
69 1768748 : mask_side = 1 << (8 - (nbbits - (bp_side << 3)));
70 :
71 1768748 : if (bp_side < 19 || bp_side >= LC3PLUS_MAX_BYTES) {
72 0 : return 1;
73 : }
74 :
75 1768748 : ptr = bytes;
76 :
77 1768748 : if (bw_cutoff_bits > 0) {
78 1768748 : read_uint_fl(bw_cutoff_bits, ptr, &mask_side, &bp_side, &bw_cutoff_idx);
79 : }
80 :
81 1768748 : read_uint_fl(nbits, ptr, &mask_side, &bp_side, &lastnz);
82 :
83 1768748 : lastnz_threshold = (1 << nbits) - 1 - 1;
84 :
85 1768748 : 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 1768748 : if (ep_enabled != 0)
120 : {
121 0 : *total_padding = *total_padding + *np_zero;
122 : }
123 :
124 1768748 : return 0;
125 : }
126 : #endif
127 :
128 1768748 : 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 1768748 : LC3_INT gainMSBbits[4] = {1, 1, 2, 2};
138 :
139 1768748 : *bp_side = -1;
140 1768748 : bp_side_local = numbytes - 1; /* Matlab offset by 1 */
141 1768748 : mask_side_local = 1;
142 1768748 : *mask_side = -1;
143 1768748 : ptr = bytes;
144 1768748 : *lsbMode = -1;
145 1768748 : *lastnz = -1;
146 :
147 1768748 : plc_trigger_bw = 1; /* Bandwidth */
148 1768748 : plc_trigger_last_nz = 1; /* Last non-zero tuple */
149 1768748 : plc_trigger_SNS1 = 1; /* SNS-VQ 2nd stage MPVQ data (24-25 bits) */
150 1768748 : plc_trigger_SNS2 = 1; /* SNS-VQ 2nd stage MPVQ data (24-25 bits) */
151 :
152 :
153 :
154 : /* Bandwidth */
155 1768748 : if (bw_cutoff_bits > 0) {
156 1768748 : read_uint_fl(bw_cutoff_bits, ptr, &mask_side_local, &bp_side_local, bw_cutoff_idx);
157 :
158 1768748 : 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 1768748 : if (*bw_cutoff_idx < 3 || frame_dms == 25) {
171 18160 : *tns_numfilters = 1;
172 : } else {
173 1750588 : *tns_numfilters = 2;
174 : }
175 :
176 : /* Last non-zero tuple */
177 1768748 : read_uint_fl(ceil(LC3_LOGTWO(N >> 1)), ptr, &mask_side_local, &bp_side_local, lastnz);
178 1768748 : *lastnz = (*lastnz + 1) * 2;
179 :
180 1768748 : 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 1768748 : read_bit_fl(ptr, &mask_side_local, &bp_side_local, lsbMode);
189 :
190 : /* Global gain */
191 1768748 : read_uint_fl(8, ptr, &mask_side_local, &bp_side_local, gg_idx);
192 :
193 : /* TNS activation flag */
194 5288084 : for (i = 0; i < *tns_numfilters; i++) {
195 3519336 : read_bit_fl(ptr, &mask_side_local, &bp_side_local, &bit);
196 3519336 : tns_order[i] = bit;
197 : }
198 :
199 : /* LTPF activation flag */
200 1768748 : read_bit_fl(ptr, &mask_side_local, &bp_side_local, <pf_tmp[0]);
201 :
202 : /* SNS-VQ 1st stage */
203 1768748 : read_uint_fl(5, ptr, &mask_side_local, &bp_side_local, &scf_idx[0]);
204 1768748 : 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 1768748 : read_bit_fl(ptr, &mask_side_local, &bp_side_local, &submodeMSB);
208 1768748 : scf_idx[2] = submodeMSB * 2;
209 :
210 1768748 : read_uint_fl(gainMSBbits[scf_idx[2]], ptr, &mask_side_local, &bp_side_local, &scf_idx[3]);
211 1768748 : 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 1768748 : if (submodeMSB == 0) {
215 878535 : read_uint_fl(25, ptr, &mask_side_local, &bp_side_local, &tmp);
216 878535 : if (tmp >= 33460056) {
217 0 : *bfi = plc_trigger_SNS1;
218 0 : if (*bfi) {
219 0 : return;
220 : }
221 : }
222 :
223 878535 : ind = floor(tmp / 2390004);
224 878535 : scf_idx[5] = tmp - ind * 2390004;
225 :
226 878535 : if (ind < 2) {
227 182390 : submodeLSB = 1;
228 182390 : scf_idx[3] = scf_idx[3] * 2 + ind;
229 182390 : scf_idx[6] = -2;
230 : } else {
231 696145 : submodeLSB = 0;
232 696145 : scf_idx[6] = ind - 2;
233 : }
234 :
235 : } else {
236 890213 : read_uint_fl(24, ptr, &mask_side_local, &bp_side_local, &tmp);
237 :
238 890213 : if (tmp >= 16708096) {
239 0 : *bfi = plc_trigger_SNS2;
240 0 : if (*bfi) {
241 0 : return;
242 : }
243 : }
244 :
245 890213 : if (tmp >= 15158272) {
246 57412 : submodeLSB = 1;
247 57412 : scf_idx[3] = scf_idx[3] * 2 + ((tmp - 15158272) & 1);
248 57412 : scf_idx[5] = floor((tmp - 15158272) / 2);
249 57412 : scf_idx[6] = -2;
250 : } else {
251 832801 : submodeLSB = 0;
252 832801 : scf_idx[5] = tmp;
253 832801 : scf_idx[6] = -1;
254 : }
255 : }
256 :
257 1768748 : scf_idx[2] = scf_idx[2] + submodeLSB;
258 :
259 : /* LTPF data */
260 1768748 : if (ltpf_tmp[0] == 1) {
261 1371850 : read_bit_fl(ptr, &mask_side_local, &bp_side_local, <pf_tmp[1]);
262 1371850 : read_uint_fl(9, ptr, &mask_side_local, &bp_side_local, <pf_tmp[2]);
263 : } else {
264 396898 : ltpf_tmp[1] = 0;
265 396898 : ltpf_tmp[2] = 0;
266 : }
267 :
268 7074992 : for (i = 0; i < 3; i++) {
269 5306244 : ltpf_idx[i] = ltpf_tmp[i];
270 : }
271 :
272 : /* Noise factor */
273 1768748 : read_uint_fl(3, ptr, &mask_side_local, &bp_side_local, fac_ns_idx);
274 :
275 1768748 : *bp_side = bp_side_local;
276 1768748 : *mask_side = mask_side_local;
277 : }
|