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 Speex file. Returns
31 * false if metadata needed by the Speex codec couldn't be read.
34 bool get_speex_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 Speex has the following structure:
41 * 1) Identification header (containing samplerate, numchannels, etc)
42 Described in this page: (http://www.speex.org/manual2/node7.html)
43 * 2) Comment header - containing the Vorbis Comments
44 * 3) Many audio packets...
47 /* Use the path name of the id3 structure as a temporary buffer. */
48 unsigned char* buf
= (unsigned char*)id3
->path
;
57 if ((lseek(fd
, 0, SEEK_SET
) < 0) || (read(fd
, buf
, 58) < 33))
62 if ((memcmp(buf
, "OggS", 4) != 0) || (memcmp(&buf
[28], "Speex", 5) != 0))
67 /* We need to ensure the serial number from this page is the same as the
68 * one from the last page (since we only support a single bitstream).
70 serial
= get_long_le(&buf
[14]);
71 if ((lseek(fd
, 33, SEEK_SET
) < 0)||(read(fd
, buf
, 58) < 4))
76 id3
->frequency
= get_slong(&buf
[31]);
77 last_serial
= get_long_le(&buf
[27]);/*temporary, header size*/
78 id3
->bitrate
= get_long_le(&buf
[47]);
79 id3
->vbr
= get_long_le(&buf
[55]);
80 id3
->filesize
= filesize(fd
);
81 /* Comments are in second Ogg page */
82 if (lseek(fd
, 28+last_serial
/*(temporary for header size)*/, SEEK_SET
) < 0)
87 /* Minimum header length for Ogg pages is 27. */
88 if (read(fd
, buf
, 27) < 27)
93 if (memcmp(buf
, "OggS", 4) !=0 )
99 /* read in segment table */
100 if (read(fd
, buf
, segments
) < segments
)
105 /* The second packet in a vorbis stream is the comment packet. It *may*
106 * extend beyond the second page, but usually does not. Here we find the
107 * length of the comment packet (or the rest of the page if the comment
108 * packet extends to the third page).
110 for (i
= 0; i
< segments
; i
++)
113 /* The last segment of a packet is always < 255 bytes */
120 comment_size
= remaining
;
122 /* Failure to read the tags isn't fatal. */
123 read_vorbis_tags(fd
, id3
, remaining
);
125 /* We now need to search for the last page in the file - identified by
126 * by ('O','g','g','S',0) and retrieve totalsamples.
129 /* A page is always < 64 kB */
130 if (lseek(fd
, -(MIN(64 * 1024, id3
->filesize
)), SEEK_END
) < 0)
139 r
= read(fd
, &buf
[remaining
], MAX_PATH
- remaining
);
150 /* Inefficient (but simple) search */
153 while (i
< (remaining
- 3))
155 if ((buf
[i
] == 'O') && (memcmp(&buf
[i
], "OggS", 4) == 0))
157 if (i
< (remaining
- 17))
159 /* Note that this only reads the low 32 bits of a
162 id3
->samples
= get_long_le(&buf
[i
+ 6]);
163 last_serial
= get_long_le(&buf
[i
+ 14]);
165 /* If this page is very small the beginning of the next
166 * header could be in buffer. Jump near end of this header
183 /* Move the remaining bytes to start of buffer.
184 * Reuse var 'segments' as it is no longer needed */
186 while (i
< remaining
)
188 buf
[segments
++] = buf
[i
++];
190 remaining
= segments
;
194 /* Discard the rest of the buffer */
199 /* This file has mutiple vorbis bitstreams (or is corrupt). */
200 /* FIXME we should display an error here. */
201 if (serial
!= last_serial
)
203 logf("serialno mismatch");
205 logf("%ld", last_serial
);
209 id3
->length
= (id3
->samples
/ id3
->frequency
) * 1000;
210 id3
->bitrate
= (((int64_t) id3
->filesize
- comment_size
) * 8) / id3
->length
;