1 /********************************************************************
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. *
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/ *
12 ********************************************************************
14 function: simple example decoder
15 last mod: $Id: decoder_example.c,v 1.10 2000/08/14 22:33:50 xiphmont Exp $
17 ********************************************************************/
19 /* Takes a vorbis bitstream from stdin and writes raw stereo PCM to
20 stdout. Decodes simple and chained OggVorbis files from beginning
21 to end. Vorbisfile.a is somewhat more complex than the code below. */
23 /* Note that this is POSIX, not ANSI code */
28 #include "vorbis/codec.h"
30 #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
35 #if defined(macintosh) && defined(__MWERKS__)
36 #include <console.h> /* CodeWarrior's Mac "command-line" support */
39 int16_t convbuffer[4096]; /* take 8k out of the data segment, not the stack */
42 int main(int argc, char **argv){
43 ogg_sync_state oy; /* sync and verify incoming physical bitstream */
44 ogg_stream_state os; /* take physical pages, weld into a logical
46 ogg_page og; /* one Ogg bitstream page. Vorbis packets are inside */
47 ogg_packet op; /* one raw packet of data for decode */
49 vorbis_info vi; /* struct that stores all the static vorbis bitstream
51 vorbis_comment vc; /* struct that stores all the bitstream user comments */
52 vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
53 vorbis_block vb; /* local working space for packet->PCM decode */
58 #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
59 /* Beware the evil ifdef. We avoid these where we can, but this one we
60 cannot. Don't add any more, you'll probably go to hell if you do. */
61 _setmode( _fileno( stdin ), _O_BINARY );
62 _setmode( _fileno( stdout ), _O_BINARY );
65 #if defined(macintosh) && defined(__MWERKS__)
69 argc = ccommand(&argv); /* get a "command line" from the Mac user */
70 /* this also lets the user set stdin and stdout */
73 /********** Decode setup ************/
75 ogg_sync_init(&oy); /* Now we can read pages */
77 while(1){ /* we repeat if the bitstream is chained */
81 /* grab some data at the head of the stream. We want the first page
82 (which is guaranteed to be small and only contain the Vorbis
83 stream initial header) We need the first page to get the stream
86 /* submit a 4k block to libvorbis' Ogg layer */
87 buffer=ogg_sync_buffer(&oy,4096);
88 bytes=fread(buffer,1,4096,stdin);
89 ogg_sync_wrote(&oy,bytes);
91 /* Get the first page. */
92 if(ogg_sync_pageout(&oy,&og)!=1){
93 /* have we simply run out of data? If so, we're done. */
96 /* error case. Must not be Vorbis data */
97 fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
101 /* Get the serial number and set up the rest of decode. */
102 /* serialno first; use it to set up a logical stream */
103 ogg_stream_init(&os,ogg_page_serialno(&og));
105 /* extract the initial header from the first page and verify that the
106 Ogg bitstream is in fact Vorbis data */
108 /* I handle the initial header first instead of just having the code
109 read all three Vorbis headers at once because reading the initial
110 header is an easy way to identify a Vorbis bitstream and it's
111 useful to see that functionality seperated out. */
113 vorbis_info_init(&vi);
114 vorbis_comment_init(&vc);
115 if(ogg_stream_pagein(&os,&og)<0){
116 /* error; stream version mismatch perhaps */
117 fprintf(stderr,"Error reading first page of Ogg bitstream data.\n");
121 if(ogg_stream_packetout(&os,&op)!=1){
122 /* no page? must not be vorbis */
123 fprintf(stderr,"Error reading initial header packet.\n");
127 if(vorbis_synthesis_headerin(&vi,&vc,&op)<0){
128 /* error case; not a vorbis header */
129 fprintf(stderr,"This Ogg bitstream does not contain Vorbis "
134 /* At this point, we're sure we're Vorbis. We've set up the logical
135 (Ogg) bitstream decoder. Get the comment and codebook headers and
136 set up the Vorbis decoder */
138 /* The next two packets in order are the comment and codebook headers.
139 They're likely large and may span multiple pages. Thus we reead
140 and submit data until we get our two pacakets, watching that no
141 pages are missing. If a page is missing, error out; losing a
142 header page is the only place where missing data is fatal. */
147 int result=ogg_sync_pageout(&oy,&og);
148 if(result==0)break; /* Need more data */
149 /* Don't complain about missing or corrupt data yet. We'll
150 catch it at the packet output phase */
152 ogg_stream_pagein(&os,&og); /* we can ignore any errors here
153 as they'll also become apparent
156 result=ogg_stream_packetout(&os,&op);
159 /* Uh oh; data at some point was corrupted or missing!
160 We can't tolerate that in a header. Die. */
161 fprintf(stderr,"Corrupt secondary header. Exiting.\n");
164 vorbis_synthesis_headerin(&vi,&vc,&op);
169 /* no harm in not checking before adding more */
170 buffer=ogg_sync_buffer(&oy,4096);
171 bytes=fread(buffer,1,4096,stdin);
173 fprintf(stderr,"End of file before finding all Vorbis headers!\n");
176 ogg_sync_wrote(&oy,bytes);
179 /* Throw the comments plus a few lines about the bitstream we're
182 char **ptr=vc.user_comments;
184 fprintf(stderr,"%s\n",*ptr);
187 fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi.channels,vi.rate);
188 fprintf(stderr,"Encoded by: %s\n\n",vc.vendor);
191 convsize=4096/vi.channels;
193 /* OK, got and parsed all three headers. Initialize the Vorbis
194 packet->PCM decoder. */
195 vorbis_synthesis_init(&vd,&vi); /* central decode state */
196 vorbis_block_init(&vd,&vb); /* local state for most of the decode
197 so multiple block decodes can
198 proceed in parallel. We could init
199 multiple vorbis_block structures
202 /* The rest is just a straight decode loop until end of stream */
205 int result=ogg_sync_pageout(&oy,&og);
206 if(result==0)break; /* need more data */
207 if(result==-1){ /* missing or corrupt data at this page position */
208 fprintf(stderr,"Corrupt or missing data in bitstream; "
211 ogg_stream_pagein(&os,&og); /* can safely ignore errors at
214 result=ogg_stream_packetout(&os,&op);
216 if(result==0)break; /* need more data */
217 if(result==-1){ /* missing or corrupt data at this page position */
218 /* no reason to complain; already complained above */
220 /* we have a packet. Decode it */
224 if(vorbis_synthesis(&vb,&op)==0) /* test for success! */
225 vorbis_synthesis_blockin(&vd,&vb);
228 **pcm is a multichannel double vector. In stereo, for
229 example, pcm[0] is left, and pcm[1] is right. samples is
230 the size of each channel. Convert the float values
231 (-1.<=range<=1.) to whatever PCM format and write it out */
233 while((samples=vorbis_synthesis_pcmout(&vd,&pcm))>0){
236 int bout=(samples<convsize?samples:convsize);
238 /* convert doubles to 16 bit signed ints (host order) and
240 for(i=0;i<vi.channels;i++){
241 int16_t *ptr=convbuffer+i;
244 int val=mono[j]*32767.;
245 /* might as well guard against clipping */
260 fprintf(stderr,"Clipping in frame %ld\n",(long)(vd.sequence));
263 fwrite(convbuffer,2*vi.channels,bout,stdout);
265 vorbis_synthesis_read(&vd,bout); /* tell libvorbis how
271 if(ogg_page_eos(&og))eos=1;
275 buffer=ogg_sync_buffer(&oy,4096);
276 bytes=fread(buffer,1,4096,stdin);
277 ogg_sync_wrote(&oy,bytes);
282 /* clean up this logical bitstream; before exit we see if we're
283 followed by another [chained] */
285 ogg_stream_clear(&os);
287 /* ogg_page and ogg_packet structs always point to storage in
288 libvorbis. They're never freed or manipulated directly */
290 vorbis_block_clear(&vb);
291 vorbis_dsp_clear(&vd);
292 vorbis_info_clear(&vi); /* must be called last */
295 /* OK, clean up the framer */
298 fprintf(stderr,"Done.\n");