3 * Bluetooth low-complexity, subband codec (SBC) decoder
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
37 #include <sys/ioctl.h>
38 #include <sys/soundcard.h>
45 static int verbose = 0;
47 static void decode(char *filename, char *output, int tofile)
49 unsigned char buf[BUF_SIZE], *stream;
52 int fd, ad, pos, streamlen, framelen, count;
54 int format = AFMT_S16_BE, frequency, channels;
57 if (stat(filename, &st) < 0) {
58 fprintf(stderr, "Can't get size of file %s: %s\n",
59 filename, strerror(errno));
63 stream = malloc(st.st_size);
66 fprintf(stderr, "Can't allocate memory for %s: %s\n",
67 filename, strerror(errno));
71 fd = open(filename, O_RDONLY);
73 fprintf(stderr, "Can't open file %s: %s\n",
74 filename, strerror(errno));
78 if (read(fd, stream, st.st_size) != st.st_size) {
79 fprintf(stderr, "Can't read content of %s: %s\n",
80 filename, strerror(errno));
88 streamlen = st.st_size;
91 ad = open(output, O_WRONLY | O_CREAT | O_TRUNC, 0644);
93 ad = open(output, O_WRONLY, 0);
96 fprintf(stderr, "Can't open output %s: %s\n",
97 output, strerror(errno));
104 framelen = sbc_decode(&sbc, stream, streamlen, buf, sizeof(buf), &len);
105 channels = sbc.mode == SBC_MODE_MONO ? 1 : 2;
106 switch (sbc.frequency) {
127 fprintf(stderr,"decoding %s with rate %d, %d subbands, "
128 "%d bits, allocation method %s and mode %s\n",
129 filename, frequency, sbc.subbands * 4 + 4, sbc.bitpool,
130 sbc.allocation == SBC_AM_SNR ? "SNR" : "LOUDNESS",
131 sbc.mode == SBC_MODE_MONO ? "MONO" :
132 sbc.mode == SBC_MODE_STEREO ?
133 "STEREO" : "JOINTSTEREO");
137 struct au_header au_hdr;
139 au_hdr.magic = AU_MAGIC;
140 au_hdr.hdr_size = BE_INT(24);
141 au_hdr.data_size = BE_INT(0);
142 au_hdr.encoding = BE_INT(AU_FMT_LIN16);
143 au_hdr.sample_rate = BE_INT(frequency);
144 au_hdr.channels = BE_INT(channels);
146 written = write(ad, &au_hdr, sizeof(au_hdr));
147 if (written < (ssize_t) sizeof(au_hdr)) {
148 fprintf(stderr, "Failed to write header\n");
152 if (ioctl(ad, SNDCTL_DSP_SETFMT, &format) < 0) {
153 fprintf(stderr, "Can't set audio format on %s: %s\n",
154 output, strerror(errno));
158 if (ioctl(ad, SNDCTL_DSP_CHANNELS, &channels) < 0) {
159 fprintf(stderr, "Can't set number of channels on %s: %s\n",
160 output, strerror(errno));
164 if (ioctl(ad, SNDCTL_DSP_SPEED, &frequency) < 0) {
165 fprintf(stderr, "Can't set audio rate on %s: %s\n",
166 output, strerror(errno));
173 while (framelen > 0) {
174 /* we have completed an sbc_decode at this point sbc.len is the
175 * length of the frame we just decoded count is the number of
176 * decoded bytes yet to be written */
178 if (count + len >= BUF_SIZE) {
179 /* buffer is too full to stuff decoded audio in so it
180 * must be written to the device */
181 written = write(ad, buf, count);
187 if (count + len >= BUF_SIZE) {
189 "buffer size of %d is too small for decoded"
190 " data (%lu)\n", BUF_SIZE, (unsigned long) (len + count));
194 /* push the pointer in the file forward to the next bit to be
195 * decoded tell the decoder to decode up to the remaining
196 * length of the file (!) */
198 framelen = sbc_decode(&sbc, stream + pos, streamlen - pos,
199 buf + count, sizeof(buf) - count, &len);
201 /* increase the count */
206 written = write(ad, buf, count);
220 static void usage(void)
222 printf("SBC decoder utility ver %s\n", VERSION);
223 printf("Copyright (c) 2004-2010 Marcel Holtmann\n\n");
226 "\tsbcdec [options] file(s)\n"
230 "\t-h, --help Display help\n"
231 "\t-v, --verbose Verbose mode\n"
232 "\t-d, --device <dsp> Sound device\n"
233 "\t-f, --file <file> Decode to a file\n"
237 static struct option main_options[] = {
238 { "help", 0, 0, 'h' },
239 { "device", 1, 0, 'd' },
240 { "verbose", 0, 0, 'v' },
241 { "file", 1, 0, 'f' },
245 int main(int argc, char *argv[])
248 int i, opt, tofile = 0;
250 while ((opt = getopt_long(argc, argv, "+hvd:f:",
251 main_options, NULL)) != -1) {
263 output = strdup(optarg);
269 output = strdup(optarg);
287 for (i = 0; i < argc; i++)
288 decode(argv[i], output ? output : "/dev/dsp", tofile);