Re-add the lseek to the beginning of the file which was accidentally removed.
[kugel-rb.git] / apps / metadata.c
blob866e035f4e0c8ed4434107c8b94b70098c7354e8
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 */
59 const struct afmt_entry audio_formats[AFMT_NUM_CODECS] =
61 /* Unknown file format */
62 [AFMT_UNKNOWN] =
63 AFMT_ENTRY("???", NULL, NULL, NULL, NULL ),
65 /* MPEG Audio layer 1 */
66 [AFMT_MPA_L1] =
67 AFMT_ENTRY("MP1", "mpa", NULL, get_mp3_metadata, "mp1\0"),
68 /* MPEG Audio layer 2 */
69 [AFMT_MPA_L2] =
70 AFMT_ENTRY("MP2", "mpa", NULL, get_mp3_metadata, "mpa\0mp2\0"),
71 /* MPEG Audio layer 3 */
72 [AFMT_MPA_L3] =
73 AFMT_ENTRY("MP3", "mpa", "mp3_enc", get_mp3_metadata, "mp3\0"),
75 #if CONFIG_CODEC == SWCODEC
76 /* Audio Interchange File Format */
77 [AFMT_AIFF] =
78 AFMT_ENTRY("AIFF", "aiff", "aiff_enc", get_aiff_metadata, "aiff\0aif\0"),
79 /* Uncompressed PCM in a WAV file OR ATRAC3 stream in WAV file (.at3) */
80 [AFMT_PCM_WAV] =
81 AFMT_ENTRY("WAV", "wav", "wav_enc", get_wave_metadata, "wav\0at3\0"),
82 /* Ogg Vorbis */
83 [AFMT_OGG_VORBIS] =
84 AFMT_ENTRY("Ogg", "vorbis", NULL, get_ogg_metadata, "ogg\0oga\0"),
85 /* FLAC */
86 [AFMT_FLAC] =
87 AFMT_ENTRY("FLAC", "flac", NULL, get_flac_metadata, "flac\0"),
88 /* Musepack */
89 [AFMT_MPC] =
90 AFMT_ENTRY("MPC", "mpc", NULL, get_musepack_metadata,"mpc\0"),
91 /* A/52 (aka AC3) audio */
92 [AFMT_A52] =
93 AFMT_ENTRY("AC3", "a52", NULL, get_a52_metadata, "a52\0ac3\0"),
94 /* WavPack */
95 [AFMT_WAVPACK] =
96 AFMT_ENTRY("WV","wavpack","wavpack_enc",get_wavpack_metadata,"wv\0"),
97 /* Apple Lossless Audio Codec */
98 [AFMT_MP4_ALAC] =
99 AFMT_ENTRY("ALAC", "alac", NULL, get_mp4_metadata, "m4a\0m4b\0"),
100 /* Advanced Audio Coding in M4A container */
101 [AFMT_MP4_AAC] =
102 AFMT_ENTRY("AAC", "aac", NULL, get_mp4_metadata, "mp4\0"),
103 /* Shorten */
104 [AFMT_SHN] =
105 AFMT_ENTRY("SHN","shorten", NULL, get_shn_metadata, "shn\0"),
106 /* SID File Format */
107 [AFMT_SID] =
108 AFMT_ENTRY("SID", "sid", NULL, get_sid_metadata, "sid\0"),
109 /* ADX File Format */
110 [AFMT_ADX] =
111 AFMT_ENTRY("ADX", "adx", NULL, get_adx_metadata, "adx\0"),
112 /* NESM (NES Sound Format) */
113 [AFMT_NSF] =
114 AFMT_ENTRY("NSF", "nsf", NULL, get_nsf_metadata, "nsf\0nsfe\0"),
115 /* Speex File Format */
116 [AFMT_SPEEX] =
117 AFMT_ENTRY("Speex", "speex",NULL, get_ogg_metadata, "spx\0"),
118 /* SPC700 Save State */
119 [AFMT_SPC] =
120 AFMT_ENTRY("SPC", "spc", NULL, get_spc_metadata, "spc\0"),
121 /* APE (Monkey's Audio) */
122 [AFMT_APE] =
123 AFMT_ENTRY("APE", "ape", NULL, get_monkeys_metadata,"ape\0mac\0"),
124 /* WMA (WMAV1/V2 in ASF) */
125 [AFMT_WMA] =
126 AFMT_ENTRY("WMA", "wma", NULL, get_asf_metadata,"wma\0wmv\0asf\0"),
127 /* WMA Professional in ASF */
128 [AFMT_WMAPRO] =
129 AFMT_ENTRY("WMAPro","wmapro",NULL, NULL, "wma\0wmv\0asf\0"),
130 /* Amiga MOD File */
131 [AFMT_MOD] =
132 AFMT_ENTRY("MOD", "mod", NULL, get_mod_metadata, "mod\0"),
133 /* Atari SAP File */
134 [AFMT_SAP] =
135 AFMT_ENTRY("SAP", "asap", NULL, get_asap_metadata, "sap\0"),
136 /* Cook in RM/RA */
137 [AFMT_RM_COOK] =
138 AFMT_ENTRY("Cook", "cook", NULL, get_rm_metadata,"rm\0ra\0rmvb\0"),
139 /* AAC in RM/RA */
140 [AFMT_RM_AAC] =
141 AFMT_ENTRY("RAAC", "raac", NULL, NULL, "rm\0ra\0rmvb\0"),
142 /* AC3 in RM/RA */
143 [AFMT_RM_AC3] =
144 AFMT_ENTRY("AC3", "a52_rm", NULL, NULL, "rm\0ra\0rmvb\0"),
145 /* ATRAC3 in RM/RA */
146 [AFMT_RM_ATRAC3] =
147 AFMT_ENTRY("ATRAC3","atrac3_rm",NULL, NULL, "rm\0ra\0rmvb\0"),
148 /* Atari CMC File */
149 [AFMT_CMC] =
150 AFMT_ENTRY("CMC", "asap", NULL, get_other_asap_metadata,"cmc\0"),
151 /* Atari CM3 File */
152 [AFMT_CM3] =
153 AFMT_ENTRY("CM3", "asap", NULL, get_other_asap_metadata,"cm3\0"),
154 /* Atari CMR File */
155 [AFMT_CMR] =
156 AFMT_ENTRY("CMR", "asap", NULL, get_other_asap_metadata,"cmr\0"),
157 /* Atari CMS File */
158 [AFMT_CMS] =
159 AFMT_ENTRY("CMS", "asap", NULL, get_other_asap_metadata,"cms\0"),
160 /* Atari DMC File */
161 [AFMT_DMC] =
162 AFMT_ENTRY("DMC", "asap", NULL, get_other_asap_metadata,"dmc\0"),
163 /* Atari DLT File */
164 [AFMT_DLT] =
165 AFMT_ENTRY("DLT", "asap", NULL, get_other_asap_metadata,"dlt\0"),
166 /* Atari MPT File */
167 [AFMT_MPT] =
168 AFMT_ENTRY("MPT", "asap", NULL, get_other_asap_metadata,"mpt\0"),
169 /* Atari MPD File */
170 [AFMT_MPD] =
171 AFMT_ENTRY("MPD", "asap", NULL, get_other_asap_metadata,"mpd\0"),
172 /* Atari RMT File */
173 [AFMT_RMT] =
174 AFMT_ENTRY("RMT", "asap", NULL, get_other_asap_metadata,"rmt\0"),
175 /* Atari TMC File */
176 [AFMT_TMC] =
177 AFMT_ENTRY("TMC", "asap", NULL, get_other_asap_metadata,"tmc\0"),
178 /* Atari TM8 File */
179 [AFMT_TM8] =
180 AFMT_ENTRY("TM8", "asap", NULL, get_other_asap_metadata,"tm8\0"),
181 /* Atari TM2 File */
182 [AFMT_TM2] =
183 AFMT_ENTRY("TM2", "asap", NULL, get_other_asap_metadata,"tm2\0"),
184 /* Atrac3 in Sony OMA Container */
185 [AFMT_OMA_ATRAC3] =
186 AFMT_ENTRY("ATRAC3","atrac3_oma",NULL, get_oma_metadata, "oma\0aa3\0"),
187 /* SMAF (Synthetic music Mobile Application Format) */
188 [AFMT_SMAF] =
189 AFMT_ENTRY("SMAF", "smaf", NULL, get_smaf_metadata, "mmf\0"),
190 /* Sun Audio file */
191 [AFMT_AU] =
192 AFMT_ENTRY("AU", "au", NULL, get_au_metadata, "au\0snd\0"),
193 /* VOX (Dialogic telephony file formats) */
194 [AFMT_VOX] =
195 AFMT_ENTRY("VOX", "vox", NULL, get_vox_metadata, "vox\0"),
196 /* Wave64 */
197 [AFMT_WAVE64] =
198 AFMT_ENTRY("WAVE64","wav64",NULL, get_wave64_metadata,"w64\0"),
199 /* True Audio */
200 [AFMT_TTA] =
201 AFMT_ENTRY("TTA", "tta", NULL, get_tta_metadata, "tta\0"),
202 /* WMA Voice in ASF */
203 [AFMT_WMAVOICE] =
204 AFMT_ENTRY("WMAVoice","wmavoice",NULL, NULL, "wma\0wmv\0"),
205 #endif
208 #if CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING)
209 /* get REC_FORMAT_* corresponding AFMT_* */
210 const int rec_format_afmt[REC_NUM_FORMATS] =
212 /* give AFMT_UNKNOWN by default */
213 [0 ... REC_NUM_FORMATS-1] = AFMT_UNKNOWN,
214 /* add new entries below this line */
215 [REC_FORMAT_AIFF] = AFMT_AIFF,
216 [REC_FORMAT_MPA_L3] = AFMT_MPA_L3,
217 [REC_FORMAT_WAVPACK] = AFMT_WAVPACK,
218 [REC_FORMAT_PCM_WAV] = AFMT_PCM_WAV,
221 /* get AFMT_* corresponding REC_FORMAT_* */
222 const int afmt_rec_format[AFMT_NUM_CODECS] =
224 /* give -1 by default */
225 [0 ... AFMT_NUM_CODECS-1] = -1,
226 /* add new entries below this line */
227 [AFMT_AIFF] = REC_FORMAT_AIFF,
228 [AFMT_MPA_L3] = REC_FORMAT_MPA_L3,
229 [AFMT_WAVPACK] = REC_FORMAT_WAVPACK,
230 [AFMT_PCM_WAV] = REC_FORMAT_PCM_WAV,
232 #endif /* CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING) */
235 /* Simple file type probing by looking at the filename extension. */
236 unsigned int probe_file_format(const char *filename)
238 char *suffix;
239 unsigned int i;
241 suffix = strrchr(filename, '.');
243 if (suffix == NULL)
245 return AFMT_UNKNOWN;
248 /* skip '.' */
249 suffix++;
251 for (i = 1; i < AFMT_NUM_CODECS; i++)
253 /* search extension list for type */
254 const char *ext = audio_formats[i].ext_list;
258 if (strcasecmp(suffix, ext) == 0)
260 return i;
263 ext += strlen(ext) + 1;
265 while (*ext != '\0');
268 return AFMT_UNKNOWN;
271 /* Note, that this returns false for successful, true for error! */
272 bool mp3info(struct mp3entry *entry, const char *filename)
274 int fd;
275 bool result;
277 fd = open(filename, O_RDONLY);
278 if (fd < 0)
279 return true;
281 result = !get_mp3_metadata(fd, entry);
283 close(fd);
285 return result;
288 /* Get metadata for track - return false if parsing showed problems with the
289 * file that would prevent playback.
291 bool get_metadata(struct mp3entry* id3, int fd, const char* trackname)
293 const struct afmt_entry *entry;
294 /* Clear the mp3entry to avoid having bogus pointers appear */
295 memset(id3, 0, sizeof(struct mp3entry));
297 /* Take our best guess at the codec type based on file extension */
298 id3->codectype = probe_file_format(trackname);
300 entry = &audio_formats[id3->codectype];
302 /* Load codec specific track tag information and confirm the codec type. */
303 if (!entry->parse_func)
305 DEBUGF("nothing to parse for %s (format %s)", trackname, entry->label);
306 return false;
309 if (!entry->parse_func(fd, id3))
311 DEBUGF("parsing %s failed (format: %s)", trackname, entry->label);
312 return false;
315 lseek(fd, 0, SEEK_SET);
316 strlcpy(id3->path, trackname, sizeof(id3->path));
317 /* We have successfully read the metadata from the file */
318 return true;
321 #ifndef __PCTOOL__
322 #if CONFIG_CODEC == SWCODEC
323 void strip_tags(int handle_id)
325 static const unsigned char tag[] = "TAG";
326 static const unsigned char apetag[] = "APETAGEX";
327 size_t len, version;
328 void *tail;
330 if (bufgettail(handle_id, 128, &tail) != 128)
331 return;
333 if (memcmp(tail, tag, 3) == 0)
335 /* Skip id3v1 tag */
336 logf("Cutting off ID3v1 tag");
337 bufcuttail(handle_id, 128);
340 /* Get a new tail, as the old one may have been cut */
341 if (bufgettail(handle_id, 32, &tail) != 32)
342 return;
344 /* Check for APE tag (look for the APE tag footer) */
345 if (memcmp(tail, apetag, 8) != 0)
346 return;
348 /* Read the version and length from the footer */
349 version = get_long_le(&((unsigned char *)tail)[8]);
350 len = get_long_le(&((unsigned char *)tail)[12]);
351 if (version == 2000)
352 len += 32; /* APEv2 has a 32 byte header */
354 /* Skip APE tag */
355 logf("Cutting off APE tag (%ldB)", len);
356 bufcuttail(handle_id, len);
358 #endif /* CONFIG_CODEC == SWCODEC */
359 #endif /* ! __PCTOOL__ */
361 void adjust_mp3entry(struct mp3entry *entry, void *dest, const void *orig)
363 long offset;
364 if (orig > dest)
365 offset = - ((size_t)orig - (size_t)dest);
366 else
367 offset = (size_t)dest - (size_t)orig;
369 if (entry->title)
370 entry->title += offset;
371 if (entry->artist)
372 entry->artist += offset;
373 if (entry->album)
374 entry->album += offset;
375 if (entry->genre_string > (char*)orig
376 && entry->genre_string < (char*)orig + sizeof(struct mp3entry))
377 /* Don't adjust that if it points to an entry of the "genres" array */
378 entry->genre_string += offset;
379 if (entry->track_string)
380 entry->track_string += offset;
381 if (entry->disc_string)
382 entry->disc_string += offset;
383 if (entry->year_string)
384 entry->year_string += offset;
385 if (entry->composer)
386 entry->composer += offset;
387 if (entry->comment)
388 entry->comment += offset;
389 if (entry->albumartist)
390 entry->albumartist += offset;
391 if (entry->grouping)
392 entry->grouping += offset;
393 #if CONFIG_CODEC == SWCODEC
394 if (entry->track_gain_string)
395 entry->track_gain_string += offset;
396 if (entry->album_gain_string)
397 entry->album_gain_string += offset;
398 #endif
399 if (entry->mb_track_id)
400 entry->mb_track_id += offset;
403 void copy_mp3entry(struct mp3entry *dest, const struct mp3entry *orig)
405 memcpy(dest, orig, sizeof(struct mp3entry));
406 adjust_mp3entry(dest, dest, orig);