FFmpeg  4.4.9
sbcenc.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 encoder implementation
31  */
32 
33 #include "libavutil/opt.h"
34 #include "avcodec.h"
35 #include "internal.h"
36 #include "profiles.h"
37 #include "put_bits.h"
38 #include "sbc.h"
39 #include "sbcdsp.h"
40 
41 typedef struct SBCEncContext {
42  AVClass *class;
44  int msbc;
46  DECLARE_ALIGNED(SBC_ALIGN, SBCDSPContext, dsp);
48 
49 static int sbc_analyze_audio(SBCDSPContext *s, struct sbc_frame *frame)
50 {
51  int ch, blk;
52  int16_t *x;
53 
54  switch (frame->subbands) {
55  case 4:
56  for (ch = 0; ch < frame->channels; ch++) {
57  x = &s->X[ch][s->position - 4 *
58  s->increment + frame->blocks * 4];
59  for (blk = 0; blk < frame->blocks;
60  blk += s->increment) {
61  s->sbc_analyze_4s(
62  s, x,
63  frame->sb_sample_f[blk][ch],
64  frame->sb_sample_f[blk + 1][ch] -
65  frame->sb_sample_f[blk][ch]);
66  x -= 4 * s->increment;
67  }
68  }
69  return frame->blocks * 4;
70 
71  case 8:
72  for (ch = 0; ch < frame->channels; ch++) {
73  x = &s->X[ch][s->position - 8 *
74  s->increment + frame->blocks * 8];
75  for (blk = 0; blk < frame->blocks;
76  blk += s->increment) {
77  s->sbc_analyze_8s(
78  s, x,
79  frame->sb_sample_f[blk][ch],
80  frame->sb_sample_f[blk + 1][ch] -
81  frame->sb_sample_f[blk][ch]);
82  x -= 8 * s->increment;
83  }
84  }
85  return frame->blocks * 8;
86 
87  default:
88  return AVERROR(EIO);
89  }
90 }
91 
92 /*
93  * Packs the SBC frame from frame into the memory in avpkt.
94  * Returns the length of the packed frame.
95  */
96 static size_t sbc_pack_frame(AVPacket *avpkt, struct sbc_frame *frame,
97  int joint, int msbc)
98 {
99  PutBitContext pb;
100 
101  /* Will copy the header parts for CRC-8 calculation here */
102  uint8_t crc_header[11] = { 0 };
103  int crc_pos;
104 
105  uint32_t audio_sample;
106 
107  int ch, sb, blk; /* channel, subband, block and bit counters */
108  int bits[2][8]; /* bits distribution */
109  uint32_t levels[2][8]; /* levels are derived from that */
110  uint32_t sb_sample_delta[2][8];
111 
112  if (msbc) {
113  avpkt->data[0] = MSBC_SYNCWORD;
114  avpkt->data[1] = 0;
115  avpkt->data[2] = 0;
116  } else {
117  avpkt->data[0] = SBC_SYNCWORD;
118 
119  avpkt->data[1] = (frame->frequency & 0x03) << 6;
120  avpkt->data[1] |= (((frame->blocks >> 2) - 1) & 0x03) << 4;
121  avpkt->data[1] |= (frame->mode & 0x03) << 2;
122  avpkt->data[1] |= (frame->allocation & 0x01) << 1;
123  avpkt->data[1] |= ((frame->subbands == 8) & 0x01) << 0;
124 
125  avpkt->data[2] = frame->bitpool;
126  }
127 
128  /* Can't fill in crc yet */
129  crc_header[0] = avpkt->data[1];
130  crc_header[1] = avpkt->data[2];
131  crc_pos = 16;
132 
133  init_put_bits(&pb, avpkt->data + 4, avpkt->size);
134 
135  if (frame->mode == JOINT_STEREO) {
136  put_bits(&pb, frame->subbands, joint);
137  crc_header[crc_pos >> 3] = joint;
138  crc_pos += frame->subbands;
139  }
140 
141  for (ch = 0; ch < frame->channels; ch++) {
142  for (sb = 0; sb < frame->subbands; sb++) {
143  put_bits(&pb, 4, frame->scale_factor[ch][sb] & 0x0F);
144  crc_header[crc_pos >> 3] <<= 4;
145  crc_header[crc_pos >> 3] |= frame->scale_factor[ch][sb] & 0x0F;
146  crc_pos += 4;
147  }
148  }
149 
150  /* align the last crc byte */
151  if (crc_pos % 8)
152  crc_header[crc_pos >> 3] <<= 8 - (crc_pos % 8);
153 
154  avpkt->data[3] = ff_sbc_crc8(frame->crc_ctx, crc_header, crc_pos);
155 
157 
158  for (ch = 0; ch < frame->channels; ch++) {
159  for (sb = 0; sb < frame->subbands; sb++) {
160  levels[ch][sb] = ((1 << bits[ch][sb]) - 1) <<
161  (32 - (frame->scale_factor[ch][sb] +
162  SCALE_OUT_BITS + 2));
163  sb_sample_delta[ch][sb] = (uint32_t) 1 <<
164  (frame->scale_factor[ch][sb] +
165  SCALE_OUT_BITS + 1);
166  }
167  }
168 
169  for (blk = 0; blk < frame->blocks; blk++) {
170  for (ch = 0; ch < frame->channels; ch++) {
171  for (sb = 0; sb < frame->subbands; sb++) {
172 
173  if (bits[ch][sb] == 0)
174  continue;
175 
176  audio_sample = ((uint64_t) levels[ch][sb] *
177  (sb_sample_delta[ch][sb] +
178  frame->sb_sample_f[blk][ch][sb])) >> 32;
179 
180  put_bits(&pb, bits[ch][sb], audio_sample);
181  }
182  }
183  }
184 
185  flush_put_bits(&pb);
186 
187  return (put_bits_count(&pb) + 7) / 8;
188 }
189 
190 static int sbc_encode_init(AVCodecContext *avctx)
191 {
192  SBCEncContext *sbc = avctx->priv_data;
193  struct sbc_frame *frame = &sbc->frame;
194 
195  if (avctx->profile == FF_PROFILE_SBC_MSBC)
196  sbc->msbc = 1;
197 
198  if (sbc->msbc) {
199  if (avctx->channels != 1) {
200  av_log(avctx, AV_LOG_ERROR, "mSBC require mono channel.\n");
201  return AVERROR(EINVAL);
202  }
203 
204  if (avctx->sample_rate != 16000) {
205  av_log(avctx, AV_LOG_ERROR, "mSBC require 16 kHz samplerate.\n");
206  return AVERROR(EINVAL);
207  }
208 
209  frame->mode = SBC_MODE_MONO;
210  frame->subbands = 8;
211  frame->blocks = MSBC_BLOCKS;
212  frame->allocation = SBC_AM_LOUDNESS;
213  frame->bitpool = 26;
214 
215  avctx->frame_size = 8 * MSBC_BLOCKS;
216  } else {
217  int d;
218 
219  if (avctx->global_quality > 255*FF_QP2LAMBDA) {
220  av_log(avctx, AV_LOG_ERROR, "bitpool > 255 is not allowed.\n");
221  return AVERROR(EINVAL);
222  }
223 
224  if (avctx->channels == 1) {
225  frame->mode = SBC_MODE_MONO;
226  if (sbc->max_delay <= 3000 || avctx->bit_rate > 270000)
227  frame->subbands = 4;
228  else
229  frame->subbands = 8;
230  } else {
231  if (avctx->bit_rate < 180000 || avctx->bit_rate > 420000)
233  else
234  frame->mode = SBC_MODE_STEREO;
235  if (sbc->max_delay <= 4000 || avctx->bit_rate > 420000)
236  frame->subbands = 4;
237  else
238  frame->subbands = 8;
239  }
240  /* sbc algorithmic delay is ((blocks + 10) * subbands - 2) / sample_rate */
241  frame->blocks = av_clip(((sbc->max_delay * avctx->sample_rate + 2)
242  / (1000000 * frame->subbands)) - 10, 4, 16) & ~3;
243 
244  frame->allocation = SBC_AM_LOUDNESS;
245 
246  d = frame->blocks * ((frame->mode == SBC_MODE_DUAL_CHANNEL) + 1);
247  frame->bitpool = (((avctx->bit_rate * frame->subbands * frame->blocks) / avctx->sample_rate)
248  - 4 * frame->subbands * avctx->channels
249  - (frame->mode == SBC_MODE_JOINT_STEREO)*frame->subbands - 32 + d/2) / d;
250  if (avctx->global_quality > 0)
251  frame->bitpool = avctx->global_quality / FF_QP2LAMBDA;
252 
253  if (frame->bitpool > frame->subbands << (4 + (frame->mode == STEREO
254  || frame->mode == JOINT_STEREO))) {
255  av_log(avctx, AV_LOG_ERROR, "Invalid parameter combination\n");
256  return AVERROR_PATCHWELCOME;
257  }
258 
259  avctx->frame_size = 4*((frame->subbands >> 3) + 1) * 4*(frame->blocks >> 2);
260  }
261 
262  for (int i = 0; avctx->codec->supported_samplerates[i]; i++)
263  if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
264  frame->frequency = i;
265 
266  frame->channels = avctx->channels;
267  frame->codesize = frame->subbands * frame->blocks * avctx->channels * 2;
268  frame->crc_ctx = av_crc_get_table(AV_CRC_8_EBU);
269 
270  memset(&sbc->dsp.X, 0, sizeof(sbc->dsp.X));
271  sbc->dsp.position = (SBC_X_BUFFER_SIZE - frame->subbands * 9) & ~7;
272  sbc->dsp.increment = sbc->msbc ? 1 : 4;
273  ff_sbcdsp_init(&sbc->dsp);
274 
275  return 0;
276 }
277 
278 static int sbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
279  const AVFrame *av_frame, int *got_packet_ptr)
280 {
281  SBCEncContext *sbc = avctx->priv_data;
282  struct sbc_frame *frame = &sbc->frame;
284  uint8_t dual = frame->mode == SBC_MODE_DUAL_CHANNEL;
285  int ret, j = 0;
286 
287  int frame_length = 4 + (4 * frame->subbands * frame->channels) / 8
288  + ((frame->blocks * frame->bitpool * (1 + dual)
289  + joint * frame->subbands) + 7) / 8;
290 
291  /* input must be large enough to encode a complete frame */
292  if (av_frame->nb_samples * frame->channels * 2 < frame->codesize)
293  return 0;
294 
295  if ((ret = ff_alloc_packet2(avctx, avpkt, frame_length, 0)) < 0)
296  return ret;
297 
298  /* Select the needed input data processing function and call it */
299  if (frame->subbands == 8)
300  sbc->dsp.position = sbc->dsp.sbc_enc_process_input_8s(
301  sbc->dsp.position, av_frame->data[0], sbc->dsp.X,
302  frame->subbands * frame->blocks, frame->channels);
303  else
304  sbc->dsp.position = sbc->dsp.sbc_enc_process_input_4s(
305  sbc->dsp.position, av_frame->data[0], sbc->dsp.X,
306  frame->subbands * frame->blocks, frame->channels);
307 
308  sbc_analyze_audio(&sbc->dsp, &sbc->frame);
309 
310  if (frame->mode == JOINT_STEREO)
311  j = sbc->dsp.sbc_calc_scalefactors_j(frame->sb_sample_f,
312  frame->scale_factor,
313  frame->blocks,
314  frame->subbands);
315  else
316  sbc->dsp.sbc_calc_scalefactors(frame->sb_sample_f,
317  frame->scale_factor,
318  frame->blocks,
319  frame->channels,
320  frame->subbands);
321  emms_c();
322  sbc_pack_frame(avpkt, frame, j, sbc->msbc);
323 
324  *got_packet_ptr = 1;
325  return 0;
326 }
327 
328 #define OFFSET(x) offsetof(SBCEncContext, x)
329 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
330 static const AVOption options[] = {
331  { "sbc_delay", "set maximum algorithmic latency",
332  OFFSET(max_delay), AV_OPT_TYPE_DURATION, {.i64 = 13000}, 1000,13000, AE },
333  { "msbc", "use mSBC mode (wideband speech mono SBC)",
334  OFFSET(msbc), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AE },
336  { NULL },
337 };
338 
339 static const AVClass sbc_class = {
340  .class_name = "sbc encoder",
341  .item_name = av_default_item_name,
342  .option = options,
343  .version = LIBAVUTIL_VERSION_INT,
344 };
345 
347  .name = "sbc",
348  .long_name = NULL_IF_CONFIG_SMALL("SBC (low-complexity subband codec)"),
349  .type = AVMEDIA_TYPE_AUDIO,
350  .id = AV_CODEC_ID_SBC,
351  .priv_data_size = sizeof(SBCEncContext),
353  .encode2 = sbc_encode_frame,
354  .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
355  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
356  .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
358  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
360  .supported_samplerates = (const int[]) { 16000, 32000, 44100, 48000, 0 },
361  .priv_class = &sbc_class,
363 };
#define JOINT_STEREO
Definition: atrac3.c:58
uint8_t
Libavcodec external API header.
#define FF_PROFILE_SBC_MSBC
Definition: avcodec.h:1968
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define s(width, name)
Definition: cbs_vp9.c:257
#define av_clip
Definition: common.h:122
#define STEREO
Definition: cook.c:62
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static const uint16_t channel_layouts[7]
Definition: dca_lbr.c:114
static AVFrame * frame
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:34
@ AV_OPT_TYPE_DURATION
Definition: opt.h:239
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
#define AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_STEREO
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: codec.h:82
@ AV_CODEC_ID_SBC
Definition: codec_id.h:512
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
@ AV_CRC_8_EBU
Definition: crc.h:57
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
Definition: mem.h:117
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int i
Definition: input.c:407
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:218
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:41
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
#define emms_c()
Definition: internal.h:54
AVOptions.
const AVProfile ff_sbc_profiles[]
Definition: profiles.c:154
#define FF_AVCTX_PROFILE_OPTION(name, description, type, value)
Definition: profiles.h:25
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:57
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:76
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:110
uint8_t ff_sbc_crc8(const AVCRC *ctx, const uint8_t *data, size_t len)
Definition: sbc.c:55
void ff_sbc_calculate_bits(const struct sbc_frame *frame, int(*bits)[8])
Definition: sbc.c:79
SBC common definitions for the encoder and decoder.
#define SBC_MODE_JOINT_STEREO
Definition: sbc.h:58
#define SBC_ALIGN
Definition: sbc.h:79
#define SBC_AM_LOUDNESS
Definition: sbc.h:61
#define MSBC_BLOCKS
Definition: sbc.h:40
#define SBC_MODE_STEREO
Definition: sbc.h:57
#define SBC_SYNCWORD
Definition: sbc.h:69
#define MSBC_SYNCWORD
Definition: sbc.h:70
#define SBC_MODE_DUAL_CHANNEL
Definition: sbc.h:56
#define SBC_MODE_MONO
Definition: sbc.h:55
av_cold void ff_sbcdsp_init(SBCDSPContext *s)
Definition: sbcdsp.c:364
SBC basic "building bricks".
#define SBC_X_BUFFER_SIZE
Definition: sbcdsp.h:41
#define SCALE_OUT_BITS
Definition: sbcdsp.h:40
static size_t sbc_pack_frame(AVPacket *avpkt, struct sbc_frame *frame, int joint, int msbc)
Definition: sbcenc.c:96
#define AE
Definition: sbcenc.c:329
static int sbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *av_frame, int *got_packet_ptr)
Definition: sbcenc.c:278
static const AVOption options[]
Definition: sbcenc.c:330
static int sbc_encode_init(AVCodecContext *avctx)
Definition: sbcenc.c:190
AVCodec ff_sbc_encoder
Definition: sbcenc.c:346
static const AVClass sbc_class
Definition: sbcenc.c:339
static int sbc_analyze_audio(SBCDSPContext *s, struct sbc_frame *frame)
Definition: sbcenc.c:49
#define OFFSET(x)
Definition: sbcenc.c:328
#define blk(i)
Definition: sha.c:185
Describe the class of an AVClass context structure.
Definition: log.h:67
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
main external API structure.
Definition: avcodec.h:536
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:602
int64_t bit_rate
the average bitrate
Definition: avcodec.h:586
const struct AVCodec * codec
Definition: avcodec.h:545
int profile
profile
Definition: avcodec.h:1862
int sample_rate
samples per second
Definition: avcodec.h:1196
int channels
number of audio channels
Definition: avcodec.h:1197
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1216
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
const int * supported_samplerates
array of supported audio samplerates, or NULL if unknown, array is terminated by 0
Definition: codec.h:219
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:384
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int channels
number of audio channels, only used for audio.
Definition: frame.h:624
AVOption.
Definition: opt.h:248
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
SBCDSPContext dsp
Definition: sbcenc.c:46
int msbc
Definition: sbcenc.c:44
struct sbc_frame frame
Definition: sbcenc.c:45
int64_t max_delay
Definition: sbcenc.c:43
Definition: sbc.h:83
uint8_t joint
Definition: sbc.h:102
#define av_log(a,...)
uint8_t bits
Definition: vp3data.h:141