FFmpeg
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"
37 #include "libavutil/intreadwrite.h"
38 #include "libavutil/mem_internal.h"
39 #include "sbc.h"
40 #include "sbcdec_data.h"
41 
43  int32_t V[2][170];
44  int offset[2][16];
45 };
46 
47 typedef struct SBCDecContext {
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  */
63 static 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;
71  int32_t temp;
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 
213 static 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 
256 static inline void sbc_synthesize_eight(struct sbc_decoder_state *state,
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),
372  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
373 };
SBCDecContext::frame
struct sbc_frame frame
Definition: sbcdec.c:48
AV_CRC_8_EBU
@ AV_CRC_8_EBU
Definition: crc.h:56
MSBC_SYNCWORD
#define MSBC_SYNCWORD
Definition: sbc.h:71
JOINT_STEREO
#define JOINT_STEREO
Definition: atrac3.c:59
mem_internal.h
sbc_proto_8_80m1
static const int32_t sbc_proto_8_80m1[]
Definition: sbcdec_data.h:72
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
AVPacket::data
uint8_t * data
Definition: packet.h:588
sbc_proto_8_80m0
static const int32_t sbc_proto_8_80m0[]
Definition: sbcdec_data.h:59
data
const char data[16]
Definition: mxf.c:149
sbc_frame::subbands
uint8_t subbands
Definition: sbc.h:98
SBCDEC_FIXED_EXTRA_BITS
#define SBCDEC_FIXED_EXTRA_BITS
Definition: sbc.h:74
FFCodec
Definition: codec_internal.h:127
STEREO
#define STEREO
Definition: cook.c:65
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:324
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:329
sbc_synthesize_eight
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
MSBC_BLOCKS
#define MSBC_BLOCKS
Definition: sbc.h:41
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:56
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1051
synmatrix8
static const int32_t synmatrix8[16][8]
Definition: sbcdec_data.h:96
sbc_decoder_state::offset
int offset[2][16]
Definition: sbcdec.c:44
AV_CODEC_ID_SBC
@ AV_CODEC_ID_SBC
Definition: codec_id.h:547
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:111
state
static struct @568 state
sbc_decoder_state::V
int32_t V[2][170]
Definition: sbcdec.c:43
ff_sbc_decoder
const FFCodec ff_sbc_decoder
Definition: sbcdec.c:364
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:347
intreadwrite.h
sbc_unpack_frame
static int sbc_unpack_frame(const uint8_t *data, struct sbc_frame *frame, size_t len)
Definition: sbcdec.c:63
sbc_decode_init
static av_cold int sbc_decode_init(AVCodecContext *avctx)
Definition: sbcdec.c:323
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:201
AV_CHANNEL_ORDER_UNSPEC
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
Definition: channel_layout.h:119
bits
uint8_t bits
Definition: vp3data.h:128
decode.h
AV_WN16A
#define AV_WN16A(p, v)
Definition: intreadwrite.h:530
SBC_SYNCWORD
#define SBC_SYNCWORD
Definition: sbc.h:70
blk
#define blk(i)
Definition: sha.c:186
sbc_proto_4_40m1
static const int32_t sbc_proto_4_40m1[]
Definition: sbcdec_data.h:51
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:332
SBCDecContext
Definition: sbcdec.c:47
SBCDecContext::dsp
struct sbc_decoder_state dsp
Definition: sbcdec.c:49
av_clip_int16
#define av_clip_int16
Definition: common.h:115
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:91
SBC_ALIGN
#define SBC_ALIGN
Definition: sbc.h:80
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1747
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:550
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:589
codec_internal.h
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:104
shift
static int shift(int a, int b)
Definition: bonk.c:261
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1043
sbc_frame::channels
uint8_t channels
Definition: sbc.h:93
sbc_synthesize_audio
static void sbc_synthesize_audio(struct sbc_decoder_state *state, struct sbc_frame *frame, AVFrame *output_frame)
Definition: sbcdec.c:303
sbc_decode_frame
static int sbc_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: sbcdec.c:339
sbcdec_data.h
av_crc_get_table
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:389
output_frame
static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
Definition: h264dec.c:859
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
xf
#define xf(width, name, var, range_min, range_max, subs,...)
Definition: cbs_av1.c:622
synmatrix4
static const int32_t synmatrix4[8][4]
Definition: sbcdec_data.h:85
sbc.h
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
sbc_frame
Definition: sbc.h:84
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
len
int len
Definition: vorbis_enc_data.h:426
ff_sbc_crc8
uint8_t ff_sbc_crc8(const AVCRC *ctx, const uint8_t *data, size_t len)
Definition: sbc.c:54
avcodec.h
sbc_frame::crc_ctx
const AVCRC * crc_ctx
Definition: sbc.h:105
MONO
#define MONO
Definition: cook.c:64
SBC_FREQ_16000
#define SBC_FREQ_16000
Definition: sbc.h:44
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:265
AVCodecContext
main external API structure.
Definition: avcodec.h:439
channel_layout.h
LOUDNESS
#define LOUDNESS(energy)
Definition: f_ebur128.c:512
sbc_synthesize_four
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
sbc_frame::blocks
uint8_t blocks
Definition: sbc.h:86
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:442
temp
else temp
Definition: vf_mcdeint.c:271
ff_sbc_calculate_bits
void ff_sbc_calculate_bits(const struct sbc_frame *frame, int(*bits)[8])
Definition: sbc.c:78
AVPacket
This structure stores compressed data.
Definition: packet.h:565
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:466
int32_t
int32_t
Definition: audioconvert.c:56
sbc_decoder_state
Definition: sbcdec.c:42
sbc_proto_4_40m0
static const int32_t sbc_proto_4_40m0[]
Definition: sbcdec_data.h:43