1 /********************************************************************
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. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2007 *
9 * by the Xiph.Org Foundation https://xiph.org/ *
11 ********************************************************************
13 function: utility functions for vorbis codec test suite.
15 ********************************************************************/
23 #include <vorbis/codec.h>
24 #include <vorbis/vorbisenc.h>
26 #include "write_read.h"
28 /* The following function is basically a hacked version of the code in
29 * examples/encoder_example.c */
31 write_vorbis_data_or_die (const char *filename, int srate, float q, const float * data, int count, int ch)
44 if ((file = fopen (filename, "wb")) == NULL) {
45 printf("\n\nError : fopen failed : %s\n", strerror (errno)) ;
49 /********** Encode setup ************/
51 vorbis_info_init (&vi);
53 ret = vorbis_encode_init_vbr (&vi,ch,srate,q);
55 printf ("vorbis_encode_init_vbr return %d\n", ret) ;
59 vorbis_comment_init (&vc);
60 vorbis_comment_add_tag (&vc,"ENCODER","test/util.c");
61 vorbis_analysis_init (&vd,&vi);
62 vorbis_block_init (&vd,&vb);
64 ogg_stream_init (&os,12345678);
68 ogg_packet header_comm;
69 ogg_packet header_code;
71 vorbis_analysis_headerout (&vd,&vc,&header,&header_comm,&header_code);
72 ogg_stream_packetin (&os,&header);
73 ogg_stream_packetin (&os,&header_comm);
74 ogg_stream_packetin (&os,&header_code);
76 /* Ensures the audio data will start on a new page. */
78 int result = ogg_stream_flush (&os,&og);
81 fwrite (og.header,1,og.header_len,file);
82 fwrite (og.body,1,og.body_len,file);
88 /* expose the buffer to submit data */
89 float **buffer = vorbis_analysis_buffer (&vd,count);
93 memcpy (buffer [i], data, count * sizeof (float)) ;
95 /* tell the library how much we actually submitted */
96 vorbis_analysis_wrote (&vd,count);
97 vorbis_analysis_wrote (&vd,0);
100 while (vorbis_analysis_blockout (&vd,&vb) == 1) {
101 vorbis_analysis (&vb,NULL);
102 vorbis_bitrate_addblock (&vb);
104 while (vorbis_bitrate_flushpacket (&vd,&op)) {
105 ogg_stream_packetin (&os,&op);
108 int result = ogg_stream_pageout (&os,&og);
111 fwrite (og.header,1,og.header_len,file);
112 fwrite (og.body,1,og.body_len,file);
114 if (ogg_page_eos (&og))
120 ogg_stream_clear (&os);
121 vorbis_block_clear (&vb);
122 vorbis_dsp_clear (&vd);
123 vorbis_comment_clear (&vc);
124 vorbis_info_clear (&vi);
129 /* The following function is basically a hacked version of the code in
130 * examples/decoder_example.c */
132 read_vorbis_data_or_die (const char *filename, int srate, float * data, int count)
151 if ((file = fopen (filename, "rb")) == NULL) {
152 printf("\n\nError : fopen failed : %s\n", strerror (errno)) ;
159 /* fragile! Assumes all of our headers will fit in the first 8kB,
160 which currently they will */
161 buffer = ogg_sync_buffer (&oy,8192);
162 bytes = fread (buffer,1,8192,file);
163 ogg_sync_wrote (&oy,bytes);
165 if(ogg_sync_pageout (&oy,&og) != 1) {
167 printf ("Out of data.\n") ;
171 fprintf (stderr,"Input does not appear to be an Ogg bitstream.\n");
175 ogg_stream_init (&os,ogg_page_serialno(&og));
177 vorbis_info_init (&vi);
178 vorbis_comment_init (&vc);
179 if (ogg_stream_pagein (&os,&og) < 0) {
180 fprintf (stderr,"Error reading first page of Ogg bitstream data.\n");
184 if (ogg_stream_packetout(&os,&op) != 1) {
185 fprintf (stderr,"Error reading initial header packet.\n");
189 if (vorbis_synthesis_headerin (&vi,&vc,&op) < 0) {
190 fprintf (stderr,"This Ogg bitstream does not contain Vorbis "
199 int result = ogg_sync_pageout (&oy,&og);
203 ogg_stream_pagein(&os,&og);
206 result = ogg_stream_packetout (&os,&op);
207 if (result == 0) break;
209 fprintf (stderr,"Corrupt secondary header. Exiting.\n");
212 vorbis_synthesis_headerin (&vi,&vc,&op);
218 buffer = ogg_sync_buffer (&oy,4096);
219 bytes = fread (buffer,1,4096,file);
220 if (bytes == 0 && i < 2) {
221 fprintf (stderr,"End of file before finding all Vorbis headers!\n");
225 ogg_sync_wrote (&oy,bytes);
228 if (vi.rate != srate) {
229 printf ("\n\nError : File '%s' has sample rate of %ld when it should be %d.\n\n", filename, vi.rate, srate);
233 vorbis_synthesis_init (&vd,&vi);
234 vorbis_block_init (&vd,&vb);
238 int result = ogg_sync_pageout (&oy,&og);
242 fprintf (stderr,"Corrupt or missing data in bitstream; "
245 ogg_stream_pagein (&os,&og);
247 result = ogg_stream_packetout (&os,&op);
252 /* no reason to complain; already complained above */
257 if (vorbis_synthesis (&vb,&op) == 0)
258 vorbis_synthesis_blockin(&vd,&vb);
259 while ((samples = vorbis_synthesis_pcmout (&vd,&pcm)) > 0 && read_total < count) {
260 int bout = samples < count ? samples : count;
261 bout = read_total + bout > count ? count - read_total : bout;
263 memcpy (data + read_total, pcm[0], bout * sizeof (float)) ;
265 vorbis_synthesis_read (&vd,bout);
271 if (ogg_page_eos (&og)) eos = 1;
276 buffer = ogg_sync_buffer (&oy,4096);
277 bytes = fread (buffer,1,4096,file);
278 ogg_sync_wrote (&oy,bytes);
279 if (bytes == 0) eos = 1;
283 ogg_stream_clear (&os);
285 vorbis_block_clear (&vb);
286 vorbis_dsp_clear (&vd);
287 vorbis_comment_clear (&vc);
288 vorbis_info_clear (&vi);
292 /* OK, clean up the framer */
293 ogg_sync_clear (&oy);