1 /*
2 * Bluetooth low-complexity, subband codec (SBC)
3 *
4 * Copyright (C) 2017 Aurelien Jacobs <aurel@gnuage.org>
5 * Copyright (C) 2012-2013 Intel Corporation
6 * Copyright (C) 2008-2010 Nokia Corporation
7 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
8 * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
9 * Copyright (C) 2005-2008 Brad Midgley <bmidgley@xmission.com>
10 *
11 * This file is part of FFmpeg.
12 *
13 * FFmpeg is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
17 *
18 * FFmpeg is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with FFmpeg; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 */
27
28 /**
29 * @file
30 * SBC common functions for the encoder and decoder
31 */
32
34
35 /* A2DP specification: Appendix B, page 69 */
37 { -1, 0, 0, 0 },
38 { -2, 0, 0, 1 },
39 { -2, 0, 0, 1 },
40 { -2, 0, 0, 1 }
41 };
42
43 /* A2DP specification: Appendix B, page 69 */
45 { -2, 0, 0, 0, 0, 0, 0, 1 },
46 { -3, 0, 0, 0, 0, 0, 1, 2 },
47 { -4, 0, 0, 0, 0, 0, 1, 2 },
48 { -4, 0, 0, 0, 0, 0, 1, 2 }
49 };
50
51 /*
52 * Calculates the CRC-8 of the first len bits in data
53 */
55 {
56 size_t byte_length =
len >> 3;
57 int bit_length =
len & 7;
58 uint8_t crc;
59
61
62 if (bit_length) {
64 while (bit_length--) {
66 crc = (crc << 1) ^ ((
mask >> 7) & 0x1D);
68 }
69 }
70
71 return crc;
72 }
73
74 /*
75 * Code straight from the spec to calculate the bits array
76 * Takes a pointer to the frame in question and a pointer to the bits array
77 */
79 {
81 uint8_t sf =
frame->frequency;
82
84 int bitneed[2][8],
loudness, max_bitneed, bitcount, slicecount, bitslice;
85 int ch, sb;
86
87 for (ch = 0; ch <
frame->channels; ch++) {
88 max_bitneed = 0;
89 if (
frame->allocation == SNR) {
91 bitneed[ch][sb] =
frame->scale_factor[ch][sb];
92 if (bitneed[ch][sb] > max_bitneed)
93 max_bitneed = bitneed[ch][sb];
94 }
95 } else {
97 if (
frame->scale_factor[ch][sb] == 0)
98 bitneed[ch][sb] = -5;
99 else {
102 else
106 else
108 }
109 if (bitneed[ch][sb] > max_bitneed)
110 max_bitneed = bitneed[ch][sb];
111 }
112 }
113
114 bitcount = 0;
115 slicecount = 0;
116 bitslice = max_bitneed + 1;
117 do {
118 bitslice--;
119 bitcount += slicecount;
120 slicecount = 0;
122 if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
123 slicecount++;
124 else if (bitneed[ch][sb] == bitslice + 1)
125 slicecount += 2;
126 }
127 } while (bitcount + slicecount < frame->bitpool);
128
129 if (bitcount + slicecount ==
frame->bitpool) {
130 bitcount += slicecount;
131 bitslice--;
132 }
133
135 if (bitneed[ch][sb] < bitslice + 2)
137 else {
138 bits[ch][sb] = bitneed[ch][sb] - bitslice;
139 if (
bits[ch][sb] > 16)
141 }
142 }
143
144 for (sb = 0; bitcount <
frame->bitpool &&
146 if ((
bits[ch][sb] >= 2) && (
bits[ch][sb] < 16)) {
148 bitcount++;
149 }
else if ((bitneed[ch][sb] == bitslice + 1) && (
frame->bitpool > bitcount + 1)) {
151 bitcount += 2;
152 }
153 }
154
155 for (sb = 0; bitcount <
frame->bitpool &&
157 if (
bits[ch][sb] < 16) {
159 bitcount++;
160 }
161 }
162
163 }
164
166 int bitneed[2][8],
loudness, max_bitneed, bitcount, slicecount, bitslice;
167 int ch, sb;
168
169 max_bitneed = 0;
170 if (
frame->allocation == SNR) {
171 for (ch = 0; ch < 2; ch++) {
173 bitneed[ch][sb] =
frame->scale_factor[ch][sb];
174 if (bitneed[ch][sb] > max_bitneed)
175 max_bitneed = bitneed[ch][sb];
176 }
177 }
178 } else {
179 for (ch = 0; ch < 2; ch++) {
181 if (
frame->scale_factor[ch][sb] == 0)
182 bitneed[ch][sb] = -5;
183 else {
186 else
190 else
192 }
193 if (bitneed[ch][sb] > max_bitneed)
194 max_bitneed = bitneed[ch][sb];
195 }
196 }
197 }
198
199 bitcount = 0;
200 slicecount = 0;
201 bitslice = max_bitneed + 1;
202 do {
203 bitslice--;
204 bitcount += slicecount;
205 slicecount = 0;
206 for (ch = 0; ch < 2; ch++) {
208 if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
209 slicecount++;
210 else if (bitneed[ch][sb] == bitslice + 1)
211 slicecount += 2;
212 }
213 }
214 } while (bitcount + slicecount < frame->bitpool);
215
216 if (bitcount + slicecount ==
frame->bitpool) {
217 bitcount += slicecount;
218 bitslice--;
219 }
220
221 for (ch = 0; ch < 2; ch++) {
223 if (bitneed[ch][sb] < bitslice + 2) {
225 } else {
226 bits[ch][sb] = bitneed[ch][sb] - bitslice;
227 if (
bits[ch][sb] > 16)
229 }
230 }
231 }
232
233 ch = 0;
234 sb = 0;
235 while (bitcount < frame->bitpool) {
236 if ((
bits[ch][sb] >= 2) && (
bits[ch][sb] < 16)) {
238 bitcount++;
239 }
else if ((bitneed[ch][sb] == bitslice + 1) && (
frame->bitpool > bitcount + 1)) {
241 bitcount += 2;
242 }
243 if (ch == 1) {
244 ch = 0;
245 sb++;
247 break;
248 } else
249 ch = 1;
250 }
251
252 ch = 0;
253 sb = 0;
254 while (bitcount < frame->bitpool) {
255 if (
bits[ch][sb] < 16) {
257 bitcount++;
258 }
259 if (ch == 1) {
260 ch = 0;
261 sb++;
263 break;
264 } else
265 ch = 1;
266 }
267
268 }
269
270 }