Oops.
[Rockbox.git] / apps / metadata.c
blob15813c679d0777b07a453246a65eb9458a044b7a
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 static const long wavpack_sample_rates [] =
56 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
57 32000, 44100, 48000, 64000, 88200, 96000, 192000
60 #endif /* CONFIG_CODEC == SWCODEC */
63 /* Simple file type probing by looking at the filename extension. */
64 unsigned int probe_file_format(const char *filename)
66 char *suffix;
67 unsigned int i;
69 suffix = strrchr(filename, '.');
71 if (suffix == NULL)
73 return AFMT_UNKNOWN;
76 /* skip '.' */
77 suffix++;
79 for (i = 1; i < AFMT_NUM_CODECS; i++)
81 /* search extension list for type */
82 const char *ext = audio_formats[i].ext_list;
86 if (strcasecmp(suffix, ext) == 0)
88 return i;
91 ext += strlen(ext) + 1;
93 while (*ext != '\0');
96 return AFMT_UNKNOWN;
99 /* Get metadata for track - return false if parsing showed problems with the
100 * file that would prevent playback.
102 bool get_metadata(struct track_info* track, int fd, const char* trackname,
103 bool v1first)
105 #if CONFIG_CODEC == SWCODEC
106 unsigned char* buf;
107 unsigned long totalsamples;
108 int i;
109 #endif
111 /* Take our best guess at the codec type based on file extension */
112 track->id3.codectype = probe_file_format(trackname);
114 /* Load codec specific track tag information and confirm the codec type. */
115 switch (track->id3.codectype)
117 case AFMT_MPA_L1:
118 case AFMT_MPA_L2:
119 case AFMT_MPA_L3:
120 if (!get_mp3_metadata(fd, &track->id3, trackname, v1first))
122 return false;
125 break;
127 #if CONFIG_CODEC == SWCODEC
128 case AFMT_FLAC:
129 if (!get_flac_metadata(fd, &(track->id3)))
131 return false;
134 break;
136 case AFMT_WMA:
137 if (!get_asf_metadata(fd, &(track->id3)))
139 return false;
141 break;
143 case AFMT_APE:
144 if (!get_monkeys_metadata(fd, &(track->id3)))
146 return false;
148 read_ape_tags(fd, &(track->id3));
149 break;
151 case AFMT_MPC:
152 if (!get_musepack_metadata(fd, &(track->id3)))
153 return false;
154 read_ape_tags(fd, &(track->id3));
155 break;
157 case AFMT_OGG_VORBIS:
158 if (!get_vorbis_metadata(fd, &(track->id3)))/*detects and handles Ogg/Speex files*/
160 return false;
163 break;
165 case AFMT_SPEEX:
166 if (!get_speex_metadata(fd, &(track->id3)))
168 return false;
171 break;
173 case AFMT_PCM_WAV:
174 if (!get_wave_metadata(fd, &(track->id3)))
176 return false;
179 break;
181 case AFMT_WAVPACK:
182 /* A simple parser to read basic information from a WavPack file. This
183 * now works with self-extrating WavPack files. This no longer fails on
184 * WavPack files containing floating-point audio data because these are
185 * now converted to standard Rockbox format in the decoder.
188 /* Use the trackname part of the id3 structure as a temporary buffer */
189 buf = (unsigned char *)track->id3.path;
191 for (i = 0; i < 256; ++i) {
193 /* at every 256 bytes into file, try to read a WavPack header */
195 if ((lseek(fd, i * 256, SEEK_SET) < 0) || (read(fd, buf, 32) < 32))
197 return false;
200 /* if valid WavPack 4 header version & not floating data, break */
202 if (memcmp (buf, "wvpk", 4) == 0 && buf [9] == 4 &&
203 (buf [8] >= 2 && buf [8] <= 0x10))
205 break;
209 if (i == 256) {
210 logf ("%s is not a WavPack file\n", trackname);
211 return false;
214 track->id3.vbr = true; /* All WavPack files are VBR */
215 track->id3.filesize = filesize (fd);
217 if ((buf [20] | buf [21] | buf [22] | buf [23]) &&
218 (buf [12] & buf [13] & buf [14] & buf [15]) != 0xff)
220 int srindx = ((buf [26] >> 7) & 1) + ((buf [27] << 1) & 14);
222 if (srindx == 15)
224 track->id3.frequency = 44100;
226 else
228 track->id3.frequency = wavpack_sample_rates[srindx];
231 totalsamples = get_long_le(&buf[12]);
232 track->id3.length = totalsamples / (track->id3.frequency / 100) * 10;
233 track->id3.bitrate = filesize (fd) / (track->id3.length / 8);
236 read_ape_tags(fd, &track->id3); /* use any apetag info we find */
237 break;
239 case AFMT_A52:
240 /* Use the trackname part of the id3 structure as a temporary buffer */
241 buf = (unsigned char *)track->id3.path;
243 if ((lseek(fd, 0, SEEK_SET) < 0) || (read(fd, buf, 5) < 5))
245 return false;
248 if ((buf[0] != 0x0b) || (buf[1] != 0x77))
250 logf("%s is not an A52/AC3 file\n",trackname);
251 return false;
254 i = buf[4] & 0x3e;
256 if (i > 36)
258 logf("A52: Invalid frmsizecod: %d\n",i);
259 return false;
262 track->id3.bitrate = a52_bitrates[i >> 1];
263 track->id3.vbr = false;
264 track->id3.filesize = filesize(fd);
266 switch (buf[4] & 0xc0)
268 case 0x00:
269 track->id3.frequency = 48000;
270 track->id3.bytesperframe=track->id3.bitrate * 2 * 2;
271 break;
273 case 0x40:
274 track->id3.frequency = 44100;
275 track->id3.bytesperframe = a52_441framesizes[i];
276 break;
278 case 0x80:
279 track->id3.frequency = 32000;
280 track->id3.bytesperframe = track->id3.bitrate * 3 * 2;
281 break;
283 default:
284 logf("A52: Invalid samplerate code: 0x%02x\n", buf[4] & 0xc0);
285 return false;
286 break;
289 /* One A52 frame contains 6 blocks, each containing 256 samples */
290 totalsamples = track->id3.filesize / track->id3.bytesperframe * 6 * 256;
291 track->id3.length = totalsamples / track->id3.frequency * 1000;
292 break;
294 case AFMT_ALAC:
295 case AFMT_AAC:
296 if (!get_mp4_metadata(fd, &(track->id3)))
298 return false;
301 break;
303 case AFMT_SHN:
304 track->id3.vbr = true;
305 track->id3.filesize = filesize(fd);
306 if (!skip_id3v2(fd, &(track->id3)))
308 return false;
310 /* TODO: read the id3v2 header if it exists */
311 break;
313 case AFMT_SID:
314 if (!get_sid_metadata(fd, &(track->id3)))
316 return false;
318 break;
319 case AFMT_SPC:
320 if(!get_spc_metadata(fd, &(track->id3)))
322 DEBUGF("get_spc_metadata error\n");
325 track->id3.filesize = filesize(fd);
326 track->id3.genre_string = id3_get_num_genre(36);
327 break;
328 case AFMT_ADX:
329 if (!get_adx_metadata(fd, &(track->id3)))
331 DEBUGF("get_adx_metadata error\n");
332 return false;
335 break;
336 case AFMT_NSF:
337 buf = (unsigned char *)track->id3.path;
338 if ((lseek(fd, 0, SEEK_SET) < 0) || ((read(fd, buf, 8)) < 8))
340 DEBUGF("lseek or read failed\n");
341 return false;
343 track->id3.vbr = false;
344 track->id3.filesize = filesize(fd);
345 if (memcmp(buf,"NESM",4) && memcmp(buf,"NSFE",4)) return false;
346 break;
348 case AFMT_AIFF:
349 if (!get_aiff_metadata(fd, &(track->id3)))
351 return false;
354 break;
356 #endif /* CONFIG_CODEC == SWCODEC */
358 default:
359 /* If we don't know how to read the metadata, assume we can't play
360 the file */
361 return false;
362 break;
365 /* We have successfully read the metadata from the file */
367 #ifndef __PCTOOL__
368 if (cuesheet_is_enabled() && look_for_cuesheet_file(trackname, NULL))
370 track->id3.cuesheet_type = 1;
372 #endif
374 lseek(fd, 0, SEEK_SET);
375 strncpy(track->id3.path, trackname, sizeof(track->id3.path));
376 track->taginfo_ready = true;
378 return true;