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 ****************************************************************************/
24 #include "string-extra.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 */
46 AFMT_ENTRY("???", NULL
, NULL
, NULL
),
48 /* MPEG Audio layer 1 */
50 AFMT_ENTRY("MP1", "mpa", NULL
, "mp1\0" ),
51 /* MPEG Audio layer 2 */
53 AFMT_ENTRY("MP2", "mpa", NULL
, "mpa\0mp2\0" ),
54 /* MPEG Audio layer 3 */
56 AFMT_ENTRY("MP3", "mpa", "mp3_enc", "mp3\0" ),
58 #if CONFIG_CODEC == SWCODEC
59 /* Audio Interchange File Format */
61 AFMT_ENTRY("AIFF", "aiff", "aiff_enc", "aiff\0aif\0"),
62 /* Uncompressed PCM in a WAV file OR ATRAC3 stream in WAV file (.at3) */
64 AFMT_ENTRY("WAV", "wav", "wav_enc", "wav\0at3\0" ),
67 AFMT_ENTRY("Ogg", "vorbis", NULL
, "ogg\0oga\0" ),
70 AFMT_ENTRY("FLAC", "flac", NULL
, "flac\0" ),
73 AFMT_ENTRY("MPC", "mpc", NULL
, "mpc\0" ),
74 /* A/52 (aka AC3) audio */
76 AFMT_ENTRY("AC3", "a52", NULL
, "a52\0ac3\0" ),
79 AFMT_ENTRY("WV", "wavpack", "wavpack_enc", "wv\0" ),
80 /* Apple Lossless Audio Codec */
82 AFMT_ENTRY("ALAC", "alac", NULL
, "m4a\0m4b\0" ),
83 /* Advanced Audio Coding in M4A container */
85 AFMT_ENTRY("AAC", "aac", NULL
, "mp4\0" ),
88 AFMT_ENTRY("SHN", "shorten", NULL
, "shn\0" ),
91 AFMT_ENTRY("SID", "sid", NULL
, "sid\0" ),
94 AFMT_ENTRY("ADX", "adx", NULL
, "adx\0" ),
95 /* NESM (NES Sound Format) */
97 AFMT_ENTRY("NSF", "nsf", NULL
, "nsf\0nsfe\0" ),
98 /* Speex File Format */
100 AFMT_ENTRY("Speex","speex", NULL
, "spx\0" ),
101 /* SPC700 Save State */
103 AFMT_ENTRY("SPC", "spc", NULL
, "spc\0" ),
104 /* APE (Monkey's Audio) */
106 AFMT_ENTRY("APE", "ape", NULL
, "ape\0mac\0" ),
107 /* WMA (WMAV1/V2 in ASF) */
109 AFMT_ENTRY("WMA", "wma", NULL
, "wma\0wmv\0asf\0" ),
110 /* WMA Professional in ASF */
112 AFMT_ENTRY("WMAPro", "wmapro", NULL
, "wma\0wmv\0asf\0" ),
115 AFMT_ENTRY("MOD", "mod", NULL
, "mod\0" ),
118 AFMT_ENTRY("SAP", "asap", NULL
, "sap\0" ),
121 AFMT_ENTRY("Cook", "cook", NULL
, "rm\0ra\0rmvb\0" ),
124 AFMT_ENTRY("RAAC", "raac", NULL
, "rm\0ra\0rmvb\0" ),
127 AFMT_ENTRY("AC3", "a52_rm", NULL
, "rm\0ra\0rmvb\0" ),
128 /* ATRAC3 in RM/RA */
130 AFMT_ENTRY("ATRAC3","atrac3_rm", NULL
, "rm\0ra\0rmvb\0" ),
133 AFMT_ENTRY("CMC", "asap", NULL
, "cmc\0" ),
136 AFMT_ENTRY("CM3", "asap", NULL
, "cm3\0" ),
139 AFMT_ENTRY("CMR", "asap", NULL
, "cmr\0" ),
142 AFMT_ENTRY("CMS", "asap", NULL
, "cms\0" ),
145 AFMT_ENTRY("DMC", "asap", NULL
, "dmc\0" ),
148 AFMT_ENTRY("DLT", "asap", NULL
, "dlt\0" ),
151 AFMT_ENTRY("MPT", "asap", NULL
, "mpt\0" ),
154 AFMT_ENTRY("MPD", "asap", NULL
, "mpd\0" ),
157 AFMT_ENTRY("RMT", "asap", NULL
, "rmt\0" ),
160 AFMT_ENTRY("TMC", "asap", NULL
, "tmc\0" ),
163 AFMT_ENTRY("TM8", "asap", NULL
, "tm8\0" ),
166 AFMT_ENTRY("TM2", "asap", NULL
, "tm2\0" ),
167 /* Atrac3 in Sony OMA Container */
169 AFMT_ENTRY("ATRAC3", "atrac3_oma", NULL
, "oma\0aa3\0" ),
170 /* SMAF (Synthetic music Mobile Application Format) */
172 AFMT_ENTRY("SMAF", "smaf", NULL
, "mmf\0" ),
175 AFMT_ENTRY("AU", "au", NULL
, "au\0snd\0" ),
176 /* VOX (Dialogic telephony file formats) */
178 AFMT_ENTRY("VOX", "vox", NULL
, "vox\0" ),
181 AFMT_ENTRY("WAVE64", "wav64", NULL
, "w64\0" ),
184 AFMT_ENTRY("TTA", "tta", NULL
, "tta\0" ),
185 /* WMA Voice in ASF */
187 AFMT_ENTRY("WMAVoice", "wmavoice", NULL
, "wma\0wmv\0asf\0" ),
191 #if CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING)
192 /* get REC_FORMAT_* corresponding AFMT_* */
193 const int rec_format_afmt
[REC_NUM_FORMATS
] =
195 /* give AFMT_UNKNOWN by default */
196 [0 ... REC_NUM_FORMATS
-1] = AFMT_UNKNOWN
,
197 /* add new entries below this line */
198 [REC_FORMAT_AIFF
] = AFMT_AIFF
,
199 [REC_FORMAT_MPA_L3
] = AFMT_MPA_L3
,
200 [REC_FORMAT_WAVPACK
] = AFMT_WAVPACK
,
201 [REC_FORMAT_PCM_WAV
] = AFMT_PCM_WAV
,
204 /* get AFMT_* corresponding REC_FORMAT_* */
205 const int afmt_rec_format
[AFMT_NUM_CODECS
] =
207 /* give -1 by default */
208 [0 ... AFMT_NUM_CODECS
-1] = -1,
209 /* add new entries below this line */
210 [AFMT_AIFF
] = REC_FORMAT_AIFF
,
211 [AFMT_MPA_L3
] = REC_FORMAT_MPA_L3
,
212 [AFMT_WAVPACK
] = REC_FORMAT_WAVPACK
,
213 [AFMT_PCM_WAV
] = REC_FORMAT_PCM_WAV
,
215 #endif /* CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING) */
218 /* Simple file type probing by looking at the filename extension. */
219 unsigned int probe_file_format(const char *filename
)
224 suffix
= strrchr(filename
, '.');
234 for (i
= 1; i
< AFMT_NUM_CODECS
; i
++)
236 /* search extension list for type */
237 const char *ext
= audio_formats
[i
].ext_list
;
241 if (strcasecmp(suffix
, ext
) == 0)
246 ext
+= strlen(ext
) + 1;
248 while (*ext
!= '\0');
254 /* Note, that this returns false for successful, true for error! */
255 bool mp3info(struct mp3entry
*entry
, const char *filename
)
260 fd
= open(filename
, O_RDONLY
);
264 result
= !get_mp3_metadata(fd
, entry
, filename
);
271 /* Get metadata for track - return false if parsing showed problems with the
272 * file that would prevent playback.
274 bool get_metadata(struct mp3entry
* id3
, int fd
, const char* trackname
)
276 /* Clear the mp3entry to avoid having bogus pointers appear */
277 memset(id3
, 0, sizeof(struct mp3entry
));
279 /* Take our best guess at the codec type based on file extension */
280 id3
->codectype
= probe_file_format(trackname
);
282 /* Load codec specific track tag information and confirm the codec type. */
283 switch (id3
->codectype
)
288 if (!get_mp3_metadata(fd
, id3
, trackname
))
295 #if CONFIG_CODEC == SWCODEC
297 if (!get_flac_metadata(fd
, id3
))
305 if (!get_asf_metadata(fd
, id3
))
312 if (!get_monkeys_metadata(fd
, id3
))
316 read_ape_tags(fd
, id3
);
320 if (!get_musepack_metadata(fd
, id3
))
322 read_ape_tags(fd
, id3
);
325 case AFMT_OGG_VORBIS
:
327 if (!get_ogg_metadata(fd
, id3
))/*detects and handles Ogg/Speex files*/
335 if (!get_wave_metadata(fd
, id3
))
343 if (!get_wavpack_metadata(fd
, id3
))
348 read_ape_tags(fd
, id3
); /* use any apetag info we find */
352 if (!get_a52_metadata(fd
, id3
))
361 if (!get_mp4_metadata(fd
, id3
))
369 if (!get_mod_metadata(fd
, id3
))
378 id3
->filesize
= filesize(fd
);
379 if (!skip_id3v2(fd
, id3
))
383 /* TODO: read the id3v2 header if it exists */
387 if (!get_sid_metadata(fd
, id3
))
394 if (!get_spc_metadata(fd
, id3
))
396 DEBUGF("get_spc_metadata error\n");
399 id3
->filesize
= filesize(fd
);
400 id3
->genre_string
= id3_get_num_genre(36);
404 if (!get_adx_metadata(fd
, id3
))
406 DEBUGF("get_adx_metadata error\n");
413 if (!get_nsf_metadata(fd
, id3
))
415 DEBUGF("get_nsf_metadata error\n");
421 if (!get_aiff_metadata(fd
, id3
))
429 if (!get_asap_metadata(fd
, id3
))
431 DEBUGF("get_sap_metadata error\n");
434 id3
->filesize
= filesize(fd
);
435 id3
->genre_string
= id3_get_num_genre(36);
451 id3
->frequency
= 44100;
453 id3
->filesize
= filesize(fd
);
454 id3
->genre_string
= id3_get_num_genre(36);
457 if (!get_rm_metadata(fd
, id3
))
459 DEBUGF("get_rm_metadata error\n");
463 case AFMT_OMA_ATRAC3
:
464 if (!get_oma_metadata(fd
, id3
))
466 DEBUGF("get_oma_metadata error\n");
472 if (!get_smaf_metadata(fd
, id3
))
474 DEBUGF("get_smaf_metadata error\n");
480 if (!get_au_metadata(fd
, id3
))
482 DEBUGF("get_au_metadata error\n");
488 if (!get_vox_metadata(fd
, id3
))
490 DEBUGF("get_vox_metadata error\n");
496 if (!get_wave64_metadata(fd
, id3
))
498 DEBUGF("get_wave64_metadata error\n");
504 if (!get_tta_metadata(fd
, id3
))
506 DEBUGF("get_tta_metadata error\n");
511 #endif /* CONFIG_CODEC == SWCODEC */
514 /* If we don't know how to read the metadata, assume we can't play
520 /* We have successfully read the metadata from the file */
522 lseek(fd
, 0, SEEK_SET
);
523 strlcpy(id3
->path
, trackname
, sizeof(id3
->path
));
529 #if CONFIG_CODEC == SWCODEC
530 void strip_tags(int handle_id
)
532 static const unsigned char tag
[] = "TAG";
533 static const unsigned char apetag
[] = "APETAGEX";
537 if (bufgettail(handle_id
, 128, &tail
) != 128)
540 if (memcmp(tail
, tag
, 3) == 0)
543 logf("Cutting off ID3v1 tag");
544 bufcuttail(handle_id
, 128);
547 /* Get a new tail, as the old one may have been cut */
548 if (bufgettail(handle_id
, 32, &tail
) != 32)
551 /* Check for APE tag (look for the APE tag footer) */
552 if (memcmp(tail
, apetag
, 8) != 0)
555 /* Read the version and length from the footer */
556 version
= get_long_le(&((unsigned char *)tail
)[8]);
557 len
= get_long_le(&((unsigned char *)tail
)[12]);
559 len
+= 32; /* APEv2 has a 32 byte header */
562 logf("Cutting off APE tag (%ldB)", len
);
563 bufcuttail(handle_id
, len
);
565 #endif /* CONFIG_CODEC == SWCODEC */
566 #endif /* ! __PCTOOL__ */
568 void adjust_mp3entry(struct mp3entry
*entry
, void *dest
, const void *orig
)
572 offset
= - ((size_t)orig
- (size_t)dest
);
574 offset
= (size_t)dest
- (size_t)orig
;
577 entry
->title
+= offset
;
579 entry
->artist
+= offset
;
581 entry
->album
+= offset
;
582 if (entry
->genre_string
> (char*)orig
583 && entry
->genre_string
< (char*)orig
+ sizeof(struct mp3entry
))
584 /* Don't adjust that if it points to an entry of the "genres" array */
585 entry
->genre_string
+= offset
;
586 if (entry
->track_string
)
587 entry
->track_string
+= offset
;
588 if (entry
->disc_string
)
589 entry
->disc_string
+= offset
;
590 if (entry
->year_string
)
591 entry
->year_string
+= offset
;
593 entry
->composer
+= offset
;
595 entry
->comment
+= offset
;
596 if (entry
->albumartist
)
597 entry
->albumartist
+= offset
;
599 entry
->grouping
+= offset
;
600 #if CONFIG_CODEC == SWCODEC
601 if (entry
->track_gain_string
)
602 entry
->track_gain_string
+= offset
;
603 if (entry
->album_gain_string
)
604 entry
->album_gain_string
+= offset
;
606 if (entry
->mb_track_id
)
607 entry
->mb_track_id
+= offset
;
610 void copy_mp3entry(struct mp3entry
*dest
, const struct mp3entry
*orig
)
612 memcpy(dest
, orig
, sizeof(struct mp3entry
));
613 adjust_mp3entry(dest
, dest
, orig
);