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-2007 * Chris@1: * by the Xiph.Org Foundation http://www.xiph.org/ * Chris@1: * * Chris@1: ******************************************************************** Chris@1: Chris@1: function: illustrate seeking, and test it too Chris@1: last mod: $Id: seeking_example.c 17561 2010-10-23 10:34:24Z xiphmont $ Chris@1: Chris@1: ********************************************************************/ Chris@1: Chris@1: #include Chris@1: #include Chris@1: #include "vorbis/codec.h" Chris@1: #include "vorbis/vorbisfile.h" Chris@1: Chris@1: #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */ Chris@1: # include Chris@1: # include Chris@1: #endif Chris@1: Chris@1: void _verify(OggVorbis_File *ov, Chris@1: ogg_int64_t val,ogg_int64_t pcmval,double timeval, Chris@1: ogg_int64_t pcmlength, Chris@1: char *bigassbuffer){ Chris@1: off_t i; Chris@1: int j; Chris@1: long bread; Chris@1: char buffer[4096]; Chris@1: int dummy; Chris@1: ogg_int64_t pos; Chris@1: int hs = ov_halfrate_p(ov); Chris@1: Chris@1: /* verify the raw position, the pcm position and position decode */ Chris@1: if(val!=-1 && ov_raw_tell(ov)pcmval){ Chris@1: fprintf(stderr,"pcm position out of tolerance: requested %ld, got %ld\n", Chris@1: (long)pcmval,(long)ov_pcm_tell(ov)); Chris@1: exit(1); Chris@1: } Chris@1: if(timeval!=-1 && ov_time_tell(ov)>timeval){ Chris@1: fprintf(stderr,"time position out of tolerance: requested %f, got %f\n", Chris@1: timeval,ov_time_tell(ov)); Chris@1: exit(1); Chris@1: } Chris@1: pos=ov_pcm_tell(ov); Chris@1: if(pos<0 || pos>pcmlength){ Chris@1: fprintf(stderr,"pcm position out of bounds: got %ld\n",(long)pos); Chris@1: exit(1); Chris@1: } Chris@1: bread=ov_read(ov,buffer,4096,1,1,1,&dummy); Chris@1: for(j=0;j>hs)*2)]){ Chris@1: fprintf(stderr,"data after seek doesn't match declared pcm position %ld\n",(long)pos); Chris@1: Chris@1: for(i=0;i<(pcmlength>>hs)*2-bread;i++){ Chris@1: for(j=0;j>hs)>=0 && (j+((pos*2)>>hs))<(pcmlength>>hs)*2) Chris@1: fprintf(f,"%d %d\n",j,(int)bigassbuffer[j+((pos*2)>>hs)]); Chris@1: fclose(f); Chris@1: } Chris@1: Chris@1: exit(1); Chris@1: } Chris@1: } Chris@1: } Chris@1: Chris@1: int main(){ Chris@1: OggVorbis_File ov; Chris@1: int i,ret; Chris@1: ogg_int64_t pcmlength; Chris@1: double timelength; Chris@1: char *bigassbuffer; Chris@1: int dummy; Chris@1: int hs=0; Chris@1: Chris@1: #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */ Chris@1: _setmode( _fileno( stdin ), _O_BINARY ); Chris@1: #endif Chris@1: Chris@1: Chris@1: /* open the file/pipe on stdin */ Chris@1: if(ov_open_callbacks(stdin,&ov,NULL,-1,OV_CALLBACKS_NOCLOSE)<0){ Chris@1: fprintf(stderr,"Could not open input as an OggVorbis file.\n\n"); Chris@1: exit(1); Chris@1: } Chris@1: Chris@1: #if 0 /*enable this code to test seeking with halfrate decode */ Chris@1: if(ov_halfrate(&ov,1)){ Chris@1: fprintf(stderr,"Sorry; unable to set half-rate decode.\n\n"); Chris@1: exit(1); Chris@1: }else Chris@1: hs=1; Chris@1: #endif Chris@1: Chris@1: if(ov_seekable(&ov)){ Chris@1: Chris@1: /* to simplify our own lives, we want to assume the whole file is Chris@1: stereo. Verify this to avoid potentially mystifying users Chris@1: (pissing them off is OK, just don't confuse them) */ Chris@1: for(i=0;ichannels!=2){ Chris@1: fprintf(stderr,"Sorry; right now seeking_test can only use Vorbis files\n" Chris@1: "that are entirely stereo.\n\n"); Chris@1: exit(1); Chris@1: } Chris@1: } Chris@1: Chris@1: /* because we want to do sample-level verification that the seek Chris@1: does what it claimed, decode the entire file into memory */ Chris@1: pcmlength=ov_pcm_total(&ov,-1); Chris@1: timelength=ov_time_total(&ov,-1); Chris@1: bigassbuffer=malloc((pcmlength>>hs)*2); /* w00t */ Chris@1: i=0; Chris@1: while(i<(pcmlength>>hs)*2){ Chris@1: int ret=ov_read(&ov,bigassbuffer+i,((pcmlength>>hs)*2)-i,1,1,1,&dummy); Chris@1: if(ret<0){ Chris@1: fprintf(stderr,"Error reading file.\n"); Chris@1: exit(1); Chris@1: } Chris@1: if(ret){ Chris@1: i+=ret; Chris@1: }else{ Chris@1: pcmlength=(i/2)<>hs)*2-i)); Chris@1: } Chris@1: Chris@1: { Chris@1: ogg_int64_t length=ov.end; Chris@1: fprintf(stderr,"\rtesting raw seeking to random places in %ld bytes....\n", Chris@1: (long)length); Chris@1: Chris@1: for(i=0;i<1000;i++){ Chris@1: ogg_int64_t val=(double)rand()/RAND_MAX*length; Chris@1: fprintf(stderr,"\r\t%d [raw position %ld]... ",i,(long)val); Chris@1: ret=ov_raw_seek(&ov,val); Chris@1: if(ret<0){ Chris@1: fprintf(stderr,"seek failed: %d\n",ret); Chris@1: exit(1); Chris@1: } Chris@1: Chris@1: _verify(&ov,val,-1,-1.,pcmlength,bigassbuffer); Chris@1: Chris@1: } Chris@1: } Chris@1: Chris@1: fprintf(stderr,"\r"); Chris@1: { Chris@1: fprintf(stderr,"testing pcm page seeking to random places in %ld samples....\n", Chris@1: (long)pcmlength); Chris@1: Chris@1: for(i=0;i<1000;i++){ Chris@1: ogg_int64_t val=(double)rand()/RAND_MAX*pcmlength; Chris@1: fprintf(stderr,"\r\t%d [pcm position %ld]... ",i,(long)val); Chris@1: ret=ov_pcm_seek_page(&ov,val); Chris@1: if(ret<0){ Chris@1: fprintf(stderr,"seek failed: %d\n",ret); Chris@1: exit(1); Chris@1: } Chris@1: Chris@1: _verify(&ov,-1,val,-1.,pcmlength,bigassbuffer); Chris@1: Chris@1: } Chris@1: } Chris@1: Chris@1: fprintf(stderr,"\r"); Chris@1: { Chris@1: fprintf(stderr,"testing pcm exact seeking to random places in %ld samples....\n", Chris@1: (long)pcmlength); Chris@1: Chris@1: for(i=0;i<1000;i++){ Chris@1: ogg_int64_t val=(double)rand()/RAND_MAX*pcmlength; Chris@1: fprintf(stderr,"\r\t%d [pcm position %ld]... ",i,(long)val); Chris@1: ret=ov_pcm_seek(&ov,val); Chris@1: if(ret<0){ Chris@1: fprintf(stderr,"seek failed: %d\n",ret); Chris@1: exit(1); Chris@1: } Chris@1: if(ov_pcm_tell(&ov)!=((val>>hs)<val+1){ Chris@1: fprintf(stderr,"Declared position didn't perfectly match request: %f != %f\n", Chris@1: val,ov_time_tell(&ov)); Chris@1: exit(1); Chris@1: } Chris@1: Chris@1: _verify(&ov,-1,-1,val,pcmlength,bigassbuffer); Chris@1: Chris@1: } Chris@1: } Chris@1: Chris@1: fprintf(stderr,"\r \nOK.\n\n"); Chris@1: Chris@1: Chris@1: }else{ Chris@1: fprintf(stderr,"Standard input was not seekable.\n"); Chris@1: } Chris@1: Chris@1: ov_clear(&ov); Chris@1: return 0; Chris@1: } Chris@1: Chris@1: Chris@1: Chris@1: Chris@1: Chris@1: Chris@1: Chris@1: Chris@1: Chris@1: Chris@1: Chris@1: Chris@1: