Theme Editor: Added support for upper-case wps file extensions
[kugel-rb.git] / apps / codecs / wav64.c
blobc06f78c80249f0d0c69cd63971dfd15c090c38f6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Yoshihisa Uchida
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "codeclib.h"
23 #include "codecs/libpcm/support_formats.h"
25 CODEC_HEADER
27 /* Wave64 codec
29 * References
30 * [1] VCS Aktiengesellschaft, Sony Wave64, Informations_about_Sony_Wave64.pdf
33 #define PCM_SAMPLE_SIZE (4096*2)
35 static int32_t samples[PCM_SAMPLE_SIZE] IBSS_ATTR;
37 /* Wave64 GUIDs */
38 #define WAVE64_GUID_RIFF "riff\x2e\x91\xcf\x11\xa5\xd6\x28\xdb\x04\xc1\x00\x00"
39 #define WAVE64_GUID_WAVE "wave\xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
40 #define WAVE64_GUID_FMT "fmt \xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
41 #define WAVE64_GUID_FACT "fact\xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
42 #define WAVE64_GUID_DATA "data\xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
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 /* Read an unaligned 64-bit little endian unsigned integer from buffer. */
101 static uint64_t get_uint64_le(void* buf)
103 unsigned char* p = (unsigned char*) buf;
105 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24) | ((uint64_t)p[4] << 32) |
106 ((uint64_t)p[5] << 40) | ((uint64_t)p[6] << 48) | ((uint64_t)p[7] << 56);
109 static bool set_msadpcm_coeffs(unsigned char *buf)
111 int i;
112 int num;
113 uint64_t size;
115 buf += 16; /* skip 'fmt ' GUID */
116 size = get_uint64_le(buf);
117 if (size < 50)
119 DEBUGF("CODEC_ERROR: microsoft adpcm 'fmt ' chunk size=%d < 50\n", (int)size);
120 return false;
123 /* get nNumCoef */
124 buf += 28;
125 num = buf[0] | (buf[1] << 8);
128 * In many case, nNumCoef is 7.
129 * Depending upon the encoder, as for this value there is a possibility of
130 * increasing more.
131 * If you found the file where this value exceeds 7, please report.
133 if (num != MSADPCM_NUM_COEFF)
135 DEBUGF("CODEC_ERROR: microsoft adpcm nNumCoef=%d != 7\n", num);
136 return false;
139 /* get aCoeffs */
140 buf += 2;
141 for (i = 0; i < MSADPCM_NUM_COEFF; i++)
143 format.coeffs[i][0] = buf[0] | (SE(buf[1]) << 8);
144 format.coeffs[i][1] = buf[2] | (SE(buf[3]) << 8);
145 buf += 4;
148 return true;
151 static uint8_t *read_buffer(size_t *realsize)
153 uint8_t *buffer = (uint8_t *)ci->request_buffer(realsize, format.chunksize);
154 if (bytesdone + (*realsize) > format.numbytes)
155 *realsize = format.numbytes - bytesdone;
156 bytesdone += *realsize;
157 ci->advance_buffer(*realsize);
158 return buffer;
161 /* this is the codec entry point */
162 enum codec_status codec_main(void)
164 int status = CODEC_OK;
165 uint32_t decodedsamples;
166 size_t n;
167 int bufcount;
168 int endofstream;
169 unsigned char *buf;
170 uint8_t *wavbuf;
171 off_t firstblockposn; /* position of the first block in file */
172 const struct pcm_codec *codec;
173 uint64_t size;
175 /* Generic codec initialisation */
176 ci->configure(DSP_SET_SAMPLE_DEPTH, PCM_OUTPUT_DEPTH-1);
178 next_track:
179 if (codec_init()) {
180 DEBUGF("codec_init() error\n");
181 status = CODEC_ERROR;
182 goto exit;
185 while (!*ci->taginfo_ready && !ci->stop_codec)
186 ci->sleep(1);
188 codec_set_replaygain(ci->id3);
190 /* Need to save offset for later use (cleared indirectly by advance_buffer) */
191 bytesdone = ci->id3->offset;
193 /* get RIFF chunk header */
194 buf = ci->request_buffer(&n, 40);
195 if (n < 40) {
196 DEBUGF("request_buffer error\n");
197 status = CODEC_ERROR;
198 goto done;
200 if ((memcmp(buf , WAVE64_GUID_RIFF, 16) != 0) ||
201 (memcmp(buf+24, WAVE64_GUID_WAVE, 16) != 0))
203 status = CODEC_ERROR;
204 goto done;
207 /* advance to first WAVE chunk */
208 ci->advance_buffer(40);
210 firstblockposn = 40;
211 ci->memset(&format, 0, sizeof(struct pcm_format));
212 format.is_signed = true;
213 format.is_little_endian = true;
215 decodedsamples = 0;
216 codec = 0;
218 /* iterate over WAVE chunks until the 'data' chunk, which should be after the 'fmt ' chunk */
219 while (true) {
220 /* get WAVE chunk header */
221 buf = ci->request_buffer(&n, 1024);
222 if (n < 8) {
223 DEBUGF("data chunk request_buffer error\n");
224 /* no more chunks, 'data' chunk must not have been found */
225 status = CODEC_ERROR;
226 goto done;
229 /* chunkSize */
230 size = get_uint64_le(buf+16) - 24;
231 if (memcmp(buf, WAVE64_GUID_FMT, 16) == 0) {
232 if (size < 16) {
233 DEBUGF("CODEC_ERROR: 'fmt ' chunk size=%d < 16\n", (int)size);
234 status = CODEC_ERROR;
235 goto done;
237 /* wFormatTag */
238 format.formattag=buf[24]|(buf[25]<<8);
239 /* wChannels */
240 format.channels=buf[26]|(buf[27]<<8);
241 /* skipping dwSamplesPerSec */
242 /* skipping dwAvgBytesPerSec */
243 /* wBlockAlign */
244 format.blockalign=buf[36]|(buf[37]<<8);
245 /* wBitsPerSample */
246 format.bitspersample=buf[38]|(buf[39]<<8);
247 if (format.formattag != WAVE_FORMAT_PCM) {
248 if (size < 18) {
249 /* this is not a fatal error with some formats,
250 * we'll see later if we can't decode it */
251 DEBUGF("CODEC_WARNING: non-PCM WAVE (formattag=0x%x) "
252 "doesn't have ext. fmt descr (chunksize=%d<18).\n",
253 (unsigned int)format.formattag, (int)size);
255 else
257 if (format.formattag != WAVE_FORMAT_EXTENSIBLE)
258 format.samplesperblock = buf[42]|(buf[43]<<8);
259 else {
260 format.size = buf[40]|(buf[41]<<8);
261 if (format.size < 22) {
262 DEBUGF("CODEC_ERROR: WAVE_FORMAT_EXTENSIBLE is "
263 "missing extension\n");
264 status = CODEC_ERROR;
265 goto done;
267 /* wValidBitsPerSample */
268 format.bitspersample = buf[42]|(buf[43]<<8);
269 /* skipping dwChannelMask (4bytes) */
270 /* SubFormat (only get the first two bytes) */
271 format.formattag = buf[48]|(buf[49]<<8);
276 /* msadpcm specific */
277 if (format.formattag == WAVE_FORMAT_ADPCM)
279 if (!set_msadpcm_coeffs(buf))
281 status = CODEC_ERROR;
282 goto done;
286 /* get codec */
287 codec = get_wave_codec(format.formattag);
288 if (!codec)
290 DEBUGF("CODEC_ERROR: unsupported wave format 0x%x\n",
291 (unsigned int) format.formattag);
292 status = CODEC_ERROR;
293 goto done;
296 /* riff 8bit linear pcm is unsigned */
297 if (format.formattag == WAVE_FORMAT_PCM && format.bitspersample == 8)
298 format.is_signed = false;
300 /* check format, and calculate chunk size */
301 if (!codec->set_format(&format))
303 status = CODEC_ERROR;
304 goto done;
306 } else if (memcmp(buf, WAVE64_GUID_DATA, 16) == 0) {
307 format.numbytes = size;
308 /* advance to start of data */
309 ci->advance_buffer(24);
310 firstblockposn += 24;
311 break;
312 } else if (memcmp(buf, WAVE64_GUID_FACT, 16) == 0) {
313 /* skip 'fact' chunk */
314 } else {
315 DEBUGF("unknown Wave64 chunk: "
316 "'%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x'\n",
317 buf[0], buf[1], buf[ 2], buf[ 3], buf[ 4], buf[ 5], buf[ 6], buf[ 7],
318 buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]);
321 /* go to next chunk (8byte bound) */
322 size += 24 + ((1 + ~size) & 0x07);
324 ci->advance_buffer(size);
325 firstblockposn += size;
328 if (!codec)
330 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found\n");
331 status = CODEC_ERROR;
332 goto done;
335 /* common format check */
336 if (format.channels == 0) {
337 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-channels file\n");
338 status = CODEC_ERROR;
339 goto done;
341 if (format.samplesperblock == 0) {
342 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-wSamplesPerBlock file\n");
343 status = CODEC_ERROR;
344 goto done;
346 if (format.blockalign == 0)
348 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-blockalign file\n");
349 status = CODEC_ERROR;
350 goto done;
352 if (format.numbytes == 0) {
353 DEBUGF("CODEC_ERROR: 'data' chunk not found or has zero-length\n");
354 status = CODEC_ERROR;
355 goto done;
358 /* check chunksize */
359 if ((format.chunksize / format.blockalign) * format.samplesperblock * format.channels
360 > PCM_SAMPLE_SIZE)
361 format.chunksize = (PCM_SAMPLE_SIZE / format.blockalign) * format.blockalign;
362 if (format.chunksize == 0)
364 DEBUGF("CODEC_ERROR: chunksize is 0\n");
365 status = CODEC_ERROR;
366 goto done;
369 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
370 if (format.channels == 2) {
371 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
372 } else if (format.channels == 1) {
373 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
374 } else {
375 DEBUGF("CODEC_ERROR: more than 2 channels\n");
376 status = CODEC_ERROR;
377 goto done;
380 /* make sure we're at the correct offset */
381 if (bytesdone > (uint32_t) firstblockposn) {
382 /* Round down to previous block */
383 struct pcm_pos *newpos = codec->get_seek_pos(bytesdone - firstblockposn,
384 PCM_SEEK_POS, &read_buffer);
386 if (newpos->pos > format.numbytes)
387 goto done;
388 if (ci->seek_buffer(firstblockposn + newpos->pos))
390 bytesdone = newpos->pos;
391 decodedsamples = newpos->samples;
393 ci->seek_complete();
394 } else {
395 /* already where we need to be */
396 bytesdone = 0;
399 /* The main decoder loop */
400 endofstream = 0;
402 while (!endofstream) {
403 ci->yield();
404 if (ci->stop_codec || ci->new_track) {
405 break;
408 if (ci->seek_time) {
409 struct pcm_pos *newpos = codec->get_seek_pos(ci->seek_time, PCM_SEEK_TIME,
410 &read_buffer);
412 if (newpos->pos > format.numbytes)
413 break;
414 if (ci->seek_buffer(firstblockposn + newpos->pos))
416 bytesdone = newpos->pos;
417 decodedsamples = newpos->samples;
419 ci->seek_complete();
422 wavbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize);
423 if (n == 0)
424 break; /* End of stream */
425 if (bytesdone + n > format.numbytes) {
426 n = format.numbytes - bytesdone;
427 endofstream = 1;
430 status = codec->decode(wavbuf, n, samples, &bufcount);
431 if (status == CODEC_ERROR)
433 DEBUGF("codec error\n");
434 goto done;
437 ci->pcmbuf_insert(samples, NULL, bufcount);
438 ci->advance_buffer(n);
439 bytesdone += n;
440 decodedsamples += bufcount;
442 if (bytesdone >= format.numbytes)
443 endofstream = 1;
444 ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
446 status = CODEC_OK;
448 done:
449 if (ci->request_next_track())
450 goto next_track;
452 exit:
453 return status;