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