3
* Bluetooth low-complexity, subband codec (SBC) library
5
* Copyright (C) 2004-2008 Marcel Holtmann <marcel@holtmann.org>
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37
#if __BYTE_ORDER == __LITTLE_ENDIAN
38
struct sbc_frame_hdr {
39
uint8_t syncword:8; /* Sync word */
40
uint8_t subbands:1; /* Subbands */
41
uint8_t allocation_method:1; /* Allocation method */
42
uint8_t channel_mode:2; /* Channel mode */
43
uint8_t blocks:2; /* Blocks */
44
uint8_t sampling_frequency:2; /* Sampling frequency */
45
uint8_t bitpool:8; /* Bitpool */
46
uint8_t crc_check:8; /* CRC check */
47
} __attribute__ ((packed));
48
#elif __BYTE_ORDER == __BIG_ENDIAN
49
struct sbc_frame_hdr {
50
uint8_t syncword:8; /* Sync word */
51
uint8_t sampling_frequency:2; /* Sampling frequency */
52
uint8_t blocks:2; /* Blocks */
53
uint8_t channel_mode:2; /* Channel mode */
54
uint8_t allocation_method:1; /* Allocation method */
55
uint8_t subbands:1; /* Subbands */
56
uint8_t bitpool:8; /* Bitpool */
57
uint8_t crc_check:8; /* CRC check */
58
} __attribute__ ((packed));
60
#error "Unknown byte order"
63
static int calc_frame_len(struct sbc_frame_hdr *hdr)
65
int tmp, nrof_subbands, nrof_blocks;
67
nrof_subbands = (hdr->subbands + 1) * 4;
68
nrof_blocks = (hdr->blocks + 1) * 4;
70
switch (hdr->channel_mode) {
73
tmp = nrof_blocks * hdr->bitpool;
76
tmp = nrof_blocks * hdr->bitpool * 2;
79
tmp = nrof_blocks * hdr->bitpool;
82
tmp = nrof_blocks * hdr->bitpool + nrof_subbands;
88
return (nrof_subbands + ((tmp + 7) / 8));
91
static double calc_bit_rate(struct sbc_frame_hdr *hdr)
93
int nrof_subbands, nrof_blocks;
96
nrof_subbands = (hdr->subbands + 1) * 4;
97
nrof_blocks = (hdr->blocks + 1) * 4;
99
switch (hdr->sampling_frequency) {
116
return ((8 * (calc_frame_len(hdr) + 4) * f) /
117
(nrof_subbands * nrof_blocks));
120
static char *freq2str(uint8_t freq)
136
static char *mode2str(uint8_t mode)
142
return "Dual Channel";
146
return "Joint Stereo";
152
static ssize_t __read(int fd, void *buf, size_t count)
154
ssize_t len, pos = 0;
157
len = read(fd, buf + pos, count);
170
static int analyze_file(char *filename)
172
struct sbc_frame_hdr hdr;
173
unsigned char buf[64];
175
int bitpool[SIZE], frame_len[SIZE];
176
int subbands, blocks, freq, mode, method;
177
int n, p1, p2, fd, len, size, count, num;
179
if (strcmp(filename, "-")) {
180
printf("Filename\t\t%s\n", basename(filename));
182
fd = open(filename, O_RDONLY);
184
perror("Can't open file");
190
len = __read(fd, &hdr, sizeof(hdr));
191
if (len != sizeof(hdr) || hdr.syncword != 0x9c) {
192
fprintf(stderr, "Not a SBC audio file\n");
196
subbands = (hdr.subbands + 1) * 4;
197
blocks = (hdr.blocks + 1) * 4;
198
freq = hdr.sampling_frequency;
199
mode = hdr.channel_mode;
200
method = hdr.allocation_method;
202
count = calc_frame_len(&hdr);
204
bitpool[0] = hdr.bitpool;
205
frame_len[0] = count + 4;
207
for (n = 1; n < SIZE; n++) {
212
if (lseek(fd, 0, SEEK_SET) < 0) {
214
rate = calc_bit_rate(&hdr);
216
size = count > sizeof(buf) ? sizeof(buf) : count;
217
len = __read(fd, buf, size);
228
len = __read(fd, &hdr, sizeof(hdr));
230
fprintf(stderr, "Unable to read frame header"
231
" (error %d)\n", errno);
238
if (len < sizeof(hdr) || hdr.syncword != 0x9c) {
239
fprintf(stderr, "Corrupted SBC stream "
240
"(len %d syncword 0x%02x)\n",
245
count = calc_frame_len(&hdr);
250
for (n = 0; n < SIZE; n++) {
251
if (p1 < 0 && (bitpool[n] == 0 || bitpool[n] == hdr.bitpool))
253
if (p2 < 0 && (frame_len[n] == 0 || frame_len[n] == len))
257
bitpool[p1] = hdr.bitpool;
262
size = count > sizeof(buf) ? sizeof(buf) : count;
264
len = __read(fd, buf, size);
266
fprintf(stderr, "Unable to read frame data "
267
"(error %d)\n", errno);
274
rate += calc_bit_rate(&hdr);
278
printf("Subbands\t\t%d\n", subbands);
279
printf("Block length\t\t%d\n", blocks);
280
printf("Sampling frequency\t%s\n", freq2str(freq));
281
printf("Channel mode\t\t%s\n", mode2str(hdr.channel_mode));
282
printf("Allocation method\t%s\n", method ? "SNR" : "Loudness");
283
printf("Bitpool\t\t\t%d", bitpool[0]);
284
for (n = 1; n < SIZE; n++)
286
printf(", %d", bitpool[n]);
288
printf("Number of frames\t%d\n", num);
289
printf("Frame length\t\t%d", frame_len[0]);
290
for (n = 1; n < SIZE; n++)
291
if (frame_len[n] > 0)
292
printf(", %d", frame_len[n]);
295
printf("Bit rate\t\t%.3f kbps\n", rate / num);
297
if (fd > fileno(stderr))
305
int main(int argc, char *argv[])
310
fprintf(stderr, "Usage: sbcinfo <file>\n");
314
for (i = 0; i < argc - 1; i++)
315
if (analyze_file(argv[i + 1]) < 0)