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