FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 }
enum sbc_frame::@140 mode
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define JOINT_STEREO
Definition: atrac3.c:55
int loudness
Definition: normalize.py:20
uint8_t
static AVFrame * frame
static const uint16_t mask[17]
Definition: lzw.c:38
uint32_t scale_factor[2][8]
Definition: sbc.h:104
uint8_t bitpool
Definition: sbc.h:97
uint8_t channels
Definition: sbc.h:91
AVFormatContext * ctx
Definition: movenc.c:48
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
uint8_t ff_sbc_crc8(const AVCRC *ctx, const uint8_t *data, size_t len)
Definition: sbc.c:55
enum sbc_frame::@141 allocation
Libavcodec external API header.
uint8_t frequency
Definition: sbc.h:83
#define MONO
Definition: cook.c:60
#define STEREO
Definition: cook.c:61
SBC common definitions for the encoder and decoder.
static const int sbc_offset4[4][4]
Definition: sbc.c:37
int len
void ff_sbc_calculate_bits(const struct sbc_frame *frame, int(*bits)[8])
Definition: sbc.c:79
Definition: sbc.h:82
subbands
Definition: aptx.c:36
static const int sbc_offset8[4][8]
Definition: sbc.c:45
uint8_t subbands
Definition: sbc.h:96
uint32_t AVCRC
Definition: crc.h:47
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV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L, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56