├── README.md ├── README.txt ├── test ├── Android.mk ├── frame2.txt ├── sbc_debug.c └── sbc_debug.h └── decoder ├── include └── sbc_decoder.h └── srce └── sbc_decoder.c /README.md: -------------------------------------------------------------------------------- 1 | sbc-decoder 2 | =========== 3 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | Introduction 2 | ============ 3 | 4 | this SBC decoder derived from Bluez, you can also use it in Bluedroid. 5 | 6 | Usage 7 | ======== 8 | 9 | 1.if you want to SBC decode, please call the intereface function: 10 | SBC_API extern void SBC_Decoder(SBC_DEC_PARAMS *strDecParams); 11 | strDecParams will contain the SBC data stream and it's length, then the decoded PCM will be put in s16Pcmsample. 12 | 2.sbc_debug.c in TEST can help you to verify your decoder, please run it just like usual c programe. 13 | ./main.o -f a ../frame2.txt -------------------------------------------------------------------------------- /test/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH:= $(call my-dir) 2 | 3 | include $(CLEAR_VARS) 4 | LOCAL_MODULE := test 5 | 6 | LOCAL_CFLAGS += -DBUILDCFG -DHAS_BDROID_BUILDCFG 7 | 8 | LOCAL_SRC_FILES := sbc_debug.c \ 9 | ../decoder/srce/sbc_decoder.c 10 | 11 | LOCAL_C_INCLUDES += \ 12 | $(LOCAL_PATH)/../decoder/include \ 13 | $(LOCAL_PATH)/../encoder/include \ 14 | external/bluetooth/bluedroid/include \ 15 | external/bluetooth/bluedroid/stack/include \ 16 | device/sony/tsubasa/bluetooth \ 17 | external/bluetooth/bluedroid/gki/ulinux \ 18 | external/bluedroid/include 19 | 20 | LOCAL_LDLIBS += -lpthread -ldl -llog -lreadline 21 | LIBS_c += -lreadline 22 | 23 | LOCAL_SHARED_LIBRARIES += libcutils \ 24 | libutils \ 25 | libhardware \ 26 | libhardware_legacy 27 | 28 | include $(BUILD_EXECUTABLE) 29 | 30 | #bt_target.h external/bluetooth/bluedroid/include \ 31 | #bt_types.h external/bluetooth/bluedroid/stack/include 32 | #bdroid_buildcfg.h device/sony/tsubasa/bluetooth 33 | #data_type.h external/bluetooth/bluedroid/stack/include \ -------------------------------------------------------------------------------- /decoder/include/sbc_decoder.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * Copyright (C) 2014 Tieto Corporation 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at: 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | ******************************************************************************/ 18 | 19 | /****************************************************************************** 20 | * 21 | * This file contains constants and structures used by Decoder. 22 | * 23 | ******************************************************************************/ 24 | 25 | #ifndef SBC_DECODER_H 26 | #define SBC_DECODER_H 27 | 28 | #define DECODER_VERSION "0025" 29 | 30 | #ifdef BUILDCFG 31 | #include "bt_target.h" 32 | #endif 33 | 34 | /*DEFINES*/ 35 | #define MINIMUM_DEC_VX_BUFFER_SIZE (8*10*2) 36 | #ifndef DEC_VX_BUFFER_SIZE 37 | #define DEC_VX_BUFFER_SIZE (MINIMUM_DEC_VX_BUFFER_SIZE + 64) 38 | /*#define DEC_VX_BUFFER_SIZE MINIMUM_DEC_VX_BUFFER_SIZE + 1024*/ 39 | #endif 40 | 41 | #include "sbc_types.h" 42 | #include "sbc_encoder.h" 43 | 44 | typedef struct SBC_DEC_PARAMS_TAG 45 | { 46 | /*-----------output parameter---------------*/ 47 | // the final decoded PCM for one frame 48 | SINT16 as16SbBuffer[SBC_MAX_NUM_OF_CHANNELS * SBC_MAX_NUM_OF_SUBBANDS * SBC_MAX_NUM_OF_BLOCKS]; 49 | // the length of PCM for one frame so that the caller can copy 50 | SINT16 s16PcmLength; 51 | // the lenght of a decoded SBC frame 52 | SINT16 s16SbcFrameLength; 53 | 54 | /*-----------input parameter---------------*/ 55 | // the data need to be decoded 56 | UINT8 *pu8Packet; 57 | UINT16 u16PacketLength; 58 | 59 | }SBC_DEC_PARAMS; 60 | 61 | #ifdef __cplusplus 62 | extern "C" 63 | { 64 | #endif 65 | SBC_API extern void SBC_Decoder(SBC_DEC_PARAMS *strDecParams); 66 | 67 | SBC_API extern void SBC_Decoder_Init(SBC_DEC_PARAMS *strDecParams); 68 | 69 | #ifdef __cplusplus 70 | } 71 | #endif 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /test/frame2.txt: -------------------------------------------------------------------------------- 1 | 10011100 10111101 00110101 01111101 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 01101000 01110110 11011011 01101110 11101101 10110110 11011011 10110011 00110110 11011011 01110111 01101101 10110110 11011010 00011010 00110110 11011011 10111011 01101101 10110110 11101110 11101101 10110110 11011001 11011011 01101101 10110111 01110111 01101101 10110110 11001110 11011011 01101101 10110100 00111011 01101101 10110110 01110110 11100011 01101101 10100001 11011011 01101101 10111011 10110110 11011011 01101110 11101100 11011011 01101101 10011001 10110110 11011011 01100111 01110110 11011011 01101110 11101101 10110110 11011011 10111011 10110110 11011011 01110111 01101101 10110110 11011101 11011101 10110110 11011011 00111011 01101101 10110110 11001111 00001101 10110110 11011010 00011011 01101101 10110111 01110111 01101101 10110110 11001100 11010011 01101101 10111011 10110011 01101101 10110110 01110110 11100011 01101101 11011101 11011011 01101101 10111011 10110110 11011011 01101100 11101110 11011011 01101101 11011101 10110110 11011011 10011100 10111101 00110101 01111101 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 01100111 01110110 11011011 01101100 11101101 10110110 11011011 11000011 00110110 11011011 01101000 01101101 10110110 11011110 00011101 10110110 11011011 10111011 01101101 10110110 11101110 11101101 10110110 11011101 11011011 01101101 10110111 01110111 01101101 10110110 11101110 11011011 01101101 10111011 10111011 01101101 10110111 01110110 11011011 01101101 11011101 11011011 01101101 10111011 10110110 11011011 01101110 11101110 11011011 01101101 10011101 10110110 11011011 01110111 01110110 11011011 01101100 11101101 10100110 11011011 10111011 10110110 11011011 01110111 01101101 10110110 11011101 11011101 10110110 11011011 00111011 01101101 10110110 11001110 11101101 10110110 11011101 11011011 01101101 10110111 10000110 01101101 10110110 11001110 11011011 01101101 10111011 10111011 01101101 10110111 01110110 11011011 01101101 11011101 11011011 01101101 10110011 00110110 11011011 01101110 11101110 11011011 01101101 11011001 10110110 11011011 10011100 10111101 00110101 01111101 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 01110111 01110110 11011011 01101110 11101101 10110110 11011011 10111011 10110110 11011011 01110111 01101101 10110110 11011101 11011101 10110110 11011011 00111011 01101101 10110110 11101110 11101101 10110110 11011101 11011011 01101101 10110111 01110111 01101101 10110110 11101110 11011011 01101101 10110011 10111011 01101101 10110111 01110110 11011011 01101101 10011110 00011011 01101101 10110011 10110110 11011011 01101111 00001100 11011011 01101101 11011101 10110110 11011011 01110111 01110110 11011011 01101010 11101101 10110110 11011011 01000011 10110110 10011011 01110111 01101101 10110110 11011101 11011101 10110110 11011011 10111011 01101101 10110110 11101110 11101101 10110110 11011001 11011011 01101101 10110111 01110111 01101101 10110110 11101110 11011011 01101101 10111011 10111011 01101101 10110111 01110110 11011011 01101101 10011001 11011011 01101101 10111011 10110110 11011011 01101110 11110000 11011011 01101101 11011101 10110110 11011011 10011100 10111101 00110101 01111101 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 01110111 01110110 11011011 01101110 11101101 10110110 11011011 10111011 10110110 11011011 01110111 01101101 10110110 11011101 11011101 10110110 11011011 10111011 01101101 10110110 11001110 11101101 10110110 11010101 11011011 01101101 10110110 10000111 01101101 10110110 11001110 11011010 01101101 10111011 10111011 01101101 10110111 01110110 11011011 01101101 10100010 00011011 01101101 10111011 10110110 11011011 01101100 11010001 00011011 01101101 10100001 10110110 11011011 01100110 10000110 11011011 01101110 11101101 10110110 11011011 00111011 10100110 11011011 01010111 01101101 10110110 11011001 10011001 10110110 11011011 10110011 01101101 10110110 11001111 00001101 10110110 11010101 11011011 01101101 10110110 01111000 01101101 10110110 11110000 11011100 01101101 10111011 10110011 01101101 10110111 01110110 11011011 01101101 11011101 11011011 01101101 10110100 00110110 11011011 01101100 11101110 11011011 01101101 11011101 10110110 11011011 10011100 10111101 00110101 01111101 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 01101000 10000110 10011011 01101111 00001101 10110110 11011011 00111011 10111000 11011011 01110111 01101101 10110110 11011101 11011101 10110110 11011011 10111011 01101101 10110110 11101110 11101101 10110110 11011101 11011011 01101101 10110111 01110111 01101101 10110110 11101110 11011011 01101101 10111011 10111011 01101101 10110110 10000110 11011011 01101101 11011101 11011011 01101101 10110011 10110110 11011011 01101110 11101110 11011011 01101101 10011110 00110110 11011011 01110111 10000110 11011011 01101100 11001110 00110110 11011011 10111011 10110110 11011011 01110111 01101101 10110110 11011101 11011101 10110110 11011011 00111011 01101001 10110110 11001100 11101101 10110110 11011101 11011011 01101101 10110111 01101000 01101101 10110110 11001110 11011011 01101101 10111011 10111011 01101101 10110111 01110110 11011011 01101101 11011101 11011011 01101101 10111011 10110110 11011011 01101111 00001110 11011011 01101101 11011101 10110110 11011011 10011100 10111101 00110101 01111101 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 01110111 01110110 11011011 01101100 11101101 10110110 11011011 10111011 10110110 11011011 01101000 01101101 10110110 11011101 11011101 10110110 11011011 10111011 01101101 10110110 11101110 11101101 10110110 11011001 10011011 01101101 10110111 01110111 01101101 10110110 11101110 11011011 01101101 10111011 10111011 01101101 10110111 01110110 11011011 01101101 11011101 11011011 01101101 10111011 10110110 11011011 01101110 11101110 11011011 01101101 11011101 10110110 11011011 01110111 01110110 11011011 01101110 11101101 10110110 11011011 00111100 00110110 11011011 01110111 01101101 10110110 11011101 10100001 10110110 11011011 10111011 01101101 10110110 11010000 11101110 00110110 11011101 11011011 01101101 10110111 01110111 01101101 10110110 11001110 11011011 01101101 10111011 10111011 01101101 10110110 01110110 11011010 01101101 10011110 00011011 01101101 10110011 10110110 11011011 01101110 11101110 11011011 01101101 11011001 10110110 11011011 -------------------------------------------------------------------------------- /test/sbc_debug.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Bluetooth low-complexity, subband codec (SBC) decoder 4 | * 5 | * Copyright (C) 2014 Tieto Corporation 6 | * Copyright (C) 2008-2010 Nokia Corporation 7 | * Copyright (C) 2004-2010 Marcel Holtmann 8 | * 9 | * 10 | * This program is free software; you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation; either version 2 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program; if not, write to the Free Software 22 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23 | * 24 | */ 25 | 26 | #ifdef HAVE_CONFIG_H 27 | #include 28 | #endif 29 | 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | #include "../encoder/include/sbc_types.h" 39 | #include "../../../bluedroid/include/bt_trace.h" 40 | 41 | #include "sbc_decoder.h" 42 | #include "sbc_debug.h" 43 | 44 | #define BUF_SIZE 8192 45 | #define FILESIZE 7000 46 | 47 | #define NUM 119*1 48 | 49 | #define bswap_16(x) \ 50 | ({ \ 51 | UINT16 __x = (x); \ 52 | ((UINT16)( \ 53 | (((UINT16)(__x) & (UINT16)0x00ffU) << 8) | \ 54 | (((UINT16)(__x) & (UINT16)0xff00U) >> 8) )); \ 55 | }) 56 | 57 | #define bswap_32(x) \ 58 | ({ \ 59 | UINT32 __x = (x); \ 60 | ((UINT32)( \ 61 | (((UINT32)(__x) & (UINT32)0x000000ffUL) << 24) | \ 62 | (((UINT32)(__x) & (UINT32)0x0000ff00UL) << 8) | \ 63 | (((UINT32)(__x) & (UINT32)0x00ff0000UL) >> 8) | \ 64 | (((UINT32)(__x) & (UINT32)0xff000000UL) >> 24) )); \ 65 | }) 66 | 67 | #if __BIG_ENDIAN != TRUE 68 | #define COMPOSE_ID(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d)<<24)) 69 | #define LE_SHORT(v) (v) 70 | #define LE_INT(v) (v) 71 | #define BE_SHORT(v) bswap_16(v) 72 | #define BE_INT(v) bswap_32(v) 73 | #else 74 | #define COMPOSE_ID(a,b,c,d) ((d) | ((c)<<8) | ((b)<<16) | ((a)<<24)) 75 | #define LE_SHORT(v) bswap_16(v) 76 | #define LE_INT(v) bswap_32(v) 77 | #define BE_SHORT(v) (v) 78 | #define BE_INT(v) (v) 79 | #endif 80 | 81 | //#define COMPOSE_ID(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d)<<24)) 82 | #define AU_MAGIC COMPOSE_ID('.','s','n','d') 83 | 84 | //#define O_ACCMODE 00000003 85 | //#define O_RDONLY 00000000 86 | //#define O_WRONLY 00000001 87 | 88 | /* allocation method */ 89 | #define SBC_AM_LOUDNESS 0x00 90 | #define SBC_AM_SNR 0x01 91 | 92 | static int verbose = 1; 93 | 94 | #define AU_FMT_ULAW 1 95 | #define AU_FMT_LIN8 2 96 | #define AU_FMT_LIN16 3 97 | 98 | struct au_header { 99 | UINT32 magic; /* '.snd' */ 100 | UINT32 hdr_size; /* size of header (min 24) */ 101 | UINT32 data_size; /* size of data */ 102 | UINT32 encoding; /* see to AU_FMT_XXXX */ 103 | UINT32 sample_rate; /* sample rate */ 104 | UINT32 channels; /* number of channels (voices) */ 105 | }; 106 | 107 | struct stat 108 | { 109 | UINT32 st_size; 110 | }; 111 | 112 | struct option 113 | { 114 | #if defined (__STDC__) && __STDC__ 115 | const char *name; 116 | #else 117 | char *name; 118 | #endif 119 | /* has_arg can't be an enum because some compilers complain about 120 | type mismatches in all the code that assumes it is an int. */ 121 | int has_arg; 122 | int *flag; 123 | int val; 124 | }; 125 | 126 | static void decode(char *filename, char *output, int tofile); 127 | static void usage(void); 128 | int main(int argc, char *argv[]); 129 | 130 | void bdt_log(const char *fmt_str, ...) 131 | { 132 | static char buffer[1024]; 133 | va_list ap; 134 | 135 | va_start(ap, fmt_str); 136 | vsnprintf(buffer, 1024, fmt_str, ap); 137 | va_end(ap); 138 | 139 | fprintf(stdout, "%s\n", buffer); 140 | } 141 | 142 | unsigned char TransferBinary2Int(unsigned char* p) 143 | { 144 | int i=0,sum=0; 145 | char str[8]; 146 | int len=8; 147 | int temp=0; 148 | 149 | memcpy(str,p,8); 150 | static int n=0; 151 | 152 | 153 | for (i=0;ipriv_alloc_base); 193 | 194 | memset(sbc, 0, sizeof(sbc_t)); 195 | } 196 | 197 | static void decode(char *filename, char *output, int tofile) 198 | { 199 | unsigned char buf[BUF_SIZE], *stream,*pBuf, *p; 200 | struct stat st; 201 | st.st_size=8*NUM; 202 | sbc_t sbc; 203 | int fd, ad, pos, streamlen, framelen, count; 204 | UINT32 len; 205 | int frequency, channels; //format = AFMT_S16_BE, 206 | UINT32 written; 207 | FILE *fp=NULL; 208 | int n=0; 209 | 210 | stream =(unsigned char*) malloc(FILESIZE); 211 | memset(stream,0, FILESIZE); 212 | p=stream; 213 | 214 | pBuf=(unsigned char*) malloc(FILESIZE/8); 215 | memset(pBuf,0, FILESIZE/8); 216 | 217 | if (!stream) { 218 | bdt_log("Can't allocate memory for %s\n", 219 | filename); 220 | 221 | return; 222 | } 223 | 224 | fp = fopen(filename, "r"); 225 | if (!fp) { 226 | bdt_log( "Can't open file %s\n", 227 | filename); 228 | goto free; 229 | } 230 | 231 | while (!feof(fp)) 232 | { 233 | fread(p++,1,1,fp); 234 | n++; 235 | 236 | if (feof(fp)) 237 | { 238 | break; 239 | } 240 | } 241 | *(p)='\n'; 242 | 243 | streamlen= getIntBufFromBinary(stream, pBuf); 244 | 245 | fclose(fp); 246 | 247 | pos = 0; 248 | 249 | 250 | if (tofile) 251 | ad = open(output, O_WRONLY | O_CREAT | O_TRUNC, 0644); 252 | else 253 | ad = open(output, O_WRONLY, 0); 254 | 255 | if (ad < 0) { 256 | bdt_log( "Can't open output %s\n", 257 | output); 258 | 259 | goto free; 260 | } 261 | 262 | 263 | 264 | sbc_init(&sbc, 0L); 265 | sbc.endian = SBC_BE; 266 | 267 | framelen = sbc_decode(&sbc, pBuf, streamlen, buf, sizeof(buf), &len); 268 | channels = sbc.mode == SBC_MONO ? 1 : 2; 269 | switch (sbc.frequency) { 270 | case SBC_FREQ_16000: 271 | frequency = 16000; 272 | break; 273 | 274 | case SBC_FREQ_32000: 275 | frequency = 32000; 276 | break; 277 | 278 | case SBC_FREQ_44100: 279 | frequency = 44100; 280 | break; 281 | 282 | case SBC_FREQ_48000: 283 | frequency = 48000; 284 | break; 285 | default: 286 | frequency = 0; 287 | } 288 | #if 1 289 | if (verbose) { 290 | bdt_log("decoding %s with rate %d, %d subbands, " 291 | "%d bits, allocation method %s and mode %s\n", 292 | filename, frequency, sbc.subbands * 4 + 4, sbc.bitpool, 293 | sbc.allocation == SBC_AM_SNR ? "SNR" : "LOUDNESS", 294 | sbc.mode == SBC_MODE_MONO ? "MONO" : 295 | sbc.mode == SBC_MODE_STEREO ? 296 | "STEREO" : "JOINTSTEREO"); 297 | } 298 | #endif 299 | #if 1 300 | if (tofile) { 301 | struct au_header au_hdr; 302 | 303 | au_hdr.magic = AU_MAGIC; 304 | au_hdr.hdr_size = BE_INT(24); 305 | au_hdr.data_size =BE_INT (0); 306 | au_hdr.encoding = BE_INT(AU_FMT_LIN16); 307 | au_hdr.sample_rate =BE_INT (frequency); 308 | au_hdr.channels =BE_INT (channels); 309 | 310 | written = write(ad, &au_hdr, sizeof(au_hdr)); 311 | if (written < (UINT32) sizeof(au_hdr)) { 312 | bdt_log( "Failed to write header\n"); 313 | goto close; 314 | } 315 | } 316 | #endif 317 | #if 0 318 | else { 319 | if (ioctl(ad, SNDCTL_DSP_SETFMT, &format) < 0) { 320 | //fprintf(stderr, "Can't set audio format on %s: %s\n", 321 | //output, strerror(errno)); 322 | goto close; 323 | } 324 | 325 | if (ioctl(ad, SNDCTL_DSP_CHANNELS, &channels) < 0) { 326 | //fprintf(stderr, "Can't set number of channels on %s: %s\n", 327 | // output, strerror(errno)); 328 | goto close; 329 | } 330 | 331 | if (ioctl(ad, SNDCTL_DSP_SPEED, &frequency) < 0) { 332 | //fprintf(stderr, "Can't set audio rate on %s: %s\n", 333 | //output, strerror(errno)); 334 | goto close; 335 | } 336 | } 337 | #endif 338 | count = len; 339 | 340 | while (framelen > 0) { 341 | /* we have completed an sbc_decode at this point sbc.len is the 342 | * length of the frame we just decoded count is the number of 343 | * decoded bytes yet to be written */ 344 | 345 | if (count + len >= BUF_SIZE) { 346 | /* buffer is too full to stuff decoded audio in so it 347 | * must be written to the device */ 348 | written = write(ad, buf, count); 349 | if (written > 0) 350 | count -= written; 351 | } 352 | 353 | /* sanity check */ 354 | if (count + len >= BUF_SIZE) { 355 | //fprintf(stderr, 356 | //"buffer size of %d is too small for decoded" 357 | //" data (%lu)\n", BUF_SIZE, (unsigned long) (len + count)); 358 | exit(1); 359 | } 360 | 361 | /* push the pointer in the file forward to the next bit to be 362 | * decoded tell the decoder to decode up to the remaining 363 | * length of the file (!) */ 364 | pos += framelen; 365 | framelen = sbc_decode(&sbc, pBuf + pos, streamlen - pos, 366 | buf + count, sizeof(buf) - count, &len); 367 | 368 | /* increase the count */ 369 | count += len; 370 | } 371 | 372 | if (count > 0) { 373 | written = write(ad, buf, count); 374 | if (written > 0) 375 | count -= written; 376 | } 377 | 378 | close: 379 | sbc_finish(&sbc); 380 | 381 | close(ad); 382 | 383 | free: 384 | free(stream); 385 | free(pBuf); 386 | } 387 | 388 | static void usage(void) 389 | { 390 | //printf("SBC decoder utility ver %s\n", VERSION); 391 | printf("Copyright (c) 2004-2010 Marcel Holtmann\n\n"); 392 | 393 | printf("Usage:\n" 394 | "\tsbcdec [options] file(s)\n" 395 | "\n"); 396 | 397 | printf("Options:\n" 398 | "\t-h, --help Display help\n" 399 | "\t-v, --verbose Verbose mode\n" 400 | "\t-d, --device Sound device\n" 401 | "\t-f, --file Decode to a file\n" 402 | "\n"); 403 | } 404 | 405 | static struct option main_options[] = { 406 | { "help", 0, 0, 'h' }, 407 | { "device", 1, 0, 'd' }, 408 | { "verbose", 0, 0, 'v' }, 409 | { "file", 1, 0, 'f' }, 410 | { 0, 0, 0, 0 } 411 | }; 412 | 413 | int main(int argc, char *argv[]) 414 | { 415 | 416 | char *output = NULL; 417 | int i, opt, tofile = 0; 418 | 419 | while ((opt = getopt_long(argc, argv, "+hvd:f:", 420 | main_options, NULL)) != -1) { 421 | switch(opt) { 422 | case 'h': 423 | usage(); 424 | exit(0); 425 | 426 | case 'v': 427 | verbose = 1; 428 | break; 429 | 430 | case 'd': 431 | free(output); 432 | output = strdup(optarg); 433 | tofile = 0; 434 | break; 435 | 436 | case 'f' : 437 | free(output); 438 | output = strdup(optarg); 439 | tofile = 1; 440 | break; 441 | 442 | default: 443 | exit(1); 444 | } 445 | } 446 | 447 | argc -= optind; 448 | argv += optind; 449 | optind = 0; 450 | 451 | if (argc < 1) { 452 | usage(); 453 | exit(1); 454 | } 455 | 456 | for (i = 0; i < argc; i++) 457 | decode(argv[i], output ? output : "/dev/dsp", tofile); 458 | 459 | free(output); 460 | 461 | return 0; 462 | } 463 | -------------------------------------------------------------------------------- /test/sbc_debug.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Bluetooth low-complexity, subband codec (SBC) decoder 4 | * 5 | * Copyright (C) 2008-2010 Nokia Corporation 6 | * Copyright (C) 2004-2010 Marcel Holtmann 7 | * 8 | * 9 | * This program is free software; you can redistribute it and/or modify 10 | * it under the terms of the GNU General Public License as published by 11 | * the Free Software Foundation; either version 2 of the License, or 12 | * (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU General Public License 20 | * along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 22 | * 23 | */ 24 | 25 | /****************************************************************************** 26 | * 27 | * This file contains constants and structures used by Decoder. 28 | * 29 | ******************************************************************************/ 30 | #ifndef SBC_DEBUG_H 31 | #define SBC_DEBUF_H 32 | 33 | #define fabs(x) ((x) < 0 ? -(x) : (x)) 34 | #define ASR(val, bits) ((-2 >> 1 == -1) ? \ 35 | ((SINT32)(val)) >> (bits) : ((SINT32) (val)) / (1 << (bits))) 36 | #define SCALE_SPROTO4_TBL 12 37 | #define SCALE_SPROTO8_TBL 14 38 | #define SCALE_NPROTO4_TBL 11 39 | #define SCALE_NPROTO8_TBL 11 40 | #define SCALE4_STAGED1_BITS 15 41 | #define SCALE4_STAGED2_BITS 16 42 | #define SCALE8_STAGED1_BITS 15 43 | #define SCALE8_STAGED2_BITS 16 44 | typedef SINT32 sbc_fixed_t; 45 | #define SCALE4_STAGED1(src) ASR(src, SCALE4_STAGED1_BITS) 46 | #define SCALE4_STAGED2(src) ASR(src, SCALE4_STAGED2_BITS) 47 | #define SCALE8_STAGED1(src) ASR(src, SCALE8_STAGED1_BITS) 48 | #define SCALE8_STAGED2(src) ASR(src, SCALE8_STAGED2_BITS) 49 | #define SBC_FIXED_0(val) { val = 0; } 50 | #define MUL(a, b) ((a) * (b)) 51 | #if defined(__arm__) && (!defined(__thumb__) || defined(__thumb2__)) 52 | #define MULA(a, b, res) ({ \ 53 | int tmp = res; \ 54 | __asm__( \ 55 | "mla %0, %2, %3, %0" \ 56 | : "=&r" (tmp) \ 57 | : "0" (tmp), "r" (a), "r" (b)); \ 58 | tmp; }) 59 | #else 60 | #define MULA(a, b, res) ((a) * (b) + (res)) 61 | #endif 62 | static const int sbc_offset4[4][4] = { 63 | { -1, 0, 0, 0 }, 64 | { -2, 0, 0, 1 }, 65 | { -2, 0, 0, 1 }, 66 | { -2, 0, 0, 1 } 67 | }; 68 | static const int sbc_offset8[4][8] = { 69 | { -2, 0, 0, 0, 0, 0, 0, 1 }, 70 | { -3, 0, 0, 0, 0, 0, 1, 2 }, 71 | { -4, 0, 0, 0, 0, 0, 1, 2 }, 72 | { -4, 0, 0, 0, 0, 0, 1, 2 } 73 | }; 74 | #define SBCDEC_FIXED_EXTRA_BITS 2 75 | #define SS4(val) ASR(val, SCALE_SPROTO4_TBL) 76 | #define SS8(val) ASR(val, SCALE_SPROTO8_TBL) 77 | #define SN4(val) ASR(val, SCALE_NPROTO4_TBL + 1 + SBCDEC_FIXED_EXTRA_BITS) 78 | #define SN8(val) ASR(val, SCALE_NPROTO8_TBL + 1 + SBCDEC_FIXED_EXTRA_BITS) 79 | static const SINT32 sbc_proto_4_40m0[] = { 80 | SS4(0x00000000), SS4(0xffa6982f), SS4(0xfba93848), SS4(0x0456c7b8), 81 | SS4(0x005967d1), SS4(0xfffb9ac7), SS4(0xff589157), SS4(0xf9c2a8d8), 82 | SS4(0x027c1434), SS4(0x0019118b), SS4(0xfff3c74c), SS4(0xff137330), 83 | SS4(0xf81b8d70), SS4(0x00ec1b8b), SS4(0xfff0b71a), SS4(0xffe99b00), 84 | SS4(0xfef84470), SS4(0xf6fb4370), SS4(0xffcdc351), SS4(0xffe01dc7) 85 | }; 86 | static const SINT32 sbc_proto_4_40m1[] = { 87 | SS4(0xffe090ce), SS4(0xff2c0475), SS4(0xf694f800), SS4(0xff2c0475), 88 | SS4(0xffe090ce), SS4(0xffe01dc7), SS4(0xffcdc351), SS4(0xf6fb4370), 89 | SS4(0xfef84470), SS4(0xffe99b00), SS4(0xfff0b71a), SS4(0x00ec1b8b), 90 | SS4(0xf81b8d70), SS4(0xff137330), SS4(0xfff3c74c), SS4(0x0019118b), 91 | SS4(0x027c1434), SS4(0xf9c2a8d8), SS4(0xff589157), SS4(0xfffb9ac7) 92 | }; 93 | static const SINT32 sbc_proto_8_80m0[] = { 94 | SS8(0x00000000), SS8(0xfe8d1970), SS8(0xee979f00), SS8(0x11686100), 95 | SS8(0x0172e690), SS8(0xfff5bd1a), SS8(0xfdf1c8d4), SS8(0xeac182c0), 96 | SS8(0x0d9daee0), SS8(0x00e530da), SS8(0xffe9811d), SS8(0xfd52986c), 97 | SS8(0xe7054ca0), SS8(0x0a00d410), SS8(0x006c1de4), SS8(0xffdba705), 98 | SS8(0xfcbc98e8), SS8(0xe3889d20), SS8(0x06af2308), SS8(0x000bb7db), 99 | SS8(0xffca00ed), SS8(0xfc3fbb68), SS8(0xe071bc00), SS8(0x03bf7948), 100 | SS8(0xffc4e05c), SS8(0xffb54b3b), SS8(0xfbedadc0), SS8(0xdde26200), 101 | SS8(0x0142291c), SS8(0xff960e94), SS8(0xff9f3e17), SS8(0xfbd8f358), 102 | SS8(0xdbf79400), SS8(0xff405e01), SS8(0xff7d4914), SS8(0xff8b1a31), 103 | SS8(0xfc1417b8), SS8(0xdac7bb40), SS8(0xfdbb828c), SS8(0xff762170) 104 | }; 105 | static const SINT32 sbc_proto_8_80m1[] = { 106 | SS8(0xff7c272c), SS8(0xfcb02620), SS8(0xda612700), SS8(0xfcb02620), 107 | SS8(0xff7c272c), SS8(0xff762170), SS8(0xfdbb828c), SS8(0xdac7bb40), 108 | SS8(0xfc1417b8), SS8(0xff8b1a31), SS8(0xff7d4914), SS8(0xff405e01), 109 | SS8(0xdbf79400), SS8(0xfbd8f358), SS8(0xff9f3e17), SS8(0xff960e94), 110 | SS8(0x0142291c), SS8(0xdde26200), SS8(0xfbedadc0), SS8(0xffb54b3b), 111 | SS8(0xffc4e05c), SS8(0x03bf7948), SS8(0xe071bc00), SS8(0xfc3fbb68), 112 | SS8(0xffca00ed), SS8(0x000bb7db), SS8(0x06af2308), SS8(0xe3889d20), 113 | SS8(0xfcbc98e8), SS8(0xffdba705), SS8(0x006c1de4), SS8(0x0a00d410), 114 | SS8(0xe7054ca0), SS8(0xfd52986c), SS8(0xffe9811d), SS8(0x00e530da), 115 | SS8(0x0d9daee0), SS8(0xeac182c0), SS8(0xfdf1c8d4), SS8(0xfff5bd1a) 116 | }; 117 | static const SINT32 synmatrix4[8][4] = { 118 | { SN4(0x05a82798), SN4(0xfa57d868), SN4(0xfa57d868), SN4(0x05a82798) }, 119 | { SN4(0x030fbc54), SN4(0xf89be510), SN4(0x07641af0), SN4(0xfcf043ac) }, 120 | { SN4(0x00000000), SN4(0x00000000), SN4(0x00000000), SN4(0x00000000) }, 121 | { SN4(0xfcf043ac), SN4(0x07641af0), SN4(0xf89be510), SN4(0x030fbc54) }, 122 | { SN4(0xfa57d868), SN4(0x05a82798), SN4(0x05a82798), SN4(0xfa57d868) }, 123 | { SN4(0xf89be510), SN4(0xfcf043ac), SN4(0x030fbc54), SN4(0x07641af0) }, 124 | { SN4(0xf8000000), SN4(0xf8000000), SN4(0xf8000000), SN4(0xf8000000) }, 125 | { SN4(0xf89be510), SN4(0xfcf043ac), SN4(0x030fbc54), SN4(0x07641af0) } 126 | }; 127 | static const SINT32 synmatrix8[16][8] = { 128 | { SN8(0x05a82798), SN8(0xfa57d868), SN8(0xfa57d868), SN8(0x05a82798), 129 | SN8(0x05a82798), SN8(0xfa57d868), SN8(0xfa57d868), SN8(0x05a82798) }, 130 | { SN8(0x0471ced0), SN8(0xf8275a10), SN8(0x018f8b84), SN8(0x06a6d988), 131 | SN8(0xf9592678), SN8(0xfe70747c), SN8(0x07d8a5f0), SN8(0xfb8e3130) }, 132 | { SN8(0x030fbc54), SN8(0xf89be510), SN8(0x07641af0), SN8(0xfcf043ac), 133 | SN8(0xfcf043ac), SN8(0x07641af0), SN8(0xf89be510), SN8(0x030fbc54) }, 134 | { SN8(0x018f8b84), SN8(0xfb8e3130), SN8(0x06a6d988), SN8(0xf8275a10), 135 | SN8(0x07d8a5f0), SN8(0xf9592678), SN8(0x0471ced0), SN8(0xfe70747c) }, 136 | { SN8(0x00000000), SN8(0x00000000), SN8(0x00000000), SN8(0x00000000), 137 | SN8(0x00000000), SN8(0x00000000), SN8(0x00000000), SN8(0x00000000) }, 138 | { SN8(0xfe70747c), SN8(0x0471ced0), SN8(0xf9592678), SN8(0x07d8a5f0), 139 | SN8(0xf8275a10), SN8(0x06a6d988), SN8(0xfb8e3130), SN8(0x018f8b84) }, 140 | { SN8(0xfcf043ac), SN8(0x07641af0), SN8(0xf89be510), SN8(0x030fbc54), 141 | SN8(0x030fbc54), SN8(0xf89be510), SN8(0x07641af0), SN8(0xfcf043ac) }, 142 | { SN8(0xfb8e3130), SN8(0x07d8a5f0), SN8(0xfe70747c), SN8(0xf9592678), 143 | SN8(0x06a6d988), SN8(0x018f8b84), SN8(0xf8275a10), SN8(0x0471ced0) }, 144 | { SN8(0xfa57d868), SN8(0x05a82798), SN8(0x05a82798), SN8(0xfa57d868), 145 | SN8(0xfa57d868), SN8(0x05a82798), SN8(0x05a82798), SN8(0xfa57d868) }, 146 | { SN8(0xf9592678), SN8(0x018f8b84), SN8(0x07d8a5f0), SN8(0x0471ced0), 147 | SN8(0xfb8e3130), SN8(0xf8275a10), SN8(0xfe70747c), SN8(0x06a6d988) }, 148 | { SN8(0xf89be510), SN8(0xfcf043ac), SN8(0x030fbc54), SN8(0x07641af0), 149 | SN8(0x07641af0), SN8(0x030fbc54), SN8(0xfcf043ac), SN8(0xf89be510) }, 150 | { SN8(0xf8275a10), SN8(0xf9592678), SN8(0xfb8e3130), SN8(0xfe70747c), 151 | SN8(0x018f8b84), SN8(0x0471ced0), SN8(0x06a6d988), SN8(0x07d8a5f0) }, 152 | { SN8(0xf8000000), SN8(0xf8000000), SN8(0xf8000000), SN8(0xf8000000), 153 | SN8(0xf8000000), SN8(0xf8000000), SN8(0xf8000000), SN8(0xf8000000) }, 154 | { SN8(0xf8275a10), SN8(0xf9592678), SN8(0xfb8e3130), SN8(0xfe70747c), 155 | SN8(0x018f8b84), SN8(0x0471ced0), SN8(0x06a6d988), SN8(0x07d8a5f0) }, 156 | { SN8(0xf89be510), SN8(0xfcf043ac), SN8(0x030fbc54), SN8(0x07641af0), 157 | SN8(0x07641af0), SN8(0x030fbc54), SN8(0xfcf043ac), SN8(0xf89be510) }, 158 | { SN8(0xf9592678), SN8(0x018f8b84), SN8(0x07d8a5f0), SN8(0x0471ced0), 159 | SN8(0xfb8e3130), SN8(0xf8275a10), SN8(0xfe70747c), SN8(0x06a6d988) } 160 | }; 161 | #define EPERM 1 /* Operation not permitted */ 162 | #define ENOENT 2 /* No such file or directory */ 163 | #define ESRCH 3 /* No such process */ 164 | #define EINTR 4 /* Interrupted system call */ 165 | #define EIO 5 /* I/O error */ 166 | #define ENXIO 6 /* No such device or address */ 167 | #define E2BIG 7 /* Argument list too long */ 168 | #define ENOEXEC 8 /* Exec format error */ 169 | #define EBADF 9 /* Bad file number */ 170 | #define ECHILD 10 /* No child processes */ 171 | #define EAGAIN 11 /* Try again */ 172 | #define ENOMEM 12 /* Out of memory */ 173 | #define EACCES 13 /* Permission denied */ 174 | #define EFAULT 14 /* Bad address */ 175 | #define ENOTBLK 15 /* Block device required */ 176 | #define EBUSY 16 /* Device or resource busy */ 177 | #define EEXIST 17 /* File exists */ 178 | #define EXDEV 18 /* Cross-device link */ 179 | #define ENODEV 19 /* No such device */ 180 | #define ENOTDIR 20 /* Not a directory */ 181 | #define EISDIR 21 /* Is a directory */ 182 | #define EINVAL 22 /* Invalid argument */ 183 | #define ENFILE 23 /* File table overflow */ 184 | #define EMFILE 24 /* Too many open files */ 185 | #define ENOTTY 25 /* Not a typewriter */ 186 | #define ETXTBSY 26 /* Text file busy */ 187 | #define EFBIG 27 /* File too large */ 188 | #define ENOSPC 28 /* No space left on device */ 189 | #define ESPIPE 29 /* Illegal seek */ 190 | #define EROFS 30 /* Read-only file system */ 191 | #define EMLINK 31 /* Too many links */ 192 | #define EPIPE 32 /* Broken pipe */ 193 | #define EDOM 33 /* Math argument out of domain of func */ 194 | #define ERANGE 34 /* Math result not representable */ 195 | #define SBC_BLK_4 0x00 196 | #define SBC_BLK_8 0x01 197 | #define SBC_BLK_12 0x02 198 | #define SBC_BLK_16 0x03 199 | #define SBC_SB_4 0x00 200 | #define SBC_SB_8 0x01 201 | #define SBC_FREQ_16000 0x00 202 | #define SBC_FREQ_32000 0x01 203 | #define SBC_FREQ_44100 0x02 204 | #define SBC_FREQ_48000 0x03 205 | #define SBC_MODE_MONO 0x00 206 | #define SBC_MODE_DUAL_CHANNEL 0x01 207 | #define SBC_MODE_STEREO 0x02 208 | #define SBC_MODE_JOINT_STEREO 0x03 209 | #define SBC_ALIGN_BITS 4 210 | #define SBC_ALIGN_MASK ((1 << (SBC_ALIGN_BITS)) - 1) 211 | #define SBC_SYNCWORD 0x9C 212 | #define SBC_LE 0x00 213 | #define SBC_BE 0x01 214 | #ifdef __GNUC__ 215 | #define SBC_ALIGNED __attribute__((aligned(1 << (SBC_ALIGN_BITS)))) 216 | #else 217 | #define SBC_ALIGNED 218 | #endif 219 | struct sbc_decoder_state { 220 | int subbands; 221 | SINT32 V[2][170]; 222 | int offset[2][16]; 223 | }; 224 | struct sbc_struct { 225 | unsigned long flags; 226 | UINT8 frequency; 227 | UINT8 blocks; 228 | UINT8 subbands; 229 | UINT8 mode; 230 | UINT8 allocation; 231 | UINT8 bitpool; 232 | UINT8 endian; 233 | void *priv; 234 | void *priv_alloc_base; 235 | }; 236 | typedef struct sbc_struct sbc_t; 237 | struct sbc_frame { 238 | UINT8 frequency; 239 | UINT8 block_mode; 240 | UINT8 blocks; 241 | enum { 242 | MONO = SBC_MONO, 243 | DUAL_CHANNEL = SBC_DUAL, 244 | STEREO = SBC_STEREO, 245 | JOINT_STEREO = SBC_JOINT_STEREO 246 | } mode; 247 | UINT8 channels; 248 | enum { 249 | LOUDNESS = SBC_LOUDNESS, 250 | SNR = SBC_SNR 251 | } allocation; 252 | UINT8 subband_mode; 253 | UINT8 subbands; 254 | UINT8 bitpool; 255 | UINT16 codesize; 256 | UINT8 length; 257 | UINT8 joint; 258 | UINT32 scale_factor[2][8]; 259 | SINT32 sb_sample_f[16][2][8]; 260 | SINT32 sb_sample[16][2][8]; 261 | SINT16 pcm_sample[2][16*8]; 262 | }; 263 | struct sbc_priv { 264 | int init; 265 | struct sbc_frame frame; 266 | struct sbc_decoder_state dec_state; 267 | }; 268 | #endif 269 | -------------------------------------------------------------------------------- /decoder/srce/sbc_decoder.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * Bluetooth low-complexity, subband codec (SBC) library 4 | * 5 | * Copyright (C) 2014 Tieto Corporation 6 | * Copyright (C) 2008-2010 Nokia Corporation 7 | * Copyright (C) 2004-2010 Marcel Holtmann 8 | * Copyright (C) 2004-2005 Henryk Ploetz 9 | * Copyright (C) 2005-2008 Brad Midgley 10 | * 11 | * 12 | * This library is free software; you can redistribute it and/or 13 | * modify it under the terms of the GNU Lesser General Public 14 | * License as published by the Free Software Foundation; either 15 | * version 2.1 of the License, or (at your option) any later version. 16 | * 17 | * This library is distributed in the hope that it will be useful, 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20 | * Lesser General Public License for more details. 21 | * 22 | * You should have received a copy of the GNU Lesser General Public 23 | * License along with this library; if not, write to the Free Software 24 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 25 | * 26 | */ 27 | 28 | /****************************************************************************** 29 | * 30 | * contains code for decoder flow and initalization of decoder 31 | * 32 | ******************************************************************************/ 33 | #include 34 | #include 35 | #include "sbc_decoder.h" 36 | #include "../../../bluedroid/include/bt_trace.h" 37 | 38 | #define fabs(x) ((x) < 0 ? -(x) : (x)) 39 | #define ASR(val, bits) ((-2 >> 1 == -1) ? \ 40 | ((int)(val)) >> (bits) : ((int) (val)) / (1 << (bits))) 41 | #define SCALE_SPROTO4_TBL 12 42 | #define SCALE_SPROTO8_TBL 14 43 | #define SCALE_NPROTO4_TBL 11 44 | #define SCALE_NPROTO8_TBL 11 45 | #define SCALE4_STAGED1_BITS 15 46 | #define SCALE4_STAGED2_BITS 16 47 | #define SCALE8_STAGED1_BITS 15 48 | #define SCALE8_STAGED2_BITS 16 49 | typedef int sbc_fixed_t; 50 | #define SCALE4_STAGED1(src) ASR(src, SCALE4_STAGED1_BITS) 51 | #define SCALE4_STAGED2(src) ASR(src, SCALE4_STAGED2_BITS) 52 | #define SCALE8_STAGED1(src) ASR(src, SCALE8_STAGED1_BITS) 53 | #define SCALE8_STAGED2(src) ASR(src, SCALE8_STAGED2_BITS) 54 | #define SBC_FIXED_0(val) { val = 0; } 55 | #define MUL(a, b) ((a) * (b)) 56 | #if defined(__arm__) && (!defined(__thumb__) || defined(__thumb2__)) 57 | #define MULA(a, b, res) ({ \ 58 | int tmp = res; \ 59 | __asm__( \ 60 | "mla %0, %2, %3, %0" \ 61 | : "=&r" (tmp) \ 62 | : "0" (tmp), "r" (a), "r" (b)); \ 63 | tmp; }) 64 | #else 65 | #define MULA(a, b, res) ((a) * (b) + (res)) 66 | #endif 67 | 68 | #define EPERM 1 /* Operation not permitted */ 69 | #define ENOENT 2 /* No such file or directory */ 70 | #define ESRCH 3 /* No such process */ 71 | #define EINTR 4 /* Interrupted system call */ 72 | #define EIO 5 /* I/O error */ 73 | #define ENXIO 6 /* No such device or address */ 74 | #define E2BIG 7 /* Argument list too long */ 75 | #define ENOEXEC 8 /* Exec format error */ 76 | #define EBADF 9 /* Bad file number */ 77 | #define ECHILD 10 /* No child processes */ 78 | #define EAGAIN 11 /* Try again */ 79 | #define ENOMEM 12 /* Out of memory */ 80 | #define EACCES 13 /* Permission denied */ 81 | #define EFAULT 14 /* Bad address */ 82 | #define ENOTBLK 15 /* Block device required */ 83 | #define EBUSY 16 /* Device or resource busy */ 84 | #define EEXIST 17 /* File exists */ 85 | #define EXDEV 18 /* Cross-device link */ 86 | #define ENODEV 19 /* No such device */ 87 | #define ENOTDIR 20 /* Not a directory */ 88 | #define EISDIR 21 /* Is a directory */ 89 | #define EINVAL 22 /* Invalid argument */ 90 | #define ENFILE 23 /* File table overflow */ 91 | #define EMFILE 24 /* Too many open files */ 92 | #define ENOTTY 25 /* Not a typewriter */ 93 | #define ETXTBSY 26 /* Text file busy */ 94 | #define EFBIG 27 /* File too large */ 95 | #define ENOSPC 28 /* No space left on device */ 96 | #define ESPIPE 29 /* Illegal seek */ 97 | #define EROFS 30 /* Read-only file system */ 98 | #define EMLINK 31 /* Too many links */ 99 | #define EPIPE 32 /* Broken pipe */ 100 | #define EDOM 33 /* Math argument out of domain of func */ 101 | #define ERANGE 34 /* Math result not representable */ 102 | #define SBC_BLK_4 0x00 103 | #define SBC_BLK_8 0x01 104 | #define SBC_BLK_12 0x02 105 | #define SBC_BLK_16 0x03 106 | #define SBC_SB_4 0x00 107 | #define SBC_SB_8 0x01 108 | #define SBC_FREQ_16000 0x00 109 | #define SBC_FREQ_32000 0x01 110 | #define SBC_FREQ_44100 0x02 111 | #define SBC_FREQ_48000 0x03 112 | #define SBC_MODE_MONO 0x00 113 | #define SBC_MODE_DUAL_CHANNEL 0x01 114 | #define SBC_MODE_STEREO 0x02 115 | #define SBC_MODE_JOINT_STEREO 0x03 116 | #define SBC_ALIGN_BITS 4 117 | #define SBC_ALIGN_MASK ((1 << (SBC_ALIGN_BITS)) - 1) 118 | #define SBC_SYNCWORD 0x9C 119 | #define SBC_LE 0x00 120 | #define SBC_BE 0x01 121 | #ifdef __GNUC__ 122 | #define SBC_ALIGNED __attribute__((aligned(1 << (SBC_ALIGN_BITS)))) 123 | #else 124 | #define SBC_ALIGNED 125 | #endif 126 | 127 | 128 | struct sbc_decoder_state { 129 | int subbands; 130 | int V[2][170]; 131 | int offset[2][16]; 132 | }; 133 | struct sbc_struct { 134 | unsigned long flags; 135 | unsigned char frequency; 136 | unsigned char blocks; 137 | unsigned char subbands; 138 | unsigned char mode; 139 | unsigned char allocation; 140 | unsigned char bitpool; 141 | unsigned char endian; 142 | void *priv; 143 | void *priv_alloc_base; 144 | }; 145 | typedef struct sbc_struct sbc_t; 146 | struct sbc_frame { 147 | unsigned char frequency; 148 | unsigned char block_mode; 149 | unsigned char blocks; 150 | enum { 151 | MONO = SBC_MONO, 152 | DUAL_CHANNEL = SBC_DUAL, 153 | STEREO = SBC_STEREO, 154 | JOINT_STEREO = SBC_JOINT_STEREO 155 | } mode; 156 | unsigned char channels; 157 | enum { 158 | LOUDNESS = SBC_LOUDNESS, 159 | SNR = SBC_SNR 160 | } allocation; 161 | unsigned char subband_mode; 162 | unsigned char subbands; 163 | unsigned char bitpool; 164 | unsigned short codesize; 165 | unsigned char length; 166 | unsigned char joint; 167 | unsigned int scale_factor[2][8]; 168 | int sb_sample_f[16][2][8]; 169 | int sb_sample[16][2][8]; 170 | short pcm_sample[2][16*8]; 171 | }; 172 | struct sbc_priv { 173 | int init; 174 | struct sbc_frame frame; 175 | struct sbc_decoder_state dec_state; 176 | }; 177 | static const int sbc_offset4[4][4] = { 178 | { -1, 0, 0, 0 }, 179 | { -2, 0, 0, 1 }, 180 | { -2, 0, 0, 1 }, 181 | { -2, 0, 0, 1 } 182 | }; 183 | static const int sbc_offset8[4][8] = { 184 | { -2, 0, 0, 0, 0, 0, 0, 1 }, 185 | { -3, 0, 0, 0, 0, 0, 1, 2 }, 186 | { -4, 0, 0, 0, 0, 0, 1, 2 }, 187 | { -4, 0, 0, 0, 0, 0, 1, 2 } 188 | }; 189 | #define SBCDEC_FIXED_EXTRA_BITS 2 190 | #define SS4(val) ASR(val, SCALE_SPROTO4_TBL) 191 | #define SS8(val) ASR(val, SCALE_SPROTO8_TBL) 192 | #define SN4(val) ASR(val, SCALE_NPROTO4_TBL + 1 + SBCDEC_FIXED_EXTRA_BITS) 193 | #define SN8(val) ASR(val, SCALE_NPROTO8_TBL + 1 + SBCDEC_FIXED_EXTRA_BITS) 194 | static const int sbc_proto_4_40m0[] = { 195 | SS4(0x00000000), SS4(0xffa6982f), SS4(0xfba93848), SS4(0x0456c7b8), 196 | SS4(0x005967d1), SS4(0xfffb9ac7), SS4(0xff589157), SS4(0xf9c2a8d8), 197 | SS4(0x027c1434), SS4(0x0019118b), SS4(0xfff3c74c), SS4(0xff137330), 198 | SS4(0xf81b8d70), SS4(0x00ec1b8b), SS4(0xfff0b71a), SS4(0xffe99b00), 199 | SS4(0xfef84470), SS4(0xf6fb4370), SS4(0xffcdc351), SS4(0xffe01dc7) 200 | }; 201 | static const int sbc_proto_4_40m1[] = { 202 | SS4(0xffe090ce), SS4(0xff2c0475), SS4(0xf694f800), SS4(0xff2c0475), 203 | SS4(0xffe090ce), SS4(0xffe01dc7), SS4(0xffcdc351), SS4(0xf6fb4370), 204 | SS4(0xfef84470), SS4(0xffe99b00), SS4(0xfff0b71a), SS4(0x00ec1b8b), 205 | SS4(0xf81b8d70), SS4(0xff137330), SS4(0xfff3c74c), SS4(0x0019118b), 206 | SS4(0x027c1434), SS4(0xf9c2a8d8), SS4(0xff589157), SS4(0xfffb9ac7) 207 | }; 208 | static const int sbc_proto_8_80m0[] = { 209 | SS8(0x00000000), SS8(0xfe8d1970), SS8(0xee979f00), SS8(0x11686100), 210 | SS8(0x0172e690), SS8(0xfff5bd1a), SS8(0xfdf1c8d4), SS8(0xeac182c0), 211 | SS8(0x0d9daee0), SS8(0x00e530da), SS8(0xffe9811d), SS8(0xfd52986c), 212 | SS8(0xe7054ca0), SS8(0x0a00d410), SS8(0x006c1de4), SS8(0xffdba705), 213 | SS8(0xfcbc98e8), SS8(0xe3889d20), SS8(0x06af2308), SS8(0x000bb7db), 214 | SS8(0xffca00ed), SS8(0xfc3fbb68), SS8(0xe071bc00), SS8(0x03bf7948), 215 | SS8(0xffc4e05c), SS8(0xffb54b3b), SS8(0xfbedadc0), SS8(0xdde26200), 216 | SS8(0x0142291c), SS8(0xff960e94), SS8(0xff9f3e17), SS8(0xfbd8f358), 217 | SS8(0xdbf79400), SS8(0xff405e01), SS8(0xff7d4914), SS8(0xff8b1a31), 218 | SS8(0xfc1417b8), SS8(0xdac7bb40), SS8(0xfdbb828c), SS8(0xff762170) 219 | }; 220 | static const int sbc_proto_8_80m1[] = { 221 | SS8(0xff7c272c), SS8(0xfcb02620), SS8(0xda612700), SS8(0xfcb02620), 222 | SS8(0xff7c272c), SS8(0xff762170), SS8(0xfdbb828c), SS8(0xdac7bb40), 223 | SS8(0xfc1417b8), SS8(0xff8b1a31), SS8(0xff7d4914), SS8(0xff405e01), 224 | SS8(0xdbf79400), SS8(0xfbd8f358), SS8(0xff9f3e17), SS8(0xff960e94), 225 | SS8(0x0142291c), SS8(0xdde26200), SS8(0xfbedadc0), SS8(0xffb54b3b), 226 | SS8(0xffc4e05c), SS8(0x03bf7948), SS8(0xe071bc00), SS8(0xfc3fbb68), 227 | SS8(0xffca00ed), SS8(0x000bb7db), SS8(0x06af2308), SS8(0xe3889d20), 228 | SS8(0xfcbc98e8), SS8(0xffdba705), SS8(0x006c1de4), SS8(0x0a00d410), 229 | SS8(0xe7054ca0), SS8(0xfd52986c), SS8(0xffe9811d), SS8(0x00e530da), 230 | SS8(0x0d9daee0), SS8(0xeac182c0), SS8(0xfdf1c8d4), SS8(0xfff5bd1a) 231 | }; 232 | static const int synmatrix4[8][4] = { 233 | { SN4(0x05a82798), SN4(0xfa57d868), SN4(0xfa57d868), SN4(0x05a82798) }, 234 | { SN4(0x030fbc54), SN4(0xf89be510), SN4(0x07641af0), SN4(0xfcf043ac) }, 235 | { SN4(0x00000000), SN4(0x00000000), SN4(0x00000000), SN4(0x00000000) }, 236 | { SN4(0xfcf043ac), SN4(0x07641af0), SN4(0xf89be510), SN4(0x030fbc54) }, 237 | { SN4(0xfa57d868), SN4(0x05a82798), SN4(0x05a82798), SN4(0xfa57d868) }, 238 | { SN4(0xf89be510), SN4(0xfcf043ac), SN4(0x030fbc54), SN4(0x07641af0) }, 239 | { SN4(0xf8000000), SN4(0xf8000000), SN4(0xf8000000), SN4(0xf8000000) }, 240 | { SN4(0xf89be510), SN4(0xfcf043ac), SN4(0x030fbc54), SN4(0x07641af0) } 241 | }; 242 | static const int synmatrix8[16][8] = { 243 | { SN8(0x05a82798), SN8(0xfa57d868), SN8(0xfa57d868), SN8(0x05a82798), 244 | SN8(0x05a82798), SN8(0xfa57d868), SN8(0xfa57d868), SN8(0x05a82798) }, 245 | { SN8(0x0471ced0), SN8(0xf8275a10), SN8(0x018f8b84), SN8(0x06a6d988), 246 | SN8(0xf9592678), SN8(0xfe70747c), SN8(0x07d8a5f0), SN8(0xfb8e3130) }, 247 | { SN8(0x030fbc54), SN8(0xf89be510), SN8(0x07641af0), SN8(0xfcf043ac), 248 | SN8(0xfcf043ac), SN8(0x07641af0), SN8(0xf89be510), SN8(0x030fbc54) }, 249 | { SN8(0x018f8b84), SN8(0xfb8e3130), SN8(0x06a6d988), SN8(0xf8275a10), 250 | SN8(0x07d8a5f0), SN8(0xf9592678), SN8(0x0471ced0), SN8(0xfe70747c) }, 251 | { SN8(0x00000000), SN8(0x00000000), SN8(0x00000000), SN8(0x00000000), 252 | SN8(0x00000000), SN8(0x00000000), SN8(0x00000000), SN8(0x00000000) }, 253 | { SN8(0xfe70747c), SN8(0x0471ced0), SN8(0xf9592678), SN8(0x07d8a5f0), 254 | SN8(0xf8275a10), SN8(0x06a6d988), SN8(0xfb8e3130), SN8(0x018f8b84) }, 255 | { SN8(0xfcf043ac), SN8(0x07641af0), SN8(0xf89be510), SN8(0x030fbc54), 256 | SN8(0x030fbc54), SN8(0xf89be510), SN8(0x07641af0), SN8(0xfcf043ac) }, 257 | { SN8(0xfb8e3130), SN8(0x07d8a5f0), SN8(0xfe70747c), SN8(0xf9592678), 258 | SN8(0x06a6d988), SN8(0x018f8b84), SN8(0xf8275a10), SN8(0x0471ced0) }, 259 | { SN8(0xfa57d868), SN8(0x05a82798), SN8(0x05a82798), SN8(0xfa57d868), 260 | SN8(0xfa57d868), SN8(0x05a82798), SN8(0x05a82798), SN8(0xfa57d868) }, 261 | { SN8(0xf9592678), SN8(0x018f8b84), SN8(0x07d8a5f0), SN8(0x0471ced0), 262 | SN8(0xfb8e3130), SN8(0xf8275a10), SN8(0xfe70747c), SN8(0x06a6d988) }, 263 | { SN8(0xf89be510), SN8(0xfcf043ac), SN8(0x030fbc54), SN8(0x07641af0), 264 | SN8(0x07641af0), SN8(0x030fbc54), SN8(0xfcf043ac), SN8(0xf89be510) }, 265 | { SN8(0xf8275a10), SN8(0xf9592678), SN8(0xfb8e3130), SN8(0xfe70747c), 266 | SN8(0x018f8b84), SN8(0x0471ced0), SN8(0x06a6d988), SN8(0x07d8a5f0) }, 267 | { SN8(0xf8000000), SN8(0xf8000000), SN8(0xf8000000), SN8(0xf8000000), 268 | SN8(0xf8000000), SN8(0xf8000000), SN8(0xf8000000), SN8(0xf8000000) }, 269 | { SN8(0xf8275a10), SN8(0xf9592678), SN8(0xfb8e3130), SN8(0xfe70747c), 270 | SN8(0x018f8b84), SN8(0x0471ced0), SN8(0x06a6d988), SN8(0x07d8a5f0) }, 271 | { SN8(0xf89be510), SN8(0xfcf043ac), SN8(0x030fbc54), SN8(0x07641af0), 272 | SN8(0x07641af0), SN8(0x030fbc54), SN8(0xfcf043ac), SN8(0xf89be510) }, 273 | { SN8(0xf9592678), SN8(0x018f8b84), SN8(0x07d8a5f0), SN8(0x0471ced0), 274 | SN8(0xfb8e3130), SN8(0xf8275a10), SN8(0xfe70747c), SN8(0x06a6d988) } 275 | }; 276 | /* 277 | * Calculates the CRC-8 of the first len bits in data 278 | */ 279 | static const unsigned char crc_table[256] = { 280 | 0x00, 0x1D, 0x3A, 0x27, 0x74, 0x69, 0x4E, 0x53, 281 | 0xE8, 0xF5, 0xD2, 0xCF, 0x9C, 0x81, 0xA6, 0xBB, 282 | 0xCD, 0xD0, 0xF7, 0xEA, 0xB9, 0xA4, 0x83, 0x9E, 283 | 0x25, 0x38, 0x1F, 0x02, 0x51, 0x4C, 0x6B, 0x76, 284 | 0x87, 0x9A, 0xBD, 0xA0, 0xF3, 0xEE, 0xC9, 0xD4, 285 | 0x6F, 0x72, 0x55, 0x48, 0x1B, 0x06, 0x21, 0x3C, 286 | 0x4A, 0x57, 0x70, 0x6D, 0x3E, 0x23, 0x04, 0x19, 287 | 0xA2, 0xBF, 0x98, 0x85, 0xD6, 0xCB, 0xEC, 0xF1, 288 | 0x13, 0x0E, 0x29, 0x34, 0x67, 0x7A, 0x5D, 0x40, 289 | 0xFB, 0xE6, 0xC1, 0xDC, 0x8F, 0x92, 0xB5, 0xA8, 290 | 0xDE, 0xC3, 0xE4, 0xF9, 0xAA, 0xB7, 0x90, 0x8D, 291 | 0x36, 0x2B, 0x0C, 0x11, 0x42, 0x5F, 0x78, 0x65, 292 | 0x94, 0x89, 0xAE, 0xB3, 0xE0, 0xFD, 0xDA, 0xC7, 293 | 0x7C, 0x61, 0x46, 0x5B, 0x08, 0x15, 0x32, 0x2F, 294 | 0x59, 0x44, 0x63, 0x7E, 0x2D, 0x30, 0x17, 0x0A, 295 | 0xB1, 0xAC, 0x8B, 0x96, 0xC5, 0xD8, 0xFF, 0xE2, 296 | 0x26, 0x3B, 0x1C, 0x01, 0x52, 0x4F, 0x68, 0x75, 297 | 0xCE, 0xD3, 0xF4, 0xE9, 0xBA, 0xA7, 0x80, 0x9D, 298 | 0xEB, 0xF6, 0xD1, 0xCC, 0x9F, 0x82, 0xA5, 0xB8, 299 | 0x03, 0x1E, 0x39, 0x24, 0x77, 0x6A, 0x4D, 0x50, 300 | 0xA1, 0xBC, 0x9B, 0x86, 0xD5, 0xC8, 0xEF, 0xF2, 301 | 0x49, 0x54, 0x73, 0x6E, 0x3D, 0x20, 0x07, 0x1A, 302 | 0x6C, 0x71, 0x56, 0x4B, 0x18, 0x05, 0x22, 0x3F, 303 | 0x84, 0x99, 0xBE, 0xA3, 0xF0, 0xED, 0xCA, 0xD7, 304 | 0x35, 0x28, 0x0F, 0x12, 0x41, 0x5C, 0x7B, 0x66, 305 | 0xDD, 0xC0, 0xE7, 0xFA, 0xA9, 0xB4, 0x93, 0x8E, 306 | 0xF8, 0xE5, 0xC2, 0xDF, 0x8C, 0x91, 0xB6, 0xAB, 307 | 0x10, 0x0D, 0x2A, 0x37, 0x64, 0x79, 0x5E, 0x43, 308 | 0xB2, 0xAF, 0x88, 0x95, 0xC6, 0xDB, 0xFC, 0xE1, 309 | 0x5A, 0x47, 0x60, 0x7D, 0x2E, 0x33, 0x14, 0x09, 310 | 0x7F, 0x62, 0x45, 0x58, 0x0B, 0x16, 0x31, 0x2C, 311 | 0x97, 0x8A, 0xAD, 0xB0, 0xE3, 0xFE, 0xD9, 0xC4 312 | }; 313 | 314 | static unsigned char sbc_crc8(const unsigned char *data, unsigned int len); 315 | static void sbc_set_defaults(sbc_t *sbc, unsigned long flags); 316 | static unsigned int sbc_get_codesize(sbc_t *sbc); 317 | static int sbc_init(sbc_t *sbc, unsigned long flags); 318 | static unsigned int sbc_parse(sbc_t *sbc, const void *input, unsigned int input_len); 319 | static unsigned int sbc_decode(sbc_t *sbc, const void *input, unsigned int input_len, 320 | void *output, unsigned int output_len, unsigned int *written); 321 | static inline void sbc_dec_copy_pcmbuffer(SBC_DEC_PARAMS * pDestPcmBuffer, struct sbc_frame *pSrcPcm, int len); 322 | static int sbc_unpack_frame(const unsigned char *data, struct sbc_frame *frame, 323 | unsigned int len); 324 | static void sbc_decoder_init(struct sbc_decoder_state *state, 325 | const struct sbc_frame *frame); 326 | static inline short sbc_clip16(int s); 327 | static inline void sbc_synthesize_four(struct sbc_decoder_state *state, 328 | struct sbc_frame *frame, int ch, int blk); 329 | static inline void sbc_synthesize_eight(struct sbc_decoder_state *state, 330 | struct sbc_frame *frame, int ch, int blk); 331 | static int sbc_synthesize_audio(struct sbc_decoder_state *state, 332 | struct sbc_frame *frame); 333 | static inline void sbc_calculate_bits_internal(const struct sbc_frame *frame, 334 | int (*bits)[8], int subbands); 335 | static void sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8]); 336 | 337 | static unsigned char sbc_crc8(const unsigned char *data, unsigned int len) 338 | { 339 | unsigned char crc = 0x0f; 340 | unsigned int i; 341 | unsigned char octet; 342 | 343 | for (i = 0; i < len / 8; i++) 344 | crc = crc_table[crc ^ data[i]]; 345 | 346 | octet = data[i]; 347 | for (i = 0; i < len % 8; i++) { 348 | char bit = ((octet ^ crc) & 0x80) >> 7; 349 | 350 | crc = ((crc & 0x7f) << 1) ^ (bit ? 0x1d : 0); 351 | 352 | octet = octet << 1; 353 | } 354 | 355 | return crc; 356 | } 357 | 358 | static void sbc_set_defaults(sbc_t *sbc, unsigned long flags) 359 | { 360 | sbc->frequency = SBC_sf44100; 361 | sbc->mode = SBC_STEREO; 362 | sbc->subbands = SBC_SB_8; 363 | sbc->blocks = SBC_BLK_16; 364 | sbc->bitpool = 32; 365 | #if __BIG_ENDIAN != TRUE 366 | sbc->endian = SBC_LE; 367 | #else 368 | sbc->endian = SBC_BE; 369 | #endif 370 | } 371 | 372 | unsigned int sbc_get_codesize(sbc_t *sbc) 373 | { 374 | unsigned short subbands, channels, blocks; 375 | struct sbc_priv *priv; 376 | 377 | priv = sbc->priv; 378 | if (!priv->init) { 379 | subbands = sbc->subbands ? 8 : 4; 380 | blocks = 4 + (sbc->blocks * 4); 381 | channels = sbc->mode == SBC_MONO ? 1 : 2; 382 | } else { 383 | subbands = priv->frame.subbands; 384 | blocks = priv->frame.blocks; 385 | channels = priv->frame.channels; 386 | } 387 | 388 | return subbands * blocks * channels * 2; 389 | } 390 | 391 | int sbc_init(sbc_t *sbc, unsigned long flags) 392 | { 393 | if (!sbc) 394 | return -EIO; 395 | 396 | memset(sbc, 0, sizeof(sbc_t)); 397 | 398 | sbc->priv_alloc_base = malloc(sizeof(struct sbc_priv) + SBC_ALIGN_MASK); 399 | if (!sbc->priv_alloc_base) 400 | return -ENOMEM; 401 | 402 | sbc->priv = (void *) (((unsigned int) sbc->priv_alloc_base + 403 | SBC_ALIGN_MASK) & ~((unsigned int) SBC_ALIGN_MASK)); 404 | 405 | memset(sbc->priv, 0, sizeof(struct sbc_priv)); 406 | 407 | sbc_set_defaults(sbc, flags); 408 | 409 | return 0; 410 | } 411 | 412 | unsigned int sbc_parse(sbc_t *sbc, const void *input, unsigned int input_len) 413 | { 414 | return sbc_decode(sbc, input, input_len, NULL, 0, NULL); 415 | } 416 | 417 | unsigned int sbc_decode(sbc_t *sbc, const void *input, unsigned int input_len, 418 | void *output, unsigned int output_len, unsigned int *written) 419 | { 420 | struct sbc_priv *priv; 421 | char *ptr; 422 | int i, ch, framelen, samples; 423 | 424 | if (!sbc || !input) 425 | return -EIO; 426 | 427 | priv = sbc->priv; 428 | 429 | framelen = sbc_unpack_frame(input, &priv->frame, input_len); 430 | 431 | if (!priv->init) { 432 | sbc_decoder_init(&priv->dec_state, &priv->frame); 433 | priv->init = 1; 434 | 435 | sbc->frequency = priv->frame.frequency; 436 | sbc->mode = priv->frame.mode; 437 | sbc->subbands = priv->frame.subband_mode; 438 | sbc->blocks = priv->frame.block_mode; 439 | sbc->allocation = priv->frame.allocation; 440 | sbc->bitpool = priv->frame.bitpool; 441 | 442 | priv->frame.codesize = sbc_get_codesize(sbc); 443 | priv->frame.length = framelen; 444 | } else if (priv->frame.bitpool != sbc->bitpool) { 445 | priv->frame.length = framelen; 446 | sbc->bitpool = priv->frame.bitpool; 447 | } 448 | 449 | if (!output) 450 | return framelen; 451 | 452 | if (written) 453 | *written = 0; 454 | 455 | if (framelen <= 0) 456 | return framelen; 457 | 458 | samples = sbc_synthesize_audio(&priv->dec_state, &priv->frame); 459 | 460 | ptr = output; 461 | 462 | if (output_len < (unsigned int) (samples * priv->frame.channels * 2)) 463 | samples = output_len / (priv->frame.channels * 2); 464 | 465 | for (i = 0; i < samples; i++) { 466 | for (ch = 0; ch < priv->frame.channels; ch++) { 467 | short s; 468 | s = priv->frame.pcm_sample[ch][i]; 469 | 470 | if (sbc->endian == SBC_BE) { 471 | *ptr++ = (s & 0xff00) >> 8; 472 | *ptr++ = (s & 0x00ff); 473 | } else { 474 | *ptr++ = (s & 0x00ff); 475 | *ptr++ = (s & 0xff00) >> 8; 476 | } 477 | 478 | } 479 | } 480 | 481 | if (written) 482 | *written = samples * priv->frame.channels * 2; 483 | 484 | return framelen; 485 | } 486 | 487 | static inline void sbc_dec_copy_pcmbuffer(SBC_DEC_PARAMS * pDestPcmBuffer, struct sbc_frame *pSrcPcm, int len) 488 | { 489 | int i=0; 490 | int ch=0; 491 | short *ptr=pDestPcmBuffer->as16SbBuffer; 492 | 493 | for (i = 0; i < len; i++) { 494 | for (ch = 0; ch < pSrcPcm->channels; ch++) { 495 | short s; 496 | s = pSrcPcm->pcm_sample[ch][i]; //16 bits 497 | *ptr++= s; 498 | } 499 | } 500 | 501 | // len = suband*block 502 | pDestPcmBuffer->s16PcmLength= len * pSrcPcm->channels * 2; 503 | 504 | } 505 | 506 | void SBC_Decoder(SBC_DEC_PARAMS *pstrDecParams) 507 | { 508 | int i=0, ch=0, framelen=0, samples=0; 509 | struct sbc_frame frame={0}; 510 | struct sbc_decoder_state dec_state={0}; 511 | 512 | if ( !pstrDecParams) 513 | return ; 514 | 515 | framelen = sbc_unpack_frame(pstrDecParams->pu8Packet, &frame, pstrDecParams->u16PacketLength); 516 | pstrDecParams->s16SbcFrameLength= framelen; 517 | 518 | sbc_decoder_init(&dec_state, &frame); 519 | //frame.codesize = sbc_get_codesize(&sbc); 520 | frame.length = framelen; 521 | 522 | if (framelen <= 0) 523 | return ; 524 | samples = sbc_synthesize_audio(&dec_state, &frame); 525 | sbc_dec_copy_pcmbuffer(pstrDecParams, &frame, samples); 526 | } 527 | 528 | void SBC_Decoder_Init(SBC_DEC_PARAMS *pstrDecParams) 529 | { 530 | } 531 | 532 | /* 533 | * Unpacks a SBC frame at the beginning of the stream in data, 534 | * which has at most len bytes into frame. 535 | * Returns the length in bytes of the packed frame, or a negative 536 | * value on error. The error codes are: 537 | * 538 | * -1 Data stream too short 539 | * -2 Sync byte incorrect 540 | * -3 CRC8 incorrect 541 | * -4 Bitpool value out of bounds 542 | */ 543 | static int sbc_unpack_frame(const unsigned char *data, struct sbc_frame *frame, 544 | unsigned int len) 545 | { 546 | unsigned int consumed; 547 | /* Will copy the parts of the header that are relevant to crc 548 | * calculation here */ 549 | unsigned char crc_header[11] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 550 | int crc_pos = 0; 551 | int temp; 552 | 553 | int audio_sample; 554 | int ch, sb, blk, bit; /* channel, subband, block and bit standard 555 | counters */ 556 | int bits[2][8]; /* bits distribution */ 557 | unsigned int levels[2][8]; /* levels derived from that */ 558 | 559 | if (len < 4) 560 | return -1; 561 | 562 | if (data[0] != SBC_SYNCWORD) 563 | return -2; 564 | 565 | frame->frequency = (data[1] >> 6) & 0x03; 566 | 567 | frame->block_mode = (data[1] >> 4) & 0x03; 568 | switch (frame->block_mode) { 569 | case SBC_BLK_4: 570 | frame->blocks = 4; 571 | break; 572 | case SBC_BLK_8: 573 | frame->blocks = 8; 574 | break; 575 | case SBC_BLK_12: 576 | frame->blocks = 12; 577 | break; 578 | case SBC_BLK_16: 579 | frame->blocks = 16; 580 | break; 581 | } 582 | 583 | frame->mode = (data[1] >> 2) & 0x03; 584 | switch (frame->mode) { 585 | case MONO: 586 | frame->channels = 1; 587 | break; 588 | case DUAL_CHANNEL: /* fall-through */ 589 | case STEREO: 590 | case JOINT_STEREO: 591 | frame->channels = 2; 592 | break; 593 | } 594 | 595 | frame->allocation = (data[1] >> 1) & 0x01; 596 | 597 | frame->subband_mode = (data[1] & 0x01); 598 | frame->subbands = frame->subband_mode ? 8 : 4; 599 | 600 | frame->bitpool = data[2]; 601 | 602 | if ((frame->mode == MONO || frame->mode == DUAL_CHANNEL) && 603 | frame->bitpool > 16 * frame->subbands) 604 | return -4; 605 | 606 | if ((frame->mode == STEREO || frame->mode == JOINT_STEREO) && 607 | frame->bitpool > 32 * frame->subbands) 608 | return -4; 609 | 610 | /* data[3] is crc, we're checking it later */ 611 | 612 | consumed = 32; 613 | 614 | crc_header[0] = data[1]; 615 | crc_header[1] = data[2]; 616 | crc_pos = 16; 617 | 618 | if (frame->mode == JOINT_STEREO) { 619 | if (len * 8 < consumed + frame->subbands) 620 | return -1; 621 | 622 | frame->joint = 0x00; 623 | for (sb = 0; sb < frame->subbands - 1; sb++) 624 | frame->joint |= ((data[4] >> (7 - sb)) & 0x01) << sb; 625 | 626 | if (frame->subbands == 4) 627 | crc_header[crc_pos / 8] = data[4] & 0xf0; 628 | else 629 | crc_header[crc_pos / 8] = data[4]; 630 | 631 | consumed += frame->subbands; 632 | crc_pos += frame->subbands; 633 | } 634 | 635 | if (len * 8 < consumed + (4 * frame->subbands * frame->channels)) 636 | return -1; 637 | 638 | 639 | for (ch = 0; ch < frame->channels; ch++) { 640 | for (sb = 0; sb < frame->subbands; sb++) { 641 | /* FIXME assert(consumed % 4 == 0); */ 642 | frame->scale_factor[ch][sb] = 643 | (data[consumed >> 3] >> (4 - (consumed & 0x7))) & 0x0F; 644 | 645 | crc_header[crc_pos >> 3] |= 646 | frame->scale_factor[ch][sb] << (4 - (crc_pos & 0x7)); 647 | 648 | consumed += 4; 649 | crc_pos += 4; 650 | } 651 | } 652 | 653 | if (data[3] != sbc_crc8(crc_header, crc_pos)) 654 | return -3; 655 | 656 | sbc_calculate_bits(frame, bits); 657 | 658 | for (ch = 0; ch < frame->channels; ch++) { 659 | for (sb = 0; sb < frame->subbands; sb++){ 660 | levels[ch][sb] = (1 << bits[ch][sb]) - 1; 661 | } 662 | } 663 | 664 | 665 | for (blk = 0; blk < frame->blocks; blk++) { 666 | for (ch = 0; ch < frame->channels; ch++) { 667 | for (sb = 0; sb < frame->subbands; sb++) { 668 | unsigned int shift; 669 | 670 | if (levels[ch][sb] == 0) { 671 | frame->sb_sample[blk][ch][sb] = 0; 672 | continue; 673 | } 674 | 675 | shift = frame->scale_factor[ch][sb] + 676 | 1 + SBCDEC_FIXED_EXTRA_BITS; 677 | 678 | audio_sample = 0; 679 | for (bit = 0; bit < bits[ch][sb]; bit++) { 680 | if (consumed > len * 8) 681 | return -1; 682 | 683 | if ((data[consumed >> 3] >> (7 - (consumed & 0x7))) & 0x01) 684 | audio_sample |= 1 << (bits[ch][sb] - bit - 1); 685 | 686 | consumed++; 687 | } 688 | 689 | frame->sb_sample[blk][ch][sb] = (int) 690 | (((((unsigned long long) audio_sample << 1) | 1) << shift) / 691 | levels[ch][sb]) - (1 << shift); 692 | } 693 | } 694 | } 695 | 696 | 697 | if (frame->mode == SBC_JOINT_STEREO) { 698 | for (blk = 0; blk < frame->blocks; blk++) { 699 | for (sb = 0; sb < frame->subbands; sb++) { 700 | if (frame->joint & (0x01 << sb)) { 701 | temp = frame->sb_sample[blk][0][sb] + 702 | frame->sb_sample[blk][1][sb]; 703 | frame->sb_sample[blk][1][sb] = 704 | frame->sb_sample[blk][0][sb] - 705 | frame->sb_sample[blk][1][sb]; 706 | frame->sb_sample[blk][0][sb] = temp; 707 | } 708 | } 709 | } 710 | } 711 | 712 | if ((consumed & 0x7) != 0) 713 | consumed += 8 - (consumed & 0x7); 714 | 715 | return consumed >> 3; 716 | } 717 | 718 | static void sbc_decoder_init(struct sbc_decoder_state *state, 719 | const struct sbc_frame *frame) 720 | { 721 | int i, ch; 722 | 723 | memset(state->V, 0, sizeof(state->V)); 724 | state->subbands = frame->subbands; 725 | 726 | for (ch = 0; ch < 2; ch++) 727 | for (i = 0; i < frame->subbands * 2; i++) 728 | state->offset[ch][i] = (10 * i + 10); 729 | } 730 | 731 | static inline short sbc_clip16(int s) 732 | { 733 | if (s > 0x7FFF) 734 | return 0x7FFF; 735 | else if (s < -0x8000) 736 | return -0x8000; 737 | else 738 | return s; 739 | } 740 | 741 | static inline void sbc_synthesize_four(struct sbc_decoder_state *state, 742 | struct sbc_frame *frame, int ch, int blk) 743 | { 744 | int i, k, idx; 745 | int *v = state->V[ch]; 746 | int *offset = state->offset[ch]; 747 | 748 | for (i = 0; i < 8; i++) { 749 | /* Shifting */ 750 | offset[i]--; 751 | if (offset[i] < 0) { 752 | offset[i] = 79; 753 | memcpy(v + 80, v, 9 * sizeof(*v)); 754 | } 755 | 756 | /* Distribute the new matrix value to the shifted position */ 757 | v[offset[i]] = SCALE4_STAGED1( 758 | MULA(synmatrix4[i][0], frame->sb_sample[blk][ch][0], 759 | MULA(synmatrix4[i][1], frame->sb_sample[blk][ch][1], 760 | MULA(synmatrix4[i][2], frame->sb_sample[blk][ch][2], 761 | MUL (synmatrix4[i][3], frame->sb_sample[blk][ch][3]))))); 762 | } 763 | 764 | /* Compute the samples */ 765 | for (idx = 0, i = 0; i < 4; i++, idx += 5) { 766 | k = (i + 4) & 0xf; 767 | 768 | /* Store in output, Q0 */ 769 | frame->pcm_sample[ch][blk * 4 + i] = sbc_clip16(SCALE4_STAGED1( 770 | MULA(v[offset[i] + 0], sbc_proto_4_40m0[idx + 0], 771 | MULA(v[offset[k] + 1], sbc_proto_4_40m1[idx + 0], 772 | MULA(v[offset[i] + 2], sbc_proto_4_40m0[idx + 1], 773 | MULA(v[offset[k] + 3], sbc_proto_4_40m1[idx + 1], 774 | MULA(v[offset[i] + 4], sbc_proto_4_40m0[idx + 2], 775 | MULA(v[offset[k] + 5], sbc_proto_4_40m1[idx + 2], 776 | MULA(v[offset[i] + 6], sbc_proto_4_40m0[idx + 3], 777 | MULA(v[offset[k] + 7], sbc_proto_4_40m1[idx + 3], 778 | MULA(v[offset[i] + 8], sbc_proto_4_40m0[idx + 4], 779 | MUL( v[offset[k] + 9], sbc_proto_4_40m1[idx + 4])))))))))))); 780 | } 781 | } 782 | 783 | static inline void sbc_synthesize_eight(struct sbc_decoder_state *state, 784 | struct sbc_frame *frame, int ch, int blk) 785 | { 786 | int i, j, k, idx; 787 | int *offset = state->offset[ch]; 788 | 789 | for (i = 0; i < 16; i++) { 790 | /* Shifting */ 791 | offset[i]--; 792 | if (offset[i] < 0) { 793 | offset[i] = 159; 794 | for (j = 0; j < 9; j++) 795 | state->V[ch][j + 160] = state->V[ch][j]; 796 | } 797 | 798 | /* Distribute the new matrix value to the shifted position */ 799 | state->V[ch][offset[i]] = SCALE8_STAGED1( 800 | MULA(synmatrix8[i][0], frame->sb_sample[blk][ch][0], 801 | MULA(synmatrix8[i][1], frame->sb_sample[blk][ch][1], 802 | MULA(synmatrix8[i][2], frame->sb_sample[blk][ch][2], 803 | MULA(synmatrix8[i][3], frame->sb_sample[blk][ch][3], 804 | MULA(synmatrix8[i][4], frame->sb_sample[blk][ch][4], 805 | MULA(synmatrix8[i][5], frame->sb_sample[blk][ch][5], 806 | MULA(synmatrix8[i][6], frame->sb_sample[blk][ch][6], 807 | MUL( synmatrix8[i][7], frame->sb_sample[blk][ch][7]))))))))); 808 | } 809 | 810 | /* Compute the samples */ 811 | for (idx = 0, i = 0; i < 8; i++, idx += 5) { 812 | k = (i + 8) & 0xf; 813 | 814 | /* Store in output, Q0 */ 815 | frame->pcm_sample[ch][blk * 8 + i] = sbc_clip16(SCALE8_STAGED1( 816 | MULA(state->V[ch][offset[i] + 0], sbc_proto_8_80m0[idx + 0], 817 | MULA(state->V[ch][offset[k] + 1], sbc_proto_8_80m1[idx + 0], 818 | MULA(state->V[ch][offset[i] + 2], sbc_proto_8_80m0[idx + 1], 819 | MULA(state->V[ch][offset[k] + 3], sbc_proto_8_80m1[idx + 1], 820 | MULA(state->V[ch][offset[i] + 4], sbc_proto_8_80m0[idx + 2], 821 | MULA(state->V[ch][offset[k] + 5], sbc_proto_8_80m1[idx + 2], 822 | MULA(state->V[ch][offset[i] + 6], sbc_proto_8_80m0[idx + 3], 823 | MULA(state->V[ch][offset[k] + 7], sbc_proto_8_80m1[idx + 3], 824 | MULA(state->V[ch][offset[i] + 8], sbc_proto_8_80m0[idx + 4], 825 | MUL( state->V[ch][offset[k] + 9], sbc_proto_8_80m1[idx + 4])))))))))))); 826 | } 827 | } 828 | 829 | static int sbc_synthesize_audio(struct sbc_decoder_state *state, 830 | struct sbc_frame *frame) 831 | { 832 | int ch, blk; 833 | 834 | switch (frame->subbands) { 835 | case 4: 836 | for (ch = 0; ch < frame->channels; ch++) { 837 | for (blk = 0; blk < frame->blocks; blk++) 838 | sbc_synthesize_four(state, frame, ch, blk); 839 | } 840 | return frame->blocks * 4; 841 | 842 | case 8: 843 | for (ch = 0; ch < frame->channels; ch++) { 844 | for (blk = 0; blk < frame->blocks; blk++) 845 | sbc_synthesize_eight(state, frame, ch, blk); 846 | } 847 | return frame->blocks * 8; 848 | 849 | default: 850 | return -EIO; 851 | } 852 | } 853 | 854 | /* 855 | * Code straight from the spec to calculate the bits array 856 | * Takes a pointer to the frame in question, a pointer to the bits array and 857 | * the sampling frequency (as 2 bit integer) 858 | */ 859 | static inline void sbc_calculate_bits_internal( 860 | const struct sbc_frame *frame, int (*bits)[8], int subbands) 861 | { 862 | unsigned char sf = frame->frequency; 863 | 864 | if (frame->mode == SBC_MONO|| frame->mode == SBC_DUAL) { 865 | int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice; 866 | int ch, sb; 867 | 868 | for (ch = 0; ch < frame->channels; ch++) { 869 | max_bitneed = 0; 870 | if (frame->allocation == SBC_SNR) { 871 | for (sb = 0; sb < subbands; sb++) { 872 | bitneed[ch][sb] = frame->scale_factor[ch][sb]; 873 | if (bitneed[ch][sb] > max_bitneed) 874 | max_bitneed = bitneed[ch][sb]; 875 | } 876 | } else { 877 | for (sb = 0; sb < subbands; sb++) { 878 | if (frame->scale_factor[ch][sb] == 0) 879 | bitneed[ch][sb] = -5; 880 | else { 881 | if (subbands == 4) 882 | loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb]; 883 | else 884 | loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb]; 885 | if (loudness > 0) 886 | bitneed[ch][sb] = loudness / 2; 887 | else 888 | bitneed[ch][sb] = loudness; 889 | } 890 | if (bitneed[ch][sb] > max_bitneed) 891 | max_bitneed = bitneed[ch][sb]; 892 | } 893 | } 894 | 895 | bitcount = 0; 896 | slicecount = 0; 897 | bitslice = max_bitneed + 1; 898 | do { 899 | bitslice--; 900 | bitcount += slicecount; 901 | slicecount = 0; 902 | for (sb = 0; sb < subbands; sb++) { 903 | if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16)) 904 | slicecount++; 905 | else if (bitneed[ch][sb] == bitslice + 1) 906 | slicecount += 2; 907 | } 908 | } while (bitcount + slicecount < frame->bitpool); 909 | 910 | if (bitcount + slicecount == frame->bitpool) { 911 | bitcount += slicecount; 912 | bitslice--; 913 | } 914 | 915 | for (sb = 0; sb < subbands; sb++) { 916 | if (bitneed[ch][sb] < bitslice + 2) 917 | bits[ch][sb] = 0; 918 | else { 919 | bits[ch][sb] = bitneed[ch][sb] - bitslice; 920 | if (bits[ch][sb] > 16) 921 | bits[ch][sb] = 16; 922 | } 923 | } 924 | 925 | for (sb = 0; bitcount < frame->bitpool && 926 | sb < subbands; sb++) { 927 | if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) { 928 | bits[ch][sb]++; 929 | bitcount++; 930 | } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) { 931 | bits[ch][sb] = 2; 932 | bitcount += 2; 933 | } 934 | } 935 | 936 | for (sb = 0; bitcount < frame->bitpool && 937 | sb < subbands; sb++) { 938 | if (bits[ch][sb] < 16) { 939 | bits[ch][sb]++; 940 | bitcount++; 941 | } 942 | } 943 | 944 | } 945 | 946 | } 947 | else if (frame->mode == SBC_STEREO|| frame->mode == SBC_JOINT_STEREO) { 948 | int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice; 949 | int ch, sb; 950 | 951 | max_bitneed = 0; 952 | if (frame->allocation == SBC_SNR) { 953 | for (ch = 0; ch < 2; ch++) { 954 | for (sb = 0; sb < subbands; sb++) { 955 | bitneed[ch][sb] = frame->scale_factor[ch][sb]; 956 | if (bitneed[ch][sb] > max_bitneed) 957 | max_bitneed = bitneed[ch][sb]; 958 | } 959 | } 960 | } else { 961 | for (ch = 0; ch < 2; ch++) { 962 | for (sb = 0; sb < subbands; sb++) { 963 | if (frame->scale_factor[ch][sb] == 0) 964 | bitneed[ch][sb] = -5; 965 | else { 966 | if (subbands == 4) 967 | loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb]; 968 | else 969 | loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb]; 970 | if (loudness > 0) 971 | bitneed[ch][sb] = loudness / 2; 972 | else 973 | bitneed[ch][sb] = loudness; 974 | } 975 | if (bitneed[ch][sb] > max_bitneed) 976 | max_bitneed = bitneed[ch][sb]; 977 | } 978 | } 979 | } 980 | 981 | bitcount = 0; 982 | slicecount = 0; 983 | bitslice = max_bitneed + 1; 984 | do { 985 | bitslice--; 986 | bitcount += slicecount; 987 | slicecount = 0; 988 | for (ch = 0; ch < 2; ch++) { 989 | for (sb = 0; sb < subbands; sb++) { 990 | if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16)) 991 | slicecount++; 992 | else if (bitneed[ch][sb] == bitslice + 1) 993 | slicecount += 2; 994 | } 995 | } 996 | } while (bitcount + slicecount < frame->bitpool); 997 | 998 | if (bitcount + slicecount == frame->bitpool) { 999 | bitcount += slicecount; 1000 | bitslice--; 1001 | } 1002 | 1003 | for (ch = 0; ch < 2; ch++) { 1004 | for (sb = 0; sb < subbands; sb++) { 1005 | if (bitneed[ch][sb] < bitslice + 2) { 1006 | bits[ch][sb] = 0; 1007 | } else { 1008 | bits[ch][sb] = bitneed[ch][sb] - bitslice; 1009 | if (bits[ch][sb] > 16) 1010 | bits[ch][sb] = 16; 1011 | } 1012 | } 1013 | } 1014 | 1015 | ch = 0; 1016 | sb = 0; 1017 | while (bitcount < frame->bitpool) { 1018 | if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) { 1019 | bits[ch][sb]++; 1020 | bitcount++; 1021 | } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) { 1022 | bits[ch][sb] = 2; 1023 | bitcount += 2; 1024 | } 1025 | if (ch == 1) { 1026 | ch = 0; 1027 | sb++; 1028 | if (sb >= subbands) 1029 | break; 1030 | } else 1031 | ch = 1; 1032 | } 1033 | 1034 | ch = 0; 1035 | sb = 0; 1036 | while (bitcount < frame->bitpool) { 1037 | if (bits[ch][sb] < 16) { 1038 | bits[ch][sb]++; 1039 | bitcount++; 1040 | } 1041 | if (ch == 1) { 1042 | ch = 0; 1043 | sb++; 1044 | if (sb >= subbands) 1045 | break; 1046 | } else 1047 | ch = 1; 1048 | } 1049 | 1050 | } 1051 | 1052 | } 1053 | 1054 | 1055 | static void sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8]) 1056 | { 1057 | if (frame->subbands == 4) 1058 | sbc_calculate_bits_internal(frame, bits, 4); 1059 | else 1060 | sbc_calculate_bits_internal(frame, bits, 8); 1061 | } 1062 | 1063 | 1064 | 1065 | --------------------------------------------------------------------------------