Recording screen: show a more compact view if 6 lines do not fit but 4 do. Should...
[kugel-rb.git] / apps / metadata.c
blob17c89f17c1d42ccb836c73b2b3e73ab34f5e26db
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 "system.h"
28 #include "playback.h"
29 #include "debug.h"
30 #include "logf.h"
31 #include "cuesheet.h"
32 #include "metadata.h"
34 #if CONFIG_CODEC == SWCODEC
36 /* For trailing tag stripping */
37 #include "buffering.h"
39 #include "metadata/metadata_common.h"
40 #include "metadata/metadata_parsers.h"
42 #endif /* CONFIG_CODEC == SWCODEC */
45 /* Simple file type probing by looking at the filename extension. */
46 unsigned int probe_file_format(const char *filename)
48 char *suffix;
49 unsigned int i;
51 suffix = strrchr(filename, '.');
53 if (suffix == NULL)
55 return AFMT_UNKNOWN;
58 /* skip '.' */
59 suffix++;
61 for (i = 1; i < AFMT_NUM_CODECS; i++)
63 /* search extension list for type */
64 const char *ext = audio_formats[i].ext_list;
68 if (strcasecmp(suffix, ext) == 0)
70 return i;
73 ext += strlen(ext) + 1;
75 while (*ext != '\0');
78 return AFMT_UNKNOWN;
81 /* Get metadata for track - return false if parsing showed problems with the
82 * file that would prevent playback.
84 bool get_metadata(struct mp3entry* id3, int fd, const char* trackname)
86 #if CONFIG_CODEC == SWCODEC
87 unsigned char* buf;
88 #endif
90 /* Clear the mp3entry to avoid having bogus pointers appear */
91 memset(id3, 0, sizeof(struct mp3entry));
93 /* Take our best guess at the codec type based on file extension */
94 id3->codectype = probe_file_format(trackname);
96 /* Load codec specific track tag information and confirm the codec type. */
97 switch (id3->codectype)
99 case AFMT_MPA_L1:
100 case AFMT_MPA_L2:
101 case AFMT_MPA_L3:
102 if (!get_mp3_metadata(fd, id3, trackname))
104 return false;
107 break;
109 #if CONFIG_CODEC == SWCODEC
110 case AFMT_FLAC:
111 if (!get_flac_metadata(fd, id3))
113 return false;
116 break;
118 case AFMT_WMA:
119 if (!get_asf_metadata(fd, id3))
121 return false;
123 break;
125 case AFMT_APE:
126 if (!get_monkeys_metadata(fd, id3))
128 return false;
130 read_ape_tags(fd, id3);
131 break;
133 case AFMT_MPC:
134 if (!get_musepack_metadata(fd, id3))
135 return false;
136 read_ape_tags(fd, id3);
137 break;
139 case AFMT_OGG_VORBIS:
140 case AFMT_SPEEX:
141 if (!get_ogg_metadata(fd, id3))/*detects and handles Ogg/Speex files*/
143 return false;
146 break;
148 case AFMT_PCM_WAV:
149 if (!get_wave_metadata(fd, id3))
151 return false;
154 break;
156 case AFMT_WAVPACK:
157 if (!get_wavpack_metadata(fd, id3))
159 return false;
162 read_ape_tags(fd, id3); /* use any apetag info we find */
163 break;
165 case AFMT_A52:
166 if (!get_a52_metadata(fd, id3))
168 return false;
171 break;
173 case AFMT_ALAC:
174 case AFMT_AAC:
175 if (!get_mp4_metadata(fd, id3))
177 return false;
180 break;
182 case AFMT_MOD:
183 if (!get_mod_metadata(fd, id3))
185 return false;
188 break;
190 case AFMT_SHN:
191 id3->vbr = true;
192 id3->filesize = filesize(fd);
193 if (!skip_id3v2(fd, id3))
195 return false;
197 /* TODO: read the id3v2 header if it exists */
198 break;
200 case AFMT_SID:
201 if (!get_sid_metadata(fd, id3))
203 return false;
205 break;
207 case AFMT_SPC:
208 if (!get_spc_metadata(fd, id3))
210 DEBUGF("get_spc_metadata error\n");
211 return false;
213 id3->filesize = filesize(fd);
214 id3->genre_string = id3_get_num_genre(36);
215 break;
217 case AFMT_ADX:
218 if (!get_adx_metadata(fd, id3))
220 DEBUGF("get_adx_metadata error\n");
221 return false;
224 break;
226 case AFMT_NSF:
227 buf = (unsigned char *)id3->path;
228 if ((lseek(fd, 0, SEEK_SET) < 0) || ((read(fd, buf, 8)) < 8))
230 DEBUGF("lseek or read failed\n");
231 return false;
233 id3->vbr = false;
234 id3->filesize = filesize(fd);
235 if (memcmp(buf,"NESM",4) && memcmp(buf,"NSFE",4)) return false;
236 break;
238 case AFMT_AIFF:
239 if (!get_aiff_metadata(fd, id3))
241 return false;
244 break;
246 case AFMT_SAP:
247 if (!get_asap_metadata(fd, id3))
249 DEBUGF("get_sap_metadata error\n");
250 return false;
252 id3->filesize = filesize(fd);
253 id3->genre_string = id3_get_num_genre(36);
254 break;
256 #endif /* CONFIG_CODEC == SWCODEC */
258 default:
259 /* If we don't know how to read the metadata, assume we can't play
260 the file */
261 return false;
262 break;
265 /* We have successfully read the metadata from the file */
267 #ifndef __PCTOOL__
268 if (cuesheet_is_enabled() && look_for_cuesheet_file(trackname, NULL))
270 id3->cuesheet_type = 1;
272 #endif
274 lseek(fd, 0, SEEK_SET);
275 strncpy(id3->path, trackname, sizeof(id3->path));
277 return true;
280 #if CONFIG_CODEC == SWCODEC
281 void strip_tags(int handle_id)
283 static const unsigned char tag[] = "TAG";
284 static const unsigned char apetag[] = "APETAGEX";
285 size_t len, version;
286 void *tail;
288 if (bufgettail(handle_id, 128, &tail) != 128)
289 return;
291 if (memcmp(tail, tag, 3) == 0)
293 /* Skip id3v1 tag */
294 logf("Cutting off ID3v1 tag");
295 bufcuttail(handle_id, 128);
298 /* Get a new tail, as the old one may have been cut */
299 if (bufgettail(handle_id, 32, &tail) != 32)
300 return;
302 /* Check for APE tag (look for the APE tag footer) */
303 if (memcmp(tail, apetag, 8) != 0)
304 return;
306 /* Read the version and length from the footer */
307 version = get_long_le(&((unsigned char *)tail)[8]);
308 len = get_long_le(&((unsigned char *)tail)[12]);
309 if (version == 2000)
310 len += 32; /* APEv2 has a 32 byte header */
312 /* Skip APE tag */
313 logf("Cutting off APE tag (%ldB)", len);
314 bufcuttail(handle_id, len);
316 #endif /* CONFIG_CODEC == SWCODEC */