summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/codecs/SOURCES1
-rw-r--r--apps/codecs/codecs.make1
-rw-r--r--apps/codecs/wav64.c447
-rw-r--r--apps/filetypes.c1
-rw-r--r--apps/metadata.c11
-rw-r--r--apps/metadata.h1
-rw-r--r--apps/metadata/metadata_common.c9
-rw-r--r--apps/metadata/metadata_common.h1
-rw-r--r--apps/metadata/metadata_parsers.h1
-rw-r--r--apps/metadata/wave.c146
10 files changed, 600 insertions, 19 deletions
diff --git a/apps/codecs/SOURCES b/apps/codecs/SOURCES
index 41247658c7..8970567b65 100644
--- a/apps/codecs/SOURCES
+++ b/apps/codecs/SOURCES
@@ -30,6 +30,7 @@ adx.c
30smaf.c 30smaf.c
31au.c 31au.c
32vox.c 32vox.c
33wav64.c
33#if defined(HAVE_RECORDING) && !defined(SIMULATOR) 34#if defined(HAVE_RECORDING) && !defined(SIMULATOR)
34/* encoders */ 35/* encoders */
35aiff_enc.c 36aiff_enc.c
diff --git a/apps/codecs/codecs.make b/apps/codecs/codecs.make
index 7bd141e9cd..1f377abf3d 100644
--- a/apps/codecs/codecs.make
+++ b/apps/codecs/codecs.make
@@ -93,6 +93,7 @@ $(CODECDIR)/wav.codec : $(CODECDIR)/libpcm.a
93$(CODECDIR)/smaf.codec : $(CODECDIR)/libpcm.a 93$(CODECDIR)/smaf.codec : $(CODECDIR)/libpcm.a
94$(CODECDIR)/au.codec : $(CODECDIR)/libpcm.a 94$(CODECDIR)/au.codec : $(CODECDIR)/libpcm.a
95$(CODECDIR)/vox.codec : $(CODECDIR)/libpcm.a 95$(CODECDIR)/vox.codec : $(CODECDIR)/libpcm.a
96$(CODECDIR)/wav64.codec : $(CODECDIR)/libpcm.a
96 97
97$(CODECS): $(CODECLIB) # this must be last in codec dependency list 98$(CODECS): $(CODECLIB) # this must be last in codec dependency list
98 99
diff --git a/apps/codecs/wav64.c b/apps/codecs/wav64.c
new file mode 100644
index 0000000000..e607a86a96
--- /dev/null
+++ b/apps/codecs/wav64.c
@@ -0,0 +1,447 @@
1/***************************************************************************
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 */
162enum codec_status codec_main(void)
163{
164 int status = CODEC_OK;
165 uint32_t decodedsamples;
166 uint32_t i;
167 size_t n;
168 int bufcount;
169 int endofstream;
170 unsigned char *buf;
171 uint8_t *wavbuf;
172 off_t firstblockposn; /* position of the first block in file */
173 const struct pcm_codec *codec;
174 uint64_t size;
175
176 /* Generic codec initialisation */
177 ci->configure(DSP_SET_SAMPLE_DEPTH, 28);
178
179next_track:
180 if (codec_init()) {
181 DEBUGF("codec_init() error\n");
182 status = CODEC_ERROR;
183 goto exit;
184 }
185
186 while (!*ci->taginfo_ready && !ci->stop_codec)
187 ci->sleep(1);
188
189 codec_set_replaygain(ci->id3);
190
191 /* Need to save offset for later use (cleared indirectly by advance_buffer) */
192 bytesdone = ci->id3->offset;
193
194 /* get RIFF chunk header */
195 buf = ci->request_buffer(&n, 40);
196 if (n < 40) {
197 DEBUGF("request_buffer error\n");
198 status = CODEC_ERROR;
199 goto done;
200 }
201 if ((memcmp(buf , WAVE64_GUID_RIFF, 16) != 0) ||
202 (memcmp(buf+24, WAVE64_GUID_WAVE, 16) != 0))
203 {
204 status = CODEC_ERROR;
205 goto done;
206 }
207
208 /* advance to first WAVE chunk */
209 ci->advance_buffer(40);
210
211 firstblockposn = 40;
212 ci->memset(&format, 0, sizeof(struct pcm_format));
213 format.is_signed = true;
214 format.is_little_endian = true;
215
216 decodedsamples = 0;
217 codec = 0;
218
219 /* iterate over WAVE chunks until the 'data' chunk, which should be after the 'fmt ' chunk */
220 while (true) {
221 /* get WAVE chunk header */
222 buf = ci->request_buffer(&n, 1024);
223 if (n < 8) {
224 DEBUGF("data chunk request_buffer error\n");
225 /* no more chunks, 'data' chunk must not have been found */
226 status = CODEC_ERROR;
227 goto done;
228 }
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);
235 status = CODEC_ERROR;
236 goto done;
237 }
238 /* wFormatTag */
239 format.formattag=buf[24]|(buf[25]<<8);
240 /* wChannels */
241 format.channels=buf[26]|(buf[27]<<8);
242 /* skipping dwSamplesPerSec */
243 /* skipping dwAvgBytesPerSec */
244 /* wBlockAlign */
245 format.blockalign=buf[36]|(buf[37]<<8);
246 /* wBitsPerSample */
247 format.bitspersample=buf[38]|(buf[39]<<8);
248 if (format.formattag != WAVE_FORMAT_PCM) {
249 if (size < 18) {
250 /* this is not a fatal error with some formats,
251 * we'll see later if we can't decode it */
252 DEBUGF("CODEC_WARNING: non-PCM WAVE (formattag=0x%x) "
253 "doesn't have ext. fmt descr (chunksize=%d<18).\n",
254 (unsigned int)format.formattag, (int)size);
255 }
256 else
257 {
258 format.size = buf[40]|(buf[41]<<8);
259 if (format.formattag != WAVE_FORMAT_EXTENSIBLE)
260 format.samplesperblock = buf[42]|(buf[43]<<8);
261 else {
262 if (format.size < 22) {
263 DEBUGF("CODEC_ERROR: WAVE_FORMAT_EXTENSIBLE is "
264 "missing extension\n");
265 status = CODEC_ERROR;
266 goto done;
267 }
268 /* wValidBitsPerSample */
269 format.bitspersample = buf[42]|(buf[43]<<8);
270 /* skipping dwChannelMask (4bytes) */
271 /* SubFormat (only get the first two bytes) */
272 format.formattag = buf[48]|(buf[49]<<8);
273 }
274 }
275 }
276
277 /* msadpcm specific */
278 if (format.formattag == WAVE_FORMAT_ADPCM)
279 {
280 if (!set_msadpcm_coeffs(buf))
281 {
282 status = CODEC_ERROR;
283 goto done;
284 }
285 }
286
287 /* get codec */
288 codec = get_wave_codec(format.formattag);
289 if (!codec)
290 {
291 DEBUGF("CODEC_ERROR: unsupported wave format 0x%x\n",
292 (unsigned int) format.formattag);
293 status = CODEC_ERROR;
294 goto done;
295 }
296
297 /* riff 8bit linear pcm is unsigned */
298 if (format.formattag == WAVE_FORMAT_PCM && format.bitspersample == 8)
299 format.is_signed = false;
300
301 /* check format, and calculate chunk size */
302 if (!codec->set_format(&format))
303 {
304 status = CODEC_ERROR;
305 goto done;
306 }
307 } else if (memcmp(buf, WAVE64_GUID_DATA, 16) == 0) {
308 format.numbytes = size;
309 /* advance to start of data */
310 ci->advance_buffer(24);
311 firstblockposn += 24;
312 break;
313 } else if (memcmp(buf, WAVE64_GUID_FACT, 16) == 0) {
314 /* skip 'fact' chunk */
315 } else {
316 DEBUGF("unknown Wave64 chunk: "
317 "'%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x'\n",
318 buf[0], buf[1], buf[ 2], buf[ 3], buf[ 4], buf[ 5], buf[ 6], buf[ 7],
319 buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]);
320 }
321
322 /* go to next chunk (8byte bound) */
323 if (size & 0x07)
324 size += 8 - (size & 0x7);
325 ci->advance_buffer(size + 24);
326 firstblockposn += size + 24;
327 }
328
329 if (!codec)
330 {
331 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found\n");
332 status = CODEC_ERROR;
333 goto done;
334 }
335
336 /* common format check */
337 if (format.channels == 0) {
338 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-channels file\n");
339 status = CODEC_ERROR;
340 goto done;
341 }
342 if (format.samplesperblock == 0) {
343 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-wSamplesPerBlock file\n");
344 status = CODEC_ERROR;
345 goto done;
346 }
347 if (format.blockalign == 0)
348 {
349 DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-blockalign file\n");
350 i = CODEC_ERROR;
351 goto done;
352 }
353 if (format.numbytes == 0) {
354 DEBUGF("CODEC_ERROR: 'data' chunk not found or has zero-length\n");
355 status = CODEC_ERROR;
356 goto done;
357 }
358
359 /* check chunksize */
360 if ((format.chunksize / format.blockalign) * format.samplesperblock * format.channels
361 > PCM_SAMPLE_SIZE)
362 format.chunksize = (PCM_SAMPLE_SIZE / format.blockalign) * format.blockalign;
363 if (format.chunksize == 0)
364 {
365 DEBUGF("CODEC_ERROR: chunksize is 0\n");
366 i = CODEC_ERROR;
367 goto done;
368 }
369
370 ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency);
371 if (format.channels == 2) {
372 ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
373 } else if (format.channels == 1) {
374 ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
375 } else {
376 DEBUGF("CODEC_ERROR: more than 2 channels\n");
377 status = CODEC_ERROR;
378 goto done;
379 }
380
381 /* make sure we're at the correct offset */
382 if (bytesdone > (uint32_t) firstblockposn) {
383 /* Round down to previous block */
384 uint32_t offset = bytesdone - bytesdone % format.blockalign;
385
386 ci->advance_buffer(offset-firstblockposn);
387 bytesdone = offset - firstblockposn;
388 } else {
389 /* already where we need to be */
390 bytesdone = 0;
391 }
392
393 /* The main decoder loop */
394 endofstream = 0;
395
396 while (!endofstream) {
397 ci->yield();
398 if (ci->stop_codec || ci->new_track) {
399 break;
400 }
401
402 if (ci->seek_time) {
403 struct pcm_pos *newpos = codec->get_seek_pos(ci->seek_time, &read_buffer);
404
405 decodedsamples = newpos->samples;
406 if (newpos->pos > format.numbytes)
407 break;
408 if (ci->seek_buffer(firstblockposn + newpos->pos))
409 {
410 bytesdone = newpos->pos;
411 }
412 ci->seek_complete();
413 }
414
415 wavbuf = (uint8_t *)ci->request_buffer(&n, format.chunksize);
416 if (n == 0)
417 break; /* End of stream */
418 if (bytesdone + n > format.numbytes) {
419 n = format.numbytes - bytesdone;
420 endofstream = 1;
421 }
422
423 status = codec->decode(wavbuf, n, samples, &bufcount);
424 if (status == CODEC_ERROR)
425 {
426 DEBUGF("codec error\n");
427 goto done;
428 }
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 }
439 status = CODEC_OK;
440
441done:
442 if (ci->request_next_track())
443 goto next_track;
444
445exit:
446 return status;
447}
diff --git a/apps/filetypes.c b/apps/filetypes.c
index 7837fbc73b..4aefef1f0d 100644
--- a/apps/filetypes.c
+++ b/apps/filetypes.c
@@ -105,6 +105,7 @@ static const struct filetype inbuilt_filetypes[] = {
105 { "au", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, 105 { "au", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
106 { "snd", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, 106 { "snd", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
107 { "vox", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA }, 107 { "vox", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
108 { "w64", FILE_ATTR_AUDIO, Icon_Audio, VOICE_EXT_MPA },
108#endif 109#endif
109 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST }, 110 { "m3u", FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
110 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST }, 111 { "m3u8",FILE_ATTR_M3U, Icon_Playlist, LANG_PLAYLIST },
diff --git a/apps/metadata.c b/apps/metadata.c
index 4e6e47e2bb..1dfc3763c6 100644
--- a/apps/metadata.c
+++ b/apps/metadata.c
@@ -174,6 +174,9 @@ const struct afmt_entry audio_formats[AFMT_NUM_CODECS] =
174 /* VOX (Dialogic telephony file formats) */ 174 /* VOX (Dialogic telephony file formats) */
175 [AFMT_VOX] = 175 [AFMT_VOX] =
176 AFMT_ENTRY("VOX", "vox", NULL, "vox\0" ), 176 AFMT_ENTRY("VOX", "vox", NULL, "vox\0" ),
177 /* Wave64 */
178 [AFMT_WAVE64] =
179 AFMT_ENTRY("WAVE64", "wav64", NULL, "w64\0" ),
177#endif 180#endif
178}; 181};
179 182
@@ -481,6 +484,14 @@ bool get_metadata(struct mp3entry* id3, int fd, const char* trackname)
481 } 484 }
482 break; 485 break;
483 486
487 case AFMT_WAVE64:
488 if (!get_wave64_metadata(fd, id3))
489 {
490 DEBUGF("get_wave64_metadata error\n");
491 return false;
492 }
493 break;
494
484#endif /* CONFIG_CODEC == SWCODEC */ 495#endif /* CONFIG_CODEC == SWCODEC */
485 496
486 default: 497 default:
diff --git a/apps/metadata.h b/apps/metadata.h
index 8ca3f2f746..7bf086f27d 100644
--- a/apps/metadata.h
+++ b/apps/metadata.h
@@ -81,6 +81,7 @@ enum
81 AFMT_SMAF, /* SMAF */ 81 AFMT_SMAF, /* SMAF */
82 AFMT_AU, /* Sun Audio file */ 82 AFMT_AU, /* Sun Audio file */
83 AFMT_VOX, /* VOX */ 83 AFMT_VOX, /* VOX */
84 AFMT_WAVE64, /* Wave64 */
84#endif 85#endif
85 86
86 /* add new formats at any index above this line to have a sensible order - 87 /* add new formats at any index above this line to have a sensible order -
diff --git a/apps/metadata/metadata_common.c b/apps/metadata/metadata_common.c
index 318399693c..fd1cd553e8 100644
--- a/apps/metadata/metadata_common.c
+++ b/apps/metadata/metadata_common.c
@@ -174,6 +174,15 @@ int read_uint64le(int fd, uint64_t* buf)
174} 174}
175#endif 175#endif
176 176
177/* Read an unaligned 64-bit little endian unsigned integer from buffer. */
178uint64_t get_uint64_le(void* buf)
179{
180 unsigned char* p = (unsigned char*) buf;
181
182 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24) | ((uint64_t)p[4] << 32) |
183 ((uint64_t)p[5] << 40) | ((uint64_t)p[6] << 48) | ((uint64_t)p[7] << 56);
184}
185
177/* Read an unaligned 32-bit little endian long from buffer. */ 186/* Read an unaligned 32-bit little endian long from buffer. */
178unsigned long get_long_le(void* buf) 187unsigned long get_long_le(void* buf)
179{ 188{
diff --git a/apps/metadata/metadata_common.h b/apps/metadata/metadata_common.h
index c7d7d67dfb..3962e2cd17 100644
--- a/apps/metadata/metadata_common.h
+++ b/apps/metadata/metadata_common.h
@@ -57,6 +57,7 @@ int read_uint64be(int fd, uint64_t* buf);
57#define read_uint64le(fd,buf) read((fd), (buf), 8) 57#define read_uint64le(fd,buf) read((fd), (buf), 8)
58#endif 58#endif
59 59
60uint64_t get_uint64_le(void* buf);
60unsigned long get_long_le(void* buf); 61unsigned long get_long_le(void* buf);
61unsigned short get_short_le(void* buf); 62unsigned short get_short_le(void* buf);
62unsigned long get_long_be(void* buf); 63unsigned long get_long_be(void* buf);
diff --git a/apps/metadata/metadata_parsers.h b/apps/metadata/metadata_parsers.h
index 3e7abb5c53..0e813ccb48 100644
--- a/apps/metadata/metadata_parsers.h
+++ b/apps/metadata/metadata_parsers.h
@@ -45,3 +45,4 @@ bool get_oma_metadata(int fd, struct mp3entry* id3);
45bool get_smaf_metadata(int fd, struct mp3entry* id3); 45bool get_smaf_metadata(int fd, struct mp3entry* id3);
46bool get_au_metadata(int fd, struct mp3entry* id3); 46bool get_au_metadata(int fd, struct mp3entry* id3);
47bool get_vox_metadata(int fd, struct mp3entry* id3); 47bool get_vox_metadata(int fd, struct mp3entry* id3);
48bool get_wave64_metadata(int fd, struct mp3entry* id3);
diff --git a/apps/metadata/wave.c b/apps/metadata/wave.c
index 79bb8178bd..8fe755735d 100644
--- a/apps/metadata/wave.c
+++ b/apps/metadata/wave.c
@@ -8,6 +8,7 @@
8 * $Id$ 8 * $Id$
9 * 9 *
10 * Copyright (C) 2005 Dave Chapman 10 * Copyright (C) 2005 Dave Chapman
11 * Copyright (C) 2010 Yoshihisa Uchida
11 * 12 *
12 * This program is free software; you can redistribute it and/or 13 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License 14 * modify it under the terms of the GNU General Public License
@@ -41,6 +42,15 @@
41 ((uint8_t*)(p))[1] = (d)>>8; \ 42 ((uint8_t*)(p))[1] = (d)>>8; \
42 } while(0) 43 } while(0)
43 44
45/* Wave(RIFF)/Wave64 format */
46
47/* Wave64 GUIDs */
48#define WAVE64_GUID_RIFF "riff\x2e\x91\xcf\x11\xa5\xd6\x28\xdb\x04\xc1\x00\x00"
49#define WAVE64_GUID_WAVE "wave\xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
50#define WAVE64_GUID_FMT "fmt \xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
51#define WAVE64_GUID_FACT "fact\xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
52#define WAVE64_GUID_DATA "data\xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
53
44enum 54enum
45{ 55{
46 WAVE_FORMAT_PCM = 0x0001, /* Microsoft PCM Format */ 56 WAVE_FORMAT_PCM = 0x0001, /* Microsoft PCM Format */
@@ -64,7 +74,7 @@ struct wave_fmt {
64 unsigned int blockalign; 74 unsigned int blockalign;
65 unsigned long bitspersample; 75 unsigned long bitspersample;
66 unsigned int samplesperblock; 76 unsigned int samplesperblock;
67 unsigned long numbytes; 77 uint64_t numbytes;
68}; 78};
69 79
70static unsigned long get_totalsamples(struct wave_fmt *fmt, struct mp3entry* id3) 80static unsigned long get_totalsamples(struct wave_fmt *fmt, struct mp3entry* id3)
@@ -114,6 +124,28 @@ static unsigned long get_totalsamples(struct wave_fmt *fmt, struct mp3entry* id3
114 return totalsamples; 124 return totalsamples;
115} 125}
116 126
127static void parse_riff_format(unsigned char* buf, int fmtsize, struct wave_fmt *fmt,
128 struct mp3entry* id3)
129{
130 /* wFormatTag */
131 fmt->formattag = buf[0] | (buf[1] << 8);
132 /* wChannels */
133 fmt->channels = buf[2] | (buf[3] << 8);
134 /* dwSamplesPerSec */
135 id3->frequency = get_long_le(&buf[4]);
136 /* dwAvgBytesPerSec */
137 id3->bitrate = (get_long_le(&buf[8]) * 8) / 1000;
138 /* wBlockAlign */
139 fmt->blockalign = buf[12] | (buf[13] << 8);
140 /* wBitsPerSample */
141 fmt->bitspersample = buf[14] | (buf[15] << 8);
142 if (fmtsize > 19)
143 {
144 /* wSamplesPerBlock */
145 fmt->samplesperblock = buf[18] | (buf[19] << 8);
146 }
147}
148
117bool get_wave_metadata(int fd, struct mp3entry* id3) 149bool get_wave_metadata(int fd, struct mp3entry* id3)
118{ 150{
119 /* Use the trackname part of the id3 structure as a temporary buffer */ 151 /* Use the trackname part of the id3 structure as a temporary buffer */
@@ -166,24 +198,7 @@ bool get_wave_metadata(int fd, struct mp3entry* id3)
166 offset += read_bytes; 198 offset += read_bytes;
167 i -= read_bytes; 199 i -= read_bytes;
168 200
169 /* wFormatTag */ 201 parse_riff_format(buf, i, &fmt, id3);
170 fmt.formattag = buf[0] | (buf[1] << 8);
171 /* wChannels */
172 fmt.channels = buf[2] | (buf[3] << 8);
173 /* dwSamplesPerSec */
174 id3->frequency = get_long_le(&buf[4]);
175 /* dwAvgBytesPerSec */
176 id3->bitrate = (get_long_le(&buf[8]) * 8) / 1000;
177 /* wBlockAlign */
178 fmt.blockalign = buf[12] | (buf[13] << 8);
179 id3->bytesperframe = fmt.blockalign;
180 /* wBitsPerSample */
181 fmt.bitspersample = buf[14] | (buf[15] << 8);
182 if (read_bytes > 19)
183 {
184 /* wSamplesPerBlock */
185 fmt.samplesperblock = buf[18] | (buf[19] << 8);
186 }
187 202
188 /* Check for ATRAC3 stream */ 203 /* Check for ATRAC3 stream */
189 if (fmt.formattag == WAVE_FORMAT_ATRAC3) 204 if (fmt.formattag == WAVE_FORMAT_ATRAC3)
@@ -256,3 +271,96 @@ bool get_wave_metadata(int fd, struct mp3entry* id3)
256 271
257 return true; 272 return true;
258} 273}
274
275bool get_wave64_metadata(int fd, struct mp3entry* id3)
276{
277 /* Use the trackname part of the id3 structure as a temporary buffer */
278 unsigned char* buf = (unsigned char *)id3->path;
279 struct wave_fmt fmt;
280 unsigned long totalsamples = 0;
281 int read_bytes;
282 uint64_t i;
283
284 memset(&fmt, 0, sizeof(struct wave_fmt));
285
286 /* get RIFF chunk header */
287 if ((lseek(fd, 0, SEEK_SET) < 0) || (read(fd, buf, 40) < 40))
288 return false;
289
290 if ((memcmp(buf , WAVE64_GUID_RIFF, 16) != 0)||
291 (memcmp(buf+24, WAVE64_GUID_WAVE, 16) != 0))
292 {
293 DEBUGF("metada error: does not wave64 file\n");
294 return false;
295 }
296
297 /* iterate over WAVE chunks until 'data' chunk */
298 while (true)
299 {
300 /* get chunk header */
301 if (read(fd, buf, 24) < 24)
302 return false;
303
304 /* chunkSize (excludes GUID and size length) */
305 i = get_uint64_le(&buf[16]) - 24;
306
307 if (memcmp(buf, WAVE64_GUID_FMT, 16) == 0)
308 {
309 DEBUGF("find 'fmt ' chunk\n");
310 if (i < 16)
311 return false;
312
313 read_bytes = 16;
314 if (i > 16)
315 {
316 read_bytes = 24;
317 if (i < 24)
318 i = 24;
319 }
320
321 /* get rest of chunk */
322 if (read(fd, buf, read_bytes) < read_bytes)
323 return false;
324
325 i -= read_bytes;
326 parse_riff_format(buf, read_bytes, &fmt, id3);
327 }
328 else if (memcmp(buf, WAVE64_GUID_DATA, 16) == 0)
329 {
330 DEBUGF("find 'data' chunk\n");
331 fmt.numbytes = i;
332 break;
333 }
334 else if (memcmp(buf, WAVE64_GUID_FACT, 16) == 0)
335 {
336 /* Skip 'fact' chunk */
337 DEBUGF("find 'fact' chunk\n");
338 }
339
340 /* seek to next chunk (8byte bound) */
341 if (i & 0x07)
342 i += 8 - (i & 0x7);
343
344 if(lseek(fd, i, SEEK_CUR) < 0)
345 return false;
346 }
347
348 if ((fmt.numbytes == 0) || (fmt.channels == 0) || (fmt.blockalign == 0))
349 {
350 DEBUGF("metadata error: numbytes, channels, or blockalign is 0\n");
351 return false;
352 }
353
354 if (totalsamples == 0)
355 {
356 totalsamples = get_totalsamples(&fmt, id3);
357 }
358
359 id3->vbr = false; /* All Wave64 files are CBR */
360 id3->filesize = filesize(fd);
361
362 /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
363 id3->length = ((int64_t) totalsamples * 1000) / id3->frequency;
364
365 return true;
366}