Missed the encode ctl headers
[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.13 2002/02/28 04:12:47 xiphmont 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
58       {
59         FILE *f=fopen("a.m","w");
60         for(j=0;j<bread;j++)fprintf(f,"%d\n",(int)buffer[j]);
61         fclose(f);
62         f=fopen("b.m","w");
63         for(j=0;j<bread;j++)fprintf(f,"%d\n",(int)bigassbuffer[j+pos*2]);
64         fclose(f);
65       }
66
67       exit(1);
68     }
69   }
70 }
71
72 int main(){
73   OggVorbis_File ov;
74   int i,ret;
75   ogg_int64_t pcmlength;
76   char *bigassbuffer;
77   int dummy;
78
79 #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
80   _setmode( _fileno( stdin ), _O_BINARY );
81   _setmode( _fileno( stdout ), _O_BINARY );
82 #endif
83
84
85   /* open the file/pipe on stdin */
86   if(ov_open(stdin,&ov,NULL,-1)<0){
87     printf("Could not open input as an OggVorbis file.\n\n");
88     exit(1);
89   }
90
91   if(ov_seekable(&ov)){
92
93     /* to simplify our own lives, we want to assume the whole file is
94        stereo.  Verify this to avoid potentially mystifying users
95        (pissing them off is OK, just don't confuse them) */
96     for(i=0;i<ov.links;i++){
97       vorbis_info *vi=ov_info(&ov,i);
98       if(vi->channels!=2){
99         printf("Sorry; right now seeking_test can only use Vorbis files\n"
100                "that are entirely stereo.\n\n");
101         exit(1);
102       }
103     }
104     
105     /* because we want to do sample-level verification that the seek
106        does what it claimed, decode the entire file into memory */
107     fflush(stdout);
108     pcmlength=ov_pcm_total(&ov,-1);
109     bigassbuffer=malloc(pcmlength*2); /* w00t */
110     i=0;
111     while(i<pcmlength*2){
112       int ret=ov_read(&ov,bigassbuffer+i,pcmlength*2-i,1,1,1,&dummy);
113       if(ret<0)continue;
114       if(ret){
115         i+=ret;
116       }else{
117         pcmlength=i/2;
118       }
119       fprintf(stderr,"\rloading.... [%ld left]              ",
120               (long)(pcmlength*2-i));
121     }
122     
123     /* Exercise all the real seeking cases; ov_raw_seek,
124        ov_pcm_seek_page and ov_pcm_seek.  time seek is just a wrapper
125        on pcm_seek */
126     {
127       ogg_int64_t length=ov.end;
128       printf("\rtesting raw seeking to random places in %ld bytes....\n",
129              (long)length);
130     
131       for(i=0;i<1000;i++){
132         ogg_int64_t val=(double)rand()/RAND_MAX*length;
133         ogg_int64_t pos;
134         printf("\r\t%d [raw position %ld]...     ",i,(long)val);
135         fflush(stdout);
136         ret=ov_raw_seek(&ov,val);
137         if(ret<0){
138           printf("seek failed: %d\n",ret);
139           exit(1);
140         }
141
142         _verify(&ov,pos,val,-1,pcmlength,bigassbuffer);
143
144       }
145     }
146
147     printf("\r");
148     {
149       ogg_int64_t length=ov.end;
150       printf("testing pcm page seeking to random places in %ld samples....\n",
151              (long)pcmlength);
152     
153       for(i=0;i<1000;i++){
154         ogg_int64_t val=(double)rand()/RAND_MAX*pcmlength;
155         ogg_int64_t pos;
156         printf("\r\t%d [pcm position %ld]...     ",i,(long)val);
157         fflush(stdout);
158         ret=ov_pcm_seek_page(&ov,val);
159         if(ret<0){
160           printf("seek failed: %d\n",ret);
161           exit(1);
162         }
163
164         _verify(&ov,pos,-1,val,pcmlength,bigassbuffer);
165
166       }
167     }
168     
169     printf("\r");
170     {
171       ogg_int64_t length=ov.end;
172       printf("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=(double)rand()/RAND_MAX*pcmlength;
177         ogg_int64_t pos;
178         printf("\r\t%d [pcm position %ld]...     ",i,(long)val);
179         fflush(stdout);
180         ret=ov_pcm_seek(&ov,val);
181         if(ret<0){
182           printf("seek failed: %d\n",ret);
183           exit(1);
184         }
185         if(ov_pcm_tell(&ov)!=val){
186           printf("Declared position didn't perfectly match request: %ld != %ld\n",
187                  (long)val,(long)ov_pcm_tell(&ov));
188           exit(1);
189         }
190
191         _verify(&ov,pos,-1,val,pcmlength,bigassbuffer);
192
193       }
194     }
195     
196     printf("\r                                           \nOK.\n\n");
197
198
199   }else{
200     printf("Standard input was not seekable.\n");
201   }
202
203   ov_clear(&ov);
204   return 0;
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218