commit FS#10424 and FS#10425
[kugel-rb.git] / apps / codecs / wav.c
blob293089a7373cb8b1549c624e4ea88c71b85e3220
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
11 * Copyright (C) 2009 Yoshihisa Uchida
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 #include "codeclib.h"
24 #include "inttypes.h"
25 #include "codecs/libpcm/support_formats.h"
27 CODEC_HEADER
29 /* WAVE (RIFF) codec:
31 * For a good documentation on WAVE files, see:
32 * http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/WAVE/WAVE.html
33 * and
34 * http://www.sonicspot.com/guide/wavefiles.html
36 * For sample WAV files, see:
37 * http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/WAVE/Samples.html
41 static int32_t samples[PCM_CHUNK_SIZE] IBSS_ATTR;
43 /* This codec support WAVE files with the following formats: */
44 enum
46 WAVE_FORMAT_UNKNOWN = 0x0000, /* Microsoft Unknown Wave Format */
47 WAVE_FORMAT_PCM = 0x0001, /* Microsoft PCM Format */
48 WAVE_FORMAT_ADPCM = 0x0002, /* Microsoft ADPCM Format */
49 WAVE_FORMAT_IEEE_FLOAT = 0x0003, /* IEEE Float */
50 WAVE_FORMAT_ALAW = 0x0006, /* Microsoft ALAW */
51 WAVE_FORMAT_MULAW = 0x0007, /* Microsoft MULAW */
52 WAVE_FORMAT_DVI_ADPCM = 0x0011, /* Intel's DVI ADPCM */
53 WAVE_FORMAT_DIALOGIC_OKI_ADPCM = 0x0017, /* Dialogic OKI ADPCM */
54 WAVE_FORMAT_YAMAHA_ADPCM = 0x0020, /* Yamaha ADPCM */
55 WAVE_FORMAT_XBOX_ADPCM = 0x0069, /* XBOX ADPCM */
56 IBM_FORMAT_MULAW = 0x0101, /* same as WAVE_FORMAT_MULAW */
57 IBM_FORMAT_ALAW = 0x0102, /* same as WAVE_FORMAT_ALAW */
58 WAVE_FORMAT_SWF_ADPCM = 0x5346, /* Adobe SWF ADPCM */
59 WAVE_FORMAT_EXTENSIBLE = 0xFFFE
62 const struct pcm_entry wave_codecs[] = {
63 { WAVE_FORMAT_UNKNOWN, 0 },
64 { WAVE_FORMAT_PCM, get_linear_pcm_codec },
65 { WAVE_FORMAT_ADPCM, get_ms_adpcm_codec },
66 { WAVE_FORMAT_IEEE_FLOAT, get_ieee_float_codec },
67 { WAVE_FORMAT_ALAW, get_itut_g711_alaw_codec },
68 { WAVE_FORMAT_MULAW, get_itut_g711_mulaw_codec },
69 { WAVE_FORMAT_DVI_ADPCM, get_dvi_adpcm_codec },
70 { WAVE_FORMAT_DIALOGIC_OKI_ADPCM, get_dialogic_oki_adpcm_codec },
71 { WAVE_FORMAT_YAMAHA_ADPCM, get_yamaha_adpcm_codec },
72 { WAVE_FORMAT_XBOX_ADPCM, get_dvi_adpcm_codec },
73 { IBM_FORMAT_MULAW, get_itut_g711_mulaw_codec },
74 { IBM_FORMAT_ALAW, get_itut_g711_alaw_codec },
75 { WAVE_FORMAT_SWF_ADPCM, get_swf_adpcm_codec },
78 #define NUM_FORMATS 13
80 static const struct pcm_codec *get_wave_codec(uint32_t formattag)
82 int i;
84 for (i = 0; i < NUM_FORMATS; i++)
86 if (wave_codecs[i].format_tag == formattag)
88 if (wave_codecs[i].get_codec)
89 return wave_codecs[i].get_codec();
90 return 0;
93 return 0;
96 static struct pcm_format format;
97 static uint32_t bytesdone;
99 static bool set_msadpcm_coeffs(const uint8_t *buf)
101 int i;
102 int num;
103 int size;
105 buf += 4; /* skip 'fmt ' */
106 size = buf[0] | (buf[1] << 8) | (buf[1] << 16) | (buf[1] << 24);
107 if (size < 50)
109 DEBUGF("CODEC_ERROR: microsoft adpcm 'fmt ' chunk size=%lu < 50\n",
110 (unsigned long)size);
111 return false;
114 /* get nNumCoef */
115 buf += 24;
116 num = buf[0] | (buf[1] << 8);
119 * In many case, nNumCoef is 7.
120 * Depending upon the encoder, as for this value there is a possibility of
121 * increasing more.
122 * If you found the file where this value exceeds 7, please report.
124 if (num != MSADPCM_NUM_COEFF)
126 DEBUGF("CODEC_ERROR: microsoft adpcm nNumCoef=%d != 7\n", num);
127 return false;
130 /* get aCoeffs */
131 buf += 2;
132 for (i = 0; i < MSADPCM_NUM_COEFF; i++)
134 format.coeffs[i][0] = buf[0] | (SE(buf[1]) << 8);
135 format.coeffs[i][1] = buf[2] | (SE(buf[3]) << 8);
136 buf += 4;
139 return true;
142 static uint8_t *read_buffer(size_t *realsize)
144 uint8_t *buffer = (uint8_t *)ci->request_buffer(realsize, format.chunksize);
145 if (bytesdone + (*realsize) > format.numbytes)
146 *realsize = format.numbytes - bytesdone;
147 bytesdone += *realsize;
148 ci->advance_buffer(*realsize);
149 return buffer;
152 /* this is the codec entry point */
153 enum codec_status codec_main(void)
155 int status = CODEC_OK;
156 uint32_t decodedsamples;
157 uint32_t i;
158 size_t n;
159 int bufcount;
160 int endofstream;
161 unsigned char *buf;
162 uint8_t *wavbuf;
163 off_t firstblockposn; /* position of the first block in file */
164 const struct pcm_codec *codec;
166 /* Generic codec initialisation */
167 ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
169 next_track:
170 if (codec_init()) {
171 DEBUGF("codec_init() error\n");
172 status = CODEC_ERROR;
173 goto exit;
176 while (!*ci->taginfo_ready && !ci->stop_codec)
177 ci->sleep(1);
179 codec_set_replaygain(ci->id3);
181 /* Need to save offset for later use (cleared indirectly by advance_buffer) */
182 bytesdone = ci->id3->offset;
184 /* get RIFF chunk header */
185 buf = ci->request_buffer(&n, 12);
186 if (n < 12) {
187 DEBUGF("request_buffer error\n");
188 status = CODEC_ERROR;
189 goto done;
191 if ((memcmp(buf, "RIFF", 4) != 0) || (memcmp(&buf[8], "WAVE", 4) != 0)) {
192 DEBUGF("CODEC_ERROR: missing riff header\n");
193 status = CODEC_ERROR;
194 goto done;
197 /* advance to first WAVE chunk */
198 ci->advance_buffer(12);
200 firstblockposn = 12;
201 ci->memset(&format, 0, sizeof(struct pcm_format));
202 format.is_signed = true;
203 format.is_little_endian = true;
205 decodedsamples = 0;
206 codec = 0;
208 /* iterate over WAVE chunks until the 'data' chunk, which should be after the 'fmt ' chunk */
209 while (true) {
210 /* get WAVE chunk header */
211 buf = ci->request_buffer(&n, 1024);
212 if (n < 8) {
213 DEBUGF("data chunk request_buffer error\n");
214 /* no more chunks, 'data' chunk must not have been found */
215 status = CODEC_ERROR;
216 goto done;
219 /* chunkSize */
220 i = (buf[4]|(buf[5]<<8)|(buf[6]<<16)|(buf[7]<<24));
221 if (memcmp(buf, "fmt ", 4) == 0) {
222 if (i < 16) {
223 DEBUGF("CODEC_ERROR: 'fmt ' chunk size=%lu < 16\n",
224 (unsigned long)i);
225 status = CODEC_ERROR;
226 goto done;
228 /* wFormatTag */
229 format.formattag=buf[8]|(buf[9]<<8);
230 /* wChannels */
231 format.channels=buf[10]|(buf[11]<<8);
232 /* skipping dwSamplesPerSec */
233 /* dwAvgBytesPerSec */
234 format.avgbytespersec = buf[16]|(buf[17]<<8)|(buf[18]<<16)|(buf[19]<<24);
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 (i < 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)i);
247 else
249 format.size = buf[24]|(buf[25]<<8);
250 if (format.formattag != WAVE_FORMAT_EXTENSIBLE)
251 format.samplesperblock = buf[26]|(buf[27]<<8);
252 else {
253 if (format.size < 22) {
254 DEBUGF("CODEC_ERROR: WAVE_FORMAT_EXTENSIBLE is "
255 "missing extension\n");
256 status = CODEC_ERROR;
257 goto done;
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);
268 /* msadpcm specific */
269 if (format.formattag == WAVE_FORMAT_ADPCM)
271 if (!set_msadpcm_coeffs(buf))
273 status = CODEC_ERROR;
274 goto done;
278 /* get codec */
279 codec = get_wave_codec(format.formattag);
280 if (!codec)
282 DEBUGF("CODEC_ERROR: unsupported wave format 0x%x\n",
283 (unsigned int) format.formattag);
284 status = CODEC_ERROR;
285 goto done;
288 /* riff 8bit linear pcm is unsigned */
289 if (format.formattag == WAVE_FORMAT_PCM && format.bitspersample == 8)
290 format.is_signed = false;
292 /* set format, parse codec specific tag, check format, and calculate chunk size */
293 if (!codec->set_format(&format))
295 status = CODEC_ERROR;
296 goto done;
298 } else if (memcmp(buf, "data", 4) == 0) {
299 format.numbytes = i;
300 /* advance to start of data */
301 ci->advance_buffer(8);
302 firstblockposn += 8;
303 break;
304 } else if (memcmp(buf, "fact", 4) == 0) {
305 /* dwSampleLength */
306 if (i >= 4)
307 format.totalsamples =
308 (buf[8]|(buf[9]<<8)|(buf[10]<<16)|(buf[11]<<24));
309 } else {
310 DEBUGF("unknown WAVE chunk: '%c%c%c%c', size=%lu\n",
311 buf[0], buf[1], buf[2], buf[3], (unsigned long)i);
314 /* go to next chunk (even chunk sizes must be padded) */
315 if (i & 0x01)
316 i++;
317 ci->advance_buffer(i+8);
318 firstblockposn += i + 8;
321 if (!codec)
323 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found\n");
324 status = CODEC_ERROR;
325 goto done;
328 /* common format check */
329 if (format.channels == 0) {
330 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-channels file\n");
331 status = CODEC_ERROR;
332 goto done;
334 if (format.samplesperblock == 0) {
335 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-wSamplesPerBlock file\n");
336 status = CODEC_ERROR;
337 goto done;
339 if (format.blockalign == 0)
341 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-blockalign file\n");
342 i = CODEC_ERROR;
343 goto done;
345 if (format.numbytes == 0) {
346 DEBUGF("CODEC_ERROR: 'data' chunk not found or has zero-length\n");
347 status = CODEC_ERROR;
348 goto done;
351 /* check chunksize */
352 if ((format.chunksize / format.blockalign) * format.samplesperblock * format.channels
353 > PCM_CHUNK_SIZE)
354 format.chunksize = (PCM_CHUNK_SIZE / format.blockalign) * format.blockalign;
355 if (format.chunksize == 0)
357 DEBUGF("CODEC_ERROR: chunksize is 0\n");
358 i = CODEC_ERROR;
359 goto done;
362 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
363 if (format.channels == 2) {
364 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
365 } else if (format.channels == 1) {
366 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
367 } else {
368 DEBUGF("CODEC_ERROR: more than 2 channels\n");
369 status = CODEC_ERROR;
370 goto done;
373 /* make sure we're at the correct offset */
374 if (bytesdone > (uint32_t) firstblockposn) {
375 /* Round down to previous block */
376 uint32_t offset = bytesdone - bytesdone % format.blockalign;
378 ci->advance_buffer(offset-firstblockposn);
379 bytesdone = offset - firstblockposn;
380 } else {
381 /* already where we need to be */
382 bytesdone = 0;
385 /* The main decoder loop */
386 endofstream = 0;
388 while (!endofstream) {
389 ci->yield();
390 if (ci->stop_codec || ci->new_track) {
391 break;
394 if (ci->seek_time) {
395 struct pcm_pos *newpos = codec->get_seek_pos(ci->seek_time, &read_buffer);
397 decodedsamples = newpos->samples;
398 if (newpos->pos > format.numbytes)
399 break;
400 if (ci->seek_buffer(firstblockposn + newpos->pos))
402 bytesdone = newpos->pos;
404 ci->seek_complete();
407 wavbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize);
408 if (n == 0)
409 break; /* End of stream */
410 if (bytesdone + n > format.numbytes) {
411 n = format.numbytes - bytesdone;
412 endofstream = 1;
415 status = codec->decode(wavbuf, n, samples, &bufcount);
416 if (status == CODEC_ERROR)
418 DEBUGF("codec error\n");
419 goto done;
422 ci->pcmbuf_insert(samples, NULL, bufcount);
423 ci->advance_buffer(n);
424 bytesdone += n;
425 decodedsamples += bufcount;
427 if (bytesdone >= format.numbytes)
428 endofstream = 1;
429 ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
431 status = CODEC_OK;
433 done:
434 if (ci->request_next_track())
435 goto next_track;
437 exit:
438 return status;