1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007 *
9 * by the Xiph.Org Foundation https://xiph.org/ *
11 ********************************************************************
13 function: illustrate seeking, and test it too
15 ********************************************************************/
19 #include "vorbis/codec.h"
20 #include "vorbis/vorbisfile.h"
22 #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
27 void _verify(OggVorbis_File *ov,
28 ogg_int64_t val,ogg_int64_t pcmval,double timeval,
29 ogg_int64_t pcmlength,
37 int hs = ov_halfrate_p(ov);
39 /* verify the raw position, the pcm position and position decode */
40 if(val!=-1 && ov_raw_tell(ov)<val){
41 fprintf(stderr,"raw position out of tolerance: requested %ld, got %ld\n",
42 (long)val,(long)ov_raw_tell(ov));
45 if(pcmval!=-1 && ov_pcm_tell(ov)>pcmval){
46 fprintf(stderr,"pcm position out of tolerance: requested %ld, got %ld\n",
47 (long)pcmval,(long)ov_pcm_tell(ov));
50 if(timeval!=-1 && ov_time_tell(ov)>timeval){
51 fprintf(stderr,"time position out of tolerance: requested %f, got %f\n",
52 timeval,ov_time_tell(ov));
56 if(pos<0 || pos>pcmlength){
57 fprintf(stderr,"pcm position out of bounds: got %ld\n",(long)pos);
60 bread=ov_read(ov,buffer,4096,1,1,1,&dummy);
62 if(buffer[j]!=bigassbuffer[j+((pos>>hs)*2)]){
63 fprintf(stderr,"data after seek doesn't match declared pcm position %ld\n",(long)pos);
65 for(i=0;i<(pcmlength>>hs)*2-bread;i++){
67 if(buffer[j] != bigassbuffer[i+j])break;
69 fprintf(stderr,"data after seek appears to match position %ld\n",(long)((i/2)<<hs));
73 FILE *f=fopen("a.m","w");
74 for(j=0;j<bread;j++)fprintf(f,"%d %d\n",j,(int)buffer[j]);
77 for(j=-4096;j<bread+4096;j++)
78 if(j+((pos*2)>>hs)>=0 && (j+((pos*2)>>hs))<(pcmlength>>hs)*2)
79 fprintf(f,"%d %d\n",j,(int)bigassbuffer[j+((pos*2)>>hs)]);
91 ogg_int64_t pcmlength;
97 #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
98 _setmode( _fileno( stdin ), _O_BINARY );
102 /* open the file/pipe on stdin */
103 if(ov_open_callbacks(stdin,&ov,NULL,-1,OV_CALLBACKS_NOCLOSE)<0){
104 fprintf(stderr,"Could not open input as an OggVorbis file.\n\n");
108 #if 0 /*enable this code to test seeking with halfrate decode */
109 if(ov_halfrate(&ov,1)){
110 fprintf(stderr,"Sorry; unable to set half-rate decode.\n\n");
116 if(ov_seekable(&ov)){
118 /* to simplify our own lives, we want to assume the whole file is
119 stereo. Verify this to avoid potentially mystifying users
120 (pissing them off is OK, just don't confuse them) */
121 for(i=0;i<ov.links;i++){
122 vorbis_info *vi=ov_info(&ov,i);
124 fprintf(stderr,"Sorry; right now seeking_test can only use Vorbis files\n"
125 "that are entirely stereo.\n\n");
130 /* because we want to do sample-level verification that the seek
131 does what it claimed, decode the entire file into memory */
132 pcmlength=ov_pcm_total(&ov,-1);
133 timelength=ov_time_total(&ov,-1);
134 bigassbuffer=malloc((pcmlength>>hs)*2); /* w00t */
136 while(i<(pcmlength>>hs)*2){
137 int ret=ov_read(&ov,bigassbuffer+i,((pcmlength>>hs)*2)-i,1,1,1,&dummy);
139 fprintf(stderr,"Error reading file.\n");
147 fprintf(stderr,"\rloading.... [%ld left] ",
148 (long)((pcmlength>>hs)*2-i));
152 ogg_int64_t length=ov.end;
153 fprintf(stderr,"\rtesting raw seeking to random places in %ld bytes....\n",
157 ogg_int64_t val=(double)rand()/RAND_MAX*length;
158 fprintf(stderr,"\r\t%d [raw position %ld]... ",i,(long)val);
159 ret=ov_raw_seek(&ov,val);
161 fprintf(stderr,"seek failed: %d\n",ret);
165 _verify(&ov,val,-1,-1.,pcmlength,bigassbuffer);
170 fprintf(stderr,"\r");
172 fprintf(stderr,"testing pcm page seeking to random places in %ld samples....\n",
176 ogg_int64_t val= i==0?(ogg_int64_t)0:(double)rand()/RAND_MAX*pcmlength;
177 fprintf(stderr,"\r\t%d [pcm position %ld]... ",i,(long)val);
178 ret=ov_pcm_seek_page(&ov,val);
180 fprintf(stderr,"seek failed: %d\n",ret);
184 _verify(&ov,-1,val,-1.,pcmlength,bigassbuffer);
189 fprintf(stderr,"\r");
191 fprintf(stderr,"testing pcm exact seeking to random places in %f seconds....\n",
194 ogg_int64_t val= i==0?(ogg_int64_t)0:(double)rand()/RAND_MAX*pcmlength;
195 fprintf(stderr,"\r\t%d [pcm position %ld]... ",i,(long)val);
196 ret=ov_pcm_seek(&ov,val);
198 fprintf(stderr,"seek failed: %d\n",ret);
201 if(ov_pcm_tell(&ov)!=((val>>hs)<<hs)){
202 fprintf(stderr,"Declared position didn't perfectly match request: %ld != %ld\n",
203 (long)val,(long)ov_pcm_tell(&ov));
207 _verify(&ov,-1,val,-1.,pcmlength,bigassbuffer);
212 fprintf(stderr,"\r");
214 fprintf(stderr,"testing time page seeking to random places in %f seconds....\n",
218 double val=(double)rand()/RAND_MAX*timelength;
219 fprintf(stderr,"\r\t%d [time position %f]... ",i,val);
220 ret=ov_time_seek_page(&ov,val);
222 fprintf(stderr,"seek failed: %d\n",ret);
226 _verify(&ov,-1,-1,val,pcmlength,bigassbuffer);
231 fprintf(stderr,"\r");
233 fprintf(stderr,"testing time exact seeking to random places in %f seconds....\n",
237 double val=(double)rand()/RAND_MAX*timelength;
238 fprintf(stderr,"\r\t%d [time position %f]... ",i,val);
239 ret=ov_time_seek(&ov,val);
241 fprintf(stderr,"seek failed: %d\n",ret);
244 if(ov_time_tell(&ov)<val-1 || ov_time_tell(&ov)>val+1){
245 fprintf(stderr,"Declared position didn't perfectly match request: %f != %f\n",
246 val,ov_time_tell(&ov));
250 _verify(&ov,-1,-1,val,pcmlength,bigassbuffer);
255 fprintf(stderr,"\r \nOK.\n\n");
259 fprintf(stderr,"Standard input was not seekable.\n");