FFmpeg
Loading...
Searching...
No Matches
sbcdec.c
Go to the documentation of this file.
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
33#include "avcodec.h"
34#include "codec_internal.h"
35#include "decode.h"
39#include "sbc.h"
40#include "sbcdec_data.h"
41
43 int32_t V[2][170];
44 int offset[2][16];
45};
46
51
52/*
53 * Unpacks a SBC frame at the beginning of the stream in data,
54 * which has at most len bytes into frame.
55 * Returns the length in bytes of the packed frame, or a negative
56 * value on error. The error codes are:
57 *
58 * -1 Data stream too short
59 * -2 Sync byte incorrect
60 * -3 CRC8 incorrect
61 * -4 Bitpool value out of bounds
62 */
63static int sbc_unpack_frame(const uint8_t *data, struct sbc_frame *frame,
64 size_t len)
65{
66 unsigned int consumed;
67 /* Will copy the parts of the header that are relevant to crc
68 * calculation here */
69 uint8_t crc_header[11] = { 0 };
70 int crc_pos;
72
73 uint32_t audio_sample;
74 int ch, sb, blk, bit; /* channel, subband, block and bit standard
75 counters */
76 int bits[2][8]; /* bits distribution */
77 uint32_t levels[2][8]; /* levels derived from that */
78
79 if (len < 4)
80 return -1;
81
82 if (data[0] == MSBC_SYNCWORD) {
83 if (data[1] != 0)
84 return -2;
85 if (data[2] != 0)
86 return -2;
87
88 frame->frequency = SBC_FREQ_16000;
89 frame->blocks = MSBC_BLOCKS;
90 frame->allocation = LOUDNESS;
91 frame->mode = MONO;
92 frame->channels = 1;
93 frame->subbands = 8;
94 frame->bitpool = 26;
95 } else if (data[0] == SBC_SYNCWORD) {
96 frame->frequency = (data[1] >> 6) & 0x03;
97 frame->blocks = 4 * ((data[1] >> 4) & 0x03) + 4;
98 frame->mode = (data[1] >> 2) & 0x03;
99 frame->channels = frame->mode == MONO ? 1 : 2;
100 frame->allocation = (data[1] >> 1) & 0x01;
101 frame->subbands = data[1] & 0x01 ? 8 : 4;
102 frame->bitpool = data[2];
103
104 if ((frame->mode == MONO || frame->mode == DUAL_CHANNEL) &&
105 frame->bitpool > 16 * frame->subbands)
106 return -4;
107
108 if ((frame->mode == STEREO || frame->mode == JOINT_STEREO) &&
109 frame->bitpool > 32 * frame->subbands)
110 return -4;
111 } else
112 return -2;
113
114 consumed = 32;
115 crc_header[0] = data[1];
116 crc_header[1] = data[2];
117 crc_pos = 16;
118
119 if (frame->mode == JOINT_STEREO) {
120 if (len * 8 < consumed + frame->subbands)
121 return -1;
122
123 frame->joint = 0x00;
124 for (sb = 0; sb < frame->subbands - 1; sb++)
125 frame->joint |= ((data[4] >> (7 - sb)) & 0x01) << sb;
126 if (frame->subbands == 4)
127 crc_header[crc_pos / 8] = data[4] & 0xf0;
128 else
129 crc_header[crc_pos / 8] = data[4];
130
131 consumed += frame->subbands;
132 crc_pos += frame->subbands;
133 }
134
135 if (len * 8 < consumed + (4 * frame->subbands * frame->channels))
136 return -1;
137
138 for (ch = 0; ch < frame->channels; ch++) {
139 for (sb = 0; sb < frame->subbands; sb++) {
140 /* FIXME assert(consumed % 4 == 0); */
141 frame->scale_factor[ch][sb] =
142 (data[consumed >> 3] >> (4 - (consumed & 0x7))) & 0x0F;
143 crc_header[crc_pos >> 3] |=
144 frame->scale_factor[ch][sb] << (4 - (crc_pos & 0x7));
145
146 consumed += 4;
147 crc_pos += 4;
148 }
149 }
150
151 if (data[3] != ff_sbc_crc8(frame->crc_ctx, crc_header, crc_pos))
152 return -3;
153
155
156 for (ch = 0; ch < frame->channels; ch++) {
157 for (sb = 0; sb < frame->subbands; sb++)
158 levels[ch][sb] = (1 << bits[ch][sb]) - 1;
159 }
160
161 for (blk = 0; blk < frame->blocks; blk++) {
162 for (ch = 0; ch < frame->channels; ch++) {
163 for (sb = 0; sb < frame->subbands; sb++) {
164 uint32_t shift;
165
166 if (levels[ch][sb] == 0) {
167 frame->sb_sample[blk][ch][sb] = 0;
168 continue;
169 }
170
171 shift = frame->scale_factor[ch][sb] +
173
174 audio_sample = 0;
175 for (bit = 0; bit < bits[ch][sb]; bit++) {
176 if (consumed > len * 8)
177 return -1;
178
179 if ((data[consumed >> 3] >> (7 - (consumed & 0x7))) & 0x01)
180 audio_sample |= 1 << (bits[ch][sb] - bit - 1);
181
182 consumed++;
183 }
184
185 frame->sb_sample[blk][ch][sb] = (int32_t)
186 (((((uint64_t) audio_sample << 1) | 1) << shift) /
187 levels[ch][sb]) - (1 << shift);
188 }
189 }
190 }
191
192 if (frame->mode == JOINT_STEREO) {
193 for (blk = 0; blk < frame->blocks; blk++) {
194 for (sb = 0; sb < frame->subbands; sb++) {
195 if (frame->joint & (0x01 << sb)) {
196 temp = frame->sb_sample[blk][0][sb] +
197 frame->sb_sample[blk][1][sb];
198 frame->sb_sample[blk][1][sb] =
199 frame->sb_sample[blk][0][sb] -
200 frame->sb_sample[blk][1][sb];
201 frame->sb_sample[blk][0][sb] = temp;
202 }
203 }
204 }
205 }
206
207 if ((consumed & 0x7) != 0)
208 consumed += 8 - (consumed & 0x7);
209
210 return consumed >> 3;
211}
212
213static inline void sbc_synthesize_four(struct sbc_decoder_state *state,
214 struct sbc_frame *frame,
215 int ch, int blk, AVFrame *output_frame)
216{
217 int i, k, idx;
218 int32_t *v = state->V[ch];
219 int *offset = state->offset[ch];
220
221 for (i = 0; i < 8; i++) {
222 /* Shifting */
223 offset[i]--;
224 if (offset[i] < 0) {
225 offset[i] = 79;
226 memcpy(v + 80, v, 9 * sizeof(*v));
227 }
228
229 /* Distribute the new matrix value to the shifted position */
230 v[offset[i]] =
231 (int)( (unsigned)synmatrix4[i][0] * frame->sb_sample[blk][ch][0] +
232 (unsigned)synmatrix4[i][1] * frame->sb_sample[blk][ch][1] +
233 (unsigned)synmatrix4[i][2] * frame->sb_sample[blk][ch][2] +
234 (unsigned)synmatrix4[i][3] * frame->sb_sample[blk][ch][3] ) >> 15;
235 }
236
237 /* Compute the samples */
238 for (idx = 0, i = 0; i < 4; i++, idx += 5) {
239 k = (i + 4) & 0xf;
240
241 /* Store in output, Q0 */
242 AV_WN16A(&output_frame->data[ch][blk * 8 + i * 2], av_clip_int16(
243 (int)( (unsigned)v[offset[i] + 0] * sbc_proto_4_40m0[idx + 0] +
244 (unsigned)v[offset[k] + 1] * sbc_proto_4_40m1[idx + 0] +
245 (unsigned)v[offset[i] + 2] * sbc_proto_4_40m0[idx + 1] +
246 (unsigned)v[offset[k] + 3] * sbc_proto_4_40m1[idx + 1] +
247 (unsigned)v[offset[i] + 4] * sbc_proto_4_40m0[idx + 2] +
248 (unsigned)v[offset[k] + 5] * sbc_proto_4_40m1[idx + 2] +
249 (unsigned)v[offset[i] + 6] * sbc_proto_4_40m0[idx + 3] +
250 (unsigned)v[offset[k] + 7] * sbc_proto_4_40m1[idx + 3] +
251 (unsigned)v[offset[i] + 8] * sbc_proto_4_40m0[idx + 4] +
252 (unsigned)v[offset[k] + 9] * sbc_proto_4_40m1[idx + 4] ) >> 15));
253 }
254}
255
257 struct sbc_frame *frame,
258 int ch, int blk, AVFrame *output_frame)
259{
260 int i, k, idx;
261 int32_t *v = state->V[ch];
262 int *offset = state->offset[ch];
263
264 for (i = 0; i < 16; i++) {
265 /* Shifting */
266 offset[i]--;
267 if (offset[i] < 0) {
268 offset[i] = 159;
269 memcpy(v + 160, v, 9 * sizeof(*v));
270 }
271
272 /* Distribute the new matrix value to the shifted position */
273 v[offset[i]] =
274 (int)( (unsigned)synmatrix8[i][0] * frame->sb_sample[blk][ch][0] +
275 (unsigned)synmatrix8[i][1] * frame->sb_sample[blk][ch][1] +
276 (unsigned)synmatrix8[i][2] * frame->sb_sample[blk][ch][2] +
277 (unsigned)synmatrix8[i][3] * frame->sb_sample[blk][ch][3] +
278 (unsigned)synmatrix8[i][4] * frame->sb_sample[blk][ch][4] +
279 (unsigned)synmatrix8[i][5] * frame->sb_sample[blk][ch][5] +
280 (unsigned)synmatrix8[i][6] * frame->sb_sample[blk][ch][6] +
281 (unsigned)synmatrix8[i][7] * frame->sb_sample[blk][ch][7] ) >> 15;
282 }
283
284 /* Compute the samples */
285 for (idx = 0, i = 0; i < 8; i++, idx += 5) {
286 k = (i + 8) & 0xf;
287
288 /* Store in output, Q0 */
289 AV_WN16A(&output_frame->data[ch][blk * 16 + i * 2], av_clip_int16(
290 (int)( (unsigned)v[offset[i] + 0] * sbc_proto_8_80m0[idx + 0] +
291 (unsigned)v[offset[k] + 1] * sbc_proto_8_80m1[idx + 0] +
292 (unsigned)v[offset[i] + 2] * sbc_proto_8_80m0[idx + 1] +
293 (unsigned)v[offset[k] + 3] * sbc_proto_8_80m1[idx + 1] +
294 (unsigned)v[offset[i] + 4] * sbc_proto_8_80m0[idx + 2] +
295 (unsigned)v[offset[k] + 5] * sbc_proto_8_80m1[idx + 2] +
296 (unsigned)v[offset[i] + 6] * sbc_proto_8_80m0[idx + 3] +
297 (unsigned)v[offset[k] + 7] * sbc_proto_8_80m1[idx + 3] +
298 (unsigned)v[offset[i] + 8] * sbc_proto_8_80m0[idx + 4] +
299 (unsigned)v[offset[k] + 9] * sbc_proto_8_80m1[idx + 4] ) >> 15));
300 }
301}
302
305{
306 int ch, blk;
307
308 switch (frame->subbands) {
309 case 4:
310 for (ch = 0; ch < frame->channels; ch++)
311 for (blk = 0; blk < frame->blocks; blk++)
313 break;
314
315 case 8:
316 for (ch = 0; ch < frame->channels; ch++)
317 for (blk = 0; blk < frame->blocks; blk++)
319 break;
320 }
321}
322
324{
325 SBCDecContext *sbc = avctx->priv_data;
326 int i, ch;
327
329
331
332 memset(sbc->dsp.V, 0, sizeof(sbc->dsp.V));
333 for (ch = 0; ch < 2; ch++)
334 for (i = 0; i < FF_ARRAY_ELEMS(sbc->dsp.offset[0]); i++)
335 sbc->dsp.offset[ch][i] = (10 * i + 10);
336 return 0;
337}
338
340 int *got_frame_ptr, AVPacket *avpkt)
341{
342 SBCDecContext *sbc = avctx->priv_data;
343 int ret, frame_length;
344
345 frame_length = sbc_unpack_frame(avpkt->data, &sbc->frame, avpkt->size);
346 if (frame_length <= 0)
347 return frame_length;
348
351 avctx->ch_layout.nb_channels = sbc->frame.channels;
352
353 frame->nb_samples = sbc->frame.blocks * sbc->frame.subbands;
354 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
355 return ret;
356
357 sbc_synthesize_audio(&sbc->dsp, &sbc->frame, frame);
358
359 *got_frame_ptr = 1;
360
361 return frame_length;
362}
363
365 .p.name = "sbc",
366 CODEC_LONG_NAME("SBC (low-complexity subband codec)"),
367 .p.type = AVMEDIA_TYPE_AUDIO,
368 .p.id = AV_CODEC_ID_SBC,
369 .priv_data_size = sizeof(SBCDecContext),
373};
const FFCodec ff_sbc_decoder
Definition sbcdec.c:364
#define JOINT_STEREO
Definition atrac3.c:59
int32_t
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define bit(string, value)
Definition cbs_mpeg2.c:56
Public libavutil channel layout APIs header.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define av_clip_int16
Definition common.h:115
#define STEREO
Definition cook.c:65
#define MONO
Definition cook.c:64
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static struct @346255127015250356166251341105367306144006377143 state
#define LOUDNESS(energy)
Definition f_ebur128.c:520
static const uint8_t bits[8]
Definition fastaudio.c:100
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
@ AV_CODEC_ID_SBC
Definition codec_id.h:540
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition crc.c:389
@ AV_CRC_8_EBU
Definition crc.h:56
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition samplefmt.h:64
#define AV_WN16A(p, v)
unsigned offset
Definition libaomenc.c:763
static int shift(int a, int b)
Definition bonk.c:261
static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
Definition h264dec.c:860
static int sbc_unpack_frame(const uint8_t *data, struct sbc_frame *frame, size_t len)
Definition sbcdec.c:63
static void sbc_synthesize_four(struct sbc_decoder_state *state, struct sbc_frame *frame, int ch, int blk, AVFrame *output_frame)
Definition sbcdec.c:213
static av_cold int sbc_decode_init(AVCodecContext *avctx)
Definition sbcdec.c:323
static void sbc_synthesize_audio(struct sbc_decoder_state *state, struct sbc_frame *frame, AVFrame *output_frame)
Definition sbcdec.c:303
static void sbc_synthesize_eight(struct sbc_decoder_state *state, struct sbc_frame *frame, int ch, int blk, AVFrame *output_frame)
Definition sbcdec.c:256
static int sbc_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition sbcdec.c:339
#define av_cold
Definition attributes.h:117
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
const char data[16]
Definition mxf.c:149
uint8_t ff_sbc_crc8(const AVCRC *ctx, const uint8_t *data, size_t len)
Definition sbc.c:54
void ff_sbc_calculate_bits(const struct sbc_frame *frame, int(*bits)[8])
Definition sbc.c:78
SBC common definitions for the encoder and decoder.
#define SBC_ALIGN
Definition sbc.h:80
#define SBCDEC_FIXED_EXTRA_BITS
Definition sbc.h:74
#define MSBC_BLOCKS
Definition sbc.h:41
#define SBC_SYNCWORD
Definition sbc.h:70
#define SBC_FREQ_16000
Definition sbc.h:44
#define MSBC_SYNCWORD
Definition sbc.h:71
SBC decoder tables.
static const int32_t synmatrix8[16][8]
Definition sbcdec_data.h:96
static const int32_t sbc_proto_4_40m1[]
Definition sbcdec_data.h:51
static const int32_t sbc_proto_8_80m1[]
Definition sbcdec_data.h:72
static const int32_t sbc_proto_8_80m0[]
Definition sbcdec_data.h:59
static const int32_t sbc_proto_4_40m0[]
Definition sbcdec_data.h:43
static const int32_t synmatrix4[8][4]
Definition sbcdec_data.h:85
#define blk(i)
Definition sha.c:55
#define FF_ARRAY_ELEMS(a)
enum AVChannelOrder order
Channel order used in this layout.
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
struct sbc_decoder_state dsp
Definition sbcdec.c:49
struct sbc_frame frame
Definition sbcdec.c:48
int32_t V[2][170]
Definition sbcdec.c:43
int offset[2][16]
Definition sbcdec.c:44
uint8_t channels
Definition sbc.h:93
const AVCRC * crc_ctx
Definition sbc.h:105
uint8_t subbands
Definition sbc.h:98
uint8_t blocks
Definition sbc.h:86
else temp
Definition vf_mcdeint.c:275
int len