Line data Source code
1 0 : /*
2 : * Copyright 2024 - 2025 NXP
3 : *
4 : * SPDX-License-Identifier: Apache-2.0
5 : */
6 :
7 : #ifndef ZEPHYR_INCLUDE_LIB_SBC_H_
8 : #define ZEPHYR_INCLUDE_LIB_SBC_H_
9 :
10 : #include <stdint.h>
11 : #include <stdbool.h>
12 : #include <string.h>
13 : #include "sbc_encoder.h"
14 : #include "oi_codec_sbc.h"
15 : #include "oi_status.h"
16 :
17 : /** @brief SBC channel mode */
18 1 : enum __packed sbc_ch_mode {
19 : /** Mono channel */
20 : SBC_CH_MODE_MONO,
21 : /** Dual channel */
22 : SBC_CH_MODE_DUAL_CHANNEL,
23 : /** Stereo channel */
24 : SBC_CH_MODE_STEREO,
25 : /** Joint stereo channel */
26 : SBC_CH_MODE_JOINT_STEREO,
27 : };
28 :
29 : /** @brief SBC allocation method */
30 1 : enum __packed sbc_alloc_mthd {
31 : /** Loudness allocation method */
32 : SBC_ALLOC_MTHD_LOUDNESS,
33 : /** SNR allocation method */
34 : SBC_ALLOC_MTHD_SNR,
35 : };
36 :
37 : /** @brief SBC encoder */
38 1 : struct sbc_encoder {
39 : /** @cond INTERNAL_HIDDEN */
40 : /** Internally used field for encoder */
41 : SBC_ENC_PARAMS sbc_encoder_params;
42 : /** @endcond */
43 : };
44 :
45 : /** @brief Encoder initialization parameters */
46 1 : struct sbc_encoder_init_param {
47 : /** Bit rate after encoded */
48 1 : uint32_t bit_rate;
49 : /** Sample frequency */
50 1 : uint32_t samp_freq;
51 : /** Block length */
52 1 : uint8_t blk_len;
53 : /** Number of subbands */
54 1 : uint8_t subband;
55 : /** Allocation method */
56 1 : enum sbc_alloc_mthd alloc_mthd;
57 : /** Channel mode */
58 1 : enum sbc_ch_mode ch_mode;
59 : /** Number of channels */
60 1 : uint8_t ch_num;
61 : /** Minimum bitpool */
62 1 : uint8_t min_bitpool;
63 : /** Maximum bitpool */
64 1 : uint8_t max_bitpool;
65 : };
66 :
67 : /** @brief Setup encoder.
68 : *
69 : * @param encoder Handle of the encoder.
70 : * @param param The parameters to initialize encoder @ref sbc_encoder_init_param.
71 : *
72 : * @return Zero on success or (negative) error code otherwise.
73 : */
74 1 : int sbc_setup_encoder(struct sbc_encoder *encoder, struct sbc_encoder_init_param *param);
75 :
76 : /** @brief Encode a frame.
77 : *
78 : * @param encoder Handle of the encoder.
79 : * @param in_data Input PCM samples.
80 : * @param out_data Encoded SBC frame.
81 : *
82 : * @return The encoded size of the frame in bytes.
83 : */
84 1 : uint32_t sbc_encode(struct sbc_encoder *encoder, const void *in_data, void *out_data);
85 :
86 : /** @brief Return the number of PCM samples in a frame.
87 : *
88 : * @param encoder Handle of the encoder.
89 : *
90 : * @return Number of PCM samples or (negative) error code otherwise.
91 : */
92 1 : int sbc_frame_samples(struct sbc_encoder *encoder);
93 :
94 : /** @brief Return the number of PCM bytes in a frame.
95 : *
96 : * @param encoder Handle of the encoder.
97 : *
98 : * @return Number of PCM bytes or (negative) error code otherwise
99 : */
100 1 : int sbc_frame_bytes(struct sbc_encoder *encoder);
101 :
102 : /** @brief Return the encoded size of one frame.
103 : *
104 : * @param encoder Handle of the encoder.
105 : *
106 : * @return The encoded size of one frame in bytes or (negative) error code otherwise.
107 : */
108 1 : int sbc_frame_encoded_bytes(struct sbc_encoder *encoder);
109 :
110 : /** @brief SBC decoder */
111 1 : struct sbc_decoder {
112 : /** @cond INTERNAL_HIDDEN */
113 : /** Internally used fields for decoder */
114 : OI_CODEC_SBC_DECODER_CONTEXT context;
115 : uint32_t context_data[CODEC_DATA_WORDS(2, SBC_CODEC_FAST_FILTER_BUFFERS)];
116 : /** @endcond */
117 : };
118 :
119 : /** @brief Setup the SBC decoder.
120 : *
121 : * @param decoder Handle of the decoder.
122 : *
123 : * @return Zero on success or (negative) error code otherwise.
124 : */
125 1 : int sbc_setup_decoder(struct sbc_decoder *decoder);
126 :
127 : /** @brief Decode a frame.
128 : *
129 : * @param decoder Handle of the decoder.
130 : * @param in_data Input bitstream, it is increased after decode one frame
131 : * @param in_size Input data size in bytes, it is decreased after decode one frame
132 : * @param out_data Output PCM samples
133 : * @param out_size Output data size in bytes
134 : *
135 : * @return Zero on success or (negative) error code otherwise.
136 : */
137 1 : int sbc_decode(struct sbc_decoder *decoder, const void **in_data, uint32_t *in_size,
138 : void *out_data, uint32_t *out_size);
139 : #endif /* ZEPHYR_INCLUDE_LIB_SBC_H_ */
|