2007-01-31 16:52:52 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Bluetooth low-complexity, subband codec (SBC) library
|
|
|
|
*
|
2010-01-07 17:02:51 +08:00
|
|
|
* Copyright (C) 2008-2010 Nokia Corporation
|
2010-01-02 09:08:17 +08:00
|
|
|
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
|
2007-03-17 21:37:31 +08:00
|
|
|
* Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
|
2008-01-19 23:56:52 +08:00
|
|
|
* Copyright (C) 2005-2008 Brad Midgley <bmidgley@xmission.com>
|
2007-01-31 16:52:52 +08:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-03-17 21:37:31 +08:00
|
|
|
/* todo items:
|
|
|
|
|
2007-11-01 23:27:38 +08:00
|
|
|
use a log2 table for byte integer scale factors calculation (sum log2 results
|
|
|
|
for high and low bytes) fill bitpool by 16 bits instead of one at a time in
|
|
|
|
bits allocation/bitpool generation port to the dsp
|
2007-03-17 21:37:31 +08:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-01-31 16:52:52 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2007-03-17 21:37:31 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
2008-12-28 09:22:59 +08:00
|
|
|
#include <limits.h>
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2007-02-21 22:23:25 +08:00
|
|
|
#include "sbc_math.h"
|
|
|
|
#include "sbc_tables.h"
|
|
|
|
|
2007-01-31 16:52:52 +08:00
|
|
|
#include "sbc.h"
|
2009-01-16 01:11:23 +08:00
|
|
|
#include "sbc_primitives.h"
|
2007-03-17 21:37:31 +08:00
|
|
|
|
|
|
|
#define SBC_SYNCWORD 0x9C
|
|
|
|
|
2007-11-01 23:27:38 +08:00
|
|
|
/* This structure contains an unpacked SBC frame.
|
2007-03-17 21:37:31 +08:00
|
|
|
Yes, there is probably quite some unused space herein */
|
|
|
|
struct sbc_frame {
|
2008-02-20 03:47:25 +08:00
|
|
|
uint8_t frequency;
|
|
|
|
uint8_t block_mode;
|
2007-03-17 21:37:31 +08:00
|
|
|
uint8_t blocks;
|
|
|
|
enum {
|
2008-02-20 03:47:25 +08:00
|
|
|
MONO = SBC_MODE_MONO,
|
|
|
|
DUAL_CHANNEL = SBC_MODE_DUAL_CHANNEL,
|
|
|
|
STEREO = SBC_MODE_STEREO,
|
|
|
|
JOINT_STEREO = SBC_MODE_JOINT_STEREO
|
|
|
|
} mode;
|
2007-03-17 21:37:31 +08:00
|
|
|
uint8_t channels;
|
|
|
|
enum {
|
|
|
|
LOUDNESS = SBC_AM_LOUDNESS,
|
|
|
|
SNR = SBC_AM_SNR
|
2008-02-20 03:47:25 +08:00
|
|
|
} allocation;
|
|
|
|
uint8_t subband_mode;
|
2007-03-17 21:37:31 +08:00
|
|
|
uint8_t subbands;
|
|
|
|
uint8_t bitpool;
|
2009-01-05 20:26:08 +08:00
|
|
|
uint16_t codesize;
|
2007-11-13 02:15:59 +08:00
|
|
|
uint8_t length;
|
2007-11-01 23:27:38 +08:00
|
|
|
|
|
|
|
/* bit number x set means joint stereo has been used in subband x */
|
2008-02-20 03:47:25 +08:00
|
|
|
uint8_t joint;
|
2007-11-01 23:27:38 +08:00
|
|
|
|
|
|
|
/* only the lower 4 bits of every element are to be used */
|
2009-01-29 08:17:36 +08:00
|
|
|
uint32_t scale_factor[2][8];
|
2007-11-01 23:27:38 +08:00
|
|
|
|
|
|
|
/* raw integer subband samples in the frame */
|
2009-01-16 01:45:36 +08:00
|
|
|
int32_t SBC_ALIGNED sb_sample_f[16][2][8];
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2009-01-16 01:45:36 +08:00
|
|
|
/* modified subband samples */
|
|
|
|
int32_t SBC_ALIGNED sb_sample[16][2][8];
|
|
|
|
|
|
|
|
/* original pcm audio samples */
|
|
|
|
int16_t SBC_ALIGNED pcm_sample[2][16*8];
|
2007-03-17 21:37:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct sbc_decoder_state {
|
|
|
|
int subbands;
|
|
|
|
int32_t V[2][170];
|
|
|
|
int offset[2][16];
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculates the CRC-8 of the first len bits in data
|
|
|
|
*/
|
|
|
|
static const uint8_t crc_table[256] = {
|
|
|
|
0x00, 0x1D, 0x3A, 0x27, 0x74, 0x69, 0x4E, 0x53,
|
|
|
|
0xE8, 0xF5, 0xD2, 0xCF, 0x9C, 0x81, 0xA6, 0xBB,
|
|
|
|
0xCD, 0xD0, 0xF7, 0xEA, 0xB9, 0xA4, 0x83, 0x9E,
|
|
|
|
0x25, 0x38, 0x1F, 0x02, 0x51, 0x4C, 0x6B, 0x76,
|
|
|
|
0x87, 0x9A, 0xBD, 0xA0, 0xF3, 0xEE, 0xC9, 0xD4,
|
|
|
|
0x6F, 0x72, 0x55, 0x48, 0x1B, 0x06, 0x21, 0x3C,
|
|
|
|
0x4A, 0x57, 0x70, 0x6D, 0x3E, 0x23, 0x04, 0x19,
|
|
|
|
0xA2, 0xBF, 0x98, 0x85, 0xD6, 0xCB, 0xEC, 0xF1,
|
|
|
|
0x13, 0x0E, 0x29, 0x34, 0x67, 0x7A, 0x5D, 0x40,
|
|
|
|
0xFB, 0xE6, 0xC1, 0xDC, 0x8F, 0x92, 0xB5, 0xA8,
|
|
|
|
0xDE, 0xC3, 0xE4, 0xF9, 0xAA, 0xB7, 0x90, 0x8D,
|
|
|
|
0x36, 0x2B, 0x0C, 0x11, 0x42, 0x5F, 0x78, 0x65,
|
|
|
|
0x94, 0x89, 0xAE, 0xB3, 0xE0, 0xFD, 0xDA, 0xC7,
|
|
|
|
0x7C, 0x61, 0x46, 0x5B, 0x08, 0x15, 0x32, 0x2F,
|
|
|
|
0x59, 0x44, 0x63, 0x7E, 0x2D, 0x30, 0x17, 0x0A,
|
|
|
|
0xB1, 0xAC, 0x8B, 0x96, 0xC5, 0xD8, 0xFF, 0xE2,
|
|
|
|
0x26, 0x3B, 0x1C, 0x01, 0x52, 0x4F, 0x68, 0x75,
|
|
|
|
0xCE, 0xD3, 0xF4, 0xE9, 0xBA, 0xA7, 0x80, 0x9D,
|
|
|
|
0xEB, 0xF6, 0xD1, 0xCC, 0x9F, 0x82, 0xA5, 0xB8,
|
|
|
|
0x03, 0x1E, 0x39, 0x24, 0x77, 0x6A, 0x4D, 0x50,
|
|
|
|
0xA1, 0xBC, 0x9B, 0x86, 0xD5, 0xC8, 0xEF, 0xF2,
|
|
|
|
0x49, 0x54, 0x73, 0x6E, 0x3D, 0x20, 0x07, 0x1A,
|
|
|
|
0x6C, 0x71, 0x56, 0x4B, 0x18, 0x05, 0x22, 0x3F,
|
|
|
|
0x84, 0x99, 0xBE, 0xA3, 0xF0, 0xED, 0xCA, 0xD7,
|
|
|
|
0x35, 0x28, 0x0F, 0x12, 0x41, 0x5C, 0x7B, 0x66,
|
|
|
|
0xDD, 0xC0, 0xE7, 0xFA, 0xA9, 0xB4, 0x93, 0x8E,
|
|
|
|
0xF8, 0xE5, 0xC2, 0xDF, 0x8C, 0x91, 0xB6, 0xAB,
|
|
|
|
0x10, 0x0D, 0x2A, 0x37, 0x64, 0x79, 0x5E, 0x43,
|
|
|
|
0xB2, 0xAF, 0x88, 0x95, 0xC6, 0xDB, 0xFC, 0xE1,
|
|
|
|
0x5A, 0x47, 0x60, 0x7D, 0x2E, 0x33, 0x14, 0x09,
|
|
|
|
0x7F, 0x62, 0x45, 0x58, 0x0B, 0x16, 0x31, 0x2C,
|
|
|
|
0x97, 0x8A, 0xAD, 0xB0, 0xE3, 0xFE, 0xD9, 0xC4
|
|
|
|
};
|
|
|
|
|
2007-11-02 17:05:57 +08:00
|
|
|
static uint8_t sbc_crc8(const uint8_t *data, size_t len)
|
2007-03-17 21:37:31 +08:00
|
|
|
{
|
|
|
|
uint8_t crc = 0x0f;
|
|
|
|
size_t i;
|
|
|
|
uint8_t octet;
|
|
|
|
|
|
|
|
for (i = 0; i < len / 8; i++)
|
|
|
|
crc = crc_table[crc ^ data[i]];
|
|
|
|
|
|
|
|
octet = data[i];
|
|
|
|
for (i = 0; i < len % 8; i++) {
|
|
|
|
char bit = ((octet ^ crc) & 0x80) >> 7;
|
|
|
|
|
|
|
|
crc = ((crc & 0x7f) << 1) ^ (bit ? 0x1d : 0);
|
|
|
|
|
|
|
|
octet = octet << 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return crc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-11-01 23:27:38 +08:00
|
|
|
* Code straight from the spec to calculate the bits array
|
|
|
|
* Takes a pointer to the frame in question, a pointer to the bits array and
|
|
|
|
* the sampling frequency (as 2 bit integer)
|
2007-03-17 21:37:31 +08:00
|
|
|
*/
|
2008-02-20 03:47:25 +08:00
|
|
|
static void sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8])
|
2007-03-17 21:37:31 +08:00
|
|
|
{
|
2008-02-20 03:47:25 +08:00
|
|
|
uint8_t sf = frame->frequency;
|
|
|
|
|
|
|
|
if (frame->mode == MONO || frame->mode == DUAL_CHANNEL) {
|
2007-03-17 21:37:31 +08:00
|
|
|
int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice;
|
|
|
|
int ch, sb;
|
|
|
|
|
|
|
|
for (ch = 0; ch < frame->channels; ch++) {
|
2007-11-27 14:22:48 +08:00
|
|
|
max_bitneed = 0;
|
2008-02-20 03:47:25 +08:00
|
|
|
if (frame->allocation == SNR) {
|
2007-11-27 14:22:48 +08:00
|
|
|
for (sb = 0; sb < frame->subbands; sb++) {
|
2007-03-17 21:37:31 +08:00
|
|
|
bitneed[ch][sb] = frame->scale_factor[ch][sb];
|
2007-11-27 14:22:48 +08:00
|
|
|
if (bitneed[ch][sb] > max_bitneed)
|
|
|
|
max_bitneed = bitneed[ch][sb];
|
|
|
|
}
|
2007-03-17 21:37:31 +08:00
|
|
|
} else {
|
|
|
|
for (sb = 0; sb < frame->subbands; sb++) {
|
2007-11-01 23:27:38 +08:00
|
|
|
if (frame->scale_factor[ch][sb] == 0)
|
2007-03-17 21:37:31 +08:00
|
|
|
bitneed[ch][sb] = -5;
|
2007-11-01 23:27:38 +08:00
|
|
|
else {
|
|
|
|
if (frame->subbands == 4)
|
2007-03-17 21:37:31 +08:00
|
|
|
loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
|
2007-11-01 23:27:38 +08:00
|
|
|
else
|
2007-03-17 21:37:31 +08:00
|
|
|
loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
|
2007-11-01 23:27:38 +08:00
|
|
|
if (loudness > 0)
|
2007-03-17 21:37:31 +08:00
|
|
|
bitneed[ch][sb] = loudness / 2;
|
2007-11-01 23:27:38 +08:00
|
|
|
else
|
2007-03-17 21:37:31 +08:00
|
|
|
bitneed[ch][sb] = loudness;
|
|
|
|
}
|
2007-11-27 14:22:48 +08:00
|
|
|
if (bitneed[ch][sb] > max_bitneed)
|
|
|
|
max_bitneed = bitneed[ch][sb];
|
2007-03-17 21:37:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bitcount = 0;
|
|
|
|
slicecount = 0;
|
|
|
|
bitslice = max_bitneed + 1;
|
|
|
|
do {
|
|
|
|
bitslice--;
|
|
|
|
bitcount += slicecount;
|
|
|
|
slicecount = 0;
|
|
|
|
for (sb = 0; sb < frame->subbands; sb++) {
|
2007-11-01 23:27:38 +08:00
|
|
|
if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
|
2007-03-17 21:37:31 +08:00
|
|
|
slicecount++;
|
2007-11-01 23:27:38 +08:00
|
|
|
else if (bitneed[ch][sb] == bitslice + 1)
|
2007-03-17 21:37:31 +08:00
|
|
|
slicecount += 2;
|
|
|
|
}
|
|
|
|
} while (bitcount + slicecount < frame->bitpool);
|
|
|
|
|
|
|
|
if (bitcount + slicecount == frame->bitpool) {
|
|
|
|
bitcount += slicecount;
|
|
|
|
bitslice--;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (sb = 0; sb < frame->subbands; sb++) {
|
2007-11-01 23:27:38 +08:00
|
|
|
if (bitneed[ch][sb] < bitslice + 2)
|
2007-03-17 21:37:31 +08:00
|
|
|
bits[ch][sb] = 0;
|
2007-11-01 23:27:38 +08:00
|
|
|
else {
|
2007-03-17 21:37:31 +08:00
|
|
|
bits[ch][sb] = bitneed[ch][sb] - bitslice;
|
|
|
|
if (bits[ch][sb] > 16)
|
|
|
|
bits[ch][sb] = 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-27 14:22:48 +08:00
|
|
|
for (sb = 0; bitcount < frame->bitpool && sb < frame->subbands; sb++) {
|
2007-03-17 21:37:31 +08:00
|
|
|
if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) {
|
|
|
|
bits[ch][sb]++;
|
|
|
|
bitcount++;
|
|
|
|
} else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) {
|
|
|
|
bits[ch][sb] = 2;
|
|
|
|
bitcount += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-27 14:22:48 +08:00
|
|
|
for (sb = 0; bitcount < frame->bitpool && sb < frame->subbands; sb++) {
|
2007-03-17 21:37:31 +08:00
|
|
|
if (bits[ch][sb] < 16) {
|
|
|
|
bits[ch][sb]++;
|
|
|
|
bitcount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
} else if (frame->mode == STEREO || frame->mode == JOINT_STEREO) {
|
2007-03-17 21:37:31 +08:00
|
|
|
int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice;
|
|
|
|
int ch, sb;
|
|
|
|
|
2007-11-27 14:47:38 +08:00
|
|
|
max_bitneed = 0;
|
2008-02-20 03:47:25 +08:00
|
|
|
if (frame->allocation == SNR) {
|
2007-03-17 21:37:31 +08:00
|
|
|
for (ch = 0; ch < 2; ch++) {
|
2007-11-27 14:47:38 +08:00
|
|
|
for (sb = 0; sb < frame->subbands; sb++) {
|
2007-03-17 21:37:31 +08:00
|
|
|
bitneed[ch][sb] = frame->scale_factor[ch][sb];
|
2007-11-27 14:47:38 +08:00
|
|
|
if (bitneed[ch][sb] > max_bitneed)
|
|
|
|
max_bitneed = bitneed[ch][sb];
|
|
|
|
}
|
2007-03-17 21:37:31 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (ch = 0; ch < 2; ch++) {
|
|
|
|
for (sb = 0; sb < frame->subbands; sb++) {
|
2007-11-01 23:27:38 +08:00
|
|
|
if (frame->scale_factor[ch][sb] == 0)
|
2007-03-17 21:37:31 +08:00
|
|
|
bitneed[ch][sb] = -5;
|
2007-11-01 23:27:38 +08:00
|
|
|
else {
|
|
|
|
if (frame->subbands == 4)
|
2007-03-17 21:37:31 +08:00
|
|
|
loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
|
2007-11-01 23:27:38 +08:00
|
|
|
else
|
2007-03-17 21:37:31 +08:00
|
|
|
loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
|
2007-11-01 23:27:38 +08:00
|
|
|
if (loudness > 0)
|
2007-03-17 21:37:31 +08:00
|
|
|
bitneed[ch][sb] = loudness / 2;
|
2007-11-01 23:27:38 +08:00
|
|
|
else
|
2007-03-17 21:37:31 +08:00
|
|
|
bitneed[ch][sb] = loudness;
|
|
|
|
}
|
2007-11-27 14:47:38 +08:00
|
|
|
if (bitneed[ch][sb] > max_bitneed)
|
|
|
|
max_bitneed = bitneed[ch][sb];
|
2007-03-17 21:37:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bitcount = 0;
|
|
|
|
slicecount = 0;
|
|
|
|
bitslice = max_bitneed + 1;
|
|
|
|
do {
|
|
|
|
bitslice--;
|
|
|
|
bitcount += slicecount;
|
|
|
|
slicecount = 0;
|
|
|
|
for (ch = 0; ch < 2; ch++) {
|
|
|
|
for (sb = 0; sb < frame->subbands; sb++) {
|
2007-11-01 23:27:38 +08:00
|
|
|
if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
|
2007-03-17 21:37:31 +08:00
|
|
|
slicecount++;
|
2007-11-01 23:27:38 +08:00
|
|
|
else if (bitneed[ch][sb] == bitslice + 1)
|
2007-03-17 21:37:31 +08:00
|
|
|
slicecount += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (bitcount + slicecount < frame->bitpool);
|
2007-11-02 17:05:57 +08:00
|
|
|
|
2007-03-17 21:37:31 +08:00
|
|
|
if (bitcount + slicecount == frame->bitpool) {
|
|
|
|
bitcount += slicecount;
|
|
|
|
bitslice--;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ch = 0; ch < 2; ch++) {
|
|
|
|
for (sb = 0; sb < frame->subbands; sb++) {
|
|
|
|
if (bitneed[ch][sb] < bitslice + 2) {
|
|
|
|
bits[ch][sb] = 0;
|
|
|
|
} else {
|
|
|
|
bits[ch][sb] = bitneed[ch][sb] - bitslice;
|
|
|
|
if (bits[ch][sb] > 16)
|
|
|
|
bits[ch][sb] = 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ch = 0;
|
|
|
|
sb = 0;
|
2007-11-27 14:47:38 +08:00
|
|
|
while (bitcount < frame->bitpool) {
|
2007-03-17 21:37:31 +08:00
|
|
|
if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) {
|
|
|
|
bits[ch][sb]++;
|
|
|
|
bitcount++;
|
|
|
|
} else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) {
|
|
|
|
bits[ch][sb] = 2;
|
|
|
|
bitcount += 2;
|
|
|
|
}
|
|
|
|
if (ch == 1) {
|
|
|
|
ch = 0;
|
|
|
|
sb++;
|
2007-11-27 14:47:38 +08:00
|
|
|
if (sb >= frame->subbands) break;
|
2007-11-01 23:27:38 +08:00
|
|
|
} else
|
2007-03-17 21:37:31 +08:00
|
|
|
ch = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ch = 0;
|
|
|
|
sb = 0;
|
2007-11-27 14:47:38 +08:00
|
|
|
while (bitcount < frame->bitpool) {
|
2007-03-17 21:37:31 +08:00
|
|
|
if (bits[ch][sb] < 16) {
|
|
|
|
bits[ch][sb]++;
|
|
|
|
bitcount++;
|
|
|
|
}
|
|
|
|
if (ch == 1) {
|
|
|
|
ch = 0;
|
|
|
|
sb++;
|
2007-11-27 14:47:38 +08:00
|
|
|
if (sb >= frame->subbands) break;
|
2007-11-01 23:27:38 +08:00
|
|
|
} else
|
2007-03-17 21:37:31 +08:00
|
|
|
ch = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-11-01 23:27:38 +08:00
|
|
|
/*
|
2007-03-17 21:37:31 +08:00
|
|
|
* Unpacks a SBC frame at the beginning of the stream in data,
|
|
|
|
* which has at most len bytes into frame.
|
|
|
|
* Returns the length in bytes of the packed frame, or a negative
|
|
|
|
* value on error. The error codes are:
|
|
|
|
*
|
|
|
|
* -1 Data stream too short
|
|
|
|
* -2 Sync byte incorrect
|
|
|
|
* -3 CRC8 incorrect
|
|
|
|
* -4 Bitpool value out of bounds
|
|
|
|
*/
|
2007-11-02 17:05:57 +08:00
|
|
|
static int sbc_unpack_frame(const uint8_t *data, struct sbc_frame *frame,
|
2009-04-17 07:55:42 +08:00
|
|
|
size_t len)
|
2007-03-17 21:37:31 +08:00
|
|
|
{
|
2009-01-30 07:02:58 +08:00
|
|
|
unsigned int consumed;
|
2007-11-01 23:27:38 +08:00
|
|
|
/* Will copy the parts of the header that are relevant to crc
|
|
|
|
* calculation here */
|
2007-03-17 21:37:31 +08:00
|
|
|
uint8_t crc_header[11] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
|
|
int crc_pos = 0;
|
|
|
|
int32_t temp;
|
|
|
|
|
2008-03-08 13:21:26 +08:00
|
|
|
int audio_sample;
|
2007-11-01 23:27:38 +08:00
|
|
|
int ch, sb, blk, bit; /* channel, subband, block and bit standard
|
|
|
|
counters */
|
2007-03-17 21:37:31 +08:00
|
|
|
int bits[2][8]; /* bits distribution */
|
2008-02-29 11:56:22 +08:00
|
|
|
uint32_t levels[2][8]; /* levels derived from that */
|
2007-03-17 21:37:31 +08:00
|
|
|
|
|
|
|
if (len < 4)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (data[0] != SBC_SYNCWORD)
|
|
|
|
return -2;
|
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
frame->frequency = (data[1] >> 6) & 0x03;
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
frame->block_mode = (data[1] >> 4) & 0x03;
|
|
|
|
switch (frame->block_mode) {
|
|
|
|
case SBC_BLK_4:
|
2007-03-17 21:37:31 +08:00
|
|
|
frame->blocks = 4;
|
|
|
|
break;
|
2008-02-20 03:47:25 +08:00
|
|
|
case SBC_BLK_8:
|
2007-03-17 21:37:31 +08:00
|
|
|
frame->blocks = 8;
|
|
|
|
break;
|
2008-02-20 03:47:25 +08:00
|
|
|
case SBC_BLK_12:
|
2007-03-17 21:37:31 +08:00
|
|
|
frame->blocks = 12;
|
|
|
|
break;
|
2008-02-20 03:47:25 +08:00
|
|
|
case SBC_BLK_16:
|
2007-03-17 21:37:31 +08:00
|
|
|
frame->blocks = 16;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
frame->mode = (data[1] >> 2) & 0x03;
|
|
|
|
switch (frame->mode) {
|
2007-03-17 21:37:31 +08:00
|
|
|
case MONO:
|
|
|
|
frame->channels = 1;
|
|
|
|
break;
|
|
|
|
case DUAL_CHANNEL: /* fall-through */
|
|
|
|
case STEREO:
|
|
|
|
case JOINT_STEREO:
|
|
|
|
frame->channels = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
frame->allocation = (data[1] >> 1) & 0x01;
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
frame->subband_mode = (data[1] & 0x01);
|
|
|
|
frame->subbands = frame->subband_mode ? 8 : 4;
|
2007-03-17 21:37:31 +08:00
|
|
|
|
|
|
|
frame->bitpool = data[2];
|
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
if ((frame->mode == MONO || frame->mode == DUAL_CHANNEL) &&
|
2007-11-02 17:05:57 +08:00
|
|
|
frame->bitpool > 16 * frame->subbands)
|
|
|
|
return -4;
|
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
if ((frame->mode == STEREO || frame->mode == JOINT_STEREO) &&
|
2007-11-02 17:05:57 +08:00
|
|
|
frame->bitpool > 32 * frame->subbands)
|
2007-03-17 21:37:31 +08:00
|
|
|
return -4;
|
|
|
|
|
|
|
|
/* data[3] is crc, we're checking it later */
|
|
|
|
|
|
|
|
consumed = 32;
|
|
|
|
|
|
|
|
crc_header[0] = data[1];
|
|
|
|
crc_header[1] = data[2];
|
|
|
|
crc_pos = 16;
|
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
if (frame->mode == JOINT_STEREO) {
|
2007-03-17 21:37:31 +08:00
|
|
|
if (len * 8 < consumed + frame->subbands)
|
|
|
|
return -1;
|
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
frame->joint = 0x00;
|
2007-11-01 23:27:38 +08:00
|
|
|
for (sb = 0; sb < frame->subbands - 1; sb++)
|
2008-02-20 03:47:25 +08:00
|
|
|
frame->joint |= ((data[4] >> (7 - sb)) & 0x01) << sb;
|
2007-11-01 23:27:38 +08:00
|
|
|
if (frame->subbands == 4)
|
2007-03-17 21:37:31 +08:00
|
|
|
crc_header[crc_pos / 8] = data[4] & 0xf0;
|
2007-11-01 23:27:38 +08:00
|
|
|
else
|
2007-03-17 21:37:31 +08:00
|
|
|
crc_header[crc_pos / 8] = data[4];
|
|
|
|
|
|
|
|
consumed += frame->subbands;
|
|
|
|
crc_pos += frame->subbands;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len * 8 < consumed + (4 * frame->subbands * frame->channels))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (ch = 0; ch < frame->channels; ch++) {
|
|
|
|
for (sb = 0; sb < frame->subbands; sb++) {
|
|
|
|
/* FIXME assert(consumed % 4 == 0); */
|
2007-11-01 23:27:38 +08:00
|
|
|
frame->scale_factor[ch][sb] =
|
|
|
|
(data[consumed >> 3] >> (4 - (consumed & 0x7))) & 0x0F;
|
|
|
|
crc_header[crc_pos >> 3] |=
|
|
|
|
frame->scale_factor[ch][sb] << (4 - (crc_pos & 0x7));
|
2007-03-17 21:37:31 +08:00
|
|
|
|
|
|
|
consumed += 4;
|
|
|
|
crc_pos += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data[3] != sbc_crc8(crc_header, crc_pos))
|
|
|
|
return -3;
|
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
sbc_calculate_bits(frame, bits);
|
2007-03-17 21:37:31 +08:00
|
|
|
|
|
|
|
for (ch = 0; ch < frame->channels; ch++) {
|
2007-11-01 23:27:38 +08:00
|
|
|
for (sb = 0; sb < frame->subbands; sb++)
|
2007-03-17 21:37:31 +08:00
|
|
|
levels[ch][sb] = (1 << bits[ch][sb]) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (blk = 0; blk < frame->blocks; blk++) {
|
|
|
|
for (ch = 0; ch < frame->channels; ch++) {
|
|
|
|
for (sb = 0; sb < frame->subbands; sb++) {
|
|
|
|
if (levels[ch][sb] > 0) {
|
2008-03-08 13:21:26 +08:00
|
|
|
audio_sample = 0;
|
|
|
|
for (bit = 0; bit < bits[ch][sb]; bit++) {
|
|
|
|
if (consumed > len * 8)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((data[consumed >> 3] >> (7 - (consumed & 0x7))) & 0x01)
|
|
|
|
audio_sample |= 1 << (bits[ch][sb] - bit - 1);
|
|
|
|
|
|
|
|
consumed++;
|
|
|
|
}
|
|
|
|
|
2007-11-01 23:27:38 +08:00
|
|
|
frame->sb_sample[blk][ch][sb] =
|
2008-03-08 13:21:26 +08:00
|
|
|
(((audio_sample << 1) | 1) << frame->scale_factor[ch][sb]) /
|
2008-02-16 02:06:32 +08:00
|
|
|
levels[ch][sb] - (1 << frame->scale_factor[ch][sb]);
|
2007-11-01 23:27:38 +08:00
|
|
|
} else
|
2007-03-17 21:37:31 +08:00
|
|
|
frame->sb_sample[blk][ch][sb] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
if (frame->mode == JOINT_STEREO) {
|
2007-03-17 21:37:31 +08:00
|
|
|
for (blk = 0; blk < frame->blocks; blk++) {
|
|
|
|
for (sb = 0; sb < frame->subbands; sb++) {
|
2008-02-20 03:47:25 +08:00
|
|
|
if (frame->joint & (0x01 << sb)) {
|
2007-11-01 23:27:38 +08:00
|
|
|
temp = frame->sb_sample[blk][0][sb] +
|
|
|
|
frame->sb_sample[blk][1][sb];
|
|
|
|
frame->sb_sample[blk][1][sb] =
|
|
|
|
frame->sb_sample[blk][0][sb] -
|
|
|
|
frame->sb_sample[blk][1][sb];
|
2007-03-17 21:37:31 +08:00
|
|
|
frame->sb_sample[blk][0][sb] = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((consumed & 0x7) != 0)
|
|
|
|
consumed += 8 - (consumed & 0x7);
|
|
|
|
|
|
|
|
return consumed >> 3;
|
|
|
|
}
|
|
|
|
|
2007-11-01 23:27:38 +08:00
|
|
|
static void sbc_decoder_init(struct sbc_decoder_state *state,
|
2009-04-17 07:55:42 +08:00
|
|
|
const struct sbc_frame *frame)
|
2007-03-17 21:37:31 +08:00
|
|
|
{
|
|
|
|
int i, ch;
|
|
|
|
|
|
|
|
memset(state->V, 0, sizeof(state->V));
|
|
|
|
state->subbands = frame->subbands;
|
|
|
|
|
|
|
|
for (ch = 0; ch < 2; ch++)
|
|
|
|
for (i = 0; i < frame->subbands * 2; i++)
|
|
|
|
state->offset[ch][i] = (10 * i + 10);
|
|
|
|
}
|
|
|
|
|
2009-04-17 23:27:38 +08:00
|
|
|
static SBC_ALWAYS_INLINE int16_t sbc_clip16(int32_t s)
|
|
|
|
{
|
|
|
|
if (s > 0x7FFF)
|
|
|
|
return 0x7FFF;
|
|
|
|
else if (s < -0x8000)
|
|
|
|
return -0x8000;
|
|
|
|
else
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2007-03-17 21:37:31 +08:00
|
|
|
static inline void sbc_synthesize_four(struct sbc_decoder_state *state,
|
|
|
|
struct sbc_frame *frame, int ch, int blk)
|
|
|
|
{
|
2008-03-06 22:04:43 +08:00
|
|
|
int i, k, idx;
|
|
|
|
int32_t *v = state->V[ch];
|
|
|
|
int *offset = state->offset[ch];
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2007-11-02 17:05:57 +08:00
|
|
|
for (i = 0; i < 8; i++) {
|
2007-03-17 21:37:31 +08:00
|
|
|
/* Shifting */
|
2008-03-06 22:04:43 +08:00
|
|
|
offset[i]--;
|
|
|
|
if (offset[i] < 0) {
|
|
|
|
offset[i] = 79;
|
|
|
|
memcpy(v + 80, v, 9 * sizeof(*v));
|
2007-03-17 21:37:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Distribute the new matrix value to the shifted position */
|
2008-03-06 22:04:43 +08:00
|
|
|
v[offset[i]] = SCALE4_STAGED1(
|
|
|
|
MULA(synmatrix4[i][0], frame->sb_sample[blk][ch][0],
|
|
|
|
MULA(synmatrix4[i][1], frame->sb_sample[blk][ch][1],
|
|
|
|
MULA(synmatrix4[i][2], frame->sb_sample[blk][ch][2],
|
|
|
|
MUL (synmatrix4[i][3], frame->sb_sample[blk][ch][3])))));
|
2007-03-17 21:37:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the samples */
|
2008-03-06 22:04:43 +08:00
|
|
|
for (idx = 0, i = 0; i < 4; i++, idx += 5) {
|
2007-03-17 21:37:31 +08:00
|
|
|
k = (i + 4) & 0xf;
|
2007-11-01 23:27:38 +08:00
|
|
|
|
|
|
|
/* Store in output, Q0 */
|
2009-04-17 23:27:38 +08:00
|
|
|
frame->pcm_sample[ch][blk * 4 + i] = sbc_clip16(SCALE4_STAGED1(
|
2008-03-06 22:04:43 +08:00
|
|
|
MULA(v[offset[i] + 0], sbc_proto_4_40m0[idx + 0],
|
|
|
|
MULA(v[offset[k] + 1], sbc_proto_4_40m1[idx + 0],
|
|
|
|
MULA(v[offset[i] + 2], sbc_proto_4_40m0[idx + 1],
|
|
|
|
MULA(v[offset[k] + 3], sbc_proto_4_40m1[idx + 1],
|
|
|
|
MULA(v[offset[i] + 4], sbc_proto_4_40m0[idx + 2],
|
|
|
|
MULA(v[offset[k] + 5], sbc_proto_4_40m1[idx + 2],
|
|
|
|
MULA(v[offset[i] + 6], sbc_proto_4_40m0[idx + 3],
|
|
|
|
MULA(v[offset[k] + 7], sbc_proto_4_40m1[idx + 3],
|
|
|
|
MULA(v[offset[i] + 8], sbc_proto_4_40m0[idx + 4],
|
2009-04-17 23:27:38 +08:00
|
|
|
MUL( v[offset[k] + 9], sbc_proto_4_40m1[idx + 4]))))))))))));
|
2007-03-17 21:37:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void sbc_synthesize_eight(struct sbc_decoder_state *state,
|
|
|
|
struct sbc_frame *frame, int ch, int blk)
|
|
|
|
{
|
|
|
|
int i, j, k, idx;
|
2008-03-06 22:04:43 +08:00
|
|
|
int *offset = state->offset[ch];
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2007-11-02 17:05:57 +08:00
|
|
|
for (i = 0; i < 16; i++) {
|
2007-03-17 21:37:31 +08:00
|
|
|
/* Shifting */
|
2008-03-06 22:04:43 +08:00
|
|
|
offset[i]--;
|
|
|
|
if (offset[i] < 0) {
|
|
|
|
offset[i] = 159;
|
2007-11-02 17:05:57 +08:00
|
|
|
for (j = 0; j < 9; j++)
|
2008-03-06 22:04:43 +08:00
|
|
|
state->V[ch][j + 160] = state->V[ch][j];
|
2007-03-17 21:37:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Distribute the new matrix value to the shifted position */
|
2008-03-06 22:04:43 +08:00
|
|
|
state->V[ch][offset[i]] = SCALE8_STAGED1(
|
|
|
|
MULA(synmatrix8[i][0], frame->sb_sample[blk][ch][0],
|
|
|
|
MULA(synmatrix8[i][1], frame->sb_sample[blk][ch][1],
|
|
|
|
MULA(synmatrix8[i][2], frame->sb_sample[blk][ch][2],
|
|
|
|
MULA(synmatrix8[i][3], frame->sb_sample[blk][ch][3],
|
|
|
|
MULA(synmatrix8[i][4], frame->sb_sample[blk][ch][4],
|
|
|
|
MULA(synmatrix8[i][5], frame->sb_sample[blk][ch][5],
|
|
|
|
MULA(synmatrix8[i][6], frame->sb_sample[blk][ch][6],
|
|
|
|
MUL( synmatrix8[i][7], frame->sb_sample[blk][ch][7])))))))));
|
2007-03-17 21:37:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the samples */
|
2008-03-06 22:04:43 +08:00
|
|
|
for (idx = 0, i = 0; i < 8; i++, idx += 5) {
|
2007-03-17 21:37:31 +08:00
|
|
|
k = (i + 8) & 0xf;
|
|
|
|
|
2009-04-17 23:27:38 +08:00
|
|
|
/* Store in output, Q0 */
|
|
|
|
frame->pcm_sample[ch][blk * 8 + i] = sbc_clip16(SCALE8_STAGED1(
|
2008-03-06 22:04:43 +08:00
|
|
|
MULA(state->V[ch][offset[i] + 0], sbc_proto_8_80m0[idx + 0],
|
|
|
|
MULA(state->V[ch][offset[k] + 1], sbc_proto_8_80m1[idx + 0],
|
|
|
|
MULA(state->V[ch][offset[i] + 2], sbc_proto_8_80m0[idx + 1],
|
|
|
|
MULA(state->V[ch][offset[k] + 3], sbc_proto_8_80m1[idx + 1],
|
|
|
|
MULA(state->V[ch][offset[i] + 4], sbc_proto_8_80m0[idx + 2],
|
|
|
|
MULA(state->V[ch][offset[k] + 5], sbc_proto_8_80m1[idx + 2],
|
|
|
|
MULA(state->V[ch][offset[i] + 6], sbc_proto_8_80m0[idx + 3],
|
|
|
|
MULA(state->V[ch][offset[k] + 7], sbc_proto_8_80m1[idx + 3],
|
|
|
|
MULA(state->V[ch][offset[i] + 8], sbc_proto_8_80m0[idx + 4],
|
2009-04-17 23:27:38 +08:00
|
|
|
MUL( state->V[ch][offset[k] + 9], sbc_proto_8_80m1[idx + 4]))))))))))));
|
2007-03-17 21:37:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-01 23:27:38 +08:00
|
|
|
static int sbc_synthesize_audio(struct sbc_decoder_state *state,
|
2009-04-17 07:55:42 +08:00
|
|
|
struct sbc_frame *frame)
|
2007-03-17 21:37:31 +08:00
|
|
|
{
|
|
|
|
int ch, blk;
|
2007-11-01 23:27:38 +08:00
|
|
|
|
2007-03-17 21:37:31 +08:00
|
|
|
switch (frame->subbands) {
|
|
|
|
case 4:
|
|
|
|
for (ch = 0; ch < frame->channels; ch++) {
|
|
|
|
for (blk = 0; blk < frame->blocks; blk++)
|
|
|
|
sbc_synthesize_four(state, frame, ch, blk);
|
|
|
|
}
|
|
|
|
return frame->blocks * 4;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
for (ch = 0; ch < frame->channels; ch++) {
|
|
|
|
for (blk = 0; blk < frame->blocks; blk++)
|
|
|
|
sbc_synthesize_eight(state, frame, ch, blk);
|
|
|
|
}
|
|
|
|
return frame->blocks * 8;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-01 23:27:38 +08:00
|
|
|
static int sbc_analyze_audio(struct sbc_encoder_state *state,
|
2009-04-17 07:55:42 +08:00
|
|
|
struct sbc_frame *frame)
|
2007-03-17 21:37:31 +08:00
|
|
|
{
|
|
|
|
int ch, blk;
|
2009-01-30 00:15:31 +08:00
|
|
|
int16_t *x;
|
2007-03-17 21:37:31 +08:00
|
|
|
|
|
|
|
switch (frame->subbands) {
|
|
|
|
case 4:
|
2009-01-30 00:15:31 +08:00
|
|
|
for (ch = 0; ch < frame->channels; ch++) {
|
|
|
|
x = &state->X[ch][state->position - 16 +
|
|
|
|
frame->blocks * 4];
|
2008-12-31 15:14:25 +08:00
|
|
|
for (blk = 0; blk < frame->blocks; blk += 4) {
|
|
|
|
state->sbc_analyze_4b_4s(
|
2009-01-30 00:15:31 +08:00
|
|
|
x,
|
2008-12-31 15:14:25 +08:00
|
|
|
frame->sb_sample_f[blk][ch],
|
|
|
|
frame->sb_sample_f[blk + 1][ch] -
|
|
|
|
frame->sb_sample_f[blk][ch]);
|
2009-01-30 00:15:31 +08:00
|
|
|
x -= 16;
|
2008-12-31 15:14:25 +08:00
|
|
|
}
|
2009-01-30 00:15:31 +08:00
|
|
|
}
|
2007-03-17 21:37:31 +08:00
|
|
|
return frame->blocks * 4;
|
|
|
|
|
|
|
|
case 8:
|
2009-01-30 00:15:31 +08:00
|
|
|
for (ch = 0; ch < frame->channels; ch++) {
|
|
|
|
x = &state->X[ch][state->position - 32 +
|
|
|
|
frame->blocks * 8];
|
2008-12-31 15:14:25 +08:00
|
|
|
for (blk = 0; blk < frame->blocks; blk += 4) {
|
|
|
|
state->sbc_analyze_4b_8s(
|
2009-01-30 00:15:31 +08:00
|
|
|
x,
|
2008-12-31 15:14:25 +08:00
|
|
|
frame->sb_sample_f[blk][ch],
|
|
|
|
frame->sb_sample_f[blk + 1][ch] -
|
|
|
|
frame->sb_sample_f[blk][ch]);
|
2009-01-30 00:15:31 +08:00
|
|
|
x -= 32;
|
2008-12-31 15:14:25 +08:00
|
|
|
}
|
2009-01-30 00:15:31 +08:00
|
|
|
}
|
2007-03-17 21:37:31 +08:00
|
|
|
return frame->blocks * 8;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-12 03:21:28 +08:00
|
|
|
/* Supplementary bitstream writing macros for 'sbc_pack_frame' */
|
|
|
|
|
2009-01-18 02:30:40 +08:00
|
|
|
#define PUT_BITS(data_ptr, bits_cache, bits_count, v, n) \
|
|
|
|
do { \
|
|
|
|
bits_cache = (v) | (bits_cache << (n)); \
|
|
|
|
bits_count += (n); \
|
|
|
|
if (bits_count >= 16) { \
|
|
|
|
bits_count -= 8; \
|
|
|
|
*data_ptr++ = (uint8_t) \
|
|
|
|
(bits_cache >> bits_count); \
|
|
|
|
bits_count -= 8; \
|
|
|
|
*data_ptr++ = (uint8_t) \
|
|
|
|
(bits_cache >> bits_count); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define FLUSH_BITS(data_ptr, bits_cache, bits_count) \
|
|
|
|
do { \
|
|
|
|
while (bits_count >= 8) { \
|
|
|
|
bits_count -= 8; \
|
|
|
|
*data_ptr++ = (uint8_t) \
|
|
|
|
(bits_cache >> bits_count); \
|
|
|
|
} \
|
|
|
|
if (bits_count > 0) \
|
|
|
|
*data_ptr++ = (uint8_t) \
|
|
|
|
(bits_cache << (8 - bits_count)); \
|
|
|
|
} while (0)
|
2008-12-12 03:21:28 +08:00
|
|
|
|
2007-03-17 21:37:31 +08:00
|
|
|
/*
|
|
|
|
* Packs the SBC frame from frame into the memory at data. At most len
|
2007-11-01 23:27:38 +08:00
|
|
|
* bytes will be used, should more memory be needed an appropriate
|
2007-03-17 21:37:31 +08:00
|
|
|
* error code will be returned. Returns the length of the packed frame
|
2007-11-01 23:27:38 +08:00
|
|
|
* on success or a negative value on error.
|
2007-03-17 21:37:31 +08:00
|
|
|
*
|
|
|
|
* The error codes are:
|
|
|
|
* -1 Not enough memory reserved
|
|
|
|
* -2 Unsupported sampling rate
|
|
|
|
* -3 Unsupported number of blocks
|
|
|
|
* -4 Unsupported number of subbands
|
|
|
|
* -5 Bitpool value out of bounds
|
|
|
|
* -99 not implemented
|
|
|
|
*/
|
|
|
|
|
2009-04-17 07:55:42 +08:00
|
|
|
static SBC_ALWAYS_INLINE int sbc_pack_frame_internal(uint8_t *data,
|
|
|
|
struct sbc_frame *frame, size_t len,
|
|
|
|
int frame_subbands, int frame_channels)
|
2007-03-17 21:37:31 +08:00
|
|
|
{
|
2008-12-12 03:21:28 +08:00
|
|
|
/* Bitstream writer starts from the fourth byte */
|
|
|
|
uint8_t *data_ptr = data + 4;
|
|
|
|
uint32_t bits_cache = 0;
|
|
|
|
uint32_t bits_count = 0;
|
|
|
|
|
2007-03-17 21:37:31 +08:00
|
|
|
/* Will copy the header parts for CRC-8 calculation here */
|
|
|
|
uint8_t crc_header[11] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
|
|
int crc_pos = 0;
|
|
|
|
|
2009-01-04 09:11:12 +08:00
|
|
|
uint32_t audio_sample;
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2008-12-12 03:21:28 +08:00
|
|
|
int ch, sb, blk; /* channel, subband, block and bit counters */
|
2007-03-17 21:37:31 +08:00
|
|
|
int bits[2][8]; /* bits distribution */
|
2009-01-04 09:11:12 +08:00
|
|
|
uint32_t levels[2][8]; /* levels are derived from that */
|
|
|
|
uint32_t sb_sample_delta[2][8];
|
2007-03-17 21:37:31 +08:00
|
|
|
|
|
|
|
data[0] = SBC_SYNCWORD;
|
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
data[1] = (frame->frequency & 0x03) << 6;
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
data[1] |= (frame->block_mode & 0x03) << 4;
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
data[1] |= (frame->mode & 0x03) << 2;
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
data[1] |= (frame->allocation & 0x01) << 1;
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2009-01-22 03:08:34 +08:00
|
|
|
switch (frame_subbands) {
|
2007-03-17 21:37:31 +08:00
|
|
|
case 4:
|
|
|
|
/* Nothing to do */
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
data[1] |= 0x01;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
data[2] = frame->bitpool;
|
2007-11-02 17:05:57 +08:00
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
if ((frame->mode == MONO || frame->mode == DUAL_CHANNEL) &&
|
2009-01-22 03:08:34 +08:00
|
|
|
frame->bitpool > frame_subbands << 4)
|
2007-11-01 23:27:38 +08:00
|
|
|
return -5;
|
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
if ((frame->mode == STEREO || frame->mode == JOINT_STEREO) &&
|
2009-01-22 03:08:34 +08:00
|
|
|
frame->bitpool > frame_subbands << 5)
|
2007-03-17 21:37:31 +08:00
|
|
|
return -5;
|
|
|
|
|
|
|
|
/* Can't fill in crc yet */
|
|
|
|
|
|
|
|
crc_header[0] = data[1];
|
|
|
|
crc_header[1] = data[2];
|
|
|
|
crc_pos = 16;
|
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
if (frame->mode == JOINT_STEREO) {
|
2007-11-01 23:27:38 +08:00
|
|
|
/* like frame->sb_sample but joint stereo */
|
2007-11-27 10:42:29 +08:00
|
|
|
int32_t sb_sample_j[16][2];
|
2007-11-01 23:27:38 +08:00
|
|
|
/* scalefactor and scale_factor in joint case */
|
2009-01-30 07:02:58 +08:00
|
|
|
uint32_t scalefactor_j[2];
|
2007-11-27 10:42:29 +08:00
|
|
|
uint8_t scale_factor_j[2];
|
|
|
|
|
2008-12-12 03:21:28 +08:00
|
|
|
uint8_t joint = 0;
|
2008-02-20 03:47:25 +08:00
|
|
|
frame->joint = 0;
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2009-01-22 03:08:34 +08:00
|
|
|
for (sb = 0; sb < frame_subbands - 1; sb++) {
|
2007-11-27 11:15:36 +08:00
|
|
|
scale_factor_j[0] = 0;
|
2008-12-28 09:22:59 +08:00
|
|
|
scalefactor_j[0] = 2 << SCALE_OUT_BITS;
|
2007-11-27 11:15:36 +08:00
|
|
|
scale_factor_j[1] = 0;
|
2008-12-28 09:22:59 +08:00
|
|
|
scalefactor_j[1] = 2 << SCALE_OUT_BITS;
|
2007-11-27 11:15:36 +08:00
|
|
|
|
2007-03-17 21:37:31 +08:00
|
|
|
for (blk = 0; blk < frame->blocks; blk++) {
|
2009-01-30 07:02:58 +08:00
|
|
|
uint32_t tmp;
|
2007-11-27 11:15:36 +08:00
|
|
|
/* Calculate joint stereo signal */
|
2007-11-27 10:42:29 +08:00
|
|
|
sb_sample_j[blk][0] =
|
2008-12-28 09:22:59 +08:00
|
|
|
ASR(frame->sb_sample_f[blk][0][sb], 1) +
|
|
|
|
ASR(frame->sb_sample_f[blk][1][sb], 1);
|
2007-11-27 10:42:29 +08:00
|
|
|
sb_sample_j[blk][1] =
|
2008-12-28 09:22:59 +08:00
|
|
|
ASR(frame->sb_sample_f[blk][0][sb], 1) -
|
|
|
|
ASR(frame->sb_sample_f[blk][1][sb], 1);
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2007-11-27 11:15:36 +08:00
|
|
|
/* calculate scale_factor_j and scalefactor_j for joint case */
|
2009-01-30 07:02:58 +08:00
|
|
|
tmp = fabs(sb_sample_j[blk][0]);
|
|
|
|
while (scalefactor_j[0] < tmp) {
|
2007-11-27 10:42:29 +08:00
|
|
|
scale_factor_j[0]++;
|
|
|
|
scalefactor_j[0] *= 2;
|
|
|
|
}
|
2009-01-30 07:02:58 +08:00
|
|
|
tmp = fabs(sb_sample_j[blk][1]);
|
|
|
|
while (scalefactor_j[1] < tmp) {
|
2007-11-27 10:42:29 +08:00
|
|
|
scale_factor_j[1]++;
|
|
|
|
scalefactor_j[1] *= 2;
|
2007-03-17 21:37:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-27 10:42:29 +08:00
|
|
|
/* decide whether to join this subband */
|
2008-12-28 01:36:14 +08:00
|
|
|
if ((frame->scale_factor[0][sb] +
|
|
|
|
frame->scale_factor[1][sb]) >
|
|
|
|
(scale_factor_j[0] +
|
|
|
|
scale_factor_j[1])) {
|
2007-03-17 21:37:31 +08:00
|
|
|
/* use joint stereo for this subband */
|
2009-01-22 03:08:34 +08:00
|
|
|
joint |= 1 << (frame_subbands - 1 - sb);
|
2008-02-20 03:47:25 +08:00
|
|
|
frame->joint |= 1 << sb;
|
2007-11-27 10:42:29 +08:00
|
|
|
frame->scale_factor[0][sb] = scale_factor_j[0];
|
|
|
|
frame->scale_factor[1][sb] = scale_factor_j[1];
|
2007-03-17 21:37:31 +08:00
|
|
|
for (blk = 0; blk < frame->blocks; blk++) {
|
2007-11-01 23:27:38 +08:00
|
|
|
frame->sb_sample_f[blk][0][sb] =
|
2007-11-27 10:42:29 +08:00
|
|
|
sb_sample_j[blk][0];
|
2007-11-01 23:27:38 +08:00
|
|
|
frame->sb_sample_f[blk][1][sb] =
|
2007-11-27 10:42:29 +08:00
|
|
|
sb_sample_j[blk][1];
|
2007-03-17 21:37:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-18 02:30:40 +08:00
|
|
|
PUT_BITS(data_ptr, bits_cache, bits_count,
|
2009-01-22 03:08:34 +08:00
|
|
|
joint, frame_subbands);
|
2008-12-12 03:21:28 +08:00
|
|
|
crc_header[crc_pos >> 3] = joint;
|
2009-01-22 03:08:34 +08:00
|
|
|
crc_pos += frame_subbands;
|
2007-03-17 21:37:31 +08:00
|
|
|
}
|
|
|
|
|
2009-01-22 03:08:34 +08:00
|
|
|
for (ch = 0; ch < frame_channels; ch++) {
|
|
|
|
for (sb = 0; sb < frame_subbands; sb++) {
|
2009-01-18 02:30:40 +08:00
|
|
|
PUT_BITS(data_ptr, bits_cache, bits_count,
|
|
|
|
frame->scale_factor[ch][sb] & 0x0F, 4);
|
2007-12-14 14:43:06 +08:00
|
|
|
crc_header[crc_pos >> 3] <<= 4;
|
|
|
|
crc_header[crc_pos >> 3] |= frame->scale_factor[ch][sb] & 0x0F;
|
2007-03-17 21:37:31 +08:00
|
|
|
crc_pos += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-14 14:43:06 +08:00
|
|
|
/* align the last crc byte */
|
2008-01-28 18:38:40 +08:00
|
|
|
if (crc_pos % 8)
|
2007-12-14 14:43:06 +08:00
|
|
|
crc_header[crc_pos >> 3] <<= 8 - (crc_pos % 8);
|
2007-12-14 14:07:52 +08:00
|
|
|
|
2007-03-17 21:37:31 +08:00
|
|
|
data[3] = sbc_crc8(crc_header, crc_pos);
|
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
sbc_calculate_bits(frame, bits);
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2009-01-22 03:08:34 +08:00
|
|
|
for (ch = 0; ch < frame_channels; ch++) {
|
|
|
|
for (sb = 0; sb < frame_subbands; sb++) {
|
2009-01-04 09:11:12 +08:00
|
|
|
levels[ch][sb] = ((1 << bits[ch][sb]) - 1) <<
|
|
|
|
(32 - (frame->scale_factor[ch][sb] +
|
|
|
|
SCALE_OUT_BITS + 2));
|
|
|
|
sb_sample_delta[ch][sb] = (uint32_t) 1 <<
|
|
|
|
(frame->scale_factor[ch][sb] +
|
|
|
|
SCALE_OUT_BITS + 1);
|
|
|
|
}
|
2007-03-17 21:37:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (blk = 0; blk < frame->blocks; blk++) {
|
2009-01-22 03:08:34 +08:00
|
|
|
for (ch = 0; ch < frame_channels; ch++) {
|
|
|
|
for (sb = 0; sb < frame_subbands; sb++) {
|
2008-12-28 09:22:59 +08:00
|
|
|
|
|
|
|
if (bits[ch][sb] == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
audio_sample = ((uint64_t) levels[ch][sb] *
|
2009-01-04 09:11:12 +08:00
|
|
|
(sb_sample_delta[ch][sb] +
|
|
|
|
frame->sb_sample_f[blk][ch][sb])) >> 32;
|
2008-12-28 09:22:59 +08:00
|
|
|
|
2009-01-18 02:30:40 +08:00
|
|
|
PUT_BITS(data_ptr, bits_cache, bits_count,
|
|
|
|
audio_sample, bits[ch][sb]);
|
2007-03-17 21:37:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-14 14:07:52 +08:00
|
|
|
|
2009-01-18 02:30:40 +08:00
|
|
|
FLUSH_BITS(data_ptr, bits_cache, bits_count);
|
2007-12-14 14:07:52 +08:00
|
|
|
|
2008-12-12 03:21:28 +08:00
|
|
|
return data_ptr - data;
|
2007-03-17 21:37:31 +08:00
|
|
|
}
|
|
|
|
|
2009-01-22 03:08:34 +08:00
|
|
|
static int sbc_pack_frame(uint8_t *data, struct sbc_frame *frame, size_t len)
|
|
|
|
{
|
|
|
|
if (frame->subbands == 4) {
|
|
|
|
if (frame->channels == 1)
|
|
|
|
return sbc_pack_frame_internal(data, frame, len, 4, 1);
|
|
|
|
else
|
|
|
|
return sbc_pack_frame_internal(data, frame, len, 4, 2);
|
|
|
|
} else {
|
|
|
|
if (frame->channels == 1)
|
|
|
|
return sbc_pack_frame_internal(data, frame, len, 8, 1);
|
|
|
|
else
|
|
|
|
return sbc_pack_frame_internal(data, frame, len, 8, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-31 15:14:25 +08:00
|
|
|
static void sbc_encoder_init(struct sbc_encoder_state *state,
|
2009-04-17 07:55:42 +08:00
|
|
|
const struct sbc_frame *frame)
|
2008-12-31 15:14:25 +08:00
|
|
|
{
|
|
|
|
memset(&state->X, 0, sizeof(state->X));
|
2009-03-16 08:27:26 +08:00
|
|
|
state->position = (SBC_X_BUFFER_SIZE - frame->subbands * 9) & ~7;
|
2008-12-31 15:14:25 +08:00
|
|
|
|
2009-01-16 01:11:23 +08:00
|
|
|
sbc_init_primitives(state);
|
2008-12-31 15:14:25 +08:00
|
|
|
}
|
|
|
|
|
2007-03-17 21:37:31 +08:00
|
|
|
struct sbc_priv {
|
|
|
|
int init;
|
2009-01-16 01:45:36 +08:00
|
|
|
struct SBC_ALIGNED sbc_frame frame;
|
|
|
|
struct SBC_ALIGNED sbc_decoder_state dec_state;
|
|
|
|
struct SBC_ALIGNED sbc_encoder_state enc_state;
|
2007-03-17 21:37:31 +08:00
|
|
|
};
|
|
|
|
|
2007-11-14 04:59:17 +08:00
|
|
|
static void sbc_set_defaults(sbc_t *sbc, unsigned long flags)
|
2007-11-14 04:04:12 +08:00
|
|
|
{
|
2008-02-20 03:47:25 +08:00
|
|
|
sbc->frequency = SBC_FREQ_44100;
|
|
|
|
sbc->mode = SBC_MODE_STEREO;
|
|
|
|
sbc->subbands = SBC_SB_8;
|
|
|
|
sbc->blocks = SBC_BLK_16;
|
2007-11-14 04:04:12 +08:00
|
|
|
sbc->bitpool = 32;
|
2008-02-20 03:47:25 +08:00
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
sbc->endian = SBC_LE;
|
|
|
|
#elif __BYTE_ORDER == __BIG_ENDIAN
|
|
|
|
sbc->endian = SBC_BE;
|
|
|
|
#else
|
|
|
|
#error "Unknown byte order"
|
|
|
|
#endif
|
2007-11-14 04:04:12 +08:00
|
|
|
}
|
|
|
|
|
2007-03-17 21:37:31 +08:00
|
|
|
int sbc_init(sbc_t *sbc, unsigned long flags)
|
|
|
|
{
|
|
|
|
if (!sbc)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
memset(sbc, 0, sizeof(sbc_t));
|
|
|
|
|
2009-01-16 01:45:36 +08:00
|
|
|
sbc->priv_alloc_base = malloc(sizeof(struct sbc_priv) + SBC_ALIGN_MASK);
|
|
|
|
if (!sbc->priv_alloc_base)
|
2007-03-17 21:37:31 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2009-01-16 01:45:36 +08:00
|
|
|
sbc->priv = (void *) (((uintptr_t) sbc->priv_alloc_base +
|
|
|
|
SBC_ALIGN_MASK) & ~((uintptr_t) SBC_ALIGN_MASK));
|
|
|
|
|
2007-03-17 21:37:31 +08:00
|
|
|
memset(sbc->priv, 0, sizeof(struct sbc_priv));
|
|
|
|
|
2007-11-14 04:59:17 +08:00
|
|
|
sbc_set_defaults(sbc, flags);
|
2007-03-17 21:37:31 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-23 23:44:11 +08:00
|
|
|
ssize_t sbc_parse(sbc_t *sbc, const void *input, size_t input_len)
|
2007-08-26 21:54:24 +08:00
|
|
|
{
|
2007-11-13 02:15:59 +08:00
|
|
|
return sbc_decode(sbc, input, input_len, NULL, 0, NULL);
|
2007-08-26 21:54:24 +08:00
|
|
|
}
|
|
|
|
|
2009-04-17 07:55:42 +08:00
|
|
|
ssize_t sbc_decode(sbc_t *sbc, const void *input, size_t input_len,
|
|
|
|
void *output, size_t output_len, size_t *written)
|
2007-03-17 21:37:31 +08:00
|
|
|
{
|
|
|
|
struct sbc_priv *priv;
|
|
|
|
char *ptr;
|
|
|
|
int i, ch, framelen, samples;
|
|
|
|
|
2009-02-18 04:46:41 +08:00
|
|
|
if (!sbc || !input)
|
2007-03-17 21:37:31 +08:00
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
priv = sbc->priv;
|
|
|
|
|
2007-11-13 02:15:59 +08:00
|
|
|
framelen = sbc_unpack_frame(input, &priv->frame, input_len);
|
2007-03-17 21:37:31 +08:00
|
|
|
|
|
|
|
if (!priv->init) {
|
|
|
|
sbc_decoder_init(&priv->dec_state, &priv->frame);
|
|
|
|
priv->init = 1;
|
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
sbc->frequency = priv->frame.frequency;
|
|
|
|
sbc->mode = priv->frame.mode;
|
|
|
|
sbc->subbands = priv->frame.subband_mode;
|
|
|
|
sbc->blocks = priv->frame.block_mode;
|
|
|
|
sbc->allocation = priv->frame.allocation;
|
2007-03-17 21:37:31 +08:00
|
|
|
sbc->bitpool = priv->frame.bitpool;
|
2007-11-13 02:15:59 +08:00
|
|
|
|
|
|
|
priv->frame.codesize = sbc_get_codesize(sbc);
|
2009-03-21 05:40:43 +08:00
|
|
|
priv->frame.length = framelen;
|
2007-03-17 21:37:31 +08:00
|
|
|
}
|
|
|
|
|
2007-11-13 02:15:59 +08:00
|
|
|
if (!output)
|
|
|
|
return framelen;
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2007-11-13 02:15:59 +08:00
|
|
|
if (written)
|
|
|
|
*written = 0;
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2008-12-24 06:41:38 +08:00
|
|
|
if (framelen <= 0)
|
|
|
|
return framelen;
|
|
|
|
|
2007-11-13 02:15:59 +08:00
|
|
|
samples = sbc_synthesize_audio(&priv->dec_state, &priv->frame);
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2007-11-13 02:15:59 +08:00
|
|
|
ptr = output;
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2009-03-23 23:44:11 +08:00
|
|
|
if (output_len < (size_t) (samples * priv->frame.channels * 2))
|
2007-11-13 02:15:59 +08:00
|
|
|
samples = output_len / (priv->frame.channels * 2);
|
2007-03-17 21:37:31 +08:00
|
|
|
|
|
|
|
for (i = 0; i < samples; i++) {
|
|
|
|
for (ch = 0; ch < priv->frame.channels; ch++) {
|
|
|
|
int16_t s;
|
|
|
|
s = priv->frame.pcm_sample[ch][i];
|
2007-08-22 05:32:09 +08:00
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
if (sbc->endian == SBC_BE) {
|
2007-08-22 05:32:09 +08:00
|
|
|
*ptr++ = (s & 0xff00) >> 8;
|
|
|
|
*ptr++ = (s & 0x00ff);
|
|
|
|
} else {
|
|
|
|
*ptr++ = (s & 0x00ff);
|
|
|
|
*ptr++ = (s & 0xff00) >> 8;
|
|
|
|
}
|
2007-03-17 21:37:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-13 02:15:59 +08:00
|
|
|
if (written)
|
|
|
|
*written = samples * priv->frame.channels * 2;
|
2007-03-17 21:37:31 +08:00
|
|
|
|
|
|
|
return framelen;
|
|
|
|
}
|
|
|
|
|
2009-04-17 07:55:42 +08:00
|
|
|
ssize_t sbc_encode(sbc_t *sbc, const void *input, size_t input_len,
|
|
|
|
void *output, size_t output_len, size_t *written)
|
2007-03-17 21:37:31 +08:00
|
|
|
{
|
|
|
|
struct sbc_priv *priv;
|
2009-01-28 00:57:35 +08:00
|
|
|
int framelen, samples;
|
|
|
|
int (*sbc_enc_process_input)(int position,
|
|
|
|
const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
|
|
|
|
int nsamples, int nchannels);
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2009-02-18 04:46:41 +08:00
|
|
|
if (!sbc || !input)
|
2007-03-17 21:37:31 +08:00
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
priv = sbc->priv;
|
|
|
|
|
2007-11-13 02:15:59 +08:00
|
|
|
if (written)
|
|
|
|
*written = 0;
|
|
|
|
|
2007-03-17 21:37:31 +08:00
|
|
|
if (!priv->init) {
|
2008-02-20 03:47:25 +08:00
|
|
|
priv->frame.frequency = sbc->frequency;
|
|
|
|
priv->frame.mode = sbc->mode;
|
|
|
|
priv->frame.channels = sbc->mode == SBC_MODE_MONO ? 1 : 2;
|
|
|
|
priv->frame.allocation = sbc->allocation;
|
|
|
|
priv->frame.subband_mode = sbc->subbands;
|
|
|
|
priv->frame.subbands = sbc->subbands ? 8 : 4;
|
|
|
|
priv->frame.block_mode = sbc->blocks;
|
|
|
|
priv->frame.blocks = 4 + (sbc->blocks * 4);
|
2007-03-17 21:37:31 +08:00
|
|
|
priv->frame.bitpool = sbc->bitpool;
|
2007-11-13 02:15:59 +08:00
|
|
|
priv->frame.codesize = sbc_get_codesize(sbc);
|
|
|
|
priv->frame.length = sbc_get_frame_length(sbc);
|
2007-03-17 21:37:31 +08:00
|
|
|
|
|
|
|
sbc_encoder_init(&priv->enc_state, &priv->frame);
|
|
|
|
priv->init = 1;
|
|
|
|
}
|
|
|
|
|
2007-08-26 09:05:25 +08:00
|
|
|
/* input must be large enough to encode a complete frame */
|
2007-11-13 02:15:59 +08:00
|
|
|
if (input_len < priv->frame.codesize)
|
2007-08-26 09:05:25 +08:00
|
|
|
return 0;
|
|
|
|
|
2007-11-13 02:15:59 +08:00
|
|
|
/* output must be large enough to receive the encoded frame */
|
|
|
|
if (!output || output_len < priv->frame.length)
|
|
|
|
return -ENOSPC;
|
|
|
|
|
2009-01-28 00:57:35 +08:00
|
|
|
/* Select the needed input data processing function and call it */
|
|
|
|
if (priv->frame.subbands == 8) {
|
|
|
|
if (sbc->endian == SBC_BE)
|
|
|
|
sbc_enc_process_input =
|
|
|
|
priv->enc_state.sbc_enc_process_input_8s_be;
|
|
|
|
else
|
|
|
|
sbc_enc_process_input =
|
|
|
|
priv->enc_state.sbc_enc_process_input_8s_le;
|
|
|
|
} else {
|
|
|
|
if (sbc->endian == SBC_BE)
|
|
|
|
sbc_enc_process_input =
|
|
|
|
priv->enc_state.sbc_enc_process_input_4s_be;
|
|
|
|
else
|
|
|
|
sbc_enc_process_input =
|
|
|
|
priv->enc_state.sbc_enc_process_input_4s_le;
|
2007-03-17 21:37:31 +08:00
|
|
|
}
|
|
|
|
|
2009-01-28 00:57:35 +08:00
|
|
|
priv->enc_state.position = sbc_enc_process_input(
|
|
|
|
priv->enc_state.position, (const uint8_t *) input,
|
|
|
|
priv->enc_state.X, priv->frame.subbands * priv->frame.blocks,
|
|
|
|
priv->frame.channels);
|
|
|
|
|
2007-03-17 21:37:31 +08:00
|
|
|
samples = sbc_analyze_audio(&priv->enc_state, &priv->frame);
|
|
|
|
|
2009-01-29 08:17:36 +08:00
|
|
|
priv->enc_state.sbc_calc_scalefactors(
|
|
|
|
priv->frame.sb_sample_f, priv->frame.scale_factor,
|
|
|
|
priv->frame.blocks, priv->frame.channels, priv->frame.subbands);
|
|
|
|
|
2007-11-13 02:15:59 +08:00
|
|
|
framelen = sbc_pack_frame(output, &priv->frame, output_len);
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2007-11-13 02:15:59 +08:00
|
|
|
if (written)
|
|
|
|
*written = framelen;
|
2007-03-17 21:37:31 +08:00
|
|
|
|
2008-02-20 03:47:25 +08:00
|
|
|
return samples * priv->frame.channels * 2;
|
2007-03-17 21:37:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void sbc_finish(sbc_t *sbc)
|
|
|
|
{
|
|
|
|
if (!sbc)
|
|
|
|
return;
|
|
|
|
|
2009-01-16 01:45:36 +08:00
|
|
|
if (sbc->priv_alloc_base)
|
|
|
|
free(sbc->priv_alloc_base);
|
2007-03-17 21:37:31 +08:00
|
|
|
|
|
|
|
memset(sbc, 0, sizeof(sbc_t));
|
|
|
|
}
|
2007-11-13 02:15:59 +08:00
|
|
|
|
2009-03-23 23:44:11 +08:00
|
|
|
size_t sbc_get_frame_length(sbc_t *sbc)
|
2007-11-13 02:15:59 +08:00
|
|
|
{
|
|
|
|
int ret;
|
2009-03-21 05:40:43 +08:00
|
|
|
uint8_t subbands, channels, blocks, joint, bitpool;
|
2008-02-20 03:47:25 +08:00
|
|
|
struct sbc_priv *priv;
|
|
|
|
|
|
|
|
priv = sbc->priv;
|
2009-03-21 05:40:43 +08:00
|
|
|
if (priv->init)
|
|
|
|
return priv->frame.length;
|
2007-11-13 02:15:59 +08:00
|
|
|
|
2009-03-21 05:40:43 +08:00
|
|
|
subbands = sbc->subbands ? 8 : 4;
|
|
|
|
blocks = 4 + (sbc->blocks * 4);
|
|
|
|
channels = sbc->mode == SBC_MODE_MONO ? 1 : 2;
|
|
|
|
joint = sbc->mode == SBC_MODE_JOINT_STEREO ? 1 : 0;
|
|
|
|
bitpool = sbc->bitpool;
|
2007-11-13 02:15:59 +08:00
|
|
|
|
2009-03-21 05:40:43 +08:00
|
|
|
ret = 4 + (4 * subbands * channels) / 8;
|
2007-11-14 04:04:12 +08:00
|
|
|
/* This term is not always evenly divide so we round it up */
|
2008-02-20 03:47:25 +08:00
|
|
|
if (channels == 1)
|
2009-03-21 05:40:43 +08:00
|
|
|
ret += ((blocks * channels * bitpool) + 7) / 8;
|
2007-11-13 02:15:59 +08:00
|
|
|
else
|
2009-03-21 05:40:43 +08:00
|
|
|
ret += (((joint ? subbands : 0) + blocks * bitpool) + 7) / 8;
|
2007-11-13 02:15:59 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-03-23 23:44:11 +08:00
|
|
|
unsigned sbc_get_frame_duration(sbc_t *sbc)
|
2007-11-23 22:24:23 +08:00
|
|
|
{
|
2008-02-20 03:47:25 +08:00
|
|
|
uint8_t subbands, blocks;
|
|
|
|
uint16_t frequency;
|
|
|
|
struct sbc_priv *priv;
|
|
|
|
|
|
|
|
priv = sbc->priv;
|
|
|
|
if (!priv->init) {
|
|
|
|
subbands = sbc->subbands ? 8 : 4;
|
|
|
|
blocks = 4 + (sbc->blocks * 4);
|
|
|
|
} else {
|
|
|
|
subbands = priv->frame.subbands;
|
|
|
|
blocks = priv->frame.blocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (sbc->frequency) {
|
|
|
|
case SBC_FREQ_16000:
|
|
|
|
frequency = 16000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SBC_FREQ_32000:
|
|
|
|
frequency = 32000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SBC_FREQ_44100:
|
|
|
|
frequency = 44100;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SBC_FREQ_48000:
|
|
|
|
frequency = 48000;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (1000000 * blocks * subbands) / frequency;
|
2007-11-23 22:24:23 +08:00
|
|
|
}
|
|
|
|
|
2009-03-23 23:44:11 +08:00
|
|
|
size_t sbc_get_codesize(sbc_t *sbc)
|
2007-11-13 02:15:59 +08:00
|
|
|
{
|
2009-01-05 20:26:08 +08:00
|
|
|
uint16_t subbands, channels, blocks;
|
2008-02-20 03:47:25 +08:00
|
|
|
struct sbc_priv *priv;
|
|
|
|
|
|
|
|
priv = sbc->priv;
|
|
|
|
if (!priv->init) {
|
|
|
|
subbands = sbc->subbands ? 8 : 4;
|
|
|
|
blocks = 4 + (sbc->blocks * 4);
|
|
|
|
channels = sbc->mode == SBC_MODE_MONO ? 1 : 2;
|
|
|
|
} else {
|
|
|
|
subbands = priv->frame.subbands;
|
|
|
|
blocks = priv->frame.blocks;
|
|
|
|
channels = priv->frame.channels;
|
|
|
|
}
|
|
|
|
|
|
|
|
return subbands * blocks * channels * 2;
|
2007-11-13 02:15:59 +08:00
|
|
|
}
|
2007-11-14 04:04:12 +08:00
|
|
|
|
2009-02-16 21:59:51 +08:00
|
|
|
const char *sbc_get_implementation_info(sbc_t *sbc)
|
|
|
|
{
|
|
|
|
struct sbc_priv *priv;
|
|
|
|
|
|
|
|
if (!sbc)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
priv = sbc->priv;
|
|
|
|
if (!priv)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return priv->enc_state.implementation_info;
|
|
|
|
}
|
|
|
|
|
2007-11-14 04:04:12 +08:00
|
|
|
int sbc_reinit(sbc_t *sbc, unsigned long flags)
|
|
|
|
{
|
|
|
|
struct sbc_priv *priv;
|
|
|
|
|
|
|
|
if (!sbc || !sbc->priv)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
priv = sbc->priv;
|
|
|
|
|
|
|
|
if (priv->init == 1)
|
|
|
|
memset(sbc->priv, 0, sizeof(struct sbc_priv));
|
|
|
|
|
2007-11-14 04:59:17 +08:00
|
|
|
sbc_set_defaults(sbc, flags);
|
2007-11-14 04:04:12 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|