decoder_example.c 11 KB

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