[d71f86]: examples / seeking_example.c Maximize Restore History

Download this file

seeking_example.c    264 lines (211 with data), 6.7 kB

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