1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Daniel Stenberg
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 ****************************************************************************/
20 * Parts of this code has been stolen from the Ample project and was written
21 * by David Härdeman. It has since been extended and enhanced pretty much by
22 * all sorts of friendly Rockbox people.
26 /* tagResolver and associated code copyright 2003 Thomas Paul Diffenbach
44 #define UNSYNC(b0,b1,b2,b3) (((b0 & 0x7F) << (3*7)) | \
45 ((b1 & 0x7F) << (2*7)) | \
46 ((b2 & 0x7F) << (1*7)) | \
47 ((b3 & 0x7F) << (0*7)))
49 #define BYTES2INT(b0,b1,b2,b3) (((b0 & 0xFF) << (3*8)) | \
50 ((b1 & 0xFF) << (2*8)) | \
51 ((b2 & 0xFF) << (1*8)) | \
52 ((b3 & 0xFF) << (0*8)))
54 static const char* const genres
[] = {
55 "Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge",
56 "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B",
57 "Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative", "Ska",
58 "Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop",
59 "Vocal", "Jazz+Funk", "Fusion", "Trance", "Classical", "Instrumental",
60 "Acid", "House", "Game", "Sound Clip", "Gospel", "Noise", "AlternRock",
61 "Bass", "Soul", "Punk", "Space", "Meditative", "Instrumental Pop",
62 "Instrumental Rock", "Ethnic", "Gothic", "Darkwave", "Techno-Industrial",
63 "Electronic", "Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy",
64 "Cult", "Gangsta", "Top 40", "Christian Rap", "Pop/Funk", "Jungle",
65 "Native American", "Cabaret", "New Wave", "Psychadelic", "Rave",
66 "Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz",
67 "Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock",
69 /* winamp extensions */
70 "Folk", "Folk-Rock", "National Folk", "Swing", "Fast Fusion", "Bebob",
71 "Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock",
72 "Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock",
73 "Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour", "Speech",
74 "Chanson", "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass",
75 "Primus", "Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba",
76 "Folklore", "Ballad", "Power Ballad", "Rhythmic Soul", "Freestyle",
77 "Duet", "Punk Rock", "Drum Solo", "A capella", "Euro-House", "Dance Hall"
80 char* id3_get_genre(const struct mp3entry
* id3
)
82 if( id3
->genre_string
)
83 return id3
->genre_string
;
85 if (id3
->genre
< sizeof(genres
)/sizeof(char*))
86 return (char*)genres
[id3
->genre
];
91 HOW TO ADD ADDITIONAL ID3 VERSION 2 TAGS
92 Code and comments by Thomas Paul Diffenbach
94 To add another ID3v2 Tag, do the following:
95 1. add a char* named for the tag to struct mp3entry in id3.h,
96 (I (tpd) prefer to use char* rather than ints, even for what seems like
97 numerical values, for cases where a number won't do, e.g.,
98 YEAR: "circa 1765", "1790/1977" (composed/performed), "28 Feb 1969"
99 TRACK: "1/12", "1 of 12", GENRE: "Freeform genre name"
100 Text is more flexible, and as the main use of id3 data is to
101 display it, converting it to an int just means reconverting to
102 display it, at a runtime cost.)
104 2. If any special processing beyond copying the tag value from the Id3
105 block to the struct mp3entry is rrequired (such as converting to an
106 int), write a function to perform this special processing.
108 This function's prototype must match that of
109 typedef tagPostProcessFunc, that is it must be:
110 int func( struct mp3entry*, char* tag, int bufferpos )
111 the first argument is a pointer to the current mp3entry structure the
112 second argument is a pointer to the null terminated string value of the
113 tag found the third argument is the offset of the next free byte in the
114 mp3entry's buffer your function should return the corrected offset; if
115 you don't lengthen or shorten the tag string, you can return the third
118 Unless you have a good reason no to, make the function static.
119 TO JUST COPY THE TAG NO SPECIAL PROCESSING FUNCTION IS NEEDED.
121 3. add one or more entries to the tagList array, using the format:
122 char* ID3 Tag symbolic name -- see the ID3 specification for these,
123 sizeof() that name minus 1,
124 offsetof( struct mp3entry, variable_name_in_struct_mp3entry ),
125 pointer to your special processing function or NULL
126 if you need no special processing
127 Many ID3 symbolic names come in more than one form. You can add both
128 forms, each referencing the same variable in struct mp3entry.
129 If both forms are present, the last found will be used.
131 4. Alternately, use the TAG_LIST_ENTRY macro with
132 ID3 tag symbolic name,
133 variable in struct mp3entry,
134 special processing function address
136 5. Add code to wps-display.c function get_tag to assign a printf-like
137 format specifier for the tag */
139 /* Structure for ID3 Tag extraction information */
140 struct tag_resolver
{
144 int (*ppFunc
)(struct mp3entry
*, char* tag
, int bufferpos
);
147 static bool global_ff_found
;
149 static int unsynchronize(char* tag
, int len
, bool *ff_found
)
153 unsigned char *rp
, *wp
;
157 rp
= (unsigned char *)tag
;
158 for(i
= 0;i
< len
;i
++) {
159 /* Read the next byte and write it back, but don't increment the
164 /* Increment the write pointer if it isn't an unsynch pattern */
174 return (int)wp
- (int)tag
;
177 static int unsynchronize_frame(char* tag
, int len
)
179 bool ff_found
= false;
181 return unsynchronize(tag
, len
, &ff_found
);
184 static int read_unsynched(int fd
, void *buf
, int len
)
196 rc
= read(fd
, rp
, remaining
);
200 i
= unsynchronize(wp
, remaining
, &global_ff_found
);
208 static int skip_unsynched(int fd
, int len
)
216 rlen
= MIN(sizeof(buf
), (unsigned int)remaining
);
217 rc
= read(fd
, buf
, rlen
);
221 remaining
-= unsynchronize(buf
, rlen
, &global_ff_found
);
227 /* parse numeric value from string */
228 static int parsetracknum( struct mp3entry
* entry
, char* tag
, int bufferpos
)
230 entry
->tracknum
= atoi( tag
);
234 /* parse numeric value from string */
235 static int parseyearnum( struct mp3entry
* entry
, char* tag
, int bufferpos
)
237 entry
->year
= atoi( tag
);
241 /* parse numeric genre from string, version 2.2 and 2.3 */
242 static int parsegenre( struct mp3entry
* entry
, char* tag
, int bufferpos
)
244 if(entry
->id3version
>= ID3_VER_2_4
) {
245 /* In version 2.4 and up, there are no parentheses, and the genre frame
246 is a list of strings, either numbers or text. */
248 /* Is it a number? */
249 if(isdigit(tag
[0])) {
250 entry
->genre
= atoi( tag
);
251 entry
->genre_string
= 0;
252 return tag
- entry
->id3v2buf
;
254 entry
->genre_string
= tag
;
259 if( tag
[0] == '(' && tag
[1] != '(' ) {
260 entry
->genre
= atoi( tag
+ 1 );
261 entry
->genre_string
= 0;
262 return tag
- entry
->id3v2buf
;
265 entry
->genre_string
= tag
;
272 static const struct tag_resolver taglist
[] = {
273 { "TPE1", 4, offsetof(struct mp3entry
, artist
), NULL
},
274 { "TP1", 3, offsetof(struct mp3entry
, artist
), NULL
},
275 { "TIT2", 4, offsetof(struct mp3entry
, title
), NULL
},
276 { "TT2", 3, offsetof(struct mp3entry
, title
), NULL
},
277 { "TALB", 4, offsetof(struct mp3entry
, album
), NULL
},
278 { "TAL", 3, offsetof(struct mp3entry
, album
), NULL
},
279 { "TRK", 3, offsetof(struct mp3entry
, track_string
), &parsetracknum
},
280 { "TRCK", 4, offsetof(struct mp3entry
, track_string
), &parsetracknum
},
281 { "TYER", 4, offsetof(struct mp3entry
, year_string
), &parseyearnum
},
282 { "TYE", 3, offsetof(struct mp3entry
, year_string
), &parseyearnum
},
283 { "TCOM", 4, offsetof(struct mp3entry
, composer
), NULL
},
284 { "TCON", 4, offsetof(struct mp3entry
, genre_string
), &parsegenre
},
285 { "TCO", 3, offsetof(struct mp3entry
, genre_string
), &parsegenre
},
288 #define TAGLIST_SIZE ((int)(sizeof(taglist) / sizeof(taglist[0])))
290 /* Checks to see if the passed in string is a 16-bit wide Unicode v2
291 string. If it is, we attempt to convert it to a 8-bit ASCII string
292 (for valid 8-bit ASCII characters). If it's not unicode, we leave
293 it alone. At some point we should fully support unicode strings */
294 static int unicode_munge(char** string
, int *len
) {
299 char *outstr
= *string
;
304 /* Plain old string */
308 /* Type 0x00 is ordinary ISO 8859-1 */
311 (*string
)++; /* Skip the encoding type byte */
315 /* Unicode with or without BOM */
316 if(str
[0] == 0x01 || str
[0] == 0x02) {
319 tmp
= BYTES2INT(0, 0, str
[0], str
[1]);
321 /* Now check if there is a BOM (zero-width non-breaking space, 0xfeff)
322 and if it is in little or big endian format */
323 if(tmp
== 0xfffe) { /* Little endian? */
330 if(tmp
== 0xfeff) { /* Big endian? */
336 /* If there is no BOM (which is a specification violation),
337 let's try to guess it. If one of the bytes is 0x00, it is
338 probably the most significant one. */
353 outstr
[i
++] = str
[0];
358 outstr
[i
++] = str
[1];
361 } while((str
[0] || str
[1]) && (i
< outlen
));
365 outstr
[i
] = 0; /* Terminate the string */
369 /* If we come here, the string was of an unsupported type */
376 * Sets the title of an MP3 entry based on its ID3v1 tag.
378 * Arguments: file - the MP3 file to scen for a ID3v1 tag
379 * entry - the entry to set the title in
381 * Returns: true if a title was found and created, else false
383 static bool setid3v1title(int fd
, struct mp3entry
*entry
)
385 unsigned char buffer
[128];
386 static const char offsets
[] = {3, 33, 63, 93, 125, 127};
389 if (-1 == lseek(fd
, -128, SEEK_END
))
392 if (read(fd
, buffer
, sizeof buffer
) != sizeof buffer
)
395 if (strncmp(buffer
, "TAG", 3))
398 entry
->id3v1len
= 128;
399 entry
->id3version
= ID3_VER_1_0
;
401 for (i
=0; i
< (int)sizeof offsets
; i
++) {
402 char* ptr
= buffer
+ offsets
[i
];
405 /* kill trailing space in strings */
406 for (j
=29; j
&& ptr
[j
]==' '; j
--)
412 strncpy(entry
->id3v1buf
[2], ptr
, 30);
413 entry
->title
= entry
->id3v1buf
[2];
417 strncpy(entry
->id3v1buf
[0], ptr
, 30);
418 entry
->artist
= entry
->id3v1buf
[0];
422 strncpy(entry
->id3v1buf
[1], ptr
, 30);
423 entry
->album
= entry
->id3v1buf
[1];
428 entry
->year
= atoi(ptr
);
432 /* id3v1.1 uses last two bytes of comment field for track
433 number: first must be 0 and second is track num */
434 if (!ptr
[0] && ptr
[1]) {
435 entry
->tracknum
= ptr
[1];
436 entry
->id3version
= ID3_VER_1_1
;
442 entry
->genre
= ptr
[0];
452 * Sets the title of an MP3 entry based on its ID3v2 tag.
454 * Arguments: file - the MP3 file to scan for a ID3v2 tag
455 * entry - the entry to set the title in
457 * Returns: true if a title was found and created, else false
459 static void setid3v2title(int fd
, struct mp3entry
*entry
)
463 int bufferpos
= 0, totframelen
, framelen
;
466 unsigned char version
;
467 char *buffer
= entry
->id3v2buf
;
469 int buffersize
= sizeof(entry
->id3v2buf
);
470 unsigned char global_flags
;
473 bool global_unsynch
= false;
474 bool unsynch
= false;
479 global_ff_found
= false;
481 /* Bail out if the tag is shorter than 10 bytes */
482 if(entry
->id3v2len
< 10)
485 /* Read the ID3 tag version from the header */
486 lseek(fd
, 0, SEEK_SET
);
487 if(10 != read(fd
, header
, 10))
490 /* Get the total ID3 tag size */
491 size
= entry
->id3v2len
- 10;
496 version
= ID3_VER_2_2
;
501 version
= ID3_VER_2_3
;
506 version
= ID3_VER_2_4
;
511 /* unsupported id3 version */
514 entry
->id3version
= version
;
515 entry
->tracknum
= entry
->year
= 0;
517 entry
->title
= entry
->artist
= entry
->album
= NULL
;
519 global_flags
= header
[5];
521 /* Skip the extended header if it is present */
522 if(version
>= ID3_VER_2_4
) {
523 if(global_flags
& 0x40) {
524 if(4 != read(fd
, header
, 4))
527 framelen
= UNSYNC(header
[0], header
[1],
528 header
[2], header
[3]);
530 lseek(fd
, framelen
- 4, SEEK_CUR
);
534 /* Is unsynchronization applied? */
535 if(global_flags
& 0x80) {
536 global_unsynch
= true;
540 * We must have at least minframesize bytes left for the
541 * remaining frames to be interesting
543 while(size
>= minframesize
) {
546 /* Read frame header and check length */
547 if(version
>= ID3_VER_2_3
) {
548 if(global_unsynch
&& version
<= ID3_VER_2_3
)
549 rc
= read_unsynched(fd
, header
, 10);
551 rc
= read(fd
, header
, 10);
554 /* Adjust for the 10 bytes we read */
557 flags
= BYTES2INT(0, 0, header
[8], header
[9]);
559 if (version
>= ID3_VER_2_4
) {
560 framelen
= UNSYNC(header
[4], header
[5],
561 header
[6], header
[7]);
563 /* version .3 files don't use synchsafe ints for
565 framelen
= BYTES2INT(header
[4], header
[5],
566 header
[6], header
[7]);
569 if(6 != read(fd
, header
, 6))
571 /* Adjust for the 6 bytes we read */
574 framelen
= BYTES2INT(0, header
[3], header
[4], header
[5]);
577 /* Keep track of the total size */
578 totframelen
= framelen
;
590 if (version
>= ID3_VER_2_4
) {
591 if(flags
& 0x0040) { /* Grouping identity */
592 lseek(fd
, 1, SEEK_CUR
); /* Skip 1 byte */
596 if(flags
& 0x0020) { /* Grouping identity */
597 lseek(fd
, 1, SEEK_CUR
); /* Skip 1 byte */
602 if(flags
& 0x000c) /* Compression or encryption */
604 /* Skip it using the total size in case
607 lseek(fd
, totframelen
, SEEK_CUR
);
611 if(flags
& 0x0002) /* Unsynchronization */
614 if (version
>= ID3_VER_2_4
) {
615 if(flags
& 0x0001) { /* Data length indicator */
616 if(4 != read(fd
, tmp
, 4))
619 data_length_ind
= UNSYNC(tmp
[0], tmp
[1], tmp
[2], tmp
[3]);
625 /* If the frame is larger than the remaining buffer space we try
626 to read as much as would fit in the buffer */
627 if(framelen
>= buffersize
- bufferpos
)
628 framelen
= buffersize
- bufferpos
- 1;
630 DEBUGF("id3v2 frame: %.4s\n", header
);
632 /* Check for certain frame headers
634 'size' is the amount of frame bytes remaining. We decrement it by
635 the amount of bytes we read. If we fail to read as many bytes as
636 we expect, we assume that we can't read from this file, and bail
639 For each frame. we will iterate over the list of supported tags,
640 and read the tag into entry's buffer. All tags will be kept as
641 strings, for cases where a number won't do, e.g., YEAR: "circa
642 1765", "1790/1977" (composed/performed), "28 Feb 1969" TRACK:
643 "1/12", "1 of 12", GENRE: "Freeform genre name" Text is more
644 flexible, and as the main use of id3 data is to display it,
645 converting it to an int just means reconverting to display it, at a
648 For tags that the current code does convert to ints, a post
649 processing function will be called via a pointer to function. */
651 for (i
=0; i
<TAGLIST_SIZE
; i
++) {
652 const struct tag_resolver
* tr
= &taglist
[i
];
653 char** ptag
= (char**) (((char*)entry
) + tr
->offset
);
656 if( !*ptag
&& !memcmp( header
, tr
->tag
, tr
->tag_length
) ) {
658 /* found a tag matching one in tagList, and not yet filled */
659 if(global_unsynch
&& version
<= ID3_VER_2_3
)
660 bytesread
= read_unsynched(fd
, buffer
+ bufferpos
,
663 bytesread
= read(fd
, buffer
+ bufferpos
, framelen
);
665 if( bytesread
!= framelen
)
669 *ptag
= buffer
+ bufferpos
;
671 if(unsynch
|| (global_unsynch
&& version
>= ID3_VER_2_4
))
672 bytesread
= unsynchronize_frame(*ptag
, bytesread
);
674 unicode_munge( ptag
, &bytesread
);
676 tag
[bytesread
+ 1] = 0;
677 bufferpos
+= bytesread
+ 2;
679 bufferpos
= tr
->ppFunc(entry
, tag
, bufferpos
);
684 if( i
== TAGLIST_SIZE
) {
685 /* no tag in tagList was found, or it was a repeat.
686 skip it using the total size */
688 if(global_unsynch
&& version
<= ID3_VER_2_3
) {
689 size
-= skip_unsynched(fd
, totframelen
);
692 totframelen
= data_length_ind
;
695 if( lseek(fd
, totframelen
, SEEK_CUR
) == -1 )
703 * Calculates the size of the ID3v2 tag.
705 * Arguments: file - the file to search for a tag.
707 * Returns: the size of the tag or 0 if none was found
709 static int getid3v2len(int fd
)
714 /* Make sure file has a ID3 tag */
715 if((-1 == lseek(fd
, 0, SEEK_SET
)) ||
716 (read(fd
, buf
, 6) != 6) ||
717 (strncmp(buf
, "ID3", strlen("ID3")) != 0))
720 /* Now check what the ID3v2 size field says */
722 if(read(fd
, buf
, 4) != 4)
725 offset
= UNSYNC(buf
[0], buf
[1], buf
[2], buf
[3]) + 10;
727 DEBUGF("ID3V2 Length: 0x%x\n", offset
);
732 * Calculates the length (in milliseconds) of an MP3 file.
734 * Modified to only use integers.
736 * Arguments: file - the file to calculate the length upon
737 * entry - the entry to update with the length
739 * Returns: the song length in milliseconds,
740 * 0 means that it couldn't be calculated
742 static int getsonglength(int fd
, struct mp3entry
*entry
)
744 unsigned int filetime
= 0;
748 /* Start searching after ID3v2 header */
749 if(-1 == lseek(fd
, entry
->id3v2len
, SEEK_SET
))
752 bytecount
= get_mp3file_info(fd
, &info
);
754 DEBUGF("Space between ID3V2 tag and first audio frame: 0x%x bytes\n",
760 bytecount
+= entry
->id3v2len
;
762 entry
->bitrate
= info
.bitrate
;
763 entry
->frequency
= info
.frequency
;
764 entry
->version
= info
.version
;
765 entry
->layer
= info
.layer
;
767 /* If the file time hasn't been established, this may be a fixed
768 rate MP3, so just use the default formula */
770 filetime
= info
.file_time
;
776 * ((filesize)/(bytes per frame))*(time per frame)
778 filetime
= entry
->filesize
/info
.frame_size
*info
.frame_time
;
781 entry
->tpf
= info
.frame_time
;
782 entry
->bpf
= info
.frame_size
;
784 entry
->vbr
= info
.is_vbr
;
785 entry
->has_toc
= info
.has_toc
;
786 memcpy(entry
->toc
, info
.toc
, sizeof(info
.toc
));
788 entry
->vbr_header_pos
= info
.vbr_header_pos
;
790 /* Update the seek point for the first playable frame */
791 entry
->first_frame_offset
= bytecount
;
792 DEBUGF("First frame is at %x\n", entry
->first_frame_offset
);
798 * Checks all relevant information (such as ID3v1 tag, ID3v2 tag, length etc)
799 * about an MP3 file and updates it's entry accordingly.
802 bool mp3info(struct mp3entry
*entry
, const char *filename
, bool v1first
)
807 fd
= open(filename
, O_RDONLY
);
811 memset(entry
, 0, sizeof(struct mp3entry
));
813 strncpy(entry
->path
, filename
, sizeof(entry
->path
));
816 entry
->filesize
= filesize(fd
);
817 entry
->id3v2len
= getid3v2len(fd
);
822 v1found
= setid3v1title(fd
, entry
);
824 if (!v1found
&& entry
->id3v2len
)
825 setid3v2title(fd
, entry
);
826 entry
->length
= getsonglength(fd
, entry
);
828 /* Subtract the meta information from the file size to get
829 the true size of the MP3 stream */
830 entry
->filesize
-= entry
->first_frame_offset
;
832 /* only seek to end of file if no id3v2 tags were found,
833 and we already haven't looked for a v1 tag */
834 if (!v1first
&& !entry
->id3v2len
) {
835 setid3v1title(fd
, entry
);
840 if(!entry
->length
|| (entry
->filesize
< 8 ))
841 /* no song length or less than 8 bytes is hereby considered to be an
842 invalid mp3 and won't be played by us! */
848 #ifdef DEBUG_STANDALONE
850 char *secs2str(int ms
)
852 static char buffer
[32];
855 snprintf(buffer
, sizeof(buffer
), "%d:%02d.%d", secs
/60, secs
%60, ms
/100);
859 int main(int argc
, char **argv
)
862 for(i
=1; i
<argc
; i
++) {
865 if(mp3info(&mp3
, argv
[i
], false)) {
866 printf("Failed to get %s\n", argv
[i
]);
870 printf("****** File: %s\n"
878 " Length: %s / %d s\n"
882 mp3
.title
?mp3
.title
:"<blank>",
883 mp3
.artist
?mp3
.artist
:"<blank>",
884 mp3
.album
?mp3
.album
:"<blank>",
885 mp3
.genre_string
?mp3
.genre_string
:"<blank>",
887 mp3
.composer
?mp3
.composer
:"<blank>",
888 mp3
.year_string
?mp3
.year_string
:"<blank>",
890 mp3
.track_string
?mp3
.track_string
:"<blank>",
892 secs2str(mp3
.length
),