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 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) */
242 #if CONFIG_CODEC == SWCODEC
243 /* Get the canonical AFMT type */
244 int get_audio_base_codec_type(int type
)
246 int base_type
= type
;
251 base_type
= AFMT_MPA_L3
;
255 base_type
= AFMT_MPC_SV7
;
258 case AFMT_MP4_AAC_HE
:
259 base_type
= AFMT_MP4_AAC
;
274 base_type
= AFMT_SAP
;
283 /* Get the basic audio type */
284 enum data_type
get_audio_base_data_type(int afmt
)
286 if ((unsigned)afmt
>= AFMT_NUM_CODECS
)
289 switch (get_audio_base_codec_type(afmt
))
296 /* Type must be allocated and loaded in its entirety onto
298 return TYPE_ATOMIC_AUDIO
;
301 /* Assume type may be loaded and discarded incrementally */
302 return TYPE_PACKET_AUDIO
;
305 /* Have no idea at all */
310 /* Is the format allowed to buffer starting at some offset other than 0
311 or first frame only for resume purposes? */
312 bool format_buffers_with_offset(int afmt
)
320 /* Format may be loaded at the first needed frame */
323 /* Format must be loaded from the beginning of the file
324 (does not imply 'atomic', while 'atomic' implies 'no offset') */
328 #endif /* CONFIG_CODEC == SWCODEC */
331 /* Simple file type probing by looking at the filename extension. */
332 unsigned int probe_file_format(const char *filename
)
337 suffix
= strrchr(filename
, '.');
347 for (i
= 1; i
< AFMT_NUM_CODECS
; i
++)
349 /* search extension list for type */
350 const char *ext
= audio_formats
[i
].ext_list
;
354 if (strcasecmp(suffix
, ext
) == 0)
359 ext
+= strlen(ext
) + 1;
361 while (*ext
!= '\0');
367 /* Note, that this returns false for successful, true for error! */
368 bool mp3info(struct mp3entry
*entry
, const char *filename
)
373 fd
= open(filename
, O_RDONLY
);
377 result
= !get_metadata(entry
, fd
, filename
);
384 /* Get metadata for track - return false if parsing showed problems with the
385 * file that would prevent playback.
387 bool get_metadata(struct mp3entry
* id3
, int fd
, const char* trackname
)
389 const struct afmt_entry
*entry
;
391 DEBUGF("Read metadata for %s\n", trackname
);
392 if (write_metadata_log
)
394 logfd
= open("/metadata.log", O_WRONLY
| O_APPEND
| O_CREAT
, 0666);
397 write(logfd
, trackname
, strlen(trackname
));
398 write(logfd
, "\n", 1);
403 /* Clear the mp3entry to avoid having bogus pointers appear */
406 /* Take our best guess at the codec type based on file extension */
407 id3
->codectype
= probe_file_format(trackname
);
409 entry
= &audio_formats
[id3
->codectype
];
411 /* Load codec specific track tag information and confirm the codec type. */
412 if (!entry
->parse_func
)
414 DEBUGF("nothing to parse for %s (format %s)", trackname
, entry
->label
);
418 if (!entry
->parse_func(fd
, id3
))
420 DEBUGF("parsing %s failed (format: %s)", trackname
, entry
->label
);
424 lseek(fd
, 0, SEEK_SET
);
425 strlcpy(id3
->path
, trackname
, sizeof(id3
->path
));
426 /* We have successfully read the metadata from the file */
431 #if CONFIG_CODEC == SWCODEC
432 void strip_tags(int handle_id
)
434 static const unsigned char tag
[] = "TAG";
435 static const unsigned char apetag
[] = "APETAGEX";
439 if (bufgettail(handle_id
, 128, &tail
) != 128)
442 if (memcmp(tail
, tag
, 3) == 0)
445 logf("Cutting off ID3v1 tag");
446 bufcuttail(handle_id
, 128);
449 /* Get a new tail, as the old one may have been cut */
450 if (bufgettail(handle_id
, 32, &tail
) != 32)
453 /* Check for APE tag (look for the APE tag footer) */
454 if (memcmp(tail
, apetag
, 8) != 0)
457 /* Read the version and length from the footer */
458 version
= get_long_le(&((unsigned char *)tail
)[8]);
459 len
= get_long_le(&((unsigned char *)tail
)[12]);
461 len
+= 32; /* APEv2 has a 32 byte header */
464 logf("Cutting off APE tag (%ldB)", len
);
465 bufcuttail(handle_id
, len
);
467 #endif /* CONFIG_CODEC == SWCODEC */
468 #endif /* ! __PCTOOL__ */
470 #define MOVE_ENTRY(x) if (x) x += offset;
472 void adjust_mp3entry(struct mp3entry
*entry
, void *dest
, const void *orig
)
476 offset
= -((size_t)orig
- (size_t)dest
);
478 offset
= ((size_t)dest
- (size_t)orig
);
480 MOVE_ENTRY(entry
->title
)
481 MOVE_ENTRY(entry
->artist
)
482 MOVE_ENTRY(entry
->album
)
484 if (entry
->genre_string
> (char*)orig
&&
485 entry
->genre_string
< (char*)orig
+ sizeof(struct mp3entry
))
486 /* Don't adjust that if it points to an entry of the "genres" array */
487 entry
->genre_string
+= offset
;
489 MOVE_ENTRY(entry
->track_string
)
490 MOVE_ENTRY(entry
->disc_string
)
491 MOVE_ENTRY(entry
->year_string
)
492 MOVE_ENTRY(entry
->composer
)
493 MOVE_ENTRY(entry
->comment
)
494 MOVE_ENTRY(entry
->albumartist
)
495 MOVE_ENTRY(entry
->grouping
)
496 MOVE_ENTRY(entry
->mb_track_id
)
499 void copy_mp3entry(struct mp3entry
*dest
, const struct mp3entry
*orig
)
501 memcpy(dest
, orig
, sizeof(struct mp3entry
));
502 adjust_mp3entry(dest
, dest
, orig
);
505 /* A shortcut to simplify the common task of clearing the struct */
506 void wipe_mp3entry(struct mp3entry
*id3
)
508 memset(id3
, 0, sizeof (struct mp3entry
));
511 #if CONFIG_CODEC == SWCODEC
512 /* Glean what is possible from the filename alone - does not parse metadata */
513 void fill_metadata_from_path(struct mp3entry
*id3
, const char *trackname
)
517 /* Clear the mp3entry to avoid having bogus pointers appear */
520 /* Find the filename portion of the path */
521 p
= strrchr(trackname
, '/');
522 strlcpy(id3
->id3v2buf
, p
? ++p
: id3
->path
, ID3V2_BUF_SIZE
);
524 /* Get the format from the extension and trim it off */
525 p
= strrchr(id3
->id3v2buf
, '.');
528 /* Might be wrong for container formats - should we bother? */
529 id3
->codectype
= probe_file_format(p
);
531 if (id3
->codectype
!= AFMT_UNKNOWN
)
535 /* Set the filename as the title */
536 id3
->title
= id3
->id3v2buf
;
538 /* Copy the path info */
539 strlcpy(id3
->path
, trackname
, sizeof (id3
->path
));
541 #endif /* CONFIG_CODEC == SWCODEC */
545 #if CONFIG_CODEC == SWCODEC
547 enum { AUTORESUMABLE_UNKNOWN
= 0, AUTORESUMABLE_TRUE
, AUTORESUMABLE_FALSE
};
549 bool autoresumable(struct mp3entry
*id3
)
555 if (id3
->autoresumable
) /* result cached? */
556 return id3
->autoresumable
== AUTORESUMABLE_TRUE
;
558 is_resumable
= false;
562 for (path
= global_settings
.autoresume_paths
;
563 *path
; /* search terms left? */
566 if (*path
== ':') /* Skip empty search patterns */
569 /* FIXME: As soon as strcspn or strchrnul are made available in
570 the core, the following can be made more efficient. */
571 endp
= strchr(path
, ':');
577 /* Note: At this point, len is always > 0 */
579 if (strncasecmp(id3
->path
, path
, len
) == 0)
581 /* Full directory-name matches only. Trailing '/' in
583 if (id3
->path
[len
] == '/' || id3
->path
[len
- 1] == '/')
595 is_resumable
? AUTORESUMABLE_TRUE
: AUTORESUMABLE_FALSE
;
597 logf("autoresumable: %s is%s resumable",
598 id3
->path
, is_resumable
? "" : " not");
604 #endif /* HAVE_TAGCACHE */
605 #endif /* __PCTOOL__ */