add True Audio (TTA) codec
[kugel-rb.git] / apps / metadata.c
blob076b7539582361ef40ef793d3e7117ef3148b102
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 /* WMA Professional in ASF */
111 [AFMT_WMAPRO] =
112 AFMT_ENTRY("WMAPro", "wmapro", NULL, "wma\0wmv\0asf\0" ),
113 /* Amiga MOD File */
114 [AFMT_MOD] =
115 AFMT_ENTRY("MOD", "mod", NULL, "mod\0" ),
116 /* Atari SAP File */
117 [AFMT_SAP] =
118 AFMT_ENTRY("SAP", "asap", NULL, "sap\0" ),
119 /* Cook in RM/RA */
120 [AFMT_RM_COOK] =
121 AFMT_ENTRY("Cook", "cook", NULL, "rm\0ra\0rmvb\0" ),
122 /* AAC in RM/RA */
123 [AFMT_RM_AAC] =
124 AFMT_ENTRY("RAAC", "raac", NULL, "rm\0ra\0rmvb\0" ),
125 /* AC3 in RM/RA */
126 [AFMT_RM_AC3] =
127 AFMT_ENTRY("AC3", "a52_rm", NULL, "rm\0ra\0rmvb\0" ),
128 /* ATRAC3 in RM/RA */
129 [AFMT_RM_ATRAC3] =
130 AFMT_ENTRY("ATRAC3","atrac3_rm", NULL, "rm\0ra\0rmvb\0" ),
131 /* Atari CMC File */
132 [AFMT_CMC] =
133 AFMT_ENTRY("CMC", "asap", NULL, "cmc\0" ),
134 /* Atari CM3 File */
135 [AFMT_CM3] =
136 AFMT_ENTRY("CM3", "asap", NULL, "cm3\0" ),
137 /* Atari CMR File */
138 [AFMT_CMR] =
139 AFMT_ENTRY("CMR", "asap", NULL, "cmr\0" ),
140 /* Atari CMS File */
141 [AFMT_CMS] =
142 AFMT_ENTRY("CMS", "asap", NULL, "cms\0" ),
143 /* Atari DMC File */
144 [AFMT_DMC] =
145 AFMT_ENTRY("DMC", "asap", NULL, "dmc\0" ),
146 /* Atari DLT File */
147 [AFMT_DLT] =
148 AFMT_ENTRY("DLT", "asap", NULL, "dlt\0" ),
149 /* Atari MPT File */
150 [AFMT_MPT] =
151 AFMT_ENTRY("MPT", "asap", NULL, "mpt\0" ),
152 /* Atari MPD File */
153 [AFMT_MPD] =
154 AFMT_ENTRY("MPD", "asap", NULL, "mpd\0" ),
155 /* Atari RMT File */
156 [AFMT_RMT] =
157 AFMT_ENTRY("RMT", "asap", NULL, "rmt\0" ),
158 /* Atari TMC File */
159 [AFMT_TMC] =
160 AFMT_ENTRY("TMC", "asap", NULL, "tmc\0" ),
161 /* Atari TM8 File */
162 [AFMT_TM8] =
163 AFMT_ENTRY("TM8", "asap", NULL, "tm8\0" ),
164 /* Atari TM2 File */
165 [AFMT_TM2] =
166 AFMT_ENTRY("TM2", "asap", NULL, "tm2\0" ),
167 /* Atrac3 in Sony OMA Container */
168 [AFMT_OMA_ATRAC3] =
169 AFMT_ENTRY("ATRAC3", "atrac3_oma", NULL, "oma\0aa3\0" ),
170 /* SMAF (Synthetic music Mobile Application Format) */
171 [AFMT_SMAF] =
172 AFMT_ENTRY("SMAF", "smaf", NULL, "mmf\0" ),
173 /* Sun Audio file */
174 [AFMT_AU] =
175 AFMT_ENTRY("AU", "au", NULL, "au\0snd\0" ),
176 /* VOX (Dialogic telephony file formats) */
177 [AFMT_VOX] =
178 AFMT_ENTRY("VOX", "vox", NULL, "vox\0" ),
179 /* Wave64 */
180 [AFMT_WAVE64] =
181 AFMT_ENTRY("WAVE64", "wav64", NULL, "w64\0" ),
182 /* True Audio */
183 [AFMT_TTA] =
184 AFMT_ENTRY("TTA", "tta", NULL, "tta\0" ),
185 #endif
188 #if CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING)
189 /* get REC_FORMAT_* corresponding AFMT_* */
190 const int rec_format_afmt[REC_NUM_FORMATS] =
192 /* give AFMT_UNKNOWN by default */
193 [0 ... REC_NUM_FORMATS-1] = AFMT_UNKNOWN,
194 /* add new entries below this line */
195 [REC_FORMAT_AIFF] = AFMT_AIFF,
196 [REC_FORMAT_MPA_L3] = AFMT_MPA_L3,
197 [REC_FORMAT_WAVPACK] = AFMT_WAVPACK,
198 [REC_FORMAT_PCM_WAV] = AFMT_PCM_WAV,
201 /* get AFMT_* corresponding REC_FORMAT_* */
202 const int afmt_rec_format[AFMT_NUM_CODECS] =
204 /* give -1 by default */
205 [0 ... AFMT_NUM_CODECS-1] = -1,
206 /* add new entries below this line */
207 [AFMT_AIFF] = REC_FORMAT_AIFF,
208 [AFMT_MPA_L3] = REC_FORMAT_MPA_L3,
209 [AFMT_WAVPACK] = REC_FORMAT_WAVPACK,
210 [AFMT_PCM_WAV] = REC_FORMAT_PCM_WAV,
212 #endif /* CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING) */
215 /* Simple file type probing by looking at the filename extension. */
216 unsigned int probe_file_format(const char *filename)
218 char *suffix;
219 unsigned int i;
221 suffix = strrchr(filename, '.');
223 if (suffix == NULL)
225 return AFMT_UNKNOWN;
228 /* skip '.' */
229 suffix++;
231 for (i = 1; i < AFMT_NUM_CODECS; i++)
233 /* search extension list for type */
234 const char *ext = audio_formats[i].ext_list;
238 if (strcasecmp(suffix, ext) == 0)
240 return i;
243 ext += strlen(ext) + 1;
245 while (*ext != '\0');
248 return AFMT_UNKNOWN;
251 /* Note, that this returns false for successful, true for error! */
252 bool mp3info(struct mp3entry *entry, const char *filename)
254 int fd;
255 bool result;
257 fd = open(filename, O_RDONLY);
258 if (fd < 0)
259 return true;
261 result = !get_mp3_metadata(fd, entry, filename);
263 close(fd);
265 return result;
268 /* Get metadata for track - return false if parsing showed problems with the
269 * file that would prevent playback.
271 bool get_metadata(struct mp3entry* id3, int fd, const char* trackname)
273 /* Clear the mp3entry to avoid having bogus pointers appear */
274 memset(id3, 0, sizeof(struct mp3entry));
276 /* Take our best guess at the codec type based on file extension */
277 id3->codectype = probe_file_format(trackname);
279 /* Load codec specific track tag information and confirm the codec type. */
280 switch (id3->codectype)
282 case AFMT_MPA_L1:
283 case AFMT_MPA_L2:
284 case AFMT_MPA_L3:
285 if (!get_mp3_metadata(fd, id3, trackname))
287 return false;
290 break;
292 #if CONFIG_CODEC == SWCODEC
293 case AFMT_FLAC:
294 if (!get_flac_metadata(fd, id3))
296 return false;
299 break;
301 case AFMT_WMA:
302 if (!get_asf_metadata(fd, id3))
304 return false;
306 break;
308 case AFMT_APE:
309 if (!get_monkeys_metadata(fd, id3))
311 return false;
313 read_ape_tags(fd, id3);
314 break;
316 case AFMT_MPC:
317 if (!get_musepack_metadata(fd, id3))
318 return false;
319 read_ape_tags(fd, id3);
320 break;
322 case AFMT_OGG_VORBIS:
323 case AFMT_SPEEX:
324 if (!get_ogg_metadata(fd, id3))/*detects and handles Ogg/Speex files*/
326 return false;
329 break;
331 case AFMT_PCM_WAV:
332 if (!get_wave_metadata(fd, id3))
334 return false;
337 break;
339 case AFMT_WAVPACK:
340 if (!get_wavpack_metadata(fd, id3))
342 return false;
345 read_ape_tags(fd, id3); /* use any apetag info we find */
346 break;
348 case AFMT_A52:
349 if (!get_a52_metadata(fd, id3))
351 return false;
354 break;
356 case AFMT_MP4_ALAC:
357 case AFMT_MP4_AAC:
358 if (!get_mp4_metadata(fd, id3))
360 return false;
363 break;
365 case AFMT_MOD:
366 if (!get_mod_metadata(fd, id3))
368 return false;
371 break;
373 case AFMT_SHN:
374 id3->vbr = true;
375 id3->filesize = filesize(fd);
376 if (!skip_id3v2(fd, id3))
378 return false;
380 /* TODO: read the id3v2 header if it exists */
381 break;
383 case AFMT_SID:
384 if (!get_sid_metadata(fd, id3))
386 return false;
388 break;
390 case AFMT_SPC:
391 if (!get_spc_metadata(fd, id3))
393 DEBUGF("get_spc_metadata error\n");
394 return false;
396 id3->filesize = filesize(fd);
397 id3->genre_string = id3_get_num_genre(36);
398 break;
400 case AFMT_ADX:
401 if (!get_adx_metadata(fd, id3))
403 DEBUGF("get_adx_metadata error\n");
404 return false;
407 break;
409 case AFMT_NSF:
410 if (!get_nsf_metadata(fd, id3))
412 DEBUGF("get_nsf_metadata error\n");
413 return false;
415 break;
417 case AFMT_AIFF:
418 if (!get_aiff_metadata(fd, id3))
420 return false;
423 break;
425 case AFMT_SAP:
426 if (!get_asap_metadata(fd, id3))
428 DEBUGF("get_sap_metadata error\n");
429 return false;
431 id3->filesize = filesize(fd);
432 id3->genre_string = id3_get_num_genre(36);
433 break;
435 case AFMT_CMC:
436 case AFMT_CM3:
437 case AFMT_CMR:
438 case AFMT_CMS:
439 case AFMT_DMC:
440 case AFMT_DLT:
441 case AFMT_MPT:
442 case AFMT_MPD:
443 case AFMT_RMT:
444 case AFMT_TMC:
445 case AFMT_TM8:
446 case AFMT_TM2:
447 id3->bitrate = 706;
448 id3->frequency = 44100;
449 id3->vbr = false;
450 id3->filesize = filesize(fd);
451 id3->genre_string = id3_get_num_genre(36);
452 break;
453 case AFMT_RM_COOK:
454 if (!get_rm_metadata(fd, id3))
456 DEBUGF("get_rm_metadata error\n");
457 return false;
459 break;
460 case AFMT_OMA_ATRAC3:
461 if (!get_oma_metadata(fd, id3))
463 DEBUGF("get_oma_metadata error\n");
464 return false;
466 break;
468 case AFMT_SMAF:
469 if (!get_smaf_metadata(fd, id3))
471 DEBUGF("get_smaf_metadata error\n");
472 return false;
474 break;
476 case AFMT_AU:
477 if (!get_au_metadata(fd, id3))
479 DEBUGF("get_au_metadata error\n");
480 return false;
482 break;
484 case AFMT_VOX:
485 if (!get_vox_metadata(fd, id3))
487 DEBUGF("get_vox_metadata error\n");
488 return false;
490 break;
492 case AFMT_WAVE64:
493 if (!get_wave64_metadata(fd, id3))
495 DEBUGF("get_wave64_metadata error\n");
496 return false;
498 break;
500 case AFMT_TTA:
501 if (!get_tta_metadata(fd, id3))
503 DEBUGF("get_tta_metadata error\n");
504 return false;
506 break;
508 #endif /* CONFIG_CODEC == SWCODEC */
510 default:
511 /* If we don't know how to read the metadata, assume we can't play
512 the file */
513 return false;
514 break;
517 /* We have successfully read the metadata from the file */
519 lseek(fd, 0, SEEK_SET);
520 strlcpy(id3->path, trackname, sizeof(id3->path));
522 return true;
525 #ifndef __PCTOOL__
526 #if CONFIG_CODEC == SWCODEC
527 void strip_tags(int handle_id)
529 static const unsigned char tag[] = "TAG";
530 static const unsigned char apetag[] = "APETAGEX";
531 size_t len, version;
532 void *tail;
534 if (bufgettail(handle_id, 128, &tail) != 128)
535 return;
537 if (memcmp(tail, tag, 3) == 0)
539 /* Skip id3v1 tag */
540 logf("Cutting off ID3v1 tag");
541 bufcuttail(handle_id, 128);
544 /* Get a new tail, as the old one may have been cut */
545 if (bufgettail(handle_id, 32, &tail) != 32)
546 return;
548 /* Check for APE tag (look for the APE tag footer) */
549 if (memcmp(tail, apetag, 8) != 0)
550 return;
552 /* Read the version and length from the footer */
553 version = get_long_le(&((unsigned char *)tail)[8]);
554 len = get_long_le(&((unsigned char *)tail)[12]);
555 if (version == 2000)
556 len += 32; /* APEv2 has a 32 byte header */
558 /* Skip APE tag */
559 logf("Cutting off APE tag (%ldB)", len);
560 bufcuttail(handle_id, len);
562 #endif /* CONFIG_CODEC == SWCODEC */
563 #endif /* ! __PCTOOL__ */
565 void adjust_mp3entry(struct mp3entry *entry, void *dest, const void *orig)
567 long offset;
568 if (orig > dest)
569 offset = - ((size_t)orig - (size_t)dest);
570 else
571 offset = (size_t)dest - (size_t)orig;
573 if (entry->title)
574 entry->title += offset;
575 if (entry->artist)
576 entry->artist += offset;
577 if (entry->album)
578 entry->album += offset;
579 if (entry->genre_string > (char*)orig
580 && entry->genre_string < (char*)orig + sizeof(struct mp3entry))
581 /* Don't adjust that if it points to an entry of the "genres" array */
582 entry->genre_string += offset;
583 if (entry->track_string)
584 entry->track_string += offset;
585 if (entry->disc_string)
586 entry->disc_string += offset;
587 if (entry->year_string)
588 entry->year_string += offset;
589 if (entry->composer)
590 entry->composer += offset;
591 if (entry->comment)
592 entry->comment += offset;
593 if (entry->albumartist)
594 entry->albumartist += offset;
595 if (entry->grouping)
596 entry->grouping += offset;
597 #if CONFIG_CODEC == SWCODEC
598 if (entry->track_gain_string)
599 entry->track_gain_string += offset;
600 if (entry->album_gain_string)
601 entry->album_gain_string += offset;
602 #endif
603 if (entry->mb_track_id)
604 entry->mb_track_id += offset;
607 void copy_mp3entry(struct mp3entry *dest, const struct mp3entry *orig)
609 memcpy(dest, orig, sizeof(struct mp3entry));
610 adjust_mp3entry(dest, dest, orig);