831cb39f0bff9c03e2a9d9eb323d39d1b2e2708b
[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 LIBRARY SOURCE IS     *
5  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015             *
9  * by the Xiph.Org Foundation http://www.xiph.org/                  *
10  *                                                                  *
11  ********************************************************************
12
13  function: stdio-based convenience library for opening/seeking/decoding
14
15  ********************************************************************/
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <math.h>
22
23 #include "vorbis/codec.h"
24
25 /* we don't need or want the static callback symbols here */
26 #define OV_EXCLUDE_STATIC_CALLBACKS
27 #include "vorbis/vorbisfile.h"
28
29 #include "os.h"
30 #include "misc.h"
31
32 /* A 'chained bitstream' is a Vorbis bitstream that contains more than
33    one logical bitstream arranged end to end (the only form of Ogg
34    multiplexing allowed in a Vorbis bitstream; grouping [parallel
35    multiplexing] is not allowed in Vorbis) */
36
37 /* A Vorbis file can be played beginning to end (streamed) without
38    worrying ahead of time about chaining (see decoder_example.c).  If
39    we have the whole file, however, and want random access
40    (seeking/scrubbing) or desire to know the total length/time of a
41    file, we need to account for the possibility of chaining. */
42
43 /* We can handle things a number of ways; we can determine the entire
44    bitstream structure right off the bat, or find pieces on demand.
45    This example determines and caches structure for the entire
46    bitstream, but builds a virtual decoder on the fly when moving
47    between links in the chain. */
48
49 /* There are also different ways to implement seeking.  Enough
50    information exists in an Ogg bitstream to seek to
51    sample-granularity positions in the output.  Or, one can seek by
52    picking some portion of the stream roughly in the desired area if
53    we only want coarse navigation through the stream. */
54
55 /*************************************************************************
56  * Many, many internal helpers.  The intention is not to be confusing;
57  * rampant duplication and monolithic function implementation would be
58  * harder to understand anyway.  The high level functions are last.  Begin
59  * grokking near the end of the file */
60
61 /* read a little more data from the file/pipe into the ogg_sync framer
62 */
63 #define CHUNKSIZE 65536 /* greater-than-page-size granularity seeking */
64 #define READSIZE 2048 /* a smaller read size is needed for low-rate streaming. */
65
66 static long _get_data(OggVorbis_File *vf){
67   errno=0;
68   if(!(vf->callbacks.read_func))return(-1);
69   if(vf->datasource){
70     char *buffer=ogg_sync_buffer(&vf->oy,READSIZE);
71     long bytes=(vf->callbacks.read_func)(buffer,1,READSIZE,vf->datasource);
72     if(bytes>0)ogg_sync_wrote(&vf->oy,bytes);
73     if(bytes==0 && errno)return(-1);
74     return(bytes);
75   }else
76     return(0);
77 }
78
79 /* save a tiny smidge of verbosity to make the code more readable */
80 static int _seek_helper(OggVorbis_File *vf,ogg_int64_t offset){
81   if(vf->datasource){
82     /* only seek if the file position isn't already there */
83     if(vf->offset != offset){
84       if(!(vf->callbacks.seek_func)||
85          (vf->callbacks.seek_func)(vf->datasource, offset, SEEK_SET) == -1)
86         return OV_EREAD;
87       vf->offset=offset;
88       ogg_sync_reset(&vf->oy);
89     }
90   }else{
91     /* shouldn't happen unless someone writes a broken callback */
92     return OV_EFAULT;
93   }
94   return 0;
95 }
96
97 /* The read/seek functions track absolute position within the stream */
98
99 /* from the head of the stream, get the next page.  boundary specifies
100    if the function is allowed to fetch more data from the stream (and
101    how much) or only use internally buffered data.
102
103    boundary: -1) unbounded search
104               0) read no additional data; use cached only
105               n) search for a new page beginning for n bytes
106
107    return:   <0) did not find a page (OV_FALSE, OV_EOF, OV_EREAD)
108               n) found a page at absolute offset n */
109
110 static ogg_int64_t _get_next_page(OggVorbis_File *vf,ogg_page *og,
111                                   ogg_int64_t boundary){
112   if(boundary>0)boundary+=vf->offset;
113   while(1){
114     long more;
115
116     if(boundary>0 && vf->offset>=boundary)return(OV_FALSE);
117     more=ogg_sync_pageseek(&vf->oy,og);
118
119     if(more<0){
120       /* skipped n bytes */
121       vf->offset-=more;
122     }else{
123       if(more==0){
124         /* send more paramedics */
125         if(!boundary)return(OV_FALSE);
126         {
127           long ret=_get_data(vf);
128           if(ret==0)return(OV_EOF);
129           if(ret<0)return(OV_EREAD);
130         }
131       }else{
132         /* got a page.  Return the offset at the page beginning,
133            advance the internal offset past the page end */
134         ogg_int64_t ret=vf->offset;
135         vf->offset+=more;
136         return(ret);
137
138       }
139     }
140   }
141 }
142
143 /* find the latest page beginning before the passed in position. Much
144    dirtier than the above as Ogg doesn't have any backward search
145    linkage.  no 'readp' as it will certainly have to read. */
146 /* returns offset or OV_EREAD, OV_FAULT */
147 static ogg_int64_t _get_prev_page(OggVorbis_File *vf,ogg_int64_t begin,ogg_page *og){
148   ogg_int64_t end = begin;
149   ogg_int64_t ret;
150   ogg_int64_t offset=-1;
151
152   while(offset==-1){
153     begin-=CHUNKSIZE;
154     if(begin<0)
155       begin=0;
156
157     ret=_seek_helper(vf,begin);
158     if(ret)return(ret);
159
160     while(vf->offset<end){
161       memset(og,0,sizeof(*og));
162       ret=_get_next_page(vf,og,end-vf->offset);
163       if(ret==OV_EREAD)return(OV_EREAD);
164       if(ret<0){
165         break;
166       }else{
167         offset=ret;
168       }
169     }
170   }
171
172   /* In a fully compliant, non-multiplexed stream, we'll still be
173      holding the last page.  In multiplexed (or noncompliant streams),
174      we will probably have to re-read the last page we saw */
175   if(og->header_len==0){
176     ret=_seek_helper(vf,offset);
177     if(ret)return(ret);
178
179     ret=_get_next_page(vf,og,CHUNKSIZE);
180     if(ret<0)
181       /* this shouldn't be possible */
182       return(OV_EFAULT);
183   }
184
185   return(offset);
186 }
187
188 static void _add_serialno(ogg_page *og,long **serialno_list, int *n){
189   long s = ogg_page_serialno(og);
190   (*n)++;
191
192   if(*serialno_list){
193     *serialno_list = _ogg_realloc(*serialno_list, sizeof(**serialno_list)*(*n));
194   }else{
195     *serialno_list = _ogg_malloc(sizeof(**serialno_list));
196   }
197
198   (*serialno_list)[(*n)-1] = s;
199 }
200
201 /* returns nonzero if found */
202 static int _lookup_serialno(long s, long *serialno_list, int n){
203   if(serialno_list){
204     while(n--){
205       if(*serialno_list == s) return 1;
206       serialno_list++;
207     }
208   }
209   return 0;
210 }
211
212 static int _lookup_page_serialno(ogg_page *og, long *serialno_list, int n){
213   long s = ogg_page_serialno(og);
214   return _lookup_serialno(s,serialno_list,n);
215 }
216
217 /* performs the same search as _get_prev_page, but prefers pages of
218    the specified serial number. If a page of the specified serialno is
219    spotted during the seek-back-and-read-forward, it will return the
220    info of last page of the matching serial number instead of the very
221    last page.  If no page of the specified serialno is seen, it will
222    return the info of last page and alter *serialno.  */
223 static ogg_int64_t _get_prev_page_serial(OggVorbis_File *vf, ogg_int64_t begin,
224                                          long *serial_list, int serial_n,
225                                          int *serialno, ogg_int64_t *granpos){
226   ogg_page og;
227   ogg_int64_t end=begin;
228   ogg_int64_t ret;
229
230   ogg_int64_t prefoffset=-1;
231   ogg_int64_t offset=-1;
232   ogg_int64_t ret_serialno=-1;
233   ogg_int64_t ret_gran=-1;
234
235   while(offset==-1){
236     begin-=CHUNKSIZE;
237     if(begin<0)
238       begin=0;
239
240     ret=_seek_helper(vf,begin);
241     if(ret)return(ret);
242
243     while(vf->offset<end){
244       ret=_get_next_page(vf,&og,end-vf->offset);
245       if(ret==OV_EREAD)return(OV_EREAD);
246       if(ret<0){
247         break;
248       }else{
249         ret_serialno=ogg_page_serialno(&og);
250         ret_gran=ogg_page_granulepos(&og);
251         offset=ret;
252
253         if(ret_serialno == *serialno){
254           prefoffset=ret;
255           *granpos=ret_gran;
256         }
257
258         if(!_lookup_serialno(ret_serialno,serial_list,serial_n)){
259           /* we fell off the end of the link, which means we seeked
260              back too far and shouldn't have been looking in that link
261              to begin with.  If we found the preferred serial number,
262              forget that we saw it. */
263           prefoffset=-1;
264         }
265       }
266     }
267   }
268
269   /* we're not interested in the page... just the serialno and granpos. */
270   if(prefoffset>=0)return(prefoffset);
271
272   *serialno = ret_serialno;
273   *granpos = ret_gran;
274   return(offset);
275
276 }
277
278 /* uses the local ogg_stream storage in vf; this is important for
279    non-streaming input sources */
280 static int _fetch_headers(OggVorbis_File *vf,vorbis_info *vi,vorbis_comment *vc,
281                           long **serialno_list, int *serialno_n,
282                           ogg_page *og_ptr){
283   ogg_page og;
284   ogg_packet op;
285   int i,ret;
286   int allbos=0;
287
288   if(!og_ptr){
289     ogg_int64_t llret=_get_next_page(vf,&og,CHUNKSIZE);
290     if(llret==OV_EREAD)return(OV_EREAD);
291     if(llret<0)return(OV_ENOTVORBIS);
292     og_ptr=&og;
293   }
294
295   vorbis_info_init(vi);
296   vorbis_comment_init(vc);
297   vf->ready_state=OPENED;
298
299   /* extract the serialnos of all BOS pages + the first set of vorbis
300      headers we see in the link */
301
302   while(ogg_page_bos(og_ptr)){
303     if(serialno_list){
304       if(_lookup_page_serialno(og_ptr,*serialno_list,*serialno_n)){
305         /* a dupe serialnumber in an initial header packet set == invalid stream */
306         if(*serialno_list)_ogg_free(*serialno_list);
307         *serialno_list=0;
308         *serialno_n=0;
309         ret=OV_EBADHEADER;
310         goto bail_header;
311       }
312
313       _add_serialno(og_ptr,serialno_list,serialno_n);
314     }
315
316     if(vf->ready_state<STREAMSET){
317       /* we don't have a vorbis stream in this link yet, so begin
318          prospective stream setup. We need a stream to get packets */
319       ogg_stream_reset_serialno(&vf->os,ogg_page_serialno(og_ptr));
320       ogg_stream_pagein(&vf->os,og_ptr);
321
322       if(ogg_stream_packetout(&vf->os,&op) > 0 &&
323          vorbis_synthesis_idheader(&op)){
324         /* vorbis header; continue setup */
325         vf->ready_state=STREAMSET;
326         if((ret=vorbis_synthesis_headerin(vi,vc,&op))){
327           ret=OV_EBADHEADER;
328           goto bail_header;
329         }
330       }
331     }
332
333     /* get next page */
334     {
335       ogg_int64_t llret=_get_next_page(vf,og_ptr,CHUNKSIZE);
336       if(llret==OV_EREAD){
337         ret=OV_EREAD;
338         goto bail_header;
339       }
340       if(llret<0){
341         ret=OV_ENOTVORBIS;
342         goto bail_header;
343       }
344
345       /* if this page also belongs to our vorbis stream, submit it and break */
346       if(vf->ready_state==STREAMSET &&
347          vf->os.serialno == ogg_page_serialno(og_ptr)){
348         ogg_stream_pagein(&vf->os,og_ptr);
349         break;
350       }
351     }
352   }
353
354   if(vf->ready_state!=STREAMSET){
355     ret = OV_ENOTVORBIS;
356     goto bail_header;
357   }
358
359   while(1){
360
361     i=0;
362     while(i<2){ /* get a page loop */
363
364       while(i<2){ /* get a packet loop */
365
366         int result=ogg_stream_packetout(&vf->os,&op);
367         if(result==0)break;
368         if(result==-1){
369           ret=OV_EBADHEADER;
370           goto bail_header;
371         }
372
373         if((ret=vorbis_synthesis_headerin(vi,vc,&op)))
374           goto bail_header;
375
376         i++;
377       }
378
379       while(i<2){
380         if(_get_next_page(vf,og_ptr,CHUNKSIZE)<0){
381           ret=OV_EBADHEADER;
382           goto bail_header;
383         }
384
385         /* if this page belongs to the correct stream, go parse it */
386         if(vf->os.serialno == ogg_page_serialno(og_ptr)){
387           ogg_stream_pagein(&vf->os,og_ptr);
388           break;
389         }
390
391         /* if we never see the final vorbis headers before the link
392            ends, abort */
393         if(ogg_page_bos(og_ptr)){
394           if(allbos){
395             ret = OV_EBADHEADER;
396             goto bail_header;
397           }else
398             allbos=1;
399         }
400
401         /* otherwise, keep looking */
402       }
403     }
404
405     return 0;
406   }
407
408  bail_header:
409   vorbis_info_clear(vi);
410   vorbis_comment_clear(vc);
411   vf->ready_state=OPENED;
412
413   return ret;
414 }
415
416 /* Starting from current cursor position, get initial PCM offset of
417    next page.  Consumes the page in the process without decoding
418    audio, however this is only called during stream parsing upon
419    seekable open. */
420 static ogg_int64_t _initial_pcmoffset(OggVorbis_File *vf, vorbis_info *vi){
421   ogg_page    og;
422   ogg_int64_t accumulated=0;
423   long        lastblock=-1;
424   int         result;
425   int         serialno = vf->os.serialno;
426
427   while(1){
428     ogg_packet op;
429     if(_get_next_page(vf,&og,-1)<0)
430       break; /* should not be possible unless the file is truncated/mangled */
431
432     if(ogg_page_bos(&og)) break;
433     if(ogg_page_serialno(&og)!=serialno) continue;
434
435     /* count blocksizes of all frames in the page */
436     ogg_stream_pagein(&vf->os,&og);
437     while((result=ogg_stream_packetout(&vf->os,&op))){
438       if(result>0){ /* ignore holes */
439         long thisblock=vorbis_packet_blocksize(vi,&op);
440         if(thisblock>=0){
441           if(lastblock!=-1)
442             accumulated+=(lastblock+thisblock)>>2;
443           lastblock=thisblock;
444         }
445       }
446     }
447
448     if(ogg_page_granulepos(&og)!=-1){
449       /* pcm offset of last packet on the first audio page */
450       accumulated= ogg_page_granulepos(&og)-accumulated;
451       break;
452     }
453   }
454
455   /* less than zero?  Either a corrupt file or a stream with samples
456      trimmed off the beginning, a normal occurrence; in both cases set
457      the offset to zero */
458   if(accumulated<0)accumulated=0;
459
460   return accumulated;
461 }
462
463 /* finds each bitstream link one at a time using a bisection search
464    (has to begin by knowing the offset of the lb's initial page).
465    Recurses for each link so it can alloc the link storage after
466    finding them all, then unroll and fill the cache at the same time */
467 static int _bisect_forward_serialno(OggVorbis_File *vf,
468                                     ogg_int64_t begin,
469                                     ogg_int64_t searched,
470                                     ogg_int64_t end,
471                                     ogg_int64_t endgran,
472                                     int endserial,
473                                     long *currentno_list,
474                                     int  currentnos,
475                                     long m){
476   ogg_int64_t pcmoffset;
477   ogg_int64_t dataoffset=searched;
478   ogg_int64_t endsearched=end;
479   ogg_int64_t next=end;
480   ogg_int64_t searchgran=-1;
481   ogg_page og;
482   ogg_int64_t ret,last;
483   int serialno = vf->os.serialno;
484
485   /* invariants:
486      we have the headers and serialnos for the link beginning at 'begin'
487      we have the offset and granpos of the last page in the file (potentially
488        not a page we care about)
489   */
490
491   /* Is the last page in our list of current serialnumbers? */
492   if(_lookup_serialno(endserial,currentno_list,currentnos)){
493
494     /* last page is in the starting serialno list, so we've bisected
495        down to (or just started with) a single link.  Now we need to
496        find the last vorbis page belonging to the first vorbis stream
497        for this link. */
498     searched = end;
499     while(endserial != serialno){
500       endserial = serialno;
501       searched=_get_prev_page_serial(vf,searched,currentno_list,currentnos,&endserial,&endgran);
502     }
503
504     vf->links=m+1;
505     if(vf->offsets)_ogg_free(vf->offsets);
506     if(vf->serialnos)_ogg_free(vf->serialnos);
507     if(vf->dataoffsets)_ogg_free(vf->dataoffsets);
508
509     vf->offsets=_ogg_malloc((vf->links+1)*sizeof(*vf->offsets));
510     vf->vi=_ogg_realloc(vf->vi,vf->links*sizeof(*vf->vi));
511     vf->vc=_ogg_realloc(vf->vc,vf->links*sizeof(*vf->vc));
512     vf->serialnos=_ogg_malloc(vf->links*sizeof(*vf->serialnos));
513     vf->dataoffsets=_ogg_malloc(vf->links*sizeof(*vf->dataoffsets));
514     vf->pcmlengths=_ogg_malloc(vf->links*2*sizeof(*vf->pcmlengths));
515
516     vf->offsets[m+1]=end;
517     vf->offsets[m]=begin;
518     vf->pcmlengths[m*2+1]=(endgran<0?0:endgran);
519
520   }else{
521
522     /* last page is not in the starting stream's serial number list,
523        so we have multiple links.  Find where the stream that begins
524        our bisection ends. */
525
526     long *next_serialno_list=NULL;
527     int next_serialnos=0;
528     vorbis_info vi;
529     vorbis_comment vc;
530     int testserial = serialno+1;
531
532     /* the below guards against garbage seperating the last and
533        first pages of two links. */
534     while(searched<endsearched){
535       ogg_int64_t bisect;
536
537       if(endsearched-searched<CHUNKSIZE){
538         bisect=searched;
539       }else{
540         bisect=(searched+endsearched)/2;
541       }
542
543       ret=_seek_helper(vf,bisect);
544       if(ret)return(ret);
545
546       last=_get_next_page(vf,&og,-1);
547       if(last==OV_EREAD)return(OV_EREAD);
548       if(last<0 || !_lookup_page_serialno(&og,currentno_list,currentnos)){
549         endsearched=bisect;
550         if(last>=0)next=last;
551       }else{
552         searched=vf->offset;
553       }
554     }
555
556     /* Bisection point found */
557     /* for the time being, fetch end PCM offset the simple way */
558     searched = next;
559     while(testserial != serialno){
560       testserial = serialno;
561       searched = _get_prev_page_serial(vf,searched,currentno_list,currentnos,&testserial,&searchgran);
562     }
563
564     ret=_seek_helper(vf,next);
565     if(ret)return(ret);
566
567     ret=_fetch_headers(vf,&vi,&vc,&next_serialno_list,&next_serialnos,NULL);
568     if(ret)return(ret);
569     serialno = vf->os.serialno;
570     dataoffset = vf->offset;
571
572     /* this will consume a page, however the next bisection always
573        starts with a raw seek */
574     pcmoffset = _initial_pcmoffset(vf,&vi);
575
576     ret=_bisect_forward_serialno(vf,next,vf->offset,end,endgran,endserial,
577                                  next_serialno_list,next_serialnos,m+1);
578     if(ret)return(ret);
579
580     if(next_serialno_list)_ogg_free(next_serialno_list);
581
582     vf->offsets[m+1]=next;
583     vf->serialnos[m+1]=serialno;
584     vf->dataoffsets[m+1]=dataoffset;
585
586     vf->vi[m+1]=vi;
587     vf->vc[m+1]=vc;
588
589     vf->pcmlengths[m*2+1]=searchgran;
590     vf->pcmlengths[m*2+2]=pcmoffset;
591     vf->pcmlengths[m*2+3]-=pcmoffset;
592     if(vf->pcmlengths[m*2+3]<0)vf->pcmlengths[m*2+3]=0;
593   }
594   return(0);
595 }
596
597 static int _make_decode_ready(OggVorbis_File *vf){
598   if(vf->ready_state>STREAMSET)return 0;
599   if(vf->ready_state<STREAMSET)return OV_EFAULT;
600   if(vf->seekable){
601     if(vorbis_synthesis_init(&vf->vd,vf->vi+vf->current_link))
602       return OV_EBADLINK;
603   }else{
604     if(vorbis_synthesis_init(&vf->vd,vf->vi))
605       return OV_EBADLINK;
606   }
607   vorbis_block_init(&vf->vd,&vf->vb);
608   vf->ready_state=INITSET;
609   vf->bittrack=0.f;
610   vf->samptrack=0.f;
611   return 0;
612 }
613
614 static int _open_seekable2(OggVorbis_File *vf){
615   ogg_int64_t dataoffset=vf->dataoffsets[0],end,endgran=-1;
616   int endserial=vf->os.serialno;
617   int serialno=vf->os.serialno;
618
619   /* we're partially open and have a first link header state in
620      storage in vf */
621
622   /* fetch initial PCM offset */
623   ogg_int64_t pcmoffset = _initial_pcmoffset(vf,vf->vi);
624
625   /* we can seek, so set out learning all about this file */
626   if(vf->callbacks.seek_func && vf->callbacks.tell_func){
627     (vf->callbacks.seek_func)(vf->datasource,0,SEEK_END);
628     vf->offset=vf->end=(vf->callbacks.tell_func)(vf->datasource);
629   }else{
630     vf->offset=vf->end=-1;
631   }
632
633   /* If seek_func is implemented, tell_func must also be implemented */
634   if(vf->end==-1) return(OV_EINVAL);
635
636   /* Get the offset of the last page of the physical bitstream, or, if
637      we're lucky the last vorbis page of this link as most OggVorbis
638      files will contain a single logical bitstream */
639   end=_get_prev_page_serial(vf,vf->end,vf->serialnos+2,vf->serialnos[1],&endserial,&endgran);
640   if(end<0)return(end);
641
642   /* now determine bitstream structure recursively */
643   if(_bisect_forward_serialno(vf,0,dataoffset,end,endgran,endserial,
644                               vf->serialnos+2,vf->serialnos[1],0)<0)return(OV_EREAD);
645
646   vf->offsets[0]=0;
647   vf->serialnos[0]=serialno;
648   vf->dataoffsets[0]=dataoffset;
649   vf->pcmlengths[0]=pcmoffset;
650   vf->pcmlengths[1]-=pcmoffset;
651   if(vf->pcmlengths[1]<0)vf->pcmlengths[1]=0;
652
653   return(ov_raw_seek(vf,dataoffset));
654 }
655
656 /* clear out the current logical bitstream decoder */
657 static void _decode_clear(OggVorbis_File *vf){
658   vorbis_dsp_clear(&vf->vd);
659   vorbis_block_clear(&vf->vb);
660   vf->ready_state=OPENED;
661 }
662
663 /* fetch and process a packet.  Handles the case where we're at a
664    bitstream boundary and dumps the decoding machine.  If the decoding
665    machine is unloaded, it loads it.  It also keeps pcm_offset up to
666    date (seek and read both use this.  seek uses a special hack with
667    readp).
668
669    return: <0) error, OV_HOLE (lost packet) or OV_EOF
670             0) need more data (only if readp==0)
671             1) got a packet
672 */
673
674 static int _fetch_and_process_packet(OggVorbis_File *vf,
675                                      ogg_packet *op_in,
676                                      int readp,
677                                      int spanp){
678   ogg_page og;
679
680   /* handle one packet.  Try to fetch it from current stream state */
681   /* extract packets from page */
682   while(1){
683
684     if(vf->ready_state==STREAMSET){
685       int ret=_make_decode_ready(vf);
686       if(ret<0)return ret;
687     }
688
689     /* process a packet if we can. */
690
691     if(vf->ready_state==INITSET){
692       int hs=vorbis_synthesis_halfrate_p(vf->vi);
693
694       while(1) {
695               ogg_packet op;
696               ogg_packet *op_ptr=(op_in?op_in:&op);
697         int result=ogg_stream_packetout(&vf->os,op_ptr);
698         ogg_int64_t granulepos;
699
700         op_in=NULL;
701         if(result==-1)return(OV_HOLE); /* hole in the data. */
702         if(result>0){
703           /* got a packet.  process it */
704           granulepos=op_ptr->granulepos;
705           if(!vorbis_synthesis(&vf->vb,op_ptr)){ /* lazy check for lazy
706                                                     header handling.  The
707                                                     header packets aren't
708                                                     audio, so if/when we
709                                                     submit them,
710                                                     vorbis_synthesis will
711                                                     reject them */
712
713             /* suck in the synthesis data and track bitrate */
714             {
715               int oldsamples=vorbis_synthesis_pcmout(&vf->vd,NULL);
716               /* for proper use of libvorbis within libvorbisfile,
717                  oldsamples will always be zero. */
718               if(oldsamples)return(OV_EFAULT);
719
720               vorbis_synthesis_blockin(&vf->vd,&vf->vb);
721               vf->samptrack+=(vorbis_synthesis_pcmout(&vf->vd,NULL)<<hs);
722               vf->bittrack+=op_ptr->bytes*8;
723             }
724
725             /* update the pcm offset. */
726             if(granulepos!=-1 && !op_ptr->e_o_s){
727               int link=(vf->seekable?vf->current_link:0);
728               int i,samples;
729
730               /* this packet has a pcm_offset on it (the last packet
731                  completed on a page carries the offset) After processing
732                  (above), we know the pcm position of the *last* sample
733                  ready to be returned. Find the offset of the *first*
734
735                  As an aside, this trick is inaccurate if we begin
736                  reading anew right at the last page; the end-of-stream
737                  granulepos declares the last frame in the stream, and the
738                  last packet of the last page may be a partial frame.
739                  So, we need a previous granulepos from an in-sequence page
740                  to have a reference point.  Thus the !op_ptr->e_o_s clause
741                  above */
742
743               if(vf->seekable && link>0)
744                 granulepos-=vf->pcmlengths[link*2];
745               if(granulepos<0)granulepos=0; /* actually, this
746                                                shouldn't be possible
747                                                here unless the stream
748                                                is very broken */
749
750               samples=(vorbis_synthesis_pcmout(&vf->vd,NULL)<<hs);
751
752               granulepos-=samples;
753               for(i=0;i<link;i++)
754                 granulepos+=vf->pcmlengths[i*2+1];
755               vf->pcm_offset=granulepos;
756             }
757             return(1);
758           }
759         }
760         else
761           break;
762       }
763     }
764
765     if(vf->ready_state>=OPENED){
766       ogg_int64_t ret;
767
768       while(1){
769         /* the loop is not strictly necessary, but there's no sense in
770            doing the extra checks of the larger loop for the common
771            case in a multiplexed bistream where the page is simply
772            part of a different logical bitstream; keep reading until
773            we get one with the correct serialno */
774
775         if(!readp)return(0);
776         if((ret=_get_next_page(vf,&og,-1))<0){
777           return(OV_EOF); /* eof. leave unitialized */
778         }
779
780         /* bitrate tracking; add the header's bytes here, the body bytes
781            are done by packet above */
782         vf->bittrack+=og.header_len*8;
783
784         if(vf->ready_state==INITSET){
785           if(vf->current_serialno!=ogg_page_serialno(&og)){
786
787             /* two possibilities:
788                1) our decoding just traversed a bitstream boundary
789                2) another stream is multiplexed into this logical section */
790
791             if(ogg_page_bos(&og)){
792               /* boundary case */
793               if(!spanp)
794                 return(OV_EOF);
795
796               _decode_clear(vf);
797
798               if(!vf->seekable){
799                 vorbis_info_clear(vf->vi);
800                 vorbis_comment_clear(vf->vc);
801               }
802               break;
803
804             }else
805               continue; /* possibility #2 */
806           }
807         }
808
809         break;
810       }
811     }
812
813     /* Do we need to load a new machine before submitting the page? */
814     /* This is different in the seekable and non-seekable cases.
815
816        In the seekable case, we already have all the header
817        information loaded and cached; we just initialize the machine
818        with it and continue on our merry way.
819
820        In the non-seekable (streaming) case, we'll only be at a
821        boundary if we just left the previous logical bitstream and
822        we're now nominally at the header of the next bitstream
823     */
824
825     if(vf->ready_state!=INITSET){
826       int link;
827
828       if(vf->ready_state<STREAMSET){
829         if(vf->seekable){
830           long serialno = ogg_page_serialno(&og);
831
832           /* match the serialno to bitstream section.  We use this rather than
833              offset positions to avoid problems near logical bitstream
834              boundaries */
835
836           for(link=0;link<vf->links;link++)
837             if(vf->serialnos[link]==serialno)break;
838
839           if(link==vf->links) continue; /* not the desired Vorbis
840                                            bitstream section; keep
841                                            trying */
842
843           vf->current_serialno=serialno;
844           vf->current_link=link;
845
846           ogg_stream_reset_serialno(&vf->os,vf->current_serialno);
847           vf->ready_state=STREAMSET;
848
849         }else{
850           /* we're streaming */
851           /* fetch the three header packets, build the info struct */
852
853           int ret=_fetch_headers(vf,vf->vi,vf->vc,NULL,NULL,&og);
854           if(ret)return(ret);
855           vf->current_serialno=vf->os.serialno;
856           vf->current_link++;
857           link=0;
858         }
859       }
860     }
861
862     /* the buffered page is the data we want, and we're ready for it;
863        add it to the stream state */
864     ogg_stream_pagein(&vf->os,&og);
865
866   }
867 }
868
869 /* if, eg, 64 bit stdio is configured by default, this will build with
870    fseek64 */
871 static int _fseek64_wrap(FILE *f,ogg_int64_t off,int whence){
872   if(f==NULL)return(-1);
873   return fseek(f,off,whence);
874 }
875
876 static int _ov_open1(void *f,OggVorbis_File *vf,const char *initial,
877                      long ibytes, ov_callbacks callbacks){
878   int offsettest=((f && callbacks.seek_func)?callbacks.seek_func(f,0,SEEK_CUR):-1);
879   long *serialno_list=NULL;
880   int serialno_list_size=0;
881   int ret;
882
883   memset(vf,0,sizeof(*vf));
884   vf->datasource=f;
885   vf->callbacks = callbacks;
886
887   /* init the framing state */
888   ogg_sync_init(&vf->oy);
889
890   /* perhaps some data was previously read into a buffer for testing
891      against other stream types.  Allow initialization from this
892      previously read data (especially as we may be reading from a
893      non-seekable stream) */
894   if(initial){
895     char *buffer=ogg_sync_buffer(&vf->oy,ibytes);
896     memcpy(buffer,initial,ibytes);
897     ogg_sync_wrote(&vf->oy,ibytes);
898   }
899
900   /* can we seek? Stevens suggests the seek test was portable */
901   if(offsettest!=-1)vf->seekable=1;
902
903   /* No seeking yet; Set up a 'single' (current) logical bitstream
904      entry for partial open */
905   vf->links=1;
906   vf->vi=_ogg_calloc(vf->links,sizeof(*vf->vi));
907   vf->vc=_ogg_calloc(vf->links,sizeof(*vf->vc));
908   ogg_stream_init(&vf->os,-1); /* fill in the serialno later */
909
910   /* Fetch all BOS pages, store the vorbis header and all seen serial
911      numbers, load subsequent vorbis setup headers */
912   if((ret=_fetch_headers(vf,vf->vi,vf->vc,&serialno_list,&serialno_list_size,NULL))<0){
913     vf->datasource=NULL;
914     ov_clear(vf);
915   }else{
916     /* serial number list for first link needs to be held somewhere
917        for second stage of seekable stream open; this saves having to
918        seek/reread first link's serialnumber data then. */
919     vf->serialnos=_ogg_calloc(serialno_list_size+2,sizeof(*vf->serialnos));
920     vf->serialnos[0]=vf->current_serialno=vf->os.serialno;
921     vf->serialnos[1]=serialno_list_size;
922     memcpy(vf->serialnos+2,serialno_list,serialno_list_size*sizeof(*vf->serialnos));
923
924     vf->offsets=_ogg_calloc(1,sizeof(*vf->offsets));
925     vf->dataoffsets=_ogg_calloc(1,sizeof(*vf->dataoffsets));
926     vf->offsets[0]=0;
927     vf->dataoffsets[0]=vf->offset;
928
929     vf->ready_state=PARTOPEN;
930   }
931   if(serialno_list)_ogg_free(serialno_list);
932   return(ret);
933 }
934
935 static int _ov_open2(OggVorbis_File *vf){
936   if(vf->ready_state != PARTOPEN) return OV_EINVAL;
937   vf->ready_state=OPENED;
938   if(vf->seekable){
939     int ret=_open_seekable2(vf);
940     if(ret){
941       vf->datasource=NULL;
942       ov_clear(vf);
943     }
944     return(ret);
945   }else
946     vf->ready_state=STREAMSET;
947
948   return 0;
949 }
950
951
952 /* clear out the OggVorbis_File struct */
953 int ov_clear(OggVorbis_File *vf){
954   if(vf){
955     vorbis_block_clear(&vf->vb);
956     vorbis_dsp_clear(&vf->vd);
957     ogg_stream_clear(&vf->os);
958
959     if(vf->vi && vf->links){
960       int i;
961       for(i=0;i<vf->links;i++){
962         vorbis_info_clear(vf->vi+i);
963         vorbis_comment_clear(vf->vc+i);
964       }
965       _ogg_free(vf->vi);
966       _ogg_free(vf->vc);
967     }
968     if(vf->dataoffsets)_ogg_free(vf->dataoffsets);
969     if(vf->pcmlengths)_ogg_free(vf->pcmlengths);
970     if(vf->serialnos)_ogg_free(vf->serialnos);
971     if(vf->offsets)_ogg_free(vf->offsets);
972     ogg_sync_clear(&vf->oy);
973     if(vf->datasource && vf->callbacks.close_func)
974       (vf->callbacks.close_func)(vf->datasource);
975     memset(vf,0,sizeof(*vf));
976   }
977 #ifdef DEBUG_LEAKS
978   _VDBG_dump();
979 #endif
980   return(0);
981 }
982
983 /* inspects the OggVorbis file and finds/documents all the logical
984    bitstreams contained in it.  Tries to be tolerant of logical
985    bitstream sections that are truncated/woogie.
986
987    return: -1) error
988             0) OK
989 */
990
991 int ov_open_callbacks(void *f,OggVorbis_File *vf,
992     const char *initial,long ibytes,ov_callbacks callbacks){
993   int ret=_ov_open1(f,vf,initial,ibytes,callbacks);
994   if(ret)return ret;
995   return _ov_open2(vf);
996 }
997
998 int ov_open(FILE *f,OggVorbis_File *vf,const char *initial,long ibytes){
999   ov_callbacks callbacks = {
1000     (size_t (*)(void *, size_t, size_t, void *))  fread,
1001     (int (*)(void *, ogg_int64_t, int))              _fseek64_wrap,
1002     (int (*)(void *))                             fclose,
1003     (long (*)(void *))                            ftell
1004   };
1005
1006   return ov_open_callbacks((void *)f, vf, initial, ibytes, callbacks);
1007 }
1008
1009 int ov_fopen(const char *path,OggVorbis_File *vf){
1010   int ret;
1011   FILE *f = fopen(path,"rb");
1012   if(!f) return -1;
1013
1014   ret = ov_open(f,vf,NULL,0);
1015   if(ret) fclose(f);
1016   return ret;
1017 }
1018
1019
1020 /* cheap hack for game usage where downsampling is desirable; there's
1021    no need for SRC as we can just do it cheaply in libvorbis. */
1022
1023 int ov_halfrate(OggVorbis_File *vf,int flag){
1024   int i;
1025   if(vf->vi==NULL)return OV_EINVAL;
1026   if(vf->ready_state>STREAMSET){
1027     /* clear out stream state; dumping the decode machine is needed to
1028        reinit the MDCT lookups. */
1029     vorbis_dsp_clear(&vf->vd);
1030     vorbis_block_clear(&vf->vb);
1031     vf->ready_state=STREAMSET;
1032     if(vf->pcm_offset>=0){
1033       ogg_int64_t pos=vf->pcm_offset;
1034       vf->pcm_offset=-1; /* make sure the pos is dumped if unseekable */
1035       ov_pcm_seek(vf,pos);
1036     }
1037   }
1038
1039   for(i=0;i<vf->links;i++){
1040     if(vorbis_synthesis_halfrate(vf->vi+i,flag)){
1041       if(flag) ov_halfrate(vf,0);
1042       return OV_EINVAL;
1043     }
1044   }
1045   return 0;
1046 }
1047
1048 int ov_halfrate_p(OggVorbis_File *vf){
1049   if(vf->vi==NULL)return OV_EINVAL;
1050   return vorbis_synthesis_halfrate_p(vf->vi);
1051 }
1052
1053 /* Only partially open the vorbis file; test for Vorbisness, and load
1054    the headers for the first chain.  Do not seek (although test for
1055    seekability).  Use ov_test_open to finish opening the file, else
1056    ov_clear to close/free it. Same return codes as open.
1057
1058    Note that vorbisfile does _not_ take ownership of the file if the
1059    call fails; the calling applicaiton is responsible for closing the file
1060    if this call returns an error. */
1061
1062 int ov_test_callbacks(void *f,OggVorbis_File *vf,
1063     const char *initial,long ibytes,ov_callbacks callbacks)
1064 {
1065   return _ov_open1(f,vf,initial,ibytes,callbacks);
1066 }
1067
1068 int ov_test(FILE *f,OggVorbis_File *vf,const char *initial,long ibytes){
1069   ov_callbacks callbacks = {
1070     (size_t (*)(void *, size_t, size_t, void *))  fread,
1071     (int (*)(void *, ogg_int64_t, int))              _fseek64_wrap,
1072     (int (*)(void *))                             fclose,
1073     (long (*)(void *))                            ftell
1074   };
1075
1076   return ov_test_callbacks((void *)f, vf, initial, ibytes, callbacks);
1077 }
1078
1079 int ov_test_open(OggVorbis_File *vf){
1080   if(vf->ready_state!=PARTOPEN)return(OV_EINVAL);
1081   return _ov_open2(vf);
1082 }
1083
1084 /* How many logical bitstreams in this physical bitstream? */
1085 long ov_streams(OggVorbis_File *vf){
1086   return vf->links;
1087 }
1088
1089 /* Is the FILE * associated with vf seekable? */
1090 long ov_seekable(OggVorbis_File *vf){
1091   return vf->seekable;
1092 }
1093
1094 /* returns the bitrate for a given logical bitstream or the entire
1095    physical bitstream.  If the file is open for random access, it will
1096    find the *actual* average bitrate.  If the file is streaming, it
1097    returns the nominal bitrate (if set) else the average of the
1098    upper/lower bounds (if set) else -1 (unset).
1099
1100    If you want the actual bitrate field settings, get them from the
1101    vorbis_info structs */
1102
1103 long ov_bitrate(OggVorbis_File *vf,int i){
1104   if(vf->ready_state<OPENED)return(OV_EINVAL);
1105   if(i>=vf->links)return(OV_EINVAL);
1106   if(!vf->seekable && i!=0)return(ov_bitrate(vf,0));
1107   if(i<0){
1108     ogg_int64_t bits=0;
1109     int i;
1110     float br;
1111     for(i=0;i<vf->links;i++)
1112       bits+=(vf->offsets[i+1]-vf->dataoffsets[i])*8;
1113     /* This once read: return(rint(bits/ov_time_total(vf,-1)));
1114      * gcc 3.x on x86 miscompiled this at optimisation level 2 and above,
1115      * so this is slightly transformed to make it work.
1116      */
1117     br = bits/ov_time_total(vf,-1);
1118     return(rint(br));
1119   }else{
1120     if(vf->seekable){
1121       /* return the actual bitrate */
1122       return(rint((vf->offsets[i+1]-vf->dataoffsets[i])*8/ov_time_total(vf,i)));
1123     }else{
1124       /* return nominal if set */
1125       if(vf->vi[i].bitrate_nominal>0){
1126         return vf->vi[i].bitrate_nominal;
1127       }else{
1128         if(vf->vi[i].bitrate_upper>0){
1129           if(vf->vi[i].bitrate_lower>0){
1130             return (vf->vi[i].bitrate_upper+vf->vi[i].bitrate_lower)/2;
1131           }else{
1132             return vf->vi[i].bitrate_upper;
1133           }
1134         }
1135         return(OV_FALSE);
1136       }
1137     }
1138   }
1139 }
1140
1141 /* returns the actual bitrate since last call.  returns -1 if no
1142    additional data to offer since last call (or at beginning of stream),
1143    EINVAL if stream is only partially open
1144 */
1145 long ov_bitrate_instant(OggVorbis_File *vf){
1146   int link=(vf->seekable?vf->current_link:0);
1147   long ret;
1148   if(vf->ready_state<OPENED)return(OV_EINVAL);
1149   if(vf->samptrack==0)return(OV_FALSE);
1150   ret=vf->bittrack/vf->samptrack*vf->vi[link].rate+.5;
1151   vf->bittrack=0.f;
1152   vf->samptrack=0.f;
1153   return(ret);
1154 }
1155
1156 /* Guess */
1157 long ov_serialnumber(OggVorbis_File *vf,int i){
1158   if(i>=vf->links)return(ov_serialnumber(vf,vf->links-1));
1159   if(!vf->seekable && i>=0)return(ov_serialnumber(vf,-1));
1160   if(i<0){
1161     return(vf->current_serialno);
1162   }else{
1163     return(vf->serialnos[i]);
1164   }
1165 }
1166
1167 /* returns: total raw (compressed) length of content if i==-1
1168             raw (compressed) length of that logical bitstream for i==0 to n
1169             OV_EINVAL if the stream is not seekable (we can't know the length)
1170             or if stream is only partially open
1171 */
1172 ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i){
1173   if(vf->ready_state<OPENED)return(OV_EINVAL);
1174   if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
1175   if(i<0){
1176     ogg_int64_t acc=0;
1177     int i;
1178     for(i=0;i<vf->links;i++)
1179       acc+=ov_raw_total(vf,i);
1180     return(acc);
1181   }else{
1182     return(vf->offsets[i+1]-vf->offsets[i]);
1183   }
1184 }
1185
1186 /* returns: total PCM length (samples) of content if i==-1 PCM length
1187             (samples) of that logical bitstream for i==0 to n
1188             OV_EINVAL if the stream is not seekable (we can't know the
1189             length) or only partially open
1190 */
1191 ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i){
1192   if(vf->ready_state<OPENED)return(OV_EINVAL);
1193   if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
1194   if(i<0){
1195     ogg_int64_t acc=0;
1196     int i;
1197     for(i=0;i<vf->links;i++)
1198       acc+=ov_pcm_total(vf,i);
1199     return(acc);
1200   }else{
1201     return(vf->pcmlengths[i*2+1]);
1202   }
1203 }
1204
1205 /* returns: total seconds of content if i==-1
1206             seconds in that logical bitstream for i==0 to n
1207             OV_EINVAL if the stream is not seekable (we can't know the
1208             length) or only partially open
1209 */
1210 double ov_time_total(OggVorbis_File *vf,int i){
1211   if(vf->ready_state<OPENED)return(OV_EINVAL);
1212   if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
1213   if(i<0){
1214     double acc=0;
1215     int i;
1216     for(i=0;i<vf->links;i++)
1217       acc+=ov_time_total(vf,i);
1218     return(acc);
1219   }else{
1220     return((double)(vf->pcmlengths[i*2+1])/vf->vi[i].rate);
1221   }
1222 }
1223
1224 /* seek to an offset relative to the *compressed* data. This also
1225    scans packets to update the PCM cursor. It will cross a logical
1226    bitstream boundary, but only if it can't get any packets out of the
1227    tail of the bitstream we seek to (so no surprises).
1228
1229    returns zero on success, nonzero on failure */
1230
1231 int ov_raw_seek(OggVorbis_File *vf,ogg_int64_t pos){
1232   ogg_stream_state work_os;
1233
1234   if(vf->ready_state<OPENED)return(OV_EINVAL);
1235   if(!vf->seekable)
1236     return(OV_ENOSEEK); /* don't dump machine if we can't seek */
1237
1238   if(pos<0 || pos>vf->end)return(OV_EINVAL);
1239
1240   /* is the seek position outside our current link [if any]? */
1241   if(vf->ready_state>=STREAMSET){
1242     if(pos<vf->offsets[vf->current_link] || pos>=vf->offsets[vf->current_link+1])
1243       _decode_clear(vf); /* clear out stream state */
1244   }
1245
1246   /* don't yet clear out decoding machine (if it's initialized), in
1247      the case we're in the same link.  Restart the decode lapping, and
1248      let _fetch_and_process_packet deal with a potential bitstream
1249      boundary */
1250   vf->pcm_offset=-1;
1251   ogg_stream_reset_serialno(&vf->os,
1252                             vf->current_serialno); /* must set serialno */
1253   vorbis_synthesis_restart(&vf->vd);
1254
1255   if(_seek_helper(vf,pos)) {
1256     /* dump the machine so we're in a known state */
1257     vf->pcm_offset=-1;
1258     _decode_clear(vf);
1259     return OV_EBADLINK;
1260   }
1261
1262   /* we need to make sure the pcm_offset is set, but we don't want to
1263      advance the raw cursor past good packets just to get to the first
1264      with a granulepos.  That's not equivalent behavior to beginning
1265      decoding as immediately after the seek position as possible.
1266
1267      So, a hack.  We use two stream states; a local scratch state and
1268      the shared vf->os stream state.  We use the local state to
1269      scan, and the shared state as a buffer for later decode.
1270
1271      Unfortuantely, on the last page we still advance to last packet
1272      because the granulepos on the last page is not necessarily on a
1273      packet boundary, and we need to make sure the granpos is
1274      correct.
1275   */
1276
1277   {
1278     ogg_page og;
1279     ogg_packet op;
1280     int lastblock=0;
1281     int accblock=0;
1282     int thisblock=0;
1283     int lastflag=0;
1284     int firstflag=0;
1285     ogg_int64_t pagepos=-1;
1286
1287     ogg_stream_init(&work_os,vf->current_serialno); /* get the memory ready */
1288     ogg_stream_reset(&work_os); /* eliminate the spurious OV_HOLE
1289                                    return from not necessarily
1290                                    starting from the beginning */
1291
1292     while(1){
1293       if(vf->ready_state>=STREAMSET){
1294         /* snarf/scan a packet if we can */
1295         int result=ogg_stream_packetout(&work_os,&op);
1296
1297         if(result>0){
1298
1299           if(vf->vi[vf->current_link].codec_setup){
1300             thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op);
1301             if(thisblock<0){
1302               ogg_stream_packetout(&vf->os,NULL);
1303               thisblock=0;
1304             }else{
1305
1306               /* We can't get a guaranteed correct pcm position out of the
1307                  last page in a stream because it might have a 'short'
1308                  granpos, which can only be detected in the presence of a
1309                  preceding page.  However, if the last page is also the first
1310                  page, the granpos rules of a first page take precedence.  Not
1311                  only that, but for first==last, the EOS page must be treated
1312                  as if its a normal first page for the stream to open/play. */
1313               if(lastflag && !firstflag)
1314                 ogg_stream_packetout(&vf->os,NULL);
1315               else
1316                 if(lastblock)accblock+=(lastblock+thisblock)>>2;
1317             }
1318
1319             if(op.granulepos!=-1){
1320               int i,link=vf->current_link;
1321               ogg_int64_t granulepos=op.granulepos-vf->pcmlengths[link*2];
1322               if(granulepos<0)granulepos=0;
1323
1324               for(i=0;i<link;i++)
1325                 granulepos+=vf->pcmlengths[i*2+1];
1326               vf->pcm_offset=granulepos-accblock;
1327               if(vf->pcm_offset<0)vf->pcm_offset=0;
1328               break;
1329             }
1330             lastblock=thisblock;
1331             continue;
1332           }else
1333             ogg_stream_packetout(&vf->os,NULL);
1334         }
1335       }
1336
1337       if(!lastblock){
1338         pagepos=_get_next_page(vf,&og,-1);
1339         if(pagepos<0){
1340           vf->pcm_offset=ov_pcm_total(vf,-1);
1341           break;
1342         }
1343       }else{
1344         /* huh?  Bogus stream with packets but no granulepos */
1345         vf->pcm_offset=-1;
1346         break;
1347       }
1348
1349       /* has our decoding just traversed a bitstream boundary? */
1350       if(vf->ready_state>=STREAMSET){
1351         if(vf->current_serialno!=ogg_page_serialno(&og)){
1352
1353           /* two possibilities:
1354              1) our decoding just traversed a bitstream boundary
1355              2) another stream is multiplexed into this logical section? */
1356
1357           if(ogg_page_bos(&og)){
1358             /* we traversed */
1359             _decode_clear(vf); /* clear out stream state */
1360             ogg_stream_clear(&work_os);
1361           } /* else, do nothing; next loop will scoop another page */
1362         }
1363       }
1364
1365       if(vf->ready_state<STREAMSET){
1366         int link;
1367         long serialno = ogg_page_serialno(&og);
1368
1369         for(link=0;link<vf->links;link++)
1370           if(vf->serialnos[link]==serialno)break;
1371
1372         if(link==vf->links) continue; /* not the desired Vorbis
1373                                          bitstream section; keep
1374                                          trying */
1375         vf->current_link=link;
1376         vf->current_serialno=serialno;
1377         ogg_stream_reset_serialno(&vf->os,serialno);
1378         ogg_stream_reset_serialno(&work_os,serialno);
1379         vf->ready_state=STREAMSET;
1380         firstflag=(pagepos<=vf->dataoffsets[link]);
1381       }
1382
1383       ogg_stream_pagein(&vf->os,&og);
1384       ogg_stream_pagein(&work_os,&og);
1385       lastflag=ogg_page_eos(&og);
1386
1387     }
1388   }
1389
1390   ogg_stream_clear(&work_os);
1391   vf->bittrack=0.f;
1392   vf->samptrack=0.f;
1393   return(0);
1394 }
1395
1396 /* Page granularity seek (faster than sample granularity because we
1397    don't do the last bit of decode to find a specific sample).
1398
1399    Seek to the last [granule marked] page preceding the specified pos
1400    location, such that decoding past the returned point will quickly
1401    arrive at the requested position. */
1402 int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos){
1403   int link=-1;
1404   ogg_int64_t result=0;
1405   ogg_int64_t total=ov_pcm_total(vf,-1);
1406
1407   if(vf->ready_state<OPENED)return(OV_EINVAL);
1408   if(!vf->seekable)return(OV_ENOSEEK);
1409
1410   if(pos<0 || pos>total)return(OV_EINVAL);
1411
1412   /* which bitstream section does this pcm offset occur in? */
1413   for(link=vf->links-1;link>=0;link--){
1414     total-=vf->pcmlengths[link*2+1];
1415     if(pos>=total)break;
1416   }
1417
1418   /* Search within the logical bitstream for the page with the highest
1419      pcm_pos preceding pos.  If we're looking for a position on the
1420      first page, bisection will halt without finding our position as
1421      it's before the first explicit granulepos fencepost. That case is
1422      handled separately below.
1423
1424      There is a danger here; missing pages or incorrect frame number
1425      information in the bitstream could make our task impossible.
1426      Account for that (it would be an error condition) */
1427
1428   /* new search algorithm originally by HB (Nicholas Vinen) */
1429
1430   {
1431     ogg_int64_t end=vf->offsets[link+1];
1432     ogg_int64_t begin=vf->dataoffsets[link];
1433     ogg_int64_t begintime = vf->pcmlengths[link*2];
1434     ogg_int64_t endtime = vf->pcmlengths[link*2+1]+begintime;
1435     ogg_int64_t target=pos-total+begintime;
1436     ogg_int64_t best=-1;
1437     int         got_page=0;
1438
1439     ogg_page og;
1440
1441     /* if we have only one page, there will be no bisection.  Grab the page here */
1442     if(begin==end){
1443       result=_seek_helper(vf,begin);
1444       if(result) goto seek_error;
1445
1446       result=_get_next_page(vf,&og,1);
1447       if(result<0) goto seek_error;
1448
1449       got_page=1;
1450     }
1451
1452     /* bisection loop */
1453     while(begin<end){
1454       ogg_int64_t bisect;
1455
1456       if(end-begin<CHUNKSIZE){
1457         bisect=begin;
1458       }else{
1459         /* take a (pretty decent) guess. */
1460         bisect=begin +
1461           (ogg_int64_t)((double)(target-begintime)*(end-begin)/(endtime-begintime))
1462           - CHUNKSIZE;
1463         if(bisect<begin+CHUNKSIZE)
1464           bisect=begin;
1465       }
1466
1467       result=_seek_helper(vf,bisect);
1468       if(result) goto seek_error;
1469
1470       /* read loop within the bisection loop */
1471       while(begin<end){
1472         result=_get_next_page(vf,&og,end-vf->offset);
1473         if(result==OV_EREAD) goto seek_error;
1474         if(result<0){
1475           /* there is no next page! */
1476           if(bisect<=begin+1)
1477               /* No bisection left to perform.  We've either found the
1478                  best candidate already or failed. Exit loop. */
1479             end=begin;
1480           else{
1481             /* We tried to load a fraction of the last page; back up a
1482                bit and try to get the whole last page */
1483             if(bisect==0) goto seek_error;
1484             bisect-=CHUNKSIZE;
1485
1486             /* don't repeat/loop on a read we've already performed */
1487             if(bisect<=begin)bisect=begin+1;
1488
1489             /* seek and cntinue bisection */
1490             result=_seek_helper(vf,bisect);
1491             if(result) goto seek_error;
1492           }
1493         }else{
1494           ogg_int64_t granulepos;
1495           got_page=1;
1496
1497           /* got a page. analyze it */
1498           /* only consider pages from primary vorbis stream */
1499           if(ogg_page_serialno(&og)!=vf->serialnos[link])
1500             continue;
1501
1502           /* only consider pages with the granulepos set */
1503           granulepos=ogg_page_granulepos(&og);
1504           if(granulepos==-1)continue;
1505
1506           if(granulepos<target){
1507             /* this page is a successful candidate! Set state */
1508
1509             best=result;  /* raw offset of packet with granulepos */
1510             begin=vf->offset; /* raw offset of next page */
1511             begintime=granulepos;
1512
1513             /* if we're before our target but within a short distance,
1514                don't bisect; read forward */
1515             if(target-begintime>44100)break;
1516
1517             bisect=begin; /* *not* begin + 1 as above */
1518           }else{
1519
1520             /* This is one of our pages, but the granpos is
1521                post-target; it is not a bisection return
1522                candidate. (The only way we'd use it is if it's the
1523                first page in the stream; we handle that case later
1524                outside the bisection) */
1525             if(bisect<=begin+1){
1526               /* No bisection left to perform.  We've either found the
1527                  best candidate already or failed. Exit loop. */
1528               end=begin;
1529             }else{
1530               if(end==vf->offset){
1531                 /* bisection read to the end; use the known page
1532                    boundary (result) to update bisection, back up a
1533                    little bit, and try again */
1534                 end=result;
1535                 bisect-=CHUNKSIZE;
1536                 if(bisect<=begin)bisect=begin+1;
1537                 result=_seek_helper(vf,bisect);
1538                 if(result) goto seek_error;
1539               }else{
1540                 /* Normal bisection */
1541                 end=bisect;
1542                 endtime=granulepos;
1543                 break;
1544               }
1545             }
1546           }
1547         }
1548       }
1549     }
1550
1551     /* Out of bisection: did it 'fail?' */
1552     if(best == -1){
1553
1554       /* Check the 'looking for data in first page' special case;
1555          bisection would 'fail' because our search target was before the
1556          first PCM granule position fencepost. */
1557
1558       if(got_page &&
1559          begin == vf->dataoffsets[link] &&
1560          ogg_page_serialno(&og)==vf->serialnos[link]){
1561
1562         /* Yes, this is the beginning-of-stream case. We already have
1563            our page, right at the beginning of PCM data.  Set state
1564            and return. */
1565
1566         vf->pcm_offset=total;
1567
1568         if(link!=vf->current_link){
1569           /* Different link; dump entire decode machine */
1570           _decode_clear(vf);
1571
1572           vf->current_link=link;
1573           vf->current_serialno=vf->serialnos[link];
1574           vf->ready_state=STREAMSET;
1575
1576         }else{
1577           vorbis_synthesis_restart(&vf->vd);
1578         }
1579
1580         ogg_stream_reset_serialno(&vf->os,vf->current_serialno);
1581         ogg_stream_pagein(&vf->os,&og);
1582
1583       }else
1584         goto seek_error;
1585
1586     }else{
1587
1588       /* Bisection found our page. seek to it, update pcm offset. Easier case than
1589          raw_seek, don't keep packets preceding granulepos. */
1590
1591       ogg_page og;
1592       ogg_packet op;
1593
1594       /* seek */
1595       result=_seek_helper(vf,best);
1596       vf->pcm_offset=-1;
1597       if(result) goto seek_error;
1598       result=_get_next_page(vf,&og,-1);
1599       if(result<0) goto seek_error;
1600
1601       if(link!=vf->current_link){
1602         /* Different link; dump entire decode machine */
1603         _decode_clear(vf);
1604
1605         vf->current_link=link;
1606         vf->current_serialno=vf->serialnos[link];
1607         vf->ready_state=STREAMSET;
1608
1609       }else{
1610         vorbis_synthesis_restart(&vf->vd);
1611       }
1612
1613       ogg_stream_reset_serialno(&vf->os,vf->current_serialno);
1614       ogg_stream_pagein(&vf->os,&og);
1615
1616       /* pull out all but last packet; the one with granulepos */
1617       while(1){
1618         result=ogg_stream_packetpeek(&vf->os,&op);
1619         if(result==0){
1620           /* No packet returned; we exited the bisection with 'best'
1621              pointing to a page with a granule position, so the packet
1622              finishing this page ('best') originated on a preceding
1623              page. Keep fetching previous pages until we get one with
1624              a granulepos or without the 'continued' flag set.  Then
1625              just use raw_seek for simplicity. */
1626           /* Do not rewind past the beginning of link data; if we do,
1627              it's either a bug or a broken stream */
1628           result=best;
1629           while(result>vf->dataoffsets[link]){
1630             result=_get_prev_page(vf,result,&og);
1631             if(result<0) goto seek_error;
1632             if(ogg_page_serialno(&og)==vf->current_serialno &&
1633                (ogg_page_granulepos(&og)>-1 ||
1634                 !ogg_page_continued(&og))){
1635               return ov_raw_seek(vf,result);
1636             }
1637           }
1638         }
1639         if(result<0){
1640           result = OV_EBADPACKET;
1641           goto seek_error;
1642         }
1643         if(op.granulepos!=-1){
1644           vf->pcm_offset=op.granulepos-vf->pcmlengths[vf->current_link*2];
1645           if(vf->pcm_offset<0)vf->pcm_offset=0;
1646           vf->pcm_offset+=total;
1647           break;
1648         }else
1649           result=ogg_stream_packetout(&vf->os,NULL);
1650       }
1651     }
1652   }
1653
1654   /* verify result */
1655   if(vf->pcm_offset>pos || pos>ov_pcm_total(vf,-1)){
1656     result=OV_EFAULT;
1657     goto seek_error;
1658   }
1659   vf->bittrack=0.f;
1660   vf->samptrack=0.f;
1661   return(0);
1662
1663  seek_error:
1664   /* dump machine so we're in a known state */
1665   vf->pcm_offset=-1;
1666   _decode_clear(vf);
1667   return (int)result;
1668 }
1669
1670 /* seek to a sample offset relative to the decompressed pcm stream
1671    returns zero on success, nonzero on failure */
1672
1673 int ov_pcm_seek(OggVorbis_File *vf,ogg_int64_t pos){
1674   int thisblock,lastblock=0;
1675   int ret=ov_pcm_seek_page(vf,pos);
1676   if(ret<0)return(ret);
1677   if((ret=_make_decode_ready(vf)))return ret;
1678
1679   /* discard leading packets we don't need for the lapping of the
1680      position we want; don't decode them */
1681
1682   while(1){
1683     ogg_packet op;
1684     ogg_page og;
1685
1686     int ret=ogg_stream_packetpeek(&vf->os,&op);
1687     if(ret>0){
1688       thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op);
1689       if(thisblock<0){
1690         ogg_stream_packetout(&vf->os,NULL);
1691         continue; /* non audio packet */
1692       }
1693       if(lastblock)vf->pcm_offset+=(lastblock+thisblock)>>2;
1694
1695       if(vf->pcm_offset+((thisblock+
1696                           vorbis_info_blocksize(vf->vi,1))>>2)>=pos)break;
1697
1698       /* remove the packet from packet queue and track its granulepos */
1699       ogg_stream_packetout(&vf->os,NULL);
1700       vorbis_synthesis_trackonly(&vf->vb,&op);  /* set up a vb with
1701                                                    only tracking, no
1702                                                    pcm_decode */
1703       vorbis_synthesis_blockin(&vf->vd,&vf->vb);
1704
1705       /* end of logical stream case is hard, especially with exact
1706          length positioning. */
1707
1708       if(op.granulepos>-1){
1709         int i;
1710         /* always believe the stream markers */
1711         vf->pcm_offset=op.granulepos-vf->pcmlengths[vf->current_link*2];
1712         if(vf->pcm_offset<0)vf->pcm_offset=0;
1713         for(i=0;i<vf->current_link;i++)
1714           vf->pcm_offset+=vf->pcmlengths[i*2+1];
1715       }
1716
1717       lastblock=thisblock;
1718
1719     }else{
1720       if(ret<0 && ret!=OV_HOLE)break;
1721
1722       /* suck in a new page */
1723       if(_get_next_page(vf,&og,-1)<0)break;
1724       if(ogg_page_bos(&og))_decode_clear(vf);
1725
1726       if(vf->ready_state<STREAMSET){
1727         long serialno=ogg_page_serialno(&og);
1728         int link;
1729
1730         for(link=0;link<vf->links;link++)
1731           if(vf->serialnos[link]==serialno)break;
1732         if(link==vf->links) continue;
1733         vf->current_link=link;
1734
1735         vf->ready_state=STREAMSET;
1736         vf->current_serialno=ogg_page_serialno(&og);
1737         ogg_stream_reset_serialno(&vf->os,serialno);
1738         ret=_make_decode_ready(vf);
1739         if(ret)return ret;
1740         lastblock=0;
1741       }
1742
1743       ogg_stream_pagein(&vf->os,&og);
1744     }
1745   }
1746
1747   vf->bittrack=0.f;
1748   vf->samptrack=0.f;
1749   /* discard samples until we reach the desired position. Crossing a
1750      logical bitstream boundary with abandon is OK. */
1751   {
1752     /* note that halfrate could be set differently in each link, but
1753        vorbisfile encoforces all links are set or unset */
1754     int hs=vorbis_synthesis_halfrate_p(vf->vi);
1755     while(vf->pcm_offset<((pos>>hs)<<hs)){
1756       ogg_int64_t target=(pos-vf->pcm_offset)>>hs;
1757       long samples=vorbis_synthesis_pcmout(&vf->vd,NULL);
1758
1759       if(samples>target)samples=target;
1760       vorbis_synthesis_read(&vf->vd,samples);
1761       vf->pcm_offset+=samples<<hs;
1762
1763       if(samples<target)
1764         if(_fetch_and_process_packet(vf,NULL,1,1)<=0)
1765           vf->pcm_offset=ov_pcm_total(vf,-1); /* eof */
1766     }
1767   }
1768   return 0;
1769 }
1770
1771 /* seek to a playback time relative to the decompressed pcm stream
1772    returns zero on success, nonzero on failure */
1773 int ov_time_seek(OggVorbis_File *vf,double seconds){
1774   /* translate time to PCM position and call ov_pcm_seek */
1775
1776   int link=-1;
1777   ogg_int64_t pcm_total=0;
1778   double time_total=0.;
1779
1780   if(vf->ready_state<OPENED)return(OV_EINVAL);
1781   if(!vf->seekable)return(OV_ENOSEEK);
1782   if(seconds<0)return(OV_EINVAL);
1783
1784   /* which bitstream section does this time offset occur in? */
1785   for(link=0;link<vf->links;link++){
1786     double addsec = ov_time_total(vf,link);
1787     if(seconds<time_total+addsec)break;
1788     time_total+=addsec;
1789     pcm_total+=vf->pcmlengths[link*2+1];
1790   }
1791
1792   if(link==vf->links)return(OV_EINVAL);
1793
1794   /* enough information to convert time offset to pcm offset */
1795   {
1796     ogg_int64_t target=pcm_total+(seconds-time_total)*vf->vi[link].rate;
1797     return(ov_pcm_seek(vf,target));
1798   }
1799 }
1800
1801 /* page-granularity version of ov_time_seek
1802    returns zero on success, nonzero on failure */
1803 int ov_time_seek_page(OggVorbis_File *vf,double seconds){
1804   /* translate time to PCM position and call ov_pcm_seek */
1805
1806   int link=-1;
1807   ogg_int64_t pcm_total=0;
1808   double time_total=0.;
1809
1810   if(vf->ready_state<OPENED)return(OV_EINVAL);
1811   if(!vf->seekable)return(OV_ENOSEEK);
1812   if(seconds<0)return(OV_EINVAL);
1813
1814   /* which bitstream section does this time offset occur in? */
1815   for(link=0;link<vf->links;link++){
1816     double addsec = ov_time_total(vf,link);
1817     if(seconds<time_total+addsec)break;
1818     time_total+=addsec;
1819     pcm_total+=vf->pcmlengths[link*2+1];
1820   }
1821
1822   if(link==vf->links)return(OV_EINVAL);
1823
1824   /* enough information to convert time offset to pcm offset */
1825   {
1826     ogg_int64_t target=pcm_total+(seconds-time_total)*vf->vi[link].rate;
1827     return(ov_pcm_seek_page(vf,target));
1828   }
1829 }
1830
1831 /* tell the current stream offset cursor.  Note that seek followed by
1832    tell will likely not give the set offset due to caching */
1833 ogg_int64_t ov_raw_tell(OggVorbis_File *vf){
1834   if(vf->ready_state<OPENED)return(OV_EINVAL);
1835   return(vf->offset);
1836 }
1837
1838 /* return PCM offset (sample) of next PCM sample to be read */
1839 ogg_int64_t ov_pcm_tell(OggVorbis_File *vf){
1840   if(vf->ready_state<OPENED)return(OV_EINVAL);
1841   return(vf->pcm_offset);
1842 }
1843
1844 /* return time offset (seconds) of next PCM sample to be read */
1845 double ov_time_tell(OggVorbis_File *vf){
1846   int link=0;
1847   ogg_int64_t pcm_total=0;
1848   double time_total=0.f;
1849
1850   if(vf->ready_state<OPENED)return(OV_EINVAL);
1851   if(vf->seekable){
1852     pcm_total=ov_pcm_total(vf,-1);
1853     time_total=ov_time_total(vf,-1);
1854
1855     /* which bitstream section does this time offset occur in? */
1856     for(link=vf->links-1;link>=0;link--){
1857       pcm_total-=vf->pcmlengths[link*2+1];
1858       time_total-=ov_time_total(vf,link);
1859       if(vf->pcm_offset>=pcm_total)break;
1860     }
1861   }
1862
1863   return((double)time_total+(double)(vf->pcm_offset-pcm_total)/vf->vi[link].rate);
1864 }
1865
1866 /*  link:   -1) return the vorbis_info struct for the bitstream section
1867                 currently being decoded
1868            0-n) to request information for a specific bitstream section
1869
1870     In the case of a non-seekable bitstream, any call returns the
1871     current bitstream.  NULL in the case that the machine is not
1872     initialized */
1873
1874 vorbis_info *ov_info(OggVorbis_File *vf,int link){
1875   if(vf->seekable){
1876     if(link<0)
1877       if(vf->ready_state>=STREAMSET)
1878         return vf->vi+vf->current_link;
1879       else
1880       return vf->vi;
1881     else
1882       if(link>=vf->links)
1883         return NULL;
1884       else
1885         return vf->vi+link;
1886   }else{
1887     return vf->vi;
1888   }
1889 }
1890
1891 /* grr, strong typing, grr, no templates/inheritence, grr */
1892 vorbis_comment *ov_comment(OggVorbis_File *vf,int link){
1893   if(vf->seekable){
1894     if(link<0)
1895       if(vf->ready_state>=STREAMSET)
1896         return vf->vc+vf->current_link;
1897       else
1898         return vf->vc;
1899     else
1900       if(link>=vf->links)
1901         return NULL;
1902       else
1903         return vf->vc+link;
1904   }else{
1905     return vf->vc;
1906   }
1907 }
1908
1909 static int host_is_big_endian() {
1910   ogg_int32_t pattern = 0xfeedface; /* deadbeef */
1911   unsigned char *bytewise = (unsigned char *)&pattern;
1912   if (bytewise[0] == 0xfe) return 1;
1913   return 0;
1914 }
1915
1916 /* up to this point, everything could more or less hide the multiple
1917    logical bitstream nature of chaining from the toplevel application
1918    if the toplevel application didn't particularly care.  However, at
1919    the point that we actually read audio back, the multiple-section
1920    nature must surface: Multiple bitstream sections do not necessarily
1921    have to have the same number of channels or sampling rate.
1922
1923    ov_read returns the sequential logical bitstream number currently
1924    being decoded along with the PCM data in order that the toplevel
1925    application can take action on channel/sample rate changes.  This
1926    number will be incremented even for streamed (non-seekable) streams
1927    (for seekable streams, it represents the actual logical bitstream
1928    index within the physical bitstream.  Note that the accessor
1929    functions above are aware of this dichotomy).
1930
1931    ov_read_filter is exactly the same as ov_read except that it processes
1932    the decoded audio data through a filter before packing it into the
1933    requested format. This gives greater accuracy than applying a filter
1934    after the audio has been converted into integral PCM.
1935
1936    input values: buffer) a buffer to hold packed PCM data for return
1937                  length) the byte length requested to be placed into buffer
1938                  bigendianp) should the data be packed LSB first (0) or
1939                              MSB first (1)
1940                  word) word size for output.  currently 1 (byte) or
1941                        2 (16 bit short)
1942
1943    return values: <0) error/hole in data (OV_HOLE), partial open (OV_EINVAL)
1944                    0) EOF
1945                    n) number of bytes of PCM actually returned.  The
1946                    below works on a packet-by-packet basis, so the
1947                    return length is not related to the 'length' passed
1948                    in, just guaranteed to fit.
1949
1950             *section) set to the logical bitstream number */
1951
1952 long ov_read_filter(OggVorbis_File *vf,char *buffer,int length,
1953                     int bigendianp,int word,int sgned,int *bitstream,
1954                     void (*filter)(float **pcm,long channels,long samples,void *filter_param),void *filter_param){
1955   int i,j;
1956   int host_endian = host_is_big_endian();
1957   int hs;
1958
1959   float **pcm;
1960   long samples;
1961
1962   if(vf->ready_state<OPENED)return(OV_EINVAL);
1963
1964   while(1){
1965     if(vf->ready_state==INITSET){
1966       samples=vorbis_synthesis_pcmout(&vf->vd,&pcm);
1967       if(samples)break;
1968     }
1969
1970     /* suck in another packet */
1971     {
1972       int ret=_fetch_and_process_packet(vf,NULL,1,1);
1973       if(ret==OV_EOF)
1974         return(0);
1975       if(ret<=0)
1976         return(ret);
1977     }
1978
1979   }
1980
1981   if(samples>0){
1982
1983     /* yay! proceed to pack data into the byte buffer */
1984
1985     long channels=ov_info(vf,-1)->channels;
1986     long bytespersample=word * channels;
1987     vorbis_fpu_control fpu;
1988     if(samples>length/bytespersample)samples=length/bytespersample;
1989
1990     if(samples <= 0)
1991       return OV_EINVAL;
1992
1993     /* Here. */
1994     if(filter)
1995       filter(pcm,channels,samples,filter_param);
1996
1997     /* a tight loop to pack each size */
1998     {
1999       int val;
2000       if(word==1){
2001         int off=(sgned?0:128);
2002         vorbis_fpu_setround(&fpu);
2003         for(j=0;j<samples;j++)
2004           for(i=0;i<channels;i++){
2005             val=vorbis_ftoi(pcm[i][j]*128.f);
2006             if(val>127)val=127;
2007             else if(val<-128)val=-128;
2008             *buffer++=val+off;
2009           }
2010         vorbis_fpu_restore(fpu);
2011       }else{
2012         int off=(sgned?0:32768);
2013
2014         if(host_endian==bigendianp){
2015           if(sgned){
2016
2017             vorbis_fpu_setround(&fpu);
2018             for(i=0;i<channels;i++) { /* It's faster in this order */
2019               float *src=pcm[i];
2020               short *dest=((short *)buffer)+i;
2021               for(j=0;j<samples;j++) {
2022                 val=vorbis_ftoi(src[j]*32768.f);
2023                 if(val>32767)val=32767;
2024                 else if(val<-32768)val=-32768;
2025                 *dest=val;
2026                 dest+=channels;
2027               }
2028             }
2029             vorbis_fpu_restore(fpu);
2030
2031           }else{
2032
2033             vorbis_fpu_setround(&fpu);
2034             for(i=0;i<channels;i++) {
2035               float *src=pcm[i];
2036               short *dest=((short *)buffer)+i;
2037               for(j=0;j<samples;j++) {
2038                 val=vorbis_ftoi(src[j]*32768.f);
2039                 if(val>32767)val=32767;
2040                 else if(val<-32768)val=-32768;
2041                 *dest=val+off;
2042                 dest+=channels;
2043               }
2044             }
2045             vorbis_fpu_restore(fpu);
2046
2047           }
2048         }else if(bigendianp){
2049
2050           vorbis_fpu_setround(&fpu);
2051           for(j=0;j<samples;j++)
2052             for(i=0;i<channels;i++){
2053               val=vorbis_ftoi(pcm[i][j]*32768.f);
2054               if(val>32767)val=32767;
2055               else if(val<-32768)val=-32768;
2056               val+=off;
2057               *buffer++=(val>>8);
2058               *buffer++=(val&0xff);
2059             }
2060           vorbis_fpu_restore(fpu);
2061
2062         }else{
2063           int val;
2064           vorbis_fpu_setround(&fpu);
2065           for(j=0;j<samples;j++)
2066             for(i=0;i<channels;i++){
2067               val=vorbis_ftoi(pcm[i][j]*32768.f);
2068               if(val>32767)val=32767;
2069               else if(val<-32768)val=-32768;
2070               val+=off;
2071               *buffer++=(val&0xff);
2072               *buffer++=(val>>8);
2073                   }
2074           vorbis_fpu_restore(fpu);
2075
2076         }
2077       }
2078     }
2079
2080     vorbis_synthesis_read(&vf->vd,samples);
2081     hs=vorbis_synthesis_halfrate_p(vf->vi);
2082     vf->pcm_offset+=(samples<<hs);
2083     if(bitstream)*bitstream=vf->current_link;
2084     return(samples*bytespersample);
2085   }else{
2086     return(samples);
2087   }
2088 }
2089
2090 long ov_read(OggVorbis_File *vf,char *buffer,int length,
2091              int bigendianp,int word,int sgned,int *bitstream){
2092   return ov_read_filter(vf, buffer, length, bigendianp, word, sgned, bitstream, NULL, NULL);
2093 }
2094
2095 /* input values: pcm_channels) a float vector per channel of output
2096                  length) the sample length being read by the app
2097
2098    return values: <0) error/hole in data (OV_HOLE), partial open (OV_EINVAL)
2099                    0) EOF
2100                    n) number of samples of PCM actually returned.  The
2101                    below works on a packet-by-packet basis, so the
2102                    return length is not related to the 'length' passed
2103                    in, just guaranteed to fit.
2104
2105             *section) set to the logical bitstream number */
2106
2107
2108
2109 long ov_read_float(OggVorbis_File *vf,float ***pcm_channels,int length,
2110                    int *bitstream){
2111
2112   if(vf->ready_state<OPENED)return(OV_EINVAL);
2113
2114   while(1){
2115     if(vf->ready_state==INITSET){
2116       float **pcm;
2117       long samples=vorbis_synthesis_pcmout(&vf->vd,&pcm);
2118       if(samples){
2119         int hs=vorbis_synthesis_halfrate_p(vf->vi);
2120         if(pcm_channels)*pcm_channels=pcm;
2121         if(samples>length)samples=length;
2122         vorbis_synthesis_read(&vf->vd,samples);
2123         vf->pcm_offset+=samples<<hs;
2124         if(bitstream)*bitstream=vf->current_link;
2125         return samples;
2126
2127       }
2128     }
2129
2130     /* suck in another packet */
2131     {
2132       int ret=_fetch_and_process_packet(vf,NULL,1,1);
2133       if(ret==OV_EOF)return(0);
2134       if(ret<=0)return(ret);
2135     }
2136
2137   }
2138 }
2139
2140 extern const float *vorbis_window(vorbis_dsp_state *v,int W);
2141
2142 static void _ov_splice(float **pcm,float **lappcm,
2143                        int n1, int n2,
2144                        int ch1, int ch2,
2145                        const float *w1, const float *w2){
2146   int i,j;
2147   const float *w=w1;
2148   int n=n1;
2149
2150   if(n1>n2){
2151     n=n2;
2152     w=w2;
2153   }
2154
2155   /* splice */
2156   for(j=0;j<ch1 && j<ch2;j++){
2157     float *s=lappcm[j];
2158     float *d=pcm[j];
2159
2160     for(i=0;i<n;i++){
2161       float wd=w[i]*w[i];
2162       float ws=1.-wd;
2163       d[i]=d[i]*wd + s[i]*ws;
2164     }
2165   }
2166   /* window from zero */
2167   for(;j<ch2;j++){
2168     float *d=pcm[j];
2169     for(i=0;i<n;i++){
2170       float wd=w[i]*w[i];
2171       d[i]=d[i]*wd;
2172     }
2173   }
2174
2175 }
2176
2177 /* make sure vf is INITSET */
2178 static int _ov_initset(OggVorbis_File *vf){
2179   while(1){
2180     if(vf->ready_state==INITSET)break;
2181     /* suck in another packet */
2182     {
2183       int ret=_fetch_and_process_packet(vf,NULL,1,0);
2184       if(ret<0 && ret!=OV_HOLE)return(ret);
2185     }
2186   }
2187   return 0;
2188 }
2189
2190 /* make sure vf is INITSET and that we have a primed buffer; if
2191    we're crosslapping at a stream section boundary, this also makes
2192    sure we're sanity checking against the right stream information */
2193 static int _ov_initprime(OggVorbis_File *vf){
2194   vorbis_dsp_state *vd=&vf->vd;
2195   while(1){
2196     if(vf->ready_state==INITSET)
2197       if(vorbis_synthesis_pcmout(vd,NULL))break;
2198
2199     /* suck in another packet */
2200     {
2201       int ret=_fetch_and_process_packet(vf,NULL,1,0);
2202       if(ret<0 && ret!=OV_HOLE)return(ret);
2203     }
2204   }
2205   return 0;
2206 }
2207
2208 /* grab enough data for lapping from vf; this may be in the form of
2209    unreturned, already-decoded pcm, remaining PCM we will need to
2210    decode, or synthetic postextrapolation from last packets. */
2211 static void _ov_getlap(OggVorbis_File *vf,vorbis_info *vi,vorbis_dsp_state *vd,
2212                        float **lappcm,int lapsize){
2213   int lapcount=0,i;
2214   float **pcm;
2215
2216   /* try first to decode the lapping data */
2217   while(lapcount<lapsize){
2218     int samples=vorbis_synthesis_pcmout(vd,&pcm);
2219     if(samples){
2220       if(samples>lapsize-lapcount)samples=lapsize-lapcount;
2221       for(i=0;i<vi->channels;i++)
2222         memcpy(lappcm[i]+lapcount,pcm[i],sizeof(**pcm)*samples);
2223       lapcount+=samples;
2224       vorbis_synthesis_read(vd,samples);
2225     }else{
2226     /* suck in another packet */
2227       int ret=_fetch_and_process_packet(vf,NULL,1,0); /* do *not* span */
2228       if(ret==OV_EOF)break;
2229     }
2230   }
2231   if(lapcount<lapsize){
2232     /* failed to get lapping data from normal decode; pry it from the
2233        postextrapolation buffering, or the second half of the MDCT
2234        from the last packet */
2235     int samples=vorbis_synthesis_lapout(&vf->vd,&pcm);
2236     if(samples==0){
2237       for(i=0;i<vi->channels;i++)
2238         memset(lappcm[i]+lapcount,0,sizeof(**pcm)*lapsize-lapcount);
2239       lapcount=lapsize;
2240     }else{
2241       if(samples>lapsize-lapcount)samples=lapsize-lapcount;
2242       for(i=0;i<vi->channels;i++)
2243         memcpy(lappcm[i]+lapcount,pcm[i],sizeof(**pcm)*samples);
2244       lapcount+=samples;
2245     }
2246   }
2247 }
2248
2249 /* this sets up crosslapping of a sample by using trailing data from
2250    sample 1 and lapping it into the windowing buffer of sample 2 */
2251 int ov_crosslap(OggVorbis_File *vf1, OggVorbis_File *vf2){
2252   vorbis_info *vi1,*vi2;
2253   float **lappcm;
2254   float **pcm;
2255   const float *w1,*w2;
2256   int n1,n2,i,ret,hs1,hs2;
2257
2258   if(vf1==vf2)return(0); /* degenerate case */
2259   if(vf1->ready_state<OPENED)return(OV_EINVAL);
2260   if(vf2->ready_state<OPENED)return(OV_EINVAL);
2261
2262   /* the relevant overlap buffers must be pre-checked and pre-primed
2263      before looking at settings in the event that priming would cross
2264      a bitstream boundary.  So, do it now */
2265
2266   ret=_ov_initset(vf1);
2267   if(ret)return(ret);
2268   ret=_ov_initprime(vf2);
2269   if(ret)return(ret);
2270
2271   vi1=ov_info(vf1,-1);
2272   vi2=ov_info(vf2,-1);
2273   hs1=ov_halfrate_p(vf1);
2274   hs2=ov_halfrate_p(vf2);
2275
2276   lappcm=alloca(sizeof(*lappcm)*vi1->channels);
2277   n1=vorbis_info_blocksize(vi1,0)>>(1+hs1);
2278   n2=vorbis_info_blocksize(vi2,0)>>(1+hs2);
2279   w1=vorbis_window(&vf1->vd,0);
2280   w2=vorbis_window(&vf2->vd,0);
2281
2282   for(i=0;i<vi1->channels;i++)
2283     lappcm[i]=alloca(sizeof(**lappcm)*n1);
2284
2285   _ov_getlap(vf1,vi1,&vf1->vd,lappcm,n1);
2286
2287   /* have a lapping buffer from vf1; now to splice it into the lapping
2288      buffer of vf2 */
2289   /* consolidate and expose the buffer. */
2290   vorbis_synthesis_lapout(&vf2->vd,&pcm);
2291
2292 #if 0
2293   _analysis_output_always("pcmL",0,pcm[0],n1*2,0,0,0);
2294   _analysis_output_always("pcmR",0,pcm[1],n1*2,0,0,0);
2295 #endif
2296
2297   /* splice */
2298   _ov_splice(pcm,lappcm,n1,n2,vi1->channels,vi2->channels,w1,w2);
2299
2300   /* done */
2301   return(0);
2302 }
2303
2304 static int _ov_64_seek_lap(OggVorbis_File *vf,ogg_int64_t pos,
2305                            int (*localseek)(OggVorbis_File *,ogg_int64_t)){
2306   vorbis_info *vi;
2307   float **lappcm;
2308   float **pcm;
2309   const float *w1,*w2;
2310   int n1,n2,ch1,ch2,hs;
2311   int i,ret;
2312
2313   if(vf->ready_state<OPENED)return(OV_EINVAL);
2314   ret=_ov_initset(vf);
2315   if(ret)return(ret);
2316   vi=ov_info(vf,-1);
2317   hs=ov_halfrate_p(vf);
2318
2319   ch1=vi->channels;
2320   n1=vorbis_info_blocksize(vi,0)>>(1+hs);
2321   w1=vorbis_window(&vf->vd,0);  /* window arrays from libvorbis are
2322                                    persistent; even if the decode state
2323                                    from this link gets dumped, this
2324                                    window array continues to exist */
2325
2326   lappcm=alloca(sizeof(*lappcm)*ch1);
2327   for(i=0;i<ch1;i++)
2328     lappcm[i]=alloca(sizeof(**lappcm)*n1);
2329   _ov_getlap(vf,vi,&vf->vd,lappcm,n1);
2330
2331   /* have lapping data; seek and prime the buffer */
2332   ret=localseek(vf,pos);
2333   if(ret)return ret;
2334   ret=_ov_initprime(vf);
2335   if(ret)return(ret);
2336
2337  /* Guard against cross-link changes; they're perfectly legal */
2338   vi=ov_info(vf,-1);
2339   ch2=vi->channels;
2340   n2=vorbis_info_blocksize(vi,0)>>(1+hs);
2341   w2=vorbis_window(&vf->vd,0);
2342
2343   /* consolidate and expose the buffer. */
2344   vorbis_synthesis_lapout(&vf->vd,&pcm);
2345
2346   /* splice */
2347   _ov_splice(pcm,lappcm,n1,n2,ch1,ch2,w1,w2);
2348
2349   /* done */
2350   return(0);
2351 }
2352
2353 int ov_raw_seek_lap(OggVorbis_File *vf,ogg_int64_t pos){
2354   return _ov_64_seek_lap(vf,pos,ov_raw_seek);
2355 }
2356
2357 int ov_pcm_seek_lap(OggVorbis_File *vf,ogg_int64_t pos){
2358   return _ov_64_seek_lap(vf,pos,ov_pcm_seek);
2359 }
2360
2361 int ov_pcm_seek_page_lap(OggVorbis_File *vf,ogg_int64_t pos){
2362   return _ov_64_seek_lap(vf,pos,ov_pcm_seek_page);
2363 }
2364
2365 static int _ov_d_seek_lap(OggVorbis_File *vf,double pos,
2366                            int (*localseek)(OggVorbis_File *,double)){
2367   vorbis_info *vi;
2368   float **lappcm;
2369   float **pcm;
2370   const float *w1,*w2;
2371   int n1,n2,ch1,ch2,hs;
2372   int i,ret;
2373
2374   if(vf->ready_state<OPENED)return(OV_EINVAL);
2375   ret=_ov_initset(vf);
2376   if(ret)return(ret);
2377   vi=ov_info(vf,-1);
2378   hs=ov_halfrate_p(vf);
2379
2380   ch1=vi->channels;
2381   n1=vorbis_info_blocksize(vi,0)>>(1+hs);
2382   w1=vorbis_window(&vf->vd,0);  /* window arrays from libvorbis are
2383                                    persistent; even if the decode state
2384                                    from this link gets dumped, this
2385                                    window array continues to exist */
2386
2387   lappcm=alloca(sizeof(*lappcm)*ch1);
2388   for(i=0;i<ch1;i++)
2389     lappcm[i]=alloca(sizeof(**lappcm)*n1);
2390   _ov_getlap(vf,vi,&vf->vd,lappcm,n1);
2391
2392   /* have lapping data; seek and prime the buffer */
2393   ret=localseek(vf,pos);
2394   if(ret)return ret;
2395   ret=_ov_initprime(vf);
2396   if(ret)return(ret);
2397
2398  /* Guard against cross-link changes; they're perfectly legal */
2399   vi=ov_info(vf,-1);
2400   ch2=vi->channels;
2401   n2=vorbis_info_blocksize(vi,0)>>(1+hs);
2402   w2=vorbis_window(&vf->vd,0);
2403
2404   /* consolidate and expose the buffer. */
2405   vorbis_synthesis_lapout(&vf->vd,&pcm);
2406
2407   /* splice */
2408   _ov_splice(pcm,lappcm,n1,n2,ch1,ch2,w1,w2);
2409
2410   /* done */
2411   return(0);
2412 }
2413
2414 int ov_time_seek_lap(OggVorbis_File *vf,double pos){
2415   return _ov_d_seek_lap(vf,pos,ov_time_seek);
2416 }
2417
2418 int ov_time_seek_page_lap(OggVorbis_File *vf,double pos){
2419   return _ov_d_seek_lap(vf,pos,ov_time_seek_page);
2420 }