diff options
Diffstat (limited to 'tools/rbspeex/rbspeexdec.c')
-rw-r--r-- | tools/rbspeex/rbspeexdec.c | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/tools/rbspeex/rbspeexdec.c b/tools/rbspeex/rbspeexdec.c new file mode 100644 index 0000000000..90562f7309 --- /dev/null +++ b/tools/rbspeex/rbspeexdec.c | |||
@@ -0,0 +1,118 @@ | |||
1 | /************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * | ||
9 | * Copyright (C) 2007 Thom Johansen | ||
10 | * | ||
11 | * All files in this archive are subject to the GNU General Public License. | ||
12 | * See the file COPYING in the source tree root for full license agreement. | ||
13 | * | ||
14 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
15 | * KIND, either express or implied. | ||
16 | * | ||
17 | ***************************************************************************/ | ||
18 | |||
19 | #include <speex/speex.h> | ||
20 | #include <stdio.h> | ||
21 | #include <stdlib.h> | ||
22 | #include "string.h" | ||
23 | |||
24 | #define USAGE_TEXT \ | ||
25 | "Usage: rbspeexdec infile outfile\n"\ | ||
26 | "rbspeexdec outputs mono 16 bit 16 kHz WAV files.\n"\ | ||
27 | "WARNING: This tool will only decode files made with rbspeexenc!\n" | ||
28 | |||
29 | void put_ushort_le(unsigned short x, unsigned char *out) | ||
30 | { | ||
31 | out[0] = x & 0xff; | ||
32 | out[1] = x >> 8; | ||
33 | } | ||
34 | |||
35 | void put_uint_le(unsigned int x, unsigned char *out) | ||
36 | { | ||
37 | out[0] = x & 0xff; | ||
38 | out[1] = (x >> 8) & 0xff; | ||
39 | out[2] = (x >> 16) & 0xff; | ||
40 | out[3] = x >> 24; | ||
41 | } | ||
42 | |||
43 | int main(int argc, char **argv) | ||
44 | { | ||
45 | FILE *fin, *fout; | ||
46 | char *indata; | ||
47 | short out[640]; /* max frame size (UWB) */ | ||
48 | unsigned char wavhdr[44]; | ||
49 | int numbytes; | ||
50 | void *st; /* decoder state */ | ||
51 | SpeexBits bits; | ||
52 | int i, tmp, lookahead, frame_size; | ||
53 | unsigned int samples = 0; | ||
54 | long insize; | ||
55 | |||
56 | if (argc < 3) { | ||
57 | printf(USAGE_TEXT); | ||
58 | return 1; | ||
59 | } | ||
60 | |||
61 | /* Rockbox speex streams are always assumed to be WB */ | ||
62 | st = speex_decoder_init(&speex_wb_mode); | ||
63 | |||
64 | /* Set the perceptual enhancement on (is default, but doesn't hurt) */ | ||
65 | tmp = 1; | ||
66 | speex_decoder_ctl(st, SPEEX_SET_ENH, &tmp); | ||
67 | speex_decoder_ctl(st, SPEEX_GET_LOOKAHEAD, &lookahead); | ||
68 | speex_decoder_ctl(st, SPEEX_GET_FRAME_SIZE, &frame_size); | ||
69 | |||
70 | if ((fin = fopen(argv[1], "rb")) == NULL) { | ||
71 | printf("Error: could not open input file\n"); | ||
72 | return 1; | ||
73 | } | ||
74 | if ((fout = fopen(argv[2], "wb")) == NULL) { | ||
75 | printf("Error: could not open output file\n"); | ||
76 | return 1; | ||
77 | } | ||
78 | /* slurp infile */ | ||
79 | fseek(fin, 0, SEEK_END); | ||
80 | insize = ftell(fin); | ||
81 | fseek(fin, 0, SEEK_SET); | ||
82 | indata = malloc(insize); | ||
83 | fread(indata, 1, insize, fin); | ||
84 | fclose(fin); | ||
85 | |||
86 | /* fill in wav header */ | ||
87 | strcpy(wavhdr, "RIFF"); | ||
88 | strcpy(wavhdr + 8, "WAVEfmt "); | ||
89 | put_uint_le(16, wavhdr + 16); | ||
90 | put_ushort_le(1, wavhdr + 20); /* PCM data */ | ||
91 | put_ushort_le(1, wavhdr + 22); /* mono */ | ||
92 | put_uint_le(16000, wavhdr + 24); /* 16000 Hz */ | ||
93 | put_uint_le(16000*2, wavhdr + 28); /* chan*sr*bbs/8 */ | ||
94 | put_ushort_le(2, wavhdr + 32); /* chan*bps/8 */ | ||
95 | put_ushort_le(16, wavhdr + 34); /* bits per sample */ | ||
96 | strcpy(wavhdr + 36, "data"); | ||
97 | fwrite(wavhdr, 1, 44, fout); /* write header */ | ||
98 | /* make bit buffer use our own buffer */ | ||
99 | speex_bits_set_bit_buffer(&bits, indata, insize); | ||
100 | while (speex_decode_int(st, &bits, out) == 0) { | ||
101 | /* if no error, write decoded audio */ | ||
102 | fwrite(out + lookahead, sizeof(short), frame_size - lookahead, fout); | ||
103 | samples += frame_size - lookahead; | ||
104 | lookahead = 0; /* only skip samples at the start */ | ||
105 | } | ||
106 | speex_decoder_destroy(st); | ||
107 | /* now fill in the values in the wav header we didn't have at the start */ | ||
108 | fseek(fout, 4, SEEK_SET); | ||
109 | put_uint_le(36 + samples*2, wavhdr); /* header size + data size */ | ||
110 | fwrite(wavhdr, 1, 4, fout); | ||
111 | fseek(fout, 40, SEEK_SET); | ||
112 | put_uint_le(samples*2, wavhdr); /* data size */ | ||
113 | fwrite(wavhdr, 1, 4, fout); | ||
114 | fclose(fout); | ||
115 | free(indata); | ||
116 | return 0; | ||
117 | } | ||
118 | |||