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.11 2001/12/18 01:07:54 segher Exp $
16 ********************************************************************/
20 #include "vorbis/codec.h"
21 #include "vorbis/vorbisfile.h"
23 #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
28 void _verify(OggVorbis_File *ov,ogg_int64_t pos,
29 ogg_int64_t val,ogg_int64_t pcmval,
30 ogg_int64_t pcmlength,
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));
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));
49 if(pos<0 || pos>pcmlength){
50 printf("pcm position out of bounds: got %ld\n",(long)pos);
53 bread=ov_read(ov,buffer,4096,1,1,1,&dummy);
55 if(buffer[j]!=bigassbuffer[j+pos*2]){
56 printf("data position after seek doesn't match pcm position\n");
65 ogg_int64_t pcmlength;
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 );
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");
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);
89 printf("Sorry; right now seeking_test can only use Vorbis files\n"
90 "that are entirely stereo.\n\n");
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");
99 pcmlength=ov_pcm_total(&ov,-1);
100 bigassbuffer=malloc(pcmlength*2); /* w00t */
102 while(i<pcmlength*2){
103 int ret=ov_read(&ov,bigassbuffer+i,pcmlength*2-i,1,1,1,&dummy);
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
116 ogg_int64_t length=ov.end;
117 printf("testing raw seeking to random places in %ld bytes....\n",
121 ogg_int64_t val=(double)rand()/RAND_MAX*length;
123 printf("\r\t%d [raw position %ld]... ",i,(long)val);
125 ret=ov_raw_seek(&ov,val);
127 printf("seek failed: %d\n",ret);
131 _verify(&ov,pos,val,-1,pcmlength,bigassbuffer);
138 ogg_int64_t length=ov.end;
139 printf("testing pcm page seeking to random places in %ld samples....\n",
143 ogg_int64_t val=(double)rand()/RAND_MAX*pcmlength;
145 printf("\r\t%d [pcm position %ld]... ",i,(long)val);
147 ret=ov_pcm_seek_page(&ov,val);
149 printf("seek failed: %d\n",ret);
153 _verify(&ov,pos,-1,val,pcmlength,bigassbuffer);
160 ogg_int64_t length=ov.end;
161 printf("testing pcm exact seeking to random places in %ld samples....\n",
165 ogg_int64_t val=(double)rand()/RAND_MAX*pcmlength;
167 printf("\r\t%d [pcm position %ld]... ",i,(long)val);
169 ret=ov_pcm_seek(&ov,val);
171 printf("seek failed: %d\n",ret);
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));
180 _verify(&ov,pos,-1,val,pcmlength,bigassbuffer);
185 printf("\r \nOK.\n\n");
189 printf("Standard input was not seekable.\n");