Manual: Simplify Makefile htlatex call a bit.
[kugel-rb.git] / apps / metadata.c
blobe88603721b22c4bacfc2361bcfe3da10e9d02c95
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <ctype.h>
24 #include "string-extra.h"
26 #include "debug.h"
27 #include "logf.h"
28 #include "settings.h"
29 #include "cuesheet.h"
30 #include "metadata.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 */
44 id3->vbr = true;
45 id3->filesize = filesize(fd);
46 return skip_id3v2(fd, id3);
49 static bool get_other_asap_metadata(int fd, struct mp3entry *id3)
51 id3->bitrate = 706;
52 id3->frequency = 44100;
53 id3->vbr = false;
54 id3->filesize = filesize(fd);
55 id3->genre_string = id3_get_num_genre(36);
56 return true;
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 */
64 [AFMT_UNKNOWN] =
65 AFMT_ENTRY("???", NULL, NULL, NULL, NULL ),
67 /* MPEG Audio layer 1 */
68 [AFMT_MPA_L1] =
69 AFMT_ENTRY("MP1", "mpa", NULL, get_mp3_metadata, "mp1\0"),
70 /* MPEG Audio layer 2 */
71 [AFMT_MPA_L2] =
72 AFMT_ENTRY("MP2", "mpa", NULL, get_mp3_metadata, "mpa\0mp2\0"),
73 /* MPEG Audio layer 3 */
74 [AFMT_MPA_L3] =
75 AFMT_ENTRY("MP3", "mpa", "mp3_enc", get_mp3_metadata, "mp3\0"),
77 #if CONFIG_CODEC == SWCODEC
78 /* Audio Interchange File Format */
79 [AFMT_AIFF] =
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) */
82 [AFMT_PCM_WAV] =
83 AFMT_ENTRY("WAV", "wav", "wav_enc", get_wave_metadata, "wav\0at3\0"),
84 /* Ogg Vorbis */
85 [AFMT_OGG_VORBIS] =
86 AFMT_ENTRY("Ogg", "vorbis", NULL, get_ogg_metadata, "ogg\0oga\0"),
87 /* FLAC */
88 [AFMT_FLAC] =
89 AFMT_ENTRY("FLAC", "flac", NULL, get_flac_metadata, "flac\0"),
90 /* Musepack SV7 */
91 [AFMT_MPC_SV7] =
92 AFMT_ENTRY("MPCv7", "mpc", NULL, get_musepack_metadata,"mpc\0"),
93 /* A/52 (aka AC3) audio */
94 [AFMT_A52] =
95 AFMT_ENTRY("AC3", "a52", NULL, get_a52_metadata, "a52\0ac3\0"),
96 /* WavPack */
97 [AFMT_WAVPACK] =
98 AFMT_ENTRY("WV","wavpack","wavpack_enc",get_wavpack_metadata,"wv\0"),
99 /* Apple Lossless Audio Codec */
100 [AFMT_MP4_ALAC] =
101 AFMT_ENTRY("ALAC", "alac", NULL, get_mp4_metadata, "m4a\0m4b\0"),
102 /* Advanced Audio Coding in M4A container */
103 [AFMT_MP4_AAC] =
104 AFMT_ENTRY("AAC", "aac", NULL, get_mp4_metadata, "mp4\0"),
105 /* Shorten */
106 [AFMT_SHN] =
107 AFMT_ENTRY("SHN","shorten", NULL, get_shn_metadata, "shn\0"),
108 /* SID File Format */
109 [AFMT_SID] =
110 AFMT_ENTRY("SID", "sid", NULL, get_sid_metadata, "sid\0"),
111 /* ADX File Format */
112 [AFMT_ADX] =
113 AFMT_ENTRY("ADX", "adx", NULL, get_adx_metadata, "adx\0"),
114 /* NESM (NES Sound Format) */
115 [AFMT_NSF] =
116 AFMT_ENTRY("NSF", "nsf", NULL, get_nsf_metadata, "nsf\0nsfe\0"),
117 /* Speex File Format */
118 [AFMT_SPEEX] =
119 AFMT_ENTRY("Speex", "speex",NULL, get_ogg_metadata, "spx\0"),
120 /* SPC700 Save State */
121 [AFMT_SPC] =
122 AFMT_ENTRY("SPC", "spc", NULL, get_spc_metadata, "spc\0"),
123 /* APE (Monkey's Audio) */
124 [AFMT_APE] =
125 AFMT_ENTRY("APE", "ape", NULL, get_monkeys_metadata,"ape\0mac\0"),
126 /* WMA (WMAV1/V2 in ASF) */
127 [AFMT_WMA] =
128 AFMT_ENTRY("WMA", "wma", NULL, get_asf_metadata,"wma\0wmv\0asf\0"),
129 /* WMA Professional in ASF */
130 [AFMT_WMAPRO] =
131 AFMT_ENTRY("WMAPro","wmapro",NULL, NULL, "wma\0wmv\0asf\0"),
132 /* Amiga MOD File */
133 [AFMT_MOD] =
134 AFMT_ENTRY("MOD", "mod", NULL, get_mod_metadata, "mod\0"),
135 /* Atari SAP File */
136 [AFMT_SAP] =
137 AFMT_ENTRY("SAP", "asap", NULL, get_asap_metadata, "sap\0"),
138 /* Cook in RM/RA */
139 [AFMT_RM_COOK] =
140 AFMT_ENTRY("Cook", "cook", NULL, get_rm_metadata,"rm\0ra\0rmvb\0"),
141 /* AAC in RM/RA */
142 [AFMT_RM_AAC] =
143 AFMT_ENTRY("RAAC", "raac", NULL, NULL, "rm\0ra\0rmvb\0"),
144 /* AC3 in RM/RA */
145 [AFMT_RM_AC3] =
146 AFMT_ENTRY("AC3", "a52_rm", NULL, NULL, "rm\0ra\0rmvb\0"),
147 /* ATRAC3 in RM/RA */
148 [AFMT_RM_ATRAC3] =
149 AFMT_ENTRY("ATRAC3","atrac3_rm",NULL, NULL, "rm\0ra\0rmvb\0"),
150 /* Atari CMC File */
151 [AFMT_CMC] =
152 AFMT_ENTRY("CMC", "asap", NULL, get_other_asap_metadata,"cmc\0"),
153 /* Atari CM3 File */
154 [AFMT_CM3] =
155 AFMT_ENTRY("CM3", "asap", NULL, get_other_asap_metadata,"cm3\0"),
156 /* Atari CMR File */
157 [AFMT_CMR] =
158 AFMT_ENTRY("CMR", "asap", NULL, get_other_asap_metadata,"cmr\0"),
159 /* Atari CMS File */
160 [AFMT_CMS] =
161 AFMT_ENTRY("CMS", "asap", NULL, get_other_asap_metadata,"cms\0"),
162 /* Atari DMC File */
163 [AFMT_DMC] =
164 AFMT_ENTRY("DMC", "asap", NULL, get_other_asap_metadata,"dmc\0"),
165 /* Atari DLT File */
166 [AFMT_DLT] =
167 AFMT_ENTRY("DLT", "asap", NULL, get_other_asap_metadata,"dlt\0"),
168 /* Atari MPT File */
169 [AFMT_MPT] =
170 AFMT_ENTRY("MPT", "asap", NULL, get_other_asap_metadata,"mpt\0"),
171 /* Atari MPD File */
172 [AFMT_MPD] =
173 AFMT_ENTRY("MPD", "asap", NULL, get_other_asap_metadata,"mpd\0"),
174 /* Atari RMT File */
175 [AFMT_RMT] =
176 AFMT_ENTRY("RMT", "asap", NULL, get_other_asap_metadata,"rmt\0"),
177 /* Atari TMC File */
178 [AFMT_TMC] =
179 AFMT_ENTRY("TMC", "asap", NULL, get_other_asap_metadata,"tmc\0"),
180 /* Atari TM8 File */
181 [AFMT_TM8] =
182 AFMT_ENTRY("TM8", "asap", NULL, get_other_asap_metadata,"tm8\0"),
183 /* Atari TM2 File */
184 [AFMT_TM2] =
185 AFMT_ENTRY("TM2", "asap", NULL, get_other_asap_metadata,"tm2\0"),
186 /* Atrac3 in Sony OMA Container */
187 [AFMT_OMA_ATRAC3] =
188 AFMT_ENTRY("ATRAC3","atrac3_oma",NULL, get_oma_metadata, "oma\0aa3\0"),
189 /* SMAF (Synthetic music Mobile Application Format) */
190 [AFMT_SMAF] =
191 AFMT_ENTRY("SMAF", "smaf", NULL, get_smaf_metadata, "mmf\0"),
192 /* Sun Audio file */
193 [AFMT_AU] =
194 AFMT_ENTRY("AU", "au", NULL, get_au_metadata, "au\0snd\0"),
195 /* VOX (Dialogic telephony file formats) */
196 [AFMT_VOX] =
197 AFMT_ENTRY("VOX", "vox", NULL, get_vox_metadata, "vox\0"),
198 /* Wave64 */
199 [AFMT_WAVE64] =
200 AFMT_ENTRY("WAVE64","wav64",NULL, get_wave64_metadata,"w64\0"),
201 /* True Audio */
202 [AFMT_TTA] =
203 AFMT_ENTRY("TTA", "tta", NULL, get_tta_metadata, "tta\0"),
204 /* WMA Voice in ASF */
205 [AFMT_WMAVOICE] =
206 AFMT_ENTRY("WMAVoice","wmavoice",NULL, NULL, "wma\0wmv\0"),
207 /* Musepack SV8 */
208 [AFMT_MPC_SV8] =
209 AFMT_ENTRY("MPCv8", "mpc", NULL, get_musepack_metadata,"mpc\0"),
210 /* Advanced Audio Coding High Efficiency in M4A container */
211 [AFMT_MP4_AAC_HE] =
212 AFMT_ENTRY("AAC-HE","aac", NULL, get_mp4_metadata, "mp4\0"),
213 #endif
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;
247 switch (type) {
248 case AFMT_MPA_L1:
249 case AFMT_MPA_L2:
250 case AFMT_MPA_L3:
251 base_type = AFMT_MPA_L3;
252 break;
253 case AFMT_MPC_SV7:
254 case AFMT_MPC_SV8:
255 base_type = AFMT_MPC_SV7;
256 break;
257 case AFMT_MP4_AAC:
258 case AFMT_MP4_AAC_HE:
259 base_type = AFMT_MP4_AAC;
260 break;
261 case AFMT_SAP:
262 case AFMT_CMC:
263 case AFMT_CM3:
264 case AFMT_CMR:
265 case AFMT_CMS:
266 case AFMT_DMC:
267 case AFMT_DLT:
268 case AFMT_MPT:
269 case AFMT_MPD:
270 case AFMT_RMT:
271 case AFMT_TMC:
272 case AFMT_TM8:
273 case AFMT_TM2:
274 base_type = AFMT_SAP;
275 break;
276 default:
277 break;
280 return base_type;
283 /* Get the basic audio type */
284 enum data_type get_audio_base_data_type(int afmt)
286 if ((unsigned)afmt >= AFMT_NUM_CODECS)
287 return TYPE_UNKNOWN;
289 switch (get_audio_base_codec_type(afmt))
291 case AFMT_NSF:
292 case AFMT_SPC:
293 case AFMT_SID:
294 case AFMT_MOD:
295 case AFMT_SAP:
296 /* Type must be allocated and loaded in its entirety onto
297 the buffer */
298 return TYPE_ATOMIC_AUDIO;
300 default:
301 /* Assume type may be loaded and discarded incrementally */
302 return TYPE_PACKET_AUDIO;
304 case AFMT_UNKNOWN:
305 /* Have no idea at all */
306 return TYPE_UNKNOWN;
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)
314 switch (afmt)
316 case AFMT_MPA_L1:
317 case AFMT_MPA_L2:
318 case AFMT_MPA_L3:
319 case AFMT_WAVPACK:
320 /* Format may be loaded at the first needed frame */
321 return true;
322 default:
323 /* Format must be loaded from the beginning of the file
324 (does not imply 'atomic', while 'atomic' implies 'no offset') */
325 return false;
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)
334 char *suffix;
335 unsigned int i;
337 suffix = strrchr(filename, '.');
339 if (suffix == NULL)
341 return AFMT_UNKNOWN;
344 /* skip '.' */
345 suffix++;
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)
356 return i;
359 ext += strlen(ext) + 1;
361 while (*ext != '\0');
364 return AFMT_UNKNOWN;
367 /* Note, that this returns false for successful, true for error! */
368 bool mp3info(struct mp3entry *entry, const char *filename)
370 int fd;
371 bool result;
373 fd = open(filename, O_RDONLY);
374 if (fd < 0)
375 return true;
377 result = !get_metadata(entry, fd, filename);
379 close(fd);
381 return result;
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;
390 int logfd = 0;
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);
395 if (logfd >= 0)
397 write(logfd, trackname, strlen(trackname));
398 write(logfd, "\n", 1);
399 close(logfd);
403 /* Clear the mp3entry to avoid having bogus pointers appear */
404 wipe_mp3entry(id3);
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);
415 return false;
418 if (!entry->parse_func(fd, id3))
420 DEBUGF("parsing %s failed (format: %s)", trackname, entry->label);
421 return false;
424 lseek(fd, 0, SEEK_SET);
425 strlcpy(id3->path, trackname, sizeof(id3->path));
426 /* We have successfully read the metadata from the file */
427 return true;
430 #ifndef __PCTOOL__
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";
436 size_t len, version;
437 void *tail;
439 if (bufgettail(handle_id, 128, &tail) != 128)
440 return;
442 if (memcmp(tail, tag, 3) == 0)
444 /* Skip id3v1 tag */
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)
451 return;
453 /* Check for APE tag (look for the APE tag footer) */
454 if (memcmp(tail, apetag, 8) != 0)
455 return;
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]);
460 if (version == 2000)
461 len += 32; /* APEv2 has a 32 byte header */
463 /* Skip APE tag */
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)
474 long offset;
475 if (orig > dest)
476 offset = -((size_t)orig - (size_t)dest);
477 else
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)
515 char *p;
517 /* Clear the mp3entry to avoid having bogus pointers appear */
518 wipe_mp3entry(id3);
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, '.');
526 if (p)
528 /* Might be wrong for container formats - should we bother? */
529 id3->codectype = probe_file_format(p);
531 if (id3->codectype != AFMT_UNKNOWN)
532 *p = '\0';
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 */
543 #ifndef __PCTOOL__
544 #ifdef HAVE_TAGCACHE
545 #if CONFIG_CODEC == SWCODEC
547 enum { AUTORESUMABLE_UNKNOWN = 0, AUTORESUMABLE_TRUE, AUTORESUMABLE_FALSE };
549 bool autoresumable(struct mp3entry *id3)
551 char *endp, *path;
552 size_t len;
553 bool is_resumable;
555 if (id3->autoresumable) /* result cached? */
556 return id3->autoresumable == AUTORESUMABLE_TRUE;
558 is_resumable = false;
560 if (id3->path)
562 for (path = global_settings.autoresume_paths;
563 *path; /* search terms left? */
564 path++)
566 if (*path == ':') /* Skip empty search patterns */
567 continue;
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, ':');
572 if (endp)
573 len = endp - path;
574 else
575 len = strlen(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
582 search path OK. */
583 if (id3->path[len] == '/' || id3->path[len - 1] == '/')
585 is_resumable = true;
586 break;
589 path += len - 1;
593 /* cache result */
594 id3->autoresumable =
595 is_resumable ? AUTORESUMABLE_TRUE : AUTORESUMABLE_FALSE;
597 logf("autoresumable: %s is%s resumable",
598 id3->path, is_resumable ? "" : " not");
600 return is_resumable;
603 #endif /* SWCODEC */
604 #endif /* HAVE_TAGCACHE */
605 #endif /* __PCTOOL__ */