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"
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
;
59 int segments
, header_size
;
63 /* 92 bytes is enough for both Vorbis and Speex headers */
64 if ((lseek(fd
, 0, SEEK_SET
) < 0) || (read(fd
, buf
, 92) < 92))
69 /* All Ogg streams start with OggS */
70 if (memcmp(buf
, "OggS", 4) != 0)
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]);
82 /* Comments are in second Ogg page (byte 58 onwards for Vorbis) */
83 if (lseek(fd
, 58, SEEK_SET
) < 0)
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)
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)
120 if (memcmp(buf
, "OggS", 4) !=0 )
127 /* read in segment table */
128 if (read(fd
, buf
, segments
) < segments
)
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
++)
142 /* The last segment of a packet is always < 255 bytes */
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)
160 /* The first byte of a packet is the packet type; comment packets are
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)
186 r
= read(fd
, &buf
[remaining
], MAX_PATH
- remaining
);
197 /* Inefficient (but simple) search */
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
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
230 /* Move the remaining bytes to start of buffer.
231 * Reuse var 'segments' as it is no longer needed */
233 while (i
< remaining
)
235 buf
[segments
++] = buf
[i
++];
237 remaining
= segments
;
241 /* Discard the rest of the buffer */
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");
252 logf("%ld", last_serial
);
256 id3
->length
= ((int64_t) id3
->samples
* 1000) / id3
->frequency
;
257 if (id3
->length
<= 0)
259 logf("ogg length invalid!");
263 id3
->bitrate
= (((int64_t) id3
->filesize
- comment_size
) * 8) / id3
->length
;