write_read.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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: utility functions for vorbis codec test suite.
  13. last mod: $Id: util.c 13293 2007-07-24 00:09:47Z erikd $
  14. ********************************************************************/
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <math.h>
  18. #include <string.h>
  19. #include <errno.h>
  20. #include <vorbis/codec.h>
  21. #include <vorbis/vorbisenc.h>
  22. #include "write_read.h"
  23. /* The following function is basically a hacked version of the code in
  24. * examples/encoder_example.c */
  25. void
  26. write_vorbis_data_or_die (const char *filename, int srate, float q, const float * data, int count, int ch)
  27. {
  28. FILE * file ;
  29. ogg_stream_state os;
  30. ogg_page og;
  31. ogg_packet op;
  32. vorbis_info vi;
  33. vorbis_comment vc;
  34. vorbis_dsp_state vd;
  35. vorbis_block vb;
  36. int eos = 0, ret;
  37. if ((file = fopen (filename, "wb")) == NULL) {
  38. printf("\n\nError : fopen failed : %s\n", strerror (errno)) ;
  39. exit (1) ;
  40. }
  41. /********** Encode setup ************/
  42. vorbis_info_init (&vi);
  43. ret = vorbis_encode_init_vbr (&vi,ch,srate,q);
  44. if (ret) {
  45. printf ("vorbis_encode_init_vbr return %d\n", ret) ;
  46. exit (1) ;
  47. }
  48. vorbis_comment_init (&vc);
  49. vorbis_comment_add_tag (&vc,"ENCODER","test/util.c");
  50. vorbis_analysis_init (&vd,&vi);
  51. vorbis_block_init (&vd,&vb);
  52. ogg_stream_init (&os,12345678);
  53. {
  54. ogg_packet header;
  55. ogg_packet header_comm;
  56. ogg_packet header_code;
  57. vorbis_analysis_headerout (&vd,&vc,&header,&header_comm,&header_code);
  58. ogg_stream_packetin (&os,&header);
  59. ogg_stream_packetin (&os,&header_comm);
  60. ogg_stream_packetin (&os,&header_code);
  61. /* Ensures the audio data will start on a new page. */
  62. while (!eos){
  63. int result = ogg_stream_flush (&os,&og);
  64. if (result == 0)
  65. break;
  66. fwrite (og.header,1,og.header_len,file);
  67. fwrite (og.body,1,og.body_len,file);
  68. }
  69. }
  70. {
  71. /* expose the buffer to submit data */
  72. float **buffer = vorbis_analysis_buffer (&vd,count);
  73. int i;
  74. for(i=0;i<ch;i++)
  75. memcpy (buffer [i], data, count * sizeof (float)) ;
  76. /* tell the library how much we actually submitted */
  77. vorbis_analysis_wrote (&vd,count);
  78. vorbis_analysis_wrote (&vd,0);
  79. }
  80. while (vorbis_analysis_blockout (&vd,&vb) == 1) {
  81. vorbis_analysis (&vb,NULL);
  82. vorbis_bitrate_addblock (&vb);
  83. while (vorbis_bitrate_flushpacket (&vd,&op)) {
  84. ogg_stream_packetin (&os,&op);
  85. while (!eos) {
  86. int result = ogg_stream_pageout (&os,&og);
  87. if (result == 0)
  88. break;
  89. fwrite (og.header,1,og.header_len,file);
  90. fwrite (og.body,1,og.body_len,file);
  91. if (ogg_page_eos (&og))
  92. eos = 1;
  93. }
  94. }
  95. }
  96. ogg_stream_clear (&os);
  97. vorbis_block_clear (&vb);
  98. vorbis_dsp_clear (&vd);
  99. vorbis_comment_clear (&vc);
  100. vorbis_info_clear (&vi);
  101. fclose (file) ;
  102. }
  103. /* The following function is basically a hacked version of the code in
  104. * examples/decoder_example.c */
  105. void
  106. read_vorbis_data_or_die (const char *filename, int srate, float * data, int count)
  107. {
  108. ogg_sync_state oy;
  109. ogg_stream_state os;
  110. ogg_page og;
  111. ogg_packet op;
  112. vorbis_info vi;
  113. vorbis_comment vc;
  114. vorbis_dsp_state vd;
  115. vorbis_block vb;
  116. FILE *file;
  117. char *buffer;
  118. int bytes;
  119. int eos = 0;
  120. int i;
  121. int read_total = 0 ;
  122. if ((file = fopen (filename, "rb")) == NULL) {
  123. printf("\n\nError : fopen failed : %s\n", strerror (errno)) ;
  124. exit (1) ;
  125. }
  126. ogg_sync_init (&oy);
  127. {
  128. /* fragile! Assumes all of our headers will fit in the first 8kB,
  129. which currently they will */
  130. buffer = ogg_sync_buffer (&oy,8192);
  131. bytes = fread (buffer,1,8192,file);
  132. ogg_sync_wrote (&oy,bytes);
  133. if(ogg_sync_pageout (&oy,&og) != 1) {
  134. if(bytes < 8192) {
  135. printf ("Out of data.\n") ;
  136. goto done_decode ;
  137. }
  138. fprintf (stderr,"Input does not appear to be an Ogg bitstream.\n");
  139. exit (1);
  140. }
  141. ogg_stream_init (&os,ogg_page_serialno(&og));
  142. vorbis_info_init (&vi);
  143. vorbis_comment_init (&vc);
  144. if (ogg_stream_pagein (&os,&og) < 0) {
  145. fprintf (stderr,"Error reading first page of Ogg bitstream data.\n");
  146. exit (1);
  147. }
  148. if (ogg_stream_packetout(&os,&op) != 1) {
  149. fprintf (stderr,"Error reading initial header packet.\n");
  150. exit (1);
  151. }
  152. if (vorbis_synthesis_headerin (&vi,&vc,&op) < 0) {
  153. fprintf (stderr,"This Ogg bitstream does not contain Vorbis "
  154. "audio data.\n");
  155. exit (1);
  156. }
  157. i = 0;
  158. while ( i < 2) {
  159. while (i < 2) {
  160. int result = ogg_sync_pageout (&oy,&og);
  161. if(result == 0)
  162. break;
  163. if(result==1) {
  164. ogg_stream_pagein(&os,&og);
  165. while (i < 2) {
  166. result = ogg_stream_packetout (&os,&op);
  167. if (result == 0) break;
  168. if (result < 0) {
  169. fprintf (stderr,"Corrupt secondary header. Exiting.\n");
  170. exit(1);
  171. }
  172. vorbis_synthesis_headerin (&vi,&vc,&op);
  173. i++;
  174. }
  175. }
  176. }
  177. buffer = ogg_sync_buffer (&oy,4096);
  178. bytes = fread (buffer,1,4096,file);
  179. if (bytes == 0 && i < 2) {
  180. fprintf (stderr,"End of file before finding all Vorbis headers!\n");
  181. exit (1);
  182. }
  183. ogg_sync_wrote (&oy,bytes);
  184. }
  185. if (vi.rate != srate) {
  186. printf ("\n\nError : File '%s' has sample rate of %ld when it should be %d.\n\n", filename, vi.rate, srate);
  187. exit (1) ;
  188. }
  189. vorbis_synthesis_init (&vd,&vi);
  190. vorbis_block_init (&vd,&vb);
  191. while(!eos) {
  192. while (!eos) {
  193. int result = ogg_sync_pageout (&oy,&og);
  194. if (result == 0)
  195. break;
  196. if (result < 0) {
  197. fprintf (stderr,"Corrupt or missing data in bitstream; "
  198. "continuing...\n");
  199. } else {
  200. ogg_stream_pagein (&os,&og);
  201. while (1) {
  202. result = ogg_stream_packetout (&os,&op);
  203. if (result == 0)
  204. break;
  205. if (result < 0) {
  206. /* no reason to complain; already complained above */
  207. } else {
  208. float **pcm;
  209. int samples;
  210. if (vorbis_synthesis (&vb,&op) == 0)
  211. vorbis_synthesis_blockin(&vd,&vb);
  212. while ((samples = vorbis_synthesis_pcmout (&vd,&pcm)) > 0 && read_total < count) {
  213. int bout = samples < count ? samples : count;
  214. bout = read_total + bout > count ? count - read_total : bout;
  215. memcpy (data + read_total, pcm[0], bout * sizeof (float)) ;
  216. vorbis_synthesis_read (&vd,bout);
  217. read_total += bout ;
  218. }
  219. }
  220. }
  221. if (ogg_page_eos (&og)) eos = 1;
  222. }
  223. }
  224. if (!eos) {
  225. buffer = ogg_sync_buffer (&oy,4096);
  226. bytes = fread (buffer,1,4096,file);
  227. ogg_sync_wrote (&oy,bytes);
  228. if (bytes == 0) eos = 1;
  229. }
  230. }
  231. ogg_stream_clear (&os);
  232. vorbis_block_clear (&vb);
  233. vorbis_dsp_clear (&vd);
  234. vorbis_comment_clear (&vc);
  235. vorbis_info_clear (&vi);
  236. }
  237. done_decode:
  238. /* OK, clean up the framer */
  239. ogg_sync_clear (&oy);
  240. fclose (file) ;
  241. }