Debian Codegraph @

src/wav.c

raw symbols SCIP
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17 #include "wav.h"
18
19 static int
20 get_wav_metadata(PerlIO *infile, char *file, HV *info, HV *tags)
21 {
22 Buffer buf;
23 off_t file_size;
24 int err = 0;
25 uint32_t chunk_size;
26
27 file_size = _file_size(infile);
28
29 buffer_init(&buf, WAV_BLOCK_SIZE);
30
31 if ( !_check_buf(infile, &buf, 12, WAV_BLOCK_SIZE) ) {
32 err = -1;
33 goto out;
34 }
35
36 if ( !strncmp( (char *)buffer_ptr(&buf), "RIFF", 4 ) ) {
37 // We've got a RIFF file
38 buffer_consume(&buf, 4);
39
40 chunk_size = buffer_get_int_le(&buf);
41
42 // Check format
43 if ( strncmp( (char *)buffer_ptr(&buf), "WAVE", 4 ) ) {
44 PerlIO_printf(PerlIO_stderr(), "Invalid WAV file: missing WAVE header: %s\n", file);
45 err = -1;
46 goto out;
47 }
48
49 buffer_consume(&buf, 4);
50
51 my_hv_store( info, "file_size", newSVuv(file_size) );
52
53 _parse_wav(infile, &buf, file, file_size, info, tags);
54 }
55 else if ( !strncmp( (char *)buffer_ptr(&buf), "FORM", 4 ) ) {
56 // We've got an AIFF file
57 char *bptr;
58
59 buffer_consume(&buf, 4);
60
61 chunk_size = buffer_get_int(&buf);
62
63 // Check format
64 bptr = buffer_ptr(&buf);
65 if ( bptr[0] == 'A' && bptr[1] == 'I' && bptr[2] == 'F' && (bptr[3] == 'F' || bptr[3] == 'C') ) {
66 buffer_consume(&buf, 4);
67
68 my_hv_store( info, "file_size", newSVuv(file_size) );
69
70 _parse_aiff(infile, &buf, file, file_size, info, tags);
71 }
72 else {
73 PerlIO_printf(PerlIO_stderr(), "Invalid AIFF file: missing AIFF header: %s\n", file);
74 err = -1;
75 goto out;
76 }
77 }
78 else {
79 PerlIO_printf(PerlIO_stderr(), "Invalid WAV file: missing RIFF header: %s\n", file);
80 err = -1;
81 goto out;
82 }
83
84 out:
85 buffer_free(&buf);
86
87 if (err) return err;
88
89 return 0;
90 }
91
92 void
93 _parse_wav(PerlIO *infile, Buffer *buf, char *file, uint32_t file_size, HV *info, HV *tags)
94 {
95 uint32_t offset = 12;
96
97 while ( offset < file_size - 8 ) {
98 char chunk_id[5];
99 uint32_t chunk_size;
100
101 // Verify we have at least 8 bytes
102 if ( !_check_buf(infile, buf, 8, WAV_BLOCK_SIZE) ) {
103 return;
104 }
105
106 strncpy( chunk_id, (char *)buffer_ptr(buf), 4 );
107 chunk_id[4] = '\0';
108 buffer_consume(buf, 4);
109
110 chunk_size = buffer_get_int_le(buf);
111
112 // Adjust for padding
113 if ( chunk_size % 2 ) {
114 chunk_size++;
115 }
116
117 offset += 8;
118
119 DEBUG_TRACE("%s size %d\n", chunk_id, chunk_size);
120
121 // Seek past data, everything else we parse
122 // XXX: Are there other large chunks we should ignore?
123 if ( !strcmp( chunk_id, "data" ) ) {
124 SV **bitrate;
125
126 my_hv_store( info, "audio_offset", newSVuv(offset) );
127 my_hv_store( info, "audio_size", newSVuv(chunk_size) );
128
129 // Calculate duration, unless we already know it (i.e. from 'fact')
130 if ( !my_hv_fetch( info, "song_length_ms" ) ) {
131 bitrate = my_hv_fetch( info, "bitrate" );
132 if (bitrate != NULL) {
133 my_hv_store( info, "song_length_ms", newSVuv( (chunk_size / (SvIV(*bitrate) / 8.)) * 1000 ) );
134 }
135 }
136
137 // sanity check size, this is inside the data chunk code
138 // to support setting audio_offset even when the data size is wrong
139 if (chunk_size > file_size - offset) {
140 DEBUG_TRACE("data size > file_size, skipping\n");
141 return;
142 }
143
144 // Seek past data if there are more chunks after it
145 if ( file_size > offset + chunk_size ) {
146 PerlIO_seek(infile, offset + chunk_size, SEEK_SET);
147 }
148
149 buffer_clear(buf);
150 }
151 else if ( !strcmp( chunk_id, "id3 " ) || !strcmp( chunk_id, "ID3 " ) || !strcmp( chunk_id, "ID32" ) ) {
152 // Read header to verify version
153 unsigned char *bptr = buffer_ptr(buf);
154
155 if (
156 (bptr[0] == 'I' && bptr[1] == 'D' && bptr[2] == '3') &&
157 bptr[3] < 0xff && bptr[4] < 0xff &&
158 bptr[6] < 0x80 && bptr[7] < 0x80 && bptr[8] < 0x80 && bptr[9] < 0x80
159 ) {
160 // Start parsing ID3 from offset
161 parse_id3(infile, file, info, tags, offset, file_size);
162 }
163
164 // Seek past ID3 and clear buffer
165 PerlIO_seek(infile, offset + chunk_size, SEEK_SET);
166 buffer_clear(buf);
167 }
168 else {
169 // sanity check size
170 if (chunk_size > file_size - offset) {
171 DEBUG_TRACE("chunk_size > file_size, skipping\n");
172 return;
173 }
174
175 // Make sure we have enough data
176 if ( !_check_buf(infile, buf, chunk_size, WAV_BLOCK_SIZE) ) {
177 return;
178 }
179
180 if ( !strcmp( chunk_id, "fmt " ) ) {
181 _parse_wav_fmt(buf, chunk_size, info);
182 }
183 else if ( !strcmp( chunk_id, "LIST" ) ) {
184 _parse_wav_list(buf, chunk_size, tags);
185 }
186 else if ( !strcmp( chunk_id, "PEAK" ) ) {
187 _parse_wav_peak(buf, chunk_size, info, 0);
188 }
189 else if ( !strcmp( chunk_id, "fact" ) ) {
190 // A 4-byte fact chunk in a non-PCM wav is the number of samples
191 // Use it to calculate duration
192 if ( chunk_size == 4 ) {
193 uint32_t num_samples = buffer_get_int_le(buf);
194 SV **samplerate = my_hv_fetch( info, "samplerate" );
195 if (samplerate != NULL) {
196 DEBUG_TRACE("[wav] Setting song_length_ms from fact chunk: ( num_samples(%d) * 1000 / samplerate(%ld) )\n", num_samples, SvIV(*samplerate));
197 // GH#2, cast num_samples to 64-bit to avoid 32-bit overflow
198 my_hv_store( info, "song_length_ms", newSVuv( ((uint64_t)num_samples * 1000) / SvIV(*samplerate) ) );
199 }
200 }
201 else {
202 // Unknown, skip it
203 buffer_consume(buf, chunk_size);
204 }
205 }
206 else {
207 if (
208 !strcmp(chunk_id, "SAUR") // Wavosour data chunk
209 || !strcmp(chunk_id, "otom") // Wavosaur?
210 || !strcmp(chunk_id, "PAD ") // Padding
211 ) {
212 // Known chunks to skip
213 }
214 else {
215 // Warn about unknown chunks so we can investigate them
216 PerlIO_printf(PerlIO_stderr(), "Unhandled WAV chunk %s size %d (skipped)\n", chunk_id, chunk_size);
217 }
218
219 buffer_consume(buf, chunk_size);
220 }
221 }
222
223 offset += chunk_size;
224 }
225 }
226
227 void
228 _parse_wav_fmt(Buffer *buf, uint32_t chunk_size, HV *info)
229 {
230 uint32_t samplerate;
231 uint16_t channels, bps;
232 uint16_t format = buffer_get_short_le(buf);
233
234 my_hv_store( info, "format", newSVuv(format) );
235
236 channels = buffer_get_short_le(buf);
237 my_hv_store( info, "channels", newSVuv(channels) );
238
239 samplerate = buffer_get_int_le(buf);
240 my_hv_store( info, "samplerate", newSVuv(samplerate) );
241 my_hv_store( info, "bitrate", newSVuv( buffer_get_int_le(buf) * 8 ) );
242 my_hv_store( info, "block_align", newSVuv( buffer_get_short_le(buf) ) );
243
244 bps = buffer_get_short_le(buf);
245 my_hv_store( info, "bits_per_sample", newSVuv(bps) );
246
247 if ( chunk_size > 16 ) {
248 uint16_t extra_len = buffer_get_short_le(buf);
249
250 // Bug 14462, a WAV file with only an 18-byte fmt chunk should ignore extra_len bytes
251 if (extra_len && chunk_size > 18) {
252 DEBUG_TRACE(" skipping extra_len bytes in fmt: %d\n", extra_len);
253 buffer_consume(buf, extra_len);
254 }
255 }
256
257 // DLNA
258 if (channels <= 2 && bps == 16) {
259 if (samplerate == 44100 || samplerate == 48000)
260 my_hv_store( info, "dlna_profile", newSVpv("LPCM", 0) );
261 else if (samplerate >= 8000 && samplerate <= 32000)
262 my_hv_store( info, "dlna_profile", newSVpv("LPCM_low", 0) );
263 }
264 }
265
266 void
267 _parse_wav_list(Buffer *buf, uint32_t chunk_size, HV *tags)
268 {
269 char type_id[5];
270 uint32_t pos = 4;
271
272 strncpy( type_id, (char *)buffer_ptr(buf), 4 );
273 type_id[4] = '\0';
274 buffer_consume(buf, 4);
275
276 DEBUG_TRACE(" LIST type %s\n", type_id);
277
278 if ( !strcmp( type_id, "adtl" ) ) {
279 // XXX need test file
280 PerlIO_printf(PerlIO_stderr(), "Unhandled LIST type adtl\n");
281 buffer_consume(buf, chunk_size - 4);
282 }
283 else if ( !strcmp( type_id, "INFO" ) ) {
284 while ( pos < chunk_size ) {
285 uint32_t len;
286 uint32_t nulls = 0;
287 SV *key;
288 SV *value;
289 unsigned char *bptr;
290
291 key = newSVpvn( buffer_ptr(buf), 4 );
292 buffer_consume(buf, 4);
293 pos += 4;
294
295 len = buffer_get_int_le(buf);
296
297 // Bug 12250, apparently some WAV files don't use the padding byte
298 // so we can't read them.
299 if ( len > chunk_size - pos ) {
300 PerlIO_printf(PerlIO_stderr(), "Invalid data in WAV LIST INFO chunk (len %d > chunk_size - pos %d)\n", len, chunk_size - pos);
301 break;
302 }
303
304 pos += 4 + len;
305
306 // Bug 14946, Strip any nulls from the end of the value
307 bptr = buffer_ptr(buf);
308 while ( len && bptr[len - 1] == '\0' ) {
309 len--;
310 nulls++;
311 }
312
313 value = newSVpvn( buffer_ptr(buf), len );
314 buffer_consume(buf, len + nulls);
315
316 DEBUG_TRACE(" %s / %s (%d + %d nulls)\n", SvPVX(key), SvPVX(value), len, nulls);
317
318 my_hv_store_ent( tags, key, value );
319 SvREFCNT_dec(key);
320
321 // Handle padding
322 if ( (len + nulls) % 2 ) {
323 buffer_consume(buf, 1);
324 pos++;
325 }
326 }
327 }
328 else {
329 PerlIO_printf(PerlIO_stderr(), "Unhandled LIST type %s\n", type_id);
330 buffer_consume(buf, chunk_size - 4);
331 }
332 }
333
334 void
335 _parse_wav_peak(Buffer *buf, uint32_t chunk_size, HV *info, uint8_t big_endian)
336 {
337 uint16_t channels = 0;
338 AV *peaklist = newAV();
339
340 SV **entry = my_hv_fetch( info, "channels" );
341 if ( entry != NULL ) {
342 channels = SvIV(*entry);
343 }
344
345 // Skip version/timestamp
346 buffer_consume(buf, 8);
347
348 while ( channels-- ) {
349 HV *peak = newHV();
350
351 my_hv_store( peak, "value", newSVnv( big_endian ? buffer_get_float32(buf) : buffer_get_float32_le(buf) ) );
352 my_hv_store( peak, "position", newSVuv( big_endian ? buffer_get_int(buf) : buffer_get_int_le(buf) ) );
353
354 av_push( peaklist, newRV_noinc( (SV *)peak) );
355 }
356
357 my_hv_store( info, "peak", newRV_noinc( (SV *)peaklist ) );
358 }
359
360 void
361 _parse_aiff(PerlIO *infile, Buffer *buf, char *file, uint32_t file_size, HV *info, HV *tags)
362 {
363 uint32_t offset = 12;
364
365 while ( offset < file_size - 8 ) {
366 char chunk_id[5];
367 int chunk_size;
368
369 // Verify we have at least 8 bytes
370 if ( !_check_buf(infile, buf, 8, WAV_BLOCK_SIZE) ) {
371 return;
372 }
373
374 strncpy( chunk_id, (char *)buffer_ptr(buf), 4 );
375 chunk_id[4] = '\0';
376 buffer_consume(buf, 4);
377
378 chunk_size = buffer_get_int(buf);
379
380 // Adjust for padding
381 if ( chunk_size % 2 ) {
382 chunk_size++;
383 }
384
385 offset += 8;
386
387 DEBUG_TRACE("%s size %d\n", chunk_id, chunk_size);
388
389 // Seek past SSND, everything else we parse
390 // XXX: Are there other large chunks we should ignore?
391 if ( !strcmp( chunk_id, "SSND" ) ) {
392 int ssnd_offset, ssnd_blocksize;
393
394 if ( !_check_buf(infile, buf, 8, WAV_BLOCK_SIZE) ) {
395 return;
396 }
397
398 ssnd_offset = buffer_get_int(buf);
399 ssnd_blocksize = buffer_get_int(buf);
400
401 DEBUG_TRACE("SSND offset: %u block size: %u\n", ssnd_offset, ssnd_blocksize);
402
403 my_hv_store( info, "audio_offset", newSVuv(offset + 8 + ssnd_offset) );
404 my_hv_store( info, "audio_size", newSVuv(chunk_size - 8 - ssnd_offset) );
405
406 // Seek past data if there are more chunks after it
407 if ( file_size > offset + chunk_size ) {
408 PerlIO_seek(infile, offset + chunk_size, SEEK_SET);
409 }
410
411 buffer_clear(buf);
412 }
413 else if ( !strcmp( chunk_id, "id3 " ) || !strcmp( chunk_id, "ID3 " ) || !strcmp( chunk_id, "ID32" ) ) {
414 // Read header to verify version
415 unsigned char *bptr = buffer_ptr(buf);
416
417 if (
418 (bptr[0] == 'I' && bptr[1] == 'D' && bptr[2] == '3') &&
419 bptr[3] < 0xff && bptr[4] < 0xff &&
420 bptr[6] < 0x80 && bptr[7] < 0x80 && bptr[8] < 0x80 && bptr[9] < 0x80
421 ) {
422 // Start parsing ID3 from offset
423 parse_id3(infile, file, info, tags, offset, file_size);
424 }
425
426 // Seen ID3 chunks with the chunk size in little-endian instead of big-endian
427 if (chunk_size < 0 || offset + chunk_size > file_size) {
428 break;
429 }
430
431 // Seek past ID3 and clear buffer
432 DEBUG_TRACE("Seeking past ID3 to %d\n", offset + chunk_size);
433 PerlIO_seek(infile, offset + chunk_size, SEEK_SET);
434 buffer_clear(buf);
435 }
436 else {
437 // sanity check size
438 if (chunk_size > file_size - offset) {
439 DEBUG_TRACE("chunk_size > file_size, skipping\n");
440 return;
441 }
442
443 // Make sure we have enough data
444 if ( !_check_buf(infile, buf, chunk_size, WAV_BLOCK_SIZE) ) {
445 return;
446 }
447
448 if ( !strcmp( chunk_id, "COMM" ) ) {
449 _parse_aiff_comm(buf, chunk_size, info);
450 }
451 else if ( !strcmp( chunk_id, "PEAK" ) ) {
452 _parse_wav_peak(buf, chunk_size, info, 1);
453 }
454 else {
455 PerlIO_printf(PerlIO_stderr(), "Unhandled AIFF chunk %s size %d (skipped)\n", chunk_id, chunk_size);
456 buffer_consume(buf, chunk_size);
457 }
458 }
459
460 offset += chunk_size;
461 }
462 }
463
464 void
465 _parse_aiff_comm(Buffer *buf, uint32_t chunk_size, HV *info)
466 {
467 uint16_t channels = buffer_get_short(buf);
468 uint32_t frames = buffer_get_int(buf);
469 uint16_t bits_per_sample = buffer_get_short(buf);
470 double samplerate = buffer_get_ieee_float(buf);
471
472 my_hv_store( info, "channels", newSVuv(channels) );
473 my_hv_store( info, "bits_per_sample", newSVuv(bits_per_sample) );
474 my_hv_store( info, "samplerate", newSVuv(samplerate) );
475
476 my_hv_store( info, "bitrate", newSVuv( samplerate * channels * bits_per_sample ) );
477 my_hv_store( info, "song_length_ms", newSVuv( ((frames * 1.0) / samplerate) * 1000 ) );
478 my_hv_store( info, "block_align", newSVuv( channels * bits_per_sample / 8 ) );
479
480 if (chunk_size > 18) {
481 // AIFC extra data
482 my_hv_store( info, "compression_type", newSVpvn( buffer_ptr(buf), 4 ) );
483 buffer_consume(buf, 4);
484
485 my_hv_store( info, "compression_name", newSVpvn( buffer_ptr(buf), chunk_size - 22 ) );
486 buffer_consume(buf, chunk_size - 22);
487 }
488
489 // DLNA
490 if (channels <= 2 && bits_per_sample == 16) {
491 if (samplerate == 44100 || samplerate == 48000)
492 my_hv_store( info, "dlna_profile", newSVpv("LPCM", 0) );
493 else if (samplerate >= 8000 && samplerate <= 32000)
494 my_hv_store( info, "dlna_profile", newSVpv("LPCM_low", 0) );
495 }
496 }