Chris@1
|
1 /********************************************************************
|
Chris@1
|
2 * *
|
Chris@1
|
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
|
Chris@1
|
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
|
Chris@1
|
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
|
Chris@1
|
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
|
Chris@1
|
7 * *
|
Chris@1
|
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 *
|
Chris@1
|
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
|
Chris@1
|
10 * *
|
Chris@1
|
11 ********************************************************************
|
Chris@1
|
12
|
Chris@1
|
13 function: simple example decoder
|
Chris@1
|
14 last mod: $Id: decoder_example.c 16243 2009-07-10 02:49:31Z xiphmont $
|
Chris@1
|
15
|
Chris@1
|
16 ********************************************************************/
|
Chris@1
|
17
|
Chris@1
|
18 /* Takes a vorbis bitstream from stdin and writes raw stereo PCM to
|
Chris@1
|
19 stdout. Decodes simple and chained OggVorbis files from beginning
|
Chris@1
|
20 to end. Vorbisfile.a is somewhat more complex than the code below. */
|
Chris@1
|
21
|
Chris@1
|
22 /* Note that this is POSIX, not ANSI code */
|
Chris@1
|
23
|
Chris@1
|
24 #include <stdio.h>
|
Chris@1
|
25 #include <stdlib.h>
|
Chris@1
|
26 #include <math.h>
|
Chris@1
|
27 #include <vorbis/codec.h>
|
Chris@1
|
28
|
Chris@1
|
29 #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
|
Chris@1
|
30 #include <io.h>
|
Chris@1
|
31 #include <fcntl.h>
|
Chris@1
|
32 #endif
|
Chris@1
|
33
|
Chris@1
|
34 #if defined(__MACOS__) && defined(__MWERKS__)
|
Chris@1
|
35 #include <console.h> /* CodeWarrior's Mac "command-line" support */
|
Chris@1
|
36 #endif
|
Chris@1
|
37
|
Chris@1
|
38 ogg_int16_t convbuffer[4096]; /* take 8k out of the data segment, not the stack */
|
Chris@1
|
39 int convsize=4096;
|
Chris@1
|
40
|
Chris@1
|
41 extern void _VDBG_dump(void);
|
Chris@1
|
42
|
Chris@1
|
43 int main(){
|
Chris@1
|
44 ogg_sync_state oy; /* sync and verify incoming physical bitstream */
|
Chris@1
|
45 ogg_stream_state os; /* take physical pages, weld into a logical
|
Chris@1
|
46 stream of packets */
|
Chris@1
|
47 ogg_page og; /* one Ogg bitstream page. Vorbis packets are inside */
|
Chris@1
|
48 ogg_packet op; /* one raw packet of data for decode */
|
Chris@1
|
49
|
Chris@1
|
50 vorbis_info vi; /* struct that stores all the static vorbis bitstream
|
Chris@1
|
51 settings */
|
Chris@1
|
52 vorbis_comment vc; /* struct that stores all the bitstream user comments */
|
Chris@1
|
53 vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
|
Chris@1
|
54 vorbis_block vb; /* local working space for packet->PCM decode */
|
Chris@1
|
55
|
Chris@1
|
56 char *buffer;
|
Chris@1
|
57 int bytes;
|
Chris@1
|
58
|
Chris@1
|
59 #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
|
Chris@1
|
60 /* Beware the evil ifdef. We avoid these where we can, but this one we
|
Chris@1
|
61 cannot. Don't add any more, you'll probably go to hell if you do. */
|
Chris@1
|
62 _setmode( _fileno( stdin ), _O_BINARY );
|
Chris@1
|
63 _setmode( _fileno( stdout ), _O_BINARY );
|
Chris@1
|
64 #endif
|
Chris@1
|
65
|
Chris@1
|
66 #if defined(macintosh) && defined(__MWERKS__)
|
Chris@1
|
67 {
|
Chris@1
|
68 int argc;
|
Chris@1
|
69 char **argv;
|
Chris@1
|
70 argc=ccommand(&argv); /* get a "command line" from the Mac user */
|
Chris@1
|
71 /* this also lets the user set stdin and stdout */
|
Chris@1
|
72 }
|
Chris@1
|
73 #endif
|
Chris@1
|
74
|
Chris@1
|
75 /********** Decode setup ************/
|
Chris@1
|
76
|
Chris@1
|
77 ogg_sync_init(&oy); /* Now we can read pages */
|
Chris@1
|
78
|
Chris@1
|
79 while(1){ /* we repeat if the bitstream is chained */
|
Chris@1
|
80 int eos=0;
|
Chris@1
|
81 int i;
|
Chris@1
|
82
|
Chris@1
|
83 /* grab some data at the head of the stream. We want the first page
|
Chris@1
|
84 (which is guaranteed to be small and only contain the Vorbis
|
Chris@1
|
85 stream initial header) We need the first page to get the stream
|
Chris@1
|
86 serialno. */
|
Chris@1
|
87
|
Chris@1
|
88 /* submit a 4k block to libvorbis' Ogg layer */
|
Chris@1
|
89 buffer=ogg_sync_buffer(&oy,4096);
|
Chris@1
|
90 bytes=fread(buffer,1,4096,stdin);
|
Chris@1
|
91 ogg_sync_wrote(&oy,bytes);
|
Chris@1
|
92
|
Chris@1
|
93 /* Get the first page. */
|
Chris@1
|
94 if(ogg_sync_pageout(&oy,&og)!=1){
|
Chris@1
|
95 /* have we simply run out of data? If so, we're done. */
|
Chris@1
|
96 if(bytes<4096)break;
|
Chris@1
|
97
|
Chris@1
|
98 /* error case. Must not be Vorbis data */
|
Chris@1
|
99 fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
|
Chris@1
|
100 exit(1);
|
Chris@1
|
101 }
|
Chris@1
|
102
|
Chris@1
|
103 /* Get the serial number and set up the rest of decode. */
|
Chris@1
|
104 /* serialno first; use it to set up a logical stream */
|
Chris@1
|
105 ogg_stream_init(&os,ogg_page_serialno(&og));
|
Chris@1
|
106
|
Chris@1
|
107 /* extract the initial header from the first page and verify that the
|
Chris@1
|
108 Ogg bitstream is in fact Vorbis data */
|
Chris@1
|
109
|
Chris@1
|
110 /* I handle the initial header first instead of just having the code
|
Chris@1
|
111 read all three Vorbis headers at once because reading the initial
|
Chris@1
|
112 header is an easy way to identify a Vorbis bitstream and it's
|
Chris@1
|
113 useful to see that functionality seperated out. */
|
Chris@1
|
114
|
Chris@1
|
115 vorbis_info_init(&vi);
|
Chris@1
|
116 vorbis_comment_init(&vc);
|
Chris@1
|
117 if(ogg_stream_pagein(&os,&og)<0){
|
Chris@1
|
118 /* error; stream version mismatch perhaps */
|
Chris@1
|
119 fprintf(stderr,"Error reading first page of Ogg bitstream data.\n");
|
Chris@1
|
120 exit(1);
|
Chris@1
|
121 }
|
Chris@1
|
122
|
Chris@1
|
123 if(ogg_stream_packetout(&os,&op)!=1){
|
Chris@1
|
124 /* no page? must not be vorbis */
|
Chris@1
|
125 fprintf(stderr,"Error reading initial header packet.\n");
|
Chris@1
|
126 exit(1);
|
Chris@1
|
127 }
|
Chris@1
|
128
|
Chris@1
|
129 if(vorbis_synthesis_headerin(&vi,&vc,&op)<0){
|
Chris@1
|
130 /* error case; not a vorbis header */
|
Chris@1
|
131 fprintf(stderr,"This Ogg bitstream does not contain Vorbis "
|
Chris@1
|
132 "audio data.\n");
|
Chris@1
|
133 exit(1);
|
Chris@1
|
134 }
|
Chris@1
|
135
|
Chris@1
|
136 /* At this point, we're sure we're Vorbis. We've set up the logical
|
Chris@1
|
137 (Ogg) bitstream decoder. Get the comment and codebook headers and
|
Chris@1
|
138 set up the Vorbis decoder */
|
Chris@1
|
139
|
Chris@1
|
140 /* The next two packets in order are the comment and codebook headers.
|
Chris@1
|
141 They're likely large and may span multiple pages. Thus we read
|
Chris@1
|
142 and submit data until we get our two packets, watching that no
|
Chris@1
|
143 pages are missing. If a page is missing, error out; losing a
|
Chris@1
|
144 header page is the only place where missing data is fatal. */
|
Chris@1
|
145
|
Chris@1
|
146 i=0;
|
Chris@1
|
147 while(i<2){
|
Chris@1
|
148 while(i<2){
|
Chris@1
|
149 int result=ogg_sync_pageout(&oy,&og);
|
Chris@1
|
150 if(result==0)break; /* Need more data */
|
Chris@1
|
151 /* Don't complain about missing or corrupt data yet. We'll
|
Chris@1
|
152 catch it at the packet output phase */
|
Chris@1
|
153 if(result==1){
|
Chris@1
|
154 ogg_stream_pagein(&os,&og); /* we can ignore any errors here
|
Chris@1
|
155 as they'll also become apparent
|
Chris@1
|
156 at packetout */
|
Chris@1
|
157 while(i<2){
|
Chris@1
|
158 result=ogg_stream_packetout(&os,&op);
|
Chris@1
|
159 if(result==0)break;
|
Chris@1
|
160 if(result<0){
|
Chris@1
|
161 /* Uh oh; data at some point was corrupted or missing!
|
Chris@1
|
162 We can't tolerate that in a header. Die. */
|
Chris@1
|
163 fprintf(stderr,"Corrupt secondary header. Exiting.\n");
|
Chris@1
|
164 exit(1);
|
Chris@1
|
165 }
|
Chris@1
|
166 result=vorbis_synthesis_headerin(&vi,&vc,&op);
|
Chris@1
|
167 if(result<0){
|
Chris@1
|
168 fprintf(stderr,"Corrupt secondary header. Exiting.\n");
|
Chris@1
|
169 exit(1);
|
Chris@1
|
170 }
|
Chris@1
|
171 i++;
|
Chris@1
|
172 }
|
Chris@1
|
173 }
|
Chris@1
|
174 }
|
Chris@1
|
175 /* no harm in not checking before adding more */
|
Chris@1
|
176 buffer=ogg_sync_buffer(&oy,4096);
|
Chris@1
|
177 bytes=fread(buffer,1,4096,stdin);
|
Chris@1
|
178 if(bytes==0 && i<2){
|
Chris@1
|
179 fprintf(stderr,"End of file before finding all Vorbis headers!\n");
|
Chris@1
|
180 exit(1);
|
Chris@1
|
181 }
|
Chris@1
|
182 ogg_sync_wrote(&oy,bytes);
|
Chris@1
|
183 }
|
Chris@1
|
184
|
Chris@1
|
185 /* Throw the comments plus a few lines about the bitstream we're
|
Chris@1
|
186 decoding */
|
Chris@1
|
187 {
|
Chris@1
|
188 char **ptr=vc.user_comments;
|
Chris@1
|
189 while(*ptr){
|
Chris@1
|
190 fprintf(stderr,"%s\n",*ptr);
|
Chris@1
|
191 ++ptr;
|
Chris@1
|
192 }
|
Chris@1
|
193 fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi.channels,vi.rate);
|
Chris@1
|
194 fprintf(stderr,"Encoded by: %s\n\n",vc.vendor);
|
Chris@1
|
195 }
|
Chris@1
|
196
|
Chris@1
|
197 convsize=4096/vi.channels;
|
Chris@1
|
198
|
Chris@1
|
199 /* OK, got and parsed all three headers. Initialize the Vorbis
|
Chris@1
|
200 packet->PCM decoder. */
|
Chris@1
|
201 if(vorbis_synthesis_init(&vd,&vi)==0){ /* central decode state */
|
Chris@1
|
202 vorbis_block_init(&vd,&vb); /* local state for most of the decode
|
Chris@1
|
203 so multiple block decodes can
|
Chris@1
|
204 proceed in parallel. We could init
|
Chris@1
|
205 multiple vorbis_block structures
|
Chris@1
|
206 for vd here */
|
Chris@1
|
207
|
Chris@1
|
208 /* The rest is just a straight decode loop until end of stream */
|
Chris@1
|
209 while(!eos){
|
Chris@1
|
210 while(!eos){
|
Chris@1
|
211 int result=ogg_sync_pageout(&oy,&og);
|
Chris@1
|
212 if(result==0)break; /* need more data */
|
Chris@1
|
213 if(result<0){ /* missing or corrupt data at this page position */
|
Chris@1
|
214 fprintf(stderr,"Corrupt or missing data in bitstream; "
|
Chris@1
|
215 "continuing...\n");
|
Chris@1
|
216 }else{
|
Chris@1
|
217 ogg_stream_pagein(&os,&og); /* can safely ignore errors at
|
Chris@1
|
218 this point */
|
Chris@1
|
219 while(1){
|
Chris@1
|
220 result=ogg_stream_packetout(&os,&op);
|
Chris@1
|
221
|
Chris@1
|
222 if(result==0)break; /* need more data */
|
Chris@1
|
223 if(result<0){ /* missing or corrupt data at this page position */
|
Chris@1
|
224 /* no reason to complain; already complained above */
|
Chris@1
|
225 }else{
|
Chris@1
|
226 /* we have a packet. Decode it */
|
Chris@1
|
227 float **pcm;
|
Chris@1
|
228 int samples;
|
Chris@1
|
229
|
Chris@1
|
230 if(vorbis_synthesis(&vb,&op)==0) /* test for success! */
|
Chris@1
|
231 vorbis_synthesis_blockin(&vd,&vb);
|
Chris@1
|
232 /*
|
Chris@1
|
233
|
Chris@1
|
234 **pcm is a multichannel float vector. In stereo, for
|
Chris@1
|
235 example, pcm[0] is left, and pcm[1] is right. samples is
|
Chris@1
|
236 the size of each channel. Convert the float values
|
Chris@1
|
237 (-1.<=range<=1.) to whatever PCM format and write it out */
|
Chris@1
|
238
|
Chris@1
|
239 while((samples=vorbis_synthesis_pcmout(&vd,&pcm))>0){
|
Chris@1
|
240 int j;
|
Chris@1
|
241 int clipflag=0;
|
Chris@1
|
242 int bout=(samples<convsize?samples:convsize);
|
Chris@1
|
243
|
Chris@1
|
244 /* convert floats to 16 bit signed ints (host order) and
|
Chris@1
|
245 interleave */
|
Chris@1
|
246 for(i=0;i<vi.channels;i++){
|
Chris@1
|
247 ogg_int16_t *ptr=convbuffer+i;
|
Chris@1
|
248 float *mono=pcm[i];
|
Chris@1
|
249 for(j=0;j<bout;j++){
|
Chris@1
|
250 #if 1
|
Chris@1
|
251 int val=floor(mono[j]*32767.f+.5f);
|
Chris@1
|
252 #else /* optional dither */
|
Chris@1
|
253 int val=mono[j]*32767.f+drand48()-0.5f;
|
Chris@1
|
254 #endif
|
Chris@1
|
255 /* might as well guard against clipping */
|
Chris@1
|
256 if(val>32767){
|
Chris@1
|
257 val=32767;
|
Chris@1
|
258 clipflag=1;
|
Chris@1
|
259 }
|
Chris@1
|
260 if(val<-32768){
|
Chris@1
|
261 val=-32768;
|
Chris@1
|
262 clipflag=1;
|
Chris@1
|
263 }
|
Chris@1
|
264 *ptr=val;
|
Chris@1
|
265 ptr+=vi.channels;
|
Chris@1
|
266 }
|
Chris@1
|
267 }
|
Chris@1
|
268
|
Chris@1
|
269 if(clipflag)
|
Chris@1
|
270 fprintf(stderr,"Clipping in frame %ld\n",(long)(vd.sequence));
|
Chris@1
|
271
|
Chris@1
|
272
|
Chris@1
|
273 fwrite(convbuffer,2*vi.channels,bout,stdout);
|
Chris@1
|
274
|
Chris@1
|
275 vorbis_synthesis_read(&vd,bout); /* tell libvorbis how
|
Chris@1
|
276 many samples we
|
Chris@1
|
277 actually consumed */
|
Chris@1
|
278 }
|
Chris@1
|
279 }
|
Chris@1
|
280 }
|
Chris@1
|
281 if(ogg_page_eos(&og))eos=1;
|
Chris@1
|
282 }
|
Chris@1
|
283 }
|
Chris@1
|
284 if(!eos){
|
Chris@1
|
285 buffer=ogg_sync_buffer(&oy,4096);
|
Chris@1
|
286 bytes=fread(buffer,1,4096,stdin);
|
Chris@1
|
287 ogg_sync_wrote(&oy,bytes);
|
Chris@1
|
288 if(bytes==0)eos=1;
|
Chris@1
|
289 }
|
Chris@1
|
290 }
|
Chris@1
|
291
|
Chris@1
|
292 /* ogg_page and ogg_packet structs always point to storage in
|
Chris@1
|
293 libvorbis. They're never freed or manipulated directly */
|
Chris@1
|
294
|
Chris@1
|
295 vorbis_block_clear(&vb);
|
Chris@1
|
296 vorbis_dsp_clear(&vd);
|
Chris@1
|
297 }else{
|
Chris@1
|
298 fprintf(stderr,"Error: Corrupt header during playback initialization.\n");
|
Chris@1
|
299 }
|
Chris@1
|
300
|
Chris@1
|
301 /* clean up this logical bitstream; before exit we see if we're
|
Chris@1
|
302 followed by another [chained] */
|
Chris@1
|
303
|
Chris@1
|
304 ogg_stream_clear(&os);
|
Chris@1
|
305 vorbis_comment_clear(&vc);
|
Chris@1
|
306 vorbis_info_clear(&vi); /* must be called last */
|
Chris@1
|
307 }
|
Chris@1
|
308
|
Chris@1
|
309 /* OK, clean up the framer */
|
Chris@1
|
310 ogg_sync_clear(&oy);
|
Chris@1
|
311
|
Chris@1
|
312 fprintf(stderr,"Done.\n");
|
Chris@1
|
313 return(0);
|
Chris@1
|
314 }
|