OSDN Git Service

dabdff4baf8990153538e16236002a33344069d9
[android-x86/external-bluetooth-sbc.git] / src / sbcenc.c
1 /*
2  *
3  *  Bluetooth low-complexity, subband codec (SBC) encoder
4  *
5  *  Copyright (C) 2008-2010  Nokia Corporation
6  *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
7  *  Copyright (C) 2012-2013  Intel Corporation
8  *
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23  *
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include <stdio.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <stdbool.h>
36 #include <stdint.h>
37 #include <string.h>
38 #include <getopt.h>
39 #include <sys/stat.h>
40
41 #include "sbc/sbc.h"
42 #include "formats.h"
43
44 static int verbose = 0;
45
46 #define BUF_SIZE 32768
47 static unsigned char input[BUF_SIZE], output[BUF_SIZE + BUF_SIZE / 4];
48
49 static void encode(char *filename, int subbands, int bitpool, int joint,
50                                 int dualchannel, int snr, int blocks, bool msbc)
51 {
52         struct au_header au_hdr;
53         sbc_t sbc;
54         int fd, size, srate, codesize, nframes;
55         ssize_t encoded;
56         ssize_t len;
57
58         if (sizeof(au_hdr) != 24) {
59                 /* Sanity check just in case */
60                 fprintf(stderr, "FIXME: sizeof(au_hdr) != 24\n");
61                 return;
62         }
63
64         if (strcmp(filename, "-")) {
65                 fd = open(filename, O_RDONLY);
66                 if (fd < 0) {
67                         fprintf(stderr, "Can't open file %s: %s\n",
68                                                 filename, strerror(errno));
69                         return;
70                 }
71         } else
72                 fd = fileno(stdin);
73
74         len = read(fd, &au_hdr, sizeof(au_hdr));
75         if (len < (ssize_t) sizeof(au_hdr)) {
76                 if (fd > fileno(stderr))
77                         fprintf(stderr, "Can't read header from file %s: %s\n",
78                                                 filename, strerror(errno));
79                 else
80                         perror("Can't read audio header");
81                 goto done;
82         }
83
84         if (au_hdr.magic != AU_MAGIC ||
85                         BE_INT(au_hdr.hdr_size) > 128 ||
86                         BE_INT(au_hdr.hdr_size) < sizeof(au_hdr) ||
87                         BE_INT(au_hdr.encoding) != AU_FMT_LIN16) {
88                 fprintf(stderr, "Not in Sun/NeXT audio S16_BE format\n");
89                 goto done;
90         }
91
92         if (!msbc) {
93                 sbc_init(&sbc, 0L);
94
95                 switch (BE_INT(au_hdr.sample_rate)) {
96                 case 16000:
97                         sbc.frequency = SBC_FREQ_16000;
98                         break;
99                 case 32000:
100                         sbc.frequency = SBC_FREQ_32000;
101                         break;
102                 case 44100:
103                         sbc.frequency = SBC_FREQ_44100;
104                         break;
105                 case 48000:
106                         sbc.frequency = SBC_FREQ_48000;
107                         break;
108                 }
109
110                 srate = BE_INT(au_hdr.sample_rate);
111
112                 sbc.subbands = subbands == 4 ? SBC_SB_4 : SBC_SB_8;
113
114                 if (BE_INT(au_hdr.channels) == 1) {
115                         sbc.mode = SBC_MODE_MONO;
116                         if (joint || dualchannel) {
117                                 fprintf(stderr, "Audio is mono but joint or "
118                                         "dualchannel mode has been specified\n");
119                                 goto done;
120                         }
121                 } else if (joint && !dualchannel)
122                         sbc.mode = SBC_MODE_JOINT_STEREO;
123                 else if (!joint && dualchannel)
124                         sbc.mode = SBC_MODE_DUAL_CHANNEL;
125                 else if (!joint && !dualchannel)
126                         sbc.mode = SBC_MODE_STEREO;
127                 else {
128                         fprintf(stderr, "Both joint and dualchannel "
129                                                 "mode have been specified\n");
130                         goto done;
131                 }
132
133                 sbc.endian = SBC_BE;
134                 sbc.bitpool = bitpool;
135                 sbc.allocation = snr ? SBC_AM_SNR : SBC_AM_LOUDNESS;
136                 sbc.blocks = blocks == 4 ? SBC_BLK_4 :
137                                 blocks == 8 ? SBC_BLK_8 :
138                                         blocks == 12 ? SBC_BLK_12 : SBC_BLK_16;
139         } else {
140                 if (BE_INT(au_hdr.sample_rate) != 16000 ||
141                                 BE_INT(au_hdr.channels) != 1 ||
142                                 BE_INT(au_hdr.channels) != 1) {
143                         fprintf(stderr, "mSBC requires 16 bits, 16kHz, mono "
144                                                                 "input\n");
145                         goto done;
146                 }
147
148                 sbc_init_msbc(&sbc, 0);
149                 sbc.endian = SBC_BE;
150         }
151
152         /* Skip extra bytes of the header if any */
153         if (read(fd, input, BE_INT(au_hdr.hdr_size) - len) < 0)
154                 goto done;
155
156         if (verbose) {
157                 fprintf(stderr, "encoding %s with rate %d, %d blocks, "
158                         "%d subbands, %d bits, allocation method %s, "
159                                                         "and mode %s\n",
160                         filename, srate, blocks, subbands, bitpool,
161                         sbc.allocation == SBC_AM_SNR ? "SNR" : "LOUDNESS",
162                         sbc.mode == SBC_MODE_MONO ? "MONO" :
163                                         sbc.mode == SBC_MODE_STEREO ?
164                                                 "STEREO" : "JOINTSTEREO");
165         }
166
167         codesize = sbc_get_codesize(&sbc);
168         nframes = sizeof(input) / codesize;
169         while (1) {
170                 unsigned char *inp, *outp;
171                 /* read data for up to 'nframes' frames of input data */
172                 size = read(fd, input, codesize * nframes);
173                 if (size < 0) {
174                         /* Something really bad happened */
175                         perror("Can't read audio data");
176                         break;
177                 }
178                 if (size < codesize) {
179                         /* Not enough data for encoding even a single frame */
180                         break;
181                 }
182                 /* encode all the data from the input buffer in a loop */
183                 inp = input;
184                 outp = output;
185                 while (size >= codesize) {
186                         len = sbc_encode(&sbc, inp, codesize,
187                                 outp, sizeof(output) - (outp - output),
188                                 &encoded);
189                         if (len != codesize || encoded <= 0) {
190                                 fprintf(stderr,
191                                         "sbc_encode fail, len=%zd, encoded=%lu\n",
192                                         len, (unsigned long) encoded);
193                                 break;
194                         }
195                         size -= len;
196                         inp += len;
197                         outp += encoded;
198                 }
199                 len = write(fileno(stdout), output, outp - output);
200                 if (len != outp - output) {
201                         perror("Can't write SBC output");
202                         break;
203                 }
204                 if (size != 0) {
205                         /*
206                          * sbc_encode failure has been detected earlier or end
207                          * of file reached (have trailing partial data which is
208                          * insufficient to encode SBC frame)
209                          */
210                         break;
211                 }
212         }
213
214         sbc_finish(&sbc);
215
216 done:
217         if (fd > fileno(stderr))
218                 close(fd);
219 }
220
221 static void usage(void)
222 {
223         printf("SBC encoder utility ver %s\n", VERSION);
224         printf("Copyright (c) 2004-2010  Marcel Holtmann\n\n");
225
226         printf("Usage:\n"
227                 "\tsbcenc [options] file(s)\n"
228                 "\n");
229
230         printf("Options:\n"
231                 "\t-h, --help           Display help\n"
232                 "\t-v, --verbose        Verbose mode\n"
233                 "\t-m, --msbc           mSBC codec\n"
234                 "\t-s, --subbands       Number of subbands to use (4 or 8)\n"
235                 "\t-b, --bitpool        Bitpool value (default is 32)\n"
236                 "\t-j, --joint          Joint stereo\n"
237                 "\t-d, --dualchannel    Dual channel\n"
238                 "\t-S, --snr            Use SNR mode (default is loudness)\n"
239                 "\t-B, --blocks         Number of blocks (4, 8, 12 or 16)\n"
240                 "\n");
241 }
242
243 static struct option main_options[] = {
244         { "help",       0, 0, 'h' },
245         { "verbose",    0, 0, 'v' },
246         { "msbc",       0, 0, 'm' },
247         { "subbands",   1, 0, 's' },
248         { "bitpool",    1, 0, 'b' },
249         { "joint",      0, 0, 'j' },
250         { "dualchannel",0, 0, 'd' },
251         { "snr",        0, 0, 'S' },
252         { "blocks",     1, 0, 'B' },
253         { 0, 0, 0, 0 }
254 };
255
256 int main(int argc, char *argv[])
257 {
258         int i, opt, subbands = 8, bitpool = 32, joint = 0, dualchannel = 0;
259         int snr = 0, blocks = 16;
260         bool msbc = false;
261
262         while ((opt = getopt_long(argc, argv, "+hmvs:b:jdSB:",
263                                                 main_options, NULL)) != -1) {
264                 switch(opt) {
265                 case 'h':
266                         usage();
267                         exit(0);
268
269                 case 'v':
270                         verbose = 1;
271                         break;
272
273                 case 'm':
274                         msbc = true;
275                         break;
276
277                 case 's':
278                         subbands = atoi(optarg);
279                         if (subbands != 8 && subbands != 4) {
280                                 fprintf(stderr, "Invalid subbands\n");
281                                 exit(1);
282                         }
283                         break;
284
285                 case 'b':
286                         bitpool = atoi(optarg);
287                         break;
288
289                 case 'j':
290                         joint = 1;
291                         break;
292
293                 case 'd':
294                         dualchannel = 1;
295                         break;
296
297                 case 'S':
298                         snr = 1;
299                         break;
300
301                 case 'B':
302                         blocks = atoi(optarg);
303                         if (blocks != 16 && blocks != 12 &&
304                                                 blocks != 8 && blocks != 4) {
305                                 fprintf(stderr, "Invalid blocks\n");
306                                 exit(1);
307                         }
308                         break;
309
310                 default:
311                         usage();
312                         exit(1);
313                 }
314         }
315
316         argc -= optind;
317         argv += optind;
318         optind = 0;
319
320         if (argc < 1) {
321                 usage();
322                 exit(1);
323         }
324
325         for (i = 0; i < argc; i++)
326                 encode(argv[i], subbands, bitpool, joint, dualchannel,
327                                                         snr, blocks, msbc);
328
329         return 0;
330 }