Submit FS#11843 v17. Integrate YUV-blitting of nano 2G to nano1G/color LCD driver...
[kugel-rb.git] / apps / metadata / wave.c
blob5401b68146cf6749aee460d6a91d0ef31fd610b6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
11 * Copyright (C) 2010 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 ****************************************************************************/
22 #include <stdio.h>
23 #include <string.h>
24 #include <inttypes.h>
26 #include "system.h"
27 #include "metadata.h"
28 #include "metadata_common.h"
29 #include "metadata_parsers.h"
30 #include "rbunicode.h"
31 #include "logf.h"
33 /* Wave(RIFF)/Wave64 format */
36 # define AV_WL32(p, d) do { \
37 ((uint8_t*)(p))[0] = (d); \
38 ((uint8_t*)(p))[1] = (d)>>8; \
39 ((uint8_t*)(p))[2] = (d)>>16; \
40 ((uint8_t*)(p))[3] = (d)>>24; \
41 } while(0)
42 # define AV_WL16(p, d) do { \
43 ((uint8_t*)(p))[0] = (d); \
44 ((uint8_t*)(p))[1] = (d)>>8; \
45 } while(0)
47 enum {
48 RIFF_CHUNK = 0,
49 WAVE_CHUNK,
50 FMT_CHUNK,
51 FACT_CHUNK,
52 DATA_CHUNK,
53 LIST_CHUNK,
56 /* Wave chunk names */
57 #define WAVE_CHUNKNAME_LENGTH 4
58 #define WAVE_CHUNKSIZE_LENGTH 4
60 static const unsigned char *wave_chunklist = "RIFF"
61 "WAVE"
62 "fmt "
63 "fact"
64 "data"
65 "LIST";
67 /* Wave64 GUIDs */
68 #define WAVE64_CHUNKNAME_LENGTH 16
69 #define WAVE64_CHUNKSIZE_LENGTH 8
71 static const unsigned char *wave64_chunklist
72 = "riff\x2e\x91\xcf\x11\xa5\xd6\x28\xdb\x04\xc1\x00\x00"
73 "wave\xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
74 "fmt \xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
75 "fact\xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
76 "data\xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
77 "\xbc\x94\x5f\x92\x5a\x52\xd2\x11\x86\xdc\x00\xc0\x4f\x8e\xdb\x8a";
79 /* list/info chunk */
81 struct info_chunk {
82 const unsigned char* tag;
83 size_t offset;
86 /* info chunk names are common wave/wave64 */
87 static const struct info_chunk info_chunks[] = {
88 { "INAM", offsetof(struct mp3entry, title), }, /* title */
89 { "IART", offsetof(struct mp3entry, artist), }, /* artist */
90 { "ISBJ", offsetof(struct mp3entry, albumartist), }, /* albumartist */
91 { "IPRD", offsetof(struct mp3entry, album), }, /* album */
92 { "IWRI", offsetof(struct mp3entry, composer), }, /* composer */
93 { "ICMT", offsetof(struct mp3entry, comment), }, /* comment */
94 { "ISRF", offsetof(struct mp3entry, grouping), }, /* grouping */
95 { "IGNR", offsetof(struct mp3entry, genre_string), }, /* genre */
96 { "ICRD", offsetof(struct mp3entry, year_string), }, /* date */
97 { "IPRT", offsetof(struct mp3entry, track_string), }, /* track/trackcount */
98 { "IFRM", offsetof(struct mp3entry, disc_string), }, /* disc/disccount */
101 #define INFO_CHUNK_COUNT ((int)ARRAYLEN(info_chunks))
103 /* support formats */
104 enum
106 WAVE_FORMAT_PCM = 0x0001, /* Microsoft PCM Format */
107 WAVE_FORMAT_ADPCM = 0x0002, /* Microsoft ADPCM Format */
108 WAVE_FORMAT_IEEE_FLOAT = 0x0003, /* IEEE Float */
109 WAVE_FORMAT_ALAW = 0x0006, /* Microsoft ALAW */
110 WAVE_FORMAT_MULAW = 0x0007, /* Microsoft MULAW */
111 WAVE_FORMAT_DVI_ADPCM = 0x0011, /* Intel's DVI ADPCM */
112 WAVE_FORMAT_DIALOGIC_OKI_ADPCM = 0x0017, /* Dialogic OKI ADPCM */
113 WAVE_FORMAT_YAMAHA_ADPCM = 0x0020, /* Yamaha ADPCM */
114 WAVE_FORMAT_XBOX_ADPCM = 0x0069, /* XBOX ADPCM */
115 IBM_FORMAT_MULAW = 0x0101, /* same as WAVE_FORMAT_MULAW */
116 IBM_FORMAT_ALAW = 0x0102, /* same as WAVE_FORMAT_ALAW */
117 WAVE_FORMAT_ATRAC3 = 0x0270, /* Atrac3 stream */
118 WAVE_FORMAT_SWF_ADPCM = 0x5346, /* Adobe SWF ADPCM */
119 WAVE_FORMAT_EXTENSIBLE = 0xFFFE,
122 struct wave_fmt {
123 unsigned int formattag;
124 unsigned int channels;
125 unsigned int blockalign;
126 unsigned int bitspersample;
127 unsigned int samplesperblock;
128 uint32_t totalsamples;
129 uint64_t numbytes;
132 static unsigned char *convert_utf8(const unsigned char *src, unsigned char *dst,
133 int size, bool is_64)
135 if (is_64)
137 /* Note: wave64: metadata codepage is UTF-16 only */
138 return utf16LEdecode(src, dst, size);
140 return iso_decode(src, dst, -1, size);
143 static void set_totalsamples(struct wave_fmt *fmt, struct mp3entry* id3)
145 switch (fmt->formattag)
147 case WAVE_FORMAT_PCM:
148 case WAVE_FORMAT_IEEE_FLOAT:
149 case WAVE_FORMAT_ALAW:
150 case WAVE_FORMAT_MULAW:
151 case IBM_FORMAT_ALAW:
152 case IBM_FORMAT_MULAW:
153 fmt->blockalign = fmt->bitspersample * fmt->channels >> 3;
154 fmt->samplesperblock = 1;
155 break;
156 case WAVE_FORMAT_YAMAHA_ADPCM:
157 if (id3->channels != 0)
159 fmt->samplesperblock =
160 (fmt->blockalign == ((id3->frequency / 60) + 4) * fmt->channels)?
161 id3->frequency / 30 : (fmt->blockalign << 1) / fmt->channels;
163 break;
164 case WAVE_FORMAT_DIALOGIC_OKI_ADPCM:
165 fmt->blockalign = 1;
166 fmt->samplesperblock = 2;
167 break;
168 case WAVE_FORMAT_SWF_ADPCM:
169 if (fmt->bitspersample != 0 && id3->channels != 0)
171 fmt->samplesperblock
172 = (((fmt->blockalign << 3) - 2) / fmt->channels - 22)
173 / fmt->bitspersample + 1;
175 break;
176 default:
177 break;
180 if (fmt->blockalign != 0)
181 fmt->totalsamples = (fmt->numbytes / fmt->blockalign) * fmt->samplesperblock;
184 static void parse_riff_format(unsigned char* buf, int fmtsize, struct wave_fmt *fmt,
185 struct mp3entry* id3)
187 /* wFormatTag */
188 fmt->formattag = buf[0] | (buf[1] << 8);
189 /* wChannels */
190 fmt->channels = buf[2] | (buf[3] << 8);
191 /* dwSamplesPerSec */
192 id3->frequency = get_long_le(&buf[4]);
193 /* dwAvgBytesPerSec */
194 id3->bitrate = (get_long_le(&buf[8]) * 8) / 1000;
195 /* wBlockAlign */
196 fmt->blockalign = buf[12] | (buf[13] << 8);
197 /* wBitsPerSample */
198 fmt->bitspersample = buf[14] | (buf[15] << 8);
200 if (fmt->formattag != WAVE_FORMAT_EXTENSIBLE)
202 if (fmtsize > 19)
204 /* wSamplesPerBlock */
205 fmt->samplesperblock = buf[18] | (buf[19] << 8);
208 else if (fmtsize > 25)
210 /* wValidBitsPerSample */
211 fmt->bitspersample = buf[18] | (buf[19] << 8);
212 /* SubFormat */
213 fmt->formattag = buf[24] | (buf[25] << 8);
216 /* Check for ATRAC3 stream */
217 if (fmt->formattag == WAVE_FORMAT_ATRAC3)
219 int jsflag = 0;
220 if(id3->bitrate == 66 || id3->bitrate == 94)
221 jsflag = 1;
223 id3->extradata_size = 14;
224 id3->channels = 2;
225 id3->codectype = AFMT_OMA_ATRAC3;
226 id3->bytesperframe = fmt->blockalign;
228 /* Store the extradata for the codec */
229 AV_WL16(&id3->id3v2buf[0], 1); // always 1
230 AV_WL32(&id3->id3v2buf[2], id3->frequency);// samples rate
231 AV_WL16(&id3->id3v2buf[6], jsflag); // coding mode
232 AV_WL16(&id3->id3v2buf[8], jsflag); // coding mode
233 AV_WL16(&id3->id3v2buf[10], 1); // always 1
234 AV_WL16(&id3->id3v2buf[12], 0); // always 0
238 static void parse_list_chunk(int fd, struct mp3entry* id3, int chunksize, bool is_64)
240 unsigned char tmpbuf[ID3V2_BUF_SIZE];
241 unsigned char *bp = tmpbuf;
242 unsigned char *endp;
243 unsigned char *data_pos;
244 unsigned char *tag_pos = id3->id3v2buf;
245 int datasize;
246 int infosize;
247 int remain;
248 int i;
250 if (is_64)
251 lseek(fd, 4, SEEK_CUR);
252 else if (read(fd, bp, 4) < 4 || memcmp(bp, "INFO", 4))
253 return;
255 /* decrease skip bytes */
256 chunksize -= 4;
258 infosize = read(fd, bp, (ID3V2_BUF_SIZE > chunksize)? chunksize : ID3V2_BUF_SIZE);
259 if (infosize <= 8)
260 return;
262 endp = bp + infosize;
263 while (bp < endp)
265 datasize = get_long_le(bp + 4);
266 data_pos = bp + 8;
267 remain = ID3V2_BUF_SIZE - (tag_pos - (unsigned char*)id3->id3v2buf);
268 if (remain < 1)
269 break;
271 for (i = 0; i < INFO_CHUNK_COUNT; i++)
273 if (memcmp(bp, info_chunks[i].tag, 4) == 0)
275 *((char **)(((char*)id3) + info_chunks[i].offset)) = tag_pos;
276 tag_pos = convert_utf8(data_pos, tag_pos,
277 (datasize + 1 >= remain )? remain - 1 : datasize,
278 is_64);
279 *tag_pos++ = 0;
280 break;
283 bp = data_pos + datasize + (datasize & 1);
287 static bool read_header(int fd, struct mp3entry* id3, const unsigned char *chunknames,
288 bool is_64)
290 /* Use the temporary buffer */
291 unsigned char* buf = (unsigned char *)id3->path;
293 struct wave_fmt fmt;
295 const unsigned int namelen = (is_64)? WAVE64_CHUNKNAME_LENGTH : WAVE_CHUNKNAME_LENGTH;
296 const unsigned int sizelen = (is_64)? WAVE64_CHUNKSIZE_LENGTH : WAVE_CHUNKSIZE_LENGTH;
297 const unsigned int len = namelen + sizelen;
298 uint64_t chunksize;
299 uint64_t offset = len + namelen;
300 int read_data;
302 memset(&fmt, 0, sizeof(struct wave_fmt));
304 id3->vbr = false; /* All Wave/Wave64 files are CBR */
305 id3->filesize = filesize(fd);
307 /* get RIFF chunk header */
308 lseek(fd, 0, SEEK_SET);
309 read(fd, buf, offset);
311 if ((memcmp(buf, chunknames + RIFF_CHUNK * namelen, namelen) != 0) ||
312 (memcmp(buf + len, chunknames + WAVE_CHUNK * namelen, namelen) != 0))
314 DEBUGF("metadata error: missing riff header.\n");
315 return false;
318 /* iterate over WAVE chunks until 'data' chunk */
319 while (read(fd, buf, len) > 0)
321 offset += len;
323 /* get chunk size (when the header is wave64, chunksize includes GUID and data length) */
324 chunksize = (is_64) ? get_uint64_le(buf + namelen) - len :
325 get_long_le(buf + namelen);
327 read_data = 0;
328 if (memcmp(buf, chunknames + FMT_CHUNK * namelen, namelen) == 0)
330 DEBUGF("find 'fmt ' chunk\n");
332 if (chunksize < 16)
334 DEBUGF("metadata error: 'fmt ' chunk is too small: %d\n", (int)chunksize);
335 return false;
338 /* get and parse format */
339 read_data = (chunksize > 25)? 26 : chunksize;
341 read(fd, buf, read_data);
342 parse_riff_format(buf, read_data, &fmt, id3);
344 else if (memcmp(buf, chunknames + FACT_CHUNK * namelen, namelen) == 0)
346 DEBUGF("find 'fact' chunk\n");
348 /* dwSampleLength */
349 if (chunksize >= sizelen)
351 /* get totalsamples */
352 read_data = sizelen;
353 read(fd, buf, read_data);
354 fmt.totalsamples = (is_64)? get_uint64_le(buf) : get_long_le(buf);
357 else if (memcmp(buf, chunknames + DATA_CHUNK * namelen, namelen) == 0)
359 DEBUGF("find 'data' chunk\n");
360 fmt.numbytes = chunksize;
361 if (fmt.formattag == WAVE_FORMAT_ATRAC3)
362 id3->first_frame_offset = offset;
364 else if (memcmp(buf, chunknames + LIST_CHUNK * namelen, namelen) == 0)
366 DEBUGF("find 'LIST' chunk\n");
367 parse_list_chunk(fd, id3, chunksize, is_64);
368 lseek(fd, offset, SEEK_SET);
371 /* padded to next chunk */
372 chunksize += ((is_64)? ((1 + ~chunksize) & 0x07) : (chunksize & 1));
374 offset += chunksize;
375 if (offset >= id3->filesize)
376 break;
378 lseek(fd, chunksize - read_data, SEEK_CUR);
381 if (fmt.numbytes == 0)
383 DEBUGF("metadata error: read error or missing 'data' chunk.\n");
384 return false;
387 if (fmt.totalsamples == 0)
388 set_totalsamples(&fmt, id3);
390 if (id3->frequency == 0 || id3->bitrate == 0)
392 DEBUGF("metadata error: frequency or bitrate is 0\n");
393 return false;
396 /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
397 id3->length = (fmt.formattag != WAVE_FORMAT_ATRAC3)?
398 (uint64_t)fmt.totalsamples * 1000 / id3->frequency :
399 ((id3->filesize - id3->first_frame_offset) * 8) / id3->bitrate;
401 /* output header/id3 info (for debug) */
402 DEBUGF("%s header info ----\n", (is_64)? "wave64" : "wave");
403 DEBUGF(" format: %04x\n", (int)fmt.formattag);
404 DEBUGF(" channels: %u\n", fmt.channels);
405 DEBUGF(" blockalign: %u\n", fmt.blockalign);
406 DEBUGF(" bitspersample: %u\n", fmt.bitspersample);
407 DEBUGF(" samplesperblock: %u\n", fmt.samplesperblock);
408 DEBUGF(" totalsamples: %u\n", (unsigned int)fmt.totalsamples);
409 DEBUGF(" numbytes: %u\n", (unsigned int)fmt.numbytes);
410 DEBUGF("id3 info ----\n");
411 DEBUGF(" frequency: %u\n", (unsigned int)id3->frequency);
412 DEBUGF(" bitrate: %d\n", id3->bitrate);
413 DEBUGF(" length: %u\n", (unsigned int)id3->length);
415 return true;
418 bool get_wave_metadata(int fd, struct mp3entry* id3)
420 return read_header(fd, id3, wave_chunklist, false);
423 bool get_wave64_metadata(int fd, struct mp3entry* id3)
425 return read_header(fd, id3, wave64_chunklist, true);