| 1 | /******************************************************************** |
| 2 | * * |
| 3 | * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE. * |
| 4 | * * |
| 5 | * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * |
| 6 | * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * |
| 7 | * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * |
| 8 | * * |
| 9 | * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2009 * |
| 10 | * BY THE Xiph.Org FOUNDATION http://www.xiph.org/ * |
| 11 | * * |
| 12 | ******************************************************************** |
| 13 | |
| 14 | function: stdio-based convenience library for opening/seeking/decoding |
| 15 | last mod: $Id: vorbisfile.c,v 1.6 2003/03/30 23:40:56 xiphmont Exp $ |
| 16 | |
| 17 | ********************************************************************/ |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <stdio.h> |
| 21 | #include <errno.h> |
| 22 | #include <string.h> |
| 23 | #include <math.h> |
| 24 | |
| 25 | #include "ivorbiscodec.h" |
| 26 | #include "ivorbisfile.h" |
| 27 | |
| 28 | #include "os.h" |
| 29 | #include "misc.h" |
| 30 | |
| 31 | /* A 'chained bitstream' is a Vorbis bitstream that contains more than |
| 32 | one logical bitstream arranged end to end (the only form of Ogg |
| 33 | multiplexing allowed in a Vorbis bitstream; grouping [parallel |
| 34 | multiplexing] is not allowed in Vorbis) */ |
| 35 | |
| 36 | /* A Vorbis file can be played beginning to end (streamed) without |
| 37 | worrying ahead of time about chaining (see decoder_example.c). If |
| 38 | we have the whole file, however, and want random access |
| 39 | (seeking/scrubbing) or desire to know the total length/time of a |
| 40 | file, we need to account for the possibility of chaining. */ |
| 41 | |
| 42 | /* We can handle things a number of ways; we can determine the entire |
| 43 | bitstream structure right off the bat, or find pieces on demand. |
| 44 | This example determines and caches structure for the entire |
| 45 | bitstream, but builds a virtual decoder on the fly when moving |
| 46 | between links in the chain. */ |
| 47 | |
| 48 | /* There are also different ways to implement seeking. Enough |
| 49 | information exists in an Ogg bitstream to seek to |
| 50 | sample-granularity positions in the output. Or, one can seek by |
| 51 | picking some portion of the stream roughly in the desired area if |
| 52 | we only want coarse navigation through the stream. */ |
| 53 | |
| 54 | /************************************************************************* |
| 55 | * Many, many internal helpers. The intention is not to be confusing; |
| 56 | * rampant duplication and monolithic function implementation would be |
| 57 | * harder to understand anyway. The high level functions are last. Begin |
| 58 | * grokking near the end of the file */ |
| 59 | |
| 60 | |
| 61 | /* read a little more data from the file/pipe into the ogg_sync framer */ |
| 62 | static long _get_data(OggVorbis_File *vf){ |
| 63 | errno=0; |
| 64 | if(!(vf->callbacks.read_func))return(-1); |
| 65 | if(vf->datasource){ |
| 66 | char *buffer=ogg_sync_buffer(&vf->oy,CHUNKSIZE); |
| 67 | long bytes=(vf->callbacks.read_func)(buffer,1,CHUNKSIZE,vf->datasource); |
| 68 | if(bytes>0)ogg_sync_wrote(&vf->oy,bytes); |
| 69 | #ifdef GEKKO |
| 70 | // It seems that EOVERFLOW is set whenever |
| 71 | // we read to the end of the file on WII... |
| 72 | // Fix copied from WiiMednafen code. |
| 73 | if(bytes==0 && errno && errno != EOVERFLOW) |
| 74 | #else |
| 75 | if(bytes==0 && errno) |
| 76 | #endif |
| 77 | { |
| 78 | return(-1); |
| 79 | } |
| 80 | return(bytes); |
| 81 | }else |
| 82 | return(0); |
| 83 | } |
| 84 | |
| 85 | /* save a tiny smidge of verbosity to make the code more readable */ |
| 86 | static int _seek_helper(OggVorbis_File *vf,ogg_int64_t offset){ |
| 87 | if(vf->datasource){ |
| 88 | if(!(vf->callbacks.seek_func)|| |
| 89 | (vf->callbacks.seek_func)(vf->datasource, offset, SEEK_SET) == -1) |
| 90 | return OV_EREAD; |
| 91 | vf->offset=offset; |
| 92 | ogg_sync_reset(&vf->oy); |
| 93 | }else{ |
| 94 | /* shouldn't happen unless someone writes a broken callback */ |
| 95 | return OV_EFAULT; |
| 96 | } |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | /* The read/seek functions track absolute position within the stream */ |
| 101 | |
| 102 | /* from the head of the stream, get the next page. boundary specifies |
| 103 | if the function is allowed to fetch more data from the stream (and |
| 104 | how much) or only use internally buffered data. |
| 105 | |
| 106 | boundary: -1) unbounded search |
| 107 | 0) read no additional data; use cached only |
| 108 | n) search for a new page beginning for n bytes |
| 109 | |
| 110 | return: <0) did not find a page (OV_FALSE, OV_EOF, OV_EREAD) |
| 111 | n) found a page at absolute offset n */ |
| 112 | |
| 113 | static ogg_int64_t _get_next_page(OggVorbis_File *vf,ogg_page *og, |
| 114 | ogg_int64_t boundary){ |
| 115 | if(boundary>0)boundary+=vf->offset; |
| 116 | while(1){ |
| 117 | long more; |
| 118 | |
| 119 | if(boundary>0 && vf->offset>=boundary)return(OV_FALSE); |
| 120 | more=ogg_sync_pageseek(&vf->oy,og); |
| 121 | |
| 122 | if(more<0){ |
| 123 | /* skipped n bytes */ |
| 124 | vf->offset-=more; |
| 125 | }else{ |
| 126 | if(more==0){ |
| 127 | /* send more paramedics */ |
| 128 | if(!boundary)return(OV_FALSE); |
| 129 | { |
| 130 | long ret=_get_data(vf); |
| 131 | if(ret==0)return(OV_EOF); |
| 132 | if(ret<0)return(OV_EREAD); |
| 133 | } |
| 134 | }else{ |
| 135 | /* got a page. Return the offset at the page beginning, |
| 136 | advance the internal offset past the page end */ |
| 137 | ogg_int64_t ret=vf->offset; |
| 138 | vf->offset+=more; |
| 139 | return(ret); |
| 140 | |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /* find the latest page beginning before the current stream cursor |
| 147 | position. Much dirtier than the above as Ogg doesn't have any |
| 148 | backward search linkage. no 'readp' as it will certainly have to |
| 149 | read. */ |
| 150 | /* returns offset or OV_EREAD, OV_FAULT */ |
| 151 | static ogg_int64_t _get_prev_page(OggVorbis_File *vf,ogg_page *og){ |
| 152 | ogg_int64_t begin=vf->offset; |
| 153 | ogg_int64_t end=begin; |
| 154 | ogg_int64_t ret; |
| 155 | ogg_int64_t offset=-1; |
| 156 | |
| 157 | while(offset==-1){ |
| 158 | begin-=CHUNKSIZE; |
| 159 | if(begin<0) |
| 160 | begin=0; |
| 161 | |
| 162 | ret=_seek_helper(vf,begin); |
| 163 | if(ret)return(ret); |
| 164 | |
| 165 | while(vf->offset<end){ |
| 166 | memset(og,0,sizeof(*og)); |
| 167 | ret=_get_next_page(vf,og,end-vf->offset); |
| 168 | if(ret==OV_EREAD)return(OV_EREAD); |
| 169 | if(ret<0){ |
| 170 | break; |
| 171 | }else{ |
| 172 | offset=ret; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /* In a fully compliant, non-multiplexed stream, we'll still be |
| 178 | holding the last page. In multiplexed (or noncompliant streams), |
| 179 | we will probably have to re-read the last page we saw */ |
| 180 | if(og->header_len==0){ |
| 181 | ret=_seek_helper(vf,offset); |
| 182 | if(ret)return(ret); |
| 183 | |
| 184 | ret=_get_next_page(vf,og,CHUNKSIZE); |
| 185 | if(ret<0) |
| 186 | /* this shouldn't be possible */ |
| 187 | return(OV_EFAULT); |
| 188 | } |
| 189 | |
| 190 | return(offset); |
| 191 | } |
| 192 | |
| 193 | static void _add_serialno(ogg_page *og,ogg_uint32_t **serialno_list, int *n){ |
| 194 | ogg_uint32_t s = ogg_page_serialno(og); |
| 195 | (*n)++; |
| 196 | |
| 197 | if(*serialno_list){ |
| 198 | *serialno_list = _ogg_realloc(*serialno_list, sizeof(**serialno_list)*(*n)); |
| 199 | }else{ |
| 200 | *serialno_list = _ogg_malloc(sizeof(**serialno_list)); |
| 201 | } |
| 202 | |
| 203 | (*serialno_list)[(*n)-1] = s; |
| 204 | } |
| 205 | |
| 206 | /* returns nonzero if found */ |
| 207 | static int _lookup_serialno(ogg_uint32_t s, ogg_uint32_t *serialno_list, int n){ |
| 208 | if(serialno_list){ |
| 209 | while(n--){ |
| 210 | if(*serialno_list == s) return 1; |
| 211 | serialno_list++; |
| 212 | } |
| 213 | } |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static int _lookup_page_serialno(ogg_page *og, ogg_uint32_t *serialno_list, int n){ |
| 218 | ogg_uint32_t s = ogg_page_serialno(og); |
| 219 | return _lookup_serialno(s,serialno_list,n); |
| 220 | } |
| 221 | |
| 222 | /* performs the same search as _get_prev_page, but prefers pages of |
| 223 | the specified serial number. If a page of the specified serialno is |
| 224 | spotted during the seek-back-and-read-forward, it will return the |
| 225 | info of last page of the matching serial number instead of the very |
| 226 | last page. If no page of the specified serialno is seen, it will |
| 227 | return the info of last page and alter *serialno. */ |
| 228 | static ogg_int64_t _get_prev_page_serial(OggVorbis_File *vf, |
| 229 | ogg_uint32_t *serial_list, int serial_n, |
| 230 | int *serialno, ogg_int64_t *granpos){ |
| 231 | ogg_page og; |
| 232 | ogg_int64_t begin=vf->offset; |
| 233 | ogg_int64_t end=begin; |
| 234 | ogg_int64_t ret; |
| 235 | |
| 236 | ogg_int64_t prefoffset=-1; |
| 237 | ogg_int64_t offset=-1; |
| 238 | ogg_int64_t ret_serialno=-1; |
| 239 | ogg_int64_t ret_gran=-1; |
| 240 | |
| 241 | while(offset==-1){ |
| 242 | begin-=CHUNKSIZE; |
| 243 | if(begin<0) |
| 244 | begin=0; |
| 245 | |
| 246 | ret=_seek_helper(vf,begin); |
| 247 | if(ret)return(ret); |
| 248 | |
| 249 | while(vf->offset<end){ |
| 250 | ret=_get_next_page(vf,&og,end-vf->offset); |
| 251 | if(ret==OV_EREAD)return(OV_EREAD); |
| 252 | if(ret<0){ |
| 253 | break; |
| 254 | }else{ |
| 255 | ret_serialno=ogg_page_serialno(&og); |
| 256 | ret_gran=ogg_page_granulepos(&og); |
| 257 | offset=ret; |
| 258 | |
| 259 | if((ogg_uint32_t)ret_serialno == *serialno){ |
| 260 | prefoffset=ret; |
| 261 | *granpos=ret_gran; |
| 262 | } |
| 263 | |
| 264 | if(!_lookup_serialno((ogg_uint32_t)ret_serialno,serial_list,serial_n)){ |
| 265 | /* we fell off the end of the link, which means we seeked |
| 266 | back too far and shouldn't have been looking in that link |
| 267 | to begin with. If we found the preferred serial number, |
| 268 | forget that we saw it. */ |
| 269 | prefoffset=-1; |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /* we're not interested in the page... just the serialno and granpos. */ |
| 276 | if(prefoffset>=0)return(prefoffset); |
| 277 | |
| 278 | *serialno = ret_serialno; |
| 279 | *granpos = ret_gran; |
| 280 | return(offset); |
| 281 | |
| 282 | } |
| 283 | |
| 284 | /* uses the local ogg_stream storage in vf; this is important for |
| 285 | non-streaming input sources */ |
| 286 | static int _fetch_headers(OggVorbis_File *vf,vorbis_info *vi,vorbis_comment *vc, |
| 287 | ogg_uint32_t **serialno_list, int *serialno_n, |
| 288 | ogg_page *og_ptr){ |
| 289 | ogg_page og; |
| 290 | ogg_packet op; |
| 291 | int i,ret; |
| 292 | int allbos=0; |
| 293 | |
| 294 | if(!og_ptr){ |
| 295 | ogg_int64_t llret=_get_next_page(vf,&og,CHUNKSIZE); |
| 296 | if(llret==OV_EREAD)return(OV_EREAD); |
| 297 | if(llret<0)return(OV_ENOTVORBIS); |
| 298 | og_ptr=&og; |
| 299 | } |
| 300 | |
| 301 | vorbis_info_init(vi); |
| 302 | vorbis_comment_init(vc); |
| 303 | vf->ready_state=OPENED; |
| 304 | |
| 305 | /* extract the serialnos of all BOS pages + the first set of vorbis |
| 306 | headers we see in the link */ |
| 307 | |
| 308 | while(ogg_page_bos(og_ptr)){ |
| 309 | if(serialno_list){ |
| 310 | if(_lookup_page_serialno(og_ptr,*serialno_list,*serialno_n)){ |
| 311 | /* a dupe serialnumber in an initial header packet set == invalid stream */ |
| 312 | if(*serialno_list)_ogg_free(*serialno_list); |
| 313 | *serialno_list=0; |
| 314 | *serialno_n=0; |
| 315 | ret=OV_EBADHEADER; |
| 316 | goto bail_header; |
| 317 | } |
| 318 | |
| 319 | _add_serialno(og_ptr,serialno_list,serialno_n); |
| 320 | } |
| 321 | |
| 322 | if(vf->ready_state<STREAMSET){ |
| 323 | /* we don't have a vorbis stream in this link yet, so begin |
| 324 | prospective stream setup. We need a stream to get packets */ |
| 325 | ogg_stream_reset_serialno(&vf->os,ogg_page_serialno(og_ptr)); |
| 326 | ogg_stream_pagein(&vf->os,og_ptr); |
| 327 | |
| 328 | if(ogg_stream_packetout(&vf->os,&op) > 0 && |
| 329 | vorbis_synthesis_idheader(&op)){ |
| 330 | /* vorbis header; continue setup */ |
| 331 | vf->ready_state=STREAMSET; |
| 332 | if((ret=vorbis_synthesis_headerin(vi,vc,&op))){ |
| 333 | ret=OV_EBADHEADER; |
| 334 | goto bail_header; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /* get next page */ |
| 340 | { |
| 341 | ogg_int64_t llret=_get_next_page(vf,og_ptr,CHUNKSIZE); |
| 342 | if(llret==OV_EREAD){ |
| 343 | ret=OV_EREAD; |
| 344 | goto bail_header; |
| 345 | } |
| 346 | if(llret<0){ |
| 347 | ret=OV_ENOTVORBIS; |
| 348 | goto bail_header; |
| 349 | } |
| 350 | |
| 351 | /* if this page also belongs to our vorbis stream, submit it and break */ |
| 352 | if(vf->ready_state==STREAMSET && |
| 353 | vf->os.serialno == ogg_page_serialno(og_ptr)){ |
| 354 | ogg_stream_pagein(&vf->os,og_ptr); |
| 355 | break; |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | if(vf->ready_state!=STREAMSET){ |
| 361 | ret = OV_ENOTVORBIS; |
| 362 | goto bail_header; |
| 363 | } |
| 364 | |
| 365 | while(1){ |
| 366 | |
| 367 | i=0; |
| 368 | while(i<2){ /* get a page loop */ |
| 369 | |
| 370 | while(i<2){ /* get a packet loop */ |
| 371 | |
| 372 | int result=ogg_stream_packetout(&vf->os,&op); |
| 373 | if(result==0)break; |
| 374 | if(result==-1){ |
| 375 | ret=OV_EBADHEADER; |
| 376 | goto bail_header; |
| 377 | } |
| 378 | |
| 379 | if((ret=vorbis_synthesis_headerin(vi,vc,&op))) |
| 380 | goto bail_header; |
| 381 | |
| 382 | i++; |
| 383 | } |
| 384 | |
| 385 | while(i<2){ |
| 386 | if(_get_next_page(vf,og_ptr,CHUNKSIZE)<0){ |
| 387 | ret=OV_EBADHEADER; |
| 388 | goto bail_header; |
| 389 | } |
| 390 | |
| 391 | /* if this page belongs to the correct stream, go parse it */ |
| 392 | if(vf->os.serialno == ogg_page_serialno(og_ptr)){ |
| 393 | ogg_stream_pagein(&vf->os,og_ptr); |
| 394 | break; |
| 395 | } |
| 396 | |
| 397 | /* if we never see the final vorbis headers before the link |
| 398 | ends, abort */ |
| 399 | if(ogg_page_bos(og_ptr)){ |
| 400 | if(allbos){ |
| 401 | ret = OV_EBADHEADER; |
| 402 | goto bail_header; |
| 403 | }else |
| 404 | allbos=1; |
| 405 | } |
| 406 | |
| 407 | /* otherwise, keep looking */ |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | bail_header: |
| 415 | vorbis_info_clear(vi); |
| 416 | vorbis_comment_clear(vc); |
| 417 | vf->ready_state=OPENED; |
| 418 | |
| 419 | return ret; |
| 420 | } |
| 421 | |
| 422 | /* Starting from current cursor position, get initial PCM offset of |
| 423 | next page. Consumes the page in the process without decoding |
| 424 | audio, however this is only called during stream parsing upon |
| 425 | seekable open. */ |
| 426 | static ogg_int64_t _initial_pcmoffset(OggVorbis_File *vf, vorbis_info *vi){ |
| 427 | ogg_page og; |
| 428 | ogg_int64_t accumulated=0; |
| 429 | long lastblock=-1; |
| 430 | int result; |
| 431 | int serialno = vf->os.serialno; |
| 432 | |
| 433 | while(1){ |
| 434 | ogg_packet op; |
| 435 | if(_get_next_page(vf,&og,-1)<0) |
| 436 | break; /* should not be possible unless the file is truncated/mangled */ |
| 437 | |
| 438 | if(ogg_page_bos(&og)) break; |
| 439 | if(ogg_page_serialno(&og)!=serialno) continue; |
| 440 | |
| 441 | /* count blocksizes of all frames in the page */ |
| 442 | ogg_stream_pagein(&vf->os,&og); |
| 443 | while((result=ogg_stream_packetout(&vf->os,&op))){ |
| 444 | if(result>0){ /* ignore holes */ |
| 445 | long thisblock=vorbis_packet_blocksize(vi,&op); |
| 446 | if(lastblock!=-1) |
| 447 | accumulated+=(lastblock+thisblock)>>2; |
| 448 | lastblock=thisblock; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | if(ogg_page_granulepos(&og)!=-1){ |
| 453 | /* pcm offset of last packet on the first audio page */ |
| 454 | accumulated= ogg_page_granulepos(&og)-accumulated; |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /* less than zero? This is a stream with samples trimmed off |
| 460 | the beginning, a normal occurrence; set the offset to zero */ |
| 461 | if(accumulated<0)accumulated=0; |
| 462 | |
| 463 | return accumulated; |
| 464 | } |
| 465 | |
| 466 | /* finds each bitstream link one at a time using a bisection search |
| 467 | (has to begin by knowing the offset of the lb's initial page). |
| 468 | Recurses for each link so it can alloc the link storage after |
| 469 | finding them all, then unroll and fill the cache at the same time */ |
| 470 | static int _bisect_forward_serialno(OggVorbis_File *vf, |
| 471 | ogg_int64_t begin, |
| 472 | ogg_int64_t searched, |
| 473 | ogg_int64_t end, |
| 474 | ogg_int64_t endgran, |
| 475 | int endserial, |
| 476 | ogg_uint32_t *currentno_list, |
| 477 | int currentnos, |
| 478 | long m){ |
| 479 | ogg_int64_t pcmoffset; |
| 480 | ogg_int64_t dataoffset=searched; |
| 481 | ogg_int64_t endsearched=end; |
| 482 | ogg_int64_t next=end; |
| 483 | ogg_int64_t searchgran=-1; |
| 484 | ogg_page og; |
| 485 | ogg_int64_t ret,last; |
| 486 | int serialno = vf->os.serialno; |
| 487 | |
| 488 | /* invariants: |
| 489 | we have the headers and serialnos for the link beginning at 'begin' |
| 490 | we have the offset and granpos of the last page in the file (potentially |
| 491 | not a page we care about) |
| 492 | */ |
| 493 | |
| 494 | /* Is the last page in our list of current serialnumbers? */ |
| 495 | if(_lookup_serialno(endserial,currentno_list,currentnos)){ |
| 496 | |
| 497 | /* last page is in the starting serialno list, so we've bisected |
| 498 | down to (or just started with) a single link. Now we need to |
| 499 | find the last vorbis page belonging to the first vorbis stream |
| 500 | for this link. */ |
| 501 | |
| 502 | while(endserial != serialno){ |
| 503 | endserial = serialno; |
| 504 | vf->offset=_get_prev_page_serial(vf,currentno_list,currentnos,&endserial,&endgran); |
| 505 | } |
| 506 | |
| 507 | vf->links=m+1; |
| 508 | if(vf->offsets)_ogg_free(vf->offsets); |
| 509 | if(vf->serialnos)_ogg_free(vf->serialnos); |
| 510 | if(vf->dataoffsets)_ogg_free(vf->dataoffsets); |
| 511 | |
| 512 | vf->offsets=_ogg_malloc((vf->links+1)*sizeof(*vf->offsets)); |
| 513 | vf->vi=_ogg_realloc(vf->vi,vf->links*sizeof(*vf->vi)); |
| 514 | vf->vc=_ogg_realloc(vf->vc,vf->links*sizeof(*vf->vc)); |
| 515 | vf->serialnos=_ogg_malloc(vf->links*sizeof(*vf->serialnos)); |
| 516 | vf->dataoffsets=_ogg_malloc(vf->links*sizeof(*vf->dataoffsets)); |
| 517 | vf->pcmlengths=_ogg_malloc(vf->links*2*sizeof(*vf->pcmlengths)); |
| 518 | |
| 519 | vf->offsets[m+1]=end; |
| 520 | vf->offsets[m]=begin; |
| 521 | vf->pcmlengths[m*2+1]=endgran; |
| 522 | |
| 523 | }else{ |
| 524 | |
| 525 | ogg_uint32_t *next_serialno_list=NULL; |
| 526 | int next_serialnos=0; |
| 527 | vorbis_info vi; |
| 528 | vorbis_comment vc; |
| 529 | |
| 530 | /* the below guards against garbage seperating the last and |
| 531 | first pages of two links. */ |
| 532 | while(searched<endsearched){ |
| 533 | ogg_int64_t bisect; |
| 534 | |
| 535 | if(endsearched-searched<CHUNKSIZE){ |
| 536 | bisect=searched; |
| 537 | }else{ |
| 538 | bisect=(searched+endsearched)/2; |
| 539 | } |
| 540 | |
| 541 | if(bisect != vf->offset){ |
| 542 | ret=_seek_helper(vf,bisect); |
| 543 | if(ret)return(ret); |
| 544 | } |
| 545 | |
| 546 | last=_get_next_page(vf,&og,-1); |
| 547 | if(last==OV_EREAD)return(OV_EREAD); |
| 548 | if(last<0 || !_lookup_page_serialno(&og,currentno_list,currentnos)){ |
| 549 | endsearched=bisect; |
| 550 | if(last>=0)next=last; |
| 551 | }else{ |
| 552 | searched=vf->offset; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | /* Bisection point found */ |
| 557 | |
| 558 | /* for the time being, fetch end PCM offset the simple way */ |
| 559 | { |
| 560 | int testserial = serialno+1; |
| 561 | vf->offset = next; |
| 562 | while(testserial != serialno){ |
| 563 | testserial = serialno; |
| 564 | vf->offset=_get_prev_page_serial(vf,currentno_list,currentnos,&testserial,&searchgran); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | if(vf->offset!=next){ |
| 569 | ret=_seek_helper(vf,next); |
| 570 | if(ret)return(ret); |
| 571 | } |
| 572 | |
| 573 | ret=_fetch_headers(vf,&vi,&vc,&next_serialno_list,&next_serialnos,NULL); |
| 574 | if(ret)return(ret); |
| 575 | serialno = vf->os.serialno; |
| 576 | dataoffset = vf->offset; |
| 577 | |
| 578 | /* this will consume a page, however the next bistection always |
| 579 | starts with a raw seek */ |
| 580 | pcmoffset = _initial_pcmoffset(vf,&vi); |
| 581 | |
| 582 | ret=_bisect_forward_serialno(vf,next,vf->offset,end,endgran,endserial, |
| 583 | next_serialno_list,next_serialnos,m+1); |
| 584 | if(ret)return(ret); |
| 585 | |
| 586 | if(next_serialno_list)_ogg_free(next_serialno_list); |
| 587 | |
| 588 | vf->offsets[m+1]=next; |
| 589 | vf->serialnos[m+1]=serialno; |
| 590 | vf->dataoffsets[m+1]=dataoffset; |
| 591 | |
| 592 | vf->vi[m+1]=vi; |
| 593 | vf->vc[m+1]=vc; |
| 594 | |
| 595 | vf->pcmlengths[m*2+1]=searchgran; |
| 596 | vf->pcmlengths[m*2+2]=pcmoffset; |
| 597 | vf->pcmlengths[m*2+3]-=pcmoffset; |
| 598 | |
| 599 | } |
| 600 | return(0); |
| 601 | } |
| 602 | |
| 603 | static int _make_decode_ready(OggVorbis_File *vf){ |
| 604 | if(vf->ready_state>STREAMSET)return 0; |
| 605 | if(vf->ready_state<STREAMSET)return OV_EFAULT; |
| 606 | if(vf->seekable){ |
| 607 | if(vorbis_synthesis_init(&vf->vd,vf->vi+vf->current_link)) |
| 608 | return OV_EBADLINK; |
| 609 | }else{ |
| 610 | if(vorbis_synthesis_init(&vf->vd,vf->vi)) |
| 611 | return OV_EBADLINK; |
| 612 | } |
| 613 | vorbis_block_init(&vf->vd,&vf->vb); |
| 614 | vf->ready_state=INITSET; |
| 615 | vf->bittrack=0; |
| 616 | vf->samptrack=0; |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | static int _open_seekable2(OggVorbis_File *vf){ |
| 621 | ogg_int64_t dataoffset=vf->dataoffsets[0],end,endgran=-1; |
| 622 | int endserial=vf->os.serialno; |
| 623 | int serialno=vf->os.serialno; |
| 624 | |
| 625 | /* we're partially open and have a first link header state in |
| 626 | storage in vf */ |
| 627 | |
| 628 | /* fetch initial PCM offset */ |
| 629 | ogg_int64_t pcmoffset = _initial_pcmoffset(vf,vf->vi); |
| 630 | |
| 631 | /* we can seek, so set out learning all about this file */ |
| 632 | if(vf->callbacks.seek_func && vf->callbacks.tell_func){ |
| 633 | (vf->callbacks.seek_func)(vf->datasource,0,SEEK_END); |
| 634 | vf->offset=vf->end=(vf->callbacks.tell_func)(vf->datasource); |
| 635 | }else{ |
| 636 | vf->offset=vf->end=-1; |
| 637 | } |
| 638 | |
| 639 | /* If seek_func is implemented, tell_func must also be implemented */ |
| 640 | if(vf->end==-1) return(OV_EINVAL); |
| 641 | |
| 642 | /* Get the offset of the last page of the physical bitstream, or, if |
| 643 | we're lucky the last vorbis page of this link as most OggVorbis |
| 644 | files will contain a single logical bitstream */ |
| 645 | end=_get_prev_page_serial(vf,vf->serialnos+2,vf->serialnos[1],&endserial,&endgran); |
| 646 | if(end<0)return(end); |
| 647 | |
| 648 | /* now determine bitstream structure recursively */ |
| 649 | if(_bisect_forward_serialno(vf,0,dataoffset,vf->offset,endgran,endserial, |
| 650 | vf->serialnos+2,vf->serialnos[1],0)<0)return(OV_EREAD); |
| 651 | |
| 652 | vf->offsets[0]=0; |
| 653 | vf->serialnos[0]=serialno; |
| 654 | vf->dataoffsets[0]=dataoffset; |
| 655 | vf->pcmlengths[0]=pcmoffset; |
| 656 | vf->pcmlengths[1]-=pcmoffset; |
| 657 | |
| 658 | return(ov_raw_seek(vf,dataoffset)); |
| 659 | } |
| 660 | |
| 661 | /* clear out the current logical bitstream decoder */ |
| 662 | static void _decode_clear(OggVorbis_File *vf){ |
| 663 | vorbis_dsp_clear(&vf->vd); |
| 664 | vorbis_block_clear(&vf->vb); |
| 665 | vf->ready_state=OPENED; |
| 666 | } |
| 667 | |
| 668 | /* fetch and process a packet. Handles the case where we're at a |
| 669 | bitstream boundary and dumps the decoding machine. If the decoding |
| 670 | machine is unloaded, it loads it. It also keeps pcm_offset up to |
| 671 | date (seek and read both use this. seek uses a special hack with |
| 672 | readp). |
| 673 | |
| 674 | return: <0) error, OV_HOLE (lost packet) or OV_EOF |
| 675 | 0) need more data (only if readp==0) |
| 676 | 1) got a packet |
| 677 | */ |
| 678 | |
| 679 | static int _fetch_and_process_packet(OggVorbis_File *vf, |
| 680 | ogg_packet *op_in, |
| 681 | int readp, |
| 682 | int spanp){ |
| 683 | ogg_page og; |
| 684 | |
| 685 | /* handle one packet. Try to fetch it from current stream state */ |
| 686 | /* extract packets from page */ |
| 687 | while(1){ |
| 688 | |
| 689 | if(vf->ready_state==STREAMSET){ |
| 690 | int ret=_make_decode_ready(vf); |
| 691 | if(ret<0)return ret; |
| 692 | } |
| 693 | |
| 694 | /* process a packet if we can. If the machine isn't loaded, |
| 695 | neither is a page */ |
| 696 | if(vf->ready_state==INITSET){ |
| 697 | while(1) { |
| 698 | ogg_packet op; |
| 699 | ogg_packet *op_ptr=(op_in?op_in:&op); |
| 700 | int result=ogg_stream_packetout(&vf->os,op_ptr); |
| 701 | ogg_int64_t granulepos; |
| 702 | |
| 703 | op_in=NULL; |
| 704 | if(result==-1)return(OV_HOLE); /* hole in the data. */ |
| 705 | if(result>0){ |
| 706 | /* got a packet. process it */ |
| 707 | granulepos=op_ptr->granulepos; |
| 708 | if(!vorbis_synthesis(&vf->vb,op_ptr)){ /* lazy check for lazy |
| 709 | header handling. The |
| 710 | header packets aren't |
| 711 | audio, so if/when we |
| 712 | submit them, |
| 713 | vorbis_synthesis will |
| 714 | reject them */ |
| 715 | |
| 716 | /* suck in the synthesis data and track bitrate */ |
| 717 | { |
| 718 | int oldsamples=vorbis_synthesis_pcmout(&vf->vd,NULL); |
| 719 | /* for proper use of libvorbis within libvorbisfile, |
| 720 | oldsamples will always be zero. */ |
| 721 | if(oldsamples)return(OV_EFAULT); |
| 722 | |
| 723 | vorbis_synthesis_blockin(&vf->vd,&vf->vb); |
| 724 | vf->samptrack+=vorbis_synthesis_pcmout(&vf->vd,NULL)-oldsamples; |
| 725 | vf->bittrack+=op_ptr->bytes*8; |
| 726 | } |
| 727 | |
| 728 | /* update the pcm offset. */ |
| 729 | if(granulepos!=-1 && !op_ptr->e_o_s){ |
| 730 | int link=(vf->seekable?vf->current_link:0); |
| 731 | int i,samples; |
| 732 | |
| 733 | /* this packet has a pcm_offset on it (the last packet |
| 734 | completed on a page carries the offset) After processing |
| 735 | (above), we know the pcm position of the *last* sample |
| 736 | ready to be returned. Find the offset of the *first* |
| 737 | |
| 738 | As an aside, this trick is inaccurate if we begin |
| 739 | reading anew right at the last page; the end-of-stream |
| 740 | granulepos declares the last frame in the stream, and the |
| 741 | last packet of the last page may be a partial frame. |
| 742 | So, we need a previous granulepos from an in-sequence page |
| 743 | to have a reference point. Thus the !op_ptr->e_o_s clause |
| 744 | above */ |
| 745 | |
| 746 | if(vf->seekable && link>0) |
| 747 | granulepos-=vf->pcmlengths[link*2]; |
| 748 | if(granulepos<0)granulepos=0; /* actually, this |
| 749 | shouldn't be possible |
| 750 | here unless the stream |
| 751 | is very broken */ |
| 752 | |
| 753 | samples=vorbis_synthesis_pcmout(&vf->vd,NULL); |
| 754 | |
| 755 | granulepos-=samples; |
| 756 | for(i=0;i<link;i++) |
| 757 | granulepos+=vf->pcmlengths[i*2+1]; |
| 758 | vf->pcm_offset=granulepos; |
| 759 | } |
| 760 | return(1); |
| 761 | } |
| 762 | } |
| 763 | else |
| 764 | break; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | if(vf->ready_state>=OPENED){ |
| 769 | ogg_int64_t ret; |
| 770 | |
| 771 | while(1){ |
| 772 | /* the loop is not strictly necessary, but there's no sense in |
| 773 | doing the extra checks of the larger loop for the common |
| 774 | case in a multiplexed bistream where the page is simply |
| 775 | part of a different logical bitstream; keep reading until |
| 776 | we get one with the correct serialno */ |
| 777 | |
| 778 | if(!readp)return(0); |
| 779 | if((ret=_get_next_page(vf,&og,-1))<0){ |
| 780 | return(OV_EOF); /* eof. leave unitialized */ |
| 781 | } |
| 782 | |
| 783 | /* bitrate tracking; add the header's bytes here, the body bytes |
| 784 | are done by packet above */ |
| 785 | vf->bittrack+=og.header_len*8; |
| 786 | |
| 787 | if(vf->ready_state==INITSET){ |
| 788 | if(vf->current_serialno!=ogg_page_serialno(&og)){ |
| 789 | |
| 790 | /* two possibilities: |
| 791 | 1) our decoding just traversed a bitstream boundary |
| 792 | 2) another stream is multiplexed into this logical section */ |
| 793 | |
| 794 | if(ogg_page_bos(&og)){ |
| 795 | /* boundary case */ |
| 796 | if(!spanp) |
| 797 | return(OV_EOF); |
| 798 | |
| 799 | _decode_clear(vf); |
| 800 | |
| 801 | if(!vf->seekable){ |
| 802 | vorbis_info_clear(vf->vi); |
| 803 | vorbis_comment_clear(vf->vc); |
| 804 | } |
| 805 | break; |
| 806 | |
| 807 | }else |
| 808 | continue; /* possibility #2 */ |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | break; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | /* Do we need to load a new machine before submitting the page? */ |
| 817 | /* This is different in the seekable and non-seekable cases. |
| 818 | |
| 819 | In the seekable case, we already have all the header |
| 820 | information loaded and cached; we just initialize the machine |
| 821 | with it and continue on our merry way. |
| 822 | |
| 823 | In the non-seekable (streaming) case, we'll only be at a |
| 824 | boundary if we just left the previous logical bitstream and |
| 825 | we're now nominally at the header of the next bitstream |
| 826 | */ |
| 827 | |
| 828 | if(vf->ready_state!=INITSET){ |
| 829 | int link; |
| 830 | |
| 831 | if(vf->ready_state<STREAMSET){ |
| 832 | if(vf->seekable){ |
| 833 | ogg_uint32_t serialno = ogg_page_serialno(&og); |
| 834 | |
| 835 | /* match the serialno to bitstream section. We use this rather than |
| 836 | offset positions to avoid problems near logical bitstream |
| 837 | boundaries */ |
| 838 | |
| 839 | for(link=0;link<vf->links;link++) |
| 840 | if(vf->serialnos[link]==serialno)break; |
| 841 | |
| 842 | if(link==vf->links) continue; /* not the desired Vorbis |
| 843 | bitstream section; keep |
| 844 | trying */ |
| 845 | |
| 846 | vf->current_serialno=serialno; |
| 847 | vf->current_link=link; |
| 848 | |
| 849 | ogg_stream_reset_serialno(&vf->os,vf->current_serialno); |
| 850 | vf->ready_state=STREAMSET; |
| 851 | |
| 852 | }else{ |
| 853 | /* we're streaming */ |
| 854 | /* fetch the three header packets, build the info struct */ |
| 855 | |
| 856 | int ret=_fetch_headers(vf,vf->vi,vf->vc,NULL,NULL,&og); |
| 857 | if(ret)return(ret); |
| 858 | vf->current_serialno=vf->os.serialno; |
| 859 | vf->current_link++; |
| 860 | link=0; |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | /* the buffered page is the data we want, and we're ready for it; |
| 866 | add it to the stream state */ |
| 867 | ogg_stream_pagein(&vf->os,&og); |
| 868 | |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | /* if, eg, 64 bit stdio is configured by default, this will build with |
| 873 | fseek64 */ |
| 874 | static int _fseek64_wrap(FILE *f,ogg_int64_t off,int whence){ |
| 875 | if(f==NULL)return(-1); |
| 876 | return fseek(f,off,whence); |
| 877 | } |
| 878 | |
| 879 | static int _ov_open1(void *f,OggVorbis_File *vf,const char *initial, |
| 880 | long ibytes, ov_callbacks callbacks){ |
| 881 | int offsettest=((f && callbacks.seek_func)?callbacks.seek_func(f,0,SEEK_CUR):-1); |
| 882 | ogg_uint32_t *serialno_list=NULL; |
| 883 | int serialno_list_size=0; |
| 884 | int ret; |
| 885 | |
| 886 | memset(vf,0,sizeof(*vf)); |
| 887 | vf->datasource=f; |
| 888 | vf->callbacks = callbacks; |
| 889 | |
| 890 | /* init the framing state */ |
| 891 | ogg_sync_init(&vf->oy); |
| 892 | |
| 893 | /* perhaps some data was previously read into a buffer for testing |
| 894 | against other stream types. Allow initialization from this |
| 895 | previously read data (especially as we may be reading from a |
| 896 | non-seekable stream) */ |
| 897 | if(initial){ |
| 898 | char *buffer=ogg_sync_buffer(&vf->oy,ibytes); |
| 899 | memcpy(buffer,initial,ibytes); |
| 900 | ogg_sync_wrote(&vf->oy,ibytes); |
| 901 | } |
| 902 | |
| 903 | /* can we seek? Stevens suggests the seek test was portable */ |
| 904 | if(offsettest!=-1)vf->seekable=1; |
| 905 | |
| 906 | /* No seeking yet; Set up a 'single' (current) logical bitstream |
| 907 | entry for partial open */ |
| 908 | vf->links=1; |
| 909 | vf->vi=_ogg_calloc(vf->links,sizeof(*vf->vi)); |
| 910 | vf->vc=_ogg_calloc(vf->links,sizeof(*vf->vc)); |
| 911 | ogg_stream_init(&vf->os,-1); /* fill in the serialno later */ |
| 912 | |
| 913 | /* Fetch all BOS pages, store the vorbis header and all seen serial |
| 914 | numbers, load subsequent vorbis setup headers */ |
| 915 | if((ret=_fetch_headers(vf,vf->vi,vf->vc,&serialno_list,&serialno_list_size,NULL))<0){ |
| 916 | vf->datasource=NULL; |
| 917 | ov_clear(vf); |
| 918 | }else{ |
| 919 | /* serial number list for first link needs to be held somewhere |
| 920 | for second stage of seekable stream open; this saves having to |
| 921 | seek/reread first link's serialnumber data then. */ |
| 922 | vf->serialnos=_ogg_calloc(serialno_list_size+2,sizeof(*vf->serialnos)); |
| 923 | vf->serialnos[0]=vf->current_serialno=vf->os.serialno; |
| 924 | vf->serialnos[1]=serialno_list_size; |
| 925 | memcpy(vf->serialnos+2,serialno_list,serialno_list_size*sizeof(*vf->serialnos)); |
| 926 | |
| 927 | vf->offsets=_ogg_calloc(1,sizeof(*vf->offsets)); |
| 928 | vf->dataoffsets=_ogg_calloc(1,sizeof(*vf->dataoffsets)); |
| 929 | vf->offsets[0]=0; |
| 930 | vf->dataoffsets[0]=vf->offset; |
| 931 | |
| 932 | vf->ready_state=PARTOPEN; |
| 933 | } |
| 934 | if(serialno_list)_ogg_free(serialno_list); |
| 935 | return(ret); |
| 936 | } |
| 937 | |
| 938 | static int _ov_open2(OggVorbis_File *vf){ |
| 939 | if(vf->ready_state != PARTOPEN) return OV_EINVAL; |
| 940 | vf->ready_state=OPENED; |
| 941 | if(vf->seekable){ |
| 942 | int ret=_open_seekable2(vf); |
| 943 | if(ret){ |
| 944 | vf->datasource=NULL; |
| 945 | ov_clear(vf); |
| 946 | } |
| 947 | return(ret); |
| 948 | }else |
| 949 | vf->ready_state=STREAMSET; |
| 950 | |
| 951 | return 0; |
| 952 | } |
| 953 | |
| 954 | |
| 955 | /* clear out the OggVorbis_File struct */ |
| 956 | int ov_clear(OggVorbis_File *vf){ |
| 957 | if(vf){ |
| 958 | vorbis_block_clear(&vf->vb); |
| 959 | vorbis_dsp_clear(&vf->vd); |
| 960 | ogg_stream_clear(&vf->os); |
| 961 | |
| 962 | if(vf->vi && vf->links){ |
| 963 | int i; |
| 964 | for(i=0;i<vf->links;i++){ |
| 965 | vorbis_info_clear(vf->vi+i); |
| 966 | vorbis_comment_clear(vf->vc+i); |
| 967 | } |
| 968 | _ogg_free(vf->vi); |
| 969 | _ogg_free(vf->vc); |
| 970 | } |
| 971 | if(vf->dataoffsets)_ogg_free(vf->dataoffsets); |
| 972 | if(vf->pcmlengths)_ogg_free(vf->pcmlengths); |
| 973 | if(vf->serialnos)_ogg_free(vf->serialnos); |
| 974 | if(vf->offsets)_ogg_free(vf->offsets); |
| 975 | ogg_sync_clear(&vf->oy); |
| 976 | if(vf->datasource && vf->callbacks.close_func) |
| 977 | (vf->callbacks.close_func)(vf->datasource); |
| 978 | memset(vf,0,sizeof(*vf)); |
| 979 | } |
| 980 | #ifdef DEBUG_LEAKS |
| 981 | _VDBG_dump(); |
| 982 | #endif |
| 983 | return(0); |
| 984 | } |
| 985 | |
| 986 | /* inspects the OggVorbis file and finds/documents all the logical |
| 987 | bitstreams contained in it. Tries to be tolerant of logical |
| 988 | bitstream sections that are truncated/woogie. |
| 989 | |
| 990 | return: -1) error |
| 991 | 0) OK |
| 992 | */ |
| 993 | |
| 994 | int ov_open_callbacks(void *f,OggVorbis_File *vf, |
| 995 | const char *initial,long ibytes,ov_callbacks callbacks){ |
| 996 | int ret=_ov_open1(f,vf,initial,ibytes,callbacks); |
| 997 | if(ret)return ret; |
| 998 | return _ov_open2(vf); |
| 999 | } |
| 1000 | |
| 1001 | int ov_open(FILE *f,OggVorbis_File *vf,const char *initial,long ibytes){ |
| 1002 | ov_callbacks callbacks = { |
| 1003 | (size_t (*)(void *, size_t, size_t, void *)) fread, |
| 1004 | (int (*)(void *, ogg_int64_t, int)) _fseek64_wrap, |
| 1005 | (int (*)(void *)) fclose, |
| 1006 | (long (*)(void *)) ftell |
| 1007 | }; |
| 1008 | |
| 1009 | return ov_open_callbacks((void *)f, vf, initial, ibytes, callbacks); |
| 1010 | } |
| 1011 | |
| 1012 | int ov_fopen(const char *path,OggVorbis_File *vf){ |
| 1013 | int ret; |
| 1014 | FILE *f = fopen(path,"rb"); |
| 1015 | if(!f) return -1; |
| 1016 | |
| 1017 | ret = ov_open(f,vf,NULL,0); |
| 1018 | if(ret) fclose(f); |
| 1019 | return ret; |
| 1020 | } |
| 1021 | |
| 1022 | |
| 1023 | /* Only partially open the vorbis file; test for Vorbisness, and load |
| 1024 | the headers for the first chain. Do not seek (although test for |
| 1025 | seekability). Use ov_test_open to finish opening the file, else |
| 1026 | ov_clear to close/free it. Same return codes as open. */ |
| 1027 | |
| 1028 | int ov_test_callbacks(void *f,OggVorbis_File *vf, |
| 1029 | const char *initial,long ibytes,ov_callbacks callbacks) |
| 1030 | { |
| 1031 | return _ov_open1(f,vf,initial,ibytes,callbacks); |
| 1032 | } |
| 1033 | |
| 1034 | int ov_test(FILE *f,OggVorbis_File *vf,const char *initial,long ibytes){ |
| 1035 | ov_callbacks callbacks = { |
| 1036 | (size_t (*)(void *, size_t, size_t, void *)) fread, |
| 1037 | (int (*)(void *, ogg_int64_t, int)) _fseek64_wrap, |
| 1038 | (int (*)(void *)) fclose, |
| 1039 | (long (*)(void *)) ftell |
| 1040 | }; |
| 1041 | |
| 1042 | return ov_test_callbacks((void *)f, vf, initial, ibytes, callbacks); |
| 1043 | } |
| 1044 | |
| 1045 | int ov_test_open(OggVorbis_File *vf){ |
| 1046 | if(vf->ready_state!=PARTOPEN)return(OV_EINVAL); |
| 1047 | return _ov_open2(vf); |
| 1048 | } |
| 1049 | |
| 1050 | /* How many logical bitstreams in this physical bitstream? */ |
| 1051 | long ov_streams(OggVorbis_File *vf){ |
| 1052 | return vf->links; |
| 1053 | } |
| 1054 | |
| 1055 | /* Is the FILE * associated with vf seekable? */ |
| 1056 | long ov_seekable(OggVorbis_File *vf){ |
| 1057 | return vf->seekable; |
| 1058 | } |
| 1059 | |
| 1060 | /* returns the bitrate for a given logical bitstream or the entire |
| 1061 | physical bitstream. If the file is open for random access, it will |
| 1062 | find the *actual* average bitrate. If the file is streaming, it |
| 1063 | returns the nominal bitrate (if set) else the average of the |
| 1064 | upper/lower bounds (if set) else -1 (unset). |
| 1065 | |
| 1066 | If you want the actual bitrate field settings, get them from the |
| 1067 | vorbis_info structs */ |
| 1068 | |
| 1069 | long ov_bitrate(OggVorbis_File *vf,int i){ |
| 1070 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1071 | if(i>=vf->links)return(OV_EINVAL); |
| 1072 | if(!vf->seekable && i!=0)return(ov_bitrate(vf,0)); |
| 1073 | if(i<0){ |
| 1074 | ogg_int64_t bits=0; |
| 1075 | int i; |
| 1076 | for(i=0;i<vf->links;i++) |
| 1077 | bits+=(vf->offsets[i+1]-vf->dataoffsets[i])*8; |
| 1078 | /* This once read: return(rint(bits/ov_time_total(vf,-1))); |
| 1079 | * gcc 3.x on x86 miscompiled this at optimisation level 2 and above, |
| 1080 | * so this is slightly transformed to make it work. |
| 1081 | */ |
| 1082 | return(bits*1000/ov_time_total(vf,-1)); |
| 1083 | }else{ |
| 1084 | if(vf->seekable){ |
| 1085 | /* return the actual bitrate */ |
| 1086 | return((vf->offsets[i+1]-vf->dataoffsets[i])*8000/ov_time_total(vf,i)); |
| 1087 | }else{ |
| 1088 | /* return nominal if set */ |
| 1089 | if(vf->vi[i].bitrate_nominal>0){ |
| 1090 | return vf->vi[i].bitrate_nominal; |
| 1091 | }else{ |
| 1092 | if(vf->vi[i].bitrate_upper>0){ |
| 1093 | if(vf->vi[i].bitrate_lower>0){ |
| 1094 | return (vf->vi[i].bitrate_upper+vf->vi[i].bitrate_lower)/2; |
| 1095 | }else{ |
| 1096 | return vf->vi[i].bitrate_upper; |
| 1097 | } |
| 1098 | } |
| 1099 | return(OV_FALSE); |
| 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | /* returns the actual bitrate since last call. returns -1 if no |
| 1106 | additional data to offer since last call (or at beginning of stream), |
| 1107 | EINVAL if stream is only partially open |
| 1108 | */ |
| 1109 | long ov_bitrate_instant(OggVorbis_File *vf){ |
| 1110 | int link=(vf->seekable?vf->current_link:0); |
| 1111 | long ret; |
| 1112 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1113 | if(vf->samptrack==0)return(OV_FALSE); |
| 1114 | ret=vf->bittrack/vf->samptrack*vf->vi[link].rate; |
| 1115 | vf->bittrack=0; |
| 1116 | vf->samptrack=0; |
| 1117 | return(ret); |
| 1118 | } |
| 1119 | |
| 1120 | /* Guess */ |
| 1121 | long ov_serialnumber(OggVorbis_File *vf,int i){ |
| 1122 | if(i>=vf->links)return(ov_serialnumber(vf,vf->links-1)); |
| 1123 | if(!vf->seekable && i>=0)return(ov_serialnumber(vf,-1)); |
| 1124 | if(i<0){ |
| 1125 | return(vf->current_serialno); |
| 1126 | }else{ |
| 1127 | return(vf->serialnos[i]); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | /* returns: total raw (compressed) length of content if i==-1 |
| 1132 | raw (compressed) length of that logical bitstream for i==0 to n |
| 1133 | OV_EINVAL if the stream is not seekable (we can't know the length) |
| 1134 | or if stream is only partially open |
| 1135 | */ |
| 1136 | ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i){ |
| 1137 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1138 | if(!vf->seekable || i>=vf->links)return(OV_EINVAL); |
| 1139 | if(i<0){ |
| 1140 | ogg_int64_t acc=0; |
| 1141 | int i; |
| 1142 | for(i=0;i<vf->links;i++) |
| 1143 | acc+=ov_raw_total(vf,i); |
| 1144 | return(acc); |
| 1145 | }else{ |
| 1146 | return(vf->offsets[i+1]-vf->offsets[i]); |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | /* returns: total PCM length (samples) of content if i==-1 PCM length |
| 1151 | (samples) of that logical bitstream for i==0 to n |
| 1152 | OV_EINVAL if the stream is not seekable (we can't know the |
| 1153 | length) or only partially open |
| 1154 | */ |
| 1155 | ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i){ |
| 1156 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1157 | if(!vf->seekable || i>=vf->links)return(OV_EINVAL); |
| 1158 | if(i<0){ |
| 1159 | ogg_int64_t acc=0; |
| 1160 | int i; |
| 1161 | for(i=0;i<vf->links;i++) |
| 1162 | acc+=ov_pcm_total(vf,i); |
| 1163 | return(acc); |
| 1164 | }else{ |
| 1165 | return(vf->pcmlengths[i*2+1]); |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | /* returns: total milliseconds of content if i==-1 |
| 1170 | milliseconds in that logical bitstream for i==0 to n |
| 1171 | OV_EINVAL if the stream is not seekable (we can't know the |
| 1172 | length) or only partially open |
| 1173 | */ |
| 1174 | ogg_int64_t ov_time_total(OggVorbis_File *vf,int i){ |
| 1175 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1176 | if(!vf->seekable || i>=vf->links)return(OV_EINVAL); |
| 1177 | if(i<0){ |
| 1178 | ogg_int64_t acc=0; |
| 1179 | int i; |
| 1180 | for(i=0;i<vf->links;i++) |
| 1181 | acc+=ov_time_total(vf,i); |
| 1182 | return(acc); |
| 1183 | }else{ |
| 1184 | return(((ogg_int64_t)vf->pcmlengths[i*2+1])*1000/vf->vi[i].rate); |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | /* seek to an offset relative to the *compressed* data. This also |
| 1189 | scans packets to update the PCM cursor. It will cross a logical |
| 1190 | bitstream boundary, but only if it can't get any packets out of the |
| 1191 | tail of the bitstream we seek to (so no surprises). |
| 1192 | |
| 1193 | returns zero on success, nonzero on failure */ |
| 1194 | |
| 1195 | int ov_raw_seek(OggVorbis_File *vf,ogg_int64_t pos){ |
| 1196 | ogg_stream_state work_os; |
| 1197 | int ret; |
| 1198 | |
| 1199 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1200 | if(!vf->seekable) |
| 1201 | return(OV_ENOSEEK); /* don't dump machine if we can't seek */ |
| 1202 | |
| 1203 | if(pos<0 || pos>vf->end)return(OV_EINVAL); |
| 1204 | |
| 1205 | /* is the seek position outside our current link [if any]? */ |
| 1206 | if(vf->ready_state>=STREAMSET){ |
| 1207 | if(pos<vf->offsets[vf->current_link] || pos>=vf->offsets[vf->current_link+1]) |
| 1208 | _decode_clear(vf); /* clear out stream state */ |
| 1209 | } |
| 1210 | |
| 1211 | /* don't yet clear out decoding machine (if it's initialized), in |
| 1212 | the case we're in the same link. Restart the decode lapping, and |
| 1213 | let _fetch_and_process_packet deal with a potential bitstream |
| 1214 | boundary */ |
| 1215 | vf->pcm_offset=-1; |
| 1216 | ogg_stream_reset_serialno(&vf->os, |
| 1217 | vf->current_serialno); /* must set serialno */ |
| 1218 | vorbis_synthesis_restart(&vf->vd); |
| 1219 | |
| 1220 | ret=_seek_helper(vf,pos); |
| 1221 | if(ret)goto seek_error; |
| 1222 | |
| 1223 | /* we need to make sure the pcm_offset is set, but we don't want to |
| 1224 | advance the raw cursor past good packets just to get to the first |
| 1225 | with a granulepos. That's not equivalent behavior to beginning |
| 1226 | decoding as immediately after the seek position as possible. |
| 1227 | |
| 1228 | So, a hack. We use two stream states; a local scratch state and |
| 1229 | the shared vf->os stream state. We use the local state to |
| 1230 | scan, and the shared state as a buffer for later decode. |
| 1231 | |
| 1232 | Unfortuantely, on the last page we still advance to last packet |
| 1233 | because the granulepos on the last page is not necessarily on a |
| 1234 | packet boundary, and we need to make sure the granpos is |
| 1235 | correct. |
| 1236 | */ |
| 1237 | |
| 1238 | { |
| 1239 | ogg_page og; |
| 1240 | ogg_packet op; |
| 1241 | int lastblock=0; |
| 1242 | int accblock=0; |
| 1243 | int thisblock=0; |
| 1244 | int lastflag=0; |
| 1245 | int firstflag=0; |
| 1246 | ogg_int64_t pagepos=-1; |
| 1247 | |
| 1248 | ogg_stream_init(&work_os,vf->current_serialno); /* get the memory ready */ |
| 1249 | ogg_stream_reset(&work_os); /* eliminate the spurious OV_HOLE |
| 1250 | return from not necessarily |
| 1251 | starting from the beginning */ |
| 1252 | |
| 1253 | while(1){ |
| 1254 | if(vf->ready_state>=STREAMSET){ |
| 1255 | /* snarf/scan a packet if we can */ |
| 1256 | int result=ogg_stream_packetout(&work_os,&op); |
| 1257 | |
| 1258 | if(result>0){ |
| 1259 | |
| 1260 | if(vf->vi[vf->current_link].codec_setup){ |
| 1261 | thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op); |
| 1262 | if(thisblock<0){ |
| 1263 | ogg_stream_packetout(&vf->os,NULL); |
| 1264 | thisblock=0; |
| 1265 | }else{ |
| 1266 | |
| 1267 | /* We can't get a guaranteed correct pcm position out of the |
| 1268 | last page in a stream because it might have a 'short' |
| 1269 | granpos, which can only be detected in the presence of a |
| 1270 | preceding page. However, if the last page is also the first |
| 1271 | page, the granpos rules of a first page take precedence. Not |
| 1272 | only that, but for first==last, the EOS page must be treated |
| 1273 | as if its a normal first page for the stream to open/play. */ |
| 1274 | if(lastflag && !firstflag) |
| 1275 | ogg_stream_packetout(&vf->os,NULL); |
| 1276 | else |
| 1277 | if(lastblock)accblock+=(lastblock+thisblock)>>2; |
| 1278 | } |
| 1279 | |
| 1280 | if(op.granulepos!=-1){ |
| 1281 | int i,link=vf->current_link; |
| 1282 | ogg_int64_t granulepos=op.granulepos-vf->pcmlengths[link*2]; |
| 1283 | if(granulepos<0)granulepos=0; |
| 1284 | |
| 1285 | for(i=0;i<link;i++) |
| 1286 | granulepos+=vf->pcmlengths[i*2+1]; |
| 1287 | vf->pcm_offset=granulepos-accblock; |
| 1288 | if(vf->pcm_offset<0)vf->pcm_offset=0; |
| 1289 | break; |
| 1290 | } |
| 1291 | lastblock=thisblock; |
| 1292 | continue; |
| 1293 | }else |
| 1294 | ogg_stream_packetout(&vf->os,NULL); |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | if(!lastblock){ |
| 1299 | pagepos=_get_next_page(vf,&og,-1); |
| 1300 | if(pagepos<0){ |
| 1301 | vf->pcm_offset=ov_pcm_total(vf,-1); |
| 1302 | break; |
| 1303 | } |
| 1304 | }else{ |
| 1305 | /* huh? Bogus stream with packets but no granulepos */ |
| 1306 | vf->pcm_offset=-1; |
| 1307 | break; |
| 1308 | } |
| 1309 | |
| 1310 | /* has our decoding just traversed a bitstream boundary? */ |
| 1311 | if(vf->ready_state>=STREAMSET){ |
| 1312 | if(vf->current_serialno!=ogg_page_serialno(&og)){ |
| 1313 | |
| 1314 | /* two possibilities: |
| 1315 | 1) our decoding just traversed a bitstream boundary |
| 1316 | 2) another stream is multiplexed into this logical section? */ |
| 1317 | |
| 1318 | if(ogg_page_bos(&og)){ |
| 1319 | /* we traversed */ |
| 1320 | _decode_clear(vf); /* clear out stream state */ |
| 1321 | ogg_stream_clear(&work_os); |
| 1322 | } /* else, do nothing; next loop will scoop another page */ |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | if(vf->ready_state<STREAMSET){ |
| 1327 | int link; |
| 1328 | ogg_uint32_t serialno = ogg_page_serialno(&og); |
| 1329 | |
| 1330 | for(link=0;link<vf->links;link++) |
| 1331 | if(vf->serialnos[link]==serialno)break; |
| 1332 | |
| 1333 | if(link==vf->links) continue; /* not the desired Vorbis |
| 1334 | bitstream section; keep |
| 1335 | trying */ |
| 1336 | vf->current_link=link; |
| 1337 | vf->current_serialno=serialno; |
| 1338 | ogg_stream_reset_serialno(&vf->os,serialno); |
| 1339 | ogg_stream_reset_serialno(&work_os,serialno); |
| 1340 | vf->ready_state=STREAMSET; |
| 1341 | firstflag=(pagepos<=vf->dataoffsets[link]); |
| 1342 | } |
| 1343 | |
| 1344 | ogg_stream_pagein(&vf->os,&og); |
| 1345 | ogg_stream_pagein(&work_os,&og); |
| 1346 | lastflag=ogg_page_eos(&og); |
| 1347 | |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | ogg_stream_clear(&work_os); |
| 1352 | vf->bittrack=0; |
| 1353 | vf->samptrack=0; |
| 1354 | return(0); |
| 1355 | |
| 1356 | seek_error: |
| 1357 | /* dump the machine so we're in a known state */ |
| 1358 | vf->pcm_offset=-1; |
| 1359 | ogg_stream_clear(&work_os); |
| 1360 | _decode_clear(vf); |
| 1361 | return OV_EBADLINK; |
| 1362 | } |
| 1363 | |
| 1364 | /* rescales the number x from the range of [0,from] to [0,to] |
| 1365 | x is in the range [0,from] |
| 1366 | from, to are in the range [1, 1<<62-1] */ |
| 1367 | ogg_int64_t rescale64(ogg_int64_t x, ogg_int64_t from, ogg_int64_t to){ |
| 1368 | ogg_int64_t frac=0; |
| 1369 | ogg_int64_t ret=0; |
| 1370 | int i; |
| 1371 | if(x >= from) return to; |
| 1372 | if(x <= 0) return 0; |
| 1373 | |
| 1374 | for(i=0;i<64;i++){ |
| 1375 | if(x>=from){ |
| 1376 | frac|=1; |
| 1377 | x-=from; |
| 1378 | } |
| 1379 | x<<=1; |
| 1380 | frac<<=1; |
| 1381 | } |
| 1382 | |
| 1383 | for(i=0;i<64;i++){ |
| 1384 | if(frac & 1){ |
| 1385 | ret+=to; |
| 1386 | } |
| 1387 | frac>>=1; |
| 1388 | ret>>=1; |
| 1389 | } |
| 1390 | |
| 1391 | return ret; |
| 1392 | } |
| 1393 | |
| 1394 | /* Page granularity seek (faster than sample granularity because we |
| 1395 | don't do the last bit of decode to find a specific sample). |
| 1396 | |
| 1397 | Seek to the last [granule marked] page preceding the specified pos |
| 1398 | location, such that decoding past the returned point will quickly |
| 1399 | arrive at the requested position. */ |
| 1400 | int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos){ |
| 1401 | int link=-1; |
| 1402 | ogg_int64_t result=0; |
| 1403 | ogg_int64_t total=ov_pcm_total(vf,-1); |
| 1404 | |
| 1405 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1406 | if(!vf->seekable)return(OV_ENOSEEK); |
| 1407 | |
| 1408 | if(pos<0 || pos>total)return(OV_EINVAL); |
| 1409 | |
| 1410 | /* which bitstream section does this pcm offset occur in? */ |
| 1411 | for(link=vf->links-1;link>=0;link--){ |
| 1412 | total-=vf->pcmlengths[link*2+1]; |
| 1413 | if(pos>=total)break; |
| 1414 | } |
| 1415 | |
| 1416 | /* search within the logical bitstream for the page with the highest |
| 1417 | pcm_pos preceding (or equal to) pos. There is a danger here; |
| 1418 | missing pages or incorrect frame number information in the |
| 1419 | bitstream could make our task impossible. Account for that (it |
| 1420 | would be an error condition) */ |
| 1421 | |
| 1422 | /* new search algorithm by HB (Nicholas Vinen) */ |
| 1423 | { |
| 1424 | ogg_int64_t end=vf->offsets[link+1]; |
| 1425 | ogg_int64_t begin=vf->offsets[link]; |
| 1426 | ogg_int64_t begintime = vf->pcmlengths[link*2]; |
| 1427 | ogg_int64_t endtime = vf->pcmlengths[link*2+1]+begintime; |
| 1428 | ogg_int64_t target=pos-total+begintime; |
| 1429 | ogg_int64_t best=begin; |
| 1430 | |
| 1431 | ogg_page og; |
| 1432 | while(begin<end){ |
| 1433 | ogg_int64_t bisect; |
| 1434 | |
| 1435 | if(end-begin<CHUNKSIZE){ |
| 1436 | bisect=begin; |
| 1437 | }else{ |
| 1438 | /* take a (pretty decent) guess. */ |
| 1439 | bisect=begin + rescale64(target-begintime, |
| 1440 | endtime-begintime, |
| 1441 | end-begin) - CHUNKSIZE; |
| 1442 | if(bisect<begin+CHUNKSIZE) |
| 1443 | bisect=begin; |
| 1444 | } |
| 1445 | |
| 1446 | if(bisect!=vf->offset){ |
| 1447 | result=_seek_helper(vf,bisect); |
| 1448 | if(result) goto seek_error; |
| 1449 | } |
| 1450 | |
| 1451 | while(begin<end){ |
| 1452 | result=_get_next_page(vf,&og,end-vf->offset); |
| 1453 | if(result==OV_EREAD) goto seek_error; |
| 1454 | if(result<0){ |
| 1455 | if(bisect<=begin+1) |
| 1456 | end=begin; /* found it */ |
| 1457 | else{ |
| 1458 | if(bisect==0) goto seek_error; |
| 1459 | bisect-=CHUNKSIZE; |
| 1460 | if(bisect<=begin)bisect=begin+1; |
| 1461 | result=_seek_helper(vf,bisect); |
| 1462 | if(result) goto seek_error; |
| 1463 | } |
| 1464 | }else{ |
| 1465 | ogg_int64_t granulepos; |
| 1466 | |
| 1467 | if(ogg_page_serialno(&og)!=vf->serialnos[link]) |
| 1468 | continue; |
| 1469 | |
| 1470 | granulepos=ogg_page_granulepos(&og); |
| 1471 | if(granulepos==-1)continue; |
| 1472 | |
| 1473 | if(granulepos<target){ |
| 1474 | best=result; /* raw offset of packet with granulepos */ |
| 1475 | begin=vf->offset; /* raw offset of next page */ |
| 1476 | begintime=granulepos; |
| 1477 | |
| 1478 | if(target-begintime>44100)break; |
| 1479 | bisect=begin; /* *not* begin + 1 */ |
| 1480 | }else{ |
| 1481 | if(bisect<=begin+1) |
| 1482 | end=begin; /* found it */ |
| 1483 | else{ |
| 1484 | if(end==vf->offset){ /* we're pretty close - we'd be stuck in */ |
| 1485 | end=result; |
| 1486 | bisect-=CHUNKSIZE; /* an endless loop otherwise. */ |
| 1487 | if(bisect<=begin)bisect=begin+1; |
| 1488 | result=_seek_helper(vf,bisect); |
| 1489 | if(result) goto seek_error; |
| 1490 | }else{ |
| 1491 | end=bisect; |
| 1492 | endtime=granulepos; |
| 1493 | break; |
| 1494 | } |
| 1495 | } |
| 1496 | } |
| 1497 | } |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | /* found our page. seek to it, update pcm offset. Easier case than |
| 1502 | raw_seek, don't keep packets preceding granulepos. */ |
| 1503 | { |
| 1504 | ogg_page og; |
| 1505 | ogg_packet op; |
| 1506 | |
| 1507 | /* seek */ |
| 1508 | result=_seek_helper(vf,best); |
| 1509 | vf->pcm_offset=-1; |
| 1510 | if(result) goto seek_error; |
| 1511 | result=_get_next_page(vf,&og,-1); |
| 1512 | if(result<0) goto seek_error; |
| 1513 | |
| 1514 | if(link!=vf->current_link){ |
| 1515 | /* Different link; dump entire decode machine */ |
| 1516 | _decode_clear(vf); |
| 1517 | |
| 1518 | vf->current_link=link; |
| 1519 | vf->current_serialno=vf->serialnos[link]; |
| 1520 | vf->ready_state=STREAMSET; |
| 1521 | |
| 1522 | }else{ |
| 1523 | vorbis_synthesis_restart(&vf->vd); |
| 1524 | } |
| 1525 | |
| 1526 | ogg_stream_reset_serialno(&vf->os,vf->current_serialno); |
| 1527 | ogg_stream_pagein(&vf->os,&og); |
| 1528 | |
| 1529 | /* pull out all but last packet; the one with granulepos */ |
| 1530 | while(1){ |
| 1531 | result=ogg_stream_packetpeek(&vf->os,&op); |
| 1532 | if(result==0){ |
| 1533 | /* !!! the packet finishing this page originated on a |
| 1534 | preceding page. Keep fetching previous pages until we |
| 1535 | get one with a granulepos or without the 'continued' flag |
| 1536 | set. Then just use raw_seek for simplicity. */ |
| 1537 | |
| 1538 | result=_seek_helper(vf,best); |
| 1539 | if(result<0) goto seek_error; |
| 1540 | |
| 1541 | while(1){ |
| 1542 | result=_get_prev_page(vf,&og); |
| 1543 | if(result<0) goto seek_error; |
| 1544 | if(ogg_page_serialno(&og)==vf->current_serialno && |
| 1545 | (ogg_page_granulepos(&og)>-1 || |
| 1546 | !ogg_page_continued(&og))){ |
| 1547 | return ov_raw_seek(vf,result); |
| 1548 | } |
| 1549 | vf->offset=result; |
| 1550 | } |
| 1551 | } |
| 1552 | if(result<0){ |
| 1553 | result = OV_EBADPACKET; |
| 1554 | goto seek_error; |
| 1555 | } |
| 1556 | if(op.granulepos!=-1){ |
| 1557 | vf->pcm_offset=op.granulepos-vf->pcmlengths[vf->current_link*2]; |
| 1558 | if(vf->pcm_offset<0)vf->pcm_offset=0; |
| 1559 | vf->pcm_offset+=total; |
| 1560 | break; |
| 1561 | }else |
| 1562 | result=ogg_stream_packetout(&vf->os,NULL); |
| 1563 | } |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | /* verify result */ |
| 1568 | if(vf->pcm_offset>pos || pos>ov_pcm_total(vf,-1)){ |
| 1569 | result=OV_EFAULT; |
| 1570 | goto seek_error; |
| 1571 | } |
| 1572 | vf->bittrack=0; |
| 1573 | vf->samptrack=0; |
| 1574 | return(0); |
| 1575 | |
| 1576 | seek_error: |
| 1577 | /* dump machine so we're in a known state */ |
| 1578 | vf->pcm_offset=-1; |
| 1579 | _decode_clear(vf); |
| 1580 | return (int)result; |
| 1581 | } |
| 1582 | |
| 1583 | /* seek to a sample offset relative to the decompressed pcm stream |
| 1584 | returns zero on success, nonzero on failure */ |
| 1585 | |
| 1586 | int ov_pcm_seek(OggVorbis_File *vf,ogg_int64_t pos){ |
| 1587 | int thisblock,lastblock=0; |
| 1588 | int ret=ov_pcm_seek_page(vf,pos); |
| 1589 | if(ret<0)return(ret); |
| 1590 | if((ret=_make_decode_ready(vf)))return ret; |
| 1591 | |
| 1592 | /* discard leading packets we don't need for the lapping of the |
| 1593 | position we want; don't decode them */ |
| 1594 | |
| 1595 | while(1){ |
| 1596 | ogg_packet op; |
| 1597 | ogg_page og; |
| 1598 | |
| 1599 | int ret=ogg_stream_packetpeek(&vf->os,&op); |
| 1600 | if(ret>0){ |
| 1601 | thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op); |
| 1602 | if(thisblock<0){ |
| 1603 | ogg_stream_packetout(&vf->os,NULL); |
| 1604 | continue; /* non audio packet */ |
| 1605 | } |
| 1606 | if(lastblock)vf->pcm_offset+=(lastblock+thisblock)>>2; |
| 1607 | |
| 1608 | if(vf->pcm_offset+((thisblock+ |
| 1609 | vorbis_info_blocksize(vf->vi,1))>>2)>=pos)break; |
| 1610 | |
| 1611 | /* remove the packet from packet queue and track its granulepos */ |
| 1612 | ogg_stream_packetout(&vf->os,NULL); |
| 1613 | vorbis_synthesis_trackonly(&vf->vb,&op); /* set up a vb with |
| 1614 | only tracking, no |
| 1615 | pcm_decode */ |
| 1616 | vorbis_synthesis_blockin(&vf->vd,&vf->vb); |
| 1617 | |
| 1618 | /* end of logical stream case is hard, especially with exact |
| 1619 | length positioning. */ |
| 1620 | |
| 1621 | if(op.granulepos>-1){ |
| 1622 | int i; |
| 1623 | /* always believe the stream markers */ |
| 1624 | vf->pcm_offset=op.granulepos-vf->pcmlengths[vf->current_link*2]; |
| 1625 | if(vf->pcm_offset<0)vf->pcm_offset=0; |
| 1626 | for(i=0;i<vf->current_link;i++) |
| 1627 | vf->pcm_offset+=vf->pcmlengths[i*2+1]; |
| 1628 | } |
| 1629 | |
| 1630 | lastblock=thisblock; |
| 1631 | |
| 1632 | }else{ |
| 1633 | if(ret<0 && ret!=OV_HOLE)break; |
| 1634 | |
| 1635 | /* suck in a new page */ |
| 1636 | if(_get_next_page(vf,&og,-1)<0)break; |
| 1637 | if(ogg_page_bos(&og))_decode_clear(vf); |
| 1638 | |
| 1639 | if(vf->ready_state<STREAMSET){ |
| 1640 | ogg_uint32_t serialno=ogg_page_serialno(&og); |
| 1641 | int link; |
| 1642 | |
| 1643 | for(link=0;link<vf->links;link++) |
| 1644 | if(vf->serialnos[link]==serialno)break; |
| 1645 | if(link==vf->links) continue; |
| 1646 | vf->current_link=link; |
| 1647 | |
| 1648 | vf->ready_state=STREAMSET; |
| 1649 | vf->current_serialno=ogg_page_serialno(&og); |
| 1650 | ogg_stream_reset_serialno(&vf->os,serialno); |
| 1651 | ret=_make_decode_ready(vf); |
| 1652 | if(ret)return ret; |
| 1653 | lastblock=0; |
| 1654 | } |
| 1655 | |
| 1656 | ogg_stream_pagein(&vf->os,&og); |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | vf->bittrack=0; |
| 1661 | vf->samptrack=0; |
| 1662 | /* discard samples until we reach the desired position. Crossing a |
| 1663 | logical bitstream boundary with abandon is OK. */ |
| 1664 | while(vf->pcm_offset<pos){ |
| 1665 | ogg_int64_t target=pos-vf->pcm_offset; |
| 1666 | long samples=vorbis_synthesis_pcmout(&vf->vd,NULL); |
| 1667 | |
| 1668 | if(samples>target)samples=target; |
| 1669 | vorbis_synthesis_read(&vf->vd,samples); |
| 1670 | vf->pcm_offset+=samples; |
| 1671 | |
| 1672 | if(samples<target) |
| 1673 | if(_fetch_and_process_packet(vf,NULL,1,1)<=0) |
| 1674 | vf->pcm_offset=ov_pcm_total(vf,-1); /* eof */ |
| 1675 | } |
| 1676 | return 0; |
| 1677 | } |
| 1678 | |
| 1679 | /* seek to a playback time relative to the decompressed pcm stream |
| 1680 | returns zero on success, nonzero on failure */ |
| 1681 | int ov_time_seek(OggVorbis_File *vf,ogg_int64_t milliseconds){ |
| 1682 | /* translate time to PCM position and call ov_pcm_seek */ |
| 1683 | |
| 1684 | int link=-1; |
| 1685 | ogg_int64_t pcm_total=0; |
| 1686 | ogg_int64_t time_total=0; |
| 1687 | |
| 1688 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1689 | if(!vf->seekable)return(OV_ENOSEEK); |
| 1690 | if(milliseconds<0)return(OV_EINVAL); |
| 1691 | |
| 1692 | /* which bitstream section does this time offset occur in? */ |
| 1693 | for(link=0;link<vf->links;link++){ |
| 1694 | ogg_int64_t addsec = ov_time_total(vf,link); |
| 1695 | if(milliseconds<time_total+addsec)break; |
| 1696 | time_total+=addsec; |
| 1697 | pcm_total+=vf->pcmlengths[link*2+1]; |
| 1698 | } |
| 1699 | |
| 1700 | if(link==vf->links)return(OV_EINVAL); |
| 1701 | |
| 1702 | /* enough information to convert time offset to pcm offset */ |
| 1703 | { |
| 1704 | ogg_int64_t target=pcm_total+(milliseconds-time_total)*vf->vi[link].rate/1000; |
| 1705 | return(ov_pcm_seek(vf,target)); |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | /* page-granularity version of ov_time_seek |
| 1710 | returns zero on success, nonzero on failure */ |
| 1711 | int ov_time_seek_page(OggVorbis_File *vf,ogg_int64_t milliseconds){ |
| 1712 | /* translate time to PCM position and call ov_pcm_seek */ |
| 1713 | |
| 1714 | int link=-1; |
| 1715 | ogg_int64_t pcm_total=0; |
| 1716 | ogg_int64_t time_total=0; |
| 1717 | |
| 1718 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1719 | if(!vf->seekable)return(OV_ENOSEEK); |
| 1720 | if(milliseconds<0)return(OV_EINVAL); |
| 1721 | |
| 1722 | /* which bitstream section does this time offset occur in? */ |
| 1723 | for(link=0;link<vf->links;link++){ |
| 1724 | ogg_int64_t addsec = ov_time_total(vf,link); |
| 1725 | if(milliseconds<time_total+addsec)break; |
| 1726 | time_total+=addsec; |
| 1727 | pcm_total+=vf->pcmlengths[link*2+1]; |
| 1728 | } |
| 1729 | |
| 1730 | if(link==vf->links)return(OV_EINVAL); |
| 1731 | |
| 1732 | /* enough information to convert time offset to pcm offset */ |
| 1733 | { |
| 1734 | ogg_int64_t target=pcm_total+(milliseconds-time_total)*vf->vi[link].rate/1000; |
| 1735 | return(ov_pcm_seek_page(vf,target)); |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | /* tell the current stream offset cursor. Note that seek followed by |
| 1740 | tell will likely not give the set offset due to caching */ |
| 1741 | ogg_int64_t ov_raw_tell(OggVorbis_File *vf){ |
| 1742 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1743 | return(vf->offset); |
| 1744 | } |
| 1745 | |
| 1746 | /* return PCM offset (sample) of next PCM sample to be read */ |
| 1747 | ogg_int64_t ov_pcm_tell(OggVorbis_File *vf){ |
| 1748 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1749 | return(vf->pcm_offset); |
| 1750 | } |
| 1751 | |
| 1752 | /* return time offset (milliseconds) of next PCM sample to be read */ |
| 1753 | ogg_int64_t ov_time_tell(OggVorbis_File *vf){ |
| 1754 | int link=0; |
| 1755 | ogg_int64_t pcm_total=0; |
| 1756 | ogg_int64_t time_total=0; |
| 1757 | |
| 1758 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1759 | if(vf->seekable){ |
| 1760 | pcm_total=ov_pcm_total(vf,-1); |
| 1761 | time_total=ov_time_total(vf,-1); |
| 1762 | |
| 1763 | /* which bitstream section does this time offset occur in? */ |
| 1764 | for(link=vf->links-1;link>=0;link--){ |
| 1765 | pcm_total-=vf->pcmlengths[link*2+1]; |
| 1766 | time_total-=ov_time_total(vf,link); |
| 1767 | if(vf->pcm_offset>=pcm_total)break; |
| 1768 | } |
| 1769 | } |
| 1770 | |
| 1771 | return(time_total+(1000*vf->pcm_offset-pcm_total)/vf->vi[link].rate); |
| 1772 | } |
| 1773 | |
| 1774 | /* link: -1) return the vorbis_info struct for the bitstream section |
| 1775 | currently being decoded |
| 1776 | 0-n) to request information for a specific bitstream section |
| 1777 | |
| 1778 | In the case of a non-seekable bitstream, any call returns the |
| 1779 | current bitstream. NULL in the case that the machine is not |
| 1780 | initialized */ |
| 1781 | |
| 1782 | vorbis_info *ov_info(OggVorbis_File *vf,int link){ |
| 1783 | if(vf->seekable){ |
| 1784 | if(link<0) |
| 1785 | if(vf->ready_state>=STREAMSET) |
| 1786 | return vf->vi+vf->current_link; |
| 1787 | else |
| 1788 | return vf->vi; |
| 1789 | else |
| 1790 | if(link>=vf->links) |
| 1791 | return NULL; |
| 1792 | else |
| 1793 | return vf->vi+link; |
| 1794 | }else{ |
| 1795 | return vf->vi; |
| 1796 | } |
| 1797 | } |
| 1798 | |
| 1799 | /* grr, strong typing, grr, no templates/inheritence, grr */ |
| 1800 | vorbis_comment *ov_comment(OggVorbis_File *vf,int link){ |
| 1801 | if(vf->seekable){ |
| 1802 | if(link<0) |
| 1803 | if(vf->ready_state>=STREAMSET) |
| 1804 | return vf->vc+vf->current_link; |
| 1805 | else |
| 1806 | return vf->vc; |
| 1807 | else |
| 1808 | if(link>=vf->links) |
| 1809 | return NULL; |
| 1810 | else |
| 1811 | return vf->vc+link; |
| 1812 | }else{ |
| 1813 | return vf->vc; |
| 1814 | } |
| 1815 | } |
| 1816 | |
| 1817 | /* up to this point, everything could more or less hide the multiple |
| 1818 | logical bitstream nature of chaining from the toplevel application |
| 1819 | if the toplevel application didn't particularly care. However, at |
| 1820 | the point that we actually read audio back, the multiple-section |
| 1821 | nature must surface: Multiple bitstream sections do not necessarily |
| 1822 | have to have the same number of channels or sampling rate. |
| 1823 | |
| 1824 | ov_read returns the sequential logical bitstream number currently |
| 1825 | being decoded along with the PCM data in order that the toplevel |
| 1826 | application can take action on channel/sample rate changes. This |
| 1827 | number will be incremented even for streamed (non-seekable) streams |
| 1828 | (for seekable streams, it represents the actual logical bitstream |
| 1829 | index within the physical bitstream. Note that the accessor |
| 1830 | functions above are aware of this dichotomy). |
| 1831 | |
| 1832 | input values: buffer) a buffer to hold packed PCM data for return |
| 1833 | bytes_req) the byte length requested to be placed into buffer |
| 1834 | |
| 1835 | return values: <0) error/hole in data (OV_HOLE), partial open (OV_EINVAL) |
| 1836 | 0) EOF |
| 1837 | n) number of bytes of PCM actually returned. The |
| 1838 | below works on a packet-by-packet basis, so the |
| 1839 | return length is not related to the 'length' passed |
| 1840 | in, just guaranteed to fit. |
| 1841 | |
| 1842 | *section) set to the logical bitstream number */ |
| 1843 | |
| 1844 | long ov_read(OggVorbis_File *vf,char *buffer,int bytes_req,int *bitstream){ |
| 1845 | int i,j; |
| 1846 | |
| 1847 | ogg_int32_t **pcm; |
| 1848 | long samples; |
| 1849 | |
| 1850 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1851 | |
| 1852 | while(1){ |
| 1853 | if(vf->ready_state==INITSET){ |
| 1854 | samples=vorbis_synthesis_pcmout(&vf->vd,&pcm); |
| 1855 | if(samples)break; |
| 1856 | } |
| 1857 | |
| 1858 | /* suck in another packet */ |
| 1859 | { |
| 1860 | int ret=_fetch_and_process_packet(vf,NULL,1,1); |
| 1861 | if(ret==OV_EOF) |
| 1862 | return(0); |
| 1863 | if(ret<=0) |
| 1864 | return(ret); |
| 1865 | } |
| 1866 | |
| 1867 | } |
| 1868 | |
| 1869 | if(samples>0){ |
| 1870 | |
| 1871 | /* yay! proceed to pack data into the byte buffer */ |
| 1872 | |
| 1873 | long channels=ov_info(vf,-1)->channels; |
| 1874 | |
| 1875 | if(samples>(bytes_req/(2*channels))) |
| 1876 | samples=bytes_req/(2*channels); |
| 1877 | |
| 1878 | for(i=0;i<channels;i++) { /* It's faster in this order */ |
| 1879 | ogg_int32_t *src=pcm[i]; |
| 1880 | short *dest=((short *)buffer)+i; |
| 1881 | for(j=0;j<samples;j++) { |
| 1882 | *dest=CLIP_TO_15(src[j]>>9); |
| 1883 | dest+=channels; |
| 1884 | } |
| 1885 | } |
| 1886 | |
| 1887 | vorbis_synthesis_read(&vf->vd,samples); |
| 1888 | vf->pcm_offset+=samples; |
| 1889 | if(bitstream)*bitstream=vf->current_link; |
| 1890 | return(samples*2*channels); |
| 1891 | }else{ |
| 1892 | return(samples); |
| 1893 | } |
| 1894 | } |