No bufclosing of handles after track change
[Rockbox.git] / firmware / id3.c
blobc94c11fd55a1e7e15ff35d8b65c4964e91b5fd43
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�deman. 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 "config.h"
37 #include "file.h"
38 #include "debug.h"
39 #include "atoi.h"
41 #include "id3.h"
42 #include "mp3data.h"
43 #include "system.h"
44 #include "replaygain.h"
45 #include "rbunicode.h"
47 /** Database of audio formats **/
48 const struct afmt_entry audio_formats[AFMT_NUM_CODECS] =
50 /* Unknown file format */
51 [AFMT_UNKNOWN] =
52 AFMT_ENTRY("???", NULL, NULL, NULL ),
54 /* MPEG Audio layer 1 */
55 [AFMT_MPA_L1] =
56 AFMT_ENTRY("MP1", "mpa", NULL, "mp1\0" ),
57 /* MPEG Audio layer 2 */
58 [AFMT_MPA_L2] =
59 AFMT_ENTRY("MP2", "mpa", NULL, "mpa\0mp2\0" ),
60 /* MPEG Audio layer 3 */
61 [AFMT_MPA_L3] =
62 AFMT_ENTRY("MP3", "mpa", "mp3_enc", "mp3\0" ),
64 #if CONFIG_CODEC == SWCODEC
65 /* Audio Interchange File Format */
66 [AFMT_AIFF] =
67 AFMT_ENTRY("AIFF", "aiff", "aiff_enc", "aiff\0aif\0"),
68 /* Uncompressed PCM in a WAV file */
69 [AFMT_PCM_WAV] =
70 AFMT_ENTRY("WAV", "wav", "wav_enc", "wav\0" ),
71 /* Ogg Vorbis */
72 [AFMT_OGG_VORBIS] =
73 AFMT_ENTRY("Ogg", "vorbis", NULL, "ogg\0" ),
74 /* FLAC */
75 [AFMT_FLAC] =
76 AFMT_ENTRY("FLAC", "flac", NULL, "flac\0" ),
77 /* Musepack */
78 [AFMT_MPC] =
79 AFMT_ENTRY("MPC", "mpc", NULL, "mpc\0" ),
80 /* A/52 (aka AC3) audio */
81 [AFMT_A52] =
82 AFMT_ENTRY("AC3", "a52", NULL, "a52\0ac3\0" ),
83 /* WavPack */
84 [AFMT_WAVPACK] =
85 AFMT_ENTRY("WV", "wavpack", "wavpack_enc", "wv\0" ),
86 /* Apple Lossless Audio Codec */
87 [AFMT_ALAC] =
88 AFMT_ENTRY("ALAC", "alac", NULL, "m4a\0m4b\0" ),
89 /* Advanced Audio Coding in M4A container */
90 [AFMT_AAC] =
91 AFMT_ENTRY("AAC", "aac", NULL, "mp4\0" ),
92 /* Shorten */
93 [AFMT_SHN] =
94 AFMT_ENTRY("SHN", "shorten", NULL, "shn\0" ),
95 /* SID File Format */
96 [AFMT_SID] =
97 AFMT_ENTRY("SID", "sid", NULL, "sid\0" ),
98 /* ADX File Format */
99 [AFMT_ADX] =
100 AFMT_ENTRY("ADX", "adx", NULL, "adx\0" ),
101 /* NESM (NES Sound Format) */
102 [AFMT_NSF] =
103 AFMT_ENTRY("NSF", "nsf", NULL, "nsf\0nsfe\0" ),
104 /* Speex File Format */
105 [AFMT_SPEEX] =
106 AFMT_ENTRY("Speex","speex", NULL, "spx\0" ),
107 /* SPC700 Save State */
108 [AFMT_SPC] =
109 AFMT_ENTRY("SPC", "spc", NULL, "spc\0" ),
110 /* APE (Monkey's Audio) */
111 [AFMT_APE] =
112 AFMT_ENTRY("APE", "ape", NULL, "ape\0mac\0" ),
113 /* WMA (WMAV1/V2 in ASF) */
114 [AFMT_WMA] =
115 AFMT_ENTRY("WMA", "wma", NULL, "wma\0wmv\0asf\0" ),
116 #endif
119 #if CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING)
120 /* get REC_FORMAT_* corresponding AFMT_* */
121 const int rec_format_afmt[REC_NUM_FORMATS] =
123 /* give AFMT_UNKNOWN by default */
124 [0 ... REC_NUM_FORMATS-1] = AFMT_UNKNOWN,
125 /* add new entries below this line */
126 [REC_FORMAT_AIFF] = AFMT_AIFF,
127 [REC_FORMAT_MPA_L3] = AFMT_MPA_L3,
128 [REC_FORMAT_WAVPACK] = AFMT_WAVPACK,
129 [REC_FORMAT_PCM_WAV] = AFMT_PCM_WAV,
132 /* get AFMT_* corresponding REC_FORMAT_* */
133 const int afmt_rec_format[AFMT_NUM_CODECS] =
135 /* give -1 by default */
136 [0 ... AFMT_NUM_CODECS-1] = -1,
137 /* add new entries below this line */
138 [AFMT_AIFF] = REC_FORMAT_AIFF,
139 [AFMT_MPA_L3] = REC_FORMAT_MPA_L3,
140 [AFMT_WAVPACK] = REC_FORMAT_WAVPACK,
141 [AFMT_PCM_WAV] = REC_FORMAT_PCM_WAV,
143 #endif /* CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING) */
144 /****/
146 unsigned long unsync(unsigned long b0,
147 unsigned long b1,
148 unsigned long b2,
149 unsigned long b3)
151 return (((long)(b0 & 0x7F) << (3*7)) |
152 ((long)(b1 & 0x7F) << (2*7)) |
153 ((long)(b2 & 0x7F) << (1*7)) |
154 ((long)(b3 & 0x7F) << (0*7)));
157 static const char* const genres[] = {
158 "Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge",
159 "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B",
160 "Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative", "Ska",
161 "Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop",
162 "Vocal", "Jazz+Funk", "Fusion", "Trance", "Classical", "Instrumental",
163 "Acid", "House", "Game", "Sound Clip", "Gospel", "Noise", "AlternRock",
164 "Bass", "Soul", "Punk", "Space", "Meditative", "Instrumental Pop",
165 "Instrumental Rock", "Ethnic", "Gothic", "Darkwave", "Techno-Industrial",
166 "Electronic", "Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy",
167 "Cult", "Gangsta", "Top 40", "Christian Rap", "Pop/Funk", "Jungle",
168 "Native American", "Cabaret", "New Wave", "Psychadelic", "Rave",
169 "Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz",
170 "Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock",
172 /* winamp extensions */
173 "Folk", "Folk-Rock", "National Folk", "Swing", "Fast Fusion", "Bebob",
174 "Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock",
175 "Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock",
176 "Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour", "Speech",
177 "Chanson", "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass",
178 "Primus", "Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba",
179 "Folklore", "Ballad", "Power Ballad", "Rhythmic Soul", "Freestyle",
180 "Duet", "Punk Rock", "Drum Solo", "A capella", "Euro-House", "Dance Hall",
181 "Goa", "Drum & Bass", "Club-House", "Hardcore", "Terror", "Indie",
182 "BritPop", "Negerpunk", "Polsk Punk", "Beat", "Christian Gangsta Rap",
183 "Heavy Metal", "Black Metal", "Crossover", "Contemporary Christian",
184 "Christian Rock", "Merengue", "Salsa", "Thrash Metal", "Anime", "Jpop",
185 "Synthpop"
188 char* id3_get_num_genre(const unsigned int genre_num)
190 if (genre_num < sizeof(genres)/sizeof(char*))
191 return (char*)genres[genre_num];
192 return NULL;
195 char* id3_get_codec(const struct mp3entry* id3)
197 if (id3->codectype < AFMT_NUM_CODECS) {
198 return (char*)audio_formats[id3->codectype].label;
199 } else {
200 return NULL;
205 HOW TO ADD ADDITIONAL ID3 VERSION 2 TAGS
206 Code and comments by Thomas Paul Diffenbach
208 To add another ID3v2 Tag, do the following:
209 1. add a char* named for the tag to struct mp3entry in id3.h,
210 (I (tpd) prefer to use char* rather than ints, even for what seems like
211 numerical values, for cases where a number won't do, e.g.,
212 YEAR: "circa 1765", "1790/1977" (composed/performed), "28 Feb 1969"
213 TRACK: "1/12", "1 of 12", GENRE: "Freeform genre name"
214 Text is more flexible, and as the main use of id3 data is to
215 display it, converting it to an int just means reconverting to
216 display it, at a runtime cost.)
218 2. If any special processing beyond copying the tag value from the Id3
219 block to the struct mp3entry is rrequired (such as converting to an
220 int), write a function to perform this special processing.
222 This function's prototype must match that of
223 typedef tagPostProcessFunc, that is it must be:
224 int func( struct mp3entry*, char* tag, int bufferpos )
225 the first argument is a pointer to the current mp3entry structure the
226 second argument is a pointer to the null terminated string value of the
227 tag found the third argument is the offset of the next free byte in the
228 mp3entry's buffer your function should return the corrected offset; if
229 you don't lengthen or shorten the tag string, you can return the third
230 argument unchanged.
232 Unless you have a good reason no to, make the function static.
233 TO JUST COPY THE TAG NO SPECIAL PROCESSING FUNCTION IS NEEDED.
235 3. add one or more entries to the tagList array, using the format:
236 char* ID3 Tag symbolic name -- see the ID3 specification for these,
237 sizeof() that name minus 1,
238 offsetof( struct mp3entry, variable_name_in_struct_mp3entry ),
239 pointer to your special processing function or NULL
240 if you need no special processing
241 flag indicating if this tag is binary or textual
242 Many ID3 symbolic names come in more than one form. You can add both
243 forms, each referencing the same variable in struct mp3entry.
244 If both forms are present, the last found will be used.
245 Note that the offset can be zero, in which case no entry will be set
246 in the mp3entry struct; the frame is still read into the buffer and
247 the special processing function is called (several times, if there
248 are several frames with the same name).
250 4. Alternately, use the TAG_LIST_ENTRY macro with
251 ID3 tag symbolic name,
252 variable in struct mp3entry,
253 special processing function address
255 5. Add code to wps-display.c function get_tag to assign a printf-like
256 format specifier for the tag */
258 /* Structure for ID3 Tag extraction information */
259 struct tag_resolver {
260 const char* tag;
261 int tag_length;
262 size_t offset;
263 int (*ppFunc)(struct mp3entry*, char* tag, int bufferpos);
264 bool binary;
267 static bool global_ff_found;
269 static int unsynchronize(char* tag, int len, bool *ff_found)
271 int i;
272 unsigned char c;
273 unsigned char *rp, *wp;
275 wp = rp = (unsigned char *)tag;
277 rp = (unsigned char *)tag;
278 for(i = 0;i < len;i++) {
279 /* Read the next byte and write it back, but don't increment the
280 write pointer */
281 c = *rp++;
282 *wp = c;
283 if(*ff_found) {
284 /* Increment the write pointer if it isn't an unsynch pattern */
285 if(c != 0)
286 wp++;
287 *ff_found = false;
288 } else {
289 if(c == 0xff)
290 *ff_found = true;
291 wp++;
294 return (long)wp - (long)tag;
297 static int unsynchronize_frame(char* tag, int len)
299 bool ff_found = false;
301 return unsynchronize(tag, len, &ff_found);
304 static int read_unsynched(int fd, void *buf, int len)
306 int i;
307 int rc;
308 int remaining = len;
309 char *wp;
310 char *rp;
312 wp = buf;
314 while(remaining) {
315 rp = wp;
316 rc = read(fd, rp, remaining);
317 if(rc <= 0)
318 return rc;
320 i = unsynchronize(wp, remaining, &global_ff_found);
321 remaining -= i;
322 wp += i;
325 return len;
328 static int skip_unsynched(int fd, int len)
330 int rc;
331 int remaining = len;
332 int rlen;
333 char buf[32];
335 while(remaining) {
336 rlen = MIN(sizeof(buf), (unsigned int)remaining);
337 rc = read(fd, buf, rlen);
338 if(rc <= 0)
339 return rc;
341 remaining -= unsynchronize(buf, rlen, &global_ff_found);
344 return len;
347 /* parse numeric value from string */
348 static int parsetracknum( struct mp3entry* entry, char* tag, int bufferpos )
350 entry->tracknum = atoi( tag );
351 return bufferpos;
354 /* parse numeric value from string */
355 static int parsediscnum( struct mp3entry* entry, char* tag, int bufferpos )
357 entry->discnum = atoi( tag );
358 return bufferpos;
361 /* parse numeric value from string */
362 static int parseyearnum( struct mp3entry* entry, char* tag, int bufferpos )
364 entry->year = atoi( tag );
365 return bufferpos;
368 /* parse numeric genre from string, version 2.2 and 2.3 */
369 static int parsegenre( struct mp3entry* entry, char* tag, int bufferpos )
371 if(entry->id3version >= ID3_VER_2_4) {
372 /* In version 2.4 and up, there are no parentheses, and the genre frame
373 is a list of strings, either numbers or text. */
375 /* Is it a number? */
376 if(isdigit(tag[0])) {
377 entry->genre_string = id3_get_num_genre(atoi( tag ));
378 return tag - entry->id3v2buf;
379 } else {
380 entry->genre_string = tag;
381 return bufferpos;
383 } else {
384 if( tag[0] == '(' && tag[1] != '(' ) {
385 entry->genre_string = id3_get_num_genre(atoi( tag + 1 ));
386 return tag - entry->id3v2buf;
388 else {
389 entry->genre_string = tag;
390 return bufferpos;
395 #if CONFIG_CODEC == SWCODEC
396 /* parse user defined text, looking for replaygain information. */
397 static int parseuser( struct mp3entry* entry, char* tag, int bufferpos )
399 char* value = NULL;
400 int desc_len = strlen(tag);
401 int value_len = 0;
403 if ((tag - entry->id3v2buf + desc_len + 2) < bufferpos) {
404 /* At least part of the value was read, so we can safely try to
405 * parse it
407 value = tag + desc_len + 1;
408 value_len = parse_replaygain(tag, value, entry, tag,
409 bufferpos - (tag - entry->id3v2buf));
412 return tag - entry->id3v2buf + value_len;
415 /* parse RVA2 binary data and convert to replaygain information. */
416 static int parserva2( struct mp3entry* entry, char* tag, int bufferpos )
418 int desc_len = strlen(tag);
419 int end_pos = tag - entry->id3v2buf + desc_len + 5;
420 int value_len = 0;
421 unsigned char* value = tag + desc_len + 1;
423 /* Only parse RVA2 replaygain tags if tag version == 2.4 and channel
424 * type is master volume.
426 if (entry->id3version == ID3_VER_2_4 && end_pos < bufferpos
427 && *value++ == 1) {
428 long gain = 0;
429 long peak = 0;
430 long peakbits;
431 long peakbytes;
432 bool album = false;
434 /* The RVA2 specification is unclear on some things (id string and
435 * peak volume), but this matches how Quod Libet use them.
438 gain = (int16_t) ((value[0] << 8) | value[1]);
439 value += 2;
440 peakbits = *value++;
441 peakbytes = (peakbits + 7) / 8;
443 /* Only use the topmost 24 bits for peak volume */
444 if (peakbytes > 3) {
445 peakbytes = 3;
448 /* Make sure the peak bits were read */
449 if (end_pos + peakbytes < bufferpos) {
450 long shift = ((8 - (peakbits & 7)) & 7) + (3 - peakbytes) * 8;
452 for ( ; peakbytes; peakbytes--) {
453 peak <<= 8;
454 peak += *value++;
457 peak <<= shift;
459 if (peakbits > 24) {
460 peak += *value >> (8 - shift);
464 if (strcasecmp(tag, "album") == 0) {
465 album = true;
466 } else if (strcasecmp(tag, "track") != 0) {
467 gain = 0;
470 value_len = parse_replaygain_int(album, gain, peak * 2, entry,
471 tag, sizeof(entry->id3v2buf) - (tag - entry->id3v2buf));
474 return tag - entry->id3v2buf + value_len;
476 #endif
478 static const struct tag_resolver taglist[] = {
479 { "TPE1", 4, offsetof(struct mp3entry, artist), NULL, false },
480 { "TP1", 3, offsetof(struct mp3entry, artist), NULL, false },
481 { "TIT2", 4, offsetof(struct mp3entry, title), NULL, false },
482 { "TT2", 3, offsetof(struct mp3entry, title), NULL, false },
483 { "TALB", 4, offsetof(struct mp3entry, album), NULL, false },
484 { "TAL", 3, offsetof(struct mp3entry, album), NULL, false },
485 { "TRK", 3, offsetof(struct mp3entry, track_string), &parsetracknum, false },
486 { "TPOS", 4, offsetof(struct mp3entry, disc_string), &parsediscnum, false },
487 { "TRCK", 4, offsetof(struct mp3entry, track_string), &parsetracknum, false },
488 { "TDRC", 4, offsetof(struct mp3entry, year_string), &parseyearnum, false },
489 { "TYER", 4, offsetof(struct mp3entry, year_string), &parseyearnum, false },
490 { "TYE", 3, offsetof(struct mp3entry, year_string), &parseyearnum, false },
491 { "TCOM", 4, offsetof(struct mp3entry, composer), NULL, false },
492 { "TPE2", 4, offsetof(struct mp3entry, albumartist), NULL, false },
493 { "TP2", 3, offsetof(struct mp3entry, albumartist), NULL, false },
494 { "TIT1", 4, offsetof(struct mp3entry, grouping), NULL, false },
495 { "TT1", 3, offsetof(struct mp3entry, grouping), NULL, false },
496 { "COMM", 4, offsetof(struct mp3entry, comment), NULL, false },
497 { "TCON", 4, offsetof(struct mp3entry, genre_string), &parsegenre, false },
498 { "TCO", 3, offsetof(struct mp3entry, genre_string), &parsegenre, false },
499 #if CONFIG_CODEC == SWCODEC
500 { "TXXX", 4, 0, &parseuser, false },
501 { "RVA2", 4, 0, &parserva2, true },
502 #endif
505 #define TAGLIST_SIZE ((int)(sizeof(taglist) / sizeof(taglist[0])))
507 /* Get the length of an ID3 string in the given encoding. Returns the length
508 * in bytes, including end nil, or -1 if the encoding is unknown.
510 static int unicode_len(char encoding, const void* string)
512 int len = 0;
514 if (encoding == 0x01 || encoding == 0x02) {
515 char first;
516 const char *s = string;
517 /* string might be unaligned, so using short* can crash on ARM and SH1 */
518 do {
519 first = *s++;
520 } while ((first | *s++) != 0);
522 len = s - (const char*) string;
523 } else {
524 len = strlen((char*) string) + 1;
527 return len;
530 /* Checks to see if the passed in string is a 16-bit wide Unicode v2
531 string. If it is, we convert it to a UTF-8 string. If it's not unicode,
532 we convert from the default codepage */
533 static int unicode_munge(char* string, char* utf8buf, int *len) {
534 long tmp;
535 bool le = false;
536 int i = 0;
537 unsigned char *str = (unsigned char *)string;
538 int templen = 0;
539 unsigned char* utf8 = (unsigned char *)utf8buf;
541 switch (str[0]) {
542 case 0x00: /* Type 0x00 is ordinary ISO 8859-1 */
543 str++;
544 (*len)--;
545 utf8 = iso_decode(str, utf8, -1, *len);
546 *utf8 = 0;
547 *len = (unsigned long)utf8 - (unsigned long)utf8buf;
548 break;
550 case 0x01: /* Unicode with or without BOM */
551 case 0x02:
552 (*len)--;
553 str++;
555 /* Handle frames with more than one string
556 (needed for TXXX frames).*/
557 do {
558 tmp = bytes2int(0, 0, str[0], str[1]);
560 /* Now check if there is a BOM
561 (zero-width non-breaking space, 0xfeff)
562 and if it is in little or big endian format */
563 if(tmp == 0xfffe) { /* Little endian? */
564 le = true;
565 str += 2;
566 (*len)-=2;
567 } else if(tmp == 0xfeff) { /* Big endian? */
568 str += 2;
569 (*len)-=2;
570 } else
571 /* If there is no BOM (which is a specification violation),
572 let's try to guess it. If one of the bytes is 0x00, it is
573 probably the most significant one. */
574 if(str[1] == 0)
575 le = true;
577 do {
578 if(le)
579 utf8 = utf16LEdecode(str, utf8, 1);
580 else
581 utf8 = utf16BEdecode(str, utf8, 1);
583 str+=2;
584 i += 2;
585 } while((str[0] || str[1]) && (i < *len));
587 *utf8++ = 0; /* Terminate the string */
588 templen += (strlen(&utf8buf[templen]) + 1);
589 str += 2;
590 i+=2;
591 } while(i < *len);
592 *len = templen - 1;
593 break;
595 case 0x03: /* UTF-8 encoded string */
596 for(i=0; i < *len; i++)
597 utf8[i] = str[i+1];
598 (*len)--;
599 break;
601 default: /* Plain old string */
602 utf8 = iso_decode(str, utf8, -1, *len);
603 *utf8 = 0;
604 *len = (unsigned long)utf8 - (unsigned long)utf8buf;
605 break;
607 return 0;
611 * Sets the title of an MP3 entry based on its ID3v1 tag.
613 * Arguments: file - the MP3 file to scen for a ID3v1 tag
614 * entry - the entry to set the title in
616 * Returns: true if a title was found and created, else false
618 static bool setid3v1title(int fd, struct mp3entry *entry)
620 unsigned char buffer[128];
621 static const char offsets[] = {3, 33, 63, 97, 93, 125, 127};
622 int i, j;
623 unsigned char* utf8;
625 if (-1 == lseek(fd, -128, SEEK_END))
626 return false;
628 if (read(fd, buffer, sizeof buffer) != sizeof buffer)
629 return false;
631 if (strncmp((char *)buffer, "TAG", 3))
632 return false;
634 entry->id3v1len = 128;
635 entry->id3version = ID3_VER_1_0;
637 for (i=0; i < (int)sizeof offsets; i++) {
638 unsigned char* ptr = (unsigned char *)buffer + offsets[i];
640 switch(i) {
641 case 0:
642 case 1:
643 case 2:
644 /* kill trailing space in strings */
645 for (j=29; j && (ptr[j]==0 || ptr[j]==' '); j--)
646 ptr[j] = 0;
647 /* convert string to utf8 */
648 utf8 = (unsigned char *)entry->id3v1buf[i];
649 utf8 = iso_decode(ptr, utf8, -1, 30);
650 /* make sure string is terminated */
651 *utf8 = 0;
652 break;
654 case 3:
655 /* kill trailing space in strings */
656 for (j=27; j && (ptr[j]==0 || ptr[j]==' '); j--)
657 ptr[j] = 0;
658 /* convert string to utf8 */
659 utf8 = (unsigned char *)entry->id3v1buf[3];
660 utf8 = iso_decode(ptr, utf8, -1, 28);
661 /* make sure string is terminated */
662 *utf8 = 0;
663 break;
665 case 4:
666 ptr[4] = 0;
667 entry->year = atoi((char *)ptr);
668 break;
670 case 5:
671 /* id3v1.1 uses last two bytes of comment field for track
672 number: first must be 0 and second is track num */
673 if (!ptr[0] && ptr[1]) {
674 entry->tracknum = ptr[1];
675 entry->id3version = ID3_VER_1_1;
677 break;
679 case 6:
680 /* genre */
681 entry->genre_string = id3_get_num_genre(ptr[0]);
682 break;
686 entry->title = entry->id3v1buf[0];
687 entry->artist = entry->id3v1buf[1];
688 entry->album = entry->id3v1buf[2];
689 entry->comment = entry->id3v1buf[3];
691 return true;
696 * Sets the title of an MP3 entry based on its ID3v2 tag.
698 * Arguments: file - the MP3 file to scan for a ID3v2 tag
699 * entry - the entry to set the title in
701 * Returns: true if a title was found and created, else false
703 static void setid3v2title(int fd, struct mp3entry *entry)
705 int minframesize;
706 int size;
707 long bufferpos = 0, totframelen, framelen;
708 char header[10];
709 char tmp[4];
710 unsigned char version;
711 char *buffer = entry->id3v2buf;
712 int bytesread = 0;
713 int buffersize = sizeof(entry->id3v2buf);
714 unsigned char global_flags;
715 int flags;
716 int skip;
717 bool global_unsynch = false;
718 bool unsynch = false;
719 int data_length_ind;
720 int i, j;
721 int rc;
723 global_ff_found = false;
725 /* Bail out if the tag is shorter than 10 bytes */
726 if(entry->id3v2len < 10)
727 return;
729 /* Read the ID3 tag version from the header */
730 lseek(fd, 0, SEEK_SET);
731 if(10 != read(fd, header, 10))
732 return;
734 /* Get the total ID3 tag size */
735 size = entry->id3v2len - 10;
737 version = header[3];
738 switch ( version ) {
739 case 2:
740 version = ID3_VER_2_2;
741 minframesize = 8;
742 break;
744 case 3:
745 version = ID3_VER_2_3;
746 minframesize = 12;
747 break;
749 case 4:
750 version = ID3_VER_2_4;
751 minframesize = 12;
752 break;
754 default:
755 /* unsupported id3 version */
756 return;
758 entry->id3version = version;
759 entry->tracknum = entry->year = entry->discnum = 0;
760 entry->title = entry->artist = entry->album = NULL; /* FIXME incomplete */
762 global_flags = header[5];
764 /* Skip the extended header if it is present */
765 if(global_flags & 0x40) {
766 if(version == ID3_VER_2_3) {
767 if(10 != read(fd, header, 10))
768 return;
769 /* The 2.3 extended header size doesn't include the header size
770 field itself. Also, it is not unsynched. */
771 framelen =
772 bytes2int(header[0], header[1], header[2], header[3]) + 4;
774 /* Skip the rest of the header */
775 lseek(fd, framelen - 10, SEEK_CUR);
778 if(version >= ID3_VER_2_4) {
779 if(4 != read(fd, header, 4))
780 return;
782 /* The 2.4 extended header size does include the entire header,
783 so here we can just skip it. This header is unsynched. */
784 framelen = unsync(header[0], header[1],
785 header[2], header[3]);
787 lseek(fd, framelen - 4, SEEK_CUR);
791 /* Is unsynchronization applied? */
792 if(global_flags & 0x80) {
793 global_unsynch = true;
797 * We must have at least minframesize bytes left for the
798 * remaining frames to be interesting
800 while (size >= minframesize && bufferpos < buffersize - 1) {
801 flags = 0;
803 /* Read frame header and check length */
804 if(version >= ID3_VER_2_3) {
805 if(global_unsynch && version <= ID3_VER_2_3)
806 rc = read_unsynched(fd, header, 10);
807 else
808 rc = read(fd, header, 10);
809 if(rc != 10)
810 return;
811 /* Adjust for the 10 bytes we read */
812 size -= 10;
814 flags = bytes2int(0, 0, header[8], header[9]);
816 if (version >= ID3_VER_2_4) {
817 framelen = unsync(header[4], header[5],
818 header[6], header[7]);
819 } else {
820 /* version .3 files don't use synchsafe ints for
821 * size */
822 framelen = bytes2int(header[4], header[5],
823 header[6], header[7]);
825 } else {
826 if(6 != read(fd, header, 6))
827 return;
828 /* Adjust for the 6 bytes we read */
829 size -= 6;
831 framelen = bytes2int(0, header[3], header[4], header[5]);
834 /* Keep track of the total size */
835 totframelen = framelen;
837 //DEBUGF("framelen = %ld\n", framelen);
838 if(framelen == 0){
839 if (header[0] == 0 && header[1] == 0 && header[2] == 0)
840 return;
841 else
842 continue;
845 unsynch = false;
846 data_length_ind = 0;
848 if(flags)
850 skip = 0;
852 if (version >= ID3_VER_2_4) {
853 if(flags & 0x0040) { /* Grouping identity */
854 lseek(fd, 1, SEEK_CUR); /* Skip 1 byte */
855 framelen--;
857 } else {
858 if(flags & 0x0020) { /* Grouping identity */
859 lseek(fd, 1, SEEK_CUR); /* Skip 1 byte */
860 framelen--;
864 if(flags & 0x000c) /* Compression or encryption */
866 /* Skip it using the total size in case
867 it was truncated */
868 size -= totframelen;
869 lseek(fd, totframelen, SEEK_CUR);
870 continue;
873 if(flags & 0x0002) /* Unsynchronization */
874 unsynch = true;
876 if (version >= ID3_VER_2_4) {
877 if(flags & 0x0001) { /* Data length indicator */
878 if(4 != read(fd, tmp, 4))
879 return;
881 data_length_ind = unsync(tmp[0], tmp[1], tmp[2], tmp[3]);
882 framelen -= 4;
887 /* If the frame is larger than the remaining buffer space we try
888 to read as much as would fit in the buffer */
889 if(framelen >= buffersize - bufferpos)
890 framelen = buffersize - bufferpos - 1;
892 //DEBUGF("id3v2 frame: %.4s\n", header);
894 /* Check for certain frame headers
896 'size' is the amount of frame bytes remaining. We decrement it by
897 the amount of bytes we read. If we fail to read as many bytes as
898 we expect, we assume that we can't read from this file, and bail
899 out.
901 For each frame. we will iterate over the list of supported tags,
902 and read the tag into entry's buffer. All tags will be kept as
903 strings, for cases where a number won't do, e.g., YEAR: "circa
904 1765", "1790/1977" (composed/performed), "28 Feb 1969" TRACK:
905 "1/12", "1 of 12", GENRE: "Freeform genre name" Text is more
906 flexible, and as the main use of id3 data is to display it,
907 converting it to an int just means reconverting to display it, at a
908 runtime cost.
910 For tags that the current code does convert to ints, a post
911 processing function will be called via a pointer to function. */
913 for (i=0; i<TAGLIST_SIZE; i++) {
914 const struct tag_resolver* tr = &taglist[i];
915 char** ptag = tr->offset ? (char**) (((char*)entry) + tr->offset)
916 : NULL;
917 char* tag;
919 /* Only ID3_VER_2_2 uses frames with three-character names. */
920 if (((version == ID3_VER_2_2) && (tr->tag_length != 3))
921 || ((version > ID3_VER_2_2) && (tr->tag_length != 4))) {
922 continue;
925 /* Note that parser functions sometimes set *ptag to NULL, so
926 * the "!*ptag" check here doesn't always have the desired
927 * effect. Should the parser functions (parsegenre in
928 * particular) be updated to handle the case of being called
929 * multiple times, or should the "*ptag" check be removed?
931 if( (!ptag || !*ptag) && !memcmp( header, tr->tag, tr->tag_length ) ) {
933 /* found a tag matching one in tagList, and not yet filled */
934 tag = buffer + bufferpos;
936 if(global_unsynch && version <= ID3_VER_2_3)
937 bytesread = read_unsynched(fd, tag, framelen);
938 else
939 bytesread = read(fd, tag, framelen);
941 if( bytesread != framelen )
942 return;
944 size -= bytesread;
946 if(unsynch || (global_unsynch && version >= ID3_VER_2_4))
947 bytesread = unsynchronize_frame(tag, bytesread);
949 /* the COMM frame has a 3 char field to hold an ISO-639-1
950 * language string and an optional short description;
951 * remove them so unicode_munge can work correctly
954 if(!memcmp( header, "COMM", 4 )) {
955 int offset;
956 /* ignore comments with iTunes 7 soundcheck/gapless data */
957 if(!strncmp(tag+4, "iTun", 4))
958 break;
959 offset = 3 + unicode_len(*tag, tag + 4);
960 if(bytesread > offset) {
961 bytesread -= offset;
962 memmove(tag + 1, tag + 1 + offset, bytesread - 1);
966 /* Attempt to parse Unicode string only if the tag contents
967 aren't binary */
968 if(!tr->binary) {
969 /* UTF-8 could potentially be 3 times larger */
970 /* so we need to create a new buffer */
971 char utf8buf[(3 * bytesread) + 1];
973 unicode_munge( tag, utf8buf, &bytesread );
975 if(bytesread >= buffersize - bufferpos)
976 bytesread = buffersize - bufferpos - 1;
978 for (j = 0; j < bytesread; j++)
979 tag[j] = utf8buf[j];
981 /* remove trailing spaces */
982 while ( bytesread > 0 && isspace(tag[bytesread-1]))
983 bytesread--;
986 tag[bytesread] = 0;
987 bufferpos += bytesread + 1;
989 if (ptag)
990 *ptag = tag;
992 if( tr->ppFunc )
993 bufferpos = tr->ppFunc(entry, tag, bufferpos);
995 /* Seek to the next frame */
996 if(framelen < totframelen)
997 lseek(fd, totframelen - framelen, SEEK_CUR);
998 break;
1002 if( i == TAGLIST_SIZE ) {
1003 /* no tag in tagList was found, or it was a repeat.
1004 skip it using the total size */
1006 if(global_unsynch && version <= ID3_VER_2_3) {
1007 size -= skip_unsynched(fd, totframelen);
1008 } else {
1009 if(data_length_ind)
1010 totframelen = data_length_ind;
1012 size -= totframelen;
1013 if( lseek(fd, totframelen, SEEK_CUR) == -1 )
1014 return;
1021 * Calculates the size of the ID3v2 tag.
1023 * Arguments: file - the file to search for a tag.
1025 * Returns: the size of the tag or 0 if none was found
1027 int getid3v2len(int fd)
1029 char buf[6];
1030 int offset;
1032 /* Make sure file has a ID3 tag */
1033 if((-1 == lseek(fd, 0, SEEK_SET)) ||
1034 (read(fd, buf, 6) != 6) ||
1035 (strncmp(buf, "ID3", strlen("ID3")) != 0))
1036 offset = 0;
1038 /* Now check what the ID3v2 size field says */
1039 else
1040 if(read(fd, buf, 4) != 4)
1041 offset = 0;
1042 else
1043 offset = unsync(buf[0], buf[1], buf[2], buf[3]) + 10;
1045 //DEBUGF("ID3V2 Length: 0x%x\n", offset);
1046 return offset;
1050 * Calculates the length (in milliseconds) of an MP3 file.
1052 * Modified to only use integers.
1054 * Arguments: file - the file to calculate the length upon
1055 * entry - the entry to update with the length
1057 * Returns: the song length in milliseconds,
1058 * 0 means that it couldn't be calculated
1060 static int getsonglength(int fd, struct mp3entry *entry)
1062 unsigned long filetime = 0;
1063 struct mp3info info;
1064 long bytecount;
1066 /* Start searching after ID3v2 header */
1067 if(-1 == lseek(fd, entry->id3v2len, SEEK_SET))
1068 return 0;
1070 bytecount = get_mp3file_info(fd, &info);
1072 //DEBUGF("Space between ID3V2 tag and first audio frame: 0x%lx bytes\n",
1073 // bytecount);
1075 if(bytecount < 0)
1076 return -1;
1078 bytecount += entry->id3v2len;
1080 /* Validate byte count, in case the file has been edited without
1081 * updating the header.
1083 if (info.byte_count)
1085 const unsigned long expected = entry->filesize - entry->id3v1len
1086 - entry->id3v2len;
1087 const unsigned long diff = MAX(10240, info.byte_count / 20);
1089 if ((info.byte_count > expected + diff)
1090 || (info.byte_count < expected - diff))
1092 //DEBUGF("Note: info.byte_count differs from expected value by "
1093 // "%ld bytes\n", labs((long) (expected - info.byte_count)));
1094 info.byte_count = 0;
1095 info.frame_count = 0;
1096 info.file_time = 0;
1097 info.enc_padding = 0;
1099 /* Even if the bitrate was based on "known bad" values, it
1100 * should still be better for VBR files than using the bitrate
1101 * of the first audio frame.
1106 entry->bitrate = info.bitrate;
1107 entry->frequency = info.frequency;
1108 entry->version = info.version;
1109 entry->layer = info.layer;
1110 switch(entry->layer) {
1111 #if CONFIG_CODEC==SWCODEC
1112 case 0:
1113 entry->codectype=AFMT_MPA_L1;
1114 break;
1115 #endif
1116 case 1:
1117 entry->codectype=AFMT_MPA_L2;
1118 break;
1119 case 2:
1120 entry->codectype=AFMT_MPA_L3;
1121 break;
1124 /* If the file time hasn't been established, this may be a fixed
1125 rate MP3, so just use the default formula */
1127 filetime = info.file_time;
1129 if(filetime == 0)
1131 /* Prevent a division by zero */
1132 if (info.bitrate < 8)
1133 filetime = 0;
1134 else
1135 filetime = (entry->filesize - bytecount) / (info.bitrate / 8);
1136 /* bitrate is in kbps so this delivers milliseconds. Doing bitrate / 8
1137 * instead of filesize * 8 is exact, because mpeg audio bitrates are
1138 * always multiples of 8, and it avoids overflows. */
1141 entry->frame_count = info.frame_count;
1143 entry->vbr = info.is_vbr;
1144 entry->has_toc = info.has_toc;
1146 #if CONFIG_CODEC==SWCODEC
1147 entry->lead_trim = info.enc_delay;
1148 entry->tail_trim = info.enc_padding;
1149 #endif
1151 memcpy(entry->toc, info.toc, sizeof(info.toc));
1153 entry->vbr_header_pos = info.vbr_header_pos;
1155 /* Update the seek point for the first playable frame */
1156 entry->first_frame_offset = bytecount;
1157 //DEBUGF("First frame is at %lx\n", entry->first_frame_offset);
1159 return filetime;
1163 * Checks all relevant information (such as ID3v1 tag, ID3v2 tag, length etc)
1164 * about an MP3 file and updates it's entry accordingly.
1166 Note, that this returns true for successful, false for error! */
1167 bool get_mp3_metadata(int fd, struct mp3entry *entry, const char *filename)
1169 #if CONFIG_CODEC != SWCODEC
1170 memset(entry, 0, sizeof(struct mp3entry));
1171 #endif
1173 strncpy(entry->path, filename, sizeof(entry->path));
1175 entry->title = NULL;
1176 entry->filesize = filesize(fd);
1177 entry->id3v2len = getid3v2len(fd);
1178 entry->tracknum = 0;
1179 entry->discnum = 0;
1181 if (entry->id3v2len)
1182 setid3v2title(fd, entry);
1183 int len = getsonglength(fd, entry);
1184 if (len < 0)
1185 return false;
1186 entry->length = len;
1188 /* Subtract the meta information from the file size to get
1189 the true size of the MP3 stream */
1190 entry->filesize -= entry->first_frame_offset;
1192 /* only seek to end of file if no id3v2 tags were found */
1193 if (!entry->id3v2len) {
1194 setid3v1title(fd, entry);
1197 if(!entry->length || (entry->filesize < 8 ))
1198 /* no song length or less than 8 bytes is hereby considered to be an
1199 invalid mp3 and won't be played by us! */
1200 return false;
1202 return true;
1205 /* Note, that this returns false for successful, true for error! */
1206 bool mp3info(struct mp3entry *entry, const char *filename)
1208 int fd;
1209 bool result;
1211 fd = open(filename, O_RDONLY);
1212 if (fd < 0)
1213 return true;
1215 result = !get_mp3_metadata(fd, entry, filename);
1217 close(fd);
1219 return result;
1222 void adjust_mp3entry(struct mp3entry *entry, void *dest, void *orig)
1224 long offset;
1225 if (orig > dest)
1226 offset = - ((size_t)orig - (size_t)dest);
1227 else
1228 offset = (size_t)dest - (size_t)orig;
1230 if (entry->title)
1231 entry->title += offset;
1232 if (entry->artist)
1233 entry->artist += offset;
1234 if (entry->album)
1235 entry->album += offset;
1236 if (entry->genre_string)
1237 entry->genre_string += offset;
1238 if (entry->track_string)
1239 entry->track_string += offset;
1240 if (entry->disc_string)
1241 entry->disc_string += offset;
1242 if (entry->year_string)
1243 entry->year_string += offset;
1244 if (entry->composer)
1245 entry->composer += offset;
1246 if (entry->comment)
1247 entry->comment += offset;
1248 if (entry->albumartist)
1249 entry->albumartist += offset;
1250 if (entry->grouping)
1251 entry->grouping += offset;
1252 #if CONFIG_CODEC == SWCODEC
1253 if (entry->track_gain_string)
1254 entry->track_gain_string += offset;
1255 if (entry->album_gain_string)
1256 entry->album_gain_string += offset;
1257 #endif
1260 void copy_mp3entry(struct mp3entry *dest, struct mp3entry *orig)
1262 memcpy(dest, orig, sizeof(struct mp3entry));
1263 adjust_mp3entry(dest, dest, orig);
1266 #ifdef DEBUG_STANDALONE
1268 char *secs2str(int ms)
1270 static char buffer[32];
1271 int secs = ms/1000;
1272 ms %= 1000;
1273 snprintf(buffer, sizeof(buffer), "%d:%02d.%d", secs/60, secs%60, ms/100);
1274 return buffer;
1277 int main(int argc, char **argv)
1279 int i;
1280 for(i=1; i<argc; i++) {
1281 struct mp3entry mp3;
1282 mp3.album = "Bogus";
1283 if(mp3info(&mp3, argv[i], false)) {
1284 printf("Failed to get %s\n", argv[i]);
1285 return 0;
1288 printf("****** File: %s\n"
1289 " Title: %s\n"
1290 " Artist: %s\n"
1291 " Album: %s\n"
1292 " Genre: %s (%d) \n"
1293 " Composer: %s\n"
1294 " Year: %s (%d)\n"
1295 " Track: %s (%d)\n"
1296 " Length: %s / %d s\n"
1297 " Bitrate: %d\n"
1298 " Frequency: %d\n",
1299 argv[i],
1300 mp3.title?mp3.title:"<blank>",
1301 mp3.artist?mp3.artist:"<blank>",
1302 mp3.album?mp3.album:"<blank>",
1303 mp3.genre_string?mp3.genre_string:"<blank>",
1304 mp3.genre,
1305 mp3.composer?mp3.composer:"<blank>",
1306 mp3.year_string?mp3.year_string:"<blank>",
1307 mp3.year,
1308 mp3.track_string?mp3.track_string:"<blank>",
1309 mp3.tracknum,
1310 secs2str(mp3.length),
1311 mp3.length/1000,
1312 mp3.bitrate,
1313 mp3.frequency);
1316 return 0;
1319 #endif