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.3 2000/01/05 03:10:25 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 */
27 #include "vorbis/codec.h"
29 int16_t convbuffer[4096]; /* take 8k out of the data segment, not the stack */
33 ogg_sync_state oy; /* sync and verify incoming physical bitstream */
34 ogg_stream_state os; /* take physical pages, weld into a logical
36 ogg_page og; /* one Ogg bitstream page. Vorbis packets are inside */
37 ogg_packet op; /* one raw packet of data for decode */
39 vorbis_info vi; /* struct that stores all the static vorbis bitstream
41 vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
42 vorbis_block vb; /* local working space for packet->PCM decode */
47 /********** Decode setup ************/
49 ogg_sync_init(&oy); /* Now we can read pages */
51 while(1){ /* we repeat if the bitstream is chained */
55 /* grab some data at the head of the stream. We want the first page
56 (which is guaranteed to be small and only contain the Vorbis
57 stream initial header) We need the first page to get the stream
60 /* submit a 4k block to libvorbis' Ogg layer */
61 buffer=ogg_sync_buffer(&oy,4096);
62 bytes=fread(buffer,1,4096,stdin);
63 ogg_sync_wrote(&oy,bytes);
65 /* Get the first page. */
66 if(ogg_sync_pageout(&oy,&og)!=1){
67 /* have we simply run out of data? If so, we're done. */
70 /* error case. Must not be Vorbis data */
71 fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
75 /* Get the serial number and set up the rest of decode. */
76 /* serialno first; use it to set up a logical stream */
77 ogg_stream_init(&os,ogg_page_serialno(&og));
79 /* extract the initial header from the first page and verify that the
80 Ogg bitstream is in fact Vorbis data */
82 /* I handle the initial header first instead of just having the code
83 read all three Vorbis headers at once because reading the initial
84 header is an easy way to identify a Vorbis bitstream and it's
85 useful to see that functionality seperated out. */
87 vorbis_info_init(&vi);
88 if(ogg_stream_pagein(&os,&og)<0){
89 /* error; stream version mismatch perhaps */
90 fprintf(stderr,"Error reading first page of Ogg bitstream data.\n");
94 if(ogg_stream_packetout(&os,&op)!=1){
95 /* no page? must not be vorbis */
96 fprintf(stderr,"Error reading initial header packet.\n");
100 if(vorbis_info_headerin(&vi,&op)<0){
101 /* error case; not a vorbis header */
102 fprintf(stderr,"This Ogg bitstream does not contain Vorbis "
107 /* At this point, we're sure we're Vorbis. We've set up the logical
108 (Ogg) bitstream decoder. Get the comment and codebook headers and
109 set up the Vorbis decoder */
111 /* The next two packets in order are the comment and codebook headers.
112 They're likely large and may span multiple pages. Thus we reead
113 and submit data until we get our two pacakets, watching that no
114 pages are missing. If a page is missing, error out; losing a
115 header page is the only place where missing data is fatal. */
120 int result=ogg_sync_pageout(&oy,&og);
121 if(result==0)break; /* Need more data */
122 /* Don't complain about missing or corrupt data yet. We'll
123 catch it at the packet output phase */
125 ogg_stream_pagein(&os,&og); /* we can ignore any errors here
126 as they'll also become apparent
129 result=ogg_stream_packetout(&os,&op);
132 /* Uh oh; data at some point was corrupted or missing!
133 We can't tolerate that in a header. Die. */
134 fprintf(stderr,"Corrupt secondary header. Exiting.\n");
137 vorbis_info_headerin(&vi,&op);
142 /* no harm in not checking before adding more */
143 buffer=ogg_sync_buffer(&oy,4096);
144 bytes=fread(buffer,1,4096,stdin);
146 fprintf(stderr,"End of file before finding all Vorbis headers!\n");
149 ogg_sync_wrote(&oy,bytes);
152 /* Throw the comments plus a few lines about the bitstream we're
155 char **ptr=vi.user_comments;
157 fprintf(stderr,"%s\n",*ptr);
160 fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi.channels,vi.rate);
161 fprintf(stderr,"Encoded by: %s\n\n",vi.vendor);
164 convsize=4096/vi.channels;
166 /* OK, got and parsed all three headers. Initialize the Vorbis
167 packet->PCM decoder. */
168 vorbis_synthesis_init(&vd,&vi); /* central decode state */
169 vorbis_block_init(&vd,&vb); /* local state for most of the decode
170 so multiple block decodes can
171 proceed in parallel. We could init
172 multiple vorbis_block structures
175 /* The rest is just a straight decode loop until end of stream */
178 int result=ogg_sync_pageout(&oy,&og);
179 if(result==0)break; /* need more data */
180 if(result==-1){ /* missing or corrupt data at this page position */
181 fprintf(stderr,"Corrupt or missing data in bitstream; "
184 ogg_stream_pagein(&os,&og); /* can safely ignore errors at
187 result=ogg_stream_packetout(&os,&op);
188 if(result==0)break; /* need more data */
189 if(result==-1){ /* missing or corrupt data at this page position */
190 /* no reason to complain; already complained above */
192 /* we have a packet. Decode it */
196 vorbis_synthesis(&vb,&op);
197 vorbis_synthesis_blockin(&vd,&vb);
200 **pcm is a multichannel double vector. In stereo, for
201 example, pcm[0] is left, and pcm[1] is right. samples is
202 the size of each channel. Convert the float values
203 (-1.<=range<=1.) to whatever PCM format and write it out */
205 while((samples=vorbis_synthesis_pcmout(&vd,&pcm))>0){
208 int out=(samples<convsize?samples:convsize);
210 /* convert doubles to 16 bit signed ints (host order) and
212 for(i=0;i<vi.channels;i++){
213 int16_t *ptr=convbuffer+i;
216 int val=mono[j]*32767.;
217 /* might as well guard against clipping */
232 fprintf(stderr,"Clipping in frame %ld\n",vd.sequence);
235 fwrite(convbuffer,2*vi.channels,out,stdout);
237 vorbis_synthesis_read(&vd,out); /* tell libvorbis how
243 if(ogg_page_eos(&og))eos=1;
247 buffer=ogg_sync_buffer(&oy,4096);
248 bytes=fread(buffer,1,4096,stdin);
249 ogg_sync_wrote(&oy,bytes);
254 /* clean up this logical bitstream; before exit we see if we're
255 followed by another [chained] */
257 ogg_stream_clear(&os);
259 /* ogg_page and ogg_packet structs always point to storage in
260 libvorbis. They're never freed or manipulated directly */
262 vorbis_dsp_clear(&vd);
263 vorbis_block_clear(&vb);
264 vorbis_info_clear(&vi); /* must be called last */
267 /* OK, clean up the framer */
270 fprintf(stderr,"Done.\n");