3 * Bluetooth low-complexity, subband codec (SBC) decoder
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
36 #include <sys/ioctl.h>
37 #include <sys/soundcard.h>
43 static void decode(char *filename, char *output, int tofile)
45 unsigned char buf[BUF_SIZE], *stream;
49 int fd, ad, pos, streamlen, framelen, count, written, len;
50 int format = AFMT_S16_BE, frequency, channels;
52 if (stat(filename, &st) < 0) {
53 fprintf(stderr, "Can't get size of file %s: %s\n",
54 filename, strerror(errno));
58 filesize = st.st_size;
59 stream = malloc(st.st_size);
62 fprintf(stderr, "Can't allocate memory for %s: %s\n",
63 filename, strerror(errno));
67 fd = open(filename, O_RDONLY);
69 fprintf(stderr, "Can't open file %s: %s\n",
70 filename, strerror(errno));
74 if (read(fd, stream, st.st_size) != st.st_size) {
75 fprintf(stderr, "Can't read content of %s: %s\n",
76 filename, strerror(errno));
84 streamlen = st.st_size;
87 ad = open(output, O_WRONLY | O_CREAT | O_TRUNC, 0644);
89 ad = open(output, O_WRONLY, 0);
92 fprintf(stderr, "Can't open output %s: %s\n",
93 output, strerror(errno));
100 framelen = sbc_decode(&sbc, stream, streamlen, buf, sizeof(buf), &len);
101 channels = sbc.mode == SBC_MODE_MONO ? 1 : 2;
102 switch (sbc.frequency) {
122 printf("%d Hz, %d channels\n", frequency, channels);
124 if (ioctl(ad, SNDCTL_DSP_SETFMT, &format) < 0) {
125 fprintf(stderr, "Can't set audio format on %s: %s\n",
126 output, strerror(errno));
129 if (ioctl(ad, SNDCTL_DSP_CHANNELS, &channels) < 0) {
131 "Can't set number of channels on %s: %s\n",
132 output, strerror(errno));
136 if (ioctl(ad, SNDCTL_DSP_SPEED, &frequency) < 0) {
137 fprintf(stderr, "Can't set audio rate on %s: %s\n",
138 output, strerror(errno));
144 while (framelen > 0) {
145 /* we have completed an sbc_decode at this point sbc.len is the
146 * length of the frame we just decoded count is the number of
147 * decoded bytes yet to be written */
149 if (count + len >= BUF_SIZE) {
150 /* buffer is too full to stuff decoded audio in so it
151 * must be written to the device */
152 written = write(ad, buf, count);
158 if (count + len >= BUF_SIZE) {
160 "buffer size of %d is too small for decoded"
161 " data (%d)\n", BUF_SIZE, len + count);
165 /* increase the count */
168 /* push the pointer in the file forward to the next bit to be
169 * decoded tell the decoder to decode up to the remaining
170 * length of the file (!) */
172 framelen = sbc_decode(&sbc, stream + pos, streamlen - pos,
173 buf + count, sizeof(buf) - count,
178 written = write(ad, buf, count);
192 static void usage(void)
194 printf("SBC decoder utility ver %s\n", VERSION);
195 printf("Copyright (c) 2004-2008 Marcel Holtmann\n\n");
198 "\tsbcdec [options] file(s)\n"
202 "\t-h, --help Display help\n"
203 "\t-v, --verbose Verbose mode\n"
204 "\t-d, --device <dsp> Sound device\n"
205 "\t-f, --file <file> Decode to a file\n"
209 static struct option main_options[] = {
210 { "help", 0, 0, 'h' },
211 { "device", 1, 0, 'd' },
212 { "verbose", 0, 0, 'v' },
213 { "file", 1, 0, 'f' },
217 int main(int argc, char *argv[])
220 int i, opt, verbose = 0, tofile = 0;
222 while ((opt = getopt_long(argc, argv, "+hvd:f:", main_options, NULL)) != -1) {
235 output = strdup(optarg);
242 output = strdup(optarg);
260 for (i = 0; i < argc; i++)
261 decode(argv[i], output ? output : "/dev/dsp", tofile);