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 ****************************************************************************/
26 #include "mp3_playback.h"
29 #include "replaygain.h"
33 enum tagtype
{ TAGTYPE_APE
= 1, TAGTYPE_VORBIS
};
35 #define APETAG_HEADER_LENGTH 32
36 #define APETAG_HEADER_FORMAT "8LLLL"
37 #define APETAG_ITEM_HEADER_FORMAT "LL"
38 #define APETAG_ITEM_TYPE_MASK 3
40 #define TAG_NAME_LENGTH 32
41 #define TAG_VALUE_LENGTH 128
53 struct apetag_item_header
65 static const struct format_list formats
[] =
67 { AFMT_MPA_L1
, "mp1" },
68 { AFMT_MPA_L2
, "mp2" },
69 { AFMT_MPA_L2
, "mpa" },
70 { AFMT_MPA_L3
, "mp3" },
71 #if CONFIG_CODEC == SWCODEC
72 { AFMT_OGG_VORBIS
, "ogg" },
73 { AFMT_PCM_WAV
, "wav" },
74 { AFMT_FLAC
, "flac" },
78 { AFMT_WAVPACK
, "wv" },
83 { AFMT_AIFF
, "aiff" },
88 #if CONFIG_CODEC == SWCODEC
89 static const unsigned short a52_bitrates
[] =
91 32, 40, 48, 56, 64, 80, 96, 112, 128, 160,
92 192, 224, 256, 320, 384, 448, 512, 576, 640
95 /* Only store frame sizes for 44.1KHz - others are simply multiples
97 static const unsigned short a52_441framesizes
[] =
99 69 * 2, 70 * 2, 87 * 2, 88 * 2, 104 * 2, 105 * 2, 121 * 2,
100 122 * 2, 139 * 2, 140 * 2, 174 * 2, 175 * 2, 208 * 2, 209 * 2,
101 243 * 2, 244 * 2, 278 * 2, 279 * 2, 348 * 2, 349 * 2, 417 * 2,
102 418 * 2, 487 * 2, 488 * 2, 557 * 2, 558 * 2, 696 * 2, 697 * 2,
103 835 * 2, 836 * 2, 975 * 2, 976 * 2, 1114 * 2, 1115 * 2, 1253 * 2,
104 1254 * 2, 1393 * 2, 1394 * 2
107 static const long wavpack_sample_rates
[] =
109 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
110 32000, 44100, 48000, 64000, 88200, 96000, 192000
113 /* Read a string from the file. Read up to size bytes, or, if eos != -1,
114 * until the eos character is found (eos is not stored in buf, unless it is
115 * nil). Writes up to buf_size chars to buf, always terminating with a nil.
116 * Returns number of chars read or -1 on read error.
118 static long read_string(int fd
, char* buf
, long buf_size
, int eos
, long size
)
125 if (read(fd
, &c
, 1) != 1)
134 if ((eos
!= -1) && (eos
== (unsigned char) c
))
150 /* Convert a little-endian structure to native format using a format string.
151 * Does nothing on a little-endian machine.
153 static void convert_endian(void *data
, const char *format
)
161 long* d
= (long*) data
;
171 short* d
= (short*) data
;
180 if (isdigit(*format
))
182 data
= ((char*) data
) + *format
- '0';
192 /* read_uint32be() - read an unsigned integer from a big-endian
193 (e.g. Quicktime) file. This is used by the .m4a parser
195 #ifdef ROCKBOX_BIG_ENDIAN
196 #define read_uint32be(fd,buf) read((fd),(buf),4)
198 int read_uint32be(int fd
, unsigned int* buf
) {
217 /* Read an unaligned 32-bit little endian long from buffer. */
218 static unsigned long get_long(void* buf
)
220 unsigned char* p
= (unsigned char*) buf
;
222 return p
[0] | (p
[1] << 8) | (p
[2] << 16) | (p
[3] << 24);
225 /* Read a string tag from an M4A file */
226 void read_m4a_tag_string(int fd
, int len
,char** bufptr
,size_t* bytes_remaining
, char** dest
)
230 if (bytes_remaining
==0) {
231 lseek(fd
,len
,SEEK_CUR
); /* Skip everything */
233 /* Skip the data tag header - maybe we should parse it properly? */
234 lseek(fd
,16,SEEK_CUR
);
238 if ((size_t)len
+1 > *bytes_remaining
) {
239 read(fd
,*bufptr
,*bytes_remaining
-1);
240 lseek(fd
,len
-(*bytes_remaining
-1),SEEK_CUR
);
241 *bufptr
+=(*bytes_remaining
-1);
243 read(fd
,*bufptr
,len
);
248 data_length
= strlen(*dest
)+1;
249 *bufptr
=(*dest
)+data_length
;
250 *bytes_remaining
-=data_length
;
254 /* Parse the tag (the name-value pair) and fill id3 and buffer accordingly.
255 * String values to keep are written to buf. Returns number of bytes written
256 * to buf (including end nil).
258 static long parse_tag(const char* name
, char* value
, struct mp3entry
* id3
,
259 char* buf
, long buf_remaining
, enum tagtype type
)
264 if ((((strcasecmp(name
, "track") == 0) && (type
== TAGTYPE_APE
)))
265 || ((strcasecmp(name
, "tracknumber") == 0) && (type
== TAGTYPE_VORBIS
)))
267 id3
->tracknum
= atoi(value
);
268 p
= &(id3
->track_string
);
270 else if (((strcasecmp(name
, "year") == 0) && (type
== TAGTYPE_APE
))
271 || ((strcasecmp(name
, "date") == 0) && (type
== TAGTYPE_VORBIS
)))
273 /* Date can be in more any format in a Vorbis tag, so don't try to
276 if (type
!= TAGTYPE_VORBIS
)
278 id3
->year
= atoi(value
);
281 p
= &(id3
->year_string
);
283 else if (strcasecmp(name
, "title") == 0)
287 else if (strcasecmp(name
, "artist") == 0)
291 else if (strcasecmp(name
, "album") == 0)
295 else if (strcasecmp(name
, "genre") == 0)
297 p
= &(id3
->genre_string
);
299 else if (strcasecmp(name
, "composer") == 0)
301 p
= &(id3
->composer
);
305 len
= parse_replaygain(name
, value
, id3
, buf
, buf_remaining
);
312 len
= MIN(len
, buf_remaining
- 1);
316 strncpy(buf
, value
, len
);
330 /* Read the items in an APEV2 tag. Only looks for a tag at the end of a
331 * file. Returns true if a tag was found and fully read, false otherwise.
333 static bool read_ape_tags(int fd
, struct mp3entry
* id3
)
335 struct apetag_header header
;
337 if ((lseek(fd
, -APETAG_HEADER_LENGTH
, SEEK_END
) < 0)
338 || (read(fd
, &header
, APETAG_HEADER_LENGTH
) != APETAG_HEADER_LENGTH
)
339 || (memcmp(header
.id
, "APETAGEX", sizeof(header
.id
))))
344 convert_endian(&header
, APETAG_HEADER_FORMAT
);
347 if ((header
.version
== 2000) && (header
.item_count
> 0)
348 && (header
.length
> APETAG_HEADER_LENGTH
))
350 char *buf
= id3
->id3v2buf
;
351 unsigned int buf_remaining
= sizeof(id3
->id3v2buf
)
352 + sizeof(id3
->id3v1buf
);
353 unsigned int tag_remaining
= header
.length
- APETAG_HEADER_LENGTH
;
356 if (lseek(fd
, -header
.length
, SEEK_END
) < 0)
361 for (i
= 0; i
< header
.item_count
; i
++)
363 struct apetag_item_header item
;
364 char name
[TAG_NAME_LENGTH
];
365 char value
[TAG_VALUE_LENGTH
];
368 if (tag_remaining
< sizeof(item
))
373 if (read(fd
, &item
, sizeof(item
)) < (long) sizeof(item
))
378 convert_endian(&item
, APETAG_ITEM_HEADER_FORMAT
);
379 tag_remaining
-= sizeof(item
);
380 r
= read_string(fd
, name
, sizeof(name
), 0, tag_remaining
);
387 tag_remaining
-= r
+ item
.length
;
389 if ((item
.flags
& APETAG_ITEM_TYPE_MASK
) == 0)
393 if (read_string(fd
, value
, sizeof(value
), -1, item
.length
)
399 len
= parse_tag(name
, value
, id3
, buf
, buf_remaining
,
402 buf_remaining
-= len
;
406 if (lseek(fd
, item
.length
, SEEK_CUR
) < 0)
417 /* Read the items in a Vorbis comment packet. Returns true the items were
418 * fully read, false otherwise.
420 static bool read_vorbis_tags(int fd
, struct mp3entry
*id3
,
423 char *buf
= id3
->id3v2buf
;
426 int buf_remaining
= sizeof(id3
->id3v2buf
) + sizeof(id3
->id3v1buf
);
431 if (read(fd
, &len
, sizeof(len
)) < (long) sizeof(len
))
436 convert_endian(&len
, "L");
438 if ((lseek(fd
, len
, SEEK_CUR
) < 0)
439 || (read(fd
, &comment_count
, sizeof(comment_count
))
440 < (long) sizeof(comment_count
)))
445 convert_endian(&comment_count
, "L");
446 tag_remaining
-= len
+ sizeof(len
) + sizeof(comment_count
);
448 if (tag_remaining
<= 0)
453 for (i
= 0; i
< comment_count
; i
++)
455 char name
[TAG_NAME_LENGTH
];
456 char value
[TAG_VALUE_LENGTH
];
459 if (tag_remaining
< 4)
464 if (read(fd
, &len
, sizeof(len
)) < (long) sizeof(len
))
469 convert_endian(&len
, "L");
472 /* Quit if we've passed the end of the page */
473 if (tag_remaining
< len
)
478 tag_remaining
-= len
;
479 read_len
= read_string(fd
, name
, sizeof(name
), '=', len
);
488 if (read_string(fd
, value
, sizeof(value
), -1, len
) < 0)
493 len
= parse_tag(name
, value
, id3
, buf
, buf_remaining
,
496 buf_remaining
-= len
;
499 /* Skip to the end of the block */
502 if (lseek(fd
, tag_remaining
, SEEK_CUR
) < 0)
511 /* Skip an ID3v2 tag if it can be found. We assume the tag is located at the
512 * start of the file, which should be true in all cases where we need to skip it.
513 * Returns true if successfully skipped or not skipped, and false if
514 * something went wrong while skipping.
516 static bool skip_id3v2(int fd
, struct mp3entry
*id3
)
521 if (memcmp(buf
, "ID3", 3) == 0)
523 /* We have found an ID3v2 tag at the start of the file - find its
524 length and then skip it. */
525 if ((id3
->first_frame_offset
= getid3v2len(fd
)) == 0)
528 if ((lseek(fd
, id3
->first_frame_offset
, SEEK_SET
) < 0))
533 lseek(fd
, 0, SEEK_SET
);
534 id3
->first_frame_offset
= 0;
539 /* A simple parser to read vital metadata from an Ogg Vorbis file. Returns
540 * false if metadata needed by the Vorbis codec couldn't be read.
542 static bool get_vorbis_metadata(int fd
, struct mp3entry
* id3
)
544 /* An Ogg File is split into pages, each starting with the string
545 * "OggS". Each page has a timestamp (in PCM samples) referred to as
546 * the "granule position".
548 * An Ogg Vorbis has the following structure:
549 * 1) Identification header (containing samplerate, numchannels, etc)
550 * 2) Comment header - containing the Vorbis Comments
551 * 3) Setup header - containing codec setup information
552 * 4) Many audio packets...
555 /* Use the path name of the id3 structure as a temporary buffer. */
556 unsigned char* buf
= id3
->path
;
559 long last_serial
= 0;
565 if ((lseek(fd
, 0, SEEK_SET
) < 0) || (read(fd
, buf
, 58) < 4))
570 if ((memcmp(buf
, "OggS", 4) != 0) || (memcmp(&buf
[29], "vorbis", 6) != 0))
575 /* We need to ensure the serial number from this page is the same as the
576 * one from the last page (since we only support a single bitstream).
578 serial
= get_long(&buf
[14]);
579 id3
->frequency
= get_long(&buf
[40]);
580 id3
->filesize
= filesize(fd
);
582 /* Comments are in second Ogg page */
583 if (lseek(fd
, 58, SEEK_SET
) < 0)
588 /* Minimum header length for Ogg pages is 27. */
589 if (read(fd
, buf
, 27) < 27)
594 if (memcmp(buf
, "OggS", 4) !=0 )
601 /* read in segment table */
602 if (read(fd
, buf
, segments
) < segments
)
607 /* The second packet in a vorbis stream is the comment packet. It *may*
608 * extend beyond the second page, but usually does not. Here we find the
609 * length of the comment packet (or the rest of the page if the comment
610 * packet extends to the third page).
612 for (i
= 0; i
< segments
; i
++)
616 /* The last segment of a packet is always < 255 bytes */
623 /* Now read in packet header (type and id string) */
624 if (read(fd
, buf
, 7) < 7)
629 comment_size
= remaining
;
632 /* The first byte of a packet is the packet type; comment packets are
635 if ((buf
[0] != 3) || (memcmp(buf
+ 1, "vorbis", 6) !=0))
640 /* Failure to read the tags isn't fatal. */
641 read_vorbis_tags(fd
, id3
, remaining
);
643 /* We now need to search for the last page in the file - identified by
644 * by ('O','g','g','S',0) and retrieve totalsamples.
647 /* A page is always < 64 kB */
648 if (lseek(fd
, -(MIN(64 * 1024, id3
->filesize
)), SEEK_END
) < 0)
657 r
= read(fd
, &buf
[remaining
], MAX_PATH
- remaining
);
668 /* Inefficient (but simple) search */
671 while (i
< (remaining
- 3))
673 if ((buf
[i
] == 'O') && (memcmp(&buf
[i
], "OggS", 4) == 0))
675 if (i
< (remaining
- 17))
677 /* Note that this only reads the low 32 bits of a
680 id3
->samples
= get_long(&buf
[i
+ 6]);
681 last_serial
= get_long(&buf
[i
+ 14]);
683 /* If this page is very small the beginning of the next
684 * header could be in buffer. Jump near end of this header
701 /* Move the remaining bytes to start of buffer.
702 * Reuse var 'segments' as it is no longer needed */
704 while (i
< remaining
)
706 buf
[segments
++] = buf
[i
++];
708 remaining
= segments
;
712 /* Discard the rest of the buffer */
717 /* This file has mutiple vorbis bitstreams (or is corrupt). */
718 /* FIXME we should display an error here. */
719 if (serial
!= last_serial
)
721 logf("serialno mismatch");
723 logf("%ld", last_serial
);
727 id3
->length
= (id3
->samples
/ id3
->frequency
) * 1000;
728 id3
->bitrate
= (((int64_t) id3
->filesize
- comment_size
) * 8) / id3
->length
;
734 static bool get_flac_metadata(int fd
, struct mp3entry
* id3
)
736 /* A simple parser to read vital metadata from a FLAC file - length,
737 * frequency, bitrate etc. This code should either be moved to a
738 * seperate file, or discarded in favour of the libFLAC code.
739 * The FLAC stream specification can be found at
740 * http://flac.sourceforge.net/format.html#stream
743 /* Use the trackname part of the id3 structure as a temporary buffer */
744 unsigned char* buf
= id3
->path
;
747 if (!skip_id3v2(fd
, id3
) || (read(fd
, buf
, 4) < 4))
752 if (memcmp(buf
, "fLaC", 4) != 0)
761 if (read(fd
, buf
, 4) < 0)
766 /* The length of the block */
767 i
= (buf
[1] << 16) | (buf
[2] << 8) | buf
[3];
769 if ((buf
[0] & 0x7f) == 0) /* 0 is the STREAMINFO block */
771 unsigned long totalsamples
;
773 /* FIXME: Don't trust the value of i */
774 if (read(fd
, buf
, i
) < 0)
779 id3
->vbr
= true; /* All FLAC files are VBR */
780 id3
->filesize
= filesize(fd
);
781 id3
->frequency
= (buf
[10] << 12) | (buf
[11] << 4)
782 | ((buf
[12] & 0xf0) >> 4);
783 rc
= true; /* Got vital metadata */
785 /* totalsamples is a 36-bit field, but we assume <= 32 bits are used */
786 totalsamples
= (buf
[14] << 24) | (buf
[15] << 16)
787 | (buf
[16] << 8) | buf
[17];
789 /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
790 id3
->length
= (totalsamples
/ id3
->frequency
) * 1000;
791 id3
->bitrate
= (id3
->filesize
* 8) / id3
->length
;
793 else if ((buf
[0] & 0x7f) == 4) /* 4 is the VORBIS_COMMENT block */
795 /* The next i bytes of the file contain the VORBIS COMMENTS. */
796 if (!read_vorbis_tags(fd
, id3
, i
))
805 /* If we have reached the last metadata block, abort. */
810 /* Skip to next metadata block */
811 if (lseek(fd
, i
, SEEK_CUR
) < 0)
822 static bool get_wave_metadata(int fd
, struct mp3entry
* id3
)
824 /* Use the trackname part of the id3 structure as a temporary buffer */
825 unsigned char* buf
= id3
->path
;
826 unsigned long totalsamples
= 0;
827 unsigned long channels
= 0;
828 unsigned long bitspersample
= 0;
829 unsigned long numbytes
= 0;
833 /* get RIFF chunk header */
834 if ((lseek(fd
, 0, SEEK_SET
) < 0)
835 || ((read_bytes
= read(fd
, buf
, 12)) < 12))
840 if ((memcmp(buf
, "RIFF",4) != 0)
841 || (memcmp(&buf
[8], "WAVE", 4) !=0 ))
846 /* iterate over WAVE chunks until 'data' chunk */
849 /* get chunk header */
850 if ((read_bytes
= read(fd
, buf
, 8)) < 8)
854 i
= get_long(&buf
[4]);
856 if (memcmp(buf
, "fmt ", 4) == 0)
858 /* get rest of chunk */
859 if ((read_bytes
= read(fd
, buf
, 16)) < 16)
864 /* skipping wFormatTag */
866 channels
= buf
[2] | (buf
[3] << 8);
867 /* dwSamplesPerSec */
868 id3
->frequency
= get_long(&buf
[4]);
869 /* dwAvgBytesPerSec */
870 id3
->bitrate
= (get_long(&buf
[8]) * 8) / 1000;
871 /* skipping wBlockAlign */
873 bitspersample
= buf
[14] | (buf
[15] << 8);
875 else if (memcmp(buf
, "data", 4) == 0)
880 else if (memcmp(buf
, "fact", 4) == 0)
885 /* get rest of chunk */
886 if ((read_bytes
= read(fd
, buf
, 2)) < 2)
890 totalsamples
= get_long(buf
);
894 /* seek to next chunk (even chunk sizes must be padded) */
898 if(lseek(fd
, i
, SEEK_CUR
) < 0)
902 if ((numbytes
== 0) || (channels
== 0))
907 if (totalsamples
== 0)
910 totalsamples
= numbytes
911 / ((((bitspersample
- 1) / 8) + 1) * channels
);
914 id3
->vbr
= false; /* All WAV files are CBR */
915 id3
->filesize
= filesize(fd
);
917 /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */
918 id3
->length
= (totalsamples
/ id3
->frequency
) * 1000;
923 static bool get_m4a_metadata(int fd
, struct mp3entry
* id3
)
926 unsigned long totalsamples
;
929 size_t bytes_remaining
;
931 unsigned int compressedsize
;
932 unsigned int sample_count
;
933 unsigned int sample_duration
;
938 unsigned char chunk_id
[4];
940 unsigned char sub_chunk_id
[4];
942 /* A simple parser to read vital metadata from an ALAC file.
943 This parser also works for AAC files - they are both stored in
944 a Quicktime M4A container. */
946 /* Use the trackname part of the id3 structure as a temporary buffer */
949 lseek(fd
, 0, SEEK_SET
);
953 /* read the chunks - we stop when we find the mdat chunk and set compressedsize */
954 while (compressedsize
==0) {
955 n
=read_uint32be(fd
,&chunk_len
);
957 // This means it was a 64-bit file, so we have problems.
958 if (chunk_len
== 1) {
959 logf("need 64bit support\n");
963 n
=read(fd
,&chunk_id
,4);
967 if (memcmp(&chunk_id
,"ftyp",4)==0) {
968 /* Check for M4A type */
969 n
=read(fd
,&chunk_id
,4);
970 if ((memcmp(&chunk_id
,"M4A ",4)!=0) &&
971 (memcmp(&chunk_id
,"mp42",4)!=0)) {
972 logf("Not an M4A file, aborting\n");
975 /* Skip rest of chunk */
976 lseek(fd
, chunk_len
- 8 - 4, SEEK_CUR
); /* FIXME not 8 */
977 } else if (memcmp(&chunk_id
,"moov",4)==0) {
978 size_remaining
=chunk_len
- 8; /* FIXME not 8 */
980 while (size_remaining
> 0) {
981 n
=read_uint32be(fd
,&sub_chunk_len
);
982 if ((sub_chunk_len
< 1) || (sub_chunk_len
> size_remaining
)) {
983 logf("Strange sub_chunk_len value inside moov: %d (remaining: %d)\n",sub_chunk_len
,size_remaining
);
986 n
=read(fd
,&sub_chunk_id
,4);
989 if (memcmp(&sub_chunk_id
,"mvhd",4)==0) {
990 /* We don't need anything from here - skip */
991 lseek(fd
, sub_chunk_len
- 8, SEEK_CUR
); /* FIXME not 8 */
992 size_remaining
-=(sub_chunk_len
-8);
993 } else if (memcmp(&sub_chunk_id
,"udta",4)==0) {
994 /* The udta chunk contains the metadata - track, artist, album etc.
995 The format appears to be:
1004 NOTE: This code was written by examination of some .m4a files
1005 produced by iTunes v4.9 - it may not therefore be 100%
1006 compliant with all streams. But it should fail gracefully.
1008 j
=(sub_chunk_len
-8);
1010 n
=read_uint32be(fd
,&sub_chunk_len
);
1011 n
=read(fd
,&sub_chunk_id
,4);
1013 if (memcmp(&sub_chunk_id
,"meta",4)==0) {
1014 lseek(fd
, 4, SEEK_CUR
);
1016 n
=read_uint32be(fd
,&sub_chunk_len
);
1017 n
=read(fd
,&sub_chunk_id
,4);
1019 if (memcmp(&sub_chunk_id
,"hdlr",4)==0) {
1020 lseek(fd
, sub_chunk_len
- 8, SEEK_CUR
);
1021 j
-=(sub_chunk_len
- 8);
1022 n
=read_uint32be(fd
,&sub_chunk_len
);
1023 n
=read(fd
,&sub_chunk_id
,4);
1025 if (memcmp(&sub_chunk_id
,"ilst",4)==0) {
1026 /* Here are the actual tags. We use the id3v2 300-byte buffer
1027 to store the string data */
1028 bytes_remaining
=sizeof(id3
->id3v2buf
);
1029 id3
->genre
=255; /* Not every track is the Blues */
1030 id3buf
=id3
->id3v2buf
;
1034 n
=read_uint32be(fd
,&sub_chunk_len
);
1035 n
=read(fd
,&sub_chunk_id
,4);
1037 if (memcmp(sub_chunk_id
,"\251nam",4)==0) {
1038 read_m4a_tag_string(fd
,sub_chunk_len
-8,&id3buf
,&bytes_remaining
,&id3
->title
);
1039 } else if (memcmp(sub_chunk_id
,"\251ART",4)==0) {
1040 read_m4a_tag_string(fd
,sub_chunk_len
-8,&id3buf
,&bytes_remaining
,&id3
->artist
);
1041 } else if (memcmp(sub_chunk_id
,"\251alb",4)==0) {
1042 read_m4a_tag_string(fd
,sub_chunk_len
-8,&id3buf
,&bytes_remaining
,&id3
->album
);
1043 } else if (memcmp(sub_chunk_id
,"\251gen",4)==0) {
1044 read_m4a_tag_string(fd
,sub_chunk_len
-8,&id3buf
,&bytes_remaining
,&id3
->genre_string
);
1045 } else if (memcmp(sub_chunk_id
,"\251day",4)==0) {
1046 read_m4a_tag_string(fd
,sub_chunk_len
-8,&id3buf
,&bytes_remaining
,&id3
->year_string
);
1047 } else if (memcmp(sub_chunk_id
,"trkn",4)==0) {
1048 if (sub_chunk_len
==0x20) {
1049 read(fd
,buf
,sub_chunk_len
-8);
1050 id3
->tracknum
=buf
[19];
1052 lseek(fd
, sub_chunk_len
-8,SEEK_CUR
);
1055 lseek(fd
, sub_chunk_len
-8,SEEK_CUR
);
1057 k
-=(sub_chunk_len
-8);
1062 /* Skip any remaining data in udta chunk */
1063 lseek(fd
, j
, SEEK_CUR
);
1064 } else if (memcmp(&sub_chunk_id
,"trak",4)==0) {
1065 /* Format of trak chunk:
1074 stsd - Samplerate, Samplesize, Numchannels
1075 stts - time_to_sample array - RLE'd table containing duration of each block
1076 stsz - sample_byte_size array - ?Size in bytes of each compressed block
1077 stsc - Seek table related?
1078 stco - Seek table related?
1081 /* Skip tkhd - not needed */
1082 n
=read_uint32be(fd
,&sub_chunk_len
);
1083 n
=read(fd
,&sub_chunk_id
,4);
1084 if (memcmp(&sub_chunk_id
,"tkhd",4)!=0) {
1085 logf("Expecting tkhd\n");
1088 lseek(fd
, sub_chunk_len
- 8, SEEK_CUR
); /* FIXME not 8 */
1089 size_remaining
-=sub_chunk_len
;
1091 /* Process mdia - skipping possible edts */
1092 n
=read_uint32be(fd
,&sub_chunk_len
);
1093 n
=read(fd
,&sub_chunk_id
,4);
1094 if (memcmp(&sub_chunk_id
,"edts",4)==0) {
1095 lseek(fd
, sub_chunk_len
- 8, SEEK_CUR
); /* FIXME not 8 */
1096 size_remaining
-=sub_chunk_len
;
1097 n
=read_uint32be(fd
,&sub_chunk_len
);
1098 n
=read(fd
,&sub_chunk_id
,4);
1101 if (memcmp(&sub_chunk_id
,"mdia",4)!=0) {
1102 logf("Expecting mdia\n");
1105 size_remaining
-=sub_chunk_len
;
1109 n
=read_uint32be(fd
,&sub_chunk_len
);
1110 n
=read(fd
,&sub_chunk_id
,4);
1112 if (memcmp(&sub_chunk_id
,"minf",4)==0) {
1114 } else if (memcmp(&sub_chunk_id
,"stbl",4)==0) {
1116 } else if (memcmp(&sub_chunk_id
,"stsd",4)==0) {
1117 n
=read(fd
,buf
,sub_chunk_len
-8);
1120 /* Skip version and flags */
1123 numentries
=(buf
[i
]<<24)|(buf
[i
+1]<<16)|(buf
[i
+2]<<8)|buf
[i
+3];
1125 if (numentries
!=1) {
1126 logf("ERROR: Expecting only one entry in stsd\n");
1129 entry_size
=(buf
[i
]<<24)|(buf
[i
+1]<<16)|(buf
[i
+2]<<8)|buf
[i
+3];
1132 if (memcmp(&buf
[i
],"alac",4)==0) {
1133 id3
->codectype
=AFMT_ALAC
;
1134 } else if (memcmp(&buf
[i
],"mp4a",4)==0) {
1135 id3
->codectype
=AFMT_AAC
;
1137 logf("Not an ALAC or AAC file\n");
1141 //numchannels=(buf[i+20]<<8)|buf[i+21]; /* Not used - assume Stereo */
1142 //samplesize=(buf[i+22]<<8)|buf[i+23]; /* Not used - assume 16-bit */
1144 /* Samplerate is 32-bit fixed point, but this works for < 65536 Hz */
1145 id3
->frequency
=(buf
[i
+28]<<8)|buf
[i
+29];
1146 } else if (memcmp(&sub_chunk_id
,"stts",4)==0) {
1151 numentries
=(buf
[4]<<24)|(buf
[5]<<16)|(buf
[6]<<8)|buf
[7];
1152 for (k
=0;k
<numentries
;k
++) {
1153 n
=read_uint32be(fd
,&sample_count
);
1154 n
=read_uint32be(fd
,&sample_duration
);
1155 totalsamples
+=sample_count
*sample_duration
;
1158 if (i
> 0) lseek(fd
, sub_chunk_len
- i
, SEEK_CUR
);
1159 } else if (memcmp(&sub_chunk_id
,"stsz",4)==0) {
1164 numentries
=(buf
[4]<<24)|(buf
[5]<<16)|(buf
[6]<<8)|buf
[7];
1165 for (k
=0;k
<numentries
;k
++) {
1166 n
=read_uint32be(fd
,&sample_count
);
1167 n
=read_uint32be(fd
,&sample_duration
);
1168 totalsamples
+=sample_count
*sample_duration
;
1171 if (i
> 0) lseek(fd
, sub_chunk_len
- i
, SEEK_CUR
);
1173 lseek(fd
, sub_chunk_len
- 8, SEEK_CUR
); /* FIXME not 8 */
1178 logf("Unexpected sub_chunk_id inside moov: %c%c%c%c\n",
1179 sub_chunk_id
[0],sub_chunk_id
[1],sub_chunk_id
[2],sub_chunk_id
[3]);
1183 } else if (memcmp(&chunk_id
,"mdat",4)==0) {
1184 /* once we hit mdat we stop reading and return.
1185 * this is on the assumption that there is no furhter interesting
1186 * stuff in the stream. if there is stuff will fail (:()).
1187 * But we need the read pointer to be at the mdat stuff
1188 * for the decoder. And we don't want to rely on fseek/ftell,
1189 * as they may not always be avilable */
1190 lseek(fd
, chunk_len
- 8, SEEK_CUR
); /* FIXME not 8 */
1191 compressedsize
=chunk_len
-8;
1192 } else if (memcmp(&chunk_id
,"free",4)==0) {
1193 /* these following atoms can be skipped !!!! */
1194 lseek(fd
, chunk_len
- 8, SEEK_CUR
); /* FIXME not 8 */
1196 logf("(top) unknown chunk id: %c%c%c%c\n", chunk_id
[0],chunk_id
[1],chunk_id
[2],chunk_id
[3]);
1201 id3
->vbr
=true; /* All ALAC files are VBR */
1202 id3
->filesize
=filesize(fd
);
1203 id3
->samples
=totalsamples
;
1204 id3
->length
=(10*totalsamples
)/(id3
->frequency
/100);
1205 id3
->bitrate
=(compressedsize
*8)/id3
->length
;;
1210 static bool get_musepack_metadata(int fd
, struct mp3entry
*id3
)
1212 const int32_t sfreqs_sv7
[4] = { 44100, 48000, 37800, 32000 };
1214 uint64_t samples
= 0;
1217 if (!skip_id3v2(fd
, id3
))
1219 if (read(fd
, header
, 4*8) != 4*8) return false;
1220 /* Musepack files are little endian, might need swapping */
1221 for (i
= 1; i
< 8; i
++)
1222 header
[i
] = letoh32(header
[i
]);
1223 if (!memcmp(header
, "MP+", 3)) { /* Compare to sig "MP+" */
1224 unsigned int streamversion
;
1226 header
[0] = letoh32(header
[0]);
1227 streamversion
= (header
[0] >> 24) & 15;
1228 if (streamversion
>= 8) {
1229 return false; /* SV8 or higher don't exist yet, so no support */
1230 } else if (streamversion
== 7) {
1231 unsigned int gapless
= (header
[5] >> 31) & 0x0001;
1232 unsigned int last_frame_samples
= (header
[5] >> 20) & 0x07ff;
1233 int track_gain
, album_gain
;
1234 unsigned int bufused
;
1236 id3
->frequency
= sfreqs_sv7
[(header
[2] >> 16) & 0x0003];
1237 samples
= (uint64_t)header
[1]*1152; /* 1152 is mpc frame size */
1239 samples
-= 1152 - last_frame_samples
;
1241 samples
-= 481; /* Musepack subband synth filter delay */
1243 /* Extract ReplayGain data from header */
1244 track_gain
= (int16_t)((header
[3] >> 16) & 0xffff);
1245 id3
->track_gain
= get_replaygain_int(track_gain
);
1246 id3
->track_peak
= ((uint16_t)(header
[3] & 0xffff)) << 9;
1248 album_gain
= (int16_t)((header
[4] >> 16) & 0xffff);
1249 id3
->album_gain
= get_replaygain_int(album_gain
);
1250 id3
->album_peak
= ((uint16_t)(header
[4] & 0xffff)) << 9;
1252 /* Write replaygain values to strings for use in id3 screen. We use
1253 the XING header as buffer space since Musepack files shouldn't
1254 need to use it in any other way */
1255 id3
->track_gain_string
= id3
->toc
;
1256 bufused
= snprintf(id3
->track_gain_string
, 45,
1257 "%d.%d dB", track_gain
/100, abs(track_gain
)%100);
1258 id3
->album_gain_string
= id3
->toc
+ bufused
+ 1;
1259 bufused
= snprintf(id3
->album_gain_string
, 45,
1260 "%d.%d dB", album_gain
/100, abs(album_gain
)%100);
1263 header
[0] = letoh32(header
[0]);
1264 unsigned int streamversion
= (header
[0] >> 11) & 0x03FF;
1265 if (streamversion
!= 4 && streamversion
!= 5 && streamversion
!= 6)
1267 id3
->frequency
= 44100;
1268 id3
->track_gain
= 0;
1269 id3
->track_peak
= 0;
1270 id3
->album_gain
= 0;
1271 id3
->album_peak
= 0;
1273 if (streamversion
>= 5)
1274 samples
= (uint64_t)header
[1]*1152; // 32 bit
1276 samples
= (uint64_t)(header
[1] >> 16)*1152; // 16 bit
1279 if (streamversion
< 6)
1284 /* Estimate bitrate, we should probably subtract the various header sizes
1285 here for super-accurate results */
1286 id3
->length
= samples
/id3
->frequency
*1000;
1287 id3
->filesize
= filesize(fd
);
1288 id3
->bitrate
= id3
->filesize
*8/id3
->length
;
1292 static bool get_sid_metadata(int fd
, struct mp3entry
* id3
)
1294 /* Use the trackname part of the id3 structure as a temporary buffer */
1295 unsigned char* buf
= id3
->path
;
1300 if ((lseek(fd
, 0, SEEK_SET
) < 0)
1301 || ((read_bytes
= read(fd
, buf
, sizeof(id3
->path
))) < 44))
1306 if ((memcmp(buf
, "PSID",4) != 0))
1314 strcpy(p
, &buf
[0x16]);
1319 strcpy(p
, &buf
[0x36]);
1324 id3
->frequency
= 44100;
1325 /* New idea as posted by Marco Alanen (ravon):
1326 * Set the songlength in seconds to the number of subsongs
1327 * so every second represents a subsong.
1328 * Users can then skip the current subsong by seeking */
1329 id3
->length
= (buf
[0xf]-1)*1000;
1331 id3
->filesize
= filesize(fd
);
1336 #endif /* CONFIG_CODEC == SWCODEC */
1338 static bool get_aiff_metadata(int fd
, struct mp3entry
* id3
)
1340 /* Use the trackname part of the id3 structure as a temporary buffer */
1341 unsigned char* buf
= id3
->path
;
1342 unsigned long numChannels
= 0;
1343 unsigned long numSampleFrames
= 0;
1344 unsigned long sampleSize
= 0;
1345 unsigned long sampleRate
= 0;
1346 unsigned long numbytes
= 0;
1350 if ((lseek(fd
, 0, SEEK_SET
) < 0)
1351 || ((read_bytes
= read(fd
, buf
, sizeof(id3
->path
))) < 44))
1356 if ((memcmp(buf
, "FORM",4) != 0)
1357 || (memcmp(&buf
[8], "AIFF", 4) !=0 ))
1365 while ((numbytes
== 0) && (read_bytes
>= 8))
1368 i
= ((buf
[4]<<24)|(buf
[5]<<16)|(buf
[6]<<8)|buf
[7]);
1370 if (memcmp(buf
, "COMM", 4) == 0)
1373 numChannels
= ((buf
[8]<<8)|buf
[9]);
1374 /* numSampleFrames */
1375 numSampleFrames
=((buf
[10]<<24)|(buf
[11]<<16)|(buf
[12]<<8)|buf
[13]);
1377 sampleSize
= ((buf
[14]<<8)|buf
[15]);
1379 sampleRate
= ((buf
[18]<<24)|(buf
[19]<<16)|(buf
[20]<<8)|buf
[21]);
1380 sampleRate
= sampleRate
>> (16+14-buf
[17]);
1381 /* save format infos */
1382 id3
->bitrate
= (sampleSize
* numChannels
* sampleRate
) / 1000;
1383 id3
->frequency
= sampleRate
;
1384 id3
->length
= (numSampleFrames
/ id3
->frequency
) * 1000;
1385 id3
->vbr
= false; /* AIFF files are CBR */
1386 id3
->filesize
= filesize(fd
);
1388 else if (memcmp(buf
, "SSND", 4) == 0)
1395 i
++; /* odd chunk sizes must be padded */
1398 read_bytes
-= i
+ 8;
1401 if ((numbytes
== 0) || (numChannels
== 0))
1408 /* Simple file type probing by looking at the filename extension. */
1409 unsigned int probe_file_format(const char *filename
)
1414 suffix
= strrchr(filename
, '.');
1418 return AFMT_UNKNOWN
;
1423 for (i
= 0; i
< sizeof(formats
) / sizeof(formats
[0]); i
++)
1425 if (strcasecmp(suffix
, formats
[i
].extension
) == 0)
1427 return formats
[i
].format
;
1431 return AFMT_UNKNOWN
;
1434 /* Get metadata for track - return false if parsing showed problems with the
1435 * file that would prevent playback.
1437 bool get_metadata(struct track_info
* track
, int fd
, const char* trackname
,
1440 #if CONFIG_CODEC == SWCODEC
1442 unsigned long totalsamples
;
1446 /* Take our best guess at the codec type based on file extension */
1447 track
->id3
.codectype
= probe_file_format(trackname
);
1449 /* Load codec specific track tag information and confirm the codec type. */
1450 switch (track
->id3
.codectype
)
1455 if (mp3info(&track
->id3
, trackname
, v1first
))
1462 #if CONFIG_CODEC == SWCODEC
1464 if (!get_flac_metadata(fd
, &(track
->id3
)))
1472 if (!get_musepack_metadata(fd
, &(track
->id3
)))
1474 read_ape_tags(fd
, &(track
->id3
));
1477 case AFMT_OGG_VORBIS
:
1478 if (!get_vorbis_metadata(fd
, &(track
->id3
)))
1486 if (!get_wave_metadata(fd
, &(track
->id3
)))
1494 /* A simple parser to read basic information from a WavPack file. This
1495 * now works with self-extrating WavPack files and also will fail on
1496 * WavPack files containing floating-point audio data (although these
1497 * should be possible to play in theory).
1500 /* Use the trackname part of the id3 structure as a temporary buffer */
1501 buf
= track
->id3
.path
;
1503 for (i
= 0; i
< 256; ++i
) {
1505 /* at every 256 bytes into file, try to read a WavPack header */
1507 if ((lseek(fd
, i
* 256, SEEK_SET
) < 0) || (read(fd
, buf
, 32) < 32))
1512 /* if valid WavPack 4 header version & not floating data, break */
1514 if (memcmp (buf
, "wvpk", 4) == 0 && buf
[9] == 4 &&
1515 (buf
[8] >= 2 && buf
[8] <= 0x10) && !(buf
[24] & 0x80))
1522 logf ("%s is not a WavPack file\n", trackname
);
1526 track
->id3
.vbr
= true; /* All WavPack files are VBR */
1527 track
->id3
.filesize
= filesize (fd
);
1529 if ((buf
[20] | buf
[21] | buf
[22] | buf
[23]) &&
1530 (buf
[12] & buf
[13] & buf
[14] & buf
[15]) != 0xff)
1532 int srindx
= ((buf
[26] >> 7) & 1) + ((buf
[27] << 1) & 14);
1536 track
->id3
.frequency
= 44100;
1540 track
->id3
.frequency
= wavpack_sample_rates
[srindx
];
1543 totalsamples
= get_long(&buf
[12]);
1544 track
->id3
.length
= totalsamples
/ (track
->id3
.frequency
/ 100) * 10;
1545 track
->id3
.bitrate
= filesize (fd
) / (track
->id3
.length
/ 8);
1548 read_ape_tags(fd
, &track
->id3
); /* use any apetag info we find */
1552 /* Use the trackname part of the id3 structure as a temporary buffer */
1553 buf
= track
->id3
.path
;
1555 if ((lseek(fd
, 0, SEEK_SET
) < 0) || (read(fd
, buf
, 5) < 5))
1560 if ((buf
[0] != 0x0b) || (buf
[1] != 0x77))
1562 logf("%s is not an A52/AC3 file\n",trackname
);
1570 logf("A52: Invalid frmsizecod: %d\n",i
);
1574 track
->id3
.bitrate
= a52_bitrates
[i
>> 1];
1575 track
->id3
.vbr
= false;
1576 track
->id3
.filesize
= filesize(fd
);
1578 switch (buf
[4] & 0xc0)
1581 track
->id3
.frequency
= 48000;
1582 track
->id3
.bytesperframe
=track
->id3
.bitrate
* 2 * 2;
1586 track
->id3
.frequency
= 44100;
1587 track
->id3
.bytesperframe
= a52_441framesizes
[i
];
1591 track
->id3
.frequency
= 32000;
1592 track
->id3
.bytesperframe
= track
->id3
.bitrate
* 3 * 2;
1596 logf("A52: Invalid samplerate code: 0x%02x\n", buf
[4] & 0xc0);
1601 /* One A52 frame contains 6 blocks, each containing 256 samples */
1602 totalsamples
= track
->id3
.filesize
/ track
->id3
.bytesperframe
* 6 * 256;
1603 track
->id3
.length
= totalsamples
/ track
->id3
.frequency
* 1000;
1608 if (!get_m4a_metadata(fd
, &(track
->id3
)))
1616 track
->id3
.vbr
= true;
1617 track
->id3
.filesize
= filesize(fd
);
1618 if (!skip_id3v2(fd
, &(track
->id3
)))
1622 /* TODO: read the id3v2 header if it exists */
1626 if (!get_sid_metadata(fd
, &(track
->id3
)))
1632 #endif /* CONFIG_CODEC == SWCODEC */
1635 if (!get_aiff_metadata(fd
, &(track
->id3
)))
1643 /* If we don't know how to read the metadata, assume we can't play
1649 /* We have successfully read the metadata from the file */
1651 lseek(fd
, 0, SEEK_SET
);
1652 strncpy(track
->id3
.path
, trackname
, sizeof(track
->id3
.path
));
1653 track
->taginfo_ready
= true;