write_read.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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, const float * data, int count)
  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,1,srate,0.8);
  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. memcpy (buffer [0], data, count * sizeof (float)) ;
  74. /* tell the library how much we actually submitted */
  75. vorbis_analysis_wrote (&vd,count);
  76. vorbis_analysis_wrote (&vd,0);
  77. }
  78. while (vorbis_analysis_blockout (&vd,&vb) == 1) {
  79. vorbis_analysis (&vb,NULL);
  80. vorbis_bitrate_addblock (&vb);
  81. while (vorbis_bitrate_flushpacket (&vd,&op)) {
  82. ogg_stream_packetin (&os,&op);
  83. while (!eos) {
  84. int result = ogg_stream_pageout (&os,&og);
  85. if (result == 0)
  86. break;
  87. fwrite (og.header,1,og.header_len,file);
  88. fwrite (og.body,1,og.body_len,file);
  89. if (ogg_page_eos (&og))
  90. eos = 1;
  91. }
  92. }
  93. }
  94. ogg_stream_clear (&os);
  95. vorbis_block_clear (&vb);
  96. vorbis_dsp_clear (&vd);
  97. vorbis_comment_clear (&vc);
  98. vorbis_info_clear (&vi);
  99. fclose (file) ;
  100. }
  101. /* The following function is basically a hacked version of the code in
  102. * examples/decoder_example.c */
  103. void
  104. read_vorbis_data_or_die (const char *filename, int srate, float * data, int count)
  105. {
  106. ogg_sync_state oy;
  107. ogg_stream_state os;
  108. ogg_page og;
  109. ogg_packet op;
  110. vorbis_info vi;
  111. vorbis_comment vc;
  112. vorbis_dsp_state vd;
  113. vorbis_block vb;
  114. FILE *file;
  115. char *buffer;
  116. int bytes;
  117. int eos = 0;
  118. int i;
  119. int read_total = 0 ;
  120. if ((file = fopen (filename, "rb")) == NULL) {
  121. printf("\n\nError : fopen failed : %s\n", strerror (errno)) ;
  122. exit (1) ;
  123. }
  124. ogg_sync_init (&oy);
  125. {
  126. buffer = ogg_sync_buffer (&oy,4096);
  127. bytes = fread (buffer,1,4096,file);
  128. ogg_sync_wrote (&oy,bytes);
  129. if(ogg_sync_pageout (&oy,&og) != 1) {
  130. if(bytes < 4096) {
  131. printf ("Out of data.\n") ;
  132. goto done_decode ;
  133. }
  134. fprintf (stderr,"Input does not appear to be an Ogg bitstream.\n");
  135. exit (1);
  136. }
  137. ogg_stream_init (&os,ogg_page_serialno(&og));
  138. vorbis_info_init (&vi);
  139. vorbis_comment_init (&vc);
  140. if (ogg_stream_pagein (&os,&og) < 0) {
  141. fprintf (stderr,"Error reading first page of Ogg bitstream data.\n");
  142. exit (1);
  143. }
  144. if (ogg_stream_packetout(&os,&op) != 1) {
  145. fprintf (stderr,"Error reading initial header packet.\n");
  146. exit (1);
  147. }
  148. if (vorbis_synthesis_headerin (&vi,&vc,&op) < 0) {
  149. fprintf (stderr,"This Ogg bitstream does not contain Vorbis "
  150. "audio data.\n");
  151. exit (1);
  152. }
  153. i = 0;
  154. while ( i < 2) {
  155. while (i < 2) {
  156. int result = ogg_sync_pageout (&oy,&og);
  157. if(result == 0)
  158. break;
  159. if(result==1) {
  160. ogg_stream_pagein(&os,&og);
  161. while (i < 2) {
  162. result = ogg_stream_packetout (&os,&op);
  163. if (result == 0)
  164. goto done_decode;
  165. if (result < 0) {
  166. fprintf (stderr,"Corrupt secondary header. Exiting.\n");
  167. exit(1);
  168. }
  169. vorbis_synthesis_headerin (&vi,&vc,&op);
  170. i++;
  171. }
  172. }
  173. }
  174. buffer = ogg_sync_buffer (&oy,4096);
  175. bytes = fread (buffer,1,4096,file);
  176. if (bytes == 0 && i < 2) {
  177. fprintf (stderr,"End of file before finding all Vorbis headers!\n");
  178. exit (1);
  179. }
  180. ogg_sync_wrote (&oy,bytes);
  181. }
  182. if (vi.rate != srate) {
  183. printf ("\n\nError : File '%s' has sample rate of %ld when it should be %d.\n\n", filename, vi.rate, srate);
  184. exit (1) ;
  185. }
  186. if (vi.channels != 1) {
  187. printf ("\n\nError : File '%s' has %d channels, but should be mono.\n\n", filename, vi.channels);
  188. exit (1) ;
  189. }
  190. vorbis_synthesis_init (&vd,&vi);
  191. vorbis_block_init (&vd,&vb);
  192. while(!eos) {
  193. while (!eos) {
  194. int result = ogg_sync_pageout (&oy,&og);
  195. if (result == 0)
  196. break;
  197. if (result < 0) {
  198. fprintf (stderr,"Corrupt or missing data in bitstream; "
  199. "continuing...\n");
  200. } else {
  201. ogg_stream_pagein (&os,&og);
  202. while (1) {
  203. result = ogg_stream_packetout (&os,&op);
  204. if (result == 0)
  205. break;
  206. if (result < 0) {
  207. /* no reason to complain; already complained above */
  208. } else {
  209. float **pcm;
  210. int samples;
  211. if (vorbis_synthesis (&vb,&op) == 0)
  212. vorbis_synthesis_blockin(&vd,&vb);
  213. while ((samples = vorbis_synthesis_pcmout (&vd,&pcm)) > 0 && read_total < count) {
  214. int bout = samples < count ? samples : count;
  215. bout = read_total + bout > count ? count - read_total : bout;
  216. memcpy (data + read_total, pcm[0], bout * sizeof (float)) ;
  217. vorbis_synthesis_read (&vd,bout);
  218. read_total += bout ;
  219. }
  220. }
  221. }
  222. if (ogg_page_eos (&og)) eos = 1;
  223. }
  224. }
  225. if (!eos) {
  226. buffer = ogg_sync_buffer (&oy,4096);
  227. bytes = fread (buffer,1,4096,file);
  228. ogg_sync_wrote (&oy,bytes);
  229. if (bytes == 0) eos = 1;
  230. }
  231. }
  232. ogg_stream_clear (&os);
  233. vorbis_block_clear (&vb);
  234. vorbis_dsp_clear (&vd);
  235. vorbis_comment_clear (&vc);
  236. vorbis_info_clear (&vi);
  237. }
  238. done_decode:
  239. /* OK, clean up the framer */
  240. ogg_sync_clear (&oy);
  241. fclose (file) ;
  242. }