squash annoying warnings.
[platform/upstream/libvorbis.git] / examples / seeking_example.c
1 /********************************************************************
2  *                                                                  *
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.       *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
9  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
10
11  ********************************************************************
12
13  function: illustrate seeking, and test it too
14  last mod: $Id: seeking_example.c,v 1.11 2001/12/18 01:07:54 segher Exp $
15
16  ********************************************************************/
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include "vorbis/codec.h"
21 #include "vorbis/vorbisfile.h"
22
23 #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
24 # include <io.h>
25 # include <fcntl.h>
26 #endif
27
28 void _verify(OggVorbis_File *ov,ogg_int64_t pos,
29              ogg_int64_t val,ogg_int64_t pcmval,
30              ogg_int64_t pcmlength,
31              char *bigassbuffer){
32   int j;
33   long bread;
34   char buffer[4096];
35   int dummy;
36
37   /* verify the raw position, the pcm position and position decode */
38   if(val!=-1 && ov_raw_tell(ov)<val){
39     printf("raw position out of tolerance: requested %ld, got %ld\n",
40            (long)val,(long)ov_raw_tell(ov));
41     exit(1);
42   }
43   if(pcmval!=-1 && ov_pcm_tell(ov)>pcmval){
44     printf("pcm position out of tolerance: requested %ld, got %ld\n",
45            (long)pcmval,(long)ov_pcm_tell(ov));
46     exit(1);
47   }
48   pos=ov_pcm_tell(ov);
49   if(pos<0 || pos>pcmlength){
50     printf("pcm position out of bounds: got %ld\n",(long)pos);
51     exit(1);
52   }
53   bread=ov_read(ov,buffer,4096,1,1,1,&dummy);
54   for(j=0;j<bread;j++){
55     if(buffer[j]!=bigassbuffer[j+pos*2]){
56       printf("data position after seek doesn't match pcm position\n");
57       exit(1);
58     }
59   }
60 }
61
62 int main(){
63   OggVorbis_File ov;
64   int i,ret;
65   ogg_int64_t pcmlength;
66   char *bigassbuffer;
67   int dummy;
68
69 #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
70   _setmode( _fileno( stdin ), _O_BINARY );
71   _setmode( _fileno( stdout ), _O_BINARY );
72 #endif
73
74
75   /* open the file/pipe on stdin */
76   if(ov_open(stdin,&ov,NULL,-1)<0){
77     printf("Could not open input as an OggVorbis file.\n\n");
78     exit(1);
79   }
80
81   if(ov_seekable(&ov)){
82
83     /* to simplify our own lives, we want to assume the whole file is
84        stereo.  Verify this to avoid potentially mystifying users
85        (pissing them off is OK, just don't confuse them) */
86     for(i=0;i<ov.links;i++){
87       vorbis_info *vi=ov_info(&ov,i);
88       if(vi->channels!=2){
89         printf("Sorry; right now seeking_test can only use Vorbis files\n"
90                "that are entirely stereo.\n\n");
91         exit(1);
92       }
93     }
94     
95     /* because we want to do sample-level verification that the seek
96        does what it claimed, decode the entire file into memory */
97     printf("loading....\n");
98     fflush(stdout);
99     pcmlength=ov_pcm_total(&ov,-1);
100     bigassbuffer=malloc(pcmlength*2); /* w00t */
101     i=0;
102     while(i<pcmlength*2){
103       int ret=ov_read(&ov,bigassbuffer+i,pcmlength*2-i,1,1,1,&dummy);
104       if(ret<0)continue;
105       if(ret){
106         i+=ret;
107       }else{
108         pcmlength=i/2;
109       }
110     }
111     
112     /* Exercise all the real seeking cases; ov_raw_seek,
113        ov_pcm_seek_page and ov_pcm_seek.  time seek is just a wrapper
114        on pcm_seek */
115     {
116       ogg_int64_t length=ov.end;
117       printf("testing raw seeking to random places in %ld bytes....\n",
118              (long)length);
119     
120       for(i=0;i<1000;i++){
121         ogg_int64_t val=(double)rand()/RAND_MAX*length;
122         ogg_int64_t pos;
123         printf("\r\t%d [raw position %ld]...     ",i,(long)val);
124         fflush(stdout);
125         ret=ov_raw_seek(&ov,val);
126         if(ret<0){
127           printf("seek failed: %d\n",ret);
128           exit(1);
129         }
130
131         _verify(&ov,pos,val,-1,pcmlength,bigassbuffer);
132
133       }
134     }
135
136     printf("\r");
137     {
138       ogg_int64_t length=ov.end;
139       printf("testing pcm page seeking to random places in %ld samples....\n",
140              (long)pcmlength);
141     
142       for(i=0;i<1000;i++){
143         ogg_int64_t val=(double)rand()/RAND_MAX*pcmlength;
144         ogg_int64_t pos;
145         printf("\r\t%d [pcm position %ld]...     ",i,(long)val);
146         fflush(stdout);
147         ret=ov_pcm_seek_page(&ov,val);
148         if(ret<0){
149           printf("seek failed: %d\n",ret);
150           exit(1);
151         }
152
153         _verify(&ov,pos,-1,val,pcmlength,bigassbuffer);
154
155       }
156     }
157     
158     printf("\r");
159     {
160       ogg_int64_t length=ov.end;
161       printf("testing pcm exact seeking to random places in %ld samples....\n",
162              (long)pcmlength);
163     
164       for(i=0;i<1000;i++){
165         ogg_int64_t val=(double)rand()/RAND_MAX*pcmlength;
166         ogg_int64_t pos;
167         printf("\r\t%d [pcm position %ld]...     ",i,(long)val);
168         fflush(stdout);
169         ret=ov_pcm_seek(&ov,val);
170         if(ret<0){
171           printf("seek failed: %d\n",ret);
172           exit(1);
173         }
174         if(ov_pcm_tell(&ov)!=val){
175           printf("Decalred position didn't perfectly match request: %ld != %ld\n",
176                  (long)val,(long)ov_pcm_tell(&ov));
177           exit(1);
178         }
179
180         _verify(&ov,pos,-1,val,pcmlength,bigassbuffer);
181
182       }
183     }
184     
185     printf("\r                                           \nOK.\n\n");
186
187
188   }else{
189     printf("Standard input was not seekable.\n");
190   }
191
192   ov_clear(&ov);
193   return 0;
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207