Roll back unintentionally submitted file.
[maemo-rb.git] / apps / metadata.c
blob8ffd1a6af1909a3206b4fa63ba43921b9e41ed14
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 "cuesheet.h"
29 #include "metadata.h"
31 #include "metadata/metadata_parsers.h"
33 #if CONFIG_CODEC == SWCODEC
35 /* For trailing tag stripping */
36 #include "buffering.h"
38 #include "metadata/metadata_common.h"
40 static bool get_shn_metadata(int fd, struct mp3entry *id3)
42 /* TODO: read the id3v2 header if it exists */
43 id3->vbr = true;
44 id3->filesize = filesize(fd);
45 return skip_id3v2(fd, id3);
48 static bool get_other_asap_metadata(int fd, struct mp3entry *id3)
50 id3->bitrate = 706;
51 id3->frequency = 44100;
52 id3->vbr = false;
53 id3->filesize = filesize(fd);
54 id3->genre_string = id3_get_num_genre(36);
55 return true;
57 #endif /* CONFIG_CODEC == SWCODEC */
58 bool write_metadata_log = false;
60 const struct afmt_entry audio_formats[AFMT_NUM_CODECS] =
62 /* Unknown file format */
63 [AFMT_UNKNOWN] =
64 AFMT_ENTRY("???", NULL, NULL, NULL, NULL ),
66 /* MPEG Audio layer 1 */
67 [AFMT_MPA_L1] =
68 AFMT_ENTRY("MP1", "mpa", NULL, get_mp3_metadata, "mp1\0"),
69 /* MPEG Audio layer 2 */
70 [AFMT_MPA_L2] =
71 AFMT_ENTRY("MP2", "mpa", NULL, get_mp3_metadata, "mpa\0mp2\0"),
72 /* MPEG Audio layer 3 */
73 [AFMT_MPA_L3] =
74 AFMT_ENTRY("MP3", "mpa", "mp3_enc", get_mp3_metadata, "mp3\0"),
76 #if CONFIG_CODEC == SWCODEC
77 /* Audio Interchange File Format */
78 [AFMT_AIFF] =
79 AFMT_ENTRY("AIFF", "aiff", "aiff_enc", get_aiff_metadata, "aiff\0aif\0"),
80 /* Uncompressed PCM in a WAV file OR ATRAC3 stream in WAV file (.at3) */
81 [AFMT_PCM_WAV] =
82 AFMT_ENTRY("WAV", "wav", "wav_enc", get_wave_metadata, "wav\0at3\0"),
83 /* Ogg Vorbis */
84 [AFMT_OGG_VORBIS] =
85 AFMT_ENTRY("Ogg", "vorbis", NULL, get_ogg_metadata, "ogg\0oga\0"),
86 /* FLAC */
87 [AFMT_FLAC] =
88 AFMT_ENTRY("FLAC", "flac", NULL, get_flac_metadata, "flac\0"),
89 /* Musepack SV7 */
90 [AFMT_MPC_SV7] =
91 AFMT_ENTRY("MPCv7", "mpc", NULL, get_musepack_metadata,"mpc\0"),
92 /* Musepack SV8 */
93 [AFMT_MPC_SV8] =
94 AFMT_ENTRY("MPCv8", "mpc", NULL, get_musepack_metadata,"mpc\0"),
95 /* A/52 (aka AC3) audio */
96 [AFMT_A52] =
97 AFMT_ENTRY("AC3", "a52", NULL, get_a52_metadata, "a52\0ac3\0"),
98 /* WavPack */
99 [AFMT_WAVPACK] =
100 AFMT_ENTRY("WV","wavpack","wavpack_enc",get_wavpack_metadata,"wv\0"),
101 /* Apple Lossless Audio Codec */
102 [AFMT_MP4_ALAC] =
103 AFMT_ENTRY("ALAC", "alac", NULL, get_mp4_metadata, "m4a\0m4b\0"),
104 /* Advanced Audio Coding in M4A container */
105 [AFMT_MP4_AAC] =
106 AFMT_ENTRY("AAC", "aac", NULL, get_mp4_metadata, "mp4\0"),
107 /* Advanced Audio Coding High Efficiency in M4A container */
108 [AFMT_MP4_AAC_HE] =
109 AFMT_ENTRY("AAC-HE","aac", NULL, get_mp4_metadata, "mp4\0"),
110 /* Shorten */
111 [AFMT_SHN] =
112 AFMT_ENTRY("SHN","shorten", NULL, get_shn_metadata, "shn\0"),
113 /* SID File Format */
114 [AFMT_SID] =
115 AFMT_ENTRY("SID", "sid", NULL, get_sid_metadata, "sid\0"),
116 /* ADX File Format */
117 [AFMT_ADX] =
118 AFMT_ENTRY("ADX", "adx", NULL, get_adx_metadata, "adx\0"),
119 /* NESM (NES Sound Format) */
120 [AFMT_NSF] =
121 AFMT_ENTRY("NSF", "nsf", NULL, get_nsf_metadata, "nsf\0nsfe\0"),
122 /* Speex File Format */
123 [AFMT_SPEEX] =
124 AFMT_ENTRY("Speex", "speex",NULL, get_ogg_metadata, "spx\0"),
125 /* SPC700 Save State */
126 [AFMT_SPC] =
127 AFMT_ENTRY("SPC", "spc", NULL, get_spc_metadata, "spc\0"),
128 /* APE (Monkey's Audio) */
129 [AFMT_APE] =
130 AFMT_ENTRY("APE", "ape", NULL, get_monkeys_metadata,"ape\0mac\0"),
131 /* WMA (WMAV1/V2 in ASF) */
132 [AFMT_WMA] =
133 AFMT_ENTRY("WMA", "wma", NULL, get_asf_metadata,"wma\0wmv\0asf\0"),
134 /* WMA Professional in ASF */
135 [AFMT_WMAPRO] =
136 AFMT_ENTRY("WMAPro","wmapro",NULL, NULL, "wma\0wmv\0asf\0"),
137 /* Amiga MOD File */
138 [AFMT_MOD] =
139 AFMT_ENTRY("MOD", "mod", NULL, get_mod_metadata, "mod\0"),
140 /* Atari SAP File */
141 [AFMT_SAP] =
142 AFMT_ENTRY("SAP", "asap", NULL, get_asap_metadata, "sap\0"),
143 /* Cook in RM/RA */
144 [AFMT_RM_COOK] =
145 AFMT_ENTRY("Cook", "cook", NULL, get_rm_metadata,"rm\0ra\0rmvb\0"),
146 /* AAC in RM/RA */
147 [AFMT_RM_AAC] =
148 AFMT_ENTRY("RAAC", "raac", NULL, NULL, "rm\0ra\0rmvb\0"),
149 /* AC3 in RM/RA */
150 [AFMT_RM_AC3] =
151 AFMT_ENTRY("AC3", "a52_rm", NULL, NULL, "rm\0ra\0rmvb\0"),
152 /* ATRAC3 in RM/RA */
153 [AFMT_RM_ATRAC3] =
154 AFMT_ENTRY("ATRAC3","atrac3_rm",NULL, NULL, "rm\0ra\0rmvb\0"),
155 /* Atari CMC File */
156 [AFMT_CMC] =
157 AFMT_ENTRY("CMC", "asap", NULL, get_other_asap_metadata,"cmc\0"),
158 /* Atari CM3 File */
159 [AFMT_CM3] =
160 AFMT_ENTRY("CM3", "asap", NULL, get_other_asap_metadata,"cm3\0"),
161 /* Atari CMR File */
162 [AFMT_CMR] =
163 AFMT_ENTRY("CMR", "asap", NULL, get_other_asap_metadata,"cmr\0"),
164 /* Atari CMS File */
165 [AFMT_CMS] =
166 AFMT_ENTRY("CMS", "asap", NULL, get_other_asap_metadata,"cms\0"),
167 /* Atari DMC File */
168 [AFMT_DMC] =
169 AFMT_ENTRY("DMC", "asap", NULL, get_other_asap_metadata,"dmc\0"),
170 /* Atari DLT File */
171 [AFMT_DLT] =
172 AFMT_ENTRY("DLT", "asap", NULL, get_other_asap_metadata,"dlt\0"),
173 /* Atari MPT File */
174 [AFMT_MPT] =
175 AFMT_ENTRY("MPT", "asap", NULL, get_other_asap_metadata,"mpt\0"),
176 /* Atari MPD File */
177 [AFMT_MPD] =
178 AFMT_ENTRY("MPD", "asap", NULL, get_other_asap_metadata,"mpd\0"),
179 /* Atari RMT File */
180 [AFMT_RMT] =
181 AFMT_ENTRY("RMT", "asap", NULL, get_other_asap_metadata,"rmt\0"),
182 /* Atari TMC File */
183 [AFMT_TMC] =
184 AFMT_ENTRY("TMC", "asap", NULL, get_other_asap_metadata,"tmc\0"),
185 /* Atari TM8 File */
186 [AFMT_TM8] =
187 AFMT_ENTRY("TM8", "asap", NULL, get_other_asap_metadata,"tm8\0"),
188 /* Atari TM2 File */
189 [AFMT_TM2] =
190 AFMT_ENTRY("TM2", "asap", NULL, get_other_asap_metadata,"tm2\0"),
191 /* Atrac3 in Sony OMA Container */
192 [AFMT_OMA_ATRAC3] =
193 AFMT_ENTRY("ATRAC3","atrac3_oma",NULL, get_oma_metadata, "oma\0aa3\0"),
194 /* SMAF (Synthetic music Mobile Application Format) */
195 [AFMT_SMAF] =
196 AFMT_ENTRY("SMAF", "smaf", NULL, get_smaf_metadata, "mmf\0"),
197 /* Sun Audio file */
198 [AFMT_AU] =
199 AFMT_ENTRY("AU", "au", NULL, get_au_metadata, "au\0snd\0"),
200 /* VOX (Dialogic telephony file formats) */
201 [AFMT_VOX] =
202 AFMT_ENTRY("VOX", "vox", NULL, get_vox_metadata, "vox\0"),
203 /* Wave64 */
204 [AFMT_WAVE64] =
205 AFMT_ENTRY("WAVE64","wav64",NULL, get_wave64_metadata,"w64\0"),
206 /* True Audio */
207 [AFMT_TTA] =
208 AFMT_ENTRY("TTA", "tta", NULL, get_tta_metadata, "tta\0"),
209 /* WMA Voice in ASF */
210 [AFMT_WMAVOICE] =
211 AFMT_ENTRY("WMAVoice","wmavoice",NULL, NULL, "wma\0wmv\0"),
212 #endif
215 #if CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING)
216 /* get REC_FORMAT_* corresponding AFMT_* */
217 const int rec_format_afmt[REC_NUM_FORMATS] =
219 /* give AFMT_UNKNOWN by default */
220 [0 ... REC_NUM_FORMATS-1] = AFMT_UNKNOWN,
221 /* add new entries below this line */
222 [REC_FORMAT_AIFF] = AFMT_AIFF,
223 [REC_FORMAT_MPA_L3] = AFMT_MPA_L3,
224 [REC_FORMAT_WAVPACK] = AFMT_WAVPACK,
225 [REC_FORMAT_PCM_WAV] = AFMT_PCM_WAV,
228 /* get AFMT_* corresponding REC_FORMAT_* */
229 const int afmt_rec_format[AFMT_NUM_CODECS] =
231 /* give -1 by default */
232 [0 ... AFMT_NUM_CODECS-1] = -1,
233 /* add new entries below this line */
234 [AFMT_AIFF] = REC_FORMAT_AIFF,
235 [AFMT_MPA_L3] = REC_FORMAT_MPA_L3,
236 [AFMT_WAVPACK] = REC_FORMAT_WAVPACK,
237 [AFMT_PCM_WAV] = REC_FORMAT_PCM_WAV,
239 #endif /* CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING) */
242 /* Simple file type probing by looking at the filename extension. */
243 unsigned int probe_file_format(const char *filename)
245 char *suffix;
246 unsigned int i;
248 suffix = strrchr(filename, '.');
250 if (suffix == NULL)
252 return AFMT_UNKNOWN;
255 /* skip '.' */
256 suffix++;
258 for (i = 1; i < AFMT_NUM_CODECS; i++)
260 /* search extension list for type */
261 const char *ext = audio_formats[i].ext_list;
265 if (strcasecmp(suffix, ext) == 0)
267 return i;
270 ext += strlen(ext) + 1;
272 while (*ext != '\0');
275 return AFMT_UNKNOWN;
278 /* Note, that this returns false for successful, true for error! */
279 bool mp3info(struct mp3entry *entry, const char *filename)
281 int fd;
282 bool result;
284 fd = open(filename, O_RDONLY);
285 if (fd < 0)
286 return true;
288 result = !get_mp3_metadata(fd, entry);
290 close(fd);
292 return result;
295 /* Get metadata for track - return false if parsing showed problems with the
296 * file that would prevent playback.
298 bool get_metadata(struct mp3entry* id3, int fd, const char* trackname)
300 const struct afmt_entry *entry;
301 int logfd = 0;
302 DEBUGF("Read metadata for %s\n", trackname);
303 if (write_metadata_log)
305 logfd = open("/metadata.log", O_WRONLY | O_APPEND | O_CREAT, 0666);
306 if (logfd >= 0)
308 write(logfd, trackname, strlen(trackname));
309 write(logfd, "\n", 1);
310 close(logfd);
314 /* Clear the mp3entry to avoid having bogus pointers appear */
315 memset(id3, 0, sizeof(struct mp3entry));
317 /* Take our best guess at the codec type based on file extension */
318 id3->codectype = probe_file_format(trackname);
320 entry = &audio_formats[id3->codectype];
322 /* Load codec specific track tag information and confirm the codec type. */
323 if (!entry->parse_func)
325 DEBUGF("nothing to parse for %s (format %s)", trackname, entry->label);
326 return false;
329 if (!entry->parse_func(fd, id3))
331 DEBUGF("parsing %s failed (format: %s)", trackname, entry->label);
332 return false;
335 lseek(fd, 0, SEEK_SET);
336 strlcpy(id3->path, trackname, sizeof(id3->path));
337 /* We have successfully read the metadata from the file */
338 return true;
341 #ifndef __PCTOOL__
342 #if CONFIG_CODEC == SWCODEC
343 void strip_tags(int handle_id)
345 static const unsigned char tag[] = "TAG";
346 static const unsigned char apetag[] = "APETAGEX";
347 size_t len, version;
348 void *tail;
350 if (bufgettail(handle_id, 128, &tail) != 128)
351 return;
353 if (memcmp(tail, tag, 3) == 0)
355 /* Skip id3v1 tag */
356 logf("Cutting off ID3v1 tag");
357 bufcuttail(handle_id, 128);
360 /* Get a new tail, as the old one may have been cut */
361 if (bufgettail(handle_id, 32, &tail) != 32)
362 return;
364 /* Check for APE tag (look for the APE tag footer) */
365 if (memcmp(tail, apetag, 8) != 0)
366 return;
368 /* Read the version and length from the footer */
369 version = get_long_le(&((unsigned char *)tail)[8]);
370 len = get_long_le(&((unsigned char *)tail)[12]);
371 if (version == 2000)
372 len += 32; /* APEv2 has a 32 byte header */
374 /* Skip APE tag */
375 logf("Cutting off APE tag (%ldB)", len);
376 bufcuttail(handle_id, len);
378 #endif /* CONFIG_CODEC == SWCODEC */
379 #endif /* ! __PCTOOL__ */
381 void adjust_mp3entry(struct mp3entry *entry, void *dest, const void *orig)
383 long offset;
384 if (orig > dest)
385 offset = - ((size_t)orig - (size_t)dest);
386 else
387 offset = (size_t)dest - (size_t)orig;
389 if (entry->title)
390 entry->title += offset;
391 if (entry->artist)
392 entry->artist += offset;
393 if (entry->album)
394 entry->album += offset;
395 if (entry->genre_string > (char*)orig
396 && entry->genre_string < (char*)orig + sizeof(struct mp3entry))
397 /* Don't adjust that if it points to an entry of the "genres" array */
398 entry->genre_string += offset;
399 if (entry->track_string)
400 entry->track_string += offset;
401 if (entry->disc_string)
402 entry->disc_string += offset;
403 if (entry->year_string)
404 entry->year_string += offset;
405 if (entry->composer)
406 entry->composer += offset;
407 if (entry->comment)
408 entry->comment += offset;
409 if (entry->albumartist)
410 entry->albumartist += offset;
411 if (entry->grouping)
412 entry->grouping += offset;
413 #if CONFIG_CODEC == SWCODEC
414 if (entry->track_gain_string)
415 entry->track_gain_string += offset;
416 if (entry->album_gain_string)
417 entry->album_gain_string += offset;
418 #endif
419 if (entry->mb_track_id)
420 entry->mb_track_id += offset;
423 void copy_mp3entry(struct mp3entry *dest, const struct mp3entry *orig)
425 memcpy(dest, orig, sizeof(struct mp3entry));
426 adjust_mp3entry(dest, dest, orig);