encoder_example.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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-2001 *
  9. * by the XIPHOPHORUS Company http://www.xiph.org/ *
  10. ********************************************************************
  11. function: simple example encoder
  12. last mod: $Id: encoder_example.c,v 1.28.2.1 2001/12/18 23:49:15 xiphmont Exp $
  13. ********************************************************************/
  14. /* takes a stereo 16bit 44.1kHz WAV file from stdin and encodes it into
  15. a Vorbis bitstream */
  16. /* Note that this is POSIX, not ANSI, code */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include <math.h>
  22. #include <vorbis/vorbisenc.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(macintosh) && defined(__MWERKS__)
  28. #include <console.h> /* CodeWarrior's Mac "command-line" support */
  29. #endif
  30. #define READ 1024
  31. signed char readbuffer[READ*4+44]; /* out of the data segment, not the stack */
  32. int main(){
  33. ogg_stream_state os; /* take physical pages, weld into a logical
  34. stream of packets */
  35. ogg_page og; /* one Ogg bitstream page. Vorbis packets are inside */
  36. ogg_packet op; /* one raw packet of data for decode */
  37. vorbis_info vi; /* struct that stores all the static vorbis bitstream
  38. settings */
  39. vorbis_comment vc; /* struct that stores all the user comments */
  40. vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
  41. vorbis_block vb; /* local working space for packet->PCM decode */
  42. int eos=0;
  43. int i, founddata;
  44. #if defined(macintosh) && defined(__MWERKS__)
  45. int argc = 0;
  46. char **argv = NULL;
  47. argc = ccommand(&argv); /* get a "command line" from the Mac user */
  48. /* this also lets the user set stdin and stdout */
  49. #endif
  50. /* we cheat on the WAV header; we just bypass 44 bytes and never
  51. verify that it matches 16bit/stereo/44.1kHz. This is just an
  52. example, after all. */
  53. #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
  54. /* Beware the evil ifdef. We avoid these where we can, but this one we
  55. cannot. Don't add any more, you'll probably go to hell if you do. */
  56. _setmode( _fileno( stdin ), _O_BINARY );
  57. _setmode( _fileno( stdout ), _O_BINARY );
  58. #endif
  59. /* we cheat on the WAV header; we just bypass the header and never
  60. verify that it matches 16bit/stereo/44.1kHz. This is just an
  61. example, after all. */
  62. readbuffer[0] = '\0';
  63. for (i=0, founddata=0; i<30 && ! feof(stdin) && ! ferror(stdin); i++)
  64. {
  65. fread(readbuffer,1,2,stdin);
  66. if ( ! strncmp(readbuffer, "da", 2) )
  67. {
  68. founddata = 1;
  69. fread(readbuffer,1,6,stdin);
  70. break;
  71. }
  72. }
  73. /********** Encode setup ************/
  74. /* choose an encoding mode */
  75. /* (mode 0: 44kHz stereo uncoupled, roughly 128kbps VBR) */
  76. vorbis_info_init(&vi);
  77. vorbis_encode_init_vbr(&vi,2,44100,.4);
  78. /* add a comment */
  79. vorbis_comment_init(&vc);
  80. vorbis_comment_add(&vc,"ENCODER=encoder_example.c");
  81. /* set up the analysis state and auxiliary encoding storage */
  82. vorbis_analysis_init(&vd,&vi);
  83. vorbis_block_init(&vd,&vb);
  84. /* set up our packet->stream encoder */
  85. /* pick a random serial number; that way we can more likely build
  86. chained streams just by concatenation */
  87. srand(time(NULL));
  88. ogg_stream_init(&os,rand());
  89. /* Vorbis streams begin with three headers; the initial header (with
  90. most of the codec setup parameters) which is mandated by the Ogg
  91. bitstream spec. The second header holds any comment fields. The
  92. third header holds the bitstream codebook. We merely need to
  93. make the headers, then pass them to libvorbis one at a time;
  94. libvorbis handles the additional Ogg bitstream constraints */
  95. {
  96. ogg_packet header;
  97. ogg_packet header_comm;
  98. ogg_packet header_code;
  99. vorbis_analysis_headerout(&vd,&vc,&header,&header_comm,&header_code);
  100. ogg_stream_packetin(&os,&header); /* automatically placed in its own
  101. page */
  102. ogg_stream_packetin(&os,&header_comm);
  103. ogg_stream_packetin(&os,&header_code);
  104. /* We don't have to write out here, but doing so makes streaming
  105. * much easier, so we do, flushing ALL pages. This ensures the actual
  106. * audio data will start on a new page
  107. */
  108. while(!eos){
  109. int result=ogg_stream_flush(&os,&og);
  110. if(result==0)break;
  111. fwrite(og.header,1,og.header_len,stdout);
  112. fwrite(og.body,1,og.body_len,stdout);
  113. }
  114. }
  115. while(!eos){
  116. long i;
  117. long bytes=fread(readbuffer,1,READ*4,stdin); /* stereo hardwired here */
  118. if(bytes==0){
  119. /* end of file. this can be done implicitly in the mainline,
  120. but it's easier to see here in non-clever fashion.
  121. Tell the library we're at end of stream so that it can handle
  122. the last frame and mark end of stream in the output properly */
  123. vorbis_analysis_wrote(&vd,0);
  124. }else{
  125. /* data to encode */
  126. /* expose the buffer to submit data */
  127. float **buffer=vorbis_analysis_buffer(&vd,READ);
  128. /* uninterleave samples */
  129. for(i=0;i<bytes/4;i++){
  130. buffer[0][i]=((readbuffer[i*4+1]<<8)|
  131. (0x00ff&(int)readbuffer[i*4]))/32768.f;
  132. buffer[1][i]=((readbuffer[i*4+3]<<8)|
  133. (0x00ff&(int)readbuffer[i*4+2]))/32768.f;
  134. }
  135. /* tell the library how much we actually submitted */
  136. vorbis_analysis_wrote(&vd,i);
  137. }
  138. /* vorbis does some data preanalysis, then divvies up blocks for
  139. more involved (potentially parallel) processing. Get a single
  140. block for encoding now */
  141. while(vorbis_analysis_blockout(&vd,&vb)==1){
  142. /* analysis, assume we want to use bitrate management */
  143. vorbis_analysis(&vb,NULL);
  144. vorbis_bitrate_addblock(&vb);
  145. while(vorbis_bitrate_flushpacket(&vd,&op)){
  146. /* weld the packet into the bitstream */
  147. ogg_stream_packetin(&os,&op);
  148. /* write out pages (if any) */
  149. while(!eos){
  150. int result=ogg_stream_pageout(&os,&og);
  151. if(result==0)break;
  152. fwrite(og.header,1,og.header_len,stdout);
  153. fwrite(og.body,1,og.body_len,stdout);
  154. /* this could be set above, but for illustrative purposes, I do
  155. it here (to show that vorbis does know where the stream ends) */
  156. if(ogg_page_eos(&og))eos=1;
  157. }
  158. }
  159. }
  160. }
  161. /* clean up and exit. vorbis_info_clear() must be called last */
  162. ogg_stream_clear(&os);
  163. vorbis_block_clear(&vb);
  164. vorbis_dsp_clear(&vd);
  165. vorbis_comment_clear(&vc);
  166. vorbis_info_clear(&vi);
  167. /* ogg_page and ogg_packet structs always point to storage in
  168. libvorbis. They're never freed or manipulated directly */
  169. fprintf(stderr,"Done.\n");
  170. return(0);
  171. }