Gigabeat S: Reclaim about 800K of memory that was laying unused. Get rid of DEVBSS_AT...
[kugel-rb.git] / apps / metadata.c
blob2a49392193218b404cbef3c54544b22f7bf8a8d5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
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 ****************************************************************************/
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <ctype.h>
24 #include "string-extra.h"
26 #include "debug.h"
27 #include "logf.h"
28 #include "cuesheet.h"
29 #include "metadata.h"
31 #include "metadata/metadata_parsers.h"
33 #if CONFIG_CODEC == SWCODEC
35 /* For trailing tag stripping */
36 #include "buffering.h"
38 #include "metadata/metadata_common.h"
40 #endif /* CONFIG_CODEC == SWCODEC */
42 const struct afmt_entry audio_formats[AFMT_NUM_CODECS] =
44 /* Unknown file format */
45 [AFMT_UNKNOWN] =
46 AFMT_ENTRY("???", NULL, NULL, NULL ),
48 /* MPEG Audio layer 1 */
49 [AFMT_MPA_L1] =
50 AFMT_ENTRY("MP1", "mpa", NULL, "mp1\0" ),
51 /* MPEG Audio layer 2 */
52 [AFMT_MPA_L2] =
53 AFMT_ENTRY("MP2", "mpa", NULL, "mpa\0mp2\0" ),
54 /* MPEG Audio layer 3 */
55 [AFMT_MPA_L3] =
56 AFMT_ENTRY("MP3", "mpa", "mp3_enc", "mp3\0" ),
58 #if CONFIG_CODEC == SWCODEC
59 /* Audio Interchange File Format */
60 [AFMT_AIFF] =
61 AFMT_ENTRY("AIFF", "aiff", "aiff_enc", "aiff\0aif\0"),
62 /* Uncompressed PCM in a WAV file OR ATRAC3 stream in WAV file (.at3) */
63 [AFMT_PCM_WAV] =
64 AFMT_ENTRY("WAV", "wav", "wav_enc", "wav\0at3\0" ),
65 /* Ogg Vorbis */
66 [AFMT_OGG_VORBIS] =
67 AFMT_ENTRY("Ogg", "vorbis", NULL, "ogg\0oga\0" ),
68 /* FLAC */
69 [AFMT_FLAC] =
70 AFMT_ENTRY("FLAC", "flac", NULL, "flac\0" ),
71 /* Musepack */
72 [AFMT_MPC] =
73 AFMT_ENTRY("MPC", "mpc", NULL, "mpc\0" ),
74 /* A/52 (aka AC3) audio */
75 [AFMT_A52] =
76 AFMT_ENTRY("AC3", "a52", NULL, "a52\0ac3\0" ),
77 /* WavPack */
78 [AFMT_WAVPACK] =
79 AFMT_ENTRY("WV", "wavpack", "wavpack_enc", "wv\0" ),
80 /* Apple Lossless Audio Codec */
81 [AFMT_MP4_ALAC] =
82 AFMT_ENTRY("ALAC", "alac", NULL, "m4a\0m4b\0" ),
83 /* Advanced Audio Coding in M4A container */
84 [AFMT_MP4_AAC] =
85 AFMT_ENTRY("AAC", "aac", NULL, "mp4\0" ),
86 /* Shorten */
87 [AFMT_SHN] =
88 AFMT_ENTRY("SHN", "shorten", NULL, "shn\0" ),
89 /* SID File Format */
90 [AFMT_SID] =
91 AFMT_ENTRY("SID", "sid", NULL, "sid\0" ),
92 /* ADX File Format */
93 [AFMT_ADX] =
94 AFMT_ENTRY("ADX", "adx", NULL, "adx\0" ),
95 /* NESM (NES Sound Format) */
96 [AFMT_NSF] =
97 AFMT_ENTRY("NSF", "nsf", NULL, "nsf\0nsfe\0" ),
98 /* Speex File Format */
99 [AFMT_SPEEX] =
100 AFMT_ENTRY("Speex","speex", NULL, "spx\0" ),
101 /* SPC700 Save State */
102 [AFMT_SPC] =
103 AFMT_ENTRY("SPC", "spc", NULL, "spc\0" ),
104 /* APE (Monkey's Audio) */
105 [AFMT_APE] =
106 AFMT_ENTRY("APE", "ape", NULL, "ape\0mac\0" ),
107 /* WMA (WMAV1/V2 in ASF) */
108 [AFMT_WMA] =
109 AFMT_ENTRY("WMA", "wma", NULL, "wma\0wmv\0asf\0" ),
110 /* Amiga MOD File */
111 [AFMT_MOD] =
112 AFMT_ENTRY("MOD", "mod", NULL, "mod\0" ),
113 /* Atari SAP File */
114 [AFMT_SAP] =
115 AFMT_ENTRY("SAP", "asap", NULL, "sap\0" ),
116 /* Cook in RM/RA */
117 [AFMT_RM_COOK] =
118 AFMT_ENTRY("Cook", "cook", NULL, "rm\0ra\0rmvb\0" ),
119 /* AAC in RM/RA */
120 [AFMT_RM_AAC] =
121 AFMT_ENTRY("RAAC", "raac", NULL, "rm\0ra\0rmvb\0" ),
122 /* AC3 in RM/RA */
123 [AFMT_RM_AC3] =
124 AFMT_ENTRY("AC3", "a52_rm", NULL, "rm\0ra\0rmvb\0" ),
125 /* ATRAC3 in RM/RA */
126 [AFMT_RM_ATRAC3] =
127 AFMT_ENTRY("ATRAC3","atrac3_rm", NULL, "rm\0ra\0rmvb\0" ),
128 /* Atari CMC File */
129 [AFMT_CMC] =
130 AFMT_ENTRY("CMC", "asap", NULL, "cmc\0" ),
131 /* Atari CM3 File */
132 [AFMT_CM3] =
133 AFMT_ENTRY("CM3", "asap", NULL, "cm3\0" ),
134 /* Atari CMR File */
135 [AFMT_CMR] =
136 AFMT_ENTRY("CMR", "asap", NULL, "cmr\0" ),
137 /* Atari CMS File */
138 [AFMT_CMS] =
139 AFMT_ENTRY("CMS", "asap", NULL, "cms\0" ),
140 /* Atari DMC File */
141 [AFMT_DMC] =
142 AFMT_ENTRY("DMC", "asap", NULL, "dmc\0" ),
143 /* Atari DLT File */
144 [AFMT_DLT] =
145 AFMT_ENTRY("DLT", "asap", NULL, "dlt\0" ),
146 /* Atari MPT File */
147 [AFMT_MPT] =
148 AFMT_ENTRY("MPT", "asap", NULL, "mpt\0" ),
149 /* Atari MPD File */
150 [AFMT_MPD] =
151 AFMT_ENTRY("MPD", "asap", NULL, "mpd\0" ),
152 /* Atari RMT File */
153 [AFMT_RMT] =
154 AFMT_ENTRY("RMT", "asap", NULL, "rmt\0" ),
155 /* Atari TMC File */
156 [AFMT_TMC] =
157 AFMT_ENTRY("TMC", "asap", NULL, "tmc\0" ),
158 /* Atari TM8 File */
159 [AFMT_TM8] =
160 AFMT_ENTRY("TM8", "asap", NULL, "tm8\0" ),
161 /* Atari TM2 File */
162 [AFMT_TM2] =
163 AFMT_ENTRY("TM2", "asap", NULL, "tm2\0" ),
164 /* Atrac3 in Sony OMA Container */
165 [AFMT_OMA_ATRAC3] =
166 AFMT_ENTRY("ATRAC3", "atrac3_oma", NULL, "oma\0aa3\0" ),
167 /* SMAF (Synthetic music Mobile Application Format) */
168 [AFMT_SMAF] =
169 AFMT_ENTRY("SMAF", "smaf", NULL, "mmf\0" ),
170 /* Sun Audio file */
171 [AFMT_AU] =
172 AFMT_ENTRY("AU", "au", NULL, "au\0snd\0" ),
173 /* VOX (Dialogic telephony file formats) */
174 [AFMT_VOX] =
175 AFMT_ENTRY("VOX", "vox", NULL, "vox\0" ),
176 /* Wave64 */
177 [AFMT_WAVE64] =
178 AFMT_ENTRY("WAVE64", "wav64", NULL, "w64\0" ),
179 #endif
182 #if CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING)
183 /* get REC_FORMAT_* corresponding AFMT_* */
184 const int rec_format_afmt[REC_NUM_FORMATS] =
186 /* give AFMT_UNKNOWN by default */
187 [0 ... REC_NUM_FORMATS-1] = AFMT_UNKNOWN,
188 /* add new entries below this line */
189 [REC_FORMAT_AIFF] = AFMT_AIFF,
190 [REC_FORMAT_MPA_L3] = AFMT_MPA_L3,
191 [REC_FORMAT_WAVPACK] = AFMT_WAVPACK,
192 [REC_FORMAT_PCM_WAV] = AFMT_PCM_WAV,
195 /* get AFMT_* corresponding REC_FORMAT_* */
196 const int afmt_rec_format[AFMT_NUM_CODECS] =
198 /* give -1 by default */
199 [0 ... AFMT_NUM_CODECS-1] = -1,
200 /* add new entries below this line */
201 [AFMT_AIFF] = REC_FORMAT_AIFF,
202 [AFMT_MPA_L3] = REC_FORMAT_MPA_L3,
203 [AFMT_WAVPACK] = REC_FORMAT_WAVPACK,
204 [AFMT_PCM_WAV] = REC_FORMAT_PCM_WAV,
206 #endif /* CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING) */
209 /* Simple file type probing by looking at the filename extension. */
210 unsigned int probe_file_format(const char *filename)
212 char *suffix;
213 unsigned int i;
215 suffix = strrchr(filename, '.');
217 if (suffix == NULL)
219 return AFMT_UNKNOWN;
222 /* skip '.' */
223 suffix++;
225 for (i = 1; i < AFMT_NUM_CODECS; i++)
227 /* search extension list for type */
228 const char *ext = audio_formats[i].ext_list;
232 if (strcasecmp(suffix, ext) == 0)
234 return i;
237 ext += strlen(ext) + 1;
239 while (*ext != '\0');
242 return AFMT_UNKNOWN;
245 /* Note, that this returns false for successful, true for error! */
246 bool mp3info(struct mp3entry *entry, const char *filename)
248 int fd;
249 bool result;
251 fd = open(filename, O_RDONLY);
252 if (fd < 0)
253 return true;
255 result = !get_mp3_metadata(fd, entry, filename);
257 close(fd);
259 return result;
262 /* Get metadata for track - return false if parsing showed problems with the
263 * file that would prevent playback.
265 bool get_metadata(struct mp3entry* id3, int fd, const char* trackname)
267 /* Clear the mp3entry to avoid having bogus pointers appear */
268 memset(id3, 0, sizeof(struct mp3entry));
270 /* Take our best guess at the codec type based on file extension */
271 id3->codectype = probe_file_format(trackname);
273 /* Load codec specific track tag information and confirm the codec type. */
274 switch (id3->codectype)
276 case AFMT_MPA_L1:
277 case AFMT_MPA_L2:
278 case AFMT_MPA_L3:
279 if (!get_mp3_metadata(fd, id3, trackname))
281 return false;
284 break;
286 #if CONFIG_CODEC == SWCODEC
287 case AFMT_FLAC:
288 if (!get_flac_metadata(fd, id3))
290 return false;
293 break;
295 case AFMT_WMA:
296 if (!get_asf_metadata(fd, id3))
298 return false;
300 break;
302 case AFMT_APE:
303 if (!get_monkeys_metadata(fd, id3))
305 return false;
307 read_ape_tags(fd, id3);
308 break;
310 case AFMT_MPC:
311 if (!get_musepack_metadata(fd, id3))
312 return false;
313 read_ape_tags(fd, id3);
314 break;
316 case AFMT_OGG_VORBIS:
317 case AFMT_SPEEX:
318 if (!get_ogg_metadata(fd, id3))/*detects and handles Ogg/Speex files*/
320 return false;
323 break;
325 case AFMT_PCM_WAV:
326 if (!get_wave_metadata(fd, id3))
328 return false;
331 break;
333 case AFMT_WAVPACK:
334 if (!get_wavpack_metadata(fd, id3))
336 return false;
339 read_ape_tags(fd, id3); /* use any apetag info we find */
340 break;
342 case AFMT_A52:
343 if (!get_a52_metadata(fd, id3))
345 return false;
348 break;
350 case AFMT_MP4_ALAC:
351 case AFMT_MP4_AAC:
352 if (!get_mp4_metadata(fd, id3))
354 return false;
357 break;
359 case AFMT_MOD:
360 if (!get_mod_metadata(fd, id3))
362 return false;
365 break;
367 case AFMT_SHN:
368 id3->vbr = true;
369 id3->filesize = filesize(fd);
370 if (!skip_id3v2(fd, id3))
372 return false;
374 /* TODO: read the id3v2 header if it exists */
375 break;
377 case AFMT_SID:
378 if (!get_sid_metadata(fd, id3))
380 return false;
382 break;
384 case AFMT_SPC:
385 if (!get_spc_metadata(fd, id3))
387 DEBUGF("get_spc_metadata error\n");
388 return false;
390 id3->filesize = filesize(fd);
391 id3->genre_string = id3_get_num_genre(36);
392 break;
394 case AFMT_ADX:
395 if (!get_adx_metadata(fd, id3))
397 DEBUGF("get_adx_metadata error\n");
398 return false;
401 break;
403 case AFMT_NSF:
404 if (!get_nsf_metadata(fd, id3))
406 DEBUGF("get_nsf_metadata error\n");
407 return false;
409 break;
411 case AFMT_AIFF:
412 if (!get_aiff_metadata(fd, id3))
414 return false;
417 break;
419 case AFMT_SAP:
420 if (!get_asap_metadata(fd, id3))
422 DEBUGF("get_sap_metadata error\n");
423 return false;
425 id3->filesize = filesize(fd);
426 id3->genre_string = id3_get_num_genre(36);
427 break;
429 case AFMT_CMC:
430 case AFMT_CM3:
431 case AFMT_CMR:
432 case AFMT_CMS:
433 case AFMT_DMC:
434 case AFMT_DLT:
435 case AFMT_MPT:
436 case AFMT_MPD:
437 case AFMT_RMT:
438 case AFMT_TMC:
439 case AFMT_TM8:
440 case AFMT_TM2:
441 id3->bitrate = 706;
442 id3->frequency = 44100;
443 id3->vbr = false;
444 id3->filesize = filesize(fd);
445 id3->genre_string = id3_get_num_genre(36);
446 break;
447 case AFMT_RM_COOK:
448 if (!get_rm_metadata(fd, id3))
450 DEBUGF("get_rm_metadata error\n");
451 return false;
453 break;
454 case AFMT_OMA_ATRAC3:
455 if (!get_oma_metadata(fd, id3))
457 DEBUGF("get_oma_metadata error\n");
458 return false;
460 break;
462 case AFMT_SMAF:
463 if (!get_smaf_metadata(fd, id3))
465 DEBUGF("get_smaf_metadata error\n");
466 return false;
468 break;
470 case AFMT_AU:
471 if (!get_au_metadata(fd, id3))
473 DEBUGF("get_au_metadata error\n");
474 return false;
476 break;
478 case AFMT_VOX:
479 if (!get_vox_metadata(fd, id3))
481 DEBUGF("get_vox_metadata error\n");
482 return false;
484 break;
486 case AFMT_WAVE64:
487 if (!get_wave64_metadata(fd, id3))
489 DEBUGF("get_wave64_metadata error\n");
490 return false;
492 break;
494 #endif /* CONFIG_CODEC == SWCODEC */
496 default:
497 /* If we don't know how to read the metadata, assume we can't play
498 the file */
499 return false;
500 break;
503 /* We have successfully read the metadata from the file */
505 lseek(fd, 0, SEEK_SET);
506 strlcpy(id3->path, trackname, sizeof(id3->path));
508 return true;
511 #ifndef __PCTOOL__
512 #if CONFIG_CODEC == SWCODEC
513 void strip_tags(int handle_id)
515 static const unsigned char tag[] = "TAG";
516 static const unsigned char apetag[] = "APETAGEX";
517 size_t len, version;
518 void *tail;
520 if (bufgettail(handle_id, 128, &tail) != 128)
521 return;
523 if (memcmp(tail, tag, 3) == 0)
525 /* Skip id3v1 tag */
526 logf("Cutting off ID3v1 tag");
527 bufcuttail(handle_id, 128);
530 /* Get a new tail, as the old one may have been cut */
531 if (bufgettail(handle_id, 32, &tail) != 32)
532 return;
534 /* Check for APE tag (look for the APE tag footer) */
535 if (memcmp(tail, apetag, 8) != 0)
536 return;
538 /* Read the version and length from the footer */
539 version = get_long_le(&((unsigned char *)tail)[8]);
540 len = get_long_le(&((unsigned char *)tail)[12]);
541 if (version == 2000)
542 len += 32; /* APEv2 has a 32 byte header */
544 /* Skip APE tag */
545 logf("Cutting off APE tag (%ldB)", len);
546 bufcuttail(handle_id, len);
548 #endif /* CONFIG_CODEC == SWCODEC */
549 #endif /* ! __PCTOOL__ */
551 void adjust_mp3entry(struct mp3entry *entry, void *dest, const void *orig)
553 long offset;
554 if (orig > dest)
555 offset = - ((size_t)orig - (size_t)dest);
556 else
557 offset = (size_t)dest - (size_t)orig;
559 if (entry->title)
560 entry->title += offset;
561 if (entry->artist)
562 entry->artist += offset;
563 if (entry->album)
564 entry->album += offset;
565 if (entry->genre_string > (char*)orig
566 && entry->genre_string < (char*)orig + sizeof(struct mp3entry))
567 /* Don't adjust that if it points to an entry of the "genres" array */
568 entry->genre_string += offset;
569 if (entry->track_string)
570 entry->track_string += offset;
571 if (entry->disc_string)
572 entry->disc_string += offset;
573 if (entry->year_string)
574 entry->year_string += offset;
575 if (entry->composer)
576 entry->composer += offset;
577 if (entry->comment)
578 entry->comment += offset;
579 if (entry->albumartist)
580 entry->albumartist += offset;
581 if (entry->grouping)
582 entry->grouping += offset;
583 #if CONFIG_CODEC == SWCODEC
584 if (entry->track_gain_string)
585 entry->track_gain_string += offset;
586 if (entry->album_gain_string)
587 entry->album_gain_string += offset;
588 #endif
589 if (entry->mb_track_id)
590 entry->mb_track_id += offset;
593 void copy_mp3entry(struct mp3entry *dest, const struct mp3entry *orig)
595 memcpy(dest, orig, sizeof(struct mp3entry));
596 adjust_mp3entry(dest, dest, orig);