2 * Bluetooth low-complexity, subband codec (SBC)
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>
11 * This file is part of FFmpeg.
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.
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.
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
30 * SBC encoder implementation
34 #include "libavutil/opt.h"
42 typedef struct SBCEncContext {
46 DECLARE_ALIGNED(SBC_ALIGN, struct sbc_frame, frame);
47 DECLARE_ALIGNED(SBC_ALIGN, SBCDSPContext, dsp);
50 static int sbc_analyze_audio(SBCDSPContext *s, struct sbc_frame *frame)
55 switch (frame->subbands) {
57 for (ch = 0; ch < frame->channels; ch++) {
58 x = &s->X[ch][s->position - 4 *
59 s->increment + frame->blocks * 4];
60 for (blk = 0; blk < frame->blocks;
61 blk += s->increment) {
64 frame->sb_sample_f[blk][ch],
65 frame->sb_sample_f[blk + 1][ch] -
66 frame->sb_sample_f[blk][ch]);
67 x -= 4 * s->increment;
70 return frame->blocks * 4;
73 for (ch = 0; ch < frame->channels; ch++) {
74 x = &s->X[ch][s->position - 8 *
75 s->increment + frame->blocks * 8];
76 for (blk = 0; blk < frame->blocks;
77 blk += s->increment) {
80 frame->sb_sample_f[blk][ch],
81 frame->sb_sample_f[blk + 1][ch] -
82 frame->sb_sample_f[blk][ch]);
83 x -= 8 * s->increment;
86 return frame->blocks * 8;
94 * Packs the SBC frame from frame into the memory in avpkt.
95 * Returns the length of the packed frame.
97 static size_t sbc_pack_frame(AVPacket *avpkt, struct sbc_frame *frame,
102 /* Will copy the header parts for CRC-8 calculation here */
103 uint8_t crc_header[11] = { 0 };
106 uint32_t audio_sample;
108 int ch, sb, blk; /* channel, subband, block and bit counters */
109 int bits[2][8]; /* bits distribution */
110 uint32_t levels[2][8]; /* levels are derived from that */
111 uint32_t sb_sample_delta[2][8];
114 avpkt->data[0] = MSBC_SYNCWORD;
118 avpkt->data[0] = SBC_SYNCWORD;
120 avpkt->data[1] = (frame->frequency & 0x03) << 6;
121 avpkt->data[1] |= (((frame->blocks >> 2) - 1) & 0x03) << 4;
122 avpkt->data[1] |= (frame->mode & 0x03) << 2;
123 avpkt->data[1] |= (frame->allocation & 0x01) << 1;
124 avpkt->data[1] |= ((frame->subbands == 8) & 0x01) << 0;
126 avpkt->data[2] = frame->bitpool;
128 if (frame->bitpool > frame->subbands << (4 + (frame->mode == STEREO
129 || frame->mode == JOINT_STEREO)))
133 /* Can't fill in crc yet */
134 crc_header[0] = avpkt->data[1];
135 crc_header[1] = avpkt->data[2];
138 init_put_bits(&pb, avpkt->data + 4, avpkt->size);
140 if (frame->mode == JOINT_STEREO) {
141 put_bits(&pb, frame->subbands, joint);
142 crc_header[crc_pos >> 3] = joint;
143 crc_pos += frame->subbands;
146 for (ch = 0; ch < frame->channels; ch++) {
147 for (sb = 0; sb < frame->subbands; sb++) {
148 put_bits(&pb, 4, frame->scale_factor[ch][sb] & 0x0F);
149 crc_header[crc_pos >> 3] <<= 4;
150 crc_header[crc_pos >> 3] |= frame->scale_factor[ch][sb] & 0x0F;
155 /* align the last crc byte */
157 crc_header[crc_pos >> 3] <<= 8 - (crc_pos % 8);
159 avpkt->data[3] = ff_sbc_crc8(frame->crc_ctx, crc_header, crc_pos);
161 ff_sbc_calculate_bits(frame, bits);
163 for (ch = 0; ch < frame->channels; ch++) {
164 for (sb = 0; sb < frame->subbands; sb++) {
165 levels[ch][sb] = ((1 << bits[ch][sb]) - 1) <<
166 (32 - (frame->scale_factor[ch][sb] +
167 SCALE_OUT_BITS + 2));
168 sb_sample_delta[ch][sb] = (uint32_t) 1 <<
169 (frame->scale_factor[ch][sb] +
174 for (blk = 0; blk < frame->blocks; blk++) {
175 for (ch = 0; ch < frame->channels; ch++) {
176 for (sb = 0; sb < frame->subbands; sb++) {
178 if (bits[ch][sb] == 0)
181 audio_sample = ((uint64_t) levels[ch][sb] *
182 (sb_sample_delta[ch][sb] +
183 frame->sb_sample_f[blk][ch][sb])) >> 32;
185 put_bits(&pb, bits[ch][sb], audio_sample);
192 return (put_bits_count(&pb) + 7) / 8;
195 static int sbc_encode_init(AVCodecContext *avctx)
197 SBCEncContext *sbc = avctx->priv_data;
198 struct sbc_frame *frame = &sbc->frame;
200 if (avctx->profile == FF_PROFILE_SBC_MSBC)
204 if (avctx->channels != 1) {
205 av_log(avctx, AV_LOG_ERROR, "mSBC require mono channel.\n");
206 return AVERROR(EINVAL);
209 if (avctx->sample_rate != 16000) {
210 av_log(avctx, AV_LOG_ERROR, "mSBC require 16 kHz samplerate.\n");
211 return AVERROR(EINVAL);
214 frame->mode = SBC_MODE_MONO;
216 frame->blocks = MSBC_BLOCKS;
217 frame->allocation = SBC_AM_LOUDNESS;
220 avctx->frame_size = 8 * MSBC_BLOCKS;
224 if (avctx->global_quality > 255*FF_QP2LAMBDA) {
225 av_log(avctx, AV_LOG_ERROR, "bitpool > 255 is not allowed.\n");
226 return AVERROR(EINVAL);
229 if (avctx->channels == 1) {
230 frame->mode = SBC_MODE_MONO;
231 if (sbc->max_delay <= 3000 || avctx->bit_rate > 270000)
236 if (avctx->bit_rate < 180000 || avctx->bit_rate > 420000)
237 frame->mode = SBC_MODE_JOINT_STEREO;
239 frame->mode = SBC_MODE_STEREO;
240 if (sbc->max_delay <= 4000 || avctx->bit_rate > 420000)
245 /* sbc algorithmic delay is ((blocks + 10) * subbands - 2) / sample_rate */
246 frame->blocks = av_clip(((sbc->max_delay * avctx->sample_rate + 2)
247 / (1000000 * frame->subbands)) - 10, 4, 16) & ~3;
249 frame->allocation = SBC_AM_LOUDNESS;
251 d = frame->blocks * ((frame->mode == SBC_MODE_DUAL_CHANNEL) + 1);
252 frame->bitpool = (((avctx->bit_rate * frame->subbands * frame->blocks) / avctx->sample_rate)
253 - 4 * frame->subbands * avctx->channels
254 - (frame->mode == SBC_MODE_JOINT_STEREO)*frame->subbands - 32 + d/2) / d;
255 if (avctx->global_quality > 0)
256 frame->bitpool = avctx->global_quality / FF_QP2LAMBDA;
258 avctx->frame_size = 4*((frame->subbands >> 3) + 1) * 4*(frame->blocks >> 2);
261 for (int i = 0; avctx->codec->supported_samplerates[i]; i++)
262 if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
263 frame->frequency = i;
265 frame->channels = avctx->channels;
266 frame->codesize = frame->subbands * frame->blocks * avctx->channels * 2;
267 frame->crc_ctx = av_crc_get_table(AV_CRC_8_EBU);
269 memset(&sbc->dsp.X, 0, sizeof(sbc->dsp.X));
270 sbc->dsp.position = (SBC_X_BUFFER_SIZE - frame->subbands * 9) & ~7;
271 sbc->dsp.increment = sbc->msbc ? 1 : 4;
272 ff_sbcdsp_init(&sbc->dsp);
277 static int sbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
278 const AVFrame *av_frame, int *got_packet_ptr)
280 SBCEncContext *sbc = avctx->priv_data;
281 struct sbc_frame *frame = &sbc->frame;
282 uint8_t joint = frame->mode == SBC_MODE_JOINT_STEREO;
283 uint8_t dual = frame->mode == SBC_MODE_DUAL_CHANNEL;
286 int frame_length = 4 + (4 * frame->subbands * frame->channels) / 8
287 + ((frame->blocks * frame->bitpool * (1 + dual)
288 + joint * frame->subbands) + 7) / 8;
290 /* input must be large enough to encode a complete frame */
291 if (av_frame->nb_samples * frame->channels * 2 < frame->codesize)
294 if ((ret = ff_alloc_packet2(avctx, avpkt, frame_length, 0)) < 0)
297 /* Select the needed input data processing function and call it */
298 if (frame->subbands == 8)
299 sbc->dsp.position = sbc->dsp.sbc_enc_process_input_8s(
300 sbc->dsp.position, av_frame->data[0], sbc->dsp.X,
301 frame->subbands * frame->blocks, frame->channels);
303 sbc->dsp.position = sbc->dsp.sbc_enc_process_input_4s(
304 sbc->dsp.position, av_frame->data[0], sbc->dsp.X,
305 frame->subbands * frame->blocks, frame->channels);
307 sbc_analyze_audio(&sbc->dsp, &sbc->frame);
309 if (frame->mode == JOINT_STEREO)
310 j = sbc->dsp.sbc_calc_scalefactors_j(frame->sb_sample_f,
315 sbc->dsp.sbc_calc_scalefactors(frame->sb_sample_f,
321 sbc_pack_frame(avpkt, frame, j, sbc->msbc);
327 #define OFFSET(x) offsetof(SBCEncContext, x)
328 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
329 static const AVOption options[] = {
330 { "sbc_delay", "set maximum algorithmic latency",
331 OFFSET(max_delay), AV_OPT_TYPE_DURATION, {.i64 = 13000}, 1000,13000, AE },
332 { "msbc", "use mSBC mode (wideband speech mono SBC)",
333 OFFSET(msbc), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AE },
337 static const AVClass sbc_class = {
338 .class_name = "sbc encoder",
339 .item_name = av_default_item_name,
341 .version = LIBAVUTIL_VERSION_INT,
344 AVCodec ff_sbc_encoder = {
346 .long_name = NULL_IF_CONFIG_SMALL("SBC (low-complexity subband codec)"),
347 .type = AVMEDIA_TYPE_AUDIO,
348 .id = AV_CODEC_ID_SBC,
349 .priv_data_size = sizeof(SBCEncContext),
350 .init = sbc_encode_init,
351 .encode2 = sbc_encode_frame,
352 .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
353 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
354 .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
355 AV_CH_LAYOUT_STEREO, 0},
356 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
357 AV_SAMPLE_FMT_NONE },
358 .supported_samplerates = (const int[]) { 16000, 32000, 44100, 48000, 0 },
359 .priv_class = &sbc_class,
360 .profiles = NULL_IF_CONFIG_SMALL(ff_sbc_profiles),