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