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 and base audio data types */
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 2 */
69 AFMT_ENTRY("MP2", "mpa", NULL
, get_mp3_metadata
, "mpa\0mp2\0"),
71 #if CONFIG_CODEC != SWCODEC
72 /* MPEG Audio layer 3 on HWCODEC: .talk clips, no encoder */
74 AFMT_ENTRY("MP3", "mpa", NULL
, get_mp3_metadata
, "mp3\0talk\0"),
76 #else /* CONFIG_CODEC == SWCODEC */
77 /* MPEG Audio layer 3 on SWCODEC */
79 AFMT_ENTRY("MP3", "mpa", "mp3_enc", get_mp3_metadata
, "mp3\0"),
81 /* MPEG Audio layer 1 */
83 AFMT_ENTRY("MP1", "mpa", NULL
, get_mp3_metadata
, "mp1\0"),
84 /* Audio Interchange File Format */
86 AFMT_ENTRY("AIFF", "aiff", "aiff_enc", get_aiff_metadata
, "aiff\0aif\0"),
87 /* Uncompressed PCM in a WAV file OR ATRAC3 stream in WAV file (.at3) */
89 AFMT_ENTRY("WAV", "wav", "wav_enc", get_wave_metadata
, "wav\0at3\0"),
92 AFMT_ENTRY("Ogg", "vorbis", NULL
, get_ogg_metadata
, "ogg\0oga\0"),
95 AFMT_ENTRY("FLAC", "flac", NULL
, get_flac_metadata
, "flac\0"),
98 AFMT_ENTRY("MPCv7", "mpc", NULL
, get_musepack_metadata
,"mpc\0"),
99 /* A/52 (aka AC3) audio */
101 AFMT_ENTRY("AC3", "a52", NULL
, get_a52_metadata
, "a52\0ac3\0"),
104 AFMT_ENTRY("WV","wavpack","wavpack_enc",get_wavpack_metadata
,"wv\0"),
105 /* Apple Lossless Audio Codec */
107 AFMT_ENTRY("ALAC", "alac", NULL
, get_mp4_metadata
, "m4a\0m4b\0"),
108 /* Advanced Audio Coding in M4A container */
110 AFMT_ENTRY("AAC", "aac", NULL
, get_mp4_metadata
, "mp4\0"),
113 AFMT_ENTRY("SHN","shorten", NULL
, get_shn_metadata
, "shn\0"),
114 /* SID File Format */
116 AFMT_ENTRY("SID", "sid", NULL
, get_sid_metadata
, "sid\0"),
117 /* ADX File Format */
119 AFMT_ENTRY("ADX", "adx", NULL
, get_adx_metadata
, "adx\0"),
120 /* NESM (NES Sound Format) */
122 AFMT_ENTRY("NSF", "nsf", NULL
, get_nsf_metadata
, "nsf\0nsfe\0"),
123 /* Speex File Format */
125 AFMT_ENTRY("Speex", "speex",NULL
, get_ogg_metadata
, "spx\0"),
126 /* SPC700 Save State */
128 AFMT_ENTRY("SPC", "spc", NULL
, get_spc_metadata
, "spc\0"),
129 /* APE (Monkey's Audio) */
131 AFMT_ENTRY("APE", "ape", NULL
, get_monkeys_metadata
,"ape\0mac\0"),
132 /* WMA (WMAV1/V2 in ASF) */
134 AFMT_ENTRY("WMA", "wma", NULL
, get_asf_metadata
,"wma\0wmv\0asf\0"),
135 /* WMA Professional in ASF */
137 AFMT_ENTRY("WMAPro","wmapro",NULL
, NULL
, "wma\0wmv\0asf\0"),
140 AFMT_ENTRY("MOD", "mod", NULL
, get_mod_metadata
, "mod\0"),
143 AFMT_ENTRY("SAP", "asap", NULL
, get_asap_metadata
, "sap\0"),
146 AFMT_ENTRY("Cook", "cook", NULL
, get_rm_metadata
,"rm\0ra\0rmvb\0"),
149 AFMT_ENTRY("RAAC", "raac", NULL
, NULL
, "rm\0ra\0rmvb\0"),
152 AFMT_ENTRY("AC3", "a52_rm", NULL
, NULL
, "rm\0ra\0rmvb\0"),
153 /* ATRAC3 in RM/RA */
155 AFMT_ENTRY("ATRAC3","atrac3_rm",NULL
, NULL
, "rm\0ra\0rmvb\0"),
158 AFMT_ENTRY("CMC", "asap", NULL
, get_other_asap_metadata
,"cmc\0"),
161 AFMT_ENTRY("CM3", "asap", NULL
, get_other_asap_metadata
,"cm3\0"),
164 AFMT_ENTRY("CMR", "asap", NULL
, get_other_asap_metadata
,"cmr\0"),
167 AFMT_ENTRY("CMS", "asap", NULL
, get_other_asap_metadata
,"cms\0"),
170 AFMT_ENTRY("DMC", "asap", NULL
, get_other_asap_metadata
,"dmc\0"),
173 AFMT_ENTRY("DLT", "asap", NULL
, get_other_asap_metadata
,"dlt\0"),
176 AFMT_ENTRY("MPT", "asap", NULL
, get_other_asap_metadata
,"mpt\0"),
179 AFMT_ENTRY("MPD", "asap", NULL
, get_other_asap_metadata
,"mpd\0"),
182 AFMT_ENTRY("RMT", "asap", NULL
, get_other_asap_metadata
,"rmt\0"),
185 AFMT_ENTRY("TMC", "asap", NULL
, get_other_asap_metadata
,"tmc\0"),
188 AFMT_ENTRY("TM8", "asap", NULL
, get_other_asap_metadata
,"tm8\0"),
191 AFMT_ENTRY("TM2", "asap", NULL
, get_other_asap_metadata
,"tm2\0"),
192 /* Atrac3 in Sony OMA Container */
194 AFMT_ENTRY("ATRAC3","atrac3_oma",NULL
, get_oma_metadata
, "oma\0aa3\0"),
195 /* SMAF (Synthetic music Mobile Application Format) */
197 AFMT_ENTRY("SMAF", "smaf", NULL
, get_smaf_metadata
, "mmf\0"),
200 AFMT_ENTRY("AU", "au", NULL
, get_au_metadata
, "au\0snd\0"),
201 /* VOX (Dialogic telephony file formats) */
203 AFMT_ENTRY("VOX", "vox", NULL
, get_vox_metadata
, "vox\0"),
206 AFMT_ENTRY("WAVE64","wav64",NULL
, get_wave64_metadata
,"w64\0"),
209 AFMT_ENTRY("TTA", "tta", NULL
, get_tta_metadata
, "tta\0"),
210 /* WMA Voice in ASF */
212 AFMT_ENTRY("WMAVoice","wmavoice",NULL
, NULL
, "wma\0wmv\0"),
215 AFMT_ENTRY("MPCv8", "mpc", NULL
, get_musepack_metadata
,"mpc\0"),
216 /* Advanced Audio Coding High Efficiency in M4A container */
218 AFMT_ENTRY("AAC-HE","aac", NULL
, get_mp4_metadata
, "mp4\0"),
219 /* AY (ZX Spectrum, Amstrad CPC Sound Format) */
221 AFMT_ENTRY("AY", "ay", NULL
, get_ay_metadata
, "ay\0"),
222 /* GBS (Game Boy Sound Format) */
224 AFMT_ENTRY("GBS", "gbs", NULL
, get_gbs_metadata
, "gbs\0"),
225 /* HES (Hudson Entertainment System Sound Format) */
227 AFMT_ENTRY("HES", "hes", NULL
, get_hes_metadata
, "hes\0"),
228 /* SGC (Sega Master System, Game Gear, Coleco Vision Sound Format) */
230 AFMT_ENTRY("SGC", "sgc", NULL
, get_sgc_metadata
, "sgc\0"),
231 /* VGM (Video Game Music Format) */
233 AFMT_ENTRY("VGM", "vgm", NULL
, get_vgm_metadata
, "vgm\0vgz\0"),
234 /* KSS (MSX computer KSS Music File) */
236 AFMT_ENTRY("KSS", "kss", NULL
, get_kss_metadata
, "kss\0"),
240 #if CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING)
241 /* get REC_FORMAT_* corresponding AFMT_* */
242 const int rec_format_afmt
[REC_NUM_FORMATS
] =
244 /* give AFMT_UNKNOWN by default */
245 [0 ... REC_NUM_FORMATS
-1] = AFMT_UNKNOWN
,
246 /* add new entries below this line */
247 [REC_FORMAT_AIFF
] = AFMT_AIFF
,
248 [REC_FORMAT_MPA_L3
] = AFMT_MPA_L3
,
249 [REC_FORMAT_WAVPACK
] = AFMT_WAVPACK
,
250 [REC_FORMAT_PCM_WAV
] = AFMT_PCM_WAV
,
253 /* get AFMT_* corresponding REC_FORMAT_* */
254 const int afmt_rec_format
[AFMT_NUM_CODECS
] =
256 /* give -1 by default */
257 [0 ... AFMT_NUM_CODECS
-1] = -1,
258 /* add new entries below this line */
259 [AFMT_AIFF
] = REC_FORMAT_AIFF
,
260 [AFMT_MPA_L3
] = REC_FORMAT_MPA_L3
,
261 [AFMT_WAVPACK
] = REC_FORMAT_WAVPACK
,
262 [AFMT_PCM_WAV
] = REC_FORMAT_PCM_WAV
,
264 #endif /* CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING) */
266 #if CONFIG_CODEC == SWCODEC
267 /* Get the canonical AFMT type */
268 int get_audio_base_codec_type(int type
)
270 int base_type
= type
;
275 base_type
= AFMT_MPA_L3
;
279 base_type
= AFMT_MPC_SV7
;
282 case AFMT_MP4_AAC_HE
:
283 base_type
= AFMT_MP4_AAC
;
298 base_type
= AFMT_SAP
;
307 /* Get the basic audio type */
308 enum data_type
get_audio_base_data_type(int afmt
)
310 if ((unsigned)afmt
>= AFMT_NUM_CODECS
)
313 switch (get_audio_base_codec_type(afmt
))
326 /* Type must be allocated and loaded in its entirety onto
328 return TYPE_ATOMIC_AUDIO
;
331 /* Assume type may be loaded and discarded incrementally */
332 return TYPE_PACKET_AUDIO
;
335 /* Have no idea at all */
340 /* Is the format allowed to buffer starting at some offset other than 0
341 or first frame only for resume purposes? */
342 bool format_buffers_with_offset(int afmt
)
350 /* Format may be loaded at the first needed frame */
353 /* Format must be loaded from the beginning of the file
354 (does not imply 'atomic', while 'atomic' implies 'no offset') */
358 #endif /* CONFIG_CODEC == SWCODEC */
361 /* Simple file type probing by looking at the filename extension. */
362 unsigned int probe_file_format(const char *filename
)
367 suffix
= strrchr(filename
, '.');
377 for (i
= 1; i
< AFMT_NUM_CODECS
; i
++)
379 /* search extension list for type */
380 const char *ext
= audio_formats
[i
].ext_list
;
384 if (strcasecmp(suffix
, ext
) == 0)
389 ext
+= strlen(ext
) + 1;
391 while (*ext
!= '\0');
397 /* Note, that this returns false for successful, true for error! */
398 bool mp3info(struct mp3entry
*entry
, const char *filename
)
403 fd
= open(filename
, O_RDONLY
);
407 result
= !get_metadata(entry
, fd
, filename
);
414 /* Get metadata for track - return false if parsing showed problems with the
415 * file that would prevent playback.
417 bool get_metadata(struct mp3entry
* id3
, int fd
, const char* trackname
)
419 const struct afmt_entry
*entry
;
421 DEBUGF("Read metadata for %s\n", trackname
);
422 if (write_metadata_log
)
424 logfd
= open("/metadata.log", O_WRONLY
| O_APPEND
| O_CREAT
, 0666);
427 write(logfd
, trackname
, strlen(trackname
));
428 write(logfd
, "\n", 1);
433 /* Clear the mp3entry to avoid having bogus pointers appear */
436 /* Take our best guess at the codec type based on file extension */
437 id3
->codectype
= probe_file_format(trackname
);
439 entry
= &audio_formats
[id3
->codectype
];
441 /* Load codec specific track tag information and confirm the codec type. */
442 if (!entry
->parse_func
)
444 DEBUGF("nothing to parse for %s (format %s)", trackname
, entry
->label
);
448 if (!entry
->parse_func(fd
, id3
))
450 DEBUGF("parsing %s failed (format: %s)", trackname
, entry
->label
);
454 lseek(fd
, 0, SEEK_SET
);
455 strlcpy(id3
->path
, trackname
, sizeof(id3
->path
));
456 /* We have successfully read the metadata from the file */
461 #if CONFIG_CODEC == SWCODEC
462 void strip_tags(int handle_id
)
464 static const unsigned char tag
[] = "TAG";
465 static const unsigned char apetag
[] = "APETAGEX";
469 if (bufgettail(handle_id
, 128, &tail
) != 128)
472 if (memcmp(tail
, tag
, 3) == 0)
475 logf("Cutting off ID3v1 tag");
476 bufcuttail(handle_id
, 128);
479 /* Get a new tail, as the old one may have been cut */
480 if (bufgettail(handle_id
, 32, &tail
) != 32)
483 /* Check for APE tag (look for the APE tag footer) */
484 if (memcmp(tail
, apetag
, 8) != 0)
487 /* Read the version and length from the footer */
488 version
= get_long_le(&((unsigned char *)tail
)[8]);
489 len
= get_long_le(&((unsigned char *)tail
)[12]);
491 len
+= 32; /* APEv2 has a 32 byte header */
494 logf("Cutting off APE tag (%ldB)", len
);
495 bufcuttail(handle_id
, len
);
497 #endif /* CONFIG_CODEC == SWCODEC */
498 #endif /* ! __PCTOOL__ */
500 #define MOVE_ENTRY(x) if (x) x += offset;
502 void adjust_mp3entry(struct mp3entry
*entry
, void *dest
, const void *orig
)
506 offset
= -((size_t)orig
- (size_t)dest
);
508 offset
= ((size_t)dest
- (size_t)orig
);
510 MOVE_ENTRY(entry
->title
)
511 MOVE_ENTRY(entry
->artist
)
512 MOVE_ENTRY(entry
->album
)
514 if (entry
->genre_string
> (char*)orig
&&
515 entry
->genre_string
< (char*)orig
+ sizeof(struct mp3entry
))
516 /* Don't adjust that if it points to an entry of the "genres" array */
517 entry
->genre_string
+= offset
;
519 MOVE_ENTRY(entry
->track_string
)
520 MOVE_ENTRY(entry
->disc_string
)
521 MOVE_ENTRY(entry
->year_string
)
522 MOVE_ENTRY(entry
->composer
)
523 MOVE_ENTRY(entry
->comment
)
524 MOVE_ENTRY(entry
->albumartist
)
525 MOVE_ENTRY(entry
->grouping
)
526 MOVE_ENTRY(entry
->mb_track_id
)
529 void copy_mp3entry(struct mp3entry
*dest
, const struct mp3entry
*orig
)
531 memcpy(dest
, orig
, sizeof(struct mp3entry
));
532 adjust_mp3entry(dest
, dest
, orig
);
535 /* A shortcut to simplify the common task of clearing the struct */
536 void wipe_mp3entry(struct mp3entry
*id3
)
538 memset(id3
, 0, sizeof (struct mp3entry
));
541 #if CONFIG_CODEC == SWCODEC
542 /* Glean what is possible from the filename alone - does not parse metadata */
543 void fill_metadata_from_path(struct mp3entry
*id3
, const char *trackname
)
547 /* Clear the mp3entry to avoid having bogus pointers appear */
550 /* Find the filename portion of the path */
551 p
= strrchr(trackname
, '/');
552 strlcpy(id3
->id3v2buf
, p
? ++p
: id3
->path
, ID3V2_BUF_SIZE
);
554 /* Get the format from the extension and trim it off */
555 p
= strrchr(id3
->id3v2buf
, '.');
558 /* Might be wrong for container formats - should we bother? */
559 id3
->codectype
= probe_file_format(p
);
561 if (id3
->codectype
!= AFMT_UNKNOWN
)
565 /* Set the filename as the title */
566 id3
->title
= id3
->id3v2buf
;
568 /* Copy the path info */
569 strlcpy(id3
->path
, trackname
, sizeof (id3
->path
));
571 #endif /* CONFIG_CODEC == SWCODEC */
575 #if CONFIG_CODEC == SWCODEC
577 enum { AUTORESUMABLE_UNKNOWN
= 0, AUTORESUMABLE_TRUE
, AUTORESUMABLE_FALSE
};
579 bool autoresumable(struct mp3entry
*id3
)
585 if (id3
->autoresumable
) /* result cached? */
586 return id3
->autoresumable
== AUTORESUMABLE_TRUE
;
588 is_resumable
= false;
592 for (path
= global_settings
.autoresume_paths
;
593 *path
; /* search terms left? */
596 if (*path
== ':') /* Skip empty search patterns */
599 /* FIXME: As soon as strcspn or strchrnul are made available in
600 the core, the following can be made more efficient. */
601 endp
= strchr(path
, ':');
607 /* Note: At this point, len is always > 0 */
609 if (strncasecmp(id3
->path
, path
, len
) == 0)
611 /* Full directory-name matches only. Trailing '/' in
613 if (id3
->path
[len
] == '/' || id3
->path
[len
- 1] == '/')
625 is_resumable
? AUTORESUMABLE_TRUE
: AUTORESUMABLE_FALSE
;
627 logf("autoresumable: %s is%s resumable",
628 id3
->path
, is_resumable
? "" : " not");
634 #endif /* HAVE_TAGCACHE */
635 #endif /* __PCTOOL__ */