3 * Bluetooth low-complexity, subband codec (SBC) library
5 * Copyright (C) 2004-2009 Marcel Holtmann <marcel@holtmann.org>
6 * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch>
7 * Copyright (C) 2005-2006 Brad Midgley <bmidgley@xmission.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 /* A2DP specification: Appendix B, page 69 */
27 static const int sbc_offset4[4][4] = {
34 /* A2DP specification: Appendix B, page 69 */
35 static const int sbc_offset8[4][8] = {
36 { -2, 0, 0, 0, 0, 0, 0, 1 },
37 { -3, 0, 0, 0, 0, 0, 1, 2 },
38 { -4, 0, 0, 0, 0, 0, 1, 2 },
39 { -4, 0, 0, 0, 0, 0, 1, 2 }
43 #define SS4(val) ASR(val, SCALE_SPROTO4_TBL)
44 #define SS8(val) ASR(val, SCALE_SPROTO8_TBL)
45 #define SN4(val) ASR(val, SCALE_NPROTO4_TBL)
46 #define SN8(val) ASR(val, SCALE_NPROTO8_TBL)
48 static const int32_t sbc_proto_4_40m0[] = {
49 SS4(0x00000000), SS4(0xffa6982f), SS4(0xfba93848), SS4(0x0456c7b8),
50 SS4(0x005967d1), SS4(0xfffb9ac7), SS4(0xff589157), SS4(0xf9c2a8d8),
51 SS4(0x027c1434), SS4(0x0019118b), SS4(0xfff3c74c), SS4(0xff137330),
52 SS4(0xf81b8d70), SS4(0x00ec1b8b), SS4(0xfff0b71a), SS4(0xffe99b00),
53 SS4(0xfef84470), SS4(0xf6fb4370), SS4(0xffcdc351), SS4(0xffe01dc7)
56 static const int32_t sbc_proto_4_40m1[] = {
57 SS4(0xffe090ce), SS4(0xff2c0475), SS4(0xf694f800), SS4(0xff2c0475),
58 SS4(0xffe090ce), SS4(0xffe01dc7), SS4(0xffcdc351), SS4(0xf6fb4370),
59 SS4(0xfef84470), SS4(0xffe99b00), SS4(0xfff0b71a), SS4(0x00ec1b8b),
60 SS4(0xf81b8d70), SS4(0xff137330), SS4(0xfff3c74c), SS4(0x0019118b),
61 SS4(0x027c1434), SS4(0xf9c2a8d8), SS4(0xff589157), SS4(0xfffb9ac7)
64 static const int32_t sbc_proto_8_80m0[] = {
65 SS8(0x00000000), SS8(0xfe8d1970), SS8(0xee979f00), SS8(0x11686100),
66 SS8(0x0172e690), SS8(0xfff5bd1a), SS8(0xfdf1c8d4), SS8(0xeac182c0),
67 SS8(0x0d9daee0), SS8(0x00e530da), SS8(0xffe9811d), SS8(0xfd52986c),
68 SS8(0xe7054ca0), SS8(0x0a00d410), SS8(0x006c1de4), SS8(0xffdba705),
69 SS8(0xfcbc98e8), SS8(0xe3889d20), SS8(0x06af2308), SS8(0x000bb7db),
70 SS8(0xffca00ed), SS8(0xfc3fbb68), SS8(0xe071bc00), SS8(0x03bf7948),
71 SS8(0xffc4e05c), SS8(0xffb54b3b), SS8(0xfbedadc0), SS8(0xdde26200),
72 SS8(0x0142291c), SS8(0xff960e94), SS8(0xff9f3e17), SS8(0xfbd8f358),
73 SS8(0xdbf79400), SS8(0xff405e01), SS8(0xff7d4914), SS8(0xff8b1a31),
74 SS8(0xfc1417b8), SS8(0xdac7bb40), SS8(0xfdbb828c), SS8(0xff762170)
77 static const int32_t sbc_proto_8_80m1[] = {
78 SS8(0xff7c272c), SS8(0xfcb02620), SS8(0xda612700), SS8(0xfcb02620),
79 SS8(0xff7c272c), SS8(0xff762170), SS8(0xfdbb828c), SS8(0xdac7bb40),
80 SS8(0xfc1417b8), SS8(0xff8b1a31), SS8(0xff7d4914), SS8(0xff405e01),
81 SS8(0xdbf79400), SS8(0xfbd8f358), SS8(0xff9f3e17), SS8(0xff960e94),
82 SS8(0x0142291c), SS8(0xdde26200), SS8(0xfbedadc0), SS8(0xffb54b3b),
83 SS8(0xffc4e05c), SS8(0x03bf7948), SS8(0xe071bc00), SS8(0xfc3fbb68),
84 SS8(0xffca00ed), SS8(0x000bb7db), SS8(0x06af2308), SS8(0xe3889d20),
85 SS8(0xfcbc98e8), SS8(0xffdba705), SS8(0x006c1de4), SS8(0x0a00d410),
86 SS8(0xe7054ca0), SS8(0xfd52986c), SS8(0xffe9811d), SS8(0x00e530da),
87 SS8(0x0d9daee0), SS8(0xeac182c0), SS8(0xfdf1c8d4), SS8(0xfff5bd1a)
90 static const int32_t synmatrix4[8][4] = {
91 { SN4(0x05a82798), SN4(0xfa57d868), SN4(0xfa57d868), SN4(0x05a82798) },
92 { SN4(0x030fbc54), SN4(0xf89be510), SN4(0x07641af0), SN4(0xfcf043ac) },
93 { SN4(0x00000000), SN4(0x00000000), SN4(0x00000000), SN4(0x00000000) },
94 { SN4(0xfcf043ac), SN4(0x07641af0), SN4(0xf89be510), SN4(0x030fbc54) },
95 { SN4(0xfa57d868), SN4(0x05a82798), SN4(0x05a82798), SN4(0xfa57d868) },
96 { SN4(0xf89be510), SN4(0xfcf043ac), SN4(0x030fbc54), SN4(0x07641af0) },
97 { SN4(0xf8000000), SN4(0xf8000000), SN4(0xf8000000), SN4(0xf8000000) },
98 { SN4(0xf89be510), SN4(0xfcf043ac), SN4(0x030fbc54), SN4(0x07641af0) }
101 static const int32_t synmatrix8[16][8] = {
102 { SN8(0x05a82798), SN8(0xfa57d868), SN8(0xfa57d868), SN8(0x05a82798),
103 SN8(0x05a82798), SN8(0xfa57d868), SN8(0xfa57d868), SN8(0x05a82798) },
104 { SN8(0x0471ced0), SN8(0xf8275a10), SN8(0x018f8b84), SN8(0x06a6d988),
105 SN8(0xf9592678), SN8(0xfe70747c), SN8(0x07d8a5f0), SN8(0xfb8e3130) },
106 { SN8(0x030fbc54), SN8(0xf89be510), SN8(0x07641af0), SN8(0xfcf043ac),
107 SN8(0xfcf043ac), SN8(0x07641af0), SN8(0xf89be510), SN8(0x030fbc54) },
108 { SN8(0x018f8b84), SN8(0xfb8e3130), SN8(0x06a6d988), SN8(0xf8275a10),
109 SN8(0x07d8a5f0), SN8(0xf9592678), SN8(0x0471ced0), SN8(0xfe70747c) },
110 { SN8(0x00000000), SN8(0x00000000), SN8(0x00000000), SN8(0x00000000),
111 SN8(0x00000000), SN8(0x00000000), SN8(0x00000000), SN8(0x00000000) },
112 { SN8(0xfe70747c), SN8(0x0471ced0), SN8(0xf9592678), SN8(0x07d8a5f0),
113 SN8(0xf8275a10), SN8(0x06a6d988), SN8(0xfb8e3130), SN8(0x018f8b84) },
114 { SN8(0xfcf043ac), SN8(0x07641af0), SN8(0xf89be510), SN8(0x030fbc54),
115 SN8(0x030fbc54), SN8(0xf89be510), SN8(0x07641af0), SN8(0xfcf043ac) },
116 { SN8(0xfb8e3130), SN8(0x07d8a5f0), SN8(0xfe70747c), SN8(0xf9592678),
117 SN8(0x06a6d988), SN8(0x018f8b84), SN8(0xf8275a10), SN8(0x0471ced0) },
118 { SN8(0xfa57d868), SN8(0x05a82798), SN8(0x05a82798), SN8(0xfa57d868),
119 SN8(0xfa57d868), SN8(0x05a82798), SN8(0x05a82798), SN8(0xfa57d868) },
120 { SN8(0xf9592678), SN8(0x018f8b84), SN8(0x07d8a5f0), SN8(0x0471ced0),
121 SN8(0xfb8e3130), SN8(0xf8275a10), SN8(0xfe70747c), SN8(0x06a6d988) },
122 { SN8(0xf89be510), SN8(0xfcf043ac), SN8(0x030fbc54), SN8(0x07641af0),
123 SN8(0x07641af0), SN8(0x030fbc54), SN8(0xfcf043ac), SN8(0xf89be510) },
124 { SN8(0xf8275a10), SN8(0xf9592678), SN8(0xfb8e3130), SN8(0xfe70747c),
125 SN8(0x018f8b84), SN8(0x0471ced0), SN8(0x06a6d988), SN8(0x07d8a5f0) },
126 { SN8(0xf8000000), SN8(0xf8000000), SN8(0xf8000000), SN8(0xf8000000),
127 SN8(0xf8000000), SN8(0xf8000000), SN8(0xf8000000), SN8(0xf8000000) },
128 { SN8(0xf8275a10), SN8(0xf9592678), SN8(0xfb8e3130), SN8(0xfe70747c),
129 SN8(0x018f8b84), SN8(0x0471ced0), SN8(0x06a6d988), SN8(0x07d8a5f0) },
130 { SN8(0xf89be510), SN8(0xfcf043ac), SN8(0x030fbc54), SN8(0x07641af0),
131 SN8(0x07641af0), SN8(0x030fbc54), SN8(0xfcf043ac), SN8(0xf89be510) },
132 { SN8(0xf9592678), SN8(0x018f8b84), SN8(0x07d8a5f0), SN8(0x0471ced0),
133 SN8(0xfb8e3130), SN8(0xf8275a10), SN8(0xfe70747c), SN8(0x06a6d988) }
136 /* Uncomment the following line to enable high precision build of SBC encoder */
138 /* #define SBC_HIGH_PRECISION */
140 #ifdef SBC_HIGH_PRECISION
141 #define FIXED_A int64_t /* data type for fixed point accumulator */
142 #define FIXED_T int32_t /* data type for fixed point constants */
143 #define SBC_FIXED_EXTRA_BITS 16
145 #define FIXED_A int32_t /* data type for fixed point accumulator */
146 #define FIXED_T int16_t /* data type for fixed point constants */
147 #define SBC_FIXED_EXTRA_BITS 0
150 /* A2DP specification: Section 12.8 Tables
152 * Original values are premultiplied by 2 for better precision (that is the
153 * maximum which is possible without overflows)
155 * Note: in each block of 8 numbers sign was changed for elements 2 and 7
156 * in order to compensate the same change applied to cos_table_fixed_4
158 #define SBC_PROTO_FIXED4_SCALE \
159 ((sizeof(FIXED_T) * CHAR_BIT - 1) - SBC_FIXED_EXTRA_BITS + 1)
160 #define F(x) (FIXED_A) ((x * 2) * \
161 ((FIXED_A) 1 << (sizeof(FIXED_T) * CHAR_BIT - 1)) + 0.5)
162 static const FIXED_T _sbc_proto_fixed4[40] = {
163 F(0.00000000E+00), F(5.36548976E-04),
164 -F(1.49188357E-03), F(2.73370904E-03),
165 F(3.83720193E-03), F(3.89205149E-03),
166 F(1.86581691E-03), F(3.06012286E-03),
168 F(1.09137620E-02), F(2.04385087E-02),
169 -F(2.88757392E-02), F(3.21939290E-02),
170 F(2.58767811E-02), F(6.13245186E-03),
171 -F(2.88217274E-02), F(7.76463494E-02),
173 F(1.35593274E-01), F(1.94987841E-01),
174 -F(2.46636662E-01), F(2.81828203E-01),
175 F(2.94315332E-01), F(2.81828203E-01),
176 F(2.46636662E-01), -F(1.94987841E-01),
178 -F(1.35593274E-01), -F(7.76463494E-02),
179 F(2.88217274E-02), F(6.13245186E-03),
180 F(2.58767811E-02), F(3.21939290E-02),
181 F(2.88757392E-02), -F(2.04385087E-02),
183 -F(1.09137620E-02), -F(3.06012286E-03),
184 -F(1.86581691E-03), F(3.89205149E-03),
185 F(3.83720193E-03), F(2.73370904E-03),
186 F(1.49188357E-03), -F(5.36548976E-04),
191 * To produce this cosine matrix in Octave:
195 * for j = 0:7 b(i+1, j+1) = cos((i + 0.5) * (j - 2) * (pi/4))
198 * printf("%.10f, ", b');
200 * Note: in each block of 8 numbers sign was changed for elements 2 and 7
202 * Change of sign for element 2 allows to replace constant 1.0 (not
203 * representable in Q15 format) with -1.0 (fine with Q15).
204 * Changed sign for element 7 allows to have more similar constants
205 * and simplify subband filter function code.
207 #define SBC_COS_TABLE_FIXED4_SCALE \
208 ((sizeof(FIXED_T) * CHAR_BIT - 1) + SBC_FIXED_EXTRA_BITS)
209 #define F(x) (FIXED_A) ((x) * \
210 ((FIXED_A) 1 << (sizeof(FIXED_T) * CHAR_BIT - 1)) + 0.5)
211 static const FIXED_T cos_table_fixed_4[32] = {
212 F(0.7071067812), F(0.9238795325), -F(1.0000000000), F(0.9238795325),
213 F(0.7071067812), F(0.3826834324), F(0.0000000000), F(0.3826834324),
215 -F(0.7071067812), F(0.3826834324), -F(1.0000000000), F(0.3826834324),
216 -F(0.7071067812), -F(0.9238795325), -F(0.0000000000), -F(0.9238795325),
218 -F(0.7071067812), -F(0.3826834324), -F(1.0000000000), -F(0.3826834324),
219 -F(0.7071067812), F(0.9238795325), F(0.0000000000), F(0.9238795325),
221 F(0.7071067812), -F(0.9238795325), -F(1.0000000000), -F(0.9238795325),
222 F(0.7071067812), -F(0.3826834324), -F(0.0000000000), -F(0.3826834324),
226 /* A2DP specification: Section 12.8 Tables
228 * Original values are premultiplied by 4 for better precision (that is the
229 * maximum which is possible without overflows)
231 * Note: in each block of 16 numbers sign was changed for elements 4, 13, 14, 15
232 * in order to compensate the same change applied to cos_table_fixed_8
234 #define SBC_PROTO_FIXED8_SCALE \
235 ((sizeof(FIXED_T) * CHAR_BIT - 1) - SBC_FIXED_EXTRA_BITS + 2)
236 #define F(x) (FIXED_A) ((x * 4) * \
237 ((FIXED_A) 1 << (sizeof(FIXED_T) * CHAR_BIT - 1)) + 0.5)
238 static const FIXED_T _sbc_proto_fixed8[80] = {
239 F(0.00000000E+00), F(1.56575398E-04),
240 F(3.43256425E-04), F(5.54620202E-04),
241 -F(8.23919506E-04), F(1.13992507E-03),
242 F(1.47640169E-03), F(1.78371725E-03),
243 F(2.01182542E-03), F(2.10371989E-03),
244 F(1.99454554E-03), F(1.61656283E-03),
245 F(9.02154502E-04), F(1.78805361E-04),
246 F(1.64973098E-03), F(3.49717454E-03),
248 F(5.65949473E-03), F(8.02941163E-03),
249 F(1.04584443E-02), F(1.27472335E-02),
250 -F(1.46525263E-02), F(1.59045603E-02),
251 F(1.62208471E-02), F(1.53184106E-02),
252 F(1.29371806E-02), F(8.85757540E-03),
253 F(2.92408442E-03), -F(4.91578024E-03),
254 -F(1.46404076E-02), F(2.61098752E-02),
255 F(3.90751381E-02), F(5.31873032E-02),
257 F(6.79989431E-02), F(8.29847578E-02),
258 F(9.75753918E-02), F(1.11196689E-01),
259 -F(1.23264548E-01), F(1.33264415E-01),
260 F(1.40753505E-01), F(1.45389847E-01),
261 F(1.46955068E-01), F(1.45389847E-01),
262 F(1.40753505E-01), F(1.33264415E-01),
263 F(1.23264548E-01), -F(1.11196689E-01),
264 -F(9.75753918E-02), -F(8.29847578E-02),
266 -F(6.79989431E-02), -F(5.31873032E-02),
267 -F(3.90751381E-02), -F(2.61098752E-02),
268 F(1.46404076E-02), -F(4.91578024E-03),
269 F(2.92408442E-03), F(8.85757540E-03),
270 F(1.29371806E-02), F(1.53184106E-02),
271 F(1.62208471E-02), F(1.59045603E-02),
272 F(1.46525263E-02), -F(1.27472335E-02),
273 -F(1.04584443E-02), -F(8.02941163E-03),
275 -F(5.65949473E-03), -F(3.49717454E-03),
276 -F(1.64973098E-03), -F(1.78805361E-04),
277 -F(9.02154502E-04), F(1.61656283E-03),
278 F(1.99454554E-03), F(2.10371989E-03),
279 F(2.01182542E-03), F(1.78371725E-03),
280 F(1.47640169E-03), F(1.13992507E-03),
281 F(8.23919506E-04), -F(5.54620202E-04),
282 -F(3.43256425E-04), -F(1.56575398E-04),
287 * To produce this cosine matrix in Octave:
291 * for j = 0:15 b(i+1, j+1) = cos((i + 0.5) * (j - 4) * (pi/8))
293 * printf("%.10f, ", b');
295 * Note: in each block of 16 numbers sign was changed for elements 4, 13, 14, 15
297 * Change of sign for element 4 allows to replace constant 1.0 (not
298 * representable in Q15 format) with -1.0 (fine with Q15).
299 * Changed signs for elements 13, 14, 15 allow to have more similar constants
300 * and simplify subband filter function code.
302 #define SBC_COS_TABLE_FIXED8_SCALE \
303 ((sizeof(FIXED_T) * CHAR_BIT - 1) + SBC_FIXED_EXTRA_BITS)
304 #define F(x) (FIXED_A) ((x) * \
305 ((FIXED_A) 1 << (sizeof(FIXED_T) * CHAR_BIT - 1)) + 0.5)
306 static const FIXED_T cos_table_fixed_8[128] = {
307 F(0.7071067812), F(0.8314696123), F(0.9238795325), F(0.9807852804),
308 -F(1.0000000000), F(0.9807852804), F(0.9238795325), F(0.8314696123),
309 F(0.7071067812), F(0.5555702330), F(0.3826834324), F(0.1950903220),
310 F(0.0000000000), F(0.1950903220), F(0.3826834324), F(0.5555702330),
312 -F(0.7071067812), -F(0.1950903220), F(0.3826834324), F(0.8314696123),
313 -F(1.0000000000), F(0.8314696123), F(0.3826834324), -F(0.1950903220),
314 -F(0.7071067812), -F(0.9807852804), -F(0.9238795325), -F(0.5555702330),
315 -F(0.0000000000), -F(0.5555702330), -F(0.9238795325), -F(0.9807852804),
317 -F(0.7071067812), -F(0.9807852804), -F(0.3826834324), F(0.5555702330),
318 -F(1.0000000000), F(0.5555702330), -F(0.3826834324), -F(0.9807852804),
319 -F(0.7071067812), F(0.1950903220), F(0.9238795325), F(0.8314696123),
320 F(0.0000000000), F(0.8314696123), F(0.9238795325), F(0.1950903220),
322 F(0.7071067812), -F(0.5555702330), -F(0.9238795325), F(0.1950903220),
323 -F(1.0000000000), F(0.1950903220), -F(0.9238795325), -F(0.5555702330),
324 F(0.7071067812), F(0.8314696123), -F(0.3826834324), -F(0.9807852804),
325 -F(0.0000000000), -F(0.9807852804), -F(0.3826834324), F(0.8314696123),
327 F(0.7071067812), F(0.5555702330), -F(0.9238795325), -F(0.1950903220),
328 -F(1.0000000000), -F(0.1950903220), -F(0.9238795325), F(0.5555702330),
329 F(0.7071067812), -F(0.8314696123), -F(0.3826834324), F(0.9807852804),
330 F(0.0000000000), F(0.9807852804), -F(0.3826834324), -F(0.8314696123),
332 -F(0.7071067812), F(0.9807852804), -F(0.3826834324), -F(0.5555702330),
333 -F(1.0000000000), -F(0.5555702330), -F(0.3826834324), F(0.9807852804),
334 -F(0.7071067812), -F(0.1950903220), F(0.9238795325), -F(0.8314696123),
335 -F(0.0000000000), -F(0.8314696123), F(0.9238795325), -F(0.1950903220),
337 -F(0.7071067812), F(0.1950903220), F(0.3826834324), -F(0.8314696123),
338 -F(1.0000000000), -F(0.8314696123), F(0.3826834324), F(0.1950903220),
339 -F(0.7071067812), F(0.9807852804), -F(0.9238795325), F(0.5555702330),
340 -F(0.0000000000), F(0.5555702330), -F(0.9238795325), F(0.9807852804),
342 F(0.7071067812), -F(0.8314696123), F(0.9238795325), -F(0.9807852804),
343 -F(1.0000000000), -F(0.9807852804), F(0.9238795325), -F(0.8314696123),
344 F(0.7071067812), -F(0.5555702330), F(0.3826834324), -F(0.1950903220),
345 -F(0.0000000000), -F(0.1950903220), F(0.3826834324), -F(0.5555702330),