Debian Codegraph @

src/mp3.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 /*
18 * This file is derived from mt-daap project.
19 */
20
21 #include "mp3.h"
22
23 int
24 get_mp3fileinfo(PerlIO *infile, char *file, HV *info)
25 {
26 mp3info *mp3 = _mp3_parse(infile, file, info);
27
28 buffer_free(mp3->buf);
29 Safefree(mp3->buf);
30 Safefree(mp3->first_frame);
31 Safefree(mp3->xing_frame);
32 Safefree(mp3);
33
34 return 0;
35 }
36
37 int
38 get_mp3tags(PerlIO *infile, char *file, HV *info, HV *tags)
39 {
40 int ret;
41
42 off_t file_size = _file_size(infile);
43
44 // See if this file has an APE tag as fast as possible
45 // This is still a big performance hit :(
46 if ( _has_ape(infile, file_size, info) ) {
47 get_ape_metadata(infile, file, info, tags);
48 }
49
50 ret = parse_id3(infile, file, info, tags, 0, file_size);
51
52 return ret;
53 }
54
55 int
56 _is_ape_header(char *bptr)
57 {
58 if ( bptr[0] == 'A' && bptr[1] == 'P' && bptr[2] == 'E'
59 && bptr[3] == 'T' && bptr[4] == 'A' && bptr[5] == 'G'
60 && bptr[6] == 'E' && bptr[7] == 'X'
61 ) {
62 return 1;
63 }
64
65 return 0;
66 }
67
68 int
69 _has_ape(PerlIO *infile, off_t file_size, HV *info)
70 {
71 Buffer buf;
72 uint8_t ret = 0;
73 char *bptr;
74
75 if ( (PerlIO_seek(infile, file_size - 160, SEEK_SET)) == -1 ) {
76 return 0;
77 }
78
79 DEBUG_TRACE("Seeked to %d looking for APE tag\n", (int)PerlIO_tell(infile));
80
81 // Bug 9942, read 136 bytes so we can check at -32 bytes in case file
82 // does not have an ID3v1 tag
83 buffer_init(&buf, 136);
84 if ( !_check_buf(infile, &buf, 136, 136) ) {
85 goto out;
86 }
87
88 bptr = buffer_ptr(&buf);
89
90 if ( _is_ape_header(bptr) ) {
91 DEBUG_TRACE("APE tag found at -160 (with ID3v1)\n");
92 ret = 1;
93 }
94 else {
95 // Look for Lyrics tag which may possibly be between APE and ID3v1
96 bptr += 23;
97 if ( bptr[0] == 'L' && bptr[1] == 'Y' && bptr[2] == 'R'
98 && bptr[3] == 'I' && bptr[4] == 'C' && bptr[5] == 'S'
99 && bptr[6] == '2' && bptr[7] == '0' && bptr[8] == '0'
100 ) {
101 // read Lyrics tag size, stored as a 6-digit number (!?)
102 // http://www.id3.org/Lyrics3v2
103 uint32_t lyrics_size = 0;
104 off_t file_size = _file_size(infile);
105
106 bptr -= 6;
107 lyrics_size = atoi(bptr);
108
109 DEBUG_TRACE("LYRICS200 tag found (size %d), adjusting APE offset (%d)\n", lyrics_size, -(160 + lyrics_size + 15));
110
111 if ( (PerlIO_seek(infile, file_size - (160 + lyrics_size + 15), SEEK_SET)) == -1 ) {
112 goto out;
113 }
114
115 DEBUG_TRACE("Seeked before Lyrics tag to %d\n", (int)PerlIO_tell(infile));
116
117 buffer_clear(&buf);
118 if ( !_check_buf(infile, &buf, 136, 136) ) {
119 goto out;
120 }
121
122 if ( _is_ape_header( buffer_ptr(&buf) ) ) {
123 DEBUG_TRACE("APE tag found at %d (ID3v1 + Lyricsv2)\n", -(160 + lyrics_size + 15));
124 ret = 1;
125 goto out;
126 }
127
128 // APE code will remove the lyrics_size from audio_size, but if no APE tag do it here
129 if (my_hv_exists(info, "audio_size")) {
130 int audio_size = SvIV(*(my_hv_fetch(info, "audio_size")));
131 my_hv_store(info, "audio_size", newSVuv(audio_size - lyrics_size - 15));
132 DEBUG_TRACE("Reduced audio_size value by Lyrics2 tag size %d\n", lyrics_size + 15);
133 }
134 }
135
136 // APE tag without ID3v1 tag will be -32 bytes from end
137 buffer_consume(&buf, 128);
138
139 bptr = buffer_ptr(&buf);
140
141 if ( _is_ape_header(bptr) ) {
142 DEBUG_TRACE("APE tag found at -32 (no ID3v1)\n");
143 ret = 1;
144 }
145 }
146
147 out:
148 buffer_free(&buf);
149
150 return ret;
151 }
152
153 // _decode_mp3_frame, based on pcutmp3 FrameHeader.decode()
154 int
155 _decode_mp3_frame(unsigned char *bptr, struct mp3frame *frame)
156 {
157 int i;
158
159 frame->header32 = GET_INT32BE(bptr);
160
161 frame->mpegID = (frame->header32 >> 19) & 3;
162 frame->layerID = (frame->header32 >> 17) & 3;
163 frame->crc16_used = (frame->header32 & 0x00010000) == 0;
164 frame->bitrate_index = (frame->header32 >> 12) & 0xF;
165 frame->samplingrate_index = (frame->header32 >> 10) & 3;
166 frame->padding = (frame->header32 & 0x00000200) != 0;
167 frame->private_bit_set = (frame->header32 & 0x00000100) != 0;
168 frame->mode = (frame->header32 >> 6) & 3;
169 frame->mode_extension = (frame->header32 >> 4) & 3;
170 frame->copyrighted = (frame->header32 & 0x00000008) != 0;
171 frame->original = (frame->header32 & 0x00000004) == 0; // bit set -> copy
172 frame->emphasis = frame->header32 & 3;
173
174 frame->valid = (frame->mpegID != ILLEGAL_MPEG_ID)
175 && (frame->layerID != ILLEGAL_LAYER_ID)
176 && (frame->bitrate_index != 0)
177 && (frame->bitrate_index != 15)
178 && (frame->samplingrate_index != ILLEGAL_SR);
179
180 if (!frame->valid) {
181 return -1;
182 }
183
184 frame->samplerate = sample_rate_tbl[ frame->samplingrate_index ];
185
186 if (frame->mpegID == MPEG2_ID)
187 frame->samplerate >>= 1; // 16,22,48 kHz
188
189 if (frame->mpegID == MPEG25_ID)
190 frame->samplerate >>= 2; // 8,11,24 kHz
191
192 frame->channels = (frame->mode == MODE_MONO) ? 1 : 2;
193
194 frame->bitrate_kbps = bitrate_map[ frame->mpegID ][ frame->layerID ][ frame->bitrate_index ];
195
196 if (frame->layerID == LAYER1_ID) {
197 // layer 1: always 384 samples/frame and 4byte-slots
198 frame->samples_per_frame = 384;
199 frame->bytes_per_slot = 4;
200 }
201 else {
202 // layer 2: always 1152 samples/frame
203 // layer 3: MPEG1: 1152 samples/frame, MPEG2/2.5: 576 samples/frame
204 frame->samples_per_frame = ((frame->mpegID == MPEG1_ID) || (frame->layerID == LAYER2_ID)) ? 1152 : 576;
205 frame->bytes_per_slot = 1;
206 }
207
208 frame->frame_size = ((frame->bitrate_kbps * 125) * frame->samples_per_frame) / frame->samplerate;
209
210 if (frame->bytes_per_slot > 1)
211 frame->frame_size -= frame->frame_size % frame->bytes_per_slot;
212
213 if (frame->padding)
214 frame->frame_size += frame->bytes_per_slot;
215
216 DEBUG_TRACE("Frame @%p: size=%d, %d samples, %dkbps %d/%d\n",
217 bptr, frame->frame_size, frame->samples_per_frame,
218 frame->bitrate_kbps, frame->samplerate, frame->channels);
219
220 return 0;
221 }
222
223 // _mp3_get_average_bitrate
224 // average bitrate by averaging all the frames in the file. This used
225 // to seek to the middle of the file and take a 32K chunk but this was
226 // found to have bugs if it seeked near invalid FF sync bytes that could
227 // be detected as a real frame
228 static short _mp3_get_average_bitrate(mp3info *mp3, uint32_t offset, uint32_t audio_size)
229 {
230 struct mp3frame frame;
231 int frame_count = 0;
232 int bitrate_total = 0;
233 int err = 0;
234 int done = 0;
235 int wrap_skip = 0;
236 int prev_bitrate = 0;
237 bool vbr = FALSE;
238
239 unsigned char *bptr;
240
241 buffer_clear(mp3->buf);
242
243 // Seek to offset
244 PerlIO_seek(mp3->infile, offset, SEEK_SET);
245
246 while ( done < audio_size - 4 ) {
247 // Buffer size is optimized for a possible common case: 20 frames of 192kbps CBR
248 if ( !_check_buf(mp3->infile, mp3->buf, 4, MP3_BLOCK_SIZE * 3) ) {
249 err = -1;
250 goto out;
251 }
252
253 done += buffer_len(mp3->buf);
254
255 if (wrap_skip) {
256 // Skip rest of frame from last buffer
257 DEBUG_TRACE("Wrapped, consuming %d bytes from previous frame\n", wrap_skip);
258 buffer_consume(mp3->buf, wrap_skip);
259 wrap_skip = 0;
260 }
261
262 while ( buffer_len(mp3->buf) >= 4 ) {
263 bptr = buffer_ptr(mp3->buf);
264 while ( *bptr != 0xFF ) {
265 buffer_consume(mp3->buf, 1);
266
267 if ( buffer_len(mp3->buf) < 4 ) {
268 // ran out of data
269 goto out;
270 }
271
272 bptr = buffer_ptr(mp3->buf);
273 }
274
275 if ( !_decode_mp3_frame( buffer_ptr(mp3->buf), &frame ) ) {
276 // Found a valid frame
277 frame_count++;
278 bitrate_total += frame.bitrate_kbps;
279
280 if ( !vbr ) {
281 // If we see the bitrate changing, we have a VBR file, and read
282 // the entire file. Otherwise, if we see 20 frames with the same
283 // bitrate, assume CBR and stop
284 if (prev_bitrate > 0 && prev_bitrate != frame.bitrate_kbps) {
285 DEBUG_TRACE("Bitrate changed, assuming file is VBR\n");
286 vbr = TRUE;
287 }
288 else {
289 if (frame_count > 20) {
290 DEBUG_TRACE("Found 20 frames with same bitrate, assuming CBR\n");
291 goto out;
292 }
293
294 prev_bitrate = frame.bitrate_kbps;
295 }
296 }
297
298 //DEBUG_TRACE(" Frame %d: %dkbps, %dkHz\n", frame_count, frame.bitrate_kbps, frame.samplerate);
299
300 if (frame.frame_size > buffer_len(mp3->buf)) {
301 // Partial frame in buffer
302 wrap_skip = frame.frame_size - buffer_len(mp3->buf);
303 buffer_consume(mp3->buf, buffer_len(mp3->buf));
304 }
305 else {
306 buffer_consume(mp3->buf, frame.frame_size);
307 }
308 }
309 else {
310 // Not a valid frame, stray 0xFF
311 buffer_consume(mp3->buf, 1);
312 }
313 }
314 }
315
316 out:
317 if (err) return err;
318
319 if (!frame_count) return -1;
320
321 DEBUG_TRACE("Average of %d frames: %dkbps\n", frame_count, bitrate_total / frame_count);
322
323 return bitrate_total / frame_count;
324 }
325
326 static int
327 _parse_xing(mp3info *mp3)
328 {
329 int i;
330 unsigned char *bptr;
331 int xing_offset = 4;
332
333 if (mp3->first_frame->mpegID == MPEG1_ID) {
334 xing_offset += mp3->first_frame->channels == 2 ? 32 : 17;
335 }
336 else {
337 xing_offset += mp3->first_frame->channels == 2 ? 17 : 9;
338 }
339
340 if ( !_check_buf(mp3->infile, mp3->buf, 4 + xing_offset, MP3_BLOCK_SIZE) ) {
341 return 0;
342 }
343
344 buffer_consume(mp3->buf, xing_offset);
345
346 bptr = buffer_ptr(mp3->buf);
347
348 if ( bptr[0] == 'X' || bptr[0] == 'I' ) {
349 if (
350 ( bptr[1] == 'i' && bptr[2] == 'n' && bptr[3] == 'g' )
351 ||
352 ( bptr[1] == 'n' && bptr[2] == 'f' && bptr[3] == 'o' )
353 ) {
354 DEBUG_TRACE("Found Xing/Info tag\n");
355
356 mp3->xing_frame->xing_tag = bptr[0] == 'X';
357 mp3->xing_frame->info_tag = bptr[0] == 'I';
358 mp3->xing_frame->frame_size = mp3->first_frame->frame_size;
359
360 if ( !_check_buf(mp3->infile, mp3->buf, 160, MP3_BLOCK_SIZE) ) {
361 return 0;
362 }
363
364 // It's VBR if tag is Xing, and CBR if Info
365 mp3->vbr = bptr[1] == 'i' ? VBR : CBR;
366
367 buffer_consume(mp3->buf, 4);
368
369 mp3->xing_frame->flags = buffer_get_int(mp3->buf);
370
371 if (mp3->xing_frame->flags & XING_FRAMES) {
372 mp3->xing_frame->xing_frames = buffer_get_int(mp3->buf);
373 }
374
375 if ( mp3->xing_frame->flags & XING_BYTES) {
376 mp3->xing_frame->xing_bytes = buffer_get_int(mp3->buf);
377 }
378
379 if (mp3->xing_frame->flags & XING_TOC) {
380 uint8_t i;
381 bptr = buffer_ptr(mp3->buf);
382 for (i = 0; i < 100; i++) {
383 mp3->xing_frame->xing_toc[i] = bptr[i];
384 }
385
386 mp3->xing_frame->has_toc = 1;
387
388 buffer_consume(mp3->buf, 100);
389 }
390
391 if (mp3->xing_frame->flags & XING_QUALITY) {
392 mp3->xing_frame->xing_quality = buffer_get_int(mp3->buf);
393 }
394
395 // LAME tag
396 bptr = buffer_ptr(mp3->buf);
397 if ( bptr[0] == 'L' && bptr[1] == 'A' && bptr[2] == 'M' && bptr[3] == 'E' ) {
398 mp3->xing_frame->lame_tag = TRUE;
399
400 strncpy(mp3->xing_frame->lame_encoder_version, (char *)bptr, 9);
401 bptr += 9;
402
403 // revision/vbr method byte
404 mp3->xing_frame->lame_tag_revision = bptr[0] >> 4;
405 mp3->xing_frame->lame_vbr_method = bptr[0] & 15;
406 buffer_consume(mp3->buf, 10);
407
408 // Determine vbr status
409 switch (mp3->xing_frame->lame_vbr_method) {
410 case 1:
411 case 8:
412 mp3->vbr = CBR;
413 break;
414 case 2:
415 case 9:
416 mp3->vbr = ABR;
417 break;
418 default:
419 mp3->vbr = VBR;
420 }
421
422 mp3->xing_frame->lame_lowpass = buffer_get_char(mp3->buf) * 100;
423
424 // Skip peak
425 buffer_consume(mp3->buf, 4);
426
427 // Replay Gain, code from mpg123
428 mp3->xing_frame->lame_replay_gain[0] = 0;
429 mp3->xing_frame->lame_replay_gain[1] = 0;
430
431 for (i=0; i<2; i++) {
432 // Originator
433 unsigned char origin;
434 bptr = buffer_ptr(mp3->buf);
435
436 origin = (bptr[0] >> 2) & 0x7;
437
438 if (origin != 0) {
439 // Gain type
440 unsigned char gt = bptr[0] >> 5;
441 if (gt == 1)
442 gt = 0; /* radio */
443 else if (gt == 2)
444 gt = 1; /* audiophile */
445 else
446 continue;
447
448 mp3->xing_frame->lame_replay_gain[gt]
449 = (( (bptr[0] & 0x4) >> 2 ) ? -0.1 : 0.1)
450 * ( (bptr[0] & 0x3) | bptr[1] );
451 }
452
453 buffer_consume(mp3->buf, 2);
454 }
455
456 // Skip encoding flags
457 buffer_consume(mp3->buf, 1);
458
459 // ABR rate/VBR minimum
460 mp3->xing_frame->lame_abr_rate = buffer_get_char(mp3->buf);
461
462 // Encoder delay/padding
463 bptr = buffer_ptr(mp3->buf);
464 mp3->xing_frame->lame_encoder_delay = ((((int)bptr[0]) << 4) | (((int)bptr[1]) >> 4));
465 mp3->xing_frame->lame_encoder_padding = (((((int)bptr[1]) << 8) | (((int)bptr[2]))) & 0xfff);
466 // sanity check
467 if (mp3->xing_frame->lame_encoder_delay < 0 || mp3->xing_frame->lame_encoder_delay > 3000) {
468 mp3->xing_frame->lame_encoder_delay = -1;
469 }
470 if (mp3->xing_frame->lame_encoder_padding < 0 || mp3->xing_frame->lame_encoder_padding > 3000) {
471 mp3->xing_frame->lame_encoder_padding = -1;
472 }
473 buffer_consume(mp3->buf, 3);
474
475 // Misc
476 bptr = buffer_ptr(mp3->buf);
477 mp3->xing_frame->lame_noise_shaping = bptr[0] & 0x3;
478 mp3->xing_frame->lame_stereo_mode = (bptr[0] & 0x1C) >> 2;
479 mp3->xing_frame->lame_unwise = (bptr[0] & 0x20) >> 5;
480 mp3->xing_frame->lame_source_freq = (bptr[0] & 0xC0) >> 6;
481 buffer_consume(mp3->buf, 1);
482
483 // XXX MP3 Gain, can't find a test file, current
484 // mp3gain doesn't write this data
485 /*
486 bptr = buffer_ptr(mp3->buf);
487 unsigned char sign = (bptr[0] & 0x80) >> 7;
488 mp3->xing_frame->lame_mp3gain = bptr[0] & 0x7F;
489 if (sign) {
490 mp3->xing_frame->lame_mp3gain *= -1;
491 }
492 mp3->xing_frame->lame_mp3gain_db = mp3->xing_frame->lame_mp3gain * 1.5;
493 */
494 buffer_consume(mp3->buf, 1);
495
496 // Preset/Surround
497 bptr = buffer_ptr(mp3->buf);
498 mp3->xing_frame->lame_surround = (bptr[0] & 0x38) >> 3;
499 mp3->xing_frame->lame_preset = ((bptr[0] << 8) | bptr[1]) & 0x7ff;
500 buffer_consume(mp3->buf, 2);
501
502 // Music Length
503 mp3->xing_frame->lame_music_length = buffer_get_int(mp3->buf);
504
505 // Skip CRCs
506 }
507 }
508 }
509 // Check for VBRI header from Fhg encoders
510 else if ( bptr[0] == 'V' && bptr[1] == 'B' && bptr[2] == 'R' && bptr[3] == 'I' ) {
511 DEBUG_TRACE("Found VBRI tag\n");
512
513 mp3->xing_frame->vbri_tag = TRUE;
514 mp3->vbr = VBR;
515
516 if ( !_check_buf(mp3->infile, mp3->buf, 14, MP3_BLOCK_SIZE) ) {
517 return 0;
518 }
519
520 // Skip tag and version ID
521 buffer_consume(mp3->buf, 6);
522
523 mp3->xing_frame->vbri_delay = buffer_get_short(mp3->buf);
524 mp3->xing_frame->vbri_quality = buffer_get_short(mp3->buf);
525 mp3->xing_frame->vbri_bytes = buffer_get_int(mp3->buf);
526 mp3->xing_frame->vbri_frames = buffer_get_int(mp3->buf);
527 }
528
529 return 1;
530 }
531
532 static int
533 _is_mp3x_profile(mp3info *mp3)
534 {
535 if (mp3->first_frame->layerID != LAYER3_ID)
536 return 0;
537
538 if (mp3->first_frame->mpegID != MPEG1_ID && mp3->first_frame->mpegID != MPEG2_ID)
539 return 0;
540
541 if (mp3->first_frame->samplerate != 16000
542 && mp3->first_frame->samplerate != 22050
543 && mp3->first_frame->samplerate != 24000)
544 return 0;
545
546 if (mp3->bitrate >= 8 && mp3->bitrate <= 320)
547 return 1;
548
549 return 0;
550 }
551
552 static int
553 _is_mp3_profile(mp3info *mp3)
554 {
555 if (mp3->first_frame->layerID != LAYER3_ID)
556 return 0;
557
558 if (mp3->first_frame->mpegID != MPEG1_ID)
559 return 0;
560
561 if (mp3->first_frame->samplerate != 32000
562 && mp3->first_frame->samplerate != 44100
563 && mp3->first_frame->samplerate != 48000)
564 return 0;
565
566 if (mp3->bitrate >= 32 && mp3->bitrate <= 320)
567 return 1;
568
569 return 0;
570 }
571
572 mp3info *
573 _mp3_parse(PerlIO *infile, char *file, HV *info)
574 {
575 unsigned char *bptr;
576 char id3v1taghdr[4];
577
578 uint32_t song_length_ms = 0;
579 uint64_t total_samples = 0;
580 struct mp3frame frame;
581
582 bool found_first_frame = FALSE;
583
584 mp3info *mp3;
585 Newz(0, mp3, sizeof(mp3info), mp3info);
586 Newz(0, mp3->buf, sizeof(Buffer), Buffer);
587 Newz(0, mp3->first_frame, sizeof(mp3frame), mp3frame);
588 Newz(0, mp3->xing_frame, sizeof(xingframe), xingframe);
589
590 mp3->infile = infile;
591 mp3->file = file;
592 mp3->info = info;
593
594 mp3->file_size = _file_size(infile);
595 mp3->id3_size = 0;
596 mp3->audio_offset = 0;
597 mp3->audio_size = 0;
598 mp3->bitrate = 0;
599
600 buffer_init(mp3->buf, MP3_BLOCK_SIZE);
601
602 my_hv_store( info, "file_size", newSVuv(mp3->file_size) );
603
604 if ( !_check_buf(mp3->infile, mp3->buf, 10, MP3_BLOCK_SIZE) ) {
605 goto out;
606 }
607
608 bptr = buffer_ptr(mp3->buf);
609
610 if (
611 (bptr[0] == 'I' && bptr[1] == 'D' && bptr[2] == '3') &&
612 bptr[3] < 0xff && bptr[4] < 0xff &&
613 bptr[6] < 0x80 && bptr[7] < 0x80 && bptr[8] < 0x80 && bptr[9] < 0x80
614 ) {
615 /* found an ID3 header... */
616 mp3->id3_size = 10 + (bptr[6]<<21) + (bptr[7]<<14) + (bptr[8]<<7) + bptr[9];
617
618 if (bptr[5] & 0x10) {
619 // footer present
620 mp3->id3_size += 10;
621 }
622
623 DEBUG_TRACE("Found ID3v2.%d.%d tag, size %d\n", bptr[3], bptr[4], mp3->id3_size);
624
625 // Always seek past the ID3 tags
626 _mp3_skip(mp3, mp3->id3_size);
627
628 if ( !_check_buf(mp3->infile, mp3->buf, 4, MP3_BLOCK_SIZE) ) {
629 goto out;
630 }
631
632 mp3->audio_offset += mp3->id3_size;
633 }
634
635 // Find an MP3 frame
636 while ( !found_first_frame && buffer_len(mp3->buf) ) {
637 bptr = buffer_ptr(mp3->buf);
638
639 while ( *bptr != 0xFF ) {
640 buffer_consume(mp3->buf, 1);
641
642 mp3->audio_offset++;
643
644 if ( !buffer_len(mp3->buf) ) {
645 if (mp3->audio_offset >= mp3->file_size - 4) {
646 // No audio frames in file
647 warn("Unable to find any MP3 frames in file: %s\n", file);
648 goto out;
649 }
650
651 if ( !_check_buf(mp3->infile, mp3->buf, 4, MP3_BLOCK_SIZE) ) {
652 warn("Unable to find any MP3 frames in file: %s\n", file);
653 goto out;
654 }
655 }
656
657 bptr = buffer_ptr(mp3->buf);
658 }
659
660 DEBUG_TRACE("Found FF sync at offset %d\n", (int)mp3->audio_offset);
661
662 // Make sure we have 4 bytes
663 if ( !_check_buf(mp3->infile, mp3->buf, 4, MP3_BLOCK_SIZE) ) {
664 goto out;
665 }
666
667 if ( !_decode_mp3_frame( (unsigned char *)buffer_ptr(mp3->buf), &frame ) ) {
668 struct mp3frame frame2, frame3;
669
670 // Need the whole frame to consider it valid
671 if ( _check_buf(mp3->infile, mp3->buf, frame.frame_size, MP3_BLOCK_SIZE)
672
673 // If we have enough data for the start of the next frame then
674 // it must also look valid and be consistent
675 && (
676 !_check_buf(mp3->infile, mp3->buf, frame.frame_size + 4, MP3_BLOCK_SIZE)
677 || (
678 !_decode_mp3_frame( (unsigned char *)buffer_ptr(mp3->buf) + frame.frame_size, &frame2 )
679 && frame.samplerate == frame2.samplerate
680 && frame.channels == frame2.channels
681 )
682 )
683
684 // If we have enough data for the start of the over-next frame then
685 // it must also look valid and be consistent
686 && (
687 !_check_buf(mp3->infile, mp3->buf, frame.frame_size + frame2.frame_size + 4, MP3_BLOCK_SIZE)
688 || (
689 !_decode_mp3_frame( (unsigned char *)buffer_ptr(mp3->buf) + frame.frame_size + frame2.frame_size, &frame3 )
690 && frame.samplerate == frame3.samplerate
691 && frame.channels == frame3.channels
692 )
693 )
694 ) {
695 // Found a valid frame
696 DEBUG_TRACE(" valid frame\n");
697
698 found_first_frame = 1;
699 }
700 else {
701 DEBUG_TRACE(" false sync\n");
702 }
703 }
704
705 if (!found_first_frame) {
706 // Not a valid frame, stray 0xFF
707 DEBUG_TRACE(" invalid frame\n");
708
709 buffer_consume(mp3->buf, 1);
710 mp3->audio_offset++;
711 }
712 }
713
714 if ( !found_first_frame ) {
715 warn("Unable to find any MP3 frames in file (checked 4K): %s\n", file);
716 goto out;
717 }
718
719 mp3->audio_size = mp3->file_size - mp3->audio_offset;
720
721 memcpy(mp3->first_frame, &frame, sizeof(mp3frame));
722
723 // now check for Xing/Info/VBRI/LAME headers
724 if ( !_parse_xing(mp3) ) {
725 goto out;
726 }
727
728 // use LAME CBR/ABR value for bitrate if available
729 if ( (mp3->vbr == CBR || mp3->vbr == ABR) && mp3->xing_frame->lame_abr_rate ) {
730 if (mp3->xing_frame->lame_abr_rate >= 255) {
731 // ABR rate field only codes up to 255, use preset value instead
732 if (mp3->xing_frame->lame_preset <= 320) {
733 mp3->bitrate = mp3->xing_frame->lame_preset;
734 DEBUG_TRACE("bitrate from lame_preset: %d\n", mp3->bitrate);
735 }
736 }
737 else {
738 mp3->bitrate = mp3->xing_frame->lame_abr_rate;
739 DEBUG_TRACE("bitrate from lame_abr_rate: %d\n", mp3->bitrate);
740 }
741 }
742
743 // Or if we have a Xing header, use it to determine bitrate
744 if (!mp3->bitrate && (mp3->xing_frame->xing_frames && mp3->xing_frame->xing_bytes)) {
745 float mfs = (float)frame.samplerate / ( frame.mpegID == MPEG2_ID || frame.mpegID == MPEG25_ID ? 72000. : 144000. );
746 mp3->bitrate = ( (float)mp3->xing_frame->xing_bytes / (float)mp3->xing_frame->xing_frames * mfs );
747 DEBUG_TRACE("bitrate from Xing header: %d\n", mp3->bitrate);
748 }
749
750 // Or use VBRI header
751 else if (mp3->xing_frame->vbri_frames && mp3->xing_frame->vbri_bytes) {
752 float mfs = (float)frame.samplerate / ( frame.mpegID == MPEG2_ID || frame.mpegID == MPEG25_ID ? 72000. : 144000. );
753 mp3->bitrate = ( mp3->xing_frame->vbri_bytes / mp3->xing_frame->vbri_frames * mfs );
754 DEBUG_TRACE("bitrate from VBRI header: %d\n", mp3->bitrate);
755 }
756
757 // check if last 128 bytes is ID3v1.0 or ID3v1.1 tag
758 PerlIO_seek(infile, mp3->file_size - 128, SEEK_SET);
759 if (PerlIO_read(infile, id3v1taghdr, 4) == 4) {
760 if (id3v1taghdr[0]=='T' && id3v1taghdr[1]=='A' && id3v1taghdr[2]=='G') {
761 DEBUG_TRACE("ID3v1 tag found\n");
762 mp3->audio_size -= 128;
763 }
764 }
765
766 // If we don't know the bitrate from Xing/LAME/VBRI, calculate average
767 if ( !mp3->bitrate ) {
768 DEBUG_TRACE("Calculating average bitrate starting from %d...\n", (int)mp3->audio_offset);
769 mp3->bitrate = _mp3_get_average_bitrate(mp3, mp3->audio_offset, mp3->audio_size);
770
771 if (mp3->bitrate <= 0) {
772 // Couldn't determine bitrate, just use
773 // the bitrate from the last frame we parsed
774 DEBUG_TRACE("Unable to determine bitrate, using bitrate of most recent frame (%d)\n", frame.bitrate_kbps);
775 mp3->bitrate = frame.bitrate_kbps;
776 }
777 }
778
779 if (mp3->xing_frame->xing_frames) {
780 total_samples = mp3->xing_frame->xing_frames * frame.samples_per_frame;
781
782 if (mp3->xing_frame->lame_tag) {
783 // subtract delay/padding to get accurate sample count
784 total_samples -= (mp3->xing_frame->lame_encoder_delay + mp3->xing_frame->lame_encoder_padding);
785 }
786
787 song_length_ms = (int) ((double)(total_samples * 1000.) / (double) frame.samplerate);
788 }
789 else if (mp3->xing_frame->vbri_frames) {
790 song_length_ms = (int) ((double)(mp3->xing_frame->vbri_frames * frame.samples_per_frame * 1000.)/
791 (double) frame.samplerate);
792 total_samples = mp3->xing_frame->vbri_frames * frame.samples_per_frame;
793 }
794 else {
795 song_length_ms = (int) ((double)mp3->audio_size * 8. /
796 (double)mp3->bitrate);
797 }
798
799 mp3->song_length_ms = song_length_ms;
800
801 my_hv_store( info, "song_length_ms", newSVuv(song_length_ms) );
802 my_hv_store( info, "layer", newSVuv(frame.layerID) );
803 my_hv_store( info, "stereo", newSVuv(frame.channels == 2 ? 1 : 0) );
804 my_hv_store( info, "samples_per_frame", newSVuv(frame.samples_per_frame) );
805 my_hv_store( info, "padding", newSVuv(frame.padding) );
806 my_hv_store( info, "audio_size", newSVuv(mp3->audio_size) );
807 my_hv_store( info, "audio_offset", newSVuv(mp3->audio_offset) );
808 my_hv_store( info, "bitrate", newSVuv( mp3->bitrate * 1000 ) );
809 my_hv_store( info, "samplerate", newSVuv( frame.samplerate ) );
810
811 if (mp3->xing_frame->xing_tag || mp3->xing_frame->info_tag) {
812 if (mp3->xing_frame->xing_frames) {
813 my_hv_store( info, "xing_frames", newSVuv(mp3->xing_frame->xing_frames) );
814 }
815
816 if (mp3->xing_frame->xing_bytes) {
817 my_hv_store( info, "xing_bytes", newSVuv(mp3->xing_frame->xing_bytes) );
818 }
819
820 if (mp3->xing_frame->has_toc) {
821 uint8_t i;
822 AV *xing_toc = newAV();
823
824 for (i = 0; i < 100; i++) {
825 av_push( xing_toc, newSVuv(mp3->xing_frame->xing_toc[i]) );
826 }
827
828 my_hv_store( info, "xing_toc", newRV_noinc( (SV *)xing_toc ) );
829 }
830
831 if (mp3->xing_frame->xing_quality) {
832 my_hv_store( info, "xing_quality", newSVuv(mp3->xing_frame->xing_quality) );
833 }
834 }
835
836 if (mp3->xing_frame->vbri_tag) {
837 my_hv_store( info, "vbri_delay", newSVuv(mp3->xing_frame->vbri_delay) );
838 my_hv_store( info, "vbri_frames", newSVuv(mp3->xing_frame->vbri_frames) );
839 my_hv_store( info, "vbri_bytes", newSVuv(mp3->xing_frame->vbri_bytes) );
840 my_hv_store( info, "vbri_quality", newSVuv(mp3->xing_frame->vbri_quality) );
841 }
842
843 if (mp3->xing_frame->lame_tag) {
844 my_hv_store( info, "lame_encoder_version", newSVpvn(mp3->xing_frame->lame_encoder_version, 9) );
845 my_hv_store( info, "lame_tag_revision", newSViv(mp3->xing_frame->lame_tag_revision) );
846 my_hv_store( info, "lame_vbr_method", newSVpv( vbr_methods[mp3->xing_frame->lame_vbr_method], 0 ) );
847 my_hv_store( info, "lame_lowpass", newSViv(mp3->xing_frame->lame_lowpass) );
848
849 if (mp3->xing_frame->lame_replay_gain[0]) {
850 my_hv_store( info, "lame_replay_gain_radio", newSVpvf( "%.1f dB", mp3->xing_frame->lame_replay_gain[0] ) );
851 }
852
853 if (mp3->xing_frame->lame_replay_gain[1]) {
854 my_hv_store( info, "lame_replay_gain_audiophile", newSVpvf( "%.1f dB", mp3->xing_frame->lame_replay_gain[1] ) );
855 }
856
857 my_hv_store( info, "lame_encoder_delay", newSViv(mp3->xing_frame->lame_encoder_delay) );
858 my_hv_store( info, "lame_encoder_padding", newSViv(mp3->xing_frame->lame_encoder_padding) );
859
860 my_hv_store( info, "lame_noise_shaping", newSViv(mp3->xing_frame->lame_noise_shaping) );
861 my_hv_store( info, "lame_stereo_mode", newSVpv( stereo_modes[mp3->xing_frame->lame_stereo_mode], 0 ) );
862 my_hv_store( info, "lame_unwise_settings", newSViv(mp3->xing_frame->lame_unwise) );
863 my_hv_store( info, "lame_source_freq", newSVpv( source_freqs[mp3->xing_frame->lame_source_freq], 0 ) );
864
865 // my_hv_store( info, "lame_mp3gain", newSViv(mp3->xing_frame->lame_mp3gain) );
866 // my_hv_store( info, "lame_mp3gain_db", newSVnv(mp3->xing_frame->lame_mp3gain_db) );
867
868 my_hv_store( info, "lame_surround", newSVpv( surround[mp3->xing_frame->lame_surround], 0 ) );
869
870 if (mp3->xing_frame->lame_preset < 8) {
871 my_hv_store( info, "lame_preset", newSVpvn( "Unknown", 7 ) );
872 }
873 else if (mp3->xing_frame->lame_preset <= 320) {
874 my_hv_store( info, "lame_preset", newSVpvf( "ABR %d", mp3->xing_frame->lame_preset ) );
875 }
876 else if (mp3->xing_frame->lame_preset <= 500) {
877 mp3->xing_frame->lame_preset /= 10;
878 mp3->xing_frame->lame_preset -= 41;
879 if ( presets_v[mp3->xing_frame->lame_preset] ) {
880 my_hv_store( info, "lame_preset", newSVpv( presets_v[mp3->xing_frame->lame_preset], 0 ) );
881 }
882 }
883 else if (mp3->xing_frame->lame_preset >= 1000 && mp3->xing_frame->lame_preset <= 1007) {
884 mp3->xing_frame->lame_preset -= 1000;
885 if ( presets_old[mp3->xing_frame->lame_preset] ) {
886 my_hv_store( info, "lame_preset", newSVpv( presets_old[mp3->xing_frame->lame_preset], 0 ) );
887 }
888 }
889 }
890
891 if (mp3->vbr == ABR || mp3->vbr == VBR) {
892 my_hv_store( info, "vbr", newSViv(1) );
893 }
894
895 // DLNA profile detection
896 if (_is_mp3x_profile(mp3))
897 my_hv_store( info, "dlna_profile", newSVpvn( "MP3X", 4 ) );
898 else if (_is_mp3_profile(mp3))
899 my_hv_store( info, "dlna_profile", newSVpvn( "MP3", 3 ) );
900
901 out:
902
903 return mp3;
904 }
905
906 off_t
907 mp3_find_frame(PerlIO *infile, char *file, int offset)
908 {
909 Buffer mp3_buf;
910 unsigned char *bptr;
911 unsigned int buf_size;
912 struct mp3frame frame;
913 int frame_offset = -1;
914 HV *info = newHV();
915
916 mp3info *mp3 = _mp3_parse(infile, file, info);
917
918 buffer_init(&mp3_buf, MP3_BLOCK_SIZE);
919
920 if (!mp3->song_length_ms)
921 goto out;
922
923 // (undocumented) If offset is negative, treat it as an absolute file offset in bytes
924 // This is a bit ugly but avoids the need to write an entirely new method
925 if (offset < 0) {
926 frame_offset = abs(offset);
927 if (frame_offset < mp3->audio_offset) {
928 // Force offset to be at least audio_offset, so we don't end up in an ID3 tag
929 frame_offset = mp3->audio_offset;
930 }
931 DEBUG_TRACE("find_frame: using absolute offset value %d\n", frame_offset);
932 }
933 else {
934 if (offset >= mp3->song_length_ms) {
935 goto out;
936 }
937
938 // Use Xing TOC if available
939 if ( mp3->xing_frame->has_toc ) {
940 float percent;
941 uint8_t ipercent;
942 uint16_t tva;
943 uint16_t tvb;
944 float tvx;
945
946 percent = (offset * 1.0 / mp3->song_length_ms) * 100;
947 ipercent = (int)percent;
948
949 if (ipercent > 99)
950 ipercent = 99;
951
952 // Interpolate between 2 TOC points
953 tva = mp3->xing_frame->xing_toc[ipercent];
954 if (ipercent < 99) {
955 tvb = mp3->xing_frame->xing_toc[ipercent + 1];
956 }
957 else {
958 tvb = 256;
959 }
960
961 tvx = tva + (tvb - tva) * (percent - ipercent);
962
963 frame_offset = (int)((1.0/256.0) * tvx * mp3->xing_frame->xing_bytes);
964
965 frame_offset += mp3->audio_offset;
966
967 // Don't return offset == audio_offset, because that would be the Xing frame
968 if (frame_offset == mp3->audio_offset) {
969 DEBUG_TRACE("find_frame: frame_offset == audio_offset, skipping to next frame\n");
970 frame_offset += 1;
971 }
972
973 DEBUG_TRACE("find_frame: using Xing TOC, song_length_ms: %d, percent: %f, tva: %d, tvb: %d, tvx: %f, frame offset: %d\n",
974 mp3->song_length_ms, percent, tva, tvb, tvx, frame_offset
975 );
976 }
977 else {
978 // calculate offset using bitrate
979 float bytes_per_ms = mp3->bitrate / 8.0;
980
981 frame_offset = (int)(bytes_per_ms * offset);
982
983 frame_offset += mp3->audio_offset;
984
985 DEBUG_TRACE("find_frame: using bitrate %d, bytes_per_ms: %f, frame offset: %d\n", mp3->bitrate, bytes_per_ms, frame_offset);
986 }
987 }
988
989 // If frame_offset is too near the end of the file we won't find a valid frame
990 // so require offset to be at least 1000 bytes from the end of the file
991 // XXX this would be more accurate if we determined max_frame_len
992 if ((mp3->file_size - frame_offset) < 1000) {
993 frame_offset -= 1000 - (mp3->file_size - frame_offset);
994 if (frame_offset < 0)
995 frame_offset = 0;
996 DEBUG_TRACE("find_frame: offset too close to end of file, adjusted to %d\n", frame_offset);
997 }
998
999 PerlIO_seek(infile, frame_offset, SEEK_SET);
1000
1001 if ( !_check_buf(infile, &mp3_buf, 4, MP3_BLOCK_SIZE) ) {
1002 frame_offset = -1;
1003 goto out;
1004 }
1005
1006 bptr = (unsigned char *)buffer_ptr(&mp3_buf);
1007 buf_size = buffer_len(&mp3_buf);
1008
1009 // Find 0xFF sync and verify it's a valid mp3 frame header
1010 while (1) {
1011 if (
1012 buf_size < 4
1013 ||
1014 ( bptr[0] == 0xFF && !_decode_mp3_frame( bptr, &frame ) )
1015 ) {
1016 break;
1017 }
1018
1019 bptr++;
1020 buf_size--;
1021 }
1022
1023 if (buf_size >= 4) {
1024 frame_offset += buffer_len(&mp3_buf) - buf_size;
1025 DEBUG_TRACE("find_frame: frame_offset: %d\n", frame_offset);
1026 }
1027 else {
1028 // Didn't find a valid frame, probably too near the end of the file
1029 DEBUG_TRACE("find_frame: did not find a valid frame\n");
1030 frame_offset = -1;
1031 }
1032
1033 out:
1034 buffer_free(&mp3_buf);
1035 SvREFCNT_dec(info);
1036
1037 buffer_free(mp3->buf);
1038 Safefree(mp3->buf);
1039 Safefree(mp3->first_frame);
1040 Safefree(mp3->xing_frame);
1041 Safefree(mp3);
1042
1043 return frame_offset;
1044 }
1045
1046 void
1047 _mp3_skip(mp3info *mp3, uint32_t size)
1048 {
1049 if ( buffer_len(mp3->buf) >= size ) {
1050 buffer_consume(mp3->buf, size);
1051
1052 DEBUG_TRACE(" skipped buffer data size %d\n", size);
1053 }
1054 else {
1055 PerlIO_seek(mp3->infile, size - buffer_len(mp3->buf), SEEK_CUR);
1056 buffer_clear(mp3->buf);
1057
1058 DEBUG_TRACE(" seeked past %d bytes to %d\n", size, (int)PerlIO_tell(mp3->infile));
1059 }
1060 }