1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
27 #include "metadata_common.h"
28 #include "metadata_parsers.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
;
60 int segments
, header_size
;
64 /* 92 bytes is enough for both Vorbis and Speex headers */
65 if ((lseek(fd
, 0, SEEK_SET
) < 0) || (read(fd
, buf
, 92) < 92))
70 /* All Ogg streams start with OggS */
71 if (memcmp(buf
, "OggS", 4) != 0)
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]);
83 /* Comments are in second Ogg page (byte 58 onwards for Vorbis) */
84 if (lseek(fd
, 58, SEEK_SET
) < 0)
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)
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)
121 if (memcmp(buf
, "OggS", 4) !=0 )
128 /* read in segment table */
129 if (read(fd
, buf
, segments
) < segments
)
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
++)
143 /* The last segment of a packet is always < 255 bytes */
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)
161 /* The first byte of a packet is the packet type; comment packets are
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)
187 r
= read(fd
, &buf
[remaining
], MAX_PATH
- remaining
);
198 /* Inefficient (but simple) search */
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
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
231 /* Move the remaining bytes to start of buffer.
232 * Reuse var 'segments' as it is no longer needed */
234 while (i
< remaining
)
236 buf
[segments
++] = buf
[i
++];
238 remaining
= segments
;
242 /* Discard the rest of the buffer */
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");
253 logf("%ld", last_serial
);
257 id3
->length
= ((int64_t) id3
->samples
* 1000) / id3
->frequency
;
258 if (id3
->length
<= 0)
260 logf("ogg length invalid!");
264 id3
->bitrate
= (((int64_t) id3
->filesize
- comment_size
) * 8) / id3
->length
;