Chris@1: /******************************************************************** Chris@1: * * Chris@1: * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * Chris@1: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * Chris@1: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * Chris@1: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * Chris@1: * * Chris@1: * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * Chris@1: * by the Xiph.Org Foundation http://www.xiph.org/ * Chris@1: * * Chris@1: ******************************************************************** Chris@1: Chris@1: function: stdio-based convenience library for opening/seeking/decoding Chris@1: last mod: $Id: vorbisfile.c 17573 2010-10-27 14:53:59Z xiphmont $ Chris@1: Chris@1: ********************************************************************/ Chris@1: Chris@1: #include Chris@1: #include Chris@1: #include Chris@1: #include Chris@1: #include Chris@1: Chris@1: #include "vorbis/codec.h" Chris@1: Chris@1: /* we don't need or want the static callback symbols here */ Chris@1: #define OV_EXCLUDE_STATIC_CALLBACKS Chris@1: #include "vorbis/vorbisfile.h" Chris@1: Chris@1: #include "os.h" Chris@1: #include "misc.h" Chris@1: Chris@1: /* A 'chained bitstream' is a Vorbis bitstream that contains more than Chris@1: one logical bitstream arranged end to end (the only form of Ogg Chris@1: multiplexing allowed in a Vorbis bitstream; grouping [parallel Chris@1: multiplexing] is not allowed in Vorbis) */ Chris@1: Chris@1: /* A Vorbis file can be played beginning to end (streamed) without Chris@1: worrying ahead of time about chaining (see decoder_example.c). If Chris@1: we have the whole file, however, and want random access Chris@1: (seeking/scrubbing) or desire to know the total length/time of a Chris@1: file, we need to account for the possibility of chaining. */ Chris@1: Chris@1: /* We can handle things a number of ways; we can determine the entire Chris@1: bitstream structure right off the bat, or find pieces on demand. Chris@1: This example determines and caches structure for the entire Chris@1: bitstream, but builds a virtual decoder on the fly when moving Chris@1: between links in the chain. */ Chris@1: Chris@1: /* There are also different ways to implement seeking. Enough Chris@1: information exists in an Ogg bitstream to seek to Chris@1: sample-granularity positions in the output. Or, one can seek by Chris@1: picking some portion of the stream roughly in the desired area if Chris@1: we only want coarse navigation through the stream. */ Chris@1: Chris@1: /************************************************************************* Chris@1: * Many, many internal helpers. The intention is not to be confusing; Chris@1: * rampant duplication and monolithic function implementation would be Chris@1: * harder to understand anyway. The high level functions are last. Begin Chris@1: * grokking near the end of the file */ Chris@1: Chris@1: /* read a little more data from the file/pipe into the ogg_sync framer Chris@1: */ Chris@1: #define CHUNKSIZE 65536 /* greater-than-page-size granularity seeking */ Chris@1: #define READSIZE 2048 /* a smaller read size is needed for low-rate streaming. */ Chris@1: Chris@1: static long _get_data(OggVorbis_File *vf){ Chris@1: errno=0; Chris@1: if(!(vf->callbacks.read_func))return(-1); Chris@1: if(vf->datasource){ Chris@1: char *buffer=ogg_sync_buffer(&vf->oy,READSIZE); Chris@1: long bytes=(vf->callbacks.read_func)(buffer,1,READSIZE,vf->datasource); Chris@1: if(bytes>0)ogg_sync_wrote(&vf->oy,bytes); Chris@1: if(bytes==0 && errno)return(-1); Chris@1: return(bytes); Chris@1: }else Chris@1: return(0); Chris@1: } Chris@1: Chris@1: /* save a tiny smidge of verbosity to make the code more readable */ Chris@1: static int _seek_helper(OggVorbis_File *vf,ogg_int64_t offset){ Chris@1: if(vf->datasource){ Chris@1: if(!(vf->callbacks.seek_func)|| Chris@1: (vf->callbacks.seek_func)(vf->datasource, offset, SEEK_SET) == -1) Chris@1: return OV_EREAD; Chris@1: vf->offset=offset; Chris@1: ogg_sync_reset(&vf->oy); Chris@1: }else{ Chris@1: /* shouldn't happen unless someone writes a broken callback */ Chris@1: return OV_EFAULT; Chris@1: } Chris@1: return 0; Chris@1: } Chris@1: Chris@1: /* The read/seek functions track absolute position within the stream */ Chris@1: Chris@1: /* from the head of the stream, get the next page. boundary specifies Chris@1: if the function is allowed to fetch more data from the stream (and Chris@1: how much) or only use internally buffered data. Chris@1: Chris@1: boundary: -1) unbounded search Chris@1: 0) read no additional data; use cached only Chris@1: n) search for a new page beginning for n bytes Chris@1: Chris@1: return: <0) did not find a page (OV_FALSE, OV_EOF, OV_EREAD) Chris@1: n) found a page at absolute offset n */ Chris@1: Chris@1: static ogg_int64_t _get_next_page(OggVorbis_File *vf,ogg_page *og, Chris@1: ogg_int64_t boundary){ Chris@1: if(boundary>0)boundary+=vf->offset; Chris@1: while(1){ Chris@1: long more; Chris@1: Chris@1: if(boundary>0 && vf->offset>=boundary)return(OV_FALSE); Chris@1: more=ogg_sync_pageseek(&vf->oy,og); Chris@1: Chris@1: if(more<0){ Chris@1: /* skipped n bytes */ Chris@1: vf->offset-=more; Chris@1: }else{ Chris@1: if(more==0){ Chris@1: /* send more paramedics */ Chris@1: if(!boundary)return(OV_FALSE); Chris@1: { Chris@1: long ret=_get_data(vf); Chris@1: if(ret==0)return(OV_EOF); Chris@1: if(ret<0)return(OV_EREAD); Chris@1: } Chris@1: }else{ Chris@1: /* got a page. Return the offset at the page beginning, Chris@1: advance the internal offset past the page end */ Chris@1: ogg_int64_t ret=vf->offset; Chris@1: vf->offset+=more; Chris@1: return(ret); Chris@1: Chris@1: } Chris@1: } Chris@1: } Chris@1: } Chris@1: Chris@1: /* find the latest page beginning before the current stream cursor Chris@1: position. Much dirtier than the above as Ogg doesn't have any Chris@1: backward search linkage. no 'readp' as it will certainly have to Chris@1: read. */ Chris@1: /* returns offset or OV_EREAD, OV_FAULT */ Chris@1: static ogg_int64_t _get_prev_page(OggVorbis_File *vf,ogg_page *og){ Chris@1: ogg_int64_t begin=vf->offset; Chris@1: ogg_int64_t end=begin; Chris@1: ogg_int64_t ret; Chris@1: ogg_int64_t offset=-1; Chris@1: Chris@1: while(offset==-1){ Chris@1: begin-=CHUNKSIZE; Chris@1: if(begin<0) Chris@1: begin=0; Chris@1: Chris@1: ret=_seek_helper(vf,begin); Chris@1: if(ret)return(ret); Chris@1: Chris@1: while(vf->offsetoffset); Chris@1: if(ret==OV_EREAD)return(OV_EREAD); Chris@1: if(ret<0){ Chris@1: break; Chris@1: }else{ Chris@1: offset=ret; Chris@1: } Chris@1: } Chris@1: } Chris@1: Chris@1: /* In a fully compliant, non-multiplexed stream, we'll still be Chris@1: holding the last page. In multiplexed (or noncompliant streams), Chris@1: we will probably have to re-read the last page we saw */ Chris@1: if(og->header_len==0){ Chris@1: ret=_seek_helper(vf,offset); Chris@1: if(ret)return(ret); Chris@1: Chris@1: ret=_get_next_page(vf,og,CHUNKSIZE); Chris@1: if(ret<0) Chris@1: /* this shouldn't be possible */ Chris@1: return(OV_EFAULT); Chris@1: } Chris@1: Chris@1: return(offset); Chris@1: } Chris@1: Chris@1: static void _add_serialno(ogg_page *og,long **serialno_list, int *n){ Chris@1: long s = ogg_page_serialno(og); Chris@1: (*n)++; Chris@1: Chris@1: if(*serialno_list){ Chris@1: *serialno_list = _ogg_realloc(*serialno_list, sizeof(**serialno_list)*(*n)); Chris@1: }else{ Chris@1: *serialno_list = _ogg_malloc(sizeof(**serialno_list)); Chris@1: } Chris@1: Chris@1: (*serialno_list)[(*n)-1] = s; Chris@1: } Chris@1: Chris@1: /* returns nonzero if found */ Chris@1: static int _lookup_serialno(long s, long *serialno_list, int n){ Chris@1: if(serialno_list){ Chris@1: while(n--){ Chris@1: if(*serialno_list == s) return 1; Chris@1: serialno_list++; Chris@1: } Chris@1: } Chris@1: return 0; Chris@1: } Chris@1: Chris@1: static int _lookup_page_serialno(ogg_page *og, long *serialno_list, int n){ Chris@1: long s = ogg_page_serialno(og); Chris@1: return _lookup_serialno(s,serialno_list,n); Chris@1: } Chris@1: Chris@1: /* performs the same search as _get_prev_page, but prefers pages of Chris@1: the specified serial number. If a page of the specified serialno is Chris@1: spotted during the seek-back-and-read-forward, it will return the Chris@1: info of last page of the matching serial number instead of the very Chris@1: last page. If no page of the specified serialno is seen, it will Chris@1: return the info of last page and alter *serialno. */ Chris@1: static ogg_int64_t _get_prev_page_serial(OggVorbis_File *vf, Chris@1: long *serial_list, int serial_n, Chris@1: int *serialno, ogg_int64_t *granpos){ Chris@1: ogg_page og; Chris@1: ogg_int64_t begin=vf->offset; Chris@1: ogg_int64_t end=begin; Chris@1: ogg_int64_t ret; Chris@1: Chris@1: ogg_int64_t prefoffset=-1; Chris@1: ogg_int64_t offset=-1; Chris@1: ogg_int64_t ret_serialno=-1; Chris@1: ogg_int64_t ret_gran=-1; Chris@1: Chris@1: while(offset==-1){ Chris@1: begin-=CHUNKSIZE; Chris@1: if(begin<0) Chris@1: begin=0; Chris@1: Chris@1: ret=_seek_helper(vf,begin); Chris@1: if(ret)return(ret); Chris@1: Chris@1: while(vf->offsetoffset); Chris@1: if(ret==OV_EREAD)return(OV_EREAD); Chris@1: if(ret<0){ Chris@1: break; Chris@1: }else{ Chris@1: ret_serialno=ogg_page_serialno(&og); Chris@1: ret_gran=ogg_page_granulepos(&og); Chris@1: offset=ret; Chris@1: Chris@1: if(ret_serialno == *serialno){ Chris@1: prefoffset=ret; Chris@1: *granpos=ret_gran; Chris@1: } Chris@1: Chris@1: if(!_lookup_serialno(ret_serialno,serial_list,serial_n)){ Chris@1: /* we fell off the end of the link, which means we seeked Chris@1: back too far and shouldn't have been looking in that link Chris@1: to begin with. If we found the preferred serial number, Chris@1: forget that we saw it. */ Chris@1: prefoffset=-1; Chris@1: } Chris@1: } Chris@1: } Chris@1: } Chris@1: Chris@1: /* we're not interested in the page... just the serialno and granpos. */ Chris@1: if(prefoffset>=0)return(prefoffset); Chris@1: Chris@1: *serialno = ret_serialno; Chris@1: *granpos = ret_gran; Chris@1: return(offset); Chris@1: Chris@1: } Chris@1: Chris@1: /* uses the local ogg_stream storage in vf; this is important for Chris@1: non-streaming input sources */ Chris@1: static int _fetch_headers(OggVorbis_File *vf,vorbis_info *vi,vorbis_comment *vc, Chris@1: long **serialno_list, int *serialno_n, Chris@1: ogg_page *og_ptr){ Chris@1: ogg_page og; Chris@1: ogg_packet op; Chris@1: int i,ret; Chris@1: int allbos=0; Chris@1: Chris@1: if(!og_ptr){ Chris@1: ogg_int64_t llret=_get_next_page(vf,&og,CHUNKSIZE); Chris@1: if(llret==OV_EREAD)return(OV_EREAD); Chris@1: if(llret<0)return(OV_ENOTVORBIS); Chris@1: og_ptr=&og; Chris@1: } Chris@1: Chris@1: vorbis_info_init(vi); Chris@1: vorbis_comment_init(vc); Chris@1: vf->ready_state=OPENED; Chris@1: Chris@1: /* extract the serialnos of all BOS pages + the first set of vorbis Chris@1: headers we see in the link */ Chris@1: Chris@1: while(ogg_page_bos(og_ptr)){ Chris@1: if(serialno_list){ Chris@1: if(_lookup_page_serialno(og_ptr,*serialno_list,*serialno_n)){ Chris@1: /* a dupe serialnumber in an initial header packet set == invalid stream */ Chris@1: if(*serialno_list)_ogg_free(*serialno_list); Chris@1: *serialno_list=0; Chris@1: *serialno_n=0; Chris@1: ret=OV_EBADHEADER; Chris@1: goto bail_header; Chris@1: } Chris@1: Chris@1: _add_serialno(og_ptr,serialno_list,serialno_n); Chris@1: } Chris@1: Chris@1: if(vf->ready_stateos,ogg_page_serialno(og_ptr)); Chris@1: ogg_stream_pagein(&vf->os,og_ptr); Chris@1: Chris@1: if(ogg_stream_packetout(&vf->os,&op) > 0 && Chris@1: vorbis_synthesis_idheader(&op)){ Chris@1: /* vorbis header; continue setup */ Chris@1: vf->ready_state=STREAMSET; Chris@1: if((ret=vorbis_synthesis_headerin(vi,vc,&op))){ Chris@1: ret=OV_EBADHEADER; Chris@1: goto bail_header; Chris@1: } Chris@1: } Chris@1: } Chris@1: Chris@1: /* get next page */ Chris@1: { Chris@1: ogg_int64_t llret=_get_next_page(vf,og_ptr,CHUNKSIZE); Chris@1: if(llret==OV_EREAD){ Chris@1: ret=OV_EREAD; Chris@1: goto bail_header; Chris@1: } Chris@1: if(llret<0){ Chris@1: ret=OV_ENOTVORBIS; Chris@1: goto bail_header; Chris@1: } Chris@1: Chris@1: /* if this page also belongs to our vorbis stream, submit it and break */ Chris@1: if(vf->ready_state==STREAMSET && Chris@1: vf->os.serialno == ogg_page_serialno(og_ptr)){ Chris@1: ogg_stream_pagein(&vf->os,og_ptr); Chris@1: break; Chris@1: } Chris@1: } Chris@1: } Chris@1: Chris@1: if(vf->ready_state!=STREAMSET){ Chris@1: ret = OV_ENOTVORBIS; Chris@1: goto bail_header; Chris@1: } Chris@1: Chris@1: while(1){ Chris@1: Chris@1: i=0; Chris@1: while(i<2){ /* get a page loop */ Chris@1: Chris@1: while(i<2){ /* get a packet loop */ Chris@1: Chris@1: int result=ogg_stream_packetout(&vf->os,&op); Chris@1: if(result==0)break; Chris@1: if(result==-1){ Chris@1: ret=OV_EBADHEADER; Chris@1: goto bail_header; Chris@1: } Chris@1: Chris@1: if((ret=vorbis_synthesis_headerin(vi,vc,&op))) Chris@1: goto bail_header; Chris@1: Chris@1: i++; Chris@1: } Chris@1: Chris@1: while(i<2){ Chris@1: if(_get_next_page(vf,og_ptr,CHUNKSIZE)<0){ Chris@1: ret=OV_EBADHEADER; Chris@1: goto bail_header; Chris@1: } Chris@1: Chris@1: /* if this page belongs to the correct stream, go parse it */ Chris@1: if(vf->os.serialno == ogg_page_serialno(og_ptr)){ Chris@1: ogg_stream_pagein(&vf->os,og_ptr); Chris@1: break; Chris@1: } Chris@1: Chris@1: /* if we never see the final vorbis headers before the link Chris@1: ends, abort */ Chris@1: if(ogg_page_bos(og_ptr)){ Chris@1: if(allbos){ Chris@1: ret = OV_EBADHEADER; Chris@1: goto bail_header; Chris@1: }else Chris@1: allbos=1; Chris@1: } Chris@1: Chris@1: /* otherwise, keep looking */ Chris@1: } Chris@1: } Chris@1: Chris@1: return 0; Chris@1: } Chris@1: Chris@1: bail_header: Chris@1: vorbis_info_clear(vi); Chris@1: vorbis_comment_clear(vc); Chris@1: vf->ready_state=OPENED; Chris@1: Chris@1: return ret; Chris@1: } Chris@1: Chris@1: /* Starting from current cursor position, get initial PCM offset of Chris@1: next page. Consumes the page in the process without decoding Chris@1: audio, however this is only called during stream parsing upon Chris@1: seekable open. */ Chris@1: static ogg_int64_t _initial_pcmoffset(OggVorbis_File *vf, vorbis_info *vi){ Chris@1: ogg_page og; Chris@1: ogg_int64_t accumulated=0; Chris@1: long lastblock=-1; Chris@1: int result; Chris@1: int serialno = vf->os.serialno; Chris@1: Chris@1: while(1){ Chris@1: ogg_packet op; Chris@1: if(_get_next_page(vf,&og,-1)<0) Chris@1: break; /* should not be possible unless the file is truncated/mangled */ Chris@1: Chris@1: if(ogg_page_bos(&og)) break; Chris@1: if(ogg_page_serialno(&og)!=serialno) continue; Chris@1: Chris@1: /* count blocksizes of all frames in the page */ Chris@1: ogg_stream_pagein(&vf->os,&og); Chris@1: while((result=ogg_stream_packetout(&vf->os,&op))){ Chris@1: if(result>0){ /* ignore holes */ Chris@1: long thisblock=vorbis_packet_blocksize(vi,&op); Chris@1: if(lastblock!=-1) Chris@1: accumulated+=(lastblock+thisblock)>>2; Chris@1: lastblock=thisblock; Chris@1: } Chris@1: } Chris@1: Chris@1: if(ogg_page_granulepos(&og)!=-1){ Chris@1: /* pcm offset of last packet on the first audio page */ Chris@1: accumulated= ogg_page_granulepos(&og)-accumulated; Chris@1: break; Chris@1: } Chris@1: } Chris@1: Chris@1: /* less than zero? Either a corrupt file or a stream with samples Chris@1: trimmed off the beginning, a normal occurrence; in both cases set Chris@1: the offset to zero */ Chris@1: if(accumulated<0)accumulated=0; Chris@1: Chris@1: return accumulated; Chris@1: } Chris@1: Chris@1: /* finds each bitstream link one at a time using a bisection search Chris@1: (has to begin by knowing the offset of the lb's initial page). Chris@1: Recurses for each link so it can alloc the link storage after Chris@1: finding them all, then unroll and fill the cache at the same time */ Chris@1: static int _bisect_forward_serialno(OggVorbis_File *vf, Chris@1: ogg_int64_t begin, Chris@1: ogg_int64_t searched, Chris@1: ogg_int64_t end, Chris@1: ogg_int64_t endgran, Chris@1: int endserial, Chris@1: long *currentno_list, Chris@1: int currentnos, Chris@1: long m){ Chris@1: ogg_int64_t pcmoffset; Chris@1: ogg_int64_t dataoffset=searched; Chris@1: ogg_int64_t endsearched=end; Chris@1: ogg_int64_t next=end; Chris@1: ogg_int64_t searchgran=-1; Chris@1: ogg_page og; Chris@1: ogg_int64_t ret,last; Chris@1: int serialno = vf->os.serialno; Chris@1: Chris@1: /* invariants: Chris@1: we have the headers and serialnos for the link beginning at 'begin' Chris@1: we have the offset and granpos of the last page in the file (potentially Chris@1: not a page we care about) Chris@1: */ Chris@1: Chris@1: /* Is the last page in our list of current serialnumbers? */ Chris@1: if(_lookup_serialno(endserial,currentno_list,currentnos)){ Chris@1: Chris@1: /* last page is in the starting serialno list, so we've bisected Chris@1: down to (or just started with) a single link. Now we need to Chris@1: find the last vorbis page belonging to the first vorbis stream Chris@1: for this link. */ Chris@1: Chris@1: while(endserial != serialno){ Chris@1: endserial = serialno; Chris@1: vf->offset=_get_prev_page_serial(vf,currentno_list,currentnos,&endserial,&endgran); Chris@1: } Chris@1: Chris@1: vf->links=m+1; Chris@1: if(vf->offsets)_ogg_free(vf->offsets); Chris@1: if(vf->serialnos)_ogg_free(vf->serialnos); Chris@1: if(vf->dataoffsets)_ogg_free(vf->dataoffsets); Chris@1: Chris@1: vf->offsets=_ogg_malloc((vf->links+1)*sizeof(*vf->offsets)); Chris@1: vf->vi=_ogg_realloc(vf->vi,vf->links*sizeof(*vf->vi)); Chris@1: vf->vc=_ogg_realloc(vf->vc,vf->links*sizeof(*vf->vc)); Chris@1: vf->serialnos=_ogg_malloc(vf->links*sizeof(*vf->serialnos)); Chris@1: vf->dataoffsets=_ogg_malloc(vf->links*sizeof(*vf->dataoffsets)); Chris@1: vf->pcmlengths=_ogg_malloc(vf->links*2*sizeof(*vf->pcmlengths)); Chris@1: Chris@1: vf->offsets[m+1]=end; Chris@1: vf->offsets[m]=begin; Chris@1: vf->pcmlengths[m*2+1]=(endgran<0?0:endgran); Chris@1: Chris@1: }else{ Chris@1: Chris@1: long *next_serialno_list=NULL; Chris@1: int next_serialnos=0; Chris@1: vorbis_info vi; Chris@1: vorbis_comment vc; Chris@1: Chris@1: /* the below guards against garbage seperating the last and Chris@1: first pages of two links. */ Chris@1: while(searchedoffset){ Chris@1: ret=_seek_helper(vf,bisect); Chris@1: if(ret)return(ret); Chris@1: } Chris@1: Chris@1: last=_get_next_page(vf,&og,-1); Chris@1: if(last==OV_EREAD)return(OV_EREAD); Chris@1: if(last<0 || !_lookup_page_serialno(&og,currentno_list,currentnos)){ Chris@1: endsearched=bisect; Chris@1: if(last>=0)next=last; Chris@1: }else{ Chris@1: searched=vf->offset; Chris@1: } Chris@1: } Chris@1: Chris@1: /* Bisection point found */ Chris@1: Chris@1: /* for the time being, fetch end PCM offset the simple way */ Chris@1: { Chris@1: int testserial = serialno+1; Chris@1: vf->offset = next; Chris@1: while(testserial != serialno){ Chris@1: testserial = serialno; Chris@1: vf->offset=_get_prev_page_serial(vf,currentno_list,currentnos,&testserial,&searchgran); Chris@1: } Chris@1: } Chris@1: Chris@1: if(vf->offset!=next){ Chris@1: ret=_seek_helper(vf,next); Chris@1: if(ret)return(ret); Chris@1: } Chris@1: Chris@1: ret=_fetch_headers(vf,&vi,&vc,&next_serialno_list,&next_serialnos,NULL); Chris@1: if(ret)return(ret); Chris@1: serialno = vf->os.serialno; Chris@1: dataoffset = vf->offset; Chris@1: Chris@1: /* this will consume a page, however the next bistection always Chris@1: starts with a raw seek */ Chris@1: pcmoffset = _initial_pcmoffset(vf,&vi); Chris@1: Chris@1: ret=_bisect_forward_serialno(vf,next,vf->offset,end,endgran,endserial, Chris@1: next_serialno_list,next_serialnos,m+1); Chris@1: if(ret)return(ret); Chris@1: Chris@1: if(next_serialno_list)_ogg_free(next_serialno_list); Chris@1: Chris@1: vf->offsets[m+1]=next; Chris@1: vf->serialnos[m+1]=serialno; Chris@1: vf->dataoffsets[m+1]=dataoffset; Chris@1: Chris@1: vf->vi[m+1]=vi; Chris@1: vf->vc[m+1]=vc; Chris@1: Chris@1: vf->pcmlengths[m*2+1]=searchgran; Chris@1: vf->pcmlengths[m*2+2]=pcmoffset; Chris@1: vf->pcmlengths[m*2+3]-=pcmoffset; Chris@1: if(vf->pcmlengths[m*2+3]<0)vf->pcmlengths[m*2+3]=0; Chris@1: } Chris@1: return(0); Chris@1: } Chris@1: Chris@1: static int _make_decode_ready(OggVorbis_File *vf){ Chris@1: if(vf->ready_state>STREAMSET)return 0; Chris@1: if(vf->ready_stateseekable){ Chris@1: if(vorbis_synthesis_init(&vf->vd,vf->vi+vf->current_link)) Chris@1: return OV_EBADLINK; Chris@1: }else{ Chris@1: if(vorbis_synthesis_init(&vf->vd,vf->vi)) Chris@1: return OV_EBADLINK; Chris@1: } Chris@1: vorbis_block_init(&vf->vd,&vf->vb); Chris@1: vf->ready_state=INITSET; Chris@1: vf->bittrack=0.f; Chris@1: vf->samptrack=0.f; Chris@1: return 0; Chris@1: } Chris@1: Chris@1: static int _open_seekable2(OggVorbis_File *vf){ Chris@1: ogg_int64_t dataoffset=vf->dataoffsets[0],end,endgran=-1; Chris@1: int endserial=vf->os.serialno; Chris@1: int serialno=vf->os.serialno; Chris@1: Chris@1: /* we're partially open and have a first link header state in Chris@1: storage in vf */ Chris@1: Chris@1: /* fetch initial PCM offset */ Chris@1: ogg_int64_t pcmoffset = _initial_pcmoffset(vf,vf->vi); Chris@1: Chris@1: /* we can seek, so set out learning all about this file */ Chris@1: if(vf->callbacks.seek_func && vf->callbacks.tell_func){ Chris@1: (vf->callbacks.seek_func)(vf->datasource,0,SEEK_END); Chris@1: vf->offset=vf->end=(vf->callbacks.tell_func)(vf->datasource); Chris@1: }else{ Chris@1: vf->offset=vf->end=-1; Chris@1: } Chris@1: Chris@1: /* If seek_func is implemented, tell_func must also be implemented */ Chris@1: if(vf->end==-1) return(OV_EINVAL); Chris@1: Chris@1: /* Get the offset of the last page of the physical bitstream, or, if Chris@1: we're lucky the last vorbis page of this link as most OggVorbis Chris@1: files will contain a single logical bitstream */ Chris@1: end=_get_prev_page_serial(vf,vf->serialnos+2,vf->serialnos[1],&endserial,&endgran); Chris@1: if(end<0)return(end); Chris@1: Chris@1: /* now determine bitstream structure recursively */ Chris@1: if(_bisect_forward_serialno(vf,0,dataoffset,vf->offset,endgran,endserial, Chris@1: vf->serialnos+2,vf->serialnos[1],0)<0)return(OV_EREAD); Chris@1: Chris@1: vf->offsets[0]=0; Chris@1: vf->serialnos[0]=serialno; Chris@1: vf->dataoffsets[0]=dataoffset; Chris@1: vf->pcmlengths[0]=pcmoffset; Chris@1: vf->pcmlengths[1]-=pcmoffset; Chris@1: if(vf->pcmlengths[1]<0)vf->pcmlengths[1]=0; Chris@1: Chris@1: return(ov_raw_seek(vf,dataoffset)); Chris@1: } Chris@1: Chris@1: /* clear out the current logical bitstream decoder */ Chris@1: static void _decode_clear(OggVorbis_File *vf){ Chris@1: vorbis_dsp_clear(&vf->vd); Chris@1: vorbis_block_clear(&vf->vb); Chris@1: vf->ready_state=OPENED; Chris@1: } Chris@1: Chris@1: /* fetch and process a packet. Handles the case where we're at a Chris@1: bitstream boundary and dumps the decoding machine. If the decoding Chris@1: machine is unloaded, it loads it. It also keeps pcm_offset up to Chris@1: date (seek and read both use this. seek uses a special hack with Chris@1: readp). Chris@1: Chris@1: return: <0) error, OV_HOLE (lost packet) or OV_EOF Chris@1: 0) need more data (only if readp==0) Chris@1: 1) got a packet Chris@1: */ Chris@1: Chris@1: static int _fetch_and_process_packet(OggVorbis_File *vf, Chris@1: ogg_packet *op_in, Chris@1: int readp, Chris@1: int spanp){ Chris@1: ogg_page og; Chris@1: Chris@1: /* handle one packet. Try to fetch it from current stream state */ Chris@1: /* extract packets from page */ Chris@1: while(1){ Chris@1: Chris@1: if(vf->ready_state==STREAMSET){ Chris@1: int ret=_make_decode_ready(vf); Chris@1: if(ret<0)return ret; Chris@1: } Chris@1: Chris@1: /* process a packet if we can. */ Chris@1: Chris@1: if(vf->ready_state==INITSET){ Chris@1: int hs=vorbis_synthesis_halfrate_p(vf->vi); Chris@1: Chris@1: while(1) { Chris@1: ogg_packet op; Chris@1: ogg_packet *op_ptr=(op_in?op_in:&op); Chris@1: int result=ogg_stream_packetout(&vf->os,op_ptr); Chris@1: ogg_int64_t granulepos; Chris@1: Chris@1: op_in=NULL; Chris@1: if(result==-1)return(OV_HOLE); /* hole in the data. */ Chris@1: if(result>0){ Chris@1: /* got a packet. process it */ Chris@1: granulepos=op_ptr->granulepos; Chris@1: if(!vorbis_synthesis(&vf->vb,op_ptr)){ /* lazy check for lazy Chris@1: header handling. The Chris@1: header packets aren't Chris@1: audio, so if/when we Chris@1: submit them, Chris@1: vorbis_synthesis will Chris@1: reject them */ Chris@1: Chris@1: /* suck in the synthesis data and track bitrate */ Chris@1: { Chris@1: int oldsamples=vorbis_synthesis_pcmout(&vf->vd,NULL); Chris@1: /* for proper use of libvorbis within libvorbisfile, Chris@1: oldsamples will always be zero. */ Chris@1: if(oldsamples)return(OV_EFAULT); Chris@1: Chris@1: vorbis_synthesis_blockin(&vf->vd,&vf->vb); Chris@1: vf->samptrack+=(vorbis_synthesis_pcmout(&vf->vd,NULL)<bittrack+=op_ptr->bytes*8; Chris@1: } Chris@1: Chris@1: /* update the pcm offset. */ Chris@1: if(granulepos!=-1 && !op_ptr->e_o_s){ Chris@1: int link=(vf->seekable?vf->current_link:0); Chris@1: int i,samples; Chris@1: Chris@1: /* this packet has a pcm_offset on it (the last packet Chris@1: completed on a page carries the offset) After processing Chris@1: (above), we know the pcm position of the *last* sample Chris@1: ready to be returned. Find the offset of the *first* Chris@1: Chris@1: As an aside, this trick is inaccurate if we begin Chris@1: reading anew right at the last page; the end-of-stream Chris@1: granulepos declares the last frame in the stream, and the Chris@1: last packet of the last page may be a partial frame. Chris@1: So, we need a previous granulepos from an in-sequence page Chris@1: to have a reference point. Thus the !op_ptr->e_o_s clause Chris@1: above */ Chris@1: Chris@1: if(vf->seekable && link>0) Chris@1: granulepos-=vf->pcmlengths[link*2]; Chris@1: if(granulepos<0)granulepos=0; /* actually, this Chris@1: shouldn't be possible Chris@1: here unless the stream Chris@1: is very broken */ Chris@1: Chris@1: samples=(vorbis_synthesis_pcmout(&vf->vd,NULL)<pcmlengths[i*2+1]; Chris@1: vf->pcm_offset=granulepos; Chris@1: } Chris@1: return(1); Chris@1: } Chris@1: } Chris@1: else Chris@1: break; Chris@1: } Chris@1: } Chris@1: Chris@1: if(vf->ready_state>=OPENED){ Chris@1: ogg_int64_t ret; Chris@1: Chris@1: while(1){ Chris@1: /* the loop is not strictly necessary, but there's no sense in Chris@1: doing the extra checks of the larger loop for the common Chris@1: case in a multiplexed bistream where the page is simply Chris@1: part of a different logical bitstream; keep reading until Chris@1: we get one with the correct serialno */ Chris@1: Chris@1: if(!readp)return(0); Chris@1: if((ret=_get_next_page(vf,&og,-1))<0){ Chris@1: return(OV_EOF); /* eof. leave unitialized */ Chris@1: } Chris@1: Chris@1: /* bitrate tracking; add the header's bytes here, the body bytes Chris@1: are done by packet above */ Chris@1: vf->bittrack+=og.header_len*8; Chris@1: Chris@1: if(vf->ready_state==INITSET){ Chris@1: if(vf->current_serialno!=ogg_page_serialno(&og)){ Chris@1: Chris@1: /* two possibilities: Chris@1: 1) our decoding just traversed a bitstream boundary Chris@1: 2) another stream is multiplexed into this logical section */ Chris@1: Chris@1: if(ogg_page_bos(&og)){ Chris@1: /* boundary case */ Chris@1: if(!spanp) Chris@1: return(OV_EOF); Chris@1: Chris@1: _decode_clear(vf); Chris@1: Chris@1: if(!vf->seekable){ Chris@1: vorbis_info_clear(vf->vi); Chris@1: vorbis_comment_clear(vf->vc); Chris@1: } Chris@1: break; Chris@1: Chris@1: }else Chris@1: continue; /* possibility #2 */ Chris@1: } Chris@1: } Chris@1: Chris@1: break; Chris@1: } Chris@1: } Chris@1: Chris@1: /* Do we need to load a new machine before submitting the page? */ Chris@1: /* This is different in the seekable and non-seekable cases. Chris@1: Chris@1: In the seekable case, we already have all the header Chris@1: information loaded and cached; we just initialize the machine Chris@1: with it and continue on our merry way. Chris@1: Chris@1: In the non-seekable (streaming) case, we'll only be at a Chris@1: boundary if we just left the previous logical bitstream and Chris@1: we're now nominally at the header of the next bitstream Chris@1: */ Chris@1: Chris@1: if(vf->ready_state!=INITSET){ Chris@1: int link; Chris@1: Chris@1: if(vf->ready_stateseekable){ Chris@1: long serialno = ogg_page_serialno(&og); Chris@1: Chris@1: /* match the serialno to bitstream section. We use this rather than Chris@1: offset positions to avoid problems near logical bitstream Chris@1: boundaries */ Chris@1: Chris@1: for(link=0;linklinks;link++) Chris@1: if(vf->serialnos[link]==serialno)break; Chris@1: Chris@1: if(link==vf->links) continue; /* not the desired Vorbis Chris@1: bitstream section; keep Chris@1: trying */ Chris@1: Chris@1: vf->current_serialno=serialno; Chris@1: vf->current_link=link; Chris@1: Chris@1: ogg_stream_reset_serialno(&vf->os,vf->current_serialno); Chris@1: vf->ready_state=STREAMSET; Chris@1: Chris@1: }else{ Chris@1: /* we're streaming */ Chris@1: /* fetch the three header packets, build the info struct */ Chris@1: Chris@1: int ret=_fetch_headers(vf,vf->vi,vf->vc,NULL,NULL,&og); Chris@1: if(ret)return(ret); Chris@1: vf->current_serialno=vf->os.serialno; Chris@1: vf->current_link++; Chris@1: link=0; Chris@1: } Chris@1: } Chris@1: } Chris@1: Chris@1: /* the buffered page is the data we want, and we're ready for it; Chris@1: add it to the stream state */ Chris@1: ogg_stream_pagein(&vf->os,&og); Chris@1: Chris@1: } Chris@1: } Chris@1: Chris@1: /* if, eg, 64 bit stdio is configured by default, this will build with Chris@1: fseek64 */ Chris@1: static int _fseek64_wrap(FILE *f,ogg_int64_t off,int whence){ Chris@1: if(f==NULL)return(-1); Chris@1: return fseek(f,off,whence); Chris@1: } Chris@1: Chris@1: static int _ov_open1(void *f,OggVorbis_File *vf,const char *initial, Chris@1: long ibytes, ov_callbacks callbacks){ Chris@1: int offsettest=((f && callbacks.seek_func)?callbacks.seek_func(f,0,SEEK_CUR):-1); Chris@1: long *serialno_list=NULL; Chris@1: int serialno_list_size=0; Chris@1: int ret; Chris@1: Chris@1: memset(vf,0,sizeof(*vf)); Chris@1: vf->datasource=f; Chris@1: vf->callbacks = callbacks; Chris@1: Chris@1: /* init the framing state */ Chris@1: ogg_sync_init(&vf->oy); Chris@1: Chris@1: /* perhaps some data was previously read into a buffer for testing Chris@1: against other stream types. Allow initialization from this Chris@1: previously read data (especially as we may be reading from a Chris@1: non-seekable stream) */ Chris@1: if(initial){ Chris@1: char *buffer=ogg_sync_buffer(&vf->oy,ibytes); Chris@1: memcpy(buffer,initial,ibytes); Chris@1: ogg_sync_wrote(&vf->oy,ibytes); Chris@1: } Chris@1: Chris@1: /* can we seek? Stevens suggests the seek test was portable */ Chris@1: if(offsettest!=-1)vf->seekable=1; Chris@1: Chris@1: /* No seeking yet; Set up a 'single' (current) logical bitstream Chris@1: entry for partial open */ Chris@1: vf->links=1; Chris@1: vf->vi=_ogg_calloc(vf->links,sizeof(*vf->vi)); Chris@1: vf->vc=_ogg_calloc(vf->links,sizeof(*vf->vc)); Chris@1: ogg_stream_init(&vf->os,-1); /* fill in the serialno later */ Chris@1: Chris@1: /* Fetch all BOS pages, store the vorbis header and all seen serial Chris@1: numbers, load subsequent vorbis setup headers */ Chris@1: if((ret=_fetch_headers(vf,vf->vi,vf->vc,&serialno_list,&serialno_list_size,NULL))<0){ Chris@1: vf->datasource=NULL; Chris@1: ov_clear(vf); Chris@1: }else{ Chris@1: /* serial number list for first link needs to be held somewhere Chris@1: for second stage of seekable stream open; this saves having to Chris@1: seek/reread first link's serialnumber data then. */ Chris@1: vf->serialnos=_ogg_calloc(serialno_list_size+2,sizeof(*vf->serialnos)); Chris@1: vf->serialnos[0]=vf->current_serialno=vf->os.serialno; Chris@1: vf->serialnos[1]=serialno_list_size; Chris@1: memcpy(vf->serialnos+2,serialno_list,serialno_list_size*sizeof(*vf->serialnos)); Chris@1: Chris@1: vf->offsets=_ogg_calloc(1,sizeof(*vf->offsets)); Chris@1: vf->dataoffsets=_ogg_calloc(1,sizeof(*vf->dataoffsets)); Chris@1: vf->offsets[0]=0; Chris@1: vf->dataoffsets[0]=vf->offset; Chris@1: Chris@1: vf->ready_state=PARTOPEN; Chris@1: } Chris@1: if(serialno_list)_ogg_free(serialno_list); Chris@1: return(ret); Chris@1: } Chris@1: Chris@1: static int _ov_open2(OggVorbis_File *vf){ Chris@1: if(vf->ready_state != PARTOPEN) return OV_EINVAL; Chris@1: vf->ready_state=OPENED; Chris@1: if(vf->seekable){ Chris@1: int ret=_open_seekable2(vf); Chris@1: if(ret){ Chris@1: vf->datasource=NULL; Chris@1: ov_clear(vf); Chris@1: } Chris@1: return(ret); Chris@1: }else Chris@1: vf->ready_state=STREAMSET; Chris@1: Chris@1: return 0; Chris@1: } Chris@1: Chris@1: Chris@1: /* clear out the OggVorbis_File struct */ Chris@1: int ov_clear(OggVorbis_File *vf){ Chris@1: if(vf){ Chris@1: vorbis_block_clear(&vf->vb); Chris@1: vorbis_dsp_clear(&vf->vd); Chris@1: ogg_stream_clear(&vf->os); Chris@1: Chris@1: if(vf->vi && vf->links){ Chris@1: int i; Chris@1: for(i=0;ilinks;i++){ Chris@1: vorbis_info_clear(vf->vi+i); Chris@1: vorbis_comment_clear(vf->vc+i); Chris@1: } Chris@1: _ogg_free(vf->vi); Chris@1: _ogg_free(vf->vc); Chris@1: } Chris@1: if(vf->dataoffsets)_ogg_free(vf->dataoffsets); Chris@1: if(vf->pcmlengths)_ogg_free(vf->pcmlengths); Chris@1: if(vf->serialnos)_ogg_free(vf->serialnos); Chris@1: if(vf->offsets)_ogg_free(vf->offsets); Chris@1: ogg_sync_clear(&vf->oy); Chris@1: if(vf->datasource && vf->callbacks.close_func) Chris@1: (vf->callbacks.close_func)(vf->datasource); Chris@1: memset(vf,0,sizeof(*vf)); Chris@1: } Chris@1: #ifdef DEBUG_LEAKS Chris@1: _VDBG_dump(); Chris@1: #endif Chris@1: return(0); Chris@1: } Chris@1: Chris@1: /* inspects the OggVorbis file and finds/documents all the logical Chris@1: bitstreams contained in it. Tries to be tolerant of logical Chris@1: bitstream sections that are truncated/woogie. Chris@1: Chris@1: return: -1) error Chris@1: 0) OK Chris@1: */ Chris@1: Chris@1: int ov_open_callbacks(void *f,OggVorbis_File *vf, Chris@1: const char *initial,long ibytes,ov_callbacks callbacks){ Chris@1: int ret=_ov_open1(f,vf,initial,ibytes,callbacks); Chris@1: if(ret)return ret; Chris@1: return _ov_open2(vf); Chris@1: } Chris@1: Chris@1: int ov_open(FILE *f,OggVorbis_File *vf,const char *initial,long ibytes){ Chris@1: ov_callbacks callbacks = { Chris@1: (size_t (*)(void *, size_t, size_t, void *)) fread, Chris@1: (int (*)(void *, ogg_int64_t, int)) _fseek64_wrap, Chris@1: (int (*)(void *)) fclose, Chris@1: (long (*)(void *)) ftell Chris@1: }; Chris@1: Chris@1: return ov_open_callbacks((void *)f, vf, initial, ibytes, callbacks); Chris@1: } Chris@1: Chris@1: int ov_fopen(const char *path,OggVorbis_File *vf){ Chris@1: int ret; Chris@1: FILE *f = fopen(path,"rb"); Chris@1: if(!f) return -1; Chris@1: Chris@1: ret = ov_open(f,vf,NULL,0); Chris@1: if(ret) fclose(f); Chris@1: return ret; Chris@1: } Chris@1: Chris@1: Chris@1: /* cheap hack for game usage where downsampling is desirable; there's Chris@1: no need for SRC as we can just do it cheaply in libvorbis. */ Chris@1: Chris@1: int ov_halfrate(OggVorbis_File *vf,int flag){ Chris@1: int i; Chris@1: if(vf->vi==NULL)return OV_EINVAL; Chris@1: if(vf->ready_state>STREAMSET){ Chris@1: /* clear out stream state; dumping the decode machine is needed to Chris@1: reinit the MDCT lookups. */ Chris@1: vorbis_dsp_clear(&vf->vd); Chris@1: vorbis_block_clear(&vf->vb); Chris@1: vf->ready_state=STREAMSET; Chris@1: if(vf->pcm_offset>=0){ Chris@1: ogg_int64_t pos=vf->pcm_offset; Chris@1: vf->pcm_offset=-1; /* make sure the pos is dumped if unseekable */ Chris@1: ov_pcm_seek(vf,pos); Chris@1: } Chris@1: } Chris@1: Chris@1: for(i=0;ilinks;i++){ Chris@1: if(vorbis_synthesis_halfrate(vf->vi+i,flag)){ Chris@1: if(flag) ov_halfrate(vf,0); Chris@1: return OV_EINVAL; Chris@1: } Chris@1: } Chris@1: return 0; Chris@1: } Chris@1: Chris@1: int ov_halfrate_p(OggVorbis_File *vf){ Chris@1: if(vf->vi==NULL)return OV_EINVAL; Chris@1: return vorbis_synthesis_halfrate_p(vf->vi); Chris@1: } Chris@1: Chris@1: /* Only partially open the vorbis file; test for Vorbisness, and load Chris@1: the headers for the first chain. Do not seek (although test for Chris@1: seekability). Use ov_test_open to finish opening the file, else Chris@1: ov_clear to close/free it. Same return codes as open. */ Chris@1: Chris@1: int ov_test_callbacks(void *f,OggVorbis_File *vf, Chris@1: const char *initial,long ibytes,ov_callbacks callbacks) Chris@1: { Chris@1: return _ov_open1(f,vf,initial,ibytes,callbacks); Chris@1: } Chris@1: Chris@1: int ov_test(FILE *f,OggVorbis_File *vf,const char *initial,long ibytes){ Chris@1: ov_callbacks callbacks = { Chris@1: (size_t (*)(void *, size_t, size_t, void *)) fread, Chris@1: (int (*)(void *, ogg_int64_t, int)) _fseek64_wrap, Chris@1: (int (*)(void *)) fclose, Chris@1: (long (*)(void *)) ftell Chris@1: }; Chris@1: Chris@1: return ov_test_callbacks((void *)f, vf, initial, ibytes, callbacks); Chris@1: } Chris@1: Chris@1: int ov_test_open(OggVorbis_File *vf){ Chris@1: if(vf->ready_state!=PARTOPEN)return(OV_EINVAL); Chris@1: return _ov_open2(vf); Chris@1: } Chris@1: Chris@1: /* How many logical bitstreams in this physical bitstream? */ Chris@1: long ov_streams(OggVorbis_File *vf){ Chris@1: return vf->links; Chris@1: } Chris@1: Chris@1: /* Is the FILE * associated with vf seekable? */ Chris@1: long ov_seekable(OggVorbis_File *vf){ Chris@1: return vf->seekable; Chris@1: } Chris@1: Chris@1: /* returns the bitrate for a given logical bitstream or the entire Chris@1: physical bitstream. If the file is open for random access, it will Chris@1: find the *actual* average bitrate. If the file is streaming, it Chris@1: returns the nominal bitrate (if set) else the average of the Chris@1: upper/lower bounds (if set) else -1 (unset). Chris@1: Chris@1: If you want the actual bitrate field settings, get them from the Chris@1: vorbis_info structs */ Chris@1: Chris@1: long ov_bitrate(OggVorbis_File *vf,int i){ Chris@1: if(vf->ready_state=vf->links)return(OV_EINVAL); Chris@1: if(!vf->seekable && i!=0)return(ov_bitrate(vf,0)); Chris@1: if(i<0){ Chris@1: ogg_int64_t bits=0; Chris@1: int i; Chris@1: float br; Chris@1: for(i=0;ilinks;i++) Chris@1: bits+=(vf->offsets[i+1]-vf->dataoffsets[i])*8; Chris@1: /* This once read: return(rint(bits/ov_time_total(vf,-1))); Chris@1: * gcc 3.x on x86 miscompiled this at optimisation level 2 and above, Chris@1: * so this is slightly transformed to make it work. Chris@1: */ Chris@1: br = bits/ov_time_total(vf,-1); Chris@1: return(rint(br)); Chris@1: }else{ Chris@1: if(vf->seekable){ Chris@1: /* return the actual bitrate */ Chris@1: return(rint((vf->offsets[i+1]-vf->dataoffsets[i])*8/ov_time_total(vf,i))); Chris@1: }else{ Chris@1: /* return nominal if set */ Chris@1: if(vf->vi[i].bitrate_nominal>0){ Chris@1: return vf->vi[i].bitrate_nominal; Chris@1: }else{ Chris@1: if(vf->vi[i].bitrate_upper>0){ Chris@1: if(vf->vi[i].bitrate_lower>0){ Chris@1: return (vf->vi[i].bitrate_upper+vf->vi[i].bitrate_lower)/2; Chris@1: }else{ Chris@1: return vf->vi[i].bitrate_upper; Chris@1: } Chris@1: } Chris@1: return(OV_FALSE); Chris@1: } Chris@1: } Chris@1: } Chris@1: } Chris@1: Chris@1: /* returns the actual bitrate since last call. returns -1 if no Chris@1: additional data to offer since last call (or at beginning of stream), Chris@1: EINVAL if stream is only partially open Chris@1: */ Chris@1: long ov_bitrate_instant(OggVorbis_File *vf){ Chris@1: int link=(vf->seekable?vf->current_link:0); Chris@1: long ret; Chris@1: if(vf->ready_statesamptrack==0)return(OV_FALSE); Chris@1: ret=vf->bittrack/vf->samptrack*vf->vi[link].rate+.5; Chris@1: vf->bittrack=0.f; Chris@1: vf->samptrack=0.f; Chris@1: return(ret); Chris@1: } Chris@1: Chris@1: /* Guess */ Chris@1: long ov_serialnumber(OggVorbis_File *vf,int i){ Chris@1: if(i>=vf->links)return(ov_serialnumber(vf,vf->links-1)); Chris@1: if(!vf->seekable && i>=0)return(ov_serialnumber(vf,-1)); Chris@1: if(i<0){ Chris@1: return(vf->current_serialno); Chris@1: }else{ Chris@1: return(vf->serialnos[i]); Chris@1: } Chris@1: } Chris@1: Chris@1: /* returns: total raw (compressed) length of content if i==-1 Chris@1: raw (compressed) length of that logical bitstream for i==0 to n Chris@1: OV_EINVAL if the stream is not seekable (we can't know the length) Chris@1: or if stream is only partially open Chris@1: */ Chris@1: ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i){ Chris@1: if(vf->ready_stateseekable || i>=vf->links)return(OV_EINVAL); Chris@1: if(i<0){ Chris@1: ogg_int64_t acc=0; Chris@1: int i; Chris@1: for(i=0;ilinks;i++) Chris@1: acc+=ov_raw_total(vf,i); Chris@1: return(acc); Chris@1: }else{ Chris@1: return(vf->offsets[i+1]-vf->offsets[i]); Chris@1: } Chris@1: } Chris@1: Chris@1: /* returns: total PCM length (samples) of content if i==-1 PCM length Chris@1: (samples) of that logical bitstream for i==0 to n Chris@1: OV_EINVAL if the stream is not seekable (we can't know the Chris@1: length) or only partially open Chris@1: */ Chris@1: ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i){ Chris@1: if(vf->ready_stateseekable || i>=vf->links)return(OV_EINVAL); Chris@1: if(i<0){ Chris@1: ogg_int64_t acc=0; Chris@1: int i; Chris@1: for(i=0;ilinks;i++) Chris@1: acc+=ov_pcm_total(vf,i); Chris@1: return(acc); Chris@1: }else{ Chris@1: return(vf->pcmlengths[i*2+1]); Chris@1: } Chris@1: } Chris@1: Chris@1: /* returns: total seconds of content if i==-1 Chris@1: seconds in that logical bitstream for i==0 to n Chris@1: OV_EINVAL if the stream is not seekable (we can't know the Chris@1: length) or only partially open Chris@1: */ Chris@1: double ov_time_total(OggVorbis_File *vf,int i){ Chris@1: if(vf->ready_stateseekable || i>=vf->links)return(OV_EINVAL); Chris@1: if(i<0){ Chris@1: double acc=0; Chris@1: int i; Chris@1: for(i=0;ilinks;i++) Chris@1: acc+=ov_time_total(vf,i); Chris@1: return(acc); Chris@1: }else{ Chris@1: return((double)(vf->pcmlengths[i*2+1])/vf->vi[i].rate); Chris@1: } Chris@1: } Chris@1: Chris@1: /* seek to an offset relative to the *compressed* data. This also Chris@1: scans packets to update the PCM cursor. It will cross a logical Chris@1: bitstream boundary, but only if it can't get any packets out of the Chris@1: tail of the bitstream we seek to (so no surprises). Chris@1: Chris@1: returns zero on success, nonzero on failure */ Chris@1: Chris@1: int ov_raw_seek(OggVorbis_File *vf,ogg_int64_t pos){ Chris@1: ogg_stream_state work_os; Chris@1: int ret; Chris@1: Chris@1: if(vf->ready_stateseekable) Chris@1: return(OV_ENOSEEK); /* don't dump machine if we can't seek */ Chris@1: Chris@1: if(pos<0 || pos>vf->end)return(OV_EINVAL); Chris@1: Chris@1: /* is the seek position outside our current link [if any]? */ Chris@1: if(vf->ready_state>=STREAMSET){ Chris@1: if(posoffsets[vf->current_link] || pos>=vf->offsets[vf->current_link+1]) Chris@1: _decode_clear(vf); /* clear out stream state */ Chris@1: } Chris@1: Chris@1: /* don't yet clear out decoding machine (if it's initialized), in Chris@1: the case we're in the same link. Restart the decode lapping, and Chris@1: let _fetch_and_process_packet deal with a potential bitstream Chris@1: boundary */ Chris@1: vf->pcm_offset=-1; Chris@1: ogg_stream_reset_serialno(&vf->os, Chris@1: vf->current_serialno); /* must set serialno */ Chris@1: vorbis_synthesis_restart(&vf->vd); Chris@1: Chris@1: ret=_seek_helper(vf,pos); Chris@1: if(ret)goto seek_error; Chris@1: Chris@1: /* we need to make sure the pcm_offset is set, but we don't want to Chris@1: advance the raw cursor past good packets just to get to the first Chris@1: with a granulepos. That's not equivalent behavior to beginning Chris@1: decoding as immediately after the seek position as possible. Chris@1: Chris@1: So, a hack. We use two stream states; a local scratch state and Chris@1: the shared vf->os stream state. We use the local state to Chris@1: scan, and the shared state as a buffer for later decode. Chris@1: Chris@1: Unfortuantely, on the last page we still advance to last packet Chris@1: because the granulepos on the last page is not necessarily on a Chris@1: packet boundary, and we need to make sure the granpos is Chris@1: correct. Chris@1: */ Chris@1: Chris@1: { Chris@1: ogg_page og; Chris@1: ogg_packet op; Chris@1: int lastblock=0; Chris@1: int accblock=0; Chris@1: int thisblock=0; Chris@1: int lastflag=0; Chris@1: int firstflag=0; Chris@1: ogg_int64_t pagepos=-1; Chris@1: Chris@1: ogg_stream_init(&work_os,vf->current_serialno); /* get the memory ready */ Chris@1: ogg_stream_reset(&work_os); /* eliminate the spurious OV_HOLE Chris@1: return from not necessarily Chris@1: starting from the beginning */ Chris@1: Chris@1: while(1){ Chris@1: if(vf->ready_state>=STREAMSET){ Chris@1: /* snarf/scan a packet if we can */ Chris@1: int result=ogg_stream_packetout(&work_os,&op); Chris@1: Chris@1: if(result>0){ Chris@1: Chris@1: if(vf->vi[vf->current_link].codec_setup){ Chris@1: thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op); Chris@1: if(thisblock<0){ Chris@1: ogg_stream_packetout(&vf->os,NULL); Chris@1: thisblock=0; Chris@1: }else{ Chris@1: Chris@1: /* We can't get a guaranteed correct pcm position out of the Chris@1: last page in a stream because it might have a 'short' Chris@1: granpos, which can only be detected in the presence of a Chris@1: preceding page. However, if the last page is also the first Chris@1: page, the granpos rules of a first page take precedence. Not Chris@1: only that, but for first==last, the EOS page must be treated Chris@1: as if its a normal first page for the stream to open/play. */ Chris@1: if(lastflag && !firstflag) Chris@1: ogg_stream_packetout(&vf->os,NULL); Chris@1: else Chris@1: if(lastblock)accblock+=(lastblock+thisblock)>>2; Chris@1: } Chris@1: Chris@1: if(op.granulepos!=-1){ Chris@1: int i,link=vf->current_link; Chris@1: ogg_int64_t granulepos=op.granulepos-vf->pcmlengths[link*2]; Chris@1: if(granulepos<0)granulepos=0; Chris@1: Chris@1: for(i=0;ipcmlengths[i*2+1]; Chris@1: vf->pcm_offset=granulepos-accblock; Chris@1: if(vf->pcm_offset<0)vf->pcm_offset=0; Chris@1: break; Chris@1: } Chris@1: lastblock=thisblock; Chris@1: continue; Chris@1: }else Chris@1: ogg_stream_packetout(&vf->os,NULL); Chris@1: } Chris@1: } Chris@1: Chris@1: if(!lastblock){ Chris@1: pagepos=_get_next_page(vf,&og,-1); Chris@1: if(pagepos<0){ Chris@1: vf->pcm_offset=ov_pcm_total(vf,-1); Chris@1: break; Chris@1: } Chris@1: }else{ Chris@1: /* huh? Bogus stream with packets but no granulepos */ Chris@1: vf->pcm_offset=-1; Chris@1: break; Chris@1: } Chris@1: Chris@1: /* has our decoding just traversed a bitstream boundary? */ Chris@1: if(vf->ready_state>=STREAMSET){ Chris@1: if(vf->current_serialno!=ogg_page_serialno(&og)){ Chris@1: Chris@1: /* two possibilities: Chris@1: 1) our decoding just traversed a bitstream boundary Chris@1: 2) another stream is multiplexed into this logical section? */ Chris@1: Chris@1: if(ogg_page_bos(&og)){ Chris@1: /* we traversed */ Chris@1: _decode_clear(vf); /* clear out stream state */ Chris@1: ogg_stream_clear(&work_os); Chris@1: } /* else, do nothing; next loop will scoop another page */ Chris@1: } Chris@1: } Chris@1: Chris@1: if(vf->ready_statelinks;link++) Chris@1: if(vf->serialnos[link]==serialno)break; Chris@1: Chris@1: if(link==vf->links) continue; /* not the desired Vorbis Chris@1: bitstream section; keep Chris@1: trying */ Chris@1: vf->current_link=link; Chris@1: vf->current_serialno=serialno; Chris@1: ogg_stream_reset_serialno(&vf->os,serialno); Chris@1: ogg_stream_reset_serialno(&work_os,serialno); Chris@1: vf->ready_state=STREAMSET; Chris@1: firstflag=(pagepos<=vf->dataoffsets[link]); Chris@1: } Chris@1: Chris@1: ogg_stream_pagein(&vf->os,&og); Chris@1: ogg_stream_pagein(&work_os,&og); Chris@1: lastflag=ogg_page_eos(&og); Chris@1: Chris@1: } Chris@1: } Chris@1: Chris@1: ogg_stream_clear(&work_os); Chris@1: vf->bittrack=0.f; Chris@1: vf->samptrack=0.f; Chris@1: return(0); Chris@1: Chris@1: seek_error: Chris@1: /* dump the machine so we're in a known state */ Chris@1: vf->pcm_offset=-1; Chris@1: ogg_stream_clear(&work_os); Chris@1: _decode_clear(vf); Chris@1: return OV_EBADLINK; Chris@1: } Chris@1: Chris@1: /* Page granularity seek (faster than sample granularity because we Chris@1: don't do the last bit of decode to find a specific sample). Chris@1: Chris@1: Seek to the last [granule marked] page preceding the specified pos Chris@1: location, such that decoding past the returned point will quickly Chris@1: arrive at the requested position. */ Chris@1: int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos){ Chris@1: int link=-1; Chris@1: ogg_int64_t result=0; Chris@1: ogg_int64_t total=ov_pcm_total(vf,-1); Chris@1: Chris@1: if(vf->ready_stateseekable)return(OV_ENOSEEK); Chris@1: Chris@1: if(pos<0 || pos>total)return(OV_EINVAL); Chris@1: Chris@1: /* which bitstream section does this pcm offset occur in? */ Chris@1: for(link=vf->links-1;link>=0;link--){ Chris@1: total-=vf->pcmlengths[link*2+1]; Chris@1: if(pos>=total)break; Chris@1: } Chris@1: Chris@1: /* search within the logical bitstream for the page with the highest Chris@1: pcm_pos preceding (or equal to) pos. There is a danger here; Chris@1: missing pages or incorrect frame number information in the Chris@1: bitstream could make our task impossible. Account for that (it Chris@1: would be an error condition) */ Chris@1: Chris@1: /* new search algorithm by HB (Nicholas Vinen) */ Chris@1: { Chris@1: ogg_int64_t end=vf->offsets[link+1]; Chris@1: ogg_int64_t begin=vf->offsets[link]; Chris@1: ogg_int64_t begintime = vf->pcmlengths[link*2]; Chris@1: ogg_int64_t endtime = vf->pcmlengths[link*2+1]+begintime; Chris@1: ogg_int64_t target=pos-total+begintime; Chris@1: ogg_int64_t best=begin; Chris@1: Chris@1: ogg_page og; Chris@1: while(beginoffset){ Chris@1: result=_seek_helper(vf,bisect); Chris@1: if(result) goto seek_error; Chris@1: } Chris@1: Chris@1: while(beginoffset); Chris@1: if(result==OV_EREAD) goto seek_error; Chris@1: if(result<0){ Chris@1: if(bisect<=begin+1) Chris@1: end=begin; /* found it */ Chris@1: else{ Chris@1: if(bisect==0) goto seek_error; Chris@1: bisect-=CHUNKSIZE; Chris@1: if(bisect<=begin)bisect=begin+1; Chris@1: result=_seek_helper(vf,bisect); Chris@1: if(result) goto seek_error; Chris@1: } Chris@1: }else{ Chris@1: ogg_int64_t granulepos; Chris@1: Chris@1: if(ogg_page_serialno(&og)!=vf->serialnos[link]) Chris@1: continue; Chris@1: Chris@1: granulepos=ogg_page_granulepos(&og); Chris@1: if(granulepos==-1)continue; Chris@1: Chris@1: if(granuleposoffset; /* raw offset of next page */ Chris@1: begintime=granulepos; Chris@1: Chris@1: if(target-begintime>44100)break; Chris@1: bisect=begin; /* *not* begin + 1 */ Chris@1: }else{ Chris@1: if(bisect<=begin+1) Chris@1: end=begin; /* found it */ Chris@1: else{ Chris@1: if(end==vf->offset){ /* we're pretty close - we'd be stuck in */ Chris@1: end=result; Chris@1: bisect-=CHUNKSIZE; /* an endless loop otherwise. */ Chris@1: if(bisect<=begin)bisect=begin+1; Chris@1: result=_seek_helper(vf,bisect); Chris@1: if(result) goto seek_error; Chris@1: }else{ Chris@1: end=bisect; Chris@1: endtime=granulepos; Chris@1: break; Chris@1: } Chris@1: } Chris@1: } Chris@1: } Chris@1: } Chris@1: } Chris@1: Chris@1: /* found our page. seek to it, update pcm offset. Easier case than Chris@1: raw_seek, don't keep packets preceding granulepos. */ Chris@1: { Chris@1: ogg_page og; Chris@1: ogg_packet op; Chris@1: Chris@1: /* seek */ Chris@1: result=_seek_helper(vf,best); Chris@1: vf->pcm_offset=-1; Chris@1: if(result) goto seek_error; Chris@1: result=_get_next_page(vf,&og,-1); Chris@1: if(result<0) goto seek_error; Chris@1: Chris@1: if(link!=vf->current_link){ Chris@1: /* Different link; dump entire decode machine */ Chris@1: _decode_clear(vf); Chris@1: Chris@1: vf->current_link=link; Chris@1: vf->current_serialno=vf->serialnos[link]; Chris@1: vf->ready_state=STREAMSET; Chris@1: Chris@1: }else{ Chris@1: vorbis_synthesis_restart(&vf->vd); Chris@1: } Chris@1: Chris@1: ogg_stream_reset_serialno(&vf->os,vf->current_serialno); Chris@1: ogg_stream_pagein(&vf->os,&og); Chris@1: Chris@1: /* pull out all but last packet; the one with granulepos */ Chris@1: while(1){ Chris@1: result=ogg_stream_packetpeek(&vf->os,&op); Chris@1: if(result==0){ Chris@1: /* !!! the packet finishing this page originated on a Chris@1: preceding page. Keep fetching previous pages until we Chris@1: get one with a granulepos or without the 'continued' flag Chris@1: set. Then just use raw_seek for simplicity. */ Chris@1: Chris@1: result=_seek_helper(vf,best); Chris@1: if(result<0) goto seek_error; Chris@1: Chris@1: while(1){ Chris@1: result=_get_prev_page(vf,&og); Chris@1: if(result<0) goto seek_error; Chris@1: if(ogg_page_serialno(&og)==vf->current_serialno && Chris@1: (ogg_page_granulepos(&og)>-1 || Chris@1: !ogg_page_continued(&og))){ Chris@1: return ov_raw_seek(vf,result); Chris@1: } Chris@1: vf->offset=result; Chris@1: } Chris@1: } Chris@1: if(result<0){ Chris@1: result = OV_EBADPACKET; Chris@1: goto seek_error; Chris@1: } Chris@1: if(op.granulepos!=-1){ Chris@1: vf->pcm_offset=op.granulepos-vf->pcmlengths[vf->current_link*2]; Chris@1: if(vf->pcm_offset<0)vf->pcm_offset=0; Chris@1: vf->pcm_offset+=total; Chris@1: break; Chris@1: }else Chris@1: result=ogg_stream_packetout(&vf->os,NULL); Chris@1: } Chris@1: } Chris@1: } Chris@1: Chris@1: /* verify result */ Chris@1: if(vf->pcm_offset>pos || pos>ov_pcm_total(vf,-1)){ Chris@1: result=OV_EFAULT; Chris@1: goto seek_error; Chris@1: } Chris@1: vf->bittrack=0.f; Chris@1: vf->samptrack=0.f; Chris@1: return(0); Chris@1: Chris@1: seek_error: Chris@1: /* dump machine so we're in a known state */ Chris@1: vf->pcm_offset=-1; Chris@1: _decode_clear(vf); Chris@1: return (int)result; Chris@1: } Chris@1: Chris@1: /* seek to a sample offset relative to the decompressed pcm stream Chris@1: returns zero on success, nonzero on failure */ Chris@1: Chris@1: int ov_pcm_seek(OggVorbis_File *vf,ogg_int64_t pos){ Chris@1: int thisblock,lastblock=0; Chris@1: int ret=ov_pcm_seek_page(vf,pos); Chris@1: if(ret<0)return(ret); Chris@1: if((ret=_make_decode_ready(vf)))return ret; Chris@1: Chris@1: /* discard leading packets we don't need for the lapping of the Chris@1: position we want; don't decode them */ Chris@1: Chris@1: while(1){ Chris@1: ogg_packet op; Chris@1: ogg_page og; Chris@1: Chris@1: int ret=ogg_stream_packetpeek(&vf->os,&op); Chris@1: if(ret>0){ Chris@1: thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op); Chris@1: if(thisblock<0){ Chris@1: ogg_stream_packetout(&vf->os,NULL); Chris@1: continue; /* non audio packet */ Chris@1: } Chris@1: if(lastblock)vf->pcm_offset+=(lastblock+thisblock)>>2; Chris@1: Chris@1: if(vf->pcm_offset+((thisblock+ Chris@1: vorbis_info_blocksize(vf->vi,1))>>2)>=pos)break; Chris@1: Chris@1: /* remove the packet from packet queue and track its granulepos */ Chris@1: ogg_stream_packetout(&vf->os,NULL); Chris@1: vorbis_synthesis_trackonly(&vf->vb,&op); /* set up a vb with Chris@1: only tracking, no Chris@1: pcm_decode */ Chris@1: vorbis_synthesis_blockin(&vf->vd,&vf->vb); Chris@1: Chris@1: /* end of logical stream case is hard, especially with exact Chris@1: length positioning. */ Chris@1: Chris@1: if(op.granulepos>-1){ Chris@1: int i; Chris@1: /* always believe the stream markers */ Chris@1: vf->pcm_offset=op.granulepos-vf->pcmlengths[vf->current_link*2]; Chris@1: if(vf->pcm_offset<0)vf->pcm_offset=0; Chris@1: for(i=0;icurrent_link;i++) Chris@1: vf->pcm_offset+=vf->pcmlengths[i*2+1]; Chris@1: } Chris@1: Chris@1: lastblock=thisblock; Chris@1: Chris@1: }else{ Chris@1: if(ret<0 && ret!=OV_HOLE)break; Chris@1: Chris@1: /* suck in a new page */ Chris@1: if(_get_next_page(vf,&og,-1)<0)break; Chris@1: if(ogg_page_bos(&og))_decode_clear(vf); Chris@1: Chris@1: if(vf->ready_statelinks;link++) Chris@1: if(vf->serialnos[link]==serialno)break; Chris@1: if(link==vf->links) continue; Chris@1: vf->current_link=link; Chris@1: Chris@1: vf->ready_state=STREAMSET; Chris@1: vf->current_serialno=ogg_page_serialno(&og); Chris@1: ogg_stream_reset_serialno(&vf->os,serialno); Chris@1: ret=_make_decode_ready(vf); Chris@1: if(ret)return ret; Chris@1: lastblock=0; Chris@1: } Chris@1: Chris@1: ogg_stream_pagein(&vf->os,&og); Chris@1: } Chris@1: } Chris@1: Chris@1: vf->bittrack=0.f; Chris@1: vf->samptrack=0.f; Chris@1: /* discard samples until we reach the desired position. Crossing a Chris@1: logical bitstream boundary with abandon is OK. */ Chris@1: { Chris@1: /* note that halfrate could be set differently in each link, but Chris@1: vorbisfile encoforces all links are set or unset */ Chris@1: int hs=vorbis_synthesis_halfrate_p(vf->vi); Chris@1: while(vf->pcm_offset<((pos>>hs)<pcm_offset)>>hs; Chris@1: long samples=vorbis_synthesis_pcmout(&vf->vd,NULL); Chris@1: Chris@1: if(samples>target)samples=target; Chris@1: vorbis_synthesis_read(&vf->vd,samples); Chris@1: vf->pcm_offset+=samples<pcm_offset=ov_pcm_total(vf,-1); /* eof */ Chris@1: } Chris@1: } Chris@1: return 0; Chris@1: } Chris@1: Chris@1: /* seek to a playback time relative to the decompressed pcm stream Chris@1: returns zero on success, nonzero on failure */ Chris@1: int ov_time_seek(OggVorbis_File *vf,double seconds){ Chris@1: /* translate time to PCM position and call ov_pcm_seek */ Chris@1: Chris@1: int link=-1; Chris@1: ogg_int64_t pcm_total=0; Chris@1: double time_total=0.; Chris@1: Chris@1: if(vf->ready_stateseekable)return(OV_ENOSEEK); Chris@1: if(seconds<0)return(OV_EINVAL); Chris@1: Chris@1: /* which bitstream section does this time offset occur in? */ Chris@1: for(link=0;linklinks;link++){ Chris@1: double addsec = ov_time_total(vf,link); Chris@1: if(secondspcmlengths[link*2+1]; Chris@1: } Chris@1: Chris@1: if(link==vf->links)return(OV_EINVAL); Chris@1: Chris@1: /* enough information to convert time offset to pcm offset */ Chris@1: { Chris@1: ogg_int64_t target=pcm_total+(seconds-time_total)*vf->vi[link].rate; Chris@1: return(ov_pcm_seek(vf,target)); Chris@1: } Chris@1: } Chris@1: Chris@1: /* page-granularity version of ov_time_seek Chris@1: returns zero on success, nonzero on failure */ Chris@1: int ov_time_seek_page(OggVorbis_File *vf,double seconds){ Chris@1: /* translate time to PCM position and call ov_pcm_seek */ Chris@1: Chris@1: int link=-1; Chris@1: ogg_int64_t pcm_total=0; Chris@1: double time_total=0.; Chris@1: Chris@1: if(vf->ready_stateseekable)return(OV_ENOSEEK); Chris@1: if(seconds<0)return(OV_EINVAL); Chris@1: Chris@1: /* which bitstream section does this time offset occur in? */ Chris@1: for(link=0;linklinks;link++){ Chris@1: double addsec = ov_time_total(vf,link); Chris@1: if(secondspcmlengths[link*2+1]; Chris@1: } Chris@1: Chris@1: if(link==vf->links)return(OV_EINVAL); Chris@1: Chris@1: /* enough information to convert time offset to pcm offset */ Chris@1: { Chris@1: ogg_int64_t target=pcm_total+(seconds-time_total)*vf->vi[link].rate; Chris@1: return(ov_pcm_seek_page(vf,target)); Chris@1: } Chris@1: } Chris@1: Chris@1: /* tell the current stream offset cursor. Note that seek followed by Chris@1: tell will likely not give the set offset due to caching */ Chris@1: ogg_int64_t ov_raw_tell(OggVorbis_File *vf){ Chris@1: if(vf->ready_stateoffset); Chris@1: } Chris@1: Chris@1: /* return PCM offset (sample) of next PCM sample to be read */ Chris@1: ogg_int64_t ov_pcm_tell(OggVorbis_File *vf){ Chris@1: if(vf->ready_statepcm_offset); Chris@1: } Chris@1: Chris@1: /* return time offset (seconds) of next PCM sample to be read */ Chris@1: double ov_time_tell(OggVorbis_File *vf){ Chris@1: int link=0; Chris@1: ogg_int64_t pcm_total=0; Chris@1: double time_total=0.f; Chris@1: Chris@1: if(vf->ready_stateseekable){ Chris@1: pcm_total=ov_pcm_total(vf,-1); Chris@1: time_total=ov_time_total(vf,-1); Chris@1: Chris@1: /* which bitstream section does this time offset occur in? */ Chris@1: for(link=vf->links-1;link>=0;link--){ Chris@1: pcm_total-=vf->pcmlengths[link*2+1]; Chris@1: time_total-=ov_time_total(vf,link); Chris@1: if(vf->pcm_offset>=pcm_total)break; Chris@1: } Chris@1: } Chris@1: Chris@1: return((double)time_total+(double)(vf->pcm_offset-pcm_total)/vf->vi[link].rate); Chris@1: } Chris@1: Chris@1: /* link: -1) return the vorbis_info struct for the bitstream section Chris@1: currently being decoded Chris@1: 0-n) to request information for a specific bitstream section Chris@1: Chris@1: In the case of a non-seekable bitstream, any call returns the Chris@1: current bitstream. NULL in the case that the machine is not Chris@1: initialized */ Chris@1: Chris@1: vorbis_info *ov_info(OggVorbis_File *vf,int link){ Chris@1: if(vf->seekable){ Chris@1: if(link<0) Chris@1: if(vf->ready_state>=STREAMSET) Chris@1: return vf->vi+vf->current_link; Chris@1: else Chris@1: return vf->vi; Chris@1: else Chris@1: if(link>=vf->links) Chris@1: return NULL; Chris@1: else Chris@1: return vf->vi+link; Chris@1: }else{ Chris@1: return vf->vi; Chris@1: } Chris@1: } Chris@1: Chris@1: /* grr, strong typing, grr, no templates/inheritence, grr */ Chris@1: vorbis_comment *ov_comment(OggVorbis_File *vf,int link){ Chris@1: if(vf->seekable){ Chris@1: if(link<0) Chris@1: if(vf->ready_state>=STREAMSET) Chris@1: return vf->vc+vf->current_link; Chris@1: else Chris@1: return vf->vc; Chris@1: else Chris@1: if(link>=vf->links) Chris@1: return NULL; Chris@1: else Chris@1: return vf->vc+link; Chris@1: }else{ Chris@1: return vf->vc; Chris@1: } Chris@1: } Chris@1: Chris@1: static int host_is_big_endian() { Chris@1: ogg_int32_t pattern = 0xfeedface; /* deadbeef */ Chris@1: unsigned char *bytewise = (unsigned char *)&pattern; Chris@1: if (bytewise[0] == 0xfe) return 1; Chris@1: return 0; Chris@1: } Chris@1: Chris@1: /* up to this point, everything could more or less hide the multiple Chris@1: logical bitstream nature of chaining from the toplevel application Chris@1: if the toplevel application didn't particularly care. However, at Chris@1: the point that we actually read audio back, the multiple-section Chris@1: nature must surface: Multiple bitstream sections do not necessarily Chris@1: have to have the same number of channels or sampling rate. Chris@1: Chris@1: ov_read returns the sequential logical bitstream number currently Chris@1: being decoded along with the PCM data in order that the toplevel Chris@1: application can take action on channel/sample rate changes. This Chris@1: number will be incremented even for streamed (non-seekable) streams Chris@1: (for seekable streams, it represents the actual logical bitstream Chris@1: index within the physical bitstream. Note that the accessor Chris@1: functions above are aware of this dichotomy). Chris@1: Chris@1: ov_read_filter is exactly the same as ov_read except that it processes Chris@1: the decoded audio data through a filter before packing it into the Chris@1: requested format. This gives greater accuracy than applying a filter Chris@1: after the audio has been converted into integral PCM. Chris@1: Chris@1: input values: buffer) a buffer to hold packed PCM data for return Chris@1: length) the byte length requested to be placed into buffer Chris@1: bigendianp) should the data be packed LSB first (0) or Chris@1: MSB first (1) Chris@1: word) word size for output. currently 1 (byte) or Chris@1: 2 (16 bit short) Chris@1: Chris@1: return values: <0) error/hole in data (OV_HOLE), partial open (OV_EINVAL) Chris@1: 0) EOF Chris@1: n) number of bytes of PCM actually returned. The Chris@1: below works on a packet-by-packet basis, so the Chris@1: return length is not related to the 'length' passed Chris@1: in, just guaranteed to fit. Chris@1: Chris@1: *section) set to the logical bitstream number */ Chris@1: Chris@1: long ov_read_filter(OggVorbis_File *vf,char *buffer,int length, Chris@1: int bigendianp,int word,int sgned,int *bitstream, Chris@1: void (*filter)(float **pcm,long channels,long samples,void *filter_param),void *filter_param){ Chris@1: int i,j; Chris@1: int host_endian = host_is_big_endian(); Chris@1: int hs; Chris@1: Chris@1: float **pcm; Chris@1: long samples; Chris@1: Chris@1: if(vf->ready_stateready_state==INITSET){ Chris@1: samples=vorbis_synthesis_pcmout(&vf->vd,&pcm); Chris@1: if(samples)break; Chris@1: } Chris@1: Chris@1: /* suck in another packet */ Chris@1: { Chris@1: int ret=_fetch_and_process_packet(vf,NULL,1,1); Chris@1: if(ret==OV_EOF) Chris@1: return(0); Chris@1: if(ret<=0) Chris@1: return(ret); Chris@1: } Chris@1: Chris@1: } Chris@1: Chris@1: if(samples>0){ Chris@1: Chris@1: /* yay! proceed to pack data into the byte buffer */ Chris@1: Chris@1: long channels=ov_info(vf,-1)->channels; Chris@1: long bytespersample=word * channels; Chris@1: vorbis_fpu_control fpu; Chris@1: if(samples>length/bytespersample)samples=length/bytespersample; Chris@1: Chris@1: if(samples <= 0) Chris@1: return OV_EINVAL; Chris@1: Chris@1: /* Here. */ Chris@1: if(filter) Chris@1: filter(pcm,channels,samples,filter_param); Chris@1: Chris@1: /* a tight loop to pack each size */ Chris@1: { Chris@1: int val; Chris@1: if(word==1){ Chris@1: int off=(sgned?0:128); Chris@1: vorbis_fpu_setround(&fpu); Chris@1: for(j=0;j127)val=127; Chris@1: else if(val<-128)val=-128; Chris@1: *buffer++=val+off; Chris@1: } Chris@1: vorbis_fpu_restore(fpu); Chris@1: }else{ Chris@1: int off=(sgned?0:32768); Chris@1: Chris@1: if(host_endian==bigendianp){ Chris@1: if(sgned){ Chris@1: Chris@1: vorbis_fpu_setround(&fpu); Chris@1: for(i=0;i32767)val=32767; Chris@1: else if(val<-32768)val=-32768; Chris@1: *dest=val; Chris@1: dest+=channels; Chris@1: } Chris@1: } Chris@1: vorbis_fpu_restore(fpu); Chris@1: Chris@1: }else{ Chris@1: Chris@1: vorbis_fpu_setround(&fpu); Chris@1: for(i=0;i32767)val=32767; Chris@1: else if(val<-32768)val=-32768; Chris@1: *dest=val+off; Chris@1: dest+=channels; Chris@1: } Chris@1: } Chris@1: vorbis_fpu_restore(fpu); Chris@1: Chris@1: } Chris@1: }else if(bigendianp){ Chris@1: Chris@1: vorbis_fpu_setround(&fpu); Chris@1: for(j=0;j32767)val=32767; Chris@1: else if(val<-32768)val=-32768; Chris@1: val+=off; Chris@1: *buffer++=(val>>8); Chris@1: *buffer++=(val&0xff); Chris@1: } Chris@1: vorbis_fpu_restore(fpu); Chris@1: Chris@1: }else{ Chris@1: int val; Chris@1: vorbis_fpu_setround(&fpu); Chris@1: for(j=0;j32767)val=32767; Chris@1: else if(val<-32768)val=-32768; Chris@1: val+=off; Chris@1: *buffer++=(val&0xff); Chris@1: *buffer++=(val>>8); Chris@1: } Chris@1: vorbis_fpu_restore(fpu); Chris@1: Chris@1: } Chris@1: } Chris@1: } Chris@1: Chris@1: vorbis_synthesis_read(&vf->vd,samples); Chris@1: hs=vorbis_synthesis_halfrate_p(vf->vi); Chris@1: vf->pcm_offset+=(samples<current_link; Chris@1: return(samples*bytespersample); Chris@1: }else{ Chris@1: return(samples); Chris@1: } Chris@1: } Chris@1: Chris@1: long ov_read(OggVorbis_File *vf,char *buffer,int length, Chris@1: int bigendianp,int word,int sgned,int *bitstream){ Chris@1: return ov_read_filter(vf, buffer, length, bigendianp, word, sgned, bitstream, NULL, NULL); Chris@1: } Chris@1: Chris@1: /* input values: pcm_channels) a float vector per channel of output Chris@1: length) the sample length being read by the app Chris@1: Chris@1: return values: <0) error/hole in data (OV_HOLE), partial open (OV_EINVAL) Chris@1: 0) EOF Chris@1: n) number of samples of PCM actually returned. The Chris@1: below works on a packet-by-packet basis, so the Chris@1: return length is not related to the 'length' passed Chris@1: in, just guaranteed to fit. Chris@1: Chris@1: *section) set to the logical bitstream number */ Chris@1: Chris@1: Chris@1: Chris@1: long ov_read_float(OggVorbis_File *vf,float ***pcm_channels,int length, Chris@1: int *bitstream){ Chris@1: Chris@1: if(vf->ready_stateready_state==INITSET){ Chris@1: float **pcm; Chris@1: long samples=vorbis_synthesis_pcmout(&vf->vd,&pcm); Chris@1: if(samples){ Chris@1: int hs=vorbis_synthesis_halfrate_p(vf->vi); Chris@1: if(pcm_channels)*pcm_channels=pcm; Chris@1: if(samples>length)samples=length; Chris@1: vorbis_synthesis_read(&vf->vd,samples); Chris@1: vf->pcm_offset+=samples<current_link; Chris@1: return samples; Chris@1: Chris@1: } Chris@1: } Chris@1: Chris@1: /* suck in another packet */ Chris@1: { Chris@1: int ret=_fetch_and_process_packet(vf,NULL,1,1); Chris@1: if(ret==OV_EOF)return(0); Chris@1: if(ret<=0)return(ret); Chris@1: } Chris@1: Chris@1: } Chris@1: } Chris@1: Chris@1: extern float *vorbis_window(vorbis_dsp_state *v,int W); Chris@1: Chris@1: static void _ov_splice(float **pcm,float **lappcm, Chris@1: int n1, int n2, Chris@1: int ch1, int ch2, Chris@1: float *w1, float *w2){ Chris@1: int i,j; Chris@1: float *w=w1; Chris@1: int n=n1; Chris@1: Chris@1: if(n1>n2){ Chris@1: n=n2; Chris@1: w=w2; Chris@1: } Chris@1: Chris@1: /* splice */ Chris@1: for(j=0;jready_state==INITSET)break; Chris@1: /* suck in another packet */ Chris@1: { Chris@1: int ret=_fetch_and_process_packet(vf,NULL,1,0); Chris@1: if(ret<0 && ret!=OV_HOLE)return(ret); Chris@1: } Chris@1: } Chris@1: return 0; Chris@1: } Chris@1: Chris@1: /* make sure vf is INITSET and that we have a primed buffer; if Chris@1: we're crosslapping at a stream section boundary, this also makes Chris@1: sure we're sanity checking against the right stream information */ Chris@1: static int _ov_initprime(OggVorbis_File *vf){ Chris@1: vorbis_dsp_state *vd=&vf->vd; Chris@1: while(1){ Chris@1: if(vf->ready_state==INITSET) Chris@1: if(vorbis_synthesis_pcmout(vd,NULL))break; Chris@1: Chris@1: /* suck in another packet */ Chris@1: { Chris@1: int ret=_fetch_and_process_packet(vf,NULL,1,0); Chris@1: if(ret<0 && ret!=OV_HOLE)return(ret); Chris@1: } Chris@1: } Chris@1: return 0; Chris@1: } Chris@1: Chris@1: /* grab enough data for lapping from vf; this may be in the form of Chris@1: unreturned, already-decoded pcm, remaining PCM we will need to Chris@1: decode, or synthetic postextrapolation from last packets. */ Chris@1: static void _ov_getlap(OggVorbis_File *vf,vorbis_info *vi,vorbis_dsp_state *vd, Chris@1: float **lappcm,int lapsize){ Chris@1: int lapcount=0,i; Chris@1: float **pcm; Chris@1: Chris@1: /* try first to decode the lapping data */ Chris@1: while(lapcountlapsize-lapcount)samples=lapsize-lapcount; Chris@1: for(i=0;ichannels;i++) Chris@1: memcpy(lappcm[i]+lapcount,pcm[i],sizeof(**pcm)*samples); Chris@1: lapcount+=samples; Chris@1: vorbis_synthesis_read(vd,samples); Chris@1: }else{ Chris@1: /* suck in another packet */ Chris@1: int ret=_fetch_and_process_packet(vf,NULL,1,0); /* do *not* span */ Chris@1: if(ret==OV_EOF)break; Chris@1: } Chris@1: } Chris@1: if(lapcountvd,&pcm); Chris@1: if(samples==0){ Chris@1: for(i=0;ichannels;i++) Chris@1: memset(lappcm[i]+lapcount,0,sizeof(**pcm)*lapsize-lapcount); Chris@1: lapcount=lapsize; Chris@1: }else{ Chris@1: if(samples>lapsize-lapcount)samples=lapsize-lapcount; Chris@1: for(i=0;ichannels;i++) Chris@1: memcpy(lappcm[i]+lapcount,pcm[i],sizeof(**pcm)*samples); Chris@1: lapcount+=samples; Chris@1: } Chris@1: } Chris@1: } Chris@1: Chris@1: /* this sets up crosslapping of a sample by using trailing data from Chris@1: sample 1 and lapping it into the windowing buffer of sample 2 */ Chris@1: int ov_crosslap(OggVorbis_File *vf1, OggVorbis_File *vf2){ Chris@1: vorbis_info *vi1,*vi2; Chris@1: float **lappcm; Chris@1: float **pcm; Chris@1: float *w1,*w2; Chris@1: int n1,n2,i,ret,hs1,hs2; Chris@1: Chris@1: if(vf1==vf2)return(0); /* degenerate case */ Chris@1: if(vf1->ready_stateready_statechannels); Chris@1: n1=vorbis_info_blocksize(vi1,0)>>(1+hs1); Chris@1: n2=vorbis_info_blocksize(vi2,0)>>(1+hs2); Chris@1: w1=vorbis_window(&vf1->vd,0); Chris@1: w2=vorbis_window(&vf2->vd,0); Chris@1: Chris@1: for(i=0;ichannels;i++) Chris@1: lappcm[i]=alloca(sizeof(**lappcm)*n1); Chris@1: Chris@1: _ov_getlap(vf1,vi1,&vf1->vd,lappcm,n1); Chris@1: Chris@1: /* have a lapping buffer from vf1; now to splice it into the lapping Chris@1: buffer of vf2 */ Chris@1: /* consolidate and expose the buffer. */ Chris@1: vorbis_synthesis_lapout(&vf2->vd,&pcm); Chris@1: Chris@1: #if 0 Chris@1: _analysis_output_always("pcmL",0,pcm[0],n1*2,0,0,0); Chris@1: _analysis_output_always("pcmR",0,pcm[1],n1*2,0,0,0); Chris@1: #endif Chris@1: Chris@1: /* splice */ Chris@1: _ov_splice(pcm,lappcm,n1,n2,vi1->channels,vi2->channels,w1,w2); Chris@1: Chris@1: /* done */ Chris@1: return(0); Chris@1: } Chris@1: Chris@1: static int _ov_64_seek_lap(OggVorbis_File *vf,ogg_int64_t pos, Chris@1: int (*localseek)(OggVorbis_File *,ogg_int64_t)){ Chris@1: vorbis_info *vi; Chris@1: float **lappcm; Chris@1: float **pcm; Chris@1: float *w1,*w2; Chris@1: int n1,n2,ch1,ch2,hs; Chris@1: int i,ret; Chris@1: Chris@1: if(vf->ready_statechannels; Chris@1: n1=vorbis_info_blocksize(vi,0)>>(1+hs); Chris@1: w1=vorbis_window(&vf->vd,0); /* window arrays from libvorbis are Chris@1: persistent; even if the decode state Chris@1: from this link gets dumped, this Chris@1: window array continues to exist */ Chris@1: Chris@1: lappcm=alloca(sizeof(*lappcm)*ch1); Chris@1: for(i=0;ivd,lappcm,n1); Chris@1: Chris@1: /* have lapping data; seek and prime the buffer */ Chris@1: ret=localseek(vf,pos); Chris@1: if(ret)return ret; Chris@1: ret=_ov_initprime(vf); Chris@1: if(ret)return(ret); Chris@1: Chris@1: /* Guard against cross-link changes; they're perfectly legal */ Chris@1: vi=ov_info(vf,-1); Chris@1: ch2=vi->channels; Chris@1: n2=vorbis_info_blocksize(vi,0)>>(1+hs); Chris@1: w2=vorbis_window(&vf->vd,0); Chris@1: Chris@1: /* consolidate and expose the buffer. */ Chris@1: vorbis_synthesis_lapout(&vf->vd,&pcm); Chris@1: Chris@1: /* splice */ Chris@1: _ov_splice(pcm,lappcm,n1,n2,ch1,ch2,w1,w2); Chris@1: Chris@1: /* done */ Chris@1: return(0); Chris@1: } Chris@1: Chris@1: int ov_raw_seek_lap(OggVorbis_File *vf,ogg_int64_t pos){ Chris@1: return _ov_64_seek_lap(vf,pos,ov_raw_seek); Chris@1: } Chris@1: Chris@1: int ov_pcm_seek_lap(OggVorbis_File *vf,ogg_int64_t pos){ Chris@1: return _ov_64_seek_lap(vf,pos,ov_pcm_seek); Chris@1: } Chris@1: Chris@1: int ov_pcm_seek_page_lap(OggVorbis_File *vf,ogg_int64_t pos){ Chris@1: return _ov_64_seek_lap(vf,pos,ov_pcm_seek_page); Chris@1: } Chris@1: Chris@1: static int _ov_d_seek_lap(OggVorbis_File *vf,double pos, Chris@1: int (*localseek)(OggVorbis_File *,double)){ Chris@1: vorbis_info *vi; Chris@1: float **lappcm; Chris@1: float **pcm; Chris@1: float *w1,*w2; Chris@1: int n1,n2,ch1,ch2,hs; Chris@1: int i,ret; Chris@1: Chris@1: if(vf->ready_statechannels; Chris@1: n1=vorbis_info_blocksize(vi,0)>>(1+hs); Chris@1: w1=vorbis_window(&vf->vd,0); /* window arrays from libvorbis are Chris@1: persistent; even if the decode state Chris@1: from this link gets dumped, this Chris@1: window array continues to exist */ Chris@1: Chris@1: lappcm=alloca(sizeof(*lappcm)*ch1); Chris@1: for(i=0;ivd,lappcm,n1); Chris@1: Chris@1: /* have lapping data; seek and prime the buffer */ Chris@1: ret=localseek(vf,pos); Chris@1: if(ret)return ret; Chris@1: ret=_ov_initprime(vf); Chris@1: if(ret)return(ret); Chris@1: Chris@1: /* Guard against cross-link changes; they're perfectly legal */ Chris@1: vi=ov_info(vf,-1); Chris@1: ch2=vi->channels; Chris@1: n2=vorbis_info_blocksize(vi,0)>>(1+hs); Chris@1: w2=vorbis_window(&vf->vd,0); Chris@1: Chris@1: /* consolidate and expose the buffer. */ Chris@1: vorbis_synthesis_lapout(&vf->vd,&pcm); Chris@1: Chris@1: /* splice */ Chris@1: _ov_splice(pcm,lappcm,n1,n2,ch1,ch2,w1,w2); Chris@1: Chris@1: /* done */ Chris@1: return(0); Chris@1: } Chris@1: Chris@1: int ov_time_seek_lap(OggVorbis_File *vf,double pos){ Chris@1: return _ov_d_seek_lap(vf,pos,ov_time_seek); Chris@1: } Chris@1: Chris@1: int ov_time_seek_page_lap(OggVorbis_File *vf,double pos){ Chris@1: return _ov_d_seek_lap(vf,pos,ov_time_seek_page); Chris@1: }