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