FS#11968 by Peter Lecky - Slovak language update
[maemo-rb.git] / apps / codecs / wav.c
blobe179470f272d242df269a5c17e3826d1f6a36e40
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 "codecs/libpcm/support_formats.h"
26 CODEC_HEADER
28 /* WAVE (RIFF) codec:
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
35 * For sample WAV files, see:
36 * http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/WAVE/Samples.html
40 #define PCM_SAMPLE_SIZE (4096*2)
42 static int32_t samples[PCM_SAMPLE_SIZE] IBSS_ATTR;
44 /* This codec support WAVE files with the following formats: */
45 enum
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
63 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 },
79 #define NUM_FORMATS 13
81 static const struct pcm_codec *get_wave_codec(uint32_t formattag)
83 int i;
85 for (i = 0; i < NUM_FORMATS; i++)
87 if (wave_codecs[i].format_tag == formattag)
89 if (wave_codecs[i].get_codec)
90 return wave_codecs[i].get_codec();
91 return 0;
94 return 0;
97 static struct pcm_format format;
98 static uint32_t bytesdone;
100 static bool set_msadpcm_coeffs(const uint8_t *buf)
102 int i;
103 int num;
104 int size;
106 buf += 4; /* skip 'fmt ' */
107 size = buf[0] | (buf[1] << 8) | (buf[1] << 16) | (buf[1] << 24);
108 if (size < 50)
110 DEBUGF("CODEC_ERROR: microsoft adpcm 'fmt ' chunk size=%lu < 50\n",
111 (unsigned long)size);
112 return false;
115 /* get nNumCoef */
116 buf += 24;
117 num = buf[0] | (buf[1] << 8);
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.
125 if (num != MSADPCM_NUM_COEFF)
127 DEBUGF("CODEC_ERROR: microsoft adpcm nNumCoef=%d != 7\n", num);
128 return false;
131 /* get aCoeffs */
132 buf += 2;
133 for (i = 0; i < MSADPCM_NUM_COEFF; i++)
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;
140 return true;
143 static uint8_t *read_buffer(size_t *realsize)
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;
153 /* this is the codec entry point */
154 enum codec_status codec_main(void)
156 int status;
157 uint32_t decodedsamples;
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;
165 uint32_t size;
167 /* Generic codec initialisation */
168 ci->configure(DSP_SET_SAMPLE_DEPTH, PCM_OUTPUT_DEPTH-1);
170 next_track:
171 status = CODEC_OK;
173 if (codec_init()) {
174 DEBUGF("codec_init() error\n");
175 status = CODEC_ERROR;
176 goto exit;
179 if (codec_wait_taginfo() != 0)
180 goto done;
182 codec_set_replaygain(ci->id3);
184 /* Need to save offset for later use (cleared indirectly by advance_buffer) */
185 bytesdone = ci->id3->offset;
187 /* get RIFF chunk header */
188 buf = ci->request_buffer(&n, 12);
189 if (n < 12) {
190 DEBUGF("request_buffer error\n");
191 status = CODEC_ERROR;
192 goto done;
194 if ((memcmp(buf, "RIFF", 4) != 0) || (memcmp(&buf[8], "WAVE", 4) != 0)) {
195 DEBUGF("CODEC_ERROR: missing riff header\n");
196 status = CODEC_ERROR;
197 goto done;
200 /* advance to first WAVE chunk */
201 ci->advance_buffer(12);
203 firstblockposn = 12;
204 ci->memset(&format, 0, sizeof(struct pcm_format));
205 format.is_signed = true;
206 format.is_little_endian = true;
208 decodedsamples = 0;
209 codec = 0;
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 status = CODEC_ERROR;
219 goto done;
222 /* chunkSize */
223 size = (buf[4]|(buf[5]<<8)|(buf[6]<<16)|(buf[7]<<24));
224 if (memcmp(buf, "fmt ", 4) == 0) {
225 if (size < 16) {
226 DEBUGF("CODEC_ERROR: 'fmt ' chunk size=%lu < 16\n",
227 (unsigned long)size);
228 status = CODEC_ERROR;
229 goto done;
231 /* wFormatTag */
232 format.formattag=buf[8]|(buf[9]<<8);
233 /* wChannels */
234 format.channels=buf[10]|(buf[11]<<8);
235 /* skipping dwSamplesPerSec */
236 /* skipping dwAvgBytesPerSec */
237 /* wBlockAlign */
238 format.blockalign=buf[20]|(buf[21]<<8);
239 /* wBitsPerSample */
240 format.bitspersample=buf[22]|(buf[23]<<8);
241 if (format.formattag != WAVE_FORMAT_PCM) {
242 if (size < 18) {
243 /* this is not a fatal error with some formats,
244 * we'll see later if we can't decode it */
245 DEBUGF("CODEC_WARNING: non-PCM WAVE (formattag=0x%x) "
246 "doesn't have ext. fmt descr (chunksize=%d<18).\n",
247 (unsigned int)format.formattag, (int)size);
249 else
251 if (format.formattag != WAVE_FORMAT_EXTENSIBLE)
252 format.samplesperblock = buf[26]|(buf[27]<<8);
253 else
255 format.size = buf[24]|(buf[25]<<8);
256 if (format.size < 22) {
257 DEBUGF("CODEC_ERROR: WAVE_FORMAT_EXTENSIBLE is "
258 "missing extension\n");
259 status = CODEC_ERROR;
260 goto done;
262 /* wValidBitsPerSample */
263 format.bitspersample = buf[26]|(buf[27]<<8);
264 /* skipping dwChannelMask (4bytes) */
265 /* SubFormat (only get the first two bytes) */
266 format.formattag = buf[32]|(buf[33]<<8);
271 /* msadpcm specific */
272 if (format.formattag == WAVE_FORMAT_ADPCM)
274 if (!set_msadpcm_coeffs(buf))
276 status = CODEC_ERROR;
277 goto done;
281 /* get codec */
282 codec = get_wave_codec(format.formattag);
283 if (!codec)
285 DEBUGF("CODEC_ERROR: unsupported wave format 0x%x\n",
286 (unsigned int) format.formattag);
287 status = CODEC_ERROR;
288 goto done;
291 /* riff 8bit linear pcm is unsigned */
292 if (format.formattag == WAVE_FORMAT_PCM && format.bitspersample == 8)
293 format.is_signed = false;
295 /* set format, parse codec specific tag, check format, and calculate chunk size */
296 if (!codec->set_format(&format))
298 status = CODEC_ERROR;
299 goto done;
301 } else if (memcmp(buf, "data", 4) == 0) {
302 format.numbytes = size;
303 /* advance to start of data */
304 ci->advance_buffer(8);
305 firstblockposn += 8;
306 break;
307 } else if (memcmp(buf, "fact", 4) == 0) {
308 /* dwSampleLength */
309 if (size >= 4)
310 format.totalsamples =
311 (buf[8]|(buf[9]<<8)|(buf[10]<<16)|(buf[11]<<24));
312 } else {
313 DEBUGF("unknown WAVE chunk: '%c%c%c%c', size=%lu\n",
314 buf[0], buf[1], buf[2], buf[3], (unsigned long)size);
317 /* go to next chunk (even chunk sizes must be padded) */
318 size += 8 + (size & 0x01);
320 ci->advance_buffer(size);
321 firstblockposn += size;
324 if (!codec)
326 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found\n");
327 status = CODEC_ERROR;
328 goto done;
331 /* common format check */
332 if (format.channels == 0) {
333 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-channels file\n");
334 status = CODEC_ERROR;
335 goto done;
337 if (format.samplesperblock == 0) {
338 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-wSamplesPerBlock file\n");
339 status = CODEC_ERROR;
340 goto done;
342 if (format.blockalign == 0)
344 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-blockalign file\n");
345 status = CODEC_ERROR;
346 goto done;
348 if (format.numbytes == 0) {
349 DEBUGF("CODEC_ERROR: 'data' chunk not found or has zero-length\n");
350 status = CODEC_ERROR;
351 goto done;
354 /* check chunksize */
355 if ((format.chunksize / format.blockalign) * format.samplesperblock * format.channels
356 > PCM_SAMPLE_SIZE)
357 format.chunksize = (PCM_SAMPLE_SIZE / format.blockalign) * format.blockalign;
358 if (format.chunksize == 0)
360 DEBUGF("CODEC_ERROR: chunksize is 0\n");
361 status = CODEC_ERROR;
362 goto done;
365 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
366 if (format.channels == 2) {
367 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
368 } else if (format.channels == 1) {
369 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
370 } else {
371 DEBUGF("CODEC_ERROR: more than 2 channels\n");
372 status = CODEC_ERROR;
373 goto done;
376 /* make sure we're at the correct offset */
377 if (bytesdone > (uint32_t) firstblockposn) {
378 /* Round down to previous block */
379 struct pcm_pos *newpos = codec->get_seek_pos(bytesdone - firstblockposn,
380 PCM_SEEK_POS, &read_buffer);
382 if (newpos->pos > format.numbytes)
383 goto done;
384 if (ci->seek_buffer(firstblockposn + newpos->pos))
386 bytesdone = newpos->pos;
387 decodedsamples = newpos->samples;
389 ci->seek_complete();
390 } else {
391 /* already where we need to be */
392 bytesdone = 0;
395 /* The main decoder loop */
396 endofstream = 0;
398 while (!endofstream) {
399 ci->yield();
400 if (ci->stop_codec || ci->new_track) {
401 break;
404 if (ci->seek_time) {
405 struct pcm_pos *newpos = codec->get_seek_pos(ci->seek_time, PCM_SEEK_TIME,
406 &read_buffer);
408 if (newpos->pos > format.numbytes)
409 break;
410 if (ci->seek_buffer(firstblockposn + newpos->pos))
412 bytesdone = newpos->pos;
413 decodedsamples = newpos->samples;
415 ci->seek_complete();
418 wavbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize);
419 if (n == 0)
420 break; /* End of stream */
421 if (bytesdone + n > format.numbytes) {
422 n = format.numbytes - bytesdone;
423 endofstream = 1;
426 status = codec->decode(wavbuf, n, samples, &bufcount);
427 if (status == CODEC_ERROR)
429 DEBUGF("codec error\n");
430 goto done;
433 ci->pcmbuf_insert(samples, NULL, bufcount);
434 ci->advance_buffer(n);
435 bytesdone += n;
436 decodedsamples += bufcount;
438 if (bytesdone >= format.numbytes)
439 endofstream = 1;
440 ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
443 done:
444 if (ci->request_next_track())
445 goto next_track;
447 exit:
448 return status;