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 encoder implementation
31 */
32
43
51
53 {
55 int16_t *x;
56
57 switch (
frame->subbands) {
58 case 4:
59 for (ch = 0; ch <
frame->channels; ch++) {
60 x = &
s->X[ch][
s->position - 4 *
61 s->increment +
frame->blocks * 4];
63 blk +=
s->increment) {
69 x -= 4 *
s->increment;
70 }
71 }
72 return frame->blocks * 4;
73
74 case 8:
75 for (ch = 0; ch <
frame->channels; ch++) {
76 x = &
s->X[ch][
s->position - 8 *
77 s->increment +
frame->blocks * 8];
79 blk +=
s->increment) {
85 x -= 8 *
s->increment;
86 }
87 }
88 return frame->blocks * 8;
89
90 default:
92 }
93 }
94
95 /*
96 * Packs the SBC frame from frame into the memory in avpkt.
97 * Returns the length of the packed frame.
98 */
100 int joint, int msbc)
101 {
103
104 /* Will copy the header parts for CRC-8 calculation here */
105 uint8_t crc_header[11] = { 0 };
106 int crc_pos;
107
108 uint32_t audio_sample;
109
110 int ch, sb,
blk;
/* channel, subband, block and bit counters */
111 int bits[2][8];
/* bits distribution */
112 uint32_t levels[2][8]; /* levels are derived from that */
113 uint32_t sb_sample_delta[2][8];
114
115 if (msbc) {
119 } else {
121
122 avpkt->
data[1] = (
frame->frequency & 0x03) << 6;
123 avpkt->
data[1] |= (((
frame->blocks >> 2) - 1) & 0x03) << 4;
124 avpkt->
data[1] |= (
frame->mode & 0x03) << 2;
125 avpkt->
data[1] |= (
frame->allocation & 0x01) << 1;
126 avpkt->
data[1] |= ((
frame->subbands == 8) & 0x01) << 0;
127
129
132 return -5;
133 }
134
135 /* Can't fill in crc yet */
136 crc_header[0] = avpkt->
data[1];
137 crc_header[1] = avpkt->
data[2];
138 crc_pos = 16;
139
141
144 crc_header[crc_pos >> 3] = joint;
145 crc_pos +=
frame->subbands;
146 }
147
148 for (ch = 0; ch <
frame->channels; ch++) {
149 for (sb = 0; sb <
frame->subbands; sb++) {
151 crc_header[crc_pos >> 3] <<= 4;
152 crc_header[crc_pos >> 3] |=
frame->scale_factor[ch][sb] & 0x0F;
153 crc_pos += 4;
154 }
155 }
156
157 /* align the last crc byte */
158 if (crc_pos % 8)
159 crc_header[crc_pos >> 3] <<= 8 - (crc_pos % 8);
160
162
164
165 for (ch = 0; ch <
frame->channels; ch++) {
166 for (sb = 0; sb <
frame->subbands; sb++) {
167 levels[ch][sb] = ((1 <<
bits[ch][sb]) - 1) <<
168 (32 - (
frame->scale_factor[ch][sb] +
170 sb_sample_delta[ch][sb] = (uint32_t) 1 <<
171 (
frame->scale_factor[ch][sb] +
173 }
174 }
175
177 for (ch = 0; ch <
frame->channels; ch++) {
178 for (sb = 0; sb <
frame->subbands; sb++) {
179
180 if (
bits[ch][sb] == 0)
181 continue;
182
183 audio_sample = ((uint64_t) levels[ch][sb] *
184 (sb_sample_delta[ch][sb] +
185 frame->sb_sample_f[
blk][ch][sb])) >> 32;
186
188 }
189 }
190 }
191
193
195 }
196
198 {
201
204
209 }
210
214 }
215
221
223 } else {
224 int d;
225
229 }
230
235 else
237 } else {
240 else
244 else
246 }
247 /* sbc algorithmic delay is ((blocks + 10) * subbands - 2) / sample_rate */
249 / (1000000 *
frame->subbands)) - 10, 4, 16) & ~3;
250
252
259
261 }
262
266
270
271 memset(&sbc->
dsp.X, 0,
sizeof(sbc->
dsp.X));
273 sbc->
dsp.increment = sbc->
msbc ? 1 : 4;
275
276 return 0;
277 }
278
280 const AVFrame *av_frame,
int *got_packet_ptr)
281 {
287
288 int frame_length = 4 + (4 *
frame->subbands *
frame->channels) / 8
289 + ((
frame->blocks *
frame->bitpool * (1 + dual)
291
292 /* input must be large enough to encode a complete frame */
294 return 0;
295
298
299 /* Select the needed input data processing function and call it */
300 if (
frame->subbands == 8)
301 sbc->
dsp.position = sbc->
dsp.sbc_enc_process_input_8s(
302 sbc->
dsp.position, av_frame->
data[0], sbc->
dsp.X,
304 else
305 sbc->
dsp.position = sbc->
dsp.sbc_enc_process_input_4s(
306 sbc->
dsp.position, av_frame->
data[0], sbc->
dsp.X,
308
310
312 j = sbc->
dsp.sbc_calc_scalefactors_j(
frame->sb_sample_f,
316 else
317 sbc->
dsp.sbc_calc_scalefactors(
frame->sb_sample_f,
324
325 *got_packet_ptr = 1;
326 return 0;
327 }
328
329 #define OFFSET(x) offsetof(SBCEncContext, x)
330 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
332 { "sbc_delay", "set maximum algorithmic latency",
334 { "msbc", "use mSBC mode (wideband speech mono SBC)",
338 };
339
345 };
346
359 { 0 } },
362 .p.supported_samplerates = (const int[]) { 16000, 32000, 44100, 48000, 0 },
365 };