diff options
-rw-r--r-- | apps/SOURCES | 2 | ||||
-rw-r--r-- | apps/metadata/ogg.c (renamed from apps/metadata/speex.c) | 212 | ||||
-rw-r--r-- | apps/metadata/vorbis.c | 212 |
3 files changed, 213 insertions, 213 deletions
diff --git a/apps/SOURCES b/apps/SOURCES index a48017c7cd..8c3f27d1f0 100644 --- a/apps/SOURCES +++ b/apps/SOURCES | |||
@@ -115,9 +115,9 @@ metadata/flac.c | |||
115 | metadata/monkeys.c | 115 | metadata/monkeys.c |
116 | metadata/mp4.c | 116 | metadata/mp4.c |
117 | metadata/mpc.c | 117 | metadata/mpc.c |
118 | metadata/ogg.c | ||
118 | metadata/sid.c | 119 | metadata/sid.c |
119 | metadata/spc.c | 120 | metadata/spc.c |
120 | metadata/speex.c | ||
121 | metadata/vorbis.c | 121 | metadata/vorbis.c |
122 | metadata/wave.c | 122 | metadata/wave.c |
123 | #endif | 123 | #endif |
diff --git a/apps/metadata/speex.c b/apps/metadata/ogg.c index e5f6a127ae..a109694e9d 100644 --- a/apps/metadata/speex.c +++ b/apps/metadata/ogg.c | |||
@@ -210,3 +210,215 @@ bool get_speex_metadata(int fd, struct mp3entry* id3) | |||
210 | id3->bitrate = (((int64_t) id3->filesize - comment_size) * 8) / id3->length; | 210 | id3->bitrate = (((int64_t) id3->filesize - comment_size) * 8) / id3->length; |
211 | return true; | 211 | return true; |
212 | } | 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 | |||
diff --git a/apps/metadata/vorbis.c b/apps/metadata/vorbis.c index 5112615e47..a93d271ad0 100644 --- a/apps/metadata/vorbis.c +++ b/apps/metadata/vorbis.c | |||
@@ -116,215 +116,3 @@ bool read_vorbis_tags(int fd, struct mp3entry *id3, | |||
116 | 116 | ||
117 | return true; | 117 | return true; |
118 | } | 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 | |||