1/*
2 *
3 *  Bluetooth low-complexity, subband codec (SBC) decoder
4 *
5 *  Copyright (C) 2008-2010  Nokia Corporation
6 *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
7 *
8 *
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.
13 *
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.
18 *
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
22 *
23 */
24
25#ifdef HAVE_CONFIG_H
26#include <config.h>
27#endif
28
29#include <stdio.h>
30#include <errno.h>
31#include <fcntl.h>
32#include <unistd.h>
33#include <stdlib.h>
34#include <string.h>
35#include <getopt.h>
36#include <sys/stat.h>
37#include <sys/ioctl.h>
38#include <sys/soundcard.h>
39
40#include "sbc.h"
41#include "formats.h"
42
43#define BUF_SIZE 8192
44
45static int verbose = 0;
46
47static void decode(char *filename, char *output, int tofile)
48{
49	unsigned char buf[BUF_SIZE], *stream;
50	struct stat st;
51	sbc_t sbc;
52	int fd, ad, pos, streamlen, framelen, count;
53	size_t len;
54	int format = AFMT_S16_BE, frequency, channels;
55	ssize_t written;
56
57	if (stat(filename, &st) < 0) {
58		fprintf(stderr, "Can't get size of file %s: %s\n",
59						filename, strerror(errno));
60		return;
61	}
62
63	stream = malloc(st.st_size);
64
65	if (!stream) {
66		fprintf(stderr, "Can't allocate memory for %s: %s\n",
67						filename, strerror(errno));
68		return;
69	}
70
71	fd = open(filename, O_RDONLY);
72	if (fd < 0) {
73		fprintf(stderr, "Can't open file %s: %s\n",
74						filename, strerror(errno));
75		goto free;
76	}
77
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));
81		close(fd);
82		goto free;
83	}
84
85	close(fd);
86
87	pos = 0;
88	streamlen = st.st_size;
89
90	if (tofile)
91		ad = open(output, O_WRONLY | O_CREAT | O_TRUNC, 0644);
92	else
93		ad = open(output, O_WRONLY, 0);
94
95	if (ad < 0) {
96		fprintf(stderr, "Can't open output %s: %s\n",
97						output, strerror(errno));
98		goto free;
99	}
100
101	sbc_init(&sbc, 0L);
102	sbc.endian = SBC_BE;
103
104	framelen = sbc_decode(&sbc, stream, streamlen, buf, sizeof(buf), &len);
105	channels = sbc.mode == SBC_MODE_MONO ? 1 : 2;
106	switch (sbc.frequency) {
107	case SBC_FREQ_16000:
108		frequency = 16000;
109		break;
110
111	case SBC_FREQ_32000:
112		frequency = 32000;
113		break;
114
115	case SBC_FREQ_44100:
116		frequency = 44100;
117		break;
118
119	case SBC_FREQ_48000:
120		frequency = 48000;
121		break;
122	default:
123		frequency = 0;
124	}
125
126	if (verbose) {
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");
134	}
135
136	if (tofile) {
137		struct au_header au_hdr;
138
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);
145
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");
149			goto close;
150		}
151	} else {
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));
155			goto close;
156		}
157
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));
161			goto close;
162		}
163
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));
167			goto close;
168		}
169	}
170
171	count = len;
172
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 */
177
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);
182			if (written > 0)
183				count -= written;
184		}
185
186		/* sanity check */
187		if (count + len >= BUF_SIZE) {
188			fprintf(stderr,
189				"buffer size of %d is too small for decoded"
190				" data (%lu)\n", BUF_SIZE, (unsigned long) (len + count));
191			exit(1);
192		}
193
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 (!) */
197		pos += framelen;
198		framelen = sbc_decode(&sbc, stream + pos, streamlen - pos,
199					buf + count, sizeof(buf) - count, &len);
200
201		/* increase the count */
202		count += len;
203	}
204
205	if (count > 0) {
206		written = write(ad, buf, count);
207		if (written > 0)
208			count -= written;
209	}
210
211close:
212	sbc_finish(&sbc);
213
214	close(ad);
215
216free:
217	free(stream);
218}
219
220static void usage(void)
221{
222	printf("SBC decoder utility ver %s\n", VERSION);
223	printf("Copyright (c) 2004-2010  Marcel Holtmann\n\n");
224
225	printf("Usage:\n"
226		"\tsbcdec [options] file(s)\n"
227		"\n");
228
229	printf("Options:\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"
234		"\n");
235}
236
237static struct option main_options[] = {
238	{ "help",	0, 0, 'h' },
239	{ "device",	1, 0, 'd' },
240	{ "verbose",	0, 0, 'v' },
241	{ "file",	1, 0, 'f' },
242	{ 0, 0, 0, 0 }
243};
244
245int main(int argc, char *argv[])
246{
247	char *output = NULL;
248	int i, opt, tofile = 0;
249
250	while ((opt = getopt_long(argc, argv, "+hvd:f:",
251						main_options, NULL)) != -1) {
252		switch(opt) {
253		case 'h':
254			usage();
255			exit(0);
256
257		case 'v':
258			verbose = 1;
259			break;
260
261		case 'd':
262			free(output);
263			output = strdup(optarg);
264			tofile = 0;
265			break;
266
267		case 'f' :
268			free(output);
269			output = strdup(optarg);
270			tofile = 1;
271			break;
272
273		default:
274			exit(1);
275		}
276	}
277
278	argc -= optind;
279	argv += optind;
280	optind = 0;
281
282	if (argc < 1) {
283		usage();
284		exit(1);
285	}
286
287	for (i = 0; i < argc; i++)
288		decode(argv[i], output ? output : "/dev/dsp", tofile);
289
290	free(output);
291
292	return 0;
293}
294