summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/wav.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/wav.c')
-rw-r--r--lib/rbcodec/codecs/wav.c437
1 files changed, 437 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/wav.c b/lib/rbcodec/codecs/wav.c
new file mode 100644
index 0000000000..d20331bc6c
--- /dev/null
+++ b/lib/rbcodec/codecs/wav.c
@@ -0,0 +1,437 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 Dave Chapman
11 * Copyright (C) 2009 Yoshihisa Uchida
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22
23#include "codeclib.h"
24#include "codecs/libpcm/support_formats.h"
25
26CODEC_HEADER
27
28/* WAVE (RIFF) codec:
29 *
30 * For a good documentation on WAVE files, see:
31 * http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/WAVE/WAVE.html
32 * and
33 * http://www.sonicspot.com/guide/wavefiles.html
34 *
35 * For sample WAV files, see:
36 * http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/WAVE/Samples.html
37 *
38 */
39
40#define PCM_SAMPLE_SIZE (4096*2)
41
42static int32_t samples[PCM_SAMPLE_SIZE] IBSS_ATTR;
43
44/* This codec support WAVE files with the following formats: */
45enum
46{
47 WAVE_FORMAT_UNKNOWN = 0x0000, /* Microsoft Unknown Wave Format */
48 WAVE_FORMAT_PCM = 0x0001, /* Microsoft PCM Format */
49 WAVE_FORMAT_ADPCM = 0x0002, /* Microsoft ADPCM Format */
50 WAVE_FORMAT_IEEE_FLOAT = 0x0003, /* IEEE Float */
51 WAVE_FORMAT_ALAW = 0x0006, /* Microsoft ALAW */
52 WAVE_FORMAT_MULAW = 0x0007, /* Microsoft MULAW */
53 WAVE_FORMAT_DVI_ADPCM = 0x0011, /* Intel's DVI ADPCM */
54 WAVE_FORMAT_DIALOGIC_OKI_ADPCM = 0x0017, /* Dialogic OKI ADPCM */
55 WAVE_FORMAT_YAMAHA_ADPCM = 0x0020, /* Yamaha ADPCM */
56 WAVE_FORMAT_XBOX_ADPCM = 0x0069, /* XBOX ADPCM */
57 IBM_FORMAT_MULAW = 0x0101, /* same as WAVE_FORMAT_MULAW */
58 IBM_FORMAT_ALAW = 0x0102, /* same as WAVE_FORMAT_ALAW */
59 WAVE_FORMAT_SWF_ADPCM = 0x5346, /* Adobe SWF ADPCM */
60 WAVE_FORMAT_EXTENSIBLE = 0xFFFE
61};
62
63static const struct pcm_entry wave_codecs[] = {
64 { WAVE_FORMAT_UNKNOWN, 0 },
65 { WAVE_FORMAT_PCM, get_linear_pcm_codec },
66 { WAVE_FORMAT_ADPCM, get_ms_adpcm_codec },
67 { WAVE_FORMAT_IEEE_FLOAT, get_ieee_float_codec },
68 { WAVE_FORMAT_ALAW, get_itut_g711_alaw_codec },
69 { WAVE_FORMAT_MULAW, get_itut_g711_mulaw_codec },
70 { WAVE_FORMAT_DVI_ADPCM, get_dvi_adpcm_codec },
71 { WAVE_FORMAT_DIALOGIC_OKI_ADPCM, get_dialogic_oki_adpcm_codec },
72 { WAVE_FORMAT_YAMAHA_ADPCM, get_yamaha_adpcm_codec },
73 { WAVE_FORMAT_XBOX_ADPCM, get_dvi_adpcm_codec },
74 { IBM_FORMAT_MULAW, get_itut_g711_mulaw_codec },
75 { IBM_FORMAT_ALAW, get_itut_g711_alaw_codec },
76 { WAVE_FORMAT_SWF_ADPCM, get_swf_adpcm_codec },
77};
78
79#define NUM_FORMATS 13
80
81static const struct pcm_codec *get_wave_codec(uint32_t formattag)
82{
83 int i;
84
85 for (i = 0; i < NUM_FORMATS; i++)
86 {
87 if (wave_codecs[i].format_tag == formattag)
88 {
89 if (wave_codecs[i].get_codec)
90 return wave_codecs[i].get_codec();
91 return 0;
92 }
93 }
94 return 0;
95}
96
97static struct pcm_format format;
98static uint32_t bytesdone;
99
100static bool set_msadpcm_coeffs(const uint8_t *buf)
101{
102 int i;
103 int num;
104 int size;
105
106 buf += 4; /* skip 'fmt ' */
107 size = buf[0] | (buf[1] << 8) | (buf[1] << 16) | (buf[1] << 24);
108 if (size < 50)
109 {
110 DEBUGF("CODEC_ERROR: microsoft adpcm 'fmt ' chunk size=%lu < 50\n",
111 (unsigned long)size);
112 return false;
113 }
114
115 /* get nNumCoef */
116 buf += 24;
117 num = buf[0] | (buf[1] << 8);
118
119 /*
120 * In many case, nNumCoef is 7.
121 * Depending upon the encoder, as for this value there is a possibility of
122 * increasing more.
123 * If you found the file where this value exceeds 7, please report.
124 */
125 if (num != MSADPCM_NUM_COEFF)
126 {
127 DEBUGF("CODEC_ERROR: microsoft adpcm nNumCoef=%d != 7\n", num);
128 return false;
129 }
130
131 /* get aCoeffs */
132 buf += 2;
133 for (i = 0; i < MSADPCM_NUM_COEFF; i++)
134 {
135 format.coeffs[i][0] = buf[0] | (SE(buf[1]) << 8);
136 format.coeffs[i][1] = buf[2] | (SE(buf[3]) << 8);
137 buf += 4;
138 }
139
140 return true;
141}
142
143static uint8_t *read_buffer(size_t *realsize)
144{
145 uint8_t *buffer = (uint8_t *)ci->request_buffer(realsize, format.chunksize);
146 if (bytesdone + (*realsize) > format.numbytes)
147 *realsize = format.numbytes - bytesdone;
148 bytesdone += *realsize;
149 ci->advance_buffer(*realsize);
150 return buffer;
151}
152
153/* this is the codec entry point */
154enum codec_status codec_main(enum codec_entry_call_reason reason)
155{
156 if (reason == CODEC_LOAD) {
157 /* Generic codec initialisation */
158 ci->configure(DSP_SET_SAMPLE_DEPTH, PCM_OUTPUT_DEPTH-1);
159 }
160
161 return CODEC_OK;
162}
163
164/* this is called for each file to process */
165enum codec_status codec_run(void)
166{
167 uint32_t decodedsamples;
168 size_t n;
169 int bufcount;
170 int endofstream;
171 unsigned char *buf;
172 uint8_t *wavbuf;
173 off_t firstblockposn; /* position of the first block in file */
174 const struct pcm_codec *codec;
175 uint32_t size;
176 intptr_t param;
177
178 if (codec_init()) {
179 DEBUGF("codec_init() error\n");
180 return CODEC_ERROR;
181 }
182
183 codec_set_replaygain(ci->id3);
184
185 /* Need to save offset for later use (cleared indirectly by advance_buffer) */
186 bytesdone = ci->id3->offset;
187
188 /* get RIFF chunk header */
189 ci->seek_buffer(0);
190 buf = ci->request_buffer(&n, 12);
191 if (n < 12) {
192 DEBUGF("request_buffer error\n");
193 return CODEC_ERROR;
194 }
195 if ((memcmp(buf, "RIFF", 4) != 0) || (memcmp(&buf[8], "WAVE", 4) != 0)) {
196 DEBUGF("CODEC_ERROR: missing riff header\n");
197 return CODEC_ERROR;
198 }
199
200 /* advance to first WAVE chunk */
201 ci->advance_buffer(12);
202
203 firstblockposn = 12;
204 ci->memset(&format, 0, sizeof(struct pcm_format));
205 format.is_signed = true;
206 format.is_little_endian = true;
207
208 decodedsamples = 0;
209 codec = 0;
210
211 /* iterate over WAVE chunks until the 'data' chunk, which should be after the 'fmt ' chunk */
212 while (true) {
213 /* get WAVE chunk header */
214 buf = ci->request_buffer(&n, 1024);
215 if (n < 8) {
216 DEBUGF("data chunk request_buffer error\n");
217 /* no more chunks, 'data' chunk must not have been found */
218 return CODEC_ERROR;
219 }
220
221 /* chunkSize */
222 size = (buf[4]|(buf[5]<<8)|(buf[6]<<16)|(buf[7]<<24));
223 if (memcmp(buf, "fmt ", 4) == 0) {
224 if (size < 16) {
225 DEBUGF("CODEC_ERROR: 'fmt ' chunk size=%lu < 16\n",
226 (unsigned long)size);
227 return CODEC_ERROR;
228 }
229 /* wFormatTag */
230 format.formattag=buf[8]|(buf[9]<<8);
231 /* wChannels */
232 format.channels=buf[10]|(buf[11]<<8);
233 /* skipping dwSamplesPerSec */
234 /* skipping dwAvgBytesPerSec */
235 /* wBlockAlign */
236 format.blockalign=buf[20]|(buf[21]<<8);
237 /* wBitsPerSample */
238 format.bitspersample=buf[22]|(buf[23]<<8);
239 if (format.formattag != WAVE_FORMAT_PCM) {
240 if (size < 18) {
241 /* this is not a fatal error with some formats,
242 * we'll see later if we can't decode it */
243 DEBUGF("CODEC_WARNING: non-PCM WAVE (formattag=0x%x) "
244 "doesn't have ext. fmt descr (chunksize=%d<18).\n",
245 (unsigned int)format.formattag, (int)size);
246 }
247 else
248 {
249 if (format.formattag != WAVE_FORMAT_EXTENSIBLE)
250 format.samplesperblock = buf[26]|(buf[27]<<8);
251 else
252 {
253 format.size = buf[24]|(buf[25]<<8);
254 if (format.size < 22) {
255 DEBUGF("CODEC_ERROR: WAVE_FORMAT_EXTENSIBLE is "
256 "missing extension\n");
257 return CODEC_ERROR;
258 }
259 /* wValidBitsPerSample */
260 format.bitspersample = buf[26]|(buf[27]<<8);
261 /* skipping dwChannelMask (4bytes) */
262 /* SubFormat (only get the first two bytes) */
263 format.formattag = buf[32]|(buf[33]<<8);
264 }
265 }
266 }
267
268 /* msadpcm specific */
269 if (format.formattag == WAVE_FORMAT_ADPCM)
270 {
271 if (!set_msadpcm_coeffs(buf))
272 {
273 return CODEC_ERROR;
274 }
275 }
276
277 /* get codec */
278 codec = get_wave_codec(format.formattag);
279 if (!codec)
280 {
281 DEBUGF("CODEC_ERROR: unsupported wave format 0x%x\n",
282 (unsigned int) format.formattag);
283 return CODEC_ERROR;
284 }
285
286 /* riff 8bit linear pcm is unsigned */
287 if (format.formattag == WAVE_FORMAT_PCM && format.bitspersample == 8)
288 format.is_signed = false;
289
290 /* set format, parse codec specific tag, check format, and calculate chunk size */
291 if (!codec->set_format(&format))
292 {
293 return CODEC_ERROR;
294 }
295 } else if (memcmp(buf, "data", 4) == 0) {
296 format.numbytes = size;
297 /* advance to start of data */
298 ci->advance_buffer(8);
299 firstblockposn += 8;
300 break;
301 } else if (memcmp(buf, "fact", 4) == 0) {
302 /* dwSampleLength */
303 if (size >= 4)
304 format.totalsamples =
305 (buf[8]|(buf[9]<<8)|(buf[10]<<16)|(buf[11]<<24));
306 } else {
307 DEBUGF("unknown WAVE chunk: '%c%c%c%c', size=%lu\n",
308 buf[0], buf[1], buf[2], buf[3], (unsigned long)size);
309 }
310
311 /* go to next chunk (even chunk sizes must be padded) */
312 size += 8 + (size & 0x01);
313
314 ci->advance_buffer(size);
315 firstblockposn += size;
316 }
317
318 if (!codec)
319 {
320 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found\n");
321 return CODEC_ERROR;
322 }
323
324 /* common format check */
325 if (format.channels == 0) {
326 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-channels file\n");
327 return CODEC_ERROR;
328 }
329 if (format.samplesperblock == 0) {
330 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-wSamplesPerBlock file\n");
331 return CODEC_ERROR;
332 }
333 if (format.blockalign == 0)
334 {
335 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-blockalign file\n");
336 return CODEC_ERROR;
337 }
338 if (format.numbytes == 0) {
339 DEBUGF("CODEC_ERROR: 'data' chunk not found or has zero-length\n");
340 return CODEC_ERROR;
341 }
342
343 /* check chunksize */
344 if ((format.chunksize / format.blockalign) * format.samplesperblock * format.channels
345 > PCM_SAMPLE_SIZE)
346 format.chunksize = (PCM_SAMPLE_SIZE / format.blockalign) * format.blockalign;
347 if (format.chunksize == 0)
348 {
349 DEBUGF("CODEC_ERROR: chunksize is 0\n");
350 return CODEC_ERROR;
351 }
352
353 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
354 if (format.channels == 2) {
355 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
356 } else if (format.channels == 1) {
357 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
358 } else {
359 DEBUGF("CODEC_ERROR: more than 2 channels\n");
360 return CODEC_ERROR;
361 }
362
363 /* make sure we're at the correct offset */
364 if (bytesdone > (uint32_t) firstblockposn) {
365 /* Round down to previous block */
366 struct pcm_pos *newpos = codec->get_seek_pos(bytesdone - firstblockposn,
367 PCM_SEEK_POS, &read_buffer);
368
369 if (newpos->pos > format.numbytes)
370 return CODEC_OK;
371 if (ci->seek_buffer(firstblockposn + newpos->pos))
372 {
373 bytesdone = newpos->pos;
374 decodedsamples = newpos->samples;
375 }
376 } else {
377 /* already where we need to be */
378 bytesdone = 0;
379 }
380
381 ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
382
383 /* The main decoder loop */
384 endofstream = 0;
385
386 while (!endofstream) {
387 enum codec_command_action action = ci->get_command(&param);
388
389 if (action == CODEC_ACTION_HALT)
390 break;
391
392 if (action == CODEC_ACTION_SEEK_TIME) {
393 struct pcm_pos *newpos = codec->get_seek_pos(param, PCM_SEEK_TIME,
394 &read_buffer);
395 if (newpos->pos > format.numbytes)
396 {
397 ci->set_elapsed(ci->id3->length);
398 ci->seek_complete();
399 break;
400 }
401
402 if (ci->seek_buffer(firstblockposn + newpos->pos))
403 {
404 bytesdone = newpos->pos;
405 decodedsamples = newpos->samples;
406 }
407
408 ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
409 ci->seek_complete();
410 }
411
412 wavbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize);
413 if (n == 0)
414 break; /* End of stream */
415 if (bytesdone + n > format.numbytes) {
416 n = format.numbytes - bytesdone;
417 endofstream = 1;
418 }
419
420 if (codec->decode(wavbuf, n, samples, &bufcount) == CODEC_ERROR)
421 {
422 DEBUGF("codec error\n");
423 return CODEC_ERROR;
424 }
425
426 ci->pcmbuf_insert(samples, NULL, bufcount);
427 ci->advance_buffer(n);
428 bytesdone += n;
429 decodedsamples += bufcount;
430
431 if (bytesdone >= format.numbytes)
432 endofstream = 1;
433 ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
434 }
435
436 return CODEC_OK;
437}