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