1 /********************************************************************
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. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
9 * by the XIPHOPHORUS Company http://www.xiph.org/ *
11 ********************************************************************
13 function: illustrate seeking, and test it too
14 last mod: $Id: seeking_example.c,v 1.9 2001/09/15 04:47:48 cwolf Exp $
16 ********************************************************************/
20 #include "vorbis/codec.h"
21 #include "vorbis/vorbisfile.h"
22 //#include "../lib/misc.h"
24 #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
29 void _verify(OggVorbis_File *ov,ogg_int64_t pos,
30 ogg_int64_t val,ogg_int64_t pcmval,
31 ogg_int64_t pcmlength,
38 /* verify the raw position, the pcm position and position decode */
39 if(val!=-1 && ov_raw_tell(ov)<val){
40 printf("raw position out of tolerance: requested %ld, got %ld\n",
41 (long)val,(long)ov_raw_tell(ov));
44 if(pcmval!=-1 && ov_pcm_tell(ov)>pcmval){
45 printf("pcm position out of tolerance: requested %ld, got %ld\n",
46 (long)pcmval,(long)ov_pcm_tell(ov));
50 if(pos<0 || pos>pcmlength){
51 printf("pcm position out of bounds: got %ld\n",(long)pos);
54 bread=ov_read(ov,buffer,4096,1,1,1,&dummy);
56 if(buffer[j]!=bigassbuffer[j+pos*2]){
57 printf("data position after seek doesn't match pcm position\n");
63 int main(int argc, char *argv[]){
66 ogg_int64_t pcmlength;
72 #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
73 _setmode( _fileno( stdin ), _O_BINARY );
74 _setmode( _fileno( stdout ), _O_BINARY );
77 /* If command line args were supplied, open the named file(s)
78 for i/o, else maintain use of stdin/stdout.*/
81 if ((fpin = fopen(argv[1], "rb")) == (FILE*)NULL)
83 (void)sprintf(msg, "Can't open \"%s\" for input", argv[1]);
91 /* open the file/pipe on stdin */
92 if(ov_open(fpin,&ov,NULL,-1)<0){
93 printf("Could not open input as an OggVorbis file.\n\n");
99 /* to simplify our own lives, we want to assume the whole file is
100 stereo. Verify this to avoid potentially mystifying users
101 (pissing them off is OK, just don't confuse them) */
102 for(i=0;i<ov.links;i++){
103 vorbis_info *vi=ov_info(&ov,i);
105 printf("Sorry; right now seeking_test can only use Vorbis files\n"
106 "that are entirely stereo.\n\n");
111 /* because we want to do sample-level verification that the seek
112 does what it claimed, decode the entire file into memory */
113 printf("loading....\n");
115 pcmlength=ov_pcm_total(&ov,-1);
116 bigassbuffer=malloc(pcmlength*2); /* w00t */
118 while(i<pcmlength*2){
119 int ret=ov_read(&ov,bigassbuffer+i,pcmlength*2-i,1,1,1,&dummy);
128 /* Exercise all the real seeking cases; ov_raw_seek,
129 ov_pcm_seek_page and ov_pcm_seek. time seek is just a wrapper
132 ogg_int64_t length=ov.end;
133 printf("testing raw seeking to random places in %ld bytes....\n",
137 ogg_int64_t val=(double)rand()/RAND_MAX*length;
139 printf("\r\t%d [raw position %ld]... ",i,(long)val);
141 ret=ov_raw_seek(&ov,val);
143 printf("seek failed: %d\n",ret);
147 _verify(&ov,pos,val,-1,pcmlength,bigassbuffer);
154 ogg_int64_t length=ov.end;
155 printf("testing pcm page seeking to random places in %ld samples....\n",
159 ogg_int64_t val=(double)rand()/RAND_MAX*pcmlength;
161 printf("\r\t%d [pcm position %ld]... ",i,(long)val);
163 ret=ov_pcm_seek_page(&ov,val);
165 printf("seek failed: %d\n",ret);
169 _verify(&ov,pos,-1,val,pcmlength,bigassbuffer);
176 ogg_int64_t length=ov.end;
177 printf("testing pcm exact seeking to random places in %ld samples....\n",
181 ogg_int64_t val=(double)rand()/RAND_MAX*pcmlength;
183 printf("\r\t%d [pcm position %ld]... ",i,(long)val);
185 ret=ov_pcm_seek(&ov,val);
187 printf("seek failed: %d\n",ret);
190 if(ov_pcm_tell(&ov)!=val){
191 printf("Decalred position didn't perfectly match request: %ld != %ld\n",
192 (long)val,(long)ov_pcm_tell(&ov));
196 _verify(&ov,pos,-1,val,pcmlength,bigassbuffer);
201 printf("\r \nOK.\n\n");
205 printf("Standard input was not seekable.\n");