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