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/channel_layout.h" 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 43typedef struct SBCEncContext { 44 AVClass *class; 45 int64_t max_delay; 46 int msbc; 47 DECLARE_ALIGNED(SBC_ALIGN, struct sbc_frame, frame); 48 DECLARE_ALIGNED(SBC_ALIGN, SBCDSPContext, dsp); 49} SBCEncContext; 50 51static int sbc_analyze_audio(SBCDSPContext *s, struct sbc_frame *frame) 52{ 53 int ch, blk; 54 int16_t *x; 55 56 switch (frame->subbands) { 57 case 4: 58 for (ch = 0; ch < frame->channels; ch++) { 59 x = &s->X[ch][s->position - 4 * 60 s->increment + frame->blocks * 4]; 61 for (blk = 0; blk < frame->blocks; 62 blk += s->increment) { 63 s->sbc_analyze_4s( 64 s, x, 65 frame->sb_sample_f[blk][ch], 66 frame->sb_sample_f[blk + 1][ch] - 67 frame->sb_sample_f[blk][ch]); 68 x -= 4 * s->increment; 69 } 70 } 71 return frame->blocks * 4; 72 73 case 8: 74 for (ch = 0; ch < frame->channels; ch++) { 75 x = &s->X[ch][s->position - 8 * 76 s->increment + frame->blocks * 8]; 77 for (blk = 0; blk < frame->blocks; 78 blk += s->increment) { 79 s->sbc_analyze_8s( 80 s, x, 81 frame->sb_sample_f[blk][ch], 82 frame->sb_sample_f[blk + 1][ch] - 83 frame->sb_sample_f[blk][ch]); 84 x -= 8 * s->increment; 85 } 86 } 87 return frame->blocks * 8; 88 89 default: 90 return AVERROR(EIO); 91 } 92} 93 94/* 95 * Packs the SBC frame from frame into the memory in avpkt. 96 * Returns the length of the packed frame. 97 */ 98static size_t sbc_pack_frame(AVPacket *avpkt, struct sbc_frame *frame, 99 int joint, int msbc) 100{ 101 PutBitContext pb; 102 103 /* Will copy the header parts for CRC-8 calculation here */ 104 uint8_t crc_header[11] = { 0 }; 105 int crc_pos; 106 107 uint32_t audio_sample; 108 109 int ch, sb, blk; /* channel, subband, block and bit counters */ 110 int bits[2][8]; /* bits distribution */ 111 uint32_t levels[2][8]; /* levels are derived from that */ 112 uint32_t sb_sample_delta[2][8]; 113 114 if (msbc) { 115 avpkt->data[0] = MSBC_SYNCWORD; 116 avpkt->data[1] = 0; 117 avpkt->data[2] = 0; 118 } else { 119 avpkt->data[0] = SBC_SYNCWORD; 120 121 avpkt->data[1] = (frame->frequency & 0x03) << 6; 122 avpkt->data[1] |= (((frame->blocks >> 2) - 1) & 0x03) << 4; 123 avpkt->data[1] |= (frame->mode & 0x03) << 2; 124 avpkt->data[1] |= (frame->allocation & 0x01) << 1; 125 avpkt->data[1] |= ((frame->subbands == 8) & 0x01) << 0; 126 127 avpkt->data[2] = frame->bitpool; 128 129 if (frame->bitpool > frame->subbands << (4 + (frame->mode == STEREO 130 || frame->mode == JOINT_STEREO))) 131 return -5; 132 } 133 134 /* Can't fill in crc yet */ 135 crc_header[0] = avpkt->data[1]; 136 crc_header[1] = avpkt->data[2]; 137 crc_pos = 16; 138 139 init_put_bits(&pb, avpkt->data + 4, avpkt->size); 140 141 if (frame->mode == JOINT_STEREO) { 142 put_bits(&pb, frame->subbands, joint); 143 crc_header[crc_pos >> 3] = joint; 144 crc_pos += frame->subbands; 145 } 146 147 for (ch = 0; ch < frame->channels; ch++) { 148 for (sb = 0; sb < frame->subbands; sb++) { 149 put_bits(&pb, 4, frame->scale_factor[ch][sb] & 0x0F); 150 crc_header[crc_pos >> 3] <<= 4; 151 crc_header[crc_pos >> 3] |= frame->scale_factor[ch][sb] & 0x0F; 152 crc_pos += 4; 153 } 154 } 155 156 /* align the last crc byte */ 157 if (crc_pos % 8) 158 crc_header[crc_pos >> 3] <<= 8 - (crc_pos % 8); 159 160 avpkt->data[3] = ff_sbc_crc8(frame->crc_ctx, crc_header, crc_pos); 161 162 ff_sbc_calculate_bits(frame, bits); 163 164 for (ch = 0; ch < frame->channels; ch++) { 165 for (sb = 0; sb < frame->subbands; sb++) { 166 levels[ch][sb] = ((1 << bits[ch][sb]) - 1) << 167 (32 - (frame->scale_factor[ch][sb] + 168 SCALE_OUT_BITS + 2)); 169 sb_sample_delta[ch][sb] = (uint32_t) 1 << 170 (frame->scale_factor[ch][sb] + 171 SCALE_OUT_BITS + 1); 172 } 173 } 174 175 for (blk = 0; blk < frame->blocks; blk++) { 176 for (ch = 0; ch < frame->channels; ch++) { 177 for (sb = 0; sb < frame->subbands; sb++) { 178 179 if (bits[ch][sb] == 0) 180 continue; 181 182 audio_sample = ((uint64_t) levels[ch][sb] * 183 (sb_sample_delta[ch][sb] + 184 frame->sb_sample_f[blk][ch][sb])) >> 32; 185 186 put_bits(&pb, bits[ch][sb], audio_sample); 187 } 188 } 189 } 190 191 flush_put_bits(&pb); 192 193 return put_bytes_output(&pb); 194} 195 196static int sbc_encode_init(AVCodecContext *avctx) 197{ 198 SBCEncContext *sbc = avctx->priv_data; 199 struct sbc_frame *frame = &sbc->frame; 200 201 if (avctx->profile == FF_PROFILE_SBC_MSBC) 202 sbc->msbc = 1; 203 204 if (sbc->msbc) { 205 if (avctx->ch_layout.nb_channels != 1) { 206 av_log(avctx, AV_LOG_ERROR, "mSBC require mono channel.\n"); 207 return AVERROR(EINVAL); 208 } 209 210 if (avctx->sample_rate != 16000) { 211 av_log(avctx, AV_LOG_ERROR, "mSBC require 16 kHz samplerate.\n"); 212 return AVERROR(EINVAL); 213 } 214 215 frame->mode = SBC_MODE_MONO; 216 frame->subbands = 8; 217 frame->blocks = MSBC_BLOCKS; 218 frame->allocation = SBC_AM_LOUDNESS; 219 frame->bitpool = 26; 220 221 avctx->frame_size = 8 * MSBC_BLOCKS; 222 } else { 223 int d; 224 225 if (avctx->global_quality > 255*FF_QP2LAMBDA) { 226 av_log(avctx, AV_LOG_ERROR, "bitpool > 255 is not allowed.\n"); 227 return AVERROR(EINVAL); 228 } 229 230 if (avctx->ch_layout.nb_channels == 1) { 231 frame->mode = SBC_MODE_MONO; 232 if (sbc->max_delay <= 3000 || avctx->bit_rate > 270000) 233 frame->subbands = 4; 234 else 235 frame->subbands = 8; 236 } else { 237 if (avctx->bit_rate < 180000 || avctx->bit_rate > 420000) 238 frame->mode = SBC_MODE_JOINT_STEREO; 239 else 240 frame->mode = SBC_MODE_STEREO; 241 if (sbc->max_delay <= 4000 || avctx->bit_rate > 420000) 242 frame->subbands = 4; 243 else 244 frame->subbands = 8; 245 } 246 /* sbc algorithmic delay is ((blocks + 10) * subbands - 2) / sample_rate */ 247 frame->blocks = av_clip(((sbc->max_delay * avctx->sample_rate + 2) 248 / (1000000 * frame->subbands)) - 10, 4, 16) & ~3; 249 250 frame->allocation = SBC_AM_LOUDNESS; 251 252 d = frame->blocks * ((frame->mode == SBC_MODE_DUAL_CHANNEL) + 1); 253 frame->bitpool = (((avctx->bit_rate * frame->subbands * frame->blocks) / avctx->sample_rate) 254 - 4 * frame->subbands * avctx->ch_layout.nb_channels 255 - (frame->mode == SBC_MODE_JOINT_STEREO)*frame->subbands - 32 + d/2) / d; 256 if (avctx->global_quality > 0) 257 frame->bitpool = avctx->global_quality / FF_QP2LAMBDA; 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->ch_layout.nb_channels; 267 frame->codesize = frame->subbands * frame->blocks * avctx->ch_layout.nb_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 278static 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; 283 uint8_t joint = frame->mode == SBC_MODE_JOINT_STEREO; 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_get_encode_buffer(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 330static 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 }, 335 FF_AVCTX_PROFILE_OPTION("msbc", NULL, AUDIO, FF_PROFILE_SBC_MSBC) 336 { NULL }, 337}; 338 339static 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 346const FFCodec ff_sbc_encoder = { 347 .p.name = "sbc", 348 .p.long_name = NULL_IF_CONFIG_SMALL("SBC (low-complexity subband codec)"), 349 .p.type = AVMEDIA_TYPE_AUDIO, 350 .p.id = AV_CODEC_ID_SBC, 351 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SMALL_LAST_FRAME, 352 .priv_data_size = sizeof(SBCEncContext), 353 .init = sbc_encode_init, 354 FF_CODEC_ENCODE_CB(sbc_encode_frame), 355 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, 356#if FF_API_OLD_CHANNEL_LAYOUT 357 .p.channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO, 358 AV_CH_LAYOUT_STEREO, 0}, 359#endif 360 .p.ch_layouts = (const AVChannelLayout[]) { AV_CHANNEL_LAYOUT_MONO, 361 AV_CHANNEL_LAYOUT_STEREO, 362 { 0 } }, 363 .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, 364 AV_SAMPLE_FMT_NONE }, 365 .p.supported_samplerates = (const int[]) { 16000, 32000, 44100, 48000, 0 }, 366 .p.priv_class = &sbc_class, 367 .p.profiles = NULL_IF_CONFIG_SMALL(ff_sbc_profiles), 368}; 369