encoder_example.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 encoder
  13. last mod: $Id$
  14. ********************************************************************/
  15. /* takes a stereo 16bit 44.1kHz WAV file from stdin and encodes it into
  16. a Vorbis bitstream */
  17. /* Note that this is POSIX, not ANSI, code */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <time.h>
  22. #include <math.h>
  23. #include <vorbis/vorbisenc.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(__MACOS__) && defined(__MWERKS__)
  29. #include <console.h> /* CodeWarrior's Mac "command-line" support */
  30. #endif
  31. #define READ 1024
  32. signed char readbuffer[READ*4+44]; /* out of the data segment, not the stack */
  33. int main(){
  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 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. int eos=0,ret;
  44. int i, founddata;
  45. #if defined(macintosh) && defined(__MWERKS__)
  46. int argc = 0;
  47. char **argv = NULL;
  48. argc = ccommand(&argv); /* get a "command line" from the Mac user */
  49. /* this also lets the user set stdin and stdout */
  50. #endif
  51. /* we cheat on the WAV header; we just bypass 44 bytes and never
  52. verify that it matches 16bit/stereo/44.1kHz. This is just an
  53. example, after all. */
  54. #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
  55. /* if we were reading/writing a file, it would also need to in
  56. binary mode, eg, fopen("file.wav","wb"); */
  57. /* Beware the evil ifdef. We avoid these where we can, but this one we
  58. cannot. Don't add any more, you'll probably go to hell if you do. */
  59. _setmode( _fileno( stdin ), _O_BINARY );
  60. _setmode( _fileno( stdout ), _O_BINARY );
  61. #endif
  62. /* we cheat on the WAV header; we just bypass the header and never
  63. verify that it matches 16bit/stereo/44.1kHz. This is just an
  64. example, after all. */
  65. readbuffer[0] = '\0';
  66. for (i=0, founddata=0; i<30 && ! feof(stdin) && ! ferror(stdin); i++)
  67. {
  68. fread(readbuffer,1,2,stdin);
  69. if ( ! strncmp((char*)readbuffer, "da", 2) )
  70. {
  71. founddata = 1;
  72. fread(readbuffer,1,6,stdin);
  73. break;
  74. }
  75. }
  76. /********** Encode setup ************/
  77. vorbis_info_init(&vi);
  78. /* choose an encoding mode. A few possibilities commented out, one
  79. actually used: */
  80. /*********************************************************************
  81. Encoding using a VBR quality mode. The usable range is -.1
  82. (lowest quality, smallest file) to 1. (highest quality, largest file).
  83. Example quality mode .4: 44kHz stereo coupled, roughly 128kbps VBR
  84. ret = vorbis_encode_init_vbr(&vi,2,44100,.4);
  85. ---------------------------------------------------------------------
  86. Encoding using an average bitrate mode (ABR).
  87. example: 44kHz stereo coupled, average 128kbps VBR
  88. ret = vorbis_encode_init(&vi,2,44100,-1,128000,-1);
  89. ---------------------------------------------------------------------
  90. Encode using a quality mode, but select that quality mode by asking for
  91. an approximate bitrate. This is not ABR, it is true VBR, but selected
  92. using the bitrate interface, and then turning bitrate management off:
  93. ret = ( vorbis_encode_setup_managed(&vi,2,44100,-1,128000,-1) ||
  94. vorbis_encode_ctl(&vi,OV_ECTL_RATEMANAGE2_SET,NULL) ||
  95. vorbis_encode_setup_init(&vi));
  96. *********************************************************************/
  97. ret=vorbis_encode_init_vbr(&vi,2,44100,0.1);
  98. /* do not continue if setup failed; this can happen if we ask for a
  99. mode that libVorbis does not support (eg, too low a bitrate, etc,
  100. will return 'OV_EIMPL') */
  101. if(ret)exit(1);
  102. /* add a comment */
  103. vorbis_comment_init(&vc);
  104. vorbis_comment_add_tag(&vc,"ENCODER","encoder_example.c");
  105. /* set up the analysis state and auxiliary encoding storage */
  106. vorbis_analysis_init(&vd,&vi);
  107. vorbis_block_init(&vd,&vb);
  108. /* set up our packet->stream encoder */
  109. /* pick a random serial number; that way we can more likely build
  110. chained streams just by concatenation */
  111. srand(time(NULL));
  112. ogg_stream_init(&os,rand());
  113. /* Vorbis streams begin with three headers; the initial header (with
  114. most of the codec setup parameters) which is mandated by the Ogg
  115. bitstream spec. The second header holds any comment fields. The
  116. third header holds the bitstream codebook. We merely need to
  117. make the headers, then pass them to libvorbis one at a time;
  118. libvorbis handles the additional Ogg bitstream constraints */
  119. {
  120. ogg_packet header;
  121. ogg_packet header_comm;
  122. ogg_packet header_code;
  123. vorbis_analysis_headerout(&vd,&vc,&header,&header_comm,&header_code);
  124. ogg_stream_packetin(&os,&header); /* automatically placed in its own
  125. page */
  126. ogg_stream_packetin(&os,&header_comm);
  127. ogg_stream_packetin(&os,&header_code);
  128. /* This ensures the actual
  129. * audio data will start on a new page, as per spec
  130. */
  131. while(!eos){
  132. int result=ogg_stream_flush(&os,&og);
  133. if(result==0)break;
  134. fwrite(og.header,1,og.header_len,stdout);
  135. fwrite(og.body,1,og.body_len,stdout);
  136. }
  137. }
  138. while(!eos){
  139. long i;
  140. long bytes=fread(readbuffer,1,READ*4,stdin); /* stereo hardwired here */
  141. if(bytes==0){
  142. /* end of file. this can be done implicitly in the mainline,
  143. but it's easier to see here in non-clever fashion.
  144. Tell the library we're at end of stream so that it can handle
  145. the last frame and mark end of stream in the output properly */
  146. vorbis_analysis_wrote(&vd,0);
  147. }else{
  148. /* data to encode */
  149. /* expose the buffer to submit data */
  150. float **buffer=vorbis_analysis_buffer(&vd,READ);
  151. /* uninterleave samples */
  152. for(i=0;i<bytes/4;i++){
  153. buffer[0][i]=((readbuffer[i*4+1]<<8)|
  154. (0x00ff&(int)readbuffer[i*4]))/32768.f;
  155. buffer[1][i]=((readbuffer[i*4+3]<<8)|
  156. (0x00ff&(int)readbuffer[i*4+2]))/32768.f;
  157. }
  158. /* tell the library how much we actually submitted */
  159. vorbis_analysis_wrote(&vd,i);
  160. }
  161. /* vorbis does some data preanalysis, then divvies up blocks for
  162. more involved (potentially parallel) processing. Get a single
  163. block for encoding now */
  164. while(vorbis_analysis_blockout(&vd,&vb)==1){
  165. /* analysis, assume we want to use bitrate management */
  166. vorbis_analysis(&vb,NULL);
  167. vorbis_bitrate_addblock(&vb);
  168. while(vorbis_bitrate_flushpacket(&vd,&op)){
  169. /* weld the packet into the bitstream */
  170. ogg_stream_packetin(&os,&op);
  171. /* write out pages (if any) */
  172. while(!eos){
  173. int result=ogg_stream_pageout(&os,&og);
  174. if(result==0)break;
  175. fwrite(og.header,1,og.header_len,stdout);
  176. fwrite(og.body,1,og.body_len,stdout);
  177. /* this could be set above, but for illustrative purposes, I do
  178. it here (to show that vorbis does know where the stream ends) */
  179. if(ogg_page_eos(&og))eos=1;
  180. }
  181. }
  182. }
  183. }
  184. /* clean up and exit. vorbis_info_clear() must be called last */
  185. ogg_stream_clear(&os);
  186. vorbis_block_clear(&vb);
  187. vorbis_dsp_clear(&vd);
  188. vorbis_comment_clear(&vc);
  189. vorbis_info_clear(&vi);
  190. /* ogg_page and ogg_packet structs always point to storage in
  191. libvorbis. They're never freed or manipulated directly */
  192. fprintf(stderr,"Done.\n");
  193. return(0);
  194. }