Meizu: fix mask for GPIO function selection used for fmradio
[kugel-rb.git] / apps / metadata.c
blobb3a4deb385b9c8f7c18b8f90c26a91252831de99
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 <string.h>
23 #include <stdlib.h>
24 #include <ctype.h>
25 #include <inttypes.h>
27 #include "playback.h"
28 #include "debug.h"
29 #include "logf.h"
30 #include "cuesheet.h"
31 #include "metadata.h"
33 #include "metadata/metadata_parsers.h"
35 #if CONFIG_CODEC == SWCODEC
37 /* For trailing tag stripping */
38 #include "buffering.h"
40 #include "metadata/metadata_common.h"
42 #endif /* CONFIG_CODEC == SWCODEC */
44 const struct afmt_entry audio_formats[AFMT_NUM_CODECS] =
46 /* Unknown file format */
47 [AFMT_UNKNOWN] =
48 AFMT_ENTRY("???", NULL, NULL, NULL ),
50 /* MPEG Audio layer 1 */
51 [AFMT_MPA_L1] =
52 AFMT_ENTRY("MP1", "mpa", NULL, "mp1\0" ),
53 /* MPEG Audio layer 2 */
54 [AFMT_MPA_L2] =
55 AFMT_ENTRY("MP2", "mpa", NULL, "mpa\0mp2\0" ),
56 /* MPEG Audio layer 3 */
57 [AFMT_MPA_L3] =
58 AFMT_ENTRY("MP3", "mpa", "mp3_enc", "mp3\0" ),
60 #if CONFIG_CODEC == SWCODEC
61 /* Audio Interchange File Format */
62 [AFMT_AIFF] =
63 AFMT_ENTRY("AIFF", "aiff", "aiff_enc", "aiff\0aif\0"),
64 /* Uncompressed PCM in a WAV file */
65 [AFMT_PCM_WAV] =
66 AFMT_ENTRY("WAV", "wav", "wav_enc", "wav\0" ),
67 /* Ogg Vorbis */
68 [AFMT_OGG_VORBIS] =
69 AFMT_ENTRY("Ogg", "vorbis", NULL, "ogg\0oga\0" ),
70 /* FLAC */
71 [AFMT_FLAC] =
72 AFMT_ENTRY("FLAC", "flac", NULL, "flac\0" ),
73 /* Musepack */
74 [AFMT_MPC] =
75 AFMT_ENTRY("MPC", "mpc", NULL, "mpc\0" ),
76 /* A/52 (aka AC3) audio */
77 [AFMT_A52] =
78 AFMT_ENTRY("AC3", "a52", NULL, "a52\0ac3\0" ),
79 /* WavPack */
80 [AFMT_WAVPACK] =
81 AFMT_ENTRY("WV", "wavpack", "wavpack_enc", "wv\0" ),
82 /* Apple Lossless Audio Codec */
83 [AFMT_MP4_ALAC] =
84 AFMT_ENTRY("ALAC", "alac", NULL, "m4a\0m4b\0" ),
85 /* Advanced Audio Coding in M4A container */
86 [AFMT_MP4_AAC] =
87 AFMT_ENTRY("AAC", "aac", NULL, "mp4\0" ),
88 /* Shorten */
89 [AFMT_SHN] =
90 AFMT_ENTRY("SHN", "shorten", NULL, "shn\0" ),
91 /* SID File Format */
92 [AFMT_SID] =
93 AFMT_ENTRY("SID", "sid", NULL, "sid\0" ),
94 /* ADX File Format */
95 [AFMT_ADX] =
96 AFMT_ENTRY("ADX", "adx", NULL, "adx\0" ),
97 /* NESM (NES Sound Format) */
98 [AFMT_NSF] =
99 AFMT_ENTRY("NSF", "nsf", NULL, "nsf\0nsfe\0" ),
100 /* Speex File Format */
101 [AFMT_SPEEX] =
102 AFMT_ENTRY("Speex","speex", NULL, "spx\0" ),
103 /* SPC700 Save State */
104 [AFMT_SPC] =
105 AFMT_ENTRY("SPC", "spc", NULL, "spc\0" ),
106 /* APE (Monkey's Audio) */
107 [AFMT_APE] =
108 AFMT_ENTRY("APE", "ape", NULL, "ape\0mac\0" ),
109 /* WMA (WMAV1/V2 in ASF) */
110 [AFMT_WMA] =
111 AFMT_ENTRY("WMA", "wma", NULL, "wma\0wmv\0asf\0" ),
112 /* Amiga MOD File */
113 [AFMT_MOD] =
114 AFMT_ENTRY("MOD", "mod", NULL, "mod\0" ),
115 /* Amiga SAP File */
116 [AFMT_SAP] =
117 AFMT_ENTRY("SAP", "asap", NULL, "sap\0" ),
118 /* Cook in RM/RA */
119 [AFMT_RM_COOK] =
120 AFMT_ENTRY("Cook", "cook", NULL, "rm\0ra\0rmvb\0" ),
121 /* AAC in RM/RA */
122 [AFMT_RM_AAC] =
123 AFMT_ENTRY("RAAC", "raac", NULL, "rm\0ra\0rmvb\0" ),
124 #endif
127 #if CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING)
128 /* get REC_FORMAT_* corresponding AFMT_* */
129 const int rec_format_afmt[REC_NUM_FORMATS] =
131 /* give AFMT_UNKNOWN by default */
132 [0 ... REC_NUM_FORMATS-1] = AFMT_UNKNOWN,
133 /* add new entries below this line */
134 [REC_FORMAT_AIFF] = AFMT_AIFF,
135 [REC_FORMAT_MPA_L3] = AFMT_MPA_L3,
136 [REC_FORMAT_WAVPACK] = AFMT_WAVPACK,
137 [REC_FORMAT_PCM_WAV] = AFMT_PCM_WAV,
140 /* get AFMT_* corresponding REC_FORMAT_* */
141 const int afmt_rec_format[AFMT_NUM_CODECS] =
143 /* give -1 by default */
144 [0 ... AFMT_NUM_CODECS-1] = -1,
145 /* add new entries below this line */
146 [AFMT_AIFF] = REC_FORMAT_AIFF,
147 [AFMT_MPA_L3] = REC_FORMAT_MPA_L3,
148 [AFMT_WAVPACK] = REC_FORMAT_WAVPACK,
149 [AFMT_PCM_WAV] = REC_FORMAT_PCM_WAV,
151 #endif /* CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING) */
154 /* Simple file type probing by looking at the filename extension. */
155 unsigned int probe_file_format(const char *filename)
157 char *suffix;
158 unsigned int i;
160 suffix = strrchr(filename, '.');
162 if (suffix == NULL)
164 return AFMT_UNKNOWN;
167 /* skip '.' */
168 suffix++;
170 for (i = 1; i < AFMT_NUM_CODECS; i++)
172 /* search extension list for type */
173 const char *ext = audio_formats[i].ext_list;
177 if (strcasecmp(suffix, ext) == 0)
179 return i;
182 ext += strlen(ext) + 1;
184 while (*ext != '\0');
187 return AFMT_UNKNOWN;
190 /* Note, that this returns false for successful, true for error! */
191 bool mp3info(struct mp3entry *entry, const char *filename)
193 int fd;
194 bool result;
196 fd = open(filename, O_RDONLY);
197 if (fd < 0)
198 return true;
200 result = !get_mp3_metadata(fd, entry, filename);
202 close(fd);
204 return result;
207 /* Get metadata for track - return false if parsing showed problems with the
208 * file that would prevent playback.
210 bool get_metadata(struct mp3entry* id3, int fd, const char* trackname)
212 #if CONFIG_CODEC == SWCODEC
213 unsigned char* buf;
214 #endif
216 /* Clear the mp3entry to avoid having bogus pointers appear */
217 memset(id3, 0, sizeof(struct mp3entry));
219 /* Take our best guess at the codec type based on file extension */
220 id3->codectype = probe_file_format(trackname);
222 /* Load codec specific track tag information and confirm the codec type. */
223 switch (id3->codectype)
225 case AFMT_MPA_L1:
226 case AFMT_MPA_L2:
227 case AFMT_MPA_L3:
228 if (!get_mp3_metadata(fd, id3, trackname))
230 return false;
233 break;
235 #if CONFIG_CODEC == SWCODEC
236 case AFMT_FLAC:
237 if (!get_flac_metadata(fd, id3))
239 return false;
242 break;
244 case AFMT_WMA:
245 if (!get_asf_metadata(fd, id3))
247 return false;
249 break;
251 case AFMT_APE:
252 if (!get_monkeys_metadata(fd, id3))
254 return false;
256 read_ape_tags(fd, id3);
257 break;
259 case AFMT_MPC:
260 if (!get_musepack_metadata(fd, id3))
261 return false;
262 read_ape_tags(fd, id3);
263 break;
265 case AFMT_OGG_VORBIS:
266 case AFMT_SPEEX:
267 if (!get_ogg_metadata(fd, id3))/*detects and handles Ogg/Speex files*/
269 return false;
272 break;
274 case AFMT_PCM_WAV:
275 if (!get_wave_metadata(fd, id3))
277 return false;
280 break;
282 case AFMT_WAVPACK:
283 if (!get_wavpack_metadata(fd, id3))
285 return false;
288 read_ape_tags(fd, id3); /* use any apetag info we find */
289 break;
291 case AFMT_A52:
292 if (!get_a52_metadata(fd, id3))
294 return false;
297 break;
299 case AFMT_MP4_ALAC:
300 case AFMT_MP4_AAC:
301 if (!get_mp4_metadata(fd, id3))
303 return false;
306 break;
308 case AFMT_MOD:
309 if (!get_mod_metadata(fd, id3))
311 return false;
314 break;
316 case AFMT_SHN:
317 id3->vbr = true;
318 id3->filesize = filesize(fd);
319 if (!skip_id3v2(fd, id3))
321 return false;
323 /* TODO: read the id3v2 header if it exists */
324 break;
326 case AFMT_SID:
327 if (!get_sid_metadata(fd, id3))
329 return false;
331 break;
333 case AFMT_SPC:
334 if (!get_spc_metadata(fd, id3))
336 DEBUGF("get_spc_metadata error\n");
337 return false;
339 id3->filesize = filesize(fd);
340 id3->genre_string = id3_get_num_genre(36);
341 break;
343 case AFMT_ADX:
344 if (!get_adx_metadata(fd, id3))
346 DEBUGF("get_adx_metadata error\n");
347 return false;
350 break;
352 case AFMT_NSF:
353 buf = (unsigned char *)id3->path;
354 if ((lseek(fd, 0, SEEK_SET) < 0) || ((read(fd, buf, 8)) < 8))
356 DEBUGF("lseek or read failed\n");
357 return false;
359 id3->vbr = false;
360 id3->filesize = filesize(fd);
361 if (memcmp(buf,"NESM",4) && memcmp(buf,"NSFE",4)) return false;
362 break;
364 case AFMT_AIFF:
365 if (!get_aiff_metadata(fd, id3))
367 return false;
370 break;
372 case AFMT_SAP:
373 if (!get_asap_metadata(fd, id3))
375 DEBUGF("get_sap_metadata error\n");
376 return false;
378 id3->filesize = filesize(fd);
379 id3->genre_string = id3_get_num_genre(36);
380 break;
382 case AFMT_RM_COOK:
383 if (!get_rm_metadata(fd, id3))
385 DEBUGF("get_rm_metadata error\n");
386 return false;
388 break;
390 #endif /* CONFIG_CODEC == SWCODEC */
392 default:
393 /* If we don't know how to read the metadata, assume we can't play
394 the file */
395 return false;
396 break;
399 /* We have successfully read the metadata from the file */
401 lseek(fd, 0, SEEK_SET);
402 strlcpy(id3->path, trackname, sizeof(id3->path));
404 return true;
407 #ifndef __PCTOOL__
408 #if CONFIG_CODEC == SWCODEC
409 void strip_tags(int handle_id)
411 static const unsigned char tag[] = "TAG";
412 static const unsigned char apetag[] = "APETAGEX";
413 size_t len, version;
414 void *tail;
416 if (bufgettail(handle_id, 128, &tail) != 128)
417 return;
419 if (memcmp(tail, tag, 3) == 0)
421 /* Skip id3v1 tag */
422 logf("Cutting off ID3v1 tag");
423 bufcuttail(handle_id, 128);
426 /* Get a new tail, as the old one may have been cut */
427 if (bufgettail(handle_id, 32, &tail) != 32)
428 return;
430 /* Check for APE tag (look for the APE tag footer) */
431 if (memcmp(tail, apetag, 8) != 0)
432 return;
434 /* Read the version and length from the footer */
435 version = get_long_le(&((unsigned char *)tail)[8]);
436 len = get_long_le(&((unsigned char *)tail)[12]);
437 if (version == 2000)
438 len += 32; /* APEv2 has a 32 byte header */
440 /* Skip APE tag */
441 logf("Cutting off APE tag (%ldB)", len);
442 bufcuttail(handle_id, len);
444 #endif /* CONFIG_CODEC == SWCODEC */
445 #endif /* ! __PCTOOL__ */
447 void adjust_mp3entry(struct mp3entry *entry, void *dest, const void *orig)
449 long offset;
450 if (orig > dest)
451 offset = - ((size_t)orig - (size_t)dest);
452 else
453 offset = (size_t)dest - (size_t)orig;
455 if (entry->title)
456 entry->title += offset;
457 if (entry->artist)
458 entry->artist += offset;
459 if (entry->album)
460 entry->album += offset;
461 if (entry->genre_string && !id3_is_genre_string(entry->genre_string))
462 /* Don't adjust that if it points to an entry of the "genres" array */
463 entry->genre_string += offset;
464 if (entry->track_string)
465 entry->track_string += offset;
466 if (entry->disc_string)
467 entry->disc_string += offset;
468 if (entry->year_string)
469 entry->year_string += offset;
470 if (entry->composer)
471 entry->composer += offset;
472 if (entry->comment)
473 entry->comment += offset;
474 if (entry->albumartist)
475 entry->albumartist += offset;
476 if (entry->grouping)
477 entry->grouping += offset;
478 #if CONFIG_CODEC == SWCODEC
479 if (entry->track_gain_string)
480 entry->track_gain_string += offset;
481 if (entry->album_gain_string)
482 entry->album_gain_string += offset;
483 #endif
484 if (entry->mb_track_id)
485 entry->mb_track_id += offset;
488 void copy_mp3entry(struct mp3entry *dest, const struct mp3entry *orig)
490 memcpy(dest, orig, sizeof(struct mp3entry));
491 adjust_mp3entry(dest, dest, orig);