Commit minor speed patch (sliding window in vorbis_blockin)
[platform/upstream/libvorbis.git] / lib / vorbisfile.c
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
5  * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH    *
6  * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.        *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2000             *
9  * by Monty <monty@xiph.org> and the XIPHOPHORUS Company            *
10  * http://www.xiph.org/                                             *
11  *                                                                  *
12  ********************************************************************
13
14  function: stdio-based convenience library for opening/seeking/decoding
15  last mod: $Id: vorbisfile.c,v 1.34 2000/12/21 21:04:41 xiphmont Exp $
16
17  ********************************************************************/
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <math.h>
23
24 #include "vorbis/codec.h"
25 #include "vorbis/vorbisfile.h"
26
27 #include "os.h"
28 #include "misc.h"
29
30 /* A 'chained bitstream' is a Vorbis bitstream that contains more than
31    one logical bitstream arranged end to end (the only form of Ogg
32    multiplexing allowed in a Vorbis bitstream; grouping [parallel
33    multiplexing] is not allowed in Vorbis) */
34
35 /* A Vorbis file can be played beginning to end (streamed) without
36    worrying ahead of time about chaining (see decoder_example.c).  If
37    we have the whole file, however, and want random access
38    (seeking/scrubbing) or desire to know the total length/time of a
39    file, we need to account for the possibility of chaining. */
40
41 /* We can handle things a number of ways; we can determine the entire
42    bitstream structure right off the bat, or find pieces on demand.
43    This example determines and caches structure for the entire
44    bitstream, but builds a virtual decoder on the fly when moving
45    between links in the chain. */
46
47 /* There are also different ways to implement seeking.  Enough
48    information exists in an Ogg bitstream to seek to
49    sample-granularity positions in the output.  Or, one can seek by
50    picking some portion of the stream roughly in the desired area if
51    we only want course navigation through the stream. */
52
53 /*************************************************************************
54  * Many, many internal helpers.  The intention is not to be confusing; 
55  * rampant duplication and monolithic function implementation would be 
56  * harder to understand anyway.  The high level functions are last.  Begin
57  * grokking near the end of the file */
58
59 /* read a little more data from the file/pipe into the ogg_sync framer */
60 #define CHUNKSIZE 4096
61 static long _get_data(OggVorbis_File *vf){
62   char *buffer=ogg_sync_buffer(&vf->oy,CHUNKSIZE);
63   long bytes=(vf->callbacks.read_func)(buffer,1,CHUNKSIZE,vf->datasource);
64   if(bytes>0)ogg_sync_wrote(&vf->oy,bytes);
65   return(bytes);
66 }
67
68 /* save a tiny smidge of verbosity to make the code more readable */
69 static void _seek_helper(OggVorbis_File *vf,long offset){
70   (vf->callbacks.seek_func)(vf->datasource, offset, SEEK_SET);
71   vf->offset=offset;
72   ogg_sync_reset(&vf->oy);
73 }
74
75 /* The read/seek functions track absolute position within the stream */
76
77 /* from the head of the stream, get the next page.  boundary specifies
78    if the function is allowed to fetch more data from the stream (and
79    how much) or only use internally buffered data.
80
81    boundary: -1) unbounded search
82               0) read no additional data; use cached only
83               n) search for a new page beginning for n bytes
84
85    return:   <0) did not find a page (OV_FALSE, OV_EOF, OV_EREAD)
86               n) found a page at absolute offset n */
87
88 static long _get_next_page(OggVorbis_File *vf,ogg_page *og,int boundary){
89   if(boundary>0)boundary+=vf->offset;
90   while(1){
91     long more;
92
93     if(boundary>0 && vf->offset>=boundary)return(OV_FALSE);
94     more=ogg_sync_pageseek(&vf->oy,og);
95     
96     if(more<0){
97       /* skipped n bytes */
98       vf->offset-=more;
99     }else{
100       if(more==0){
101         /* send more paramedics */
102         if(!boundary)return(OV_FALSE);
103         {
104           long ret=_get_data(vf);
105           if(ret==0)return(OV_EOF);
106           if(ret<0)return(OV_EREAD);
107         }
108       }else{
109         /* got a page.  Return the offset at the page beginning,
110            advance the internal offset past the page end */
111         long ret=vf->offset;
112         vf->offset+=more;
113         return(ret);
114         
115       }
116     }
117   }
118 }
119
120 /* find the latest page beginning before the current stream cursor
121    position. Much dirtier than the above as Ogg doesn't have any
122    backward search linkage.  no 'readp' as it will certainly have to
123    read. */
124 /* returns offset or OV_EREAD, OV_FAULT */
125 static long _get_prev_page(OggVorbis_File *vf,ogg_page *og){
126   long begin=vf->offset;
127   long ret;
128   int offset=-1;
129
130   while(offset==-1){
131     begin-=CHUNKSIZE;
132     _seek_helper(vf,begin);
133     while(vf->offset<begin+CHUNKSIZE){
134       ret=_get_next_page(vf,og,begin+CHUNKSIZE-vf->offset);
135       if(ret==OV_EREAD)return(OV_EREAD);
136       if(ret<0){
137         break;
138       }else{
139         offset=ret;
140       }
141     }
142   }
143
144   /* we have the offset.  Actually snork and hold the page now */
145   _seek_helper(vf,offset);
146   ret=_get_next_page(vf,og,CHUNKSIZE);
147   if(ret<0)
148     /* this shouldn't be possible */
149     return(OV_EFAULT);
150
151   return(offset);
152 }
153
154 /* finds each bitstream link one at a time using a bisection search
155    (has to begin by knowing the offset of the lb's initial page).
156    Recurses for each link so it can alloc the link storage after
157    finding them all, then unroll and fill the cache at the same time */
158 static int _bisect_forward_serialno(OggVorbis_File *vf,
159                                     long begin,
160                                     long searched,
161                                     long end,
162                                     long currentno,
163                                     long m){
164   long endsearched=end;
165   long next=end;
166   ogg_page og;
167   long ret;
168   
169   /* the below guards against garbage seperating the last and
170      first pages of two links. */
171   while(searched<endsearched){
172     long bisect;
173     
174     if(endsearched-searched<CHUNKSIZE){
175       bisect=searched;
176     }else{
177       bisect=(searched+endsearched)/2;
178     }
179     
180     _seek_helper(vf,bisect);
181     ret=_get_next_page(vf,&og,-1);
182     if(ret==OV_EREAD)return(OV_EREAD);
183     if(ret<0 || ogg_page_serialno(&og)!=currentno){
184       endsearched=bisect;
185       if(ret>=0)next=ret;
186     }else{
187       searched=ret+og.header_len+og.body_len;
188     }
189   }
190
191   _seek_helper(vf,next);
192   ret=_get_next_page(vf,&og,-1);
193   if(ret==OV_EREAD)return(OV_EREAD);
194   
195   if(searched>=end || ret<0){
196     vf->links=m+1;
197     vf->offsets=_ogg_malloc((m+2)*sizeof(ogg_int64_t));
198     vf->offsets[m+1]=searched;
199   }else{
200     ret=_bisect_forward_serialno(vf,next,vf->offset,
201                                  end,ogg_page_serialno(&og),m+1);
202     if(ret==OV_EREAD)return(OV_EREAD);
203   }
204   
205   vf->offsets[m]=begin;
206   return(0);
207 }
208
209 /* uses the local ogg_stream storage in vf; this is important for
210    non-streaming input sources */
211 static int _fetch_headers(OggVorbis_File *vf,vorbis_info *vi,vorbis_comment *vc,
212                           long *serialno,ogg_page *og_ptr){
213   ogg_page og;
214   ogg_packet op;
215   int i,ret=0;
216   
217   if(!og_ptr){
218     ret=_get_next_page(vf,&og,CHUNKSIZE);
219     if(ret==OV_EREAD)return(OV_EREAD);
220     if(ret<0)return OV_ENOTVORBIS;
221     og_ptr=&og;
222   }
223
224   if(serialno)*serialno=ogg_page_serialno(og_ptr);
225   ogg_stream_init(&vf->os,ogg_page_serialno(og_ptr));
226   
227   /* extract the initial header from the first page and verify that the
228      Ogg bitstream is in fact Vorbis data */
229   
230   vorbis_info_init(vi);
231   vorbis_comment_init(vc);
232   
233   i=0;
234   while(i<3){
235     ogg_stream_pagein(&vf->os,og_ptr);
236     while(i<3){
237       int result=ogg_stream_packetout(&vf->os,&op);
238       if(result==0)break;
239       if(result==-1){
240         ret=OV_EBADHEADER;
241         goto bail_header;
242       }
243       if((ret=vorbis_synthesis_headerin(vi,vc,&op))){
244         goto bail_header;
245       }
246       i++;
247     }
248     if(i<3)
249       if(_get_next_page(vf,og_ptr,1)<0){
250         ret=OV_EBADHEADER;
251         goto bail_header;
252       }
253   }
254   return 0; 
255
256  bail_header:
257   vorbis_info_clear(vi);
258   vorbis_comment_clear(vc);
259   ogg_stream_clear(&vf->os);
260   return ret;
261 }
262
263 /* last step of the OggVorbis_File initialization; get all the
264    vorbis_info structs and PCM positions.  Only called by the seekable
265    initialization (local stream storage is hacked slightly; pay
266    attention to how that's done) */
267
268 /* this is void and does not propogate errors up because we want to be
269    able to open and use damaged bitstreams as well as we can.  Just
270    watch out for missing information for links in the OggVorbis_File
271    struct */
272 static void _prefetch_all_headers(OggVorbis_File *vf,vorbis_info *first_i,
273                                   vorbis_comment *first_c,
274                                   long dataoffset){
275   ogg_page og;
276   int i,ret;
277   
278   vf->vi=_ogg_calloc(vf->links,sizeof(vorbis_info));
279   vf->vc=_ogg_calloc(vf->links,sizeof(vorbis_info));
280   vf->dataoffsets=_ogg_malloc(vf->links*sizeof(ogg_int64_t));
281   vf->pcmlengths=_ogg_malloc(vf->links*sizeof(ogg_int64_t));
282   vf->serialnos=_ogg_malloc(vf->links*sizeof(long));
283   
284   for(i=0;i<vf->links;i++){
285     if(first_i && first_c && i==0){
286       /* we already grabbed the initial header earlier.  This just
287          saves the waste of grabbing it again */
288       memcpy(vf->vi+i,first_i,sizeof(vorbis_info));
289       memcpy(vf->vc+i,first_c,sizeof(vorbis_comment));
290       vf->dataoffsets[i]=dataoffset;
291     }else{
292
293       /* seek to the location of the initial header */
294
295       _seek_helper(vf,vf->offsets[i]);
296       if(_fetch_headers(vf,vf->vi+i,vf->vc+i,NULL,NULL)<0){
297         vf->dataoffsets[i]=-1;
298       }else{
299         vf->dataoffsets[i]=vf->offset;
300         ogg_stream_clear(&vf->os);
301       }
302     }
303
304     /* get the serial number and PCM length of this link. To do this,
305        get the last page of the stream */
306     {
307       long end=vf->offsets[i+1];
308       _seek_helper(vf,end);
309
310       while(1){
311         ret=_get_prev_page(vf,&og);
312         if(ret<0){
313           /* this should not be possible, actually */
314           vorbis_info_clear(vf->vi+i);
315           vorbis_comment_clear(vf->vc+i);
316           break;
317         }
318         if(ogg_page_granulepos(&og)!=-1){
319           vf->serialnos[i]=ogg_page_serialno(&og);
320           vf->pcmlengths[i]=ogg_page_granulepos(&og);
321           break;
322         }
323       }
324     }
325   }
326 }
327
328 static void _make_decode_ready(OggVorbis_File *vf){
329   if(vf->decode_ready)return;
330   if(vf->seekable){
331     vorbis_synthesis_init(&vf->vd,vf->vi+vf->current_link);
332   }else{
333     vorbis_synthesis_init(&vf->vd,vf->vi);
334   }    
335   vorbis_block_init(&vf->vd,&vf->vb);
336   vf->decode_ready=1;
337   return;
338 }
339
340 static int _open_seekable(OggVorbis_File *vf){
341   vorbis_info initial_i;
342   vorbis_comment initial_c;
343   long serialno,end;
344   int ret;
345   long dataoffset;
346   ogg_page og;
347   
348   /* is this even vorbis...? */
349   ret=_fetch_headers(vf,&initial_i,&initial_c,&serialno,NULL);
350   dataoffset=vf->offset;
351   ogg_stream_clear(&vf->os);
352   if(ret<0)return(ret);
353   
354   /* we can seek, so set out learning all about this file */
355   vf->seekable=1;
356   (vf->callbacks.seek_func)(vf->datasource,0,SEEK_END);
357   vf->offset=vf->end=(vf->callbacks.tell_func)(vf->datasource);
358   
359   /* We get the offset for the last page of the physical bitstream.
360      Most OggVorbis files will contain a single logical bitstream */
361   end=_get_prev_page(vf,&og);
362   if(end<0){
363     ogg_stream_clear(&vf->os);
364     return(end);
365   }
366
367   /* more than one logical bitstream? */
368   if(ogg_page_serialno(&og)!=serialno){
369
370     /* Chained bitstream. Bisect-search each logical bitstream
371        section.  Do so based on serial number only */
372     if(_bisect_forward_serialno(vf,0,0,end+1,serialno,0)<0){
373       ogg_stream_clear(&vf->os);
374       return(OV_EREAD);
375     }
376
377   }else{
378
379     /* Only one logical bitstream */
380     if(_bisect_forward_serialno(vf,0,end,end+1,serialno,0)){
381       ogg_stream_clear(&vf->os);
382       return(OV_EREAD);
383     }
384
385   }
386
387   _prefetch_all_headers(vf,&initial_i,&initial_c,dataoffset);
388   return(ov_raw_seek(vf,0));
389
390 }
391
392 static int _open_nonseekable(OggVorbis_File *vf){
393   int ret;
394   /* we cannot seek. Set up a 'single' (current) logical bitstream entry  */
395   vf->links=1;
396   vf->vi=_ogg_calloc(vf->links,sizeof(vorbis_info));
397   vf->vc=_ogg_calloc(vf->links,sizeof(vorbis_info));
398
399   /* Try to fetch the headers, maintaining all the storage */
400   if((ret=_fetch_headers(vf,vf->vi,vf->vc,&vf->current_serialno,NULL))<0)
401     return(ret);
402   _make_decode_ready(vf);
403
404   return 0;
405 }
406
407 /* clear out the current logical bitstream decoder */ 
408 static void _decode_clear(OggVorbis_File *vf){
409   ogg_stream_clear(&vf->os);
410   vorbis_dsp_clear(&vf->vd);
411   vorbis_block_clear(&vf->vb);
412   vf->decode_ready=0;
413
414   vf->bittrack=0.f;
415   vf->samptrack=0.f;
416 }
417
418 /* fetch and process a packet.  Handles the case where we're at a
419    bitstream boundary and dumps the decoding machine.  If the decoding
420    machine is unloaded, it loads it.  It also keeps pcm_offset up to
421    date (seek and read both use this.  seek uses a special hack with
422    readp). 
423
424    return: <0) error, OV_HOLE (lost packet) or OV_EOF
425             0) need more data (only if readp==0)
426             1) got a packet 
427 */
428
429 static int _process_packet(OggVorbis_File *vf,int readp){
430   ogg_page og;
431
432   /* handle one packet.  Try to fetch it from current stream state */
433   /* extract packets from page */
434   while(1){
435     
436     /* process a packet if we can.  If the machine isn't loaded,
437        neither is a page */
438     if(vf->decode_ready){
439       ogg_packet op;
440       int result=ogg_stream_packetout(&vf->os,&op);
441       ogg_int64_t granulepos;
442       
443       if(result==-1)return(OV_HOLE); /* hole in the data. */
444       if(result>0){
445         /* got a packet.  process it */
446         granulepos=op.granulepos;
447         if(!vorbis_synthesis(&vf->vb,&op)){ /* lazy check for lazy
448                                                header handling.  The
449                                                header packets aren't
450                                                audio, so if/when we
451                                                submit them,
452                                                vorbis_synthesis will
453                                                reject them */
454
455           /* suck in the synthesis data and track bitrate */
456           {
457             int oldsamples=vorbis_synthesis_pcmout(&vf->vd,NULL);
458             vorbis_synthesis_blockin(&vf->vd,&vf->vb);
459             vf->samptrack+=vorbis_synthesis_pcmout(&vf->vd,NULL)-oldsamples;
460             vf->bittrack+=op.bytes*8;
461           }
462           
463           /* update the pcm offset. */
464           if(granulepos!=-1 && !op.e_o_s){
465             int link=(vf->seekable?vf->current_link:0);
466             int i,samples;
467             
468             /* this packet has a pcm_offset on it (the last packet
469                completed on a page carries the offset) After processing
470                (above), we know the pcm position of the *last* sample
471                ready to be returned. Find the offset of the *first*
472
473                As an aside, this trick is inaccurate if we begin
474                reading anew right at the last page; the end-of-stream
475                granulepos declares the last frame in the stream, and the
476                last packet of the last page may be a partial frame.
477                So, we need a previous granulepos from an in-sequence page
478                to have a reference point.  Thus the !op.e_o_s clause
479                above */
480             
481             samples=vorbis_synthesis_pcmout(&vf->vd,NULL);
482             
483             granulepos-=samples;
484             for(i=0;i<link;i++)
485               granulepos+=vf->pcmlengths[i];
486             vf->pcm_offset=granulepos;
487           }
488           return(1);
489         }
490       }
491     }
492
493     if(!readp)return(0);
494     if(_get_next_page(vf,&og,-1)<0)return(OV_EOF); /* eof. leave unitialized */
495
496     /* bitrate tracking; add the header's bytes here, the body bytes
497        are done by packet above */
498     vf->bittrack+=og.header_len*8;
499
500     /* has our decoding just traversed a bitstream boundary? */
501     if(vf->decode_ready){
502       if(vf->current_serialno!=ogg_page_serialno(&og)){
503         _decode_clear(vf);
504       }
505     }
506
507     /* Do we need to load a new machine before submitting the page? */
508     /* This is different in the seekable and non-seekable cases.  
509
510        In the seekable case, we already have all the header
511        information loaded and cached; we just initialize the machine
512        with it and continue on our merry way.
513
514        In the non-seekable (streaming) case, we'll only be at a
515        boundary if we just left the previous logical bitstream and
516        we're now nominally at the header of the next bitstream
517     */
518
519     if(!vf->decode_ready){
520       int link;
521       if(vf->seekable){
522         vf->current_serialno=ogg_page_serialno(&og);
523         
524         /* match the serialno to bitstream section.  We use this rather than
525            offset positions to avoid problems near logical bitstream
526            boundaries */
527         for(link=0;link<vf->links;link++)
528           if(vf->serialnos[link]==vf->current_serialno)break;
529         if(link==vf->links)return(OV_EBADLINK); /* sign of a bogus
530                                                    stream.  error out,
531                                                    leave machine
532                                                    uninitialized */
533         
534         vf->current_link=link;
535         
536         ogg_stream_init(&vf->os,vf->current_serialno);
537         ogg_stream_reset(&vf->os); 
538         
539       }else{
540         /* we're streaming */
541         /* fetch the three header packets, build the info struct */
542         
543         _fetch_headers(vf,vf->vi,vf->vc,&vf->current_serialno,&og);
544         vf->current_link++;
545         link=0;
546       }
547       
548       _make_decode_ready(vf);
549     }
550     ogg_stream_pagein(&vf->os,&og);
551   }
552 }
553
554 /**********************************************************************
555  * The helpers are over; it's all toplevel interface from here on out */
556  
557 /* clear out the OggVorbis_File struct */
558 int ov_clear(OggVorbis_File *vf){
559   if(vf){
560     vorbis_block_clear(&vf->vb);
561     vorbis_dsp_clear(&vf->vd);
562     ogg_stream_clear(&vf->os);
563     
564     if(vf->vi && vf->links){
565       int i;
566       for(i=0;i<vf->links;i++){
567         vorbis_info_clear(vf->vi+i);
568         vorbis_comment_clear(vf->vc+i);
569       }
570       _ogg_free(vf->vi);
571       _ogg_free(vf->vc);
572     }
573     if(vf->dataoffsets)_ogg_free(vf->dataoffsets);
574     if(vf->pcmlengths)_ogg_free(vf->pcmlengths);
575     if(vf->serialnos)_ogg_free(vf->serialnos);
576     if(vf->offsets)_ogg_free(vf->offsets);
577     ogg_sync_clear(&vf->oy);
578     if(vf->datasource)(vf->callbacks.close_func)(vf->datasource);
579     memset(vf,0,sizeof(OggVorbis_File));
580   }
581 #ifdef DEBUG_LEAKS
582   _VDBG_dump();
583 #endif
584   return(0);
585 }
586
587 static int _fseek64_wrap(FILE *f,ogg_int64_t off,int whence){
588   return fseek(f,(int)off,whence);
589 }
590
591 /* inspects the OggVorbis file and finds/documents all the logical
592    bitstreams contained in it.  Tries to be tolerant of logical
593    bitstream sections that are truncated/woogie. 
594
595    return: -1) error
596             0) OK
597 */
598
599 int ov_open(FILE *f,OggVorbis_File *vf,char *initial,long ibytes){
600   ov_callbacks callbacks = {
601     (size_t (*)(void *, size_t, size_t, void *))  fread,
602     (int (*)(void *, ogg_int64_t, int))              _fseek64_wrap,
603     (int (*)(void *))                             fclose,
604     (long (*)(void *))                            ftell
605   };
606
607   return ov_open_callbacks((void *)f, vf, initial, ibytes, callbacks);
608 }
609   
610
611 int ov_open_callbacks(void *f,OggVorbis_File *vf,char *initial,long ibytes,
612     ov_callbacks callbacks)
613 {
614   long offset=callbacks.seek_func(f,0,SEEK_CUR);
615   int ret;
616
617   memset(vf,0,sizeof(OggVorbis_File));
618   vf->datasource=f;
619   vf->callbacks = callbacks;
620
621   /* init the framing state */
622   ogg_sync_init(&vf->oy);
623
624   /* perhaps some data was previously read into a buffer for testing
625      against other stream types.  Allow initialization from this
626      previously read data (as we may be reading from a non-seekable
627      stream) */
628   if(initial){
629     char *buffer=ogg_sync_buffer(&vf->oy,ibytes);
630     memcpy(buffer,initial,ibytes);
631     ogg_sync_wrote(&vf->oy,ibytes);
632   }
633
634   /* can we seek? Stevens suggests the seek test was portable */
635   if(offset!=-1){
636     ret=_open_seekable(vf);
637   }else{
638     ret=_open_nonseekable(vf);
639   }
640   if(ret){
641     vf->datasource=NULL;
642     ov_clear(vf);
643   }
644   return(ret);
645 }
646
647 /* How many logical bitstreams in this physical bitstream? */
648 long ov_streams(OggVorbis_File *vf){
649   return vf->links;
650 }
651
652 /* Is the FILE * associated with vf seekable? */
653 long ov_seekable(OggVorbis_File *vf){
654   return vf->seekable;
655 }
656
657 /* returns the bitrate for a given logical bitstream or the entire
658    physical bitstream.  If the file is open for random access, it will
659    find the *actual* average bitrate.  If the file is streaming, it
660    returns the nominal bitrate (if set) else the average of the
661    upper/lower bounds (if set) else -1 (unset).
662
663    If you want the actual bitrate field settings, get them from the
664    vorbis_info structs */
665
666 long ov_bitrate(OggVorbis_File *vf,int i){
667   if(i>=vf->links)return(OV_EINVAL);
668   if(!vf->seekable && i!=0)return(ov_bitrate(vf,0));
669   if(i<0){
670     ogg_int64_t bits=0;
671     int i;
672     for(i=0;i<vf->links;i++)
673       bits+=(vf->offsets[i+1]-vf->dataoffsets[i])*8;
674     return(rint(bits/ov_time_total(vf,-1)));
675   }else{
676     if(vf->seekable){
677       /* return the actual bitrate */
678       return(rint((vf->offsets[i+1]-vf->dataoffsets[i])*8/ov_time_total(vf,i)));
679     }else{
680       /* return nominal if set */
681       if(vf->vi[i].bitrate_nominal>0){
682         return vf->vi[i].bitrate_nominal;
683       }else{
684         if(vf->vi[i].bitrate_upper>0){
685           if(vf->vi[i].bitrate_lower>0){
686             return (vf->vi[i].bitrate_upper+vf->vi[i].bitrate_lower)/2;
687           }else{
688             return vf->vi[i].bitrate_upper;
689           }
690         }
691         return(OV_FALSE);
692       }
693     }
694   }
695 }
696
697 /* returns the actual bitrate since last call.  returns -1 if no
698    additional data to offer since last call (or at beginning of stream) */
699 long ov_bitrate_instant(OggVorbis_File *vf){
700   int link=(vf->seekable?vf->current_link:0);
701   long ret;
702   if(vf->samptrack==0)return(OV_FALSE);
703   ret=vf->bittrack/vf->samptrack*vf->vi[link].rate+.5;
704   vf->bittrack=0.f;
705   vf->samptrack=0.f;
706   return(ret);
707 }
708
709 /* Guess */
710 long ov_serialnumber(OggVorbis_File *vf,int i){
711   if(i>=vf->links)return(ov_serialnumber(vf,vf->links-1));
712   if(!vf->seekable && i>=0)return(ov_serialnumber(vf,-1));
713   if(i<0){
714     return(vf->current_serialno);
715   }else{
716     return(vf->serialnos[i]);
717   }
718 }
719
720 /* returns: total raw (compressed) length of content if i==-1
721             raw (compressed) length of that logical bitstream for i==0 to n
722             -1 if the stream is not seekable (we can't know the length)
723 */
724 ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i){
725   if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
726   if(i<0){
727     long acc=0;
728     int i;
729     for(i=0;i<vf->links;i++)
730       acc+=ov_raw_total(vf,i);
731     return(acc);
732   }else{
733     return(vf->offsets[i+1]-vf->offsets[i]);
734   }
735 }
736
737 /* returns: total PCM length (samples) of content if i==-1
738             PCM length (samples) of that logical bitstream for i==0 to n
739             -1 if the stream is not seekable (we can't know the length)
740 */
741 ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i){
742   if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
743   if(i<0){
744     ogg_int64_t acc=0;
745     int i;
746     for(i=0;i<vf->links;i++)
747       acc+=ov_pcm_total(vf,i);
748     return(acc);
749   }else{
750     return(vf->pcmlengths[i]);
751   }
752 }
753
754 /* returns: total seconds of content if i==-1
755             seconds in that logical bitstream for i==0 to n
756             -1 if the stream is not seekable (we can't know the length)
757 */
758 double ov_time_total(OggVorbis_File *vf,int i){
759   if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
760   if(i<0){
761     double acc=0;
762     int i;
763     for(i=0;i<vf->links;i++)
764       acc+=ov_time_total(vf,i);
765     return(acc);
766   }else{
767     return((float)(vf->pcmlengths[i])/vf->vi[i].rate);
768   }
769 }
770
771 /* seek to an offset relative to the *compressed* data. This also
772    immediately sucks in and decodes pages to update the PCM cursor. It
773    will cross a logical bitstream boundary, but only if it can't get
774    any packets out of the tail of the bitstream we seek to (so no
775    surprises). 
776
777    returns zero on success, nonzero on failure */
778
779 int ov_raw_seek(OggVorbis_File *vf,long pos){
780   int flag=0;
781   if(!vf->seekable)return(OV_ENOSEEK); /* don't dump machine if we can't seek */
782   if(pos<0 || pos>vf->offsets[vf->links])return(OV_EINVAL);
783
784   /* clear out decoding machine state */
785   vf->pcm_offset=-1;
786   _decode_clear(vf);
787   
788   /* seek */
789   _seek_helper(vf,pos);
790
791   /* we need to make sure the pcm_offset is set.  We use the
792      _fetch_packet helper to process one packet with readp set, then
793      call it until it returns '0' with readp not set (the last packet
794      from a page has the 'granulepos' field set, and that's how the
795      helper updates the offset */
796
797   while(!flag){
798     switch(_process_packet(vf,1)){
799     case 0:case OV_EOF:
800       /* oh, eof. There are no packets remaining.  Set the pcm offset to
801          the end of file */
802       vf->pcm_offset=ov_pcm_total(vf,-1);
803       return(0);
804     case OV_HOLE:
805       break;
806     case OV_EBADLINK:
807       goto seek_error;
808     default:
809       /* all OK */
810       flag=1;
811       break;
812     }
813   }
814   
815   while(1){
816     /* don't have to check each time through for the updated granule;
817        it's always the last complete packet on a page */
818     switch(_process_packet(vf,0)){
819     case 0:case OV_EOF:
820       /* the offset is set unless it's a bogus bitstream with no
821          offset information but that's not our fault.  We still run
822          gracefully, we're just missing the offset */
823       return(0);
824     case OV_EBADLINK:
825       goto seek_error;
826     default:
827       /* continue processing packets */
828       break;
829     }
830   }
831   
832  seek_error:
833   /* dump the machine so we're in a known state */
834   vf->pcm_offset=-1;
835   _decode_clear(vf);
836   return OV_EBADLINK;
837 }
838
839 /* Page granularity seek (faster than sample granularity because we
840    don't do the last bit of decode to find a specific sample).
841
842    Seek to the last [granule marked] page preceeding the specified pos
843    location, such that decoding past the returned point will quickly
844    arrive at the requested position. */
845 int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos){
846   int link=-1;
847   long ret;
848   ogg_int64_t total=ov_pcm_total(vf,-1);
849
850   if(!vf->seekable)return(OV_ENOSEEK);
851   if(pos<0 || pos>total)return(OV_EINVAL);
852
853   /* which bitstream section does this pcm offset occur in? */
854   for(link=vf->links-1;link>=0;link--){
855     total-=vf->pcmlengths[link];
856     if(pos>=total)break;
857   }
858
859   /* search within the logical bitstream for the page with the highest
860      pcm_pos preceeding (or equal to) pos.  There is a danger here;
861      missing pages or incorrect frame number information in the
862      bitstream could make our task impossible.  Account for that (it
863      would be an error condition) */
864   {
865     ogg_int64_t target=pos-total;
866     long end=vf->offsets[link+1];
867     long begin=vf->offsets[link];
868     long best=begin;
869
870     ogg_page og;
871     while(begin<end){
872       long bisect;
873     
874       if(end-begin<CHUNKSIZE){
875         bisect=begin;
876       }else{
877         bisect=(end+begin)/2;
878       }
879     
880       _seek_helper(vf,bisect);
881       ret=_get_next_page(vf,&og,end-bisect);
882       switch(ret){
883       case OV_FALSE: case OV_EOF:
884         end=bisect;
885         break;
886       case OV_EREAD:
887         goto seek_error;
888       default:
889         {
890           ogg_int64_t granulepos=ogg_page_granulepos(&og);
891           if(granulepos<target){
892             best=ret;  /* raw offset of packet with granulepos */ 
893             begin=vf->offset; /* raw offset of next packet */
894           }else{
895             end=bisect;
896           }
897         }
898       }
899     }
900
901     /* found our page. seek to it (call raw_seek). */
902     
903     if((ret=ov_raw_seek(vf,best)))goto seek_error;
904   }
905   
906   /* verify result */
907   if(vf->pcm_offset>=pos || pos>ov_pcm_total(vf,-1)){
908     ret=OV_EFAULT;
909     goto seek_error;
910   }
911   return(0);
912   
913  seek_error:
914   /* dump machine so we're in a known state */
915   vf->pcm_offset=-1;
916   _decode_clear(vf);
917   return ret;
918 }
919
920 /* seek to a sample offset relative to the decompressed pcm stream 
921    returns zero on success, nonzero on failure */
922
923 int ov_pcm_seek(OggVorbis_File *vf,ogg_int64_t pos){
924   int ret=ov_pcm_seek_page(vf,pos);
925   if(ret<0)return(ret);
926   
927   /* discard samples until we reach the desired position. Crossing a
928      logical bitstream boundary with abandon is OK. */
929   while(vf->pcm_offset<pos){
930     float **pcm;
931     long target=pos-vf->pcm_offset;
932     long samples=vorbis_synthesis_pcmout(&vf->vd,&pcm);
933
934     if(samples>target)samples=target;
935     vorbis_synthesis_read(&vf->vd,samples);
936     vf->pcm_offset+=samples;
937     
938     if(samples<target)
939       if(_process_packet(vf,1)==0)
940         vf->pcm_offset=ov_pcm_total(vf,-1); /* eof */
941   }
942   return 0;
943 }
944
945 /* seek to a playback time relative to the decompressed pcm stream 
946    returns zero on success, nonzero on failure */
947 int ov_time_seek(OggVorbis_File *vf,double seconds){
948   /* translate time to PCM position and call ov_pcm_seek */
949
950   int link=-1;
951   ogg_int64_t pcm_total=ov_pcm_total(vf,-1);
952   double time_total=ov_time_total(vf,-1);
953
954   if(!vf->seekable)return(OV_ENOSEEK);
955   if(seconds<0 || seconds>time_total)return(OV_EINVAL);
956   
957   /* which bitstream section does this time offset occur in? */
958   for(link=vf->links-1;link>=0;link--){
959     pcm_total-=vf->pcmlengths[link];
960     time_total-=ov_time_total(vf,link);
961     if(seconds>=time_total)break;
962   }
963
964   /* enough information to convert time offset to pcm offset */
965   {
966     ogg_int64_t target=pcm_total+(seconds-time_total)*vf->vi[link].rate;
967     return(ov_pcm_seek(vf,target));
968   }
969 }
970
971 /* page-granularity version of ov_time_seek 
972    returns zero on success, nonzero on failure */
973 int ov_time_seek_page(OggVorbis_File *vf,double seconds){
974   /* translate time to PCM position and call ov_pcm_seek */
975
976   int link=-1;
977   ogg_int64_t pcm_total=ov_pcm_total(vf,-1);
978   double time_total=ov_time_total(vf,-1);
979
980   if(!vf->seekable)return(OV_ENOSEEK);
981   if(seconds<0 || seconds>time_total)return(OV_EINVAL);
982   
983   /* which bitstream section does this time offset occur in? */
984   for(link=vf->links-1;link>=0;link--){
985     pcm_total-=vf->pcmlengths[link];
986     time_total-=ov_time_total(vf,link);
987     if(seconds>=time_total)break;
988   }
989
990   /* enough information to convert time offset to pcm offset */
991   {
992     ogg_int64_t target=pcm_total+(seconds-time_total)*vf->vi[link].rate;
993     return(ov_pcm_seek_page(vf,target));
994   }
995 }
996
997 /* tell the current stream offset cursor.  Note that seek followed by
998    tell will likely not give the set offset due to caching */
999 ogg_int64_t ov_raw_tell(OggVorbis_File *vf){
1000   return(vf->offset);
1001 }
1002
1003 /* return PCM offset (sample) of next PCM sample to be read */
1004 ogg_int64_t ov_pcm_tell(OggVorbis_File *vf){
1005   return(vf->pcm_offset);
1006 }
1007
1008 /* return time offset (seconds) of next PCM sample to be read */
1009 double ov_time_tell(OggVorbis_File *vf){
1010   /* translate time to PCM position and call ov_pcm_seek */
1011
1012   int link=-1;
1013   ogg_int64_t pcm_total=0;
1014   double time_total=0.f;
1015   
1016   if(vf->seekable){
1017     pcm_total=ov_pcm_total(vf,-1);
1018     time_total=ov_time_total(vf,-1);
1019   
1020     /* which bitstream section does this time offset occur in? */
1021     for(link=vf->links-1;link>=0;link--){
1022       pcm_total-=vf->pcmlengths[link];
1023       time_total-=ov_time_total(vf,link);
1024       if(vf->pcm_offset>=pcm_total)break;
1025     }
1026   }
1027
1028   return((double)time_total+(double)(vf->pcm_offset-pcm_total)/vf->vi[link].rate);
1029 }
1030
1031 /*  link:   -1) return the vorbis_info struct for the bitstream section
1032                 currently being decoded
1033            0-n) to request information for a specific bitstream section
1034     
1035     In the case of a non-seekable bitstream, any call returns the
1036     current bitstream.  NULL in the case that the machine is not
1037     initialized */
1038
1039 vorbis_info *ov_info(OggVorbis_File *vf,int link){
1040   if(vf->seekable){
1041     if(link<0)
1042       if(vf->decode_ready)
1043         return vf->vi+vf->current_link;
1044       else
1045         return NULL;
1046     else
1047       if(link>=vf->links)
1048         return NULL;
1049       else
1050         return vf->vi+link;
1051   }else{
1052     if(vf->decode_ready)
1053       return vf->vi;
1054     else
1055       return NULL;
1056   }
1057 }
1058
1059 /* grr, strong typing, grr, no templates/inheritence, grr */
1060 vorbis_comment *ov_comment(OggVorbis_File *vf,int link){
1061   if(vf->seekable){
1062     if(link<0)
1063       if(vf->decode_ready)
1064         return vf->vc+vf->current_link;
1065       else
1066         return NULL;
1067     else
1068       if(link>=vf->links)
1069         return NULL;
1070       else
1071         return vf->vc+link;
1072   }else{
1073     if(vf->decode_ready)
1074       return vf->vc;
1075     else
1076       return NULL;
1077   }
1078 }
1079
1080 int host_is_big_endian() {
1081   ogg_int32_t pattern = 0xfeedface; /* deadbeef */
1082   unsigned char *bytewise = (unsigned char *)&pattern;
1083   if (bytewise[0] == 0xfe) return 1;
1084   return 0;
1085 }
1086
1087 /* up to this point, everything could more or less hide the multiple
1088    logical bitstream nature of chaining from the toplevel application
1089    if the toplevel application didn't particularly care.  However, at
1090    the point that we actually read audio back, the multiple-section
1091    nature must surface: Multiple bitstream sections do not necessarily
1092    have to have the same number of channels or sampling rate.
1093
1094    ov_read returns the sequential logical bitstream number currently
1095    being decoded along with the PCM data in order that the toplevel
1096    application can take action on channel/sample rate changes.  This
1097    number will be incremented even for streamed (non-seekable) streams
1098    (for seekable streams, it represents the actual logical bitstream
1099    index within the physical bitstream.  Note that the accessor
1100    functions above are aware of this dichotomy).
1101
1102    input values: buffer) a buffer to hold packed PCM data for return
1103                  length) the byte length requested to be placed into buffer
1104                  bigendianp) should the data be packed LSB first (0) or
1105                              MSB first (1)
1106                  word) word size for output.  currently 1 (byte) or 
1107                        2 (16 bit short)
1108
1109    return values: <0) error/hole in data (OV_HOLE)
1110                    0) EOF
1111                    n) number of bytes of PCM actually returned.  The
1112                    below works on a packet-by-packet basis, so the
1113                    return length is not related to the 'length' passed
1114                    in, just guaranteed to fit.
1115
1116             *section) set to the logical bitstream number */
1117
1118 long ov_read(OggVorbis_File *vf,char *buffer,int length,
1119                     int bigendianp,int word,int sgned,int *bitstream){
1120   int i,j;
1121   int host_endian = host_is_big_endian();
1122
1123   while(1){
1124     if(vf->decode_ready){
1125       float **pcm;
1126       long samples=vorbis_synthesis_pcmout(&vf->vd,&pcm);
1127       if(samples){
1128         /* yay! proceed to pack data into the byte buffer */
1129
1130         long channels=ov_info(vf,-1)->channels;
1131         long bytespersample=word * channels;
1132         vorbis_fpu_control fpu;
1133         if(samples>length/bytespersample)samples=length/bytespersample;
1134         
1135         /* a tight loop to pack each size */
1136         {
1137           int val;
1138           if(word==1){
1139             int off=(sgned?0:128);
1140             vorbis_fpu_setround(&fpu);
1141             for(j=0;j<samples;j++)
1142               for(i=0;i<channels;i++){
1143                 val=vorbis_ftoi(pcm[i][j]*128.f);
1144                 if(val>127)val=127;
1145                 else if(val<-128)val=-128;
1146                 *buffer++=val+off;
1147               }
1148             vorbis_fpu_restore(fpu);
1149           }else{
1150             int off=(sgned?0:32768);
1151
1152             if(host_endian==bigendianp){
1153               if(sgned){
1154
1155                 vorbis_fpu_setround(&fpu);
1156                 for(i=0;i<channels;i++) { /* It's faster in this order */
1157                   float *src=pcm[i];
1158                   short *dest=((short *)buffer)+i;
1159                   for(j=0;j<samples;j++) {
1160                     val=vorbis_ftoi(src[j]*32768.f);
1161                     if(val>32767)val=32767;
1162                     else if(val<-32768)val=-32768;
1163                     *dest=val;
1164                     dest+=channels;
1165                   }
1166                 }
1167                 vorbis_fpu_restore(fpu);
1168
1169               }else{
1170
1171                 vorbis_fpu_setround(&fpu);
1172                 for(i=0;i<channels;i++) {
1173                   float *src=pcm[i];
1174                   short *dest=((short *)buffer)+i;
1175                   for(j=0;j<samples;j++) {
1176                     val=vorbis_ftoi(src[j]*32768.f);
1177                     if(val>32767)val=32767;
1178                     else if(val<-32768)val=-32768;
1179                     *dest=val+off;
1180                     dest+=channels;
1181                   }
1182                 }
1183                 vorbis_fpu_restore(fpu);
1184
1185               }
1186             }else if(bigendianp){
1187
1188               vorbis_fpu_setround(&fpu);
1189               for(j=0;j<samples;j++)
1190                 for(i=0;i<channels;i++){
1191                   val=vorbis_ftoi(pcm[i][j]*32768.f);
1192                   if(val>32767)val=32767;
1193                   else if(val<-32768)val=-32768;
1194                   val+=off;
1195                   *buffer++=(val>>8);
1196                   *buffer++=(val&0xff);
1197                 }
1198               vorbis_fpu_restore(fpu);
1199
1200             }else{
1201               int val;
1202               vorbis_fpu_setround(&fpu);
1203               for(j=0;j<samples;j++)
1204                 for(i=0;i<channels;i++){
1205                   val=vorbis_ftoi(pcm[i][j]*32768.f);
1206                   if(val>32767)val=32767;
1207                   else if(val<-32768)val=-32768;
1208                   val+=off;
1209                   *buffer++=(val&0xff);
1210                   *buffer++=(val>>8);
1211                 }
1212               vorbis_fpu_restore(fpu);  
1213
1214             }
1215           }
1216         }
1217         
1218         vorbis_synthesis_read(&vf->vd,samples);
1219         vf->pcm_offset+=samples;
1220         if(bitstream)*bitstream=vf->current_link;
1221         return(samples*bytespersample);
1222       }
1223     }
1224
1225     /* suck in another packet */
1226     switch(_process_packet(vf,1)){
1227     case 0:case OV_EOF:
1228       return(0);
1229     case OV_HOLE:
1230       return(OV_HOLE);
1231     case OV_EBADLINK:
1232       return(OV_EBADLINK);
1233     }
1234   }
1235 }
1236
1237
1238
1239