write_read.c 7.8 KB

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