2002-04-23 08:43:04 +00:00
|
|
|
|
/***************************************************************************
|
|
|
|
|
* __________ __ ___.
|
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
|
* $Id$
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2002 by Daniel Stenberg
|
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-04-23 08:43:04 +00:00
|
|
|
|
*
|
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
|
* KIND, either express or implied.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
/*
|
|
|
|
|
* Parts of this code has been stolen from the Ample project and was written
|
2007-02-09 10:06:53 +00:00
|
|
|
|
* by David H<EFBFBD>deman. It has since been extended and enhanced pretty much by
|
2002-08-19 14:15:33 +00:00
|
|
|
|
* all sorts of friendly Rockbox people.
|
|
|
|
|
*
|
2002-04-23 08:43:04 +00:00
|
|
|
|
*/
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2003-06-04 15:09:35 +00:00
|
|
|
|
/* tagResolver and associated code copyright 2003 Thomas Paul Diffenbach
|
|
|
|
|
*/
|
2002-04-23 08:43:04 +00:00
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
2002-05-13 12:29:34 +00:00
|
|
|
|
#include <stdbool.h>
|
2003-06-04 15:09:35 +00:00
|
|
|
|
#include <stddef.h>
|
2003-10-21 10:44:34 +00:00
|
|
|
|
#include <ctype.h>
|
2005-06-18 16:24:27 +00:00
|
|
|
|
#include "config.h"
|
2002-05-08 15:36:18 +00:00
|
|
|
|
#include "file.h"
|
2007-10-29 18:02:41 +00:00
|
|
|
|
#include "logf.h"
|
2002-04-30 12:42:37 +00:00
|
|
|
|
|
2003-03-10 14:55:31 +00:00
|
|
|
|
#include "mp3data.h"
|
2004-01-04 00:37:30 +00:00
|
|
|
|
#include "system.h"
|
2005-07-27 11:54:33 +00:00
|
|
|
|
#include "replaygain.h"
|
2005-12-06 13:27:15 +00:00
|
|
|
|
#include "rbunicode.h"
|
2008-10-26 13:07:29 +00:00
|
|
|
|
#include "metadata_parsers.h"
|
2002-04-23 08:43:04 +00:00
|
|
|
|
|
2008-04-12 13:25:16 +00:00
|
|
|
|
static unsigned long unsync(unsigned long b0,
|
|
|
|
|
unsigned long b1,
|
|
|
|
|
unsigned long b2,
|
|
|
|
|
unsigned long b3)
|
2007-02-15 22:55:22 +00:00
|
|
|
|
{
|
|
|
|
|
return (((long)(b0 & 0x7F) << (3*7)) |
|
|
|
|
|
((long)(b1 & 0x7F) << (2*7)) |
|
|
|
|
|
((long)(b2 & 0x7F) << (1*7)) |
|
|
|
|
|
((long)(b3 & 0x7F) << (0*7)));
|
|
|
|
|
}
|
2002-09-30 18:41:59 +00:00
|
|
|
|
|
2004-05-22 01:38:41 +00:00
|
|
|
|
static const char* const genres[] = {
|
|
|
|
|
"Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge",
|
|
|
|
|
"Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B",
|
|
|
|
|
"Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative", "Ska",
|
|
|
|
|
"Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop",
|
|
|
|
|
"Vocal", "Jazz+Funk", "Fusion", "Trance", "Classical", "Instrumental",
|
|
|
|
|
"Acid", "House", "Game", "Sound Clip", "Gospel", "Noise", "AlternRock",
|
|
|
|
|
"Bass", "Soul", "Punk", "Space", "Meditative", "Instrumental Pop",
|
|
|
|
|
"Instrumental Rock", "Ethnic", "Gothic", "Darkwave", "Techno-Industrial",
|
|
|
|
|
"Electronic", "Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy",
|
|
|
|
|
"Cult", "Gangsta", "Top 40", "Christian Rap", "Pop/Funk", "Jungle",
|
|
|
|
|
"Native American", "Cabaret", "New Wave", "Psychadelic", "Rave",
|
|
|
|
|
"Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz",
|
|
|
|
|
"Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock",
|
|
|
|
|
|
|
|
|
|
/* winamp extensions */
|
|
|
|
|
"Folk", "Folk-Rock", "National Folk", "Swing", "Fast Fusion", "Bebob",
|
|
|
|
|
"Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock",
|
|
|
|
|
"Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock",
|
|
|
|
|
"Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour", "Speech",
|
|
|
|
|
"Chanson", "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass",
|
|
|
|
|
"Primus", "Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba",
|
|
|
|
|
"Folklore", "Ballad", "Power Ballad", "Rhythmic Soul", "Freestyle",
|
2005-09-07 22:51:43 +00:00
|
|
|
|
"Duet", "Punk Rock", "Drum Solo", "A capella", "Euro-House", "Dance Hall",
|
2005-09-08 00:17:11 +00:00
|
|
|
|
"Goa", "Drum & Bass", "Club-House", "Hardcore", "Terror", "Indie",
|
|
|
|
|
"BritPop", "Negerpunk", "Polsk Punk", "Beat", "Christian Gangsta Rap",
|
|
|
|
|
"Heavy Metal", "Black Metal", "Crossover", "Contemporary Christian",
|
2007-01-07 23:10:21 +00:00
|
|
|
|
"Christian Rock", "Merengue", "Salsa", "Thrash Metal", "Anime", "Jpop",
|
2005-09-07 22:51:43 +00:00
|
|
|
|
"Synthpop"
|
2004-05-22 01:38:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
2008-03-28 12:51:33 +00:00
|
|
|
|
char* id3_get_num_genre(unsigned int genre_num)
|
2004-05-22 01:38:41 +00:00
|
|
|
|
{
|
2007-03-02 21:49:42 +00:00
|
|
|
|
if (genre_num < sizeof(genres)/sizeof(char*))
|
|
|
|
|
return (char*)genres[genre_num];
|
2004-05-22 01:38:41 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 14:46:45 +00:00
|
|
|
|
/* True if the string is from the "genres" array */
|
2008-10-15 06:38:51 +00:00
|
|
|
|
bool id3_is_genre_string(const char *string)
|
2007-10-28 14:46:45 +00:00
|
|
|
|
{
|
|
|
|
|
return ( string >= genres[0] &&
|
|
|
|
|
string <= genres[sizeof(genres)/sizeof(char*) - 1] );
|
|
|
|
|
}
|
|
|
|
|
|
2003-06-04 15:09:35 +00:00
|
|
|
|
/*
|
|
|
|
|
HOW TO ADD ADDITIONAL ID3 VERSION 2 TAGS
|
|
|
|
|
Code and comments by Thomas Paul Diffenbach
|
|
|
|
|
|
|
|
|
|
To add another ID3v2 Tag, do the following:
|
|
|
|
|
1. add a char* named for the tag to struct mp3entry in id3.h,
|
|
|
|
|
(I (tpd) prefer to use char* rather than ints, even for what seems like
|
|
|
|
|
numerical values, for cases where a number won't do, e.g.,
|
|
|
|
|
YEAR: "circa 1765", "1790/1977" (composed/performed), "28 Feb 1969"
|
|
|
|
|
TRACK: "1/12", "1 of 12", GENRE: "Freeform genre name"
|
2006-07-20 10:35:20 +00:00
|
|
|
|
Text is more flexible, and as the main use of id3 data is to
|
|
|
|
|
display it, converting it to an int just means reconverting to
|
2003-06-04 15:09:35 +00:00
|
|
|
|
display it, at a runtime cost.)
|
|
|
|
|
|
|
|
|
|
2. If any special processing beyond copying the tag value from the Id3
|
|
|
|
|
block to the struct mp3entry is rrequired (such as converting to an
|
|
|
|
|
int), write a function to perform this special processing.
|
|
|
|
|
|
|
|
|
|
This function's prototype must match that of
|
|
|
|
|
typedef tagPostProcessFunc, that is it must be:
|
|
|
|
|
int func( struct mp3entry*, char* tag, int bufferpos )
|
|
|
|
|
the first argument is a pointer to the current mp3entry structure the
|
|
|
|
|
second argument is a pointer to the null terminated string value of the
|
|
|
|
|
tag found the third argument is the offset of the next free byte in the
|
|
|
|
|
mp3entry's buffer your function should return the corrected offset; if
|
|
|
|
|
you don't lengthen or shorten the tag string, you can return the third
|
|
|
|
|
argument unchanged.
|
|
|
|
|
|
|
|
|
|
Unless you have a good reason no to, make the function static.
|
|
|
|
|
TO JUST COPY THE TAG NO SPECIAL PROCESSING FUNCTION IS NEEDED.
|
|
|
|
|
|
|
|
|
|
3. add one or more entries to the tagList array, using the format:
|
|
|
|
|
char* ID3 Tag symbolic name -- see the ID3 specification for these,
|
|
|
|
|
sizeof() that name minus 1,
|
|
|
|
|
offsetof( struct mp3entry, variable_name_in_struct_mp3entry ),
|
2006-07-20 10:35:20 +00:00
|
|
|
|
pointer to your special processing function or NULL
|
2003-06-04 15:09:35 +00:00
|
|
|
|
if you need no special processing
|
2006-07-20 10:35:20 +00:00
|
|
|
|
flag indicating if this tag is binary or textual
|
|
|
|
|
Many ID3 symbolic names come in more than one form. You can add both
|
|
|
|
|
forms, each referencing the same variable in struct mp3entry.
|
2003-06-04 15:09:35 +00:00
|
|
|
|
If both forms are present, the last found will be used.
|
2005-07-27 11:54:33 +00:00
|
|
|
|
Note that the offset can be zero, in which case no entry will be set
|
|
|
|
|
in the mp3entry struct; the frame is still read into the buffer and
|
|
|
|
|
the special processing function is called (several times, if there
|
|
|
|
|
are several frames with the same name).
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2003-06-04 15:09:35 +00:00
|
|
|
|
4. Alternately, use the TAG_LIST_ENTRY macro with
|
2006-07-20 10:35:20 +00:00
|
|
|
|
ID3 tag symbolic name,
|
|
|
|
|
variable in struct mp3entry,
|
2003-06-04 15:09:35 +00:00
|
|
|
|
special processing function address
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
|
|
|
|
5. Add code to wps-display.c function get_tag to assign a printf-like
|
2003-06-04 15:09:35 +00:00
|
|
|
|
format specifier for the tag */
|
|
|
|
|
|
|
|
|
|
/* Structure for ID3 Tag extraction information */
|
|
|
|
|
struct tag_resolver {
|
|
|
|
|
const char* tag;
|
|
|
|
|
int tag_length;
|
|
|
|
|
size_t offset;
|
|
|
|
|
int (*ppFunc)(struct mp3entry*, char* tag, int bufferpos);
|
2006-07-20 10:35:20 +00:00
|
|
|
|
bool binary;
|
2003-06-04 15:09:35 +00:00
|
|
|
|
};
|
|
|
|
|
|
2004-01-04 00:45:16 +00:00
|
|
|
|
static bool global_ff_found;
|
2004-01-04 00:37:30 +00:00
|
|
|
|
|
|
|
|
|
static int unsynchronize(char* tag, int len, bool *ff_found)
|
2003-10-22 13:06:31 +00:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
unsigned char c;
|
|
|
|
|
unsigned char *rp, *wp;
|
|
|
|
|
|
2005-11-25 00:10:12 +00:00
|
|
|
|
wp = rp = (unsigned char *)tag;
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2003-10-22 13:06:31 +00:00
|
|
|
|
rp = (unsigned char *)tag;
|
|
|
|
|
for(i = 0;i < len;i++) {
|
|
|
|
|
/* Read the next byte and write it back, but don't increment the
|
|
|
|
|
write pointer */
|
|
|
|
|
c = *rp++;
|
|
|
|
|
*wp = c;
|
2004-01-04 00:37:30 +00:00
|
|
|
|
if(*ff_found) {
|
2003-10-22 13:06:31 +00:00
|
|
|
|
/* Increment the write pointer if it isn't an unsynch pattern */
|
|
|
|
|
if(c != 0)
|
|
|
|
|
wp++;
|
2004-01-04 00:37:30 +00:00
|
|
|
|
*ff_found = false;
|
2003-10-22 13:06:31 +00:00
|
|
|
|
} else {
|
|
|
|
|
if(c == 0xff)
|
2004-01-04 00:37:30 +00:00
|
|
|
|
*ff_found = true;
|
2003-10-22 13:06:31 +00:00
|
|
|
|
wp++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-02-09 10:56:54 +00:00
|
|
|
|
return (long)wp - (long)tag;
|
2003-10-22 13:06:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-01-04 00:37:30 +00:00
|
|
|
|
static int unsynchronize_frame(char* tag, int len)
|
|
|
|
|
{
|
|
|
|
|
bool ff_found = false;
|
|
|
|
|
|
|
|
|
|
return unsynchronize(tag, len, &ff_found);
|
|
|
|
|
}
|
|
|
|
|
|
2004-01-04 00:45:16 +00:00
|
|
|
|
static int read_unsynched(int fd, void *buf, int len)
|
2004-01-04 00:37:30 +00:00
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int rc;
|
|
|
|
|
int remaining = len;
|
|
|
|
|
char *wp;
|
|
|
|
|
char *rp;
|
|
|
|
|
|
|
|
|
|
wp = buf;
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2004-01-04 00:37:30 +00:00
|
|
|
|
while(remaining) {
|
|
|
|
|
rp = wp;
|
|
|
|
|
rc = read(fd, rp, remaining);
|
2007-03-30 07:11:55 +00:00
|
|
|
|
if(rc <= 0)
|
2004-01-04 00:37:30 +00:00
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
|
|
i = unsynchronize(wp, remaining, &global_ff_found);
|
|
|
|
|
remaining -= i;
|
|
|
|
|
wp += i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return len;
|
2004-06-16 11:30:28 +00:00
|
|
|
|
}
|
2004-01-04 00:37:30 +00:00
|
|
|
|
|
2004-01-04 00:45:16 +00:00
|
|
|
|
static int skip_unsynched(int fd, int len)
|
2004-01-04 00:37:30 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
int remaining = len;
|
|
|
|
|
int rlen;
|
|
|
|
|
char buf[32];
|
|
|
|
|
|
|
|
|
|
while(remaining) {
|
|
|
|
|
rlen = MIN(sizeof(buf), (unsigned int)remaining);
|
|
|
|
|
rc = read(fd, buf, rlen);
|
2007-03-30 07:11:55 +00:00
|
|
|
|
if(rc <= 0)
|
2004-01-04 00:37:30 +00:00
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
|
|
remaining -= unsynchronize(buf, rlen, &global_ff_found);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return len;
|
2004-06-16 11:26:18 +00:00
|
|
|
|
}
|
2004-01-04 00:37:30 +00:00
|
|
|
|
|
2003-06-04 15:09:35 +00:00
|
|
|
|
/* parse numeric value from string */
|
2003-06-06 07:54:50 +00:00
|
|
|
|
static int parsetracknum( struct mp3entry* entry, char* tag, int bufferpos )
|
2003-06-04 15:09:35 +00:00
|
|
|
|
{
|
|
|
|
|
entry->tracknum = atoi( tag );
|
|
|
|
|
return bufferpos;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-03 10:00:42 +00:00
|
|
|
|
/* parse numeric value from string */
|
|
|
|
|
static int parsediscnum( struct mp3entry* entry, char* tag, int bufferpos )
|
|
|
|
|
{
|
|
|
|
|
entry->discnum = atoi( tag );
|
|
|
|
|
return bufferpos;
|
|
|
|
|
}
|
|
|
|
|
|
2003-06-06 07:54:50 +00:00
|
|
|
|
/* parse numeric value from string */
|
|
|
|
|
static int parseyearnum( struct mp3entry* entry, char* tag, int bufferpos )
|
|
|
|
|
{
|
|
|
|
|
entry->year = atoi( tag );
|
|
|
|
|
return bufferpos;
|
|
|
|
|
}
|
|
|
|
|
|
2003-10-21 10:44:34 +00:00
|
|
|
|
/* parse numeric genre from string, version 2.2 and 2.3 */
|
2003-10-22 21:11:38 +00:00
|
|
|
|
static int parsegenre( struct mp3entry* entry, char* tag, int bufferpos )
|
2003-06-04 15:09:35 +00:00
|
|
|
|
{
|
2003-10-26 12:53:21 +00:00
|
|
|
|
if(entry->id3version >= ID3_VER_2_4) {
|
2003-10-22 21:11:38 +00:00
|
|
|
|
/* In version 2.4 and up, there are no parentheses, and the genre frame
|
|
|
|
|
is a list of strings, either numbers or text. */
|
|
|
|
|
|
|
|
|
|
/* Is it a number? */
|
|
|
|
|
if(isdigit(tag[0])) {
|
2007-03-02 21:49:42 +00:00
|
|
|
|
entry->genre_string = id3_get_num_genre(atoi( tag ));
|
2003-10-22 21:11:38 +00:00
|
|
|
|
return tag - entry->id3v2buf;
|
|
|
|
|
} else {
|
2004-03-25 06:58:25 +00:00
|
|
|
|
entry->genre_string = tag;
|
2003-10-22 21:11:38 +00:00
|
|
|
|
return bufferpos;
|
|
|
|
|
}
|
2003-10-21 10:44:34 +00:00
|
|
|
|
} else {
|
2003-10-22 21:11:38 +00:00
|
|
|
|
if( tag[0] == '(' && tag[1] != '(' ) {
|
2007-03-02 21:49:42 +00:00
|
|
|
|
entry->genre_string = id3_get_num_genre(atoi( tag + 1 ));
|
2003-10-22 21:11:38 +00:00
|
|
|
|
return tag - entry->id3v2buf;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2004-03-25 06:58:25 +00:00
|
|
|
|
entry->genre_string = tag;
|
2003-10-22 21:11:38 +00:00
|
|
|
|
return bufferpos;
|
|
|
|
|
}
|
2003-10-21 10:44:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-30 19:00:24 +00:00
|
|
|
|
/* parse user defined text, looking for album artist and replaygain
|
|
|
|
|
* information.
|
|
|
|
|
*/
|
2005-07-27 11:54:33 +00:00
|
|
|
|
static int parseuser( struct mp3entry* entry, char* tag, int bufferpos )
|
|
|
|
|
{
|
|
|
|
|
char* value = NULL;
|
|
|
|
|
int desc_len = strlen(tag);
|
|
|
|
|
int value_len = 0;
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2006-07-21 20:15:01 +00:00
|
|
|
|
if ((tag - entry->id3v2buf + desc_len + 2) < bufferpos) {
|
2006-07-20 10:35:20 +00:00
|
|
|
|
/* At least part of the value was read, so we can safely try to
|
|
|
|
|
* parse it
|
2005-07-27 11:54:33 +00:00
|
|
|
|
*/
|
|
|
|
|
value = tag + desc_len + 1;
|
2008-10-30 19:00:24 +00:00
|
|
|
|
value_len = bufferpos - (tag - entry->id3v2buf);
|
|
|
|
|
|
|
|
|
|
if (!strcasecmp(tag, "ALBUM ARTIST")) {
|
|
|
|
|
strncpy(tag, value, value_len);
|
|
|
|
|
tag[value_len - 1] = 0;
|
|
|
|
|
entry->albumartist = tag;
|
|
|
|
|
#if CONFIG_CODEC == SWCODEC
|
|
|
|
|
} else {
|
|
|
|
|
value_len = parse_replaygain(tag, value, entry, tag,
|
|
|
|
|
value_len);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2005-07-27 11:54:33 +00:00
|
|
|
|
}
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2007-08-25 10:25:13 +00:00
|
|
|
|
return tag - entry->id3v2buf + value_len;
|
2006-07-20 10:35:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-10-30 19:00:24 +00:00
|
|
|
|
#if CONFIG_CODEC == SWCODEC
|
2006-07-20 10:35:20 +00:00
|
|
|
|
/* parse RVA2 binary data and convert to replaygain information. */
|
|
|
|
|
static int parserva2( struct mp3entry* entry, char* tag, int bufferpos )
|
|
|
|
|
{
|
|
|
|
|
int desc_len = strlen(tag);
|
2008-02-10 10:09:42 +00:00
|
|
|
|
int start_pos = tag - entry->id3v2buf;
|
|
|
|
|
int end_pos = start_pos + desc_len + 5;
|
2006-07-20 10:35:20 +00:00
|
|
|
|
int value_len = 0;
|
2007-08-25 10:25:13 +00:00
|
|
|
|
unsigned char* value = tag + desc_len + 1;
|
|
|
|
|
|
|
|
|
|
/* Only parse RVA2 replaygain tags if tag version == 2.4 and channel
|
|
|
|
|
* type is master volume.
|
|
|
|
|
*/
|
|
|
|
|
if (entry->id3version == ID3_VER_2_4 && end_pos < bufferpos
|
|
|
|
|
&& *value++ == 1) {
|
|
|
|
|
long gain = 0;
|
|
|
|
|
long peak = 0;
|
|
|
|
|
long peakbits;
|
|
|
|
|
long peakbytes;
|
|
|
|
|
bool album = false;
|
|
|
|
|
|
|
|
|
|
/* The RVA2 specification is unclear on some things (id string and
|
|
|
|
|
* peak volume), but this matches how Quod Libet use them.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
gain = (int16_t) ((value[0] << 8) | value[1]);
|
|
|
|
|
value += 2;
|
|
|
|
|
peakbits = *value++;
|
|
|
|
|
peakbytes = (peakbits + 7) / 8;
|
|
|
|
|
|
|
|
|
|
/* Only use the topmost 24 bits for peak volume */
|
|
|
|
|
if (peakbytes > 3) {
|
|
|
|
|
peakbytes = 3;
|
|
|
|
|
}
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2007-08-25 10:25:13 +00:00
|
|
|
|
/* Make sure the peak bits were read */
|
|
|
|
|
if (end_pos + peakbytes < bufferpos) {
|
|
|
|
|
long shift = ((8 - (peakbits & 7)) & 7) + (3 - peakbytes) * 8;
|
|
|
|
|
|
|
|
|
|
for ( ; peakbytes; peakbytes--) {
|
|
|
|
|
peak <<= 8;
|
|
|
|
|
peak += *value++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
peak <<= shift;
|
|
|
|
|
|
|
|
|
|
if (peakbits > 24) {
|
|
|
|
|
peak += *value >> (8 - shift);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strcasecmp(tag, "album") == 0) {
|
|
|
|
|
album = true;
|
|
|
|
|
} else if (strcasecmp(tag, "track") != 0) {
|
2008-02-10 10:09:42 +00:00
|
|
|
|
/* Only accept non-track values if we don't have any previous
|
|
|
|
|
* value.
|
|
|
|
|
*/
|
|
|
|
|
if (entry->track_gain != 0) {
|
|
|
|
|
return start_pos;
|
|
|
|
|
}
|
2007-08-25 10:25:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-08-25 10:56:22 +00:00
|
|
|
|
value_len = parse_replaygain_int(album, gain, peak * 2, entry,
|
2008-02-10 10:09:42 +00:00
|
|
|
|
tag, sizeof(entry->id3v2buf) - start_pos);
|
2005-07-27 11:54:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-02-10 10:09:42 +00:00
|
|
|
|
return start_pos + value_len;
|
2005-07-27 11:54:33 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2008-10-07 18:39:44 +00:00
|
|
|
|
static int parsembtid( struct mp3entry* entry, char* tag, int bufferpos )
|
|
|
|
|
{
|
|
|
|
|
char* value = NULL;
|
|
|
|
|
int desc_len = strlen(tag);
|
|
|
|
|
/*DEBUGF("MBID len: %d\n", desc_len);*/
|
2009-04-16 23:10:45 +00:00
|
|
|
|
/* Musicbrainz track IDs are always 36 chars long */
|
|
|
|
|
const size_t mbtid_len = 36;
|
2008-10-07 18:39:44 +00:00
|
|
|
|
|
|
|
|
|
if ((tag - entry->id3v2buf + desc_len + 2) < bufferpos)
|
|
|
|
|
{
|
|
|
|
|
value = tag + desc_len + 1;
|
|
|
|
|
|
|
|
|
|
if (strcasecmp(tag, "http://musicbrainz.org") == 0)
|
|
|
|
|
{
|
2009-04-16 23:10:45 +00:00
|
|
|
|
if (mbtid_len == strlen(value))
|
|
|
|
|
{
|
|
|
|
|
entry->mb_track_id = value;
|
|
|
|
|
return bufferpos + mbtid_len + 1;
|
|
|
|
|
}
|
2008-10-07 18:39:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-16 23:10:45 +00:00
|
|
|
|
return bufferpos;
|
2008-10-07 18:39:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-08-03 19:22:56 +00:00
|
|
|
|
static const struct tag_resolver taglist[] = {
|
2006-07-20 10:35:20 +00:00
|
|
|
|
{ "TPE1", 4, offsetof(struct mp3entry, artist), NULL, false },
|
|
|
|
|
{ "TP1", 3, offsetof(struct mp3entry, artist), NULL, false },
|
|
|
|
|
{ "TIT2", 4, offsetof(struct mp3entry, title), NULL, false },
|
|
|
|
|
{ "TT2", 3, offsetof(struct mp3entry, title), NULL, false },
|
|
|
|
|
{ "TALB", 4, offsetof(struct mp3entry, album), NULL, false },
|
|
|
|
|
{ "TAL", 3, offsetof(struct mp3entry, album), NULL, false },
|
|
|
|
|
{ "TRK", 3, offsetof(struct mp3entry, track_string), &parsetracknum, false },
|
2007-08-03 10:00:42 +00:00
|
|
|
|
{ "TPOS", 4, offsetof(struct mp3entry, disc_string), &parsediscnum, false },
|
2006-07-20 10:35:20 +00:00
|
|
|
|
{ "TRCK", 4, offsetof(struct mp3entry, track_string), &parsetracknum, false },
|
|
|
|
|
{ "TDRC", 4, offsetof(struct mp3entry, year_string), &parseyearnum, false },
|
|
|
|
|
{ "TYER", 4, offsetof(struct mp3entry, year_string), &parseyearnum, false },
|
|
|
|
|
{ "TYE", 3, offsetof(struct mp3entry, year_string), &parseyearnum, false },
|
|
|
|
|
{ "TCOM", 4, offsetof(struct mp3entry, composer), NULL, false },
|
2007-02-10 12:09:28 +00:00
|
|
|
|
{ "TPE2", 4, offsetof(struct mp3entry, albumartist), NULL, false },
|
2007-08-08 10:19:56 +00:00
|
|
|
|
{ "TP2", 3, offsetof(struct mp3entry, albumartist), NULL, false },
|
|
|
|
|
{ "TIT1", 4, offsetof(struct mp3entry, grouping), NULL, false },
|
|
|
|
|
{ "TT1", 3, offsetof(struct mp3entry, grouping), NULL, false },
|
2007-02-17 08:46:32 +00:00
|
|
|
|
{ "COMM", 4, offsetof(struct mp3entry, comment), NULL, false },
|
2006-07-20 10:35:20 +00:00
|
|
|
|
{ "TCON", 4, offsetof(struct mp3entry, genre_string), &parsegenre, false },
|
|
|
|
|
{ "TCO", 3, offsetof(struct mp3entry, genre_string), &parsegenre, false },
|
|
|
|
|
{ "TXXX", 4, 0, &parseuser, false },
|
2008-10-30 19:00:24 +00:00
|
|
|
|
#if CONFIG_CODEC == SWCODEC
|
2006-07-20 10:35:20 +00:00
|
|
|
|
{ "RVA2", 4, 0, &parserva2, true },
|
2005-07-27 11:54:33 +00:00
|
|
|
|
#endif
|
2008-10-07 18:39:44 +00:00
|
|
|
|
{ "UFID", 4, 0, &parsembtid, false },
|
2003-06-04 15:09:35 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define TAGLIST_SIZE ((int)(sizeof(taglist) / sizeof(taglist[0])))
|
|
|
|
|
|
2007-09-03 16:15:08 +00:00
|
|
|
|
/* Get the length of an ID3 string in the given encoding. Returns the length
|
|
|
|
|
* in bytes, including end nil, or -1 if the encoding is unknown.
|
|
|
|
|
*/
|
|
|
|
|
static int unicode_len(char encoding, const void* string)
|
|
|
|
|
{
|
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
|
|
if (encoding == 0x01 || encoding == 0x02) {
|
2007-09-05 06:48:33 +00:00
|
|
|
|
char first;
|
2007-09-05 06:27:59 +00:00
|
|
|
|
const char *s = string;
|
|
|
|
|
/* string might be unaligned, so using short* can crash on ARM and SH1 */
|
|
|
|
|
do {
|
2007-09-05 06:48:33 +00:00
|
|
|
|
first = *s++;
|
|
|
|
|
} while ((first | *s++) != 0);
|
2007-09-05 06:27:59 +00:00
|
|
|
|
|
|
|
|
|
len = s - (const char*) string;
|
2007-09-03 16:15:08 +00:00
|
|
|
|
} else {
|
|
|
|
|
len = strlen((char*) string) + 1;
|
|
|
|
|
}
|
2007-09-05 06:27:59 +00:00
|
|
|
|
|
2007-09-03 16:15:08 +00:00
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
2002-09-30 18:45:50 +00:00
|
|
|
|
/* Checks to see if the passed in string is a 16-bit wide Unicode v2
|
2005-12-06 13:27:15 +00:00
|
|
|
|
string. If it is, we convert it to a UTF-8 string. If it's not unicode,
|
|
|
|
|
we convert from the default codepage */
|
|
|
|
|
static int unicode_munge(char* string, char* utf8buf, int *len) {
|
|
|
|
|
long tmp;
|
|
|
|
|
bool le = false;
|
|
|
|
|
int i = 0;
|
2006-11-10 08:03:33 +00:00
|
|
|
|
unsigned char *str = (unsigned char *)string;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
int templen = 0;
|
2006-11-10 08:03:33 +00:00
|
|
|
|
unsigned char* utf8 = (unsigned char *)utf8buf;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
|
|
switch (str[0]) {
|
|
|
|
|
case 0x00: /* Type 0x00 is ordinary ISO 8859-1 */
|
|
|
|
|
str++;
|
|
|
|
|
(*len)--;
|
|
|
|
|
utf8 = iso_decode(str, utf8, -1, *len);
|
|
|
|
|
*utf8 = 0;
|
2006-11-10 08:03:33 +00:00
|
|
|
|
*len = (unsigned long)utf8 - (unsigned long)utf8buf;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
break;
|
2002-10-01 13:20:23 +00:00
|
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
|
case 0x01: /* Unicode with or without BOM */
|
|
|
|
|
case 0x02:
|
|
|
|
|
(*len)--;
|
|
|
|
|
str++;
|
|
|
|
|
|
|
|
|
|
/* Handle frames with more than one string
|
|
|
|
|
(needed for TXXX frames).*/
|
|
|
|
|
do {
|
2007-02-15 22:55:22 +00:00
|
|
|
|
tmp = bytes2int(0, 0, str[0], str[1]);
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
|
|
/* Now check if there is a BOM
|
|
|
|
|
(zero-width non-breaking space, 0xfeff)
|
|
|
|
|
and if it is in little or big endian format */
|
|
|
|
|
if(tmp == 0xfffe) { /* Little endian? */
|
|
|
|
|
le = true;
|
|
|
|
|
str += 2;
|
|
|
|
|
(*len)-=2;
|
|
|
|
|
} else if(tmp == 0xfeff) { /* Big endian? */
|
|
|
|
|
str += 2;
|
|
|
|
|
(*len)-=2;
|
|
|
|
|
} else
|
|
|
|
|
/* If there is no BOM (which is a specification violation),
|
|
|
|
|
let's try to guess it. If one of the bytes is 0x00, it is
|
|
|
|
|
probably the most significant one. */
|
|
|
|
|
if(str[1] == 0)
|
|
|
|
|
le = true;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
if(le)
|
|
|
|
|
utf8 = utf16LEdecode(str, utf8, 1);
|
|
|
|
|
else
|
|
|
|
|
utf8 = utf16BEdecode(str, utf8, 1);
|
|
|
|
|
|
|
|
|
|
str+=2;
|
|
|
|
|
i += 2;
|
|
|
|
|
} while((str[0] || str[1]) && (i < *len));
|
|
|
|
|
|
|
|
|
|
*utf8++ = 0; /* Terminate the string */
|
|
|
|
|
templen += (strlen(&utf8buf[templen]) + 1);
|
|
|
|
|
str += 2;
|
|
|
|
|
i+=2;
|
|
|
|
|
} while(i < *len);
|
|
|
|
|
*len = templen - 1;
|
|
|
|
|
break;
|
2004-01-09 00:47:26 +00:00
|
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
|
case 0x03: /* UTF-8 encoded string */
|
|
|
|
|
for(i=0; i < *len; i++)
|
|
|
|
|
utf8[i] = str[i+1];
|
2005-12-07 22:59:09 +00:00
|
|
|
|
(*len)--;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
break;
|
2002-10-01 13:20:23 +00:00
|
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
|
default: /* Plain old string */
|
|
|
|
|
utf8 = iso_decode(str, utf8, -1, *len);
|
|
|
|
|
*utf8 = 0;
|
2006-11-10 08:03:33 +00:00
|
|
|
|
*len = (unsigned long)utf8 - (unsigned long)utf8buf;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2002-09-30 18:45:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-04-23 08:43:04 +00:00
|
|
|
|
/*
|
|
|
|
|
* Sets the title of an MP3 entry based on its ID3v1 tag.
|
|
|
|
|
*
|
|
|
|
|
* Arguments: file - the MP3 file to scen for a ID3v1 tag
|
|
|
|
|
* entry - the entry to set the title in
|
|
|
|
|
*
|
2002-05-13 12:29:34 +00:00
|
|
|
|
* Returns: true if a title was found and created, else false
|
2002-04-23 08:43:04 +00:00
|
|
|
|
*/
|
2006-07-20 10:35:20 +00:00
|
|
|
|
static bool setid3v1title(int fd, struct mp3entry *entry)
|
2002-04-23 08:43:04 +00:00
|
|
|
|
{
|
2002-12-05 14:55:14 +00:00
|
|
|
|
unsigned char buffer[128];
|
2007-02-17 08:46:32 +00:00
|
|
|
|
static const char offsets[] = {3, 33, 63, 97, 93, 125, 127};
|
2002-12-05 14:55:14 +00:00
|
|
|
|
int i, j;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
unsigned char* utf8;
|
2002-12-05 14:55:14 +00:00
|
|
|
|
|
|
|
|
|
if (-1 == lseek(fd, -128, SEEK_END))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (read(fd, buffer, sizeof buffer) != sizeof buffer)
|
|
|
|
|
return false;
|
2002-04-23 08:43:04 +00:00
|
|
|
|
|
2005-11-25 00:10:12 +00:00
|
|
|
|
if (strncmp((char *)buffer, "TAG", 3))
|
2002-12-05 14:55:14 +00:00
|
|
|
|
return false;
|
2002-04-23 08:43:04 +00:00
|
|
|
|
|
2003-03-10 14:55:31 +00:00
|
|
|
|
entry->id3v1len = 128;
|
2002-12-09 15:01:37 +00:00
|
|
|
|
entry->id3version = ID3_VER_1_0;
|
|
|
|
|
|
2002-12-05 15:21:18 +00:00
|
|
|
|
for (i=0; i < (int)sizeof offsets; i++) {
|
2005-12-06 13:27:15 +00:00
|
|
|
|
unsigned char* ptr = (unsigned char *)buffer + offsets[i];
|
|
|
|
|
|
2002-12-05 14:55:14 +00:00
|
|
|
|
switch(i) {
|
|
|
|
|
case 0:
|
|
|
|
|
case 1:
|
|
|
|
|
case 2:
|
2007-02-17 08:46:32 +00:00
|
|
|
|
/* kill trailing space in strings */
|
|
|
|
|
for (j=29; j && (ptr[j]==0 || ptr[j]==' '); j--)
|
|
|
|
|
ptr[j] = 0;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
/* convert string to utf8 */
|
2006-11-10 08:03:33 +00:00
|
|
|
|
utf8 = (unsigned char *)entry->id3v1buf[i];
|
2005-12-06 13:27:15 +00:00
|
|
|
|
utf8 = iso_decode(ptr, utf8, -1, 30);
|
|
|
|
|
/* make sure string is terminated */
|
|
|
|
|
*utf8 = 0;
|
2002-12-05 14:55:14 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 3:
|
2007-02-17 08:46:32 +00:00
|
|
|
|
/* kill trailing space in strings */
|
|
|
|
|
for (j=27; j && (ptr[j]==0 || ptr[j]==' '); j--)
|
|
|
|
|
ptr[j] = 0;
|
|
|
|
|
/* convert string to utf8 */
|
|
|
|
|
utf8 = (unsigned char *)entry->id3v1buf[3];
|
|
|
|
|
utf8 = iso_decode(ptr, utf8, -1, 28);
|
|
|
|
|
/* make sure string is terminated */
|
|
|
|
|
*utf8 = 0;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 4:
|
2002-12-05 14:55:14 +00:00
|
|
|
|
ptr[4] = 0;
|
2006-11-10 08:03:33 +00:00
|
|
|
|
entry->year = atoi((char *)ptr);
|
2002-12-05 14:55:14 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2007-02-17 08:46:32 +00:00
|
|
|
|
case 5:
|
2002-12-05 14:55:14 +00:00
|
|
|
|
/* id3v1.1 uses last two bytes of comment field for track
|
|
|
|
|
number: first must be 0 and second is track num */
|
2002-12-09 15:01:37 +00:00
|
|
|
|
if (!ptr[0] && ptr[1]) {
|
2002-12-05 14:55:14 +00:00
|
|
|
|
entry->tracknum = ptr[1];
|
2002-12-09 15:01:37 +00:00
|
|
|
|
entry->id3version = ID3_VER_1_1;
|
|
|
|
|
}
|
2002-12-05 14:55:14 +00:00
|
|
|
|
break;
|
2002-12-05 15:21:18 +00:00
|
|
|
|
|
2007-02-17 08:46:32 +00:00
|
|
|
|
case 6:
|
2002-12-05 15:21:18 +00:00
|
|
|
|
/* genre */
|
2007-03-02 21:49:42 +00:00
|
|
|
|
entry->genre_string = id3_get_num_genre(ptr[0]);
|
2002-12-05 15:21:18 +00:00
|
|
|
|
break;
|
2002-04-23 08:43:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
|
entry->title = entry->id3v1buf[0];
|
|
|
|
|
entry->artist = entry->id3v1buf[1];
|
|
|
|
|
entry->album = entry->id3v1buf[2];
|
2007-02-17 08:46:32 +00:00
|
|
|
|
entry->comment = entry->id3v1buf[3];
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
2002-05-13 12:29:34 +00:00
|
|
|
|
return true;
|
2002-04-23 08:43:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Sets the title of an MP3 entry based on its ID3v2 tag.
|
|
|
|
|
*
|
2002-10-29 10:29:57 +00:00
|
|
|
|
* Arguments: file - the MP3 file to scan for a ID3v2 tag
|
2002-04-23 08:43:04 +00:00
|
|
|
|
* entry - the entry to set the title in
|
|
|
|
|
*
|
2002-05-13 12:29:34 +00:00
|
|
|
|
* Returns: true if a title was found and created, else false
|
2002-04-23 08:43:04 +00:00
|
|
|
|
*/
|
2006-07-20 10:35:20 +00:00
|
|
|
|
static void setid3v2title(int fd, struct mp3entry *entry)
|
2002-04-23 08:43:04 +00:00
|
|
|
|
{
|
2002-08-30 22:01:35 +00:00
|
|
|
|
int minframesize;
|
2002-04-30 12:42:37 +00:00
|
|
|
|
int size;
|
2005-02-09 10:56:54 +00:00
|
|
|
|
long bufferpos = 0, totframelen, framelen;
|
2002-04-23 08:43:04 +00:00
|
|
|
|
char header[10];
|
2003-10-22 13:06:31 +00:00
|
|
|
|
char tmp[4];
|
2002-12-09 15:01:37 +00:00
|
|
|
|
unsigned char version;
|
2002-06-04 22:05:00 +00:00
|
|
|
|
char *buffer = entry->id3v2buf;
|
2002-10-28 23:00:57 +00:00
|
|
|
|
int bytesread = 0;
|
|
|
|
|
int buffersize = sizeof(entry->id3v2buf);
|
2003-10-22 13:06:31 +00:00
|
|
|
|
unsigned char global_flags;
|
2003-03-10 14:55:31 +00:00
|
|
|
|
int flags;
|
|
|
|
|
int skip;
|
2003-10-22 13:06:31 +00:00
|
|
|
|
bool global_unsynch = false;
|
|
|
|
|
bool unsynch = false;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
int i, j;
|
2004-01-04 00:37:30 +00:00
|
|
|
|
int rc;
|
2003-06-04 15:09:35 +00:00
|
|
|
|
|
2004-01-04 00:45:16 +00:00
|
|
|
|
global_ff_found = false;
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2002-10-28 23:00:57 +00:00
|
|
|
|
/* Bail out if the tag is shorter than 10 bytes */
|
2002-04-23 08:43:04 +00:00
|
|
|
|
if(entry->id3v2len < 10)
|
|
|
|
|
return;
|
|
|
|
|
|
2002-10-28 23:00:57 +00:00
|
|
|
|
/* Read the ID3 tag version from the header */
|
2002-04-30 12:42:37 +00:00
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
2002-05-08 07:10:16 +00:00
|
|
|
|
if(10 != read(fd, header, 10))
|
|
|
|
|
return;
|
|
|
|
|
|
2002-10-28 23:00:57 +00:00
|
|
|
|
/* Get the total ID3 tag size */
|
2002-04-23 08:43:04 +00:00
|
|
|
|
size = entry->id3v2len - 10;
|
2002-04-23 11:46:16 +00:00
|
|
|
|
|
2002-12-09 15:01:37 +00:00
|
|
|
|
version = header[3];
|
|
|
|
|
switch ( version ) {
|
|
|
|
|
case 2:
|
2002-12-12 15:20:37 +00:00
|
|
|
|
version = ID3_VER_2_2;
|
2002-12-09 15:01:37 +00:00
|
|
|
|
minframesize = 8;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 3:
|
2002-12-12 15:20:37 +00:00
|
|
|
|
version = ID3_VER_2_3;
|
2002-12-09 15:01:37 +00:00
|
|
|
|
minframesize = 12;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 4:
|
2002-12-12 15:20:37 +00:00
|
|
|
|
version = ID3_VER_2_4;
|
2002-12-09 15:01:37 +00:00
|
|
|
|
minframesize = 12;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
/* unsupported id3 version */
|
|
|
|
|
return;
|
|
|
|
|
}
|
2002-12-12 15:20:37 +00:00
|
|
|
|
entry->id3version = version;
|
2007-08-03 10:00:42 +00:00
|
|
|
|
entry->tracknum = entry->year = entry->discnum = 0;
|
2007-03-02 21:49:42 +00:00
|
|
|
|
entry->title = entry->artist = entry->album = NULL; /* FIXME incomplete */
|
2003-06-04 15:09:35 +00:00
|
|
|
|
|
2003-10-22 13:06:31 +00:00
|
|
|
|
global_flags = header[5];
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2003-03-10 14:55:31 +00:00
|
|
|
|
/* Skip the extended header if it is present */
|
2005-08-19 09:43:47 +00:00
|
|
|
|
if(global_flags & 0x40) {
|
|
|
|
|
if(version == ID3_VER_2_3) {
|
|
|
|
|
if(10 != read(fd, header, 10))
|
|
|
|
|
return;
|
2007-03-30 12:55:46 +00:00
|
|
|
|
/* The 2.3 extended header size doesn't include the header size
|
|
|
|
|
field itself. Also, it is not unsynched. */
|
|
|
|
|
framelen =
|
|
|
|
|
bytes2int(header[0], header[1], header[2], header[3]) + 4;
|
2005-08-19 09:43:47 +00:00
|
|
|
|
|
2007-03-30 12:55:46 +00:00
|
|
|
|
/* Skip the rest of the header */
|
2005-08-19 09:43:47 +00:00
|
|
|
|
lseek(fd, framelen - 10, SEEK_CUR);
|
|
|
|
|
}
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2005-08-19 09:43:47 +00:00
|
|
|
|
if(version >= ID3_VER_2_4) {
|
2003-03-10 14:55:31 +00:00
|
|
|
|
if(4 != read(fd, header, 4))
|
|
|
|
|
return;
|
2005-08-19 09:43:47 +00:00
|
|
|
|
|
|
|
|
|
/* The 2.4 extended header size does include the entire header,
|
|
|
|
|
so here we can just skip it. This header is unsynched. */
|
2007-02-15 22:55:22 +00:00
|
|
|
|
framelen = unsync(header[0], header[1],
|
2003-03-10 14:55:31 +00:00
|
|
|
|
header[2], header[3]);
|
|
|
|
|
|
|
|
|
|
lseek(fd, framelen - 4, SEEK_CUR);
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-10-22 13:06:31 +00:00
|
|
|
|
|
|
|
|
|
/* Is unsynchronization applied? */
|
|
|
|
|
if(global_flags & 0x80) {
|
|
|
|
|
global_unsynch = true;
|
|
|
|
|
}
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* We must have at least minframesize bytes left for the
|
|
|
|
|
* remaining frames to be interesting
|
2002-04-23 08:43:04 +00:00
|
|
|
|
*/
|
2005-08-20 18:19:56 +00:00
|
|
|
|
while (size >= minframesize && bufferpos < buffersize - 1) {
|
2003-03-10 14:55:31 +00:00
|
|
|
|
flags = 0;
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2002-04-23 08:43:04 +00:00
|
|
|
|
/* Read frame header and check length */
|
2002-12-12 15:20:37 +00:00
|
|
|
|
if(version >= ID3_VER_2_3) {
|
2004-01-04 00:37:30 +00:00
|
|
|
|
if(global_unsynch && version <= ID3_VER_2_3)
|
2004-01-04 00:45:16 +00:00
|
|
|
|
rc = read_unsynched(fd, header, 10);
|
2004-01-04 00:37:30 +00:00
|
|
|
|
else
|
|
|
|
|
rc = read(fd, header, 10);
|
|
|
|
|
if(rc != 10)
|
2002-10-28 23:00:57 +00:00
|
|
|
|
return;
|
|
|
|
|
/* Adjust for the 10 bytes we read */
|
|
|
|
|
size -= 10;
|
2003-03-10 14:55:31 +00:00
|
|
|
|
|
2007-02-15 22:55:22 +00:00
|
|
|
|
flags = bytes2int(0, 0, header[8], header[9]);
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2002-12-12 15:20:37 +00:00
|
|
|
|
if (version >= ID3_VER_2_4) {
|
2007-02-15 22:55:22 +00:00
|
|
|
|
framelen = unsync(header[4], header[5],
|
2002-10-28 23:00:57 +00:00
|
|
|
|
header[6], header[7]);
|
2002-09-30 18:41:59 +00:00
|
|
|
|
} else {
|
|
|
|
|
/* version .3 files don't use synchsafe ints for
|
|
|
|
|
* size */
|
2007-02-15 22:55:22 +00:00
|
|
|
|
framelen = bytes2int(header[4], header[5],
|
2002-10-28 23:00:57 +00:00
|
|
|
|
header[6], header[7]);
|
2002-09-30 18:41:59 +00:00
|
|
|
|
}
|
2002-04-23 08:43:04 +00:00
|
|
|
|
} else {
|
2002-10-28 23:00:57 +00:00
|
|
|
|
if(6 != read(fd, header, 6))
|
|
|
|
|
return;
|
|
|
|
|
/* Adjust for the 6 bytes we read */
|
|
|
|
|
size -= 6;
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2007-02-15 22:55:22 +00:00
|
|
|
|
framelen = bytes2int(0, header[3], header[4], header[5]);
|
2002-04-23 08:43:04 +00:00
|
|
|
|
}
|
2002-08-30 22:01:35 +00:00
|
|
|
|
|
2008-11-04 19:47:52 +00:00
|
|
|
|
logf("framelen = %ld, flags = 0x%04x", framelen, flags);
|
2005-08-30 13:01:44 +00:00
|
|
|
|
if(framelen == 0){
|
|
|
|
|
if (header[0] == 0 && header[1] == 0 && header[2] == 0)
|
2006-07-20 10:35:20 +00:00
|
|
|
|
return;
|
2005-08-30 13:01:44 +00:00
|
|
|
|
else
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2002-10-28 23:00:57 +00:00
|
|
|
|
|
2003-10-22 13:06:31 +00:00
|
|
|
|
unsynch = false;
|
|
|
|
|
|
2003-03-10 14:55:31 +00:00
|
|
|
|
if(flags)
|
|
|
|
|
{
|
|
|
|
|
skip = 0;
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2004-03-30 13:57:06 +00:00
|
|
|
|
if (version >= ID3_VER_2_4) {
|
|
|
|
|
if(flags & 0x0040) { /* Grouping identity */
|
|
|
|
|
lseek(fd, 1, SEEK_CUR); /* Skip 1 byte */
|
|
|
|
|
framelen--;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if(flags & 0x0020) { /* Grouping identity */
|
|
|
|
|
lseek(fd, 1, SEEK_CUR); /* Skip 1 byte */
|
|
|
|
|
framelen--;
|
|
|
|
|
}
|
2003-10-22 13:06:31 +00:00
|
|
|
|
}
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2003-03-10 15:32:59 +00:00
|
|
|
|
if(flags & 0x000c) /* Compression or encryption */
|
2003-03-10 14:55:31 +00:00
|
|
|
|
{
|
2008-01-06 19:49:25 +00:00
|
|
|
|
/* Skip it */
|
|
|
|
|
size -= framelen;
|
|
|
|
|
lseek(fd, framelen, SEEK_CUR);
|
2003-03-10 14:55:31 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2003-10-22 13:06:31 +00:00
|
|
|
|
if(flags & 0x0002) /* Unsynchronization */
|
|
|
|
|
unsynch = true;
|
2003-03-10 14:55:31 +00:00
|
|
|
|
|
2004-03-30 13:57:06 +00:00
|
|
|
|
if (version >= ID3_VER_2_4) {
|
|
|
|
|
if(flags & 0x0001) { /* Data length indicator */
|
|
|
|
|
if(4 != read(fd, tmp, 4))
|
|
|
|
|
return;
|
2003-10-22 13:06:31 +00:00
|
|
|
|
|
2008-08-17 12:31:32 +00:00
|
|
|
|
/* We don't need the data length */
|
2004-03-30 13:57:06 +00:00
|
|
|
|
framelen -= 4;
|
|
|
|
|
}
|
2003-03-10 14:55:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2008-11-04 19:47:52 +00:00
|
|
|
|
|
|
|
|
|
if (framelen == 0)
|
|
|
|
|
continue;
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2008-11-04 19:47:52 +00:00
|
|
|
|
if (framelen < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2008-01-06 19:49:25 +00:00
|
|
|
|
/* Keep track of the remaining frame size */
|
|
|
|
|
totframelen = framelen;
|
|
|
|
|
|
2002-10-28 23:00:57 +00:00
|
|
|
|
/* If the frame is larger than the remaining buffer space we try
|
|
|
|
|
to read as much as would fit in the buffer */
|
|
|
|
|
if(framelen >= buffersize - bufferpos)
|
|
|
|
|
framelen = buffersize - bufferpos - 1;
|
2002-10-29 10:29:57 +00:00
|
|
|
|
|
2007-10-29 18:02:41 +00:00
|
|
|
|
logf("id3v2 frame: %.4s", header);
|
2002-12-05 14:55:14 +00:00
|
|
|
|
|
2003-06-04 15:09:35 +00:00
|
|
|
|
/* Check for certain frame headers
|
2003-03-18 01:31:35 +00:00
|
|
|
|
|
2003-06-04 15:09:35 +00:00
|
|
|
|
'size' is the amount of frame bytes remaining. We decrement it by
|
|
|
|
|
the amount of bytes we read. If we fail to read as many bytes as
|
|
|
|
|
we expect, we assume that we can't read from this file, and bail
|
|
|
|
|
out.
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2003-06-04 15:09:35 +00:00
|
|
|
|
For each frame. we will iterate over the list of supported tags,
|
|
|
|
|
and read the tag into entry's buffer. All tags will be kept as
|
|
|
|
|
strings, for cases where a number won't do, e.g., YEAR: "circa
|
|
|
|
|
1765", "1790/1977" (composed/performed), "28 Feb 1969" TRACK:
|
|
|
|
|
"1/12", "1 of 12", GENRE: "Freeform genre name" Text is more
|
|
|
|
|
flexible, and as the main use of id3 data is to display it,
|
|
|
|
|
converting it to an int just means reconverting to display it, at a
|
|
|
|
|
runtime cost.
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2003-06-04 15:09:35 +00:00
|
|
|
|
For tags that the current code does convert to ints, a post
|
|
|
|
|
processing function will be called via a pointer to function. */
|
|
|
|
|
|
|
|
|
|
for (i=0; i<TAGLIST_SIZE; i++) {
|
2004-08-03 19:22:56 +00:00
|
|
|
|
const struct tag_resolver* tr = &taglist[i];
|
2005-07-27 11:54:33 +00:00
|
|
|
|
char** ptag = tr->offset ? (char**) (((char*)entry) + tr->offset)
|
|
|
|
|
: NULL;
|
2003-06-04 15:09:35 +00:00
|
|
|
|
char* tag;
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2005-08-11 19:59:47 +00:00
|
|
|
|
/* Only ID3_VER_2_2 uses frames with three-character names. */
|
|
|
|
|
if (((version == ID3_VER_2_2) && (tr->tag_length != 3))
|
|
|
|
|
|| ((version > ID3_VER_2_2) && (tr->tag_length != 4))) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Note that parser functions sometimes set *ptag to NULL, so
|
|
|
|
|
* the "!*ptag" check here doesn't always have the desired
|
|
|
|
|
* effect. Should the parser functions (parsegenre in
|
|
|
|
|
* particular) be updated to handle the case of being called
|
|
|
|
|
* multiple times, or should the "*ptag" check be removed?
|
|
|
|
|
*/
|
2006-07-20 10:35:20 +00:00
|
|
|
|
if( (!ptag || !*ptag) && !memcmp( header, tr->tag, tr->tag_length ) ) {
|
|
|
|
|
|
2003-06-04 15:09:35 +00:00
|
|
|
|
/* found a tag matching one in tagList, and not yet filled */
|
2005-07-27 11:54:33 +00:00
|
|
|
|
tag = buffer + bufferpos;
|
|
|
|
|
|
2004-01-04 00:37:30 +00:00
|
|
|
|
if(global_unsynch && version <= ID3_VER_2_3)
|
2005-07-27 11:54:33 +00:00
|
|
|
|
bytesread = read_unsynched(fd, tag, framelen);
|
2004-01-04 00:37:30 +00:00
|
|
|
|
else
|
2005-07-27 11:54:33 +00:00
|
|
|
|
bytesread = read(fd, tag, framelen);
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2003-06-04 15:09:35 +00:00
|
|
|
|
if( bytesread != framelen )
|
|
|
|
|
return;
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2003-06-04 15:09:35 +00:00
|
|
|
|
size -= bytesread;
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2004-01-04 00:37:30 +00:00
|
|
|
|
if(unsynch || (global_unsynch && version >= ID3_VER_2_4))
|
2005-07-27 11:54:33 +00:00
|
|
|
|
bytesread = unsynchronize_frame(tag, bytesread);
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
2007-02-17 08:46:32 +00:00
|
|
|
|
/* the COMM frame has a 3 char field to hold an ISO-639-1
|
|
|
|
|
* language string and an optional short description;
|
|
|
|
|
* remove them so unicode_munge can work correctly
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if(!memcmp( header, "COMM", 4 )) {
|
2007-09-03 16:15:08 +00:00
|
|
|
|
int offset;
|
|
|
|
|
/* ignore comments with iTunes 7 soundcheck/gapless data */
|
2007-09-04 08:23:12 +00:00
|
|
|
|
if(!strncmp(tag+4, "iTun", 4))
|
2007-08-12 14:13:33 +00:00
|
|
|
|
break;
|
2007-09-03 16:15:08 +00:00
|
|
|
|
offset = 3 + unicode_len(*tag, tag + 4);
|
|
|
|
|
if(bytesread > offset) {
|
|
|
|
|
bytesread -= offset;
|
|
|
|
|
memmove(tag + 1, tag + 1 + offset, bytesread - 1);
|
|
|
|
|
}
|
2007-02-17 08:46:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-07-20 10:35:20 +00:00
|
|
|
|
/* Attempt to parse Unicode string only if the tag contents
|
|
|
|
|
aren't binary */
|
|
|
|
|
if(!tr->binary) {
|
|
|
|
|
/* UTF-8 could potentially be 3 times larger */
|
|
|
|
|
/* so we need to create a new buffer */
|
|
|
|
|
char utf8buf[(3 * bytesread) + 1];
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
2006-07-20 10:35:20 +00:00
|
|
|
|
unicode_munge( tag, utf8buf, &bytesread );
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
2006-07-20 10:35:20 +00:00
|
|
|
|
if(bytesread >= buffersize - bufferpos)
|
|
|
|
|
bytesread = buffersize - bufferpos - 1;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
2006-07-20 10:35:20 +00:00
|
|
|
|
for (j = 0; j < bytesread; j++)
|
|
|
|
|
tag[j] = utf8buf[j];
|
2005-07-27 11:54:33 +00:00
|
|
|
|
|
2006-07-20 10:35:20 +00:00
|
|
|
|
/* remove trailing spaces */
|
|
|
|
|
while ( bytesread > 0 && isspace(tag[bytesread-1]))
|
|
|
|
|
bytesread--;
|
|
|
|
|
}
|
2005-07-27 11:54:33 +00:00
|
|
|
|
|
2005-06-26 22:43:57 +00:00
|
|
|
|
tag[bytesread] = 0;
|
|
|
|
|
bufferpos += bytesread + 1;
|
|
|
|
|
|
2006-07-20 10:35:20 +00:00
|
|
|
|
if (ptag)
|
|
|
|
|
*ptag = tag;
|
|
|
|
|
|
2003-06-04 15:09:35 +00:00
|
|
|
|
if( tr->ppFunc )
|
|
|
|
|
bufferpos = tr->ppFunc(entry, tag, bufferpos);
|
2005-11-11 17:04:10 +00:00
|
|
|
|
|
|
|
|
|
/* Seek to the next frame */
|
|
|
|
|
if(framelen < totframelen)
|
|
|
|
|
lseek(fd, totframelen - framelen, SEEK_CUR);
|
2003-06-04 15:09:35 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2002-12-05 14:55:14 +00:00
|
|
|
|
}
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
2003-06-04 15:09:35 +00:00
|
|
|
|
if( i == TAGLIST_SIZE ) {
|
2004-08-03 19:22:56 +00:00
|
|
|
|
/* no tag in tagList was found, or it was a repeat.
|
2003-06-04 15:09:35 +00:00
|
|
|
|
skip it using the total size */
|
|
|
|
|
|
2004-01-04 00:37:30 +00:00
|
|
|
|
if(global_unsynch && version <= ID3_VER_2_3) {
|
2004-03-13 22:55:47 +00:00
|
|
|
|
size -= skip_unsynched(fd, totframelen);
|
2003-10-22 13:06:31 +00:00
|
|
|
|
} else {
|
|
|
|
|
size -= totframelen;
|
|
|
|
|
if( lseek(fd, totframelen, SEEK_CUR) == -1 )
|
|
|
|
|
return;
|
|
|
|
|
}
|
2002-10-29 10:29:57 +00:00
|
|
|
|
}
|
2002-06-26 21:11:29 +00:00
|
|
|
|
}
|
2002-04-23 08:43:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Calculates the size of the ID3v2 tag.
|
|
|
|
|
*
|
|
|
|
|
* Arguments: file - the file to search for a tag.
|
|
|
|
|
*
|
|
|
|
|
* Returns: the size of the tag or 0 if none was found
|
|
|
|
|
*/
|
2006-07-20 10:35:20 +00:00
|
|
|
|
int getid3v2len(int fd)
|
2002-04-23 08:43:04 +00:00
|
|
|
|
{
|
|
|
|
|
char buf[6];
|
|
|
|
|
int offset;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
2002-04-23 08:43:04 +00:00
|
|
|
|
/* Make sure file has a ID3 tag */
|
2002-04-30 12:42:37 +00:00
|
|
|
|
if((-1 == lseek(fd, 0, SEEK_SET)) ||
|
|
|
|
|
(read(fd, buf, 6) != 6) ||
|
2002-04-23 08:43:04 +00:00
|
|
|
|
(strncmp(buf, "ID3", strlen("ID3")) != 0))
|
|
|
|
|
offset = 0;
|
2002-07-04 22:10:43 +00:00
|
|
|
|
|
2002-04-23 08:43:04 +00:00
|
|
|
|
/* Now check what the ID3v2 size field says */
|
|
|
|
|
else
|
2003-06-04 15:09:35 +00:00
|
|
|
|
if(read(fd, buf, 4) != 4)
|
|
|
|
|
offset = 0;
|
|
|
|
|
else
|
2007-02-15 22:55:22 +00:00
|
|
|
|
offset = unsync(buf[0], buf[1], buf[2], buf[3]) + 10;
|
2002-07-04 22:10:43 +00:00
|
|
|
|
|
2007-10-29 18:02:41 +00:00
|
|
|
|
logf("ID3V2 Length: 0x%x", offset);
|
2002-04-23 08:43:04 +00:00
|
|
|
|
return offset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2002-08-19 14:15:33 +00:00
|
|
|
|
* Calculates the length (in milliseconds) of an MP3 file.
|
2002-04-23 08:43:04 +00:00
|
|
|
|
*
|
|
|
|
|
* Modified to only use integers.
|
|
|
|
|
*
|
|
|
|
|
* Arguments: file - the file to calculate the length upon
|
|
|
|
|
* entry - the entry to update with the length
|
|
|
|
|
*
|
2006-07-20 10:35:20 +00:00
|
|
|
|
* Returns: the song length in milliseconds,
|
2002-08-22 07:59:31 +00:00
|
|
|
|
* 0 means that it couldn't be calculated
|
2002-04-23 08:43:04 +00:00
|
|
|
|
*/
|
2002-07-04 22:10:43 +00:00
|
|
|
|
static int getsonglength(int fd, struct mp3entry *entry)
|
2002-04-23 08:43:04 +00:00
|
|
|
|
{
|
2005-09-10 12:28:16 +00:00
|
|
|
|
unsigned long filetime = 0;
|
2003-03-10 14:55:31 +00:00
|
|
|
|
struct mp3info info;
|
2005-09-10 12:28:16 +00:00
|
|
|
|
long bytecount;
|
|
|
|
|
|
2006-07-20 10:35:20 +00:00
|
|
|
|
/* Start searching after ID3v2 header */
|
2002-04-30 12:42:37 +00:00
|
|
|
|
if(-1 == lseek(fd, entry->id3v2len, SEEK_SET))
|
2002-08-22 07:59:31 +00:00
|
|
|
|
return 0;
|
2002-08-19 14:15:33 +00:00
|
|
|
|
|
2003-03-10 14:55:31 +00:00
|
|
|
|
bytecount = get_mp3file_info(fd, &info);
|
2002-04-23 08:43:04 +00:00
|
|
|
|
|
2007-10-29 18:02:41 +00:00
|
|
|
|
logf("Space between ID3V2 tag and first audio frame: 0x%lx bytes",
|
2003-03-10 14:55:31 +00:00
|
|
|
|
bytecount);
|
2002-07-18 02:08:23 +00:00
|
|
|
|
|
2003-03-10 14:55:31 +00:00
|
|
|
|
if(bytecount < 0)
|
2002-07-03 21:04:09 +00:00
|
|
|
|
return -1;
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2003-03-10 14:55:31 +00:00
|
|
|
|
bytecount += entry->id3v2len;
|
2002-04-23 08:43:04 +00:00
|
|
|
|
|
2006-01-08 08:58:58 +00:00
|
|
|
|
/* Validate byte count, in case the file has been edited without
|
|
|
|
|
* updating the header.
|
|
|
|
|
*/
|
|
|
|
|
if (info.byte_count)
|
|
|
|
|
{
|
2006-07-20 10:35:20 +00:00
|
|
|
|
const unsigned long expected = entry->filesize - entry->id3v1len
|
2006-01-08 08:58:58 +00:00
|
|
|
|
- entry->id3v2len;
|
|
|
|
|
const unsigned long diff = MAX(10240, info.byte_count / 20);
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
|
|
|
|
if ((info.byte_count > expected + diff)
|
2006-01-08 08:58:58 +00:00
|
|
|
|
|| (info.byte_count < expected - diff))
|
|
|
|
|
{
|
2007-10-29 18:02:41 +00:00
|
|
|
|
logf("Note: info.byte_count differs from expected value by "
|
|
|
|
|
"%ld bytes", labs((long) (expected - info.byte_count)));
|
2006-01-08 08:58:58 +00:00
|
|
|
|
info.byte_count = 0;
|
|
|
|
|
info.frame_count = 0;
|
|
|
|
|
info.file_time = 0;
|
|
|
|
|
info.enc_padding = 0;
|
|
|
|
|
|
|
|
|
|
/* Even if the bitrate was based on "known bad" values, it
|
|
|
|
|
* should still be better for VBR files than using the bitrate
|
|
|
|
|
* of the first audio frame.
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2003-03-10 14:55:31 +00:00
|
|
|
|
entry->bitrate = info.bitrate;
|
2003-03-13 19:12:05 +00:00
|
|
|
|
entry->frequency = info.frequency;
|
|
|
|
|
entry->version = info.version;
|
|
|
|
|
entry->layer = info.layer;
|
2005-06-18 16:24:27 +00:00
|
|
|
|
switch(entry->layer) {
|
2005-08-29 21:15:27 +00:00
|
|
|
|
#if CONFIG_CODEC==SWCODEC
|
2005-06-18 16:24:27 +00:00
|
|
|
|
case 0:
|
|
|
|
|
entry->codectype=AFMT_MPA_L1;
|
|
|
|
|
break;
|
|
|
|
|
#endif
|
|
|
|
|
case 1:
|
|
|
|
|
entry->codectype=AFMT_MPA_L2;
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
entry->codectype=AFMT_MPA_L3;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2002-07-10 13:17:31 +00:00
|
|
|
|
|
2002-07-03 21:04:09 +00:00
|
|
|
|
/* If the file time hasn't been established, this may be a fixed
|
|
|
|
|
rate MP3, so just use the default formula */
|
2003-03-10 14:55:31 +00:00
|
|
|
|
|
|
|
|
|
filetime = info.file_time;
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2002-07-03 21:04:09 +00:00
|
|
|
|
if(filetime == 0)
|
|
|
|
|
{
|
2006-09-19 18:27:43 +00:00
|
|
|
|
/* Prevent a division by zero */
|
|
|
|
|
if (info.bitrate < 8)
|
|
|
|
|
filetime = 0;
|
|
|
|
|
else
|
|
|
|
|
filetime = (entry->filesize - bytecount) / (info.bitrate / 8);
|
2005-09-09 07:19:41 +00:00
|
|
|
|
/* bitrate is in kbps so this delivers milliseconds. Doing bitrate / 8
|
|
|
|
|
* instead of filesize * 8 is exact, because mpeg audio bitrates are
|
|
|
|
|
* always multiples of 8, and it avoids overflows. */
|
2002-07-03 21:04:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-07-05 19:55:40 +00:00
|
|
|
|
entry->frame_count = info.frame_count;
|
2005-09-09 07:19:41 +00:00
|
|
|
|
|
2003-03-10 14:55:31 +00:00
|
|
|
|
entry->vbr = info.is_vbr;
|
|
|
|
|
entry->has_toc = info.has_toc;
|
2005-07-05 19:55:40 +00:00
|
|
|
|
|
2007-02-17 20:49:54 +00:00
|
|
|
|
#if CONFIG_CODEC==SWCODEC
|
2005-07-05 19:55:40 +00:00
|
|
|
|
entry->lead_trim = info.enc_delay;
|
|
|
|
|
entry->tail_trim = info.enc_padding;
|
2007-02-15 22:55:22 +00:00
|
|
|
|
#endif
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2003-03-10 14:55:31 +00:00
|
|
|
|
memcpy(entry->toc, info.toc, sizeof(info.toc));
|
|
|
|
|
|
2003-03-10 18:25:40 +00:00
|
|
|
|
entry->vbr_header_pos = info.vbr_header_pos;
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2002-10-27 23:07:26 +00:00
|
|
|
|
/* Update the seek point for the first playable frame */
|
|
|
|
|
entry->first_frame_offset = bytecount;
|
2007-10-29 18:02:41 +00:00
|
|
|
|
logf("First frame is at %lx", entry->first_frame_offset);
|
2002-10-27 23:07:26 +00:00
|
|
|
|
|
2002-07-03 21:04:09 +00:00
|
|
|
|
return filetime;
|
2002-04-23 08:43:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Checks all relevant information (such as ID3v1 tag, ID3v2 tag, length etc)
|
|
|
|
|
* about an MP3 file and updates it's entry accordingly.
|
|
|
|
|
*
|
2006-10-25 16:57:53 +00:00
|
|
|
|
Note, that this returns true for successful, false for error! */
|
2007-09-19 10:40:55 +00:00
|
|
|
|
bool get_mp3_metadata(int fd, struct mp3entry *entry, const char *filename)
|
2002-04-23 08:43:04 +00:00
|
|
|
|
{
|
2005-08-29 21:15:27 +00:00
|
|
|
|
#if CONFIG_CODEC != SWCODEC
|
2002-06-25 11:39:22 +00:00
|
|
|
|
memset(entry, 0, sizeof(struct mp3entry));
|
2005-06-19 08:29:23 +00:00
|
|
|
|
#endif
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
2002-06-27 08:33:58 +00:00
|
|
|
|
strncpy(entry->path, filename, sizeof(entry->path));
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2002-04-23 08:43:04 +00:00
|
|
|
|
entry->title = NULL;
|
2003-03-18 00:45:27 +00:00
|
|
|
|
entry->filesize = filesize(fd);
|
2002-06-26 14:51:09 +00:00
|
|
|
|
entry->id3v2len = getid3v2len(fd);
|
2002-06-26 21:11:29 +00:00
|
|
|
|
entry->tracknum = 0;
|
2007-08-03 10:00:42 +00:00
|
|
|
|
entry->discnum = 0;
|
2002-06-26 21:11:29 +00:00
|
|
|
|
|
2007-09-19 10:40:55 +00:00
|
|
|
|
if (entry->id3v2len)
|
2002-04-30 12:42:37 +00:00
|
|
|
|
setid3v2title(fd, entry);
|
2007-08-14 11:56:14 +00:00
|
|
|
|
int len = getsonglength(fd, entry);
|
|
|
|
|
if (len < 0)
|
|
|
|
|
return false;
|
|
|
|
|
entry->length = len;
|
2002-04-23 08:43:04 +00:00
|
|
|
|
|
2003-03-10 14:55:31 +00:00
|
|
|
|
/* Subtract the meta information from the file size to get
|
|
|
|
|
the true size of the MP3 stream */
|
|
|
|
|
entry->filesize -= entry->first_frame_offset;
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2007-09-19 10:40:55 +00:00
|
|
|
|
/* only seek to end of file if no id3v2 tags were found */
|
|
|
|
|
if (!entry->id3v2len) {
|
2004-07-27 14:10:48 +00:00
|
|
|
|
setid3v1title(fd, entry);
|
2002-12-03 15:38:39 +00:00
|
|
|
|
}
|
2002-04-30 12:42:37 +00:00
|
|
|
|
|
2002-08-22 07:59:31 +00:00
|
|
|
|
if(!entry->length || (entry->filesize < 8 ))
|
|
|
|
|
/* no song length or less than 8 bytes is hereby considered to be an
|
|
|
|
|
invalid mp3 and won't be played by us! */
|
2006-10-25 16:57:53 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-23 08:43:04 +00:00
|
|
|
|
#ifdef DEBUG_STANDALONE
|
|
|
|
|
|
2002-04-23 11:46:16 +00:00
|
|
|
|
char *secs2str(int ms)
|
|
|
|
|
{
|
|
|
|
|
static char buffer[32];
|
|
|
|
|
int secs = ms/1000;
|
|
|
|
|
ms %= 1000;
|
2002-05-02 11:44:15 +00:00
|
|
|
|
snprintf(buffer, sizeof(buffer), "%d:%02d.%d", secs/60, secs%60, ms/100);
|
2002-04-23 11:46:16 +00:00
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-23 08:43:04 +00:00
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
2002-04-23 11:46:16 +00:00
|
|
|
|
int i;
|
|
|
|
|
for(i=1; i<argc; i++) {
|
2002-06-25 11:39:22 +00:00
|
|
|
|
struct mp3entry mp3;
|
2003-06-04 15:09:35 +00:00
|
|
|
|
mp3.album = "Bogus";
|
2004-07-27 14:10:48 +00:00
|
|
|
|
if(mp3info(&mp3, argv[i], false)) {
|
2002-04-23 11:46:16 +00:00
|
|
|
|
printf("Failed to get %s\n", argv[i]);
|
2002-04-23 08:43:04 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-23 11:46:16 +00:00
|
|
|
|
printf("****** File: %s\n"
|
|
|
|
|
" Title: %s\n"
|
|
|
|
|
" Artist: %s\n"
|
|
|
|
|
" Album: %s\n"
|
2006-07-20 10:35:20 +00:00
|
|
|
|
" Genre: %s (%d) \n"
|
|
|
|
|
" Composer: %s\n"
|
2003-06-04 15:09:35 +00:00
|
|
|
|
" Year: %s (%d)\n"
|
2006-07-20 10:35:20 +00:00
|
|
|
|
" Track: %s (%d)\n"
|
2002-04-26 08:56:43 +00:00
|
|
|
|
" Length: %s / %d s\n"
|
2002-04-23 11:46:16 +00:00
|
|
|
|
" Bitrate: %d\n"
|
|
|
|
|
" Frequency: %d\n",
|
|
|
|
|
argv[i],
|
2002-04-23 08:43:04 +00:00
|
|
|
|
mp3.title?mp3.title:"<blank>",
|
|
|
|
|
mp3.artist?mp3.artist:"<blank>",
|
|
|
|
|
mp3.album?mp3.album:"<blank>",
|
2003-06-04 15:09:35 +00:00
|
|
|
|
mp3.genre_string?mp3.genre_string:"<blank>",
|
|
|
|
|
mp3.genre,
|
|
|
|
|
mp3.composer?mp3.composer:"<blank>",
|
|
|
|
|
mp3.year_string?mp3.year_string:"<blank>",
|
|
|
|
|
mp3.year,
|
|
|
|
|
mp3.track_string?mp3.track_string:"<blank>",
|
|
|
|
|
mp3.tracknum,
|
2002-04-23 11:46:16 +00:00
|
|
|
|
secs2str(mp3.length),
|
2002-04-26 08:56:43 +00:00
|
|
|
|
mp3.length/1000,
|
2002-04-23 08:43:04 +00:00
|
|
|
|
mp3.bitrate,
|
|
|
|
|
mp3.frequency);
|
|
|
|
|
}
|
2006-07-20 10:35:20 +00:00
|
|
|
|
|
2002-04-23 08:43:04 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|