Fix a warning.
[maemo-rb.git] / apps / metadata.c
blob46b9482bc71c20908f91810175a774fda1f7bb47
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 */
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) */
243 /* Simple file type probing by looking at the filename extension. */
244 unsigned int probe_file_format(const char *filename)
246 char *suffix;
247 unsigned int i;
249 suffix = strrchr(filename, '.');
251 if (suffix == NULL)
253 return AFMT_UNKNOWN;
256 /* skip '.' */
257 suffix++;
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)
268 return i;
271 ext += strlen(ext) + 1;
273 while (*ext != '\0');
276 return AFMT_UNKNOWN;
279 /* Note, that this returns false for successful, true for error! */
280 bool mp3info(struct mp3entry *entry, const char *filename)
282 int fd;
283 bool result;
285 fd = open(filename, O_RDONLY);
286 if (fd < 0)
287 return true;
289 result = !get_metadata(entry, fd, filename);
291 close(fd);
293 return result;
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;
302 int logfd = 0;
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);
307 if (logfd >= 0)
309 write(logfd, trackname, strlen(trackname));
310 write(logfd, "\n", 1);
311 close(logfd);
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);
327 return false;
330 if (!entry->parse_func(fd, id3))
332 DEBUGF("parsing %s failed (format: %s)", trackname, entry->label);
333 return false;
336 lseek(fd, 0, SEEK_SET);
337 strlcpy(id3->path, trackname, sizeof(id3->path));
338 /* We have successfully read the metadata from the file */
339 return true;
342 #ifndef __PCTOOL__
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";
348 size_t len, version;
349 void *tail;
351 if (bufgettail(handle_id, 128, &tail) != 128)
352 return;
354 if (memcmp(tail, tag, 3) == 0)
356 /* Skip id3v1 tag */
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)
363 return;
365 /* Check for APE tag (look for the APE tag footer) */
366 if (memcmp(tail, apetag, 8) != 0)
367 return;
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]);
372 if (version == 2000)
373 len += 32; /* APEv2 has a 32 byte header */
375 /* Skip APE tag */
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)
386 long offset;
387 if (orig > dest)
388 offset = -((size_t)orig - (size_t)dest);
389 else
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);
417 #ifndef __PCTOOL__
418 #ifdef HAVE_TAGCACHE
419 #if CONFIG_CODEC == SWCODEC
421 enum { AUTORESUMABLE_UNKNOWN = 0, AUTORESUMABLE_TRUE, AUTORESUMABLE_FALSE };
423 bool autoresumable(struct mp3entry *id3)
425 char *endp, *path;
426 size_t len;
427 bool is_resumable;
429 if (id3->autoresumable) /* result cached? */
430 return id3->autoresumable == AUTORESUMABLE_TRUE;
432 is_resumable = false;
434 if (id3->path)
436 for (path = global_settings.autoresume_paths;
437 *path; /* search terms left? */
438 path++)
440 if (*path == ':') /* Skip empty search patterns */
441 continue;
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, ':');
446 if (endp)
447 len = endp - path;
448 else
449 len = strlen(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
456 search path OK. */
457 if (id3->path[len] == '/' || id3->path[len - 1] == '/')
459 is_resumable = true;
460 break;
463 path += len - 1;
467 /* cache result */
468 id3->autoresumable =
469 is_resumable ? AUTORESUMABLE_TRUE : AUTORESUMABLE_FALSE;
471 logf("autoresumable: %s is%s resumable",
472 id3->path, is_resumable ? "" : " not");
474 return is_resumable;
477 #endif /* SWCODEC */
478 #endif /* HAVE_TAGCACHE */
479 #endif /* __PCTOOL__ */