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 } OI_CODEC_SBC_FRAME_INFO; 152 153 /** Used internally. */ 154 typedef struct { 155 const OI_CHAR *codecInfo; 156 OI_CODEC_SBC_FRAME_INFO frameInfo; 157 OI_INT8 scale_factor[SBC_MAX_CHANNELS*SBC_MAX_BANDS]; 158 OI_UINT32 frameCount; 159 OI_INT32 *subdata; 160 161 SBC_BUFFER_T *filterBuffer[SBC_MAX_CHANNELS]; 162 OI_INT32 filterBufferLen; 163 OI_UINT filterBufferOffset; 164 165 union { 166 OI_UINT8 uint8[SBC_MAX_CHANNELS*SBC_MAX_BANDS]; 167 OI_UINT32 uint32[SBC_MAX_CHANNELS*SBC_MAX_BANDS/4]; 168 } bits; 169 OI_UINT8 maxBitneed; /**< Running maximum bitneed */ 170 OI_BYTE formatByte; 171 OI_UINT8 pcmStride; 172 OI_UINT8 maxChannels; 173 } OI_CODEC_SBC_COMMON_CONTEXT; 174 175 176 /* 177 * A smaller value reduces RAM usage at the expense of increased CPU usage. Values in the range 178 * 27..50 are recommended, beyond 50 there is a diminishing return on reduced CPU usage. 179 */ 180 #define SBC_CODEC_MIN_FILTER_BUFFERS 16 181 #define SBC_CODEC_FAST_FILTER_BUFFERS 27 182 183 /* Expands to the number of OI_UINT32s needed to ensure enough memory to encode 184 * or decode streams of numChannels channels, using numBuffers buffers. 185 * Example: 186 * OI_UINT32 decoderData[CODEC_DATA_WORDS(SBC_MAX_CHANNELS, SBC_DECODER_FAST_SYNTHESIS_BUFFERS)]; 187 * */ 188 #define CODEC_DATA_WORDS(numChannels, numBuffers) \ 189 ((\ 190 (sizeof(OI_INT32) * SBC_MAX_BLOCKS * numChannels * SBC_MAX_BANDS) \ 191 + (sizeof(SBC_BUFFER_T) * SBC_MAX_CHANNELS * SBC_MAX_BANDS * numBuffers) \ 192 + (sizeof (OI_UINT32) - 1) \ 193 ) / sizeof(OI_UINT32)) 194 195 /** Opaque parameter to decoding functions; maintains decoder context. */ 196 typedef struct { 197 OI_CODEC_SBC_COMMON_CONTEXT common; 198 OI_UINT8 limitFrameFormat; /* Boolean, set by OI_CODEC_SBC_DecoderLimit() */ 199 OI_UINT8 restrictSubbands; 200 OI_UINT8 enhancedEnabled; 201 OI_UINT8 bufferedBlocks; 202 } OI_CODEC_SBC_DECODER_CONTEXT; 203 204 typedef struct { 205 OI_UINT32 data[CODEC_DATA_WORDS(1, SBC_CODEC_FAST_FILTER_BUFFERS)]; 206 } OI_CODEC_SBC_CODEC_DATA_MONO; 207 208 typedef struct { 209 OI_UINT32 data[CODEC_DATA_WORDS(2, SBC_CODEC_FAST_FILTER_BUFFERS)]; 210 } OI_CODEC_SBC_CODEC_DATA_STEREO; 211 212 /** 213 @} 214 215 @addtogroup codec_lib 216 @{ 217 */ 218 219 /** 220 * This function resets the decoder. The context must be reset when 221 * changing streams, or if the following stream parameters change: 222 * number of subbands, stereo mode, or frequency. 223 * 224 * @param context Pointer to the decoder context structure to be reset. 225 * 226 * @param enhanced If true, enhanced SBC operation is enabled. If enabled, 227 * the codec will recognize the alternative syncword for 228 * decoding an enhanced SBC stream. Enhancements should not 229 * be enabled unless the stream is known to be generated 230 * by an enhanced encoder, or there is a small possibility 231 * for decoding glitches if synchronization were to be lost. 232 */ 233 OI_STATUS OI_CODEC_SBC_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT *context, 234 OI_UINT32 *decoderData, 235 OI_UINT32 decoderDataBytes, 236 OI_UINT8 maxChannels, 237 OI_UINT8 pcmStride, 238 OI_BOOL enhanced); 239 240 /** 241 * This function restricts the kind of SBC frames that the Decoder will 242 * process. Its use is optional. If used, it must be called after 243 * calling OI_CODEC_SBC_DecoderReset(). After it is called, any calls 244 * to OI_CODEC_SBC_DecodeFrame() with SBC frames that do not conform 245 * to the Subband and Enhanced SBC setting will be rejected with an 246 * OI_STATUS_INVALID_PARAMETERS return. 247 * 248 * @param context Pointer to the decoder context structure to be limited. 249 * 250 * @param enhanced If true, all frames passed to the decoder must be 251 * Enhanced SBC frames. If false, all frames must be 252 * standard SBC frames. 253 * 254 * @param subbands May be set to SBC_SUBBANDS_4 or SBC_SUBBANDS_8. All 255 * frames passed to the decoder must be encoded with 256 * the requested number of subbands. 257 * 258 */ 259 OI_STATUS OI_CODEC_SBC_DecoderLimit(OI_CODEC_SBC_DECODER_CONTEXT *context, 260 OI_BOOL enhanced, 261 OI_UINT8 subbands); 262 263 /** 264 * This function sets the decoder parameters for a raw decode where the decoder parameters are not 265 * available in the sbc data stream. OI_CODEC_SBC_DecoderReset must be called 266 * prior to calling this function. 267 * 268 * @param context Decoder context structure. This must be the context must be 269 * used each time a frame is decoded. 270 * 271 * @param enhanced Set to TRUE to enable Qualcomm proprietary 272 * quality enhancements. 273 * 274 * @param frequency One of SBC_FREQ_16000, SBC_FREQ_32000, SBC_FREQ_44100, 275 * SBC_FREQ_48000 276 * 277 * @param mode One of SBC_MONO, SBC_DUAL_CHANNEL, SBC_STEREO, 278 * SBC_JOINT_STEREO 279 * 280 * @param subbands One of SBC_SUBBANDS_4, SBC_SUBBANDS_8 281 * 282 * @param blocks One of SBC_BLOCKS_4, SBC_BLOCKS_8, SBC_BLOCKS_12, 283 * SBC_BLOCKS_16 284 * 285 * @param alloc One of SBC_LOUDNESS, SBC_SNR 286 * 287 * @param maxBitpool The maximum bitpool size for this context 288 */ 289 OI_STATUS OI_CODEC_SBC_DecoderConfigureRaw(OI_CODEC_SBC_DECODER_CONTEXT *context, 290 OI_BOOL enhanced, 291 OI_UINT8 frequency, 292 OI_UINT8 mode, 293 OI_UINT8 subbands, 294 OI_UINT8 blocks, 295 OI_UINT8 alloc, 296 OI_UINT8 maxBitpool); 297 298 /** 299 * Decode one SBC frame. The frame has no header bytes. The context must have been previously 300 * initialized by calling OI_CODEC_SBC_DecoderConfigureRaw(). 301 * 302 * @param context Pointer to a decoder context structure. The same context 303 * must be used each time when decoding from the same stream. 304 * 305 * @param bitpool The actual bitpool size for this frame. Must be <= the maxbitpool specified 306 * in the call to OI_CODEC_SBC_DecoderConfigureRaw(), 307 * 308 * @param frameData Address of a pointer to the SBC data to decode. This 309 * value will be updated to point to the next frame after 310 * successful decoding. 311 * 312 * @param frameBytes Pointer to a UINT32 containing the number of available 313 * bytes of frame data. This value will be updated to reflect 314 * the number of bytes remaining after a decoding operation. 315 * 316 * @param pcmData Address of an array of OI_INT16 pairs, which will be 317 * populated with the decoded audio data. This address 318 * is not updated. 319 * 320 * @param pcmBytes Pointer to a UINT32 in/out parameter. On input, it 321 * should contain the number of bytes available for pcm 322 * data. On output, it will contain the number of bytes 323 * written. Note that this differs from the semantics of 324 * frameBytes. 325 */ 326 OI_STATUS OI_CODEC_SBC_DecodeRaw(OI_CODEC_SBC_DECODER_CONTEXT *context, 327 OI_UINT8 bitpool, 328 const OI_BYTE **frameData, 329 OI_UINT32 *frameBytes, 330 OI_INT16 *pcmData, 331 OI_UINT32 *pcmBytes); 332 333 /** 334 * Decode one SBC frame. 335 * 336 * @param context Pointer to a decoder context structure. The same context 337 * must be used each time when decoding from the same stream. 338 * 339 * @param frameData Address of a pointer to the SBC data to decode. This 340 * value will be updated to point to the next frame after 341 * successful decoding. 342 * 343 * @param frameBytes Pointer to a UINT32 containing the number of available 344 * bytes of frame data. This value will be updated to reflect 345 * the number of bytes remaining after a decoding operation. 346 * 347 * @param pcmData Address of an array of OI_INT16 pairs, which will be 348 * populated with the decoded audio data. This address 349 * is not updated. 350 * 351 * @param pcmBytes Pointer to a UINT32 in/out parameter. On input, it 352 * should contain the number of bytes available for pcm 353 * data. On output, it will contain the number of bytes 354 * written. Note that this differs from the semantics of 355 * frameBytes. 356 */ 357 OI_STATUS OI_CODEC_SBC_DecodeFrame(OI_CODEC_SBC_DECODER_CONTEXT *context, 358 const OI_BYTE **frameData, 359 OI_UINT32 *frameBytes, 360 OI_INT16 *pcmData, 361 OI_UINT32 *pcmBytes); 362 363 /** 364 * Calculate the number of SBC frames but don't decode. CRC's are not checked, 365 * but the Sync word is found prior to count calculation. 366 * 367 * @param frameData Pointer to the SBC data. 368 * 369 * @param frameBytes Number of bytes avaiable in the frameData buffer 370 * 371 */ 372 OI_UINT8 OI_CODEC_SBC_FrameCount(OI_BYTE *frameData, 373 OI_UINT32 frameBytes); 374 375 /** 376 * Analyze an SBC frame but don't do the decode. 377 * 378 * @param context Pointer to a decoder context structure. The same context 379 * must be used each time when decoding from the same stream. 380 * 381 * @param frameData Address of a pointer to the SBC data to decode. This 382 * value will be updated to point to the next frame after 383 * successful decoding. 384 * 385 * @param frameBytes Pointer to a UINT32 containing the number of available 386 * bytes of frame data. This value will be updated to reflect 387 * the number of bytes remaining after a decoding operation. 388 * 389 */ 390 OI_STATUS OI_CODEC_SBC_SkipFrame(OI_CODEC_SBC_DECODER_CONTEXT *context, 391 const OI_BYTE **frameData, 392 OI_UINT32 *frameBytes); 393 394 /* Common functions */ 395 396 /** 397 Calculate the frame length. 398 399 @param frame The frame whose length to calculate 400 401 @return the length of an individual encoded frame in 402 bytes 403 */ 404 OI_UINT16 OI_CODEC_SBC_CalculateFramelen(OI_CODEC_SBC_FRAME_INFO *frame); 405 406 407 /** 408 * Calculate the maximum bitpool size that fits within a given frame length. 409 * 410 * @param frame The frame to calculate the bitpool size for 411 * @param frameLen The frame length to fit the bitpool to 412 * 413 * @return the maximum bitpool that will fit in the specified frame length 414 */ 415 OI_UINT16 OI_CODEC_SBC_CalculateBitpool(OI_CODEC_SBC_FRAME_INFO *frame, 416 OI_UINT16 frameLen); 417 418 /** 419 Calculate the bit rate. 420 421 @param frame The frame whose bit rate to calculate 422 423 @return the approximate bit rate in bits per second, 424 assuming that stream parameters are constant 425 */ 426 OI_UINT32 OI_CODEC_SBC_CalculateBitrate(OI_CODEC_SBC_FRAME_INFO *frame); 427 428 /** 429 Calculate decoded audio data length for one frame. 430 431 @param frame The frame whose audio data length to calculate 432 433 @return length of decoded audio data for a 434 single frame, in bytes 435 */ 436 OI_UINT16 OI_CODEC_SBC_CalculatePcmBytes(OI_CODEC_SBC_COMMON_CONTEXT *common); 437 438 /** 439 * Get the codec version text. 440 * 441 * @return pointer to text string containing codec version text 442 * 443 */ 444 OI_CHAR *OI_CODEC_Version(void); 445 446 447 /** 448 @} 449 450 @addtogroup codec_internal 451 @{ 452 */ 453 454 extern const OI_CHAR* const OI_CODEC_SBC_FreqText[]; 455 extern const OI_CHAR* const OI_CODEC_SBC_ModeText[]; 456 extern const OI_CHAR* const OI_CODEC_SBC_SubbandsText[]; 457 extern const OI_CHAR* const OI_CODEC_SBC_BlocksText[]; 458 extern const OI_CHAR* const OI_CODEC_SBC_AllocText[]; 459 460 /** 461 @} 462 463 @addtogroup codec_lib 464 @{ 465 */ 466 467 #ifdef OI_DEBUG 468 void OI_CODEC_SBC_DumpConfig(OI_CODEC_SBC_FRAME_INFO *frameInfo); 469 #else 470 #define OI_CODEC_SBC_DumpConfig(f) 471 #endif 472 473 /** 474 @} 475 */ 476 477 #ifdef __cplusplus 478 } 479 #endif 480 481 482 #endif /* _OI_CODEC_SBC_CORE_H */ 483 484 485