3 * Bluetooth low-complexity, subband codec (SBC) library
5 * Copyright (C) 2008-2010 Nokia Corporation
6 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38 #if __BYTE_ORDER == __LITTLE_ENDIAN
39 struct sbc_frame_hdr {
40 uint8_t syncword:8; /* Sync word */
41 uint8_t subbands:1; /* Subbands */
42 uint8_t allocation_method:1; /* Allocation method */
43 uint8_t channel_mode:2; /* Channel mode */
44 uint8_t blocks:2; /* Blocks */
45 uint8_t sampling_frequency:2; /* Sampling frequency */
46 uint8_t bitpool:8; /* Bitpool */
47 uint8_t crc_check:8; /* CRC check */
48 } __attribute__ ((packed));
49 #elif __BYTE_ORDER == __BIG_ENDIAN
50 struct sbc_frame_hdr {
51 uint8_t syncword:8; /* Sync word */
52 uint8_t sampling_frequency:2; /* Sampling frequency */
53 uint8_t blocks:2; /* Blocks */
54 uint8_t channel_mode:2; /* Channel mode */
55 uint8_t allocation_method:1; /* Allocation method */
56 uint8_t subbands:1; /* Subbands */
57 uint8_t bitpool:8; /* Bitpool */
58 uint8_t crc_check:8; /* CRC check */
59 } __attribute__ ((packed));
61 #error "Unknown byte order"
64 static int calc_frame_len(struct sbc_frame_hdr *hdr)
66 int tmp, nrof_subbands, nrof_blocks;
68 nrof_subbands = (hdr->subbands + 1) * 4;
69 nrof_blocks = (hdr->blocks + 1) * 4;
71 switch (hdr->channel_mode) {
74 tmp = nrof_blocks * hdr->bitpool;
77 tmp = nrof_blocks * hdr->bitpool * 2;
80 tmp = nrof_blocks * hdr->bitpool;
83 tmp = nrof_blocks * hdr->bitpool + nrof_subbands;
89 return (nrof_subbands + ((tmp + 7) / 8));
92 static double calc_bit_rate(struct sbc_frame_hdr *hdr)
94 int nrof_subbands, nrof_blocks;
97 nrof_subbands = (hdr->subbands + 1) * 4;
98 nrof_blocks = (hdr->blocks + 1) * 4;
100 switch (hdr->sampling_frequency) {
117 return ((8 * (calc_frame_len(hdr) + 4) * f) /
118 (nrof_subbands * nrof_blocks));
121 static char *freq2str(uint8_t freq)
137 static char *mode2str(uint8_t mode)
143 return "Dual Channel";
147 return "Joint Stereo";
153 static ssize_t __read(int fd, void *buf, size_t count)
155 ssize_t len, pos = 0;
158 len = read(fd, buf + pos, count);
171 static int analyze_file(char *filename)
173 struct sbc_frame_hdr hdr;
174 unsigned char buf[64];
176 int bitpool[SIZE], frame_len[SIZE];
177 int subbands, blocks, freq, method;
178 int n, p1, p2, fd, size, num;
182 if (strcmp(filename, "-")) {
183 printf("Filename\t\t%s\n", basename(filename));
185 fd = open(filename, O_RDONLY);
187 perror("Can't open file");
193 len = __read(fd, &hdr, sizeof(hdr));
194 if (len != sizeof(hdr) || hdr.syncword != 0x9c) {
195 fprintf(stderr, "Not a SBC audio file\n");
199 subbands = (hdr.subbands + 1) * 4;
200 blocks = (hdr.blocks + 1) * 4;
201 freq = hdr.sampling_frequency;
202 method = hdr.allocation_method;
204 count = calc_frame_len(&hdr);
206 bitpool[0] = hdr.bitpool;
207 frame_len[0] = count + 4;
209 for (n = 1; n < SIZE; n++) {
214 if (lseek(fd, 0, SEEK_SET) < 0) {
216 rate = calc_bit_rate(&hdr);
218 size = count > sizeof(buf) ? sizeof(buf) : count;
219 len = __read(fd, buf, size);
230 len = __read(fd, &hdr, sizeof(hdr));
232 fprintf(stderr, "Unable to read frame header"
233 " (error %d)\n", errno);
240 if ((size_t) len < sizeof(hdr) || hdr.syncword != 0x9c) {
241 fprintf(stderr, "Corrupted SBC stream "
242 "(len %zd syncword 0x%02x)\n",
247 count = calc_frame_len(&hdr);
252 for (n = 0; n < SIZE; n++) {
253 if (p1 < 0 && (bitpool[n] == 0 || bitpool[n] == hdr.bitpool))
255 if (p2 < 0 && (frame_len[n] == 0 || frame_len[n] == len))
259 bitpool[p1] = hdr.bitpool;
264 size = count > sizeof(buf) ? sizeof(buf) : count;
266 len = __read(fd, buf, size);
268 fprintf(stderr, "Unable to read frame data "
269 "(error %d)\n", errno);
276 rate += calc_bit_rate(&hdr);
280 printf("Subbands\t\t%d\n", subbands);
281 printf("Block length\t\t%d\n", blocks);
282 printf("Sampling frequency\t%s\n", freq2str(freq));
283 printf("Channel mode\t\t%s\n", mode2str(hdr.channel_mode));
284 printf("Allocation method\t%s\n", method ? "SNR" : "Loudness");
285 printf("Bitpool\t\t\t%d", bitpool[0]);
286 for (n = 1; n < SIZE; n++)
288 printf(", %d", bitpool[n]);
290 printf("Number of frames\t%d\n", num);
291 printf("Frame length\t\t%d", frame_len[0]);
292 for (n = 1; n < SIZE; n++)
293 if (frame_len[n] > 0)
294 printf(", %d", frame_len[n]);
297 printf("Bit rate\t\t%.3f kbps\n", rate / num);
299 if (fd > fileno(stderr))
307 int main(int argc, char *argv[])
312 fprintf(stderr, "Usage: sbcinfo <file>\n");
316 for (i = 0; i < argc - 1; i++)
317 if (analyze_file(argv[i + 1]) < 0)