1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE.   *
4  *                                                                  *
5  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
6  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
7  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
8  *                                                                  *
9  * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2009    *
10  * BY THE Xiph.Org FOUNDATION http://www.xiph.org/                  *
11  *                                                                  *
12  ********************************************************************
13 
14  function: illustrate seeking, and test it too
15  last mod: $Id: iseeking_example.c 16037 2009-05-26 21:10:58Z xiphmont $
16 
17  ********************************************************************/
18 
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <vorbis/ivorbiscodec.h>
22 #include <vorbis/ivorbisfile.h>
23 
24 #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
25 # include <io.h>
26 # include <fcntl.h>
27 #endif
28 
_verify(OggVorbis_File * ov,ogg_int64_t val,ogg_int64_t pcmval,ogg_int64_t timeval,ogg_int64_t pcmlength,char * bigassbuffer)29 void _verify(OggVorbis_File *ov,
30              ogg_int64_t val,
31              ogg_int64_t pcmval,
32              ogg_int64_t timeval,
33              ogg_int64_t pcmlength,
34              char *bigassbuffer){
35   int j;
36   long bread;
37   char buffer[4096];
38   int dummy;
39   ogg_int64_t pos;
40 
41   /* verify the raw position, the pcm position and position decode */
42   if(val!=-1 && ov_raw_tell(ov)<val){
43     fprintf(stderr,"raw position out of tolerance: requested %ld, got %ld\n",
44            (long)val,(long)ov_raw_tell(ov));
45     exit(1);
46   }
47   if(pcmval!=-1 && ov_pcm_tell(ov)>pcmval){
48     fprintf(stderr,"pcm position out of tolerance: requested %ld, got %ld\n",
49            (long)pcmval,(long)ov_pcm_tell(ov));
50     exit(1);
51   }
52   if(timeval!=-1 && ov_time_tell(ov)>timeval){
53     fprintf(stderr,"time position out of tolerance: requested %ld, got %ld\n",
54             (long)timeval,(long)ov_time_tell(ov));
55     exit(1);
56   }
57   pos=ov_pcm_tell(ov);
58   if(pos<0 || pos>pcmlength){
59     fprintf(stderr,"pcm position out of bounds: got %ld\n",(long)pos);
60     exit(1);
61   }
62   bread=ov_read(ov,buffer,4096,&dummy);
63   for(j=0;j<bread;j++){
64     if(buffer[j]!=bigassbuffer[j+pos*4]){
65       fprintf(stderr,"data position after seek doesn't match pcm position\n");
66 
67       {
68         FILE *f=fopen("a.m","w");
69         for(j=0;j<bread;j++)fprintf(f,"%d\n",(int)buffer[j]);
70         fclose(f);
71         f=fopen("b.m","w");
72         for(j=0;j<bread;j++)fprintf(f,"%d\n",(int)bigassbuffer[j+pos*2]);
73         fclose(f);
74       }
75 
76       exit(1);
77     }
78   }
79 }
80 
main()81 int main(){
82   OggVorbis_File ov;
83   int i,ret;
84   ogg_int64_t pcmlength;
85   ogg_int64_t timelength;
86   char *bigassbuffer;
87   int dummy;
88 
89 #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
90   _setmode( _fileno( stdin ), _O_BINARY );
91 #endif
92 
93 
94   /* open the file/pipe on stdin */
95   if(ov_open(stdin, &ov, NULL, 0) < 0) {
96     fprintf(stderr,"Could not open input as an OggVorbis file.\n\n");
97     exit(1);
98   }
99 
100   if(ov_seekable(&ov)){
101 
102     /* to simplify our own lives, we want to assume the whole file is
103        stereo.  Verify this to avoid potentially mystifying users
104        (pissing them off is OK, just don't confuse them) */
105     for(i=0;i<ov.links;i++){
106       vorbis_info *vi=ov_info(&ov,i);
107       if(vi->channels!=2){
108         fprintf(stderr,"Sorry; right now seeking_test can only use Vorbis files\n"
109                "that are entirely stereo.\n\n");
110         exit(1);
111       }
112     }
113 
114     /* because we want to do sample-level verification that the seek
115        does what it claimed, decode the entire file into memory */
116     pcmlength=ov_pcm_total(&ov,-1);
117     timelength=ov_time_total(&ov,-1);
118     bigassbuffer=malloc(pcmlength*4); /* w00t */
119     i=0;
120     while(i<pcmlength*4){
121       int ret=ov_read(&ov,bigassbuffer+i,pcmlength*4-i,&dummy);
122       if(ret<0)continue;
123       if(ret){
124         i+=ret;
125       }else{
126         pcmlength=i/4;
127       }
128       fprintf(stderr,"\rloading.... [%ld left]              ",
129               (long)(pcmlength*4-i));
130     }
131 
132     {
133       ogg_int64_t length=ov.end;
134       fprintf(stderr,"\rtesting raw seeking to random places in %ld bytes....\n",
135              (long)length);
136 
137       for(i=0;i<1000;i++){
138         ogg_int64_t val=rand()*length/RAND_MAX;
139         fprintf(stderr,"\r\t%d [raw position %ld]...     ",i,(long)val);
140         ret=ov_raw_seek(&ov,val);
141         if(ret<0){
142           fprintf(stderr,"seek failed: %d\n",ret);
143           exit(1);
144         }
145 
146         _verify(&ov,val,-1,-1.,pcmlength,bigassbuffer);
147 
148       }
149     }
150 
151     fprintf(stderr,"\r");
152     {
153       fprintf(stderr,"testing pcm page seeking to random places in %ld samples....\n",
154              (long)pcmlength);
155 
156       for(i=0;i<1000;i++){
157         ogg_int64_t val=rand()*pcmlength/RAND_MAX;
158         fprintf(stderr,"\r\t%d [pcm position %ld]...     ",i,(long)val);
159         ret=ov_pcm_seek_page(&ov,val);
160         if(ret<0){
161           fprintf(stderr,"seek failed: %d\n",ret);
162           exit(1);
163         }
164 
165         _verify(&ov,-1,val,-1.,pcmlength,bigassbuffer);
166 
167       }
168     }
169 
170     fprintf(stderr,"\r");
171     {
172       fprintf(stderr,"testing pcm exact seeking to random places in %ld samples....\n",
173              (long)pcmlength);
174 
175       for(i=0;i<1000;i++){
176         ogg_int64_t val=rand()*pcmlength/RAND_MAX;
177         fprintf(stderr,"\r\t%d [pcm position %ld]...     ",i,(long)val);
178         ret=ov_pcm_seek(&ov,val);
179         if(ret<0){
180           fprintf(stderr,"seek failed: %d\n",ret);
181           exit(1);
182         }
183         if(ov_pcm_tell(&ov)!=val){
184           fprintf(stderr,"Declared position didn't perfectly match request: %ld != %ld\n",
185                  (long)val,(long)ov_pcm_tell(&ov));
186           exit(1);
187         }
188 
189         _verify(&ov,-1,val,-1.,pcmlength,bigassbuffer);
190 
191       }
192     }
193 
194     fprintf(stderr,"\r");
195     {
196       fprintf(stderr,"testing time page seeking to random places in %ld seconds....\n",
197               (long)timelength);
198 
199       for(i=0;i<1000;i++){
200         ogg_int64_t val=rand()*timelength/RAND_MAX;
201         fprintf(stderr,"\r\t%d [time position %ld]...     ",i,(long)val);
202         ret=ov_time_seek_page(&ov,val);
203         if(ret<0){
204           fprintf(stderr,"seek failed: %d\n",ret);
205           exit(1);
206         }
207 
208         _verify(&ov,-1,-1,val,pcmlength,bigassbuffer);
209 
210       }
211     }
212 
213     fprintf(stderr,"\r");
214     {
215       fprintf(stderr,"testing time exact seeking to random places in %ld seconds....\n",
216               (long)timelength);
217 
218       for(i=0;i<1000;i++){
219         ogg_int64_t val=rand()*timelength/RAND_MAX;
220         fprintf(stderr,"\r\t%d [time position %ld]...     ",i,(long)val);
221         ret=ov_time_seek(&ov,val);
222         if(ret<0){
223           fprintf(stderr,"seek failed: %d\n",ret);
224           exit(1);
225         }
226         if(ov_time_tell(&ov)<val-1 || ov_time_tell(&ov)>val+1){
227           fprintf(stderr,"Declared position didn't perfectly match request: %ld != %ld\n",
228                   (long)val,(long)ov_time_tell(&ov));
229           exit(1);
230         }
231 
232         _verify(&ov,-1,-1,val,pcmlength,bigassbuffer);
233 
234       }
235     }
236 
237     fprintf(stderr,"\r                                           \nOK.\n\n");
238 
239 
240   }else{
241     fprintf(stderr,"Standard input was not seekable.\n");
242   }
243 
244   ov_clear(&ov);
245   return 0;
246 }
247 
248 
249 
250 
251 
252 
253 
254 
255 
256 
257 
258 
259 
260