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.8 2001/05/27 06:43:58 xiphmont Exp $
16 ********************************************************************/
20 #include "vorbis/codec.h"
21 #include "vorbis/vorbisfile.h"
22 #include "../lib/misc.h"
25 void _verify(OggVorbis_File *ov,ogg_int64_t pos,
26 ogg_int64_t val,ogg_int64_t pcmval,
27 ogg_int64_t pcmlength,
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));
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));
46 if(pos<0 || pos>pcmlength){
47 printf("pcm position out of bounds: got %ld\n",(long)pos);
50 bread=ov_read(ov,buffer,4096,1,1,1,&dummy);
52 if(buffer[j]!=bigassbuffer[j+pos*2]){
53 printf("data position after seek doesn't match pcm position\n");
62 ogg_int64_t pcmlength;
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");
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);
80 printf("Sorry; right now seeking_test can only use Vorbis files\n"
81 "that are entirely stereo.\n\n");
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");
90 pcmlength=ov_pcm_total(&ov,-1);
91 bigassbuffer=malloc(pcmlength*2); /* w00t */
94 int ret=ov_read(&ov,bigassbuffer+i,pcmlength*2-i,1,1,1,&dummy);
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
107 ogg_int64_t length=ov.end;
108 printf("testing raw seeking to random places in %ld bytes....\n",
112 ogg_int64_t val=(double)rand()/RAND_MAX*length;
114 printf("\r\t%d [raw position %ld]... ",i,(long)val);
116 ret=ov_raw_seek(&ov,val);
118 printf("seek failed: %d\n",ret);
122 _verify(&ov,pos,val,-1,pcmlength,bigassbuffer);
129 ogg_int64_t length=ov.end;
130 printf("testing pcm page seeking to random places in %ld samples....\n",
134 ogg_int64_t val=(double)rand()/RAND_MAX*pcmlength;
136 printf("\r\t%d [pcm position %ld]... ",i,(long)val);
138 ret=ov_pcm_seek_page(&ov,val);
140 printf("seek failed: %d\n",ret);
144 _verify(&ov,pos,-1,val,pcmlength,bigassbuffer);
151 ogg_int64_t length=ov.end;
152 printf("testing pcm exact seeking to random places in %ld samples....\n",
156 ogg_int64_t val=(double)rand()/RAND_MAX*pcmlength;
158 printf("\r\t%d [pcm position %ld]... ",i,(long)val);
160 ret=ov_pcm_seek(&ov,val);
162 printf("seek failed: %d\n",ret);
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));
171 _verify(&ov,pos,-1,val,pcmlength,bigassbuffer);
176 printf("\r \nOK.\n\n");
180 printf("Standard input was not seekable.\n");