FS#11195, plus. Simplified hotkey struct, thanks alle!
[kugel-rb.git] / apps / metadata / wave.c
blobeeb4f0f3431c17c3c311b2b324d20da114be6e6c
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 "logf.h"
32 /* Wave(RIFF)/Wave64 format */
35 # define AV_WL32(p, d) do { \
36 ((uint8_t*)(p))[0] = (d); \
37 ((uint8_t*)(p))[1] = (d)>>8; \
38 ((uint8_t*)(p))[2] = (d)>>16; \
39 ((uint8_t*)(p))[3] = (d)>>24; \
40 } while(0)
41 # define AV_WL16(p, d) do { \
42 ((uint8_t*)(p))[0] = (d); \
43 ((uint8_t*)(p))[1] = (d)>>8; \
44 } while(0)
46 enum {
47 RIFF_CHUNK = 0,
48 WAVE_CHUNK,
49 FMT_CHUNK,
50 FACT_CHUNK,
51 DATA_CHUNK,
54 /* Wave chunk names */
55 #define WAVE_CHUNKNAME_LENGTH 4
56 #define WAVE_CHUNKSIZE_LENGTH 4
58 static const unsigned char *wave_chunklist = "RIFF"
59 "WAVE"
60 "fmt "
61 "fact"
62 "data";
64 /* Wave64 GUIDs */
65 #define WAVE64_CHUNKNAME_LENGTH 16
66 #define WAVE64_CHUNKSIZE_LENGTH 8
68 static const unsigned char *wave64_chunklist
69 = "riff\x2e\x91\xcf\x11\xa5\xd6\x28\xdb\x04\xc1\x00\x00"
70 "wave\xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
71 "fmt \xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
72 "fact\xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a"
73 "data\xf3\xac\xd3\x11\x8c\xd1\x00\xc0\x4f\x8e\xdb\x8a";
76 /* support formats */
77 enum
79 WAVE_FORMAT_PCM = 0x0001, /* Microsoft PCM Format */
80 WAVE_FORMAT_ADPCM = 0x0002, /* Microsoft ADPCM Format */
81 WAVE_FORMAT_IEEE_FLOAT = 0x0003, /* IEEE Float */
82 WAVE_FORMAT_ALAW = 0x0006, /* Microsoft ALAW */
83 WAVE_FORMAT_MULAW = 0x0007, /* Microsoft MULAW */
84 WAVE_FORMAT_DVI_ADPCM = 0x0011, /* Intel's DVI ADPCM */
85 WAVE_FORMAT_DIALOGIC_OKI_ADPCM = 0x0017, /* Dialogic OKI ADPCM */
86 WAVE_FORMAT_YAMAHA_ADPCM = 0x0020, /* Yamaha ADPCM */
87 WAVE_FORMAT_XBOX_ADPCM = 0x0069, /* XBOX ADPCM */
88 IBM_FORMAT_MULAW = 0x0101, /* same as WAVE_FORMAT_MULAW */
89 IBM_FORMAT_ALAW = 0x0102, /* same as WAVE_FORMAT_ALAW */
90 WAVE_FORMAT_ATRAC3 = 0x0270, /* Atrac3 stream */
91 WAVE_FORMAT_SWF_ADPCM = 0x5346, /* Adobe SWF ADPCM */
92 WAVE_FORMAT_EXTENSIBLE = 0xFFFE,
95 struct wave_fmt {
96 unsigned int formattag;
97 unsigned int channels;
98 unsigned int blockalign;
99 unsigned int bitspersample;
100 unsigned int samplesperblock;
101 uint32_t totalsamples;
102 uint64_t numbytes;
105 static void set_totalsamples(struct wave_fmt *fmt, struct mp3entry* id3)
107 switch (fmt->formattag)
109 case WAVE_FORMAT_PCM:
110 case WAVE_FORMAT_IEEE_FLOAT:
111 case WAVE_FORMAT_ALAW:
112 case WAVE_FORMAT_MULAW:
113 case IBM_FORMAT_ALAW:
114 case IBM_FORMAT_MULAW:
115 fmt->blockalign = fmt->bitspersample * fmt->channels >> 3;
116 fmt->samplesperblock = 1;
117 break;
118 case WAVE_FORMAT_YAMAHA_ADPCM:
119 if (id3->channels != 0)
121 fmt->samplesperblock =
122 (fmt->blockalign == ((id3->frequency / 60) + 4) * fmt->channels)?
123 id3->frequency / 30 : (fmt->blockalign << 1) / fmt->channels;
125 break;
126 case WAVE_FORMAT_DIALOGIC_OKI_ADPCM:
127 fmt->blockalign = 1;
128 fmt->samplesperblock = 2;
129 break;
130 case WAVE_FORMAT_SWF_ADPCM:
131 if (fmt->bitspersample != 0 && id3->channels != 0)
133 fmt->samplesperblock
134 = (((fmt->blockalign << 3) - 2) / fmt->channels - 22)
135 / fmt->bitspersample + 1;
137 break;
138 default:
139 break;
142 if (fmt->blockalign != 0)
143 fmt->totalsamples = (fmt->numbytes / fmt->blockalign) * fmt->samplesperblock;
146 static void parse_riff_format(unsigned char* buf, int fmtsize, struct wave_fmt *fmt,
147 struct mp3entry* id3)
149 /* wFormatTag */
150 fmt->formattag = buf[0] | (buf[1] << 8);
151 /* wChannels */
152 fmt->channels = buf[2] | (buf[3] << 8);
153 /* dwSamplesPerSec */
154 id3->frequency = get_long_le(&buf[4]);
155 /* dwAvgBytesPerSec */
156 id3->bitrate = (get_long_le(&buf[8]) * 8) / 1000;
157 /* wBlockAlign */
158 fmt->blockalign = buf[12] | (buf[13] << 8);
159 /* wBitsPerSample */
160 fmt->bitspersample = buf[14] | (buf[15] << 8);
162 if (fmt->formattag != WAVE_FORMAT_EXTENSIBLE)
164 if (fmtsize > 19)
166 /* wSamplesPerBlock */
167 fmt->samplesperblock = buf[18] | (buf[19] << 8);
170 else if (fmtsize > 25)
172 /* wValidBitsPerSample */
173 fmt->bitspersample = buf[18] | (buf[19] << 8);
174 /* SubFormat */
175 fmt->formattag = buf[24] | (buf[25] << 8);
178 /* Check for ATRAC3 stream */
179 if (fmt->formattag == WAVE_FORMAT_ATRAC3)
181 int jsflag = 0;
182 if(id3->bitrate == 66 || id3->bitrate == 94)
183 jsflag = 1;
185 id3->extradata_size = 14;
186 id3->channels = 2;
187 id3->codectype = AFMT_OMA_ATRAC3;
188 id3->bytesperframe = fmt->blockalign;
190 /* Store the extradata for the codec */
191 AV_WL16(&id3->id3v2buf[0], 1); // always 1
192 AV_WL32(&id3->id3v2buf[2], id3->frequency);// samples rate
193 AV_WL16(&id3->id3v2buf[6], jsflag); // coding mode
194 AV_WL16(&id3->id3v2buf[8], jsflag); // coding mode
195 AV_WL16(&id3->id3v2buf[10], 1); // always 1
196 AV_WL16(&id3->id3v2buf[12], 0); // always 0
200 static bool read_header(int fd, struct mp3entry* id3, const unsigned char *chunknames,
201 bool is_64)
203 /* Use the temporary buffer */
204 unsigned char* buf = (unsigned char *)id3->path;
206 struct wave_fmt fmt;
208 unsigned int namelen = (is_64)? WAVE64_CHUNKNAME_LENGTH : WAVE_CHUNKNAME_LENGTH;
209 unsigned int sizelen = (is_64)? WAVE64_CHUNKSIZE_LENGTH : WAVE_CHUNKSIZE_LENGTH;
210 unsigned int len = namelen + sizelen;
211 uint64_t chunksize;
212 uint64_t offset = len + namelen;
213 int read_data;
215 memset(&fmt, 0, sizeof(struct wave_fmt));
217 /* get RIFF chunk header */
218 lseek(fd, 0, SEEK_SET);
219 read(fd, buf, offset);
221 if ((memcmp(buf, chunknames + RIFF_CHUNK * namelen, namelen) != 0) ||
222 (memcmp(buf + len, chunknames + WAVE_CHUNK * namelen, namelen) != 0))
224 DEBUGF("metadata error: missing riff header.\n");
225 return false;
228 /* iterate over WAVE chunks until 'data' chunk */
229 while (true)
231 /* get chunk header */
232 if (read(fd, buf, len) <= 0)
234 DEBUGF("metadata error: read error or missing 'data' chunk.\n");
235 return false;
238 offset += len;
240 /* get chunk size (when the header is wave64, chunksize includes GUID and data length) */
241 chunksize = (is_64) ? get_uint64_le(buf + namelen) - len :
242 get_long_le(buf + namelen);
244 if (memcmp(buf, chunknames + DATA_CHUNK * namelen, namelen) == 0)
246 DEBUGF("find 'data' chunk\n");
247 fmt.numbytes = chunksize;
248 if (fmt.formattag == WAVE_FORMAT_ATRAC3)
249 id3->first_frame_offset = offset;
250 break;
253 /* padded to next chunk */
254 chunksize += ((is_64)? ((1 + ~chunksize) & 0x07) : (chunksize & 1));
255 offset += chunksize;
257 read_data = 0;
258 if (memcmp(buf, chunknames + FMT_CHUNK * namelen, namelen) == 0)
260 DEBUGF("find 'fmt ' chunk\n");
262 if (chunksize < 16)
264 DEBUGF("metadata error: 'fmt ' chunk is too small: %d\n", (int)chunksize);
265 return false;
268 /* get and parse format */
269 read_data = (chunksize > 25)? 26 : chunksize;
271 read(fd, buf, read_data);
272 parse_riff_format(buf, read_data, &fmt, id3);
274 else if (memcmp(buf, chunknames + FACT_CHUNK * namelen, namelen) == 0)
276 DEBUGF("find 'fact' chunk\n");
278 /* dwSampleLength */
279 if (chunksize >= sizelen)
281 /* get totalsamples */
282 read_data = sizelen;
283 read(fd, buf, read_data);
284 fmt.totalsamples = (is_64)? get_uint64_le(buf) : get_long_le(buf);
288 lseek(fd, chunksize - read_data, SEEK_CUR);
291 if (fmt.totalsamples == 0)
292 set_totalsamples(&fmt, id3);
294 if (id3->frequency == 0 || id3->bitrate == 0)
296 DEBUGF("metadata error: frequency or bitrate is 0\n");
297 return false;
300 id3->vbr = false; /* All Wave/Wave64 files are CBR */
301 id3->filesize = filesize(fd);
303 /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
304 id3->length = (fmt.formattag != WAVE_FORMAT_ATRAC3)?
305 (uint64_t)fmt.totalsamples * 1000 / id3->frequency :
306 ((id3->filesize - id3->first_frame_offset) * 8) / id3->bitrate;
308 /* output header/id3 info (for debug) */
309 DEBUGF("%s header info ----\n", (is_64)? "wave64" : "wave");
310 DEBUGF(" format: %04x\n", (int)fmt.formattag);
311 DEBUGF(" channels: %u\n", fmt.channels);
312 DEBUGF(" blockalign: %u\n", fmt.blockalign);
313 DEBUGF(" bitspersample: %u\n", fmt.bitspersample);
314 DEBUGF(" samplesperblock: %u\n", fmt.samplesperblock);
315 DEBUGF(" totalsamples: %u\n", (unsigned int)fmt.totalsamples);
316 DEBUGF(" numbytes; %u\n", (unsigned int)fmt.numbytes);
317 DEBUGF("id3 info ----\n");
318 DEBUGF(" frquency: %u\n", (unsigned int)id3->frequency);
319 DEBUGF(" bitrate: %d\n", id3->bitrate);
320 DEBUGF(" length: %u\n", (unsigned int)id3->length);
322 return true;
325 bool get_wave_metadata(int fd, struct mp3entry* id3)
327 return read_header(fd, id3, wave_chunklist, false);
330 bool get_wave64_metadata(int fd, struct mp3entry* id3)
332 return read_header(fd, id3, wave64_chunklist, true);