15c0a5059bac7208006775dabaaa74e8dee2a62d
[kugel-rb.git] / apps / metadata / mp3.c
blob15c0a5059bac7208006775dabaaa74e8dee2a62d
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Daniel Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 * Parts of this code has been stolen from the Ample project and was written
23 * by David H�deman. It has since been extended and enhanced pretty much by
24 * all sorts of friendly Rockbox people.
28 /* tagResolver and associated code copyright 2003 Thomas Paul Diffenbach
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdbool.h>
35 #include "config.h"
36 #include "file.h"
37 #include "logf.h"
39 #include "system.h"
40 #include "metadata.h"
41 #include "mp3data.h"
42 #include "metadata_common.h"
43 #include "metadata_parsers.h"
46 * Calculates the length (in milliseconds) of an MP3 file.
48 * Modified to only use integers.
50 * Arguments: file - the file to calculate the length upon
51 * entry - the entry to update with the length
53 * Returns: the song length in milliseconds,
54 * 0 means that it couldn't be calculated
56 static int getsonglength(int fd, struct mp3entry *entry)
58 unsigned long filetime = 0;
59 struct mp3info info;
60 long bytecount;
62 /* Start searching after ID3v2 header */
63 if(-1 == lseek(fd, entry->id3v2len, SEEK_SET))
64 return 0;
66 bytecount = get_mp3file_info(fd, &info);
68 logf("Space between ID3V2 tag and first audio frame: 0x%lx bytes",
69 bytecount);
71 if(bytecount < 0)
72 return -1;
74 bytecount += entry->id3v2len;
76 /* Validate byte count, in case the file has been edited without
77 * updating the header.
79 if (info.byte_count)
81 const unsigned long expected = entry->filesize - entry->id3v1len
82 - entry->id3v2len;
83 const unsigned long diff = MAX(10240, info.byte_count / 20);
85 if ((info.byte_count > expected + diff)
86 || (info.byte_count < expected - diff))
88 logf("Note: info.byte_count differs from expected value by "
89 "%ld bytes", labs((long) (expected - info.byte_count)));
90 info.byte_count = 0;
91 info.frame_count = 0;
92 info.file_time = 0;
93 info.enc_padding = 0;
95 /* Even if the bitrate was based on "known bad" values, it
96 * should still be better for VBR files than using the bitrate
97 * of the first audio frame.
102 entry->bitrate = info.bitrate;
103 entry->frequency = info.frequency;
104 entry->version = info.version;
105 entry->layer = info.layer;
106 switch(entry->layer) {
107 #if CONFIG_CODEC==SWCODEC
108 case 0:
109 entry->codectype=AFMT_MPA_L1;
110 break;
111 #endif
112 case 1:
113 entry->codectype=AFMT_MPA_L2;
114 break;
115 case 2:
116 entry->codectype=AFMT_MPA_L3;
117 break;
120 /* If the file time hasn't been established, this may be a fixed
121 rate MP3, so just use the default formula */
123 filetime = info.file_time;
125 if(filetime == 0)
127 /* Prevent a division by zero */
128 if (info.bitrate < 8)
129 filetime = 0;
130 else
131 filetime = (entry->filesize - bytecount) / (info.bitrate / 8);
132 /* bitrate is in kbps so this delivers milliseconds. Doing bitrate / 8
133 * instead of filesize * 8 is exact, because mpeg audio bitrates are
134 * always multiples of 8, and it avoids overflows. */
137 entry->frame_count = info.frame_count;
139 entry->vbr = info.is_vbr;
140 entry->has_toc = info.has_toc;
142 #if CONFIG_CODEC==SWCODEC
143 if (!entry->lead_trim)
144 entry->lead_trim = info.enc_delay;
145 if (!entry->tail_trim)
146 entry->tail_trim = info.enc_padding;
147 #endif
149 memcpy(entry->toc, info.toc, sizeof(info.toc));
151 entry->vbr_header_pos = info.vbr_header_pos;
153 /* Update the seek point for the first playable frame */
154 entry->first_frame_offset = bytecount;
155 logf("First frame is at %lx", entry->first_frame_offset);
157 return filetime;
161 * Checks all relevant information (such as ID3v1 tag, ID3v2 tag, length etc)
162 * about an MP3 file and updates it's entry accordingly.
164 Note, that this returns true for successful, false for error! */
165 bool get_mp3_metadata(int fd, struct mp3entry *entry, const char *filename)
167 #if CONFIG_CODEC != SWCODEC
168 memset(entry, 0, sizeof(struct mp3entry));
169 #endif
171 strlcpy(entry->path, filename, sizeof(entry->path));
173 entry->title = NULL;
174 entry->filesize = filesize(fd);
175 entry->id3v2len = getid3v2len(fd);
176 entry->tracknum = 0;
177 entry->discnum = 0;
179 if (entry->id3v2len)
180 setid3v2title(fd, entry);
181 int len = getsonglength(fd, entry);
182 if (len < 0)
183 return false;
184 entry->length = len;
186 /* Subtract the meta information from the file size to get
187 the true size of the MP3 stream */
188 entry->filesize -= entry->first_frame_offset;
190 /* only seek to end of file if no id3v2 tags were found */
191 if (!entry->id3v2len) {
192 setid3v1title(fd, entry);
195 if(!entry->length || (entry->filesize < 8 ))
196 /* no song length or less than 8 bytes is hereby considered to be an
197 invalid mp3 and won't be played by us! */
198 return false;
200 return true;