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