You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

362 lines
13KB

  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. * @file
  29. * SBC encoder implementation
  30. */
  31. #include "libavutil/opt.h"
  32. #include "avcodec.h"
  33. #include "internal.h"
  34. #include "profiles.h"
  35. #include "put_bits.h"
  36. #include "sbc.h"
  37. #include "sbcdsp.h"
  38. typedef struct SBCEncContext {
  39. AVClass *class;
  40. int64_t max_delay;
  41. int msbc;
  42. DECLARE_ALIGNED(SBC_ALIGN, struct sbc_frame, frame);
  43. DECLARE_ALIGNED(SBC_ALIGN, SBCDSPContext, dsp);
  44. } SBCEncContext;
  45. static int sbc_analyze_audio(SBCDSPContext *s, struct sbc_frame *frame)
  46. {
  47. int ch, blk;
  48. int16_t *x;
  49. switch (frame->subbands) {
  50. case 4:
  51. for (ch = 0; ch < frame->channels; ch++) {
  52. x = &s->X[ch][s->position - 4 *
  53. s->increment + frame->blocks * 4];
  54. for (blk = 0; blk < frame->blocks;
  55. blk += s->increment) {
  56. s->sbc_analyze_4s(
  57. s, x,
  58. frame->sb_sample_f[blk][ch],
  59. frame->sb_sample_f[blk + 1][ch] -
  60. frame->sb_sample_f[blk][ch]);
  61. x -= 4 * s->increment;
  62. }
  63. }
  64. return frame->blocks * 4;
  65. case 8:
  66. for (ch = 0; ch < frame->channels; ch++) {
  67. x = &s->X[ch][s->position - 8 *
  68. s->increment + frame->blocks * 8];
  69. for (blk = 0; blk < frame->blocks;
  70. blk += s->increment) {
  71. s->sbc_analyze_8s(
  72. s, x,
  73. frame->sb_sample_f[blk][ch],
  74. frame->sb_sample_f[blk + 1][ch] -
  75. frame->sb_sample_f[blk][ch]);
  76. x -= 8 * s->increment;
  77. }
  78. }
  79. return frame->blocks * 8;
  80. default:
  81. return AVERROR(EIO);
  82. }
  83. }
  84. /*
  85. * Packs the SBC frame from frame into the memory in avpkt.
  86. * Returns the length of the packed frame.
  87. */
  88. static size_t sbc_pack_frame(AVPacket *avpkt, struct sbc_frame *frame,
  89. int joint, int msbc)
  90. {
  91. PutBitContext pb;
  92. /* Will copy the header parts for CRC-8 calculation here */
  93. uint8_t crc_header[11] = { 0 };
  94. int crc_pos;
  95. uint32_t audio_sample;
  96. int ch, sb, blk; /* channel, subband, block and bit counters */
  97. int bits[2][8]; /* bits distribution */
  98. uint32_t levels[2][8]; /* levels are derived from that */
  99. uint32_t sb_sample_delta[2][8];
  100. if (msbc) {
  101. avpkt->data[0] = MSBC_SYNCWORD;
  102. avpkt->data[1] = 0;
  103. avpkt->data[2] = 0;
  104. } else {
  105. avpkt->data[0] = SBC_SYNCWORD;
  106. avpkt->data[1] = (frame->frequency & 0x03) << 6;
  107. avpkt->data[1] |= (((frame->blocks >> 2) - 1) & 0x03) << 4;
  108. avpkt->data[1] |= (frame->mode & 0x03) << 2;
  109. avpkt->data[1] |= (frame->allocation & 0x01) << 1;
  110. avpkt->data[1] |= ((frame->subbands == 8) & 0x01) << 0;
  111. avpkt->data[2] = frame->bitpool;
  112. if (frame->bitpool > frame->subbands << (4 + (frame->mode == STEREO
  113. || frame->mode == JOINT_STEREO)))
  114. return -5;
  115. }
  116. /* Can't fill in crc yet */
  117. crc_header[0] = avpkt->data[1];
  118. crc_header[1] = avpkt->data[2];
  119. crc_pos = 16;
  120. init_put_bits(&pb, avpkt->data + 4, avpkt->size);
  121. if (frame->mode == JOINT_STEREO) {
  122. put_bits(&pb, frame->subbands, joint);
  123. crc_header[crc_pos >> 3] = joint;
  124. crc_pos += frame->subbands;
  125. }
  126. for (ch = 0; ch < frame->channels; ch++) {
  127. for (sb = 0; sb < frame->subbands; sb++) {
  128. put_bits(&pb, 4, frame->scale_factor[ch][sb] & 0x0F);
  129. crc_header[crc_pos >> 3] <<= 4;
  130. crc_header[crc_pos >> 3] |= frame->scale_factor[ch][sb] & 0x0F;
  131. crc_pos += 4;
  132. }
  133. }
  134. /* align the last crc byte */
  135. if (crc_pos % 8)
  136. crc_header[crc_pos >> 3] <<= 8 - (crc_pos % 8);
  137. avpkt->data[3] = ff_sbc_crc8(frame->crc_ctx, crc_header, crc_pos);
  138. ff_sbc_calculate_bits(frame, bits);
  139. for (ch = 0; ch < frame->channels; ch++) {
  140. for (sb = 0; sb < frame->subbands; sb++) {
  141. levels[ch][sb] = ((1 << bits[ch][sb]) - 1) <<
  142. (32 - (frame->scale_factor[ch][sb] +
  143. SCALE_OUT_BITS + 2));
  144. sb_sample_delta[ch][sb] = (uint32_t) 1 <<
  145. (frame->scale_factor[ch][sb] +
  146. SCALE_OUT_BITS + 1);
  147. }
  148. }
  149. for (blk = 0; blk < frame->blocks; blk++) {
  150. for (ch = 0; ch < frame->channels; ch++) {
  151. for (sb = 0; sb < frame->subbands; sb++) {
  152. if (bits[ch][sb] == 0)
  153. continue;
  154. audio_sample = ((uint64_t) levels[ch][sb] *
  155. (sb_sample_delta[ch][sb] +
  156. frame->sb_sample_f[blk][ch][sb])) >> 32;
  157. put_bits(&pb, bits[ch][sb], audio_sample);
  158. }
  159. }
  160. }
  161. flush_put_bits(&pb);
  162. return (put_bits_count(&pb) + 7) / 8;
  163. }
  164. static int sbc_encode_init(AVCodecContext *avctx)
  165. {
  166. SBCEncContext *sbc = avctx->priv_data;
  167. struct sbc_frame *frame = &sbc->frame;
  168. if (avctx->profile == FF_PROFILE_SBC_MSBC)
  169. sbc->msbc = 1;
  170. if (sbc->msbc) {
  171. if (avctx->channels != 1) {
  172. av_log(avctx, AV_LOG_ERROR, "mSBC require mono channel.\n");
  173. return AVERROR(EINVAL);
  174. }
  175. if (avctx->sample_rate != 16000) {
  176. av_log(avctx, AV_LOG_ERROR, "mSBC require 16 kHz samplerate.\n");
  177. return AVERROR(EINVAL);
  178. }
  179. frame->mode = SBC_MODE_MONO;
  180. frame->subbands = 8;
  181. frame->blocks = MSBC_BLOCKS;
  182. frame->allocation = SBC_AM_LOUDNESS;
  183. frame->bitpool = 26;
  184. avctx->frame_size = 8 * MSBC_BLOCKS;
  185. } else {
  186. int d;
  187. if (avctx->global_quality > 255*FF_QP2LAMBDA) {
  188. av_log(avctx, AV_LOG_ERROR, "bitpool > 255 is not allowed.\n");
  189. return AVERROR(EINVAL);
  190. }
  191. if (avctx->channels == 1) {
  192. frame->mode = SBC_MODE_MONO;
  193. if (sbc->max_delay <= 3000 || avctx->bit_rate > 270000)
  194. frame->subbands = 4;
  195. else
  196. frame->subbands = 8;
  197. } else {
  198. if (avctx->bit_rate < 180000 || avctx->bit_rate > 420000)
  199. frame->mode = SBC_MODE_JOINT_STEREO;
  200. else
  201. frame->mode = SBC_MODE_STEREO;
  202. if (sbc->max_delay <= 4000 || avctx->bit_rate > 420000)
  203. frame->subbands = 4;
  204. else
  205. frame->subbands = 8;
  206. }
  207. /* sbc algorithmic delay is ((blocks + 10) * subbands - 2) / sample_rate */
  208. frame->blocks = av_clip(((sbc->max_delay * avctx->sample_rate + 2)
  209. / (1000000 * frame->subbands)) - 10, 4, 16) & ~3;
  210. frame->allocation = SBC_AM_LOUDNESS;
  211. d = frame->blocks * ((frame->mode == SBC_MODE_DUAL_CHANNEL) + 1);
  212. frame->bitpool = (((avctx->bit_rate * frame->subbands * frame->blocks) / avctx->sample_rate)
  213. - 4 * frame->subbands * avctx->channels
  214. - (frame->mode == SBC_MODE_JOINT_STEREO)*frame->subbands - 32 + d/2) / d;
  215. if (avctx->global_quality > 0)
  216. frame->bitpool = avctx->global_quality / FF_QP2LAMBDA;
  217. avctx->frame_size = 4*((frame->subbands >> 3) + 1) * 4*(frame->blocks >> 2);
  218. }
  219. for (int i = 0; avctx->codec->supported_samplerates[i]; i++)
  220. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  221. frame->frequency = i;
  222. frame->channels = avctx->channels;
  223. frame->codesize = frame->subbands * frame->blocks * avctx->channels * 2;
  224. frame->crc_ctx = av_crc_get_table(AV_CRC_8_EBU);
  225. memset(&sbc->dsp.X, 0, sizeof(sbc->dsp.X));
  226. sbc->dsp.position = (SBC_X_BUFFER_SIZE - frame->subbands * 9) & ~7;
  227. sbc->dsp.increment = sbc->msbc ? 1 : 4;
  228. ff_sbcdsp_init(&sbc->dsp);
  229. return 0;
  230. }
  231. static int sbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  232. const AVFrame *av_frame, int *got_packet_ptr)
  233. {
  234. SBCEncContext *sbc = avctx->priv_data;
  235. struct sbc_frame *frame = &sbc->frame;
  236. uint8_t joint = frame->mode == SBC_MODE_JOINT_STEREO;
  237. uint8_t dual = frame->mode == SBC_MODE_DUAL_CHANNEL;
  238. int ret, j = 0;
  239. int frame_length = 4 + (4 * frame->subbands * frame->channels) / 8
  240. + ((frame->blocks * frame->bitpool * (1 + dual)
  241. + joint * frame->subbands) + 7) / 8;
  242. /* input must be large enough to encode a complete frame */
  243. if (av_frame->nb_samples * frame->channels * 2 < frame->codesize)
  244. return 0;
  245. if ((ret = ff_alloc_packet2(avctx, avpkt, frame_length, 0)) < 0)
  246. return ret;
  247. /* Select the needed input data processing function and call it */
  248. if (frame->subbands == 8)
  249. sbc->dsp.position = sbc->dsp.sbc_enc_process_input_8s(
  250. sbc->dsp.position, av_frame->data[0], sbc->dsp.X,
  251. frame->subbands * frame->blocks, frame->channels);
  252. else
  253. sbc->dsp.position = sbc->dsp.sbc_enc_process_input_4s(
  254. sbc->dsp.position, av_frame->data[0], sbc->dsp.X,
  255. frame->subbands * frame->blocks, frame->channels);
  256. sbc_analyze_audio(&sbc->dsp, &sbc->frame);
  257. if (frame->mode == JOINT_STEREO)
  258. j = sbc->dsp.sbc_calc_scalefactors_j(frame->sb_sample_f,
  259. frame->scale_factor,
  260. frame->blocks,
  261. frame->subbands);
  262. else
  263. sbc->dsp.sbc_calc_scalefactors(frame->sb_sample_f,
  264. frame->scale_factor,
  265. frame->blocks,
  266. frame->channels,
  267. frame->subbands);
  268. emms_c();
  269. sbc_pack_frame(avpkt, frame, j, sbc->msbc);
  270. *got_packet_ptr = 1;
  271. return 0;
  272. }
  273. #define OFFSET(x) offsetof(SBCEncContext, x)
  274. #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  275. static const AVOption options[] = {
  276. { "sbc_delay", "set maximum algorithmic latency",
  277. OFFSET(max_delay), AV_OPT_TYPE_DURATION, {.i64 = 13000}, 1000,13000, AE },
  278. { "msbc", "use mSBC mode (wideband speech mono SBC)",
  279. OFFSET(msbc), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AE },
  280. FF_AVCTX_PROFILE_OPTION("msbc", NULL, AUDIO, FF_PROFILE_SBC_MSBC)
  281. { NULL },
  282. };
  283. static const AVClass sbc_class = {
  284. .class_name = "sbc encoder",
  285. .item_name = av_default_item_name,
  286. .option = options,
  287. .version = LIBAVUTIL_VERSION_INT,
  288. };
  289. AVCodec ff_sbc_encoder = {
  290. .name = "sbc",
  291. .long_name = NULL_IF_CONFIG_SMALL("SBC (low-complexity subband codec)"),
  292. .type = AVMEDIA_TYPE_AUDIO,
  293. .id = AV_CODEC_ID_SBC,
  294. .priv_data_size = sizeof(SBCEncContext),
  295. .init = sbc_encode_init,
  296. .encode2 = sbc_encode_frame,
  297. .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
  298. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  299. .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
  300. AV_CH_LAYOUT_STEREO, 0},
  301. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
  302. AV_SAMPLE_FMT_NONE },
  303. .supported_samplerates = (const int[]) { 16000, 32000, 44100, 48000, 0 },
  304. .priv_class = &sbc_class,
  305. .profiles = NULL_IF_CONFIG_SMALL(ff_sbc_profiles),
  306. };