1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 Dave Chapman
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
27 #include "mp3_playback.h"
30 #include "replaygain.h"
34 enum tagtype
{ TAGTYPE_APE
= 1, TAGTYPE_VORBIS
};
36 #define APETAG_HEADER_LENGTH 32
37 #define APETAG_HEADER_FORMAT "8LLLL"
38 #define APETAG_ITEM_HEADER_FORMAT "LL"
39 #define APETAG_ITEM_TYPE_MASK 3
41 #define TAG_NAME_LENGTH 32
42 #define TAG_VALUE_LENGTH 128
44 #define MP4_ID(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
46 #define MP4_3gp6 MP4_ID('3', 'g', 'p', '6')
47 #define MP4_alac MP4_ID('a', 'l', 'a', 'c')
48 #define MP4_calb MP4_ID(0xa9, 'a', 'l', 'b')
49 #define MP4_cART MP4_ID(0xa9, 'A', 'R', 'T')
50 #define MP4_cnam MP4_ID(0xa9, 'n', 'a', 'm')
51 #define MP4_cwrt MP4_ID(0xa9, 'w', 'r', 't')
52 #define MP4_ftyp MP4_ID('f', 't', 'y', 'p')
53 #define MP4_gnre MP4_ID('g', 'n', 'r', 'e')
54 #define MP4_hdlr MP4_ID('h', 'd', 'l', 'r')
55 #define MP4_ilst MP4_ID('i', 'l', 's', 't')
56 #define MP4_M4A MP4_ID('M', '4', 'A', ' ')
57 #define MP4_mdat MP4_ID('m', 'd', 'a', 't')
58 #define MP4_mdia MP4_ID('m', 'd', 'i', 'a')
59 #define MP4_mdir MP4_ID('m', 'd', 'i', 'r')
60 #define MP4_meta MP4_ID('m', 'e', 't', 'a')
61 #define MP4_minf MP4_ID('m', 'i', 'n', 'f')
62 #define MP4_moov MP4_ID('m', 'o', 'o', 'v')
63 #define MP4_mp4a MP4_ID('m', 'p', '4', 'a')
64 #define MP4_mp42 MP4_ID('m', 'p', '4', '2')
65 #define MP4_qt MP4_ID('q', 't', ' ', ' ')
66 #define MP4_soun MP4_ID('s', 'o', 'u', 'n')
67 #define MP4_stbl MP4_ID('s', 't', 'b', 'l')
68 #define MP4_stsd MP4_ID('s', 't', 's', 'd')
69 #define MP4_stts MP4_ID('s', 't', 't', 's')
70 #define MP4_trak MP4_ID('t', 'r', 'a', 'k')
71 #define MP4_trkn MP4_ID('t', 'r', 'k', 'n')
72 #define MP4_udta MP4_ID('u', 'd', 't', 'a')
73 #define MP4_extra MP4_ID('-', '-', '-', '-')
85 struct apetag_item_header
97 static const struct format_list formats
[] =
99 { AFMT_MPA_L1
, "mp1" },
100 { AFMT_MPA_L2
, "mp2" },
101 { AFMT_MPA_L2
, "mpa" },
102 { AFMT_MPA_L3
, "mp3" },
103 #if CONFIG_CODEC == SWCODEC
104 { AFMT_OGG_VORBIS
, "ogg" },
105 { AFMT_PCM_WAV
, "wav" },
106 { AFMT_FLAC
, "flac" },
110 { AFMT_WAVPACK
, "wv" },
111 { AFMT_ALAC
, "m4a" },
114 { AFMT_AIFF
, "aif" },
115 { AFMT_AIFF
, "aiff" },
121 #if CONFIG_CODEC == SWCODEC
122 static const unsigned short a52_bitrates
[] =
124 32, 40, 48, 56, 64, 80, 96, 112, 128, 160,
125 192, 224, 256, 320, 384, 448, 512, 576, 640
128 /* Only store frame sizes for 44.1KHz - others are simply multiples
130 static const unsigned short a52_441framesizes
[] =
132 69 * 2, 70 * 2, 87 * 2, 88 * 2, 104 * 2, 105 * 2, 121 * 2,
133 122 * 2, 139 * 2, 140 * 2, 174 * 2, 175 * 2, 208 * 2, 209 * 2,
134 243 * 2, 244 * 2, 278 * 2, 279 * 2, 348 * 2, 349 * 2, 417 * 2,
135 418 * 2, 487 * 2, 488 * 2, 557 * 2, 558 * 2, 696 * 2, 697 * 2,
136 835 * 2, 836 * 2, 975 * 2, 976 * 2, 1114 * 2, 1115 * 2, 1253 * 2,
137 1254 * 2, 1393 * 2, 1394 * 2
140 static const long wavpack_sample_rates
[] =
142 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
143 32000, 44100, 48000, 64000, 88200, 96000, 192000
146 /* Read a string from the file. Read up to size bytes, or, if eos != -1,
147 * until the eos character is found (eos is not stored in buf, unless it is
148 * nil). Writes up to buf_size chars to buf, always terminating with a nil.
149 * Returns number of chars read or -1 on read error.
151 static long read_string(int fd
, char* buf
, long buf_size
, int eos
, long size
)
158 if (read(fd
, &c
, 1) != 1)
167 if ((eos
!= -1) && (eos
== (unsigned char) c
))
183 /* Convert a little-endian structure to native format using a format string.
184 * Does nothing on a little-endian machine.
186 static void convert_endian(void *data
, const char *format
)
194 long* d
= (long*) data
;
204 short* d
= (short*) data
;
213 if (isdigit(*format
))
215 data
= ((char*) data
) + *format
- '0';
225 /* Read an unsigned 16-bit integer from a big-endian file. */
226 #ifdef ROCKBOX_BIG_ENDIAN
227 #define read_uint16be(fd, buf) read((fd), (buf), 2)
229 int read_uint16be(int fd
, unsigned short* buf
)
233 n
= read(fd
, (char*) buf
, 2);
234 *buf
= betoh16(*buf
);
239 /* Read an unsigned 32-bit integer from a big-endian file. */
240 #ifdef ROCKBOX_BIG_ENDIAN
241 #define read_uint32be(fd,buf) read((fd), (buf), 4)
243 int read_uint32be(int fd
, unsigned int* buf
)
247 n
= read(fd
, (char*) buf
, 4);
248 *buf
= betoh32(*buf
);
253 /* Read an unaligned 32-bit little endian long from buffer. */
254 static unsigned long get_long(void* buf
)
256 unsigned char* p
= (unsigned char*) buf
;
258 return p
[0] | (p
[1] << 8) | (p
[2] << 16) | (p
[3] << 24);
261 /* Read a string tag from an M4A file */
262 void read_m4a_tag_string(int fd
, int len
,char** bufptr
,size_t* bytes_remaining
, char** dest
)
266 if (bytes_remaining
==0) {
267 lseek(fd
,len
,SEEK_CUR
); /* Skip everything */
269 /* Skip the data tag header - maybe we should parse it properly? */
270 lseek(fd
,16,SEEK_CUR
);
274 if ((size_t)len
+1 > *bytes_remaining
) {
275 read(fd
,*bufptr
,*bytes_remaining
-1);
276 lseek(fd
,len
-(*bytes_remaining
-1),SEEK_CUR
);
277 *bufptr
+=(*bytes_remaining
-1);
279 read(fd
,*bufptr
,len
);
284 data_length
= strlen(*dest
)+1;
285 *bufptr
=(*dest
)+data_length
;
286 *bytes_remaining
-=data_length
;
290 /* Parse the tag (the name-value pair) and fill id3 and buffer accordingly.
291 * String values to keep are written to buf. Returns number of bytes written
292 * to buf (including end nil).
294 static long parse_tag(const char* name
, char* value
, struct mp3entry
* id3
,
295 char* buf
, long buf_remaining
, enum tagtype type
)
300 if ((((strcasecmp(name
, "track") == 0) && (type
== TAGTYPE_APE
)))
301 || ((strcasecmp(name
, "tracknumber") == 0) && (type
== TAGTYPE_VORBIS
)))
303 id3
->tracknum
= atoi(value
);
304 p
= &(id3
->track_string
);
306 else if (((strcasecmp(name
, "year") == 0) && (type
== TAGTYPE_APE
))
307 || ((strcasecmp(name
, "date") == 0) && (type
== TAGTYPE_VORBIS
)))
309 /* Date can be in more any format in a Vorbis tag, so don't try to
312 if (type
!= TAGTYPE_VORBIS
)
314 id3
->year
= atoi(value
);
317 p
= &(id3
->year_string
);
319 else if (strcasecmp(name
, "title") == 0)
323 else if (strcasecmp(name
, "artist") == 0)
327 else if (strcasecmp(name
, "album") == 0)
331 else if (strcasecmp(name
, "genre") == 0)
333 p
= &(id3
->genre_string
);
335 else if (strcasecmp(name
, "composer") == 0)
337 p
= &(id3
->composer
);
341 len
= parse_replaygain(name
, value
, id3
, buf
, buf_remaining
);
348 len
= MIN(len
, buf_remaining
- 1);
352 strncpy(buf
, value
, len
);
366 /* Read the items in an APEV2 tag. Only looks for a tag at the end of a
367 * file. Returns true if a tag was found and fully read, false otherwise.
369 static bool read_ape_tags(int fd
, struct mp3entry
* id3
)
371 struct apetag_header header
;
373 if ((lseek(fd
, -APETAG_HEADER_LENGTH
, SEEK_END
) < 0)
374 || (read(fd
, &header
, APETAG_HEADER_LENGTH
) != APETAG_HEADER_LENGTH
)
375 || (memcmp(header
.id
, "APETAGEX", sizeof(header
.id
))))
380 convert_endian(&header
, APETAG_HEADER_FORMAT
);
383 if ((header
.version
== 2000) && (header
.item_count
> 0)
384 && (header
.length
> APETAG_HEADER_LENGTH
))
386 char *buf
= id3
->id3v2buf
;
387 unsigned int buf_remaining
= sizeof(id3
->id3v2buf
)
388 + sizeof(id3
->id3v1buf
);
389 unsigned int tag_remaining
= header
.length
- APETAG_HEADER_LENGTH
;
392 if (lseek(fd
, -header
.length
, SEEK_END
) < 0)
397 for (i
= 0; i
< header
.item_count
; i
++)
399 struct apetag_item_header item
;
400 char name
[TAG_NAME_LENGTH
];
401 char value
[TAG_VALUE_LENGTH
];
404 if (tag_remaining
< sizeof(item
))
409 if (read(fd
, &item
, sizeof(item
)) < (long) sizeof(item
))
414 convert_endian(&item
, APETAG_ITEM_HEADER_FORMAT
);
415 tag_remaining
-= sizeof(item
);
416 r
= read_string(fd
, name
, sizeof(name
), 0, tag_remaining
);
423 tag_remaining
-= r
+ item
.length
;
425 if ((item
.flags
& APETAG_ITEM_TYPE_MASK
) == 0)
429 if (read_string(fd
, value
, sizeof(value
), -1, item
.length
)
435 len
= parse_tag(name
, value
, id3
, buf
, buf_remaining
,
438 buf_remaining
-= len
;
442 if (lseek(fd
, item
.length
, SEEK_CUR
) < 0)
453 /* Read the items in a Vorbis comment packet. Returns true the items were
454 * fully read, false otherwise.
456 static bool read_vorbis_tags(int fd
, struct mp3entry
*id3
,
459 char *buf
= id3
->id3v2buf
;
462 int buf_remaining
= sizeof(id3
->id3v2buf
) + sizeof(id3
->id3v1buf
);
467 if (read(fd
, &len
, sizeof(len
)) < (long) sizeof(len
))
472 convert_endian(&len
, "L");
474 if ((lseek(fd
, len
, SEEK_CUR
) < 0)
475 || (read(fd
, &comment_count
, sizeof(comment_count
))
476 < (long) sizeof(comment_count
)))
481 convert_endian(&comment_count
, "L");
482 tag_remaining
-= len
+ sizeof(len
) + sizeof(comment_count
);
484 if (tag_remaining
<= 0)
489 for (i
= 0; i
< comment_count
; i
++)
491 char name
[TAG_NAME_LENGTH
];
492 char value
[TAG_VALUE_LENGTH
];
495 if (tag_remaining
< 4)
500 if (read(fd
, &len
, sizeof(len
)) < (long) sizeof(len
))
505 convert_endian(&len
, "L");
508 /* Quit if we've passed the end of the page */
509 if (tag_remaining
< len
)
514 tag_remaining
-= len
;
515 read_len
= read_string(fd
, name
, sizeof(name
), '=', len
);
524 if (read_string(fd
, value
, sizeof(value
), -1, len
) < 0)
529 len
= parse_tag(name
, value
, id3
, buf
, buf_remaining
,
532 buf_remaining
-= len
;
535 /* Skip to the end of the block */
538 if (lseek(fd
, tag_remaining
, SEEK_CUR
) < 0)
547 /* Skip an ID3v2 tag if it can be found. We assume the tag is located at the
548 * start of the file, which should be true in all cases where we need to skip it.
549 * Returns true if successfully skipped or not skipped, and false if
550 * something went wrong while skipping.
552 static bool skip_id3v2(int fd
, struct mp3entry
*id3
)
557 if (memcmp(buf
, "ID3", 3) == 0)
559 /* We have found an ID3v2 tag at the start of the file - find its
560 length and then skip it. */
561 if ((id3
->first_frame_offset
= getid3v2len(fd
)) == 0)
564 if ((lseek(fd
, id3
->first_frame_offset
, SEEK_SET
) < 0))
569 lseek(fd
, 0, SEEK_SET
);
570 id3
->first_frame_offset
= 0;
575 /* A simple parser to read vital metadata from an Ogg Vorbis file. Returns
576 * false if metadata needed by the Vorbis codec couldn't be read.
578 static bool get_vorbis_metadata(int fd
, struct mp3entry
* id3
)
580 /* An Ogg File is split into pages, each starting with the string
581 * "OggS". Each page has a timestamp (in PCM samples) referred to as
582 * the "granule position".
584 * An Ogg Vorbis has the following structure:
585 * 1) Identification header (containing samplerate, numchannels, etc)
586 * 2) Comment header - containing the Vorbis Comments
587 * 3) Setup header - containing codec setup information
588 * 4) Many audio packets...
591 /* Use the path name of the id3 structure as a temporary buffer. */
592 unsigned char* buf
= id3
->path
;
595 long last_serial
= 0;
601 if ((lseek(fd
, 0, SEEK_SET
) < 0) || (read(fd
, buf
, 58) < 4))
606 if ((memcmp(buf
, "OggS", 4) != 0) || (memcmp(&buf
[29], "vorbis", 6) != 0))
611 /* We need to ensure the serial number from this page is the same as the
612 * one from the last page (since we only support a single bitstream).
614 serial
= get_long(&buf
[14]);
615 id3
->frequency
= get_long(&buf
[40]);
616 id3
->filesize
= filesize(fd
);
618 /* Comments are in second Ogg page */
619 if (lseek(fd
, 58, SEEK_SET
) < 0)
624 /* Minimum header length for Ogg pages is 27. */
625 if (read(fd
, buf
, 27) < 27)
630 if (memcmp(buf
, "OggS", 4) !=0 )
637 /* read in segment table */
638 if (read(fd
, buf
, segments
) < segments
)
643 /* The second packet in a vorbis stream is the comment packet. It *may*
644 * extend beyond the second page, but usually does not. Here we find the
645 * length of the comment packet (or the rest of the page if the comment
646 * packet extends to the third page).
648 for (i
= 0; i
< segments
; i
++)
652 /* The last segment of a packet is always < 255 bytes */
659 /* Now read in packet header (type and id string) */
660 if (read(fd
, buf
, 7) < 7)
665 comment_size
= remaining
;
668 /* The first byte of a packet is the packet type; comment packets are
671 if ((buf
[0] != 3) || (memcmp(buf
+ 1, "vorbis", 6) !=0))
676 /* Failure to read the tags isn't fatal. */
677 read_vorbis_tags(fd
, id3
, remaining
);
679 /* We now need to search for the last page in the file - identified by
680 * by ('O','g','g','S',0) and retrieve totalsamples.
683 /* A page is always < 64 kB */
684 if (lseek(fd
, -(MIN(64 * 1024, id3
->filesize
)), SEEK_END
) < 0)
693 r
= read(fd
, &buf
[remaining
], MAX_PATH
- remaining
);
704 /* Inefficient (but simple) search */
707 while (i
< (remaining
- 3))
709 if ((buf
[i
] == 'O') && (memcmp(&buf
[i
], "OggS", 4) == 0))
711 if (i
< (remaining
- 17))
713 /* Note that this only reads the low 32 bits of a
716 id3
->samples
= get_long(&buf
[i
+ 6]);
717 last_serial
= get_long(&buf
[i
+ 14]);
719 /* If this page is very small the beginning of the next
720 * header could be in buffer. Jump near end of this header
737 /* Move the remaining bytes to start of buffer.
738 * Reuse var 'segments' as it is no longer needed */
740 while (i
< remaining
)
742 buf
[segments
++] = buf
[i
++];
744 remaining
= segments
;
748 /* Discard the rest of the buffer */
753 /* This file has mutiple vorbis bitstreams (or is corrupt). */
754 /* FIXME we should display an error here. */
755 if (serial
!= last_serial
)
757 logf("serialno mismatch");
759 logf("%ld", last_serial
);
763 id3
->length
= ((int64_t) id3
->samples
* 1000) / id3
->frequency
;
765 if (id3
->length
<= 0)
767 logf("ogg length invalid!");
771 id3
->bitrate
= (((int64_t) id3
->filesize
- comment_size
) * 8) / id3
->length
;
777 static bool get_flac_metadata(int fd
, struct mp3entry
* id3
)
779 /* A simple parser to read vital metadata from a FLAC file - length,
780 * frequency, bitrate etc. This code should either be moved to a
781 * seperate file, or discarded in favour of the libFLAC code.
782 * The FLAC stream specification can be found at
783 * http://flac.sourceforge.net/format.html#stream
786 /* Use the trackname part of the id3 structure as a temporary buffer */
787 unsigned char* buf
= id3
->path
;
790 if (!skip_id3v2(fd
, id3
) || (read(fd
, buf
, 4) < 4))
795 if (memcmp(buf
, "fLaC", 4) != 0)
804 if (read(fd
, buf
, 4) < 0)
809 /* The length of the block */
810 i
= (buf
[1] << 16) | (buf
[2] << 8) | buf
[3];
812 if ((buf
[0] & 0x7f) == 0) /* 0 is the STREAMINFO block */
814 unsigned long totalsamples
;
816 /* FIXME: Don't trust the value of i */
817 if (read(fd
, buf
, i
) < 0)
822 id3
->vbr
= true; /* All FLAC files are VBR */
823 id3
->filesize
= filesize(fd
);
824 id3
->frequency
= (buf
[10] << 12) | (buf
[11] << 4)
825 | ((buf
[12] & 0xf0) >> 4);
826 rc
= true; /* Got vital metadata */
828 /* totalsamples is a 36-bit field, but we assume <= 32 bits are used */
829 totalsamples
= (buf
[14] << 24) | (buf
[15] << 16)
830 | (buf
[16] << 8) | buf
[17];
832 /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
833 id3
->length
= ((int64_t) totalsamples
* 1000) / id3
->frequency
;
835 if (id3
->length
<= 0)
837 logf("flac length invalid!");
841 id3
->bitrate
= (id3
->filesize
* 8) / id3
->length
;
843 else if ((buf
[0] & 0x7f) == 4) /* 4 is the VORBIS_COMMENT block */
845 /* The next i bytes of the file contain the VORBIS COMMENTS. */
846 if (!read_vorbis_tags(fd
, id3
, i
))
855 /* If we have reached the last metadata block, abort. */
860 /* Skip to next metadata block */
861 if (lseek(fd
, i
, SEEK_CUR
) < 0)
872 static bool get_wave_metadata(int fd
, struct mp3entry
* id3
)
874 /* Use the trackname part of the id3 structure as a temporary buffer */
875 unsigned char* buf
= id3
->path
;
876 unsigned long totalsamples
= 0;
877 unsigned long channels
= 0;
878 unsigned long bitspersample
= 0;
879 unsigned long numbytes
= 0;
883 /* get RIFF chunk header */
884 if ((lseek(fd
, 0, SEEK_SET
) < 0)
885 || ((read_bytes
= read(fd
, buf
, 12)) < 12))
890 if ((memcmp(buf
, "RIFF",4) != 0)
891 || (memcmp(&buf
[8], "WAVE", 4) !=0 ))
896 /* iterate over WAVE chunks until 'data' chunk */
899 /* get chunk header */
900 if ((read_bytes
= read(fd
, buf
, 8)) < 8)
904 i
= get_long(&buf
[4]);
906 if (memcmp(buf
, "fmt ", 4) == 0)
908 /* get rest of chunk */
909 if ((read_bytes
= read(fd
, buf
, 16)) < 16)
914 /* skipping wFormatTag */
916 channels
= buf
[2] | (buf
[3] << 8);
917 /* dwSamplesPerSec */
918 id3
->frequency
= get_long(&buf
[4]);
919 /* dwAvgBytesPerSec */
920 id3
->bitrate
= (get_long(&buf
[8]) * 8) / 1000;
921 /* skipping wBlockAlign */
923 bitspersample
= buf
[14] | (buf
[15] << 8);
925 else if (memcmp(buf
, "data", 4) == 0)
930 else if (memcmp(buf
, "fact", 4) == 0)
935 /* get rest of chunk */
936 if ((read_bytes
= read(fd
, buf
, 2)) < 2)
940 totalsamples
= get_long(buf
);
944 /* seek to next chunk (even chunk sizes must be padded) */
948 if(lseek(fd
, i
, SEEK_CUR
) < 0)
952 if ((numbytes
== 0) || (channels
== 0))
957 if (totalsamples
== 0)
960 totalsamples
= numbytes
961 / ((((bitspersample
- 1) / 8) + 1) * channels
);
964 id3
->vbr
= false; /* All WAV files are CBR */
965 id3
->filesize
= filesize(fd
);
967 /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
968 id3
->length
= ((int64_t) totalsamples
* 1000) / id3
->frequency
;
973 /* Read the tag data from an MP4 file, storing up to buffer_size bytes in
976 unsigned long read_mp4_tag(int fd
, unsigned int size_left
, char* buffer
,
977 unsigned int buffer_left
)
979 unsigned int bytes_read
= 0;
981 if (buffer_left
== 0)
983 lseek(fd
, size_left
, SEEK_CUR
); /* Skip everything */
987 /* Skip the data tag header - maybe we should parse it properly? */
988 lseek(fd
, 16, SEEK_CUR
);
991 if (size_left
> buffer_left
)
993 read(fd
, buffer
, buffer_left
);
994 lseek(fd
, size_left
- buffer_left
, SEEK_CUR
);
995 bytes_read
= buffer_left
;
999 read(fd
, buffer
, size_left
);
1000 bytes_read
= size_left
;
1007 /* Read a string tag from an MP4 file */
1008 unsigned int read_mp4_tag_string(int fd
, int size_left
, char** buffer
,
1009 unsigned int* buffer_left
, char** dest
)
1011 unsigned int bytes_read
= read_mp4_tag(fd
, size_left
, *buffer
,
1013 unsigned int length
= 0;
1017 (*buffer
)[bytes_read
] = 0;
1019 length
= strlen(*buffer
) + 1;
1020 *buffer_left
-= length
;
1031 static unsigned int read_mp4_atom(int fd
, unsigned int* size
,
1032 unsigned int* type
, unsigned int size_left
)
1034 read_uint32be(fd
, size
);
1035 read_uint32be(fd
, type
);
1039 /* FAT32 doesn't support files this big, so something seems to
1040 * be wrong. (64-bit sizes should only be used when required.)
1049 if (*size
> size_left
)
1069 static bool read_mp4_tags(int fd
, struct mp3entry
* id3
,
1070 unsigned int size_left
)
1074 unsigned int buffer_left
= sizeof(id3
->id3v2buf
) + sizeof(id3
->id3v1buf
);
1075 char* buffer
= id3
->id3v2buf
;
1080 size_left
= read_mp4_atom(fd
, &size
, &type
, size_left
);
1082 /* DEBUGF("Tag atom: '%c%c%c%c' (%d bytes left)\n", type >> 24 & 0xff,
1083 type >> 16 & 0xff, type >> 8 & 0xff, type & 0xff, size); */
1088 read_mp4_tag_string(fd
, size
, &buffer
, &buffer_left
,
1093 read_mp4_tag_string(fd
, size
, &buffer
, &buffer_left
,
1098 read_mp4_tag_string(fd
, size
, &buffer
, &buffer_left
,
1103 read_mp4_tag_string(fd
, size
, &buffer
, &buffer_left
,
1110 unsigned short genre
;
1112 read_mp4_tag(fd
, size
, (char*) &genre
, sizeof(genre
));
1113 id3
->genre
= betoh16(genre
);
1119 unsigned short n
[2];
1121 read_mp4_tag(fd
, size
, (char*) &n
, sizeof(n
));
1122 id3
->tracknum
= betoh16(n
[1]);
1128 char tag_name
[TAG_NAME_LENGTH
];
1129 unsigned int sub_size
;
1132 read_uint32be(fd
, &sub_size
);
1134 lseek(fd
, sub_size
- 4, SEEK_CUR
);
1136 read_uint32be(fd
, &sub_size
);
1138 lseek(fd
, 8, SEEK_CUR
);
1141 if (sub_size
> sizeof(tag_name
) - 1)
1143 read(fd
, tag_name
, sizeof(tag_name
) - 1);
1144 lseek(fd
, sub_size
- sizeof(tag_name
) - 1, SEEK_CUR
);
1145 tag_name
[sizeof(tag_name
) - 1] = 0;
1149 read(fd
, tag_name
, sub_size
);
1150 tag_name
[sub_size
] = 0;
1153 if ((strcasecmp(tag_name
, "composer") == 0) && !cwrt
)
1155 read_mp4_tag_string(fd
, size
, &buffer
, &buffer_left
,
1161 unsigned int length
= read_mp4_tag_string(fd
, size
,
1162 &buffer
, &buffer_left
, &any
);
1166 /* Re-use the read buffer as the dest buffer... */
1168 buffer_left
+= length
;
1170 parse_replaygain(tag_name
, buffer
, id3
, buffer
,
1178 lseek(fd
, size
, SEEK_CUR
);
1182 while ((size_left
> 0) && (errno
== 0));
1187 static bool read_mp4_container(int fd
, struct mp3entry
* id3
,
1188 unsigned int size_left
)
1192 unsigned int handler
= 0;
1197 size_left
= read_mp4_atom(fd
, &size
, &type
, size_left
);
1199 /* DEBUGF("Atom: '%c%c%c%c' (0x%08x, %d bytes left)\n",
1200 (type >> 24) & 0xff, (type >> 16) & 0xff, (type >> 8) & 0xff,
1201 type & 0xff, type, size); */
1209 read_uint32be(fd
, &id
);
1212 if ((id
!= MP4_M4A
) && (id
!= MP4_mp42
) && (id
!= MP4_qt
)
1213 && (id
!= MP4_3gp6
))
1215 DEBUGF("Unknown MP4 file type: '%c%c%c%c'\n",
1216 id
>> 24 & 0xff, id
>> 16 & 0xff, id
>> 8 & 0xff,
1224 lseek(fd
, 4, SEEK_CUR
); /* Skip version */
1233 rc
= read_mp4_container(fd
, id3
, size
);
1238 if (handler
== MP4_mdir
)
1240 rc
= read_mp4_tags(fd
, id3
, size
);
1246 if (handler
== MP4_soun
)
1248 rc
= read_mp4_container(fd
, id3
, size
);
1254 lseek(fd
, 8, SEEK_CUR
);
1256 rc
= read_mp4_container(fd
, id3
, size
);
1261 lseek(fd
, 8, SEEK_CUR
);
1262 read_uint32be(fd
, &handler
);
1264 /* DEBUGF(" Handler '%c%c%c%c'\n", handler >> 24 & 0xff,
1265 handler >> 16 & 0xff, handler >> 8 & 0xff,handler & 0xff); */
1270 unsigned int entries
;
1273 lseek(fd
, 4, SEEK_CUR
);
1274 read_uint32be(fd
, &entries
);
1277 for (i
= 0; i
< entries
; i
++)
1282 read_uint32be(fd
, &n
);
1283 read_uint32be(fd
, &l
);
1284 id3
->samples
+= n
* l
;
1294 unsigned int frequency
;
1296 id3
->codectype
= (type
== MP4_mp4a
) ? AFMT_AAC
: AFMT_ALAC
;
1297 lseek(fd
, 22, SEEK_CUR
);
1298 read_uint32be(fd
, &frequency
);
1300 id3
->frequency
= frequency
;
1301 /* There're some child atoms here, but we don't need them */
1306 id3
->filesize
= size
;
1313 lseek(fd
, size
, SEEK_CUR
);
1315 while (rc
&& (size_left
> 0) && (errno
== 0) && (id3
->filesize
== 0));
1316 /* Break on non-zero filesize, since Rockbox currently doesn't support
1317 * metadata after the mdat atom (which sets the filesize field).
1323 static bool get_mp4_metadata(int fd
, struct mp3entry
* id3
)
1325 id3
->codectype
= AFMT_UNKNOWN
;
1330 if (read_mp4_container(fd
, id3
, filesize(fd
)) && (errno
== 0)
1331 && (id3
->samples
> 0) && (id3
->frequency
> 0)
1332 && (id3
->filesize
> 0))
1334 if (id3
->codectype
== AFMT_UNKNOWN
)
1336 logf("Not an ALAC or AAC file");
1340 id3
->length
= ((int64_t) id3
->samples
* 1000) / id3
->frequency
;
1342 if (id3
->length
<= 0)
1344 logf("mp4 length invalid!");
1348 id3
->bitrate
= ((int64_t) id3
->filesize
* 8) / id3
->length
;
1349 DEBUGF("MP4 bitrate %d, frequency %d Hz, length %d ms\n",
1350 id3
->bitrate
, id3
->frequency
, id3
->length
);
1354 logf("MP4 metadata error");
1355 DEBUGF("MP4 metadata error. errno %d, length %d, frequency %d, filesize %d\n",
1356 errno
, id3
->length
, id3
->frequency
, id3
->filesize
);
1363 static bool get_musepack_metadata(int fd
, struct mp3entry
*id3
)
1365 const int32_t sfreqs_sv7
[4] = { 44100, 48000, 37800, 32000 };
1367 uint64_t samples
= 0;
1370 if (!skip_id3v2(fd
, id3
))
1372 if (read(fd
, header
, 4*8) != 4*8) return false;
1373 /* Musepack files are little endian, might need swapping */
1374 for (i
= 1; i
< 8; i
++)
1375 header
[i
] = letoh32(header
[i
]);
1376 if (!memcmp(header
, "MP+", 3)) { /* Compare to sig "MP+" */
1377 unsigned int streamversion
;
1379 header
[0] = letoh32(header
[0]);
1380 streamversion
= (header
[0] >> 24) & 15;
1381 if (streamversion
>= 8) {
1382 return false; /* SV8 or higher don't exist yet, so no support */
1383 } else if (streamversion
== 7) {
1384 unsigned int gapless
= (header
[5] >> 31) & 0x0001;
1385 unsigned int last_frame_samples
= (header
[5] >> 20) & 0x07ff;
1386 int track_gain
, album_gain
;
1387 unsigned int bufused
;
1389 id3
->frequency
= sfreqs_sv7
[(header
[2] >> 16) & 0x0003];
1390 samples
= (uint64_t)header
[1]*1152; /* 1152 is mpc frame size */
1392 samples
-= 1152 - last_frame_samples
;
1394 samples
-= 481; /* Musepack subband synth filter delay */
1396 /* Extract ReplayGain data from header */
1397 track_gain
= (int16_t)((header
[3] >> 16) & 0xffff);
1398 id3
->track_gain
= get_replaygain_int(track_gain
);
1399 id3
->track_peak
= ((uint16_t)(header
[3] & 0xffff)) << 9;
1401 album_gain
= (int16_t)((header
[4] >> 16) & 0xffff);
1402 id3
->album_gain
= get_replaygain_int(album_gain
);
1403 id3
->album_peak
= ((uint16_t)(header
[4] & 0xffff)) << 9;
1405 /* Write replaygain values to strings for use in id3 screen. We use
1406 the XING header as buffer space since Musepack files shouldn't
1407 need to use it in any other way */
1408 id3
->track_gain_string
= id3
->toc
;
1409 bufused
= snprintf(id3
->track_gain_string
, 45,
1410 "%d.%d dB", track_gain
/100, abs(track_gain
)%100);
1411 id3
->album_gain_string
= id3
->toc
+ bufused
+ 1;
1412 bufused
= snprintf(id3
->album_gain_string
, 45,
1413 "%d.%d dB", album_gain
/100, abs(album_gain
)%100);
1416 header
[0] = letoh32(header
[0]);
1417 unsigned int streamversion
= (header
[0] >> 11) & 0x03FF;
1418 if (streamversion
!= 4 && streamversion
!= 5 && streamversion
!= 6)
1420 id3
->frequency
= 44100;
1421 id3
->track_gain
= 0;
1422 id3
->track_peak
= 0;
1423 id3
->album_gain
= 0;
1424 id3
->album_peak
= 0;
1426 if (streamversion
>= 5)
1427 samples
= (uint64_t)header
[1]*1152; // 32 bit
1429 samples
= (uint64_t)(header
[1] >> 16)*1152; // 16 bit
1432 if (streamversion
< 6)
1437 /* Estimate bitrate, we should probably subtract the various header sizes
1438 here for super-accurate results */
1439 id3
->length
= ((int64_t) samples
* 1000) / id3
->frequency
;
1441 if (id3
->length
<= 0)
1443 logf("mpc length invalid!");
1447 id3
->filesize
= filesize(fd
);
1448 id3
->bitrate
= id3
->filesize
* 8 / id3
->length
;
1452 static bool get_sid_metadata(int fd
, struct mp3entry
* id3
)
1454 /* Use the trackname part of the id3 structure as a temporary buffer */
1455 unsigned char* buf
= id3
->path
;
1460 if ((lseek(fd
, 0, SEEK_SET
) < 0)
1461 || ((read_bytes
= read(fd
, buf
, sizeof(id3
->path
))) < 44))
1466 if ((memcmp(buf
, "PSID",4) != 0))
1474 strcpy(p
, &buf
[0x16]);
1479 strcpy(p
, &buf
[0x36]);
1484 id3
->frequency
= 44100;
1485 /* New idea as posted by Marco Alanen (ravon):
1486 * Set the songlength in seconds to the number of subsongs
1487 * so every second represents a subsong.
1488 * Users can then skip the current subsong by seeking */
1489 id3
->length
= (buf
[0xf]-1)*1000;
1491 id3
->filesize
= filesize(fd
);
1496 static bool get_adx_metadata(int fd
, struct mp3entry
* id3
)
1498 /* Use the trackname part of the id3 structure as a temporary buffer */
1499 unsigned char * buf
= id3
->path
;
1500 int chanstart
, channels
, read_bytes
;
1501 int looping
= 0, start_adr
= 0, end_adr
= 0;
1503 /* try to get the basic header */
1504 if ((lseek(fd
, 0, SEEK_SET
) < 0)
1505 || ((read_bytes
= read(fd
, buf
, 0x38)) < 0x38))
1507 DEBUGF("lseek or read failed\n");
1511 /* ADX starts with 0x80 */
1512 if (buf
[0] != 0x80) {
1513 DEBUGF("get_adx_metadata: wrong first byte %c\n",buf
[0]);
1517 /* check for a reasonable offset */
1518 chanstart
= ((buf
[2] << 8) | buf
[3]) + 4;
1519 if (chanstart
> 4096) {
1520 DEBUGF("get_adx_metadata: bad chanstart %i\n", chanstart
);
1524 /* check for a workable number of channels */
1526 if (channels
!= 1 && channels
!= 2) {
1527 DEBUGF("get_adx_metadata: bad channel count %i\n",channels
);
1531 id3
->frequency
= (buf
[8] << 24) | (buf
[9] << 16) | (buf
[10] << 8) | buf
[11];
1532 /* 32 samples per 18 bytes */
1533 id3
->bitrate
= id3
->frequency
* channels
* 18 * 8 / 32 / 1000;
1534 id3
->length
= ((unsigned long)
1535 (buf
[12] << 24) | (buf
[13] << 16) | (buf
[14] << 8) | buf
[15]) /
1536 id3
->frequency
* 1000;
1538 id3
->filesize
= filesize(fd
);
1541 if (!memcmp(buf
+0x10,"\x01\xF4\x03\x00",4)) {
1542 /* Soul Calibur 2 style (type 03) */
1543 DEBUGF("get_adx_metadata: type 03 found\n");
1544 /* check if header is too small for loop data */
1545 if (chanstart
-6 < 0x2c) looping
=0;
1547 looping
= (buf
[0x18]) ||
1551 end_adr
= (buf
[0x28]<<24) |
1561 )/32*channels
*18+chanstart
;
1563 } else if (!memcmp(buf
+0x10,"\x01\xF4\x04\x00",4)) {
1564 /* Standard (type 04) */
1565 DEBUGF("get_adx_metadata: type 04 found\n");
1566 /* check if header is too small for loop data */
1567 if (chanstart
-6 < 0x38) looping
=0;
1569 looping
= (buf
[0x24]) ||
1573 end_adr
= (buf
[0x34]<<24) |
1582 )/32*channels
*18+chanstart
;
1585 DEBUGF("get_adx_metadata: error, couldn't determine ADX type\n");
1590 /* 2 loops, 10 second fade */
1591 id3
->length
= (start_adr
-chanstart
+ 2*(end_adr
-start_adr
))
1592 *8 / id3
->bitrate
+ 10000;
1595 /* try to get the channel header */
1596 if ((lseek(fd
, chanstart
-6, SEEK_SET
) < 0)
1597 || ((read_bytes
= read(fd
, buf
, 6)) < 6))
1602 /* check channel header */
1603 if (memcmp(buf
, "(c)CRI", 6) != 0) return false;
1608 static bool get_aiff_metadata(int fd
, struct mp3entry
* id3
)
1610 /* Use the trackname part of the id3 structure as a temporary buffer */
1611 unsigned char* buf
= id3
->path
;
1612 unsigned long numChannels
= 0;
1613 unsigned long numSampleFrames
= 0;
1614 unsigned long sampleSize
= 0;
1615 unsigned long sampleRate
= 0;
1616 unsigned long numbytes
= 0;
1620 if ((lseek(fd
, 0, SEEK_SET
) < 0)
1621 || ((read_bytes
= read(fd
, buf
, sizeof(id3
->path
))) < 44))
1626 if ((memcmp(buf
, "FORM",4) != 0)
1627 || (memcmp(&buf
[8], "AIFF", 4) !=0 ))
1635 while ((numbytes
== 0) && (read_bytes
>= 8))
1638 i
= ((buf
[4]<<24)|(buf
[5]<<16)|(buf
[6]<<8)|buf
[7]);
1640 if (memcmp(buf
, "COMM", 4) == 0)
1643 numChannels
= ((buf
[8]<<8)|buf
[9]);
1644 /* numSampleFrames */
1645 numSampleFrames
=((buf
[10]<<24)|(buf
[11]<<16)|(buf
[12]<<8)|buf
[13]);
1647 sampleSize
= ((buf
[14]<<8)|buf
[15]);
1649 sampleRate
= ((buf
[18]<<24)|(buf
[19]<<16)|(buf
[20]<<8)|buf
[21]);
1650 sampleRate
= sampleRate
>> (16+14-buf
[17]);
1651 /* save format infos */
1652 id3
->bitrate
= (sampleSize
* numChannels
* sampleRate
) / 1000;
1653 id3
->frequency
= sampleRate
;
1654 id3
->length
= ((int64_t) numSampleFrames
* 1000) / id3
->frequency
;
1656 id3
->vbr
= false; /* AIFF files are CBR */
1657 id3
->filesize
= filesize(fd
);
1659 else if (memcmp(buf
, "SSND", 4) == 0)
1666 i
++; /* odd chunk sizes must be padded */
1669 read_bytes
-= i
+ 8;
1672 if ((numbytes
== 0) || (numChannels
== 0))
1678 #endif /* CONFIG_CODEC == SWCODEC */
1681 /* Simple file type probing by looking at the filename extension. */
1682 unsigned int probe_file_format(const char *filename
)
1687 suffix
= strrchr(filename
, '.');
1691 return AFMT_UNKNOWN
;
1696 for (i
= 0; i
< sizeof(formats
) / sizeof(formats
[0]); i
++)
1698 if (strcasecmp(suffix
, formats
[i
].extension
) == 0)
1700 return formats
[i
].format
;
1704 return AFMT_UNKNOWN
;
1707 /* Get metadata for track - return false if parsing showed problems with the
1708 * file that would prevent playback.
1710 bool get_metadata(struct track_info
* track
, int fd
, const char* trackname
,
1713 #if CONFIG_CODEC == SWCODEC
1715 unsigned long totalsamples
;
1719 /* Take our best guess at the codec type based on file extension */
1720 track
->id3
.codectype
= probe_file_format(trackname
);
1722 /* Load codec specific track tag information and confirm the codec type. */
1723 switch (track
->id3
.codectype
)
1728 if (!get_mp3_metadata(fd
, &track
->id3
, trackname
, v1first
))
1735 #if CONFIG_CODEC == SWCODEC
1737 if (!get_flac_metadata(fd
, &(track
->id3
)))
1745 if (!get_musepack_metadata(fd
, &(track
->id3
)))
1747 read_ape_tags(fd
, &(track
->id3
));
1750 case AFMT_OGG_VORBIS
:
1751 if (!get_vorbis_metadata(fd
, &(track
->id3
)))
1759 if (!get_wave_metadata(fd
, &(track
->id3
)))
1767 /* A simple parser to read basic information from a WavPack file. This
1768 * now works with self-extrating WavPack files and also will fail on
1769 * WavPack files containing floating-point audio data (although these
1770 * should be possible to play in theory).
1773 /* Use the trackname part of the id3 structure as a temporary buffer */
1774 buf
= track
->id3
.path
;
1776 for (i
= 0; i
< 256; ++i
) {
1778 /* at every 256 bytes into file, try to read a WavPack header */
1780 if ((lseek(fd
, i
* 256, SEEK_SET
) < 0) || (read(fd
, buf
, 32) < 32))
1785 /* if valid WavPack 4 header version & not floating data, break */
1787 if (memcmp (buf
, "wvpk", 4) == 0 && buf
[9] == 4 &&
1788 (buf
[8] >= 2 && buf
[8] <= 0x10) && !(buf
[24] & 0x80))
1795 logf ("%s is not a WavPack file\n", trackname
);
1799 track
->id3
.vbr
= true; /* All WavPack files are VBR */
1800 track
->id3
.filesize
= filesize (fd
);
1802 if ((buf
[20] | buf
[21] | buf
[22] | buf
[23]) &&
1803 (buf
[12] & buf
[13] & buf
[14] & buf
[15]) != 0xff)
1805 int srindx
= ((buf
[26] >> 7) & 1) + ((buf
[27] << 1) & 14);
1809 track
->id3
.frequency
= 44100;
1813 track
->id3
.frequency
= wavpack_sample_rates
[srindx
];
1816 totalsamples
= get_long(&buf
[12]);
1817 track
->id3
.length
= totalsamples
/ (track
->id3
.frequency
/ 100) * 10;
1818 track
->id3
.bitrate
= filesize (fd
) / (track
->id3
.length
/ 8);
1821 read_ape_tags(fd
, &track
->id3
); /* use any apetag info we find */
1825 /* Use the trackname part of the id3 structure as a temporary buffer */
1826 buf
= track
->id3
.path
;
1828 if ((lseek(fd
, 0, SEEK_SET
) < 0) || (read(fd
, buf
, 5) < 5))
1833 if ((buf
[0] != 0x0b) || (buf
[1] != 0x77))
1835 logf("%s is not an A52/AC3 file\n",trackname
);
1843 logf("A52: Invalid frmsizecod: %d\n",i
);
1847 track
->id3
.bitrate
= a52_bitrates
[i
>> 1];
1848 track
->id3
.vbr
= false;
1849 track
->id3
.filesize
= filesize(fd
);
1851 switch (buf
[4] & 0xc0)
1854 track
->id3
.frequency
= 48000;
1855 track
->id3
.bytesperframe
=track
->id3
.bitrate
* 2 * 2;
1859 track
->id3
.frequency
= 44100;
1860 track
->id3
.bytesperframe
= a52_441framesizes
[i
];
1864 track
->id3
.frequency
= 32000;
1865 track
->id3
.bytesperframe
= track
->id3
.bitrate
* 3 * 2;
1869 logf("A52: Invalid samplerate code: 0x%02x\n", buf
[4] & 0xc0);
1874 /* One A52 frame contains 6 blocks, each containing 256 samples */
1875 totalsamples
= track
->id3
.filesize
/ track
->id3
.bytesperframe
* 6 * 256;
1876 track
->id3
.length
= totalsamples
/ track
->id3
.frequency
* 1000;
1881 if (!get_mp4_metadata(fd
, &(track
->id3
)))
1889 track
->id3
.vbr
= true;
1890 track
->id3
.filesize
= filesize(fd
);
1891 if (!skip_id3v2(fd
, &(track
->id3
)))
1895 /* TODO: read the id3v2 header if it exists */
1899 if (!get_sid_metadata(fd
, &(track
->id3
)))
1906 if (!get_adx_metadata(fd
, &(track
->id3
)))
1908 DEBUGF("get_adx_metadata error\n");
1915 if (!get_aiff_metadata(fd
, &(track
->id3
)))
1922 #endif /* CONFIG_CODEC == SWCODEC */
1925 /* If we don't know how to read the metadata, assume we can't play
1931 /* We have successfully read the metadata from the file */
1933 lseek(fd
, 0, SEEK_SET
);
1934 strncpy(track
->id3
.path
, trackname
, sizeof(track
->id3
.path
));
1935 track
->taginfo_ready
= true;