📄 vorbisfile.c
字号:
vf->pcm_offset=granulepos; } return(1); } } else break; } } if(vf->ready_state>=OPENED){ if(!readp)return(0); if(_get_next_page(vf,&og,-1)<0)return(OV_EOF); /* eof. leave unitialized */ /* bitrate tracking; add the header's bytes here, the body bytes are done by packet above */ vf->bittrack+=og.header_len*8; /* has our decoding just traversed a bitstream boundary? */ if(vf->ready_state==INITSET){ if(vf->current_serialno!=ogg_page_serialno(&og)){ _decode_clear(vf); if(!vf->seekable){ vorbis_info_clear(vf->vi); vorbis_comment_clear(vf->vc); } } } } /* Do we need to load a new machine before submitting the page? */ /* This is different in the seekable and non-seekable cases. In the seekable case, we already have all the header information loaded and cached; we just initialize the machine with it and continue on our merry way. In the non-seekable (streaming) case, we'll only be at a boundary if we just left the previous logical bitstream and we're now nominally at the header of the next bitstream */ if(vf->ready_state!=INITSET){ int link; if(vf->ready_state<STREAMSET){ if(vf->seekable){ vf->current_serialno=ogg_page_serialno(&og); /* match the serialno to bitstream section. We use this rather than offset positions to avoid problems near logical bitstream boundaries */ for(link=0;link<vf->links;link++) if(vf->serialnos[link]==vf->current_serialno)break; if(link==vf->links)return(OV_EBADLINK); /* sign of a bogus stream. error out, leave machine uninitialized */ vf->current_link=link; ogg_stream_init(&vf->os,vf->current_serialno); ogg_stream_reset(&vf->os); vf->ready_state=STREAMSET; }else{ /* we're streaming */ /* fetch the three header packets, build the info struct */ int ret=_fetch_headers(vf,vf->vi,vf->vc,&vf->current_serialno,&og); if(ret)return(ret); vf->current_link++; link=0; } } _make_decode_ready(vf); } ogg_stream_pagein(&vf->os,&og); }}static int _fseek64_wrap(FILE *f,ogg_int64_t off,int whence){ if(f==NULL)return(-1); return fseek(f,(int)off,whence);}static int _ov_open1(void *f,OggVorbis_File *vf,char *initial, long ibytes, ov_callbacks callbacks){ long offset=(f?callbacks.seek_func(f,0,SEEK_CUR):-1); int ret; memset(vf,0,sizeof(*vf)); vf->datasource=f; vf->callbacks = callbacks; /* init the framing state */ ogg_sync_init(&vf->oy); /* perhaps some data was previously read into a buffer for testing against other stream types. Allow initialization from this previously read data (as we may be reading from a non-seekable stream) */ if(initial){ char *buffer=ogg_sync_buffer(&vf->oy,ibytes); memcpy(buffer,initial,ibytes); ogg_sync_wrote(&vf->oy,ibytes); } /* can we seek? Stevens suggests the seek test was portable */ if(offset!=-1)vf->seekable=1; /* No seeking yet; Set up a 'single' (current) logical bitstream entry for partial open */ vf->links=1; vf->vi=(vorbis_info *)_ogg_calloc(vf->links,sizeof(*vf->vi)); vf->vc=(vorbis_comment *)_ogg_calloc(vf->links,sizeof(*vf->vc)); /* Try to fetch the headers, maintaining all the storage */ if((ret=_fetch_headers(vf,vf->vi,vf->vc,&vf->current_serialno,NULL))<0){ vf->datasource=NULL; ov_clear(vf); }else if(vf->ready_state < PARTOPEN) vf->ready_state=PARTOPEN; return(ret);}static int _ov_open2(OggVorbis_File *vf){ if(vf->ready_state < OPENED) vf->ready_state=OPENED; if(vf->seekable){ int ret=_open_seekable2(vf); if(ret){ vf->datasource=NULL; ov_clear(vf); } return(ret); } return 0;}/* clear out the OggVorbis_File struct */int ov_clear(OggVorbis_File *vf){ if(vf){ vorbis_block_clear(&vf->vb); vorbis_dsp_clear(&vf->vd); ogg_stream_clear(&vf->os); if(vf->vi && vf->links){ int i; for(i=0;i<vf->links;i++){ vorbis_info_clear(vf->vi+i); vorbis_comment_clear(vf->vc+i); } _ogg_free(vf->vi); _ogg_free(vf->vc); } if(vf->dataoffsets)_ogg_free(vf->dataoffsets); if(vf->pcmlengths)_ogg_free(vf->pcmlengths); if(vf->serialnos)_ogg_free(vf->serialnos); if(vf->offsets)_ogg_free(vf->offsets); ogg_sync_clear(&vf->oy); if(vf->datasource)(vf->callbacks.close_func)(vf->datasource); memset(vf,0,sizeof(*vf)); }#ifdef DEBUG_LEAKS _VDBG_dump();#endif return(0);}/* inspects the OggVorbis file and finds/documents all the logical bitstreams contained in it. Tries to be tolerant of logical bitstream sections that are truncated/woogie. return: -1) error 0) OK*/int ov_open_callbacks(void *f,OggVorbis_File *vf,char *initial,long ibytes, ov_callbacks callbacks){ int ret=_ov_open1(f,vf,initial,ibytes,callbacks); if(ret)return ret; return _ov_open2(vf);}int ov_open(FILE *f,OggVorbis_File *vf,char *initial,long ibytes){ ov_callbacks callbacks = { (size_t (*)(void *, size_t, size_t, void *)) fread, (int (*)(void *, ogg_int64_t, int)) _fseek64_wrap, (int (*)(void *)) fclose, (long (*)(void *)) ftell }; return ov_open_callbacks((void *)f, vf, initial, ibytes, callbacks);} /* Only partially open the vorbis file; test for Vorbisness, and load the headers for the first chain. Do not seek (although test for seekability). Use ov_test_open to finish opening the file, else ov_clear to close/free it. Same return codes as open. */int ov_test_callbacks(void *f,OggVorbis_File *vf,char *initial,long ibytes, ov_callbacks callbacks){ return _ov_open1(f,vf,initial,ibytes,callbacks);}int ov_test(FILE *f,OggVorbis_File *vf,char *initial,long ibytes){ ov_callbacks callbacks = { (size_t (*)(void *, size_t, size_t, void *)) fread, (int (*)(void *, ogg_int64_t, int)) _fseek64_wrap, (int (*)(void *)) fclose, (long (*)(void *)) ftell }; return ov_test_callbacks((void *)f, vf, initial, ibytes, callbacks);} int ov_test_open(OggVorbis_File *vf){ if(vf->ready_state!=PARTOPEN)return(OV_EINVAL); return _ov_open2(vf);}/* How many logical bitstreams in this physical bitstream? */long ov_streams(OggVorbis_File *vf){ return vf->links;}/* Is the FILE * associated with vf seekable? */long ov_seekable(OggVorbis_File *vf){ return vf->seekable;}/* returns the bitrate for a given logical bitstream or the entire physical bitstream. If the file is open for random access, it will find the *actual* average bitrate. If the file is streaming, it returns the nominal bitrate (if set) else the average of the upper/lower bounds (if set) else -1 (unset). If you want the actual bitrate field settings, get them from the vorbis_info structs */long ov_bitrate(OggVorbis_File *vf,int i){ if(vf->ready_state<OPENED)return(OV_EINVAL); if(i>=vf->links)return(OV_EINVAL); if(!vf->seekable && i!=0)return(ov_bitrate(vf,0)); if(i<0){ ogg_int64_t bits=0; int i; for(i=0;i<vf->links;i++) bits+=(vf->offsets[i+1]-vf->dataoffsets[i])*8; return(bits*1000/ov_time_total(vf,-1)); }else{ if(vf->seekable){ /* return the actual bitrate */ return((vf->offsets[i+1]-vf->dataoffsets[i])*8000/ov_time_total(vf,i)); }else{ /* return nominal if set */ if(vf->vi[i].bitrate_nominal>0){ return vf->vi[i].bitrate_nominal; }else{ if(vf->vi[i].bitrate_upper>0){ if(vf->vi[i].bitrate_lower>0){ return (vf->vi[i].bitrate_upper+vf->vi[i].bitrate_lower)/2; }else{ return vf->vi[i].bitrate_upper; } } return(OV_FALSE); } } }}/* returns the actual bitrate since last call. returns -1 if no additional data to offer since last call (or at beginning of stream), EINVAL if stream is only partially open */long ov_bitrate_instant(OggVorbis_File *vf){ int link=(vf->seekable?vf->current_link:0); long ret; if(vf->ready_state<OPENED)return(OV_EINVAL); if(vf->samptrack==0)return(OV_FALSE); ret=vf->bittrack/vf->samptrack*vf->vi[link].rate; vf->bittrack=0; vf->samptrack=0; return(ret);}/* Guess */long ov_serialnumber(OggVorbis_File *vf,int i){ if(i>=vf->links)return(ov_serialnumber(vf,vf->links-1)); if(!vf->seekable && i>=0)return(ov_serialnumber(vf,-1)); if(i<0){ return(vf->current_serialno); }else{ return(vf->serialnos[i]); }}/* returns: total raw (compressed) length of content if i==-1 raw (compressed) length of that logical bitstream for i==0 to n OV_EINVAL if the stream is not seekable (we can't know the length) or if stream is only partially open*/ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i){ if(vf->ready_state<OPENED)return(OV_EINVAL); if(!vf->seekable || i>=vf->links)return(OV_EINVAL); if(i<0){ long acc=0; int i; for(i=0;i<vf->links;i++) acc+=ov_raw_total(vf,i); return(acc); }else{ return(vf->offsets[i+1]-vf->offsets[i]); }}/* returns: total PCM length (samples) of content if i==-1 PCM length (samples) of that logical bitstream for i==0 to n OV_EINVAL if the stream is not seekable (we can't know the length) or only partially open */ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i){ if(vf->ready_state<OPENED)return(OV_EINVAL); if(!vf->seekable || i>=vf->links)return(OV_EINVAL); if(i<0){ ogg_int64_t acc=0; int i; for(i=0;i<vf->links;i++) acc+=ov_pcm_total(vf,i); return(acc); }else{ return(vf->pcmlengths[i]); }}/* returns: total milliseconds of content if i==-1 seconds in that logical bitstream for i==0 to n OV_EINVAL if the stream is not seekable (we can't know the length) or only partially open */ogg_int64_t ov_time_total(OggVorbis_File *vf,int i){ if(vf->ready_state<OPENED)return(OV_EINVAL); if(!vf->seekable || i>=vf->links)return(OV_EINVAL); if(i<0){ ogg_int64_t acc=0; int i; for(i=0;i<vf->links;i++) acc+=ov_time_total(vf,i); return(acc); }else{ return(((ogg_int64_t)vf->pcmlengths[i])*1000/vf->vi[i].rate); }}/* seek to an offset relative to the *compressed* data. This also scans packets to update the PCM cursor. It will cross a logical bitstream boundary, but only if it can't get any packets out of the tail of the bitstream we seek to (so no surprises). returns zero on success, nonzero on failure */int ov_raw_seek(OggVorbis_File *vf,long pos){ ogg_stream_state work_os; if(vf->ready_state<OPENED)return(OV_EINVAL); if(!vf->seekable) return(OV_ENOSEEK); /* don't dump machine if we can't seek */ if(pos<0 || pos>vf->offsets[vf->links])return(OV_EINVAL); /* clear out decoding machine state */ vf->pcm_offset=-1; _decode_clear(vf); _seek_helper(vf,pos); /* we need to make sure the pcm_offset is set, but we don't want to advance the raw cursor past good packets just to get to the first with a granulepos. That's not equivalent behavior to beginning decoding as immediately after the seek position as possible. So, a hack. We use two stream states; a local scratch state and a the shared vf->os stream state. We use the local state to scan, and the shared state as a buffer for later decode. Unfortuantely, on the last page we still advance to last packet because the granulepos on the last page is not necessarily on a packet boundary, and we need to make sure the granpos is correct. */ { ogg_page og; ogg_packet op; int lastblock=0; int accblock=0; int thisblock=-1; int eosflag=0; memset(&work_os,0,sizeof(work_os));/* so that it's safe to clear it later even if we don't init it */ while(1){ if(vf->ready_state==STREAMSET){ /* snarf/scan a packet if we can */ int result=ogg_stream_packetout(&work_os,&op); if(result>0){ if(vf->vi[vf->current_link].codec_setup) thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op); if(eosflag) ogg_stream_packetout(&vf->os,NULL); else if(lastblock)accblock+=(lastblock+thisblock)>>2; if(op.granulepos!=-1){ int i,link=vf->current_link; ogg_int64_t granulepos=op.granulepos; for(i=0;i<link;i++) granulepos+=vf->pcmlengths[i]; vf->pcm_offset=granulepos-accblock; break; } lastblock=thisblock; continue; } } if(!lastblock){ if(_get_next_page(vf,&og,-1)<0){ vf->pcm_offset=ov_pcm_total(vf,-1); break; } }else{ /* huh? Bogus stream with packets but no granulepos */ vf->pcm_offset=-1; break; } /* has our decoding just traversed a bitstream boundary? */ if(vf->ready_state==STREAMSET) if(vf->current_serialno!=ogg_page_serialno(&og)){ _decode_clear(vf); /* clear out stream state */ ogg_stream_clear(&work_os); } if(vf->ready_state<STREAMSET){ int link; vf->current_serialno=ogg_page_serialno(&og); for(link=0;link<vf->links;link++) if(vf->serialnos[link]==vf->current_serialno)break; if(link==vf->links)goto seek_error; /* sign of a bogus stream. error out, leave machine uninitialized */ vf->current_link=link; ogg_stream_init(&vf->os,vf->current_serialno); ogg_stream_reset(&vf->os); ogg_stream_init(&work_os,vf->current_serialno); ogg_stream_reset(&work_os); vf->ready_state=STREAMSET; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -