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"
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 static bool get_shn_metadata(int fd
, struct mp3entry
*id3
)
43 /* TODO: read the id3v2 header if it exists */
45 id3
->filesize
= filesize(fd
);
46 return skip_id3v2(fd
, id3
);
49 static bool get_other_asap_metadata(int fd
, struct mp3entry
*id3
)
52 id3
->frequency
= 44100;
54 id3
->filesize
= filesize(fd
);
55 id3
->genre_string
= id3_get_num_genre(36);
58 #endif /* CONFIG_CODEC == SWCODEC */
59 bool write_metadata_log
= false;
61 const struct afmt_entry audio_formats
[AFMT_NUM_CODECS
] =
63 /* Unknown file format */
65 AFMT_ENTRY("???", NULL
, NULL
, NULL
, NULL
),
67 /* MPEG Audio layer 1 */
69 AFMT_ENTRY("MP1", "mpa", NULL
, get_mp3_metadata
, "mp1\0"),
70 /* MPEG Audio layer 2 */
72 AFMT_ENTRY("MP2", "mpa", NULL
, get_mp3_metadata
, "mpa\0mp2\0"),
73 /* MPEG Audio layer 3 */
75 AFMT_ENTRY("MP3", "mpa", "mp3_enc", get_mp3_metadata
, "mp3\0"),
77 #if CONFIG_CODEC == SWCODEC
78 /* Audio Interchange File Format */
80 AFMT_ENTRY("AIFF", "aiff", "aiff_enc", get_aiff_metadata
, "aiff\0aif\0"),
81 /* Uncompressed PCM in a WAV file OR ATRAC3 stream in WAV file (.at3) */
83 AFMT_ENTRY("WAV", "wav", "wav_enc", get_wave_metadata
, "wav\0at3\0"),
86 AFMT_ENTRY("Ogg", "vorbis", NULL
, get_ogg_metadata
, "ogg\0oga\0"),
89 AFMT_ENTRY("FLAC", "flac", NULL
, get_flac_metadata
, "flac\0"),
92 AFMT_ENTRY("MPCv7", "mpc", NULL
, get_musepack_metadata
,"mpc\0"),
93 /* A/52 (aka AC3) audio */
95 AFMT_ENTRY("AC3", "a52", NULL
, get_a52_metadata
, "a52\0ac3\0"),
98 AFMT_ENTRY("WV","wavpack","wavpack_enc",get_wavpack_metadata
,"wv\0"),
99 /* Apple Lossless Audio Codec */
101 AFMT_ENTRY("ALAC", "alac", NULL
, get_mp4_metadata
, "m4a\0m4b\0"),
102 /* Advanced Audio Coding in M4A container */
104 AFMT_ENTRY("AAC", "aac", NULL
, get_mp4_metadata
, "mp4\0"),
107 AFMT_ENTRY("SHN","shorten", NULL
, get_shn_metadata
, "shn\0"),
108 /* SID File Format */
110 AFMT_ENTRY("SID", "sid", NULL
, get_sid_metadata
, "sid\0"),
111 /* ADX File Format */
113 AFMT_ENTRY("ADX", "adx", NULL
, get_adx_metadata
, "adx\0"),
114 /* NESM (NES Sound Format) */
116 AFMT_ENTRY("NSF", "nsf", NULL
, get_nsf_metadata
, "nsf\0nsfe\0"),
117 /* Speex File Format */
119 AFMT_ENTRY("Speex", "speex",NULL
, get_ogg_metadata
, "spx\0"),
120 /* SPC700 Save State */
122 AFMT_ENTRY("SPC", "spc", NULL
, get_spc_metadata
, "spc\0"),
123 /* APE (Monkey's Audio) */
125 AFMT_ENTRY("APE", "ape", NULL
, get_monkeys_metadata
,"ape\0mac\0"),
126 /* WMA (WMAV1/V2 in ASF) */
128 AFMT_ENTRY("WMA", "wma", NULL
, get_asf_metadata
,"wma\0wmv\0asf\0"),
129 /* WMA Professional in ASF */
131 AFMT_ENTRY("WMAPro","wmapro",NULL
, NULL
, "wma\0wmv\0asf\0"),
134 AFMT_ENTRY("MOD", "mod", NULL
, get_mod_metadata
, "mod\0"),
137 AFMT_ENTRY("SAP", "asap", NULL
, get_asap_metadata
, "sap\0"),
140 AFMT_ENTRY("Cook", "cook", NULL
, get_rm_metadata
,"rm\0ra\0rmvb\0"),
143 AFMT_ENTRY("RAAC", "raac", NULL
, NULL
, "rm\0ra\0rmvb\0"),
146 AFMT_ENTRY("AC3", "a52_rm", NULL
, NULL
, "rm\0ra\0rmvb\0"),
147 /* ATRAC3 in RM/RA */
149 AFMT_ENTRY("ATRAC3","atrac3_rm",NULL
, NULL
, "rm\0ra\0rmvb\0"),
152 AFMT_ENTRY("CMC", "asap", NULL
, get_other_asap_metadata
,"cmc\0"),
155 AFMT_ENTRY("CM3", "asap", NULL
, get_other_asap_metadata
,"cm3\0"),
158 AFMT_ENTRY("CMR", "asap", NULL
, get_other_asap_metadata
,"cmr\0"),
161 AFMT_ENTRY("CMS", "asap", NULL
, get_other_asap_metadata
,"cms\0"),
164 AFMT_ENTRY("DMC", "asap", NULL
, get_other_asap_metadata
,"dmc\0"),
167 AFMT_ENTRY("DLT", "asap", NULL
, get_other_asap_metadata
,"dlt\0"),
170 AFMT_ENTRY("MPT", "asap", NULL
, get_other_asap_metadata
,"mpt\0"),
173 AFMT_ENTRY("MPD", "asap", NULL
, get_other_asap_metadata
,"mpd\0"),
176 AFMT_ENTRY("RMT", "asap", NULL
, get_other_asap_metadata
,"rmt\0"),
179 AFMT_ENTRY("TMC", "asap", NULL
, get_other_asap_metadata
,"tmc\0"),
182 AFMT_ENTRY("TM8", "asap", NULL
, get_other_asap_metadata
,"tm8\0"),
185 AFMT_ENTRY("TM2", "asap", NULL
, get_other_asap_metadata
,"tm2\0"),
186 /* Atrac3 in Sony OMA Container */
188 AFMT_ENTRY("ATRAC3","atrac3_oma",NULL
, get_oma_metadata
, "oma\0aa3\0"),
189 /* SMAF (Synthetic music Mobile Application Format) */
191 AFMT_ENTRY("SMAF", "smaf", NULL
, get_smaf_metadata
, "mmf\0"),
194 AFMT_ENTRY("AU", "au", NULL
, get_au_metadata
, "au\0snd\0"),
195 /* VOX (Dialogic telephony file formats) */
197 AFMT_ENTRY("VOX", "vox", NULL
, get_vox_metadata
, "vox\0"),
200 AFMT_ENTRY("WAVE64","wav64",NULL
, get_wave64_metadata
,"w64\0"),
203 AFMT_ENTRY("TTA", "tta", NULL
, get_tta_metadata
, "tta\0"),
204 /* WMA Voice in ASF */
206 AFMT_ENTRY("WMAVoice","wmavoice",NULL
, NULL
, "wma\0wmv\0"),
209 AFMT_ENTRY("MPCv8", "mpc", NULL
, get_musepack_metadata
,"mpc\0"),
210 /* Advanced Audio Coding High Efficiency in M4A container */
212 AFMT_ENTRY("AAC-HE","aac", NULL
, get_mp4_metadata
, "mp4\0"),
216 #if CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING)
217 /* get REC_FORMAT_* corresponding AFMT_* */
218 const int rec_format_afmt
[REC_NUM_FORMATS
] =
220 /* give AFMT_UNKNOWN by default */
221 [0 ... REC_NUM_FORMATS
-1] = AFMT_UNKNOWN
,
222 /* add new entries below this line */
223 [REC_FORMAT_AIFF
] = AFMT_AIFF
,
224 [REC_FORMAT_MPA_L3
] = AFMT_MPA_L3
,
225 [REC_FORMAT_WAVPACK
] = AFMT_WAVPACK
,
226 [REC_FORMAT_PCM_WAV
] = AFMT_PCM_WAV
,
229 /* get AFMT_* corresponding REC_FORMAT_* */
230 const int afmt_rec_format
[AFMT_NUM_CODECS
] =
232 /* give -1 by default */
233 [0 ... AFMT_NUM_CODECS
-1] = -1,
234 /* add new entries below this line */
235 [AFMT_AIFF
] = REC_FORMAT_AIFF
,
236 [AFMT_MPA_L3
] = REC_FORMAT_MPA_L3
,
237 [AFMT_WAVPACK
] = REC_FORMAT_WAVPACK
,
238 [AFMT_PCM_WAV
] = REC_FORMAT_PCM_WAV
,
240 #endif /* CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING) */
243 /* Simple file type probing by looking at the filename extension. */
244 unsigned int probe_file_format(const char *filename
)
249 suffix
= strrchr(filename
, '.');
259 for (i
= 1; i
< AFMT_NUM_CODECS
; i
++)
261 /* search extension list for type */
262 const char *ext
= audio_formats
[i
].ext_list
;
266 if (strcasecmp(suffix
, ext
) == 0)
271 ext
+= strlen(ext
) + 1;
273 while (*ext
!= '\0');
279 /* Note, that this returns false for successful, true for error! */
280 bool mp3info(struct mp3entry
*entry
, const char *filename
)
285 fd
= open(filename
, O_RDONLY
);
289 result
= !get_metadata(entry
, fd
, filename
);
296 /* Get metadata for track - return false if parsing showed problems with the
297 * file that would prevent playback.
299 bool get_metadata(struct mp3entry
* id3
, int fd
, const char* trackname
)
301 const struct afmt_entry
*entry
;
303 DEBUGF("Read metadata for %s\n", trackname
);
304 if (write_metadata_log
)
306 logfd
= open("/metadata.log", O_WRONLY
| O_APPEND
| O_CREAT
, 0666);
309 write(logfd
, trackname
, strlen(trackname
));
310 write(logfd
, "\n", 1);
315 /* Clear the mp3entry to avoid having bogus pointers appear */
316 memset(id3
, 0, sizeof(struct mp3entry
));
318 /* Take our best guess at the codec type based on file extension */
319 id3
->codectype
= probe_file_format(trackname
);
321 entry
= &audio_formats
[id3
->codectype
];
323 /* Load codec specific track tag information and confirm the codec type. */
324 if (!entry
->parse_func
)
326 DEBUGF("nothing to parse for %s (format %s)", trackname
, entry
->label
);
330 if (!entry
->parse_func(fd
, id3
))
332 DEBUGF("parsing %s failed (format: %s)", trackname
, entry
->label
);
336 lseek(fd
, 0, SEEK_SET
);
337 strlcpy(id3
->path
, trackname
, sizeof(id3
->path
));
338 /* We have successfully read the metadata from the file */
343 #if CONFIG_CODEC == SWCODEC
344 void strip_tags(int handle_id
)
346 static const unsigned char tag
[] = "TAG";
347 static const unsigned char apetag
[] = "APETAGEX";
351 if (bufgettail(handle_id
, 128, &tail
) != 128)
354 if (memcmp(tail
, tag
, 3) == 0)
357 logf("Cutting off ID3v1 tag");
358 bufcuttail(handle_id
, 128);
361 /* Get a new tail, as the old one may have been cut */
362 if (bufgettail(handle_id
, 32, &tail
) != 32)
365 /* Check for APE tag (look for the APE tag footer) */
366 if (memcmp(tail
, apetag
, 8) != 0)
369 /* Read the version and length from the footer */
370 version
= get_long_le(&((unsigned char *)tail
)[8]);
371 len
= get_long_le(&((unsigned char *)tail
)[12]);
373 len
+= 32; /* APEv2 has a 32 byte header */
376 logf("Cutting off APE tag (%ldB)", len
);
377 bufcuttail(handle_id
, len
);
379 #endif /* CONFIG_CODEC == SWCODEC */
380 #endif /* ! __PCTOOL__ */
382 #define MOVE_ENTRY(x) if (x) x += offset;
384 void adjust_mp3entry(struct mp3entry
*entry
, void *dest
, const void *orig
)
388 offset
= -((size_t)orig
- (size_t)dest
);
390 offset
= ((size_t)dest
- (size_t)orig
);
392 MOVE_ENTRY(entry
->title
)
393 MOVE_ENTRY(entry
->artist
)
394 MOVE_ENTRY(entry
->album
)
396 if (entry
->genre_string
> (char*)orig
&&
397 entry
->genre_string
< (char*)orig
+ sizeof(struct mp3entry
))
398 /* Don't adjust that if it points to an entry of the "genres" array */
399 entry
->genre_string
+= offset
;
401 MOVE_ENTRY(entry
->track_string
)
402 MOVE_ENTRY(entry
->disc_string
)
403 MOVE_ENTRY(entry
->year_string
)
404 MOVE_ENTRY(entry
->composer
)
405 MOVE_ENTRY(entry
->comment
)
406 MOVE_ENTRY(entry
->albumartist
)
407 MOVE_ENTRY(entry
->grouping
)
408 MOVE_ENTRY(entry
->mb_track_id
)
411 void copy_mp3entry(struct mp3entry
*dest
, const struct mp3entry
*orig
)
413 memcpy(dest
, orig
, sizeof(struct mp3entry
));
414 adjust_mp3entry(dest
, dest
, orig
);
419 #if CONFIG_CODEC == SWCODEC
421 enum { AUTORESUMABLE_UNKNOWN
= 0, AUTORESUMABLE_TRUE
, AUTORESUMABLE_FALSE
};
423 bool autoresumable(struct mp3entry
*id3
)
429 if (id3
->autoresumable
) /* result cached? */
430 return id3
->autoresumable
== AUTORESUMABLE_TRUE
;
432 is_resumable
= false;
436 for (path
= global_settings
.autoresume_paths
;
437 *path
; /* search terms left? */
440 if (*path
== ':') /* Skip empty search patterns */
443 /* FIXME: As soon as strcspn or strchrnul are made available in
444 the core, the following can be made more efficient. */
445 endp
= strchr(path
, ':');
451 /* Note: At this point, len is always > 0 */
453 if (strncasecmp(id3
->path
, path
, len
) == 0)
455 /* Full directory-name matches only. Trailing '/' in
457 if (id3
->path
[len
] == '/' || id3
->path
[len
- 1] == '/')
469 is_resumable
? AUTORESUMABLE_TRUE
: AUTORESUMABLE_FALSE
;
471 logf("autoresumable: %s is%s resumable",
472 id3
->path
, is_resumable
? "" : " not");
478 #endif /* HAVE_TAGCACHE */
479 #endif /* __PCTOOL__ */