LCOV - code coverage report
Current view: top level - libavcodec - sbc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 138 0.0 %
Date: 2018-05-20 11:54:08 Functions: 0 2 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 common functions for the encoder and decoder
      31             :  */
      32             : 
      33             : #include "avcodec.h"
      34             : #include "sbc.h"
      35             : 
      36             : /* A2DP specification: Appendix B, page 69 */
      37             : static const int sbc_offset4[4][4] = {
      38             :     { -1, 0, 0, 0 },
      39             :     { -2, 0, 0, 1 },
      40             :     { -2, 0, 0, 1 },
      41             :     { -2, 0, 0, 1 }
      42             : };
      43             : 
      44             : /* A2DP specification: Appendix B, page 69 */
      45             : static const int sbc_offset8[4][8] = {
      46             :     { -2, 0, 0, 0, 0, 0, 0, 1 },
      47             :     { -3, 0, 0, 0, 0, 0, 1, 2 },
      48             :     { -4, 0, 0, 0, 0, 0, 1, 2 },
      49             :     { -4, 0, 0, 0, 0, 0, 1, 2 }
      50             : };
      51             : 
      52             : /*
      53             :  * Calculates the CRC-8 of the first len bits in data
      54             :  */
      55           0 : uint8_t ff_sbc_crc8(const AVCRC *ctx, const uint8_t *data, size_t len)
      56             : {
      57           0 :     size_t byte_length = len >> 3;
      58           0 :     int bit_length = len & 7;
      59             :     uint8_t crc;
      60             : 
      61           0 :     crc = av_crc(ctx, 0x0F, data, byte_length);
      62             : 
      63           0 :     if (bit_length) {
      64           0 :         uint8_t bits = data[byte_length];
      65           0 :         while (bit_length--) {
      66           0 :             int8_t mask = bits ^ crc;
      67           0 :             crc = (crc << 1) ^ ((mask >> 7) & 0x1D);
      68           0 :             bits <<= 1;
      69             :         }
      70             :     }
      71             : 
      72           0 :     return crc;
      73             : }
      74             : 
      75             : /*
      76             :  * Code straight from the spec to calculate the bits array
      77             :  * Takes a pointer to the frame in question and a pointer to the bits array
      78             :  */
      79           0 : void ff_sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8])
      80             : {
      81           0 :     int subbands = frame->subbands;
      82           0 :     uint8_t sf = frame->frequency;
      83             : 
      84           0 :     if (frame->mode == MONO || frame->mode == DUAL_CHANNEL) {
      85             :         int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice;
      86             :         int ch, sb;
      87             : 
      88           0 :         for (ch = 0; ch < frame->channels; ch++) {
      89           0 :             max_bitneed = 0;
      90           0 :             if (frame->allocation == SNR) {
      91           0 :                 for (sb = 0; sb < subbands; sb++) {
      92           0 :                     bitneed[ch][sb] = frame->scale_factor[ch][sb];
      93           0 :                     if (bitneed[ch][sb] > max_bitneed)
      94           0 :                         max_bitneed = bitneed[ch][sb];
      95             :                 }
      96             :             } else {
      97           0 :                 for (sb = 0; sb < subbands; sb++) {
      98           0 :                     if (frame->scale_factor[ch][sb] == 0)
      99           0 :                         bitneed[ch][sb] = -5;
     100             :                     else {
     101           0 :                         if (subbands == 4)
     102           0 :                             loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
     103             :                         else
     104           0 :                             loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
     105           0 :                         if (loudness > 0)
     106           0 :                             bitneed[ch][sb] = loudness / 2;
     107             :                         else
     108           0 :                             bitneed[ch][sb] = loudness;
     109             :                     }
     110           0 :                     if (bitneed[ch][sb] > max_bitneed)
     111           0 :                         max_bitneed = bitneed[ch][sb];
     112             :                 }
     113             :             }
     114             : 
     115           0 :             bitcount = 0;
     116           0 :             slicecount = 0;
     117           0 :             bitslice = max_bitneed + 1;
     118             :             do {
     119           0 :                 bitslice--;
     120           0 :                 bitcount += slicecount;
     121           0 :                 slicecount = 0;
     122           0 :                 for (sb = 0; sb < subbands; sb++) {
     123           0 :                     if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
     124           0 :                         slicecount++;
     125           0 :                     else if (bitneed[ch][sb] == bitslice + 1)
     126           0 :                         slicecount += 2;
     127             :                 }
     128           0 :             } while (bitcount + slicecount < frame->bitpool);
     129             : 
     130           0 :             if (bitcount + slicecount == frame->bitpool) {
     131           0 :                 bitcount += slicecount;
     132           0 :                 bitslice--;
     133             :             }
     134             : 
     135           0 :             for (sb = 0; sb < subbands; sb++) {
     136           0 :                 if (bitneed[ch][sb] < bitslice + 2)
     137           0 :                     bits[ch][sb] = 0;
     138             :                 else {
     139           0 :                     bits[ch][sb] = bitneed[ch][sb] - bitslice;
     140           0 :                     if (bits[ch][sb] > 16)
     141           0 :                         bits[ch][sb] = 16;
     142             :                 }
     143             :             }
     144             : 
     145           0 :             for (sb = 0; bitcount < frame->bitpool &&
     146           0 :                             sb < subbands; sb++) {
     147           0 :                 if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) {
     148           0 :                     bits[ch][sb]++;
     149           0 :                     bitcount++;
     150           0 :                 } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) {
     151           0 :                     bits[ch][sb] = 2;
     152           0 :                     bitcount += 2;
     153             :                 }
     154             :             }
     155             : 
     156           0 :             for (sb = 0; bitcount < frame->bitpool &&
     157           0 :                             sb < subbands; sb++) {
     158           0 :                 if (bits[ch][sb] < 16) {
     159           0 :                     bits[ch][sb]++;
     160           0 :                     bitcount++;
     161             :                 }
     162             :             }
     163             : 
     164             :         }
     165             : 
     166           0 :     } else if (frame->mode == STEREO || frame->mode == JOINT_STEREO) {
     167             :         int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice;
     168             :         int ch, sb;
     169             : 
     170           0 :         max_bitneed = 0;
     171           0 :         if (frame->allocation == SNR) {
     172           0 :             for (ch = 0; ch < 2; ch++) {
     173           0 :                 for (sb = 0; sb < subbands; sb++) {
     174           0 :                     bitneed[ch][sb] = frame->scale_factor[ch][sb];
     175           0 :                     if (bitneed[ch][sb] > max_bitneed)
     176           0 :                         max_bitneed = bitneed[ch][sb];
     177             :                 }
     178             :             }
     179             :         } else {
     180           0 :             for (ch = 0; ch < 2; ch++) {
     181           0 :                 for (sb = 0; sb < subbands; sb++) {
     182           0 :                     if (frame->scale_factor[ch][sb] == 0)
     183           0 :                         bitneed[ch][sb] = -5;
     184             :                     else {
     185           0 :                         if (subbands == 4)
     186           0 :                             loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
     187             :                         else
     188           0 :                             loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
     189           0 :                         if (loudness > 0)
     190           0 :                             bitneed[ch][sb] = loudness / 2;
     191             :                         else
     192           0 :                             bitneed[ch][sb] = loudness;
     193             :                     }
     194           0 :                     if (bitneed[ch][sb] > max_bitneed)
     195           0 :                         max_bitneed = bitneed[ch][sb];
     196             :                 }
     197             :             }
     198             :         }
     199             : 
     200           0 :         bitcount = 0;
     201           0 :         slicecount = 0;
     202           0 :         bitslice = max_bitneed + 1;
     203             :         do {
     204           0 :             bitslice--;
     205           0 :             bitcount += slicecount;
     206           0 :             slicecount = 0;
     207           0 :             for (ch = 0; ch < 2; ch++) {
     208           0 :                 for (sb = 0; sb < subbands; sb++) {
     209           0 :                     if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
     210           0 :                         slicecount++;
     211           0 :                     else if (bitneed[ch][sb] == bitslice + 1)
     212           0 :                         slicecount += 2;
     213             :                 }
     214             :             }
     215           0 :         } while (bitcount + slicecount < frame->bitpool);
     216             : 
     217           0 :         if (bitcount + slicecount == frame->bitpool) {
     218           0 :             bitcount += slicecount;
     219           0 :             bitslice--;
     220             :         }
     221             : 
     222           0 :         for (ch = 0; ch < 2; ch++) {
     223           0 :             for (sb = 0; sb < subbands; sb++) {
     224           0 :                 if (bitneed[ch][sb] < bitslice + 2) {
     225           0 :                     bits[ch][sb] = 0;
     226             :                 } else {
     227           0 :                     bits[ch][sb] = bitneed[ch][sb] - bitslice;
     228           0 :                     if (bits[ch][sb] > 16)
     229           0 :                         bits[ch][sb] = 16;
     230             :                 }
     231             :             }
     232             :         }
     233             : 
     234           0 :         ch = 0;
     235           0 :         sb = 0;
     236           0 :         while (bitcount < frame->bitpool) {
     237           0 :             if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) {
     238           0 :                 bits[ch][sb]++;
     239           0 :                 bitcount++;
     240           0 :             } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) {
     241           0 :                 bits[ch][sb] = 2;
     242           0 :                 bitcount += 2;
     243             :             }
     244           0 :             if (ch == 1) {
     245           0 :                 ch = 0;
     246           0 :                 sb++;
     247           0 :                 if (sb >= subbands)
     248           0 :                     break;
     249             :             } else
     250           0 :                 ch = 1;
     251             :         }
     252             : 
     253           0 :         ch = 0;
     254           0 :         sb = 0;
     255           0 :         while (bitcount < frame->bitpool) {
     256           0 :             if (bits[ch][sb] < 16) {
     257           0 :                 bits[ch][sb]++;
     258           0 :                 bitcount++;
     259             :             }
     260           0 :             if (ch == 1) {
     261           0 :                 ch = 0;
     262           0 :                 sb++;
     263           0 :                 if (sb >= subbands)
     264           0 :                     break;
     265             :             } else
     266           0 :                 ch = 1;
     267             :         }
     268             : 
     269             :     }
     270             : 
     271           0 : }

Generated by: LCOV version 1.13