3 * Bluetooth low-complexity, subband codec (SBC) encoder
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
42 static int verbose = 0;
44 #define BUF_SIZE 32768
45 static unsigned char input[BUF_SIZE], output[BUF_SIZE + BUF_SIZE / 4];
47 static void encode(char *filename, int subbands, int bitpool, int joint,
48 int dualchannel, int snr, int blocks)
50 struct au_header au_hdr;
52 int fd, size, srate, codesize, nframes;
56 if (sizeof(au_hdr) != 24) {
57 /* Sanity check just in case */
58 fprintf(stderr, "FIXME: sizeof(au_hdr) != 24\n");
62 if (strcmp(filename, "-")) {
63 fd = open(filename, O_RDONLY);
65 fprintf(stderr, "Can't open file %s: %s\n",
66 filename, strerror(errno));
72 len = read(fd, &au_hdr, sizeof(au_hdr));
73 if (len < (ssize_t) sizeof(au_hdr)) {
74 if (fd > fileno(stderr))
75 fprintf(stderr, "Can't read header from file %s: %s\n",
76 filename, strerror(errno));
78 perror("Can't read audio header");
82 if (au_hdr.magic != AU_MAGIC ||
83 BE_INT(au_hdr.hdr_size) > 128 ||
84 BE_INT(au_hdr.hdr_size) < sizeof(au_hdr) ||
85 BE_INT(au_hdr.encoding) != AU_FMT_LIN16) {
86 fprintf(stderr, "Not in Sun/NeXT audio S16_BE format\n");
92 switch (BE_INT(au_hdr.sample_rate)) {
94 sbc.frequency = SBC_FREQ_16000;
97 sbc.frequency = SBC_FREQ_32000;
100 sbc.frequency = SBC_FREQ_44100;
103 sbc.frequency = SBC_FREQ_48000;
107 srate = BE_INT(au_hdr.sample_rate);
109 sbc.subbands = subbands == 4 ? SBC_SB_4 : SBC_SB_8;
111 if (BE_INT(au_hdr.channels) == 1) {
112 sbc.mode = SBC_MODE_MONO;
113 if (joint || dualchannel) {
114 fprintf(stderr, "Audio is mono but joint or "
115 "dualchannel mode has been specified\n");
118 } else if (joint && !dualchannel)
119 sbc.mode = SBC_MODE_JOINT_STEREO;
120 else if (!joint && dualchannel)
121 sbc.mode = SBC_MODE_DUAL_CHANNEL;
122 else if (!joint && !dualchannel)
123 sbc.mode = SBC_MODE_STEREO;
125 fprintf(stderr, "Both joint and dualchannel mode have been "
131 /* Skip extra bytes of the header if any */
132 if (read(fd, input, BE_INT(au_hdr.hdr_size) - len) < 0)
135 sbc.bitpool = bitpool;
136 sbc.allocation = snr ? SBC_AM_SNR : SBC_AM_LOUDNESS;
137 sbc.blocks = blocks == 4 ? SBC_BLK_4 :
138 blocks == 8 ? SBC_BLK_8 :
139 blocks == 12 ? SBC_BLK_12 : SBC_BLK_16;
142 fprintf(stderr, "encoding %s with rate %d, %d blocks, "
143 "%d subbands, %d bits, allocation method %s, "
145 filename, srate, blocks, subbands, bitpool,
146 sbc.allocation == SBC_AM_SNR ? "SNR" : "LOUDNESS",
147 sbc.mode == SBC_MODE_MONO ? "MONO" :
148 sbc.mode == SBC_MODE_STEREO ?
149 "STEREO" : "JOINTSTEREO");
152 codesize = sbc_get_codesize(&sbc);
153 nframes = sizeof(input) / codesize;
155 unsigned char *inp, *outp;
156 /* read data for up to 'nframes' frames of input data */
157 size = read(fd, input, codesize * nframes);
159 /* Something really bad happened */
160 perror("Can't read audio data");
163 if (size < codesize) {
164 /* Not enough data for encoding even a single frame */
167 /* encode all the data from the input buffer in a loop */
170 while (size >= codesize) {
171 len = sbc_encode(&sbc, inp, codesize,
172 outp, sizeof(output) - (outp - output),
174 if (len != codesize || encoded <= 0) {
176 "sbc_encode fail, len=%zd, encoded=%lu\n",
177 len, (unsigned long) encoded);
184 len = write(fileno(stdout), output, outp - output);
185 if (len != outp - output) {
186 perror("Can't write SBC output");
191 * sbc_encode failure has been detected earlier or end
192 * of file reached (have trailing partial data which is
193 * insufficient to encode SBC frame)
202 if (fd > fileno(stderr))
206 static void usage(void)
208 printf("SBC encoder utility ver %s\n", VERSION);
209 printf("Copyright (c) 2004-2010 Marcel Holtmann\n\n");
212 "\tsbcenc [options] file(s)\n"
216 "\t-h, --help Display help\n"
217 "\t-v, --verbose Verbose mode\n"
218 "\t-s, --subbands Number of subbands to use (4 or 8)\n"
219 "\t-b, --bitpool Bitpool value (default is 32)\n"
220 "\t-j, --joint Joint stereo\n"
221 "\t-d, --dualchannel Dual channel\n"
222 "\t-S, --snr Use SNR mode (default is loudness)\n"
223 "\t-B, --blocks Number of blocks (4, 8, 12 or 16)\n"
227 static struct option main_options[] = {
228 { "help", 0, 0, 'h' },
229 { "verbose", 0, 0, 'v' },
230 { "subbands", 1, 0, 's' },
231 { "bitpool", 1, 0, 'b' },
232 { "joint", 0, 0, 'j' },
233 { "dualchannel",0, 0, 'd' },
234 { "snr", 0, 0, 'S' },
235 { "blocks", 1, 0, 'B' },
239 int main(int argc, char *argv[])
241 int i, opt, subbands = 8, bitpool = 32, joint = 0, dualchannel = 0;
242 int snr = 0, blocks = 16;
244 while ((opt = getopt_long(argc, argv, "+hvs:b:jdSB:",
245 main_options, NULL)) != -1) {
256 subbands = atoi(optarg);
257 if (subbands != 8 && subbands != 4) {
258 fprintf(stderr, "Invalid subbands\n");
264 bitpool = atoi(optarg);
280 blocks = atoi(optarg);
281 if (blocks != 16 && blocks != 12 &&
282 blocks != 8 && blocks != 4) {
283 fprintf(stderr, "Invalid blocks\n");
303 for (i = 0; i < argc; i++)
304 encode(argv[i], subbands, bitpool, joint, dualchannel,