encoder_example.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH *
  6. * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
  9. * by Monty <monty@xiph.org> and the XIPHOPHORUS Company *
  10. * http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: simple example encoder
  14. last mod: $Id: encoder_example.c,v 1.14.2.3 2000/11/04 06:21:39 xiphmont Exp $
  15. ********************************************************************/
  16. /* takes a stereo 16bit 44.1kHz WAV file from stdin and encodes it into
  17. a Vorbis bitstream */
  18. /* Note that this is POSIX, not ANSI, code */
  19. #include <stdio.h>
  20. #include <stdlib.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(macintosh) && 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;
  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. fread(readbuffer,1,44,stdin);
  60. /********** Encode setup ************/
  61. /* choose an encoding mode */
  62. /* (mode 0: 44kHz stereo uncoupled, roughly 128kbps VBR) */
  63. vorbis_info_init(&vi);
  64. vorbis_encode_init(&vi,2,44100, -1, 128000, -1);
  65. /* add a comment */
  66. vorbis_comment_init(&vc);
  67. vorbis_comment_add(&vc,"Track encoded by encoder_example.c");
  68. /* set up the analysis state and auxiliary encoding storage */
  69. vorbis_analysis_init(&vd,&vi);
  70. vorbis_block_init(&vd,&vb);
  71. /* set up our packet->stream encoder */
  72. /* pick a random serial number; that way we can more likely build
  73. chained streams just by concatenation */
  74. srand(time(NULL));
  75. ogg_stream_init(&os,rand());
  76. /* Vorbis streams begin with three headers; the initial header (with
  77. most of the codec setup parameters) which is mandated by the Ogg
  78. bitstream spec. The second header holds any comment fields. The
  79. third header holds the bitstream codebook. We merely need to
  80. make the headers, then pass them to libvorbis one at a time;
  81. libvorbis handles the additional Ogg bitstream constraints */
  82. {
  83. ogg_packet header;
  84. ogg_packet header_comm;
  85. ogg_packet header_code;
  86. vorbis_analysis_headerout(&vd,&vc,&header,&header_comm,&header_code);
  87. ogg_stream_packetin(&os,&header); /* automatically placed in its own
  88. page */
  89. ogg_stream_packetin(&os,&header_comm);
  90. ogg_stream_packetin(&os,&header_code);
  91. /* We don't have to write out here, but doing so makes streaming
  92. * much easier, so we do, flushing ALL pages. This ensures the actual
  93. * audio data will start on a new page
  94. */
  95. while(!eos){
  96. int result=ogg_stream_flush(&os,&og);
  97. if(result==0)break;
  98. fwrite(og.header,1,og.header_len,stdout);
  99. fwrite(og.body,1,og.body_len,stdout);
  100. }
  101. }
  102. while(!eos){
  103. long i;
  104. long bytes=fread(readbuffer,1,READ*4,stdin); /* stereo hardwired here */
  105. if(bytes==0){
  106. /* end of file. this can be done implicitly in the mainline,
  107. but it's easier to see here in non-clever fashion.
  108. Tell the library we're at end of stream so that it can handle
  109. the last frame and mark end of stream in the output properly */
  110. vorbis_analysis_wrote(&vd,0);
  111. }else{
  112. /* data to encode */
  113. /* expose the buffer to submit data */
  114. float **buffer=vorbis_analysis_buffer(&vd,READ);
  115. /* uninterleave samples */
  116. for(i=0;i<bytes/4;i++){
  117. buffer[0][i]=((readbuffer[i*4+1]<<8)|
  118. (0x00ff&(int)readbuffer[i*4]))/32768.;
  119. buffer[1][i]=((readbuffer[i*4+3]<<8)|
  120. (0x00ff&(int)readbuffer[i*4+2]))/32768.;
  121. }
  122. /* tell the library how much we actually submitted */
  123. vorbis_analysis_wrote(&vd,i);
  124. }
  125. /* vorbis does some data preanalysis, then divvies up blocks for
  126. more involved (potentially parallel) processing. Get a single
  127. block for encoding now */
  128. while(vorbis_analysis_blockout(&vd,&vb)==1){
  129. /* analysis */
  130. vorbis_analysis(&vb,&op);
  131. /* weld the packet into the bitstream */
  132. ogg_stream_packetin(&os,&op);
  133. /* write out pages (if any) */
  134. while(!eos){
  135. int result=ogg_stream_pageout(&os,&og);
  136. if(result==0)break;
  137. fwrite(og.header,1,og.header_len,stdout);
  138. fwrite(og.body,1,og.body_len,stdout);
  139. /* this could be set above, but for illustrative purposes, I do
  140. it here (to show that vorbis does know where the stream ends) */
  141. if(ogg_page_eos(&og))eos=1;
  142. }
  143. }
  144. }
  145. /* clean up and exit. vorbis_info_clear() must be called last */
  146. ogg_stream_clear(&os);
  147. vorbis_block_clear(&vb);
  148. vorbis_dsp_clear(&vd);
  149. vorbis_info_clear(&vi);
  150. /* ogg_page and ogg_packet structs always point to storage in
  151. libvorbis. They're never freed or manipulated directly */
  152. fprintf(stderr,"Done.\n");
  153. return(0);
  154. }