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