seeking_example.c 6.6 KB

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