cannam@86: /******************************************************************** cannam@86: * * cannam@86: * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. * cannam@86: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * cannam@86: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * cannam@86: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * cannam@86: * * cannam@86: * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 * cannam@86: * by the Xiph.Org Foundation http://www.xiph.org/ * cannam@86: * * cannam@86: ******************************************************************** cannam@86: cannam@86: function: simple example decoder cannam@86: last mod: $Id: decoder_example.c 16243 2009-07-10 02:49:31Z xiphmont $ cannam@86: cannam@86: ********************************************************************/ cannam@86: cannam@86: /* Takes a vorbis bitstream from stdin and writes raw stereo PCM to cannam@86: stdout. Decodes simple and chained OggVorbis files from beginning cannam@86: to end. Vorbisfile.a is somewhat more complex than the code below. */ cannam@86: cannam@86: /* Note that this is POSIX, not ANSI code */ cannam@86: cannam@86: #include cannam@86: #include cannam@86: #include cannam@86: #include cannam@86: cannam@86: #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */ cannam@86: #include cannam@86: #include cannam@86: #endif cannam@86: cannam@86: #if defined(__MACOS__) && defined(__MWERKS__) cannam@86: #include /* CodeWarrior's Mac "command-line" support */ cannam@86: #endif cannam@86: cannam@86: ogg_int16_t convbuffer[4096]; /* take 8k out of the data segment, not the stack */ cannam@86: int convsize=4096; cannam@86: cannam@86: extern void _VDBG_dump(void); cannam@86: cannam@86: int main(){ cannam@86: ogg_sync_state oy; /* sync and verify incoming physical bitstream */ cannam@86: ogg_stream_state os; /* take physical pages, weld into a logical cannam@86: stream of packets */ cannam@86: ogg_page og; /* one Ogg bitstream page. Vorbis packets are inside */ cannam@86: ogg_packet op; /* one raw packet of data for decode */ cannam@86: cannam@86: vorbis_info vi; /* struct that stores all the static vorbis bitstream cannam@86: settings */ cannam@86: vorbis_comment vc; /* struct that stores all the bitstream user comments */ cannam@86: vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */ cannam@86: vorbis_block vb; /* local working space for packet->PCM decode */ cannam@86: cannam@86: char *buffer; cannam@86: int bytes; cannam@86: cannam@86: #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */ cannam@86: /* Beware the evil ifdef. We avoid these where we can, but this one we cannam@86: cannot. Don't add any more, you'll probably go to hell if you do. */ cannam@86: _setmode( _fileno( stdin ), _O_BINARY ); cannam@86: _setmode( _fileno( stdout ), _O_BINARY ); cannam@86: #endif cannam@86: cannam@86: #if defined(macintosh) && defined(__MWERKS__) cannam@86: { cannam@86: int argc; cannam@86: char **argv; cannam@86: argc=ccommand(&argv); /* get a "command line" from the Mac user */ cannam@86: /* this also lets the user set stdin and stdout */ cannam@86: } cannam@86: #endif cannam@86: cannam@86: /********** Decode setup ************/ cannam@86: cannam@86: ogg_sync_init(&oy); /* Now we can read pages */ cannam@86: cannam@86: while(1){ /* we repeat if the bitstream is chained */ cannam@86: int eos=0; cannam@86: int i; cannam@86: cannam@86: /* grab some data at the head of the stream. We want the first page cannam@86: (which is guaranteed to be small and only contain the Vorbis cannam@86: stream initial header) We need the first page to get the stream cannam@86: serialno. */ cannam@86: cannam@86: /* submit a 4k block to libvorbis' Ogg layer */ cannam@86: buffer=ogg_sync_buffer(&oy,4096); cannam@86: bytes=fread(buffer,1,4096,stdin); cannam@86: ogg_sync_wrote(&oy,bytes); cannam@86: cannam@86: /* Get the first page. */ cannam@86: if(ogg_sync_pageout(&oy,&og)!=1){ cannam@86: /* have we simply run out of data? If so, we're done. */ cannam@86: if(bytes<4096)break; cannam@86: cannam@86: /* error case. Must not be Vorbis data */ cannam@86: fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n"); cannam@86: exit(1); cannam@86: } cannam@86: cannam@86: /* Get the serial number and set up the rest of decode. */ cannam@86: /* serialno first; use it to set up a logical stream */ cannam@86: ogg_stream_init(&os,ogg_page_serialno(&og)); cannam@86: cannam@86: /* extract the initial header from the first page and verify that the cannam@86: Ogg bitstream is in fact Vorbis data */ cannam@86: cannam@86: /* I handle the initial header first instead of just having the code cannam@86: read all three Vorbis headers at once because reading the initial cannam@86: header is an easy way to identify a Vorbis bitstream and it's cannam@86: useful to see that functionality seperated out. */ cannam@86: cannam@86: vorbis_info_init(&vi); cannam@86: vorbis_comment_init(&vc); cannam@86: if(ogg_stream_pagein(&os,&og)<0){ cannam@86: /* error; stream version mismatch perhaps */ cannam@86: fprintf(stderr,"Error reading first page of Ogg bitstream data.\n"); cannam@86: exit(1); cannam@86: } cannam@86: cannam@86: if(ogg_stream_packetout(&os,&op)!=1){ cannam@86: /* no page? must not be vorbis */ cannam@86: fprintf(stderr,"Error reading initial header packet.\n"); cannam@86: exit(1); cannam@86: } cannam@86: cannam@86: if(vorbis_synthesis_headerin(&vi,&vc,&op)<0){ cannam@86: /* error case; not a vorbis header */ cannam@86: fprintf(stderr,"This Ogg bitstream does not contain Vorbis " cannam@86: "audio data.\n"); cannam@86: exit(1); cannam@86: } cannam@86: cannam@86: /* At this point, we're sure we're Vorbis. We've set up the logical cannam@86: (Ogg) bitstream decoder. Get the comment and codebook headers and cannam@86: set up the Vorbis decoder */ cannam@86: cannam@86: /* The next two packets in order are the comment and codebook headers. cannam@86: They're likely large and may span multiple pages. Thus we read cannam@86: and submit data until we get our two packets, watching that no cannam@86: pages are missing. If a page is missing, error out; losing a cannam@86: header page is the only place where missing data is fatal. */ cannam@86: cannam@86: i=0; cannam@86: while(i<2){ cannam@86: while(i<2){ cannam@86: int result=ogg_sync_pageout(&oy,&og); cannam@86: if(result==0)break; /* Need more data */ cannam@86: /* Don't complain about missing or corrupt data yet. We'll cannam@86: catch it at the packet output phase */ cannam@86: if(result==1){ cannam@86: ogg_stream_pagein(&os,&og); /* we can ignore any errors here cannam@86: as they'll also become apparent cannam@86: at packetout */ cannam@86: while(i<2){ cannam@86: result=ogg_stream_packetout(&os,&op); cannam@86: if(result==0)break; cannam@86: if(result<0){ cannam@86: /* Uh oh; data at some point was corrupted or missing! cannam@86: We can't tolerate that in a header. Die. */ cannam@86: fprintf(stderr,"Corrupt secondary header. Exiting.\n"); cannam@86: exit(1); cannam@86: } cannam@86: result=vorbis_synthesis_headerin(&vi,&vc,&op); cannam@86: if(result<0){ cannam@86: fprintf(stderr,"Corrupt secondary header. Exiting.\n"); cannam@86: exit(1); cannam@86: } cannam@86: i++; cannam@86: } cannam@86: } cannam@86: } cannam@86: /* no harm in not checking before adding more */ cannam@86: buffer=ogg_sync_buffer(&oy,4096); cannam@86: bytes=fread(buffer,1,4096,stdin); cannam@86: if(bytes==0 && i<2){ cannam@86: fprintf(stderr,"End of file before finding all Vorbis headers!\n"); cannam@86: exit(1); cannam@86: } cannam@86: ogg_sync_wrote(&oy,bytes); cannam@86: } cannam@86: cannam@86: /* Throw the comments plus a few lines about the bitstream we're cannam@86: decoding */ cannam@86: { cannam@86: char **ptr=vc.user_comments; cannam@86: while(*ptr){ cannam@86: fprintf(stderr,"%s\n",*ptr); cannam@86: ++ptr; cannam@86: } cannam@86: fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi.channels,vi.rate); cannam@86: fprintf(stderr,"Encoded by: %s\n\n",vc.vendor); cannam@86: } cannam@86: cannam@86: convsize=4096/vi.channels; cannam@86: cannam@86: /* OK, got and parsed all three headers. Initialize the Vorbis cannam@86: packet->PCM decoder. */ cannam@86: if(vorbis_synthesis_init(&vd,&vi)==0){ /* central decode state */ cannam@86: vorbis_block_init(&vd,&vb); /* local state for most of the decode cannam@86: so multiple block decodes can cannam@86: proceed in parallel. We could init cannam@86: multiple vorbis_block structures cannam@86: for vd here */ cannam@86: cannam@86: /* The rest is just a straight decode loop until end of stream */ cannam@86: while(!eos){ cannam@86: while(!eos){ cannam@86: int result=ogg_sync_pageout(&oy,&og); cannam@86: if(result==0)break; /* need more data */ cannam@86: if(result<0){ /* missing or corrupt data at this page position */ cannam@86: fprintf(stderr,"Corrupt or missing data in bitstream; " cannam@86: "continuing...\n"); cannam@86: }else{ cannam@86: ogg_stream_pagein(&os,&og); /* can safely ignore errors at cannam@86: this point */ cannam@86: while(1){ cannam@86: result=ogg_stream_packetout(&os,&op); cannam@86: cannam@86: if(result==0)break; /* need more data */ cannam@86: if(result<0){ /* missing or corrupt data at this page position */ cannam@86: /* no reason to complain; already complained above */ cannam@86: }else{ cannam@86: /* we have a packet. Decode it */ cannam@86: float **pcm; cannam@86: int samples; cannam@86: cannam@86: if(vorbis_synthesis(&vb,&op)==0) /* test for success! */ cannam@86: vorbis_synthesis_blockin(&vd,&vb); cannam@86: /* cannam@86: cannam@86: **pcm is a multichannel float vector. In stereo, for cannam@86: example, pcm[0] is left, and pcm[1] is right. samples is cannam@86: the size of each channel. Convert the float values cannam@86: (-1.<=range<=1.) to whatever PCM format and write it out */ cannam@86: cannam@86: while((samples=vorbis_synthesis_pcmout(&vd,&pcm))>0){ cannam@86: int j; cannam@86: int clipflag=0; cannam@86: int bout=(samples32767){ cannam@86: val=32767; cannam@86: clipflag=1; cannam@86: } cannam@86: if(val<-32768){ cannam@86: val=-32768; cannam@86: clipflag=1; cannam@86: } cannam@86: *ptr=val; cannam@86: ptr+=vi.channels; cannam@86: } cannam@86: } cannam@86: cannam@86: if(clipflag) cannam@86: fprintf(stderr,"Clipping in frame %ld\n",(long)(vd.sequence)); cannam@86: cannam@86: cannam@86: fwrite(convbuffer,2*vi.channels,bout,stdout); cannam@86: cannam@86: vorbis_synthesis_read(&vd,bout); /* tell libvorbis how cannam@86: many samples we cannam@86: actually consumed */ cannam@86: } cannam@86: } cannam@86: } cannam@86: if(ogg_page_eos(&og))eos=1; cannam@86: } cannam@86: } cannam@86: if(!eos){ cannam@86: buffer=ogg_sync_buffer(&oy,4096); cannam@86: bytes=fread(buffer,1,4096,stdin); cannam@86: ogg_sync_wrote(&oy,bytes); cannam@86: if(bytes==0)eos=1; cannam@86: } cannam@86: } cannam@86: cannam@86: /* ogg_page and ogg_packet structs always point to storage in cannam@86: libvorbis. They're never freed or manipulated directly */ cannam@86: cannam@86: vorbis_block_clear(&vb); cannam@86: vorbis_dsp_clear(&vd); cannam@86: }else{ cannam@86: fprintf(stderr,"Error: Corrupt header during playback initialization.\n"); cannam@86: } cannam@86: cannam@86: /* clean up this logical bitstream; before exit we see if we're cannam@86: followed by another [chained] */ cannam@86: cannam@86: ogg_stream_clear(&os); cannam@86: vorbis_comment_clear(&vc); cannam@86: vorbis_info_clear(&vi); /* must be called last */ cannam@86: } cannam@86: cannam@86: /* OK, clean up the framer */ cannam@86: ogg_sync_clear(&oy); cannam@86: cannam@86: fprintf(stderr,"Done.\n"); cannam@86: return(0); cannam@86: }