| 1 | /******************************************************************** |
| 2 | * * |
| 3 | * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * |
| 4 | * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * |
| 5 | * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * |
| 6 | * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * |
| 7 | * * |
| 8 | * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015 * |
| 9 | * by the Xiph.Org Foundation https://xiph.org/ * |
| 10 | * * |
| 11 | ******************************************************************** |
| 12 | |
| 13 | function: stdio-based convenience library for opening/seeking/decoding |
| 14 | |
| 15 | ********************************************************************/ |
| 16 | |
| 17 | #include "vorbis_codec.h" |
| 18 | |
| 19 | /* we don't need or want the static callback symbols here */ |
| 20 | #define OV_EXCLUDE_STATIC_CALLBACKS |
| 21 | #include "vorbisfile.h" |
| 22 | |
| 23 | #include "vorbis_os.h" |
| 24 | #include "vorbis_misc.h" |
| 25 | |
| 26 | /* A 'chained bitstream' is a Vorbis bitstream that contains more than |
| 27 | one logical bitstream arranged end to end (the only form of Ogg |
| 28 | multiplexing allowed in a Vorbis bitstream; grouping [parallel |
| 29 | multiplexing] is not allowed in Vorbis) */ |
| 30 | |
| 31 | /* A Vorbis file can be played beginning to end (streamed) without |
| 32 | worrying ahead of time about chaining (see decoder_example.c). If |
| 33 | we have the whole file, however, and want random access |
| 34 | (seeking/scrubbing) or desire to know the total length/time of a |
| 35 | file, we need to account for the possibility of chaining. */ |
| 36 | |
| 37 | /* We can handle things a number of ways; we can determine the entire |
| 38 | bitstream structure right off the bat, or find pieces on demand. |
| 39 | This example determines and caches structure for the entire |
| 40 | bitstream, but builds a virtual decoder on the fly when moving |
| 41 | between links in the chain. */ |
| 42 | |
| 43 | /* There are also different ways to implement seeking. Enough |
| 44 | information exists in an Ogg bitstream to seek to |
| 45 | sample-granularity positions in the output. Or, one can seek by |
| 46 | picking some portion of the stream roughly in the desired area if |
| 47 | we only want coarse navigation through the stream. */ |
| 48 | |
| 49 | /************************************************************************* |
| 50 | * Many, many internal helpers. The intention is not to be confusing; |
| 51 | * rampant duplication and monolithic function implementation would be |
| 52 | * harder to understand anyway. The high level functions are last. Begin |
| 53 | * grokking near the end of the file */ |
| 54 | |
| 55 | /* read a little more data from the file/pipe into the ogg_sync framer |
| 56 | */ |
| 57 | #define CHUNKSIZE 65536 /* greater-than-page-size granularity seeking */ |
| 58 | #define READSIZE 2048 /* a smaller read size is needed for low-rate streaming. */ |
| 59 | |
| 60 | static long _get_data(OggVorbis_File *vf){ |
| 61 | errno=0; |
| 62 | if(!(vf->callbacks.read_func))return(-1); |
| 63 | if(vf->datasource){ |
| 64 | char *buffer=ogg_sync_buffer(&vf->oy,READSIZE); |
| 65 | long bytes=(vf->callbacks.read_func)(buffer,1,READSIZE,vf->datasource); |
| 66 | if(bytes>0)ogg_sync_wrote(&vf->oy,bytes); |
| 67 | if(bytes==0 && errno)return(-1); |
| 68 | return(bytes); |
| 69 | }else |
| 70 | return(0); |
| 71 | } |
| 72 | |
| 73 | /* save a tiny smidge of verbosity to make the code more readable */ |
| 74 | static int _seek_helper(OggVorbis_File *vf,ogg_int64_t offset){ |
| 75 | if(vf->datasource){ |
| 76 | /* only seek if the file position isn't already there */ |
| 77 | if(vf->offset != offset){ |
| 78 | if(!(vf->callbacks.seek_func)|| |
| 79 | (vf->callbacks.seek_func)(vf->datasource, offset, SEEK_SET) == -1) |
| 80 | return OV_EREAD; |
| 81 | vf->offset=offset; |
| 82 | ogg_sync_reset(&vf->oy); |
| 83 | } |
| 84 | }else{ |
| 85 | /* shouldn't happen unless someone writes a broken callback */ |
| 86 | return OV_EFAULT; |
| 87 | } |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /* The read/seek functions track absolute position within the stream */ |
| 92 | |
| 93 | /* from the head of the stream, get the next page. boundary specifies |
| 94 | if the function is allowed to fetch more data from the stream (and |
| 95 | how much) or only use internally buffered data. |
| 96 | |
| 97 | boundary: -1) unbounded search |
| 98 | 0) read no additional data; use cached only |
| 99 | n) search for a new page beginning for n bytes |
| 100 | |
| 101 | return: <0) did not find a page (OV_FALSE, OV_EOF, OV_EREAD) |
| 102 | n) found a page at absolute offset n */ |
| 103 | |
| 104 | static ogg_int64_t _get_next_page(OggVorbis_File *vf,ogg_page *og, |
| 105 | ogg_int64_t boundary){ |
| 106 | if(boundary>0)boundary+=vf->offset; |
| 107 | while(1){ |
| 108 | long more; |
| 109 | |
| 110 | if(boundary>0 && vf->offset>=boundary)return(OV_FALSE); |
| 111 | more=ogg_sync_pageseek(&vf->oy,og); |
| 112 | |
| 113 | if(more<0){ |
| 114 | /* skipped n bytes */ |
| 115 | vf->offset-=more; |
| 116 | }else{ |
| 117 | if(more==0){ |
| 118 | /* send more paramedics */ |
| 119 | if(!boundary)return(OV_FALSE); |
| 120 | { |
| 121 | long ret=_get_data(vf); |
| 122 | if(ret==0)return(OV_EOF); |
| 123 | if(ret<0)return(OV_EREAD); |
| 124 | } |
| 125 | }else{ |
| 126 | /* got a page. Return the offset at the page beginning, |
| 127 | advance the internal offset past the page end */ |
| 128 | ogg_int64_t ret=vf->offset; |
| 129 | vf->offset+=more; |
| 130 | return(ret); |
| 131 | |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /* find the latest page beginning before the passed in position. Much |
| 138 | dirtier than the above as Ogg doesn't have any backward search |
| 139 | linkage. no 'readp' as it will certainly have to read. */ |
| 140 | /* returns offset or OV_EREAD, OV_FAULT */ |
| 141 | static ogg_int64_t _get_prev_page(OggVorbis_File *vf,ogg_int64_t begin,ogg_page *og){ |
| 142 | ogg_int64_t end = begin; |
| 143 | ogg_int64_t ret; |
| 144 | ogg_int64_t offset=-1; |
| 145 | |
| 146 | while(offset==-1){ |
| 147 | begin-=CHUNKSIZE; |
| 148 | if(begin<0) |
| 149 | begin=0; |
| 150 | |
| 151 | ret=_seek_helper(vf,begin); |
| 152 | if(ret)return(ret); |
| 153 | |
| 154 | while(vf->offset<end){ |
| 155 | memset(og,0,sizeof(*og)); |
| 156 | ret=_get_next_page(vf,og,end-vf->offset); |
| 157 | if(ret==OV_EREAD)return(OV_EREAD); |
| 158 | if(ret<0){ |
| 159 | break; |
| 160 | }else{ |
| 161 | offset=ret; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /* In a fully compliant, non-multiplexed stream, we'll still be |
| 167 | holding the last page. In multiplexed (or noncompliant streams), |
| 168 | we will probably have to re-read the last page we saw */ |
| 169 | if(og->header_len==0){ |
| 170 | ret=_seek_helper(vf,offset); |
| 171 | if(ret)return(ret); |
| 172 | |
| 173 | ret=_get_next_page(vf,og,CHUNKSIZE); |
| 174 | if(ret<0) |
| 175 | /* this shouldn't be possible */ |
| 176 | return(OV_EFAULT); |
| 177 | } |
| 178 | |
| 179 | return(offset); |
| 180 | } |
| 181 | |
| 182 | static void _add_serialno(ogg_page *og,long **serialno_list, int *n){ |
| 183 | long s = ogg_page_serialno(og); |
| 184 | (*n)++; |
| 185 | |
| 186 | if(*serialno_list){ |
| 187 | *serialno_list = (long *) Melder_realloc (*serialno_list, sizeof(**serialno_list)*(*n)); |
| 188 | }else{ |
| 189 | *serialno_list = (long *) _Melder_malloc(sizeof(**serialno_list)); |
| 190 | } |
| 191 | |
| 192 | (*serialno_list)[(*n)-1] = s; |
| 193 | } |
| 194 | |
| 195 | /* returns nonzero if found */ |
| 196 | static int _lookup_serialno(long s, long *serialno_list, int n){ |
| 197 | if(serialno_list){ |
| 198 | while(n--){ |
| 199 | if(*serialno_list == s) return 1; |
| 200 | serialno_list++; |
| 201 | } |
| 202 | } |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static int _lookup_page_serialno(ogg_page *og, long *serialno_list, int n){ |
| 207 | long s = ogg_page_serialno(og); |
| 208 | return _lookup_serialno(s,serialno_list,n); |
| 209 | } |
| 210 | |
| 211 | /* performs the same search as _get_prev_page, but prefers pages of |
| 212 | the specified serial number. If a page of the specified serialno is |
| 213 | spotted during the seek-back-and-read-forward, it will return the |
| 214 | info of last page of the matching serial number instead of the very |
| 215 | last page. If no page of the specified serialno is seen, it will |
| 216 | return the info of last page and alter *serialno. */ |
| 217 | static ogg_int64_t _get_prev_page_serial(OggVorbis_File *vf, ogg_int64_t begin, |
| 218 | long *serial_list, int serial_n, |
| 219 | int *serialno, ogg_int64_t *granpos){ |
| 220 | ogg_page og; |
| 221 | ogg_int64_t end=begin; |
| 222 | ogg_int64_t ret; |
| 223 | |
| 224 | ogg_int64_t prefoffset=-1; |
| 225 | ogg_int64_t offset=-1; |
| 226 | ogg_int64_t ret_serialno=-1; |
| 227 | ogg_int64_t ret_gran=-1; |
| 228 | |
| 229 | while(offset==-1){ |
| 230 | begin-=CHUNKSIZE; |
| 231 | if(begin<0) |
| 232 | begin=0; |
| 233 | |
| 234 | ret=_seek_helper(vf,begin); |
| 235 | if(ret)return(ret); |
| 236 | |
| 237 | while(vf->offset<end){ |
| 238 | ret=_get_next_page(vf,&og,end-vf->offset); |
| 239 | if(ret==OV_EREAD)return(OV_EREAD); |
| 240 | if(ret<0){ |
| 241 | break; |
| 242 | }else{ |
| 243 | ret_serialno=ogg_page_serialno(&og); |
| 244 | ret_gran=ogg_page_granulepos(&og); |
| 245 | offset=ret; |
| 246 | |
| 247 | if(ret_serialno == *serialno){ |
| 248 | prefoffset=ret; |
| 249 | *granpos=ret_gran; |
| 250 | } |
| 251 | |
| 252 | if(!_lookup_serialno(ret_serialno,serial_list,serial_n)){ |
| 253 | /* we fell off the end of the link, which means we seeked |
| 254 | back too far and shouldn't have been looking in that link |
| 255 | to begin with. If we found the preferred serial number, |
| 256 | forget that we saw it. */ |
| 257 | prefoffset=-1; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | /*We started from the beginning of the stream and found nothing. |
| 262 | This should be impossible unless the contents of the stream changed out |
| 263 | from under us after we read from it.*/ |
| 264 | if(!begin&&vf->offset<0)return OV_EBADLINK; |
| 265 | } |
| 266 | |
| 267 | /* we're not interested in the page... just the serialno and granpos. */ |
| 268 | if(prefoffset>=0)return(prefoffset); |
| 269 | |
| 270 | *serialno = ret_serialno; |
| 271 | *granpos = ret_gran; |
| 272 | return(offset); |
| 273 | |
| 274 | } |
| 275 | |
| 276 | /* uses the local ogg_stream storage in vf; this is important for |
| 277 | non-streaming input sources */ |
| 278 | static int _fetch_headers(OggVorbis_File *vf,vorbis_info *vi,vorbis_comment *vc, |
| 279 | long **serialno_list, int *serialno_n, |
| 280 | ogg_page *og_ptr){ |
| 281 | ogg_page og; |
| 282 | ogg_packet op; |
| 283 | int i,ret; |
| 284 | int allbos=0; |
| 285 | |
| 286 | if(!og_ptr){ |
| 287 | ogg_int64_t llret=_get_next_page(vf,&og,CHUNKSIZE); |
| 288 | if(llret==OV_EREAD)return(OV_EREAD); |
| 289 | if(llret<0)return(OV_ENOTVORBIS); |
| 290 | og_ptr=&og; |
| 291 | } |
| 292 | |
| 293 | vorbis_info_init(vi); |
| 294 | vorbis_comment_init(vc); |
| 295 | vf->ready_state=OPENED; |
| 296 | |
| 297 | /* extract the serialnos of all BOS pages + the first set of vorbis |
| 298 | headers we see in the link */ |
| 299 | |
| 300 | while(ogg_page_bos(og_ptr)){ |
| 301 | if(serialno_list){ |
| 302 | if(_lookup_page_serialno(og_ptr,*serialno_list,*serialno_n)){ |
| 303 | /* a dupe serialnumber in an initial header packet set == invalid stream */ |
| 304 | if(*serialno_list)Melder_free(*serialno_list); |
| 305 | *serialno_list=0; |
| 306 | *serialno_n=0; |
| 307 | ret=OV_EBADHEADER; |
| 308 | goto bail_header; |
| 309 | } |
| 310 | |
| 311 | _add_serialno(og_ptr,serialno_list,serialno_n); |
| 312 | } |
| 313 | |
| 314 | if(vf->ready_state<STREAMSET){ |
| 315 | /* we don't have a vorbis stream in this link yet, so begin |
| 316 | prospective stream setup. We need a stream to get packets */ |
| 317 | ogg_stream_reset_serialno(&vf->os,ogg_page_serialno(og_ptr)); |
| 318 | ogg_stream_pagein(&vf->os,og_ptr); |
| 319 | |
| 320 | if(ogg_stream_packetout(&vf->os,&op) > 0 && |
| 321 | vorbis_synthesis_idheader(&op)){ |
| 322 | /* vorbis header; continue setup */ |
| 323 | vf->ready_state=STREAMSET; |
| 324 | if((ret=vorbis_synthesis_headerin(vi,vc,&op))){ |
| 325 | ret=OV_EBADHEADER; |
| 326 | goto bail_header; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /* get next page */ |
| 332 | { |
| 333 | ogg_int64_t llret=_get_next_page(vf,og_ptr,CHUNKSIZE); |
| 334 | if(llret==OV_EREAD){ |
| 335 | ret=OV_EREAD; |
| 336 | goto bail_header; |
| 337 | } |
| 338 | if(llret<0){ |
| 339 | ret=OV_ENOTVORBIS; |
| 340 | goto bail_header; |
| 341 | } |
| 342 | |
| 343 | /* if this page also belongs to our vorbis stream, submit it and break */ |
| 344 | if(vf->ready_state==STREAMSET && |
| 345 | vf->os.serialno == ogg_page_serialno(og_ptr)){ |
| 346 | ogg_stream_pagein(&vf->os,og_ptr); |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | if(vf->ready_state!=STREAMSET){ |
| 353 | ret = OV_ENOTVORBIS; |
| 354 | goto bail_header; |
| 355 | } |
| 356 | |
| 357 | while(1){ |
| 358 | |
| 359 | i=0; |
| 360 | while(i<2){ /* get a page loop */ |
| 361 | |
| 362 | while(i<2){ /* get a packet loop */ |
| 363 | |
| 364 | int result=ogg_stream_packetout(&vf->os,&op); |
| 365 | if(result==0)break; |
| 366 | if(result==-1){ |
| 367 | ret=OV_EBADHEADER; |
| 368 | goto bail_header; |
| 369 | } |
| 370 | |
| 371 | if((ret=vorbis_synthesis_headerin(vi,vc,&op))) |
| 372 | goto bail_header; |
| 373 | |
| 374 | i++; |
| 375 | } |
| 376 | |
| 377 | while(i<2){ |
| 378 | if(_get_next_page(vf,og_ptr,CHUNKSIZE)<0){ |
| 379 | ret=OV_EBADHEADER; |
| 380 | goto bail_header; |
| 381 | } |
| 382 | |
| 383 | /* if this page belongs to the correct stream, go parse it */ |
| 384 | if(vf->os.serialno == ogg_page_serialno(og_ptr)){ |
| 385 | ogg_stream_pagein(&vf->os,og_ptr); |
| 386 | break; |
| 387 | } |
| 388 | |
| 389 | /* if we never see the final vorbis headers before the link |
| 390 | ends, abort */ |
| 391 | if(ogg_page_bos(og_ptr)){ |
| 392 | if(allbos){ |
| 393 | ret = OV_EBADHEADER; |
| 394 | goto bail_header; |
| 395 | }else |
| 396 | allbos=1; |
| 397 | } |
| 398 | |
| 399 | /* otherwise, keep looking */ |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | bail_header: |
| 407 | vorbis_info_clear(vi); |
| 408 | vorbis_comment_clear(vc); |
| 409 | vf->ready_state=OPENED; |
| 410 | |
| 411 | return ret; |
| 412 | } |
| 413 | |
| 414 | /* Starting from current cursor position, get initial PCM offset of |
| 415 | next page. Consumes the page in the process without decoding |
| 416 | audio, however this is only called during stream parsing upon |
| 417 | seekable open. */ |
| 418 | static ogg_int64_t _initial_pcmoffset(OggVorbis_File *vf, vorbis_info *vi){ |
| 419 | ogg_page og; |
| 420 | ogg_int64_t accumulated=0; |
| 421 | long lastblock=-1; |
| 422 | int result; |
| 423 | int serialno = vf->os.serialno; |
| 424 | |
| 425 | while(1){ |
| 426 | ogg_packet op; |
| 427 | if(_get_next_page(vf,&og,-1)<0) |
| 428 | break; /* should not be possible unless the file is truncated/mangled */ |
| 429 | |
| 430 | if(ogg_page_bos(&og)) break; |
| 431 | if(ogg_page_serialno(&og)!=serialno) continue; |
| 432 | |
| 433 | /* count blocksizes of all frames in the page */ |
| 434 | ogg_stream_pagein(&vf->os,&og); |
| 435 | while((result=ogg_stream_packetout(&vf->os,&op))){ |
| 436 | if(result>0){ /* ignore holes */ |
| 437 | long thisblock=vorbis_packet_blocksize(vi,&op); |
| 438 | if(thisblock>=0){ |
| 439 | if(lastblock!=-1) |
| 440 | accumulated+=(lastblock+thisblock)>>2; |
| 441 | lastblock=thisblock; |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | if(ogg_page_granulepos(&og)!=-1){ |
| 447 | /* pcm offset of last packet on the first audio page */ |
| 448 | accumulated= ogg_page_granulepos(&og)-accumulated; |
| 449 | break; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /* less than zero? Either a corrupt file or a stream with samples |
| 454 | trimmed off the beginning, a normal occurrence; in both cases set |
| 455 | the offset to zero */ |
| 456 | if(accumulated<0)accumulated=0; |
| 457 | |
| 458 | return accumulated; |
| 459 | } |
| 460 | |
| 461 | /* finds each bitstream link one at a time using a bisection search |
| 462 | (has to begin by knowing the offset of the lb's initial page). |
| 463 | Recurses for each link so it can alloc the link storage after |
| 464 | finding them all, then unroll and fill the cache at the same time */ |
| 465 | static int _bisect_forward_serialno(OggVorbis_File *vf, |
| 466 | ogg_int64_t begin, |
| 467 | ogg_int64_t searched, |
| 468 | ogg_int64_t end, |
| 469 | ogg_int64_t endgran, |
| 470 | int endserial, |
| 471 | long *currentno_list, |
| 472 | int currentnos, |
| 473 | long m){ |
| 474 | ogg_int64_t pcmoffset; |
| 475 | ogg_int64_t dataoffset=searched; |
| 476 | ogg_int64_t endsearched=end; |
| 477 | ogg_int64_t next=end; |
| 478 | ogg_int64_t searchgran=-1; |
| 479 | ogg_page og; |
| 480 | ogg_int64_t ret,last; |
| 481 | int serialno = vf->os.serialno; |
| 482 | |
| 483 | /* invariants: |
| 484 | we have the headers and serialnos for the link beginning at 'begin' |
| 485 | we have the offset and granpos of the last page in the file (potentially |
| 486 | not a page we care about) |
| 487 | */ |
| 488 | |
| 489 | /* Is the last page in our list of current serialnumbers? */ |
| 490 | if(_lookup_serialno(endserial,currentno_list,currentnos)){ |
| 491 | |
| 492 | /* last page is in the starting serialno list, so we've bisected |
| 493 | down to (or just started with) a single link. Now we need to |
| 494 | find the last vorbis page belonging to the first vorbis stream |
| 495 | for this link. */ |
| 496 | searched = end; |
| 497 | while(endserial != serialno){ |
| 498 | endserial = serialno; |
| 499 | searched=_get_prev_page_serial(vf,searched,currentno_list,currentnos,&endserial,&endgran); |
| 500 | } |
| 501 | |
| 502 | vf->links=m+1; |
| 503 | if(vf->offsets)Melder_free(vf->offsets); |
| 504 | if(vf->serialnos)Melder_free(vf->serialnos); |
| 505 | if(vf->dataoffsets)Melder_free(vf->dataoffsets); |
| 506 | |
| 507 | vf->offsets=(ogg_int64_t *)_Melder_malloc((vf->links+1)*sizeof(*vf->offsets)); |
| 508 | vf->vi=(vorbis_info *)Melder_realloc(vf->vi,vf->links*sizeof(*vf->vi)); |
| 509 | vf->vc=(vorbis_comment *)Melder_realloc(vf->vc,vf->links*sizeof(*vf->vc)); |
| 510 | vf->serialnos=(long *)_Melder_malloc(vf->links*sizeof(*vf->serialnos)); |
| 511 | vf->dataoffsets=(ogg_int64_t *)_Melder_malloc(vf->links*sizeof(*vf->dataoffsets)); |
| 512 | vf->pcmlengths=(ogg_int64_t *)_Melder_malloc(vf->links*2*sizeof(*vf->pcmlengths)); |
| 513 | |
| 514 | vf->offsets[m+1]=end; |
| 515 | vf->offsets[m]=begin; |
| 516 | vf->pcmlengths[m*2+1]=(endgran<0?0:endgran); |
| 517 | |
| 518 | }else{ |
| 519 | |
| 520 | /* last page is not in the starting stream's serial number list, |
| 521 | so we have multiple links. Find where the stream that begins |
| 522 | our bisection ends. */ |
| 523 | |
| 524 | long *next_serialno_list=nullptr; |
| 525 | int next_serialnos=0; |
| 526 | vorbis_info vi; |
| 527 | vorbis_comment vc; |
| 528 | int testserial = serialno+1; |
| 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 | ret=_seek_helper(vf,bisect); |
| 542 | if(ret)return(ret); |
| 543 | |
| 544 | last=_get_next_page(vf,&og,-1); |
| 545 | if(last==OV_EREAD)return(OV_EREAD); |
| 546 | if(last<0 || !_lookup_page_serialno(&og,currentno_list,currentnos)){ |
| 547 | endsearched=bisect; |
| 548 | if(last>=0)next=last; |
| 549 | }else{ |
| 550 | searched=vf->offset; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | /* Bisection point found */ |
| 555 | /* for the time being, fetch end PCM offset the simple way */ |
| 556 | searched = next; |
| 557 | while(testserial != serialno){ |
| 558 | testserial = serialno; |
| 559 | searched = _get_prev_page_serial(vf,searched,currentno_list,currentnos,&testserial,&searchgran); |
| 560 | } |
| 561 | |
| 562 | ret=_seek_helper(vf,next); |
| 563 | if(ret)return(ret); |
| 564 | |
| 565 | ret=_fetch_headers(vf,&vi,&vc,&next_serialno_list,&next_serialnos,nullptr); |
| 566 | if(ret)return(ret); |
| 567 | serialno = vf->os.serialno; |
| 568 | dataoffset = vf->offset; |
| 569 | |
| 570 | /* this will consume a page, however the next bisection always |
| 571 | starts with a raw seek */ |
| 572 | pcmoffset = _initial_pcmoffset(vf,&vi); |
| 573 | |
| 574 | ret=_bisect_forward_serialno(vf,next,vf->offset,end,endgran,endserial, |
| 575 | next_serialno_list,next_serialnos,m+1); |
| 576 | if(ret)return(ret); |
| 577 | |
| 578 | if(next_serialno_list)Melder_free(next_serialno_list); |
| 579 | |
| 580 | vf->offsets[m+1]=next; |
| 581 | vf->serialnos[m+1]=serialno; |
| 582 | vf->dataoffsets[m+1]=dataoffset; |
| 583 | |
| 584 | vf->vi[m+1]=vi; |
| 585 | vf->vc[m+1]=vc; |
| 586 | |
| 587 | vf->pcmlengths[m*2+1]=searchgran; |
| 588 | vf->pcmlengths[m*2+2]=pcmoffset; |
| 589 | vf->pcmlengths[m*2+3]-=pcmoffset; |
| 590 | if(vf->pcmlengths[m*2+3]<0)vf->pcmlengths[m*2+3]=0; |
| 591 | } |
| 592 | return(0); |
| 593 | } |
| 594 | |
| 595 | static int _make_decode_ready(OggVorbis_File *vf){ |
| 596 | if(vf->ready_state>STREAMSET)return 0; |
| 597 | if(vf->ready_state<STREAMSET)return OV_EFAULT; |
| 598 | if(vf->seekable){ |
| 599 | if(vorbis_synthesis_init(&vf->vd,vf->vi+vf->current_link)) |
| 600 | return OV_EBADLINK; |
| 601 | }else{ |
| 602 | if(vorbis_synthesis_init(&vf->vd,vf->vi)) |
| 603 | return OV_EBADLINK; |
| 604 | } |
| 605 | vorbis_block_init(&vf->vd,&vf->vb); |
| 606 | vf->ready_state=INITSET; |
| 607 | vf->bittrack=0.f; |
| 608 | vf->samptrack=0.f; |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | static int _open_seekable2(OggVorbis_File *vf){ |
| 613 | ogg_int64_t dataoffset=vf->dataoffsets[0],end,endgran=-1; |
| 614 | int endserial=vf->os.serialno; |
| 615 | int serialno=vf->os.serialno; |
| 616 | |
| 617 | /* we're partially open and have a first link header state in |
| 618 | storage in vf */ |
| 619 | |
| 620 | /* fetch initial PCM offset */ |
| 621 | ogg_int64_t pcmoffset = _initial_pcmoffset(vf,vf->vi); |
| 622 | |
| 623 | /* we can seek, so set out learning all about this file */ |
| 624 | if(vf->callbacks.seek_func && vf->callbacks.tell_func){ |
| 625 | (vf->callbacks.seek_func)(vf->datasource,0,SEEK_END); |
| 626 | vf->offset=vf->end=(vf->callbacks.tell_func)(vf->datasource); |
| 627 | }else{ |
| 628 | vf->offset=vf->end=-1; |
| 629 | } |
| 630 | |
| 631 | /* If seek_func is implemented, tell_func must also be implemented */ |
| 632 | if(vf->end==-1) return(OV_EINVAL); |
| 633 | |
| 634 | /* Get the offset of the last page of the physical bitstream, or, if |
| 635 | we're lucky the last vorbis page of this link as most OggVorbis |
| 636 | files will contain a single logical bitstream */ |
| 637 | end=_get_prev_page_serial(vf,vf->end,vf->serialnos+2,vf->serialnos[1],&endserial,&endgran); |
| 638 | if(end<0)return(end); |
| 639 | |
| 640 | /* now determine bitstream structure recursively */ |
| 641 | if(_bisect_forward_serialno(vf,0,dataoffset,end,endgran,endserial, |
| 642 | vf->serialnos+2,vf->serialnos[1],0)<0)return(OV_EREAD); |
| 643 | |
| 644 | vf->offsets[0]=0; |
| 645 | vf->serialnos[0]=serialno; |
| 646 | vf->dataoffsets[0]=dataoffset; |
| 647 | vf->pcmlengths[0]=pcmoffset; |
| 648 | vf->pcmlengths[1]-=pcmoffset; |
| 649 | if(vf->pcmlengths[1]<0)vf->pcmlengths[1]=0; |
| 650 | |
| 651 | return(ov_raw_seek(vf,dataoffset)); |
| 652 | } |
| 653 | |
| 654 | /* clear out the current logical bitstream decoder */ |
| 655 | static void _decode_clear(OggVorbis_File *vf){ |
| 656 | vorbis_dsp_clear(&vf->vd); |
| 657 | vorbis_block_clear(&vf->vb); |
| 658 | vf->ready_state=OPENED; |
| 659 | } |
| 660 | |
| 661 | /* fetch and process a packet. Handles the case where we're at a |
| 662 | bitstream boundary and dumps the decoding machine. If the decoding |
| 663 | machine is unloaded, it loads it. It also keeps pcm_offset up to |
| 664 | date (seek and read both use this. seek uses a special hack with |
| 665 | readp). |
| 666 | |
| 667 | return: <0) error, OV_HOLE (lost packet) or OV_EOF |
| 668 | 0) need more data (only if readp==0) |
| 669 | 1) got a packet |
| 670 | */ |
| 671 | |
| 672 | static int _fetch_and_process_packet(OggVorbis_File *vf, |
| 673 | ogg_packet *op_in, |
| 674 | int readp, |
| 675 | int spanp){ |
| 676 | ogg_page og; |
| 677 | |
| 678 | /* handle one packet. Try to fetch it from current stream state */ |
| 679 | /* extract packets from page */ |
| 680 | while(1){ |
| 681 | |
| 682 | if(vf->ready_state==STREAMSET){ |
| 683 | int ret=_make_decode_ready(vf); |
| 684 | if(ret<0)return ret; |
| 685 | } |
| 686 | |
| 687 | /* process a packet if we can. */ |
| 688 | |
| 689 | if(vf->ready_state==INITSET){ |
| 690 | int hs=vorbis_synthesis_halfrate_p(vf->vi); |
| 691 | |
| 692 | while(1) { |
| 693 | ogg_packet op; |
| 694 | ogg_packet *op_ptr=(op_in?op_in:&op); |
| 695 | int result=ogg_stream_packetout(&vf->os,op_ptr); |
| 696 | ogg_int64_t granulepos; |
| 697 | |
| 698 | op_in=nullptr; |
| 699 | if(result==-1)return(OV_HOLE); /* hole in the data. */ |
| 700 | if(result>0){ |
| 701 | /* got a packet. process it */ |
| 702 | granulepos=op_ptr->granulepos; |
| 703 | if(!vorbis_synthesis(&vf->vb,op_ptr)){ /* lazy check for lazy |
| 704 | header handling. The |
| 705 | header packets aren't |
| 706 | audio, so if/when we |
| 707 | submit them, |
| 708 | vorbis_synthesis will |
| 709 | reject them */ |
| 710 | |
| 711 | /* suck in the synthesis data and track bitrate */ |
| 712 | { |
| 713 | int oldsamples=vorbis_synthesis_pcmout(&vf->vd,nullptr); |
| 714 | /* for proper use of libvorbis within libvorbisfile, |
| 715 | oldsamples will always be zero. */ |
| 716 | if(oldsamples)return(OV_EFAULT); |
| 717 | |
| 718 | vorbis_synthesis_blockin(&vf->vd,&vf->vb); |
| 719 | vf->samptrack+=(vorbis_synthesis_pcmout(&vf->vd,nullptr)<<hs); |
| 720 | vf->bittrack+=op_ptr->bytes*8; |
| 721 | } |
| 722 | |
| 723 | /* update the pcm offset. */ |
| 724 | if(granulepos!=-1 && !op_ptr->e_o_s){ |
| 725 | int link=(vf->seekable?vf->current_link:0); |
| 726 | int i,samples; |
| 727 | |
| 728 | /* this packet has a pcm_offset on it (the last packet |
| 729 | completed on a page carries the offset) After processing |
| 730 | (above), we know the pcm position of the *last* sample |
| 731 | ready to be returned. Find the offset of the *first* |
| 732 | |
| 733 | As an aside, this trick is inaccurate if we begin |
| 734 | reading anew right at the last page; the end-of-stream |
| 735 | granulepos declares the last frame in the stream, and the |
| 736 | last packet of the last page may be a partial frame. |
| 737 | So, we need a previous granulepos from an in-sequence page |
| 738 | to have a reference point. Thus the !op_ptr->e_o_s clause |
| 739 | above */ |
| 740 | |
| 741 | if(vf->seekable && link>0) |
| 742 | granulepos-=vf->pcmlengths[link*2]; |
| 743 | if(granulepos<0)granulepos=0; /* actually, this |
| 744 | shouldn't be possible |
| 745 | here unless the stream |
| 746 | is very broken */ |
| 747 | |
| 748 | samples=(vorbis_synthesis_pcmout(&vf->vd,nullptr)<<hs); |
| 749 | |
| 750 | granulepos-=samples; |
| 751 | for(i=0;i<link;i++) |
| 752 | granulepos+=vf->pcmlengths[i*2+1]; |
| 753 | vf->pcm_offset=granulepos; |
| 754 | } |
| 755 | return(1); |
| 756 | } |
| 757 | } |
| 758 | else |
| 759 | break; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | if(vf->ready_state>=OPENED){ |
| 764 | ogg_int64_t ret; |
| 765 | |
| 766 | while(1){ |
| 767 | /* the loop is not strictly necessary, but there's no sense in |
| 768 | doing the extra checks of the larger loop for the common |
| 769 | case in a multiplexed bistream where the page is simply |
| 770 | part of a different logical bitstream; keep reading until |
| 771 | we get one with the correct serialno */ |
| 772 | |
| 773 | if(!readp)return(0); |
| 774 | if((ret=_get_next_page(vf,&og,-1))<0){ |
| 775 | return(OV_EOF); /* eof. leave unitialized */ |
| 776 | } |
| 777 | |
| 778 | /* bitrate tracking; add the header's bytes here, the body bytes |
| 779 | are done by packet above */ |
| 780 | vf->bittrack+=og.header_len*8; |
| 781 | |
| 782 | if(vf->ready_state==INITSET){ |
| 783 | if(vf->current_serialno!=ogg_page_serialno(&og)){ |
| 784 | |
| 785 | /* two possibilities: |
| 786 | 1) our decoding just traversed a bitstream boundary |
| 787 | 2) another stream is multiplexed into this logical section */ |
| 788 | |
| 789 | if(ogg_page_bos(&og)){ |
| 790 | /* boundary case */ |
| 791 | if(!spanp) |
| 792 | return(OV_EOF); |
| 793 | |
| 794 | _decode_clear(vf); |
| 795 | |
| 796 | if(!vf->seekable){ |
| 797 | vorbis_info_clear(vf->vi); |
| 798 | vorbis_comment_clear(vf->vc); |
| 799 | } |
| 800 | break; |
| 801 | |
| 802 | }else |
| 803 | continue; /* possibility #2 */ |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | break; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | /* Do we need to load a new machine before submitting the page? */ |
| 812 | /* This is different in the seekable and non-seekable cases. |
| 813 | |
| 814 | In the seekable case, we already have all the header |
| 815 | information loaded and cached; we just initialize the machine |
| 816 | with it and continue on our merry way. |
| 817 | |
| 818 | In the non-seekable (streaming) case, we'll only be at a |
| 819 | boundary if we just left the previous logical bitstream and |
| 820 | we're now nominally at the header of the next bitstream |
| 821 | */ |
| 822 | |
| 823 | if(vf->ready_state!=INITSET){ |
| 824 | int link; |
| 825 | |
| 826 | if(vf->ready_state<STREAMSET){ |
| 827 | if(vf->seekable){ |
| 828 | long serialno = ogg_page_serialno(&og); |
| 829 | |
| 830 | /* match the serialno to bitstream section. We use this rather than |
| 831 | offset positions to avoid problems near logical bitstream |
| 832 | boundaries */ |
| 833 | |
| 834 | for(link=0;link<vf->links;link++) |
| 835 | if(vf->serialnos[link]==serialno)break; |
| 836 | |
| 837 | if(link==vf->links) continue; /* not the desired Vorbis |
| 838 | bitstream section; keep |
| 839 | trying */ |
| 840 | |
| 841 | vf->current_serialno=serialno; |
| 842 | vf->current_link=link; |
| 843 | |
| 844 | ogg_stream_reset_serialno(&vf->os,vf->current_serialno); |
| 845 | vf->ready_state=STREAMSET; |
| 846 | |
| 847 | }else{ |
| 848 | /* we're streaming */ |
| 849 | /* fetch the three header packets, build the info struct */ |
| 850 | |
| 851 | int ret=_fetch_headers(vf,vf->vi,vf->vc,nullptr,nullptr,&og); |
| 852 | if(ret)return(ret); |
| 853 | vf->current_serialno=vf->os.serialno; |
| 854 | vf->current_link++; |
| 855 | link=0; |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | /* the buffered page is the data we want, and we're ready for it; |
| 861 | add it to the stream state */ |
| 862 | ogg_stream_pagein(&vf->os,&og); |
| 863 | |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | /* if, eg, 64 bit stdio is configured by default, this will build with |
| 868 | fseek64 */ |
| 869 | static int _fseek64_wrap(FILE *f,ogg_int64_t off,int whence){ |
| 870 | if(f==nullptr)return(-1); |
| 871 | return fseek(f,off,whence); |
| 872 | } |
| 873 | |
| 874 | static int _ov_open1(void *f,OggVorbis_File *vf,const char *initial, |
| 875 | long ibytes, ov_callbacks callbacks){ |
| 876 | int offsettest=((f && callbacks.seek_func)?callbacks.seek_func(f,0,SEEK_CUR):-1); |
| 877 | long *serialno_list=nullptr; |
| 878 | int serialno_list_size=0; |
| 879 | int ret; |
| 880 | |
| 881 | memset(vf,0,sizeof(*vf)); |
| 882 | vf->datasource=f; |
| 883 | vf->callbacks = callbacks; |
| 884 | |
| 885 | /* init the framing state */ |
| 886 | ogg_sync_init(&vf->oy); |
| 887 | |
| 888 | /* perhaps some data was previously read into a buffer for testing |
| 889 | against other stream types. Allow initialization from this |
| 890 | previously read data (especially as we may be reading from a |
| 891 | non-seekable stream) */ |
| 892 | if(initial){ |
| 893 | char *buffer=ogg_sync_buffer(&vf->oy,ibytes); |
| 894 | memcpy(buffer,initial,ibytes); |
| 895 | ogg_sync_wrote(&vf->oy,ibytes); |
| 896 | } |
| 897 | |
| 898 | /* can we seek? Stevens suggests the seek test was portable */ |
| 899 | if(offsettest!=-1)vf->seekable=1; |
| 900 | |
| 901 | /* No seeking yet; Set up a 'single' (current) logical bitstream |
| 902 | entry for partial open */ |
| 903 | vf->links=1; |
| 904 | vf->vi=(vorbis_info *)_Melder_calloc(vf->links,sizeof(*vf->vi)); |
| 905 | vf->vc=(vorbis_comment *)_Melder_calloc(vf->links,sizeof(*vf->vc)); |
| 906 | ogg_stream_init(&vf->os,-1); /* fill in the serialno later */ |
| 907 | |
| 908 | /* Fetch all BOS pages, store the vorbis header and all seen serial |
| 909 | numbers, load subsequent vorbis setup headers */ |
| 910 | if((ret=_fetch_headers(vf,vf->vi,vf->vc,&serialno_list,&serialno_list_size,nullptr))<0){ |
| 911 | vf->datasource=nullptr; |
| 912 | ov_clear(vf); |
| 913 | }else{ |
| 914 | /* serial number list for first link needs to be held somewhere |
| 915 | for second stage of seekable stream open; this saves having to |
| 916 | seek/reread first link's serialnumber data then. */ |
| 917 | vf->serialnos=(long *)_Melder_calloc(serialno_list_size+2,sizeof(*vf->serialnos)); |
| 918 | vf->serialnos[0]=vf->current_serialno=vf->os.serialno; |
| 919 | vf->serialnos[1]=serialno_list_size; |
| 920 | memcpy(vf->serialnos+2,serialno_list,serialno_list_size*sizeof(*vf->serialnos)); |
| 921 | |
| 922 | vf->offsets=(ogg_int64_t *)_Melder_calloc(1,sizeof(*vf->offsets)); |
| 923 | vf->dataoffsets=(ogg_int64_t *)_Melder_calloc(1,sizeof(*vf->dataoffsets)); |
| 924 | vf->offsets[0]=0; |
| 925 | vf->dataoffsets[0]=vf->offset; |
| 926 | |
| 927 | vf->ready_state=PARTOPEN; |
| 928 | } |
| 929 | if(serialno_list)Melder_free(serialno_list); |
| 930 | return(ret); |
| 931 | } |
| 932 | |
| 933 | static int _ov_open2(OggVorbis_File *vf){ |
| 934 | if(vf->ready_state != PARTOPEN) return OV_EINVAL; |
| 935 | vf->ready_state=OPENED; |
| 936 | if(vf->seekable){ |
| 937 | int ret=_open_seekable2(vf); |
| 938 | if(ret){ |
| 939 | vf->datasource=nullptr; |
| 940 | ov_clear(vf); |
| 941 | } |
| 942 | return(ret); |
| 943 | }else |
| 944 | vf->ready_state=STREAMSET; |
| 945 | |
| 946 | return 0; |
| 947 | } |
| 948 | |
| 949 | |
| 950 | /* clear out the OggVorbis_File struct */ |
| 951 | int ov_clear(OggVorbis_File *vf){ |
| 952 | if(vf){ |
| 953 | vorbis_block_clear(&vf->vb); |
| 954 | vorbis_dsp_clear(&vf->vd); |
| 955 | ogg_stream_clear(&vf->os); |
| 956 | |
| 957 | if(vf->vi && vf->links){ |
| 958 | int i; |
| 959 | for(i=0;i<vf->links;i++){ |
| 960 | vorbis_info_clear(vf->vi+i); |
| 961 | vorbis_comment_clear(vf->vc+i); |
| 962 | } |
| 963 | Melder_free(vf->vi); |
| 964 | Melder_free(vf->vc); |
| 965 | } |
| 966 | if(vf->dataoffsets)Melder_free(vf->dataoffsets); |
| 967 | if(vf->pcmlengths)Melder_free(vf->pcmlengths); |
| 968 | if(vf->serialnos)Melder_free(vf->serialnos); |
| 969 | if(vf->offsets)Melder_free(vf->offsets); |
| 970 | ogg_sync_clear(&vf->oy); |
| 971 | if(vf->datasource && vf->callbacks.close_func) |
| 972 | (vf->callbacks.close_func)(vf->datasource); |
| 973 | memset(vf,0,sizeof(*vf)); |
| 974 | } |
| 975 | #ifdef DEBUG_LEAKS |
| 976 | _VDBG_dump(); |
| 977 | #endif |
| 978 | return(0); |
| 979 | } |
| 980 | |
| 981 | /* inspects the OggVorbis file and finds/documents all the logical |
| 982 | bitstreams contained in it. Tries to be tolerant of logical |
| 983 | bitstream sections that are truncated/woogie. |
| 984 | |
| 985 | return: -1) error |
| 986 | 0) OK |
| 987 | */ |
| 988 | |
| 989 | int ov_open_callbacks(void *f,OggVorbis_File *vf, |
| 990 | const char *initial,long ibytes,ov_callbacks callbacks){ |
| 991 | int ret=_ov_open1(f,vf,initial,ibytes,callbacks); |
| 992 | if(ret)return ret; |
| 993 | return _ov_open2(vf); |
| 994 | } |
| 995 | |
| 996 | int ov_open(FILE *f,OggVorbis_File *vf,const char *initial,long ibytes){ |
| 997 | ov_callbacks callbacks = { |
| 998 | (size_t (*)(void *, size_t, size_t, void *)) fread, |
| 999 | (int (*)(void *, ogg_int64_t, int)) _fseek64_wrap, |
| 1000 | (int (*)(void *)) fclose, |
| 1001 | (long (*)(void *)) ftell |
| 1002 | }; |
| 1003 | |
| 1004 | return ov_open_callbacks((void *)f, vf, initial, ibytes, callbacks); |
| 1005 | } |
| 1006 | |
| 1007 | int ov_fopen(const char *path,OggVorbis_File *vf){ |
| 1008 | int ret; |
| 1009 | FILE *f = fopen(path,"rb"); |
| 1010 | if(!f) return -1; |
| 1011 | |
| 1012 | ret = ov_open(f,vf,nullptr,0); |
| 1013 | if(ret) fclose(f); |
| 1014 | return ret; |
| 1015 | } |
| 1016 | |
| 1017 | |
| 1018 | /* cheap hack for game usage where downsampling is desirable; there's |
| 1019 | no need for SRC as we can just do it cheaply in libvorbis. */ |
| 1020 | |
| 1021 | int ov_halfrate(OggVorbis_File *vf,int flag){ |
| 1022 | int i; |
| 1023 | if(vf->vi==nullptr)return OV_EINVAL; |
| 1024 | if(vf->ready_state>STREAMSET){ |
| 1025 | /* clear out stream state; dumping the decode machine is needed to |
| 1026 | reinit the MDCT lookups. */ |
| 1027 | vorbis_dsp_clear(&vf->vd); |
| 1028 | vorbis_block_clear(&vf->vb); |
| 1029 | vf->ready_state=STREAMSET; |
| 1030 | if(vf->pcm_offset>=0){ |
| 1031 | ogg_int64_t pos=vf->pcm_offset; |
| 1032 | vf->pcm_offset=-1; /* make sure the pos is dumped if unseekable */ |
| 1033 | ov_pcm_seek(vf,pos); |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | for(i=0;i<vf->links;i++){ |
| 1038 | if(vorbis_synthesis_halfrate(vf->vi+i,flag)){ |
| 1039 | if(flag) ov_halfrate(vf,0); |
| 1040 | return OV_EINVAL; |
| 1041 | } |
| 1042 | } |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
| 1046 | int ov_halfrate_p(OggVorbis_File *vf){ |
| 1047 | if(vf->vi==nullptr)return OV_EINVAL; |
| 1048 | return vorbis_synthesis_halfrate_p(vf->vi); |
| 1049 | } |
| 1050 | |
| 1051 | /* Only partially open the vorbis file; test for Vorbisness, and load |
| 1052 | the headers for the first chain. Do not seek (although test for |
| 1053 | seekability). Use ov_test_open to finish opening the file, else |
| 1054 | ov_clear to close/free it. Same return codes as open. |
| 1055 | |
| 1056 | Note that vorbisfile does _not_ take ownership of the file if the |
| 1057 | call fails; the calling applicaiton is responsible for closing the file |
| 1058 | if this call returns an error. */ |
| 1059 | |
| 1060 | int ov_test_callbacks(void *f,OggVorbis_File *vf, |
| 1061 | const char *initial,long ibytes,ov_callbacks callbacks) |
| 1062 | { |
| 1063 | return _ov_open1(f,vf,initial,ibytes,callbacks); |
| 1064 | } |
| 1065 | |
| 1066 | int ov_test(FILE *f,OggVorbis_File *vf,const char *initial,long ibytes){ |
| 1067 | ov_callbacks callbacks = { |
| 1068 | (size_t (*)(void *, size_t, size_t, void *)) fread, |
| 1069 | (int (*)(void *, ogg_int64_t, int)) _fseek64_wrap, |
| 1070 | (int (*)(void *)) fclose, |
| 1071 | (long (*)(void *)) ftell |
| 1072 | }; |
| 1073 | |
| 1074 | return ov_test_callbacks((void *)f, vf, initial, ibytes, callbacks); |
| 1075 | } |
| 1076 | |
| 1077 | int ov_test_open(OggVorbis_File *vf){ |
| 1078 | if(vf->ready_state!=PARTOPEN)return(OV_EINVAL); |
| 1079 | return _ov_open2(vf); |
| 1080 | } |
| 1081 | |
| 1082 | /* How many logical bitstreams in this physical bitstream? */ |
| 1083 | long ov_streams(OggVorbis_File *vf){ |
| 1084 | return vf->links; |
| 1085 | } |
| 1086 | |
| 1087 | /* Is the FILE * associated with vf seekable? */ |
| 1088 | long ov_seekable(OggVorbis_File *vf){ |
| 1089 | return vf->seekable; |
| 1090 | } |
| 1091 | |
| 1092 | /* returns the bitrate for a given logical bitstream or the entire |
| 1093 | physical bitstream. If the file is open for random access, it will |
| 1094 | find the *actual* average bitrate. If the file is streaming, it |
| 1095 | returns the nominal bitrate (if set) else the average of the |
| 1096 | upper/lower bounds (if set) else -1 (unset). |
| 1097 | |
| 1098 | If you want the actual bitrate field settings, get them from the |
| 1099 | vorbis_info structs */ |
| 1100 | |
| 1101 | long ov_bitrate(OggVorbis_File *vf,int i){ |
| 1102 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1103 | if(i>=vf->links)return(OV_EINVAL); |
| 1104 | if(!vf->seekable && i!=0)return(ov_bitrate(vf,0)); |
| 1105 | if(i<0){ |
| 1106 | ogg_int64_t bits=0; |
| 1107 | //int i; |
| 1108 | float br; |
| 1109 | for(integer k=0;k<vf->links;k++) |
| 1110 | bits+=(vf->offsets[k+1]-vf->dataoffsets[k])*8; |
| 1111 | /* This once read: return(rint(bits/ov_time_total(vf,-1))); |
| 1112 | * gcc 3.x on x86 miscompiled this at optimisation level 2 and above, |
| 1113 | * so this is slightly transformed to make it work. |
| 1114 | */ |
| 1115 | br = bits/ov_time_total(vf,-1); |
| 1116 | return(rint(br)); |
| 1117 | }else{ |
| 1118 | if(vf->seekable){ |
| 1119 | /* return the actual bitrate */ |
| 1120 | return(rint((vf->offsets[i+1]-vf->dataoffsets[i])*8/ov_time_total(vf,i))); |
| 1121 | }else{ |
| 1122 | /* return nominal if set */ |
| 1123 | if(vf->vi[i].bitrate_nominal>0){ |
| 1124 | return vf->vi[i].bitrate_nominal; |
| 1125 | }else{ |
| 1126 | if(vf->vi[i].bitrate_upper>0){ |
| 1127 | if(vf->vi[i].bitrate_lower>0){ |
| 1128 | return (vf->vi[i].bitrate_upper+vf->vi[i].bitrate_lower)/2; |
| 1129 | }else{ |
| 1130 | return vf->vi[i].bitrate_upper; |
| 1131 | } |
| 1132 | } |
| 1133 | return(OV_FALSE); |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | /* returns the actual bitrate since last call. returns -1 if no |
| 1140 | additional data to offer since last call (or at beginning of stream), |
| 1141 | EINVAL if stream is only partially open |
| 1142 | */ |
| 1143 | long ov_bitrate_instant(OggVorbis_File *vf){ |
| 1144 | int link=(vf->seekable?vf->current_link:0); |
| 1145 | long ret; |
| 1146 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1147 | if(vf->samptrack==0)return(OV_FALSE); |
| 1148 | ret=vf->bittrack/vf->samptrack*vf->vi[link].rate+.5; |
| 1149 | vf->bittrack=0.f; |
| 1150 | vf->samptrack=0.f; |
| 1151 | return(ret); |
| 1152 | } |
| 1153 | |
| 1154 | /* Guess */ |
| 1155 | long ov_serialnumber(OggVorbis_File *vf,int i){ |
| 1156 | if(i>=vf->links)return(ov_serialnumber(vf,vf->links-1)); |
| 1157 | if(!vf->seekable && i>=0)return(ov_serialnumber(vf,-1)); |
| 1158 | if(i<0){ |
| 1159 | return(vf->current_serialno); |
| 1160 | }else{ |
| 1161 | return(vf->serialnos[i]); |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | /* returns: total raw (compressed) length of content if i==-1 |
| 1166 | raw (compressed) length of that logical bitstream for i==0 to n |
| 1167 | OV_EINVAL if the stream is not seekable (we can't know the length) |
| 1168 | or if stream is only partially open |
| 1169 | */ |
| 1170 | ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i){ |
| 1171 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1172 | if(!vf->seekable || i>=vf->links)return(OV_EINVAL); |
| 1173 | if(i<0){ |
| 1174 | ogg_int64_t acc=0; |
| 1175 | //int i; |
| 1176 | for(integer k=0;k<vf->links;k++) |
| 1177 | acc+=ov_raw_total(vf,k); |
| 1178 | return(acc); |
| 1179 | }else{ |
| 1180 | return(vf->offsets[i+1]-vf->offsets[i]); |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | /* returns: total PCM length (samples) of content if i==-1 PCM length |
| 1185 | (samples) of that logical bitstream for i==0 to n |
| 1186 | OV_EINVAL if the stream is not seekable (we can't know the |
| 1187 | length) or only partially open |
| 1188 | */ |
| 1189 | ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i){ |
| 1190 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1191 | if(!vf->seekable || i>=vf->links)return(OV_EINVAL); |
| 1192 | if(i<0){ |
| 1193 | ogg_int64_t acc=0; |
| 1194 | //int i; |
| 1195 | for(integer k=0;k<vf->links;k++) |
| 1196 | acc+=ov_pcm_total(vf,k); |
| 1197 | return(acc); |
| 1198 | }else{ |
| 1199 | return(vf->pcmlengths[i*2+1]); |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | /* returns: total seconds of content if i==-1 |
| 1204 | seconds in that logical bitstream for i==0 to n |
| 1205 | OV_EINVAL if the stream is not seekable (we can't know the |
| 1206 | length) or only partially open |
| 1207 | */ |
| 1208 | double ov_time_total(OggVorbis_File *vf,int i){ |
| 1209 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1210 | if(!vf->seekable || i>=vf->links)return(OV_EINVAL); |
| 1211 | if(i<0){ |
| 1212 | double acc=0; |
| 1213 | //int i; |
| 1214 | for(integer k=0;k<vf->links;k++) |
| 1215 | acc+=ov_time_total(vf,k); |
| 1216 | return(acc); |
| 1217 | }else{ |
| 1218 | return((double)(vf->pcmlengths[i*2+1])/vf->vi[i].rate); |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | /* seek to an offset relative to the *compressed* data. This also |
| 1223 | scans packets to update the PCM cursor. It will cross a logical |
| 1224 | bitstream boundary, but only if it can't get any packets out of the |
| 1225 | tail of the bitstream we seek to (so no surprises). |
| 1226 | |
| 1227 | returns zero on success, nonzero on failure */ |
| 1228 | |
| 1229 | int ov_raw_seek(OggVorbis_File *vf,ogg_int64_t pos){ |
| 1230 | ogg_stream_state work_os; |
| 1231 | |
| 1232 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1233 | if(!vf->seekable) |
| 1234 | return(OV_ENOSEEK); /* don't dump machine if we can't seek */ |
| 1235 | |
| 1236 | if(pos<0 || pos>vf->end)return(OV_EINVAL); |
| 1237 | |
| 1238 | /* is the seek position outside our current link [if any]? */ |
| 1239 | if(vf->ready_state>=STREAMSET){ |
| 1240 | if(pos<vf->offsets[vf->current_link] || pos>=vf->offsets[vf->current_link+1]) |
| 1241 | _decode_clear(vf); /* clear out stream state */ |
| 1242 | } |
| 1243 | |
| 1244 | /* don't yet clear out decoding machine (if it's initialized), in |
| 1245 | the case we're in the same link. Restart the decode lapping, and |
| 1246 | let _fetch_and_process_packet deal with a potential bitstream |
| 1247 | boundary */ |
| 1248 | vf->pcm_offset=-1; |
| 1249 | ogg_stream_reset_serialno(&vf->os, |
| 1250 | vf->current_serialno); /* must set serialno */ |
| 1251 | vorbis_synthesis_restart(&vf->vd); |
| 1252 | |
| 1253 | if(_seek_helper(vf,pos)) { |
| 1254 | /* dump the machine so we're in a known state */ |
| 1255 | vf->pcm_offset=-1; |
| 1256 | _decode_clear(vf); |
| 1257 | return OV_EBADLINK; |
| 1258 | } |
| 1259 | |
| 1260 | /* we need to make sure the pcm_offset is set, but we don't want to |
| 1261 | advance the raw cursor past good packets just to get to the first |
| 1262 | with a granulepos. That's not equivalent behavior to beginning |
| 1263 | decoding as immediately after the seek position as possible. |
| 1264 | |
| 1265 | So, a hack. We use two stream states; a local scratch state and |
| 1266 | the shared vf->os stream state. We use the local state to |
| 1267 | scan, and the shared state as a buffer for later decode. |
| 1268 | |
| 1269 | Unfortuantely, on the last page we still advance to last packet |
| 1270 | because the granulepos on the last page is not necessarily on a |
| 1271 | packet boundary, and we need to make sure the granpos is |
| 1272 | correct. |
| 1273 | */ |
| 1274 | |
| 1275 | { |
| 1276 | ogg_page og; |
| 1277 | ogg_packet op; |
| 1278 | int lastblock=0; |
| 1279 | int accblock=0; |
| 1280 | int thisblock=0; |
| 1281 | int lastflag=0; |
| 1282 | int firstflag=0; |
| 1283 | ogg_int64_t pagepos=-1; |
| 1284 | |
| 1285 | ogg_stream_init(&work_os,vf->current_serialno); /* get the memory ready */ |
| 1286 | ogg_stream_reset(&work_os); /* eliminate the spurious OV_HOLE |
| 1287 | return from not necessarily |
| 1288 | starting from the beginning */ |
| 1289 | |
| 1290 | while(1){ |
| 1291 | if(vf->ready_state>=STREAMSET){ |
| 1292 | /* snarf/scan a packet if we can */ |
| 1293 | int result=ogg_stream_packetout(&work_os,&op); |
| 1294 | |
| 1295 | if(result>0){ |
| 1296 | |
| 1297 | if(vf->vi[vf->current_link].codec_setup){ |
| 1298 | thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op); |
| 1299 | if(thisblock<0){ |
| 1300 | ogg_stream_packetout(&vf->os,nullptr); |
| 1301 | thisblock=0; |
| 1302 | }else{ |
| 1303 | |
| 1304 | /* We can't get a guaranteed correct pcm position out of the |
| 1305 | last page in a stream because it might have a 'short' |
| 1306 | granpos, which can only be detected in the presence of a |
| 1307 | preceding page. However, if the last page is also the first |
| 1308 | page, the granpos rules of a first page take precedence. Not |
| 1309 | only that, but for first==last, the EOS page must be treated |
| 1310 | as if its a normal first page for the stream to open/play. */ |
| 1311 | if(lastflag && !firstflag) |
| 1312 | ogg_stream_packetout(&vf->os,nullptr); |
| 1313 | else |
| 1314 | if(lastblock)accblock+=(lastblock+thisblock)>>2; |
| 1315 | } |
| 1316 | |
| 1317 | if(op.granulepos!=-1){ |
| 1318 | int i,link=vf->current_link; |
| 1319 | ogg_int64_t granulepos=op.granulepos-vf->pcmlengths[link*2]; |
| 1320 | if(granulepos<0)granulepos=0; |
| 1321 | |
| 1322 | for(i=0;i<link;i++) |
| 1323 | granulepos+=vf->pcmlengths[i*2+1]; |
| 1324 | vf->pcm_offset=granulepos-accblock; |
| 1325 | if(vf->pcm_offset<0)vf->pcm_offset=0; |
| 1326 | break; |
| 1327 | } |
| 1328 | lastblock=thisblock; |
| 1329 | continue; |
| 1330 | }else |
| 1331 | ogg_stream_packetout(&vf->os,nullptr); |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | if(!lastblock){ |
| 1336 | pagepos=_get_next_page(vf,&og,-1); |
| 1337 | if(pagepos<0){ |
| 1338 | vf->pcm_offset=ov_pcm_total(vf,-1); |
| 1339 | break; |
| 1340 | } |
| 1341 | }else{ |
| 1342 | /* huh? Bogus stream with packets but no granulepos */ |
| 1343 | vf->pcm_offset=-1; |
| 1344 | break; |
| 1345 | } |
| 1346 | |
| 1347 | /* has our decoding just traversed a bitstream boundary? */ |
| 1348 | if(vf->ready_state>=STREAMSET){ |
| 1349 | if(vf->current_serialno!=ogg_page_serialno(&og)){ |
| 1350 | |
| 1351 | /* two possibilities: |
| 1352 | 1) our decoding just traversed a bitstream boundary |
| 1353 | 2) another stream is multiplexed into this logical section? */ |
| 1354 | |
| 1355 | if(ogg_page_bos(&og)){ |
| 1356 | /* we traversed */ |
| 1357 | _decode_clear(vf); /* clear out stream state */ |
| 1358 | ogg_stream_clear(&work_os); |
| 1359 | } /* else, do nothing; next loop will scoop another page */ |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | if(vf->ready_state<STREAMSET){ |
| 1364 | int link; |
| 1365 | long serialno = ogg_page_serialno(&og); |
| 1366 | |
| 1367 | for(link=0;link<vf->links;link++) |
| 1368 | if(vf->serialnos[link]==serialno)break; |
| 1369 | |
| 1370 | if(link==vf->links) continue; /* not the desired Vorbis |
| 1371 | bitstream section; keep |
| 1372 | trying */ |
| 1373 | vf->current_link=link; |
| 1374 | vf->current_serialno=serialno; |
| 1375 | ogg_stream_reset_serialno(&vf->os,serialno); |
| 1376 | ogg_stream_reset_serialno(&work_os,serialno); |
| 1377 | vf->ready_state=STREAMSET; |
| 1378 | firstflag=(pagepos<=vf->dataoffsets[link]); |
| 1379 | } |
| 1380 | |
| 1381 | ogg_stream_pagein(&vf->os,&og); |
| 1382 | ogg_stream_pagein(&work_os,&og); |
| 1383 | lastflag=ogg_page_eos(&og); |
| 1384 | |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | ogg_stream_clear(&work_os); |
| 1389 | vf->bittrack=0.f; |
| 1390 | vf->samptrack=0.f; |
| 1391 | return(0); |
| 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 pos. If we're looking for a position on the |
| 1418 | first page, bisection will halt without finding our position as |
| 1419 | it's before the first explicit granulepos fencepost. That case is |
| 1420 | handled separately below. |
| 1421 | |
| 1422 | There is a danger here; missing pages or incorrect frame number |
| 1423 | information in the bitstream could make our task impossible. |
| 1424 | Account for that (it would be an error condition) */ |
| 1425 | |
| 1426 | /* new search algorithm originally by HB (Nicholas Vinen) */ |
| 1427 | |
| 1428 | { |
| 1429 | ogg_int64_t end=vf->offsets[link+1]; |
| 1430 | ogg_int64_t begin=vf->dataoffsets[link]; |
| 1431 | ogg_int64_t begintime = vf->pcmlengths[link*2]; |
| 1432 | ogg_int64_t endtime = vf->pcmlengths[link*2+1]+begintime; |
| 1433 | ogg_int64_t target=pos-total+begintime; |
| 1434 | ogg_int64_t best=-1; |
| 1435 | int got_page=0; |
| 1436 | |
| 1437 | ogg_page og; |
| 1438 | |
| 1439 | /* if we have only one page, there will be no bisection. Grab the page here */ |
| 1440 | if(begin==end){ |
| 1441 | result=_seek_helper(vf,begin); |
| 1442 | if(result) goto seek_error; |
| 1443 | |
| 1444 | result=_get_next_page(vf,&og,1); |
| 1445 | if(result<0) goto seek_error; |
| 1446 | |
| 1447 | got_page=1; |
| 1448 | } |
| 1449 | |
| 1450 | /* bisection loop */ |
| 1451 | while(begin<end){ |
| 1452 | ogg_int64_t bisect; |
| 1453 | |
| 1454 | if(end-begin<CHUNKSIZE){ |
| 1455 | bisect=begin; |
| 1456 | }else{ |
| 1457 | /* take a (pretty decent) guess. */ |
| 1458 | bisect=begin + |
| 1459 | (ogg_int64_t)((double)(target-begintime)*(end-begin)/(endtime-begintime)) |
| 1460 | - CHUNKSIZE; |
| 1461 | if(bisect<begin+CHUNKSIZE) |
| 1462 | bisect=begin; |
| 1463 | } |
| 1464 | |
| 1465 | result=_seek_helper(vf,bisect); |
| 1466 | if(result) goto seek_error; |
| 1467 | |
| 1468 | /* read loop within the bisection loop */ |
| 1469 | while(begin<end){ |
| 1470 | result=_get_next_page(vf,&og,end-vf->offset); |
| 1471 | if(result==OV_EREAD) goto seek_error; |
| 1472 | if(result<0){ |
| 1473 | /* there is no next page! */ |
| 1474 | if(bisect<=begin+1) |
| 1475 | /* No bisection left to perform. We've either found the |
| 1476 | best candidate already or failed. Exit loop. */ |
| 1477 | end=begin; |
| 1478 | else{ |
| 1479 | /* We tried to load a fraction of the last page; back up a |
| 1480 | bit and try to get the whole last page */ |
| 1481 | if(bisect==0) goto seek_error; |
| 1482 | bisect-=CHUNKSIZE; |
| 1483 | |
| 1484 | /* don't repeat/loop on a read we've already performed */ |
| 1485 | if(bisect<=begin)bisect=begin+1; |
| 1486 | |
| 1487 | /* seek and cntinue bisection */ |
| 1488 | result=_seek_helper(vf,bisect); |
| 1489 | if(result) goto seek_error; |
| 1490 | } |
| 1491 | }else{ |
| 1492 | ogg_int64_t granulepos; |
| 1493 | got_page=1; |
| 1494 | |
| 1495 | /* got a page. analyze it */ |
| 1496 | /* only consider pages from primary vorbis stream */ |
| 1497 | if(ogg_page_serialno(&og)!=vf->serialnos[link]) |
| 1498 | continue; |
| 1499 | |
| 1500 | /* only consider pages with the granulepos set */ |
| 1501 | granulepos=ogg_page_granulepos(&og); |
| 1502 | if(granulepos==-1)continue; |
| 1503 | |
| 1504 | if(granulepos<target){ |
| 1505 | /* this page is a successful candidate! Set state */ |
| 1506 | |
| 1507 | best=result; /* raw offset of packet with granulepos */ |
| 1508 | begin=vf->offset; /* raw offset of next page */ |
| 1509 | begintime=granulepos; |
| 1510 | |
| 1511 | /* if we're before our target but within a short distance, |
| 1512 | don't bisect; read forward */ |
| 1513 | if(target-begintime>44100)break; |
| 1514 | |
| 1515 | bisect=begin; /* *not* begin + 1 as above */ |
| 1516 | }else{ |
| 1517 | |
| 1518 | /* This is one of our pages, but the granpos is |
| 1519 | post-target; it is not a bisection return |
| 1520 | candidate. (The only way we'd use it is if it's the |
| 1521 | first page in the stream; we handle that case later |
| 1522 | outside the bisection) */ |
| 1523 | if(bisect<=begin+1){ |
| 1524 | /* No bisection left to perform. We've either found the |
| 1525 | best candidate already or failed. Exit loop. */ |
| 1526 | end=begin; |
| 1527 | }else{ |
| 1528 | if(end==vf->offset){ |
| 1529 | /* bisection read to the end; use the known page |
| 1530 | boundary (result) to update bisection, back up a |
| 1531 | little bit, and try again */ |
| 1532 | end=result; |
| 1533 | bisect-=CHUNKSIZE; |
| 1534 | if(bisect<=begin)bisect=begin+1; |
| 1535 | result=_seek_helper(vf,bisect); |
| 1536 | if(result) goto seek_error; |
| 1537 | }else{ |
| 1538 | /* Normal bisection */ |
| 1539 | end=bisect; |
| 1540 | endtime=granulepos; |
| 1541 | break; |
| 1542 | } |
| 1543 | } |
| 1544 | } |
| 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | /* Out of bisection: did it 'fail?' */ |
| 1550 | if(best == -1){ |
| 1551 | |
| 1552 | /* Check the 'looking for data in first page' special case; |
| 1553 | bisection would 'fail' because our search target was before the |
| 1554 | first PCM granule position fencepost. */ |
| 1555 | |
| 1556 | if(got_page && |
| 1557 | begin == vf->dataoffsets[link] && |
| 1558 | ogg_page_serialno(&og)==vf->serialnos[link]){ |
| 1559 | |
| 1560 | /* Yes, this is the beginning-of-stream case. We already have |
| 1561 | our page, right at the beginning of PCM data. Set state |
| 1562 | and return. */ |
| 1563 | |
| 1564 | vf->pcm_offset=total; |
| 1565 | |
| 1566 | if(link!=vf->current_link){ |
| 1567 | /* Different link; dump entire decode machine */ |
| 1568 | _decode_clear(vf); |
| 1569 | |
| 1570 | vf->current_link=link; |
| 1571 | vf->current_serialno=vf->serialnos[link]; |
| 1572 | vf->ready_state=STREAMSET; |
| 1573 | |
| 1574 | }else{ |
| 1575 | vorbis_synthesis_restart(&vf->vd); |
| 1576 | } |
| 1577 | |
| 1578 | ogg_stream_reset_serialno(&vf->os,vf->current_serialno); |
| 1579 | ogg_stream_pagein(&vf->os,&og); |
| 1580 | |
| 1581 | }else |
| 1582 | goto seek_error; |
| 1583 | |
| 1584 | }else{ |
| 1585 | |
| 1586 | /* Bisection found our page. seek to it, update pcm offset. Easier case than |
| 1587 | raw_seek, don't keep packets preceding granulepos. */ |
| 1588 | |
| 1589 | //ogg_page og; |
| 1590 | ogg_packet op; |
| 1591 | |
| 1592 | /* seek */ |
| 1593 | result=_seek_helper(vf,best); |
| 1594 | vf->pcm_offset=-1; |
| 1595 | if(result) goto seek_error; |
| 1596 | result=_get_next_page(vf,&og,-1); |
| 1597 | if(result<0) goto seek_error; |
| 1598 | |
| 1599 | if(link!=vf->current_link){ |
| 1600 | /* Different link; dump entire decode machine */ |
| 1601 | _decode_clear(vf); |
| 1602 | |
| 1603 | vf->current_link=link; |
| 1604 | vf->current_serialno=vf->serialnos[link]; |
| 1605 | vf->ready_state=STREAMSET; |
| 1606 | |
| 1607 | }else{ |
| 1608 | vorbis_synthesis_restart(&vf->vd); |
| 1609 | } |
| 1610 | |
| 1611 | ogg_stream_reset_serialno(&vf->os,vf->current_serialno); |
| 1612 | ogg_stream_pagein(&vf->os,&og); |
| 1613 | |
| 1614 | /* pull out all but last packet; the one with granulepos */ |
| 1615 | while(1){ |
| 1616 | result=ogg_stream_packetpeek(&vf->os,&op); |
| 1617 | if(result==0){ |
| 1618 | /* No packet returned; we exited the bisection with 'best' |
| 1619 | pointing to a page with a granule position, so the packet |
| 1620 | finishing this page ('best') originated on a preceding |
| 1621 | page. Keep fetching previous pages until we get one with |
| 1622 | a granulepos or without the 'continued' flag set. Then |
| 1623 | just use raw_seek for simplicity. */ |
| 1624 | /* Do not rewind past the beginning of link data; if we do, |
| 1625 | it's either a bug or a broken stream */ |
| 1626 | result=best; |
| 1627 | while(result>vf->dataoffsets[link]){ |
| 1628 | result=_get_prev_page(vf,result,&og); |
| 1629 | if(result<0) goto seek_error; |
| 1630 | if(ogg_page_serialno(&og)==vf->current_serialno && |
| 1631 | (ogg_page_granulepos(&og)>-1 || |
| 1632 | !ogg_page_continued(&og))){ |
| 1633 | return ov_raw_seek(vf,result); |
| 1634 | } |
| 1635 | } |
| 1636 | } |
| 1637 | if(result<0){ |
| 1638 | result = OV_EBADPACKET; |
| 1639 | goto seek_error; |
| 1640 | } |
| 1641 | if(op.granulepos!=-1){ |
| 1642 | vf->pcm_offset=op.granulepos-vf->pcmlengths[vf->current_link*2]; |
| 1643 | if(vf->pcm_offset<0)vf->pcm_offset=0; |
| 1644 | vf->pcm_offset+=total; |
| 1645 | break; |
| 1646 | }else |
| 1647 | result=ogg_stream_packetout(&vf->os,nullptr); |
| 1648 | } |
| 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | /* verify result */ |
| 1653 | if(vf->pcm_offset>pos || pos>ov_pcm_total(vf,-1)){ |
| 1654 | result=OV_EFAULT; |
| 1655 | goto seek_error; |
| 1656 | } |
| 1657 | vf->bittrack=0.f; |
| 1658 | vf->samptrack=0.f; |
| 1659 | return(0); |
| 1660 | |
| 1661 | seek_error: |
| 1662 | /* dump machine so we're in a known state */ |
| 1663 | vf->pcm_offset=-1; |
| 1664 | _decode_clear(vf); |
| 1665 | return (int)result; |
| 1666 | } |
| 1667 | |
| 1668 | /* seek to a sample offset relative to the decompressed pcm stream |
| 1669 | returns zero on success, nonzero on failure */ |
| 1670 | |
| 1671 | int ov_pcm_seek(OggVorbis_File *vf,ogg_int64_t pos){ |
| 1672 | int thisblock,lastblock=0; |
| 1673 | int ret=ov_pcm_seek_page(vf,pos); |
| 1674 | if(ret<0)return(ret); |
| 1675 | if((ret=_make_decode_ready(vf)))return ret; |
| 1676 | |
| 1677 | /* discard leading packets we don't need for the lapping of the |
| 1678 | position we want; don't decode them */ |
| 1679 | |
| 1680 | while(1){ |
| 1681 | ogg_packet op; |
| 1682 | ogg_page og; |
| 1683 | |
| 1684 | /*int*/ ret=ogg_stream_packetpeek(&vf->os,&op); |
| 1685 | if(ret>0){ |
| 1686 | thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op); |
| 1687 | if(thisblock<0){ |
| 1688 | ogg_stream_packetout(&vf->os,nullptr); |
| 1689 | continue; /* non audio packet */ |
| 1690 | } |
| 1691 | if(lastblock)vf->pcm_offset+=(lastblock+thisblock)>>2; |
| 1692 | |
| 1693 | if(vf->pcm_offset+((thisblock+ |
| 1694 | vorbis_info_blocksize(vf->vi,1))>>2)>=pos)break; |
| 1695 | |
| 1696 | /* remove the packet from packet queue and track its granulepos */ |
| 1697 | ogg_stream_packetout(&vf->os,nullptr); |
| 1698 | vorbis_synthesis_trackonly(&vf->vb,&op); /* set up a vb with |
| 1699 | only tracking, no |
| 1700 | pcm_decode */ |
| 1701 | vorbis_synthesis_blockin(&vf->vd,&vf->vb); |
| 1702 | |
| 1703 | /* end of logical stream case is hard, especially with exact |
| 1704 | length positioning. */ |
| 1705 | |
| 1706 | if(op.granulepos>-1){ |
| 1707 | int i; |
| 1708 | /* always believe the stream markers */ |
| 1709 | vf->pcm_offset=op.granulepos-vf->pcmlengths[vf->current_link*2]; |
| 1710 | if(vf->pcm_offset<0)vf->pcm_offset=0; |
| 1711 | for(i=0;i<vf->current_link;i++) |
| 1712 | vf->pcm_offset+=vf->pcmlengths[i*2+1]; |
| 1713 | } |
| 1714 | |
| 1715 | lastblock=thisblock; |
| 1716 | |
| 1717 | }else{ |
| 1718 | if(ret<0 && ret!=OV_HOLE)break; |
| 1719 | |
| 1720 | /* suck in a new page */ |
| 1721 | if(_get_next_page(vf,&og,-1)<0)break; |
| 1722 | if(ogg_page_bos(&og))_decode_clear(vf); |
| 1723 | |
| 1724 | if(vf->ready_state<STREAMSET){ |
| 1725 | long serialno=ogg_page_serialno(&og); |
| 1726 | int link; |
| 1727 | |
| 1728 | for(link=0;link<vf->links;link++) |
| 1729 | if(vf->serialnos[link]==serialno)break; |
| 1730 | if(link==vf->links) continue; |
| 1731 | vf->current_link=link; |
| 1732 | |
| 1733 | vf->ready_state=STREAMSET; |
| 1734 | vf->current_serialno=ogg_page_serialno(&og); |
| 1735 | ogg_stream_reset_serialno(&vf->os,serialno); |
| 1736 | ret=_make_decode_ready(vf); |
| 1737 | if(ret)return ret; |
| 1738 | lastblock=0; |
| 1739 | } |
| 1740 | |
| 1741 | ogg_stream_pagein(&vf->os,&og); |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | vf->bittrack=0.f; |
| 1746 | vf->samptrack=0.f; |
| 1747 | /* discard samples until we reach the desired position. Crossing a |
| 1748 | logical bitstream boundary with abandon is OK. */ |
| 1749 | { |
| 1750 | /* note that halfrate could be set differently in each link, but |
| 1751 | vorbisfile encoforces all links are set or unset */ |
| 1752 | int hs=vorbis_synthesis_halfrate_p(vf->vi); |
| 1753 | while(vf->pcm_offset<((pos>>hs)<<hs)){ |
| 1754 | ogg_int64_t target=(pos-vf->pcm_offset)>>hs; |
| 1755 | long samples=vorbis_synthesis_pcmout(&vf->vd,nullptr); |
| 1756 | |
| 1757 | if(samples>target)samples=target; |
| 1758 | vorbis_synthesis_read(&vf->vd,samples); |
| 1759 | vf->pcm_offset+=samples<<hs; |
| 1760 | |
| 1761 | if(samples<target) |
| 1762 | if(_fetch_and_process_packet(vf,nullptr,1,1)<=0) |
| 1763 | vf->pcm_offset=ov_pcm_total(vf,-1); /* eof */ |
| 1764 | } |
| 1765 | } |
| 1766 | return 0; |
| 1767 | } |
| 1768 | |
| 1769 | /* seek to a playback time relative to the decompressed pcm stream |
| 1770 | returns zero on success, nonzero on failure */ |
| 1771 | int ov_time_seek(OggVorbis_File *vf,double seconds){ |
| 1772 | /* translate time to PCM position and call ov_pcm_seek */ |
| 1773 | |
| 1774 | int link=-1; |
| 1775 | ogg_int64_t pcm_total=0; |
| 1776 | double time_total=0.; |
| 1777 | |
| 1778 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1779 | if(!vf->seekable)return(OV_ENOSEEK); |
| 1780 | if(seconds<0)return(OV_EINVAL); |
| 1781 | |
| 1782 | /* which bitstream section does this time offset occur in? */ |
| 1783 | for(link=0;link<vf->links;link++){ |
| 1784 | double addsec = ov_time_total(vf,link); |
| 1785 | if(seconds<time_total+addsec)break; |
| 1786 | time_total+=addsec; |
| 1787 | pcm_total+=vf->pcmlengths[link*2+1]; |
| 1788 | } |
| 1789 | |
| 1790 | if(link==vf->links)return(OV_EINVAL); |
| 1791 | |
| 1792 | /* enough information to convert time offset to pcm offset */ |
| 1793 | { |
| 1794 | ogg_int64_t target=pcm_total+(seconds-time_total)*vf->vi[link].rate; |
| 1795 | return(ov_pcm_seek(vf,target)); |
| 1796 | } |
| 1797 | } |
| 1798 | |
| 1799 | /* page-granularity version of ov_time_seek |
| 1800 | returns zero on success, nonzero on failure */ |
| 1801 | int ov_time_seek_page(OggVorbis_File *vf,double seconds){ |
| 1802 | /* translate time to PCM position and call ov_pcm_seek */ |
| 1803 | |
| 1804 | int link=-1; |
| 1805 | ogg_int64_t pcm_total=0; |
| 1806 | double time_total=0.; |
| 1807 | |
| 1808 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1809 | if(!vf->seekable)return(OV_ENOSEEK); |
| 1810 | if(seconds<0)return(OV_EINVAL); |
| 1811 | |
| 1812 | /* which bitstream section does this time offset occur in? */ |
| 1813 | for(link=0;link<vf->links;link++){ |
| 1814 | double addsec = ov_time_total(vf,link); |
| 1815 | if(seconds<time_total+addsec)break; |
| 1816 | time_total+=addsec; |
| 1817 | pcm_total+=vf->pcmlengths[link*2+1]; |
| 1818 | } |
| 1819 | |
| 1820 | if(link==vf->links)return(OV_EINVAL); |
| 1821 | |
| 1822 | /* enough information to convert time offset to pcm offset */ |
| 1823 | { |
| 1824 | ogg_int64_t target=pcm_total+(seconds-time_total)*vf->vi[link].rate; |
| 1825 | return(ov_pcm_seek_page(vf,target)); |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | /* tell the current stream offset cursor. Note that seek followed by |
| 1830 | tell will likely not give the set offset due to caching */ |
| 1831 | ogg_int64_t ov_raw_tell(OggVorbis_File *vf){ |
| 1832 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1833 | return(vf->offset); |
| 1834 | } |
| 1835 | |
| 1836 | /* return PCM offset (sample) of next PCM sample to be read */ |
| 1837 | ogg_int64_t ov_pcm_tell(OggVorbis_File *vf){ |
| 1838 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1839 | return(vf->pcm_offset); |
| 1840 | } |
| 1841 | |
| 1842 | /* return time offset (seconds) of next PCM sample to be read */ |
| 1843 | double ov_time_tell(OggVorbis_File *vf){ |
| 1844 | int link=0; |
| 1845 | ogg_int64_t pcm_total=0; |
| 1846 | double time_total=0.f; |
| 1847 | |
| 1848 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1849 | if(vf->seekable){ |
| 1850 | pcm_total=ov_pcm_total(vf,-1); |
| 1851 | time_total=ov_time_total(vf,-1); |
| 1852 | |
| 1853 | /* which bitstream section does this time offset occur in? */ |
| 1854 | for(link=vf->links-1;link>=0;link--){ |
| 1855 | pcm_total-=vf->pcmlengths[link*2+1]; |
| 1856 | time_total-=ov_time_total(vf,link); |
| 1857 | if(vf->pcm_offset>=pcm_total)break; |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | return((double)time_total+(double)(vf->pcm_offset-pcm_total)/vf->vi[link].rate); |
| 1862 | } |
| 1863 | |
| 1864 | /* link: -1) return the vorbis_info struct for the bitstream section |
| 1865 | currently being decoded |
| 1866 | 0-n) to request information for a specific bitstream section |
| 1867 | |
| 1868 | In the case of a non-seekable bitstream, any call returns the |
| 1869 | current bitstream. nullptr in the case that the machine is not |
| 1870 | initialized */ |
| 1871 | |
| 1872 | vorbis_info *ov_info(OggVorbis_File *vf,int link){ |
| 1873 | if(vf->seekable){ |
| 1874 | if(link<0) |
| 1875 | if(vf->ready_state>=STREAMSET) |
| 1876 | return vf->vi+vf->current_link; |
| 1877 | else |
| 1878 | return vf->vi; |
| 1879 | else |
| 1880 | if(link>=vf->links) |
| 1881 | return nullptr; |
| 1882 | else |
| 1883 | return vf->vi+link; |
| 1884 | }else{ |
| 1885 | return vf->vi; |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | /* grr, strong typing, grr, no templates/inheritence, grr */ |
| 1890 | vorbis_comment *ov_comment(OggVorbis_File *vf,int link){ |
| 1891 | if(vf->seekable){ |
| 1892 | if(link<0) |
| 1893 | if(vf->ready_state>=STREAMSET) |
| 1894 | return vf->vc+vf->current_link; |
| 1895 | else |
| 1896 | return vf->vc; |
| 1897 | else |
| 1898 | if(link>=vf->links) |
| 1899 | return nullptr; |
| 1900 | else |
| 1901 | return vf->vc+link; |
| 1902 | }else{ |
| 1903 | return vf->vc; |
| 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | static int host_is_big_endian() { |
| 1908 | ogg_int32_t pattern = 0xfeedface; /* deadbeef */ |
| 1909 | unsigned char *bytewise = (unsigned char *)&pattern; |
| 1910 | if (bytewise[0] == 0xfe) return 1; |
| 1911 | return 0; |
| 1912 | } |
| 1913 | |
| 1914 | /* up to this point, everything could more or less hide the multiple |
| 1915 | logical bitstream nature of chaining from the toplevel application |
| 1916 | if the toplevel application didn't particularly care. However, at |
| 1917 | the point that we actually read audio back, the multiple-section |
| 1918 | nature must surface: Multiple bitstream sections do not necessarily |
| 1919 | have to have the same number of channels or sampling rate. |
| 1920 | |
| 1921 | ov_read returns the sequential logical bitstream number currently |
| 1922 | being decoded along with the PCM data in order that the toplevel |
| 1923 | application can take action on channel/sample rate changes. This |
| 1924 | number will be incremented even for streamed (non-seekable) streams |
| 1925 | (for seekable streams, it represents the actual logical bitstream |
| 1926 | index within the physical bitstream. Note that the accessor |
| 1927 | functions above are aware of this dichotomy). |
| 1928 | |
| 1929 | ov_read_filter is exactly the same as ov_read except that it processes |
| 1930 | the decoded audio data through a filter before packing it into the |
| 1931 | requested format. This gives greater accuracy than applying a filter |
| 1932 | after the audio has been converted into integral PCM. |
| 1933 | |
| 1934 | input values: buffer) a buffer to hold packed PCM data for return |
| 1935 | length) the byte length requested to be placed into buffer |
| 1936 | bigendianp) should the data be packed LSB first (0) or |
| 1937 | MSB first (1) |
| 1938 | word) word size for output. currently 1 (byte) or |
| 1939 | 2 (16 bit short) |
| 1940 | |
| 1941 | return values: <0) error/hole in data (OV_HOLE), partial open (OV_EINVAL) |
| 1942 | 0) EOF |
| 1943 | n) number of bytes of PCM actually returned. The |
| 1944 | below works on a packet-by-packet basis, so the |
| 1945 | return length is not related to the 'length' passed |
| 1946 | in, just guaranteed to fit. |
| 1947 | |
| 1948 | *section) set to the logical bitstream number */ |
| 1949 | |
| 1950 | long ov_read_filter(OggVorbis_File *vf,char *buffer,int length, |
| 1951 | int bigendianp,int word,int sgned,int *bitstream, |
| 1952 | void (*filter)(float **pcm,long channels,long samples,void *filter_param),void *filter_param){ |
| 1953 | int i,j; |
| 1954 | int host_endian = host_is_big_endian(); |
| 1955 | int hs; |
| 1956 | |
| 1957 | float **pcm; |
| 1958 | long samples; |
| 1959 | |
| 1960 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 1961 | if(word<=0)return(OV_EINVAL); |
| 1962 | |
| 1963 | while(1){ |
| 1964 | if(vf->ready_state==INITSET){ |
| 1965 | samples=vorbis_synthesis_pcmout(&vf->vd,&pcm); |
| 1966 | if(samples)break; |
| 1967 | } |
| 1968 | |
| 1969 | /* suck in another packet */ |
| 1970 | { |
| 1971 | int ret=_fetch_and_process_packet(vf,nullptr,1,1); |
| 1972 | if(ret==OV_EOF) |
| 1973 | return(0); |
| 1974 | if(ret<=0) |
| 1975 | return(ret); |
| 1976 | } |
| 1977 | |
| 1978 | } |
| 1979 | |
| 1980 | if(samples>0){ |
| 1981 | |
| 1982 | /* yay! proceed to pack data into the byte buffer */ |
| 1983 | |
| 1984 | long channels=ov_info(vf,-1)->channels; |
| 1985 | long bytespersample=word * channels; |
| 1986 | // vorbis_fpu_control fpu; |
| 1987 | |
| 1988 | if(channels<1||channels>255)return(OV_EINVAL); |
| 1989 | if(samples>length/bytespersample)samples=length/bytespersample; |
| 1990 | |
| 1991 | if(samples <= 0) |
| 1992 | return OV_EINVAL; |
| 1993 | |
| 1994 | /* Here. */ |
| 1995 | if(filter) |
| 1996 | filter(pcm,channels,samples,filter_param); |
| 1997 | |
| 1998 | /* a tight loop to pack each size */ |
| 1999 | { |
| 2000 | int val; |
| 2001 | if(word==1){ |
| 2002 | int off=(sgned?0:128); |
| 2003 | vorbis_fpu_setround(&fpu); |
| 2004 | for(j=0;j<samples;j++) |
| 2005 | for(i=0;i<channels;i++){ |
| 2006 | val=vorbis_ftoi(pcm[i][j]*128.f); |
| 2007 | if(val>127)val=127; |
| 2008 | else if(val<-128)val=-128; |
| 2009 | *buffer++=val+off; |
| 2010 | } |
| 2011 | vorbis_fpu_restore(fpu); |
| 2012 | }else{ |
| 2013 | int off=(sgned?0:32768); |
| 2014 | |
| 2015 | if(host_endian==bigendianp){ |
| 2016 | if(sgned){ |
| 2017 | |
| 2018 | vorbis_fpu_setround(&fpu); |
| 2019 | for(i=0;i<channels;i++) { /* It's faster in this order */ |
| 2020 | float *src=pcm[i]; |
| 2021 | short *dest=((short *)buffer)+i; |
| 2022 | for(j=0;j<samples;j++) { |
| 2023 | val=vorbis_ftoi(src[j]*32768.f); |
| 2024 | if(val>32767)val=32767; |
| 2025 | else if(val<-32768)val=-32768; |
| 2026 | *dest=val; |
| 2027 | dest+=channels; |
| 2028 | } |
| 2029 | } |
| 2030 | vorbis_fpu_restore(fpu); |
| 2031 | |
| 2032 | }else{ |
| 2033 | |
| 2034 | vorbis_fpu_setround(&fpu); |
| 2035 | for(i=0;i<channels;i++) { |
| 2036 | float *src=pcm[i]; |
| 2037 | short *dest=((short *)buffer)+i; |
| 2038 | for(j=0;j<samples;j++) { |
| 2039 | val=vorbis_ftoi(src[j]*32768.f); |
| 2040 | if(val>32767)val=32767; |
| 2041 | else if(val<-32768)val=-32768; |
| 2042 | *dest=val+off; |
| 2043 | dest+=channels; |
| 2044 | } |
| 2045 | } |
| 2046 | vorbis_fpu_restore(fpu); |
| 2047 | |
| 2048 | } |
| 2049 | }else if(bigendianp){ |
| 2050 | |
| 2051 | vorbis_fpu_setround(&fpu); |
| 2052 | for(j=0;j<samples;j++) |
| 2053 | for(i=0;i<channels;i++){ |
| 2054 | val=vorbis_ftoi(pcm[i][j]*32768.f); |
| 2055 | if(val>32767)val=32767; |
| 2056 | else if(val<-32768)val=-32768; |
| 2057 | val+=off; |
| 2058 | *buffer++=(val>>8); |
| 2059 | *buffer++=(val&0xff); |
| 2060 | } |
| 2061 | vorbis_fpu_restore(fpu); |
| 2062 | |
| 2063 | }else{ |
| 2064 | //int val; |
| 2065 | vorbis_fpu_setround(&fpu); |
| 2066 | for(j=0;j<samples;j++) |
| 2067 | for(i=0;i<channels;i++){ |
| 2068 | val=vorbis_ftoi(pcm[i][j]*32768.f); |
| 2069 | if(val>32767)val=32767; |
| 2070 | else if(val<-32768)val=-32768; |
| 2071 | val+=off; |
| 2072 | *buffer++=(val&0xff); |
| 2073 | *buffer++=(val>>8); |
| 2074 | } |
| 2075 | vorbis_fpu_restore(fpu); |
| 2076 | |
| 2077 | } |
| 2078 | } |
| 2079 | } |
| 2080 | |
| 2081 | vorbis_synthesis_read(&vf->vd,samples); |
| 2082 | hs=vorbis_synthesis_halfrate_p(vf->vi); |
| 2083 | vf->pcm_offset+=(samples<<hs); |
| 2084 | if(bitstream)*bitstream=vf->current_link; |
| 2085 | return(samples*bytespersample); |
| 2086 | }else{ |
| 2087 | return(samples); |
| 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | long ov_read(OggVorbis_File *vf,char *buffer,int length, |
| 2092 | int bigendianp,int word,int sgned,int *bitstream){ |
| 2093 | return ov_read_filter(vf, buffer, length, bigendianp, word, sgned, bitstream, nullptr, nullptr); |
| 2094 | } |
| 2095 | |
| 2096 | /* input values: pcm_channels) a float vector per channel of output |
| 2097 | length) the sample length being read by the app |
| 2098 | |
| 2099 | return values: <0) error/hole in data (OV_HOLE), partial open (OV_EINVAL) |
| 2100 | 0) EOF |
| 2101 | n) number of samples of PCM actually returned. The |
| 2102 | below works on a packet-by-packet basis, so the |
| 2103 | return length is not related to the 'length' passed |
| 2104 | in, just guaranteed to fit. |
| 2105 | |
| 2106 | *section) set to the logical bitstream number */ |
| 2107 | |
| 2108 | |
| 2109 | |
| 2110 | long ov_read_float(OggVorbis_File *vf,float ***pcm_channels,int length, |
| 2111 | int *bitstream){ |
| 2112 | |
| 2113 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 2114 | |
| 2115 | while(1){ |
| 2116 | if(vf->ready_state==INITSET){ |
| 2117 | float **pcm; |
| 2118 | long samples=vorbis_synthesis_pcmout(&vf->vd,&pcm); |
| 2119 | if(samples){ |
| 2120 | int hs=vorbis_synthesis_halfrate_p(vf->vi); |
| 2121 | if(pcm_channels)*pcm_channels=pcm; |
| 2122 | if(samples>length)samples=length; |
| 2123 | vorbis_synthesis_read(&vf->vd,samples); |
| 2124 | vf->pcm_offset+=samples<<hs; |
| 2125 | if(bitstream)*bitstream=vf->current_link; |
| 2126 | return samples; |
| 2127 | |
| 2128 | } |
| 2129 | } |
| 2130 | |
| 2131 | /* suck in another packet */ |
| 2132 | { |
| 2133 | int ret=_fetch_and_process_packet(vf,nullptr,1,1); |
| 2134 | if(ret==OV_EOF)return(0); |
| 2135 | if(ret<=0)return(ret); |
| 2136 | } |
| 2137 | |
| 2138 | } |
| 2139 | } |
| 2140 | |
| 2141 | extern const float *vorbis_window(vorbis_dsp_state *v,int W); |
| 2142 | |
| 2143 | static void _ov_splice(float **pcm,float **lappcm, |
| 2144 | int n1, int n2, |
| 2145 | int ch1, int ch2, |
| 2146 | const float *w1, const float *w2){ |
| 2147 | int i,j; |
| 2148 | const float *w=w1; |
| 2149 | int n=n1; |
| 2150 | |
| 2151 | if(n1>n2){ |
| 2152 | n=n2; |
| 2153 | w=w2; |
| 2154 | } |
| 2155 | |
| 2156 | /* splice */ |
| 2157 | for(j=0;j<ch1 && j<ch2;j++){ |
| 2158 | float *s=lappcm[j]; |
| 2159 | float *d=pcm[j]; |
| 2160 | |
| 2161 | for(i=0;i<n;i++){ |
| 2162 | float wd=w[i]*w[i]; |
| 2163 | float ws=1.-wd; |
| 2164 | d[i]=d[i]*wd + s[i]*ws; |
| 2165 | } |
| 2166 | } |
| 2167 | /* window from zero */ |
| 2168 | for(;j<ch2;j++){ |
| 2169 | float *d=pcm[j]; |
| 2170 | for(i=0;i<n;i++){ |
| 2171 | float wd=w[i]*w[i]; |
| 2172 | d[i]=d[i]*wd; |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | } |
| 2177 | |
| 2178 | /* make sure vf is INITSET */ |
| 2179 | static int _ov_initset(OggVorbis_File *vf){ |
| 2180 | while(1){ |
| 2181 | if(vf->ready_state==INITSET)break; |
| 2182 | /* suck in another packet */ |
| 2183 | { |
| 2184 | int ret=_fetch_and_process_packet(vf,nullptr,1,0); |
| 2185 | if(ret<0 && ret!=OV_HOLE)return(ret); |
| 2186 | } |
| 2187 | } |
| 2188 | return 0; |
| 2189 | } |
| 2190 | |
| 2191 | /* make sure vf is INITSET and that we have a primed buffer; if |
| 2192 | we're crosslapping at a stream section boundary, this also makes |
| 2193 | sure we're sanity checking against the right stream information */ |
| 2194 | static int _ov_initprime(OggVorbis_File *vf){ |
| 2195 | vorbis_dsp_state *vd=&vf->vd; |
| 2196 | while(1){ |
| 2197 | if(vf->ready_state==INITSET) |
| 2198 | if(vorbis_synthesis_pcmout(vd,nullptr))break; |
| 2199 | |
| 2200 | /* suck in another packet */ |
| 2201 | { |
| 2202 | int ret=_fetch_and_process_packet(vf,nullptr,1,0); |
| 2203 | if(ret<0 && ret!=OV_HOLE)return(ret); |
| 2204 | } |
| 2205 | } |
| 2206 | return 0; |
| 2207 | } |
| 2208 | |
| 2209 | /* grab enough data for lapping from vf; this may be in the form of |
| 2210 | unreturned, already-decoded pcm, remaining PCM we will need to |
| 2211 | decode, or synthetic postextrapolation from last packets. */ |
| 2212 | static void _ov_getlap(OggVorbis_File *vf,vorbis_info *vi,vorbis_dsp_state *vd, |
| 2213 | float **lappcm,int lapsize){ |
| 2214 | int lapcount=0,i; |
| 2215 | float **pcm; |
| 2216 | |
| 2217 | /* try first to decode the lapping data */ |
| 2218 | while(lapcount<lapsize){ |
| 2219 | int samples=vorbis_synthesis_pcmout(vd,&pcm); |
| 2220 | if(samples){ |
| 2221 | if(samples>lapsize-lapcount)samples=lapsize-lapcount; |
| 2222 | for(i=0;i<vi->channels;i++) |
| 2223 | memcpy(lappcm[i]+lapcount,pcm[i],sizeof(**pcm)*samples); |
| 2224 | lapcount+=samples; |
| 2225 | vorbis_synthesis_read(vd,samples); |
| 2226 | }else{ |
| 2227 | /* suck in another packet */ |
| 2228 | int ret=_fetch_and_process_packet(vf,nullptr,1,0); /* do *not* span */ |
| 2229 | if(ret==OV_EOF)break; |
| 2230 | } |
| 2231 | } |
| 2232 | if(lapcount<lapsize){ |
| 2233 | /* failed to get lapping data from normal decode; pry it from the |
| 2234 | postextrapolation buffering, or the second half of the MDCT |
| 2235 | from the last packet */ |
| 2236 | int samples=vorbis_synthesis_lapout(&vf->vd,&pcm); |
| 2237 | if(samples==0){ |
| 2238 | for(i=0;i<vi->channels;i++) |
| 2239 | memset(lappcm[i]+lapcount,0,sizeof(**pcm)*lapsize-lapcount); |
| 2240 | lapcount=lapsize; |
| 2241 | }else{ |
| 2242 | if(samples>lapsize-lapcount)samples=lapsize-lapcount; |
| 2243 | for(i=0;i<vi->channels;i++) |
| 2244 | memcpy(lappcm[i]+lapcount,pcm[i],sizeof(**pcm)*samples); |
| 2245 | lapcount+=samples; |
| 2246 | } |
| 2247 | } |
| 2248 | } |
| 2249 | |
| 2250 | /* this sets up crosslapping of a sample by using trailing data from |
| 2251 | sample 1 and lapping it into the windowing buffer of sample 2 */ |
| 2252 | int ov_crosslap(OggVorbis_File *vf1, OggVorbis_File *vf2){ |
| 2253 | vorbis_info *vi1,*vi2; |
| 2254 | float **lappcm; |
| 2255 | float **pcm; |
| 2256 | const float *w1,*w2; |
| 2257 | int n1,n2,i,ret,hs1,hs2; |
| 2258 | |
| 2259 | if(vf1==vf2)return(0); /* degenerate case */ |
| 2260 | if(vf1->ready_state<OPENED)return(OV_EINVAL); |
| 2261 | if(vf2->ready_state<OPENED)return(OV_EINVAL); |
| 2262 | |
| 2263 | /* the relevant overlap buffers must be pre-checked and pre-primed |
| 2264 | before looking at settings in the event that priming would cross |
| 2265 | a bitstream boundary. So, do it now */ |
| 2266 | |
| 2267 | ret=_ov_initset(vf1); |
| 2268 | if(ret)return(ret); |
| 2269 | ret=_ov_initprime(vf2); |
| 2270 | if(ret)return(ret); |
| 2271 | |
| 2272 | vi1=ov_info(vf1,-1); |
| 2273 | vi2=ov_info(vf2,-1); |
| 2274 | hs1=ov_halfrate_p(vf1); |
| 2275 | hs2=ov_halfrate_p(vf2); |
| 2276 | |
| 2277 | lappcm=(float **)alloca(sizeof(*lappcm)*vi1->channels); |
| 2278 | n1=vorbis_info_blocksize(vi1,0)>>(1+hs1); |
| 2279 | n2=vorbis_info_blocksize(vi2,0)>>(1+hs2); |
| 2280 | w1=vorbis_window(&vf1->vd,0); |
| 2281 | w2=vorbis_window(&vf2->vd,0); |
| 2282 | |
| 2283 | for(i=0;i<vi1->channels;i++) |
| 2284 | lappcm[i]=(float *)alloca(sizeof(**lappcm)*n1); |
| 2285 | |
| 2286 | _ov_getlap(vf1,vi1,&vf1->vd,lappcm,n1); |
| 2287 | |
| 2288 | /* have a lapping buffer from vf1; now to splice it into the lapping |
| 2289 | buffer of vf2 */ |
| 2290 | /* consolidate and expose the buffer. */ |
| 2291 | vorbis_synthesis_lapout(&vf2->vd,&pcm); |
| 2292 | |
| 2293 | #if 0 |
| 2294 | _analysis_output_always("pcmL",0,pcm[0],n1*2,0,0,0); |
| 2295 | _analysis_output_always("pcmR",0,pcm[1],n1*2,0,0,0); |
| 2296 | #endif |
| 2297 | |
| 2298 | /* splice */ |
| 2299 | _ov_splice(pcm,lappcm,n1,n2,vi1->channels,vi2->channels,w1,w2); |
| 2300 | |
| 2301 | /* done */ |
| 2302 | return(0); |
| 2303 | } |
| 2304 | |
| 2305 | static int _ov_64_seek_lap(OggVorbis_File *vf,ogg_int64_t pos, |
| 2306 | int (*localseek)(OggVorbis_File *,ogg_int64_t)){ |
| 2307 | vorbis_info *vi; |
| 2308 | float **lappcm; |
| 2309 | float **pcm; |
| 2310 | const float *w1,*w2; |
| 2311 | int n1,n2,ch1,ch2,hs; |
| 2312 | int i,ret; |
| 2313 | |
| 2314 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 2315 | ret=_ov_initset(vf); |
| 2316 | if(ret)return(ret); |
| 2317 | vi=ov_info(vf,-1); |
| 2318 | hs=ov_halfrate_p(vf); |
| 2319 | |
| 2320 | ch1=vi->channels; |
| 2321 | n1=vorbis_info_blocksize(vi,0)>>(1+hs); |
| 2322 | w1=vorbis_window(&vf->vd,0); /* window arrays from libvorbis are |
| 2323 | persistent; even if the decode state |
| 2324 | from this link gets dumped, this |
| 2325 | window array continues to exist */ |
| 2326 | |
| 2327 | lappcm=(float **)alloca(sizeof(*lappcm)*ch1); |
| 2328 | for(i=0;i<ch1;i++) |
| 2329 | lappcm[i]=(float *)alloca(sizeof(**lappcm)*n1); |
| 2330 | _ov_getlap(vf,vi,&vf->vd,lappcm,n1); |
| 2331 | |
| 2332 | /* have lapping data; seek and prime the buffer */ |
| 2333 | ret=localseek(vf,pos); |
| 2334 | if(ret)return ret; |
| 2335 | ret=_ov_initprime(vf); |
| 2336 | if(ret)return(ret); |
| 2337 | |
| 2338 | /* Guard against cross-link changes; they're perfectly legal */ |
| 2339 | vi=ov_info(vf,-1); |
| 2340 | ch2=vi->channels; |
| 2341 | n2=vorbis_info_blocksize(vi,0)>>(1+hs); |
| 2342 | w2=vorbis_window(&vf->vd,0); |
| 2343 | |
| 2344 | /* consolidate and expose the buffer. */ |
| 2345 | vorbis_synthesis_lapout(&vf->vd,&pcm); |
| 2346 | |
| 2347 | /* splice */ |
| 2348 | _ov_splice(pcm,lappcm,n1,n2,ch1,ch2,w1,w2); |
| 2349 | |
| 2350 | /* done */ |
| 2351 | return(0); |
| 2352 | } |
| 2353 | |
| 2354 | int ov_raw_seek_lap(OggVorbis_File *vf,ogg_int64_t pos){ |
| 2355 | return _ov_64_seek_lap(vf,pos,ov_raw_seek); |
| 2356 | } |
| 2357 | |
| 2358 | int ov_pcm_seek_lap(OggVorbis_File *vf,ogg_int64_t pos){ |
| 2359 | return _ov_64_seek_lap(vf,pos,ov_pcm_seek); |
| 2360 | } |
| 2361 | |
| 2362 | int ov_pcm_seek_page_lap(OggVorbis_File *vf,ogg_int64_t pos){ |
| 2363 | return _ov_64_seek_lap(vf,pos,ov_pcm_seek_page); |
| 2364 | } |
| 2365 | |
| 2366 | static int _ov_d_seek_lap(OggVorbis_File *vf,double pos, |
| 2367 | int (*localseek)(OggVorbis_File *,double)){ |
| 2368 | vorbis_info *vi; |
| 2369 | float **lappcm; |
| 2370 | float **pcm; |
| 2371 | const float *w1,*w2; |
| 2372 | int n1,n2,ch1,ch2,hs; |
| 2373 | int i,ret; |
| 2374 | |
| 2375 | if(vf->ready_state<OPENED)return(OV_EINVAL); |
| 2376 | ret=_ov_initset(vf); |
| 2377 | if(ret)return(ret); |
| 2378 | vi=ov_info(vf,-1); |
| 2379 | hs=ov_halfrate_p(vf); |
| 2380 | |
| 2381 | ch1=vi->channels; |
| 2382 | n1=vorbis_info_blocksize(vi,0)>>(1+hs); |
| 2383 | w1=vorbis_window(&vf->vd,0); /* window arrays from libvorbis are |
| 2384 | persistent; even if the decode state |
| 2385 | from this link gets dumped, this |
| 2386 | window array continues to exist */ |
| 2387 | |
| 2388 | lappcm=(float **)alloca(sizeof(*lappcm)*ch1); |
| 2389 | for(i=0;i<ch1;i++) |
| 2390 | lappcm[i]=(float *)alloca(sizeof(**lappcm)*n1); |
| 2391 | _ov_getlap(vf,vi,&vf->vd,lappcm,n1); |
| 2392 | |
| 2393 | /* have lapping data; seek and prime the buffer */ |
| 2394 | ret=localseek(vf,pos); |
| 2395 | if(ret)return ret; |
| 2396 | ret=_ov_initprime(vf); |
| 2397 | if(ret)return(ret); |
| 2398 | |
| 2399 | /* Guard against cross-link changes; they're perfectly legal */ |
| 2400 | vi=ov_info(vf,-1); |
| 2401 | ch2=vi->channels; |
| 2402 | n2=vorbis_info_blocksize(vi,0)>>(1+hs); |
| 2403 | w2=vorbis_window(&vf->vd,0); |
| 2404 | |
| 2405 | /* consolidate and expose the buffer. */ |
| 2406 | vorbis_synthesis_lapout(&vf->vd,&pcm); |
| 2407 | |
| 2408 | /* splice */ |
| 2409 | _ov_splice(pcm,lappcm,n1,n2,ch1,ch2,w1,w2); |
| 2410 | |
| 2411 | /* done */ |
| 2412 | return(0); |
| 2413 | } |
| 2414 | |
| 2415 | int ov_time_seek_lap(OggVorbis_File *vf,double pos){ |
| 2416 | return _ov_d_seek_lap(vf,pos,ov_time_seek); |
| 2417 | } |
| 2418 | |
| 2419 | int ov_time_seek_page_lap(OggVorbis_File *vf,double pos){ |
| 2420 | return _ov_d_seek_lap(vf,pos,ov_time_seek_page); |
| 2421 | } |