LCOV - code coverage report
Current view: top level - libavcodec - sbcenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 166 0.0 %
Date: 2018-05-20 11:54:08 Functions: 0 4 0.0 %

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

Generated by: LCOV version 1.13