1 /****************************************************************************** 2 * 3 * Copyright (C) 2014 The Android Open Source Project 4 * Copyright 2003 - 2004 Open Interface North America, Inc. All rights reserved. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at: 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * 18 ******************************************************************************/ 19 20 /********************************************************************************** 21 $Revision: #1 $ 22 ***********************************************************************************/ 23 24 #ifndef _OI_CODEC_SBC_CORE_H 25 #define _OI_CODEC_SBC_CORE_H 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 /** 32 @file 33 Declarations of codec functions, data types, and macros. 34 35 @ingroup codec_lib 36 */ 37 38 /** 39 @addtogroup codec_lib 40 @{ 41 */ 42 43 /* Non-BM3 users of of the codec must include oi_codec_sbc_bm3defs.h prior to 44 * including this file, or else these includes will fail because the BM3 SDK is 45 * not in the include path */ 46 #ifndef _OI_CODEC_SBC_BM3DEFS_H 47 #include "oi_stddefs.h" 48 #include "oi_status.h" 49 #endif 50 51 #include <stdint.h> 52 53 #define SBC_MAX_CHANNELS 2 54 #define SBC_MAX_BANDS 8 55 #define SBC_MAX_BLOCKS 16 56 #define SBC_MIN_BITPOOL 2 /**< Minimum size of the bit allocation pool used to encode the stream */ 57 #define SBC_MAX_BITPOOL 250 /**< Maximum size of the bit allocation pool used to encode the stream */ 58 #define SBC_MAX_ONE_CHANNEL_BPS 320000 59 #define SBC_MAX_TWO_CHANNEL_BPS 512000 60 61 62 #define SBC_WBS_BITRATE 62000 63 #define SBC_WBS_BITPOOL 27 64 #define SBC_WBS_NROF_BLOCKS 16 65 #define SBC_WBS_FRAME_LEN 62 66 #define SBC_WBS_SAMPLES_PER_FRAME 128 67 68 69 #define SBC_HEADER_LEN 4 70 #define SBC_MAX_FRAME_LEN (SBC_HEADER_LEN + \ 71 ((SBC_MAX_BANDS * SBC_MAX_CHANNELS / 2) + \ 72 (SBC_MAX_BANDS + SBC_MAX_BLOCKS * SBC_MAX_BITPOOL + 7)/8)) 73 #define SBC_MAX_SAMPLES_PER_FRAME (SBC_MAX_BANDS * SBC_MAX_BLOCKS) 74 75 #define SBC_MAX_SCALEFACTOR_BYTES ((4*(SBC_MAX_CHANNELS * SBC_MAX_BANDS) + 7)/8) 76 77 #define OI_SBC_SYNCWORD 0x9c 78 #define OI_SBC_ENHANCED_SYNCWORD 0x9d 79 80 /**@name Sampling frequencies */ 81 /**@{*/ 82 #define SBC_FREQ_16000 0 /**< The sampling frequency is 16 kHz. One possible value for the @a frequency parameter of OI_CODEC_SBC_EncoderConfigure() */ 83 #define SBC_FREQ_32000 1 /**< The sampling frequency is 32 kHz. One possible value for the @a frequency parameter of OI_CODEC_SBC_EncoderConfigure() */ 84 #define SBC_FREQ_44100 2 /**< The sampling frequency is 44.1 kHz. One possible value for the @a frequency parameter of OI_CODEC_SBC_EncoderConfigure() */ 85 #define SBC_FREQ_48000 3 /**< The sampling frequency is 48 kHz. One possible value for the @a frequency parameter of OI_CODEC_SBC_EncoderConfigure() */ 86 /**@}*/ 87 88 /**@name Channel modes */ 89 /**@{*/ 90 #define SBC_MONO 0 /**< The mode of the encoded channel is mono. One possible value for the @a mode parameter of OI_CODEC_SBC_EncoderConfigure() */ 91 #define SBC_DUAL_CHANNEL 1 /**< The mode of the encoded channel is dual-channel. One possible value for the @a mode parameter of OI_CODEC_SBC_EncoderConfigure() */ 92 #define SBC_STEREO 2 /**< The mode of the encoded channel is stereo. One possible value for the @a mode parameter of OI_CODEC_SBC_EncoderConfigure() */ 93 #define SBC_JOINT_STEREO 3 /**< The mode of the encoded channel is joint stereo. One possible value for the @a mode parameter of OI_CODEC_SBC_EncoderConfigure() */ 94 /**@}*/ 95 96 /**@name Subbands */ 97 /**@{*/ 98 #define SBC_SUBBANDS_4 0 /**< The encoded stream has 4 subbands. One possible value for the @a subbands parameter of OI_CODEC_SBC_EncoderConfigure()*/ 99 #define SBC_SUBBANDS_8 1 /**< The encoded stream has 8 subbands. One possible value for the @a subbands parameter of OI_CODEC_SBC_EncoderConfigure() */ 100 /**@}*/ 101 102 /**@name Block lengths */ 103 /**@{*/ 104 #define SBC_BLOCKS_4 0 /**< A block size of 4 blocks was used to encode the stream. One possible value for the @a blocks parameter of OI_CODEC_SBC_EncoderConfigure() */ 105 #define SBC_BLOCKS_8 1 /**< A block size of 8 blocks was used to encode the stream is. One possible value for the @a blocks parameter of OI_CODEC_SBC_EncoderConfigure() */ 106 #define SBC_BLOCKS_12 2 /**< A block size of 12 blocks was used to encode the stream. One possible value for the @a blocks parameter of OI_CODEC_SBC_EncoderConfigure() */ 107 #define SBC_BLOCKS_16 3 /**< A block size of 16 blocks was used to encode the stream. One possible value for the @a blocks parameter of OI_CODEC_SBC_EncoderConfigure() */ 108 /**@}*/ 109 110 /**@name Bit allocation methods */ 111 /**@{*/ 112 #define SBC_LOUDNESS 0 /**< The bit allocation method. One possible value for the @a loudness parameter of OI_CODEC_SBC_EncoderConfigure() */ 113 #define SBC_SNR 1 /**< The bit allocation method. One possible value for the @a loudness parameter of OI_CODEC_SBC_EncoderConfigure() */ 114 /**@}*/ 115 116 /** 117 @} 118 119 @addtogroup codec_internal 120 @{ 121 */ 122 123 typedef OI_INT16 SBC_BUFFER_T; 124 125 126 /** Used internally. */ 127 typedef struct { 128 OI_UINT16 frequency; /**< The sampling frequency. Input parameter. */ 129 OI_UINT8 freqIndex; 130 131 OI_UINT8 nrof_blocks; /**< The block size used to encode the stream. Input parameter. */ 132 OI_UINT8 blocks; 133 134 135 OI_UINT8 nrof_subbands; /**< The number of subbands of the encoded stream. Input parameter. */ 136 OI_UINT8 subbands; 137 138 OI_UINT8 mode; /**< The mode of the encoded channel. Input parameter. */ 139 OI_UINT8 nrof_channels; /**< The number of channels of the encoded stream. */ 140 141 OI_UINT8 alloc; /**< The bit allocation method. Input parameter. */ 142 OI_UINT8 bitpool; /**< Size of the bit allocation pool used to encode the stream. Input parameter. */ 143 OI_UINT8 crc; /**< Parity check byte used for error detection. */ 144 OI_UINT8 join; /**< Whether joint stereo has been used. */ 145 OI_UINT8 enhanced; 146 OI_UINT8 min_bitpool; /**< This value is only used when encoding. SBC_MAX_BITPOOL if variable 147 bitpools are disallowed, otherwise the minimum bitpool size that will 148 be used by the bit allocator. */ 149 150 OI_UINT8 cachedInfo; /**< Information about the previous frame */ 151 152 /* BK4BTSTACK_CHANGE START */ 153 OI_UINT8 reserved_for_future_use[2]; 154 OI_UINT8 mSBCEnabled; // default 0 155 /* BK4BTSTACK_CHANGE END */ 156 } OI_CODEC_SBC_FRAME_INFO; 157 158 /** Used internally. */ 159 typedef struct { 160 const OI_CHAR *codecInfo; 161 OI_CODEC_SBC_FRAME_INFO frameInfo; 162 OI_INT8 scale_factor[SBC_MAX_CHANNELS*SBC_MAX_BANDS]; 163 OI_UINT32 frameCount; 164 OI_INT32 *subdata; 165 166 SBC_BUFFER_T *filterBuffer[SBC_MAX_CHANNELS]; 167 OI_INT32 filterBufferLen; 168 OI_UINT filterBufferOffset; 169 170 union { 171 OI_UINT8 uint8[SBC_MAX_CHANNELS*SBC_MAX_BANDS]; 172 OI_UINT32 uint32[SBC_MAX_CHANNELS*SBC_MAX_BANDS/4]; 173 } bits; 174 OI_UINT8 maxBitneed; /**< Running maximum bitneed */ 175 OI_BYTE formatByte; 176 OI_UINT8 pcmStride; 177 OI_UINT8 maxChannels; 178 } OI_CODEC_SBC_COMMON_CONTEXT; 179 180 181 /* 182 * A smaller value reduces RAM usage at the expense of increased CPU usage. Values in the range 183 * 27..50 are recommended, beyond 50 there is a diminishing return on reduced CPU usage. 184 */ 185 #define SBC_CODEC_MIN_FILTER_BUFFERS 16 186 #define SBC_CODEC_FAST_FILTER_BUFFERS 27 187 188 /* Expands to the number of OI_UINT32s needed to ensure enough memory to encode 189 * or decode streams of numChannels channels, using numBuffers buffers. 190 * Example: 191 * OI_UINT32 decoderData[CODEC_DATA_WORDS(SBC_MAX_CHANNELS, SBC_DECODER_FAST_SYNTHESIS_BUFFERS)]; 192 * */ 193 #define CODEC_DATA_WORDS(numChannels, numBuffers) \ 194 ((\ 195 (sizeof(OI_INT32) * SBC_MAX_BLOCKS * numChannels * SBC_MAX_BANDS) \ 196 + (sizeof(SBC_BUFFER_T) * SBC_MAX_CHANNELS * SBC_MAX_BANDS * numBuffers) \ 197 + (sizeof (OI_UINT32) - 1) \ 198 ) / sizeof(OI_UINT32)) 199 200 /** Opaque parameter to decoding functions; maintains decoder context. */ 201 typedef struct { 202 OI_CODEC_SBC_COMMON_CONTEXT common; 203 OI_UINT8 limitFrameFormat; /* Boolean, set by OI_CODEC_SBC_DecoderLimit() */ 204 OI_UINT8 restrictSubbands; 205 OI_UINT8 enhancedEnabled; 206 OI_UINT8 bufferedBlocks; 207 } OI_CODEC_SBC_DECODER_CONTEXT; 208 209 typedef struct { 210 OI_UINT32 data[CODEC_DATA_WORDS(1, SBC_CODEC_FAST_FILTER_BUFFERS)]; 211 } OI_CODEC_SBC_CODEC_DATA_MONO; 212 213 typedef struct { 214 OI_UINT32 data[CODEC_DATA_WORDS(2, SBC_CODEC_FAST_FILTER_BUFFERS)]; 215 } OI_CODEC_SBC_CODEC_DATA_STEREO; 216 217 /** 218 @} 219 220 @addtogroup codec_lib 221 @{ 222 */ 223 224 /** 225 * This function resets the decoder. The context must be reset when 226 * changing streams, or if the following stream parameters change: 227 * number of subbands, stereo mode, or frequency. 228 * 229 * @param context Pointer to the decoder context structure to be reset. 230 * 231 * @param enhanced If true, enhanced SBC operation is enabled. If enabled, 232 * the codec will recognize the alternative syncword for 233 * decoding an enhanced SBC stream. Enhancements should not 234 * be enabled unless the stream is known to be generated 235 * by an enhanced encoder, or there is a small possibility 236 * for decoding glitches if synchronization were to be lost. 237 */ 238 OI_STATUS OI_CODEC_SBC_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT *context, 239 OI_UINT32 *decoderData, 240 OI_UINT32 decoderDataBytes, 241 OI_UINT8 maxChannels, 242 OI_UINT8 pcmStride, 243 OI_BOOL enhanced); 244 245 /* BK4BTSTACK_CHANGE START */ 246 OI_STATUS OI_CODEC_mSBC_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT *context, 247 OI_UINT32 *decoderData, 248 OI_UINT32 decoderDataBytes); 249 /* BK4BTSTACK_CHANGE END */ 250 251 /** 252 * This function restricts the kind of SBC frames that the Decoder will 253 * process. Its use is optional. If used, it must be called after 254 * calling OI_CODEC_SBC_DecoderReset(). After it is called, any calls 255 * to OI_CODEC_SBC_DecodeFrame() with SBC frames that do not conform 256 * to the Subband and Enhanced SBC setting will be rejected with an 257 * OI_STATUS_INVALID_PARAMETERS return. 258 * 259 * @param context Pointer to the decoder context structure to be limited. 260 * 261 * @param enhanced If true, all frames passed to the decoder must be 262 * Enhanced SBC frames. If false, all frames must be 263 * standard SBC frames. 264 * 265 * @param subbands May be set to SBC_SUBBANDS_4 or SBC_SUBBANDS_8. All 266 * frames passed to the decoder must be encoded with 267 * the requested number of subbands. 268 * 269 */ 270 OI_STATUS OI_CODEC_SBC_DecoderLimit(OI_CODEC_SBC_DECODER_CONTEXT *context, 271 OI_BOOL enhanced, 272 OI_UINT8 subbands); 273 274 /** 275 * This function sets the decoder parameters for a raw decode where the decoder parameters are not 276 * available in the sbc data stream. OI_CODEC_SBC_DecoderReset must be called 277 * prior to calling this function. 278 * 279 * @param context Decoder context structure. This must be the context must be 280 * used each time a frame is decoded. 281 * 282 * @param enhanced Set to TRUE to enable Qualcomm proprietary 283 * quality enhancements. 284 * 285 * @param frequency One of SBC_FREQ_16000, SBC_FREQ_32000, SBC_FREQ_44100, 286 * SBC_FREQ_48000 287 * 288 * @param mode One of SBC_MONO, SBC_DUAL_CHANNEL, SBC_STEREO, 289 * SBC_JOINT_STEREO 290 * 291 * @param subbands One of SBC_SUBBANDS_4, SBC_SUBBANDS_8 292 * 293 * @param blocks One of SBC_BLOCKS_4, SBC_BLOCKS_8, SBC_BLOCKS_12, 294 * SBC_BLOCKS_16 295 * 296 * @param alloc One of SBC_LOUDNESS, SBC_SNR 297 * 298 * @param maxBitpool The maximum bitpool size for this context 299 */ 300 OI_STATUS OI_CODEC_SBC_DecoderConfigureRaw(OI_CODEC_SBC_DECODER_CONTEXT *context, 301 OI_BOOL enhanced, 302 OI_UINT8 frequency, 303 OI_UINT8 mode, 304 OI_UINT8 subbands, 305 OI_UINT8 blocks, 306 OI_UINT8 alloc, 307 OI_UINT8 maxBitpool); 308 309 310 /** 311 * Decode one SBC frame. The frame has no header bytes. The context must have been previously 312 * initialized by calling OI_CODEC_SBC_DecoderConfigureRaw(). 313 * 314 * @param context Pointer to a decoder context structure. The same context 315 * must be used each time when decoding from the same stream. 316 * 317 * @param bitpool The actual bitpool size for this frame. Must be <= the maxbitpool specified 318 * in the call to OI_CODEC_SBC_DecoderConfigureRaw(), 319 * 320 * @param frameData Address of a pointer to the SBC data to decode. This 321 * value will be updated to point to the next frame after 322 * successful decoding. 323 * 324 * @param frameBytes Pointer to a UINT32 containing the number of available 325 * bytes of frame data. This value will be updated to reflect 326 * the number of bytes remaining after a decoding operation. 327 * 328 * @param pcmData Address of an array of OI_INT16 pairs, which will be 329 * populated with the decoded audio data. This address 330 * is not updated. 331 * 332 * @param pcmBytes Pointer to a UINT32 in/out parameter. On input, it 333 * should contain the number of bytes available for pcm 334 * data. On output, it will contain the number of bytes 335 * written. Note that this differs from the semantics of 336 * frameBytes. 337 */ 338 OI_STATUS OI_CODEC_SBC_DecodeRaw(OI_CODEC_SBC_DECODER_CONTEXT *context, 339 OI_UINT8 bitpool, 340 const OI_BYTE **frameData, 341 OI_UINT32 *frameBytes, 342 OI_INT16 *pcmData, 343 OI_UINT32 *pcmBytes); 344 345 /** 346 * Decode one SBC frame. 347 * 348 * @param context Pointer to a decoder context structure. The same context 349 * must be used each time when decoding from the same stream. 350 * 351 * @param frameData Address of a pointer to the SBC data to decode. This 352 * value will be updated to point to the next frame after 353 * successful decoding. 354 * 355 * @param frameBytes Pointer to a UINT32 containing the number of available 356 * bytes of frame data. This value will be updated to reflect 357 * the number of bytes remaining after a decoding operation. 358 * 359 * @param pcmData Address of an array of OI_INT16 pairs, which will be 360 * populated with the decoded audio data. This address 361 * is not updated. 362 * 363 * @param pcmBytes Pointer to a UINT32 in/out parameter. On input, it 364 * should contain the number of bytes available for pcm 365 * data. On output, it will contain the number of bytes 366 * written. Note that this differs from the semantics of 367 * frameBytes. 368 */ 369 OI_STATUS OI_CODEC_SBC_DecodeFrame(OI_CODEC_SBC_DECODER_CONTEXT *context, 370 const OI_BYTE **frameData, 371 OI_UINT32 *frameBytes, 372 OI_INT16 *pcmData, 373 OI_UINT32 *pcmBytes); 374 375 /** 376 * Calculate the number of SBC frames but don't decode. CRC's are not checked, 377 * but the Sync word is found prior to count calculation. 378 * 379 * @param frameData Pointer to the SBC data. 380 * 381 * @param frameBytes Number of bytes avaiable in the frameData buffer 382 * 383 */ 384 OI_UINT8 OI_CODEC_SBC_FrameCount(OI_BYTE *frameData, 385 OI_UINT32 frameBytes); 386 387 /** 388 * Analyze an SBC frame but don't do the decode. 389 * 390 * @param context Pointer to a decoder context structure. The same context 391 * must be used each time when decoding from the same stream. 392 * 393 * @param frameData Address of a pointer to the SBC data to decode. This 394 * value will be updated to point to the next frame after 395 * successful decoding. 396 * 397 * @param frameBytes Pointer to a UINT32 containing the number of available 398 * bytes of frame data. This value will be updated to reflect 399 * the number of bytes remaining after a decoding operation. 400 * 401 */ 402 OI_STATUS OI_CODEC_SBC_SkipFrame(OI_CODEC_SBC_DECODER_CONTEXT *context, 403 const OI_BYTE **frameData, 404 OI_UINT32 *frameBytes); 405 406 /* Common functions */ 407 408 /** 409 Calculate the frame length. 410 411 @param frame The frame whose length to calculate 412 413 @return the length of an individual encoded frame in 414 bytes 415 */ 416 OI_UINT16 OI_CODEC_SBC_CalculateFramelen(OI_CODEC_SBC_FRAME_INFO *frame); 417 418 419 /** 420 * Calculate the maximum bitpool size that fits within a given frame length. 421 * 422 * @param frame The frame to calculate the bitpool size for 423 * @param frameLen The frame length to fit the bitpool to 424 * 425 * @return the maximum bitpool that will fit in the specified frame length 426 */ 427 OI_UINT16 OI_CODEC_SBC_CalculateBitpool(OI_CODEC_SBC_FRAME_INFO *frame, 428 OI_UINT16 frameLen); 429 430 /** 431 Calculate the bit rate. 432 433 @param frame The frame whose bit rate to calculate 434 435 @return the approximate bit rate in bits per second, 436 assuming that stream parameters are constant 437 */ 438 OI_UINT32 OI_CODEC_SBC_CalculateBitrate(OI_CODEC_SBC_FRAME_INFO *frame); 439 440 /** 441 Calculate decoded audio data length for one frame. 442 443 @param frame The frame whose audio data length to calculate 444 445 @return length of decoded audio data for a 446 single frame, in bytes 447 */ 448 OI_UINT16 OI_CODEC_SBC_CalculatePcmBytes(OI_CODEC_SBC_COMMON_CONTEXT *common); 449 450 /** 451 * Get the codec version text. 452 * 453 * @return pointer to text string containing codec version text 454 * 455 */ 456 OI_CHAR *OI_CODEC_Version(void); 457 458 459 /** 460 @} 461 462 @addtogroup codec_internal 463 @{ 464 */ 465 466 extern const OI_CHAR* const OI_CODEC_SBC_FreqText[]; 467 extern const OI_CHAR* const OI_CODEC_SBC_ModeText[]; 468 extern const OI_CHAR* const OI_CODEC_SBC_SubbandsText[]; 469 extern const OI_CHAR* const OI_CODEC_SBC_BlocksText[]; 470 extern const OI_CHAR* const OI_CODEC_SBC_AllocText[]; 471 472 /** 473 @} 474 475 @addtogroup codec_lib 476 @{ 477 */ 478 479 #ifdef OI_DEBUG 480 void OI_CODEC_SBC_DumpConfig(OI_CODEC_SBC_FRAME_INFO *frameInfo); 481 #else 482 #define OI_CODEC_SBC_DumpConfig(f) 483 #endif 484 485 /** 486 @} 487 */ 488 489 #ifdef __cplusplus 490 } 491 #endif 492 493 494 #endif /* _OI_CODEC_SBC_CORE_H */ 495 496 497