Fix yellows
[kugel-rb.git] / apps / metadata.c
blob1dfc3763c68db8f790e3dc7ce7a4fe67d8d0413a
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 OR ATRAC3 stream in WAV file (.at3) */
64 [AFMT_PCM_WAV] =
65 AFMT_ENTRY("WAV", "wav", "wav_enc", "wav\0at3\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 /* Atari 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 /* Atari CMC File */
130 [AFMT_CMC] =
131 AFMT_ENTRY("CMC", "asap", NULL, "cmc\0" ),
132 /* Atari CM3 File */
133 [AFMT_CM3] =
134 AFMT_ENTRY("CM3", "asap", NULL, "cm3\0" ),
135 /* Atari CMR File */
136 [AFMT_CMR] =
137 AFMT_ENTRY("CMR", "asap", NULL, "cmr\0" ),
138 /* Atari CMS File */
139 [AFMT_CMS] =
140 AFMT_ENTRY("CMS", "asap", NULL, "cms\0" ),
141 /* Atari DMC File */
142 [AFMT_DMC] =
143 AFMT_ENTRY("DMC", "asap", NULL, "dmc\0" ),
144 /* Atari DLT File */
145 [AFMT_DLT] =
146 AFMT_ENTRY("DLT", "asap", NULL, "dlt\0" ),
147 /* Atari MPT File */
148 [AFMT_MPT] =
149 AFMT_ENTRY("MPT", "asap", NULL, "mpt\0" ),
150 /* Atari MPD File */
151 [AFMT_MPD] =
152 AFMT_ENTRY("MPD", "asap", NULL, "mpd\0" ),
153 /* Atari RMT File */
154 [AFMT_RMT] =
155 AFMT_ENTRY("RMT", "asap", NULL, "rmt\0" ),
156 /* Atari TMC File */
157 [AFMT_TMC] =
158 AFMT_ENTRY("TMC", "asap", NULL, "tmc\0" ),
159 /* Atari TM8 File */
160 [AFMT_TM8] =
161 AFMT_ENTRY("TM8", "asap", NULL, "tm8\0" ),
162 /* Atari TM2 File */
163 [AFMT_TM2] =
164 AFMT_ENTRY("TM2", "asap", NULL, "tm2\0" ),
165 /* Atrac3 in Sony OMA Container */
166 [AFMT_OMA_ATRAC3] =
167 AFMT_ENTRY("ATRAC3", "atrac3_oma", NULL, "oma\0aa3\0" ),
168 /* SMAF (Synthetic music Mobile Application Format) */
169 [AFMT_SMAF] =
170 AFMT_ENTRY("SMAF", "smaf", NULL, "mmf\0" ),
171 /* Sun Audio file */
172 [AFMT_AU] =
173 AFMT_ENTRY("AU", "au", NULL, "au\0snd\0" ),
174 /* VOX (Dialogic telephony file formats) */
175 [AFMT_VOX] =
176 AFMT_ENTRY("VOX", "vox", NULL, "vox\0" ),
177 /* Wave64 */
178 [AFMT_WAVE64] =
179 AFMT_ENTRY("WAVE64", "wav64", NULL, "w64\0" ),
180 #endif
183 #if CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING)
184 /* get REC_FORMAT_* corresponding AFMT_* */
185 const int rec_format_afmt[REC_NUM_FORMATS] =
187 /* give AFMT_UNKNOWN by default */
188 [0 ... REC_NUM_FORMATS-1] = AFMT_UNKNOWN,
189 /* add new entries below this line */
190 [REC_FORMAT_AIFF] = AFMT_AIFF,
191 [REC_FORMAT_MPA_L3] = AFMT_MPA_L3,
192 [REC_FORMAT_WAVPACK] = AFMT_WAVPACK,
193 [REC_FORMAT_PCM_WAV] = AFMT_PCM_WAV,
196 /* get AFMT_* corresponding REC_FORMAT_* */
197 const int afmt_rec_format[AFMT_NUM_CODECS] =
199 /* give -1 by default */
200 [0 ... AFMT_NUM_CODECS-1] = -1,
201 /* add new entries below this line */
202 [AFMT_AIFF] = REC_FORMAT_AIFF,
203 [AFMT_MPA_L3] = REC_FORMAT_MPA_L3,
204 [AFMT_WAVPACK] = REC_FORMAT_WAVPACK,
205 [AFMT_PCM_WAV] = REC_FORMAT_PCM_WAV,
207 #endif /* CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING) */
210 /* Simple file type probing by looking at the filename extension. */
211 unsigned int probe_file_format(const char *filename)
213 char *suffix;
214 unsigned int i;
216 suffix = strrchr(filename, '.');
218 if (suffix == NULL)
220 return AFMT_UNKNOWN;
223 /* skip '.' */
224 suffix++;
226 for (i = 1; i < AFMT_NUM_CODECS; i++)
228 /* search extension list for type */
229 const char *ext = audio_formats[i].ext_list;
233 if (strcasecmp(suffix, ext) == 0)
235 return i;
238 ext += strlen(ext) + 1;
240 while (*ext != '\0');
243 return AFMT_UNKNOWN;
246 /* Note, that this returns false for successful, true for error! */
247 bool mp3info(struct mp3entry *entry, const char *filename)
249 int fd;
250 bool result;
252 fd = open(filename, O_RDONLY);
253 if (fd < 0)
254 return true;
256 result = !get_mp3_metadata(fd, entry, filename);
258 close(fd);
260 return result;
263 /* Get metadata for track - return false if parsing showed problems with the
264 * file that would prevent playback.
266 bool get_metadata(struct mp3entry* id3, int fd, const char* trackname)
268 /* Clear the mp3entry to avoid having bogus pointers appear */
269 memset(id3, 0, sizeof(struct mp3entry));
271 /* Take our best guess at the codec type based on file extension */
272 id3->codectype = probe_file_format(trackname);
274 /* Load codec specific track tag information and confirm the codec type. */
275 switch (id3->codectype)
277 case AFMT_MPA_L1:
278 case AFMT_MPA_L2:
279 case AFMT_MPA_L3:
280 if (!get_mp3_metadata(fd, id3, trackname))
282 return false;
285 break;
287 #if CONFIG_CODEC == SWCODEC
288 case AFMT_FLAC:
289 if (!get_flac_metadata(fd, id3))
291 return false;
294 break;
296 case AFMT_WMA:
297 if (!get_asf_metadata(fd, id3))
299 return false;
301 break;
303 case AFMT_APE:
304 if (!get_monkeys_metadata(fd, id3))
306 return false;
308 read_ape_tags(fd, id3);
309 break;
311 case AFMT_MPC:
312 if (!get_musepack_metadata(fd, id3))
313 return false;
314 read_ape_tags(fd, id3);
315 break;
317 case AFMT_OGG_VORBIS:
318 case AFMT_SPEEX:
319 if (!get_ogg_metadata(fd, id3))/*detects and handles Ogg/Speex files*/
321 return false;
324 break;
326 case AFMT_PCM_WAV:
327 if (!get_wave_metadata(fd, id3))
329 return false;
332 break;
334 case AFMT_WAVPACK:
335 if (!get_wavpack_metadata(fd, id3))
337 return false;
340 read_ape_tags(fd, id3); /* use any apetag info we find */
341 break;
343 case AFMT_A52:
344 if (!get_a52_metadata(fd, id3))
346 return false;
349 break;
351 case AFMT_MP4_ALAC:
352 case AFMT_MP4_AAC:
353 if (!get_mp4_metadata(fd, id3))
355 return false;
358 break;
360 case AFMT_MOD:
361 if (!get_mod_metadata(fd, id3))
363 return false;
366 break;
368 case AFMT_SHN:
369 id3->vbr = true;
370 id3->filesize = filesize(fd);
371 if (!skip_id3v2(fd, id3))
373 return false;
375 /* TODO: read the id3v2 header if it exists */
376 break;
378 case AFMT_SID:
379 if (!get_sid_metadata(fd, id3))
381 return false;
383 break;
385 case AFMT_SPC:
386 if (!get_spc_metadata(fd, id3))
388 DEBUGF("get_spc_metadata error\n");
389 return false;
391 id3->filesize = filesize(fd);
392 id3->genre_string = id3_get_num_genre(36);
393 break;
395 case AFMT_ADX:
396 if (!get_adx_metadata(fd, id3))
398 DEBUGF("get_adx_metadata error\n");
399 return false;
402 break;
404 case AFMT_NSF:
405 if (!get_nsf_metadata(fd, id3))
407 DEBUGF("get_nsf_metadata error\n");
408 return false;
410 break;
412 case AFMT_AIFF:
413 if (!get_aiff_metadata(fd, id3))
415 return false;
418 break;
420 case AFMT_SAP:
421 if (!get_asap_metadata(fd, id3))
423 DEBUGF("get_sap_metadata error\n");
424 return false;
426 id3->filesize = filesize(fd);
427 id3->genre_string = id3_get_num_genre(36);
428 break;
430 case AFMT_CMC:
431 case AFMT_CM3:
432 case AFMT_CMR:
433 case AFMT_CMS:
434 case AFMT_DMC:
435 case AFMT_DLT:
436 case AFMT_MPT:
437 case AFMT_MPD:
438 case AFMT_RMT:
439 case AFMT_TMC:
440 case AFMT_TM8:
441 case AFMT_TM2:
442 id3->bitrate = 706;
443 id3->frequency = 44100;
444 id3->vbr = false;
445 id3->filesize = filesize(fd);
446 id3->genre_string = id3_get_num_genre(36);
447 break;
448 case AFMT_RM_COOK:
449 if (!get_rm_metadata(fd, id3))
451 DEBUGF("get_rm_metadata error\n");
452 return false;
454 break;
455 case AFMT_OMA_ATRAC3:
456 if (!get_oma_metadata(fd, id3))
458 DEBUGF("get_oma_metadata error\n");
459 return false;
461 break;
463 case AFMT_SMAF:
464 if (!get_smaf_metadata(fd, id3))
466 DEBUGF("get_smaf_metadata error\n");
467 return false;
469 break;
471 case AFMT_AU:
472 if (!get_au_metadata(fd, id3))
474 DEBUGF("get_au_metadata error\n");
475 return false;
477 break;
479 case AFMT_VOX:
480 if (!get_vox_metadata(fd, id3))
482 DEBUGF("get_vox_metadata error\n");
483 return false;
485 break;
487 case AFMT_WAVE64:
488 if (!get_wave64_metadata(fd, id3))
490 DEBUGF("get_wave64_metadata error\n");
491 return false;
493 break;
495 #endif /* CONFIG_CODEC == SWCODEC */
497 default:
498 /* If we don't know how to read the metadata, assume we can't play
499 the file */
500 return false;
501 break;
504 /* We have successfully read the metadata from the file */
506 lseek(fd, 0, SEEK_SET);
507 strlcpy(id3->path, trackname, sizeof(id3->path));
509 return true;
512 #ifndef __PCTOOL__
513 #if CONFIG_CODEC == SWCODEC
514 void strip_tags(int handle_id)
516 static const unsigned char tag[] = "TAG";
517 static const unsigned char apetag[] = "APETAGEX";
518 size_t len, version;
519 void *tail;
521 if (bufgettail(handle_id, 128, &tail) != 128)
522 return;
524 if (memcmp(tail, tag, 3) == 0)
526 /* Skip id3v1 tag */
527 logf("Cutting off ID3v1 tag");
528 bufcuttail(handle_id, 128);
531 /* Get a new tail, as the old one may have been cut */
532 if (bufgettail(handle_id, 32, &tail) != 32)
533 return;
535 /* Check for APE tag (look for the APE tag footer) */
536 if (memcmp(tail, apetag, 8) != 0)
537 return;
539 /* Read the version and length from the footer */
540 version = get_long_le(&((unsigned char *)tail)[8]);
541 len = get_long_le(&((unsigned char *)tail)[12]);
542 if (version == 2000)
543 len += 32; /* APEv2 has a 32 byte header */
545 /* Skip APE tag */
546 logf("Cutting off APE tag (%ldB)", len);
547 bufcuttail(handle_id, len);
549 #endif /* CONFIG_CODEC == SWCODEC */
550 #endif /* ! __PCTOOL__ */
552 void adjust_mp3entry(struct mp3entry *entry, void *dest, const void *orig)
554 long offset;
555 if (orig > dest)
556 offset = - ((size_t)orig - (size_t)dest);
557 else
558 offset = (size_t)dest - (size_t)orig;
560 if (entry->title)
561 entry->title += offset;
562 if (entry->artist)
563 entry->artist += offset;
564 if (entry->album)
565 entry->album += offset;
566 if (entry->genre_string > (char*)orig
567 && entry->genre_string < (char*)orig + sizeof(struct mp3entry))
568 /* Don't adjust that if it points to an entry of the "genres" array */
569 entry->genre_string += offset;
570 if (entry->track_string)
571 entry->track_string += offset;
572 if (entry->disc_string)
573 entry->disc_string += offset;
574 if (entry->year_string)
575 entry->year_string += offset;
576 if (entry->composer)
577 entry->composer += offset;
578 if (entry->comment)
579 entry->comment += offset;
580 if (entry->albumartist)
581 entry->albumartist += offset;
582 if (entry->grouping)
583 entry->grouping += offset;
584 #if CONFIG_CODEC == SWCODEC
585 if (entry->track_gain_string)
586 entry->track_gain_string += offset;
587 if (entry->album_gain_string)
588 entry->album_gain_string += offset;
589 #endif
590 if (entry->mb_track_id)
591 entry->mb_track_id += offset;
594 void copy_mp3entry(struct mp3entry *dest, const struct mp3entry *orig)
596 memcpy(dest, orig, sizeof(struct mp3entry));
597 adjust_mp3entry(dest, dest, orig);