summaryrefslogtreecommitdiff
path: root/tools/rbspeex/rbspeexenc.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/rbspeex/rbspeexenc.c')
-rw-r--r--tools/rbspeex/rbspeexenc.c221
1 files changed, 3 insertions, 218 deletions
diff --git a/tools/rbspeex/rbspeexenc.c b/tools/rbspeex/rbspeexenc.c
index e919bc8990..c7ea6e429a 100644
--- a/tools/rbspeex/rbspeexenc.c
+++ b/tools/rbspeex/rbspeexenc.c
@@ -16,13 +16,13 @@
16 * 16 *
17 ***************************************************************************/ 17 ***************************************************************************/
18 18
19#include <speex/speex.h>
20#include <speex/speex_resampler.h>
21#include <stdio.h> 19#include <stdio.h>
22#include <stdlib.h> 20#include <stdlib.h>
23#include <string.h> 21#include <string.h>
24#include <stdbool.h> 22#include <stdbool.h>
25 23
24#include "rbspeex.h"
25
26#define USAGE_TEXT \ 26#define USAGE_TEXT \
27"Usage: rbspeexenc [options] infile outfile\n"\ 27"Usage: rbspeexenc [options] infile outfile\n"\
28"Options:\n"\ 28"Options:\n"\
@@ -35,222 +35,6 @@
35"to either 16 kHz by default, or 8 kHz if narrowband mode is enabled.\n"\ 35"to either 16 kHz by default, or 8 kHz if narrowband mode is enabled.\n"\
36"WARNING: This tool will create files that are only usable by Rockbox!\n" 36"WARNING: This tool will create files that are only usable by Rockbox!\n"
37 37
38/* Read an unaligned 32-bit little endian long from buffer. */
39unsigned int get_long_le(unsigned char *p)
40{
41 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
42}
43
44bool get_wave_metadata(FILE *fd, int *numchan, int *bps, int *sr, int *numsamples)
45{
46 unsigned char buf[1024];
47 unsigned long totalsamples = 0;
48 unsigned long channels = 0;
49 unsigned long bitspersample = 0;
50 unsigned long numbytes = 0;
51 size_t read_bytes;
52 int i;
53
54 if ((read_bytes = fread(buf, 1, 12, fd)) < 12)
55 return false;
56
57 if ((memcmp(buf, "RIFF",4) != 0) || (memcmp(&buf[8], "WAVE", 4) != 0))
58 return false;
59
60 /* iterate over WAVE chunks until 'data' chunk */
61 while (1) {
62 /* get chunk header */
63 if ((read_bytes = fread(buf, 1, 8, fd)) < 8)
64 return false;
65
66 /* chunkSize */
67 i = get_long_le(&buf[4]);
68
69 if (memcmp(buf, "fmt ", 4) == 0) {
70 /* get rest of chunk */
71 if ((read_bytes = fread(buf, 1, 16, fd)) < 16)
72 return false;
73
74 i -= 16;
75
76 channels = *numchan = buf[2] | (buf[3] << 8);
77 *sr = get_long_le(&buf[4]);
78 /* wBitsPerSample */
79 bitspersample = *bps = buf[14] | (buf[15] << 8);
80 } else if (memcmp(buf, "data", 4) == 0) {
81 numbytes = i;
82 break;
83 } else if (memcmp(buf, "fact", 4) == 0) {
84 /* dwSampleLength */
85 if (i >= 4) {
86 /* get rest of chunk */
87 if ((read_bytes = fread(buf, 1, 4, fd)) < 4)
88 return false;
89
90 i -= 4;
91 totalsamples = get_long_le(buf);
92 }
93 }
94
95 /* seek to next chunk (even chunk sizes must be padded) */
96 if (i & 0x01)
97 i++;
98
99 if (fseek(fd, i, SEEK_CUR) < 0)
100 return false;
101 }
102
103 if ((numbytes == 0) || (channels == 0))
104 return false;
105
106 if (totalsamples == 0) {
107 /* for PCM only */
108 totalsamples = numbytes/((((bitspersample - 1) / 8) + 1)*channels);
109 }
110 *numsamples = totalsamples;
111 return true;
112}
113
114/* We'll eat an entire WAV file here, and encode it with Speex, packing the
115 * bits as tightly as we can. Output is completely raw, with absolutely
116 * nothing to identify the contents. Files are left open, so remember to close
117 * them.
118 */
119bool encode_file(FILE *fin, FILE *fout, float quality, int complexity,
120 bool narrowband, float volume, char *errstr, size_t errlen)
121{
122 spx_int16_t *in = NULL, *inpos;
123 spx_int16_t enc_buf[640]; /* Max frame size */
124 char cbits[200];
125 void *st = NULL;
126 SpeexResamplerState *resampler = NULL;
127 SpeexBits bits;
128 int i, tmp, target_sr, numchan, bps, sr, numsamples, frame_size, lookahead;
129 int nbytes;
130 bool ret = true;
131
132 if (!get_wave_metadata(fin, &numchan, &bps, &sr, &numsamples)) {
133 snprintf(errstr, errlen, "invalid WAV file");
134 return false;
135 }
136 if (numchan != 1) {
137 snprintf(errstr, errlen, "input file must be mono");
138 return false;
139 }
140 if (bps != 16) {
141 snprintf(errstr, errlen, "samples must be 16 bit");
142 return false;
143 }
144
145 /* Allocate an encoder of specified type, defaults to wideband */
146 st = speex_encoder_init(narrowband ? &speex_nb_mode : &speex_wb_mode);
147 if (narrowband)
148 target_sr = 8000;
149 else
150 target_sr = 16000;
151 speex_bits_init(&bits);
152
153 /* VBR */
154 tmp = 1;
155 speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp);
156 /* Quality, 0-10 */
157 speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &quality);
158 /* Complexity, 0-10 */
159 speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &complexity);
160 speex_encoder_ctl(st, SPEEX_GET_FRAME_SIZE, &frame_size);
161 speex_encoder_ctl(st, SPEEX_GET_LOOKAHEAD, &lookahead);
162
163 /* Read input samples into a buffer */
164 in = calloc(numsamples + lookahead, sizeof(spx_int16_t));
165 if (in == NULL) {
166 snprintf(errstr, errlen, "could not allocate clip memory");
167 ret = false;
168 goto finish;
169 }
170 if (fread(in, 2, numsamples, fin) != numsamples) {
171 snprintf(errstr, errlen, "could not read input file data");
172 ret = false;
173 goto finish;
174 }
175
176 if (volume != 1.0f) {
177 for (i = 0; i < numsamples; ++i)
178 in[i] *= volume;
179 }
180
181 if (sr != target_sr) {
182 resampler = speex_resampler_init(1, sr, target_sr, 10, NULL);
183 speex_resampler_skip_zeros(resampler);
184 }
185
186 /* There will be 'lookahead' samples of zero at the end of the array, to
187 * make sure the Speex encoder is allowed to spit out all its data at clip
188 * end */
189 numsamples += lookahead;
190
191 inpos = in;
192 while (numsamples > 0) {
193 int samples = frame_size;
194
195 /* Check if we need to resample */
196 if (sr != target_sr) {
197 spx_uint32_t in_len = numsamples, out_len = frame_size;
198 double resample_factor = (double)sr/(double)target_sr;
199 /* Calculate how many input samples are needed for one full frame
200 * out, and add some, just in case. */
201 spx_uint32_t samples_in = frame_size*resample_factor + 50;
202
203 /* Limit this or resampler will try to allocate it all on stack */
204 if (in_len > samples_in)
205 in_len = samples_in;
206 speex_resampler_process_int(resampler, 0, inpos, &in_len,
207 enc_buf, &out_len);
208 inpos += in_len;
209 samples = out_len;
210 numsamples -= in_len;
211 } else {
212 if (samples > numsamples)
213 samples = numsamples;
214 memcpy(enc_buf, inpos, samples*2);
215 inpos += frame_size;
216 numsamples -= frame_size;
217 }
218 /* Pad out with zeros if we didn't fill all input */
219 memset(enc_buf + samples, 0, (frame_size - samples)*2);
220
221 if (speex_encode_int(st, enc_buf, &bits) < 0) {
222 snprintf(errstr, errlen, "encoder error");
223 ret = false;
224 goto finish;
225 }
226
227 /* Copy the bits to an array of char that can be written */
228 nbytes = speex_bits_write_whole_bytes(&bits, cbits, 200);
229
230 /* Write the compressed data */
231 if (fwrite(cbits, 1, nbytes, fout) != nbytes) {
232 snprintf(errstr, errlen, "could not write output data");
233 ret = false;
234 goto finish;
235 }
236 }
237 /* Squeeze out the last bits */
238 nbytes = speex_bits_write(&bits, cbits, 200);
239 if (fwrite(cbits, 1, nbytes, fout) != nbytes) {
240 snprintf(errstr, errlen, "could not write output data");
241 ret = false;
242 }
243
244finish:
245 if (st != NULL)
246 speex_encoder_destroy(st);
247 speex_bits_destroy(&bits);
248 if (resampler != NULL)
249 speex_resampler_destroy(resampler);
250 if (in != NULL)
251 free(in);
252 return ret;
253}
254 38
255int main(int argc, char **argv) 39int main(int argc, char **argv)
256{ 40{
@@ -308,3 +92,4 @@ int main(int argc, char **argv)
308 } 92 }
309 return 0; 93 return 0;
310} 94}
95