Re-removed already removed lines
[kugel-rb.git] / firmware / id3.c
blob4297c8ba70a2c2c1f060a5e15aa5d6e9d0d74c6a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <stdbool.h>
34 #include <stddef.h>
35 #include <ctype.h>
36 #include "file.h"
37 #include "debug.h"
38 #include "atoi.h"
40 #include "id3.h"
41 #include "mp3data.h"
42 #include "system.h"
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];
87 return NULL;
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
116 argument unchanged.
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 {
141 const char* tag;
142 int tag_length;
143 size_t offset;
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)
151 int i;
152 unsigned char c;
153 unsigned char *rp, *wp;
155 wp = rp = tag;
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
160 write pointer */
161 c = *rp++;
162 *wp = c;
163 if(*ff_found) {
164 /* Increment the write pointer if it isn't an unsynch pattern */
165 if(c != 0)
166 wp++;
167 *ff_found = false;
168 } else {
169 if(c == 0xff)
170 *ff_found = true;
171 wp++;
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)
186 int i;
187 int rc;
188 int remaining = len;
189 char *wp;
190 char *rp;
192 wp = buf;
194 while(remaining) {
195 rp = wp;
196 rc = read(fd, rp, remaining);
197 if(rc < 0)
198 return rc;
200 i = unsynchronize(wp, remaining, &global_ff_found);
201 remaining -= i;
202 wp += i;
205 return len;
208 static int skip_unsynched(int fd, int len)
210 int rc;
211 int remaining = len;
212 int rlen;
213 char buf[32];
215 while(remaining) {
216 rlen = MIN(sizeof(buf), (unsigned int)remaining);
217 rc = read(fd, buf, rlen);
218 if(rc < 0)
219 return rc;
221 remaining -= unsynchronize(buf, rlen, &global_ff_found);
224 return len;
227 /* parse numeric value from string */
228 static int parsetracknum( struct mp3entry* entry, char* tag, int bufferpos )
230 entry->tracknum = atoi( tag );
231 return bufferpos;
234 /* parse numeric value from string */
235 static int parseyearnum( struct mp3entry* entry, char* tag, int bufferpos )
237 entry->year = atoi( tag );
238 return bufferpos;
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;
253 } else {
254 entry->genre_string = tag;
255 entry->genre = 0xff;
256 return bufferpos;
258 } else {
259 if( tag[0] == '(' && tag[1] != '(' ) {
260 entry->genre = atoi( tag + 1 );
261 entry->genre_string = 0;
262 return tag - entry->id3v2buf;
264 else {
265 entry->genre_string = tag;
266 entry->genre = 0xff;
267 return bufferpos;
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) {
295 int tmp;
296 bool le = false;
297 int i;
298 char *str = *string;
299 char *outstr = *string;
300 bool bom = false;
301 int outlen;
303 if(str[0] > 0x03) {
304 /* Plain old string */
305 return 0;
308 /* Type 0x00 is ordinary ISO 8859-1 */
309 if(str[0] == 0x00) {
310 (*len)--;
311 (*string)++; /* Skip the encoding type byte */
312 return 0;
315 /* Unicode with or without BOM */
316 if(str[0] == 0x01 || str[0] == 0x02) {
317 (*len)--;
318 str++;
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? */
324 bom = true;
325 le = true;
326 str += 2;
327 (*len)-=2;
330 if(tmp == 0xfeff) { /* Big endian? */
331 bom = true;
332 str += 2;
333 (*len)-=2;
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. */
339 if(!bom) {
340 if(str[1] == 0)
341 le = true;
344 i = 0;
346 outlen = *len / 2;
348 do {
349 if(le) {
350 if(str[1])
351 outstr[i++] = '.';
352 else
353 outstr[i++] = str[0];
354 } else {
355 if(str[0])
356 outstr[i++] = '.';
357 else
358 outstr[i++] = str[1];
360 str += 2;
361 } while((str[0] || str[1]) && (i < outlen));
363 *len = i;
365 outstr[i] = 0; /* Terminate the string */
366 return 0;
369 /* If we come here, the string was of an unsupported type */
370 *len = 1;
371 outstr[0] = 0;
372 return -1;
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};
387 int i, j;
389 if (-1 == lseek(fd, -128, SEEK_END))
390 return false;
392 if (read(fd, buffer, sizeof buffer) != sizeof buffer)
393 return false;
395 if (strncmp(buffer, "TAG", 3))
396 return false;
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];
404 if (i<3) {
405 /* kill trailing space in strings */
406 for (j=29; j && ptr[j]==' '; j--)
407 ptr[j] = 0;
410 switch(i) {
411 case 0:
412 strncpy(entry->id3v1buf[2], ptr, 30);
413 entry->title = entry->id3v1buf[2];
414 break;
416 case 1:
417 strncpy(entry->id3v1buf[0], ptr, 30);
418 entry->artist = entry->id3v1buf[0];
419 break;
421 case 2:
422 strncpy(entry->id3v1buf[1], ptr, 30);
423 entry->album = entry->id3v1buf[1];
424 break;
426 case 3:
427 ptr[4] = 0;
428 entry->year = atoi(ptr);
429 break;
431 case 4:
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;
438 break;
440 case 5:
441 /* genre */
442 entry->genre = ptr[0];
443 break;
447 return true;
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)
461 int minframesize;
462 int size;
463 int bufferpos = 0, totframelen, framelen;
464 char header[10];
465 char tmp[4];
466 unsigned char version;
467 char *buffer = entry->id3v2buf;
468 int bytesread = 0;
469 int buffersize = sizeof(entry->id3v2buf);
470 unsigned char global_flags;
471 int flags;
472 int skip;
473 bool global_unsynch = false;
474 bool unsynch = false;
475 int data_length_ind;
476 int i;
477 int rc;
479 global_ff_found = false;
481 /* Bail out if the tag is shorter than 10 bytes */
482 if(entry->id3v2len < 10)
483 return;
485 /* Read the ID3 tag version from the header */
486 lseek(fd, 0, SEEK_SET);
487 if(10 != read(fd, header, 10))
488 return;
490 /* Get the total ID3 tag size */
491 size = entry->id3v2len - 10;
493 version = header[3];
494 switch ( version ) {
495 case 2:
496 version = ID3_VER_2_2;
497 minframesize = 8;
498 break;
500 case 3:
501 version = ID3_VER_2_3;
502 minframesize = 12;
503 break;
505 case 4:
506 version = ID3_VER_2_4;
507 minframesize = 12;
508 break;
510 default:
511 /* unsupported id3 version */
512 return;
514 entry->id3version = version;
515 entry->tracknum = entry->year = 0;
516 entry->genre = 0xff;
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))
525 return;
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 ) {
544 flags = 0;
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);
550 else
551 rc = read(fd, header, 10);
552 if(rc != 10)
553 return;
554 /* Adjust for the 10 bytes we read */
555 size -= 10;
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]);
562 } else {
563 /* version .3 files don't use synchsafe ints for
564 * size */
565 framelen = BYTES2INT(header[4], header[5],
566 header[6], header[7]);
568 } else {
569 if(6 != read(fd, header, 6))
570 return;
571 /* Adjust for the 6 bytes we read */
572 size -= 6;
574 framelen = BYTES2INT(0, header[3], header[4], header[5]);
577 /* Keep track of the total size */
578 totframelen = framelen;
580 if(framelen == 0)
581 return;
583 unsynch = false;
584 data_length_ind = 0;
586 if(flags)
588 skip = 0;
590 if (version >= ID3_VER_2_4) {
591 if(flags & 0x0040) { /* Grouping identity */
592 lseek(fd, 1, SEEK_CUR); /* Skip 1 byte */
593 framelen--;
595 } else {
596 if(flags & 0x0020) { /* Grouping identity */
597 lseek(fd, 1, SEEK_CUR); /* Skip 1 byte */
598 framelen--;
602 if(flags & 0x000c) /* Compression or encryption */
604 /* Skip it using the total size in case
605 it was truncated */
606 size -= totframelen;
607 lseek(fd, totframelen, SEEK_CUR);
608 continue;
611 if(flags & 0x0002) /* Unsynchronization */
612 unsynch = true;
614 if (version >= ID3_VER_2_4) {
615 if(flags & 0x0001) { /* Data length indicator */
616 if(4 != read(fd, tmp, 4))
617 return;
619 data_length_ind = UNSYNC(tmp[0], tmp[1], tmp[2], tmp[3]);
620 framelen -= 4;
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
637 out.
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
646 runtime cost.
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);
654 char* tag;
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,
661 framelen);
662 else
663 bytesread = read(fd, buffer + bufferpos, framelen);
665 if( bytesread != framelen )
666 return;
668 size -= bytesread;
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 );
675 tag = *ptag;
676 tag[bytesread + 1] = 0;
677 bufferpos += bytesread + 2;
678 if( tr->ppFunc )
679 bufferpos = tr->ppFunc(entry, tag, bufferpos);
680 break;
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);
690 } else {
691 if(data_length_ind)
692 totframelen = data_length_ind;
694 size -= totframelen;
695 if( lseek(fd, totframelen, SEEK_CUR) == -1 )
696 return;
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)
711 char buf[6];
712 int offset;
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))
718 offset = 0;
720 /* Now check what the ID3v2 size field says */
721 else
722 if(read(fd, buf, 4) != 4)
723 offset = 0;
724 else
725 offset = UNSYNC(buf[0], buf[1], buf[2], buf[3]) + 10;
727 DEBUGF("ID3V2 Length: 0x%x\n", offset);
728 return 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;
745 struct mp3info info;
746 int bytecount;
748 /* Start searching after ID3v2 header */
749 if(-1 == lseek(fd, entry->id3v2len, SEEK_SET))
750 return 0;
752 bytecount = get_mp3file_info(fd, &info);
754 DEBUGF("Space between ID3V2 tag and first audio frame: 0x%x bytes\n",
755 bytecount);
757 if(bytecount < 0)
758 return -1;
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;
772 if(filetime == 0)
775 * Now song length is
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);
794 return filetime;
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)
804 int fd;
805 int v1found = false;
807 fd = open(filename, O_RDONLY);
808 if(-1 == fd)
809 return true;
811 memset(entry, 0, sizeof(struct mp3entry));
813 strncpy(entry->path, filename, sizeof(entry->path));
815 entry->title = NULL;
816 entry->filesize = filesize(fd);
817 entry->id3v2len = getid3v2len(fd);
818 entry->tracknum = 0;
819 entry->genre = 0xff;
821 if(v1first)
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);
838 close(fd);
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! */
843 return true;
845 return false;
848 #ifdef DEBUG_STANDALONE
850 char *secs2str(int ms)
852 static char buffer[32];
853 int secs = ms/1000;
854 ms %= 1000;
855 snprintf(buffer, sizeof(buffer), "%d:%02d.%d", secs/60, secs%60, ms/100);
856 return buffer;
859 int main(int argc, char **argv)
861 int i;
862 for(i=1; i<argc; i++) {
863 struct mp3entry mp3;
864 mp3.album = "Bogus";
865 if(mp3info(&mp3, argv[i], false)) {
866 printf("Failed to get %s\n", argv[i]);
867 return 0;
870 printf("****** File: %s\n"
871 " Title: %s\n"
872 " Artist: %s\n"
873 " Album: %s\n"
874 " Genre: %s (%d) \n"
875 " Composer: %s\n"
876 " Year: %s (%d)\n"
877 " Track: %s (%d)\n"
878 " Length: %s / %d s\n"
879 " Bitrate: %d\n"
880 " Frequency: %d\n",
881 argv[i],
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>",
886 mp3.genre,
887 mp3.composer?mp3.composer:"<blank>",
888 mp3.year_string?mp3.year_string:"<blank>",
889 mp3.year,
890 mp3.track_string?mp3.track_string:"<blank>",
891 mp3.tracknum,
892 secs2str(mp3.length),
893 mp3.length/1000,
894 mp3.bitrate,
895 mp3.frequency);
898 return 0;
901 #endif