diff options
author | Marcoen Hirschberg <marcoen@gmail.com> | 2007-06-16 18:19:51 +0000 |
---|---|---|
committer | Marcoen Hirschberg <marcoen@gmail.com> | 2007-06-16 18:19:51 +0000 |
commit | 2175d1edf65367fd3fe3cff266b8d6ea12930f2f (patch) | |
tree | 9d6f50ebfc7919b4e2f0853fc8c0cb844cabe995 /apps/metadata/vorbis.c | |
parent | c3206a455a455fadb282d09f9af482c66b6bdf8e (diff) | |
download | rockbox-2175d1edf65367fd3fe3cff266b8d6ea12930f2f.tar.gz rockbox-2175d1edf65367fd3fe3cff266b8d6ea12930f2f.zip |
split up the metadata parser
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@13637 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/metadata/vorbis.c')
-rw-r--r-- | apps/metadata/vorbis.c | 330 |
1 files changed, 330 insertions, 0 deletions
diff --git a/apps/metadata/vorbis.c b/apps/metadata/vorbis.c new file mode 100644 index 0000000000..5112615e47 --- /dev/null +++ b/apps/metadata/vorbis.c | |||
@@ -0,0 +1,330 @@ | |||
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 "metadata_parsers.h" | ||
29 | #include "structec.h" | ||
30 | #include "logf.h" | ||
31 | |||
32 | /* Read the items in a Vorbis comment packet. Returns true the items were | ||
33 | * fully read, false otherwise. | ||
34 | */ | ||
35 | bool read_vorbis_tags(int fd, struct mp3entry *id3, | ||
36 | long tag_remaining) | ||
37 | { | ||
38 | char *buf = id3->id3v2buf; | ||
39 | int32_t comment_count; | ||
40 | int32_t len; | ||
41 | int buf_remaining = sizeof(id3->id3v2buf) + sizeof(id3->id3v1buf); | ||
42 | int i; | ||
43 | |||
44 | if (ecread(fd, &len, 1, "l", IS_BIG_ENDIAN) < (long) sizeof(len)) | ||
45 | { | ||
46 | return false; | ||
47 | } | ||
48 | |||
49 | if ((lseek(fd, len, SEEK_CUR) < 0) | ||
50 | || (ecread(fd, &comment_count, 1, "l", IS_BIG_ENDIAN) | ||
51 | < (long) sizeof(comment_count))) | ||
52 | { | ||
53 | return false; | ||
54 | } | ||
55 | |||
56 | tag_remaining -= len + sizeof(len) + sizeof(comment_count); | ||
57 | |||
58 | if (tag_remaining <= 0) | ||
59 | { | ||
60 | return true; | ||
61 | } | ||
62 | |||
63 | for (i = 0; i < comment_count; i++) | ||
64 | { | ||
65 | char name[TAG_NAME_LENGTH]; | ||
66 | char value[TAG_VALUE_LENGTH]; | ||
67 | int32_t read_len; | ||
68 | |||
69 | if (tag_remaining < 4) | ||
70 | { | ||
71 | break; | ||
72 | } | ||
73 | |||
74 | if (ecread(fd, &len, 1, "l", IS_BIG_ENDIAN) < (long) sizeof(len)) | ||
75 | { | ||
76 | return false; | ||
77 | } | ||
78 | |||
79 | tag_remaining -= 4; | ||
80 | |||
81 | /* Quit if we've passed the end of the page */ | ||
82 | if (tag_remaining < len) | ||
83 | { | ||
84 | break; | ||
85 | } | ||
86 | |||
87 | tag_remaining -= len; | ||
88 | read_len = read_string(fd, name, sizeof(name), '=', len); | ||
89 | |||
90 | if (read_len < 0) | ||
91 | { | ||
92 | return false; | ||
93 | } | ||
94 | |||
95 | len -= read_len; | ||
96 | |||
97 | if (read_string(fd, value, sizeof(value), -1, len) < 0) | ||
98 | { | ||
99 | return false; | ||
100 | } | ||
101 | |||
102 | len = parse_tag(name, value, id3, buf, buf_remaining, | ||
103 | TAGTYPE_VORBIS); | ||
104 | buf += len; | ||
105 | buf_remaining -= len; | ||
106 | } | ||
107 | |||
108 | /* Skip to the end of the block */ | ||
109 | if (tag_remaining) | ||
110 | { | ||
111 | if (lseek(fd, tag_remaining, SEEK_CUR) < 0) | ||
112 | { | ||
113 | return false; | ||
114 | } | ||
115 | } | ||
116 | |||
117 | return true; | ||
118 | } | ||
119 | |||
120 | /* A simple parser to read vital metadata from an Ogg Vorbis file. | ||
121 | * Calls get_speex_metadata if a speex file is identified. Returns | ||
122 | * false if metadata needed by the Vorbis codec couldn't be read. | ||
123 | */ | ||
124 | bool get_vorbis_metadata(int fd, struct mp3entry* id3) | ||
125 | { | ||
126 | /* An Ogg File is split into pages, each starting with the string | ||
127 | * "OggS". Each page has a timestamp (in PCM samples) referred to as | ||
128 | * the "granule position". | ||
129 | * | ||
130 | * An Ogg Vorbis has the following structure: | ||
131 | * 1) Identification header (containing samplerate, numchannels, etc) | ||
132 | * 2) Comment header - containing the Vorbis Comments | ||
133 | * 3) Setup header - containing codec setup information | ||
134 | * 4) Many audio packets... | ||
135 | */ | ||
136 | |||
137 | /* Use the path name of the id3 structure as a temporary buffer. */ | ||
138 | unsigned char* buf = (unsigned char *)id3->path; | ||
139 | long comment_size; | ||
140 | long remaining = 0; | ||
141 | long last_serial = 0; | ||
142 | long serial, r; | ||
143 | int segments; | ||
144 | int i; | ||
145 | bool eof = false; | ||
146 | |||
147 | if ((lseek(fd, 0, SEEK_SET) < 0) || (read(fd, buf, 58) < 4)) | ||
148 | { | ||
149 | return false; | ||
150 | } | ||
151 | |||
152 | if ((memcmp(buf, "OggS", 4) != 0) || (memcmp(&buf[29], "vorbis", 6) != 0)) | ||
153 | { | ||
154 | if ((memcmp(buf, "OggS", 4) != 0) || (memcmp(&buf[28], "Speex", 5) != 0)) | ||
155 | { | ||
156 | return false; | ||
157 | } | ||
158 | else | ||
159 | { | ||
160 | id3->codectype = AFMT_SPEEX; | ||
161 | return get_speex_metadata(fd, id3); | ||
162 | } | ||
163 | } | ||
164 | |||
165 | /* We need to ensure the serial number from this page is the same as the | ||
166 | * one from the last page (since we only support a single bitstream). | ||
167 | */ | ||
168 | serial = get_long_le(&buf[14]); | ||
169 | id3->frequency = get_long_le(&buf[40]); | ||
170 | id3->filesize = filesize(fd); | ||
171 | |||
172 | /* Comments are in second Ogg page */ | ||
173 | if (lseek(fd, 58, SEEK_SET) < 0) | ||
174 | { | ||
175 | return false; | ||
176 | } | ||
177 | |||
178 | /* Minimum header length for Ogg pages is 27. */ | ||
179 | if (read(fd, buf, 27) < 27) | ||
180 | { | ||
181 | return false; | ||
182 | } | ||
183 | |||
184 | if (memcmp(buf, "OggS", 4) !=0 ) | ||
185 | { | ||
186 | return false; | ||
187 | } | ||
188 | |||
189 | segments = buf[26]; | ||
190 | |||
191 | /* read in segment table */ | ||
192 | if (read(fd, buf, segments) < segments) | ||
193 | { | ||
194 | return false; | ||
195 | } | ||
196 | |||
197 | /* The second packet in a vorbis stream is the comment packet. It *may* | ||
198 | * extend beyond the second page, but usually does not. Here we find the | ||
199 | * length of the comment packet (or the rest of the page if the comment | ||
200 | * packet extends to the third page). | ||
201 | */ | ||
202 | for (i = 0; i < segments; i++) | ||
203 | { | ||
204 | remaining += buf[i]; | ||
205 | |||
206 | /* The last segment of a packet is always < 255 bytes */ | ||
207 | if (buf[i] < 255) | ||
208 | { | ||
209 | break; | ||
210 | } | ||
211 | } | ||
212 | |||
213 | /* Now read in packet header (type and id string) */ | ||
214 | if (read(fd, buf, 7) < 7) | ||
215 | { | ||
216 | return false; | ||
217 | } | ||
218 | |||
219 | comment_size = remaining; | ||
220 | remaining -= 7; | ||
221 | |||
222 | /* The first byte of a packet is the packet type; comment packets are | ||
223 | * type 3. | ||
224 | */ | ||
225 | if ((buf[0] != 3) || (memcmp(buf + 1, "vorbis", 6) !=0)) | ||
226 | { | ||
227 | return false; | ||
228 | } | ||
229 | |||
230 | /* Failure to read the tags isn't fatal. */ | ||
231 | read_vorbis_tags(fd, id3, remaining); | ||
232 | |||
233 | /* We now need to search for the last page in the file - identified by | ||
234 | * by ('O','g','g','S',0) and retrieve totalsamples. | ||
235 | */ | ||
236 | |||
237 | /* A page is always < 64 kB */ | ||
238 | if (lseek(fd, -(MIN(64 * 1024, id3->filesize)), SEEK_END) < 0) | ||
239 | { | ||
240 | return false; | ||
241 | } | ||
242 | |||
243 | remaining = 0; | ||
244 | |||
245 | while (!eof) | ||
246 | { | ||
247 | r = read(fd, &buf[remaining], MAX_PATH - remaining); | ||
248 | |||
249 | if (r <= 0) | ||
250 | { | ||
251 | eof = true; | ||
252 | } | ||
253 | else | ||
254 | { | ||
255 | remaining += r; | ||
256 | } | ||
257 | |||
258 | /* Inefficient (but simple) search */ | ||
259 | i = 0; | ||
260 | |||
261 | while (i < (remaining - 3)) | ||
262 | { | ||
263 | if ((buf[i] == 'O') && (memcmp(&buf[i], "OggS", 4) == 0)) | ||
264 | { | ||
265 | if (i < (remaining - 17)) | ||
266 | { | ||
267 | /* Note that this only reads the low 32 bits of a | ||
268 | * 64 bit value. | ||
269 | */ | ||
270 | id3->samples = get_long_le(&buf[i + 6]); | ||
271 | last_serial = get_long_le(&buf[i + 14]); | ||
272 | |||
273 | /* If this page is very small the beginning of the next | ||
274 | * header could be in buffer. Jump near end of this header | ||
275 | * and continue */ | ||
276 | i += 27; | ||
277 | } | ||
278 | else | ||
279 | { | ||
280 | break; | ||
281 | } | ||
282 | } | ||
283 | else | ||
284 | { | ||
285 | i++; | ||
286 | } | ||
287 | } | ||
288 | |||
289 | if (i < remaining) | ||
290 | { | ||
291 | /* Move the remaining bytes to start of buffer. | ||
292 | * Reuse var 'segments' as it is no longer needed */ | ||
293 | segments = 0; | ||
294 | while (i < remaining) | ||
295 | { | ||
296 | buf[segments++] = buf[i++]; | ||
297 | } | ||
298 | remaining = segments; | ||
299 | } | ||
300 | else | ||
301 | { | ||
302 | /* Discard the rest of the buffer */ | ||
303 | remaining = 0; | ||
304 | } | ||
305 | } | ||
306 | |||
307 | /* This file has mutiple vorbis bitstreams (or is corrupt). */ | ||
308 | /* FIXME we should display an error here. */ | ||
309 | if (serial != last_serial) | ||
310 | { | ||
311 | logf("serialno mismatch"); | ||
312 | logf("%ld", serial); | ||
313 | logf("%ld", last_serial); | ||
314 | return false; | ||
315 | } | ||
316 | |||
317 | id3->length = ((int64_t) id3->samples * 1000) / id3->frequency; | ||
318 | |||
319 | if (id3->length <= 0) | ||
320 | { | ||
321 | logf("ogg length invalid!"); | ||
322 | return false; | ||
323 | } | ||
324 | |||
325 | id3->bitrate = (((int64_t) id3->filesize - comment_size) * 8) / id3->length; | ||
326 | id3->vbr = true; | ||
327 | |||
328 | return true; | ||
329 | } | ||
330 | |||