3 * Bluetooth low-complexity, subband codec (SBC) encoder
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
41 #if __BYTE_ORDER == __LITTLE_ENDIAN
42 #define COMPOSE_ID(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
43 #define LE_SHORT(v) (v)
45 #define BE_SHORT(v) bswap_16(v)
46 #define BE_INT(v) bswap_32(v)
47 #elif __BYTE_ORDER == __BIG_ENDIAN
48 #define COMPOSE_ID(a,b,c,d) ((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
49 #define LE_SHORT(v) bswap_16(v)
50 #define LE_INT(v) bswap_32(v)
51 #define BE_SHORT(v) (v)
57 #define AU_MAGIC COMPOSE_ID('.','s','n','d')
61 #define AU_FMT_LIN16 3
64 uint32_t magic; /* '.snd' */
65 uint32_t hdr_size; /* size of header (min 24) */
66 uint32_t data_size; /* size of data */
67 uint32_t encoding; /* see to AU_FMT_XXXX */
68 uint32_t sample_rate; /* sample rate */
69 uint32_t channels; /* number of channels (voices) */
72 static ssize_t __read(int fd, void *buf, size_t count)
77 len = read(fd, buf + pos, count);
88 static ssize_t __write(int fd, const void *buf, size_t count)
93 len = write(fd, buf + pos, count);
104 static void encode(char *filename, int subbands, int bitpool, int joint)
106 struct au_header *au_hdr;
107 unsigned char input[2048], output[2048];
109 int fd, len, size, count, encoded;
111 if (strcmp(filename, "-")) {
112 fd = open(filename, O_RDONLY);
114 fprintf(stderr, "Can't open file %s: %s\n",
115 filename, strerror(errno));
121 len = __read(fd, input, sizeof(input));
122 if (len < sizeof(*au_hdr)) {
123 if (fd > fileno(stderr))
124 fprintf(stderr, "Can't read header from file %s: %s\n",
125 filename, strerror(errno));
127 perror("Can't read audio header");
131 au_hdr = (struct au_header *) input;
133 if (au_hdr->magic != AU_MAGIC ||
134 BE_INT(au_hdr->hdr_size) > 128 ||
135 BE_INT(au_hdr->hdr_size) < 24 ||
136 BE_INT(au_hdr->encoding) != AU_FMT_LIN16) {
137 fprintf(stderr, "Data is not in Sun/NeXT audio S16_BE format\n");
143 switch (BE_INT(au_hdr->sample_rate)) {
145 sbc.frequency = SBC_FREQ_16000;
148 sbc.frequency = SBC_FREQ_32000;
151 sbc.frequency = SBC_FREQ_44100;
154 sbc.frequency = SBC_FREQ_48000;
158 sbc.subbands = subbands == 4 ? SBC_SB_4 : SBC_SB_8;
160 if (BE_INT(au_hdr->channels) == 1)
161 sbc.mode = SBC_MODE_MONO;
163 sbc.mode = SBC_MODE_JOINT_STEREO;
165 sbc.mode = SBC_MODE_STEREO;
168 count = BE_INT(au_hdr->data_size);
169 size = len - BE_INT(au_hdr->hdr_size);
170 memmove(input, input + BE_INT(au_hdr->hdr_size), size);
172 sbc.bitpool = bitpool;
175 if (size < sizeof(input)) {
176 len = __read(fd, input + size, sizeof(input) - size);
177 if (len == 0 && size == 0)
181 perror("Can't read audio data");
188 len = sbc_encode(&sbc, input, size,
189 output, sizeof(output), &encoded);
191 memmove(input, input + len, size - len);
195 len = __write(fileno(stdout), output, encoded);
199 if (len < 0 || len != encoded) {
200 perror("Can't write SBC output");
208 if (fd > fileno(stderr))
212 static void usage(void)
214 printf("SBC encoder utility ver %s\n", VERSION);
215 printf("Copyright (c) 2004-2008 Marcel Holtmann\n\n");
218 "\tsbcenc [options] file(s)\n"
222 "\t-h, --help Display help\n"
223 "\t-v, --verbose Verbose mode\n"
224 "\t-s, --subbands Number of subbands to use (4 or 8)\n"
225 "\t-b, --bitpool Bitpool value (default is 32)\n"
226 "\t-j, --joint Joint stereo\n"
230 static struct option main_options[] = {
231 { "help", 0, 0, 'h' },
232 { "verbose", 0, 0, 'v' },
233 { "subbands", 1, 0, 's' },
234 { "bitpool", 1, 0, 'b' },
235 { "joint", 0, 0, 'j' },
239 int main(int argc, char *argv[])
241 int i, opt, verbose = 0, subbands = 8, bitpool = 32, joint = 0;
243 while ((opt = getopt_long(argc, argv, "+hvs:b:j", main_options, NULL)) != -1) {
254 subbands = atoi(optarg);
255 if (subbands != 8 && subbands != 4) {
256 fprintf(stderr, "Invalid subbands %d!\n",
263 bitpool = atoi(optarg);
284 for (i = 0; i < argc; i++)
285 encode(argv[i], subbands, bitpool, joint);