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