Debian Codegraph @

utils/a2dpconf.c

raw
1 /*
2 * BlueALSA - a2dpconf.c
3 * Copyright (c) 2016-2024 Arkadiusz Bokowy
4 *
5 * This file is a part of bluez-alsa.
6 *
7 * This project is licensed under the terms of the MIT license.
8 *
9 */
10
11 #if HAVE_CONFIG_H
12 # include <config.h>
13 #endif
14
15 #include <getopt.h>
16 #include <stdbool.h>
17 #include <stdint.h>
18 #include <string.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #include <bluetooth/bluetooth.h>
23
24 #include "shared/a2dp-codecs.h"
25 #include "shared/defs.h"
26 #include "shared/hex.h"
27
28 static uint32_t get_codec(const char *s) {
29
30 char buffer[32] = { 0 };
31 char *tmp;
32
33 strncpy(buffer, s, sizeof(buffer) - 1);
34 if ((tmp = strchr(buffer, ':')) != NULL)
35 tmp[0] = '\0';
36
37 return a2dp_codecs_codec_id_from_string(buffer);
38 }
39
40 static ssize_t get_codec_blob(const char *s, void *dest, size_t n) {
41
42 const char *tmp;
43 if ((tmp = strchr(s, ':')) != NULL)
44 s = tmp + 1;
45
46 size_t len;
47 if ((len = strlen(s)) % 2 != 0) {
48 fprintf(stderr, "Invalid blob: Size not a multiple of 2: %zd\n", len);
49 return -1;
50 }
51
52 if (n == 0)
53 return len / 2;
54
55 if (n * 2 < len) {
56 fprintf(stderr, "Invalid blob buffer size: %zd < %zd\n", n * 2, len);
57 return -1;
58 }
59
60 return hex2bin(s, dest, len);
61 }
62
63 static char *bintohex(const void *src, size_t n) {
64 char *hex = calloc(1, n * 2 + 1);
65 bin2hex(src, hex, n);
66 return hex;
67 }
68
69 static int check_blob_size(size_t size, size_t value) {
70 if (value == size)
71 return 0;
72 fprintf(stderr, "Invalid codec blob size: %zd != %zd\n", value, size);
73 return -1;
74 }
75
76 static void dump_sbc(const void *blob, size_t size) {
77 const a2dp_sbc_t *sbc = blob;
78 if (check_blob_size(sizeof(*sbc), size) == -1)
79 return;
80 printf("SBC <hex:%s> {\n"
81 " sampling-frequency:4 =%s%s%s%s\n"
82 " channel-mode:4 =%s%s%s%s\n"
83 " block-length:4 =%s%s%s%s\n"
84 " sub-bands:2 =%s%s\n"
85 " allocation-method:2 =%s%s\n"
86 " min-bit-pool-value:8 = %u\n"
87 " max-bit-pool-value:8 = %u\n"
88 "}\n",
89 bintohex(sbc, sizeof(*sbc)),
90 sbc->sampling_freq & SBC_SAMPLING_FREQ_48000 ? " 48000" : "",
91 sbc->sampling_freq & SBC_SAMPLING_FREQ_44100 ? " 44100" : "",
92 sbc->sampling_freq & SBC_SAMPLING_FREQ_32000 ? " 32000" : "",
93 sbc->sampling_freq & SBC_SAMPLING_FREQ_16000 ? " 16000" : "",
94 sbc->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO ? " JointStereo" : "",
95 sbc->channel_mode & SBC_CHANNEL_MODE_STEREO ? " Stereo" : "",
96 sbc->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL ? " DualChannel" : "",
97 sbc->channel_mode & SBC_CHANNEL_MODE_MONO ? " Mono" : "",
98 sbc->block_length & SBC_BLOCK_LENGTH_16 ? " 16" : "",
99 sbc->block_length & SBC_BLOCK_LENGTH_12 ? " 12" : "",
100 sbc->block_length & SBC_BLOCK_LENGTH_8 ? " 8" : "",
101 sbc->block_length & SBC_BLOCK_LENGTH_4 ? " 4" : "",
102 sbc->subbands & SBC_SUBBANDS_8 ? " 8" : "",
103 sbc->subbands & SBC_SUBBANDS_4 ? " 4" : "",
104 sbc->allocation_method & SBC_ALLOCATION_LOUDNESS ? " Loudness" : "",
105 sbc->allocation_method & SBC_ALLOCATION_SNR ? " SNR" : "",
106 sbc->min_bitpool, sbc->max_bitpool);
107 }
108
109 static void dump_mpeg(const void *blob, size_t size) {
110 const a2dp_mpeg_t *mpeg = blob;
111 if (check_blob_size(sizeof(*mpeg), size) == -1)
112 return;
113 printf("MPEG-1,2 Audio <hex:%s> {\n"
114 " layer:3 =%s%s%s\n"
115 " crc:1 = %s\n"
116 " channel-mode:4 =%s%s%s%s\n"
117 " <reserved>:1\n"
118 " media-payload-format:1 = MPF-1%s\n"
119 " sampling-frequency:6 =%s%s%s%s%s%s\n"
120 " vbr:1 = %s\n"
121 " bitrate-index:15 = %#x\n"
122 "}\n",
123 bintohex(mpeg, sizeof(*mpeg)),
124 mpeg->layer & MPEG_LAYER_MP3 ? " MP3" : "",
125 mpeg->layer & MPEG_LAYER_MP2 ? " MP2" : "",
126 mpeg->layer & MPEG_LAYER_MP1 ? " MP1" : "",
127 mpeg->crc ? "true" : "false",
128 mpeg->channel_mode & MPEG_CHANNEL_MODE_JOINT_STEREO ? " JointStereo" : "",
129 mpeg->channel_mode & MPEG_CHANNEL_MODE_STEREO ? " Stereo" : "",
130 mpeg->channel_mode & MPEG_CHANNEL_MODE_DUAL_CHANNEL ? " DualChannel" : "",
131 mpeg->channel_mode & MPEG_CHANNEL_MODE_MONO ? " Mono" : "",
132 mpeg->mpf ? " MPF-2" : "",
133 mpeg->sampling_freq & MPEG_SAMPLING_FREQ_48000 ? " 48000" : "",
134 mpeg->sampling_freq & MPEG_SAMPLING_FREQ_44100 ? " 44100" : "",
135 mpeg->sampling_freq & MPEG_SAMPLING_FREQ_32000 ? " 32000" : "",
136 mpeg->sampling_freq & MPEG_SAMPLING_FREQ_24000 ? " 24000" : "",
137 mpeg->sampling_freq & MPEG_SAMPLING_FREQ_22050 ? " 22050" : "",
138 mpeg->sampling_freq & MPEG_SAMPLING_FREQ_16000 ? " 16000" : "",
139 mpeg->vbr ? "true" : "false",
140 A2DP_MPEG_GET_BITRATE(*mpeg));
141 }
142
143 static void dump_aac(const void *blob, size_t size) {
144 const a2dp_aac_t *aac = blob;
145 if (check_blob_size(sizeof(*aac), size) == -1)
146 return;
147 const uint16_t aac_sampling_freq = A2DP_AAC_GET_SAMPLING_FREQ(*aac);
148 printf("MPEG-2,4 AAC <hex:%s> {\n"
149 " object-type:7 =%s%s%s%s%s%s%s\n"
150 " dynamic-range-control:1 = %s\n"
151 " sampling-frequency:12 =%s%s%s%s%s%s%s%s%s%s%s%s\n"
152 " channel-mode:4 =%s%s%s%s\n"
153 " vbr:1 = %s\n"
154 " bitrate:23 = %u\n"
155 "}\n",
156 bintohex(aac, sizeof(*aac)),
157 aac->object_type & AAC_OBJECT_TYPE_MPEG4_ELD2 ? " MPEG4-ELD2" : "",
158 aac->object_type & AAC_OBJECT_TYPE_MPEG4_HE2 ? " MPEG4-HE2" : "",
159 aac->object_type & AAC_OBJECT_TYPE_MPEG4_HE ? " MPEG4-HE" : "",
160 aac->object_type & AAC_OBJECT_TYPE_MPEG4_SCA ? " MPEG4-SCA" : "",
161 aac->object_type & AAC_OBJECT_TYPE_MPEG4_LTP ? " MPEG4-LTP" : "",
162 aac->object_type & AAC_OBJECT_TYPE_MPEG4_LC ? " MPEG4-LC" : "",
163 aac->object_type & AAC_OBJECT_TYPE_MPEG2_LC ? " MPEG2-LC" : "",
164 aac->drc ? "true" : "false",
165 aac_sampling_freq & AAC_SAMPLING_FREQ_96000 ? " 96000" : "",
166 aac_sampling_freq & AAC_SAMPLING_FREQ_88200 ? " 88200" : "",
167 aac_sampling_freq & AAC_SAMPLING_FREQ_64000 ? " 64000" : "",
168 aac_sampling_freq & AAC_SAMPLING_FREQ_48000 ? " 48000" : "",
169 aac_sampling_freq & AAC_SAMPLING_FREQ_44100 ? " 44100" : "",
170 aac_sampling_freq & AAC_SAMPLING_FREQ_32000 ? " 32000" : "",
171 aac_sampling_freq & AAC_SAMPLING_FREQ_24000 ? " 24000" : "",
172 aac_sampling_freq & AAC_SAMPLING_FREQ_22050 ? " 22050" : "",
173 aac_sampling_freq & AAC_SAMPLING_FREQ_16000 ? " 16000" : "",
174 aac_sampling_freq & AAC_SAMPLING_FREQ_12000 ? " 12000" : "",
175 aac_sampling_freq & AAC_SAMPLING_FREQ_11025 ? " 11025" : "",
176 aac_sampling_freq & AAC_SAMPLING_FREQ_8000 ? " 8000" : "",
177 aac->channel_mode & AAC_CHANNEL_MODE_7_1 ? " Surround-7.1" : "",
178 aac->channel_mode & AAC_CHANNEL_MODE_5_1 ? " Surround-5.1" : "",
179 aac->channel_mode & AAC_CHANNEL_MODE_STEREO ? " Stereo" : "",
180 aac->channel_mode & AAC_CHANNEL_MODE_MONO ? " Mono" : "",
181 aac->vbr ? "true" : "false",
182 A2DP_AAC_GET_BITRATE(*aac));
183 }
184
185 static void dump_usac(const void *blob, size_t size) {
186 const a2dp_usac_t *usac = blob;
187 if (check_blob_size(sizeof(*usac), size) == -1)
188 return;
189 const uint32_t usac_sampling_freq = A2DP_USAC_GET_SAMPLING_FREQ(*usac);
190 printf("MPEG-D USAC <hex:%s> {\n"
191 " object-type:2 =%s\n"
192 " sampling-frequency:26 =%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
193 " channel-mode:4 =%s%s\n"
194 " vbr:1 = %s\n"
195 " bitrate:23 = %u\n"
196 "}\n",
197 bintohex(usac, sizeof(*usac)),
198 usac->object_type & USAC_OBJECT_TYPE_MPEGD_DRC ? " MPEG-D-DRC" : "",
199 usac_sampling_freq & USAC_SAMPLING_FREQ_96000 ? " 96000" : "",
200 usac_sampling_freq & USAC_SAMPLING_FREQ_88200 ? " 88200" : "",
201 usac_sampling_freq & USAC_SAMPLING_FREQ_76800 ? " 76800" : "",
202 usac_sampling_freq & USAC_SAMPLING_FREQ_70560 ? " 70560" : "",
203 usac_sampling_freq & USAC_SAMPLING_FREQ_64000 ? " 64000" : "",
204 usac_sampling_freq & USAC_SAMPLING_FREQ_58800 ? " 58800" : "",
205 usac_sampling_freq & USAC_SAMPLING_FREQ_48000 ? " 48000" : "",
206 usac_sampling_freq & USAC_SAMPLING_FREQ_44100 ? " 44100" : "",
207 usac_sampling_freq & USAC_SAMPLING_FREQ_38400 ? " 38400" : "",
208 usac_sampling_freq & USAC_SAMPLING_FREQ_35280 ? " 35280" : "",
209 usac_sampling_freq & USAC_SAMPLING_FREQ_32000 ? " 32000" : "",
210 usac_sampling_freq & USAC_SAMPLING_FREQ_29400 ? " 29400" : "",
211 usac_sampling_freq & USAC_SAMPLING_FREQ_24000 ? " 24000" : "",
212 usac_sampling_freq & USAC_SAMPLING_FREQ_22050 ? " 22050" : "",
213 usac_sampling_freq & USAC_SAMPLING_FREQ_19200 ? " 19200" : "",
214 usac_sampling_freq & USAC_SAMPLING_FREQ_17640 ? " 17640" : "",
215 usac_sampling_freq & USAC_SAMPLING_FREQ_16000 ? " 16000" : "",
216 usac_sampling_freq & USAC_SAMPLING_FREQ_14700 ? " 14700" : "",
217 usac_sampling_freq & USAC_SAMPLING_FREQ_12800 ? " 12800" : "",
218 usac_sampling_freq & USAC_SAMPLING_FREQ_12000 ? " 12000" : "",
219 usac_sampling_freq & USAC_SAMPLING_FREQ_11760 ? " 11760" : "",
220 usac_sampling_freq & USAC_SAMPLING_FREQ_11025 ? " 11025" : "",
221 usac_sampling_freq & USAC_SAMPLING_FREQ_9600 ? " 9600" : "",
222 usac_sampling_freq & USAC_SAMPLING_FREQ_8820 ? " 8820" : "",
223 usac_sampling_freq & USAC_SAMPLING_FREQ_8000 ? " 8000" : "",
224 usac_sampling_freq & USAC_SAMPLING_FREQ_7350 ? " 7350" : "",
225 usac->channel_mode & USAC_CHANNEL_MODE_STEREO ? " Stereo" : "",
226 usac->channel_mode & USAC_CHANNEL_MODE_MONO ? " Mono" : "",
227 usac->vbr ? "true" : "false",
228 A2DP_USAC_GET_BITRATE(*usac));
229 }
230
231 static void dump_atrac(const void *blob, size_t size) {
232 const a2dp_atrac_t *atrac = blob;
233 if (check_blob_size(sizeof(*atrac), size) == -1)
234 return;
235 printf("ATRAC <hex:%s> {\n"
236 " version:3 = ATRAC%u\n"
237 " channel-mode:3 =%s%s%s\n"
238 " <reserved>:4\n"
239 " sampling-frequency:2 =%s%s\n"
240 " vbr:1 = %s\n"
241 " bitrate-index:19 = %#x\n"
242 " max-sound-unit-length:16 = %u\n"
243 " <reserved>:8\n"
244 "}\n",
245 bintohex(atrac, sizeof(*atrac)),
246 atrac->version,
247 atrac->channel_mode & ATRAC_CHANNEL_MODE_JOINT_STEREO ? " JointStereo" : "",
248 atrac->channel_mode & ATRAC_CHANNEL_MODE_DUAL_CHANNEL ? " DualChannel" : "",
249 atrac->channel_mode & ATRAC_CHANNEL_MODE_MONO ? " Mono" : "",
250 atrac->sampling_freq & ATRAC_SAMPLING_FREQ_48000 ? " 48000" : "",
251 atrac->sampling_freq & ATRAC_SAMPLING_FREQ_44100 ? " 44100" : "",
252 atrac->vbr ? "true" : "false",
253 A2DP_ATRAC_GET_BITRATE(*atrac),
254 A2DP_ATRAC_GET_MAX_SUL(*atrac));
255 }
256
257 static void printf_vendor(const a2dp_vendor_info_t *info) {
258 printf(""
259 " vendor-id:32 = %#010x [%s]\n"
260 " vendor-codec-id:16 = %#06x\n",
261 A2DP_VENDOR_INFO_GET_VENDOR_ID(*info),
262 bt_compidtostr(A2DP_VENDOR_INFO_GET_VENDOR_ID(*info)),
263 A2DP_VENDOR_INFO_GET_CODEC_ID(*info));
264 }
265
266 static void dump_vendor(const void *blob, size_t size) {
267 const a2dp_vendor_info_t *info = blob;
268 if (size <= sizeof(*info))
269 return;
270 const void *data = info + 1;
271 size_t data_size = size - sizeof(*info);
272 printf("<hex:%s> {\n", bintohex(blob, size));
273 printf_vendor(info);
274 printf(""
275 " data:%zu = hex:%s\n"
276 "}\n",
277 data_size * 8,
278 bintohex(data, data_size));
279 }
280
281 static void printf_aptx(const a2dp_aptx_t *aptx) {
282 printf(""
283 " channel-mode:4 =%s%s%s\n"
284 " sampling-frequency:4 =%s%s%s%s\n",
285 aptx->channel_mode & APTX_CHANNEL_MODE_STEREO ? " Stereo" : "",
286 aptx->channel_mode & APTX_CHANNEL_MODE_TWS ? " DualChannel" : "",
287 aptx->channel_mode & APTX_CHANNEL_MODE_MONO ? " Mono" : "",
288 aptx->sampling_freq & APTX_SAMPLING_FREQ_48000 ? " 48000" : "",
289 aptx->sampling_freq & APTX_SAMPLING_FREQ_44100 ? " 44100" : "",
290 aptx->sampling_freq & APTX_SAMPLING_FREQ_32000 ? " 32000" : "",
291 aptx->sampling_freq & APTX_SAMPLING_FREQ_16000 ? " 16000" : "");
292 }
293
294 static void dump_aptx(const void *blob, size_t size) {
295 const a2dp_aptx_t *aptx = blob;
296 if (check_blob_size(sizeof(*aptx), size) == -1)
297 return;
298 printf("aptX <hex:%s> {\n", bintohex(blob, size));
299 printf_vendor(&aptx->info);
300 printf_aptx(aptx);
301 printf("}\n");
302 }
303
304 static void dump_aptx_tws(const void *blob, size_t size) {
305 const a2dp_aptx_t *aptx = blob;
306 if (check_blob_size(sizeof(*aptx), size) == -1)
307 return;
308 printf("aptX-TWS <hex:%s> {\n", bintohex(blob, size));
309 printf_vendor(&aptx->info);
310 printf_aptx(aptx);
311 printf("}\n");
312 }
313
314 static void dump_aptx_hd(const void *blob, size_t size) {
315 const a2dp_aptx_hd_t *aptx_hd = blob;
316 if (check_blob_size(sizeof(*aptx_hd), size) == -1)
317 return;
318 printf("aptX HD <hex:%s> {\n", bintohex(blob, size));
319 printf_vendor(&aptx_hd->aptx.info);
320 printf_aptx(&aptx_hd->aptx);
321 printf(""
322 " <reserved>:32\n"
323 "}\n");
324 }
325
326 static void dump_aptx_ll(const void *blob, size_t size) {
327
328 const a2dp_aptx_ll_t *aptx_ll = blob;
329 const a2dp_aptx_ll_new_t *aptx_ll_new_caps = blob;
330
331 size_t conf_size = sizeof(*aptx_ll);
332 if (size >= sizeof(*aptx_ll) && aptx_ll->has_new_caps)
333 conf_size = sizeof(*aptx_ll_new_caps);
334 if (check_blob_size(conf_size, size) == -1)
335 return;
336
337 printf("aptX LL (Sprint) <hex:%s> {\n", bintohex(blob, size));
338 printf_vendor(&aptx_ll->aptx.info);
339 printf_aptx(&aptx_ll->aptx);
340 printf(""
341 " <reserved>:6\n"
342 " has-new-caps:1 = %s\n"
343 " bidirectional-link:1 = %s\n",
344 aptx_ll->has_new_caps ? "true" : "false",
345 aptx_ll->bidirect_link ? "true" : "false");
346
347 if (aptx_ll->has_new_caps)
348 printf(""
349 " <reserved>:8\n"
350 " target-codec-level:16 = %u\n"
351 " initial-codec-level:16 = %u\n"
352 " sra-max-rate:8 = %u\n"
353 " sra-avg-time:8 = %u\n"
354 " good-working-level:16 = %u\n",
355 A2DP_APTX_LL_GET_TARGET_CODEC_LEVEL(*aptx_ll_new_caps),
356 A2DP_APTX_LL_GET_INITIAL_CODEC_LEVEL(*aptx_ll_new_caps),
357 aptx_ll_new_caps->sra_max_rate,
358 aptx_ll_new_caps->sra_avg_time,
359 A2DP_APTX_LL_GET_GOOD_WORKING_LEVEL(*aptx_ll_new_caps));
360
361 printf("}\n");
362
363 }
364
365 static void dump_faststream(const void *blob, size_t size) {
366 const a2dp_faststream_t *faststream = blob;
367 if (check_blob_size(sizeof(*faststream), size) == -1)
368 return;
369 printf("FastStream <hex:%s> {\n", bintohex(blob, size));
370 printf_vendor(&faststream->info);
371 printf(""
372 " direction:8 =%s%s\n"
373 " sampling-frequency-voice:8 =%s\n"
374 " sampling-frequency-music:8 =%s%s\n"
375 "}\n",
376 faststream->direction & FASTSTREAM_DIRECTION_MUSIC ? " Music" : "",
377 faststream->direction & FASTSTREAM_DIRECTION_VOICE ? " Voice" : "",
378 faststream->sampling_freq_voice & FASTSTREAM_SAMPLING_FREQ_VOICE_16000 ? " 16000" : "",
379 faststream->sampling_freq_music & FASTSTREAM_SAMPLING_FREQ_MUSIC_48000 ? " 48000" : "",
380 faststream->sampling_freq_music & FASTSTREAM_SAMPLING_FREQ_MUSIC_44100 ? " 44100" : "");
381 }
382
383 static void dump_lc3plus(const void *blob, size_t size) {
384 const a2dp_lc3plus_t *lc3plus = blob;
385 if (check_blob_size(sizeof(*lc3plus), size) == -1)
386 return;
387 const uint16_t lc3plus_sampling_freq = A2DP_LC3PLUS_GET_SAMPLING_FREQ(*lc3plus);
388 printf("LC3plus <hex:%s> {\n", bintohex(blob, size));
389 printf_vendor(&lc3plus->info);
390 printf(""
391 " frame-duration:4 =%s%s%s\n"
392 " <reserved>:4\n"
393 " channel-mode:8 =%s%s\n"
394 " sampling-frequency:16 =%s%s\n"
395 "}\n",
396 lc3plus->frame_duration & LC3PLUS_FRAME_DURATION_025 ? " 2.5ms" : "",
397 lc3plus->frame_duration & LC3PLUS_FRAME_DURATION_050 ? " 5ms" : "",
398 lc3plus->frame_duration & LC3PLUS_FRAME_DURATION_100 ? " 10ms" : "",
399 lc3plus->channel_mode & LC3PLUS_CHANNEL_MODE_MONO ? " Mono" : "",
400 lc3plus->channel_mode & LC3PLUS_CHANNEL_MODE_STEREO ? " Stereo" : "",
401 lc3plus_sampling_freq & LC3PLUS_SAMPLING_FREQ_48000 ? " 48000" : "",
402 lc3plus_sampling_freq & LC3PLUS_SAMPLING_FREQ_96000 ? " 96000" : "");
403 }
404
405 static void dump_ldac(const void *blob, size_t size) {
406 const a2dp_ldac_t *ldac = blob;
407 if (check_blob_size(sizeof(*ldac), size) == -1)
408 return;
409 printf("LDAC <hex:%s> {\n", bintohex(blob, size));
410 printf_vendor(&ldac->info);
411 printf(""
412 " <reserved>:2\n"
413 " sampling-frequency:6 =%s%s%s%s%s%s\n"
414 " <reserved>:5\n"
415 " channel-mode:3 =%s%s%s\n"
416 "}\n",
417 ldac->sampling_freq & LDAC_SAMPLING_FREQ_192000 ? " 192000" : "",
418 ldac->sampling_freq & LDAC_SAMPLING_FREQ_176400 ? " 176400" : "",
419 ldac->sampling_freq & LDAC_SAMPLING_FREQ_96000 ? " 96000" : "",
420 ldac->sampling_freq & LDAC_SAMPLING_FREQ_88200 ? " 88200" : "",
421 ldac->sampling_freq & LDAC_SAMPLING_FREQ_48000 ? " 48000" : "",
422 ldac->sampling_freq & LDAC_SAMPLING_FREQ_44100 ? " 44100" : "",
423 ldac->channel_mode & LDAC_CHANNEL_MODE_STEREO ? " Stereo" : "",
424 ldac->channel_mode & LDAC_CHANNEL_MODE_DUAL ? " DualChannel" : "",
425 ldac->channel_mode & LDAC_CHANNEL_MODE_MONO ? " Mono" : "");
426 }
427
428 static int lhdc_get_max_bitrate(unsigned int value) {
429 switch (value) {
430 case LHDC_MAX_BITRATE_400K:
431 return 400;
432 case LHDC_MAX_BITRATE_500K:
433 return 500;
434 case LHDC_MAX_BITRATE_900K:
435 return 900;
436 default:
437 return -1;
438 }
439 }
440
441 static void dump_lhdc_v1(const void *blob, size_t size) {
442 const a2dp_lhdc_v1_t *lhdc = blob;
443 if (check_blob_size(sizeof(*lhdc), size) == -1)
444 return;
445 printf("LHDC v1 <hex:%s> {\n", bintohex(blob, size));
446 printf_vendor(&lhdc->info);
447 printf(""
448 " <reserved>:1\n"
449 " ch-separation:1 = %s\n"
450 " bit-depth:2 =%s%s\n"
451 " sampling-frequency:4 =%s%s%s%s\n"
452 "}\n",
453 lhdc->ch_separation ? "true" : "false",
454 lhdc->bit_depth & LHDC_BIT_DEPTH_24 ? " 24" : "",
455 lhdc->bit_depth & LHDC_BIT_DEPTH_16 ? " 16" : "",
456 lhdc->sampling_freq & LHDC_SAMPLING_FREQ_96000 ? " 96000" : "",
457 lhdc->sampling_freq & LHDC_SAMPLING_FREQ_88200 ? " 88200" : "",
458 lhdc->sampling_freq & LHDC_SAMPLING_FREQ_48000 ? " 48000" : "",
459 lhdc->sampling_freq & LHDC_SAMPLING_FREQ_44100 ? " 44100" : "");
460 }
461
462 static void dump_lhdc_v2(const void *blob, size_t size) {
463 const a2dp_lhdc_v2_t *lhdc = blob;
464 if (check_blob_size(sizeof(*lhdc), size) == -1)
465 return;
466 printf("LHDC v2 <hex:%s> {\n", bintohex(blob, size));
467 printf_vendor(&lhdc->info);
468 printf(""
469 " <reserved>:2\n"
470 " bit-depth:2 =%s%s\n"
471 " sampling-frequency:4 =%s%s%s%s\n"
472 " low-latency:1 = %s\n"
473 " max-bitrate:3 = %d\n"
474 " version:4 = %u\n"
475 " <reserved>:4\n"
476 " ch-split-mode:4 =%s%s%s\n"
477 "}\n",
478 lhdc->bit_depth & LHDC_BIT_DEPTH_24 ? " 24" : "",
479 lhdc->bit_depth & LHDC_BIT_DEPTH_16 ? " 16" : "",
480 lhdc->sampling_freq & LHDC_SAMPLING_FREQ_96000 ? " 96000" : "",
481 lhdc->sampling_freq & LHDC_SAMPLING_FREQ_88200 ? " 88200" : "",
482 lhdc->sampling_freq & LHDC_SAMPLING_FREQ_48000 ? " 48000" : "",
483 lhdc->sampling_freq & LHDC_SAMPLING_FREQ_44100 ? " 44100" : "",
484 lhdc->low_latency ? "true" : "false",
485 lhdc_get_max_bitrate(lhdc->max_bitrate),
486 lhdc->version,
487 lhdc->ch_split_mode & LHDC_CH_SPLIT_MODE_TWS_PLUS ? " TWS+" : "",
488 lhdc->ch_split_mode & LHDC_CH_SPLIT_MODE_TWS ? " TWS" : "",
489 lhdc->ch_split_mode & LHDC_CH_SPLIT_MODE_NONE ? " None" : "");
490 }
491
492 static void dump_lhdc_v3(const void *blob, size_t size) {
493 const a2dp_lhdc_v3_t *lhdc = blob;
494 if (check_blob_size(sizeof(*lhdc), size) == -1)
495 return;
496 printf("LHDC v3 <hex:%s> {\n", bintohex(blob, size));
497 printf_vendor(&lhdc->info);
498 printf(""
499 " ar:1 = %s\n"
500 " jas:1 = %s\n"
501 " bit-depth:2 =%s%s\n"
502 " sampling-frequency:4 =%s%s%s%s\n"
503 " llac:1 = %s\n"
504 " low-latency:1 = %s\n"
505 " max-bitrate:2 = %d\n"
506 " version:4 = %u\n"
507 " lhdc-v4:1 = %s\n"
508 " larc:1 = %s\n"
509 " min-bitrate:1 = %s\n"
510 " meta:1 = %s\n"
511 " ch-split-mode:4 =%s%s%s\n"
512 "}\n",
513 lhdc->ar ? "true" : "false",
514 lhdc->jas ? "true" : "false",
515 lhdc->bit_depth & LHDC_BIT_DEPTH_24 ? " 24" : "",
516 lhdc->bit_depth & LHDC_BIT_DEPTH_16 ? " 16" : "",
517 lhdc->sampling_freq & LHDC_SAMPLING_FREQ_96000 ? " 96000" : "",
518 lhdc->sampling_freq & LHDC_SAMPLING_FREQ_88200 ? " 88200" : "",
519 lhdc->sampling_freq & LHDC_SAMPLING_FREQ_48000 ? " 48000" : "",
520 lhdc->sampling_freq & LHDC_SAMPLING_FREQ_44100 ? " 44100" : "",
521 lhdc->llac ? "true" : "false",
522 lhdc->low_latency ? "true" : "false",
523 lhdc_get_max_bitrate(lhdc->max_bitrate),
524 lhdc->version,
525 lhdc->lhdc_v4 ? "true" : "false",
526 lhdc->larc ? "true" : "false",
527 lhdc->min_bitrate ? "true" : "false",
528 lhdc->meta ? "true" : "false",
529 lhdc->ch_split_mode & LHDC_CH_SPLIT_MODE_TWS_PLUS ? " TWS+" : "",
530 lhdc->ch_split_mode & LHDC_CH_SPLIT_MODE_TWS ? " TWS" : "",
531 lhdc->ch_split_mode & LHDC_CH_SPLIT_MODE_NONE ? " None" : "");
532 }
533
534 static void dump_lhdc_v5(const void *blob, size_t size) {
535 const a2dp_lhdc_v5_t *lhdc = blob;
536 if (check_blob_size(sizeof(*lhdc), size) == -1)
537 return;
538 printf("LHDC v5 <hex:%s> {\n", bintohex(blob, size));
539 printf_vendor(&lhdc->info);
540 printf(""
541 " data:%zu = hex:%s\n"
542 "}\n",
543 sizeof(*lhdc) - sizeof(lhdc->info),
544 bintohex(blob + sizeof(lhdc->info), sizeof(*lhdc) - sizeof(lhdc->info)));
545 }
546
547 static void dump_opus(const void *blob, size_t size) {
548 const a2dp_opus_t *opus = blob;
549 if (check_blob_size(sizeof(*opus), size) == -1)
550 return;
551 printf("Opus <hex:%s> {\n", bintohex(blob, size));
552 printf_vendor(&opus->info);
553 printf(""
554 " <reserved>:2\n"
555 " sampling-frequency:1 =%s\n"
556 " frame-duration:2 =%s%s\n"
557 " channel-mode:3 =%s%s%s\n"
558 "}\n",
559 opus->sampling_freq & OPUS_SAMPLING_FREQ_48000 ? " 48000" : "",
560 opus->frame_duration & OPUS_FRAME_DURATION_100 ? " 10ms" : "",
561 opus->frame_duration & OPUS_FRAME_DURATION_200 ? " 20ms" : "",
562 opus->channel_mode & OPUS_CHANNEL_MODE_STEREO ? " Stereo" : "",
563 opus->channel_mode & OPUS_CHANNEL_MODE_DUAL ? " DualChannel" : "",
564 opus->channel_mode & OPUS_CHANNEL_MODE_MONO ? " Mono" : "");
565 }
566
567 static void dump_opus_pw(const void *blob, size_t size) {
568 const a2dp_opus_pw_t *opus = blob;
569 if (check_blob_size(sizeof(*opus), size) == -1)
570 return;
571 printf("Opus (PipeWire) <hex:%s> {\n", bintohex(blob, size));
572 printf_vendor(&opus->info);
573 printf(""
574 " music-channels:8 = %u\n"
575 " music-coupled-streams:8 = %u\n"
576 " music-location32: = %#x\n"
577 " music-frame-duration:8 =%s%s%s%s%s\n"
578 " music-bitrate:16 = %u\n"
579 " voice-channels:8 = %u\n"
580 " voice-coupled-streams:8 = %u\n"
581 " voice-location32: = %#x\n"
582 " voice-frame-duration:8 =%s%s%s%s%s\n"
583 " voice-bitrate:16 = %u\n"
584 "}\n",
585 opus->music.channels,
586 opus->music.coupled_streams,
587 A2DP_OPUS_PW_GET_LOCATION(opus->music),
588 opus->music.frame_duration & OPUS_PW_FRAME_DURATION_025 ? " 2.5ms" : "",
589 opus->music.frame_duration & OPUS_PW_FRAME_DURATION_050 ? " 5ms" : "",
590 opus->music.frame_duration & OPUS_PW_FRAME_DURATION_100 ? " 10ms" : "",
591 opus->music.frame_duration & OPUS_PW_FRAME_DURATION_200 ? " 20ms" : "",
592 opus->music.frame_duration & OPUS_PW_FRAME_DURATION_400 ? " 40ms" : "",
593 A2DP_OPUS_PW_GET_BITRATE(opus->music) * 1024,
594 opus->voice.channels,
595 opus->voice.coupled_streams,
596 A2DP_OPUS_PW_GET_LOCATION(opus->voice),
597 opus->voice.frame_duration & OPUS_PW_FRAME_DURATION_025 ? " 2.5ms" : "",
598 opus->voice.frame_duration & OPUS_PW_FRAME_DURATION_050 ? " 5ms" : "",
599 opus->voice.frame_duration & OPUS_PW_FRAME_DURATION_100 ? " 10ms" : "",
600 opus->voice.frame_duration & OPUS_PW_FRAME_DURATION_200 ? " 20ms" : "",
601 opus->voice.frame_duration & OPUS_PW_FRAME_DURATION_400 ? " 40ms" : "",
602 A2DP_OPUS_PW_GET_BITRATE(opus->voice) * 1024);
603 }
604
605 static const struct {
606 uint32_t codec_id;
607 size_t blob_size;
608 void (*dump)(const void *, size_t);
609 } dumps[] = {
610 { A2DP_CODEC_SBC, sizeof(a2dp_sbc_t), dump_sbc },
611 { A2DP_CODEC_MPEG12, sizeof(a2dp_mpeg_t), dump_mpeg },
612 { A2DP_CODEC_MPEG24, sizeof(a2dp_aac_t), dump_aac },
613 { A2DP_CODEC_MPEGD, sizeof(a2dp_usac_t), dump_usac },
614 { A2DP_CODEC_ATRAC, sizeof(a2dp_atrac_t), dump_atrac },
615 { A2DP_CODEC_VENDOR_ID(APTX_VENDOR_ID, APTX_CODEC_ID),
616 sizeof(a2dp_aptx_t), dump_aptx },
617 { A2DP_CODEC_VENDOR_ID(APTX_TWS_VENDOR_ID, APTX_TWS_CODEC_ID),
618 sizeof(a2dp_aptx_t), dump_aptx_tws },
619 { A2DP_CODEC_VENDOR_ID(APTX_AD_VENDOR_ID, APTX_AD_CODEC_ID),
620 -1, dump_vendor },
621 { A2DP_CODEC_VENDOR_ID(APTX_HD_VENDOR_ID, APTX_HD_CODEC_ID),
622 sizeof(a2dp_aptx_hd_t), dump_aptx_hd },
623 { A2DP_CODEC_VENDOR_ID(APTX_LL_VENDOR_ID, APTX_LL_CODEC_ID),
624 sizeof(a2dp_aptx_ll_t), dump_aptx_ll },
625 { A2DP_CODEC_VENDOR_ID(APTX_LL_VENDOR_ID, APTX_LL_CODEC_ID),
626 sizeof(a2dp_aptx_ll_new_t), dump_aptx_ll },
627 { A2DP_CODEC_VENDOR_ID(FASTSTREAM_VENDOR_ID, FASTSTREAM_CODEC_ID),
628 sizeof(a2dp_faststream_t), dump_faststream },
629 { A2DP_CODEC_VENDOR_ID(LC3PLUS_VENDOR_ID, LC3PLUS_CODEC_ID),
630 sizeof(a2dp_lc3plus_t), dump_lc3plus },
631 { A2DP_CODEC_VENDOR_ID(LDAC_VENDOR_ID, LDAC_CODEC_ID),
632 sizeof(a2dp_ldac_t), dump_ldac },
633 { A2DP_CODEC_VENDOR_ID(LHDC_V1_VENDOR_ID, LHDC_V1_CODEC_ID),
634 sizeof(a2dp_lhdc_v1_t), dump_lhdc_v1 },
635 { A2DP_CODEC_VENDOR_ID(LHDC_V2_VENDOR_ID, LHDC_V2_CODEC_ID),
636 sizeof(a2dp_lhdc_v2_t), dump_lhdc_v2 },
637 { A2DP_CODEC_VENDOR_ID(LHDC_V3_VENDOR_ID, LHDC_V3_CODEC_ID),
638 sizeof(a2dp_lhdc_v3_t), dump_lhdc_v3 },
639 { A2DP_CODEC_VENDOR_ID(LHDC_V5_VENDOR_ID, LHDC_V5_CODEC_ID),
640 sizeof(a2dp_lhdc_v5_t), dump_lhdc_v5 },
641 { A2DP_CODEC_VENDOR_ID(LHDC_LL_VENDOR_ID, LHDC_LL_CODEC_ID),
642 -1, dump_vendor },
643 { A2DP_CODEC_VENDOR_ID(OPUS_VENDOR_ID, OPUS_CODEC_ID),
644 sizeof(a2dp_opus_t), dump_opus },
645 { A2DP_CODEC_VENDOR_ID(OPUS_PW_VENDOR_ID, OPUS_PW_CODEC_ID),
646 sizeof(a2dp_opus_pw_t), dump_opus_pw },
647 { A2DP_CODEC_VENDOR_ID(SAMSUNG_HD_VENDOR_ID, SAMSUNG_HD_CODEC_ID),
648 -1, dump_vendor },
649 { A2DP_CODEC_VENDOR_ID(SAMSUNG_SC_VENDOR_ID, SAMSUNG_SC_CODEC_ID),
650 -1, dump_vendor },
651 };
652
653 int dump(const char *config, bool detect) {
654
655 uint32_t codec_id = get_codec(config);
656 int rv = -1;
657
658 ssize_t blob_size;
659 if ((blob_size = get_codec_blob(config, NULL, 0)) == -1)
660 return -1;
661
662 void *blob = malloc(blob_size);
663 if (get_codec_blob(config, blob, blob_size) == -1)
664 goto final;
665
666 rv = 0;
667 for (size_t i = 0; i < ARRAYSIZE(dumps); i++)
668 if (dumps[i].codec_id == codec_id) {
669 dumps[i].dump(blob, blob_size);
670 goto final;
671 }
672
673 if (detect) {
674 for (size_t i = 0; i < ARRAYSIZE(dumps); i++)
675 if (dumps[i].blob_size == (size_t)blob_size)
676 dumps[i].dump(blob, blob_size);
677 dump_vendor(blob, blob_size);
678 goto final;
679 }
680
681 fprintf(stderr, "Couldn't detect codec type: %s\n", config);
682 rv = -1;
683
684 final:
685 free(blob);
686 return rv;
687 }
688
689 int main(int argc, char *argv[]) {
690
691 int opt;
692 const char *opts = "hVx";
693 const struct option longopts[] = {
694 { "help", no_argument, NULL, 'h' },
695 { "version", no_argument, NULL, 'V' },
696 { "auto-detect", no_argument, NULL, 'x' },
697 { 0, 0, 0, 0 },
698 };
699
700 int rv = EXIT_SUCCESS;
701 bool detect = false;
702
703 while ((opt = getopt_long(argc, argv, opts, longopts, NULL)) != -1)
704 switch (opt) {
705 case 'h' /* --help */ :
706 usage:
707 printf("Usage:\n"
708 " %s [OPTION]... <CONFIG>...\n"
709 "\nOptions:\n"
710 " -h, --help\t\tprint this help and exit\n"
711 " -V, --version\t\tprint version and exit\n"
712 " -x, --auto-detect\ttry to auto-detect codec\n"
713 "\nExamples:\n"
714 " %s sbc:ffff0235\n"
715 " %s aptx:4f0000000100ff\n",
716 argv[0], argv[0], argv[0]);
717 return EXIT_SUCCESS;
718
719 case 'V' /* --version */ :
720 printf("%s\n", PACKAGE_VERSION);
721 return EXIT_SUCCESS;
722
723 case 'x' /* --auto-detect */ :
724 detect = true;
725 break;
726
727 default:
728 fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
729 return EXIT_FAILURE;
730 }
731
732 if (argc - optind < 1)
733 goto usage;
734
735 for (int i = optind; i < argc; i++)
736 if (dump(argv[i], detect) == -1)
737 rv = EXIT_FAILURE;
738
739 return rv;
740 }