OSDN Git Service

Add more options to SBC encoder and decoder
authorMarcel Holtmann <marcel@holtmann.org>
Fri, 31 Oct 2008 23:14:46 +0000 (00:14 +0100)
committerMarcel Holtmann <marcel@holtmann.org>
Fri, 31 Oct 2008 23:14:46 +0000 (00:14 +0100)
sbc/sbcdec.c
sbc/sbcenc.c

index 2464c42..140f154 100644 (file)
@@ -41,6 +41,8 @@
 
 #define BUF_SIZE 8192
 
+static int verbose = 0;
+
 static void decode(char *filename, char *output, int tofile)
 {
        unsigned char buf[BUF_SIZE], *stream;
@@ -120,7 +122,16 @@ static void decode(char *filename, char *output, int tofile)
                frequency = 0;
        }
 
-       printf("%d Hz, %d channels\n", frequency, channels);
+       if (verbose) {
+               fprintf(stderr,"decoding %s with rate %d, %d subbands, "
+                       "%d bits, allocation method %s and mode %s\n",
+                       filename, frequency, sbc.subbands * 4 + 4, sbc.bitpool,
+                       sbc.allocation == SBC_AM_SNR ? "SNR" : "LOUDNESS",
+                       sbc.mode == SBC_MODE_MONO ? "MONO" :
+                                       sbc.mode == SBC_MODE_STEREO ?
+                                               "STEREO" : "JOINTSTEREO");
+       }
+
        if (tofile) {
                struct au_header au_hdr;
 
@@ -139,25 +150,25 @@ static void decode(char *filename, char *output, int tofile)
        } else {
                if (ioctl(ad, SNDCTL_DSP_SETFMT, &format) < 0) {
                        fprintf(stderr, "Can't set audio format on %s: %s\n",
-                                       output, strerror(errno));
+                                               output, strerror(errno));
                        goto close;
                }
 
                if (ioctl(ad, SNDCTL_DSP_CHANNELS, &channels) < 0) {
-                       fprintf(stderr,
-                               "Can't set number of channels on %s: %s\n",
-                               output, strerror(errno));
+                       fprintf(stderr, "Can't set number of channels on %s: %s\n",
+                                               output, strerror(errno));
                        goto close;
                }
 
                if (ioctl(ad, SNDCTL_DSP_SPEED, &frequency) < 0) {
                        fprintf(stderr, "Can't set audio rate on %s: %s\n",
-                                       output, strerror(errno));
+                                               output, strerror(errno));
                        goto close;
                }
        }
 
        count = len;
+
        while (framelen > 0) {
                /* we have completed an sbc_decode at this point sbc.len is the
                 * length of the frame we just decoded count is the number of
@@ -233,9 +244,10 @@ static struct option main_options[] = {
 int main(int argc, char *argv[])
 {
        char *output = NULL;
-       int i, opt, verbose = 0, tofile = 0;
+       int i, opt, tofile = 0;
 
-       while ((opt = getopt_long(argc, argv, "+hvd:f:", main_options, NULL)) != -1) {
+       while ((opt = getopt_long(argc, argv, "+hvd:f:",
+                                               main_options, NULL)) != -1) {
                switch(opt) {
                case 'h':
                        usage();
index 38670a9..74a3434 100644 (file)
@@ -38,6 +38,8 @@
 #include "sbc.h"
 #include "formats.h"
 
+static int verbose = 0;
+
 static ssize_t __read(int fd, void *buf, size_t count)
 {
        ssize_t len, pos = 0;
@@ -70,12 +72,13 @@ static ssize_t __write(int fd, const void *buf, size_t count)
        return pos;
 }
 
-static void encode(char *filename, int subbands, int bitpool, int joint)
+static void encode(char *filename, int subbands,
+                                       int bitpool, int joint, int snr)
 {
        struct au_header *au_hdr;
        unsigned char input[2048], output[2048];
        sbc_t sbc;
-       int fd, len, size, count, encoded;
+       int fd, len, size, count, encoded, srate;
 
        if (strcmp(filename, "-")) {
                fd = open(filename, O_RDONLY);
@@ -124,6 +127,8 @@ static void encode(char *filename, int subbands, int bitpool, int joint)
                break;
        }
 
+       srate = BE_INT(au_hdr->sample_rate);
+
        sbc.subbands = subbands == 4 ? SBC_SB_4 : SBC_SB_8;
 
        if (BE_INT(au_hdr->channels) == 1)
@@ -139,6 +144,17 @@ static void encode(char *filename, int subbands, int bitpool, int joint)
        memmove(input, input + BE_INT(au_hdr->hdr_size), size);
 
        sbc.bitpool = bitpool;
+       sbc.allocation = snr ? SBC_AM_SNR : SBC_AM_LOUDNESS;
+
+       if(verbose) {
+               fprintf(stderr,"encoding %s with rate %d, %d subbands, "
+                       "%d bits, allocation method %s and mode %s\n",
+                       filename, srate, subbands, bitpool,
+                       sbc.allocation == SBC_AM_SNR ? "SNR" : "LOUDNESS",
+                       sbc.mode == SBC_MODE_MONO ? "MONO" :
+                                       sbc.mode == SBC_MODE_STEREO ?
+                                               "STEREO" : "JOINTSTEREO");
+       }
 
        while (1) {
                if (size < sizeof(input)) {
@@ -193,6 +209,7 @@ static void usage(void)
                "\t-s, --subbands       Number of subbands to use (4 or 8)\n"
                "\t-b, --bitpool        Bitpool value (default is 32)\n"
                "\t-j, --joint          Joint stereo\n"
+               "\t-S, --snr            Use SNR mode (default is loudness)\n"
                "\n");
 }
 
@@ -202,14 +219,16 @@ static struct option main_options[] = {
        { "subbands",   1, 0, 's' },
        { "bitpool",    1, 0, 'b' },
        { "joint",      0, 0, 'j' },
+       { "snr",        0, 0, 'S' },
        { 0, 0, 0, 0 }
 };
 
 int main(int argc, char *argv[])
 {
-       int i, opt, verbose = 0, subbands = 8, bitpool = 32, joint = 0;
+       int i, opt, subbands = 8, bitpool = 32, joint = 0, snr= 0;
 
-       while ((opt = getopt_long(argc, argv, "+hvs:b:j", main_options, NULL)) != -1) {
+       while ((opt = getopt_long(argc, argv, "+hvs:b:jS",
+                                               main_options, NULL)) != -1) {
                switch(opt) {
                case 'h':
                        usage();
@@ -236,6 +255,10 @@ int main(int argc, char *argv[])
                        joint = 1;
                        break;
 
+               case 'S':
+                       snr = 1;
+                       break;
+
                default:
                        exit(1);
                }
@@ -251,7 +274,7 @@ int main(int argc, char *argv[])
        }
 
        for (i = 0; i < argc; i++)
-               encode(argv[i], subbands, bitpool, joint);
+               encode(argv[i], subbands, bitpool, joint, snr);
 
        return 0;
 }