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: stdio-based convenience library for opening/seeking/decoding
15 last mod: $Id: vorbisfile.c,v 1.12 2000/01/05 03:11:05 xiphmont Exp $
17 ********************************************************************/
22 #include "vorbis/codec.h"
23 #include "vorbis/vorbisfile.h"
27 /* A 'chained bitstream' is a Vorbis bitstream that contains more than
28 one logical bitstream arranged end to end (the only form of Ogg
29 multiplexing allowed in a Vorbis bitstream; grouping [parallel
30 multiplexing] is not allowed in Vorbis) */
32 /* A Vorbis file can be played beginning to end (streamed) without
33 worrying ahead of time about chaining (see decoder_example.c). If
34 we have the whole file, however, and want random access
35 (seeking/scrubbing) or desire to know the total length/time of a
36 file, we need to account for the possibility of chaining. */
38 /* We can handle things a number of ways; we can determine the entire
39 bitstream structure right off the bat, or find pieces on demand.
40 This example determines and caches structure for the entire
41 bitstream, but builds a virtual decoder on the fly when moving
42 between links in the chain. */
44 /* There are also different ways to implement seeking. Enough
45 information exists in an Ogg bitstream to seek to
46 sample-granularity positions in the output. Or, one can seek by
47 picking some portion of the stream roughly in the desired area if
48 we only want course navigation through the stream. */
50 /*************************************************************************
51 * Many, many internal helpers. The intention is not to be confusing;
52 * rampant duplication and monolithic function implementation would be
53 * harder to understand anyway. The high level functions are last. Begin
54 * grokking near the end of the file */
56 /* read a little more data from the file/pipe into the ogg_sync framer */
57 #define CHUNKSIZE 4096
58 static long _get_data(OggVorbis_File *vf){
59 char *buffer=ogg_sync_buffer(&vf->oy,4096);
60 long bytes=fread(buffer,1,4096,vf->f);
61 ogg_sync_wrote(&vf->oy,bytes);
65 /* save a tiny smidge of verbosity to make the code more readable */
66 static void _seek_helper(OggVorbis_File *vf,long offset){
67 fseek(vf->f,offset,SEEK_SET);
69 ogg_sync_reset(&vf->oy);
72 /* The read/seek functions track absolute position within the stream */
74 /* from the head of the stream, get the next page. boundary specifies
75 if the function is allowed to fetch more data from the stream (and
76 how much) or only use internally buffered data.
78 boundary: -1) unbounded search
79 0) read no additional data; use cached only
80 n) search for a new page beginning for n bytes
82 return: -1) did not find a page
83 n) found a page at absolute offset n */
85 static long _get_next_page(OggVorbis_File *vf,ogg_page *og,int boundary){
86 if(boundary>0)boundary+=vf->offset;
90 if(boundary>0 && vf->offset>=boundary)return(-1);
91 more=ogg_sync_pageseek(&vf->oy,og);
98 /* send more paramedics */
99 if(!boundary)return(-1);
100 if(_get_data(vf)<=0)return(-1);
102 /* got a page. Return the offset at the page beginning,
103 advance the internal offset past the page end */
113 /* find the latest page beginning before the current stream cursor
114 position. Much dirtier than the above as Ogg doesn't have any
115 backward search linkage. no 'readp' as it will certainly have to
117 static long _get_prev_page(OggVorbis_File *vf,ogg_page *og){
118 long begin=vf->offset;
124 _seek_helper(vf,begin);
125 while(vf->offset<begin+CHUNKSIZE){
126 ret=_get_next_page(vf,og,begin+CHUNKSIZE-vf->offset);
135 /* we have the offset. Actually snork and hold the page now */
136 _seek_helper(vf,offset);
137 ret=_get_next_page(vf,og,CHUNKSIZE);
139 /* this shouldn't be possible */
140 fprintf(stderr,"Missed page fencepost at end of logical bitstream. "
147 /* finds each bitstream link one at a time using a bisection search
148 (has to begin by knowing the offset of the lb's initial page).
149 Recurses for each link so it can alloc the link storage after
150 finding them all, then unroll and fill the cache at the same time */
151 static void _bisect_forward_serialno(OggVorbis_File *vf,
157 long endsearched=end;
162 /* the below guards against garbage seperating the last and
163 first pages of two links. */
164 while(searched<endsearched){
167 if(endsearched-searched<CHUNKSIZE){
170 bisect=(searched+endsearched)/2;
173 _seek_helper(vf,bisect);
174 ret=_get_next_page(vf,&og,-1);
175 if(ret<0 || ogg_page_serialno(&og)!=currentno){
179 searched=ret+og.header_len+og.body_len;
183 _seek_helper(vf,next);
184 ret=_get_next_page(vf,&og,-1);
186 if(searched>=end || ret==-1){
188 vf->offsets=malloc((m+2)*sizeof(long));
189 vf->offsets[m+1]=searched;
191 _bisect_forward_serialno(vf,next,vf->offset,
192 end,ogg_page_serialno(&og),m+1);
195 vf->offsets[m]=begin;
198 /* uses the local ogg_stream storage in vf; this is important for
199 non-streaming input sources */
200 static int _fetch_headers(OggVorbis_File *vf,vorbis_info *vi,long *serialno){
205 ret=_get_next_page(vf,&og,CHUNKSIZE);
207 fprintf(stderr,"Did not find initial header for bitstream.\n");
211 if(serialno)*serialno=ogg_page_serialno(&og);
212 ogg_stream_init(&vf->os,ogg_page_serialno(&og));
214 /* extract the initial header from the first page and verify that the
215 Ogg bitstream is in fact Vorbis data */
217 vorbis_info_clear(vi);
218 vorbis_info_init(vi);
222 ogg_stream_pagein(&vf->os,&og);
224 int result=ogg_stream_packetout(&vf->os,&op);
227 fprintf(stderr,"Corrupt header in logical bitstream.\n");
230 if(vorbis_info_headerin(vi,&op)){
231 fprintf(stderr,"Illegal header in logical bitstream.\n");
237 if(_get_next_page(vf,&og,1)<0){
238 fprintf(stderr,"Missing header in logical bitstream.\n");
242 ogg_stream_clear(&vf->os);
246 vorbis_info_clear(vi);
247 ogg_stream_clear(&vf->os);
251 /* last step of the OggVorbis_File initialization; get all the
252 vorbis_info structs and PCM positions. Only called by the seekable
253 initialization (local stream storage is hacked slightly; pay
254 attention to how that's done) */
255 static void _prefetch_all_headers(OggVorbis_File *vf,vorbis_info *first,
260 vf->vi=calloc(vf->links,sizeof(vorbis_info));
261 vf->dataoffsets=malloc(vf->links*sizeof(long));
262 vf->pcmlengths=malloc(vf->links*sizeof(int64_t));
263 vf->serialnos=malloc(vf->links*sizeof(long));
265 for(i=0;i<vf->links;i++){
267 /* we already grabbed the initial header earlier. This just
268 saves the waste of grabbing it again */
269 memcpy(vf->vi+i,first,sizeof(vorbis_info));
270 memset(first,0,sizeof(vorbis_info));
271 vf->dataoffsets[i]=dataoffset;
274 /* seek to the location of the initial header */
276 _seek_helper(vf,vf->offsets[i]);
277 if(_fetch_headers(vf,vf->vi+i,NULL)==-1){
278 vorbis_info_clear(vf->vi+i);
279 fprintf(stderr,"Error opening logical bitstream #%d.\n\n",i+1);
281 ogg_stream_clear(&vf->os); /* clear local storage. This is not
282 done in _fetch_headers, as that may
283 be called in a non-seekable stream
284 (in which case, we need to preserve
285 the stream local storage) */
286 vf->dataoffsets[i]=-1;
288 vf->dataoffsets[i]=vf->offset;
291 /* get the serial number and PCM length of this link. To do this,
292 get the last page of the stream */
294 long end=vf->offsets[i+1];
295 _seek_helper(vf,end);
298 ret=_get_prev_page(vf,&og);
300 /* this should not be possible */
301 fprintf(stderr,"Could not find last page of logical "
302 "bitstream #%d\n\n",i);
303 vorbis_info_clear(vf->vi+i);
306 if(ogg_page_frameno(&og)!=-1){
307 vf->serialnos[i]=ogg_page_serialno(&og);
308 vf->pcmlengths[i]=ogg_page_frameno(&og);
316 static int _open_seekable(OggVorbis_File *vf){
323 /* is this even vorbis...? */
324 memset(&initial,0,sizeof(vorbis_info));
325 ret=_fetch_headers(vf,&initial,&serialno);
326 dataoffset=vf->offset;
327 ogg_stream_clear(&vf->os);
328 if(ret==-1)return(-1);
330 /* we can seek, so set out learning all about this file */
332 fseek(vf->f,0,SEEK_END);
333 vf->offset=vf->end=ftell(vf->f);
335 /* We get the offset for the last page of the physical bitstream.
336 Most OggVorbis files will contain a single logical bitstream */
337 end=_get_prev_page(vf,&og);
339 /* moer than one logical bitstream? */
340 if(ogg_page_serialno(&og)!=serialno){
342 /* Chained bitstream. Bisect-search each logical bitstream
343 section. Do so based on serial number only */
344 _bisect_forward_serialno(vf,0,0,end+1,serialno,0);
348 /* Only one logical bitstream */
349 _bisect_forward_serialno(vf,0,end,end+1,serialno,0);
353 _prefetch_all_headers(vf,&initial,dataoffset);
359 static int _open_nonseekable(OggVorbis_File *vf){
360 /* we cannot seek. Set up a 'single' (current) logical bitstream entry */
362 vf->vi=malloc(sizeof(vorbis_info));
364 /* Try to fetch the headers, maintaining all the storage */
365 if(_fetch_headers(vf,vf->vi,&vf->current_serialno)==-1)return(-1);
370 /* clear out the current logical bitstream decoder */
371 static void _decode_clear(OggVorbis_File *vf){
372 ogg_stream_clear(&vf->os);
373 vorbis_dsp_clear(&vf->vd);
374 vorbis_block_clear(&vf->vb);
379 /* fetch and process a packet. Handles the case where we're at a
380 bitstream boundary and dumps the decoding machine. If the decoding
381 machine is unloaded, it loads it. It also keeps pcm_offset up to
382 date (seek and read both use this. seek uses a special hack with
385 return: -1) hole in the data (lost packet)
386 0) need more date (only if readp==0)/eof
390 static int _process_packet(OggVorbis_File *vf,int readp){
393 /* handle one packet. Try to fetch it from current stream state */
394 /* extract packets from page */
397 /* process a packet if we can. If the machine isn't loaded,
399 if(vf->decode_ready){
401 int result=ogg_stream_packetout(&vf->os,&op);
404 if(result==-1)return(-1); /* hole in the data. alert the toplevel */
406 /* got a packet. process it */
408 if(!vorbis_synthesis(&vf->vb,&op)){ /* lazy check for lazy
410 header packets aren't
413 vorbis_synthesis will
415 vorbis_synthesis_blockin(&vf->vd,&vf->vb);
417 /* update the pcm offset. */
419 int link=(vf->seekable?vf->current_link:0);
423 /* this packet has a pcm_offset on it (the last packet
424 completed on a page carries the offset) After processing
425 (above), we know the pcm position of the *last* sample
426 ready to be returned. Find the offset of the *first* */
428 samples=vorbis_synthesis_pcmout(&vf->vd,&dummy);
432 frameno+=vf->pcmlengths[i];
433 vf->pcm_offset=frameno;
441 if(_get_next_page(vf,&og,-1)<0)return(0); /* eof. leave unitialized */
443 /* has our decoding just traversed a bitstream boundary? */
444 if(vf->decode_ready){
445 if(vf->current_serialno!=ogg_page_serialno(&og)){
450 /* Do we need to load a new machine before submitting the page? */
451 /* This is different in the seekable and non-seekable cases.
453 In the seekable case, we already have all the header
454 information loaded and cached; we just initialize the machine
455 with it and continue on our merry way.
457 In the non-seekable (streaming) case, we'll only be at a
458 boundary if we just left the previous logical bitstream and
459 we're now nominally at the header of the next bitstream
462 if(!vf->decode_ready){
465 vf->current_serialno=ogg_page_serialno(&og);
467 /* match the serialno to bitstream section. We use this rather than
468 offset positions to avoid problems near logical bitstream
470 for(link=0;link<vf->links;link++)
471 if(vf->serialnos[link]==vf->current_serialno)break;
472 if(link==vf->links)return(-1); /* sign of a bogus stream. error out,
473 leave machine uninitialized */
475 vf->current_link=link;
477 /* we're streaming */
478 /* fetch the three header packets, build the info struct */
480 _fetch_headers(vf,vf->vi,&vf->current_serialno);
486 ogg_stream_init(&vf->os,vf->current_serialno);
487 ogg_stream_reset(&vf->os,ogg_page_pageno(&og));
488 vorbis_synthesis_init(&vf->vd,vf->vi+link);
489 vorbis_block_init(&vf->vd,&vf->vb);
492 ogg_stream_pagein(&vf->os,&og);
496 /**********************************************************************
497 * The helpers are over; it's all toplevel interface from here on out */
499 /* clear out the OggVorbis_File struct */
500 int ov_clear(OggVorbis_File *vf){
502 vorbis_block_clear(&vf->vb);
503 vorbis_dsp_clear(&vf->vd);
504 ogg_stream_clear(&vf->os);
506 if(vf->vi && vf->links){
508 for(i=0;i<vf->links;i++)
509 vorbis_info_clear(vf->vi+i);
512 if(vf->dataoffsets)free(vf->dataoffsets);
513 if(vf->pcmlengths)free(vf->pcmlengths);
514 if(vf->serialnos)free(vf->serialnos);
515 if(vf->offsets)free(vf->offsets);
516 ogg_sync_clear(&vf->oy);
517 if(vf->f)fclose(vf->f);
518 memset(vf,0,sizeof(OggVorbis_File));
523 /* inspects the OggVorbis file and finds/documents all the logical
524 bitstreams contained in it. Tries to be tolerant of logical
525 bitstream sections that are truncated/woogie.
531 int ov_open(FILE *f,OggVorbis_File *vf,char *initial,long ibytes){
532 long offset=fseek(f,0,SEEK_CUR);
535 memset(vf,0,sizeof(OggVorbis_File));
538 /* init the framing state */
539 ogg_sync_init(&vf->oy);
541 /* perhaps some data was previously read into a buffer for testing
542 against other stream types. Allow initialization from this
543 previously read data (as we may be reading from a non-seekable
546 char *buffer=ogg_sync_buffer(&vf->oy,ibytes);
547 memcpy(buffer,initial,ibytes);
548 ogg_sync_wrote(&vf->oy,ibytes);
551 /* can we seek? Stevens suggests the seek test was portable */
553 ret=_open_seekable(vf);
555 ret=_open_nonseekable(vf);
561 ogg_stream_init(&vf->os,vf->current_serialno);
562 vorbis_synthesis_init(&vf->vd,vf->vi);
563 vorbis_block_init(&vf->vd,&vf->vb);
569 /* How many logical bitstreams in this physical bitstream? */
570 long ov_streams(OggVorbis_File *vf){
574 /* Is the FILE * associated with vf seekable? */
575 long ov_seekable(OggVorbis_File *vf){
579 /* returns the bitrate for a given logical bitstream or the entire
580 physical bitstream. If the file is open for random access, it will
581 find the *actual* average bitrate. If the file is streaming, it
582 returns the nominal bitrate (if set) else the average of the
583 upper/lower bounds (if set) else -1 (unset).
585 If you want the actual bitrate field settings, get them from the
586 vorbis_info structs */
588 long ov_bitrate(OggVorbis_File *vf,int i){
589 if(i>=vf->links)return(-1);
590 if(!vf->seekable && i!=0)return(ov_bitrate(vf,0));
594 for(i=0;i<vf->links;i++)
595 bits+=(vf->offsets[i+1]-vf->dataoffsets[i])*8;
596 return(rint(bits/ov_time_total(vf,-1)));
599 /* return the actual bitrate */
600 return(rint((vf->offsets[i+1]-vf->dataoffsets[i])*8/ov_time_total(vf,i)));
602 /* return nominal if set */
603 if(vf->vi[i].bitrate_nominal>0){
604 return vf->vi[i].bitrate_nominal;
606 if(vf->vi[i].bitrate_upper>0){
607 if(vf->vi[i].bitrate_lower>0){
608 return (vf->vi[i].bitrate_upper+vf->vi[i].bitrate_lower)/2;
610 return vf->vi[i].bitrate_upper;
620 long ov_serialnumber(OggVorbis_File *vf,int i){
621 if(i>=vf->links)return(-1);
622 if(!vf->seekable && i>=0)return(ov_serialnumber(vf,-1));
624 return(vf->current_serialno);
626 return(vf->serialnos[i]);
630 /* returns: total raw (compressed) length of content if i==-1
631 raw (compressed) length of that logical bitstream for i==0 to n
632 -1 if the stream is not seekable (we can't know the length)
634 long ov_raw_total(OggVorbis_File *vf,int i){
635 if(!vf->seekable || i>=vf->links)return(-1);
639 for(i=0;i<vf->links;i++)
640 acc+=ov_raw_total(vf,i);
643 return(vf->offsets[i+1]-vf->offsets[i]);
647 /* returns: total PCM length (samples) of content if i==-1
648 PCM length (samples) of that logical bitstream for i==0 to n
649 -1 if the stream is not seekable (we can't know the length)
651 int64_t ov_pcm_total(OggVorbis_File *vf,int i){
652 if(!vf->seekable || i>=vf->links)return(-1);
656 for(i=0;i<vf->links;i++)
657 acc+=ov_pcm_total(vf,i);
660 return(vf->pcmlengths[i]);
664 /* returns: total seconds of content if i==-1
665 seconds in that logical bitstream for i==0 to n
666 -1 if the stream is not seekable (we can't know the length)
668 double ov_time_total(OggVorbis_File *vf,int i){
669 if(!vf->seekable || i>=vf->links)return(-1);
673 for(i=0;i<vf->links;i++)
674 acc+=ov_time_total(vf,i);
677 return((float)(vf->pcmlengths[i])/vf->vi[i].rate);
681 /* seek to an offset relative to the *compressed* data. This also
682 immediately sucks in and decodes pages to update the PCM cursor. It
683 will cross a logical bitstream boundary, but only if it can't get
684 any packets out of the tail of the bitstream we seek to (so no
687 returns zero on success, nonzero on failure */
689 int ov_raw_seek(OggVorbis_File *vf,long pos){
691 if(!vf->seekable)return(-1); /* don't dump machine if we can't seek */
692 if(pos<0 || pos>vf->offsets[vf->links])goto seek_error;
694 /* clear out decoding machine state */
698 _seek_helper(vf,pos);
700 /* we need to make sure the pcm_offset is set. We use the
701 _fetch_packet helper to process one packet with readp set, then
702 call it until it returns '0' with readp not set (the last packet
703 from a page has the 'frameno' field set, and that's how the
704 helper updates the offset */
706 switch(_process_packet(vf,1)){
708 /* oh, eof. There are no packets remaining. Set the pcm offset to
710 vf->pcm_offset=ov_pcm_total(vf,-1);
713 /* error! missing data or invalid bitstream structure */
721 switch(_process_packet(vf,0)){
723 /* the offset is set. If it's a bogus bitstream with no offset
724 information, it's not but that's not our fault. We still run
725 gracefully, we're just missing the offset */
728 /* error! missing data or invalid bitstream structure */
731 /* continue processing packets */
737 /* dump the machine so we're in a known state */
742 /* seek to a sample offset relative to the decompressed pcm stream
744 returns zero on success, nonzero on failure */
746 int ov_pcm_seek(OggVorbis_File *vf,int64_t pos){
748 int64_t total=ov_pcm_total(vf,-1);
750 if(!vf->seekable)return(-1); /* don't dump machine if we can't seek */
751 if(pos<0 || pos>total)goto seek_error;
753 /* which bitstream section does this pcm offset occur in? */
754 for(link=vf->links-1;link>=0;link--){
755 total-=vf->pcmlengths[link];
759 /* search within the logical bitstream for the page with the highest
760 pcm_pos preceeding (or equal to) pos. There is a danger here;
761 missing pages or incorrect frame number information in the
762 bitstream could make our task impossible. Account for that (it
763 would be an error condition) */
765 int64_t target=pos-total;
766 long end=vf->offsets[link+1];
767 long begin=vf->offsets[link];
775 if(end-begin<CHUNKSIZE){
778 bisect=(end+begin)/2;
781 _seek_helper(vf,bisect);
782 ret=_get_next_page(vf,&og,end-bisect);
787 int64_t frameno=ogg_page_frameno(&og);
789 best=ret; /* raw offset of packet with frameno */
790 begin=vf->offset; /* raw offset of next packet */
797 /* found our page. seek to it (call raw_seek). */
799 if(ov_raw_seek(vf,best))goto seek_error;
803 if(vf->pcm_offset>=pos)goto seek_error;
804 if(pos>ov_pcm_total(vf,-1))goto seek_error;
806 /* discard samples until we reach the desired position. Crossing a
807 logical bitstream boundary with abandon is OK. */
808 while(vf->pcm_offset<pos){
810 long target=pos-vf->pcm_offset;
811 long samples=vorbis_synthesis_pcmout(&vf->vd,&pcm);
813 if(samples>target)samples=target;
814 vorbis_synthesis_read(&vf->vd,samples);
815 vf->pcm_offset+=samples;
818 if(_process_packet(vf,1)==0)
819 vf->pcm_offset=ov_pcm_total(vf,-1); /* eof */
824 /* dump machine so we're in a known state */
829 /* seek to a playback time relative to the decompressed pcm stream
830 returns zero on success, nonzero on failure */
831 int ov_time_seek(OggVorbis_File *vf,double seconds){
832 /* translate time to PCM position and call ov_pcm_seek */
835 int64_t pcm_total=ov_pcm_total(vf,-1);
836 double time_total=ov_time_total(vf,-1);
838 if(!vf->seekable)return(-1); /* don't dump machine if we can't seek */
839 if(seconds<0 || seconds>time_total)goto seek_error;
841 /* which bitstream section does this time offset occur in? */
842 for(link=vf->links-1;link>=0;link--){
843 pcm_total-=vf->pcmlengths[link];
844 time_total-=ov_time_total(vf,link);
845 if(seconds>=time_total)break;
848 /* enough information to convert time offset to pcm offset */
850 int64_t target=pcm_total+(seconds-time_total)*vf->vi[link].rate;
851 return(ov_pcm_seek(vf,target));
855 /* dump machine so we're in a known state */
860 /* tell the current stream offset cursor. Note that seek followed by
861 tell will likely not give the set offset due to caching */
862 long ov_raw_tell(OggVorbis_File *vf){
866 /* return PCM offset (sample) of next PCM sample to be read */
867 int64_t ov_pcm_tell(OggVorbis_File *vf){
868 return(vf->pcm_offset);
871 /* return time offset (seconds) of next PCM sample to be read */
872 double ov_time_tell(OggVorbis_File *vf){
873 /* translate time to PCM position and call ov_pcm_seek */
877 double time_total=0.;
880 pcm_total=ov_pcm_total(vf,-1);
881 time_total=ov_time_total(vf,-1);
883 /* which bitstream section does this time offset occur in? */
884 for(link=vf->links-1;link>=0;link--){
885 pcm_total-=vf->pcmlengths[link];
886 time_total-=ov_time_total(vf,link);
887 if(vf->pcm_offset>pcm_total)break;
891 return(time_total+(vf->pcm_offset-pcm_total)/vf->vi[link].rate);
894 /* link: -1) return the vorbis_info struct for the bitstream section
895 currently being decoded
896 0-n) to request information for a specific bitstream section
898 In the case of a non-seekable bitstream, any call returns the
899 current bitstream. NULL in the case that the machine is not
902 vorbis_info *ov_info(OggVorbis_File *vf,int link){
906 return vf->vi+vf->current_link;
922 /* up to this point, everything could more or less hide the multiple
923 logical bitstream nature of chaining from the toplevel application
924 if the toplevel application didn't particularly care. However, at
925 the point that we actually read audio back, the multiple-section
926 nature must surface: Multiple bitstream sections do not necessarily
927 have to have the same number of channels or sampling rate.
929 ov_read returns the sequential logical bitstream number currently
930 being decoded along with the PCM data in order that the toplevel
931 application can take action on channel/sample rate changes. This
932 number will be incremented even for streamed (non-seekable) streams
933 (for seekable streams, it represents the actual logical bitstream
934 index within the physical bitstream. Note that the accessor
935 functions above are aware of this dichotomy).
937 input values: buffer) a buffer to hold packed PCM data for return
938 length) the byte length requested to be placed into buffer
939 bigendianp) should the data be packed LSB first (0) or
941 word) word size for output. currently 1 (byte) or
944 return values: -1) error/hole in data
946 n) number of bytes of PCM actually returned. The
947 below works on a packet-by-packet basis, so the
948 return length is not related to the 'length' passed
949 in, just guaranteed to fit.
951 *section) set to the logical bitstream number */
953 long ov_read(OggVorbis_File *vf,char *buffer,int length,
954 int bigendianp,int word,int sgned,int *bitstream){
958 if(vf->decode_ready){
960 long samples=vorbis_synthesis_pcmout(&vf->vd,&pcm);
962 /* yay! proceed to pack data into the byte buffer */
964 long channels=ov_info(vf,-1)->channels;
965 long bytespersample=word * channels;
966 if(samples>length/bytespersample)samples=length/bytespersample;
968 /* a tight loop to pack each size */
971 int off=(sgned?0:128);
972 for(j=0;j<samples;j++)
973 for(i=0;i<channels;i++){
974 int val=rint(pcm[i][j]*128.);
976 if(val<-128)val=-128;
980 int off=(sgned?0:32768);
983 for(j=0;j<samples;j++)
984 for(i=0;i<channels;i++){
985 int val=rint(pcm[i][j]*32768.);
986 if(val>32767)val=32767;
987 if(val<-32768)val=-32768;
990 *buffer++=(val&0xff);
993 for(j=0;j<samples;j++)
994 for(i=0;i<channels;i++){
995 int val=rint(pcm[i][j]*32768.);
996 if(val>32767)val=32767;
997 if(val<-32768)val=-32768;
999 *buffer++=(val&0xff);
1007 vorbis_synthesis_read(&vf->vd,samples);
1008 vf->pcm_offset+=samples;
1009 if(bitstream)*bitstream=vf->current_link;
1010 return(samples*bytespersample);
1014 /* suck in another packet */
1015 switch(_process_packet(vf,1)){