diff options
Diffstat (limited to 'apps/metadata/flac.c')
-rw-r--r-- | apps/metadata/flac.c | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/apps/metadata/flac.c b/apps/metadata/flac.c new file mode 100644 index 0000000000..5b3644ede2 --- /dev/null +++ b/apps/metadata/flac.c | |||
@@ -0,0 +1,122 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2005 Dave Chapman | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | #include <stdio.h> | ||
20 | #include <string.h> | ||
21 | #include <stdlib.h> | ||
22 | #include <ctype.h> | ||
23 | #include <inttypes.h> | ||
24 | |||
25 | #include "system.h" | ||
26 | #include "id3.h" | ||
27 | #include "metadata_common.h" | ||
28 | #include "logf.h" | ||
29 | |||
30 | bool get_flac_metadata(int fd, struct mp3entry* id3) | ||
31 | { | ||
32 | /* A simple parser to read vital metadata from a FLAC file - length, | ||
33 | * frequency, bitrate etc. This code should either be moved to a | ||
34 | * seperate file, or discarded in favour of the libFLAC code. | ||
35 | * The FLAC stream specification can be found at | ||
36 | * http://flac.sourceforge.net/format.html#stream | ||
37 | */ | ||
38 | |||
39 | /* Use the trackname part of the id3 structure as a temporary buffer */ | ||
40 | unsigned char* buf = (unsigned char *)id3->path; | ||
41 | bool rc = false; | ||
42 | |||
43 | if (!skip_id3v2(fd, id3) || (read(fd, buf, 4) < 4)) | ||
44 | { | ||
45 | return rc; | ||
46 | } | ||
47 | |||
48 | if (memcmp(buf, "fLaC", 4) != 0) | ||
49 | { | ||
50 | return rc; | ||
51 | } | ||
52 | |||
53 | while (true) | ||
54 | { | ||
55 | long i; | ||
56 | |||
57 | if (read(fd, buf, 4) < 0) | ||
58 | { | ||
59 | return rc; | ||
60 | } | ||
61 | |||
62 | /* The length of the block */ | ||
63 | i = (buf[1] << 16) | (buf[2] << 8) | buf[3]; | ||
64 | |||
65 | if ((buf[0] & 0x7f) == 0) /* 0 is the STREAMINFO block */ | ||
66 | { | ||
67 | unsigned long totalsamples; | ||
68 | |||
69 | /* FIXME: Don't trust the value of i */ | ||
70 | if (read(fd, buf, i) < 0) | ||
71 | { | ||
72 | return rc; | ||
73 | } | ||
74 | |||
75 | id3->vbr = true; /* All FLAC files are VBR */ | ||
76 | id3->filesize = filesize(fd); | ||
77 | id3->frequency = (buf[10] << 12) | (buf[11] << 4) | ||
78 | | ((buf[12] & 0xf0) >> 4); | ||
79 | rc = true; /* Got vital metadata */ | ||
80 | |||
81 | /* totalsamples is a 36-bit field, but we assume <= 32 bits are used */ | ||
82 | totalsamples = get_long_be(&buf[14]); | ||
83 | |||
84 | /* Calculate track length (in ms) and estimate the bitrate (in kbit/s) */ | ||
85 | id3->length = ((int64_t) totalsamples * 1000) / id3->frequency; | ||
86 | |||
87 | if (id3->length <= 0) | ||
88 | { | ||
89 | logf("flac length invalid!"); | ||
90 | return false; | ||
91 | } | ||
92 | |||
93 | id3->bitrate = (id3->filesize * 8) / id3->length; | ||
94 | } | ||
95 | else if ((buf[0] & 0x7f) == 4) /* 4 is the VORBIS_COMMENT block */ | ||
96 | { | ||
97 | /* The next i bytes of the file contain the VORBIS COMMENTS. */ | ||
98 | if (!read_vorbis_tags(fd, id3, i)) | ||
99 | { | ||
100 | return rc; | ||
101 | } | ||
102 | } | ||
103 | else | ||
104 | { | ||
105 | if (buf[0] & 0x80) | ||
106 | { | ||
107 | /* If we have reached the last metadata block, abort. */ | ||
108 | break; | ||
109 | } | ||
110 | else | ||
111 | { | ||
112 | /* Skip to next metadata block */ | ||
113 | if (lseek(fd, i, SEEK_CUR) < 0) | ||
114 | { | ||
115 | return rc; | ||
116 | } | ||
117 | } | ||
118 | } | ||
119 | } | ||
120 | |||
121 | return true; | ||
122 | } | ||