LCD driver for meizu M3, new type (older M3s are not yet supported)
[kugel-rb.git] / apps / metadata / ogg.c
blobedb55f53a3102932935cad2f026488563b30f6c0
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <ctype.h>
25 #include <inttypes.h>
27 #include "system.h"
28 #include "id3.h"
29 #include "metadata_common.h"
30 #include "metadata_parsers.h"
31 #include "logf.h"
33 /* A simple parser to read vital metadata from an Ogg Vorbis file.
34 * Can also handle parsing Ogg Speex files for metadata. Returns
35 * false if metadata needed by the codec couldn't be read.
37 bool get_ogg_metadata(int fd, struct mp3entry* id3)
39 /* An Ogg File is split into pages, each starting with the string
40 * "OggS". Each page has a timestamp (in PCM samples) referred to as
41 * the "granule position".
43 * An Ogg Vorbis has the following structure:
44 * 1) Identification header (containing samplerate, numchannels, etc)
45 * 2) Comment header - containing the Vorbis Comments
46 * 3) Setup header - containing codec setup information
47 * 4) Many audio packets...
49 * An Ogg Speex has the following structure:
50 * 1) Identification header (containing samplerate, numchannels, etc)
51 * Described in this page: (http://www.speex.org/manual2/node7.html)
52 * 2) Comment header - containing the Vorbis Comments
53 * 3) Many audio packets.
56 /* Use the path name of the id3 structure as a temporary buffer. */
57 unsigned char* buf = (unsigned char *)id3->path;
58 long comment_size;
59 long remaining = 0;
60 long last_serial = 0;
61 long serial, r;
62 int segments, header_size;
63 int i;
64 bool eof = false;
66 /* 92 bytes is enough for both Vorbis and Speex headers */
67 if ((lseek(fd, 0, SEEK_SET) < 0) || (read(fd, buf, 92) < 92))
69 return false;
72 /* All Ogg streams start with OggS */
73 if (memcmp(buf, "OggS", 4) != 0)
75 return false;
78 /* Check for format magic and then get metadata */
79 if (memcmp(&buf[29], "vorbis", 6) == 0)
81 id3->codectype = AFMT_OGG_VORBIS;
82 id3->frequency = get_long_le(&buf[40]);
83 id3->vbr = true;
85 /* Comments are in second Ogg page (byte 58 onwards for Vorbis) */
86 if (lseek(fd, 58, SEEK_SET) < 0)
88 return false;
91 else if (memcmp(&buf[28], "Speex ", 8) == 0)
93 id3->codectype = AFMT_SPEEX;
94 id3->frequency = get_slong(&buf[64]);
95 id3->vbr = get_long_le(&buf[88]);
97 header_size = get_long_le(&buf[60]);
99 /* Comments are in second Ogg page (byte 108 onwards for Speex) */
100 if (lseek(fd, 28 + header_size, SEEK_SET) < 0)
102 return false;
105 else
107 /* Unsupported format, try to print the marker, catches Ogg/FLAC at least */
108 DEBUGF("Usupported format in Ogg stream: %16s\n", &buf[28]);
109 return false;
112 id3->filesize = filesize(fd);
114 /* We need to ensure the serial number from this page is the same as the
115 * one from the last page (since we only support a single bitstream).
117 serial = get_long_le(&buf[14]);
119 /* Minimum header length for Ogg pages is 27. */
120 if (read(fd, buf, 27) < 27)
122 return false;
125 if (memcmp(buf, "OggS", 4) !=0 )
127 return false;
130 segments = buf[26];
132 /* read in segment table */
133 if (read(fd, buf, segments) < segments)
135 return false;
138 /* The second packet in a vorbis stream is the comment packet. It *may*
139 * extend beyond the second page, but usually does not. Here we find the
140 * length of the comment packet (or the rest of the page if the comment
141 * packet extends to the third page).
143 for (i = 0; i < segments; i++)
145 remaining += buf[i];
147 /* The last segment of a packet is always < 255 bytes */
148 if (buf[i] < 255)
150 break;
154 comment_size = remaining;
156 if (id3->codectype == AFMT_OGG_VORBIS) {
157 /* Now read in packet header (type and id string) */
158 if (read(fd, buf, 7) < 7)
160 return false;
163 remaining -= 7;
165 /* The first byte of a packet is the packet type; comment packets are
166 * type 3.
168 if (buf[0] != 3)
170 return false;
174 /* Failure to read the tags isn't fatal. */
175 read_vorbis_tags(fd, id3, remaining);
177 /* We now need to search for the last page in the file - identified by
178 * by ('O','g','g','S',0) and retrieve totalsamples.
181 /* A page is always < 64 kB */
182 if (lseek(fd, -(MIN(64 * 1024, id3->filesize)), SEEK_END) < 0)
184 return false;
187 remaining = 0;
189 while (!eof)
191 r = read(fd, &buf[remaining], MAX_PATH - remaining);
193 if (r <= 0)
195 eof = true;
197 else
199 remaining += r;
202 /* Inefficient (but simple) search */
203 i = 0;
205 while (i < (remaining - 3))
207 if ((buf[i] == 'O') && (memcmp(&buf[i], "OggS", 4) == 0))
209 if (i < (remaining - 17))
211 /* Note that this only reads the low 32 bits of a
212 * 64 bit value.
214 id3->samples = get_long_le(&buf[i + 6]);
215 last_serial = get_long_le(&buf[i + 14]);
217 /* If this page is very small the beginning of the next
218 * header could be in buffer. Jump near end of this header
219 * and continue */
220 i += 27;
222 else
224 break;
227 else
229 i++;
233 if (i < remaining)
235 /* Move the remaining bytes to start of buffer.
236 * Reuse var 'segments' as it is no longer needed */
237 segments = 0;
238 while (i < remaining)
240 buf[segments++] = buf[i++];
242 remaining = segments;
244 else
246 /* Discard the rest of the buffer */
247 remaining = 0;
251 /* This file has mutiple vorbis bitstreams (or is corrupt). */
252 /* FIXME we should display an error here. */
253 if (serial != last_serial)
255 logf("serialno mismatch");
256 logf("%ld", serial);
257 logf("%ld", last_serial);
258 return false;
261 id3->length = ((int64_t) id3->samples * 1000) / id3->frequency;
262 if (id3->length <= 0)
264 logf("ogg length invalid!");
265 return false;
268 id3->bitrate = (((int64_t) id3->filesize - comment_size) * 8) / id3->length;
270 return true;