autodetection: convert path to native separators before displaying it.
[Rockbox.git] / apps / metadata.c
blob7b3b314d74d9f4bc658836da840189a4b1242fb6
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"
30 #include "metadata.h"
32 #if CONFIG_CODEC == SWCODEC
34 /* For trailing tag stripping */
35 #include "buffering.h"
37 #include "metadata/metadata_common.h"
38 #include "metadata/metadata_parsers.h"
40 #endif /* CONFIG_CODEC == SWCODEC */
43 /* Simple file type probing by looking at the filename extension. */
44 unsigned int probe_file_format(const char *filename)
46 char *suffix;
47 unsigned int i;
49 suffix = strrchr(filename, '.');
51 if (suffix == NULL)
53 return AFMT_UNKNOWN;
56 /* skip '.' */
57 suffix++;
59 for (i = 1; i < AFMT_NUM_CODECS; i++)
61 /* search extension list for type */
62 const char *ext = audio_formats[i].ext_list;
66 if (strcasecmp(suffix, ext) == 0)
68 return i;
71 ext += strlen(ext) + 1;
73 while (*ext != '\0');
76 return AFMT_UNKNOWN;
79 /* Get metadata for track - return false if parsing showed problems with the
80 * file that would prevent playback.
82 bool get_metadata(struct mp3entry* id3, int fd, const char* trackname)
84 #if CONFIG_CODEC == SWCODEC
85 unsigned char* buf;
86 #endif
88 /* Clear the mp3entry to avoid having bogus pointers appear */
89 memset(id3, 0, sizeof(struct mp3entry));
91 /* Take our best guess at the codec type based on file extension */
92 id3->codectype = probe_file_format(trackname);
94 /* Load codec specific track tag information and confirm the codec type. */
95 switch (id3->codectype)
97 case AFMT_MPA_L1:
98 case AFMT_MPA_L2:
99 case AFMT_MPA_L3:
100 if (!get_mp3_metadata(fd, id3, trackname))
102 return false;
105 break;
107 #if CONFIG_CODEC == SWCODEC
108 case AFMT_FLAC:
109 if (!get_flac_metadata(fd, id3))
111 return false;
114 break;
116 case AFMT_WMA:
117 if (!get_asf_metadata(fd, id3))
119 return false;
121 break;
123 case AFMT_APE:
124 if (!get_monkeys_metadata(fd, id3))
126 return false;
128 read_ape_tags(fd, id3);
129 break;
131 case AFMT_MPC:
132 if (!get_musepack_metadata(fd, id3))
133 return false;
134 read_ape_tags(fd, id3);
135 break;
137 case AFMT_OGG_VORBIS:
138 if (!get_ogg_metadata(fd, id3))/*detects and handles Ogg/Speex files*/
140 return false;
143 break;
145 case AFMT_SPEEX:
146 if (!get_ogg_metadata(fd, id3))
148 return false;
151 break;
153 case AFMT_PCM_WAV:
154 if (!get_wave_metadata(fd, id3))
156 return false;
159 break;
161 case AFMT_WAVPACK:
162 if (!get_wavpack_metadata(fd, id3))
164 return false;
167 read_ape_tags(fd, id3); /* use any apetag info we find */
168 break;
170 case AFMT_A52:
171 if (!get_a52_metadata(fd, id3))
173 return false;
176 break;
178 case AFMT_ALAC:
179 case AFMT_AAC:
180 if (!get_mp4_metadata(fd, id3))
182 return false;
185 break;
187 case AFMT_SHN:
188 id3->vbr = true;
189 id3->filesize = filesize(fd);
190 if (!skip_id3v2(fd, id3))
192 return false;
194 /* TODO: read the id3v2 header if it exists */
195 break;
197 case AFMT_SID:
198 if (!get_sid_metadata(fd, id3))
200 return false;
202 break;
204 case AFMT_SPC:
205 if (!get_spc_metadata(fd, id3))
207 DEBUGF("get_spc_metadata error\n");
208 return false;
210 id3->filesize = filesize(fd);
211 id3->genre_string = id3_get_num_genre(36);
212 break;
214 case AFMT_ADX:
215 if (!get_adx_metadata(fd, id3))
217 DEBUGF("get_adx_metadata error\n");
218 return false;
221 break;
223 case AFMT_NSF:
224 buf = (unsigned char *)id3->path;
225 if ((lseek(fd, 0, SEEK_SET) < 0) || ((read(fd, buf, 8)) < 8))
227 DEBUGF("lseek or read failed\n");
228 return false;
230 id3->vbr = false;
231 id3->filesize = filesize(fd);
232 if (memcmp(buf,"NESM",4) && memcmp(buf,"NSFE",4)) return false;
233 break;
235 case AFMT_AIFF:
236 if (!get_aiff_metadata(fd, id3))
238 return false;
241 break;
243 #endif /* CONFIG_CODEC == SWCODEC */
245 default:
246 /* If we don't know how to read the metadata, assume we can't play
247 the file */
248 return false;
249 break;
252 /* We have successfully read the metadata from the file */
254 #ifndef __PCTOOL__
255 if (cuesheet_is_enabled() && look_for_cuesheet_file(trackname, NULL))
257 id3->cuesheet_type = 1;
259 #endif
261 lseek(fd, 0, SEEK_SET);
262 strncpy(id3->path, trackname, sizeof(id3->path));
264 return true;
267 #if CONFIG_CODEC == SWCODEC
268 void strip_tags(int handle_id)
270 static const unsigned char tag[] = "TAG";
271 static const unsigned char apetag[] = "APETAGEX";
272 size_t len, version;
273 void *tail;
275 if (bufgettail(handle_id, 128, &tail) != 128)
276 return;
278 if (memcmp(tail, tag, 3) == 0)
280 /* Skip id3v1 tag */
281 logf("Cutting off ID3v1 tag");
282 bufcuttail(handle_id, 128);
285 /* Get a new tail, as the old one may have been cut */
286 if (bufgettail(handle_id, 32, &tail) != 32)
287 return;
289 /* Check for APE tag (look for the APE tag footer) */
290 if (memcmp(tail, apetag, 8) != 0)
291 return;
293 /* Read the version and length from the footer */
294 version = get_long_le(&((unsigned char *)tail)[8]);
295 len = get_long_le(&((unsigned char *)tail)[12]);
296 if (version == 2000)
297 len += 32; /* APEv2 has a 32 byte header */
299 /* Skip APE tag */
300 logf("Cutting off APE tag (%ldB)", len);
301 bufcuttail(handle_id, len);
303 #endif /* CONFIG_CODEC == SWCODEC */