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