decoder_example.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH *
  6. * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
  9. * by Monty <monty@xiph.org> and the XIPHOPHORUS Company *
  10. * http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: simple example decoder
  14. last mod: $Id: decoder_example.c,v 1.13.2.2 2000/11/04 06:21:39 xiphmont Exp $
  15. ********************************************************************/
  16. /* Takes a vorbis bitstream from stdin and writes raw stereo PCM to
  17. stdout. Decodes simple and chained OggVorbis files from beginning
  18. to end. Vorbisfile.a is somewhat more complex than the code below. */
  19. /* Note that this is POSIX, not ANSI code */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <math.h>
  23. #include <vorbis/codec.h>
  24. #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
  25. #include <io.h>
  26. #include <fcntl.h>
  27. #endif
  28. #if defined(macintosh) && defined(__MWERKS__)
  29. #include <console.h> /* CodeWarrior's Mac "command-line" support */
  30. #endif
  31. ogg_int16_t convbuffer[4096]; /* take 8k out of the data segment, not the stack */
  32. int convsize=4096;
  33. int main(int argc, char **argv){
  34. ogg_sync_state oy; /* sync and verify incoming physical bitstream */
  35. ogg_stream_state os; /* take physical pages, weld into a logical
  36. stream of packets */
  37. ogg_page og; /* one Ogg bitstream page. Vorbis packets are inside */
  38. ogg_packet op; /* one raw packet of data for decode */
  39. vorbis_info vi; /* struct that stores all the static vorbis bitstream
  40. settings */
  41. vorbis_comment vc; /* struct that stores all the bitstream user comments */
  42. vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
  43. vorbis_block vb; /* local working space for packet->PCM decode */
  44. char *buffer;
  45. int bytes;
  46. #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
  47. /* Beware the evil ifdef. We avoid these where we can, but this one we
  48. cannot. Don't add any more, you'll probably go to hell if you do. */
  49. _setmode( _fileno( stdin ), _O_BINARY );
  50. _setmode( _fileno( stdout ), _O_BINARY );
  51. #endif
  52. #if defined(macintosh) && defined(__MWERKS__)
  53. argc = ccommand(&argv); /* get a "command line" from the Mac user */
  54. /* this also lets the user set stdin and stdout */
  55. #endif
  56. /********** Decode setup ************/
  57. ogg_sync_init(&oy); /* Now we can read pages */
  58. while(1){ /* we repeat if the bitstream is chained */
  59. int eos=0;
  60. int i;
  61. /* grab some data at the head of the stream. We want the first page
  62. (which is guaranteed to be small and only contain the Vorbis
  63. stream initial header) We need the first page to get the stream
  64. serialno. */
  65. /* submit a 4k block to libvorbis' Ogg layer */
  66. buffer=ogg_sync_buffer(&oy,4096);
  67. bytes=fread(buffer,1,4096,stdin);
  68. ogg_sync_wrote(&oy,bytes);
  69. /* Get the first page. */
  70. if(ogg_sync_pageout(&oy,&og)!=1){
  71. /* have we simply run out of data? If so, we're done. */
  72. if(bytes<4096)break;
  73. /* error case. Must not be Vorbis data */
  74. fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
  75. exit(1);
  76. }
  77. /* Get the serial number and set up the rest of decode. */
  78. /* serialno first; use it to set up a logical stream */
  79. ogg_stream_init(&os,ogg_page_serialno(&og));
  80. /* extract the initial header from the first page and verify that the
  81. Ogg bitstream is in fact Vorbis data */
  82. /* I handle the initial header first instead of just having the code
  83. read all three Vorbis headers at once because reading the initial
  84. header is an easy way to identify a Vorbis bitstream and it's
  85. useful to see that functionality seperated out. */
  86. vorbis_info_init(&vi);
  87. vorbis_comment_init(&vc);
  88. if(ogg_stream_pagein(&os,&og)<0){
  89. /* error; stream version mismatch perhaps */
  90. fprintf(stderr,"Error reading first page of Ogg bitstream data.\n");
  91. exit(1);
  92. }
  93. if(ogg_stream_packetout(&os,&op)!=1){
  94. /* no page? must not be vorbis */
  95. fprintf(stderr,"Error reading initial header packet.\n");
  96. exit(1);
  97. }
  98. if(vorbis_synthesis_headerin(&vi,&vc,&op)<0){
  99. /* error case; not a vorbis header */
  100. fprintf(stderr,"This Ogg bitstream does not contain Vorbis "
  101. "audio data.\n");
  102. exit(1);
  103. }
  104. /* At this point, we're sure we're Vorbis. We've set up the logical
  105. (Ogg) bitstream decoder. Get the comment and codebook headers and
  106. set up the Vorbis decoder */
  107. /* The next two packets in order are the comment and codebook headers.
  108. They're likely large and may span multiple pages. Thus we reead
  109. and submit data until we get our two pacakets, watching that no
  110. pages are missing. If a page is missing, error out; losing a
  111. header page is the only place where missing data is fatal. */
  112. i=0;
  113. while(i<2){
  114. while(i<2){
  115. int result=ogg_sync_pageout(&oy,&og);
  116. if(result==0)break; /* Need more data */
  117. /* Don't complain about missing or corrupt data yet. We'll
  118. catch it at the packet output phase */
  119. if(result==1){
  120. ogg_stream_pagein(&os,&og); /* we can ignore any errors here
  121. as they'll also become apparent
  122. at packetout */
  123. while(i<2){
  124. result=ogg_stream_packetout(&os,&op);
  125. if(result==0)break;
  126. if(result<0){
  127. /* Uh oh; data at some point was corrupted or missing!
  128. We can't tolerate that in a header. Die. */
  129. fprintf(stderr,"Corrupt secondary header. Exiting.\n");
  130. exit(1);
  131. }
  132. vorbis_synthesis_headerin(&vi,&vc,&op);
  133. i++;
  134. }
  135. }
  136. }
  137. /* no harm in not checking before adding more */
  138. buffer=ogg_sync_buffer(&oy,4096);
  139. bytes=fread(buffer,1,4096,stdin);
  140. if(bytes==0 && i<2){
  141. fprintf(stderr,"End of file before finding all Vorbis headers!\n");
  142. exit(1);
  143. }
  144. ogg_sync_wrote(&oy,bytes);
  145. }
  146. /* Throw the comments plus a few lines about the bitstream we're
  147. decoding */
  148. {
  149. char **ptr=vc.user_comments;
  150. while(*ptr){
  151. fprintf(stderr,"%s\n",*ptr);
  152. ++ptr;
  153. }
  154. fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi.channels,vi.rate);
  155. fprintf(stderr,"Encoded by: %s\n\n",vc.vendor);
  156. }
  157. convsize=4096/vi.channels;
  158. /* OK, got and parsed all three headers. Initialize the Vorbis
  159. packet->PCM decoder. */
  160. vorbis_synthesis_init(&vd,&vi); /* central decode state */
  161. vorbis_block_init(&vd,&vb); /* local state for most of the decode
  162. so multiple block decodes can
  163. proceed in parallel. We could init
  164. multiple vorbis_block structures
  165. for vd here */
  166. /* The rest is just a straight decode loop until end of stream */
  167. while(!eos){
  168. while(!eos){
  169. int result=ogg_sync_pageout(&oy,&og);
  170. if(result==0)break; /* need more data */
  171. if(result<0){ /* missing or corrupt data at this page position */
  172. fprintf(stderr,"Corrupt or missing data in bitstream; "
  173. "continuing...\n");
  174. }else{
  175. ogg_stream_pagein(&os,&og); /* can safely ignore errors at
  176. this point */
  177. while(1){
  178. result=ogg_stream_packetout(&os,&op);
  179. if(result==0)break; /* need more data */
  180. if(result<0){ /* missing or corrupt data at this page position */
  181. /* no reason to complain; already complained above */
  182. }else{
  183. /* we have a packet. Decode it */
  184. float **pcm;
  185. int samples;
  186. if(vorbis_synthesis(&vb,&op)==0) /* test for success! */
  187. vorbis_synthesis_blockin(&vd,&vb);
  188. /*
  189. **pcm is a multichannel float vector. In stereo, for
  190. example, pcm[0] is left, and pcm[1] is right. samples is
  191. the size of each channel. Convert the float values
  192. (-1.<=range<=1.) to whatever PCM format and write it out */
  193. while((samples=vorbis_synthesis_pcmout(&vd,&pcm))>0){
  194. int j;
  195. int clipflag=0;
  196. int bout=(samples<convsize?samples:convsize);
  197. /* convert floats to 16 bit signed ints (host order) and
  198. interleave */
  199. for(i=0;i<vi.channels;i++){
  200. ogg_int16_t *ptr=convbuffer+i;
  201. float *mono=pcm[i];
  202. for(j=0;j<bout;j++){
  203. #if 1
  204. int val=mono[j]*32767.;
  205. #else /* optional dither */
  206. int val=mono[j]*32767.+drand48()-0.5;
  207. #endif
  208. /* might as well guard against clipping */
  209. if(val>32767){
  210. val=32767;
  211. clipflag=1;
  212. }
  213. if(val<-32768){
  214. val=-32768;
  215. clipflag=1;
  216. }
  217. *ptr=val;
  218. ptr+=2;
  219. }
  220. }
  221. if(clipflag)
  222. fprintf(stderr,"Clipping in frame %ld\n",(long)(vd.sequence));
  223. fwrite(convbuffer,2*vi.channels,bout,stdout);
  224. vorbis_synthesis_read(&vd,bout); /* tell libvorbis how
  225. many samples we
  226. actually consumed */
  227. }
  228. }
  229. }
  230. if(ogg_page_eos(&og))eos=1;
  231. }
  232. }
  233. if(!eos){
  234. buffer=ogg_sync_buffer(&oy,4096);
  235. bytes=fread(buffer,1,4096,stdin);
  236. ogg_sync_wrote(&oy,bytes);
  237. if(bytes==0)eos=1;
  238. }
  239. }
  240. /* clean up this logical bitstream; before exit we see if we're
  241. followed by another [chained] */
  242. ogg_stream_clear(&os);
  243. /* ogg_page and ogg_packet structs always point to storage in
  244. libvorbis. They're never freed or manipulated directly */
  245. vorbis_block_clear(&vb);
  246. vorbis_dsp_clear(&vd);
  247. vorbis_info_clear(&vi); /* must be called last */
  248. }
  249. /* OK, clean up the framer */
  250. ogg_sync_clear(&oy);
  251. fprintf(stderr,"Done.\n");
  252. return(0);
  253. }