Home | History | Annotate | Download | only in sbc
      1 /*
      2  *
      3  *  Bluetooth low-complexity, subband codec (SBC) decoder
      4  *
      5  *  Copyright (C) 2004-2009  Marcel Holtmann <marcel (at) holtmann.org>
      6  *
      7  *
      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.
     12  *
     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.
     17  *
     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
     21  *
     22  */
     23 
     24 #ifdef HAVE_CONFIG_H
     25 #include <config.h>
     26 #endif
     27 
     28 #include <stdio.h>
     29 #include <errno.h>
     30 #include <fcntl.h>
     31 #include <unistd.h>
     32 #include <stdlib.h>
     33 #include <string.h>
     34 #include <getopt.h>
     35 #include <sys/stat.h>
     36 #include <sys/ioctl.h>
     37 #include <sys/soundcard.h>
     38 
     39 #include "sbc.h"
     40 #include "formats.h"
     41 
     42 #define BUF_SIZE 8192
     43 
     44 static int verbose = 0;
     45 
     46 static void decode(char *filename, char *output, int tofile)
     47 {
     48 	unsigned char buf[BUF_SIZE], *stream;
     49 	struct stat st;
     50 	sbc_t sbc;
     51 	int fd, ad, pos, streamlen, framelen, count;
     52 	size_t len;
     53 	int format = AFMT_S16_BE, frequency, channels;
     54 	ssize_t written;
     55 
     56 	if (stat(filename, &st) < 0) {
     57 		fprintf(stderr, "Can't get size of file %s: %s\n",
     58 						filename, strerror(errno));
     59 		return;
     60 	}
     61 
     62 	stream = malloc(st.st_size);
     63 
     64 	if (!stream) {
     65 		fprintf(stderr, "Can't allocate memory for %s: %s\n",
     66 						filename, strerror(errno));
     67 		return;
     68 	}
     69 
     70 	fd = open(filename, O_RDONLY);
     71 	if (fd < 0) {
     72 		fprintf(stderr, "Can't open file %s: %s\n",
     73 						filename, strerror(errno));
     74 		goto free;
     75 	}
     76 
     77 	if (read(fd, stream, st.st_size) != st.st_size) {
     78 		fprintf(stderr, "Can't read content of %s: %s\n",
     79 						filename, strerror(errno));
     80 		close(fd);
     81 		goto free;
     82 	}
     83 
     84 	close(fd);
     85 
     86 	pos = 0;
     87 	streamlen = st.st_size;
     88 
     89 	if (tofile)
     90 		ad = open(output, O_WRONLY | O_CREAT | O_TRUNC, 0644);
     91 	else
     92 		ad = open(output, O_WRONLY, 0);
     93 
     94 	if (ad < 0) {
     95 		fprintf(stderr, "Can't open output %s: %s\n",
     96 						output, strerror(errno));
     97 		goto free;
     98 	}
     99 
    100 	sbc_init(&sbc, 0L);
    101 	sbc.endian = SBC_BE;
    102 
    103 	framelen = sbc_decode(&sbc, stream, streamlen, buf, sizeof(buf), &len);
    104 	channels = sbc.mode == SBC_MODE_MONO ? 1 : 2;
    105 	switch (sbc.frequency) {
    106 	case SBC_FREQ_16000:
    107 		frequency = 16000;
    108 		break;
    109 
    110 	case SBC_FREQ_32000:
    111 		frequency = 32000;
    112 		break;
    113 
    114 	case SBC_FREQ_44100:
    115 		frequency = 44100;
    116 		break;
    117 
    118 	case SBC_FREQ_48000:
    119 		frequency = 48000;
    120 		break;
    121 	default:
    122 		frequency = 0;
    123 	}
    124 
    125 	if (verbose) {
    126 		fprintf(stderr,"decoding %s with rate %d, %d subbands, "
    127 			"%d bits, allocation method %s and mode %s\n",
    128 			filename, frequency, sbc.subbands * 4 + 4, sbc.bitpool,
    129 			sbc.allocation == SBC_AM_SNR ? "SNR" : "LOUDNESS",
    130 			sbc.mode == SBC_MODE_MONO ? "MONO" :
    131 					sbc.mode == SBC_MODE_STEREO ?
    132 						"STEREO" : "JOINTSTEREO");
    133 	}
    134 
    135 	if (tofile) {
    136 		struct au_header au_hdr;
    137 
    138 		au_hdr.magic       = AU_MAGIC;
    139 		au_hdr.hdr_size    = BE_INT(24);
    140 		au_hdr.data_size   = BE_INT(0);
    141 		au_hdr.encoding    = BE_INT(AU_FMT_LIN16);
    142 		au_hdr.sample_rate = BE_INT(frequency);
    143 		au_hdr.channels    = BE_INT(channels);
    144 
    145 		written = write(ad, &au_hdr, sizeof(au_hdr));
    146 		if (written < (ssize_t) sizeof(au_hdr)) {
    147 			fprintf(stderr, "Failed to write header\n");
    148 			goto close;
    149 		}
    150 	} else {
    151 		if (ioctl(ad, SNDCTL_DSP_SETFMT, &format) < 0) {
    152 			fprintf(stderr, "Can't set audio format on %s: %s\n",
    153 						output, strerror(errno));
    154 			goto close;
    155 		}
    156 
    157 		if (ioctl(ad, SNDCTL_DSP_CHANNELS, &channels) < 0) {
    158 			fprintf(stderr, "Can't set number of channels on %s: %s\n",
    159 						output, strerror(errno));
    160 			goto close;
    161 		}
    162 
    163 		if (ioctl(ad, SNDCTL_DSP_SPEED, &frequency) < 0) {
    164 			fprintf(stderr, "Can't set audio rate on %s: %s\n",
    165 						output, strerror(errno));
    166 			goto close;
    167 		}
    168 	}
    169 
    170 	count = len;
    171 
    172 	while (framelen > 0) {
    173 		/* we have completed an sbc_decode at this point sbc.len is the
    174 		 * length of the frame we just decoded count is the number of
    175 		 * decoded bytes yet to be written */
    176 
    177 		if (count + len >= BUF_SIZE) {
    178 			/* buffer is too full to stuff decoded audio in so it
    179 			 * must be written to the device */
    180 			written = write(ad, buf, count);
    181 			if (written > 0)
    182 				count -= written;
    183 		}
    184 
    185 		/* sanity check */
    186 		if (count + len >= BUF_SIZE) {
    187 			fprintf(stderr,
    188 				"buffer size of %d is too small for decoded"
    189 				" data (%lu)\n", BUF_SIZE, (unsigned long) (len + count));
    190 			exit(1);
    191 		}
    192 
    193 		/* push the pointer in the file forward to the next bit to be
    194 		 * decoded tell the decoder to decode up to the remaining
    195 		 * length of the file (!) */
    196 		pos += framelen;
    197 		framelen = sbc_decode(&sbc, stream + pos, streamlen - pos,
    198 					buf + count, sizeof(buf) - count, &len);
    199 
    200 		/* increase the count */
    201 		count += len;
    202 	}
    203 
    204 	if (count > 0) {
    205 		written = write(ad, buf, count);
    206 		if (written > 0)
    207 			count -= written;
    208 	}
    209 
    210 close:
    211 	sbc_finish(&sbc);
    212 
    213 	close(ad);
    214 
    215 free:
    216 	free(stream);
    217 }
    218 
    219 static void usage(void)
    220 {
    221 	printf("SBC decoder utility ver %s\n", VERSION);
    222 	printf("Copyright (c) 2004-2009  Marcel Holtmann\n\n");
    223 
    224 	printf("Usage:\n"
    225 		"\tsbcdec [options] file(s)\n"
    226 		"\n");
    227 
    228 	printf("Options:\n"
    229 		"\t-h, --help           Display help\n"
    230 		"\t-v, --verbose        Verbose mode\n"
    231 		"\t-d, --device <dsp>   Sound device\n"
    232 		"\t-f, --file <file>    Decode to a file\n"
    233 		"\n");
    234 }
    235 
    236 static struct option main_options[] = {
    237 	{ "help",	0, 0, 'h' },
    238 	{ "device",	1, 0, 'd' },
    239 	{ "verbose",	0, 0, 'v' },
    240 	{ "file",	1, 0, 'f' },
    241 	{ 0, 0, 0, 0 }
    242 };
    243 
    244 int main(int argc, char *argv[])
    245 {
    246 	char *output = NULL;
    247 	int i, opt, tofile = 0;
    248 
    249 	while ((opt = getopt_long(argc, argv, "+hvd:f:",
    250 						main_options, NULL)) != -1) {
    251 		switch(opt) {
    252 		case 'h':
    253 			usage();
    254 			exit(0);
    255 
    256 		case 'v':
    257 			verbose = 1;
    258 			break;
    259 
    260 		case 'd':
    261 			if (output)
    262 				free(output);
    263 			output = strdup(optarg);
    264 			tofile = 0;
    265 			break;
    266 
    267 		case 'f' :
    268 			if (output)
    269 				free(output);
    270 			output = strdup(optarg);
    271 			tofile = 1;
    272 			break;
    273 
    274 		default:
    275 			exit(1);
    276 		}
    277 	}
    278 
    279 	argc -= optind;
    280 	argv += optind;
    281 	optind = 0;
    282 
    283 	if (argc < 1) {
    284 		usage();
    285 		exit(1);
    286 	}
    287 
    288 	for (i = 0; i < argc; i++)
    289 		decode(argv[i], output ? output : "/dev/dsp", tofile);
    290 
    291 	if (output)
    292 		free(output);
    293 
    294 	return 0;
    295 }
    296