encoder_example.c 8.6 KB

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