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