• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2006-2010  Nokia Corporation
6  *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
7  *  Copyright (C) 2018       Pali Rohár <pali.rohar@gmail.com>
8  *
9  *
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.
14  *
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.
19  *
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
23  *
24  */
25 
26 #include <endian.h>
27 #include <stdint.h>
28 
29 #define A2DP_CODEC_SBC			0x00
30 #define A2DP_CODEC_MPEG12		0x01
31 #define A2DP_CODEC_MPEG24		0x02
32 #define A2DP_CODEC_ATRAC		0x04
33 #define A2DP_CODEC_VENDOR		0xFF
34 
35 #define SBC_SAMPLING_FREQ_16000		(1 << 3)
36 #define SBC_SAMPLING_FREQ_32000		(1 << 2)
37 #define SBC_SAMPLING_FREQ_44100		(1 << 1)
38 #define SBC_SAMPLING_FREQ_48000		1
39 
40 #define SBC_CHANNEL_MODE_MONO		(1 << 3)
41 #define SBC_CHANNEL_MODE_DUAL_CHANNEL	(1 << 2)
42 #define SBC_CHANNEL_MODE_STEREO		(1 << 1)
43 #define SBC_CHANNEL_MODE_JOINT_STEREO	1
44 
45 #define SBC_BLOCK_LENGTH_4		(1 << 3)
46 #define SBC_BLOCK_LENGTH_8		(1 << 2)
47 #define SBC_BLOCK_LENGTH_12		(1 << 1)
48 #define SBC_BLOCK_LENGTH_16		1
49 
50 #define SBC_SUBBANDS_4			(1 << 1)
51 #define SBC_SUBBANDS_8			1
52 
53 #define SBC_ALLOCATION_SNR		(1 << 1)
54 #define SBC_ALLOCATION_LOUDNESS		1
55 
56 #define SBC_MIN_BITPOOL			2
57 #define SBC_MAX_BITPOOL			250
58 
59 /* Other settings:
60  * Block length = 16
61  * Allocation method = Loudness
62  * Subbands = 8
63  */
64 #define SBC_BITPOOL_MQ_MONO_44100		19
65 #define SBC_BITPOOL_MQ_MONO_48000		18
66 #define SBC_BITPOOL_MQ_JOINT_STEREO_44100	35
67 #define SBC_BITPOOL_MQ_JOINT_STEREO_48000	33
68 #define SBC_BITPOOL_HQ_MONO_44100		31
69 #define SBC_BITPOOL_HQ_MONO_48000		29
70 #define SBC_BITPOOL_HQ_JOINT_STEREO_44100	53
71 #define SBC_BITPOOL_HQ_JOINT_STEREO_48000	51
72 
73 #define MPEG_CHANNEL_MODE_MONO		(1 << 3)
74 #define MPEG_CHANNEL_MODE_DUAL_CHANNEL	(1 << 2)
75 #define MPEG_CHANNEL_MODE_STEREO	(1 << 1)
76 #define MPEG_CHANNEL_MODE_JOINT_STEREO	1
77 
78 #define MPEG_LAYER_MP1			(1 << 2)
79 #define MPEG_LAYER_MP2			(1 << 1)
80 #define MPEG_LAYER_MP3			1
81 
82 #define MPEG_SAMPLING_FREQ_16000	(1 << 5)
83 #define MPEG_SAMPLING_FREQ_22050	(1 << 4)
84 #define MPEG_SAMPLING_FREQ_24000	(1 << 3)
85 #define MPEG_SAMPLING_FREQ_32000	(1 << 2)
86 #define MPEG_SAMPLING_FREQ_44100	(1 << 1)
87 #define MPEG_SAMPLING_FREQ_48000	1
88 
89 #define MPEG_BIT_RATE_INDEX_0		(1 << 0)
90 #define MPEG_BIT_RATE_INDEX_1		(1 << 1)
91 #define MPEG_BIT_RATE_INDEX_2		(1 << 2)
92 #define MPEG_BIT_RATE_INDEX_3		(1 << 3)
93 #define MPEG_BIT_RATE_INDEX_4		(1 << 4)
94 #define MPEG_BIT_RATE_INDEX_5		(1 << 5)
95 #define MPEG_BIT_RATE_INDEX_6		(1 << 6)
96 #define MPEG_BIT_RATE_INDEX_7		(1 << 7)
97 #define MPEG_BIT_RATE_INDEX_8		(1 << 8)
98 #define MPEG_BIT_RATE_INDEX_9		(1 << 9)
99 #define MPEG_BIT_RATE_INDEX_10		(1 << 10)
100 #define MPEG_BIT_RATE_INDEX_11		(1 << 11)
101 #define MPEG_BIT_RATE_INDEX_12		(1 << 12)
102 #define MPEG_BIT_RATE_INDEX_13		(1 << 13)
103 #define MPEG_BIT_RATE_INDEX_14		(1 << 14)
104 
105 #define MPEG_MP1_BIT_RATE_32000		MPEG_BIT_RATE_INDEX_1
106 #define MPEG_MP1_BIT_RATE_64000		MPEG_BIT_RATE_INDEX_2
107 #define MPEG_MP1_BIT_RATE_96000		MPEG_BIT_RATE_INDEX_3
108 #define MPEG_MP1_BIT_RATE_128000	MPEG_BIT_RATE_INDEX_4
109 #define MPEG_MP1_BIT_RATE_160000	MPEG_BIT_RATE_INDEX_5
110 #define MPEG_MP1_BIT_RATE_192000	MPEG_BIT_RATE_INDEX_6
111 #define MPEG_MP1_BIT_RATE_224000	MPEG_BIT_RATE_INDEX_7
112 #define MPEG_MP1_BIT_RATE_256000	MPEG_BIT_RATE_INDEX_8
113 #define MPEG_MP1_BIT_RATE_288000	MPEG_BIT_RATE_INDEX_9
114 #define MPEG_MP1_BIT_RATE_320000	MPEG_BIT_RATE_INDEX_10
115 #define MPEG_MP1_BIT_RATE_352000	MPEG_BIT_RATE_INDEX_11
116 #define MPEG_MP1_BIT_RATE_384000	MPEG_BIT_RATE_INDEX_12
117 #define MPEG_MP1_BIT_RATE_416000	MPEG_BIT_RATE_INDEX_13
118 #define MPEG_MP1_BIT_RATE_448000	MPEG_BIT_RATE_INDEX_14
119 
120 #define MPEG_MP2_BIT_RATE_32000		MPEG_BIT_RATE_INDEX_1
121 #define MPEG_MP2_BIT_RATE_48000		MPEG_BIT_RATE_INDEX_2
122 #define MPEG_MP2_BIT_RATE_56000		MPEG_BIT_RATE_INDEX_3
123 #define MPEG_MP2_BIT_RATE_64000		MPEG_BIT_RATE_INDEX_4
124 #define MPEG_MP2_BIT_RATE_80000		MPEG_BIT_RATE_INDEX_5
125 #define MPEG_MP2_BIT_RATE_96000		MPEG_BIT_RATE_INDEX_6
126 #define MPEG_MP2_BIT_RATE_112000	MPEG_BIT_RATE_INDEX_7
127 #define MPEG_MP2_BIT_RATE_128000	MPEG_BIT_RATE_INDEX_8
128 #define MPEG_MP2_BIT_RATE_160000	MPEG_BIT_RATE_INDEX_9
129 #define MPEG_MP2_BIT_RATE_192000	MPEG_BIT_RATE_INDEX_10
130 #define MPEG_MP2_BIT_RATE_224000	MPEG_BIT_RATE_INDEX_11
131 #define MPEG_MP2_BIT_RATE_256000	MPEG_BIT_RATE_INDEX_12
132 #define MPEG_MP2_BIT_RATE_320000	MPEG_BIT_RATE_INDEX_13
133 #define MPEG_MP2_BIT_RATE_384000	MPEG_BIT_RATE_INDEX_14
134 
135 #define MPEG_MP3_BIT_RATE_32000		MPEG_BIT_RATE_INDEX_1
136 #define MPEG_MP3_BIT_RATE_40000		MPEG_BIT_RATE_INDEX_2
137 #define MPEG_MP3_BIT_RATE_48000		MPEG_BIT_RATE_INDEX_3
138 #define MPEG_MP3_BIT_RATE_56000		MPEG_BIT_RATE_INDEX_4
139 #define MPEG_MP3_BIT_RATE_64000		MPEG_BIT_RATE_INDEX_5
140 #define MPEG_MP3_BIT_RATE_80000		MPEG_BIT_RATE_INDEX_6
141 #define MPEG_MP3_BIT_RATE_96000		MPEG_BIT_RATE_INDEX_7
142 #define MPEG_MP3_BIT_RATE_112000	MPEG_BIT_RATE_INDEX_8
143 #define MPEG_MP3_BIT_RATE_128000	MPEG_BIT_RATE_INDEX_9
144 #define MPEG_MP3_BIT_RATE_160000	MPEG_BIT_RATE_INDEX_10
145 #define MPEG_MP3_BIT_RATE_192000	MPEG_BIT_RATE_INDEX_11
146 #define MPEG_MP3_BIT_RATE_224000	MPEG_BIT_RATE_INDEX_12
147 #define MPEG_MP3_BIT_RATE_256000	MPEG_BIT_RATE_INDEX_13
148 #define MPEG_MP3_BIT_RATE_320000	MPEG_BIT_RATE_INDEX_14
149 
150 #define MPEG_BIT_RATE_FREE		MPEG_BIT_RATE_INDEX_0
151 
152 #define MPEG_GET_BITRATE(a) ((uint16_t)(a).bitrate1 << 8 | (a).bitrate2)
153 #define MPEG_SET_BITRATE(a, b) \
154 	do { \
155 		(a).bitrate1 = ((b) >> 8) & 0x7f; \
156 		(a).bitrate2 = (b) & 0xff; \
157 	} while (0)
158 
159 #define AAC_OBJECT_TYPE_MPEG2_AAC_LC	0x80
160 #define AAC_OBJECT_TYPE_MPEG4_AAC_LC	0x40
161 #define AAC_OBJECT_TYPE_MPEG4_AAC_LTP	0x20
162 #define AAC_OBJECT_TYPE_MPEG4_AAC_SCA	0x10
163 
164 #define AAC_SAMPLING_FREQ_8000		0x0800
165 #define AAC_SAMPLING_FREQ_11025		0x0400
166 #define AAC_SAMPLING_FREQ_12000		0x0200
167 #define AAC_SAMPLING_FREQ_16000		0x0100
168 #define AAC_SAMPLING_FREQ_22050		0x0080
169 #define AAC_SAMPLING_FREQ_24000		0x0040
170 #define AAC_SAMPLING_FREQ_32000		0x0020
171 #define AAC_SAMPLING_FREQ_44100		0x0010
172 #define AAC_SAMPLING_FREQ_48000		0x0008
173 #define AAC_SAMPLING_FREQ_64000		0x0004
174 #define AAC_SAMPLING_FREQ_88200		0x0002
175 #define AAC_SAMPLING_FREQ_96000		0x0001
176 
177 #define AAC_CHANNELS_1			0x02
178 #define AAC_CHANNELS_2			0x01
179 
180 #define AAC_GET_BITRATE(a) ((a).bitrate1 << 16 | \
181 					(a).bitrate2 << 8 | (a).bitrate3)
182 #define AAC_GET_FREQUENCY(a) ((a).frequency1 << 4 | (a).frequency2)
183 
184 #define AAC_SET_BITRATE(a, b) \
185 	do { \
186 		(a).bitrate1 = (b >> 16) & 0x7f; \
187 		(a).bitrate2 = (b >> 8) & 0xff; \
188 		(a).bitrate3 = b & 0xff; \
189 	} while (0)
190 #define AAC_SET_FREQUENCY(a, f) \
191 	do { \
192 		(a).frequency1 = (f >> 4) & 0xff; \
193 		(a).frequency2 = f & 0x0f; \
194 	} while (0)
195 
196 #define AAC_INIT_BITRATE(b) \
197 	.bitrate1 = (b >> 16) & 0x7f, \
198 	.bitrate2 = (b >> 8) & 0xff, \
199 	.bitrate3 = b & 0xff,
200 #define AAC_INIT_FREQUENCY(f) \
201 	.frequency1 = (f >> 4) & 0xff, \
202 	.frequency2 = f & 0x0f,
203 
204 #define APTX_VENDOR_ID			0x0000004f
205 #define APTX_CODEC_ID			0x0001
206 
207 #define APTX_CHANNEL_MODE_MONO		0x01
208 #define APTX_CHANNEL_MODE_STEREO	0x02
209 
210 #define APTX_SAMPLING_FREQ_16000	0x08
211 #define APTX_SAMPLING_FREQ_32000	0x04
212 #define APTX_SAMPLING_FREQ_44100	0x02
213 #define APTX_SAMPLING_FREQ_48000	0x01
214 
215 #define FASTSTREAM_VENDOR_ID		0x0000000a
216 #define FASTSTREAM_CODEC_ID		0x0001
217 
218 #define FASTSTREAM_DIRECTION_SINK	0x1
219 #define FASTSTREAM_DIRECTION_SOURCE	0x2
220 
221 #define FASTSTREAM_SINK_SAMPLING_FREQ_44100	0x2
222 #define FASTSTREAM_SINK_SAMPLING_FREQ_48000	0x1
223 
224 #define FASTSTREAM_SOURCE_SAMPLING_FREQ_16000	0x2
225 
226 #define APTX_LL_VENDOR_ID		0x0000000a
227 #define APTX_LL_CODEC_ID		0x0002
228 
229 /* Default parameters for aptX Low Latency encoder */
230 
231 /* Target codec buffer level = 180 */
232 #define APTX_LL_TARGET_LEVEL2	0xb4
233 #define APTX_LL_TARGET_LEVEL1	0x00
234 
235 /* Initial codec buffer level = 360 */
236 #define APTX_LL_INITIAL_LEVEL2	0x68
237 #define APTX_LL_INITIAL_LEVEL1	0x01
238 
239 /* SRA max rate 0.005 * 10000 = 50 */
240 #define APTX_LL_SRA_MAX_RATE		0x32
241 
242 /* SRA averaging time = 1s */
243 #define APTX_LL_SRA_AVG_TIME		0x01
244 
245 /* Good working codec buffer level = 180 */
246 #define APTX_LL_GOOD_WORKING_LEVEL2	0xB4
247 #define APTX_LL_GOOD_WORKING_LEVEL1	0x00
248 
249 #define APTX_HD_VENDOR_ID		0x000000D7
250 #define APTX_HD_CODEC_ID		0x0024
251 
252 #define LDAC_VENDOR_ID			0x0000012d
253 #define LDAC_CODEC_ID			0x00aa
254 
255 #define LDAC_SAMPLING_FREQ_44100	0x20
256 #define LDAC_SAMPLING_FREQ_48000	0x10
257 #define LDAC_SAMPLING_FREQ_88200	0x08
258 #define LDAC_SAMPLING_FREQ_96000	0x04
259 #define LDAC_SAMPLING_FREQ_176400	0x02
260 #define LDAC_SAMPLING_FREQ_192000	0x01
261 
262 #define LDAC_CHANNEL_MODE_MONO		0x04
263 #define LDAC_CHANNEL_MODE_DUAL		0x02
264 #define LDAC_CHANNEL_MODE_STEREO	0x01
265 
266 typedef struct {
267 	uint8_t vendor_id4;
268 	uint8_t vendor_id3;
269 	uint8_t vendor_id2;
270 	uint8_t vendor_id1;
271 	uint8_t codec_id2;
272 	uint8_t codec_id1;
273 } __attribute__ ((packed)) a2dp_vendor_codec_t;
274 
275 #define A2DP_GET_VENDOR_ID(a) ( \
276 		(((uint32_t)(a).vendor_id4) <<  0) | \
277 		(((uint32_t)(a).vendor_id3) <<  8) | \
278 		(((uint32_t)(a).vendor_id2) << 16) | \
279 		(((uint32_t)(a).vendor_id1) << 24) \
280 	)
281 #define A2DP_GET_CODEC_ID(a) ((a).codec_id2 | (((uint16_t)(a).codec_id1) << 8))
282 #define A2DP_SET_VENDOR_ID_CODEC_ID(v, c) ((a2dp_vendor_codec_t){ \
283 		.vendor_id4 = (((v) >>  0) & 0xff), \
284 		.vendor_id3 = (((v) >>  8) & 0xff), \
285 		.vendor_id2 = (((v) >> 16) & 0xff), \
286 		.vendor_id1 = (((v) >> 24) & 0xff), \
287 		.codec_id2 = (((c) >> 0) & 0xff), \
288 		.codec_id1 = (((c) >> 8) & 0xff), \
289 	})
290 
291 typedef struct {
292 	uint8_t reserved;
293 	uint8_t target_level2;
294 	uint8_t target_level1;
295 	uint8_t initial_level2;
296 	uint8_t initial_level1;
297 	uint8_t sra_max_rate;
298 	uint8_t sra_avg_time;
299 	uint8_t good_working_level2;
300 	uint8_t good_working_level1;
301 } __attribute__ ((packed)) a2dp_aptx_ll_new_caps_t;
302 
303 typedef struct {
304 	a2dp_vendor_codec_t info;
305 	uint8_t frequency;
306 	uint8_t channel_mode;
307 } __attribute__ ((packed)) a2dp_ldac_t;
308 
309 #if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
310 	__BYTE_ORDER == __LITTLE_ENDIAN
311 
312 typedef struct {
313 	uint8_t channel_mode:4;
314 	uint8_t frequency:4;
315 	uint8_t allocation_method:2;
316 	uint8_t subbands:2;
317 	uint8_t block_length:4;
318 	uint8_t min_bitpool;
319 	uint8_t max_bitpool;
320 } __attribute__ ((packed)) a2dp_sbc_t;
321 
322 typedef struct {
323 	uint8_t channel_mode:4;
324 	uint8_t crc:1;
325 	uint8_t layer:3;
326 	uint8_t frequency:6;
327 	uint8_t mpf:1;
328 	uint8_t rfa:1;
329 	uint8_t bitrate1:7;
330 	uint8_t vbr:1;
331 	uint8_t bitrate2;
332 } __attribute__ ((packed)) a2dp_mpeg_t;
333 
334 typedef struct {
335 	uint8_t object_type;
336 	uint8_t frequency1;
337 	uint8_t rfa:2;
338 	uint8_t channels:2;
339 	uint8_t frequency2:4;
340 	uint8_t bitrate1:7;
341 	uint8_t vbr:1;
342 	uint8_t bitrate2;
343 	uint8_t bitrate3;
344 } __attribute__ ((packed)) a2dp_aac_t;
345 
346 typedef struct {
347 	a2dp_vendor_codec_t info;
348 	uint8_t channel_mode:4;
349 	uint8_t frequency:4;
350 } __attribute__ ((packed)) a2dp_aptx_t;
351 
352 typedef struct {
353 	a2dp_vendor_codec_t info;
354 	uint8_t direction;
355 	uint8_t sink_frequency:4;
356 	uint8_t source_frequency:4;
357 } __attribute__ ((packed)) a2dp_faststream_t;
358 
359 typedef struct {
360 	a2dp_aptx_t aptx;
361 	uint8_t bidirect_link:1;
362 	uint8_t has_new_caps:1;
363 	uint8_t reserved:6;
364 	a2dp_aptx_ll_new_caps_t new_caps[0];
365 } __attribute__ ((packed)) a2dp_aptx_ll_t;
366 
367 #elif defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \
368 	__BYTE_ORDER == __BIG_ENDIAN
369 
370 typedef struct {
371 	uint8_t frequency:4;
372 	uint8_t channel_mode:4;
373 	uint8_t block_length:4;
374 	uint8_t subbands:2;
375 	uint8_t allocation_method:2;
376 	uint8_t min_bitpool;
377 	uint8_t max_bitpool;
378 } __attribute__ ((packed)) a2dp_sbc_t;
379 
380 typedef struct {
381 	uint8_t layer:3;
382 	uint8_t crc:1;
383 	uint8_t channel_mode:4;
384 	uint8_t rfa:1;
385 	uint8_t mpf:1;
386 	uint8_t frequency:6;
387 	uint8_t vbr:1;
388 	uint8_t bitrate1:7;
389 	uint8_t bitrate2;
390 } __attribute__ ((packed)) a2dp_mpeg_t;
391 
392 typedef struct {
393 	uint8_t object_type;
394 	uint8_t frequency1;
395 	uint8_t frequency2:4;
396 	uint8_t channels:2;
397 	uint8_t rfa:2;
398 	uint8_t vbr:1;
399 	uint8_t bitrate1:7;
400 	uint8_t bitrate2;
401 	uint8_t bitrate3;
402 } __attribute__ ((packed)) a2dp_aac_t;
403 
404 typedef struct {
405 	a2dp_vendor_codec_t info;
406 	uint8_t frequency:4;
407 	uint8_t channel_mode:4;
408 } __attribute__ ((packed)) a2dp_aptx_t;
409 
410 typedef struct {
411 	a2dp_vendor_codec_t info;
412 	uint8_t direction;
413 	uint8_t source_frequency:4;
414 	uint8_t sink_frequency:4;
415 } __attribute__ ((packed)) a2dp_faststream_t;
416 
417 typedef struct {
418 	a2dp_aptx_t aptx;
419 	uint8_t reserved:6;
420 	uint8_t has_new_caps:1;
421 	uint8_t bidirect_link:1;
422 	a2dp_aptx_ll_new_caps_t new_caps[0];
423 } __attribute__ ((packed)) a2dp_aptx_ll_t;
424 
425 #else
426 #error "Unknown byte order"
427 #endif
428 
429 typedef struct {
430 	a2dp_aptx_t aptx;
431 	uint8_t reserved0;
432 	uint8_t reserved1;
433 	uint8_t reserved2;
434 	uint8_t reserved3;
435 } __attribute__ ((packed)) a2dp_aptx_hd_t;
436