encoder_example.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
  6. * PLEASE READ THESE TERMS DISTRIBUTING. *
  7. * *
  8. * THE OggSQUISH 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.13.2.1.2.1 2000/09/03 08:34:51 jack 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/mode_C.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. vi=&info_C;
  64. /* add a comment */
  65. vorbis_comment_init(&vc);
  66. vorbis_comment_add(&vc,"Track encoded by encoder_example.c");
  67. /* set up the analysis state and auxiliary encoding storage */
  68. vorbis_analysis_init(&vd,vi);
  69. vorbis_block_init(&vd,&vb);
  70. /* set up our packet->stream encoder */
  71. /* pick a random serial number; that way we can more likely build
  72. chained streams just by concatenation */
  73. srand(time(NULL));
  74. ogg_stream_init(&os,rand());
  75. /* Vorbis streams begin with three headers; the initial header (with
  76. most of the codec setup parameters) which is mandated by the Ogg
  77. bitstream spec. The second header holds any comment fields. The
  78. third header holds the bitstream codebook. We merely need to
  79. make the headers, then pass them to libvorbis one at a time;
  80. libvorbis handles the additional Ogg bitstream constraints */
  81. {
  82. ogg_packet header;
  83. ogg_packet header_comm;
  84. ogg_packet header_code;
  85. vorbis_analysis_headerout(&vd,&vc,&header,&header_comm,&header_code);
  86. ogg_stream_packetin(&os,&header); /* automatically placed in its own
  87. page */
  88. ogg_stream_packetin(&os,&header_comm);
  89. ogg_stream_packetin(&os,&header_code);
  90. /* no need to write out here. We'll get to that in the main loop */
  91. }
  92. while(!eos){
  93. long i;
  94. long bytes=fread(readbuffer,1,READ*4,stdin); /* stereo hardwired here */
  95. if(bytes==0){
  96. /* end of file. this can be done implicitly in the mainline,
  97. but it's easier to see here in non-clever fashion.
  98. Tell the library we're at end of stream so that it can handle
  99. the last frame and mark end of stream in the output properly */
  100. vorbis_analysis_wrote(&vd,0);
  101. }else{
  102. /* data to encode */
  103. /* expose the buffer to submit data */
  104. float **buffer=vorbis_analysis_buffer(&vd,READ);
  105. /* uninterleave samples */
  106. for(i=0;i<bytes/4;i++){
  107. buffer[0][i]=((readbuffer[i*4+1]<<8)|
  108. (0x00ff&(int)readbuffer[i*4]))/32768.;
  109. buffer[1][i]=((readbuffer[i*4+3]<<8)|
  110. (0x00ff&(int)readbuffer[i*4+2]))/32768.;
  111. }
  112. /* tell the library how much we actually submitted */
  113. vorbis_analysis_wrote(&vd,i);
  114. }
  115. /* vorbis does some data preanalysis, then divvies up blocks for
  116. more involved (potentially parallel) processing. Get a single
  117. block for encoding now */
  118. while(vorbis_analysis_blockout(&vd,&vb)==1){
  119. /* analysis */
  120. vorbis_analysis(&vb,&op);
  121. /* weld the packet into the bitstream */
  122. ogg_stream_packetin(&os,&op);
  123. /* write out pages (if any) */
  124. while(!eos){
  125. int result=ogg_stream_pageout(&os,&og);
  126. if(result==0)break;
  127. fwrite(og.header,1,og.header_len,stdout);
  128. fwrite(og.body,1,og.body_len,stdout);
  129. /* this could be set above, but for illustrative purposes, I do
  130. it here (to show that vorbis does know where the stream ends) */
  131. if(ogg_page_eos(&og))eos=1;
  132. }
  133. }
  134. }
  135. /* clean up and exit. vorbis_info_clear() must be called last */
  136. ogg_stream_clear(&os);
  137. vorbis_block_clear(&vb);
  138. vorbis_dsp_clear(&vd);
  139. /* ogg_page and ogg_packet structs always point to storage in
  140. libvorbis. They're never freed or manipulated directly */
  141. fprintf(stderr,"Done.\n");
  142. return(0);
  143. }