summaryrefslogtreecommitdiff
path: root/apps/metadata/vorbis.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/metadata/vorbis.c')
-rw-r--r--apps/metadata/vorbis.c212
1 files changed, 0 insertions, 212 deletions
diff --git a/apps/metadata/vorbis.c b/apps/metadata/vorbis.c
index 5112615e47..a93d271ad0 100644
--- a/apps/metadata/vorbis.c
+++ b/apps/metadata/vorbis.c
@@ -116,215 +116,3 @@ bool read_vorbis_tags(int fd, struct mp3entry *id3,
116 116
117 return true; 117 return true;
118} 118}
119
120/* A simple parser to read vital metadata from an Ogg Vorbis file.
121 * Calls get_speex_metadata if a speex file is identified. Returns
122 * false if metadata needed by the Vorbis codec couldn't be read.
123 */
124bool get_vorbis_metadata(int fd, struct mp3entry* id3)
125{
126 /* An Ogg File is split into pages, each starting with the string
127 * "OggS". Each page has a timestamp (in PCM samples) referred to as
128 * the "granule position".
129 *
130 * An Ogg Vorbis has the following structure:
131 * 1) Identification header (containing samplerate, numchannels, etc)
132 * 2) Comment header - containing the Vorbis Comments
133 * 3) Setup header - containing codec setup information
134 * 4) Many audio packets...
135 */
136
137 /* Use the path name of the id3 structure as a temporary buffer. */
138 unsigned char* buf = (unsigned char *)id3->path;
139 long comment_size;
140 long remaining = 0;
141 long last_serial = 0;
142 long serial, r;
143 int segments;
144 int i;
145 bool eof = false;
146
147 if ((lseek(fd, 0, SEEK_SET) < 0) || (read(fd, buf, 58) < 4))
148 {
149 return false;
150 }
151
152 if ((memcmp(buf, "OggS", 4) != 0) || (memcmp(&buf[29], "vorbis", 6) != 0))
153 {
154 if ((memcmp(buf, "OggS", 4) != 0) || (memcmp(&buf[28], "Speex", 5) != 0))
155 {
156 return false;
157 }
158 else
159 {
160 id3->codectype = AFMT_SPEEX;
161 return get_speex_metadata(fd, id3);
162 }
163 }
164
165 /* We need to ensure the serial number from this page is the same as the
166 * one from the last page (since we only support a single bitstream).
167 */
168 serial = get_long_le(&buf[14]);
169 id3->frequency = get_long_le(&buf[40]);
170 id3->filesize = filesize(fd);
171
172 /* Comments are in second Ogg page */
173 if (lseek(fd, 58, SEEK_SET) < 0)
174 {
175 return false;
176 }
177
178 /* Minimum header length for Ogg pages is 27. */
179 if (read(fd, buf, 27) < 27)
180 {
181 return false;
182 }
183
184 if (memcmp(buf, "OggS", 4) !=0 )
185 {
186 return false;
187 }
188
189 segments = buf[26];
190
191 /* read in segment table */
192 if (read(fd, buf, segments) < segments)
193 {
194 return false;
195 }
196
197 /* The second packet in a vorbis stream is the comment packet. It *may*
198 * extend beyond the second page, but usually does not. Here we find the
199 * length of the comment packet (or the rest of the page if the comment
200 * packet extends to the third page).
201 */
202 for (i = 0; i < segments; i++)
203 {
204 remaining += buf[i];
205
206 /* The last segment of a packet is always < 255 bytes */
207 if (buf[i] < 255)
208 {
209 break;
210 }
211 }
212
213 /* Now read in packet header (type and id string) */
214 if (read(fd, buf, 7) < 7)
215 {
216 return false;
217 }
218
219 comment_size = remaining;
220 remaining -= 7;
221
222 /* The first byte of a packet is the packet type; comment packets are
223 * type 3.
224 */
225 if ((buf[0] != 3) || (memcmp(buf + 1, "vorbis", 6) !=0))
226 {
227 return false;
228 }
229
230 /* Failure to read the tags isn't fatal. */
231 read_vorbis_tags(fd, id3, remaining);
232
233 /* We now need to search for the last page in the file - identified by
234 * by ('O','g','g','S',0) and retrieve totalsamples.
235 */
236
237 /* A page is always < 64 kB */
238 if (lseek(fd, -(MIN(64 * 1024, id3->filesize)), SEEK_END) < 0)
239 {
240 return false;
241 }
242
243 remaining = 0;
244
245 while (!eof)
246 {
247 r = read(fd, &buf[remaining], MAX_PATH - remaining);
248
249 if (r <= 0)
250 {
251 eof = true;
252 }
253 else
254 {
255 remaining += r;
256 }
257
258 /* Inefficient (but simple) search */
259 i = 0;
260
261 while (i < (remaining - 3))
262 {
263 if ((buf[i] == 'O') && (memcmp(&buf[i], "OggS", 4) == 0))
264 {
265 if (i < (remaining - 17))
266 {
267 /* Note that this only reads the low 32 bits of a
268 * 64 bit value.
269 */
270 id3->samples = get_long_le(&buf[i + 6]);
271 last_serial = get_long_le(&buf[i + 14]);
272
273 /* If this page is very small the beginning of the next
274 * header could be in buffer. Jump near end of this header
275 * and continue */
276 i += 27;
277 }
278 else
279 {
280 break;
281 }
282 }
283 else
284 {
285 i++;
286 }
287 }
288
289 if (i < remaining)
290 {
291 /* Move the remaining bytes to start of buffer.
292 * Reuse var 'segments' as it is no longer needed */
293 segments = 0;
294 while (i < remaining)
295 {
296 buf[segments++] = buf[i++];
297 }
298 remaining = segments;
299 }
300 else
301 {
302 /* Discard the rest of the buffer */
303 remaining = 0;
304 }
305 }
306
307 /* This file has mutiple vorbis bitstreams (or is corrupt). */
308 /* FIXME we should display an error here. */
309 if (serial != last_serial)
310 {
311 logf("serialno mismatch");
312 logf("%ld", serial);
313 logf("%ld", last_serial);
314 return false;
315 }
316
317 id3->length = ((int64_t) id3->samples * 1000) / id3->frequency;
318
319 if (id3->length <= 0)
320 {
321 logf("ogg length invalid!");
322 return false;
323 }
324
325 id3->bitrate = (((int64_t) id3->filesize - comment_size) * 8) / id3->length;
326 id3->vbr = true;
327
328 return true;
329}
330