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
33 #include "avcodec.h"
34 #include "sbc.h"
35
36 /* A2DP specification: Appendix B, page 69 */
37 static const int sbc_offset4[4][4] = {
38 { -1, 0, 0, 0 },
39 { -2, 0, 0, 1 },
40 { -2, 0, 0, 1 },
41 { -2, 0, 0, 1 }
42 };
43
44 /* A2DP specification: Appendix B, page 69 */
45 static const int sbc_offset8[4][8] = {
46 { -2, 0, 0, 0, 0, 0, 0, 1 },
47 { -3, 0, 0, 0, 0, 0, 1, 2 },
48 { -4, 0, 0, 0, 0, 0, 1, 2 },
49 { -4, 0, 0, 0, 0, 0, 1, 2 }
50 };
51
52 /*
53 * Calculates the CRC-8 of the first len bits in data
54 */
ff_sbc_crc8(const AVCRC * ctx,const uint8_t * data,size_t len)55 uint8_t ff_sbc_crc8(const AVCRC *ctx, const uint8_t *data, size_t len)
56 {
57 size_t byte_length = len >> 3;
58 int bit_length = len & 7;
59 uint8_t crc;
60
61 crc = av_crc(ctx, 0x0F, data, byte_length);
62
63 if (bit_length) {
64 uint8_t bits = data[byte_length];
65 while (bit_length--) {
66 int8_t mask = bits ^ crc;
67 crc = (crc << 1) ^ ((mask >> 7) & 0x1D);
68 bits <<= 1;
69 }
70 }
71
72 return crc;
73 }
74
75 /*
76 * Code straight from the spec to calculate the bits array
77 * Takes a pointer to the frame in question and a pointer to the bits array
78 */
ff_sbc_calculate_bits(const struct sbc_frame * frame,int (* bits)[8])79 void ff_sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8])
80 {
81 int subbands = frame->subbands;
82 uint8_t sf = frame->frequency;
83
84 if (frame->mode == MONO || frame->mode == DUAL_CHANNEL) {
85 int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice;
86 int ch, sb;
87
88 for (ch = 0; ch < frame->channels; ch++) {
89 max_bitneed = 0;
90 if (frame->allocation == SNR) {
91 for (sb = 0; sb < subbands; sb++) {
92 bitneed[ch][sb] = frame->scale_factor[ch][sb];
93 if (bitneed[ch][sb] > max_bitneed)
94 max_bitneed = bitneed[ch][sb];
95 }
96 } else {
97 for (sb = 0; sb < subbands; sb++) {
98 if (frame->scale_factor[ch][sb] == 0)
99 bitneed[ch][sb] = -5;
100 else {
101 if (subbands == 4)
102 loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
103 else
104 loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
105 if (loudness > 0)
106 bitneed[ch][sb] = loudness / 2;
107 else
108 bitneed[ch][sb] = loudness;
109 }
110 if (bitneed[ch][sb] > max_bitneed)
111 max_bitneed = bitneed[ch][sb];
112 }
113 }
114
115 bitcount = 0;
116 slicecount = 0;
117 bitslice = max_bitneed + 1;
118 do {
119 bitslice--;
120 bitcount += slicecount;
121 slicecount = 0;
122 for (sb = 0; sb < subbands; sb++) {
123 if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
124 slicecount++;
125 else if (bitneed[ch][sb] == bitslice + 1)
126 slicecount += 2;
127 }
128 } while (bitcount + slicecount < frame->bitpool);
129
130 if (bitcount + slicecount == frame->bitpool) {
131 bitcount += slicecount;
132 bitslice--;
133 }
134
135 for (sb = 0; sb < subbands; sb++) {
136 if (bitneed[ch][sb] < bitslice + 2)
137 bits[ch][sb] = 0;
138 else {
139 bits[ch][sb] = bitneed[ch][sb] - bitslice;
140 if (bits[ch][sb] > 16)
141 bits[ch][sb] = 16;
142 }
143 }
144
145 for (sb = 0; bitcount < frame->bitpool &&
146 sb < subbands; sb++) {
147 if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) {
148 bits[ch][sb]++;
149 bitcount++;
150 } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) {
151 bits[ch][sb] = 2;
152 bitcount += 2;
153 }
154 }
155
156 for (sb = 0; bitcount < frame->bitpool &&
157 sb < subbands; sb++) {
158 if (bits[ch][sb] < 16) {
159 bits[ch][sb]++;
160 bitcount++;
161 }
162 }
163
164 }
165
166 } else if (frame->mode == STEREO || frame->mode == JOINT_STEREO) {
167 int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice;
168 int ch, sb;
169
170 max_bitneed = 0;
171 if (frame->allocation == SNR) {
172 for (ch = 0; ch < 2; ch++) {
173 for (sb = 0; sb < subbands; sb++) {
174 bitneed[ch][sb] = frame->scale_factor[ch][sb];
175 if (bitneed[ch][sb] > max_bitneed)
176 max_bitneed = bitneed[ch][sb];
177 }
178 }
179 } else {
180 for (ch = 0; ch < 2; ch++) {
181 for (sb = 0; sb < subbands; sb++) {
182 if (frame->scale_factor[ch][sb] == 0)
183 bitneed[ch][sb] = -5;
184 else {
185 if (subbands == 4)
186 loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
187 else
188 loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
189 if (loudness > 0)
190 bitneed[ch][sb] = loudness / 2;
191 else
192 bitneed[ch][sb] = loudness;
193 }
194 if (bitneed[ch][sb] > max_bitneed)
195 max_bitneed = bitneed[ch][sb];
196 }
197 }
198 }
199
200 bitcount = 0;
201 slicecount = 0;
202 bitslice = max_bitneed + 1;
203 do {
204 bitslice--;
205 bitcount += slicecount;
206 slicecount = 0;
207 for (ch = 0; ch < 2; ch++) {
208 for (sb = 0; sb < subbands; sb++) {
209 if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
210 slicecount++;
211 else if (bitneed[ch][sb] == bitslice + 1)
212 slicecount += 2;
213 }
214 }
215 } while (bitcount + slicecount < frame->bitpool);
216
217 if (bitcount + slicecount == frame->bitpool) {
218 bitcount += slicecount;
219 bitslice--;
220 }
221
222 for (ch = 0; ch < 2; ch++) {
223 for (sb = 0; sb < subbands; sb++) {
224 if (bitneed[ch][sb] < bitslice + 2) {
225 bits[ch][sb] = 0;
226 } else {
227 bits[ch][sb] = bitneed[ch][sb] - bitslice;
228 if (bits[ch][sb] > 16)
229 bits[ch][sb] = 16;
230 }
231 }
232 }
233
234 ch = 0;
235 sb = 0;
236 while (bitcount < frame->bitpool) {
237 if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) {
238 bits[ch][sb]++;
239 bitcount++;
240 } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) {
241 bits[ch][sb] = 2;
242 bitcount += 2;
243 }
244 if (ch == 1) {
245 ch = 0;
246 sb++;
247 if (sb >= subbands)
248 break;
249 } else
250 ch = 1;
251 }
252
253 ch = 0;
254 sb = 0;
255 while (bitcount < frame->bitpool) {
256 if (bits[ch][sb] < 16) {
257 bits[ch][sb]++;
258 bitcount++;
259 }
260 if (ch == 1) {
261 ch = 0;
262 sb++;
263 if (sb >= subbands)
264 break;
265 } else
266 ch = 1;
267 }
268
269 }
270
271 }
272