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