1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
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 */
47 AFMT_ENTRY("???", NULL
, NULL
, NULL
),
49 /* MPEG Audio layer 1 */
51 AFMT_ENTRY("MP1", "mpa", NULL
, "mp1\0" ),
52 /* MPEG Audio layer 2 */
54 AFMT_ENTRY("MP2", "mpa", NULL
, "mpa\0mp2\0" ),
55 /* MPEG Audio layer 3 */
57 AFMT_ENTRY("MP3", "mpa", "mp3_enc", "mp3\0" ),
59 #if CONFIG_CODEC == SWCODEC
60 /* Audio Interchange File Format */
62 AFMT_ENTRY("AIFF", "aiff", "aiff_enc", "aiff\0aif\0"),
63 /* Uncompressed PCM in a WAV file OR ATRAC3 stream in WAV file (.at3) */
65 AFMT_ENTRY("WAV", "wav", "wav_enc", "wav\0at3\0" ),
68 AFMT_ENTRY("Ogg", "vorbis", NULL
, "ogg\0oga\0" ),
71 AFMT_ENTRY("FLAC", "flac", NULL
, "flac\0" ),
74 AFMT_ENTRY("MPC", "mpc", NULL
, "mpc\0" ),
75 /* A/52 (aka AC3) audio */
77 AFMT_ENTRY("AC3", "a52", NULL
, "a52\0ac3\0" ),
80 AFMT_ENTRY("WV", "wavpack", "wavpack_enc", "wv\0" ),
81 /* Apple Lossless Audio Codec */
83 AFMT_ENTRY("ALAC", "alac", NULL
, "m4a\0m4b\0" ),
84 /* Advanced Audio Coding in M4A container */
86 AFMT_ENTRY("AAC", "aac", NULL
, "mp4\0" ),
89 AFMT_ENTRY("SHN", "shorten", NULL
, "shn\0" ),
92 AFMT_ENTRY("SID", "sid", NULL
, "sid\0" ),
95 AFMT_ENTRY("ADX", "adx", NULL
, "adx\0" ),
96 /* NESM (NES Sound Format) */
98 AFMT_ENTRY("NSF", "nsf", NULL
, "nsf\0nsfe\0" ),
99 /* Speex File Format */
101 AFMT_ENTRY("Speex","speex", NULL
, "spx\0" ),
102 /* SPC700 Save State */
104 AFMT_ENTRY("SPC", "spc", NULL
, "spc\0" ),
105 /* APE (Monkey's Audio) */
107 AFMT_ENTRY("APE", "ape", NULL
, "ape\0mac\0" ),
108 /* WMA (WMAV1/V2 in ASF) */
110 AFMT_ENTRY("WMA", "wma", NULL
, "wma\0wmv\0asf\0" ),
113 AFMT_ENTRY("MOD", "mod", NULL
, "mod\0" ),
116 AFMT_ENTRY("SAP", "asap", NULL
, "sap\0" ),
119 AFMT_ENTRY("Cook", "cook", NULL
, "rm\0ra\0rmvb\0" ),
122 AFMT_ENTRY("RAAC", "raac", NULL
, "rm\0ra\0rmvb\0" ),
125 AFMT_ENTRY("AC3", "a52_rm", NULL
, "rm\0ra\0rmvb\0" ),
126 /* ATRAC3 in RM/RA */
128 AFMT_ENTRY("ATRAC3","atrac3_rm", NULL
, "rm\0ra\0rmvb\0" ),
131 AFMT_ENTRY("CMC", "asap", NULL
, "cmc\0" ),
134 AFMT_ENTRY("CM3", "asap", NULL
, "cm3\0" ),
137 AFMT_ENTRY("CMR", "asap", NULL
, "cmr\0" ),
140 AFMT_ENTRY("CMS", "asap", NULL
, "cms\0" ),
143 AFMT_ENTRY("DMC", "asap", NULL
, "dmc\0" ),
146 AFMT_ENTRY("DLT", "asap", NULL
, "dlt\0" ),
149 AFMT_ENTRY("MPT", "asap", NULL
, "mpt\0" ),
152 AFMT_ENTRY("MPD", "asap", NULL
, "mpd\0" ),
155 AFMT_ENTRY("RMT", "asap", NULL
, "rmt\0" ),
158 AFMT_ENTRY("TMC", "asap", NULL
, "tmc\0" ),
161 AFMT_ENTRY("TM8", "asap", NULL
, "tm8\0" ),
164 AFMT_ENTRY("TM2", "asap", NULL
, "tm2\0" ),
165 /* Atrac3 in Sony OMA Container */
167 AFMT_ENTRY("ATRAC3", "atrac3_oma", NULL
, "oma\0aa3\0" ),
168 /* SMAF (Synthetic music Mobile Application Format) */
170 AFMT_ENTRY("SMAF", "smaf", NULL
, "mmf\0" ),
173 AFMT_ENTRY("AU", "au", NULL
, "au\0snd\0" ),
174 /* VOX (Dialogic telephony file formats) */
176 AFMT_ENTRY("VOX", "vox", NULL
, "vox\0" ),
180 #if CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING)
181 /* get REC_FORMAT_* corresponding AFMT_* */
182 const int rec_format_afmt
[REC_NUM_FORMATS
] =
184 /* give AFMT_UNKNOWN by default */
185 [0 ... REC_NUM_FORMATS
-1] = AFMT_UNKNOWN
,
186 /* add new entries below this line */
187 [REC_FORMAT_AIFF
] = AFMT_AIFF
,
188 [REC_FORMAT_MPA_L3
] = AFMT_MPA_L3
,
189 [REC_FORMAT_WAVPACK
] = AFMT_WAVPACK
,
190 [REC_FORMAT_PCM_WAV
] = AFMT_PCM_WAV
,
193 /* get AFMT_* corresponding REC_FORMAT_* */
194 const int afmt_rec_format
[AFMT_NUM_CODECS
] =
196 /* give -1 by default */
197 [0 ... AFMT_NUM_CODECS
-1] = -1,
198 /* add new entries below this line */
199 [AFMT_AIFF
] = REC_FORMAT_AIFF
,
200 [AFMT_MPA_L3
] = REC_FORMAT_MPA_L3
,
201 [AFMT_WAVPACK
] = REC_FORMAT_WAVPACK
,
202 [AFMT_PCM_WAV
] = REC_FORMAT_PCM_WAV
,
204 #endif /* CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING) */
207 /* Simple file type probing by looking at the filename extension. */
208 unsigned int probe_file_format(const char *filename
)
213 suffix
= strrchr(filename
, '.');
223 for (i
= 1; i
< AFMT_NUM_CODECS
; i
++)
225 /* search extension list for type */
226 const char *ext
= audio_formats
[i
].ext_list
;
230 if (strcasecmp(suffix
, ext
) == 0)
235 ext
+= strlen(ext
) + 1;
237 while (*ext
!= '\0');
243 /* Note, that this returns false for successful, true for error! */
244 bool mp3info(struct mp3entry
*entry
, const char *filename
)
249 fd
= open(filename
, O_RDONLY
);
253 result
= !get_mp3_metadata(fd
, entry
, filename
);
260 /* Get metadata for track - return false if parsing showed problems with the
261 * file that would prevent playback.
263 bool get_metadata(struct mp3entry
* id3
, int fd
, const char* trackname
)
265 /* Clear the mp3entry to avoid having bogus pointers appear */
266 memset(id3
, 0, sizeof(struct mp3entry
));
268 /* Take our best guess at the codec type based on file extension */
269 id3
->codectype
= probe_file_format(trackname
);
271 /* Load codec specific track tag information and confirm the codec type. */
272 switch (id3
->codectype
)
277 if (!get_mp3_metadata(fd
, id3
, trackname
))
284 #if CONFIG_CODEC == SWCODEC
286 if (!get_flac_metadata(fd
, id3
))
294 if (!get_asf_metadata(fd
, id3
))
301 if (!get_monkeys_metadata(fd
, id3
))
305 read_ape_tags(fd
, id3
);
309 if (!get_musepack_metadata(fd
, id3
))
311 read_ape_tags(fd
, id3
);
314 case AFMT_OGG_VORBIS
:
316 if (!get_ogg_metadata(fd
, id3
))/*detects and handles Ogg/Speex files*/
324 if (!get_wave_metadata(fd
, id3
))
332 if (!get_wavpack_metadata(fd
, id3
))
337 read_ape_tags(fd
, id3
); /* use any apetag info we find */
341 if (!get_a52_metadata(fd
, id3
))
350 if (!get_mp4_metadata(fd
, id3
))
358 if (!get_mod_metadata(fd
, id3
))
367 id3
->filesize
= filesize(fd
);
368 if (!skip_id3v2(fd
, id3
))
372 /* TODO: read the id3v2 header if it exists */
376 if (!get_sid_metadata(fd
, id3
))
383 if (!get_spc_metadata(fd
, id3
))
385 DEBUGF("get_spc_metadata error\n");
388 id3
->filesize
= filesize(fd
);
389 id3
->genre_string
= id3_get_num_genre(36);
393 if (!get_adx_metadata(fd
, id3
))
395 DEBUGF("get_adx_metadata error\n");
402 if (!get_nsf_metadata(fd
, id3
))
404 DEBUGF("get_nsf_metadata error\n");
410 if (!get_aiff_metadata(fd
, id3
))
418 if (!get_asap_metadata(fd
, id3
))
420 DEBUGF("get_sap_metadata error\n");
423 id3
->filesize
= filesize(fd
);
424 id3
->genre_string
= id3_get_num_genre(36);
440 id3
->frequency
= 44100;
442 id3
->filesize
= filesize(fd
);
443 id3
->genre_string
= id3_get_num_genre(36);
446 if (!get_rm_metadata(fd
, id3
))
448 DEBUGF("get_rm_metadata error\n");
452 case AFMT_OMA_ATRAC3
:
453 if (!get_oma_metadata(fd
, id3
))
455 DEBUGF("get_oma_metadata error\n");
461 if (!get_smaf_metadata(fd
, id3
))
463 DEBUGF("get_smaf_metadata error\n");
469 if (!get_au_metadata(fd
, id3
))
471 DEBUGF("get_au_metadata error\n");
477 if (!get_vox_metadata(fd
, id3
))
479 DEBUGF("get_vox_metadata error\n");
484 #endif /* CONFIG_CODEC == SWCODEC */
487 /* If we don't know how to read the metadata, assume we can't play
493 /* We have successfully read the metadata from the file */
495 lseek(fd
, 0, SEEK_SET
);
496 strlcpy(id3
->path
, trackname
, sizeof(id3
->path
));
502 #if CONFIG_CODEC == SWCODEC
503 void strip_tags(int handle_id
)
505 static const unsigned char tag
[] = "TAG";
506 static const unsigned char apetag
[] = "APETAGEX";
510 if (bufgettail(handle_id
, 128, &tail
) != 128)
513 if (memcmp(tail
, tag
, 3) == 0)
516 logf("Cutting off ID3v1 tag");
517 bufcuttail(handle_id
, 128);
520 /* Get a new tail, as the old one may have been cut */
521 if (bufgettail(handle_id
, 32, &tail
) != 32)
524 /* Check for APE tag (look for the APE tag footer) */
525 if (memcmp(tail
, apetag
, 8) != 0)
528 /* Read the version and length from the footer */
529 version
= get_long_le(&((unsigned char *)tail
)[8]);
530 len
= get_long_le(&((unsigned char *)tail
)[12]);
532 len
+= 32; /* APEv2 has a 32 byte header */
535 logf("Cutting off APE tag (%ldB)", len
);
536 bufcuttail(handle_id
, len
);
538 #endif /* CONFIG_CODEC == SWCODEC */
539 #endif /* ! __PCTOOL__ */
541 void adjust_mp3entry(struct mp3entry
*entry
, void *dest
, const void *orig
)
545 offset
= - ((size_t)orig
- (size_t)dest
);
547 offset
= (size_t)dest
- (size_t)orig
;
550 entry
->title
+= offset
;
552 entry
->artist
+= offset
;
554 entry
->album
+= offset
;
555 if (entry
->genre_string
> (char*)orig
556 && entry
->genre_string
< (char*)orig
+ sizeof(struct mp3entry
))
557 /* Don't adjust that if it points to an entry of the "genres" array */
558 entry
->genre_string
+= offset
;
559 if (entry
->track_string
)
560 entry
->track_string
+= offset
;
561 if (entry
->disc_string
)
562 entry
->disc_string
+= offset
;
563 if (entry
->year_string
)
564 entry
->year_string
+= offset
;
566 entry
->composer
+= offset
;
568 entry
->comment
+= offset
;
569 if (entry
->albumartist
)
570 entry
->albumartist
+= offset
;
572 entry
->grouping
+= offset
;
573 #if CONFIG_CODEC == SWCODEC
574 if (entry
->track_gain_string
)
575 entry
->track_gain_string
+= offset
;
576 if (entry
->album_gain_string
)
577 entry
->album_gain_string
+= offset
;
579 if (entry
->mb_track_id
)
580 entry
->mb_track_id
+= offset
;
583 void copy_mp3entry(struct mp3entry
*dest
, const struct mp3entry
*orig
)
585 memcpy(dest
, orig
, sizeof(struct mp3entry
));
586 adjust_mp3entry(dest
, dest
, orig
);