spacerocks: Fix indentation and remove trailing spaces. Fix some minor bugs.
[kugel-rb.git] / apps / metadata.c
blob7913226779c47e9c4f5a9cff6aaf500dce1efc60
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 <string.h>
23 #include <stdlib.h>
24 #include <ctype.h>
25 #include <inttypes.h>
27 #include "debug.h"
28 #include "logf.h"
29 #include "cuesheet.h"
30 #include "metadata.h"
32 #include "metadata/metadata_parsers.h"
34 #if CONFIG_CODEC == SWCODEC
36 /* For trailing tag stripping */
37 #include "buffering.h"
39 #include "metadata/metadata_common.h"
41 #endif /* CONFIG_CODEC == SWCODEC */
43 const struct afmt_entry audio_formats[AFMT_NUM_CODECS] =
45 /* Unknown file format */
46 [AFMT_UNKNOWN] =
47 AFMT_ENTRY("???", NULL, NULL, NULL ),
49 /* MPEG Audio layer 1 */
50 [AFMT_MPA_L1] =
51 AFMT_ENTRY("MP1", "mpa", NULL, "mp1\0" ),
52 /* MPEG Audio layer 2 */
53 [AFMT_MPA_L2] =
54 AFMT_ENTRY("MP2", "mpa", NULL, "mpa\0mp2\0" ),
55 /* MPEG Audio layer 3 */
56 [AFMT_MPA_L3] =
57 AFMT_ENTRY("MP3", "mpa", "mp3_enc", "mp3\0" ),
59 #if CONFIG_CODEC == SWCODEC
60 /* Audio Interchange File Format */
61 [AFMT_AIFF] =
62 AFMT_ENTRY("AIFF", "aiff", "aiff_enc", "aiff\0aif\0"),
63 /* Uncompressed PCM in a WAV file */
64 [AFMT_PCM_WAV] =
65 AFMT_ENTRY("WAV", "wav", "wav_enc", "wav\0" ),
66 /* Ogg Vorbis */
67 [AFMT_OGG_VORBIS] =
68 AFMT_ENTRY("Ogg", "vorbis", NULL, "ogg\0oga\0" ),
69 /* FLAC */
70 [AFMT_FLAC] =
71 AFMT_ENTRY("FLAC", "flac", NULL, "flac\0" ),
72 /* Musepack */
73 [AFMT_MPC] =
74 AFMT_ENTRY("MPC", "mpc", NULL, "mpc\0" ),
75 /* A/52 (aka AC3) audio */
76 [AFMT_A52] =
77 AFMT_ENTRY("AC3", "a52", NULL, "a52\0ac3\0" ),
78 /* WavPack */
79 [AFMT_WAVPACK] =
80 AFMT_ENTRY("WV", "wavpack", "wavpack_enc", "wv\0" ),
81 /* Apple Lossless Audio Codec */
82 [AFMT_MP4_ALAC] =
83 AFMT_ENTRY("ALAC", "alac", NULL, "m4a\0m4b\0" ),
84 /* Advanced Audio Coding in M4A container */
85 [AFMT_MP4_AAC] =
86 AFMT_ENTRY("AAC", "aac", NULL, "mp4\0" ),
87 /* Shorten */
88 [AFMT_SHN] =
89 AFMT_ENTRY("SHN", "shorten", NULL, "shn\0" ),
90 /* SID File Format */
91 [AFMT_SID] =
92 AFMT_ENTRY("SID", "sid", NULL, "sid\0" ),
93 /* ADX File Format */
94 [AFMT_ADX] =
95 AFMT_ENTRY("ADX", "adx", NULL, "adx\0" ),
96 /* NESM (NES Sound Format) */
97 [AFMT_NSF] =
98 AFMT_ENTRY("NSF", "nsf", NULL, "nsf\0nsfe\0" ),
99 /* Speex File Format */
100 [AFMT_SPEEX] =
101 AFMT_ENTRY("Speex","speex", NULL, "spx\0" ),
102 /* SPC700 Save State */
103 [AFMT_SPC] =
104 AFMT_ENTRY("SPC", "spc", NULL, "spc\0" ),
105 /* APE (Monkey's Audio) */
106 [AFMT_APE] =
107 AFMT_ENTRY("APE", "ape", NULL, "ape\0mac\0" ),
108 /* WMA (WMAV1/V2 in ASF) */
109 [AFMT_WMA] =
110 AFMT_ENTRY("WMA", "wma", NULL, "wma\0wmv\0asf\0" ),
111 /* Amiga MOD File */
112 [AFMT_MOD] =
113 AFMT_ENTRY("MOD", "mod", NULL, "mod\0" ),
114 /* Amiga SAP File */
115 [AFMT_SAP] =
116 AFMT_ENTRY("SAP", "asap", NULL, "sap\0" ),
117 /* Cook in RM/RA */
118 [AFMT_RM_COOK] =
119 AFMT_ENTRY("Cook", "cook", NULL, "rm\0ra\0rmvb\0" ),
120 /* AAC in RM/RA */
121 [AFMT_RM_AAC] =
122 AFMT_ENTRY("RAAC", "raac", NULL, "rm\0ra\0rmvb\0" ),
123 /* AC3 in RM/RA */
124 [AFMT_RM_AC3] =
125 AFMT_ENTRY("AC3", "a52_rm", NULL, "rm\0ra\0rmvb\0" ),
126 /* ATRAC3 in RM/RA */
127 [AFMT_RM_ATRAC3] =
128 AFMT_ENTRY("ATRAC3","atrac3_rm", NULL, "rm\0ra\0rmvb\0" ),
129 #endif
132 #if CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING)
133 /* get REC_FORMAT_* corresponding AFMT_* */
134 const int rec_format_afmt[REC_NUM_FORMATS] =
136 /* give AFMT_UNKNOWN by default */
137 [0 ... REC_NUM_FORMATS-1] = AFMT_UNKNOWN,
138 /* add new entries below this line */
139 [REC_FORMAT_AIFF] = AFMT_AIFF,
140 [REC_FORMAT_MPA_L3] = AFMT_MPA_L3,
141 [REC_FORMAT_WAVPACK] = AFMT_WAVPACK,
142 [REC_FORMAT_PCM_WAV] = AFMT_PCM_WAV,
145 /* get AFMT_* corresponding REC_FORMAT_* */
146 const int afmt_rec_format[AFMT_NUM_CODECS] =
148 /* give -1 by default */
149 [0 ... AFMT_NUM_CODECS-1] = -1,
150 /* add new entries below this line */
151 [AFMT_AIFF] = REC_FORMAT_AIFF,
152 [AFMT_MPA_L3] = REC_FORMAT_MPA_L3,
153 [AFMT_WAVPACK] = REC_FORMAT_WAVPACK,
154 [AFMT_PCM_WAV] = REC_FORMAT_PCM_WAV,
156 #endif /* CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING) */
159 /* Simple file type probing by looking at the filename extension. */
160 unsigned int probe_file_format(const char *filename)
162 char *suffix;
163 unsigned int i;
165 suffix = strrchr(filename, '.');
167 if (suffix == NULL)
169 return AFMT_UNKNOWN;
172 /* skip '.' */
173 suffix++;
175 for (i = 1; i < AFMT_NUM_CODECS; i++)
177 /* search extension list for type */
178 const char *ext = audio_formats[i].ext_list;
182 if (strcasecmp(suffix, ext) == 0)
184 return i;
187 ext += strlen(ext) + 1;
189 while (*ext != '\0');
192 return AFMT_UNKNOWN;
195 /* Note, that this returns false for successful, true for error! */
196 bool mp3info(struct mp3entry *entry, const char *filename)
198 int fd;
199 bool result;
201 fd = open(filename, O_RDONLY);
202 if (fd < 0)
203 return true;
205 result = !get_mp3_metadata(fd, entry, filename);
207 close(fd);
209 return result;
212 /* Get metadata for track - return false if parsing showed problems with the
213 * file that would prevent playback.
215 bool get_metadata(struct mp3entry* id3, int fd, const char* trackname)
217 /* Clear the mp3entry to avoid having bogus pointers appear */
218 memset(id3, 0, sizeof(struct mp3entry));
220 /* Take our best guess at the codec type based on file extension */
221 id3->codectype = probe_file_format(trackname);
223 /* Load codec specific track tag information and confirm the codec type. */
224 switch (id3->codectype)
226 case AFMT_MPA_L1:
227 case AFMT_MPA_L2:
228 case AFMT_MPA_L3:
229 if (!get_mp3_metadata(fd, id3, trackname))
231 return false;
234 break;
236 #if CONFIG_CODEC == SWCODEC
237 case AFMT_FLAC:
238 if (!get_flac_metadata(fd, id3))
240 return false;
243 break;
245 case AFMT_WMA:
246 if (!get_asf_metadata(fd, id3))
248 return false;
250 break;
252 case AFMT_APE:
253 if (!get_monkeys_metadata(fd, id3))
255 return false;
257 read_ape_tags(fd, id3);
258 break;
260 case AFMT_MPC:
261 if (!get_musepack_metadata(fd, id3))
262 return false;
263 read_ape_tags(fd, id3);
264 break;
266 case AFMT_OGG_VORBIS:
267 case AFMT_SPEEX:
268 if (!get_ogg_metadata(fd, id3))/*detects and handles Ogg/Speex files*/
270 return false;
273 break;
275 case AFMT_PCM_WAV:
276 if (!get_wave_metadata(fd, id3))
278 return false;
281 break;
283 case AFMT_WAVPACK:
284 if (!get_wavpack_metadata(fd, id3))
286 return false;
289 read_ape_tags(fd, id3); /* use any apetag info we find */
290 break;
292 case AFMT_A52:
293 if (!get_a52_metadata(fd, id3))
295 return false;
298 break;
300 case AFMT_MP4_ALAC:
301 case AFMT_MP4_AAC:
302 if (!get_mp4_metadata(fd, id3))
304 return false;
307 break;
309 case AFMT_MOD:
310 if (!get_mod_metadata(fd, id3))
312 return false;
315 break;
317 case AFMT_SHN:
318 id3->vbr = true;
319 id3->filesize = filesize(fd);
320 if (!skip_id3v2(fd, id3))
322 return false;
324 /* TODO: read the id3v2 header if it exists */
325 break;
327 case AFMT_SID:
328 if (!get_sid_metadata(fd, id3))
330 return false;
332 break;
334 case AFMT_SPC:
335 if (!get_spc_metadata(fd, id3))
337 DEBUGF("get_spc_metadata error\n");
338 return false;
340 id3->filesize = filesize(fd);
341 id3->genre_string = id3_get_num_genre(36);
342 break;
344 case AFMT_ADX:
345 if (!get_adx_metadata(fd, id3))
347 DEBUGF("get_adx_metadata error\n");
348 return false;
351 break;
353 case AFMT_NSF:
354 if (!get_nsf_metadata(fd, id3))
356 DEBUGF("get_nsf_metadata error\n");
357 return false;
359 break;
361 case AFMT_AIFF:
362 if (!get_aiff_metadata(fd, id3))
364 return false;
367 break;
369 case AFMT_SAP:
370 if (!get_asap_metadata(fd, id3))
372 DEBUGF("get_sap_metadata error\n");
373 return false;
375 id3->filesize = filesize(fd);
376 id3->genre_string = id3_get_num_genre(36);
377 break;
379 case AFMT_RM_COOK:
380 if (!get_rm_metadata(fd, id3))
382 DEBUGF("get_rm_metadata error\n");
383 return false;
385 break;
387 #endif /* CONFIG_CODEC == SWCODEC */
389 default:
390 /* If we don't know how to read the metadata, assume we can't play
391 the file */
392 return false;
393 break;
396 /* We have successfully read the metadata from the file */
398 lseek(fd, 0, SEEK_SET);
399 strlcpy(id3->path, trackname, sizeof(id3->path));
401 return true;
404 #ifndef __PCTOOL__
405 #if CONFIG_CODEC == SWCODEC
406 void strip_tags(int handle_id)
408 static const unsigned char tag[] = "TAG";
409 static const unsigned char apetag[] = "APETAGEX";
410 size_t len, version;
411 void *tail;
413 if (bufgettail(handle_id, 128, &tail) != 128)
414 return;
416 if (memcmp(tail, tag, 3) == 0)
418 /* Skip id3v1 tag */
419 logf("Cutting off ID3v1 tag");
420 bufcuttail(handle_id, 128);
423 /* Get a new tail, as the old one may have been cut */
424 if (bufgettail(handle_id, 32, &tail) != 32)
425 return;
427 /* Check for APE tag (look for the APE tag footer) */
428 if (memcmp(tail, apetag, 8) != 0)
429 return;
431 /* Read the version and length from the footer */
432 version = get_long_le(&((unsigned char *)tail)[8]);
433 len = get_long_le(&((unsigned char *)tail)[12]);
434 if (version == 2000)
435 len += 32; /* APEv2 has a 32 byte header */
437 /* Skip APE tag */
438 logf("Cutting off APE tag (%ldB)", len);
439 bufcuttail(handle_id, len);
441 #endif /* CONFIG_CODEC == SWCODEC */
442 #endif /* ! __PCTOOL__ */
444 void adjust_mp3entry(struct mp3entry *entry, void *dest, const void *orig)
446 long offset;
447 if (orig > dest)
448 offset = - ((size_t)orig - (size_t)dest);
449 else
450 offset = (size_t)dest - (size_t)orig;
452 if (entry->title)
453 entry->title += offset;
454 if (entry->artist)
455 entry->artist += offset;
456 if (entry->album)
457 entry->album += offset;
458 if (entry->genre_string > (char*)orig
459 && entry->genre_string < (char*)orig + sizeof(struct mp3entry))
460 /* Don't adjust that if it points to an entry of the "genres" array */
461 entry->genre_string += offset;
462 if (entry->track_string)
463 entry->track_string += offset;
464 if (entry->disc_string)
465 entry->disc_string += offset;
466 if (entry->year_string)
467 entry->year_string += offset;
468 if (entry->composer)
469 entry->composer += offset;
470 if (entry->comment)
471 entry->comment += offset;
472 if (entry->albumartist)
473 entry->albumartist += offset;
474 if (entry->grouping)
475 entry->grouping += offset;
476 #if CONFIG_CODEC == SWCODEC
477 if (entry->track_gain_string)
478 entry->track_gain_string += offset;
479 if (entry->album_gain_string)
480 entry->album_gain_string += offset;
481 #endif
482 if (entry->mb_track_id)
483 entry->mb_track_id += offset;
486 void copy_mp3entry(struct mp3entry *dest, const struct mp3entry *orig)
488 memcpy(dest, orig, sizeof(struct mp3entry));
489 adjust_mp3entry(dest, dest, orig);