Add a get_offset API call to make codec_advance_buffer_loc_callback work.
[Rockbox.git] / apps / metadata.c
blob66719754adff2a2e8a040a4ffbf1f15f097fcb9c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
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 ****************************************************************************/
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <inttypes.h>
25 #include "system.h"
26 #include "playback.h"
27 #include "debug.h"
28 #include "logf.h"
29 #include "cuesheet.h"
31 #if CONFIG_CODEC == SWCODEC
33 #include "metadata/metadata_common.h"
34 #include "metadata/metadata_parsers.h"
36 static const unsigned short a52_bitrates[] =
38 32, 40, 48, 56, 64, 80, 96, 112, 128, 160,
39 192, 224, 256, 320, 384, 448, 512, 576, 640
42 /* Only store frame sizes for 44.1KHz - others are simply multiples
43 of the bitrate */
44 static const unsigned short a52_441framesizes[] =
46 69 * 2, 70 * 2, 87 * 2, 88 * 2, 104 * 2, 105 * 2, 121 * 2,
47 122 * 2, 139 * 2, 140 * 2, 174 * 2, 175 * 2, 208 * 2, 209 * 2,
48 243 * 2, 244 * 2, 278 * 2, 279 * 2, 348 * 2, 349 * 2, 417 * 2,
49 418 * 2, 487 * 2, 488 * 2, 557 * 2, 558 * 2, 696 * 2, 697 * 2,
50 835 * 2, 836 * 2, 975 * 2, 976 * 2, 1114 * 2, 1115 * 2, 1253 * 2,
51 1254 * 2, 1393 * 2, 1394 * 2
54 #endif /* CONFIG_CODEC == SWCODEC */
57 /* Simple file type probing by looking at the filename extension. */
58 unsigned int probe_file_format(const char *filename)
60 char *suffix;
61 unsigned int i;
63 suffix = strrchr(filename, '.');
65 if (suffix == NULL)
67 return AFMT_UNKNOWN;
70 /* skip '.' */
71 suffix++;
73 for (i = 1; i < AFMT_NUM_CODECS; i++)
75 /* search extension list for type */
76 const char *ext = audio_formats[i].ext_list;
80 if (strcasecmp(suffix, ext) == 0)
82 return i;
85 ext += strlen(ext) + 1;
87 while (*ext != '\0');
90 return AFMT_UNKNOWN;
93 /* Get metadata for track - return false if parsing showed problems with the
94 * file that would prevent playback.
96 bool get_metadata(struct mp3entry* id3, int fd, const char* trackname)
98 #if CONFIG_CODEC == SWCODEC
99 unsigned char* buf;
100 unsigned long totalsamples;
101 int i;
102 #endif
104 /* Clear the mp3entry to avoid having bogus pointers appear */
105 memset(id3, 0, sizeof(struct mp3entry));
107 /* Take our best guess at the codec type based on file extension */
108 id3->codectype = probe_file_format(trackname);
110 /* Load codec specific track tag information and confirm the codec type. */
111 switch (id3->codectype)
113 case AFMT_MPA_L1:
114 case AFMT_MPA_L2:
115 case AFMT_MPA_L3:
116 if (!get_mp3_metadata(fd, id3, trackname))
118 return false;
121 break;
123 #if CONFIG_CODEC == SWCODEC
124 case AFMT_FLAC:
125 if (!get_flac_metadata(fd, id3))
127 return false;
130 break;
132 case AFMT_WMA:
133 if (!get_asf_metadata(fd, id3))
135 return false;
137 break;
139 case AFMT_APE:
140 if (!get_monkeys_metadata(fd, id3))
142 return false;
144 read_ape_tags(fd, id3);
145 break;
147 case AFMT_MPC:
148 if (!get_musepack_metadata(fd, id3))
149 return false;
150 read_ape_tags(fd, id3);
151 break;
153 case AFMT_OGG_VORBIS:
154 if (!get_vorbis_metadata(fd, id3))/*detects and handles Ogg/Speex files*/
156 return false;
159 break;
161 case AFMT_SPEEX:
162 if (!get_speex_metadata(fd, id3))
164 return false;
167 break;
169 case AFMT_PCM_WAV:
170 if (!get_wave_metadata(fd, id3))
172 return false;
175 break;
177 case AFMT_WAVPACK:
178 if (!get_wavpack_metadata(fd, id3))
179 return false;
181 read_ape_tags(fd, id3); /* use any apetag info we find */
182 break;
184 case AFMT_A52:
185 /* Use the trackname part of the id3 structure as a temporary buffer */
186 buf = (unsigned char *)id3->path;
188 if ((lseek(fd, 0, SEEK_SET) < 0) || (read(fd, buf, 5) < 5))
190 return false;
193 if ((buf[0] != 0x0b) || (buf[1] != 0x77))
195 logf("%s is not an A52/AC3 file\n",trackname);
196 return false;
199 i = buf[4] & 0x3e;
201 if (i > 36)
203 logf("A52: Invalid frmsizecod: %d\n",i);
204 return false;
207 id3->bitrate = a52_bitrates[i >> 1];
208 id3->vbr = false;
209 id3->filesize = filesize(fd);
211 switch (buf[4] & 0xc0)
213 case 0x00:
214 id3->frequency = 48000;
215 id3->bytesperframe=id3->bitrate * 2 * 2;
216 break;
218 case 0x40:
219 id3->frequency = 44100;
220 id3->bytesperframe = a52_441framesizes[i];
221 break;
223 case 0x80:
224 id3->frequency = 32000;
225 id3->bytesperframe = id3->bitrate * 3 * 2;
226 break;
228 default:
229 logf("A52: Invalid samplerate code: 0x%02x\n", buf[4] & 0xc0);
230 return false;
231 break;
234 /* One A52 frame contains 6 blocks, each containing 256 samples */
235 totalsamples = id3->filesize / id3->bytesperframe * 6 * 256;
236 id3->length = totalsamples / id3->frequency * 1000;
237 break;
239 case AFMT_ALAC:
240 case AFMT_AAC:
241 if (!get_mp4_metadata(fd, id3))
243 return false;
246 break;
248 case AFMT_SHN:
249 id3->vbr = true;
250 id3->filesize = filesize(fd);
251 if (!skip_id3v2(fd, id3))
253 return false;
255 /* TODO: read the id3v2 header if it exists */
256 break;
258 case AFMT_SID:
259 if (!get_sid_metadata(fd, id3))
261 return false;
263 break;
264 case AFMT_SPC:
265 if(!get_spc_metadata(fd, id3))
267 DEBUGF("get_spc_metadata error\n");
270 id3->filesize = filesize(fd);
271 id3->genre_string = id3_get_num_genre(36);
272 break;
273 case AFMT_ADX:
274 if (!get_adx_metadata(fd, id3))
276 DEBUGF("get_adx_metadata error\n");
277 return false;
280 break;
281 case AFMT_NSF:
282 buf = (unsigned char *)id3->path;
283 if ((lseek(fd, 0, SEEK_SET) < 0) || ((read(fd, buf, 8)) < 8))
285 DEBUGF("lseek or read failed\n");
286 return false;
288 id3->vbr = false;
289 id3->filesize = filesize(fd);
290 if (memcmp(buf,"NESM",4) && memcmp(buf,"NSFE",4)) return false;
291 break;
293 case AFMT_AIFF:
294 if (!get_aiff_metadata(fd, id3))
296 return false;
299 break;
301 #endif /* CONFIG_CODEC == SWCODEC */
303 default:
304 /* If we don't know how to read the metadata, assume we can't play
305 the file */
306 return false;
307 break;
310 /* We have successfully read the metadata from the file */
312 #ifndef __PCTOOL__
313 if (cuesheet_is_enabled() && look_for_cuesheet_file(trackname, NULL))
315 id3->cuesheet_type = 1;
317 #endif
319 lseek(fd, 0, SEEK_SET);
320 strncpy(id3->path, trackname, sizeof(id3->path));
322 return true;