decoder_example.c 9.9 KB

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