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 decoder implementation
31 */
32
41
45 };
46
52
53 /*
54 * Unpacks a SBC frame at the beginning of the stream in data,
55 * which has at most len bytes into frame.
56 * Returns the length in bytes of the packed frame, or a negative
57 * value on error. The error codes are:
58 *
59 * -1 Data stream too short
60 * -2 Sync byte incorrect
61 * -3 CRC8 incorrect
62 * -4 Bitpool value out of bounds
63 */
66 {
67 unsigned int consumed;
68 /* Will copy the parts of the header that are relevant to crc
69 * calculation here */
70 uint8_t crc_header[11] = { 0 };
71 int crc_pos;
73
74 uint32_t audio_sample;
75 int ch, sb,
blk,
bit;
/* channel, subband, block and bit standard
76 counters */
77 int bits[2][8];
/* bits distribution */
78 uint32_t levels[2][8]; /* levels derived from that */
79
81 return -1;
82
85 return -2;
87 return -2;
88
98 frame->blocks = 4 * ((
data[1] >> 4) & 0x03) + 4;
101 frame->allocation = (
data[1] >> 1) & 0x01;
102 frame->subbands =
data[1] & 0x01 ? 8 : 4;
104
107 return -4;
108
111 return -4;
112 } else
113 return -2;
114
115 consumed = 32;
116 crc_header[0] =
data[1];
117 crc_header[1] =
data[2];
118 crc_pos = 16;
119
121 if (
len * 8 < consumed +
frame->subbands)
122 return -1;
123
125 for (sb = 0; sb <
frame->subbands - 1; sb++)
126 frame->joint |= ((
data[4] >> (7 - sb)) & 0x01) << sb;
127 if (
frame->subbands == 4)
128 crc_header[crc_pos / 8] =
data[4] & 0xf0;
129 else
130 crc_header[crc_pos / 8] =
data[4];
131
132 consumed +=
frame->subbands;
133 crc_pos +=
frame->subbands;
134 }
135
136 if (
len * 8 < consumed + (4 *
frame->subbands *
frame->channels))
137 return -1;
138
139 for (ch = 0; ch <
frame->channels; ch++) {
140 for (sb = 0; sb <
frame->subbands; sb++) {
141 /* FIXME assert(consumed % 4 == 0); */
142 frame->scale_factor[ch][sb] =
143 (
data[consumed >> 3] >> (4 - (consumed & 0x7))) & 0x0F;
144 crc_header[crc_pos >> 3] |=
145 frame->scale_factor[ch][sb] << (4 - (crc_pos & 0x7));
146
147 consumed += 4;
148 crc_pos += 4;
149 }
150 }
151
153 return -3;
154
156
157 for (ch = 0; ch <
frame->channels; ch++) {
158 for (sb = 0; sb <
frame->subbands; sb++)
159 levels[ch][sb] = (1 <<
bits[ch][sb]) - 1;
160 }
161
163 for (ch = 0; ch <
frame->channels; ch++) {
164 for (sb = 0; sb <
frame->subbands; sb++) {
166
167 if (levels[ch][sb] == 0) {
169 continue;
170 }
171
174
175 audio_sample = 0;
177 if (consumed >
len * 8)
178 return -1;
179
180 if ((
data[consumed >> 3] >> (7 - (consumed & 0x7))) & 0x01)
181 audio_sample |= 1 << (
bits[ch][sb] -
bit - 1);
182
183 consumed++;
184 }
185
187 (((((uint64_t) audio_sample << 1) | 1) <<
shift) /
188 levels[ch][sb]) - (1 <<
shift);
189 }
190 }
191 }
192
195 for (sb = 0; sb <
frame->subbands; sb++) {
196 if (
frame->joint & (0x01 << sb)) {
203 }
204 }
205 }
206 }
207
208 if ((consumed & 0x7) != 0)
209 consumed += 8 - (consumed & 0x7);
210
211 return consumed >> 3;
212 }
213
217 {
221
222 for (
i = 0;
i < 8;
i++) {
223 /* Shifting */
227 memcpy(v + 80, v, 9 * sizeof(*v));
228 }
229
230 /* Distribute the new matrix value to the shifted position */
236 }
237
238 /* Compute the samples */
239 for (idx = 0,
i = 0;
i < 4;
i++, idx += 5) {
241
242 /* Store in output, Q0 */
254 }
255 }
256
260 {
264
265 for (
i = 0;
i < 16;
i++) {
266 /* Shifting */
270 memcpy(v + 160, v, 9 * sizeof(*v));
271 }
272
273 /* Distribute the new matrix value to the shifted position */
283 }
284
285 /* Compute the samples */
286 for (idx = 0,
i = 0;
i < 8;
i++, idx += 5) {
288
289 /* Store in output, Q0 */
301 }
302 }
303
306 {
308
309 switch (
frame->subbands) {
310 case 4:
311 for (ch = 0; ch <
frame->channels; ch++)
314 break;
315
316 case 8:
317 for (ch = 0; ch <
frame->channels; ch++)
320 break;
321 }
322 }
323
325 {
328
330
332
333 memset(sbc->
dsp.
V, 0,
sizeof(sbc->
dsp.
V));
334 for (ch = 0; ch < 2; ch++)
337 return 0;
338 }
339
341 int *got_frame_ptr,
AVPacket *avpkt)
342 {
344 int ret, frame_length;
345
346 if (!sbc)
348
350 if (frame_length <= 0)
351 return frame_length;
352
356
360
362
363 *got_frame_ptr = 1;
364
365 return frame_length;
366 }
367
378 #if FF_API_OLD_CHANNEL_LAYOUT
381 #endif
384 { 0 } },
387 .p.supported_samplerates = (const int[]) { 16000, 32000, 44100, 48000, 0 },
388 };