diff options
author | Magnus Holmgren <magnushol@gmail.com> | 2005-09-22 19:36:25 +0000 |
---|---|---|
committer | Magnus Holmgren <magnushol@gmail.com> | 2005-09-22 19:36:25 +0000 |
commit | b5f3365554d71fed035c641b07749a1a03b40ecb (patch) | |
tree | eefb8c58cb244fed9fef21975ca3ad0fb21c63de | |
parent | 711b2e3c886c52e04cfa3ed810f4a2ad688b5cf8 (diff) | |
download | rockbox-b5f3365554d71fed035c641b07749a1a03b40ecb.tar.gz rockbox-b5f3365554d71fed035c641b07749a1a03b40ecb.zip |
iriver: proper wav file handling by Frederic Devernay (patch #1245261). Adapted the metadata part to the new metadata.c, and did a few minor changes in the codec. Some of the file format checks in the codec should IMHO be moved to metadata.c, but I left that for later.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@7542 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r-- | apps/codecs/wav.c | 708 | ||||
-rw-r--r-- | apps/metadata.c | 130 |
2 files changed, 736 insertions, 102 deletions
diff --git a/apps/codecs/wav.c b/apps/codecs/wav.c index d2ae7bd6c3..36076caf25 100644 --- a/apps/codecs/wav.c +++ b/apps/codecs/wav.c | |||
@@ -21,11 +21,75 @@ | |||
21 | #include "playback.h" | 21 | #include "playback.h" |
22 | #include "lib/codeclib.h" | 22 | #include "lib/codeclib.h" |
23 | #include "dsp.h" | 23 | #include "dsp.h" |
24 | #include "inttypes.h" | ||
24 | 25 | ||
25 | #define BYTESWAP(x) (((x>>8) & 0xff) | ((x<<8) & 0xff00)) | 26 | /* This codec support WAVE files with the following formats: |
27 | * - PCM, up to 32 bits, supporting 32 bits playback when useful. | ||
28 | * - ALAW and MULAW (16 bits compressed on 8 bits). | ||
29 | * - DVI_ADPCM (16 bits compressed on 4 bits). | ||
30 | * | ||
31 | * For a good documentation on WAVE files, see: | ||
32 | * http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/WAVE/WAVE.html | ||
33 | * and | ||
34 | * http://www.sonicspot.com/guide/wavefiles.html | ||
35 | * | ||
36 | * For sample WAV files, see: | ||
37 | * http://www.tsp.ece.mcgill.ca/MMSP/Documents/AudioFormats/WAVE/Samples.html | ||
38 | * | ||
39 | * The most common formats seem to be PCM, ADPCM, DVI_ADPCM, IEEE_FLOAT, | ||
40 | * ALAW and MULAW | ||
41 | */ | ||
42 | |||
43 | /* These constants are from RFC 2361. */ | ||
44 | enum | ||
45 | { | ||
46 | WAVE_FORMAT_UNKNOWN = 0x0000, /* Microsoft Unknown Wave Format */ | ||
47 | WAVE_FORMAT_PCM = 0x0001, /* Microsoft PCM Format */ | ||
48 | WAVE_FORMAT_ADPCM = 0x0002, /* Microsoft ADPCM Format */ | ||
49 | WAVE_FORMAT_IEEE_FLOAT = 0x0003, /* IEEE Float */ | ||
50 | WAVE_FORMAT_VSELP = 0x0004, /* Compaq Computer's VSELP */ | ||
51 | WAVE_FORMAT_IBM_CVSD = 0x0005, /* IBM CVSD */ | ||
52 | WAVE_FORMAT_ALAW = 0x0006, /* Microsoft ALAW */ | ||
53 | WAVE_FORMAT_MULAW = 0x0007, /* Microsoft MULAW */ | ||
54 | WAVE_FORMAT_OKI_ADPCM = 0x0010, /* OKI ADPCM */ | ||
55 | WAVE_FORMAT_DVI_ADPCM = 0x0011, /* Intel's DVI ADPCM */ | ||
56 | WAVE_FORMAT_MEDIASPACE_ADPCM = 0x0012, /* Videologic's MediaSpace ADPCM */ | ||
57 | WAVE_FORMAT_SIERRA_ADPCM = 0x0013, /* Sierra ADPCM */ | ||
58 | WAVE_FORMAT_G723_ADPCM = 0x0014, /* G.723 ADPCM */ | ||
59 | WAVE_FORMAT_DIGISTD = 0x0015, /* DSP Solutions' DIGISTD */ | ||
60 | WAVE_FORMAT_DIGIFIX = 0x0016, /* DSP Solutions' DIGIFIX */ | ||
61 | WAVE_FORMAT_DIALOGIC_OKI_ADPCM = 0x0017, /* Dialogic OKI ADPCM */ | ||
62 | WAVE_FORMAT_MEDIAVISION_ADPCM = 0x0018, /* MediaVision ADPCM */ | ||
63 | WAVE_FORMAT_CU_CODEC = 0x0019, /* HP CU */ | ||
64 | WAVE_FORMAT_YAMAHA_ADPCM = 0x0020, /* Yamaha ADPCM */ | ||
65 | WAVE_FORMAT_SONARC = 0x0021, /* Speech Compression's Sonarc */ | ||
66 | WAVE_FORMAT_DSP_TRUESPEECH = 0x0022, /* DSP Group's True Speech */ | ||
67 | WAVE_FORMAT_ECHOSC1 = 0x0023, /* Echo Speech's EchoSC1 */ | ||
68 | WAVE_FORMAT_AUDIOFILE_AF36 = 0x0024, /* Audiofile AF36 */ | ||
69 | WAVE_FORMAT_APTX = 0x0025, /* APTX */ | ||
70 | WAVE_FORMAT_DOLBY_AC2 = 0x0030, /* Dolby AC2 */ | ||
71 | WAVE_FORMAT_GSM610 = 0x0031, /* GSM610 */ | ||
72 | WAVE_FORMAT_MSNAUDIO = 0x0032, /* MSNAudio */ | ||
73 | WAVE_FORMAT_ANTEX_ADPCME = 0x0033, /* Antex ADPCME */ | ||
74 | |||
75 | WAVE_FORMAT_MPEG = 0x0050, /* MPEG */ | ||
76 | WAVE_FORMAT_MPEGLAYER3 = 0x0055, /* MPEG layer 3 */ | ||
77 | WAVE_FORMAT_LUCENT_G723 = 0x0059, /* Lucent G.723 */ | ||
78 | WAVE_FORMAT_G726_ADPCM = 0x0064, /* G.726 ADPCM */ | ||
79 | WAVE_FORMAT_G722_ADPCM = 0x0065, /* G.722 ADPCM */ | ||
26 | 80 | ||
27 | /* Number of bytes to process in one iteration */ | 81 | IBM_FORMAT_MULAW = 0x0101, /* same as WAVE_FORMAT_MULAW */ |
28 | #define WAV_CHUNK_SIZE (1024*4) | 82 | IBM_FORMAT_ALAW = 0x0102, /* same as WAVE_FORMAT_ALAW */ |
83 | IBM_FORMAT_ADPCM = 0x0103, | ||
84 | |||
85 | WAVE_FORMAT_CREATIVE_ADPCM = 0x0200, | ||
86 | |||
87 | WAVE_FORMAT_EXTENSIBLE = 0xFFFE | ||
88 | }; | ||
89 | |||
90 | /* Maximum number of bytes to process in one iteration */ | ||
91 | /* for 44.1kHz stereo 16bits, this represents 0.023s ~= 1/50s */ | ||
92 | #define WAV_CHUNK_SIZE (1024*2) | ||
29 | 93 | ||
30 | #ifndef SIMULATOR | 94 | #ifndef SIMULATOR |
31 | extern char iramcopy[]; | 95 | extern char iramcopy[]; |
@@ -33,37 +97,149 @@ extern char iramstart[]; | |||
33 | extern char iramend[]; | 97 | extern char iramend[]; |
34 | #endif | 98 | #endif |
35 | 99 | ||
100 | /* Those are lookup tables, so they should be in the idata section | ||
101 | * (fast but small RAM on the coldfire processor) */ | ||
102 | static int16_t alaw2linear16[256] IDATA_ATTR = { | ||
103 | -5504, -5248, -6016, -5760, -4480, -4224, -4992, | ||
104 | -4736, -7552, -7296, -8064, -7808, -6528, -6272, | ||
105 | -7040, -6784, -2752, -2624, -3008, -2880, -2240, | ||
106 | -2112, -2496, -2368, -3776, -3648, -4032, -3904, | ||
107 | -3264, -3136, -3520, -3392, -22016, -20992, -24064, | ||
108 | -23040, -17920, -16896, -19968, -18944, -30208, -29184, | ||
109 | -32256, -31232, -26112, -25088, -28160, -27136, -11008, | ||
110 | -10496, -12032, -11520, -8960, -8448, -9984, -9472, | ||
111 | -15104, -14592, -16128, -15616, -13056, -12544, -14080, | ||
112 | -13568, -344, -328, -376, -360, -280, -264, | ||
113 | -312, -296, -472, -456, -504, -488, -408, | ||
114 | -392, -440, -424, -88, -72, -120, -104, | ||
115 | -24, -8, -56, -40, -216, -200, -248, | ||
116 | -232, -152, -136, -184, -168, -1376, -1312, | ||
117 | -1504, -1440, -1120, -1056, -1248, -1184, -1888, | ||
118 | -1824, -2016, -1952, -1632, -1568, -1760, -1696, | ||
119 | -688, -656, -752, -720, -560, -528, -624, | ||
120 | -592, -944, -912, -1008, -976, -816, -784, | ||
121 | -880, -848, 5504, 5248, 6016, 5760, 4480, | ||
122 | 4224, 4992, 4736, 7552, 7296, 8064, 7808, | ||
123 | 6528, 6272, 7040, 6784, 2752, 2624, 3008, | ||
124 | 2880, 2240, 2112, 2496, 2368, 3776, 3648, | ||
125 | 4032, 3904, 3264, 3136, 3520, 3392, 22016, | ||
126 | 20992, 24064, 23040, 17920, 16896, 19968, 18944, | ||
127 | 30208, 29184, 32256, 31232, 26112, 25088, 28160, | ||
128 | 27136, 11008, 10496, 12032, 11520, 8960, 8448, | ||
129 | 9984, 9472, 15104, 14592, 16128, 15616, 13056, | ||
130 | 12544, 14080, 13568, 344, 328, 376, 360, | ||
131 | 280, 264, 312, 296, 472, 456, 504, | ||
132 | 488, 408, 392, 440, 424, 88, 72, | ||
133 | 120, 104, 24, 8, 56, 40, 216, | ||
134 | 200, 248, 232, 152, 136, 184, 168, | ||
135 | 1376, 1312, 1504, 1440, 1120, 1056, 1248, | ||
136 | 1184, 1888, 1824, 2016, 1952, 1632, 1568, | ||
137 | 1760, 1696, 688, 656, 752, 720, 560, | ||
138 | 528, 624, 592, 944, 912, 1008, 976, | ||
139 | 816, 784, 880, 848 | ||
140 | }; | ||
141 | |||
142 | static int16_t ulaw2linear16[256] IDATA_ATTR = { | ||
143 | -32124, -31100, -30076, -29052, -28028, -27004, -25980, | ||
144 | -24956, -23932, -22908, -21884, -20860, -19836, -18812, | ||
145 | -17788, -16764, -15996, -15484, -14972, -14460, -13948, | ||
146 | -13436, -12924, -12412, -11900, -11388, -10876, -10364, | ||
147 | -9852, -9340, -8828, -8316, -7932, -7676, -7420, | ||
148 | -7164, -6908, -6652, -6396, -6140, -5884, -5628, | ||
149 | -5372, -5116, -4860, -4604, -4348, -4092, -3900, | ||
150 | -3772, -3644, -3516, -3388, -3260, -3132, -3004, | ||
151 | -2876, -2748, -2620, -2492, -2364, -2236, -2108, | ||
152 | -1980, -1884, -1820, -1756, -1692, -1628, -1564, | ||
153 | -1500, -1436, -1372, -1308, -1244, -1180, -1116, | ||
154 | -1052, -988, -924, -876, -844, -812, -780, | ||
155 | -748, -716, -684, -652, -620, -588, -556, | ||
156 | -524, -492, -460, -428, -396, -372, -356, | ||
157 | -340, -324, -308, -292, -276, -260, -244, | ||
158 | -228, -212, -196, -180, -164, -148, -132, | ||
159 | -120, -112, -104, -96, -88, -80, -72, | ||
160 | -64, -56, -48, -40, -32, -24, -16, | ||
161 | -8, 0, 32124, 31100, 30076, 29052, 28028, | ||
162 | 27004, 25980, 24956, 23932, 22908, 21884, 20860, | ||
163 | 19836, 18812, 17788, 16764, 15996, 15484, 14972, | ||
164 | 14460, 13948, 13436, 12924, 12412, 11900, 11388, | ||
165 | 10876, 10364, 9852, 9340, 8828, 8316, 7932, | ||
166 | 7676, 7420, 7164, 6908, 6652, 6396, 6140, | ||
167 | 5884, 5628, 5372, 5116, 4860, 4604, 4348, | ||
168 | 4092, 3900, 3772, 3644, 3516, 3388, 3260, | ||
169 | 3132, 3004, 2876, 2748, 2620, 2492, 2364, | ||
170 | 2236, 2108, 1980, 1884, 1820, 1756, 1692, | ||
171 | 1628, 1564, 1500, 1436, 1372, 1308, 1244, | ||
172 | 1180, 1116, 1052, 988, 924, 876, 844, | ||
173 | 812, 780, 748, 716, 684, 652, 620, | ||
174 | 588, 556, 524, 492, 460, 428, 396, | ||
175 | 372, 356, 340, 324, 308, 292, 276, | ||
176 | 260, 244, 228, 212, 196, 180, 164, | ||
177 | 148, 132, 120, 112, 104, 96, 88, | ||
178 | 80, 72, 64, 56, 48, 40, 32, | ||
179 | 24, 16, 8, 0 | ||
180 | }; | ||
181 | |||
182 | static uint16_t dvi_adpcm_steptab[ 89 ] IDATA_ATTR = { | ||
183 | 7, 8, 9, 10, 11, 12, 13, 14, | ||
184 | 16, 17, 19, 21, 23, 25, 28, 31, | ||
185 | 34, 37, 41, 45, 50, 55, 60, 66, | ||
186 | 73, 80, 88, 97, 107, 118, 130, 143, | ||
187 | 157, 173, 190, 209, 230, 253, 279, 307, | ||
188 | 337, 371, 408, 449, 494, 544, 598, 658, | ||
189 | 724, 796, 876, 963, 1060, 1166, 1282, 1411, | ||
190 | 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, | ||
191 | 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, | ||
192 | 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, | ||
193 | 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, | ||
194 | 32767 }; | ||
195 | static int dvi_adpcm_indextab4[ 8 ] IDATA_ATTR = { -1, -1, -1, -1, 2, 4, 6, 8 }; | ||
196 | static int dvi_adpcm_indextab3[ 4 ] IDATA_ATTR = { -1, -1, 1, 2 }; | ||
197 | |||
198 | static int16_t int16_samples[WAV_CHUNK_SIZE] IDATA_ATTR; | ||
199 | |||
200 | static enum codec_status | ||
201 | decode_dvi_adpcm(struct codec_api* rb, | ||
202 | const uint8_t *buf, | ||
203 | int n, | ||
204 | uint16_t channels, uint16_t bitspersample, | ||
205 | int16_t *pcmout, | ||
206 | size_t *pcmoutsize); | ||
207 | |||
36 | /* this is the codec entry point */ | 208 | /* this is the codec entry point */ |
37 | enum codec_status codec_start(struct codec_api* api) | 209 | enum codec_status codec_start(struct codec_api* api) |
38 | { | 210 | { |
39 | struct codec_api* rb = api; | 211 | struct codec_api* rb = api; |
40 | struct codec_api* ci = api; | 212 | uint32_t numbytes, bytesdone; |
41 | unsigned long samplerate,numbytes,totalsamples,samplesdone,nsamples; | 213 | uint32_t totalsamples = 0; |
42 | int channels,bytespersample,bitspersample; | 214 | uint16_t channels=0; |
43 | unsigned int i; | 215 | uint16_t samplesperblock = 0; |
44 | size_t n; | 216 | int bytespersample=0; |
217 | uint16_t bitspersample; | ||
218 | uint32_t i; | ||
219 | size_t n, wavbufsize; | ||
45 | int endofstream; | 220 | int endofstream; |
46 | unsigned char* header; | 221 | unsigned char* buf; |
47 | unsigned short* wavbuf; | 222 | uint16_t* wavbuf; |
223 | long chunksize; | ||
224 | uint16_t formattag = 0; | ||
225 | uint16_t blockalign = 0; | ||
226 | uint32_t avgbytespersec = 0; | ||
227 | off_t firstblockposn; /* position of the first block in file */ | ||
228 | int shortorlong = 1; /* do we output shorts (1) or longs (2)? */ | ||
229 | int32_t * const int32_samples = (int32_t*)int16_samples; | ||
48 | 230 | ||
49 | /* Generic codec initialisation */ | 231 | /* Generic codec initialisation */ |
50 | TEST_CODEC_API(api); | 232 | TEST_CODEC_API(api); |
51 | 233 | ||
52 | /* if you are using a global api pointer, don't forget to copy it! | ||
53 | otherwise you will get lovely "I04: IllInstr" errors... :-) */ | ||
54 | rb = api; | ||
55 | |||
56 | #ifndef SIMULATOR | 234 | #ifndef SIMULATOR |
57 | rb->memcpy(iramstart, iramcopy, iramend-iramstart); | 235 | rb->memcpy(iramstart, iramcopy, iramend-iramstart); |
58 | #endif | 236 | #endif |
59 | 237 | ||
60 | ci->configure(CODEC_SET_FILEBUF_LIMIT, (int *)(1024*1024*10)); | 238 | rb->configure(CODEC_SET_FILEBUF_LIMIT, (int *)(1024*1024*10)); |
61 | ci->configure(CODEC_SET_FILEBUF_WATERMARK, (int *)(1024*512)); | 239 | rb->configure(CODEC_SET_FILEBUF_WATERMARK, (int *)(1024*512)); |
62 | ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*256)); | 240 | rb->configure(CODEC_SET_FILEBUF_CHUNKSIZE, (int *)(1024*256)); |
63 | 241 | ||
64 | ci->configure(DSP_DITHER, (bool *)false); | 242 | rb->configure(DSP_DITHER, (bool *)false); |
65 | ci->configure(DSP_SET_STEREO_MODE, (int *)STEREO_INTERLEAVED); | ||
66 | ci->configure(DSP_SET_SAMPLE_DEPTH, (int *)(16)); | ||
67 | 243 | ||
68 | next_track: | 244 | next_track: |
69 | 245 | ||
@@ -73,76 +249,484 @@ enum codec_status codec_start(struct codec_api* api) | |||
73 | 249 | ||
74 | while (!*rb->taginfo_ready) | 250 | while (!*rb->taginfo_ready) |
75 | rb->yield(); | 251 | rb->yield(); |
76 | |||
77 | /* Always enable DSP to support voice ui. */ | ||
78 | rb->configure(CODEC_DSP_ENABLE, (bool *)true); | ||
79 | rb->configure(DSP_SET_FREQUENCY, (long *)(rb->id3->frequency)); | ||
80 | 252 | ||
81 | /* FIX: Correctly parse WAV header - we assume canonical 44-byte header */ | 253 | /* assume the WAV header is less than 1024 bytes */ |
82 | 254 | buf=rb->request_buffer(&n,1024); | |
83 | header=ci->request_buffer(&n,44); | 255 | if (n<44) { |
84 | if (n!=44) { | ||
85 | return CODEC_ERROR; | 256 | return CODEC_ERROR; |
86 | } | 257 | } |
87 | if ((memcmp(header,"RIFF",4)!=0) || (memcmp(&header[8],"WAVEfmt",7)!=0)) { | 258 | if ((memcmp(buf,"RIFF",4)!=0) || (memcmp(&buf[8],"WAVE",4)!=0)) { |
88 | return CODEC_ERROR; | 259 | return CODEC_ERROR; |
89 | } | 260 | } |
90 | 261 | ||
91 | samplerate=header[24]|(header[25]<<8)|(header[26]<<16)|(header[27]<<24); | 262 | buf += 12; |
92 | bitspersample=header[34]; | 263 | n -= 12; |
93 | channels=header[22]; | 264 | bitspersample = 0; |
94 | bytespersample=((bitspersample/8)*channels); | 265 | numbytes = 0; |
95 | numbytes=(header[40]|(header[41]<<8)|(header[42]<<16)|(header[43]<<24)); | 266 | totalsamples = 0; |
96 | totalsamples=numbytes/bytespersample; | 267 | /* read until the data chunk, which should be last */ |
268 | while(numbytes == 0 && n >= 8) { | ||
269 | /* chunkSize */ | ||
270 | i = (buf[4]|(buf[5]<<8)|(buf[6]<<16)|(buf[7]<<24)); | ||
271 | if (memcmp(buf,"fmt ",4)==0) { | ||
272 | if (i<16) { | ||
273 | DEBUGF("CODEC_ERROR: 'fmt ' chunk size=%lu < 16\n",i); | ||
274 | return CODEC_ERROR; | ||
275 | } | ||
276 | /* wFormatTag */ | ||
277 | formattag=buf[8]|(buf[9]<<8); | ||
278 | /* wChannels */ | ||
279 | channels=buf[10]|(buf[11]<<8); | ||
280 | /* skipping dwSamplesPerSec */ | ||
281 | /* dwAvgBytesPerSec */ | ||
282 | avgbytespersec = buf[16]|(buf[17]<<8)|(buf[18]<<16)|(buf[19]<<24); | ||
283 | /* wBlockAlign */ | ||
284 | blockalign=buf[20]|(buf[21]<<8); | ||
285 | /* wBitsPerSample */ | ||
286 | bitspersample=buf[22]|(buf[23]<<8); | ||
287 | if (formattag != WAVE_FORMAT_PCM) { | ||
288 | uint16_t size; | ||
289 | if (i<18) { | ||
290 | /* this is not a fatal error with some formats, | ||
291 | * we'll see later if we can't decode it */ | ||
292 | DEBUGF("CODEC_WARNING: non-PCM WAVE (formattag=0x%x) " | ||
293 | "doesn't have ext. fmt descr (chunksize=%d<18).\n", | ||
294 | formattag, i); | ||
295 | } | ||
296 | size = buf[24]|(buf[25]<<8); | ||
297 | if (formattag == WAVE_FORMAT_DVI_ADPCM) { | ||
298 | if (size < 2) { | ||
299 | DEBUGF("CODEC_ERROR: dvi_adpcm is missing " | ||
300 | "SamplesPerBlock value\n"); | ||
301 | return CODEC_ERROR; | ||
302 | } | ||
303 | samplesperblock = buf[26]|(buf[27]<<8); | ||
304 | } | ||
305 | else if (formattag == WAVE_FORMAT_EXTENSIBLE) { | ||
306 | if (size < 22) { | ||
307 | DEBUGF("CODEC_ERROR: WAVE_FORMAT_EXTENSIBLE is " | ||
308 | "missing extension\n"); | ||
309 | return CODEC_ERROR; | ||
310 | } | ||
311 | /* wValidBitsPerSample */ | ||
312 | bitspersample = buf[26]|(buf[27]<<8); | ||
313 | /* skipping dwChannelMask (4bytes) */ | ||
314 | /* SubFormat (only get the first two bytes) */ | ||
315 | formattag = buf[32]|(buf[33]<<8); | ||
316 | } | ||
317 | } | ||
318 | } | ||
319 | else if (memcmp(buf,"data",4)==0) { | ||
320 | numbytes=i; | ||
321 | i=0; /* advance to the beginning of data */ | ||
322 | } | ||
323 | else if (memcmp(buf,"fact",4)==0) { | ||
324 | /* dwSampleLength */ | ||
325 | if (i>=4) { | ||
326 | totalsamples = (buf[8]|(buf[9]<<8)|(buf[10]<<16)|(buf[11]<<24)); | ||
327 | } | ||
328 | } | ||
329 | else { | ||
330 | DEBUGF("unknown WAVE chunk: '%c%c%c%c', size=%lu\n", | ||
331 | buf[0], buf[1], buf[2], buf[3],i); | ||
332 | } | ||
333 | /* go to next chunk (even chunk sizes must be padded) */ | ||
334 | if (i & 0x01) | ||
335 | i++; | ||
336 | buf += i+8; | ||
337 | if (n < (i+8)) { | ||
338 | DEBUGF("CODEC_ERROR: WAVE header size > 1024\n"); | ||
339 | return CODEC_ERROR; | ||
340 | } | ||
341 | n -= i+8; | ||
342 | } | ||
97 | 343 | ||
98 | if ((bitspersample!=16) || (channels != 2)) { | 344 | if (channels == 0) { |
99 | return CODEC_ERROR; | 345 | DEBUGF("CODEC_ERROR: 'fmt ' chunk not found or 0-channels file\n"); |
346 | return CODEC_ERROR; | ||
347 | } | ||
348 | if (numbytes == 0) { | ||
349 | DEBUGF("CODEC_ERROR: 'data' chunk not found or has zero-length\n"); | ||
350 | return CODEC_ERROR; | ||
351 | } | ||
352 | if (formattag != WAVE_FORMAT_PCM && totalsamples == 0) { | ||
353 | /* This is non-fatal for some formats */ | ||
354 | DEBUGF("CODEC_WARNING: non-PCM WAVE doesn't have a 'fact' chunk\n"); | ||
355 | } | ||
356 | if (formattag == WAVE_FORMAT_ALAW || formattag == WAVE_FORMAT_MULAW || | ||
357 | formattag == IBM_FORMAT_ALAW || formattag == IBM_FORMAT_MULAW) { | ||
358 | if (bitspersample != 8) { | ||
359 | DEBUGF("CODEC_ERROR: alaw and mulaw must have 8 bitspersample\n"); | ||
360 | return CODEC_ERROR; | ||
361 | } | ||
362 | bytespersample = channels; | ||
363 | } | ||
364 | if ( formattag == WAVE_FORMAT_DVI_ADPCM | ||
365 | && bitspersample != 4 && bitspersample != 3) { | ||
366 | DEBUGF("CODEC_ERROR: dvi_adpcm must have 3 or 4 bitspersample\n"); | ||
367 | return CODEC_ERROR; | ||
368 | } | ||
369 | if (formattag == WAVE_FORMAT_PCM && bitspersample > 32) { | ||
370 | DEBUGF("CODEC_ERROR: pcm with more than 32 bitspersample " | ||
371 | "is unsupported\n"); | ||
372 | return CODEC_ERROR; | ||
373 | } | ||
374 | |||
375 | rb->configure(CODEC_DSP_ENABLE, (bool *)true); | ||
376 | rb->configure(DSP_SET_FREQUENCY, (long *)(rb->id3->frequency)); | ||
377 | |||
378 | if (bitspersample <= 16) { | ||
379 | rb->configure(DSP_SET_SAMPLE_DEPTH, (int *)(16)); | ||
380 | } else { | ||
381 | shortorlong = 2; | ||
382 | rb->configure(DSP_DITHER, (bool *)false); | ||
383 | rb->configure(DSP_SET_SAMPLE_DEPTH, (long *) (32)); | ||
384 | rb->configure(DSP_SET_CLIP_MAX, (long *) (2147483647)); | ||
385 | rb->configure(DSP_SET_CLIP_MIN, (long *) (-2147483647-1)); | ||
386 | } | ||
387 | |||
388 | if (channels == 2) { | ||
389 | rb->configure(DSP_SET_STEREO_MODE, (int *)STEREO_INTERLEAVED); | ||
390 | } else if (channels == 1) { | ||
391 | rb->configure(DSP_SET_STEREO_MODE, (int *)STEREO_MONO); | ||
392 | } else { | ||
393 | DEBUGF("CODEC_ERROR: more than 2 channels\n"); | ||
394 | return CODEC_ERROR; | ||
395 | } | ||
396 | |||
397 | if (totalsamples == 0) { | ||
398 | if (formattag == WAVE_FORMAT_PCM || | ||
399 | formattag == WAVE_FORMAT_ALAW || formattag == WAVE_FORMAT_MULAW || | ||
400 | formattag == IBM_FORMAT_ALAW || formattag == IBM_FORMAT_MULAW) { | ||
401 | /* for PCM and derived formats only */ | ||
402 | bytespersample=(((bitspersample-1)/8+1)*channels); | ||
403 | totalsamples=numbytes/bytespersample; | ||
404 | } | ||
405 | else { | ||
406 | DEBUGF("CODEC_ERROR: cannot compute totalsamples\n"); | ||
407 | return CODEC_ERROR; | ||
408 | } | ||
100 | } | 409 | } |
101 | 410 | ||
102 | ci->advance_buffer(44); | 411 | firstblockposn = (1024-n); |
412 | rb->advance_buffer(firstblockposn); | ||
103 | 413 | ||
104 | /* The main decoder loop */ | 414 | /* The main decoder loop */ |
105 | 415 | ||
106 | samplesdone=0; | 416 | bytesdone=0; |
107 | ci->set_elapsed(0); | 417 | rb->set_elapsed(0); |
108 | endofstream=0; | 418 | endofstream=0; |
419 | /* chunksize is computed so that one chunk is about 1/50s. | ||
420 | * this make 4096 for 44.1kHz 16bits stereo. | ||
421 | * It also has to be a multiple of blockalign */ | ||
422 | chunksize = (1 + avgbytespersec / (50*blockalign)) * blockalign; | ||
423 | /* check that the output buffer is big enough (convert to samplespersec, | ||
424 | then round to the blockalign multiple below) */ | ||
425 | if (((uint64_t)chunksize*rb->id3->frequency*channels*shortorlong) | ||
426 | / (uint64_t)avgbytespersec >= WAV_CHUNK_SIZE) { | ||
427 | chunksize = ((uint64_t)WAV_CHUNK_SIZE * avgbytespersec | ||
428 | / ((uint64_t)rb->id3->frequency * channels * shortorlong | ||
429 | * blockalign)) * blockalign; | ||
430 | } | ||
431 | |||
109 | while (!endofstream) { | 432 | while (!endofstream) { |
110 | if (ci->stop_codec || ci->reload_codec) { | 433 | uint8_t *wavbuf8; |
434 | |||
435 | rb->yield(); | ||
436 | if (rb->stop_codec || rb->reload_codec) { | ||
111 | break; | 437 | break; |
112 | } | 438 | } |
113 | 439 | ||
114 | wavbuf=ci->request_buffer(&n,WAV_CHUNK_SIZE); | 440 | if (rb->seek_time) { |
441 | uint32_t newpos; | ||
115 | 442 | ||
116 | if (n==0) break; /* End of stream */ | 443 | /* use avgbytespersec to round to the closest blockalign multiple, |
444 | add firstblockposn. 64-bit casts to avoid overflows. */ | ||
445 | newpos = (((uint64_t)avgbytespersec * rb->seek_time) | ||
446 | / (1000LL*blockalign)) * blockalign; | ||
447 | if (newpos > numbytes) | ||
448 | break; | ||
449 | if (rb->seek_buffer(firstblockposn + newpos)) { | ||
450 | bytesdone = newpos; | ||
451 | } | ||
452 | rb->seek_time = 0; | ||
453 | } | ||
454 | wavbuf=rb->request_buffer(&n,chunksize); | ||
455 | wavbuf8 = (uint8_t*)wavbuf; | ||
456 | |||
457 | if (n==0) | ||
458 | break; /* End of stream */ | ||
117 | 459 | ||
118 | nsamples=(n/bytespersample); | 460 | if (bytesdone + n > numbytes) { |
461 | n = numbytes - bytesdone; | ||
462 | endofstream = 1; | ||
463 | } | ||
119 | 464 | ||
120 | /* WAV files can contain extra data at the end - so we can't just | 465 | wavbufsize = sizeof(int16_samples); |
121 | process until the end of the file */ | ||
122 | 466 | ||
123 | if (samplesdone+nsamples > totalsamples) { | 467 | if (formattag == WAVE_FORMAT_PCM) { |
124 | nsamples=(totalsamples-samplesdone); | 468 | if (bitspersample > 24) { |
125 | n=nsamples*bytespersample; | 469 | for (i=0;i<n;i+=4) { |
126 | endofstream=1; | 470 | int32_samples[i/4]=(int32_t)(wavbuf8[i]|(wavbuf8[i+1]<<8)| |
471 | (wavbuf8[i+2]<<16)|(wavbuf8[i+3]<<24)); | ||
472 | } | ||
473 | wavbufsize = n; | ||
474 | } | ||
475 | else if (bitspersample > 16) { | ||
476 | for (i=0;i<n;i+=3) { | ||
477 | int32_samples[i/3]=(int32_t)((wavbuf8[i]<<8)| | ||
478 | (wavbuf8[i+1]<<16)|(wavbuf8[i+2]<<24)); | ||
479 | } | ||
480 | wavbufsize = n*4/3; | ||
481 | } | ||
482 | else if (bitspersample > 8) { | ||
483 | /* Byte-swap data. */ | ||
484 | for (i=0;i<n/2;i++) { | ||
485 | int16_samples[i]=(int16_t)SWAB16(wavbuf[i]); | ||
486 | } | ||
487 | wavbufsize = n; | ||
488 | } | ||
489 | else { | ||
490 | for (i=0;i<n;i++) { | ||
491 | int16_samples[i] = (wavbuf8[i]<<8) - 0x8000; | ||
492 | } | ||
493 | wavbufsize = n*2; | ||
494 | } | ||
495 | } | ||
496 | else if (formattag == WAVE_FORMAT_ALAW || formattag == IBM_FORMAT_ALAW) { | ||
497 | for (i=0;i<n;i++) { | ||
498 | int16_samples[i] = alaw2linear16[wavbuf8[i]]; | ||
499 | } | ||
500 | wavbufsize = n*2; | ||
127 | } | 501 | } |
502 | else if (formattag == WAVE_FORMAT_MULAW || formattag == IBM_FORMAT_MULAW) { | ||
503 | for (i=0;i<n;i++) { | ||
504 | int16_samples[i] = ulaw2linear16[wavbuf8[i]]; | ||
505 | } | ||
506 | wavbufsize = n*2; | ||
507 | } | ||
508 | else if (formattag == WAVE_FORMAT_DVI_ADPCM) { | ||
509 | unsigned int nblocks = chunksize/blockalign; | ||
128 | 510 | ||
129 | /* Byte-swap data */ | 511 | for (i=0; i<nblocks; i++) { |
130 | for (i=0;i<n/2;i++) { | 512 | size_t decodedsize = samplesperblock*channels; |
131 | wavbuf[i]=SWAB16(wavbuf[i]); | 513 | if (decode_dvi_adpcm(rb, ((uint8_t*)wavbuf)+i*blockalign, |
514 | blockalign, channels, bitspersample, | ||
515 | int16_samples+i*samplesperblock*channels, | ||
516 | &decodedsize) | ||
517 | != CODEC_OK) | ||
518 | return CODEC_ERROR; | ||
519 | if (decodedsize != samplesperblock) | ||
520 | return CODEC_ERROR; | ||
521 | } | ||
522 | wavbufsize = nblocks*samplesperblock*channels*2; | ||
523 | } | ||
524 | else { | ||
525 | DEBUGF("CODEC_ERROR: unsupported format %x\n", formattag); | ||
526 | return CODEC_ERROR; | ||
132 | } | 527 | } |
133 | 528 | ||
134 | samplesdone+=nsamples; | 529 | while (!rb->pcmbuf_insert((char*)int16_samples, wavbufsize)) { |
135 | ci->set_elapsed(samplesdone/(ci->id3->frequency/1000)); | 530 | rb->yield(); |
136 | 531 | } | |
137 | rb->yield(); | ||
138 | while (!ci->pcmbuf_insert((unsigned char*)wavbuf, n)) | ||
139 | rb->yield(); | ||
140 | 532 | ||
141 | ci->advance_buffer(n); | 533 | rb->advance_buffer(n); |
534 | bytesdone += n; | ||
535 | if (bytesdone >= numbytes) { | ||
536 | endofstream=1; | ||
537 | } | ||
538 | |||
539 | rb->set_elapsed(bytesdone*1000LL/avgbytespersec); | ||
142 | } | 540 | } |
143 | 541 | ||
144 | if (ci->request_next_track()) | 542 | if (rb->request_next_track()) |
145 | goto next_track; | 543 | goto next_track; |
146 | 544 | ||
147 | return CODEC_OK; | 545 | return CODEC_OK; |
148 | } | 546 | } |
547 | |||
548 | static enum codec_status | ||
549 | decode_dvi_adpcm(struct codec_api* rb, | ||
550 | const uint8_t *buf, | ||
551 | int n, | ||
552 | uint16_t channels, uint16_t bitspersample, | ||
553 | int16_t *pcmout, | ||
554 | size_t *pcmoutsize) | ||
555 | { | ||
556 | size_t nsamples = 0; | ||
557 | int sample[2]; | ||
558 | int samplecode[32][2]; | ||
559 | int i; | ||
560 | int stepindex[2]; | ||
561 | int c; | ||
562 | int diff; | ||
563 | int step; | ||
564 | int codem; | ||
565 | int code; | ||
566 | |||
567 | (void)rb; | ||
568 | if (bitspersample != 4 && bitspersample != 3) { | ||
569 | DEBUGF("decode_dvi_adpcm: wrong bitspersample\n"); | ||
570 | return CODEC_ERROR; | ||
571 | } | ||
572 | |||
573 | /* decode block header */ | ||
574 | for(c=0; c<channels && n>=4; c++) { | ||
575 | /* decode + push first sample */ | ||
576 | sample[c] = (short)(buf[0]|(buf[1]<<8));/* need cast for sign-extend */ | ||
577 | pcmout[c] = sample[c]; | ||
578 | nsamples++; | ||
579 | stepindex[c] = buf[2]; | ||
580 | /* check for step table index overflow */ | ||
581 | if (stepindex[c] > 88) { | ||
582 | DEBUGF("decode_dvi_adpcm: stepindex[%d]=%d>88\n",c,stepindex[c]); | ||
583 | return CODEC_ERROR; | ||
584 | } | ||
585 | |||
586 | buf += 4; | ||
587 | n -= 4; | ||
588 | } | ||
589 | if (bitspersample == 4) { | ||
590 | while( n>= channels*4 && (nsamples+8*channels) <= *pcmoutsize ) { | ||
591 | for (c=0; c<channels; c++) { | ||
592 | samplecode[0][c] = buf[0]&0xf; | ||
593 | samplecode[1][c] = buf[0]>>4; | ||
594 | samplecode[2][c] = buf[1]&0xf; | ||
595 | samplecode[3][c] = buf[1]>>4; | ||
596 | samplecode[4][c] = buf[2]&0xf; | ||
597 | samplecode[5][c] = buf[2]>>4; | ||
598 | samplecode[6][c] = buf[3]&0xf; | ||
599 | samplecode[7][c] = buf[3]>>4; | ||
600 | buf += 4; | ||
601 | n -= 4; | ||
602 | } | ||
603 | |||
604 | for (i=0; i<8; i++) { | ||
605 | for (c=0; c<channels; c++) { | ||
606 | step = dvi_adpcm_steptab[stepindex[c]]; | ||
607 | codem = samplecode[i][c]; | ||
608 | code = codem & 0x07; | ||
609 | |||
610 | /* adjust the step table index */ | ||
611 | stepindex[c] += dvi_adpcm_indextab4[code]; | ||
612 | /* check for step table index overflow and underflow */ | ||
613 | if (stepindex[c] > 88) | ||
614 | stepindex[c] = 88; | ||
615 | else if (stepindex[c] < 0) | ||
616 | stepindex[c] = 0; | ||
617 | /* calculate the difference */ | ||
618 | #ifdef STRICT_IMA | ||
619 | diff = 0; | ||
620 | if (code & 4) | ||
621 | diff += step; | ||
622 | step = step >> 1; | ||
623 | if (code & 2) | ||
624 | diff += step; | ||
625 | step = step >> 1; | ||
626 | if (code & 1) | ||
627 | diff += step; | ||
628 | step = step >> 1; | ||
629 | diff += step; | ||
630 | #else | ||
631 | diff = ((code+code+1) * step) >> 3; /* faster */ | ||
632 | #endif | ||
633 | /* check the sign bit */ | ||
634 | /* check for overflow and underflow errors */ | ||
635 | if (code != codem) { | ||
636 | sample[c] -= diff; | ||
637 | if (sample[c] < -32768) | ||
638 | sample[c] = -32768; | ||
639 | } | ||
640 | else { | ||
641 | sample[c] += diff; | ||
642 | if (sample[c] > 32767) | ||
643 | sample[c] = 32767; | ||
644 | } | ||
645 | /* output the new sample */ | ||
646 | pcmout[nsamples] = sample[c]; | ||
647 | nsamples++; | ||
648 | } | ||
649 | } | ||
650 | } | ||
651 | } | ||
652 | else { /* bitspersample == 3 */ | ||
653 | while( n>= channels*12 && (nsamples+32*channels) <= *pcmoutsize) { | ||
654 | for (c=0; c<channels; c++) { | ||
655 | uint16_t bitstream = 0; | ||
656 | int bitsread = 0; | ||
657 | for (i=0; i<32 && n>0; i++) { | ||
658 | if (bitsread < 3) { | ||
659 | /* read 8 more bits */ | ||
660 | bitstream |= buf[0]<<bitsread; | ||
661 | bitsread += 8; | ||
662 | n--; | ||
663 | buf++; | ||
664 | } | ||
665 | samplecode[i][c] = bitstream & 7; | ||
666 | bitstream = bitstream>>3; | ||
667 | bitsread -= 3; | ||
668 | } | ||
669 | if (bitsread != 0) { | ||
670 | /* 32*3 = 3 words, so we should end with bitsread==0 */ | ||
671 | DEBUGF("decode_dvi_adpcm: error in implementation\n"); | ||
672 | return CODEC_ERROR; | ||
673 | } | ||
674 | } | ||
675 | |||
676 | for (i=0; i<32; i++) { | ||
677 | for (c=0; c<channels; c++) { | ||
678 | step = dvi_adpcm_steptab[stepindex[c]]; | ||
679 | codem = samplecode[i][c]; | ||
680 | code = codem & 0x03; | ||
681 | |||
682 | /* adjust the step table index */ | ||
683 | stepindex[c] += dvi_adpcm_indextab3[code]; | ||
684 | /* check for step table index overflow and underflow */ | ||
685 | if (stepindex[c] > 88) | ||
686 | stepindex[c] = 88; | ||
687 | else if (stepindex[c] < 0) | ||
688 | stepindex[c] = 0; | ||
689 | /* calculate the difference */ | ||
690 | #ifdef STRICT_IMA | ||
691 | diff = 0; | ||
692 | if (code & 2) | ||
693 | diff += step; | ||
694 | step = step >> 1; | ||
695 | if (code & 1) | ||
696 | diff += step; | ||
697 | step = step >> 1; | ||
698 | diff += step; | ||
699 | #else | ||
700 | diff = ((code+code+1) * step) >> 3; /* faster */ | ||
701 | #endif | ||
702 | /* check the sign bit */ | ||
703 | /* check for overflow and underflow errors */ | ||
704 | if (code != codem) { | ||
705 | sample[c] -= diff; | ||
706 | if (sample[c] < -32768) | ||
707 | sample[c] = -32768; | ||
708 | } | ||
709 | else { | ||
710 | sample[c] += diff; | ||
711 | if (sample[c] > 32767) | ||
712 | sample[c] = 32767; | ||
713 | } | ||
714 | /* output the new sample */ | ||
715 | pcmout[nsamples] = sample[c]; | ||
716 | nsamples++; | ||
717 | } | ||
718 | } | ||
719 | } | ||
720 | } | ||
721 | |||
722 | if (nsamples > *pcmoutsize) { | ||
723 | DEBUGF("decode_dvi_adpcm: output buffer overflow!\n"); | ||
724 | return CODEC_ERROR; | ||
725 | } | ||
726 | *pcmoutsize = nsamples; | ||
727 | if (n!=0) { | ||
728 | DEBUGF("decode_dvi_adpcm: n=%d unprocessed bytes\n",n); | ||
729 | } | ||
730 | return CODEC_OK; | ||
731 | } | ||
732 | |||
diff --git a/apps/metadata.c b/apps/metadata.c index b57bf0fbc7..2883b01407 100644 --- a/apps/metadata.c +++ b/apps/metadata.c | |||
@@ -798,6 +798,95 @@ static bool get_flac_metadata(int fd, struct mp3entry* id3) | |||
798 | return true; | 798 | return true; |
799 | } | 799 | } |
800 | 800 | ||
801 | static bool get_wave_metadata(int fd, struct mp3entry* id3) | ||
802 | { | ||
803 | /* Use the trackname part of the id3 structure as a temporary buffer */ | ||
804 | unsigned char* buf = id3->path; | ||
805 | unsigned long totalsamples = 0; | ||
806 | unsigned long channels = 0; | ||
807 | unsigned long bitspersample = 0; | ||
808 | unsigned long numbytes = 0; | ||
809 | int read_bytes; | ||
810 | int i; | ||
811 | |||
812 | if ((lseek(fd, 0, SEEK_SET) < 0) | ||
813 | || ((read_bytes = read(fd, buf, sizeof(id3->path))) < 44)) | ||
814 | { | ||
815 | return false; | ||
816 | } | ||
817 | |||
818 | if ((memcmp(buf, "RIFF",4) != 0) | ||
819 | || (memcmp(&buf[8], "WAVE", 4) !=0 )) | ||
820 | { | ||
821 | return false; | ||
822 | } | ||
823 | |||
824 | buf += 12; | ||
825 | read_bytes -= 12; | ||
826 | |||
827 | while ((numbytes == 0) && (read_bytes >= 8)) | ||
828 | { | ||
829 | /* chunkSize */ | ||
830 | i = get_long(&buf[4]); | ||
831 | |||
832 | if (memcmp(buf, "fmt ", 4) == 0) | ||
833 | { | ||
834 | /* skipping wFormatTag */ | ||
835 | /* wChannels */ | ||
836 | channels = buf[10] | (buf[11] << 8); | ||
837 | /* dwSamplesPerSec */ | ||
838 | id3->frequency = get_long(&buf[12]); | ||
839 | /* dwAvgBytesPerSec */ | ||
840 | id3->bitrate = (get_long(&buf[16]) * 8) / 1000; | ||
841 | /* skipping wBlockAlign */ | ||
842 | /* wBitsPerSample */ | ||
843 | bitspersample = buf[22] | (buf[23] << 8); | ||
844 | } | ||
845 | else if (memcmp(buf, "data", 4) == 0) | ||
846 | { | ||
847 | numbytes = i; | ||
848 | } | ||
849 | else if (memcmp(buf, "fact", 4) == 0) | ||
850 | { | ||
851 | /* dwSampleLength */ | ||
852 | if (i >= 4) | ||
853 | { | ||
854 | totalsamples = get_long(&buf[8]); | ||
855 | } | ||
856 | } | ||
857 | |||
858 | /* go to next chunk (even chunk sizes must be padded) */ | ||
859 | if (i & 0x01) | ||
860 | { | ||
861 | i++; | ||
862 | } | ||
863 | |||
864 | buf += i + 8; | ||
865 | read_bytes -= i + 8; | ||
866 | } | ||
867 | |||
868 | if ((numbytes == 0) || (channels == 0)) | ||
869 | { | ||
870 | return false; | ||
871 | } | ||
872 | |||
873 | if (totalsamples == 0) | ||
874 | { | ||
875 | /* for PCM only */ | ||
876 | totalsamples = numbytes | ||
877 | / ((((bitspersample - 1) / 8) + 1) * channels); | ||
878 | } | ||
879 | |||
880 | id3->vbr = false; /* All WAV files are CBR */ | ||
881 | id3->filesize = filesize(fd); | ||
882 | |||
883 | /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */ | ||
884 | id3->length = (totalsamples / id3->frequency) * 1000; | ||
885 | |||
886 | return true; | ||
887 | } | ||
888 | |||
889 | |||
801 | /* Simple file type probing by looking at the filename extension. */ | 890 | /* Simple file type probing by looking at the filename extension. */ |
802 | unsigned int probe_file_format(const char *filename) | 891 | unsigned int probe_file_format(const char *filename) |
803 | { | 892 | { |
@@ -832,10 +921,6 @@ bool get_metadata(struct track_info* track, int fd, const char* trackname, | |||
832 | { | 921 | { |
833 | unsigned char* buf; | 922 | unsigned char* buf; |
834 | unsigned long totalsamples; | 923 | unsigned long totalsamples; |
835 | unsigned long bytespersample; | ||
836 | unsigned long channels; | ||
837 | unsigned long bitspersample; | ||
838 | unsigned long numbytes; | ||
839 | int bytesperframe; | 924 | int bytesperframe; |
840 | int i; | 925 | int i; |
841 | 926 | ||
@@ -870,46 +955,11 @@ bool get_metadata(struct track_info* track, int fd, const char* trackname, | |||
870 | break; | 955 | break; |
871 | 956 | ||
872 | case AFMT_PCM_WAV: | 957 | case AFMT_PCM_WAV: |
873 | /* Use the trackname part of the id3 structure as a temporary buffer */ | 958 | if (!get_wave_metadata(fd, &(track->id3))) |
874 | buf = track->id3.path; | ||
875 | |||
876 | if ((lseek(fd, 0, SEEK_SET) < 0) || (read(fd, buf, 44) < 44)) | ||
877 | { | 959 | { |
878 | return false; | 960 | return false; |
879 | } | 961 | } |
880 | 962 | ||
881 | if ((memcmp(buf,"RIFF",4) !=0 ) | ||
882 | || (memcmp(&buf[8], "WAVEfmt", 7) !=0 )) | ||
883 | { | ||
884 | logf("Not a WAV: %s\n", trackname); | ||
885 | return false; | ||
886 | } | ||
887 | |||
888 | /* FIX: Correctly parse WAV header - we assume canonical | ||
889 | * 44-byte header | ||
890 | */ | ||
891 | |||
892 | bitspersample = buf[34]; | ||
893 | channels = buf[22]; | ||
894 | |||
895 | if ((bitspersample != 16) || (channels != 2)) | ||
896 | { | ||
897 | logf("Unsupported WAV - %d bps, %d channels\n", | ||
898 | bitspersample, channels); | ||
899 | return false; | ||
900 | } | ||
901 | |||
902 | bytespersample = ((bitspersample / 8) * channels); | ||
903 | numbytes = get_long(&buf[40]); | ||
904 | totalsamples = numbytes / bytespersample; | ||
905 | |||
906 | track->id3.vbr = false; /* All WAV files are CBR */ | ||
907 | track->id3.filesize = filesize(fd); | ||
908 | track->id3.frequency = get_long(&buf[24]); | ||
909 | |||
910 | /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */ | ||
911 | track->id3.length = (totalsamples / track->id3.frequency) * 1000; | ||
912 | track->id3.bitrate = (track->id3.frequency * bytespersample) / (1000 / 8); | ||
913 | break; | 963 | break; |
914 | 964 | ||
915 | case AFMT_WAVPACK: | 965 | case AFMT_WAVPACK: |