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
42
50
52 {
54 int16_t *x;
55
56 switch (
frame->subbands) {
57 case 4:
58 for (ch = 0; ch <
frame->channels; ch++) {
59 x = &
s->X[ch][
s->position - 4 *
60 s->increment +
frame->blocks * 4];
62 blk +=
s->increment) {
68 x -= 4 *
s->increment;
69 }
70 }
71 return frame->blocks * 4;
72
73 case 8:
74 for (ch = 0; ch <
frame->channels; ch++) {
75 x = &
s->X[ch][
s->position - 8 *
76 s->increment +
frame->blocks * 8];
78 blk +=
s->increment) {
84 x -= 8 *
s->increment;
85 }
86 }
87 return frame->blocks * 8;
88
89 default:
91 }
92 }
93
94 /*
95 * Packs the SBC frame from frame into the memory in avpkt.
96 * Returns the length of the packed frame.
97 */
99 int joint, int msbc)
100 {
102
103 /* Will copy the header parts for CRC-8 calculation here */
104 uint8_t crc_header[11] = { 0 };
105 int crc_pos;
106
107 uint32_t audio_sample;
108
109 int ch, sb,
blk;
/* channel, subband, block and bit counters */
110 int bits[2][8];
/* bits distribution */
111 uint32_t levels[2][8]; /* levels are derived from that */
112 uint32_t sb_sample_delta[2][8];
113
114 if (msbc) {
118 } else {
120
121 avpkt->
data[1] = (
frame->frequency & 0x03) << 6;
122 avpkt->
data[1] |= (((
frame->blocks >> 2) - 1) & 0x03) << 4;
123 avpkt->
data[1] |= (
frame->mode & 0x03) << 2;
124 avpkt->
data[1] |= (
frame->allocation & 0x01) << 1;
125 avpkt->
data[1] |= ((
frame->subbands == 8) & 0x01) << 0;
126
128
131 return -5;
132 }
133
134 /* Can't fill in crc yet */
135 crc_header[0] = avpkt->
data[1];
136 crc_header[1] = avpkt->
data[2];
137 crc_pos = 16;
138
140
143 crc_header[crc_pos >> 3] = joint;
144 crc_pos +=
frame->subbands;
145 }
146
147 for (ch = 0; ch <
frame->channels; ch++) {
148 for (sb = 0; sb <
frame->subbands; sb++) {
150 crc_header[crc_pos >> 3] <<= 4;
151 crc_header[crc_pos >> 3] |=
frame->scale_factor[ch][sb] & 0x0F;
152 crc_pos += 4;
153 }
154 }
155
156 /* align the last crc byte */
157 if (crc_pos % 8)
158 crc_header[crc_pos >> 3] <<= 8 - (crc_pos % 8);
159
161
163
164 for (ch = 0; ch <
frame->channels; ch++) {
165 for (sb = 0; sb <
frame->subbands; sb++) {
166 levels[ch][sb] = ((1 <<
bits[ch][sb]) - 1) <<
167 (32 - (
frame->scale_factor[ch][sb] +
169 sb_sample_delta[ch][sb] = (uint32_t) 1 <<
170 (
frame->scale_factor[ch][sb] +
172 }
173 }
174
176 for (ch = 0; ch <
frame->channels; ch++) {
177 for (sb = 0; sb <
frame->subbands; sb++) {
178
179 if (
bits[ch][sb] == 0)
180 continue;
181
182 audio_sample = ((uint64_t) levels[ch][sb] *
183 (sb_sample_delta[ch][sb] +
184 frame->sb_sample_f[
blk][ch][sb])) >> 32;
185
187 }
188 }
189 }
190
192
194 }
195
197 {
200
203
208 }
209
213 }
214
220
222 } else {
224
228 }
229
234 else
236 } else {
239 else
243 else
245 }
246 /* sbc algorithmic delay is ((blocks + 10) * subbands - 2) / sample_rate */
248 / (1000000 *
frame->subbands)) - 10, 4, 16) & ~3;
249
251
258
260 }
261
265
269
270 memset(&sbc->
dsp.X, 0,
sizeof(sbc->
dsp.X));
272 sbc->
dsp.increment = sbc->
msbc ? 1 : 4;
274
275 return 0;
276 }
277
279 const AVFrame *av_frame,
int *got_packet_ptr)
280 {
286
287 int frame_length = 4 + (4 *
frame->subbands *
frame->channels) / 8
288 + ((
frame->blocks *
frame->bitpool * (1 + dual)
290
291 /* input must be large enough to encode a complete frame */
293 return 0;
294
297
298 /* Select the needed input data processing function and call it */
299 if (
frame->subbands == 8)
300 sbc->
dsp.position = sbc->
dsp.sbc_enc_process_input_8s(
301 sbc->
dsp.position, av_frame->
data[0], sbc->
dsp.X,
303 else
304 sbc->
dsp.position = sbc->
dsp.sbc_enc_process_input_4s(
305 sbc->
dsp.position, av_frame->
data[0], sbc->
dsp.X,
307
309
311 j = sbc->
dsp.sbc_calc_scalefactors_j(
frame->sb_sample_f,
315 else
316 sbc->
dsp.sbc_calc_scalefactors(
frame->sb_sample_f,
321 emms_c();
323
324 *got_packet_ptr = 1;
325 return 0;
326 }
327
328 #define OFFSET(x) offsetof(SBCEncContext, x)
329 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
331 { "sbc_delay", "set maximum algorithmic latency",
333 { "msbc", "use mSBC mode (wideband speech mono SBC)",
337 };
338
344 };
345
356 #if FF_API_OLD_CHANNEL_LAYOUT
359 #endif
362 { 0 } },
365 .p.supported_samplerates = (const int[]) { 16000, 32000, 44100, 48000, 0 },
368 };