FFmpeg
sbc.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 common functions for the encoder and decoder
31  */
32 
33 #include "sbc.h"
34 
35 /* A2DP specification: Appendix B, page 69 */
36 static const int sbc_offset4[4][4] = {
37  { -1, 0, 0, 0 },
38  { -2, 0, 0, 1 },
39  { -2, 0, 0, 1 },
40  { -2, 0, 0, 1 }
41 };
42 
43 /* A2DP specification: Appendix B, page 69 */
44 static const int sbc_offset8[4][8] = {
45  { -2, 0, 0, 0, 0, 0, 0, 1 },
46  { -3, 0, 0, 0, 0, 0, 1, 2 },
47  { -4, 0, 0, 0, 0, 0, 1, 2 },
48  { -4, 0, 0, 0, 0, 0, 1, 2 }
49 };
50 
51 /*
52  * Calculates the CRC-8 of the first len bits in data
53  */
54 uint8_t ff_sbc_crc8(const AVCRC *ctx, const uint8_t *data, size_t len)
55 {
56  size_t byte_length = len >> 3;
57  int bit_length = len & 7;
58  uint8_t crc;
59 
60  crc = av_crc(ctx, 0x0F, data, byte_length);
61 
62  if (bit_length) {
63  uint8_t bits = data[byte_length];
64  while (bit_length--) {
65  int8_t mask = bits ^ crc;
66  crc = (crc << 1) ^ ((mask >> 7) & 0x1D);
67  bits <<= 1;
68  }
69  }
70 
71  return crc;
72 }
73 
74 /*
75  * Code straight from the spec to calculate the bits array
76  * Takes a pointer to the frame in question and a pointer to the bits array
77  */
78 void ff_sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8])
79 {
80  int subbands = frame->subbands;
81  uint8_t sf = frame->frequency;
82 
83  if (frame->mode == MONO || frame->mode == DUAL_CHANNEL) {
84  int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice;
85  int ch, sb;
86 
87  for (ch = 0; ch < frame->channels; ch++) {
88  max_bitneed = 0;
89  if (frame->allocation == SNR) {
90  for (sb = 0; sb < subbands; sb++) {
91  bitneed[ch][sb] = frame->scale_factor[ch][sb];
92  if (bitneed[ch][sb] > max_bitneed)
93  max_bitneed = bitneed[ch][sb];
94  }
95  } else {
96  for (sb = 0; sb < subbands; sb++) {
97  if (frame->scale_factor[ch][sb] == 0)
98  bitneed[ch][sb] = -5;
99  else {
100  if (subbands == 4)
101  loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
102  else
103  loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
104  if (loudness > 0)
105  bitneed[ch][sb] = loudness / 2;
106  else
107  bitneed[ch][sb] = loudness;
108  }
109  if (bitneed[ch][sb] > max_bitneed)
110  max_bitneed = bitneed[ch][sb];
111  }
112  }
113 
114  bitcount = 0;
115  slicecount = 0;
116  bitslice = max_bitneed + 1;
117  do {
118  bitslice--;
119  bitcount += slicecount;
120  slicecount = 0;
121  for (sb = 0; sb < subbands; sb++) {
122  if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
123  slicecount++;
124  else if (bitneed[ch][sb] == bitslice + 1)
125  slicecount += 2;
126  }
127  } while (bitcount + slicecount < frame->bitpool);
128 
129  if (bitcount + slicecount == frame->bitpool) {
130  bitcount += slicecount;
131  bitslice--;
132  }
133 
134  for (sb = 0; sb < subbands; sb++) {
135  if (bitneed[ch][sb] < bitslice + 2)
136  bits[ch][sb] = 0;
137  else {
138  bits[ch][sb] = bitneed[ch][sb] - bitslice;
139  if (bits[ch][sb] > 16)
140  bits[ch][sb] = 16;
141  }
142  }
143 
144  for (sb = 0; bitcount < frame->bitpool &&
145  sb < subbands; sb++) {
146  if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) {
147  bits[ch][sb]++;
148  bitcount++;
149  } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) {
150  bits[ch][sb] = 2;
151  bitcount += 2;
152  }
153  }
154 
155  for (sb = 0; bitcount < frame->bitpool &&
156  sb < subbands; sb++) {
157  if (bits[ch][sb] < 16) {
158  bits[ch][sb]++;
159  bitcount++;
160  }
161  }
162 
163  }
164 
165  } else if (frame->mode == STEREO || frame->mode == JOINT_STEREO) {
166  int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice;
167  int ch, sb;
168 
169  max_bitneed = 0;
170  if (frame->allocation == SNR) {
171  for (ch = 0; ch < 2; ch++) {
172  for (sb = 0; sb < subbands; sb++) {
173  bitneed[ch][sb] = frame->scale_factor[ch][sb];
174  if (bitneed[ch][sb] > max_bitneed)
175  max_bitneed = bitneed[ch][sb];
176  }
177  }
178  } else {
179  for (ch = 0; ch < 2; ch++) {
180  for (sb = 0; sb < subbands; sb++) {
181  if (frame->scale_factor[ch][sb] == 0)
182  bitneed[ch][sb] = -5;
183  else {
184  if (subbands == 4)
185  loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
186  else
187  loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
188  if (loudness > 0)
189  bitneed[ch][sb] = loudness / 2;
190  else
191  bitneed[ch][sb] = loudness;
192  }
193  if (bitneed[ch][sb] > max_bitneed)
194  max_bitneed = bitneed[ch][sb];
195  }
196  }
197  }
198 
199  bitcount = 0;
200  slicecount = 0;
201  bitslice = max_bitneed + 1;
202  do {
203  bitslice--;
204  bitcount += slicecount;
205  slicecount = 0;
206  for (ch = 0; ch < 2; ch++) {
207  for (sb = 0; sb < subbands; sb++) {
208  if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
209  slicecount++;
210  else if (bitneed[ch][sb] == bitslice + 1)
211  slicecount += 2;
212  }
213  }
214  } while (bitcount + slicecount < frame->bitpool);
215 
216  if (bitcount + slicecount == frame->bitpool) {
217  bitcount += slicecount;
218  bitslice--;
219  }
220 
221  for (ch = 0; ch < 2; ch++) {
222  for (sb = 0; sb < subbands; sb++) {
223  if (bitneed[ch][sb] < bitslice + 2) {
224  bits[ch][sb] = 0;
225  } else {
226  bits[ch][sb] = bitneed[ch][sb] - bitslice;
227  if (bits[ch][sb] > 16)
228  bits[ch][sb] = 16;
229  }
230  }
231  }
232 
233  ch = 0;
234  sb = 0;
235  while (bitcount < frame->bitpool) {
236  if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) {
237  bits[ch][sb]++;
238  bitcount++;
239  } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) {
240  bits[ch][sb] = 2;
241  bitcount += 2;
242  }
243  if (ch == 1) {
244  ch = 0;
245  sb++;
246  if (sb >= subbands)
247  break;
248  } else
249  ch = 1;
250  }
251 
252  ch = 0;
253  sb = 0;
254  while (bitcount < frame->bitpool) {
255  if (bits[ch][sb] < 16) {
256  bits[ch][sb]++;
257  bitcount++;
258  }
259  if (ch == 1) {
260  ch = 0;
261  sb++;
262  if (sb >= subbands)
263  break;
264  } else
265  ch = 1;
266  }
267 
268  }
269 
270 }
JOINT_STEREO
#define JOINT_STEREO
Definition: atrac3.c:59
AVCRC
uint32_t AVCRC
Definition: crc.h:46
data
const char data[16]
Definition: mxf.c:148
STEREO
#define STEREO
Definition: cook.c:65
subbands
subbands
Definition: aptx.h:37
mask
static const uint16_t mask[17]
Definition: lzw.c:38
bits
uint8_t bits
Definition: vp3data.h:128
ctx
AVFormatContext * ctx
Definition: movenc.c:49
sbc.h
sbc_frame
Definition: sbc.h:84
sbc_offset8
static const int sbc_offset8[4][8]
Definition: sbc.c:44
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
MONO
#define MONO
Definition: cook.c:64
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:264
sbc_offset4
static const int sbc_offset4[4][4]
Definition: sbc.c:36
av_crc
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:392
ff_sbc_calculate_bits
void ff_sbc_calculate_bits(const struct sbc_frame *frame, int(*bits)[8])
Definition: sbc.c:78