diff options
Diffstat (limited to 'apps/metadata/ogg.c')
-rw-r--r-- | apps/metadata/ogg.c | 424 |
1 files changed, 424 insertions, 0 deletions
diff --git a/apps/metadata/ogg.c b/apps/metadata/ogg.c new file mode 100644 index 0000000000..a109694e9d --- /dev/null +++ b/apps/metadata/ogg.c | |||
@@ -0,0 +1,424 @@ | |||
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 | /* A simple parser to read vital metadata from an Ogg Speex file. Returns | ||
31 | * false if metadata needed by the Speex codec couldn't be read. | ||
32 | */ | ||
33 | |||
34 | bool get_speex_metadata(int fd, struct mp3entry* id3) | ||
35 | { | ||
36 | /* An Ogg File is split into pages, each starting with the string | ||
37 | * "OggS". Each page has a timestamp (in PCM samples) referred to as | ||
38 | * the "granule position". | ||
39 | * | ||
40 | * An Ogg Speex has the following structure: | ||
41 | * 1) Identification header (containing samplerate, numchannels, etc) | ||
42 | Described in this page: (http://www.speex.org/manual2/node7.html) | ||
43 | * 2) Comment header - containing the Vorbis Comments | ||
44 | * 3) Many audio packets... | ||
45 | */ | ||
46 | |||
47 | /* Use the path name of the id3 structure as a temporary buffer. */ | ||
48 | unsigned char* buf = (unsigned char*)id3->path; | ||
49 | long comment_size; | ||
50 | long remaining = 0; | ||
51 | long last_serial = 0; | ||
52 | long serial, r; | ||
53 | int segments; | ||
54 | int i; | ||
55 | bool eof = false; | ||
56 | |||
57 | if ((lseek(fd, 0, SEEK_SET) < 0) || (read(fd, buf, 58) < 33)) | ||
58 | { | ||
59 | return false; | ||
60 | } | ||
61 | |||
62 | if ((memcmp(buf, "OggS", 4) != 0) || (memcmp(&buf[28], "Speex", 5) != 0)) | ||
63 | { | ||
64 | return false; | ||
65 | } | ||
66 | |||
67 | /* We need to ensure the serial number from this page is the same as the | ||
68 | * one from the last page (since we only support a single bitstream). | ||
69 | */ | ||
70 | serial = get_long_le(&buf[14]); | ||
71 | if ((lseek(fd, 33, SEEK_SET) < 0)||(read(fd, buf, 58) < 4)) | ||
72 | { | ||
73 | return false; | ||
74 | } | ||
75 | |||
76 | id3->frequency = get_slong(&buf[31]); | ||
77 | last_serial = get_long_le(&buf[27]);/*temporary, header size*/ | ||
78 | id3->bitrate = get_long_le(&buf[47]); | ||
79 | id3->vbr = get_long_le(&buf[55]); | ||
80 | id3->filesize = filesize(fd); | ||
81 | /* Comments are in second Ogg page */ | ||
82 | if (lseek(fd, 28+last_serial/*(temporary for header size)*/, SEEK_SET) < 0) | ||
83 | { | ||
84 | return false; | ||
85 | } | ||
86 | |||
87 | /* Minimum header length for Ogg pages is 27. */ | ||
88 | if (read(fd, buf, 27) < 27) | ||
89 | { | ||
90 | return false; | ||
91 | } | ||
92 | |||
93 | if (memcmp(buf, "OggS", 4) !=0 ) | ||
94 | { | ||
95 | return false; | ||
96 | } | ||
97 | |||
98 | segments = buf[26]; | ||
99 | /* read in segment table */ | ||
100 | if (read(fd, buf, segments) < segments) | ||
101 | { | ||
102 | return false; | ||
103 | } | ||
104 | |||
105 | /* The second packet in a vorbis stream is the comment packet. It *may* | ||
106 | * extend beyond the second page, but usually does not. Here we find the | ||
107 | * length of the comment packet (or the rest of the page if the comment | ||
108 | * packet extends to the third page). | ||
109 | */ | ||
110 | for (i = 0; i < segments; i++) | ||
111 | { | ||
112 | remaining += buf[i]; | ||
113 | /* The last segment of a packet is always < 255 bytes */ | ||
114 | if (buf[i] < 255) | ||
115 | { | ||
116 | break; | ||
117 | } | ||
118 | } | ||
119 | |||
120 | comment_size = remaining; | ||
121 | |||
122 | /* Failure to read the tags isn't fatal. */ | ||
123 | read_vorbis_tags(fd, id3, remaining); | ||
124 | |||
125 | /* We now need to search for the last page in the file - identified by | ||
126 | * by ('O','g','g','S',0) and retrieve totalsamples. | ||
127 | */ | ||
128 | |||
129 | /* A page is always < 64 kB */ | ||
130 | if (lseek(fd, -(MIN(64 * 1024, id3->filesize)), SEEK_END) < 0) | ||
131 | { | ||
132 | return false; | ||
133 | } | ||
134 | |||
135 | remaining = 0; | ||
136 | |||
137 | while (!eof) | ||
138 | { | ||
139 | r = read(fd, &buf[remaining], MAX_PATH - remaining); | ||
140 | |||
141 | if (r <= 0) | ||
142 | { | ||
143 | eof = true; | ||
144 | } | ||
145 | else | ||
146 | { | ||
147 | remaining += r; | ||
148 | } | ||
149 | |||
150 | /* Inefficient (but simple) search */ | ||
151 | i = 0; | ||
152 | |||
153 | while (i < (remaining - 3)) | ||
154 | { | ||
155 | if ((buf[i] == 'O') && (memcmp(&buf[i], "OggS", 4) == 0)) | ||
156 | { | ||
157 | if (i < (remaining - 17)) | ||
158 | { | ||
159 | /* Note that this only reads the low 32 bits of a | ||
160 | * 64 bit value. | ||
161 | */ | ||
162 | id3->samples = get_long_le(&buf[i + 6]); | ||
163 | last_serial = get_long_le(&buf[i + 14]); | ||
164 | |||
165 | /* If this page is very small the beginning of the next | ||
166 | * header could be in buffer. Jump near end of this header | ||
167 | * and continue */ | ||
168 | i += 27; | ||
169 | } | ||
170 | else | ||
171 | { | ||
172 | break; | ||
173 | } | ||
174 | } | ||
175 | else | ||
176 | { | ||
177 | i++; | ||
178 | } | ||
179 | } | ||
180 | |||
181 | if (i < remaining) | ||
182 | { | ||
183 | /* Move the remaining bytes to start of buffer. | ||
184 | * Reuse var 'segments' as it is no longer needed */ | ||
185 | segments = 0; | ||
186 | while (i < remaining) | ||
187 | { | ||
188 | buf[segments++] = buf[i++]; | ||
189 | } | ||
190 | remaining = segments; | ||
191 | } | ||
192 | else | ||
193 | { | ||
194 | /* Discard the rest of the buffer */ | ||
195 | remaining = 0; | ||
196 | } | ||
197 | } | ||
198 | |||
199 | /* This file has mutiple vorbis bitstreams (or is corrupt). */ | ||
200 | /* FIXME we should display an error here. */ | ||
201 | if (serial != last_serial) | ||
202 | { | ||
203 | logf("serialno mismatch"); | ||
204 | logf("%ld", serial); | ||
205 | logf("%ld", last_serial); | ||
206 | return false; | ||
207 | } | ||
208 | |||
209 | id3->length = (id3->samples / id3->frequency) * 1000; | ||
210 | id3->bitrate = (((int64_t) id3->filesize - comment_size) * 8) / id3->length; | ||
211 | return true; | ||
212 | } | ||
213 | |||
214 | /* A simple parser to read vital metadata from an Ogg Vorbis file. | ||
215 | * Calls get_speex_metadata if a speex file is identified. Returns | ||
216 | * false if metadata needed by the Vorbis codec couldn't be read. | ||
217 | */ | ||
218 | bool get_vorbis_metadata(int fd, struct mp3entry* id3) | ||
219 | { | ||
220 | /* An Ogg File is split into pages, each starting with the string | ||
221 | * "OggS". Each page has a timestamp (in PCM samples) referred to as | ||
222 | * the "granule position". | ||
223 | * | ||
224 | * An Ogg Vorbis has the following structure: | ||
225 | * 1) Identification header (containing samplerate, numchannels, etc) | ||
226 | * 2) Comment header - containing the Vorbis Comments | ||
227 | * 3) Setup header - containing codec setup information | ||
228 | * 4) Many audio packets... | ||
229 | */ | ||
230 | |||
231 | /* Use the path name of the id3 structure as a temporary buffer. */ | ||
232 | unsigned char* buf = (unsigned char *)id3->path; | ||
233 | long comment_size; | ||
234 | long remaining = 0; | ||
235 | long last_serial = 0; | ||
236 | long serial, r; | ||
237 | int segments; | ||
238 | int i; | ||
239 | bool eof = false; | ||
240 | |||
241 | if ((lseek(fd, 0, SEEK_SET) < 0) || (read(fd, buf, 58) < 4)) | ||
242 | { | ||
243 | return false; | ||
244 | } | ||
245 | |||
246 | if ((memcmp(buf, "OggS", 4) != 0) || (memcmp(&buf[29], "vorbis", 6) != 0)) | ||
247 | { | ||
248 | if ((memcmp(buf, "OggS", 4) != 0) || (memcmp(&buf[28], "Speex", 5) != 0)) | ||
249 | { | ||
250 | return false; | ||
251 | } | ||
252 | else | ||
253 | { | ||
254 | id3->codectype = AFMT_SPEEX; | ||
255 | return get_speex_metadata(fd, id3); | ||
256 | } | ||
257 | } | ||
258 | |||
259 | /* We need to ensure the serial number from this page is the same as the | ||
260 | * one from the last page (since we only support a single bitstream). | ||
261 | */ | ||
262 | serial = get_long_le(&buf[14]); | ||
263 | id3->frequency = get_long_le(&buf[40]); | ||
264 | id3->filesize = filesize(fd); | ||
265 | |||
266 | /* Comments are in second Ogg page */ | ||
267 | if (lseek(fd, 58, SEEK_SET) < 0) | ||
268 | { | ||
269 | return false; | ||
270 | } | ||
271 | |||
272 | /* Minimum header length for Ogg pages is 27. */ | ||
273 | if (read(fd, buf, 27) < 27) | ||
274 | { | ||
275 | return false; | ||
276 | } | ||
277 | |||
278 | if (memcmp(buf, "OggS", 4) !=0 ) | ||
279 | { | ||
280 | return false; | ||
281 | } | ||
282 | |||
283 | segments = buf[26]; | ||
284 | |||
285 | /* read in segment table */ | ||
286 | if (read(fd, buf, segments) < segments) | ||
287 | { | ||
288 | return false; | ||
289 | } | ||
290 | |||
291 | /* The second packet in a vorbis stream is the comment packet. It *may* | ||
292 | * extend beyond the second page, but usually does not. Here we find the | ||
293 | * length of the comment packet (or the rest of the page if the comment | ||
294 | * packet extends to the third page). | ||
295 | */ | ||
296 | for (i = 0; i < segments; i++) | ||
297 | { | ||
298 | remaining += buf[i]; | ||
299 | |||
300 | /* The last segment of a packet is always < 255 bytes */ | ||
301 | if (buf[i] < 255) | ||
302 | { | ||
303 | break; | ||
304 | } | ||
305 | } | ||
306 | |||
307 | /* Now read in packet header (type and id string) */ | ||
308 | if (read(fd, buf, 7) < 7) | ||
309 | { | ||
310 | return false; | ||
311 | } | ||
312 | |||
313 | comment_size = remaining; | ||
314 | remaining -= 7; | ||
315 | |||
316 | /* The first byte of a packet is the packet type; comment packets are | ||
317 | * type 3. | ||
318 | */ | ||
319 | if ((buf[0] != 3) || (memcmp(buf + 1, "vorbis", 6) !=0)) | ||
320 | { | ||
321 | return false; | ||
322 | } | ||
323 | |||
324 | /* Failure to read the tags isn't fatal. */ | ||
325 | read_vorbis_tags(fd, id3, remaining); | ||
326 | |||
327 | /* We now need to search for the last page in the file - identified by | ||
328 | * by ('O','g','g','S',0) and retrieve totalsamples. | ||
329 | */ | ||
330 | |||
331 | /* A page is always < 64 kB */ | ||
332 | if (lseek(fd, -(MIN(64 * 1024, id3->filesize)), SEEK_END) < 0) | ||
333 | { | ||
334 | return false; | ||
335 | } | ||
336 | |||
337 | remaining = 0; | ||
338 | |||
339 | while (!eof) | ||
340 | { | ||
341 | r = read(fd, &buf[remaining], MAX_PATH - remaining); | ||
342 | |||
343 | if (r <= 0) | ||
344 | { | ||
345 | eof = true; | ||
346 | } | ||
347 | else | ||
348 | { | ||
349 | remaining += r; | ||
350 | } | ||
351 | |||
352 | /* Inefficient (but simple) search */ | ||
353 | i = 0; | ||
354 | |||
355 | while (i < (remaining - 3)) | ||
356 | { | ||
357 | if ((buf[i] == 'O') && (memcmp(&buf[i], "OggS", 4) == 0)) | ||
358 | { | ||
359 | if (i < (remaining - 17)) | ||
360 | { | ||
361 | /* Note that this only reads the low 32 bits of a | ||
362 | * 64 bit value. | ||
363 | */ | ||
364 | id3->samples = get_long_le(&buf[i + 6]); | ||
365 | last_serial = get_long_le(&buf[i + 14]); | ||
366 | |||
367 | /* If this page is very small the beginning of the next | ||
368 | * header could be in buffer. Jump near end of this header | ||
369 | * and continue */ | ||
370 | i += 27; | ||
371 | } | ||
372 | else | ||
373 | { | ||
374 | break; | ||
375 | } | ||
376 | } | ||
377 | else | ||
378 | { | ||
379 | i++; | ||
380 | } | ||
381 | } | ||
382 | |||
383 | if (i < remaining) | ||
384 | { | ||
385 | /* Move the remaining bytes to start of buffer. | ||
386 | * Reuse var 'segments' as it is no longer needed */ | ||
387 | segments = 0; | ||
388 | while (i < remaining) | ||
389 | { | ||
390 | buf[segments++] = buf[i++]; | ||
391 | } | ||
392 | remaining = segments; | ||
393 | } | ||
394 | else | ||
395 | { | ||
396 | /* Discard the rest of the buffer */ | ||
397 | remaining = 0; | ||
398 | } | ||
399 | } | ||
400 | |||
401 | /* This file has mutiple vorbis bitstreams (or is corrupt). */ | ||
402 | /* FIXME we should display an error here. */ | ||
403 | if (serial != last_serial) | ||
404 | { | ||
405 | logf("serialno mismatch"); | ||
406 | logf("%ld", serial); | ||
407 | logf("%ld", last_serial); | ||
408 | return false; | ||
409 | } | ||
410 | |||
411 | id3->length = ((int64_t) id3->samples * 1000) / id3->frequency; | ||
412 | |||
413 | if (id3->length <= 0) | ||
414 | { | ||
415 | logf("ogg length invalid!"); | ||
416 | return false; | ||
417 | } | ||
418 | |||
419 | id3->bitrate = (((int64_t) id3->filesize - comment_size) * 8) / id3->length; | ||
420 | id3->vbr = true; | ||
421 | |||
422 | return true; | ||
423 | } | ||
424 | |||