diff options
Diffstat (limited to 'lib/rbcodec/metadata/metadata.c')
-rw-r--r-- | lib/rbcodec/metadata/metadata.c | 641 |
1 files changed, 641 insertions, 0 deletions
diff --git a/lib/rbcodec/metadata/metadata.c b/lib/rbcodec/metadata/metadata.c new file mode 100644 index 0000000000..b91e00cc4e --- /dev/null +++ b/lib/rbcodec/metadata/metadata.c | |||
@@ -0,0 +1,641 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2005 Dave Chapman | ||
11 | * | ||
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. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | #include <stdio.h> | ||
22 | #include <stdlib.h> | ||
23 | #include <ctype.h> | ||
24 | #include "string-extra.h" | ||
25 | |||
26 | #include "debug.h" | ||
27 | #include "logf.h" | ||
28 | #include "settings.h" | ||
29 | #include "cuesheet.h" | ||
30 | #include "metadata.h" | ||
31 | |||
32 | #include "metadata_parsers.h" | ||
33 | |||
34 | #if CONFIG_CODEC == SWCODEC | ||
35 | |||
36 | /* For trailing tag stripping and base audio data types */ | ||
37 | #include "buffering.h" | ||
38 | |||
39 | #include "metadata/metadata_common.h" | ||
40 | |||
41 | static bool get_shn_metadata(int fd, struct mp3entry *id3) | ||
42 | { | ||
43 | /* TODO: read the id3v2 header if it exists */ | ||
44 | id3->vbr = true; | ||
45 | id3->filesize = filesize(fd); | ||
46 | return skip_id3v2(fd, id3); | ||
47 | } | ||
48 | |||
49 | static bool get_other_asap_metadata(int fd, struct mp3entry *id3) | ||
50 | { | ||
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; | ||
57 | } | ||
58 | #endif /* CONFIG_CODEC == SWCODEC */ | ||
59 | bool write_metadata_log = false; | ||
60 | |||
61 | const struct afmt_entry audio_formats[AFMT_NUM_CODECS] = | ||
62 | { | ||
63 | /* Unknown file format */ | ||
64 | [0 ... AFMT_NUM_CODECS-1] = | ||
65 | AFMT_ENTRY("???", NULL, NULL, NULL, "\0" ), | ||
66 | |||
67 | /* MPEG Audio layer 2 */ | ||
68 | [AFMT_MPA_L2] = | ||
69 | AFMT_ENTRY("MP2", "mpa", NULL, get_mp3_metadata, "mpa\0mp2\0"), | ||
70 | |||
71 | #if CONFIG_CODEC != SWCODEC | ||
72 | /* MPEG Audio layer 3 on HWCODEC: .talk clips, no encoder */ | ||
73 | [AFMT_MPA_L3] = | ||
74 | AFMT_ENTRY("MP3", "mpa", NULL, get_mp3_metadata, "mp3\0talk\0"), | ||
75 | |||
76 | #else /* CONFIG_CODEC == SWCODEC */ | ||
77 | /* MPEG Audio layer 3 on SWCODEC */ | ||
78 | [AFMT_MPA_L3] = | ||
79 | AFMT_ENTRY("MP3", "mpa", "mp3_enc", get_mp3_metadata, "mp3\0"), | ||
80 | |||
81 | /* MPEG Audio layer 1 */ | ||
82 | [AFMT_MPA_L1] = | ||
83 | AFMT_ENTRY("MP1", "mpa", NULL, get_mp3_metadata, "mp1\0"), | ||
84 | /* Audio Interchange File Format */ | ||
85 | [AFMT_AIFF] = | ||
86 | AFMT_ENTRY("AIFF", "aiff", "aiff_enc", get_aiff_metadata, "aiff\0aif\0"), | ||
87 | /* Uncompressed PCM in a WAV file OR ATRAC3 stream in WAV file (.at3) */ | ||
88 | [AFMT_PCM_WAV] = | ||
89 | AFMT_ENTRY("WAV", "wav", "wav_enc", get_wave_metadata, "wav\0at3\0"), | ||
90 | /* Ogg Vorbis */ | ||
91 | [AFMT_OGG_VORBIS] = | ||
92 | AFMT_ENTRY("Ogg", "vorbis", NULL, get_ogg_metadata, "ogg\0oga\0"), | ||
93 | /* FLAC */ | ||
94 | [AFMT_FLAC] = | ||
95 | AFMT_ENTRY("FLAC", "flac", NULL, get_flac_metadata, "flac\0"), | ||
96 | /* Musepack SV7 */ | ||
97 | [AFMT_MPC_SV7] = | ||
98 | AFMT_ENTRY("MPCv7", "mpc", NULL, get_musepack_metadata,"mpc\0"), | ||
99 | /* A/52 (aka AC3) audio */ | ||
100 | [AFMT_A52] = | ||
101 | AFMT_ENTRY("AC3", "a52", NULL, get_a52_metadata, "a52\0ac3\0"), | ||
102 | /* WavPack */ | ||
103 | [AFMT_WAVPACK] = | ||
104 | AFMT_ENTRY("WV","wavpack","wavpack_enc",get_wavpack_metadata,"wv\0"), | ||
105 | /* Apple Lossless Audio Codec */ | ||
106 | [AFMT_MP4_ALAC] = | ||
107 | AFMT_ENTRY("ALAC", "alac", NULL, get_mp4_metadata, "m4a\0m4b\0"), | ||
108 | /* Advanced Audio Coding in M4A container */ | ||
109 | [AFMT_MP4_AAC] = | ||
110 | AFMT_ENTRY("AAC", "aac", NULL, get_mp4_metadata, "mp4\0"), | ||
111 | /* Shorten */ | ||
112 | [AFMT_SHN] = | ||
113 | AFMT_ENTRY("SHN","shorten", NULL, get_shn_metadata, "shn\0"), | ||
114 | /* SID File Format */ | ||
115 | [AFMT_SID] = | ||
116 | AFMT_ENTRY("SID", "sid", NULL, get_sid_metadata, "sid\0"), | ||
117 | /* ADX File Format */ | ||
118 | [AFMT_ADX] = | ||
119 | AFMT_ENTRY("ADX", "adx", NULL, get_adx_metadata, "adx\0"), | ||
120 | /* NESM (NES Sound Format) */ | ||
121 | [AFMT_NSF] = | ||
122 | AFMT_ENTRY("NSF", "nsf", NULL, get_nsf_metadata, "nsf\0nsfe\0"), | ||
123 | /* Speex File Format */ | ||
124 | [AFMT_SPEEX] = | ||
125 | AFMT_ENTRY("Speex", "speex",NULL, get_ogg_metadata, "spx\0"), | ||
126 | /* SPC700 Save State */ | ||
127 | [AFMT_SPC] = | ||
128 | AFMT_ENTRY("SPC", "spc", NULL, get_spc_metadata, "spc\0"), | ||
129 | /* APE (Monkey's Audio) */ | ||
130 | [AFMT_APE] = | ||
131 | AFMT_ENTRY("APE", "ape", NULL, get_monkeys_metadata,"ape\0mac\0"), | ||
132 | /* WMA (WMAV1/V2 in ASF) */ | ||
133 | [AFMT_WMA] = | ||
134 | AFMT_ENTRY("WMA", "wma", NULL, get_asf_metadata,"wma\0wmv\0asf\0"), | ||
135 | /* WMA Professional in ASF */ | ||
136 | [AFMT_WMAPRO] = | ||
137 | AFMT_ENTRY("WMAPro","wmapro",NULL, NULL, "wma\0wmv\0asf\0"), | ||
138 | /* Amiga MOD File */ | ||
139 | [AFMT_MOD] = | ||
140 | AFMT_ENTRY("MOD", "mod", NULL, get_mod_metadata, "mod\0"), | ||
141 | /* Atari SAP File */ | ||
142 | [AFMT_SAP] = | ||
143 | AFMT_ENTRY("SAP", "asap", NULL, get_asap_metadata, "sap\0"), | ||
144 | /* Cook in RM/RA */ | ||
145 | [AFMT_RM_COOK] = | ||
146 | AFMT_ENTRY("Cook", "cook", NULL, get_rm_metadata,"rm\0ra\0rmvb\0"), | ||
147 | /* AAC in RM/RA */ | ||
148 | [AFMT_RM_AAC] = | ||
149 | AFMT_ENTRY("RAAC", "raac", NULL, NULL, "rm\0ra\0rmvb\0"), | ||
150 | /* AC3 in RM/RA */ | ||
151 | [AFMT_RM_AC3] = | ||
152 | AFMT_ENTRY("AC3", "a52_rm", NULL, NULL, "rm\0ra\0rmvb\0"), | ||
153 | /* ATRAC3 in RM/RA */ | ||
154 | [AFMT_RM_ATRAC3] = | ||
155 | AFMT_ENTRY("ATRAC3","atrac3_rm",NULL, NULL, "rm\0ra\0rmvb\0"), | ||
156 | /* Atari CMC File */ | ||
157 | [AFMT_CMC] = | ||
158 | AFMT_ENTRY("CMC", "asap", NULL, get_other_asap_metadata,"cmc\0"), | ||
159 | /* Atari CM3 File */ | ||
160 | [AFMT_CM3] = | ||
161 | AFMT_ENTRY("CM3", "asap", NULL, get_other_asap_metadata,"cm3\0"), | ||
162 | /* Atari CMR File */ | ||
163 | [AFMT_CMR] = | ||
164 | AFMT_ENTRY("CMR", "asap", NULL, get_other_asap_metadata,"cmr\0"), | ||
165 | /* Atari CMS File */ | ||
166 | [AFMT_CMS] = | ||
167 | AFMT_ENTRY("CMS", "asap", NULL, get_other_asap_metadata,"cms\0"), | ||
168 | /* Atari DMC File */ | ||
169 | [AFMT_DMC] = | ||
170 | AFMT_ENTRY("DMC", "asap", NULL, get_other_asap_metadata,"dmc\0"), | ||
171 | /* Atari DLT File */ | ||
172 | [AFMT_DLT] = | ||
173 | AFMT_ENTRY("DLT", "asap", NULL, get_other_asap_metadata,"dlt\0"), | ||
174 | /* Atari MPT File */ | ||
175 | [AFMT_MPT] = | ||
176 | AFMT_ENTRY("MPT", "asap", NULL, get_other_asap_metadata,"mpt\0"), | ||
177 | /* Atari MPD File */ | ||
178 | [AFMT_MPD] = | ||
179 | AFMT_ENTRY("MPD", "asap", NULL, get_other_asap_metadata,"mpd\0"), | ||
180 | /* Atari RMT File */ | ||
181 | [AFMT_RMT] = | ||
182 | AFMT_ENTRY("RMT", "asap", NULL, get_other_asap_metadata,"rmt\0"), | ||
183 | /* Atari TMC File */ | ||
184 | [AFMT_TMC] = | ||
185 | AFMT_ENTRY("TMC", "asap", NULL, get_other_asap_metadata,"tmc\0"), | ||
186 | /* Atari TM8 File */ | ||
187 | [AFMT_TM8] = | ||
188 | AFMT_ENTRY("TM8", "asap", NULL, get_other_asap_metadata,"tm8\0"), | ||
189 | /* Atari TM2 File */ | ||
190 | [AFMT_TM2] = | ||
191 | AFMT_ENTRY("TM2", "asap", NULL, get_other_asap_metadata,"tm2\0"), | ||
192 | /* Atrac3 in Sony OMA Container */ | ||
193 | [AFMT_OMA_ATRAC3] = | ||
194 | AFMT_ENTRY("ATRAC3","atrac3_oma",NULL, get_oma_metadata, "oma\0aa3\0"), | ||
195 | /* SMAF (Synthetic music Mobile Application Format) */ | ||
196 | [AFMT_SMAF] = | ||
197 | AFMT_ENTRY("SMAF", "smaf", NULL, get_smaf_metadata, "mmf\0"), | ||
198 | /* Sun Audio file */ | ||
199 | [AFMT_AU] = | ||
200 | AFMT_ENTRY("AU", "au", NULL, get_au_metadata, "au\0snd\0"), | ||
201 | /* VOX (Dialogic telephony file formats) */ | ||
202 | [AFMT_VOX] = | ||
203 | AFMT_ENTRY("VOX", "vox", NULL, get_vox_metadata, "vox\0"), | ||
204 | /* Wave64 */ | ||
205 | [AFMT_WAVE64] = | ||
206 | AFMT_ENTRY("WAVE64","wav64",NULL, get_wave64_metadata,"w64\0"), | ||
207 | /* True Audio */ | ||
208 | [AFMT_TTA] = | ||
209 | AFMT_ENTRY("TTA", "tta", NULL, get_tta_metadata, "tta\0"), | ||
210 | /* WMA Voice in ASF */ | ||
211 | [AFMT_WMAVOICE] = | ||
212 | AFMT_ENTRY("WMAVoice","wmavoice",NULL, NULL, "wma\0wmv\0"), | ||
213 | /* Musepack SV8 */ | ||
214 | [AFMT_MPC_SV8] = | ||
215 | AFMT_ENTRY("MPCv8", "mpc", NULL, get_musepack_metadata,"mpc\0"), | ||
216 | /* Advanced Audio Coding High Efficiency in M4A container */ | ||
217 | [AFMT_MP4_AAC_HE] = | ||
218 | AFMT_ENTRY("AAC-HE","aac", NULL, get_mp4_metadata, "mp4\0"), | ||
219 | /* AY (ZX Spectrum, Amstrad CPC Sound Format) */ | ||
220 | [AFMT_AY] = | ||
221 | AFMT_ENTRY("AY", "ay", NULL, get_ay_metadata, "ay\0"), | ||
222 | /* GBS (Game Boy Sound Format) */ | ||
223 | [AFMT_GBS] = | ||
224 | AFMT_ENTRY("GBS", "gbs", NULL, get_gbs_metadata, "gbs\0"), | ||
225 | /* HES (Hudson Entertainment System Sound Format) */ | ||
226 | [AFMT_HES] = | ||
227 | AFMT_ENTRY("HES", "hes", NULL, get_hes_metadata, "hes\0"), | ||
228 | /* SGC (Sega Master System, Game Gear, Coleco Vision Sound Format) */ | ||
229 | [AFMT_SGC] = | ||
230 | AFMT_ENTRY("SGC", "sgc", NULL, get_sgc_metadata, "sgc\0"), | ||
231 | /* VGM (Video Game Music Format) */ | ||
232 | [AFMT_VGM] = | ||
233 | AFMT_ENTRY("VGM", "vgm", NULL, get_vgm_metadata, "vgm\0vgz\0"), | ||
234 | /* KSS (MSX computer KSS Music File) */ | ||
235 | [AFMT_KSS] = | ||
236 | AFMT_ENTRY("KSS", "kss", NULL, get_kss_metadata, "kss\0"), | ||
237 | #endif | ||
238 | }; | ||
239 | |||
240 | #if CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING) | ||
241 | /* get REC_FORMAT_* corresponding AFMT_* */ | ||
242 | const int rec_format_afmt[REC_NUM_FORMATS] = | ||
243 | { | ||
244 | /* give AFMT_UNKNOWN by default */ | ||
245 | [0 ... REC_NUM_FORMATS-1] = AFMT_UNKNOWN, | ||
246 | /* add new entries below this line */ | ||
247 | [REC_FORMAT_AIFF] = AFMT_AIFF, | ||
248 | [REC_FORMAT_MPA_L3] = AFMT_MPA_L3, | ||
249 | [REC_FORMAT_WAVPACK] = AFMT_WAVPACK, | ||
250 | [REC_FORMAT_PCM_WAV] = AFMT_PCM_WAV, | ||
251 | }; | ||
252 | |||
253 | #if 0 /* Currently unused, left for reference and future use */ | ||
254 | /* get AFMT_* corresponding REC_FORMAT_* */ | ||
255 | const int afmt_rec_format[AFMT_NUM_CODECS] = | ||
256 | { | ||
257 | /* give -1 by default */ | ||
258 | [0 ... AFMT_NUM_CODECS-1] = -1, | ||
259 | /* add new entries below this line */ | ||
260 | [AFMT_AIFF] = REC_FORMAT_AIFF, | ||
261 | [AFMT_MPA_L3] = REC_FORMAT_MPA_L3, | ||
262 | [AFMT_WAVPACK] = REC_FORMAT_WAVPACK, | ||
263 | [AFMT_PCM_WAV] = REC_FORMAT_PCM_WAV, | ||
264 | }; | ||
265 | #endif | ||
266 | #endif /* CONFIG_CODEC == SWCODEC && defined (HAVE_RECORDING) */ | ||
267 | |||
268 | #if CONFIG_CODEC == SWCODEC | ||
269 | /* Get the canonical AFMT type */ | ||
270 | int get_audio_base_codec_type(int type) | ||
271 | { | ||
272 | int base_type = type; | ||
273 | switch (type) { | ||
274 | case AFMT_MPA_L1: | ||
275 | case AFMT_MPA_L2: | ||
276 | case AFMT_MPA_L3: | ||
277 | base_type = AFMT_MPA_L3; | ||
278 | break; | ||
279 | case AFMT_MPC_SV7: | ||
280 | case AFMT_MPC_SV8: | ||
281 | base_type = AFMT_MPC_SV7; | ||
282 | break; | ||
283 | case AFMT_MP4_AAC: | ||
284 | case AFMT_MP4_AAC_HE: | ||
285 | base_type = AFMT_MP4_AAC; | ||
286 | break; | ||
287 | case AFMT_SAP: | ||
288 | case AFMT_CMC: | ||
289 | case AFMT_CM3: | ||
290 | case AFMT_CMR: | ||
291 | case AFMT_CMS: | ||
292 | case AFMT_DMC: | ||
293 | case AFMT_DLT: | ||
294 | case AFMT_MPT: | ||
295 | case AFMT_MPD: | ||
296 | case AFMT_RMT: | ||
297 | case AFMT_TMC: | ||
298 | case AFMT_TM8: | ||
299 | case AFMT_TM2: | ||
300 | base_type = AFMT_SAP; | ||
301 | break; | ||
302 | default: | ||
303 | break; | ||
304 | } | ||
305 | |||
306 | return base_type; | ||
307 | } | ||
308 | |||
309 | /* Get the basic audio type */ | ||
310 | enum data_type get_audio_base_data_type(int afmt) | ||
311 | { | ||
312 | if ((unsigned)afmt >= AFMT_NUM_CODECS) | ||
313 | return TYPE_UNKNOWN; | ||
314 | |||
315 | switch (get_audio_base_codec_type(afmt)) | ||
316 | { | ||
317 | case AFMT_NSF: | ||
318 | case AFMT_SPC: | ||
319 | case AFMT_SID: | ||
320 | case AFMT_MOD: | ||
321 | case AFMT_SAP: | ||
322 | case AFMT_AY: | ||
323 | case AFMT_GBS: | ||
324 | case AFMT_HES: | ||
325 | case AFMT_SGC: | ||
326 | case AFMT_VGM: | ||
327 | case AFMT_KSS: | ||
328 | /* Type must be allocated and loaded in its entirety onto | ||
329 | the buffer */ | ||
330 | return TYPE_ATOMIC_AUDIO; | ||
331 | |||
332 | default: | ||
333 | /* Assume type may be loaded and discarded incrementally */ | ||
334 | return TYPE_PACKET_AUDIO; | ||
335 | |||
336 | case AFMT_UNKNOWN: | ||
337 | /* Have no idea at all */ | ||
338 | return TYPE_UNKNOWN; | ||
339 | } | ||
340 | } | ||
341 | |||
342 | /* Is the format allowed to buffer starting at some offset other than 0 | ||
343 | or first frame only for resume purposes? */ | ||
344 | bool format_buffers_with_offset(int afmt) | ||
345 | { | ||
346 | switch (afmt) | ||
347 | { | ||
348 | case AFMT_MPA_L1: | ||
349 | case AFMT_MPA_L2: | ||
350 | case AFMT_MPA_L3: | ||
351 | case AFMT_WAVPACK: | ||
352 | /* Format may be loaded at the first needed frame */ | ||
353 | return true; | ||
354 | default: | ||
355 | /* Format must be loaded from the beginning of the file | ||
356 | (does not imply 'atomic', while 'atomic' implies 'no offset') */ | ||
357 | return false; | ||
358 | } | ||
359 | } | ||
360 | #endif /* CONFIG_CODEC == SWCODEC */ | ||
361 | |||
362 | |||
363 | /* Simple file type probing by looking at the filename extension. */ | ||
364 | unsigned int probe_file_format(const char *filename) | ||
365 | { | ||
366 | char *suffix; | ||
367 | unsigned int i; | ||
368 | |||
369 | suffix = strrchr(filename, '.'); | ||
370 | |||
371 | if (suffix == NULL) | ||
372 | { | ||
373 | return AFMT_UNKNOWN; | ||
374 | } | ||
375 | |||
376 | /* skip '.' */ | ||
377 | suffix++; | ||
378 | |||
379 | for (i = 1; i < AFMT_NUM_CODECS; i++) | ||
380 | { | ||
381 | /* search extension list for type */ | ||
382 | const char *ext = audio_formats[i].ext_list; | ||
383 | |||
384 | do | ||
385 | { | ||
386 | if (strcasecmp(suffix, ext) == 0) | ||
387 | { | ||
388 | return i; | ||
389 | } | ||
390 | |||
391 | ext += strlen(ext) + 1; | ||
392 | } | ||
393 | while (*ext != '\0'); | ||
394 | } | ||
395 | |||
396 | return AFMT_UNKNOWN; | ||
397 | } | ||
398 | |||
399 | /* Note, that this returns false for successful, true for error! */ | ||
400 | bool mp3info(struct mp3entry *entry, const char *filename) | ||
401 | { | ||
402 | int fd; | ||
403 | bool result; | ||
404 | |||
405 | fd = open(filename, O_RDONLY); | ||
406 | if (fd < 0) | ||
407 | return true; | ||
408 | |||
409 | result = !get_metadata(entry, fd, filename); | ||
410 | |||
411 | close(fd); | ||
412 | |||
413 | return result; | ||
414 | } | ||
415 | |||
416 | /* Get metadata for track - return false if parsing showed problems with the | ||
417 | * file that would prevent playback. | ||
418 | */ | ||
419 | bool get_metadata(struct mp3entry* id3, int fd, const char* trackname) | ||
420 | { | ||
421 | const struct afmt_entry *entry; | ||
422 | int logfd = 0; | ||
423 | DEBUGF("Read metadata for %s\n", trackname); | ||
424 | if (write_metadata_log) | ||
425 | { | ||
426 | logfd = open("/metadata.log", O_WRONLY | O_APPEND | O_CREAT, 0666); | ||
427 | if (logfd >= 0) | ||
428 | { | ||
429 | write(logfd, trackname, strlen(trackname)); | ||
430 | write(logfd, "\n", 1); | ||
431 | close(logfd); | ||
432 | } | ||
433 | } | ||
434 | |||
435 | /* Clear the mp3entry to avoid having bogus pointers appear */ | ||
436 | wipe_mp3entry(id3); | ||
437 | |||
438 | /* Take our best guess at the codec type based on file extension */ | ||
439 | id3->codectype = probe_file_format(trackname); | ||
440 | |||
441 | /* default values for embedded cuesheets */ | ||
442 | id3->has_embedded_cuesheet = false; | ||
443 | id3->embedded_cuesheet.pos = 0; | ||
444 | |||
445 | entry = &audio_formats[id3->codectype]; | ||
446 | |||
447 | /* Load codec specific track tag information and confirm the codec type. */ | ||
448 | if (!entry->parse_func) | ||
449 | { | ||
450 | DEBUGF("nothing to parse for %s (format %s)", trackname, entry->label); | ||
451 | return false; | ||
452 | } | ||
453 | |||
454 | if (!entry->parse_func(fd, id3)) | ||
455 | { | ||
456 | DEBUGF("parsing %s failed (format: %s)", trackname, entry->label); | ||
457 | return false; | ||
458 | } | ||
459 | |||
460 | lseek(fd, 0, SEEK_SET); | ||
461 | strlcpy(id3->path, trackname, sizeof(id3->path)); | ||
462 | /* We have successfully read the metadata from the file */ | ||
463 | return true; | ||
464 | } | ||
465 | |||
466 | #ifndef __PCTOOL__ | ||
467 | #if CONFIG_CODEC == SWCODEC | ||
468 | void strip_tags(int handle_id) | ||
469 | { | ||
470 | static const unsigned char tag[] = "TAG"; | ||
471 | static const unsigned char apetag[] = "APETAGEX"; | ||
472 | size_t len, version; | ||
473 | void *tail; | ||
474 | |||
475 | if (bufgettail(handle_id, 128, &tail) != 128) | ||
476 | return; | ||
477 | |||
478 | if (memcmp(tail, tag, 3) == 0) | ||
479 | { | ||
480 | /* Skip id3v1 tag */ | ||
481 | logf("Cutting off ID3v1 tag"); | ||
482 | bufcuttail(handle_id, 128); | ||
483 | } | ||
484 | |||
485 | /* Get a new tail, as the old one may have been cut */ | ||
486 | if (bufgettail(handle_id, 32, &tail) != 32) | ||
487 | return; | ||
488 | |||
489 | /* Check for APE tag (look for the APE tag footer) */ | ||
490 | if (memcmp(tail, apetag, 8) != 0) | ||
491 | return; | ||
492 | |||
493 | /* Read the version and length from the footer */ | ||
494 | version = get_long_le(&((unsigned char *)tail)[8]); | ||
495 | len = get_long_le(&((unsigned char *)tail)[12]); | ||
496 | if (version == 2000) | ||
497 | len += 32; /* APEv2 has a 32 byte header */ | ||
498 | |||
499 | /* Skip APE tag */ | ||
500 | logf("Cutting off APE tag (%ldB)", len); | ||
501 | bufcuttail(handle_id, len); | ||
502 | } | ||
503 | #endif /* CONFIG_CODEC == SWCODEC */ | ||
504 | #endif /* ! __PCTOOL__ */ | ||
505 | |||
506 | #define MOVE_ENTRY(x) if (x) x += offset; | ||
507 | |||
508 | void adjust_mp3entry(struct mp3entry *entry, void *dest, const void *orig) | ||
509 | { | ||
510 | long offset; | ||
511 | if (orig > dest) | ||
512 | offset = -((size_t)orig - (size_t)dest); | ||
513 | else | ||
514 | offset = ((size_t)dest - (size_t)orig); | ||
515 | |||
516 | MOVE_ENTRY(entry->title) | ||
517 | MOVE_ENTRY(entry->artist) | ||
518 | MOVE_ENTRY(entry->album) | ||
519 | |||
520 | if (entry->genre_string > (char*)orig && | ||
521 | entry->genre_string < (char*)orig + sizeof(struct mp3entry)) | ||
522 | /* Don't adjust that if it points to an entry of the "genres" array */ | ||
523 | entry->genre_string += offset; | ||
524 | |||
525 | MOVE_ENTRY(entry->track_string) | ||
526 | MOVE_ENTRY(entry->disc_string) | ||
527 | MOVE_ENTRY(entry->year_string) | ||
528 | MOVE_ENTRY(entry->composer) | ||
529 | MOVE_ENTRY(entry->comment) | ||
530 | MOVE_ENTRY(entry->albumartist) | ||
531 | MOVE_ENTRY(entry->grouping) | ||
532 | MOVE_ENTRY(entry->mb_track_id) | ||
533 | } | ||
534 | |||
535 | void copy_mp3entry(struct mp3entry *dest, const struct mp3entry *orig) | ||
536 | { | ||
537 | memcpy(dest, orig, sizeof(struct mp3entry)); | ||
538 | adjust_mp3entry(dest, dest, orig); | ||
539 | } | ||
540 | |||
541 | /* A shortcut to simplify the common task of clearing the struct */ | ||
542 | void wipe_mp3entry(struct mp3entry *id3) | ||
543 | { | ||
544 | memset(id3, 0, sizeof (struct mp3entry)); | ||
545 | } | ||
546 | |||
547 | #if CONFIG_CODEC == SWCODEC | ||
548 | /* Glean what is possible from the filename alone - does not parse metadata */ | ||
549 | void fill_metadata_from_path(struct mp3entry *id3, const char *trackname) | ||
550 | { | ||
551 | char *p; | ||
552 | |||
553 | /* Clear the mp3entry to avoid having bogus pointers appear */ | ||
554 | wipe_mp3entry(id3); | ||
555 | |||
556 | /* Find the filename portion of the path */ | ||
557 | p = strrchr(trackname, '/'); | ||
558 | strlcpy(id3->id3v2buf, p ? ++p : id3->path, ID3V2_BUF_SIZE); | ||
559 | |||
560 | /* Get the format from the extension and trim it off */ | ||
561 | p = strrchr(id3->id3v2buf, '.'); | ||
562 | if (p) | ||
563 | { | ||
564 | /* Might be wrong for container formats - should we bother? */ | ||
565 | id3->codectype = probe_file_format(p); | ||
566 | |||
567 | if (id3->codectype != AFMT_UNKNOWN) | ||
568 | *p = '\0'; | ||
569 | } | ||
570 | |||
571 | /* Set the filename as the title */ | ||
572 | id3->title = id3->id3v2buf; | ||
573 | |||
574 | /* Copy the path info */ | ||
575 | strlcpy(id3->path, trackname, sizeof (id3->path)); | ||
576 | } | ||
577 | #endif /* CONFIG_CODEC == SWCODEC */ | ||
578 | |||
579 | #ifndef __PCTOOL__ | ||
580 | #ifdef HAVE_TAGCACHE | ||
581 | #if CONFIG_CODEC == SWCODEC | ||
582 | |||
583 | enum { AUTORESUMABLE_UNKNOWN = 0, AUTORESUMABLE_TRUE, AUTORESUMABLE_FALSE }; | ||
584 | |||
585 | bool autoresumable(struct mp3entry *id3) | ||
586 | { | ||
587 | char *endp, *path; | ||
588 | size_t len; | ||
589 | bool is_resumable; | ||
590 | |||
591 | if (id3->autoresumable) /* result cached? */ | ||
592 | return id3->autoresumable == AUTORESUMABLE_TRUE; | ||
593 | |||
594 | is_resumable = false; | ||
595 | |||
596 | if (id3->path) | ||
597 | { | ||
598 | for (path = global_settings.autoresume_paths; | ||
599 | *path; /* search terms left? */ | ||
600 | path++) | ||
601 | { | ||
602 | if (*path == ':') /* Skip empty search patterns */ | ||
603 | continue; | ||
604 | |||
605 | /* FIXME: As soon as strcspn or strchrnul are made available in | ||
606 | the core, the following can be made more efficient. */ | ||
607 | endp = strchr(path, ':'); | ||
608 | if (endp) | ||
609 | len = endp - path; | ||
610 | else | ||
611 | len = strlen(path); | ||
612 | |||
613 | /* Note: At this point, len is always > 0 */ | ||
614 | |||
615 | if (strncasecmp(id3->path, path, len) == 0) | ||
616 | { | ||
617 | /* Full directory-name matches only. Trailing '/' in | ||
618 | search path OK. */ | ||
619 | if (id3->path[len] == '/' || id3->path[len - 1] == '/') | ||
620 | { | ||
621 | is_resumable = true; | ||
622 | break; | ||
623 | } | ||
624 | } | ||
625 | path += len - 1; | ||
626 | } | ||
627 | } | ||
628 | |||
629 | /* cache result */ | ||
630 | id3->autoresumable = | ||
631 | is_resumable ? AUTORESUMABLE_TRUE : AUTORESUMABLE_FALSE; | ||
632 | |||
633 | logf("autoresumable: %s is%s resumable", | ||
634 | id3->path, is_resumable ? "" : " not"); | ||
635 | |||
636 | return is_resumable; | ||
637 | } | ||
638 | |||
639 | #endif /* SWCODEC */ | ||
640 | #endif /* HAVE_TAGCACHE */ | ||
641 | #endif /* __PCTOOL__ */ | ||