blob: 184f39bf18c65d5fe5fff40a6d7607fd26c1fb39 [file] [log] [blame]
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2010 Yoshihisa Uchida
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "codeclib.h"
23#include "codecs/libpcm/support_formats.h"
24
25CODEC_HEADER
26
27/* Wave64 codec
28 *
29 * References
30 * [1] VCS Aktiengesellschaft, Sony Wave64, Informations_about_Sony_Wave64.pdf
31 */
32
33#define PCM_SAMPLE_SIZE (4096*2)
34
35static int32_t samples[PCM_SAMPLE_SIZE] IBSS_ATTR;
36
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"
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
63const 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
100/* Read an unaligned 64-bit little endian unsigned integer from buffer. */
101static uint64_t get_uint64_le(void* buf)
102{
103 unsigned char* p = (unsigned char*) buf;
104
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);
107}
108
109static bool set_msadpcm_coeffs(unsigned char *buf)
110{
111 int i;
112 int num;
113 uint64_t size;
114
115 buf += 16; /* skip 'fmt ' GUID */
116 size = get_uint64_le(buf);
117 if (size < 50)
118 {
119 DEBUGF("CODEC_ERROR: microsoft adpcm 'fmt ' chunk size=%d < 50\n", (int)size);
120 return false;
121 }
122
123 /* get nNumCoef */
124 buf += 28;
125 num = buf[0] | (buf[1] << 8);
126
127 /*
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.
132 */
133 if (num != MSADPCM_NUM_COEFF)
134 {
135 DEBUGF("CODEC_ERROR: microsoft adpcm nNumCoef=%d != 7\n", num);
136 return false;
137 }
138
139 /* get aCoeffs */
140 buf += 2;
141 for (i = 0; i < MSADPCM_NUM_COEFF; i++)
142 {
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;
146 }
147
148 return true;
149}
150
151static uint8_t *read_buffer(size_t *realsize)
152{
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;
159}
160
161/* this is the codec entry point */
Michael Sevakisc537d592011-04-27 03:08:23 +0000162enum codec_status codec_main(enum codec_entry_call_reason reason)
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000163{
Michael Sevakisc537d592011-04-27 03:08:23 +0000164 if (reason == CODEC_LOAD) {
165 /* Generic codec initialisation */
166 ci->configure(DSP_SET_SAMPLE_DEPTH, PCM_OUTPUT_DEPTH-1);
167 }
168
169 return CODEC_OK;
170}
171
172/* this is called for each file to process */
173enum codec_status codec_run(void)
174{
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000175 uint32_t decodedsamples;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000176 size_t n;
177 int bufcount;
178 int endofstream;
179 unsigned char *buf;
180 uint8_t *wavbuf;
181 off_t firstblockposn; /* position of the first block in file */
182 const struct pcm_codec *codec;
183 uint64_t size;
Michael Sevakisc537d592011-04-27 03:08:23 +0000184 intptr_t param;
Michael Sevakis85e40252011-02-20 15:27:10 +0000185
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000186 if (codec_init()) {
187 DEBUGF("codec_init() error\n");
Michael Sevakisc537d592011-04-27 03:08:23 +0000188 return CODEC_ERROR;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000189 }
190
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000191 codec_set_replaygain(ci->id3);
192
193 /* Need to save offset for later use (cleared indirectly by advance_buffer) */
194 bytesdone = ci->id3->offset;
195
196 /* get RIFF chunk header */
Michael Sevakisc537d592011-04-27 03:08:23 +0000197 ci->seek_buffer(0);
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000198 buf = ci->request_buffer(&n, 40);
199 if (n < 40) {
200 DEBUGF("request_buffer error\n");
Michael Sevakisc537d592011-04-27 03:08:23 +0000201 return CODEC_ERROR;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000202 }
203 if ((memcmp(buf , WAVE64_GUID_RIFF, 16) != 0) ||
204 (memcmp(buf+24, WAVE64_GUID_WAVE, 16) != 0))
205 {
Michael Sevakisc537d592011-04-27 03:08:23 +0000206 return CODEC_ERROR;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000207 }
208
209 /* advance to first WAVE chunk */
210 ci->advance_buffer(40);
211
212 firstblockposn = 40;
213 ci->memset(&format, 0, sizeof(struct pcm_format));
214 format.is_signed = true;
215 format.is_little_endian = true;
216
217 decodedsamples = 0;
218 codec = 0;
219
220 /* iterate over WAVE chunks until the 'data' chunk, which should be after the 'fmt ' chunk */
221 while (true) {
222 /* get WAVE chunk header */
223 buf = ci->request_buffer(&n, 1024);
224 if (n < 8) {
225 DEBUGF("data chunk request_buffer error\n");
226 /* no more chunks, 'data' chunk must not have been found */
Michael Sevakisc537d592011-04-27 03:08:23 +0000227 return CODEC_ERROR;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000228 }
229
230 /* chunkSize */
231 size = get_uint64_le(buf+16) - 24;
232 if (memcmp(buf, WAVE64_GUID_FMT, 16) == 0) {
233 if (size < 16) {
234 DEBUGF("CODEC_ERROR: 'fmt ' chunk size=%d < 16\n", (int)size);
Michael Sevakisc537d592011-04-27 03:08:23 +0000235 return CODEC_ERROR;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000236 }
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);
254 }
255 else
256 {
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000257 if (format.formattag != WAVE_FORMAT_EXTENSIBLE)
258 format.samplesperblock = buf[42]|(buf[43]<<8);
259 else {
Yoshihisa Uchidaf640b892010-03-07 07:15:21 +0000260 format.size = buf[40]|(buf[41]<<8);
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000261 if (format.size < 22) {
262 DEBUGF("CODEC_ERROR: WAVE_FORMAT_EXTENSIBLE is "
263 "missing extension\n");
Michael Sevakisc537d592011-04-27 03:08:23 +0000264 return CODEC_ERROR;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000265 }
266 /* wValidBitsPerSample */
267 format.bitspersample = buf[42]|(buf[43]<<8);
268 /* skipping dwChannelMask (4bytes) */
269 /* SubFormat (only get the first two bytes) */
270 format.formattag = buf[48]|(buf[49]<<8);
271 }
272 }
273 }
274
275 /* msadpcm specific */
276 if (format.formattag == WAVE_FORMAT_ADPCM)
277 {
278 if (!set_msadpcm_coeffs(buf))
Michael Sevakisc537d592011-04-27 03:08:23 +0000279 return CODEC_ERROR;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000280 }
281
282 /* get codec */
283 codec = get_wave_codec(format.formattag);
284 if (!codec)
285 {
286 DEBUGF("CODEC_ERROR: unsupported wave format 0x%x\n",
287 (unsigned int) format.formattag);
Michael Sevakisc537d592011-04-27 03:08:23 +0000288 return CODEC_ERROR;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000289 }
290
291 /* riff 8bit linear pcm is unsigned */
292 if (format.formattag == WAVE_FORMAT_PCM && format.bitspersample == 8)
293 format.is_signed = false;
294
295 /* check format, and calculate chunk size */
296 if (!codec->set_format(&format))
Michael Sevakisc537d592011-04-27 03:08:23 +0000297 return CODEC_ERROR;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000298 } else if (memcmp(buf, WAVE64_GUID_DATA, 16) == 0) {
299 format.numbytes = size;
300 /* advance to start of data */
301 ci->advance_buffer(24);
302 firstblockposn += 24;
303 break;
304 } else if (memcmp(buf, WAVE64_GUID_FACT, 16) == 0) {
305 /* skip 'fact' chunk */
306 } else {
307 DEBUGF("unknown Wave64 chunk: "
308 "'%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x'\n",
309 buf[0], buf[1], buf[ 2], buf[ 3], buf[ 4], buf[ 5], buf[ 6], buf[ 7],
310 buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]);
311 }
312
313 /* go to next chunk (8byte bound) */
Yoshihisa Uchidaf640b892010-03-07 07:15:21 +0000314 size += 24 + ((1 + ~size) & 0x07);
315
316 ci->advance_buffer(size);
317 firstblockposn += size;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000318 }
319
320 if (!codec)
321 {
322 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found\n");
Michael Sevakisc537d592011-04-27 03:08:23 +0000323 return CODEC_ERROR;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000324 }
325
326 /* common format check */
327 if (format.channels == 0) {
328 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-channels file\n");
Michael Sevakisc537d592011-04-27 03:08:23 +0000329 return CODEC_ERROR;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000330 }
331 if (format.samplesperblock == 0) {
332 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-wSamplesPerBlock file\n");
Michael Sevakisc537d592011-04-27 03:08:23 +0000333 return CODEC_ERROR;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000334 }
335 if (format.blockalign == 0)
336 {
337 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-blockalign file\n");
Michael Sevakisc537d592011-04-27 03:08:23 +0000338 return CODEC_ERROR;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000339 }
340 if (format.numbytes == 0) {
341 DEBUGF("CODEC_ERROR: 'data' chunk not found or has zero-length\n");
Michael Sevakisc537d592011-04-27 03:08:23 +0000342 return CODEC_ERROR;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000343 }
344
345 /* check chunksize */
346 if ((format.chunksize / format.blockalign) * format.samplesperblock * format.channels
347 > PCM_SAMPLE_SIZE)
348 format.chunksize = (PCM_SAMPLE_SIZE / format.blockalign) * format.blockalign;
349 if (format.chunksize == 0)
350 {
351 DEBUGF("CODEC_ERROR: chunksize is 0\n");
Michael Sevakisc537d592011-04-27 03:08:23 +0000352 return CODEC_ERROR;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000353 }
354
355 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
356 if (format.channels == 2) {
357 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
358 } else if (format.channels == 1) {
359 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
360 } else {
361 DEBUGF("CODEC_ERROR: more than 2 channels\n");
Michael Sevakisc537d592011-04-27 03:08:23 +0000362 return CODEC_ERROR;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000363 }
364
365 /* make sure we're at the correct offset */
366 if (bytesdone > (uint32_t) firstblockposn) {
367 /* Round down to previous block */
Yoshihisa Uchida7a3822c2010-03-22 10:02:05 +0000368 struct pcm_pos *newpos = codec->get_seek_pos(bytesdone - firstblockposn,
369 PCM_SEEK_POS, &read_buffer);
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000370
Michael Sevakisc537d592011-04-27 03:08:23 +0000371 if (newpos->pos > format.numbytes) {
372 return CODEC_OK;
373 }
Yoshihisa Uchida7a3822c2010-03-22 10:02:05 +0000374 if (ci->seek_buffer(firstblockposn + newpos->pos))
375 {
376 bytesdone = newpos->pos;
377 decodedsamples = newpos->samples;
378 }
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000379 } else {
380 /* already where we need to be */
381 bytesdone = 0;
382 }
383
Michael Sevakis7ad2cad2011-08-28 07:45:35 +0000384 ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
385
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000386 /* The main decoder loop */
387 endofstream = 0;
388
389 while (!endofstream) {
Michael Sevakisc537d592011-04-27 03:08:23 +0000390 enum codec_command_action action = ci->get_command(&param);
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000391
Michael Sevakisc537d592011-04-27 03:08:23 +0000392 if (action == CODEC_ACTION_HALT)
393 break;
394
395 if (action == CODEC_ACTION_SEEK_TIME) {
396 struct pcm_pos *newpos = codec->get_seek_pos(param, PCM_SEEK_TIME,
Yoshihisa Uchida7a3822c2010-03-22 10:02:05 +0000397 &read_buffer);
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000398
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000399 if (newpos->pos > format.numbytes)
Michael Sevakisc537d592011-04-27 03:08:23 +0000400 {
401 ci->set_elapsed(ci->id3->length);
402 ci->seek_complete();
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000403 break;
Michael Sevakisc537d592011-04-27 03:08:23 +0000404 }
405
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000406 if (ci->seek_buffer(firstblockposn + newpos->pos))
407 {
Yoshihisa Uchida8050d472010-03-06 08:13:56 +0000408 bytesdone = newpos->pos;
409 decodedsamples = newpos->samples;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000410 }
Michael Sevakisc537d592011-04-27 03:08:23 +0000411
412 ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000413 ci->seek_complete();
414 }
415
416 wavbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize);
417 if (n == 0)
418 break; /* End of stream */
419 if (bytesdone + n > format.numbytes) {
420 n = format.numbytes - bytesdone;
421 endofstream = 1;
422 }
423
Michael Sevakisc537d592011-04-27 03:08:23 +0000424 if (codec->decode(wavbuf, n, samples, &bufcount) == CODEC_ERROR)
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000425 {
426 DEBUGF("codec error\n");
Michael Sevakisc537d592011-04-27 03:08:23 +0000427 return CODEC_ERROR;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000428 }
429
430 ci->pcmbuf_insert(samples, NULL, bufcount);
431 ci->advance_buffer(n);
432 bytesdone += n;
433 decodedsamples += bufcount;
434
435 if (bytesdone >= format.numbytes)
436 endofstream = 1;
437 ci->set_elapsed(decodedsamples*1000LL/ci->id3->frequency);
438 }
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000439
Michael Sevakisc537d592011-04-27 03:08:23 +0000440 return CODEC_OK;
Yoshihisa Uchida561cb2c2010-02-28 08:48:07 +0000441}