Fix Pacbox controls for D2 touchscreen
[Rockbox.git] / apps / metadata / ogg.c
blobd3692fd0a040a52582e470fbfa57d24a8c7516ab
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <inttypes.h>
25 #include "system.h"
26 #include "id3.h"
27 #include "metadata_common.h"
28 #include "metadata_parsers.h"
29 #include "logf.h"
31 /* A simple parser to read vital metadata from an Ogg Vorbis file.
32 * Can also handle parsing Ogg Speex files for metadata. Returns
33 * false if metadata needed by the codec couldn't be read.
35 bool get_ogg_metadata(int fd, struct mp3entry* id3)
37 /* An Ogg File is split into pages, each starting with the string
38 * "OggS". Each page has a timestamp (in PCM samples) referred to as
39 * the "granule position".
41 * An Ogg Vorbis has the following structure:
42 * 1) Identification header (containing samplerate, numchannels, etc)
43 * 2) Comment header - containing the Vorbis Comments
44 * 3) Setup header - containing codec setup information
45 * 4) Many audio packets...
47 * An Ogg Speex has the following structure:
48 * 1) Identification header (containing samplerate, numchannels, etc)
49 * Described in this page: (http://www.speex.org/manual2/node7.html)
50 * 2) Comment header - containing the Vorbis Comments
51 * 3) Many audio packets.
54 /* Use the path name of the id3 structure as a temporary buffer. */
55 unsigned char* buf = (unsigned char *)id3->path;
56 long comment_size;
57 long remaining = 0;
58 long last_serial = 0;
59 long serial, r;
60 int segments, header_size;
61 int i;
62 bool eof = false;
64 /* 92 bytes is enough for both Vorbis and Speex headers */
65 if ((lseek(fd, 0, SEEK_SET) < 0) || (read(fd, buf, 92) < 92))
67 return false;
70 /* All Ogg streams start with OggS */
71 if (memcmp(buf, "OggS", 4) != 0)
73 return false;
76 /* Check for format magic and then get metadata */
77 if (memcmp(&buf[29], "vorbis", 6) == 0)
79 id3->codectype = AFMT_OGG_VORBIS;
80 id3->frequency = get_long_le(&buf[40]);
81 id3->vbr = true;
83 /* Comments are in second Ogg page (byte 58 onwards for Vorbis) */
84 if (lseek(fd, 58, SEEK_SET) < 0)
86 return false;
89 else if (memcmp(&buf[28], "Speex ", 8) == 0)
91 id3->codectype = AFMT_SPEEX;
92 id3->frequency = get_slong(&buf[64]);
93 id3->vbr = get_long_le(&buf[88]);
95 header_size = get_long_le(&buf[60]);
97 /* Comments are in second Ogg page (byte 108 onwards for Speex) */
98 if (lseek(fd, 28 + header_size, SEEK_SET) < 0)
100 return false;
103 else
105 return false;
108 id3->filesize = filesize(fd);
110 /* We need to ensure the serial number from this page is the same as the
111 * one from the last page (since we only support a single bitstream).
113 serial = get_long_le(&buf[14]);
115 /* Minimum header length for Ogg pages is 27. */
116 if (read(fd, buf, 27) < 27)
118 return false;
121 if (memcmp(buf, "OggS", 4) !=0 )
123 return false;
126 segments = buf[26];
128 /* read in segment table */
129 if (read(fd, buf, segments) < segments)
131 return false;
134 /* The second packet in a vorbis stream is the comment packet. It *may*
135 * extend beyond the second page, but usually does not. Here we find the
136 * length of the comment packet (or the rest of the page if the comment
137 * packet extends to the third page).
139 for (i = 0; i < segments; i++)
141 remaining += buf[i];
143 /* The last segment of a packet is always < 255 bytes */
144 if (buf[i] < 255)
146 break;
150 comment_size = remaining;
152 if (id3->codectype == AFMT_OGG_VORBIS) {
153 /* Now read in packet header (type and id string) */
154 if (read(fd, buf, 7) < 7)
156 return false;
159 remaining -= 7;
161 /* The first byte of a packet is the packet type; comment packets are
162 * type 3.
164 if (buf[0] != 3)
166 return false;
170 /* Failure to read the tags isn't fatal. */
171 read_vorbis_tags(fd, id3, remaining);
173 /* We now need to search for the last page in the file - identified by
174 * by ('O','g','g','S',0) and retrieve totalsamples.
177 /* A page is always < 64 kB */
178 if (lseek(fd, -(MIN(64 * 1024, id3->filesize)), SEEK_END) < 0)
180 return false;
183 remaining = 0;
185 while (!eof)
187 r = read(fd, &buf[remaining], MAX_PATH - remaining);
189 if (r <= 0)
191 eof = true;
193 else
195 remaining += r;
198 /* Inefficient (but simple) search */
199 i = 0;
201 while (i < (remaining - 3))
203 if ((buf[i] == 'O') && (memcmp(&buf[i], "OggS", 4) == 0))
205 if (i < (remaining - 17))
207 /* Note that this only reads the low 32 bits of a
208 * 64 bit value.
210 id3->samples = get_long_le(&buf[i + 6]);
211 last_serial = get_long_le(&buf[i + 14]);
213 /* If this page is very small the beginning of the next
214 * header could be in buffer. Jump near end of this header
215 * and continue */
216 i += 27;
218 else
220 break;
223 else
225 i++;
229 if (i < remaining)
231 /* Move the remaining bytes to start of buffer.
232 * Reuse var 'segments' as it is no longer needed */
233 segments = 0;
234 while (i < remaining)
236 buf[segments++] = buf[i++];
238 remaining = segments;
240 else
242 /* Discard the rest of the buffer */
243 remaining = 0;
247 /* This file has mutiple vorbis bitstreams (or is corrupt). */
248 /* FIXME we should display an error here. */
249 if (serial != last_serial)
251 logf("serialno mismatch");
252 logf("%ld", serial);
253 logf("%ld", last_serial);
254 return false;
257 id3->length = ((int64_t) id3->samples * 1000) / id3->frequency;
258 if (id3->length <= 0)
260 logf("ogg length invalid!");
261 return false;
264 id3->bitrate = (((int64_t) id3->filesize - comment_size) * 8) / id3->length;
266 return true;