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 ****************************************************************************/
33 #include "metadata/metadata_parsers.h"
35 #if CONFIG_CODEC == SWCODEC
37 /* For trailing tag stripping */
38 #include "buffering.h"
40 #include "metadata/metadata_common.h"
42 #endif /* CONFIG_CODEC == SWCODEC */
44 const struct afmt_entry audio_formats
[AFMT_NUM_CODECS
] =
46 /* Unknown file format */
48 AFMT_ENTRY("???", NULL
, NULL
, NULL
),
50 /* MPEG Audio layer 1 */
52 AFMT_ENTRY("MP1", "mpa", NULL
, "mp1\0" ),
53 /* MPEG Audio layer 2 */
55 AFMT_ENTRY("MP2", "mpa", NULL
, "mpa\0mp2\0" ),
56 /* MPEG Audio layer 3 */
58 AFMT_ENTRY("MP3", "mpa", "mp3_enc", "mp3\0" ),
60 #if CONFIG_CODEC == SWCODEC
61 /* Audio Interchange File Format */
63 AFMT_ENTRY("AIFF", "aiff", "aiff_enc", "aiff\0aif\0"),
64 /* Uncompressed PCM in a WAV file */
66 AFMT_ENTRY("WAV", "wav", "wav_enc", "wav\0" ),
69 AFMT_ENTRY("Ogg", "vorbis", NULL
, "ogg\0oga\0" ),
72 AFMT_ENTRY("FLAC", "flac", NULL
, "flac\0" ),
75 AFMT_ENTRY("MPC", "mpc", NULL
, "mpc\0" ),
76 /* A/52 (aka AC3) audio */
78 AFMT_ENTRY("AC3", "a52", NULL
, "a52\0ac3\0" ),
81 AFMT_ENTRY("WV", "wavpack", "wavpack_enc", "wv\0" ),
82 /* Apple Lossless Audio Codec */
84 AFMT_ENTRY("ALAC", "alac", NULL
, "m4a\0m4b\0" ),
85 /* Advanced Audio Coding in M4A container */
87 AFMT_ENTRY("AAC", "aac", NULL
, "mp4\0" ),
90 AFMT_ENTRY("SHN", "shorten", NULL
, "shn\0" ),
93 AFMT_ENTRY("SID", "sid", NULL
, "sid\0" ),
96 AFMT_ENTRY("ADX", "adx", NULL
, "adx\0" ),
97 /* NESM (NES Sound Format) */
99 AFMT_ENTRY("NSF", "nsf", NULL
, "nsf\0nsfe\0" ),
100 /* Speex File Format */
102 AFMT_ENTRY("Speex","speex", NULL
, "spx\0" ),
103 /* SPC700 Save State */
105 AFMT_ENTRY("SPC", "spc", NULL
, "spc\0" ),
106 /* APE (Monkey's Audio) */
108 AFMT_ENTRY("APE", "ape", NULL
, "ape\0mac\0" ),
109 /* WMA (WMAV1/V2 in ASF) */
111 AFMT_ENTRY("WMA", "wma", NULL
, "wma\0wmv\0asf\0" ),
114 AFMT_ENTRY("MOD", "mod", NULL
, "mod\0" ),
117 AFMT_ENTRY("SAP", "asap", NULL
, "sap\0" ),
121 #if CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING)
122 /* get REC_FORMAT_* corresponding AFMT_* */
123 const int rec_format_afmt
[REC_NUM_FORMATS
] =
125 /* give AFMT_UNKNOWN by default */
126 [0 ... REC_NUM_FORMATS
-1] = AFMT_UNKNOWN
,
127 /* add new entries below this line */
128 [REC_FORMAT_AIFF
] = AFMT_AIFF
,
129 [REC_FORMAT_MPA_L3
] = AFMT_MPA_L3
,
130 [REC_FORMAT_WAVPACK
] = AFMT_WAVPACK
,
131 [REC_FORMAT_PCM_WAV
] = AFMT_PCM_WAV
,
134 /* get AFMT_* corresponding REC_FORMAT_* */
135 const int afmt_rec_format
[AFMT_NUM_CODECS
] =
137 /* give -1 by default */
138 [0 ... AFMT_NUM_CODECS
-1] = -1,
139 /* add new entries below this line */
140 [AFMT_AIFF
] = REC_FORMAT_AIFF
,
141 [AFMT_MPA_L3
] = REC_FORMAT_MPA_L3
,
142 [AFMT_WAVPACK
] = REC_FORMAT_WAVPACK
,
143 [AFMT_PCM_WAV
] = REC_FORMAT_PCM_WAV
,
145 #endif /* CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING) */
148 /* Simple file type probing by looking at the filename extension. */
149 unsigned int probe_file_format(const char *filename
)
154 suffix
= strrchr(filename
, '.');
164 for (i
= 1; i
< AFMT_NUM_CODECS
; i
++)
166 /* search extension list for type */
167 const char *ext
= audio_formats
[i
].ext_list
;
171 if (strcasecmp(suffix
, ext
) == 0)
176 ext
+= strlen(ext
) + 1;
178 while (*ext
!= '\0');
184 /* Note, that this returns false for successful, true for error! */
185 bool mp3info(struct mp3entry
*entry
, const char *filename
)
190 fd
= open(filename
, O_RDONLY
);
194 result
= !get_mp3_metadata(fd
, entry
, filename
);
201 /* Get metadata for track - return false if parsing showed problems with the
202 * file that would prevent playback.
204 bool get_metadata(struct mp3entry
* id3
, int fd
, const char* trackname
)
206 #if CONFIG_CODEC == SWCODEC
210 /* Clear the mp3entry to avoid having bogus pointers appear */
211 memset(id3
, 0, sizeof(struct mp3entry
));
213 /* Take our best guess at the codec type based on file extension */
214 id3
->codectype
= probe_file_format(trackname
);
216 /* Load codec specific track tag information and confirm the codec type. */
217 switch (id3
->codectype
)
222 if (!get_mp3_metadata(fd
, id3
, trackname
))
229 #if CONFIG_CODEC == SWCODEC
231 if (!get_flac_metadata(fd
, id3
))
239 if (!get_asf_metadata(fd
, id3
))
246 if (!get_monkeys_metadata(fd
, id3
))
250 read_ape_tags(fd
, id3
);
254 if (!get_musepack_metadata(fd
, id3
))
256 read_ape_tags(fd
, id3
);
259 case AFMT_OGG_VORBIS
:
261 if (!get_ogg_metadata(fd
, id3
))/*detects and handles Ogg/Speex files*/
269 if (!get_wave_metadata(fd
, id3
))
277 if (!get_wavpack_metadata(fd
, id3
))
282 read_ape_tags(fd
, id3
); /* use any apetag info we find */
286 if (!get_a52_metadata(fd
, id3
))
295 if (!get_mp4_metadata(fd
, id3
))
303 if (!get_mod_metadata(fd
, id3
))
312 id3
->filesize
= filesize(fd
);
313 if (!skip_id3v2(fd
, id3
))
317 /* TODO: read the id3v2 header if it exists */
321 if (!get_sid_metadata(fd
, id3
))
328 if (!get_spc_metadata(fd
, id3
))
330 DEBUGF("get_spc_metadata error\n");
333 id3
->filesize
= filesize(fd
);
334 id3
->genre_string
= id3_get_num_genre(36);
338 if (!get_adx_metadata(fd
, id3
))
340 DEBUGF("get_adx_metadata error\n");
347 buf
= (unsigned char *)id3
->path
;
348 if ((lseek(fd
, 0, SEEK_SET
) < 0) || ((read(fd
, buf
, 8)) < 8))
350 DEBUGF("lseek or read failed\n");
354 id3
->filesize
= filesize(fd
);
355 if (memcmp(buf
,"NESM",4) && memcmp(buf
,"NSFE",4)) return false;
359 if (!get_aiff_metadata(fd
, id3
))
367 if (!get_asap_metadata(fd
, id3
))
369 DEBUGF("get_sap_metadata error\n");
372 id3
->filesize
= filesize(fd
);
373 id3
->genre_string
= id3_get_num_genre(36);
376 #endif /* CONFIG_CODEC == SWCODEC */
379 /* If we don't know how to read the metadata, assume we can't play
385 /* We have successfully read the metadata from the file */
388 if (cuesheet_is_enabled() && look_for_cuesheet_file(trackname
, NULL
))
390 id3
->cuesheet_type
= 1;
394 lseek(fd
, 0, SEEK_SET
);
395 strncpy(id3
->path
, trackname
, sizeof(id3
->path
));
401 #if CONFIG_CODEC == SWCODEC
402 void strip_tags(int handle_id
)
404 static const unsigned char tag
[] = "TAG";
405 static const unsigned char apetag
[] = "APETAGEX";
409 if (bufgettail(handle_id
, 128, &tail
) != 128)
412 if (memcmp(tail
, tag
, 3) == 0)
415 logf("Cutting off ID3v1 tag");
416 bufcuttail(handle_id
, 128);
419 /* Get a new tail, as the old one may have been cut */
420 if (bufgettail(handle_id
, 32, &tail
) != 32)
423 /* Check for APE tag (look for the APE tag footer) */
424 if (memcmp(tail
, apetag
, 8) != 0)
427 /* Read the version and length from the footer */
428 version
= get_long_le(&((unsigned char *)tail
)[8]);
429 len
= get_long_le(&((unsigned char *)tail
)[12]);
431 len
+= 32; /* APEv2 has a 32 byte header */
434 logf("Cutting off APE tag (%ldB)", len
);
435 bufcuttail(handle_id
, len
);
437 #endif /* CONFIG_CODEC == SWCODEC */
438 #endif /* ! __PCTOOL__ */
440 void adjust_mp3entry(struct mp3entry
*entry
, void *dest
, const void *orig
)
444 offset
= - ((size_t)orig
- (size_t)dest
);
446 offset
= (size_t)dest
- (size_t)orig
;
449 entry
->title
+= offset
;
451 entry
->artist
+= offset
;
453 entry
->album
+= offset
;
454 if (entry
->genre_string
&& !id3_is_genre_string(entry
->genre_string
))
455 /* Don't adjust that if it points to an entry of the "genres" array */
456 entry
->genre_string
+= offset
;
457 if (entry
->track_string
)
458 entry
->track_string
+= offset
;
459 if (entry
->disc_string
)
460 entry
->disc_string
+= offset
;
461 if (entry
->year_string
)
462 entry
->year_string
+= offset
;
464 entry
->composer
+= offset
;
466 entry
->comment
+= offset
;
467 if (entry
->albumartist
)
468 entry
->albumartist
+= offset
;
470 entry
->grouping
+= offset
;
471 #if CONFIG_CODEC == SWCODEC
472 if (entry
->track_gain_string
)
473 entry
->track_gain_string
+= offset
;
474 if (entry
->album_gain_string
)
475 entry
->album_gain_string
+= offset
;
477 if (entry
->mb_track_id
)
478 entry
->mb_track_id
+= offset
;
481 void copy_mp3entry(struct mp3entry
*dest
, const struct mp3entry
*orig
)
483 memcpy(dest
, orig
, sizeof(struct mp3entry
));
484 adjust_mp3entry(dest
, dest
, orig
);