Make the image tags examples nicer.
[Rockbox.git] / apps / metadata / ogg.c
blob9b604a11a4c3b832096ca9965efc37d0b1c1ad61
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 "logf.h"
30 /* A simple parser to read vital metadata from an Ogg Vorbis file.
31 * Can also handle parsing Ogg Speex files for metadata. Returns
32 * false if metadata needed by the codec couldn't be read.
34 bool get_ogg_metadata(int fd, struct mp3entry* id3)
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".
40 * An Ogg Vorbis has the following structure:
41 * 1) Identification header (containing samplerate, numchannels, etc)
42 * 2) Comment header - containing the Vorbis Comments
43 * 3) Setup header - containing codec setup information
44 * 4) Many audio packets...
46 * An Ogg Speex has the following structure:
47 * 1) Identification header (containing samplerate, numchannels, etc)
48 * Described in this page: (http://www.speex.org/manual2/node7.html)
49 * 2) Comment header - containing the Vorbis Comments
50 * 3) Many audio packets.
53 /* Use the path name of the id3 structure as a temporary buffer. */
54 unsigned char* buf = (unsigned char *)id3->path;
55 long comment_size;
56 long remaining = 0;
57 long last_serial = 0;
58 long serial, r;
59 int segments, header_size;
60 int i;
61 bool eof = false;
63 /* 92 bytes is enough for both Vorbis and Speex headers */
64 if ((lseek(fd, 0, SEEK_SET) < 0) || (read(fd, buf, 92) < 92))
66 return false;
69 /* All Ogg streams start with OggS */
70 if (memcmp(buf, "OggS", 4) != 0)
72 return false;
75 /* Check for format magic and then get metadata */
76 if (memcmp(&buf[29], "vorbis", 6) == 0)
78 id3->codectype = AFMT_OGG_VORBIS;
79 id3->frequency = get_long_le(&buf[40]);
80 id3->vbr = true;
82 /* Comments are in second Ogg page (byte 58 onwards for Vorbis) */
83 if (lseek(fd, 58, SEEK_SET) < 0)
85 return false;
88 else if (memcmp(&buf[28], "Speex ", 8) == 0)
90 id3->codectype = AFMT_SPEEX;
91 id3->frequency = get_slong(&buf[64]);
92 id3->vbr = get_long_le(&buf[88]);
94 header_size = get_long_le(&buf[60]);
96 /* Comments are in second Ogg page (byte 108 onwards for Speex) */
97 if (lseek(fd, 28 + header_size, SEEK_SET) < 0)
99 return false;
102 else
104 return false;
107 id3->filesize = filesize(fd);
109 /* We need to ensure the serial number from this page is the same as the
110 * one from the last page (since we only support a single bitstream).
112 serial = get_long_le(&buf[14]);
114 /* Minimum header length for Ogg pages is 27. */
115 if (read(fd, buf, 27) < 27)
117 return false;
120 if (memcmp(buf, "OggS", 4) !=0 )
122 return false;
125 segments = buf[26];
127 /* read in segment table */
128 if (read(fd, buf, segments) < segments)
130 return false;
133 /* The second packet in a vorbis stream is the comment packet. It *may*
134 * extend beyond the second page, but usually does not. Here we find the
135 * length of the comment packet (or the rest of the page if the comment
136 * packet extends to the third page).
138 for (i = 0; i < segments; i++)
140 remaining += buf[i];
142 /* The last segment of a packet is always < 255 bytes */
143 if (buf[i] < 255)
145 break;
149 comment_size = remaining;
151 if (id3->codectype == AFMT_OGG_VORBIS) {
152 /* Now read in packet header (type and id string) */
153 if (read(fd, buf, 7) < 7)
155 return false;
158 remaining -= 7;
160 /* The first byte of a packet is the packet type; comment packets are
161 * type 3.
163 if (buf[0] != 3)
165 return false;
169 /* Failure to read the tags isn't fatal. */
170 read_vorbis_tags(fd, id3, remaining);
172 /* We now need to search for the last page in the file - identified by
173 * by ('O','g','g','S',0) and retrieve totalsamples.
176 /* A page is always < 64 kB */
177 if (lseek(fd, -(MIN(64 * 1024, id3->filesize)), SEEK_END) < 0)
179 return false;
182 remaining = 0;
184 while (!eof)
186 r = read(fd, &buf[remaining], MAX_PATH - remaining);
188 if (r <= 0)
190 eof = true;
192 else
194 remaining += r;
197 /* Inefficient (but simple) search */
198 i = 0;
200 while (i < (remaining - 3))
202 if ((buf[i] == 'O') && (memcmp(&buf[i], "OggS", 4) == 0))
204 if (i < (remaining - 17))
206 /* Note that this only reads the low 32 bits of a
207 * 64 bit value.
209 id3->samples = get_long_le(&buf[i + 6]);
210 last_serial = get_long_le(&buf[i + 14]);
212 /* If this page is very small the beginning of the next
213 * header could be in buffer. Jump near end of this header
214 * and continue */
215 i += 27;
217 else
219 break;
222 else
224 i++;
228 if (i < remaining)
230 /* Move the remaining bytes to start of buffer.
231 * Reuse var 'segments' as it is no longer needed */
232 segments = 0;
233 while (i < remaining)
235 buf[segments++] = buf[i++];
237 remaining = segments;
239 else
241 /* Discard the rest of the buffer */
242 remaining = 0;
246 /* This file has mutiple vorbis bitstreams (or is corrupt). */
247 /* FIXME we should display an error here. */
248 if (serial != last_serial)
250 logf("serialno mismatch");
251 logf("%ld", serial);
252 logf("%ld", last_serial);
253 return false;
256 id3->length = ((int64_t) id3->samples * 1000) / id3->frequency;
257 if (id3->length <= 0)
259 logf("ogg length invalid!");
260 return false;
263 id3->bitrate = (((int64_t) id3->filesize - comment_size) * 8) / id3->length;
265 return true;