shithub: opusfile

Download patch

ref: 97ab532bc05a545a3606cf33662008b971358b5a
parent: b5ec4491b729182ccfdf131da5acb75cf22ae678
author: Timothy B. Terriberry <[email protected]>
date: Sun Sep 16 11:19:26 EDT 2012

Initial code import.

Seeking and playback work, at least as far as the examples test
 them.
There are probably still lots of bugs.

--- /dev/null
+++ b/examples/opusfile_example.c
@@ -1,0 +1,198 @@
+/*For fileno()*/
+#if !defined(_POSIX_SOURCE)
+# define _POSIX_SOURCE 1
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#if defined(_WIN32)
+/*We need the following two to set stdin/stdout to binary.*/
+# include <io.h>
+# include <fcntl.h>
+#endif
+#include <opus/opusfile.h>
+
+/*Matrices for downmixing from the supported channel counts to stereo.*/
+static const float DOWNMIX_MATRIX[8][8][2]={
+  /*mono*/
+  {
+    {1.F,1.F}
+  },
+  /*stereo*/
+  {
+    {1.F,0.F},{0.F,1.F}
+  },
+  /*3.0*/
+  {
+    {0.5858F,0.F},{0.4142F,0.4142F},{0,0.5858F}
+  },
+  /*quadrophonic*/
+  {
+    {0.4226F,0.F},{0,0.4226F},{0.366F,0.2114F},{0.2114F,0.336F}
+  },
+  /*5.0*/
+  {
+    {0.651F,0.F},{0.46F,0.46F},{0,0.651F},{0.5636F,0.3254F},{0.3254F,0.5636F}
+  },
+  /*5.1*/
+  {
+    {0.529F,0.F},{0.3741F,0.3741F},{0.F,0.529F},{0.4582F,0.2645F},
+    {0.2645F,0.4582F},{0.3741F,0.3741F}
+  },
+  /*6.1*/
+  {
+    {0.4553F,0.F},{0.322F,0.322F},{0.F,0.4553F},{0.3943F,0.2277F},
+    {0.2277F,0.3943F},{0.2788F,0.2788F},{0.322F,0.322F}
+  },
+  /*7.1*/
+  {
+    {0.3886F,0.F},{0.2748F,0.2748F},{0.F,0.3886F},{0.3366F,0.1943F},
+    {0.1943F,0.3366F},{0.3366F,0.1943F},{0.1943F,0.3366F},{0.2748F,0.2748F}
+  }
+};
+
+int main(int _argc,const char **_argv){
+  OggOpusFile *of;
+  ogg_int64_t  pcm_offset;
+  ogg_int64_t  nsamples;
+  int          ret;
+  int          prev_li;
+#if defined(_WIN32)
+# undef fileno
+# define fileno _fileno
+  /*We need to set stdin/stdout to binary mode. Damn windows.*/
+  /*Beware the evil ifdef. We avoid these where we can, but this one we
+     cannot.
+    Don't add any more.
+    You'll probably go to hell if you do.*/
+  _setmode(fileno(stdin),_O_BINARY);
+  _setmode(fileno(stdout),_O_BINARY);
+#endif
+  if(_argc!=2){
+    fprintf(stderr,"Usage: %s <file.opus>\n",_argv[0]);
+    return EXIT_FAILURE;
+  }
+  if(strcmp(_argv[1],"-")==0){
+    OpusFileCallbacks cb={NULL,NULL,NULL,NULL};
+    of=op_open_callbacks(op_fdopen(&cb,fileno(stdin),"rb"),&cb,NULL,0,NULL);
+  }
+#if 0
+  /*For debugging: force a file to not be seekable.*/
+  else{
+    OpusFileCallbacks  cb={NULL,NULL,NULL,NULL};
+    void              *fp;
+    fp=op_fopen(&cb,_argv[1],"rb");
+    cb.seek=NULL;
+    cb.tell=NULL;
+    of=op_open_callbacks(fp,&cb,NULL,0,NULL);
+  }
+#else
+  else of=op_open_file(_argv[1],NULL);
+#endif
+  if(of==NULL){
+    fprintf(stderr,"Failed to open file '%s'.\n",_argv[1]);
+    return EXIT_FAILURE;
+  }
+  if(op_seekable(of)){
+    ogg_int64_t duration;
+    fprintf(stderr,"Total number of links: %i\n",op_link_count(of));
+    duration=op_pcm_total(of,-1);
+    fprintf(stderr,"Total duration: %f seconds (%li samples @ 48 kHz).\n",
+     duration/48000.0,(long)duration);
+  }
+  prev_li=-1;
+  nsamples=0;
+  pcm_offset=op_pcm_tell(of);
+  if(pcm_offset!=0){
+    fprintf(stderr,"Non-zero starting PCM offset: %li\n",(long)pcm_offset);
+  }
+  for(;;){
+    ogg_int64_t next_pcm_offset;
+    float       pcm[120*48*8];
+    float       stereo_pcm[120*48*2];
+    int         nchannels;
+    int         li;
+    int         i;
+    ret=op_read_float(of,pcm,sizeof(pcm)/sizeof(*pcm),&li);
+    if(ret<0){
+      fprintf(stderr,"Error decoding '%s': %i\n",_argv[1],ret);
+      ret=EXIT_FAILURE;
+      break;
+    }
+    if(li!=prev_li){
+      const OpusHead *head;
+      const OpusTags *tags;
+      int             ci;
+      /*We found a new link.
+        Print out some information.*/
+      fprintf(stderr,"Decoding link %i:\n",li);
+      head=op_head(of,li);
+      fprintf(stderr,"  Channels: %i\n",head->channel_count);
+      if(op_seekable(of)){
+        ogg_int64_t duration;
+        duration=op_pcm_total(of,li);
+        fprintf(stderr,"  Duration: %f seconds (%li samples @ 48 kHz).\n",
+         duration/48000.0,(long)duration);
+      }
+      if(head->input_sample_rate){
+        fprintf(stderr,"  Original sampling rate: %lu Hz\n",
+         (unsigned long)head->input_sample_rate);
+      }
+      tags=op_tags(of,li);
+      fprintf(stderr,"  Encoded by: %s\n",tags->vendor);
+      for(ci=0;ci<tags->comments;ci++){
+        fprintf(stderr,"  %s\n",tags->user_comments[ci]);
+      }
+      fprintf(stderr,"\n");
+      if(!op_seekable(of)){
+        pcm_offset=op_pcm_tell(of)-ret;
+        if(pcm_offset!=0){
+          fprintf(stderr,"Non-zero starting PCM offset in link %i: %li\n",
+           li,(long)pcm_offset);
+        }
+      }
+    }
+    next_pcm_offset=op_pcm_tell(of);
+    if(pcm_offset+ret!=next_pcm_offset){
+      fprintf(stderr,"PCM offset gap! %li+%i!=%li\n",
+       (long)pcm_offset,ret,(long)next_pcm_offset);
+    }
+    pcm_offset=next_pcm_offset;
+    if(ret<=0){
+      ret=EXIT_SUCCESS;
+      break;
+    }
+    /*Downmix to stereo so we can have a consistent output format.*/
+    nchannels=op_channel_count(of,li);
+    if(nchannels<0||nchannels>8){
+      fprintf(stderr,"Unsupported channel count: %i\n",nchannels);
+      ret=EXIT_FAILURE;
+      break;
+    }
+    for(i=0;i<ret;i++){
+      float l;
+      float r;
+      int   ci;
+      l=r=0.F;
+      for(ci=0;ci<nchannels;ci++){
+        l+=DOWNMIX_MATRIX[nchannels-1][ci][0]*pcm[i*nchannels+ci];
+        r+=DOWNMIX_MATRIX[nchannels-1][ci][1]*pcm[i*nchannels+ci];
+      }
+      stereo_pcm[2*i+0]=l;
+      stereo_pcm[2*i+1]=r;
+    }
+    if(!fwrite(stereo_pcm,sizeof(*stereo_pcm)*2,ret,stdout)){
+      fprintf(stderr,"Error writing decoded audio data: %s\n",strerror(errno));
+      ret=EXIT_FAILURE;
+      break;
+    }
+    nsamples+=ret;
+    prev_li=li;
+  }
+  op_free(of);
+  if(ret==EXIT_SUCCESS){
+    fprintf(stderr,"Done (played %li samples).\n",(long)nsamples);
+  }
+  return ret;
+}
--- /dev/null
+++ b/examples/seeking_example.c
@@ -1,0 +1,406 @@
+/*For fileno()*/
+#if !defined(_POSIX_SOURCE)
+# define _POSIX_SOURCE 1
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <math.h>
+#include <string.h>
+#if defined(_WIN32)
+/*We need the following two to set stdin/stdout to binary.*/
+# include <io.h>
+# include <fcntl.h>
+#endif
+#include <opus/opusfile.h>
+
+/*Use shorts, they're smaller.*/
+#define OP_FIXED_POINT (1)
+
+#if defined(OP_FIXED_POINT)
+
+typedef opus_int16 op_sample;
+
+# define op_read_native op_read
+
+/*TODO: The convergence after 80 ms of preroll is far from exact.
+  Our comparison is very rough.
+  Need to find some way to do this better.*/
+# define MATCH_TOL (16384)
+
+# define ABS(_x) ((_x)<0?-(_x):(_x))
+
+# define MATCH(_a,_b) (ABS((_a)-(_b))<MATCH_TOL)
+
+/*Don't have fixed-point downmixing code.*/
+# undef OP_WRITE_SEEK_SAMPLES
+
+#else
+
+typedef float op_sample;
+
+# define op_read_native op_read_float
+
+/*TODO: The convergence after 80 ms of preroll is far from exact.
+  Our comparison is very rough.
+  Need to find some way to do this better.*/
+# define MATCH_TOL (16384.0/32768)
+
+# define FABS(_x) ((_x)<0?-(_x):(_x))
+
+# define MATCH(_a,_b) (FABS((_a)-(_b))<MATCH_TOL)
+
+# if defined(OP_WRITE_SEEK_SAMPLES)
+/*Matrices for downmixing from the supported channel counts to stereo.*/
+static const float DOWNMIX_MATRIX[8][8][2]={
+  /*mono*/
+  {
+    {1.F,1.F}
+  },
+  /*stereo*/
+  {
+    {1.F,0.F},{0.F,1.F}
+  },
+  /*3.0*/
+  {
+    {0.5858F,0.F},{0.4142F,0.4142F},{0,0.5858F}
+  },
+  /*quadrophonic*/
+  {
+    {0.4226F,0.F},{0,0.4226F},{0.366F,0.2114F},{0.2114F,0.336F}
+  },
+  /*5.0*/
+  {
+    {0.651F,0.F},{0.46F,0.46F},{0,0.651F},{0.5636F,0.3254F},{0.3254F,0.5636F}
+  },
+  /*5.1*/
+  {
+    {0.529F,0.F},{0.3741F,0.3741F},{0.F,0.529F},{0.4582F,0.2645F},
+    {0.2645F,0.4582F},{0.3741F,0.3741F}
+  },
+  /*6.1*/
+  {
+    {0.4553F,0.F},{0.322F,0.322F},{0.F,0.4553F},{0.3943F,0.2277F},
+    {0.2277F,0.3943F},{0.2788F,0.2788F},{0.322F,0.322F}
+  },
+  /*7.1*/
+  {
+    {0.3886F,0.F},{0.2748F,0.2748F},{0.F,0.3886F},{0.3366F,0.1943F},
+    {0.1943F,0.3366F},{0.3366F,0.1943F},{0.1943F,0.3366F},{0.2748F,0.2748F}
+  }
+};
+
+static void write_samples(float *_samples,int _nsamples,int _nchannels){
+  float stereo_pcm[120*48*2];
+  int   i;
+  for(i=0;i<_nsamples;i++){
+    float l;
+    float r;
+    int   ci;
+    l=r=0.F;
+    for(ci=0;ci<_nchannels;ci++){
+      l+=DOWNMIX_MATRIX[_nchannels-1][ci][0]*_samples[i*_nchannels+ci];
+      r+=DOWNMIX_MATRIX[_nchannels-1][ci][1]*_samples[i*_nchannels+ci];
+    }
+    stereo_pcm[2*i+0]=l;
+    stereo_pcm[2*i+1]=r;
+  }
+  fwrite(stereo_pcm,sizeof(*stereo_pcm)*2,_nsamples,stdout);
+}
+# endif
+
+#endif
+
+static void verify_seek(OggOpusFile *_of,opus_int64 _byte_offset,
+ ogg_int64_t _pcm_offset,ogg_int64_t _pcm_length,op_sample *_bigassbuffer){
+  opus_int64  byte_offset;
+  ogg_int64_t pcm_offset;
+  ogg_int64_t duration;
+  op_sample   buffer[120*48*8];
+  int         nchannels;
+  int         nsamples;
+  int         li;
+  int         lj;
+  int         i;
+  byte_offset=op_raw_tell(_of);
+  if(_byte_offset!=-1&&byte_offset<_byte_offset){
+    fprintf(stderr,"\nRaw position out of tolerance: requested %li, "
+     "got %li.\n",(long)_byte_offset,(long)byte_offset);
+    exit(EXIT_FAILURE);
+  }
+  pcm_offset=op_pcm_tell(_of);
+  if(_pcm_offset!=-1&&pcm_offset>_pcm_offset){
+    fprintf(stderr,"\nPCM position out of tolerance: requested %li, "
+     "got %li.\n",(long)_pcm_offset,(long)pcm_offset);
+    exit(EXIT_FAILURE);
+  }
+  if(pcm_offset<0||pcm_offset>_pcm_length){
+    fprintf(stderr,"\nPCM position out of bounds: got %li.\n",
+     (long)pcm_offset);
+    exit(EXIT_FAILURE);
+  }
+  nsamples=op_read_native(_of,buffer,sizeof(buffer)/sizeof(*buffer),&li);
+  if(nsamples<0){
+    fprintf(stderr,"\nFailed to read PCM data after seek: %i\n",nsamples);
+    exit(EXIT_FAILURE);
+  }
+  for(lj=0;lj<li;lj++){
+    duration=op_pcm_total(_of,lj);
+    pcm_offset-=duration;
+    if(pcm_offset<0){
+      fprintf(stderr,"\nPCM data after seek came from the wrong link: "
+       "expected %i, got %i.\n",lj,li);
+      exit(EXIT_FAILURE);
+    }
+    _bigassbuffer+=op_channel_count(_of,lj)*duration;
+  }
+  duration=op_pcm_total(_of,li);
+  if(pcm_offset+nsamples>duration){
+    fprintf(stderr,"\nPCM data after seek exceeded link duration: "
+     "limit %li, got %li.\n",duration,pcm_offset+nsamples);
+    exit(EXIT_FAILURE);
+  }
+  nchannels=op_channel_count(_of,li);
+  for(i=0;i<nsamples*nchannels;i++){
+    if(!MATCH(buffer[i],_bigassbuffer[pcm_offset*nchannels+i])){
+      fprintf(stderr,"\nData after seek doesn't match declared PCM "
+       "position: mismatch %G\n",
+       (double)buffer[i]-_bigassbuffer[pcm_offset*nchannels+i]);
+      for(i=0;i<duration-nsamples;i++){
+        int j;
+        for(j=0;j<nsamples*nchannels;j++){
+          if(!MATCH(buffer[j],_bigassbuffer[i*nchannels+j]))break;
+        }
+        if(j==nsamples*nchannels){
+          fprintf(stderr,"\nData after seek appears to match position %li.\n",
+           (long)i);
+        }
+      }
+      exit(EXIT_FAILURE);
+    }
+  }
+#if defined(OP_WRITE_SEEK_SAMPLES)
+  write_samples(buffer,nsamples,nchannels);
+#endif
+}
+
+#define OP_MIN(_a,_b) ((_a)<(_b)?(_a):(_b))
+
+/*A simple wrapper that lets us count the number of underlying seek calls.*/
+
+static op_seek_func real_seek;
+
+static long nreal_seeks;
+
+static int seek_stat_counter(void *_stream,opus_int64 _offset,int _whence){
+  if(_whence==SEEK_SET)nreal_seeks++;
+  /*SEEK_CUR with an offset of 0 is free, as is SEEK_END with an offset of 0
+     (assuming we know the file size), so don't count them.*/
+  else if(_offset!=0)nreal_seeks++;
+  return (*real_seek)(_stream,_offset,_whence);
+}
+
+#define NSEEK_TESTS (1000)
+
+static void print_duration(FILE *_fp,ogg_int64_t _nsamples){
+  ogg_int64_t seconds;
+  ogg_int64_t minutes;
+  ogg_int64_t hours;
+  ogg_int64_t days;
+  ogg_int64_t weeks;
+  seconds=_nsamples/48000;
+  _nsamples-=seconds*48000;
+  minutes=seconds/60;
+  seconds-=minutes*60;
+  hours=minutes/60;
+  minutes-=hours*60;
+  days=hours/24;
+  hours-=days*24;
+  weeks=days/7;
+  days-=weeks*7;
+  if(weeks)fprintf(_fp,"%liw",(long)weeks);
+  if(weeks||days)fprintf(_fp,"%id",(int)days);
+  if(weeks||days||hours)fprintf(_fp,"%ih",(int)hours);
+  if(weeks||days||hours||minutes)fprintf(_fp,"%im",(int)minutes);
+  fprintf(_fp,"%i.%03is",(int)seconds,(int)(_nsamples+24)/48);
+}
+
+int main(int _argc,const char **_argv){
+  OpusFileCallbacks  cb;
+  OggOpusFile       *of;
+  void              *fp;
+#if defined(_WIN32)
+# undef fileno
+# define fileno _fileno
+  /*We need to set stdin/stdout to binary mode. Damn windows.*/
+  /*Beware the evil ifdef. We avoid these where we can, but this one we
+     cannot.
+    Don't add any more.
+    You'll probably go to hell if you do.*/
+  _setmode(fileno(stdin),_O_BINARY);
+  _setmode(fileno(stdout),_O_BINARY);
+#endif
+  if(_argc!=2){
+    fprintf(stderr,"Usage: %s <file.opus>\n",_argv[0]);
+    return EXIT_FAILURE;
+  }
+  memset(&cb,0,sizeof(cb));
+  if(strcmp(_argv[1],"-")==0)fp=op_fdopen(&cb,fileno(stdin),"rb");
+  else fp=op_fopen(&cb,_argv[1],"rb");
+  if(cb.seek!=NULL){
+    real_seek=cb.seek;
+    cb.seek=seek_stat_counter;
+  }
+  of=op_open_callbacks(fp,&cb,NULL,0,NULL);
+  if(of==NULL){
+    fprintf(stderr,"Failed to open file '%s'.\n",_argv[1]);
+    return EXIT_FAILURE;
+  }
+  if(op_seekable(of)){
+    op_sample   *bigassbuffer;
+    ogg_int64_t  size;
+    ogg_int64_t  pcm_print_offset;
+    ogg_int64_t  pcm_offset;
+    ogg_int64_t  pcm_length;
+    ogg_int64_t  nsamples;
+    ogg_int64_t  si;
+    opus_int32   bitrate;
+    int          nlinks;
+    int          ret;
+    int          li;
+    int          i;
+    /*Because we want to do sample-level verification that the seek does what
+       it claimed, decode the entire file into memory.*/
+    nlinks=op_link_count(of);
+    fprintf(stderr,"Opened file containing %i links with %li seeks "
+     "(%0.3f per link).\n",nlinks,nreal_seeks,nreal_seeks/(double)nlinks);
+    /*Reset the seek counter.*/
+    nreal_seeks=0;
+    nsamples=0;
+    for(li=0;li<nlinks;li++){
+      nsamples+=op_pcm_total(of,li)*op_channel_count(of,li);
+    }
+    bigassbuffer=_ogg_malloc(sizeof(*bigassbuffer)*nsamples);
+    pcm_offset=op_pcm_tell(of);
+    if(pcm_offset!=0){
+      fprintf(stderr,"Initial PCM offset was not 0, got %li instead.!\n",
+       (long)pcm_offset);
+      exit(EXIT_FAILURE);
+    }
+    pcm_print_offset=pcm_offset-48000;
+    bitrate=0;
+    for(si=0;si<nsamples;){
+      ogg_int64_t next_pcm_offset;
+      opus_int32  next_bitrate;
+      ret=op_read_native(of,bigassbuffer+si,OP_MIN(120*48*8,nsamples-si),&li);
+      if(ret<=0){
+        fprintf(stderr,"Failed to read PCM data: %i\n",ret);
+        exit(EXIT_FAILURE);
+      }
+      /*If we have gaps in the PCM positions, seeking is not likely to work
+         near them.*/
+      next_pcm_offset=op_pcm_tell(of);
+      if(pcm_offset+ret!=next_pcm_offset){
+        fprintf(stderr,"\nGap in PCM offset: expecting %li, got %li\n",
+         (long)(pcm_offset+ret),(long)next_pcm_offset);
+        exit(EXIT_FAILURE);
+      }
+      pcm_offset=next_pcm_offset;
+      si+=ret*op_channel_count(of,li);
+      if(pcm_offset>=pcm_print_offset+48000){
+        next_bitrate=op_bitrate_instant(of);
+        if(next_bitrate>=0)bitrate=next_bitrate;
+        fprintf(stderr,"\rLoading... [%li left] (%0.3f kbps)               ",
+         nsamples-si,bitrate/1000.0);
+        pcm_print_offset=pcm_offset;
+      }
+    }
+    {
+      op_sample tmp[8];
+      ret=op_read_native(of,tmp,sizeof(tmp)/sizeof(*tmp),&li);
+      if(ret<0){
+        fprintf(stderr,"Failed to read PCM data: %i\n",ret);
+        exit(EXIT_FAILURE);
+      }
+      if(ret>0){
+        fprintf(stderr,"Read too much PCM data!\n");
+        exit(EXIT_FAILURE);
+      }
+    }
+    pcm_length=op_pcm_total(of,-1);
+    size=op_raw_total(of,-1);
+    fprintf(stderr,"\rLoaded (%0.3f kbps average).                        \n",
+     op_bitrate(of,-1)/1000.0);
+    fprintf(stderr,"Testing raw seeking to random places in %li bytes...\n",
+     (long)size);
+    for(i=0;i<NSEEK_TESTS;i++){
+      opus_int64 byte_offset;
+      byte_offset=(opus_int64)(rand()/(double)RAND_MAX*size);
+      fprintf(stderr,"\r\t%3i [raw position %li]...                ",
+       i,(long)byte_offset);
+      ret=op_raw_seek(of,byte_offset);
+      if(ret<0){
+        fprintf(stderr,"\nSeek failed: %i.\n",ret);
+        exit(EXIT_FAILURE);
+      }
+      if(i==28){
+        i=28;
+      }
+      verify_seek(of,byte_offset,-1,pcm_length,bigassbuffer);
+    }
+    fprintf(stderr,"\rTotal seek operations: %li (%.3f per raw seek).\n",
+     nreal_seeks,nreal_seeks/(double)NSEEK_TESTS);
+    nreal_seeks=0;
+    fprintf(stderr,"Testing PCM page seeking to random places in %li "
+     "samples (",(long)pcm_length);
+    print_duration(stderr,pcm_length);
+    fprintf(stderr,")...\n");
+    for(i=0;i<NSEEK_TESTS;i++){
+      pcm_offset=(ogg_int64_t)(rand()/(double)RAND_MAX*pcm_length);
+      fprintf(stderr,"\r\t%3i [PCM position %li]...                ",
+       i,(long)pcm_offset);
+      ret=op_pcm_seek_page(of,pcm_offset);
+      if(ret<0){
+        fprintf(stderr,"\nSeek failed: %i.\n",ret);
+        exit(EXIT_FAILURE);
+      }
+      verify_seek(of,-1,pcm_offset,pcm_length,bigassbuffer);
+    }
+    fprintf(stderr,"\rTotal seek operations: %li (%.3f per page seek).\n",
+     nreal_seeks,nreal_seeks/(double)NSEEK_TESTS);
+    nreal_seeks=0;
+    fprintf(stderr,"Testing exact PCM seeking to random places in %li "
+     "samples (",(long)pcm_length);
+    print_duration(stderr,pcm_length);
+    fprintf(stderr,")...\n");
+    for(i=0;i<NSEEK_TESTS;i++){
+      ogg_int64_t pcm_offset2;
+      pcm_offset=(ogg_int64_t)(rand()/(double)RAND_MAX*pcm_length);
+      fprintf(stderr,"\r\t%3i [PCM position %li]...                ",
+       i,(long)pcm_offset);
+      ret=op_pcm_seek(of,pcm_offset);
+      if(ret<0){
+        fprintf(stderr,"\nSeek failed: %i.\n",ret);
+        exit(EXIT_FAILURE);
+      }
+      pcm_offset2=op_pcm_tell(of);
+      if(pcm_offset!=pcm_offset2){
+        fprintf(stderr,"\nDeclared PCM position did not perfectly match "
+         "request: requested %li, got %li.\n",
+         (long)pcm_offset,(long)pcm_offset2);
+        exit(EXIT_FAILURE);
+      }
+      verify_seek(of,-1,pcm_offset,pcm_length,bigassbuffer);
+    }
+    fprintf(stderr,"\rTotal seek operations: %li (%.3f per exact seek).\n",
+     nreal_seeks,nreal_seeks/(double)NSEEK_TESTS);
+    nreal_seeks=0;
+    fprintf(stderr,"OK.\n");
+  }
+  else{
+    fprintf(stderr,"Input was not seekable.\n");
+    exit(EXIT_FAILURE);
+  }
+  op_free(of);
+  return EXIT_SUCCESS;
+}
--- /dev/null
+++ b/include/opus/opusfile.h
@@ -1,0 +1,971 @@
+/********************************************************************
+ *                                                                  *
+ * THIS FILE IS PART OF THE libopusfile SOFTWARE CODEC SOURCE CODE. *
+ * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
+ * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
+ * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
+ *                                                                  *
+ * THE libopusfile SOURCE CODE IS (C) COPYRIGHT 1994-2012           *
+ * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
+ *                                                                  *
+ ********************************************************************
+
+ function: stdio-based convenience library for opening/seeking/decoding
+ last mod: $Id: vorbisfile.h 17182 2010-04-29 03:48:32Z xiphmont $
+
+ ********************************************************************/
+#if !defined(_opus_opusfile_h)
+# define _opus_opusfile_h (1)
+
+# if defined(__cplusplus)
+extern "C" {
+# endif
+
+# include <stdio.h>
+# include <ogg/ogg.h>
+# include <opus/opus_multistream.h>
+
+/*Enable special features for gcc and gcc-compatible compilers.*/
+# if !defined(OP_GNUC_PREREQ)
+#  if defined(__GNUC__)&&defined(__GNUC_MINOR__)
+#   define OP_GNUC_PREREQ(_maj,_min) \
+ ((__GNUC__<<16)+__GNUC_MINOR__>=((_maj)<<16)+(_min))
+#  else
+#   define OP_GNUC_PREREQ(_maj,_min) 0
+#  endif
+# endif
+
+# if OP_GNUC_PREREQ(4,0)
+#  pragma GCC visibility push(default)
+# endif
+
+typedef struct OpusHead    OpusHead;
+typedef struct OpusTags    OpusTags;
+typedef struct OggOpusFile OggOpusFile;
+
+/*Warning attributes for libopusfile functions.*/
+# if OP_GNUC_PREREQ(3,4)
+#  define OP_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
+# else
+#  define OP_WARN_UNUSED_RESULT
+# endif
+# if OP_GNUC_PREREQ(3,4)
+#  define OP_ARG_NONNULL(_x) __attribute__((__nonnull__(_x)))
+# else
+#  define OP_ARG_NONNULL(_x)
+# endif
+
+/**A request did not succeed.*/
+#define OP_FALSE         (-1)
+/*Currently not used externally.*/
+#define OP_EOF           (-2)
+/**There was a hole in the page sequence numbers (e.g., a page was corrupt or
+    missing).*/
+#define OP_HOLE          (-3)
+/**An underlying read, seek, or tell operation failed when it should have
+    succeeded.*/
+#define OP_EREAD         (-128)
+/**A <code>NULL</code> pointer was passed where one was unexpected, or an
+    internal memory allocation failed, or an internal library error was
+    encountered.*/
+#define OP_EFAULT        (-129)
+/**The stream used a feature that is not implemented, such as an unsupported
+    channel family.*/
+#define OP_EIMPL         (-130)
+/**One or more parameters to a function were invalid.*/
+#define OP_EINVAL        (-131)
+/**A purported Ogg Opus stream did not begin with an Ogg page, or a purported
+    header packet did not start with one of the required strings, "OpusHead" or
+    "OpusTags".*/
+#define OP_ENOTFORMAT    (-132)
+/**A required header packet was not properly formatted, contained illegal
+    values, or was missing altogether.*/
+#define OP_EBADHEADER    (-133)
+/**The ID header contained an unrecognized version number.*/
+#define OP_EVERSION      (-134)
+/*Currently not used at all.*/
+#define OP_ENOTAUDIO     (-135)
+/**An audio packet failed to decode properly.
+   This is usually caused by a multistream Ogg packet where the durations of
+    the individual Opus packets contained in it are not all the same.*/
+#define OP_EBADPACKET    (-136)
+/**We failed to find data we had seen before, or the bitstream structure was
+    sufficiently malformed that seeking to the target destination was
+    impossible.*/
+#define OP_EBADLINK      (-137)
+/**An operation that requires seeking was requested on an unseekable stream.*/
+#define OP_ENOSEEK       (-138)
+/**The first or last granule position of a link failed basic validity checks.*/
+#define OP_EBADTIMESTAMP (-139)
+
+/**The maximum number of channels in an Ogg Opus stream.*/
+#define OPUS_CHANNEL_COUNT_MAX (255)
+
+/**Ogg Opus bitstream information.
+   This contains the basic playback parameters for a stream, and corresponds to
+    the initial ID header packet of an Ogg Opus stream.*/
+struct OpusHead{
+  /**The Ogg Opus format version, in the range 0...255.
+     The top 4 bits represent a "major" version, and the bottom four bits
+      represent backwards-compatible "minor" revisions.
+     The current specification describes version 1.
+     This library will recognize versions up through 15 as backwards compatible
+      with the current specification.
+     An earlier draft of the specification described a version 0, but the only
+      difference between version 1 and version 0 is that version 0 did
+      not specify the semantics for handling the version field.*/
+  int           version;
+  /**The number of channels, in the range 1...255.*/
+  int           channel_count;
+  /**The number of samples that should be discarded from the beginning of the
+      stream.*/
+  unsigned      pre_skip;
+  /**The sampling rate of the original input.
+     All Opus audio is coded at 48 kHz, and should also be decoded at 48 kHz
+      for playback (unless the target hardware does not support this sampling
+      rate).
+     However, this field may be used to resample the audio back to the original
+      sampling rate, for example, when saving the output to a file.*/
+  opus_uint32   input_sample_rate;
+  /**The gain to apply to the decoded output, in dB, as a Q8 value in the range
+      -32768...32767.
+     The decoder will automatically scale the output by
+      pow(10,output_gain/(20.0*256)).*/
+  int           output_gain;
+  /**The channel mapping family, in the range 0...255.
+     Channel mapping family 0 covers mono or stereo in a single stream.
+     Channel mapping family 1 covers 1 to 8 channels in one or more streams,
+      using the Vorbis speaker assignments.
+     Channel mapping family 255 covers 1 to 255 channels in one or more
+      streams, but without any defined speaker assignment.*/
+  int           mapping_family;
+  /**The number of Opus streams in each Ogg packet, in the range 1...255.*/
+  int           stream_count;
+  /**The number of coupled Opus streams in each Ogg packet, in the range
+      0...127.
+     This must satisfy <code>0 <= coupled_count <= stream_count</code> and
+      <code>coupled_count + stream_count <= 255</code>.
+     The coupled streams appear first, before all uncoupled streams, in an Ogg
+      Opus packet.*/
+  int           coupled_count;
+  /**The mapping from coded stream channels to output channels.
+     Let <code>index=mapping[k]</code> be the value for channel <code>k</code>.
+     If <code>index<2*coupled_count</code>, then it refers to the left channel
+      from stream <code>(index/2)</code> if even, and the right channel from
+      stream <code>(index/2)</code> if odd.
+     Otherwise, it refers to the output of the uncoupled stream
+      <code>(index-coupled_count)</code>.*/
+  unsigned char mapping[OPUS_CHANNEL_COUNT_MAX];
+};
+
+/**The metadata from an Ogg Opus stream.
+
+   This structure holds the in-stream metadata corresponding to the 'comment'
+    header packet of an Ogg Opus stream.
+   The comment header is meant to be used much like someone jotting a quick
+    note on the label of a CD.
+   It should be a short, to the point text note that can be more than a couple
+    words, but not more than a short paragraph.
+
+   The metadata is stored as a series of (tag, value) pairs, in length-encoded
+    string vectors, using the same format as Vorbis (without the final "framing
+    bit"), Theora, and Speex, except for the packet header.
+   The first occurrence of the '=' character delimits the tag and value.
+   A particular tag may occur more than once, and order is significant.
+   The character set encoding for the strings is always UTF-8, but the tag
+    names are limited to ASCII, and treated as case-insensitive.
+   See <a href="http://www.xiph.org/vorbis/doc/v-comment.html">the Vorbis
+    comment header specification</a> for details.
+
+   In filling in this structure, \a libopsfile will null-terminate the
+    #user_comments strings for safety.
+   However, the bitstream format itself treats them as 8-bit clean vectors,
+    possibly containing NUL characters, so the #comment_lengths array should be
+    treated as their authoritative length.
+
+   This structure is binary and source-compatible with a
+    <code>vorbis_comment</code>, and pointers to it may be freely cast to
+    <code>vorbis_comment</code> pointers, and vice versa.
+   It is provided as a separate type to avoid introducing a compile-time
+    dependency on the libvorbis headers.*/
+struct OpusTags{
+  /**The array of comment string vectors.*/
+  char **user_comments;
+  /**An array of the corresponding length of each vector, in bytes.*/
+  int   *comment_lengths;
+  /**The total number of comment streams.*/
+  int    comments;
+  /**The null-terminated vendor string.
+     This identifies the software used to encode the stream.*/
+  char  *vendor;
+};
+
+/**\name Functions for manipulating header data
+   These functions manipulate the #OpusHead and #OpusTags structures,
+    which describe the audio parameters and tag-value metadata, respectively.*/
+/*@{*/
+/**Parses the contents of the ID header packet of an Ogg Opus stream.
+   \param[out] _head Returns the contents of the parsed packet.
+                     The contents of this structure are untouched on error.
+                     This may be <code>NULL</code> to merely test the header
+                      for validity.
+   \param[in]  _data The contents of the ID header packet.
+   \param      _len  The number of bytes of data in the ID header packet.
+   \return 0 on success or a negative value on error.
+   \retval #OP_ENOTFORMAT If the data does not start with the "OpusHead"
+                           string.
+   \retval #OP_EVERSION   If the version field signaled a version this library
+                           does not know how to parse.
+   \retval #OP_EIMPL      If the channel mapping family was 255, which general
+                           purpose players should not attempt to play.
+   \retval #OP_EBADHEADER If the contents of the packet otherwise violate the
+                           Ogg Opus specification:
+                          <ul>
+                           <li>Insufficient data,</li>
+                           <li>Too much data for the known minor versions,</li>
+                           <li>An unrecognized channel mapping family,</li>
+                           <li>Zero channels or too many channels,</li>
+                           <li>Zero coded streams,</li>
+                           <li>Too many coupled streams, or</li>
+                           <li>An invalid channel mapping index.</li>
+                          </ul>*/
+OP_WARN_UNUSED_RESULT int opus_head_parse(OpusHead *_head,
+ const unsigned char *_data,size_t _len) OP_ARG_NONNULL(2);
+/**Converts a granule position to a sample offset for a given Ogg Opus stream.
+   The sample offset is simply <code>_gp-_head->pre_skip</code>.
+   Granule position values smaller than OpusHead#pre_skip correspond to audio
+    that should never be played, and thus have no associated sample offset.
+   This function returns -1 for such values.
+   This function also correctly handles extremely large granule positions,
+    which may have wrapped around to a negative number when stored in a signed
+    ogg_int64_t value.
+   \param _head The #OpusHead information from the ID header of the stream.
+   \param _gp   The granule position to convert.
+   \return The sample offset associated with the given granule position
+            (counting at a 48 kHz sampling rate), or the special value -1 on
+            error (i.e., the granule position was smaller than the pre-skip
+            amount).*/
+ogg_int64_t opus_granule_sample(const OpusHead *_head,ogg_int64_t _gp)
+ OP_ARG_NONNULL(1);
+
+/**Parses the contents of the 'comment' header packet of an Ogg Opus stream.
+   \param[out] _tags An uninitialized #OpusTags structure.
+                     This returns the contents of the parsed packet.
+                     The contents of this structure are untouched on error.
+                     This may be <code>NULL</code> to merely test the header
+                      for validity.
+   \param[in]  _data The contents of the 'comment' header packet.
+   \param      _len  The number of bytes of data in the 'info' header packet.
+   \retval 0             Success.
+   \retval #OP_ENOTFORMAT If the data does not start with the "OpusTags"
+                           string.
+   \retval #OP_EBADHEADER If the contents of the packet otherwise violate the
+                           Ogg Opus specification.
+   \retval #OP_EFAULT     If there wasn't enough memory to store the tags.*/
+OP_WARN_UNUSED_RESULT int opus_tags_parse(OpusTags *_tags,
+ const unsigned char *_data,size_t _len) OP_ARG_NONNULL(2);
+/**Initializes an #OpusTags structure.
+   This should be called on a freshly allocated #OpusTags structure before
+    attempting to use it.
+   \param _tags The #OpusTags structure to initialize.*/
+void opus_tags_init(OpusTags *_tags) OP_ARG_NONNULL(1);
+/**Add a (tag, value) pair to an initialized #OpusTags structure.
+   \note Neither opus_tags_add() nor opus_tags_add_comment() support values
+    containing embedded NULs, although the bitstream format does support them.
+   To add such tags, you will need to manipulate the #OpusTags structure
+    directly.
+   \param _tags    The #OpusTags structure to add the (tag, value) pair to.
+   \param _tag     A NUL-terminated, case-insensitive, ASCII string containing
+                    the tag to add (without an '=' character).
+   \param _comment A NUL-terminated UTF-8 containing the corresponding value.*/
+int opus_tags_add(OpusTags *_tags,const char *_tag,const char *_value)
+ OP_ARG_NONNULL(1) OP_ARG_NONNULL(2) OP_ARG_NONNULL(3);
+/**Add a comment to an initialized #OpusTags structure.
+   \note Neither opus_tags_add_comment() nor opus_tags_add() support comments
+    containing embedded NULs, although the bitstream format does support them.
+   To add such tags, you will need to manipulate the #OpusTags structure
+    directly.
+   \param _tags    The #OpusTags structure to add the comment to.
+   \param _comment A NUL-terminated UTF-8 string containing the comment in
+                    "TAG=value" form.*/
+int opus_tags_add_comment(OpusTags *_tags,const char *_comment)
+ OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
+/**Look up a comment value by its tag.
+   \param _tags  An initialized #OpusTags structure.
+   \param _tag   The tag to look up.
+   \param _count The instance of the tag.
+                 The same tag can appear multiple times, each with a distinct
+                  value, so an index is required to retrieve them all.
+                 The order in which these values appear is significant and
+                  should be preserved.
+                 Use opus_tags_query_count() to get the legal range for the
+                  \a _count parameter.
+   \return A pointer to the queried tag's value.
+           This points directly to data in the #OpusTags structure.
+           It should not be modified or freed by the application, and
+            modifications to the structure may invalidate the pointer.
+   \retval <code>NULL</code> if no matching tag is found.*/
+const char *opus_tags_query(const OpusTags *_tags,const char *_tag,int _count)
+ OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
+/**Look up the number of instances of a tag.
+   Call this first when querying for a specific tag and then iterate over the
+    number of instances with separate calls to opus_tags_query() to retrieve
+    all the values for that tag in order.
+   \param _tags An initialized #OpusTags structure.
+   \param _tag  The tag to look up.
+   \return The number of instances of this particular tag.*/
+int opus_tags_query_count(const OpusTags *_tags,const char *_tag)
+ OP_ARG_NONNULL(1) OP_ARG_NONNULL(2);
+/**Clears the #OpusTags structure.
+   This should be called on an #OpusTags structure after it is no longer
+    needed.
+   It will free all memory used by the structure members.
+   \param _tags The #OpusTags structure to clear.*/
+void opus_tags_clear(OpusTags *_tags) OP_ARG_NONNULL(1);
+
+/*@}*/
+
+/**\name Functions for reading from streams
+   These functions define the interface used to read from and seek in a stream
+    of data.
+   A stream does not need to implement seeking, but the decoder will not be
+    able to seek if it does not do so.
+   These functions also include some convenience routines for working with
+    standard FILE pointers or complete files stored in a single block of
+    memory.*/
+/*@{*/
+
+typedef struct OpusFileCallbacks OpusFileCallbacks;
+
+/**Reads \a _nmemb elements of data, each \a _size bytes long, from
+    \a _stream.
+   \return The number of items successfully read (i.e., not the number of
+            characters).
+           Unlike normal <code>fread()</code>, this function is allowed to
+            return fewer bytes than requested (e.g., if reading more would
+            block), as long as <em>some</em> data is returned when no error
+            occurs and EOF has not been reached.
+           If an error occurs, or the end-of-file is reached, the return
+            value is zero.*/
+typedef size_t (*op_read_func)(void *_ptr,size_t _size,size_t _nmemb,
+ void *_stream);
+/**Sets the position indicator for \a _stream.
+   The new position, measured in bytes, is obtained by adding \a _offset
+    bytes to the position specified by \a _whence.
+   If \a _whence is set to <code>SEEK_SET</code>, <code>SEEK_CUR</code>, or
+    <code>SEEK_END</code>, the offset is relative to the start of the stream,
+    the current position indicator, or end-of-file, respectively.
+   \retval 0  Success.
+   \retval -1 Seeking is not supported or an error occurred.
+              <code>errno</code> need not be set.*/
+typedef int (*op_seek_func)(void *_stream,opus_int64 _offset,int _whence);
+/**Obtains the current value of the position indicator for \a _stream.
+   \return The current position indicator.*/
+typedef opus_int64 (*op_tell_func)(void *_stream);
+/**Closes the underlying stream.
+   \retval 0   Success.
+   \retval EOF An error occurred.
+               <code>errno</code> need not be set.*/
+typedef int (*op_close_func)(void *_stream);
+
+/**The callbacks used to access non-FILE stream resources.
+   The function prototypes are basically the same as for the stdio functions
+    <code>fread()</code>, <code>fseek()</code>, <code>ftell()</code>, and
+    <code>fclose()</code>.
+   The differences are that the <code>FILE *</code> arguments have been
+    replaced with a <code>void *</code>, which is to be used as a pointer to
+    whatever internal data these functions might need, that #seek_func and
+    #tell_func take and return 64-bit offsets, and that #seek_func *must*
+    return -1 if the stream is unseekable.*/
+struct OpusFileCallbacks{
+  /**Used to read data from the stream.
+     This must not be <code>NULL</code>.*/
+  op_read_func  read;
+  /**Used to seek in the stream.
+     This may be <code>NULL</code> if seeking is not implemented.*/
+  op_seek_func  seek;
+  /**Used to return the current read position in the stream.
+     This may be <code>NULL</code> if seeking is not implemented.*/
+  op_tell_func  tell;
+  /**Used to close the stream when the decoder is freed.
+     This may be <code>NULL</code> to leave the stream open.*/
+  op_close_func close;
+};
+
+/**Opens a stream with <code>fopen()</code> and fills in a set of callbacks
+    that can be used to access it.
+   This is useful to avoid writing your own portable 64-bit seeking wrappers,
+    and also avoids cross-module linking issues on Windows, where a
+    <code>FILE *</code> must be accessed by routines defined in the same module
+    that opened it.
+   \param[out] _cb   The callbacks to use for this file.
+                     If there is an error opening the file, nothing will be
+                      filled in here.
+   \param      _path The path to the file to open.
+   \param      _mode The mode to open the file in.
+   \return A stream handle to use with the callbacks, or NULL on error.*/
+OP_WARN_UNUSED_RESULT void *op_fopen(OpusFileCallbacks *_cb,
+ const char *_path,const char *_mode) OP_ARG_NONNULL(1) OP_ARG_NONNULL(2)
+ OP_ARG_NONNULL(3);
+/**Opens a stream with <code>fdopen()</code> and fills in a set of callbacks
+    that can be used to access it.
+   This is useful to avoid writing your own portable 64-bit seeking wrappers,
+    and also avoids cross-module linking issues on Windows, where a
+    <code>FILE *</code> must be accessed by routines defined in the same module
+    that opened it.
+   \param[out] _cb   The callbacks to use for this file.
+                     If there is an error opening the file, nothing will be
+                      filled in here.
+   \param      _fd   The file descriptor to open.
+   \param      _mode The mode to open the file in.
+   \return A stream handle to use with the callbacks, or NULL on error.*/
+OP_WARN_UNUSED_RESULT void *op_fdopen(OpusFileCallbacks *_cb,
+ int _fd,const char *_mode) OP_ARG_NONNULL(1) OP_ARG_NONNULL(3);
+/**Opens a stream with <code>freopen()</code> and fills in a set of callbacks
+    that can be used to access it.
+   This is useful to avoid writing your own portable 64-bit seeking wrappers,
+    and also avoids cross-module linking issues on Windows, where a
+    <code>FILE *</code> must be accessed by routines defined in the same module
+    that opened it.
+   \param[out] _cb     The callbacks to use for this file.
+                       If there is an error opening the file, nothing will be
+                        filled in here.
+   \param      _path   The path to the file to open.
+   \param      _mode   The mode to open the file in.
+   \param      _stream A stream previously returned by op_fopen(), op_fdopen(),
+                        or op_freopen().
+   \return A stream handle to use with the callbacks, or NULL on error.*/
+void *op_freopen(OpusFileCallbacks *_cb,const char *_path,const char *_mode,
+ void *_stream) OP_ARG_NONNULL(1) OP_ARG_NONNULL(2) OP_ARG_NONNULL(3)
+ OP_ARG_NONNULL(4);
+
+/**Creates a stream that reads from the given block of memory.
+   This block of memory must contain the complete file to decode.
+   This is useful for caching small files (e.g., sound effects) in RAM.
+   \param[out] _cb   The callbacks to use for this stream.
+                     If there is an error creating the stream, nothing will be
+                      filled in here.
+   \param      _data The block of memory to read from.
+   \param      _size The size of the block of memory.
+   \return A stream handle to use with the callbacks, or NULL on error.*/
+void *op_mem_stream_create(OpusFileCallbacks *_cb,
+ const unsigned char *_data,size_t _size);
+
+/*@}*/
+
+/**Test to see if this is an Opus file.
+   For good results, you will need at least 57 bytes (for a pure Opus-only
+    file).
+   Something more like 512 bytes will give more reliable results for
+    multiplexed files.
+   This function is meant to be a quick-rejection filter.
+   Its purpose is not to guarantee that a stream is a valid Opus stream, but to
+    ensure that looks enough like Opus that it isn't going to be recognized as
+    some other format (except possibly an Opus stream that is also multiplexed
+    with other codecs, such as video).
+   If you need something that gives a much better guarantee that this stream
+    can be opened successfully, use op_test_callbacks() or one of the
+    associated convenience functions.
+   \param[out] _head     The parsed ID header contents.
+                         You may pass <code>NULL</code> if you do not need
+                          this information.
+                         If the function fails, the contents of this structure
+                          remain untouched.
+   \param _initial_data  An initial buffer of data from the start of the
+                          stream.
+   \param _initial_bytes The number of bytes in \a _initial_data.
+   \return 0 if the data appears to be Opus, or a negative value on error.
+   \retval #OP_FALSE      There was not enough data to tell if this was an Opus
+                           stream or not.
+   \retval #OP_EFAULT     An internal memory allocation failed.
+   \retval #OP_EIMPL      The stream used a feature that is not implemented,
+                           such as an unsupported channel family.
+   \retval #OP_ENOTFORMAT If the data did not contain a recognizable ID
+                           header for an Opus stream.
+   \retval #OP_EVERSION   If the version field signaled a version this library
+                           does not know how to parse.
+   \retval #OP_EBADHEADER A required header packet was not properly formatted,
+                           contained illegal values, or was missing
+                           altogether.*/
+int op_test(OpusHead *_head,
+ const unsigned char *_initial_data,size_t _initial_bytes);
+
+/**Open a stream from the given file path.
+   \param      _path  The path to the file to open.
+   \param[out] _error Returns 0 on success, or a failure code on error.
+                      You may pass in NULL if you don't want the failure code.
+                      The failure code will be #OP_EFAULT if the file could not
+                       be opened, or one of the other failure codes from
+                       op_open_callbacks() otherwise.
+   \return An #OggOpusFile pointer on success, or NULL on error.*/
+OggOpusFile *op_open_file(const char *_path,int *_error);
+
+/**Open a stream from a memory buffer.
+   \param      _data  The memory buffer to open.
+   \param      _size  The number of bytes in the buffer.
+   \param[out] _error Returns 0 on success, or a failure code on error.
+                      You may pass in NULL if you don't want the failure code.
+                      See op_open_callbacks() for a full list of failure codes.
+   \return An #OggOpusFile pointer on success, or NULL on error.*/
+OggOpusFile *op_open_memory(const unsigned char *_data,size_t _size,
+ int *_error);
+
+/**Open a stream using the given set of callbacks to access it.
+   \param _source        The stream to read from (e.g., a <code>FILE *</code>).
+   \param _cb            The callbacks with which to access the stream.
+                         <code><a href="#op_read_func">read()</a></code> must
+                          be implemented.
+                         <code><a href="#op_seek_func">seek()</a></code> and
+                          <code><a href="#op_tell_func">tell()</a></code> may
+                          be <code>NULL</code>, or may always return -1 to
+                          indicate a source is unseekable, but if
+                          <code><a href="#op_seek_func">seek()</a></code> is
+                          implemented and succeeds on a particular source, then
+                          <code><a href="#op_tell_func">tell()</a></code> must
+                          also.
+                         <code><a href="#op_close_func">close()</a></code> may
+                          be <code>NULL</code>, but if it is not, it will be
+                          called when the #OggOpusFile is destroyed by
+                          op_free().
+                         It will not be called if op_open_callbacks() fails
+                          with an error.
+   \param _initial_data  An initial buffer of data from the start of the
+                          stream.
+                         Applications can read some number of bytes from the
+                          start of the stream to help identify this as an Opus
+                          stream, and then provide them here to allow the
+                          stream to be opened, even if it is unseekable.
+   \param _initial_bytes The number of bytes in \a _initial_data.
+                         If the stream is seekable, its current position (as
+                          reported by
+                          <code><a href="#opus_tell_func">tell()</a></code>
+                          at the start of this function) must be equal to
+                          \a _initial_bytes.
+                         Otherwise, seeking to absolute positions will
+                          generate inconsistent results.
+   \param[out] _error    Returns 0 on success, or a failure code on error.
+                         You may pass in NULL if you don't want the failure
+                          code.
+                         The failure code will be one of
+                         <dl>
+                           <dt>#OP_EREAD</dt>
+                           <dd>An underlying read, seek, or tell operation
+                            failed when it should have succeeded, or we failed
+                            to find data in the stream we had seen before.</dd>
+                           <dt>#OP_EFAULT</dt>
+                           <dd>There was a memory allocation failure, or an
+                            internal library error.</dd>
+                           <dt>#OP_EIMPL</dt>
+                           <dd>The stream used a feature that is not
+                            implemented, such as an unsupported channel
+                            family.</dd>
+                           <dt>#OP_EINVAL</dt>
+                           <dd><code><a href="#op_seek_func">seek()</a></code>
+                            was implemented and succeeded on this source, but
+                            <code><a href="#op_tell_func">tell()</a></code>
+                            did not, or the starting position indicator was
+                            not equal to \a _initial_bytes.</dd>
+                           <dt>#OP_ENOTFORMAT</dt>
+                           <dd>The stream contained a link that did not have
+                            any logical Opus streams in it.</dd>
+                           <dt>#OP_EBADHEADER</dt>
+                           <dd>A required header packet was not properly
+                            formatted, contained illegal values, or was missing
+                            altogether.</dd>
+                           <dt>#OP_EVERSION</dt>
+                           <dd>An ID header contained an unrecognized version
+                            number.</dd>
+                           <dt>#OP_EBADLINK</dt>
+                           <dd>We failed to find data we had seen before after
+                            seeking.</dd>
+                           <dt>#OP_EBADTIMESTAMP</dt>
+                           <dd>The first or last timestamp in a link failed
+                            basic validity checks.</dd>
+                         </dl>
+   \return A freshly opened #OggOpusFile, or <code>NULL</code> on error.*/
+OggOpusFile *op_open_callbacks(void *_source,const OpusFileCallbacks *_cb,
+ const unsigned char *_initial_data,size_t _initial_bytes,int *_error);
+
+/**Release all memory used by an #OggOpusFile.*/
+void op_free(OggOpusFile *_of);
+
+/**Partially open a stream from the given file path.
+   \param      _path  The path to the file to open.
+   \param[out] _error Returns 0 on success, or a failure code on error.
+                      You may pass in NULL if you don't want the failure code.
+                      The failure code will be #OP_EFAULT if the file could not
+                       be opened, or one of the other failure codes from
+                       op_open_callbacks() otherwise.
+   \return An #OggOpusFile pointer on success, or NULL on error.*/
+OggOpusFile *op_test_file(const char *_path,int *_error);
+
+/**Partially open a stream from a memory buffer.
+   \param      _data  The memory buffer to open.
+   \param      _size  The number of bytes in the buffer.
+   \param[out] _error Returns 0 on success, or a failure code on error.
+                      You may pass in NULL if you don't want the failure code.
+                      See op_open_callbacks() for a full list of failure codes.
+   \return An #OggOpusFile pointer on success, or NULL on error.*/
+OggOpusFile *op_test_memory(const unsigned char *_data,size_t _size,
+ int *_error);
+
+/**Partially opens the Opus file.
+   This tests for Opusness and loads the headers for the first link.
+   It does not seek (although it tests for seekability).
+   Use op_test_open() to finish opening the file, or op_free() to dispose of
+    it.
+   \param _source        The stream to read from (e.g., a <code>FILE *</code>).
+   \param _cb            The callbacks with which to access the stream.
+                         <code><a href="#op_read_func">read()</a></code> must
+                          be implemented.
+                         <code><a href="#op_seek_func">seek()</a></code> and
+                          <code><a href="#op_tell_func">tell()</a></code> may
+                          be <code>NULL</code>, or may always return -1 to
+                          indicate a source is unseekable, but if
+                          <code><a href="#op_seek_func">seek()</a></code> is
+                          implemented and succeeds on a particular source, then
+                          <code><a href="#op_tell_func">tell()</a></code> must
+                          also.
+                         <code><a href="#op_close_func">close()</a></code> may
+                          be <code>NULL</code>, but if it is not, it will be
+                          called when the #OggOpusFile is destroyed by
+                          op_free().
+                         It will not be called if op_open_callbacks() fails
+                          with an error.
+   \param _initial_data  An initial buffer of data from the start of the
+                          stream.
+                         Applications can read some number of bytes from the
+                          start of the stream to help identify this as an Opus
+                          stream, and then provide them here to allow the
+                          stream to be tested more thoroughly, even if it is
+                          unseekable.
+   \param _initial_bytes The number of bytes in \a _initial_data.
+                         If the stream is seekable, its current position (as
+                          reported by
+                          <code><a href="#opus_tell_func">tell()</a></code>
+                          at the start of this function) must be equal to
+                          \a _initial_bytes.
+                         Otherwise, seeking to absolute positions will
+                          generate inconsistent results.
+   \param[out] _error    Returns 0 on success, or a failure code on error.
+                         You may pass in NULL if you don't want the failure
+                          code.
+                         See op_open_callbacks() for a full list of failure
+                          codes.
+   \return A freshly opened #OggOpusFile, or <code>NULL</code> on error.*/
+OggOpusFile *op_test_callbacks(void *_source,const OpusFileCallbacks *_cb,
+ const unsigned char *_initial_data,size_t _initial_bytes,int *_error);
+
+/**Finish opening a stream partially opened with op_test_callbacks() or one of
+    the associated convenience functions.
+   If this function fails, you are still responsible for freeing the
+    #OggOpusFile with op_free().
+   \return 0 on success, or a negative value on error.
+   \retval #OP_EREAD         An underlying read, seek, or tell operation failed
+                              when it should have succeeded.
+   \retval #OP_EFAULT        There was a memory allocation failure, or an
+                              internal library error.
+   \retval #OP_EIMPL         The stream used a feature that is not implemented,
+                              such as an unsupported channel family.
+   \retval #OP_EINVAL        The stream was not partially opened with
+                              op_test_callbacks() or one of the associated
+                              convenience functions.
+   \retval #OP_ENOTFORMAT    The stream contained a link that did not have any
+                              logical Opus streams in it.
+   \retval #OP_EBADHEADER    A required header packet was not properly
+                              formatted, contained illegal values, or was
+                              missing altogether.
+   \retval #OP_EVERSION      An ID header contained an unrecognized version
+                              number.
+   \retval #OP_EBADLINK      We failed to find data we had seen before after
+                              seeking.
+   \retval #OP_EBADTIMESTAMP The first or last timestamp in a link failed basic
+                              validity checks.*/
+int op_test_open(OggOpusFile *_of);
+
+/**Returns the number of links in this chained file.
+   \return For seekable sources, this returns the total number of links in the
+            whole file.
+           For unseekable sources, this always returns 1.*/
+int op_link_count(OggOpusFile *_of);
+
+/**Returns whether or not the data source being read is seekable.
+   This is true if
+   a) The seek and tell callbacks are both non-NULL,
+   b) The seek callback was successfully executed at least once, and
+   c) The tell callback was successfully able to report the position indicator
+    afterwards.
+   \return A non-zero value if seekable, and 0 if unseekable.*/
+int op_seekable(OggOpusFile *_of);
+
+/**Get the serial number of the given link in a (possibly-chained) Ogg Opus
+    stream.
+   \param _li The index of the link whose serial number should be retrieved.
+              Use a negative number to get the serial number of the current
+               link.
+   \return The serial number of the given link.
+           If \a _li is greater than the total number of links, this returns
+            the serial number of the last link.
+           If the source is not seekable, this always returns the serial number
+            of the current link.*/
+opus_uint32 op_serialno(OggOpusFile *_of,int _li);
+
+/**Get the channel count of the given link in a (possibly-chained Ogg Opus
+    stream.
+   This is equivalent to <code>op_head(_of,_li)->channel_count</code>, but
+    is provided for convenience.
+   \param _li The index of the link whose channel count should be retrieved.
+              Use a negative number to get the channel count of the current
+               link.
+   \return The channel count of the given link.
+           If \a _li is greater than the total number of links, this returns
+            the channel count of the last link.
+           If the source is not seekable, this always returns the channel count
+            of the current link.*/
+int op_channel_count(OggOpusFile *_of,int _li);
+
+/**Get the total (compressed) size of the stream, or of an individual link in
+    a (possibly-chained) Ogg Opus file, including all Ogg muxing overhead.
+   \param _li The index of the link whose compressed size should be computed.
+              Use a negative number to get the compressed size of the entire
+               stream.
+   \return The compressed size of the entire stream if \a _li is negative, the
+            compressed size of link \a _li if it is non-negative, or a negative
+            value on error.
+   \retval #OP_EINVAL The source is not seekable (so we can't know the length),
+                       \a _li wasn't less than the total number of links in
+                       the stream, or the stream was only partially open.*/
+opus_int64 op_raw_total(OggOpusFile *_of,int _li);
+
+/**Get the total PCM length (number of samples at 48 kHz) of the stream, or of
+    an individual link in a (possibly-chained) Ogg Opus file.
+   Users looking for <code>op_time_total()</code> should use op_pcm_total()
+    instead.
+   Because timestamps in Opus are fixed at 48 kHz, there is no need for a
+    separate function to convert this to seconds (and leaving it out avoids
+    introducing floating point to the API, for those that wish to avoid it).
+   \param _li The index of the link whose PCM length should be computed.
+              Use a negative number to get the PCM length of the entire stream.
+   \return The PCM length of the entire stream if \a _li is negative, the PCM
+            length of link \a _li if it is non-negative, or a negative value on
+            error.
+   \retval #OP_EINVAL The source is not seekable (so we can't know the length),
+                       \a _li wasn't less than the total number of links in
+                       the stream, or the stream was only partially open.*/
+ogg_int64_t op_pcm_total(OggOpusFile *_of,int i);
+
+/**Get the ID header information for the given link in a (possibly chained) Ogg
+    Opus stream.
+   \param _li The index of the link whose ID header information should be
+               retrieved.
+              Use a negative number to get the ID header information of the
+               current link.
+              For an unseekable stream, \a _li is ignored, and the ID header
+               information for the current link is always returned, if
+               available.
+   \return The contents of the ID header for the given link, or
+            <code>NULL</code> if either \a _li is larger than the number of
+            links or the current link was requested, but the stream was only
+            partially open.*/
+const OpusHead *op_head(OggOpusFile *_of,int _li);
+
+/**Get the comment header information for the given link in a (possibly
+    chained) Ogg Opus stream.
+   \param _li The index of the link whose comment header information should be
+               retrieved.
+              Use a negative number to get the comment header information of
+               the current link.
+              For an unseekable stream, \a _li is ignored, and the comment
+               header information for the current link is always returned, if
+               available.
+   \return The contents of the comment header for the given link, or
+            <code>NULL</code> if either \a _li is larger than the number of
+            links or the current link was requested, but the stream was only
+            partially open.*/
+const OpusTags *op_tags(OggOpusFile *_of,int _li);
+
+/**Computes the bitrate for a given link in a (possibly chained) Ogg Opus
+    stream.
+   The stream must be seekable to compute the bitrate.
+   For unseekable streams, use op_bitrate_instant() to get periodic estimates.
+   \param _li The index of the link whose bitrate should be computed.
+              USe a negative number to get the bitrate of the whole stream.
+   \return The bitrate on success, or a negative value on error.
+   \retval #OP_EINVAL The stream was not fully open, the stream was not
+                       seekable, or \a _li was larger than the number of
+                       links.*/
+opus_int32 op_bitrate(OggOpusFile *_of,int _li);
+
+/**Compute the instantaneous bitrate, measured as the ratio of bits to playable
+    samples decoed since a) the last call to op_bitrate_instant(), b) the last
+    seek, or c) the start of playback, whichever was most recent.
+   This will spike somewhat after a seek or at the start/end of a chain
+    boundary, as pre-skip, pre-roll, and end-trimming causes samples to be
+    decoded but not played.
+   \return The bitrate, in bits per second, or a negative value on error.
+   \retval #OP_EFALSE No data has been decoded since any of the events
+                       described above.
+   \retval #OP_EINVAL The stream was not fully open.*/
+opus_int32 op_bitrate_instant(OggOpusFile *_of);
+
+/**Seek to a byte offset relative to the <b>compressed</b> data.
+   This also scans packets to update the PCM cursor.
+   It will cross a logical bitstream boundary, but only if it can't get any
+    packets out of the tail of the link to which it seeks.
+   \param _byte_offset The byte position to seek to.
+   \return 0 on success, or a negative error code on failure.
+   \retval #OP_EREAD    The seek failed.
+   \retval #OP_EINVAL   The stream was not fully open, or the target was
+                         outside the valid range for the stream.
+   \retval #OP_ENOSEEK  This stream is not seekable.
+   \retval #OP_EBADLINK Failed to initialize a decoder for a stream for an
+                         unknown reason.*/
+int op_raw_seek(OggOpusFile *_of,opus_int64 _byte_offset);
+
+/**Seek to a page preceding the specified PCM offset, such that decoding will
+    quickly arrive at the requested position.
+   This is faster than sample-granularity seeking because it doesn't do the
+    last bit of decode to find a specific sample.
+   \param _pcm_offset The PCM offset to seek to.
+                      This is in samples at 48 kHz relative to the start of the
+                       stream.
+   \return 0 on success, or a negative value on error.
+   \retval #OP_EREAD   The seek failed.
+   \retval #OP_EINVAL  The stream was not fully open, or the target was outside
+                        the valid range for the stream.
+   \retval #OP_ENOSEEK This stream is not seekable.*/
+int op_pcm_seek_page(OggOpusFile *_of,ogg_int64_t _pcm_offset);
+
+/**Seek to the specified PCM offset, such that decoding will begin at exactly
+    the requested position.
+   \param _pcm_offset The PCM offset to seek to.
+                      This is in samples at 48 kHz relative to the start of the
+                       stream.
+   \return 0 on success, or a negative value on error.
+   \retval #OP_EREAD   The seek failed.
+   \retval #OP_EINVAL  The stream was not fully open, or the target was outside
+                        the valid range for the stream.
+   \retval #OP_ENOSEEK This stream is not seekable.*/
+int op_pcm_seek(OggOpusFile *_of,ogg_int64_t _pcm_offset);
+
+/**Obtain the current value of the position indicator for \a _of.
+   \return The byte position that is currently being read from.*/
+opus_int64 op_raw_tell(OggOpusFile *_of);
+/**Obtain the PCM offset of the next sample to be read.
+   If the file is not properly timestamped, this might not increment by the
+    proper amount between reads, or even return monotonically increasing
+    values.
+   \return The PCM offset of the next sample to be read.*/
+ogg_int64_t op_pcm_tell(OggOpusFile *_of);
+
+/**Reads more samples from the stream.
+   \param[out] _pcm      A buffer in which to store the output PCM samples, as
+                          signed native-endian 16-bit values with a nominal
+                          range of <code>[-32768,32767)</code>.
+                         Multiple channels are interleaved using the
+                          <a href="http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9">Vorbis
+                          channel ordering</a>.
+                         This must have room for at least \a _buf_size values.
+   \param      _buf_size The number of values that can be stored in \a _pcm.
+                         It is reccommended that this be large enough for at
+                          least 120 ms of data at 48 kHz per channel (5760
+                          values per channel).
+                         Smaller buffers will simply return less data, possibly
+                          consuming more memory to buffer the data internally.
+   \param[out] _li       The index of the link this data was decoded from.
+                         You may pass NULL if you do not need this information.
+                         If this function fails (returning a negative value),
+                          this parameter is left unset.
+   \return The number of samples read per channel on success, or a negative
+            value on failure.
+           The channel count can be retrieved on success by calling
+            <code>op_head(_of,*_li)</code>.
+           The number of samples returned may be 0 if the buffer was too small
+            to store even a single sample for all channels, or if end of file
+            was reached.
+           The list of possible failure codes follows.
+           Most of them can only be returned by unseekable, chained streams
+            that encounter a new link.
+   \retval #OP_EFAULT        An internal memory allocation failed.
+   \retval #OP_EIMPL         An unseekable stream encountered a new link that
+                              used a feature that is not implemented, such as
+                              an unsupported channel family.
+   \retval #OP_EINVAL        The stream was not fully open.
+   \retval #OP_ENOTFORMAT    An unseekable stream encountered a new link that
+                              contained a link that did not have any logical
+                              Opus streams in it.
+   \retval #OP_EBADHEADER    An unseekable stream encountered a new link with a
+                              required header packet that was not properly
+                              formatted, contained illegal values, or was
+                              missing altogether.
+   \retval #OP_EVERSION      An unseekable stream encountered a new link with
+                              an ID header that contained an unrecognized
+                              version number.
+   \retval #OP_EBADPACKET    Failed to properly decode the next packet.
+   \retval #OP_EBADTIMESTAMP An unseekable stream encountered a new link with
+                              a starting timestamp that failed basic validity
+                              checks.*/
+int op_read(OggOpusFile *_of,opus_int16 *_pcm,int _buf_size,int *_li);
+
+/**Reads more samples from the stream.
+   \param[out] _pcm      A buffer in which to store the output PCM samples as
+                          signed floats with a nominal range of
+                          <code>[-1.0,1.0]</code>.
+                         Multiple channels are interleaved using the
+                          <a href="http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-800004.3.9">Vorbis
+                          channel ordering</a>.
+                         This must have room for at least \a _buf_size floats.
+   \param      _buf_size The number of floats that can be stored in \a _pcm.
+                         It is reccommended that this be large enough for at
+                          least 120 ms of data at 48 kHz per channel (5760
+                          samples per channel).
+                         Smaller buffers will simply return less data, possibly
+                          consuming more memory to buffer the data internally.
+   \param[out] _li       The index of the link this data was decoded from.
+                         You may pass NULL if you do not need this information.
+                         If this function fails (returning a negative value),
+                          this parameter is left unset.
+   \return The number of samples read per channel on success, or a negative
+            value on failure.
+           The channel count can be retrieved on success by calling
+            <code>op_head(_of,*_li)</code>.
+           The number of samples returned may be 0 if the buffer was too small
+            to store even a single sample for all channels, or if end of file
+            was reached.
+           The list of possible failure codes follows.
+           Most of them can only be returned by unseekable, chained streams
+            that encounter a new link.
+   \retval #OP_EFAULT        An internal memory allocation failed.
+   \retval #OP_EIMPL         An unseekable stream encountered a new link that
+                              used a feature that is not implemented, such as
+                              an unsupported channel family.
+   \retval #OP_EINVAL        The stream was not fully open.
+   \retval #OP_ENOTFORMAT    An unseekable stream encountered a new link that
+                              contained a link that did not have any logical
+                              Opus streams in it.
+   \retval #OP_EBADHEADER    An unseekable stream encountered a new link with a
+                              required header packet that was not properly
+                              formatted, contained illegal values, or was
+                              missing altogether.
+   \retval #OP_EVERSION      An unseekable stream encountered a new link with
+                              an ID header that contained an unrecognized
+                              version number.
+   \retval #OP_EBADPACKET    Failed to properly decode the next packet.
+   \retval #OP_EBADTIMESTAMP An unseekable stream encountered a new link with
+                              a starting timestamp that failed basic validity
+                              checks.*/
+int op_read_float(OggOpusFile *_of,float *_pcm,int _buf_size,int *_li);
+
+long op_read_filter(OggOpusFile *_of,char *buffer,int length,
+                          int bigendianp,int word,int sgned,int *bitstream,
+                          void (*filter)(float **pcm,long channels,long samples,void *filter_param),void *filter_param);
+
+# if OP_GNUC_PREREQ(4,0)
+#  pragma GCC visibility pop
+# endif
+
+# if defined(__cplusplus)
+}
+# endif
+
+#endif
--- /dev/null
+++ b/src/info.c
@@ -1,0 +1,170 @@
+#include "internal.h"
+#include <limits.h>
+#include <string.h>
+
+static unsigned op_parse_uint16le(const unsigned char *_data){
+  return _data[0]|_data[1]<<8;
+}
+
+static int op_parse_int16le(const unsigned char *_data){
+  int ret;
+  ret=_data[0]|_data[1]<<8;
+  return (ret^0x8000)-0x8000;
+}
+
+static opus_uint32 op_parse_uint32le(const unsigned char *_data){
+  return _data[0]|_data[1]<<8|_data[2]<<16|_data[3]<<24;
+}
+
+int opus_head_parse(OpusHead *_head,const unsigned char *_data,size_t _len){
+  OpusHead head;
+  if(_len<8)return OP_ENOTFORMAT;
+  if(memcmp(_data,"OpusHead",8)!=0)return OP_ENOTFORMAT;
+  if(_len<9)return OP_EBADHEADER;
+  head.version=_data[8];
+  if(head.version>15)return OP_EVERSION;
+  if(_len<19)return OP_EBADHEADER;
+  head.channel_count=_data[9];
+  head.pre_skip=op_parse_uint16le(_data+10);
+  head.input_sample_rate=op_parse_uint32le(_data+12);
+  head.output_gain=op_parse_int16le(_data+16);
+  head.mapping_family=_data[18];
+  if(head.mapping_family==0){
+    if(head.channel_count<1||head.channel_count>2)return OP_EBADHEADER;
+    if(head.version<=1&&_len>19)return OP_EBADHEADER;
+    head.stream_count=1;
+    head.coupled_count=head.channel_count-1;
+    if(_head!=NULL){
+      _head->mapping[0]=0;
+      _head->mapping[1]=1;
+    }
+  }
+  else if(head.mapping_family==1){
+    size_t size;
+    int    ci;
+    if(head.channel_count<1||head.channel_count>8)return OP_EBADHEADER;
+    size=21+head.channel_count;
+    if(_len<size||head.version<=1&&_len>size)return OP_EBADHEADER;
+    head.stream_count=_data[19];
+    if(head.stream_count<1)return OP_EBADHEADER;
+    head.coupled_count=_data[20];
+    if(head.coupled_count>head.stream_count)return OP_EBADHEADER;
+    for(ci=0;ci<head.channel_count;ci++){
+      if(_data[21+ci]>=head.stream_count+head.coupled_count
+       &&_data[21+ci]!=255){
+        return OP_EBADHEADER;
+      }
+    }
+    if(_head!=NULL)memcpy(_head->mapping,_data+21,head.channel_count);
+  }
+  /*General purpose players should not attempt to play back content with
+     channel mapping family 255.*/
+  else if(head.mapping_family==255)return OP_EIMPL;
+  /*No other channel mapping families are currently defined.*/
+  else return OP_EBADHEADER;
+  if(_head!=NULL)memcpy(_head,&head,head.mapping-(unsigned char *)&head);
+  return 0;
+}
+
+void opus_tags_init(OpusTags *_tags){
+  memset(_tags,0,sizeof(*_tags));
+}
+
+void opus_tags_clear(OpusTags *_tags){
+  int i;
+  for(i=_tags->comments;i-->0;)_ogg_free(_tags->user_comments[i]);
+  _ogg_free(_tags->user_comments);
+  _ogg_free(_tags->comment_lengths);
+  _ogg_free(_tags->vendor);
+}
+
+/*The actual implementation of opus_tags_parse().
+  Unlike the public API, this function requires _tags to already be
+   initialized, modifies its contents before success is guaranteed, and assumes
+   the caller will clear it on error.*/
+int opus_tags_parse_impl(OpusTags *_tags,
+ const unsigned char *_data,size_t _len){
+  opus_uint32 count;
+  size_t      size;
+  size_t      len;
+  int         ncomments;
+  int         i;
+  len=_len;
+  if(len<8)return OP_ENOTFORMAT;
+  if(memcmp(_data,"OpusTags",8)!=0)return OP_ENOTFORMAT;
+  if(len<16)return OP_EBADHEADER;
+  _data+=8;
+  len-=8;
+  count=op_parse_uint32le(_data);
+  _data+=4;
+  len-=4;
+  if(count>len)return OP_EBADHEADER;
+  if(_tags!=NULL){
+    char *vendor;
+    size=count+1;
+    if(size<count)return OP_EFAULT;
+    vendor=(char *)_ogg_malloc(size);
+    if(vendor==NULL)return OP_EFAULT;
+    memcpy(vendor,_data,count);
+    vendor[count]='\0';
+    _tags->vendor=vendor;
+  }
+  _data+=count;
+  len-=count;
+  if(len<4)return OP_EBADHEADER;
+  count=op_parse_uint32le(_data);
+  _data+=4;
+  len-=4;
+  /*Check to make sure there's minimally sufficient data left in the packet.*/
+  if(count>len>>2)return OP_EBADHEADER;
+  /*Check for overflow (the API limits this to an int).*/
+  if(count>(opus_uint32)INT_MAX)return OP_EFAULT;
+  if(_tags!=NULL){
+    size=sizeof(*_tags->comment_lengths)*count;
+    if(size/sizeof(*_tags->comment_lengths)!=count)return OP_EFAULT;
+    _tags->comment_lengths=(int *)_ogg_malloc(size);
+    size=sizeof(*_tags->user_comments)*count;
+    if(size/sizeof(*_tags->user_comments)!=count)return OP_EFAULT;
+    _tags->user_comments=(char **)_ogg_malloc(size);
+    if(_tags->comment_lengths==NULL||_tags->user_comments==NULL){
+      return OP_EFAULT;
+    }
+  }
+  ncomments=(int)count;
+  for(i=0;i<ncomments;i++){
+    /*Check to make sure there's minimally sufficient data left in the packet.*/
+    if((size_t)(ncomments-i)>len>>2)return OP_EBADHEADER;
+    count=op_parse_uint32le(_data);
+    _data+=4;
+    len-=4;
+    if(count>len)return OP_EBADHEADER;
+    /*Check for overflow (the API limits this to an int).*/
+    if(count>(opus_uint32)INT_MAX)return OP_EFAULT;
+    if(_tags!=NULL){
+      _tags->comment_lengths[i]=(int)count;
+      size=count+1;
+      if(size<count)return OP_EFAULT;
+      _tags->user_comments[i]=(char *)_ogg_malloc(size);
+      if(_tags->user_comments[i]==NULL)return OP_EFAULT;
+      _tags->comments=i+1;
+      memcpy(_tags->user_comments[i],_data,count);
+      _tags->user_comments[i][count]='\0';
+    }
+    _data+=count;
+    len-=count;
+  }
+  return 0;
+}
+
+int opus_tags_parse(OpusTags *_tags,const unsigned char *_data,size_t _len){
+  if(_tags!=NULL){
+    OpusTags tags;
+    int      ret;
+    opus_tags_init(&tags);
+    ret=opus_tags_parse_impl(&tags,_data,_len);
+    if(ret<0)opus_tags_clear(&tags);
+    else *_tags=*&tags;
+    return ret;
+  }
+  else return opus_tags_parse_impl(NULL,_data,_len);
+}
--- /dev/null
+++ b/src/internal.c
@@ -1,0 +1,9 @@
+#include "internal.h"
+
+#if defined(OP_ENABLE_ASSERTIONS)
+void op_fatal_impl(const char *_str,const char *_file,int _line){
+  fprintf(stderr,"Fatal (internal) error in %s, line %i: %s\n",
+   _file,_line,_str);
+  abort();
+}
+#endif
--- /dev/null
+++ b/src/internal.h
@@ -1,0 +1,185 @@
+#if !defined(_opusfile_internal_h)
+# define _opusfile_internal_h (1)
+
+# if !defined(_REENTRANT)
+#  define _REENTRANT
+# endif
+# if !defined(_GNU_SOURCE)
+#  define _GNU_SOURCE
+# endif
+# if !defined(_LARGEFILE_SOURCE)
+#  define _LARGEFILE_SOURCE
+# endif
+# if !defined(_LARGEFILE64_SOURCE)
+#  define _LARGEFILE64_SOURCE
+# endif
+# if !defined(_FILE_OFFSET_BITS)
+#  define _FILE_OFFSET_BITS 64
+# endif
+
+# include <stdlib.h>
+# include <opus/opusfile.h>
+
+typedef struct OggOpusLink OggOpusLink;
+# if defined(OP_FIXED_POINT)
+typedef opus_int16 op_sample;
+# else
+typedef float      op_sample;
+# endif
+
+# if OP_GNUC_PREREQ(3,0)
+/*Another alternative is
+    (__builtin_constant_p(_x)?!!(_x):__builtin_expect(!!(_x),1))
+   but that evaluates _x multiple times, which may be bad.*/
+#  define OP_LIKELY(_x) (__builtin_expect(!!(_x),1))
+#  define OP_UNLIKELY(_x) (__builtin_expect(!!(_x),0))
+# else
+#  define OP_LIKELY(_x)   (!!(_x))
+#  define OP_UNLIKELY(_x) (!!(_x))
+# endif
+
+# define OP_INT64_MAX ((ogg_int64_t)0x7FFFFFFFFFFFFFFFLL)
+# define OP_INT64_MIN (-OP_INT64_MAX-1)
+
+/*The maximum channel count for any mapping we'll actually decode.*/
+# define OP_NCHANNELS_MAX (8)
+
+# if defined(OP_ENABLE_ASSERTIONS)
+#  if OP_GNUC_PREREQ(2,5)||__SUNPRO_C>=0x590
+__attribute__((noreturn))
+#  endif
+void op_fatal_impl(const char *_str,const char *_file,int _line);
+
+#  define OP_FATAL(_str) (op_fatal_impl(_str,__FILE__,__LINE__))
+
+#  define OP_ASSERT(_cond) \
+  do{ \
+    if(OP_UNLIKELY(!(_cond)))OP_FATAL("assertion failed: " #_cond); \
+  } \
+  while(0)
+
+# else
+#  define OP_FATAL(_str) abort()
+#  define OP_ASSERT(_cond)
+# endif
+
+# define OP_MIN(_a,_b)        ((_a)<(_b)?(_a):(_b))
+# define OP_MAX(_a,_b)        ((_a)>(_b)?(_a):(_b))
+# define OP_CLAMP(_lo,_x,_hi) (OP_MAX(_lo,OP_MIN(_x,_hi)))
+
+/*Initial state.*/
+# define  OP_NOTOPEN   (0)
+/*We've found the first Opus stream in the first link.*/
+# define  OP_PARTOPEN  (1)
+# define  OP_OPENED    (2)
+/*We've found the first Opus stream in the current link.*/
+# define  OP_STREAMSET (3)
+/*We've initialized the decoder for the chosen Opus stream in the current
+   link.*/
+# define  OP_INITSET   (4)
+
+/*Information cached for a single link in a chained Ogg Opus file.
+  We choose the first Opus stream encountered in each link to play back (and
+   require at least one).*/
+struct OggOpusLink{
+  /*The byte offset of the first header page in this link.*/
+  opus_int64   offset;
+  /*The byte offset of the first data page from the chosen Opus stream in this
+     link (after the headers).*/
+  opus_int64   data_offset;
+  /*The byte offset of the last page from the chosen Opus stream in this link.
+    This is used when seeking to ensure we find a page before the last one, so
+     that end-trimming calculations work properly.
+    This is only valid for seekable sources.*/
+  opus_int64   end_offset;
+  /*The granule position of the last sample.
+    This is only valid for seekable sources.*/
+  ogg_int64_t  pcm_end;
+  /*The granule position before the first sample.*/
+  ogg_int64_t  pcm_start;
+  /*The serial number.*/
+  ogg_uint32_t serialno;
+  /*The contents of the info header.*/
+  OpusHead     head;
+  /*The contents of the comment header.*/
+  OpusTags     tags;
+};
+
+struct OggOpusFile{
+  /*The callbacks used to access the data source.*/
+  OpusFileCallbacks  callbacks;
+  /*A FILE *, memory bufer, etc.*/
+  void              *source;
+  /*Whether or not we can seek with this data source.*/
+  int                seekable;
+  /*The number of links in this chained Ogg Opus file.*/
+  int                nlinks;
+  /*The cached information from each link in a chained Ogg Opus file.
+    If source isn't seekable (e.g., it's a pipe), only the current link
+     appears.*/
+  OggOpusLink       *links;
+  /*The number of serial numbers from a single link.*/
+  int                nserialnos;
+  /*The capacity of the list of serial numbers from a single link.*/
+  int                cserialnos;
+  /*Storage for the list of serial numbers from a single link.*/
+  ogg_uint32_t      *serialnos;
+  /*This is the current offset of the data processed by the ogg_sync_state.
+    After a seek, this should be set to the target offset so that we can track
+     the byte offsets of subsequent pages.
+    After a call to op_get_next_page(), this will point to the first byte after
+     that page.*/
+  opus_int64         offset;
+  /*The total size of this data source, or -1 if it's unseekable.*/
+  opus_int64         end;
+  /*Used to locate pages in the data source.*/
+  ogg_sync_state     oy;
+  /*One of OP_NOTOPEN, OP_PARTOPEN, OP_OPENED, OP_STREAMSET, OP_INITSET.*/
+  int                ready_state;
+  /*The current link being played back.*/
+  int                cur_link;
+  /*The number of decoded samples to discard from the start of decoding.*/
+  opus_int32         cur_discard_count;
+  /*The granule position of the previous packet (current packet start time).*/
+  ogg_int64_t        prev_packet_gp;
+  /*The number of bytes read since the last bitrate query, including framing.*/
+  opus_int64         bytes_tracked;
+  /*The number of samples decoded since the last bitrate query.*/
+  ogg_int64_t        samples_tracked;
+  /*Takes physical pages and welds them into a logical stream of packets.*/
+  ogg_stream_state   os;
+  /*Re-timestamped packets from a single page.
+    Buffering these relies on the undocumented libogg behavior that ogg_packet
+     pointers remain valid until the next page is submitted to the
+     ogg_stream_state they came from.*/
+  ogg_packet         op[255];
+  /*The index of the next packet to return.*/
+  int                op_pos;
+  /*The total number of packets available.*/
+  int                op_count;
+  /*Central working state for the packet-to-PCM decoder.*/
+  OpusMSDecoder     *od;
+  /*The stream count used to initialize the decoder.*/
+  int                od_stream_count;
+  /*The coupled stream count used to initialize the decoder.*/
+  int                od_coupled_count;
+  /*The channel count used to initialize the decoder.*/
+  int                od_channel_count;
+  /*The channel mapping used to initialize the decoder.*/
+  unsigned char      od_mapping[OP_NCHANNELS_MAX];
+  /*The buffered data for one decoded packet.*/
+  op_sample         *od_buffer;
+  /*The current position in the decoded buffer.*/
+  int                od_buffer_pos;
+  /*The number of valid samples in the decoded buffer.*/
+  int                od_buffer_size;
+  /*Internal state for dithering float->short output.*/
+#if !defined(OP_FIXED_POINT)
+  float              dither_a[OP_NCHANNELS_MAX*4];
+  float              dither_b[OP_NCHANNELS_MAX*4];
+  int                dither_mute;
+  opus_uint32        dither_seed;
+#endif
+};
+
+#endif
--- /dev/null
+++ b/src/opusfile.c
@@ -1,0 +1,2441 @@
+/********************************************************************
+ *                                                                  *
+ * THIS FILE IS PART OF THE libopusfile SOFTWARE CODEC SOURCE CODE. *
+ * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
+ * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
+ * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
+ *                                                                  *
+ * THE libopusfile SOURCE CODE IS (C) COPYRIGHT 1994-2009           *
+ * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
+ *                                                                  *
+ ********************************************************************
+
+ function: stdio-based convenience library for opening/seeking/decoding
+ last mod: $Id: vorbisfile.c 17573 2010-10-27 14:53:59Z xiphmont $
+
+ ********************************************************************/
+#include "internal.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <limits.h>
+#include <string.h>
+#include <math.h>
+
+#include "opus/opusfile.h"
+
+/*This implementation is largely based off of libvorbisfile.
+  All of the Ogg bits work roughly the same, though I have made some
+   "improvements" that have not been folded back there, yet.*/
+
+/*A 'chained bitstream' is an Ogg Opus bitstream that contains more than one
+   logical bitstream arranged end to end (the only form of Ogg multiplexing
+   supported by this library.
+  Grouping (parallel multiplexing) is not supported, except to the extent that
+   if there are multiple logical Ogg streams in a single link of the chain, we
+   will ignore all but the first Opus stream we find.*/
+
+/*An Ogg Opus file can be played beginning to end (streamed) without worrying
+   ahead of time about chaining (see opusdec from the opus-tools package).
+  If we have the whole file, however, and want random access
+   (seeking/scrubbing) or desire to know the total length/time of a file, we
+   need to account for the possibility of chaining.*/
+
+/*We can handle things a number of ways.
+  We can determine the entire bitstream structure right off the bat, or find
+   pieces on demand.
+  This library determines and caches structure for the entire bitstream, but
+   builds a virtual decoder on the fly when moving between links in the chain.*/
+
+/*There are also different ways to implement seeking.
+  Enough information exists in an Ogg bitstream to seek to sample-granularity
+   positions in the output.
+  Or, one can seek by picking some portion of the stream roughly in the desired
+   area if we only want coarse navigation through the stream.
+  We implement and expose both strategies.*/
+
+/*Many, many internal helpers.
+  The intention is not to be confusing.
+  Rampant duplication and monolithic function implementation (though we do have
+   some large, omnibus functions still) would be harder to understand anyway.
+  The high level functions are last.
+  Begin grokking near the end of the file if you prefer to read things
+   top-down.*/
+
+/*The maximum number of bytes in a page (including the page headers).*/
+#define OP_PAGE_SIZE      (65307)
+/*The default amount to seek backwards per step when trying to find the
+   previous page.
+  This must be at least as large as the maximum size of a page.*/
+#define OP_CHUNK_SIZE     (65536)
+/*The maximum amount to seek backwards per step when trying to find the
+   previous page.*/
+#define OP_CHUNK_SIZE_MAX (1024*1024)
+/*A smaller read size is needed for low-rate streaming.*/
+#define OP_READ_SIZE      (2048)
+
+int op_test(OpusHead *_head,
+ const unsigned char *_initial_data,size_t _initial_bytes){
+  ogg_sync_state  oy;
+  char           *data;
+  int             err;
+  /*The first page of a normal Opus file will be at most 57 bytes (27 Ogg
+     page header bytes + 1 lacing value + 21 Opus header bytes + 8 channel
+     mapping bytes).
+    It will be at least 47 bytes (27 Ogg page header bytes + 1 lacing value +
+     19 Opus header bytes using channel mapping family 0).
+    If we don't have at least that much data, give up now.*/
+  if(_initial_bytes<47)return OP_FALSE;
+  /*Only proceed if we start with the magic OggS string.
+    This is to prevent us spending a lot of time allocating memory and looking
+     for Ogg pages in non-Ogg files.*/
+  if(memcmp(_initial_data,"OggS",4)!=0)return OP_ENOTFORMAT;
+  ogg_sync_init(&oy);
+  data=ogg_sync_buffer(&oy,_initial_bytes);
+  if(data!=NULL){
+    ogg_stream_state os;
+    ogg_page         og;
+    int              ret;
+    memcpy(data,_initial_data,_initial_bytes);
+    ogg_sync_wrote(&oy,_initial_bytes);
+    ogg_stream_init(&os,-1);
+    err=OP_FALSE;
+    do{
+      ogg_packet op;
+      ret=ogg_sync_pageout(&oy,&og);
+      /*Ignore holes.*/
+      if(ret<0)continue;
+      /*Stop if we run out of data.*/
+      if(!ret)break;
+      ogg_stream_reset_serialno(&os,ogg_page_serialno(&og));
+      ogg_stream_pagein(&os,&og);
+      /*Only process the first packet on this page (if it's a BOS packet,
+         it's required to be the only one).*/
+      if(ogg_stream_packetout(&os,&op)==1){
+        if(op.b_o_s){
+          ret=opus_head_parse(_head,op.packet,op.bytes);
+          /*If this didn't look like Opus, keep going.*/
+          if(ret==OP_ENOTFORMAT)continue;
+          /*Otherwise we're done, one way or another.*/
+          err=ret;
+        }
+        /*We finished parsing the headers.
+          There is no Opus to be found.*/
+        else err=OP_ENOTFORMAT;
+      }
+    }
+    while(err==OP_FALSE);
+    ogg_stream_clear(&os);
+  }
+  else err=OP_EFAULT;
+  ogg_sync_clear(&oy);
+  return err;
+}
+
+/*Read a little more data from the file/pipe into the ogg_sync framer.*/
+static int op_get_data(OggOpusFile *_of){
+  char *buffer;
+  int   bytes;
+  buffer=ogg_sync_buffer(&_of->oy,OP_READ_SIZE);
+  bytes=(int)(*_of->callbacks.read)(buffer,
+   1,OP_READ_SIZE,_of->source);
+  if(OP_LIKELY(bytes>0)){
+    ogg_sync_wrote(&_of->oy,bytes);
+    return bytes;
+  }
+  return OP_EREAD;
+}
+
+/*Save a tiny smidge of verbosity to make the code more readable.*/
+static int op_seek_helper(OggOpusFile *_of,opus_int64 _offset){
+  if(_of->callbacks.seek==NULL||
+   (*_of->callbacks.seek)(_of->source,_offset,SEEK_SET)){
+    return OP_EREAD;
+  }
+  _of->offset=_offset;
+  ogg_sync_reset(&_of->oy);
+  return 0;
+}
+
+/*The read/seek functions track absolute position within the stream.*/
+
+/*From the head of the stream, get the next page.
+  _boundary specifies if the function is allowed to fetch more data from the
+   stream (and how much) or only use internally buffered data.
+  _boundary: -1) Unbounded search.
+              0) Read no additional data.
+                 Use only cached data.
+              n) Search for the start of a new page for n bytes.
+  Return: n>=0)     Found a page at absolute offset n.
+          OP_FALSE) Hit the _boundary limit.
+          OP_EREAD) Failed to read more data.*/
+static opus_int64 op_get_next_page(OggOpusFile *_of,ogg_page *_og,
+ opus_int64 _boundary){
+  if(_boundary>0)_boundary+=_of->offset;
+  for(;;){
+    int more;
+    if(_boundary>0&&_of->offset>=_boundary)return OP_FALSE;
+    more=ogg_sync_pageseek(&_of->oy,_og);
+    /*Skipped (-more) bytes.*/
+    if(OP_UNLIKELY(more<0))_of->offset-=more;
+    else if(more==0){
+      int ret;
+      /*Send more paramedics.*/
+      if(!_boundary)return OP_FALSE;
+      ret=op_get_data(_of);
+      if(OP_UNLIKELY(ret<0))return ret;
+    }
+    else{
+      /*Got a page.
+        Return the offset at the page beginning, advance the internal offset
+         past the page end.*/
+      opus_int64 page_offset;
+      page_offset=_of->offset;
+      _of->offset+=more;
+      return page_offset;
+    }
+  }
+}
+
+#if 0
+/*Find the last page beginning before the current stream cursor position with a
+   valid granule position.
+  This is much dirtier than the above, as Ogg doesn't have any backward search
+   linkage.
+  There is no '_boundary' parameter as it will have to read more data.
+  Return: The offset of the start of the page, or a negative value on failure.
+          OP_EREAD:    Failed to read more data (error or EOF).
+          OP_EBADLINK: We read a page, and then went back and couldn't find it
+                        again.*/
+static opus_int64 op_get_prev_page(OggOpusFile *_of,ogg_page *_og){
+  opus_int64 begin;
+  opus_int64 end;
+  opus_int64 llret;
+  opus_int64 page_offset;
+  opus_int64 original_end;
+  opus_int32 chunk_size;
+  int        have_page;
+  int        ret;
+  original_end=end=begin=_of->offset;
+  page_offset=-1;
+  have_page=0;
+  chunk_size=OP_CHUNK_SIZE;
+  do{
+    OP_ASSERT(chunk_size>=OP_PAGE_SIZE);
+    begin=OP_MAX(begin-chunk_size,0);
+    ret=op_seek_helper(_of,begin);
+    if(OP_UNLIKELY(ret<0))return ret;
+    while(_of->offset<end){
+      llret=op_get_next_page(_of,_og,end-_of->offset);
+      if(OP_UNLIKELY(llret<OP_FALSE))return llret;
+      else if(llret==OP_FALSE){
+        have_page=0;
+        break;
+      }
+      /*We're only doing this search to get a timestamp, so keep going if the
+         granule position is not valid.*/
+      if(ogg_page_granulepos(_og)==-1)continue;
+      page_offset=llret;
+      have_page=1;
+    }
+    chunk_size=OP_MIN(2*chunk_size,OP_CHUNK_SIZE_MAX);
+    /*Avoid quadratic complexity if we hit an invalid patch of the file.*/
+    end=OP_MIN(begin+OP_PAGE_SIZE-1,original_end);
+  }
+  while(page_offset==-1);
+  /*In a fully compliant, non-multiplexed stream, we'll still be holding the
+     last page.
+    In multiplexed (or noncompliant streams), we will probably have to re-read
+     the last page we saw.*/
+  if(!have_page){
+    ret=op_seek_helper(_of,page_offset);
+    if(OP_UNLIKELY(ret<0))return ret;
+    llret=op_get_next_page(_of,_og,OP_CHUNK_SIZE);
+    if(OP_UNLIKELY(llret<0)||OP_UNLIKELY(ogg_page_granulepos(_og)==-1)){
+      /*This shouldn't be possible unless the stream data changed out from
+         under us.*/
+      return OP_EBADLINK;
+    }
+  }
+  return page_offset;
+}
+#endif
+
+static int op_add_serialno(ogg_page *_og,
+ ogg_uint32_t **_serialnos,int *_nserialnos,int *_cserialnos){
+  ogg_uint32_t *serialnos;
+  int           nserialnos;
+  int           cserialnos;
+  ogg_uint32_t s;
+  s=ogg_page_serialno(_og);
+  serialnos=*_serialnos;
+  nserialnos=*_nserialnos;
+  cserialnos=*_cserialnos;
+  if(OP_UNLIKELY(nserialnos>=cserialnos)){
+    if(OP_UNLIKELY(cserialnos>INT_MAX-1>>1))return OP_EFAULT;
+    cserialnos=2*cserialnos+1;
+    OP_ASSERT(nserialnos<cserialnos);
+    serialnos=_ogg_realloc(serialnos,sizeof(*serialnos)*cserialnos);
+    if(OP_UNLIKELY(serialnos==NULL))return OP_EFAULT;
+  }
+  serialnos[nserialnos++]=s;
+  *_serialnos=serialnos;
+  *_nserialnos=nserialnos;
+  *_cserialnos=cserialnos;
+  return 0;
+}
+
+/*Returns nonzero if found.*/
+static int op_lookup_serialno(ogg_uint32_t _s,
+ const ogg_uint32_t *_serialnos,int _nserialnos){
+  int i;
+  for(i=0;i<_nserialnos&&_serialnos[i]!=_s;i++);
+  return i<_nserialnos;
+}
+
+static int op_lookup_page_serialno(ogg_page *_og,
+ const ogg_uint32_t *_serialnos,int _nserialnos){
+  return op_lookup_serialno(ogg_page_serialno(_og),_serialnos,_nserialnos);
+}
+
+/*Performs the same search as op_get_prev_page(), but prefers pages of the
+   specified serial number.
+  If a page of the specified serial number is spotted during the
+   seek-back-and-read-forward, it will return the info of last page of the
+   matching serial number, instead of the very last page.
+  If no page of the specified serial number is seen, it will return the info of
+   the last page and update *_serialno.*/
+static opus_int64 op_get_prev_page_serial(OggOpusFile *_of,
+ const ogg_uint32_t *_serialnos,int _nserialnos,opus_int32 *_chunk_size,
+ ogg_uint32_t *_serialno,ogg_int64_t *_gp){
+  ogg_page     og;
+  opus_int64   begin;
+  opus_int64   end;
+  opus_int64   original_end;
+  opus_int64   offset;
+  opus_int64   preferred_offset;
+  ogg_uint32_t preferred_serialno;
+  ogg_int64_t  ret_serialno;
+  ogg_int64_t  ret_gp;
+  opus_int32   chunk_size;
+  original_end=end=begin=_of->offset;
+  preferred_offset=offset=-1;
+  ret_serialno=-1;
+  ret_gp=-1;
+  preferred_serialno=*_serialno;
+  chunk_size=_chunk_size==NULL?OP_CHUNK_SIZE:*_chunk_size;
+  do{
+    int ret;
+    OP_ASSERT(chunk_size>=OP_PAGE_SIZE);
+    begin=OP_MAX(begin-chunk_size,0);
+    ret=op_seek_helper(_of,begin);
+    if(OP_UNLIKELY(ret<0))return ret;
+    while(_of->offset<end){
+      opus_int64 llret;
+      llret=op_get_next_page(_of,&og,end-_of->offset);
+      if(OP_UNLIKELY(llret<OP_FALSE))return llret;
+      else if(llret==OP_FALSE)break;
+      ret_serialno=ogg_page_serialno(&og);
+      ret_gp=ogg_page_granulepos(&og);
+      offset=llret;
+      if(ret_serialno==preferred_serialno){
+        preferred_offset=offset;
+        *_gp=ret_gp;
+      }
+      if(!op_lookup_serialno(ret_serialno,_serialnos,_nserialnos)){
+        /*We fell off the end of the link, which means we seeked back too far
+           and shouldn't have been looking in that link to begin with.
+          If we found the preferred serial number, forget that we saw it.*/
+        preferred_offset=-1;
+      }
+    }
+    /*We started from the beginning of the stream and found nothing.
+      This should be impossible unless the contents of the source changed out
+       from under us after we read from it.*/
+    if(OP_UNLIKELY(!begin)&&OP_UNLIKELY(offset==-1))return OP_EBADLINK;
+    /*Bump up the chunk size.
+      This is mildly helpful when seeks are very expensive (http).*/
+    chunk_size=OP_MIN(2*chunk_size,OP_CHUNK_SIZE_MAX);
+    /*Avoid quadratic complexity if we hit an invalid patch of the file.*/
+    end=OP_MIN(begin+OP_PAGE_SIZE-1,original_end);
+  }
+  while(offset==-1);
+  if(_chunk_size!=NULL)*_chunk_size=chunk_size;
+  /*We're not interested in the page... just the serial number, byte offset,
+    and granule position.*/
+  if(preferred_offset>=0)return preferred_offset;
+  *_serialno=ret_serialno;
+  *_gp=ret_gp;
+  return offset;
+}
+
+/*Uses the local ogg_stream storage in _of.
+  This is important for non-streaming input sources.*/
+static int op_fetch_headers_impl(OggOpusFile *_of,OpusHead *_head,
+ OpusTags *_tags,ogg_uint32_t **_serialnos,int *_nserialnos,
+ int *_cserialnos,ogg_page *_og){
+  ogg_packet op;
+  int        ret;
+  if(_serialnos!=NULL)*_nserialnos=0;
+  /*Extract the serialnos of all BOS pages plus the first set of Opus headers
+     we see in the link.*/
+  while(ogg_page_bos(_og)){
+    opus_int64 llret;
+    if(_serialnos!=NULL){
+      if(OP_UNLIKELY(op_lookup_page_serialno(_og,*_serialnos,*_nserialnos))){
+        /*A dupe serialnumber in an initial header packet set==invalid stream.*/
+        return OP_EBADHEADER;
+      }
+      ret=op_add_serialno(_og,_serialnos,_nserialnos,_cserialnos);
+      if(OP_UNLIKELY(ret<0))return ret;
+    }
+    if(_of->ready_state<OP_STREAMSET){
+      /*We don't have an Opus stream in this link yet, so begin prospective
+         stream setup.
+        We need a stream to get packets.*/
+      ogg_stream_reset_serialno(&_of->os,ogg_page_serialno(_og));
+      ogg_stream_pagein(&_of->os,_og);
+      if(OP_LIKELY(ogg_stream_packetout(&_of->os,&op)>0)){
+        ret=opus_head_parse(_head,op.packet,op.bytes);
+        /*If it's just a stream type we don't recognize, ignore it.*/
+        if(ret==OP_ENOTFORMAT)continue;
+        /*Everything else is fatal.*/
+        if(OP_UNLIKELY(ret<0))return ret;
+        /*Found a valid Opus header.
+          Continue setup.*/
+        _of->ready_state=OP_STREAMSET;
+      }
+    }
+    /*Get the next page.*/
+    llret=op_get_next_page(_of,_og,OP_CHUNK_SIZE);
+    if(OP_UNLIKELY(llret<0))return OP_ENOTFORMAT;
+    /*If this page also belongs to our Opus stream, submit it and break.*/
+    if(_of->ready_state==OP_STREAMSET
+     &&_of->os.serialno==ogg_page_serialno(_og)){
+      ogg_stream_pagein(&_of->os,_og);
+      break;
+    }
+  }
+  if(OP_UNLIKELY(_of->ready_state!=OP_STREAMSET))return OP_ENOTFORMAT;
+  /*Loop getting packets.*/
+  for(;;){
+    switch(ogg_stream_packetout(&_of->os,&op)){
+      case 0:{
+        /*Loop getting pages.*/
+        for(;;){
+          if(OP_UNLIKELY(op_get_next_page(_of,_og,OP_CHUNK_SIZE)<0)){
+            return OP_EBADHEADER;
+          }
+          /*If this page belongs to the correct stream, go parse it.*/
+          if(_of->os.serialno==ogg_page_serialno(_og)){
+            ogg_stream_pagein(&_of->os,_og);
+            break;
+          }
+          /*If the link ends before we see the Opus comment header, abort.*/
+          if(OP_UNLIKELY(ogg_page_bos(_og)))return OP_EBADHEADER;
+          /*Otherwise, keep looking.*/
+        }
+      }break;
+      /*We shouldn't get a hole in the headers!*/
+      case -1:return OP_EBADHEADER;
+      default:{
+        /*Got a packet.
+          It should be the comment header.*/
+        ret=opus_tags_parse(_tags,op.packet,op.bytes);
+        if(OP_UNLIKELY(ret<0))return ret;
+        /*Make sure the page terminated at the end of the comment header.
+          If there is another packet on the page, or part of a packet, then
+           reject the stream.
+          Otherwise seekable sources won't be able to seek back to the start
+           properly.*/
+        ret=ogg_stream_packetout(&_of->os,&op);
+        if(OP_UNLIKELY(ret!=0)
+         ||OP_UNLIKELY(_og->header[_og->header_len-1]==255)){
+          /*If we fail, the caller assumes our tags are uninitialized.*/
+          opus_tags_clear(_tags);
+          return OP_EBADHEADER;
+        }
+        return 0;
+      }
+    }
+  }
+}
+
+static int op_fetch_headers(OggOpusFile *_of,OpusHead *_head,
+ OpusTags *_tags,ogg_uint32_t **_serialnos,int *_nserialnos,
+ int *_cserialnos,ogg_page *_og){
+  ogg_page og;
+  int      ret;
+  if(!_og){
+    ogg_int64_t llret;
+    llret=op_get_next_page(_of,&og,OP_CHUNK_SIZE);
+    if(OP_UNLIKELY(llret<0))return OP_ENOTFORMAT;
+    _og=&og;
+  }
+  _of->ready_state=OP_OPENED;
+  ret=op_fetch_headers_impl(_of,_head,_tags,_serialnos,_nserialnos,
+   _cserialnos,_og);
+  /*Revert back from OP_STREAMSET to OP_OPENED on failure, to prevent
+     double-free of the tags in an unseekable stream.*/
+  if(OP_UNLIKELY(ret<0))_of->ready_state=OP_OPENED;
+  return ret;
+}
+
+/*Granule position manipulation routines.
+  A granule position is defined to be an unsigned 64-bit integer, with the
+   special value -1 in two's complement indicating an unset or invalid granule
+   position.
+  We are not guaranteed to have an unsigned 64-bit type, so we construct the
+   following routines that
+   a) Properly order negative numbers as larger than positive numbers, and
+   b) Check for underflow or overflow past the special -1 value.
+  This lets us operate on the full, valid range of granule positions in a
+   consistent and safe manner.
+  This full range is organized into distinct regions:
+   [ -1 (invalid) ][ 0 ... OP_INT64_MAX ][ OP_INT64_MIN ... -2 ][-1 (invalid) ]
+
+  No one should actually use granule positions so large that they're negative,
+   even if they are technically valid, as very little software handles them
+   correctly (including most of Xiph.Org's).
+  This library also refuses to support durations so large they won't fit in a
+   signed 64-bit integer (to avoid exposing this mess to the application, and
+   to simplify a good deal of internal arithmetic), so the only way to use them
+   successfully is if pcm_start is very large.
+  This means there isn't anything you can do with negative granule positions
+   that you couldn't have done with purely non-negative ones.
+  The main purpose of these routines is to allow us to think very explicitly
+   about the possible failure cases of all granule position manipulations.*/
+
+/*Safely adds a small signed integer to a valid (not -1) granule position.
+  The result can use the full 64-bit range of values (both positive and
+   negative), but will fail on overflow (wrapping past -1; wrapping past
+   OP_INT64_MAX is explicitly okay).
+  [out] _dst_gp: The resulting granule position.
+                 Only modified on success.
+  _src_gp:       The granule position to add to.
+                 This must not be -1.
+  _delta:        The amount to add.
+                 This is allowed to be up to 32 bits to support the maximum
+                  duration of a single Ogg page (255 packets * 120 ms per
+                  packet == 1,468,800 samples at 48 kHz).
+  Return: 0 on success, or OP_EINVAL if the result would wrap around past -1.*/
+static int op_granpos_add(ogg_int64_t *_dst_gp,ogg_int64_t _src_gp,
+ opus_int32 _delta){
+  /*The code below handles this case correctly, but there's no reason we
+     should ever be called with these values, so make sure we aren't.*/
+  OP_ASSERT(_src_gp!=-1);
+  if(_delta>0){
+    /*Adding this amount to the granule position would overflow its 64-bit
+       range.*/
+    if(OP_UNLIKELY(_src_gp<0)&&OP_UNLIKELY(_src_gp>=-1-_delta))return OP_EINVAL;
+    if(OP_UNLIKELY(_src_gp>OP_INT64_MAX-_delta)){
+      /*Adding this amount to the granule position would overflow the positive
+         half of its 64-bit range.
+        Since signed overflow is undefined in C, do it in a way the compiler
+         isn't allowed to screw up.*/
+      _delta-=(opus_int32)(OP_INT64_MAX-_src_gp)+1;
+      _src_gp=OP_INT64_MIN;
+    }
+  }
+  else if(_delta<0){
+    /*Subtracting this amount from the granule position would underflow its
+       64-bit range.*/
+    if(_src_gp>=0&&OP_UNLIKELY(_src_gp<-_delta))return OP_EINVAL;
+    if(OP_UNLIKELY(_src_gp<OP_INT64_MIN-_delta)){
+      /*Subtracting this amount from the granule position would underflow the
+         negative half of its 64-bit range.
+        Since signed underflow is undefined in C, do it in a way the compiler
+         isn't allowed to screw up.*/
+      _delta+=(opus_int32)(_src_gp-OP_INT64_MIN)+1;
+      _src_gp=OP_INT64_MAX;
+    }
+  }
+  *_dst_gp=_src_gp+_delta;
+  return 0;
+}
+
+/*Safely computes the difference between two granule positions.
+  The difference must fit in a signed 64-bit integer, or the function fails.
+  It correctly handles the case where the granule position has wrapped around
+   from positive values to negative ones.
+  [out] _delta: The difference between the granule positions.
+                Only modified on success.
+  _gp_a:        The granule position to subtract from.
+                This must not be -1.
+  _gp_b:        The granule position to subtract.
+                This must not be -1.
+  Return: 0 on success, or OP_EINVAL if the result would not fit in a signed
+           64-bit integer.*/
+static int op_granpos_diff(ogg_int64_t *_delta,
+ ogg_int64_t _gp_a,ogg_int64_t _gp_b){
+  int gp_a_negative;
+  int gp_b_negative;
+  /*The code below handles these cases correctly, but there's no reason we
+     should ever be called with these values, so make sure we aren't.*/
+  OP_ASSERT(_gp_a!=-1);
+  OP_ASSERT(_gp_b!=-1);
+  gp_a_negative=OP_UNLIKELY(_gp_a<0);
+  gp_b_negative=OP_UNLIKELY(_gp_b<0);
+  if(OP_UNLIKELY(gp_a_negative^gp_b_negative)){
+    ogg_int64_t da;
+    ogg_int64_t db;
+    if(gp_a_negative){
+      /*_gp_a has wrapped to a negative value but _gp_b hasn't: the difference
+         should be positive.*/
+      /*Step 1: Handle wrapping.*/
+      /*_gp_a < 0 => da < 0.*/
+      da=(OP_INT64_MIN-_gp_a)-1;
+      /*_gp_b >= 0  => db >= 0.*/
+      db=OP_INT64_MAX-_gp_b;
+      /*Step 2: Check for overflow.*/
+      if(OP_UNLIKELY(OP_INT64_MAX+da<db))return OP_EINVAL;
+      *_delta=db-da;
+    }
+    else{
+      /*_gp_b has wrapped to a negative value but _gp_a hasn't: the difference
+         should be negative.*/
+      /*Step 1: Handle wrapping.*/
+      /*_gp_a >= 0 => da <= 0*/
+      da=_gp_a+OP_INT64_MIN;
+      /*_gp_b < 0 => db <= 0*/
+      db=OP_INT64_MIN-_gp_b;
+      /*Step 2: Check for overflow.*/
+      if(OP_UNLIKELY(da<OP_INT64_MIN-db))return OP_EINVAL;
+      *_delta=da+db;
+    }
+  }
+  else *_delta=_gp_a-_gp_b;
+  return 0;
+}
+
+static int op_granpos_cmp(ogg_int64_t _gp_a,ogg_int64_t _gp_b){
+  /*The invalid granule position -1 should behave like NaN: neither greater
+     than nor less than any other granule position, nor equal to any other
+     granule position, including itself.
+    However, that means there isn't anything we could sensibly return from this
+     function for it.*/
+  OP_ASSERT(_gp_a!=-1);
+  OP_ASSERT(_gp_b!=-1);
+  /*Handle the wrapping cases.*/
+  if(OP_UNLIKELY(_gp_a<0)){
+    if(_gp_b>=0)return 1;
+    /*Else fall through.*/
+  }
+  else if(OP_UNLIKELY(_gp_b<0))return -1;
+  /*No wrapping case.*/
+  return (_gp_a>_gp_b)-(_gp_b>_gp_a);
+}
+
+/*Returns the duration of the packet (in samples at 48 kHz), or a negative
+   value on error.*/
+static int op_get_packet_duration(const unsigned char *_data,int _len){
+  int nframes;
+  int frame_size;
+  int nsamples;
+  nframes=opus_packet_get_nb_frames(_data,_len);
+  if(OP_UNLIKELY(nframes<0))return OP_EBADPACKET;
+  frame_size=opus_packet_get_samples_per_frame(_data,48000);
+  nsamples=nframes*frame_size;
+  if(OP_UNLIKELY(nsamples>120*48))return OP_EBADPACKET;
+  return nsamples;
+}
+
+/*This function more properly belongs in info.c, but we define it here to allow
+   the static granule position manipulation functions to remain static.*/
+ogg_int64_t opus_granule_sample(const OpusHead *_head,ogg_int64_t _gp){
+  opus_int32 pre_skip;
+  pre_skip=_head->pre_skip;
+  if(_gp!=-1&&op_granpos_add(&_gp,_gp,-pre_skip))_gp=-1;
+  return _gp;
+}
+
+/*Grab all the packets currently in the stream state, and compute their
+   durations.
+  _of->op_count is set to the number of packets collected.
+  [out] _durations: Returns the durations of the individual packets.
+  Return: The total duration of all packets, or OP_HOLE if there was a hole.*/
+static opus_int32 op_collect_audio_packets(OggOpusFile *_of,
+ int _durations[255]){
+  opus_int32 total_duration;
+  int        op_count;
+  /*Count the durations of all packets in the page.*/
+  op_count=0;
+  total_duration=0;
+  for(;;){
+    int ret;
+    /*Unless libogg is broken, we can't get more than 255 packets from a
+       single page.*/
+    OP_ASSERT(op_count<255);
+    /*This takes advantage of undocumented libogg behavior that returned
+       ogg_packet buffers are valid at least until the next page is
+       submitted.
+      Relying on this is not too terrible, as _none_ of the Ogg memory
+       ownership/lifetime rules are well-documented.
+      But I can read its code and know this will work.*/
+    ret=ogg_stream_packetout(&_of->os,_of->op+op_count);
+    if(!ret)break;
+    if(OP_UNLIKELY(ret<0)){
+      /*We shouldn't get holes in the middle of pages.*/
+      OP_ASSERT(op_count==0);
+      return OP_HOLE;
+    }
+    _durations[op_count]=op_get_packet_duration(_of->op[op_count].packet,
+     _of->op[op_count].bytes);
+    if(OP_LIKELY(_durations[op_count]>0)){
+      /*With at most 255 packets on a page, this can't overflow.*/
+      total_duration+=_durations[op_count++];
+    }
+    /*Ignore packets with an invalid TOC sequence.*/
+  }
+  _of->op_pos=0;
+  _of->op_count=op_count;
+  return total_duration;
+}
+
+/*Starting from current cursor position, get the initial PCM offset of the next
+   page.
+  This also validates the granule position on the first page with a completed
+   audio data packet, as required by the spec.
+  If this link is completely empty (no pages with completed packets), then this
+   function sets pcm_start=pcm_end=0 and returns the BOS page of the next link
+   (if any).
+  In the seekable case, we initialize pcm_end=-1 before calling this function,
+   so that later we can detect that the link was empty before calling
+   op_find_final_pcm_offset().
+  [inout] _link: The link for which to find pcm_start.
+  [out] _og:     Returns the BOS page of the next link if this link was empty.
+                 In the unseekable case, we can then feed this to
+                  op_fetch_headers() to start the next link.
+                 The caller may pass NULL (e.g., for seekable streams), in
+                  which case this page will be discarded.
+  Return: 0 on success, 1 if there is a buffered BOS page available, or a
+           negative value on unrecoverable error.*/
+static int op_find_initial_pcm_offset(OggOpusFile *_of,
+ OggOpusLink *_link,ogg_page *_og){
+  ogg_page     og;
+  ogg_int64_t  pcm_start;
+  ogg_int64_t  prev_packet_gp;
+  ogg_int64_t  cur_page_gp;
+  ogg_uint32_t serialno;
+  opus_int32   total_duration;
+  int          durations[255];
+  int          cur_page_eos;
+  int          op_count;
+  int          pi;
+  if(_og==NULL)_og=&og;
+  serialno=_of->os.serialno;
+  cur_page_gp=-1;
+  do{
+    /*We should get a page unless the file is truncated or mangled.
+      Otherwise there are no audio data packets in the whole logical stream.*/
+    if(OP_UNLIKELY(op_get_next_page(_of,_og,-1)<0)){
+      /*Fail if the pre-skip is non-zero, since it's asking us to skip more
+         samples than exist.*/
+      if(_link->head.pre_skip>0)return OP_EBADTIMESTAMP;
+      /*Set pcm_end and end_offset so we can skip the call to
+         op_find_final_pcm_offset().*/
+      _link->pcm_start=_link->pcm_end=0;
+      _link->end_offset=_link->data_offset;
+      return 0;
+    }
+    /*Similarly, if we hit the next link in the chain, we've gone too far.*/
+    if(OP_UNLIKELY(ogg_page_bos(_og))){
+      if(_link->head.pre_skip>0)return OP_EBADTIMESTAMP;
+      /*Set pcm_end and end_offset so we can skip the call to
+         op_find_final_pcm_offset().*/
+      _link->pcm_end=_link->pcm_start=0;
+      _link->end_offset=_link->data_offset;
+      /*Tell the caller we've got a buffered page for them.*/
+      return 1;
+    }
+    /*Ignore pages from other streams (not strictly necessary, because of the
+       checks in ogg_stream_pagein(), but saves some work).*/
+    if(serialno!=(ogg_uint32_t)ogg_page_serialno(_og))continue;
+    ogg_stream_pagein(&_of->os,_og);
+    /*Bitrate tracking: add the header's bytes here.
+      The body bytes are counted when we consume the packets.*/
+    _of->bytes_tracked+=_og->header_len;
+    /*Count the durations of all packets in the page.*/
+    do total_duration=op_collect_audio_packets(_of,durations);
+    /*Ignore holes.*/
+    while(OP_UNLIKELY(total_duration<0));
+    op_count=_of->op_count;
+  }
+  while(op_count<=0);
+  /*We found the first page with a completed audio data packet: actually look
+     at the granule position.
+    RFC 3533 says, "A special value of -1 (in two's complement) indicates that
+     no packets finish on this page," which does not say that a granule
+     position that is NOT -1 indicates that some packets DO finish on that page
+     (even though this was the intention, libogg itself violated this intention
+     for years before we fixed it).
+    The Ogg Opus specification only imposes its start-time requirements
+     on the granule position of the first page with completed packets,
+     so we ignore any set granule positions until then.*/
+  cur_page_gp=_of->op[op_count-1].granulepos;
+  /*But getting a packet without a valid granule position on the page is not
+     okay.*/
+  if(cur_page_gp==-1)return OP_EBADTIMESTAMP;
+  cur_page_eos=_of->op[op_count-1].e_o_s;
+  if(OP_LIKELY(!cur_page_eos)){
+    /*The EOS flag wasn't set.
+      Work backwards from the provided granule position to get the starting PCM
+       offset.*/
+    if(OP_UNLIKELY(op_granpos_add(&pcm_start,cur_page_gp,-total_duration)<0)){
+      /*The starting granule position MUST not be smaller than the amount of
+         audio on the first page with completed packets.*/
+      return OP_EBADTIMESTAMP;
+    }
+  }
+  else{
+    /*The first page with completed packets was also the last.*/
+    if(OP_LIKELY(op_granpos_add(&pcm_start,cur_page_gp,-total_duration)<0)){
+      /*If there's less audio on the page than indicated by the granule
+         position, then we're doing end-trimming, and the starting PCM offset
+         is zero by spec mandate.*/
+      pcm_start=0;
+      /*However, the end-trimming MUST not ask us to trim more samples than
+         exist after applying the pre-skip.*/
+      if(OP_UNLIKELY(op_granpos_cmp(cur_page_gp,_link->head.pre_skip)<0)){
+        return OP_EBADTIMESTAMP;
+      }
+    }
+  }
+  /*Timestamp the individual packets.*/
+  prev_packet_gp=pcm_start;
+  for(pi=0;pi<op_count;pi++){
+    int ret;
+    if(cur_page_eos){
+      ogg_int64_t diff;
+      ret=op_granpos_diff(&diff,cur_page_gp,prev_packet_gp);
+      OP_ASSERT(!ret);
+      diff=durations[pi]-diff;
+      /*If we have samples to trim...*/
+      if(diff>0){
+        /*If we trimmed the entire packet, stop (the spec says encoders
+           shouldn't do this, but we support it anyway).*/
+        if(OP_UNLIKELY(diff>durations[pi]))break;
+        _of->op[pi].granulepos=prev_packet_gp=cur_page_gp;
+        /*Move the EOS flag to this packet, if necessary, so we'll trim the
+           samples.*/
+        _of->op[pi].e_o_s=1;
+        continue;
+      }
+    }
+    /*Update the granule position as normal.*/
+    ret=op_granpos_add(&_of->op[pi].granulepos,
+     prev_packet_gp,durations[pi]);
+    OP_ASSERT(!ret);
+    prev_packet_gp=_of->op[pi].granulepos;
+  }
+  /*Update the packet count after end-trimming.*/
+  _of->op_count=pi;
+  _of->cur_discard_count=_link->head.pre_skip;
+  _of->prev_packet_gp=_link->pcm_start=pcm_start;
+  return 0;
+}
+
+/*Starting from current cursor position, get the final PCM offset of the
+   previous page.
+  This also validates the duration of the link, which, while not strictly
+   required by the spec, we need to ensure duration calculations don't
+   overflow.
+  This is only done for seekable sources.
+  We must validate that op_find_initial_pcm_offset() succeeded for this link
+   before calling this function, otherwise it will scan the entire stream
+   backwards until it reaches the start, and then fail.*/
+static int op_find_final_pcm_offset(OggOpusFile *_of,
+ const ogg_uint32_t *_serialnos,int _nserialnos,OggOpusLink *_link,
+ ogg_int64_t _end_gp,ogg_uint32_t _end_serialno,ogg_int64_t *_total_duration){
+  opus_int64   offset;
+  ogg_int64_t  total_duration;
+  ogg_int64_t  duration;
+  ogg_uint32_t cur_serialno;
+  ogg_uint32_t test_serialno;
+  opus_int32   chunk_size;
+  /*For the time being, fetch end PCM offset the simple way.*/
+  cur_serialno=_link->serialno;
+  test_serialno=_end_serialno;
+  /*Keep track of the growing chunk size to better handle being multiplexed
+     with another high-bitrate stream.*/
+  chunk_size=OP_CHUNK_SIZE;
+  offset=_of->offset;
+  while(_end_gp==-1||test_serialno!=cur_serialno){
+    test_serialno=cur_serialno;
+    _of->offset=offset;
+    offset=op_get_prev_page_serial(_of,_serialnos,_nserialnos,
+     &chunk_size,&test_serialno,&_end_gp);
+    if(OP_UNLIKELY(offset<0))return (int)offset;
+  }
+  /*This implementation requires that difference between the first and last
+     granule positions in each link be representable in a signed, 64-bit
+     number, and that each link also have at least as many samples as the
+     pre-skip requires.*/
+  if(OP_UNLIKELY(op_granpos_diff(&duration,_end_gp,_link->pcm_start)<0)
+   ||OP_UNLIKELY(duration<_link->head.pre_skip)){
+    return OP_EBADTIMESTAMP;
+  }
+  /*We also require that the total duration be representable in a signed,
+     64-bit number.*/
+  duration-=_link->head.pre_skip;
+  total_duration=*_total_duration;
+  if(OP_UNLIKELY(OP_INT64_MAX-duration<total_duration))return OP_EBADTIMESTAMP;
+  *_total_duration=total_duration+duration;
+  _link->pcm_end=_end_gp;
+  _link->end_offset=offset;
+  return 0;
+}
+
+typedef struct op_seek_record op_seek_record;
+
+/*We use this to remember the pages we found while enumerating the links of a
+   chained stream.
+  We only need to know the starting and ending byte offsets and the serial
+   number, so we can tell if the page belonged to the current chain or not,
+   and where to bisect.*/
+struct op_seek_record{
+  opus_int64   offset;
+  opus_int32   size;
+  ogg_uint32_t serialno;
+};
+
+/*Finds each bitstream link, one at a time, using a bisection search.
+  This has to begin by knowing the offset of the first link's initial page.*/
+static int op_bisect_forward_serialno(OggOpusFile *_of,
+ opus_int64 _searched,ogg_int64_t _end_gp,op_seek_record *_sr,int _csr,
+ ogg_uint32_t **_serialnos,int *_nserialnos,int *_cserialnos){
+  ogg_page      og;
+  OggOpusLink  *links;
+  int           nlinks;
+  int           clinks;
+  ogg_uint32_t *serialnos;
+  int           nserialnos;
+  opus_int64    begin;
+  ogg_int64_t   total_duration;
+  int           nsr;
+  int           ret;
+  links=_of->links;
+  nlinks=clinks=_of->nlinks;
+  begin=0;
+  total_duration=0;
+  /*We start with one seek record, for the last page in the file.
+    We build up a list of records for places we seek to during link
+     enumeration.
+    This list is kept sorted in reverse order.
+    We only care about seek locations that were _not_ in the current link,
+     therefore we can add them one at a time to the end of the list as we
+     improve the lower bound on the location where the next link starts.*/
+  nsr=1;
+  for(;;){
+    opus_int64 data_offset;
+    opus_int64 end_searched;
+    opus_int64 next;
+    opus_int64 last;
+    int        sri;
+    serialnos=*_serialnos;
+    nserialnos=*_nserialnos;
+    if(OP_UNLIKELY(nlinks>=clinks)){
+      if(OP_UNLIKELY(clinks>INT_MAX-1>>1))return OP_EFAULT;
+      clinks=2*clinks+1;
+      OP_ASSERT(nlinks<clinks);
+      links=_ogg_realloc(links,sizeof(*links)*clinks);
+      if(OP_UNLIKELY(links==NULL))return OP_EFAULT;
+      _of->links=links;
+    }
+    data_offset=_searched;
+    /*Invariants:
+      We have the headers and serial numbers for the link beginning at 'begin'.
+      We have the offset and granule position of the last page in the file
+       (potentially not a page we care about).*/
+    /*Scan the seek records we already have to save us some bisection.*/
+    for(sri=0;sri<nsr;sri++){
+      if(op_lookup_serialno(_sr[sri].serialno,*_serialnos,*_nserialnos))break;
+    }
+    /*Is the last page in our current list of serial numbers?*/
+    if(sri<=0)break;
+    /*Last page wasn't found.
+      We have at least one more link.*/
+    end_searched=next=_sr[sri-1].offset;
+    if(sri<nsr)_searched=_sr[sri].offset+_sr[sri].size;
+    nsr=sri;
+    /*We guard against garbage separating the last and first pages of two
+       links below.*/
+    while(_searched<end_searched){
+      opus_int64 bisect;
+      if(OP_UNLIKELY(end_searched-_searched<OP_CHUNK_SIZE))bisect=_searched;
+      /*TODO: We might be able to do a better job estimating the start of
+         subsequent links by assuming its initial PCM offset is 0 and using two
+         sightings of the same stream to estimate a bitrate.*/
+      else bisect=_searched+(end_searched-_searched>>1);
+      if(OP_LIKELY(bisect!=_of->offset)){
+        ret=op_seek_helper(_of,bisect);
+        if(OP_UNLIKELY(ret<0))return ret;
+      }
+      last=op_get_next_page(_of,&og,-1);
+      /*At the worst we should have hit the page at _sr[sri-1].offset.*/
+      if(OP_UNLIKELY(last<0))return OP_EBADLINK;
+      OP_ASSERT(nsr<_csr);
+      _sr[nsr].serialno=ogg_page_serialno(&og);
+      if(!op_lookup_serialno(_sr[nsr].serialno,serialnos,nserialnos)){
+        end_searched=bisect;
+        next=last;
+        /*In reality we should always have enough room, but be paranoid.*/
+        if(OP_LIKELY(nsr+1<_csr)){
+          _sr[nsr].offset=last;
+          OP_ASSERT(_of->offset-last>=0);
+          OP_ASSERT(_of->offset-last<=OP_PAGE_SIZE);
+          _sr[nsr].size=(opus_int32)(_of->offset-last);
+          nsr++;
+        }
+      }
+      else _searched=_of->offset;
+    }
+    /*Bisection point found.
+      Get the final granule position of the previous link, assuming
+       op_find_initial_pcm_offset() didn't already determine the link was
+       empty.*/
+    if(OP_LIKELY(links[nlinks-1].pcm_end==-1)){
+      _of->offset=next;
+      ret=op_find_final_pcm_offset(_of,serialnos,nserialnos,
+       links+nlinks-1,-1,0,&total_duration);
+      if(OP_UNLIKELY(ret<0))return ret;
+    }
+    /*Restore the cursor position after the seek.
+      This should only be necessary if the last page in the link did not belong
+       to our Opus stream.
+      TODO: Read forward instead, or let seek implementations do that?*/
+    if(_of->offset!=next){
+      ret=op_seek_helper(_of,next);
+      if(OP_UNLIKELY(ret<0))return ret;
+    }
+    ret=op_fetch_headers(_of,&links[nlinks].head,&links[nlinks].tags,
+     _serialnos,_nserialnos,_cserialnos,NULL);
+    if(OP_UNLIKELY(ret<0))return ret;
+    links[nlinks].offset=next;
+    links[nlinks].data_offset=_of->offset;
+    links[nlinks].serialno=_of->os.serialno;
+    links[nlinks].pcm_end=-1;
+    /*This might consume a page from the next link, however the next bisection
+       always starts with a seek.*/
+    ret=op_find_initial_pcm_offset(_of,links+nlinks,NULL);
+    if(OP_UNLIKELY(ret<0))return ret;
+    begin=next;
+    _searched=_of->offset;
+    /*Mark the current link count so it can be cleaned up on error.*/
+    _of->nlinks=++nlinks;
+  }
+  /*Last page is in the starting serialno list, so we've reached the last link.
+    Now find the last granule position for it (if we didn't the first time we
+     looked at the end of the stream, and if op_find_initial_pcm_offset()
+     didn't already determine the link was empty).*/
+  if(OP_LIKELY(links[nlinks-1].pcm_end==-1)){
+    _of->offset=_sr[0].offset;
+    ret=op_find_final_pcm_offset(_of,serialnos,nserialnos,
+     links+nlinks-1,_end_gp,_sr[0].serialno,&total_duration);
+    if(OP_UNLIKELY(ret<0))return ret;
+  }
+  /*Trim back the links array if necessary.*/
+  links=_ogg_realloc(links,sizeof(*links)*nlinks);
+  if(OP_LIKELY(links!=NULL))_of->links=links;
+  /*We also don't need these anymore.*/
+  _ogg_free(*_serialnos);
+  *_serialnos=NULL;
+  *_cserialnos=*_nserialnos=0;
+  return 0;
+}
+
+static int op_make_decode_ready(OggOpusFile *_of){
+  OpusHead *head;
+  int       li;
+  int       stream_count;
+  int       coupled_count;
+  int       channel_count;
+  if(_of->ready_state>OP_STREAMSET)return 0;
+  if(OP_UNLIKELY(_of->ready_state<OP_STREAMSET))return OP_EFAULT;
+  li=_of->seekable?_of->cur_link:0;
+  head=&_of->links[li].head;
+  stream_count=head->stream_count;
+  coupled_count=head->coupled_count;
+  channel_count=head->channel_count;
+  /*Check to see if the current decoder is compatible with the current link.*/
+  if(_of->od!=NULL&&_of->od_stream_count==stream_count
+   &&_of->od_coupled_count==coupled_count&&_of->od_channel_count==channel_count
+   &&memcmp(_of->od_mapping,head->mapping,
+   sizeof(*head->mapping)*channel_count)==0){
+    opus_multistream_decoder_ctl(_of->od,OPUS_RESET_STATE);
+  }
+  else{
+    int err;
+    opus_multistream_decoder_destroy(_of->od);
+    _of->od=opus_multistream_decoder_create(48000,channel_count,
+     stream_count,coupled_count,head->mapping,&err);
+    if(_of->od==NULL)return OP_EFAULT;
+    _of->od_stream_count=stream_count;
+    _of->od_coupled_count=coupled_count;
+    _of->od_channel_count=channel_count;
+    memcpy(_of->od_mapping,head->mapping,sizeof(*head->mapping)*channel_count);
+  }
+  /*TODO: Implement this when not available, or require sufficiently new
+     libopus?*/
+#if defined(OPUS_SET_GAIN)
+  opus_multistream_decoder_ctl(_of->od,OPUS_SET_GAIN(head->output_gain));
+#endif
+  _of->ready_state=OP_INITSET;
+  _of->bytes_tracked=0;
+  _of->samples_tracked=0;
+#if !defined(OP_FIXED_POINT)
+  _of->dither_mute=65;
+  /*Use the serial number for the PRNG seed to get repeatable output for
+     straight play-throughs.*/
+  _of->dither_seed=_of->links[li].serialno;
+#endif
+  return 0;
+}
+
+static int op_open_seekable2(OggOpusFile *_of){
+  /*64 seek records should be enough for anybody.
+    Actually, with a bisection search in a 63-bit range down to OP_CHUNK_SIZE
+     granularity, much more than enough.*/
+  op_seek_record sr[64];
+  opus_int64     data_offset;
+  ogg_int64_t    end_gp;
+  int            ret;
+  /*We're partially open and have a first link header state in storage in _of.*/
+  /*We can seek, so set out learning all about this file.*/
+  (*_of->callbacks.seek)(_of->source,0,SEEK_END);
+  _of->offset=_of->end=(*_of->callbacks.tell)(_of->source);
+  /*Get the offset of the last page of the physical bitstream, or, if we're
+     lucky, the last Opus page of the first link, as most Ogg Opus files will
+     contain a single logical bitstream.*/
+  sr[0].serialno=_of->links[0].serialno;
+  sr[0].offset=op_get_prev_page_serial(_of,_of->serialnos,_of->nserialnos,
+   NULL,&sr[0].serialno,&end_gp);
+  if(OP_UNLIKELY(sr[0].offset<0))return (int)sr[0].offset;
+  /*Now enumerate the bitstream structure.*/
+  OP_ASSERT(_of->offset-sr[0].offset>=0);
+  OP_ASSERT(_of->offset-sr[0].offset<=OP_PAGE_SIZE);
+  sr[0].size=(opus_int32)(_of->offset-sr[0].offset);
+  data_offset=_of->links[0].data_offset;
+  ret=op_bisect_forward_serialno(_of,data_offset,end_gp,sr,
+   sizeof(sr)/sizeof(*sr),&_of->serialnos,&_of->nserialnos,&_of->cserialnos);
+  if(OP_UNLIKELY(ret<0))return ret;
+  /*And seek back to the start of the first link.*/
+  return op_raw_seek(_of,data_offset);
+}
+
+/*Clear out the current logical bitstream decoder.*/
+static void op_decode_clear(OggOpusFile *_of){
+  /*We don't actually free the decoder.
+    We might be able to re-use it for the next link.*/
+  _of->op_count=0;
+  _of->od_buffer_size=0;
+  _of->prev_packet_gp=-1;
+  if(!_of->seekable){
+    OP_ASSERT(_of->ready_state>=OP_INITSET);
+    opus_tags_clear(&_of->links[0].tags);
+  }
+  _of->ready_state=OP_OPENED;
+}
+
+static void op_clear(OggOpusFile *_of){
+  OggOpusLink *links;
+  _ogg_free(_of->od_buffer);
+  if(_of->od!=NULL)opus_multistream_decoder_destroy(_of->od);
+  links=_of->links;
+  if(!_of->seekable){
+    if(_of->ready_state>OP_OPENED)opus_tags_clear(&links[0].tags);
+  }
+  else if(OP_LIKELY(links!=NULL)){
+    int nlinks;
+    int link;
+    nlinks=_of->nlinks;
+    for(link=0;link<nlinks;link++)opus_tags_clear(&links[link].tags);
+  }
+  _ogg_free(links);
+  _ogg_free(_of->serialnos);
+  ogg_stream_clear(&_of->os);
+  ogg_sync_clear(&_of->oy);
+  if(_of->callbacks.close!=NULL)(*_of->callbacks.close)(_of->source);
+}
+
+static int op_open1(OggOpusFile *_of,
+ void *_source,const OpusFileCallbacks *_cb,
+ const unsigned char *_initial_data,size_t _initial_bytes){
+  ogg_page  og;
+  ogg_page *pog;
+  int       seekable;
+  int       ret;
+  memset(_of,0,sizeof(*_of));
+  _of->source=_source;
+  *&_of->callbacks=*_cb;
+  /*At a minimum, we need to be able to read data.*/
+  if(OP_UNLIKELY(_of->callbacks.read==NULL))return OP_EREAD;
+  /*Initialize the framing state.*/
+  ogg_sync_init(&_of->oy);
+  /*Perhaps some data was previously read into a buffer for testing against
+     other stream types.
+    Allow initialization from this previously read data (especially as we may
+     be reading from a non-seekable stream).
+    This requires copying it into a buffer allocated by ogg_sync_buffer() and
+     doesn't support seeking, so this is not a good mechanism to use for
+     decoding entire files from RAM.*/
+  if(_initial_bytes>0){
+    char *buffer;
+    buffer=ogg_sync_buffer(&_of->oy,_initial_bytes);
+    memcpy(buffer,_initial_data,_initial_bytes*sizeof(*buffer));
+    ogg_sync_wrote(&_of->oy,_initial_bytes);
+  }
+  /*Can we seek?
+    Stevens suggests the seek test is portable.*/
+  seekable=_cb->seek!=NULL&&(*_cb->seek)(_source,0,SEEK_CUR)!=-1;
+  /*If seek is implemented, tell must also be implemented.*/
+  if(seekable){
+    if(OP_UNLIKELY(_of->callbacks.tell==NULL))return OP_EINVAL;
+    else{
+      opus_int64 pos;
+      pos=(*_of->callbacks.tell)(_of->source);
+      /*If the current position is not equal to the initial bytes consumed,
+         absolute seeking will not work.*/
+      if(OP_UNLIKELY(pos!=(opus_int64)_initial_bytes))return OP_EINVAL;
+    }
+  }
+  _of->seekable=seekable;
+  /*Don't seek yet.
+    Set up a 'single' (current) logical bitstream entry for partial open.*/
+  _of->nlinks=1;
+  _of->links=(OggOpusLink *)_ogg_malloc(sizeof(*_of->links));
+  /*The serialno gets filled in later by op_fetch_headers().*/
+  ogg_stream_init(&_of->os,-1);
+  pog=NULL;
+  for(;;){
+    /*Fetch all BOS pages, store the Opus header and all seen serial numbers,
+      and load subsequent Opus setup headers.*/
+    ret=op_fetch_headers(_of,&_of->links[0].head,&_of->links[0].tags,
+     &_of->serialnos,&_of->nserialnos,&_of->cserialnos,pog);
+    if(OP_UNLIKELY(ret<0))break;
+    _of->links[0].offset=0;
+    _of->links[0].data_offset=_of->offset;
+    _of->links[0].pcm_end=-1;
+    _of->links[0].serialno=_of->os.serialno;
+    /*Fetch the initial PCM offset.*/
+    ret=op_find_initial_pcm_offset(_of,_of->links,&og);
+    if(seekable||OP_LIKELY(ret<=0))break;
+    /*This link was empty, but we already have the BOS page for the next one in
+       og.
+      We can't seek, so start processing the next link right now.*/
+    pog=&og;
+    _of->cur_link++;
+  }
+  if(OP_UNLIKELY(ret<0)){
+    /*Don't auto-close the stream on failure.*/
+    _of->callbacks.close=NULL;
+    op_clear(_of);
+  }
+  else _of->ready_state=OP_PARTOPEN;
+  return ret;
+}
+
+static int op_open2(OggOpusFile *_of){
+  int ret;
+  OP_ASSERT(_of->ready_state==OP_PARTOPEN);
+  if(_of->seekable){
+    _of->ready_state=OP_OPENED;
+    ret=op_open_seekable2(_of);
+  }
+  else{
+    /*We have buffered packets from op_find_initial_pcm_offset().
+      Move to OP_INITSET so we can use them.*/
+    _of->ready_state=OP_STREAMSET;
+    ret=op_make_decode_ready(_of);
+  }
+  if(OP_UNLIKELY(ret<0)){
+    /*Don't auto-close the stream on failure.*/
+    _of->callbacks.close=NULL;
+    op_clear(_of);
+    return ret;
+  }
+  return 0;
+}
+
+OggOpusFile *op_test_callbacks(void *_source,const OpusFileCallbacks *_cb,
+ const unsigned char *_initial_data,size_t _initial_bytes,int *_error){
+  OggOpusFile *of;
+  int          ret;
+  of=(OggOpusFile *)_ogg_malloc(sizeof(*of));
+  ret=OP_EFAULT;
+  if(OP_LIKELY(of!=NULL)){
+    ret=op_open1(of,_source,_cb,_initial_data,_initial_bytes);
+    if(OP_LIKELY(ret>=0)){
+      if(_error!=NULL)*_error=0;
+      return of;
+    }
+    _ogg_free(of);
+  }
+  if(_error!=NULL)*_error=ret;
+  return NULL;
+}
+
+OggOpusFile *op_open_callbacks(void *_source,const OpusFileCallbacks *_cb,
+ const unsigned char *_initial_data,size_t _initial_bytes,int *_error){
+  OggOpusFile *of;
+  of=op_test_callbacks(_source,_cb,_initial_data,_initial_bytes,_error);
+  if(OP_LIKELY(of!=NULL)){
+    int ret;
+    ret=op_open2(of);
+    if(OP_LIKELY(ret>=0))return of;
+    if(_error!=NULL)*_error=ret;
+    _ogg_free(of);
+  }
+  return NULL;
+}
+
+/*Convenience routine to clean up from failure for the open functions that
+   create their own streams.*/
+static OggOpusFile *op_open_close_on_failure(void *_source,
+ const OpusFileCallbacks *_cb,int *_error){
+  OggOpusFile *of;
+  if(OP_UNLIKELY(_source==NULL)){
+    if(_error!=NULL)*_error=OP_EFAULT;
+    return NULL;
+  }
+  of=op_open_callbacks(_source,_cb,NULL,0,_error);
+  if(OP_UNLIKELY(of==NULL))(*_cb->close)(_source);
+  return of;
+}
+
+OggOpusFile *op_open_file(const char *_path,int *_error){
+  OpusFileCallbacks cb;
+  return op_open_close_on_failure(op_fopen(&cb,_path,"rb"),&cb,_error);
+}
+
+OggOpusFile *op_open_memory(const unsigned char *_data,size_t _size,
+ int *_error){
+  OpusFileCallbacks cb;
+  return op_open_close_on_failure(op_mem_stream_create(&cb,_data,_size),&cb,
+   _error);
+}
+
+/*Convenience routine to clean up from failure for the open functions that
+   create their own streams.*/
+static OggOpusFile *op_test_close_on_failure(void *_source,
+ const OpusFileCallbacks *_cb,int *_error){
+  OggOpusFile *of;
+  if(OP_UNLIKELY(_source==NULL)){
+    if(_error!=NULL)*_error=OP_EFAULT;
+    return NULL;
+  }
+  of=op_test_callbacks(_source,_cb,NULL,0,_error);
+  if(OP_UNLIKELY(of==NULL))(*_cb->close)(_source);
+  return of;
+}
+
+OggOpusFile *op_test_file(const char *_path,int *_error){
+  OpusFileCallbacks cb;
+  return op_test_close_on_failure(op_fopen(&cb,_path,"rb"),&cb,_error);
+}
+
+OggOpusFile *op_test_memory(const unsigned char *_data,size_t _size,
+ int *_error){
+  OpusFileCallbacks cb;
+  return op_test_close_on_failure(op_mem_stream_create(&cb,_data,_size),&cb,
+   _error);
+}
+
+int op_test_open(OggOpusFile *_of){
+  int ret;
+  if(OP_UNLIKELY(_of->ready_state!=OP_PARTOPEN))return OP_EINVAL;
+  ret=op_open2(_of);
+  /*op_open2() will clear this structure on failure.
+    Reset its contents to prevent double-frees in op_free().*/
+  if(OP_UNLIKELY(ret<0))memset(_of,0,sizeof(*_of));
+  return ret;
+}
+
+void op_free(OggOpusFile *_of){
+  if(OP_LIKELY(_of!=NULL)){
+    op_clear(_of);
+    _ogg_free(_of);
+  }
+}
+
+int op_link_count(OggOpusFile *_of){
+  return _of->nlinks;
+}
+
+int op_seekable(OggOpusFile *_of){
+  return _of->seekable;
+}
+
+ogg_uint32_t op_serialno(OggOpusFile *_of,int _li){
+  if(OP_UNLIKELY(_li>=_of->nlinks))_li=_of->nlinks-1;
+  if(!_of->seekable&&_li!=0)_li=0;
+  return _of->links[_li<0?_of->cur_link:_li].serialno;
+}
+
+int op_channel_count(OggOpusFile *_of,int _li){
+  if(OP_UNLIKELY(_li>=_of->nlinks))_li=_of->nlinks-1;
+  if(!_of->seekable&&_li!=0)_li=0;
+  return _of->links[_li<0?_of->cur_link:_li].head.channel_count;
+}
+
+opus_int64 op_raw_total(OggOpusFile *_of,int _li){
+  if(OP_UNLIKELY(_of->ready_state<OP_OPENED)
+   ||OP_UNLIKELY(!_of->seekable)
+   ||OP_UNLIKELY(_li>=_of->nlinks)){
+    return OP_EINVAL;
+  }
+  if(_li<0)return _of->end-_of->links[0].offset;
+  return (_li+1>=_of->nlinks?_of->end:_of->links[_li+1].offset)
+   -_of->links[_li].offset;
+}
+
+ogg_int64_t op_pcm_total(OggOpusFile *_of,int _li){
+  OggOpusLink *links;
+  ogg_int64_t  diff;
+  int          ret;
+  int          nlinks;
+  nlinks=_of->nlinks;
+  if(OP_UNLIKELY(_of->ready_state<OP_OPENED)
+   ||OP_UNLIKELY(!_of->seekable)
+   ||OP_UNLIKELY(_li>=nlinks)){
+    return OP_EINVAL;
+  }
+  links=_of->links;
+  /*We verify that the granule position differences are larger than the
+     pre-skip and that the total duration does not overflow during link
+     enumeration, so we don't have to check here.*/
+  if(_li<0){
+    ogg_int64_t pcm_total;
+    int         li;
+    pcm_total=0;
+    for(li=0;li<nlinks;li++){
+      ret=op_granpos_diff(&diff,links[li].pcm_end,links[li].pcm_start);
+      OP_ASSERT(!ret);
+      pcm_total+=diff-links[li].head.pre_skip;
+    }
+    return pcm_total;
+  }
+  ret=op_granpos_diff(&diff,links[_li].pcm_end,links[_li].pcm_start);
+  OP_ASSERT(!ret);
+  return diff-links[_li].head.pre_skip;
+}
+
+const OpusHead *op_head(OggOpusFile *_of,int _li){
+  if(!_of->seekable)_li=0;
+  else if(_li<0)_li=_of->ready_state>=OP_STREAMSET?_of->cur_link:0;
+  return _li>=_of->nlinks?NULL:&_of->links[_li].head;
+}
+
+const OpusTags *op_tags(OggOpusFile *_of,int _li){
+  if(!_of->seekable)_li=0;
+  else if(_li<0)_li=_of->ready_state>=OP_STREAMSET?_of->cur_link:0;
+  return _li>=_of->nlinks?NULL:&_of->links[_li].tags;
+}
+
+/*Compute an average bitrate given a byte and sample count.
+  Return: The bitrate in bits per second.*/
+static opus_int32 op_calc_bitrate(opus_int64 _bytes,ogg_int64_t _samples){
+  /*These rates are absurd, but let's handle them anyway.*/
+  if(OP_UNLIKELY(_bytes>(OP_INT64_MAX-(_samples>>1))/(48000*8))){
+    ogg_int64_t den;
+    if(OP_UNLIKELY(_bytes/(0x7FFFFFFFF/(48000*8))>=_samples))return 0x7FFFFFFF;
+    den=_samples/(48000*8);
+    return (_bytes+(den>>1))/den;
+  }
+  if(OP_UNLIKELY(_samples<=0))return 0x7FFFFFFF;
+  /*This can't actually overflow in normal operation: even with a pre-skip of
+     545 2.5 ms frames with 8 streams running at 1282*8+1 bytes per packet
+     (1275 byte frames + Opus framing overhead + Ogg lacing values), that all
+     produce a single sample of decoded output, we still don't top 45 Mbps.
+    The only way to get bitrates larger than that is with excessive Opus
+     padding, more encoded streams than output channels, or lots and lots of
+     Ogg pages with no packets on them.*/
+  return (opus_int32)OP_MIN((_bytes*48000*8+(_samples>>1))/_samples,0x7FFFFFFF);
+}
+
+opus_int32 op_bitrate(OggOpusFile *_of,int _li){
+  if(OP_UNLIKELY(_of->ready_state<OP_OPENED)||OP_UNLIKELY(!_of->seekable)
+   ||OP_UNLIKELY(_li>=_of->nlinks)){
+    return OP_EINVAL;
+  }
+  return op_calc_bitrate(op_raw_total(_of,_li),op_pcm_total(_of,_li));
+}
+
+opus_int32 op_bitrate_instant(OggOpusFile *_of){
+  ogg_int64_t samples_tracked;
+  opus_int32  ret;
+  if(OP_UNLIKELY(_of->ready_state<OP_OPENED))return OP_EINVAL;
+  samples_tracked=_of->samples_tracked;
+  if(OP_UNLIKELY(samples_tracked==0))return OP_FALSE;
+  ret=op_calc_bitrate(_of->bytes_tracked,samples_tracked);
+  _of->bytes_tracked=0;
+  _of->samples_tracked=0;
+  return ret;
+}
+
+/*Fetch and process a page.
+  This handles the case where we're at a bitstream boundary and dumps the
+   decoding machine.
+  If the decoding machine is unloaded, it loads it.
+  It also keeps prev_packet_gp up to date (seek and read both use this; seek
+   uses a special hack with _readp).
+  Return: <0) Error, OP_HOLE (lost packet), or OP_EOF.
+           0) Need more data (only if _readp==0).
+           1) Got at least one audio data packet.*/
+static int op_fetch_and_process_page(OggOpusFile *_of,int _readp,int _spanp){
+  OggOpusLink  *links;
+  ogg_uint32_t  cur_serialno;
+  int           seekable;
+  int           cur_link;
+  int           ret;
+  if(OP_LIKELY(_of->ready_state>=OP_INITSET)
+   &&OP_LIKELY(_of->op_pos<_of->op_count)){
+    /*We're ready to decode and have at least one packet available already.*/
+    return 1;
+  }
+  if(!_readp)return 0;
+  seekable=_of->seekable;
+  links=_of->links;
+  cur_link=seekable?_of->cur_link:0;
+  cur_serialno=links[cur_link].serialno;
+  /*Handle one page.*/
+  for(;;){
+    ogg_page   og;
+    opus_int64 page_pos;
+    OP_ASSERT(_of->ready_state>=OP_OPENED);
+    /*This loop is not strictly necessary, but there's no sense in doing the
+       extra checks of the larger loop for the common case in a multiplexed
+       bistream where the page is simply part of a different logical
+       bitstream.*/
+    do{
+      /*Keep reading until we get a page with the correct serialno.*/
+      page_pos=op_get_next_page(_of,&og,-1);
+      /*EOF: Leave uninitialized.*/
+      if(page_pos<0)return OP_EOF;
+      if(OP_LIKELY(_of->ready_state>=OP_STREAMSET)){
+        if(cur_serialno!=(ogg_uint32_t)ogg_page_serialno(&og)){
+          /*Two possibilities:
+             1) Another stream is multiplexed into this logical section, or*/
+          if(OP_LIKELY(!ogg_page_bos(&og)))continue;
+          /* 2) Our decoding just traversed a bitstream boundary.*/
+          if(!_spanp)return OP_EOF;
+          if(OP_LIKELY(_of->ready_state>=OP_INITSET))op_decode_clear(_of);
+          break;
+        }
+      }
+      /*Bitrate tracking: add the header's bytes here.
+        The body bytes are counted when we consume the packets.*/
+      _of->bytes_tracked+=og.header_len;
+    }
+    while(0);
+    /*Do we need to load a new machine before submitting the page?
+      This is different in the seekable and non-seekable cases.
+      In the seekable case, we already have all the header information loaded
+       and cached.
+      We just initialize the machine with it and continue on our merry way.
+      In the non-seekable (streaming) case, we'll only be at a boundary if we
+       just left the previous logical bitstream, and we're now nominally at the
+       header of the next bitstream.*/
+    if(OP_UNLIKELY(_of->ready_state<OP_STREAMSET)){
+      if(seekable){
+        ogg_uint32_t serialno;
+        int          nlinks;
+        int          li;
+        serialno=ogg_page_serialno(&og);
+        /*Match the serialno to bitstream section.
+          We use this rather than offset positions to avoid problems near
+           logical bitstream boundaries.*/
+        nlinks=_of->nlinks;
+        for(li=0;li<nlinks&&links[li].serialno!=serialno;li++);
+        /*Not a desired Opus bitstream section.
+          Keep trying.*/
+        if(li>=nlinks)continue;
+        cur_serialno=serialno;
+        _of->cur_link=cur_link=li;
+        ogg_stream_reset_serialno(&_of->os,serialno);
+        _of->ready_state=OP_STREAMSET;
+        /*If we're at the start of this link, initialize the granule position
+           and pre-skip tracking.*/
+        if(page_pos<=links[cur_link].data_offset){
+          _of->prev_packet_gp=links[cur_link].pcm_start;
+          _of->cur_discard_count=links[cur_link].head.pre_skip;
+        }
+      }
+      else{
+        do{
+          /*We're streaming.
+            Fetch the two header packets, build the info struct.*/
+          ret=op_fetch_headers(_of,&links[0].head,&links[0].tags,
+           NULL,NULL,NULL,&og);
+          if(OP_UNLIKELY(ret<0))return ret;
+          ret=op_find_initial_pcm_offset(_of,links,&og);
+          if(OP_UNLIKELY(ret<0))return ret;
+          _of->links[0].serialno=cur_serialno=_of->os.serialno;
+          _of->cur_link++;
+        }
+        /*If the link was empty, keep going, because we already have the
+           BOS page of the next one in og.*/
+        while(OP_UNLIKELY(ret>0));
+        /*If we didn't get any packets out of op_find_initial_pcm_offset(),
+           keep going (this is possible if end-trimming trimmed them all).*/
+        if(_of->op_count<=0)continue;
+        /*Otherwise, we're done.*/
+        ret=op_make_decode_ready(_of);
+        if(OP_UNLIKELY(ret<0))return ret;
+        return 1;
+      }
+    }
+    /*The buffered page is the data we want, and we're ready for it.
+      Add it to the stream state.*/
+    if(OP_UNLIKELY(_of->ready_state==OP_STREAMSET)){
+      ret=op_make_decode_ready(_of);
+      if(OP_UNLIKELY(ret<0))return ret;
+    }
+    /*Extract all the packets from the current page.*/
+    ogg_stream_pagein(&_of->os,&og);
+    if(OP_LIKELY(_of->ready_state>=OP_INITSET)){
+      opus_int32 total_duration;
+      int        durations[255];
+      int        op_count;
+      total_duration=op_collect_audio_packets(_of,durations);
+      /*Report holes to the caller.*/
+      if(OP_UNLIKELY(total_duration<0))return (int)total_duration;
+      op_count=_of->op_count;
+      /*If we found at least one audio data packet, compute per-packet granule
+         positions for them.*/
+      if(op_count>0){
+        ogg_int64_t diff;
+        ogg_int64_t prev_packet_gp;
+        ogg_int64_t cur_packet_gp;
+        ogg_int64_t cur_page_gp;
+        int         cur_page_eos;
+        int         pi;
+        cur_page_gp=_of->op[op_count-1].granulepos;
+        cur_page_eos=_of->op[op_count-1].e_o_s;
+        prev_packet_gp=_of->prev_packet_gp;
+        if(OP_UNLIKELY(prev_packet_gp==-1)){
+          opus_int32 cur_discard_count;
+          /*This is the first call after a raw seek.
+            Try to reconstruct prev_packet_gp from scratch.*/
+          OP_ASSERT(seekable);
+          if(OP_UNLIKELY(cur_page_eos)){
+            /*If the first page we hit after our seek was the EOS page, and
+               we didn't start from data_offset or before, we don't have
+               enough information to do end-trimming.
+              Proceed to the next link, rather than risk playing back some
+               samples that shouldn't have been played.*/
+            _of->op_count=0;
+            continue;
+          }
+          /*By default discard 80 ms of data after a seek, unless we seek
+             into the pre-skip region.*/
+          cur_discard_count=80*48;
+          cur_page_gp=_of->op[op_count-1].granulepos;
+          /*Try to initialize prev_packet_gp.
+            If the current page had packets but didn't have a granule
+             position, or the granule position it had was too small (both
+             illegal), just use the starting granule position for the link.*/
+          prev_packet_gp=links[cur_link].pcm_start;
+          if(OP_LIKELY(cur_page_gp!=-1)){
+            op_granpos_add(&prev_packet_gp,cur_page_gp,-total_duration);
+          }
+          if(OP_LIKELY(!op_granpos_diff(&diff,
+           prev_packet_gp,links[cur_link].pcm_start))){
+            opus_int32 pre_skip;
+            /*If we start at the beginning of the pre-skip region, or we're
+               at least 80 ms from the end of the pre-skip region, we discard
+               to the end of the pre-skip region.
+              Otherwise, we still use the 80 ms default, which will discard
+               past the end of the pre-skip region.*/
+            pre_skip=links[cur_link].head.pre_skip;
+            if(diff>=0&&diff<=OP_MAX(0,pre_skip-80*48)){
+              cur_discard_count=pre_skip-(int)diff;
+            }
+          }
+          _of->cur_discard_count=cur_discard_count;
+        }
+        if(OP_UNLIKELY(cur_page_gp==-1)){
+          /*This page had completed packets but didn't have a valid granule
+             position.
+            This is illegal, but we'll try to handle it by continuing to count
+             forwards from the previous page.*/
+          if(op_granpos_add(&cur_page_gp,prev_packet_gp,total_duration)<0){
+            /*The timestamp for this page overflowed.*/
+            cur_page_gp=links[cur_link].pcm_end;
+          }
+        }
+        /*If we hit the last page, handle end-trimming.*/
+        if(OP_UNLIKELY(cur_page_eos)
+         &&OP_LIKELY(!op_granpos_diff(&diff,cur_page_gp,prev_packet_gp))
+         &&OP_LIKELY(diff<total_duration)){
+          cur_packet_gp=prev_packet_gp;
+          for(pi=0;pi<op_count;pi++){
+            diff=durations[pi]-diff;
+            /*If we have samples to trim...*/
+            if(diff>0){
+              /*If we trimmed the entire packet, stop (the spec says encoders
+                 shouldn't do this, but we support it anyway).*/
+              if(OP_UNLIKELY(diff>durations[pi]))break;
+              cur_packet_gp=cur_page_gp;
+              /*Move the EOS flag to this packet, if necessary, so we'll trim
+                 the samples during decode.*/
+              _of->op[pi].e_o_s=1;
+            }
+            else{
+              /*Update the granule position as normal.*/
+              ret=op_granpos_add(&cur_packet_gp,cur_packet_gp,durations[pi]);
+              OP_ASSERT(!ret);
+            }
+            _of->op[pi].granulepos=cur_packet_gp;
+            ret=op_granpos_diff(&diff,cur_page_gp,cur_packet_gp);
+            OP_ASSERT(!ret);
+          }
+        }
+        else{
+          /*Propagate timestamps to earlier packets.
+            op_granpos_add(&prev_packet_gp,prev_packet_gp,total_duration)
+             should succeed and give prev_packet_gp==cur_page_gp.
+            But we don't bother to check that, as there isn't much we can do
+             if it's not true.
+            The only thing we guarantee is that the start and end granule
+             positions of the packets are valid, and that they are monotonic
+             within a page.
+            They might be completely out of range for this link (we'll check
+             that elsewhere), or non-monotonic between pages.*/
+          if(OP_UNLIKELY(op_granpos_add(&prev_packet_gp,
+           cur_page_gp,-total_duration)<0)){
+            /*The starting timestamp for the first packet on this page
+               underflowed.
+              This is illegal, but we ignore it.*/
+            prev_packet_gp=0;
+          }
+          for(pi=0;pi<op_count;pi++){
+            if(OP_UNLIKELY(op_granpos_add(&cur_packet_gp,
+             cur_page_gp,-total_duration)<0)){
+              /*The start timestamp for this packet underflowed.
+                This is illegal, but we ignore it.*/
+              cur_packet_gp=0;
+            }
+            total_duration-=durations[pi];
+            OP_ASSERT(total_duration>=0);
+            ret=op_granpos_add(&cur_packet_gp,cur_packet_gp,durations[pi]);
+            OP_ASSERT(!ret);
+            _of->op[pi].granulepos=cur_packet_gp;
+          }
+          OP_ASSERT(total_duration==0);
+        }
+        _of->prev_packet_gp=prev_packet_gp;
+        _of->op_count=pi;
+        /*If end-trimming didn't trim all the packets, we're done.*/
+        if(OP_LIKELY(pi>0))return 1;
+      }
+    }
+  }
+}
+
+int op_raw_seek(OggOpusFile *_of,opus_int64 _pos){
+  OggOpusLink *links;
+  int          nlinks;
+  int          cur_link;
+  int          ret;
+  if(OP_UNLIKELY(_of->ready_state<OP_OPENED))return OP_EINVAL;
+  /*Don't dump the decoder state if we can't seek.*/
+  if(OP_UNLIKELY(!_of->seekable))return OP_ENOSEEK;
+  if(OP_UNLIKELY(_pos<0)||OP_UNLIKELY(_pos>_of->end))return OP_EINVAL;
+  links=_of->links;
+  nlinks=_of->nlinks;
+  cur_link=_of->cur_link;
+  /*Clear out any buffered, decoded data.*/
+  op_decode_clear(_of);
+  _of->bytes_tracked=0;
+  _of->samples_tracked=0;
+  ret=op_seek_helper(_of,_pos);
+  if(OP_UNLIKELY(ret<0))return OP_EREAD;
+  do ret=op_fetch_and_process_page(_of,1,1);
+  /*Ignore holes.*/
+  while(ret==OP_HOLE);
+  /*If we hit EOF, op_fetch_and_process_page() leaves us uninitialized.
+    Instead, jump to the end.*/
+  if(ret==OP_EOF){
+    cur_link=nlinks-1;
+    op_decode_clear(_of);
+    _of->cur_link=cur_link;
+    _of->prev_packet_gp=links[cur_link].pcm_end;
+    _of->cur_discard_count=0;
+    ret=0;
+  }
+  else if(ret>0)ret=0;
+  return ret;
+}
+
+/*Convert a PCM offset relative to the start of the whole stream to a granule
+   position in an individual link.*/
+static ogg_int64_t op_get_granulepos(const OggOpusFile *_of,
+ ogg_int64_t _pcm_offset,int *_li){
+  OggOpusLink *links;
+  ogg_int64_t  duration;
+  int          nlinks;
+  int          li;
+  int          ret;
+  OP_ASSERT(_pcm_offset>=0);
+  nlinks=_of->nlinks;
+  links=_of->links;
+  for(li=0;OP_LIKELY(li<nlinks);li++){
+    ogg_int64_t pcm_start;
+    opus_int32  pre_skip;
+    pcm_start=links[li].pcm_start;
+    pre_skip=links[li].head.pre_skip;
+    ret=op_granpos_diff(&duration,links[li].pcm_end,pcm_start);
+    OP_ASSERT(!ret);
+    duration-=pre_skip;
+    if(_pcm_offset<duration){
+      _pcm_offset+=pre_skip;
+      if(OP_UNLIKELY(pcm_start>OP_INT64_MAX-_pcm_offset)){
+        /*Adding this amount to the granule position would overflow the positive
+           half of its 64-bit range.
+          Since signed overflow is undefined in C, do it in a way the compiler
+           isn't allowed to screw up.*/
+        _pcm_offset-=OP_INT64_MAX-pcm_start+1;
+        pcm_start=OP_INT64_MIN;
+      }
+      pcm_start+=_pcm_offset;
+      *_li=li;
+      return pcm_start;
+    }
+    _pcm_offset-=duration;
+  }
+  return -1;
+}
+
+/*Rescale the number _x from the range [0,_from] to [0,_to].
+  _from and _to must be positive.*/
+opus_int64 op_rescale64(opus_int64 _x,opus_int64 _from,opus_int64 _to){
+  opus_int64 frac;
+  opus_int64 ret;
+  int        i;
+  if(_x>=_from)return _to;
+  if(_x<=0)return 0;
+  frac=0;
+  for(i=0;i<63;i++){
+    frac<<=1;
+    OP_ASSERT(_x<=_from);
+    if(_x>=_from>>1){
+      _x-=_from-_x;
+      frac|=1;
+    }
+    else _x<<=1;
+  }
+  ret=0;
+  for(i=0;i<63;i++){
+    if(frac&1)ret=(ret&_to&1)+(ret>>1)+(_to>>1);
+    else ret>>=1;
+    frac>>=1;
+  }
+  return ret;
+}
+
+/*Search within link _li for the page with the highest granule position
+   preceding (or equal to) _target_gp.
+  There is a danger here: missing pages or incorrect frame number information
+   in the bitstream could make our task impossible.
+  Account for that (it would be an error condition).*/
+static int op_pcm_seek_page_impl(OggOpusFile *_of,
+ ogg_int64_t _target_gp,int _li){
+  OggOpusLink  *link;
+  ogg_page      og;
+  ogg_int64_t   pcm_pre_skip;
+  ogg_int64_t   pcm_start;
+  ogg_int64_t   pcm_end;
+  ogg_int64_t   best_gp;
+  ogg_int64_t   diff;
+  ogg_uint32_t  serialno;
+  opus_int32    pre_skip;
+  opus_int32    cur_discard_count;
+  opus_int64    begin;
+  opus_int64    end;
+  opus_int64    best;
+  opus_int64    llret;
+  int           ret;
+  _of->bytes_tracked=0;
+  _of->samples_tracked=0;
+  op_decode_clear(_of);
+  /*New search algorithm by HB (Nicholas Vinen).*/
+  link=_of->links+_li;
+  best_gp=pcm_start=link->pcm_start;
+  pcm_end=link->pcm_end;
+  serialno=link->serialno;
+  best=begin=link->data_offset;
+  /*We discard the first 80 ms of data after a seek, so seek back that much
+     farther.
+    If we can't, simply seek to the beginning of the link.*/
+  if(OP_UNLIKELY(op_granpos_add(&_target_gp,_target_gp,-80*48)<0)){
+    _target_gp=pcm_start;
+  }
+  /*Special case seeking to the start of the link.*/
+  pre_skip=link->head.pre_skip;
+  ret=op_granpos_add(&pcm_pre_skip,pcm_start,pre_skip);
+  OP_ASSERT(!ret);
+  end=op_granpos_cmp(_target_gp,pcm_pre_skip)<0?begin:link->end_offset;
+  llret=OP_FALSE;
+  while(begin<end){
+    opus_int64 bisect;
+    if(end-begin<OP_CHUNK_SIZE)bisect=begin;
+    else{
+      ogg_int64_t diff2;
+      ret=op_granpos_diff(&diff,_target_gp,pcm_start);
+      OP_ASSERT(!ret);
+      ret=op_granpos_diff(&diff2,pcm_end,pcm_start);
+      OP_ASSERT(!ret);
+      /*Take a (pretty decent) guess.*/
+      bisect=begin+op_rescale64(diff,diff2,end-begin)-OP_CHUNK_SIZE;
+      if(bisect<begin+OP_CHUNK_SIZE)bisect=begin;
+    }
+    if(bisect!=_of->offset){
+      ret=op_seek_helper(_of,bisect);
+      if(OP_UNLIKELY(ret<0))return ret;
+    }
+    while(begin<end){
+      llret=op_get_next_page(_of,&og,end-_of->offset);
+      if(llret==OP_EREAD)return OP_EBADLINK;
+      if(llret<0){
+        /*Found it.*/
+        if(bisect<=begin+1)end=begin;
+        else{
+          bisect=OP_MAX(bisect-OP_CHUNK_SIZE,begin+1);
+          ret=op_seek_helper(_of,bisect);
+          if(OP_UNLIKELY(ret<0))return ret;
+        }
+      }
+      else{
+        ogg_int64_t gp;
+        if(serialno!=(ogg_uint32_t)ogg_page_serialno(&og))continue;
+        gp=ogg_page_granulepos(&og);
+        if(gp==-1)continue;
+        if(op_granpos_cmp(gp,_target_gp)<0){
+          /*Advance to the raw offset of the next page.*/
+          begin=_of->offset;
+          /*Don't let pcm_start get smaller!
+            That could happen with an invalid timestamp.*/
+          if(op_granpos_cmp(pcm_start,gp)<=0){
+            /*Save the byte offset of the end of the page with this granule
+               position.*/
+            best=_of->offset;
+            best_gp=pcm_start=gp;
+          }
+          if(OP_UNLIKELY(op_granpos_diff(&diff,_target_gp,pcm_start)<0)
+           ||diff>48000){
+            break;
+          }
+          /*NOT begin+1.*/
+          bisect=begin;
+        }
+        else{
+          /*Found it.*/
+          if(bisect<=begin+1)end=begin;
+          else{
+            /*We're pretty close.
+              We'd be stuck in an endless loop otherwise.*/
+            if(end==_of->offset){
+              end=llret;
+              bisect=OP_MAX(bisect-OP_CHUNK_SIZE,begin+1);
+              ret=op_seek_helper(_of,bisect);
+              if(OP_UNLIKELY(ret<0))return ret;
+            }
+            else{
+              end=bisect;
+              /*Don't let pcm_end get larger!
+                That could happen with an invalid timestamp.*/
+              if(OP_LIKELY(op_granpos_cmp(pcm_end,gp)>0))pcm_end=gp;
+              break;
+            }
+          }
+        }
+      }
+    }
+  }
+  /*Found our page.
+    Seek right after it and update prev_packet_gp and cur_discard_count.
+    This is an easier case than op_raw_seek(), as we don't need to keep any
+     packets from the page we found.*/
+  /*Seek, if necessary.*/
+  if(best!=_of->offset){
+    ret=op_seek_helper(_of,best);
+    if(OP_UNLIKELY(ret<0))return ret;
+  }
+  /*By default, discard 80 ms of data after a seek, unless we seek
+     into the pre-skip region.*/
+  cur_discard_count=80*48;
+  ret=op_granpos_diff(&diff,best_gp,pcm_start);
+  OP_ASSERT(!ret);
+  OP_ASSERT(diff>=0);
+  /*If we start at the beginning of the pre-skip region, or we're at least
+     80 ms from the end of the pre-skip region, we discard to the end of the
+     pre-skip region.
+    Otherwise, we still use the 80 ms default, which will discard past the end
+     of the pre-skip region.*/
+  if(diff<=OP_MAX(0,pre_skip-80*48))cur_discard_count=pre_skip-(int)diff;
+  _of->cur_link=_li;
+  _of->ready_state=OP_STREAMSET;
+  _of->prev_packet_gp=best_gp;
+  _of->cur_discard_count=cur_discard_count;
+  ogg_stream_reset_serialno(&_of->os,serialno);
+  do ret=op_fetch_and_process_page(_of,1,0);
+  /*Ignore holes.*/
+  while(ret==OP_HOLE);
+  if(OP_UNLIKELY(ret<=0))return OP_EBADLINK;
+  /*Verify result.*/
+  if(OP_UNLIKELY(op_granpos_cmp(_of->prev_packet_gp,_target_gp)>0)){
+    return OP_EBADLINK;
+  }
+  return 0;
+}
+
+int op_pcm_seek_page(OggOpusFile *_of,ogg_int64_t _pcm_offset){
+  ogg_int64_t target_gp;
+  int         li;
+  if(OP_UNLIKELY(_of->ready_state<OP_OPENED))return OP_EINVAL;
+  if(OP_UNLIKELY(!_of->seekable))return OP_ENOSEEK;
+  if(OP_UNLIKELY(_pcm_offset<0))return OP_EINVAL;
+  target_gp=op_get_granulepos(_of,_pcm_offset,&li);
+  if(OP_UNLIKELY(target_gp==-1))return OP_EINVAL;
+  return op_pcm_seek_page_impl(_of,target_gp,li);
+}
+
+int op_pcm_seek(OggOpusFile *_of,ogg_int64_t _pcm_offset){
+  OggOpusLink *link;
+  ogg_int64_t  pcm_start;
+  ogg_int64_t  target_gp;
+  ogg_int64_t  prev_packet_gp;
+  ogg_int64_t  skip;
+  ogg_int64_t  diff;
+  int          op_count;
+  int          op_pos;
+  int          ret;
+  int          li;
+  if(OP_UNLIKELY(_of->ready_state<OP_OPENED))return OP_EINVAL;
+  if(OP_UNLIKELY(!_of->seekable))return OP_ENOSEEK;
+  if(OP_UNLIKELY(_pcm_offset<0))return OP_EINVAL;
+  target_gp=op_get_granulepos(_of,_pcm_offset,&li);
+  if(OP_UNLIKELY(target_gp==-1))return OP_EINVAL;
+  ret=op_pcm_seek_page_impl(_of,target_gp,li);
+  /*Now skip samples until we actually get to our target.*/
+  link=_of->links+li;
+  pcm_start=link->pcm_start;
+  ret=op_granpos_diff(&_pcm_offset,target_gp,pcm_start);
+  OP_ASSERT(!ret);
+  /*Figure out where we should skip to.*/
+  if(_pcm_offset<=link->head.pre_skip)skip=0;
+  else skip=OP_MAX(_pcm_offset-80*48,0);
+  OP_ASSERT(_pcm_offset-skip>=0);
+  OP_ASSERT(_pcm_offset-skip<0x7FFFFFFF-120*48);
+  /*Skip packets until we find one with samples past our skip target.*/
+  for(;;){
+    op_count=_of->op_count;
+    prev_packet_gp=_of->prev_packet_gp;
+    for(op_pos=_of->op_pos;op_pos<op_count;op_pos++){
+      ogg_int64_t cur_packet_gp;
+      cur_packet_gp=_of->op[op_pos].granulepos;
+      if(OP_LIKELY(!op_granpos_diff(&diff,cur_packet_gp,pcm_start))
+       &&diff>skip){
+        break;
+      }
+      prev_packet_gp=cur_packet_gp;
+    }
+    _of->prev_packet_gp=prev_packet_gp;
+    _of->op_pos=op_pos;
+    if(op_pos<op_count)break;
+    /*We skipped all the packets on this page.
+      Fetch another.*/
+    do ret=op_fetch_and_process_page(_of,1,0);
+    /*Ignore holes.*/
+    while(ret==OP_HOLE);
+    if(OP_UNLIKELY(ret<=0))return OP_EBADLINK;
+  }
+  ret=op_granpos_diff(&diff,prev_packet_gp,pcm_start);
+  OP_ASSERT(!ret);
+  /*We skipped too far.
+    Either the timestamps were illegal or there was a hole in the data.*/
+  if(diff>skip)return OP_EBADLINK;
+  OP_ASSERT(_pcm_offset-diff<0x7FFFFFFF);
+  /*TODO: If there are further holes/illegal timestamps, we still won't decode
+     to the correct sample.
+    However, at least op_pcm_tell() will report the correct value immediately
+     after returning.*/
+  _of->cur_discard_count=(opus_int32)(_pcm_offset-diff);
+  return 0;
+}
+
+opus_int64 op_raw_tell(OggOpusFile *_of){
+  if(OP_UNLIKELY(_of->ready_state<OP_OPENED))return OP_EINVAL;
+  return _of->offset;
+}
+
+/*Convert a granule position from a given link to a PCM offset relative to the
+   start of the whole stream.
+  For unseekable sources, this gets reset to 0 at the beginning of each link.*/
+static ogg_int64_t op_get_pcm_offset(const OggOpusFile *_of,
+ ogg_int64_t _gp,int _li){
+  OggOpusLink *links;
+  ogg_int64_t  pcm_offset;
+  ogg_int64_t  delta;
+  int          li;
+  links=_of->links;
+  pcm_offset=0;
+  OP_ASSERT(_li<_of->nlinks);
+  for(li=0;li<_li;li++){
+    op_granpos_diff(&delta,links[li].pcm_end,links[li].pcm_start);
+    delta-=links[li].head.pre_skip;
+    pcm_offset+=delta;
+  }
+  OP_ASSERT(_li>=0);
+  if(_of->seekable&&OP_UNLIKELY(op_granpos_cmp(_gp,links[_li].pcm_end)>0)){
+    _gp=links[_li].pcm_end;
+  }
+  if(OP_LIKELY(op_granpos_cmp(_gp,links[_li].pcm_start)>0)){
+    op_granpos_diff(&delta,_gp,links[_li].pcm_start);
+    if(delta<links[_li].head.pre_skip)delta=0;
+    else delta-=links[_li].head.pre_skip;
+    pcm_offset+=delta;
+  }
+  return pcm_offset;
+}
+
+ogg_int64_t op_pcm_tell(OggOpusFile *_of){
+  ogg_int64_t gp;
+  int         nbuffered;
+  int         ret;
+  int         li;
+  if(OP_UNLIKELY(_of->ready_state<OP_OPENED))return OP_EINVAL;
+  gp=_of->prev_packet_gp;
+  if(gp==-1)return 0;
+  nbuffered=OP_MAX(_of->od_buffer_size-_of->od_buffer_pos,0);
+  ret=op_granpos_add(&gp,gp,-nbuffered);
+  OP_ASSERT(!ret);
+  li=_of->seekable?_of->cur_link:0;
+  if(op_granpos_add(&gp,gp,_of->cur_discard_count)<0){
+    gp=_of->links[li].pcm_end;
+  }
+  return op_get_pcm_offset(_of,gp,li);
+}
+
+/*Allocate the decoder scratch buffer.
+  This is done lazily, since if the user provides large enough buffers, we'll
+   never need it.*/
+static int op_init_buffer(OggOpusFile *_of){
+  int nchannels_max;
+  if(_of->seekable){
+    OggOpusLink *links;
+    int          nlinks;
+    int          li;
+    links=_of->links;
+    nlinks=_of->nlinks;
+    nchannels_max=0;
+    for(li=0;li<nlinks;li++){
+      nchannels_max=OP_MAX(nchannels_max,links[li].head.channel_count);
+    }
+  }
+  else nchannels_max=OP_NCHANNELS_MAX;
+  _of->od_buffer=(op_sample *)_ogg_malloc(
+   sizeof(*_of->od_buffer)*nchannels_max*120*48);
+  if(_of->od_buffer==NULL)return OP_EFAULT;
+  return 0;
+}
+
+/*Read more samples from the stream, using the same API as op_read() or
+   op_read_float().*/
+static int op_read_native(OggOpusFile *_of,
+ op_sample *_pcm,int _buf_size,int *_li){
+  if(OP_UNLIKELY(_of->ready_state<OP_OPENED))return OP_EINVAL;
+  for(;;){
+    int ret;
+    if(OP_LIKELY(_of->ready_state>=OP_INITSET)){
+      int nchannels;
+      int od_buffer_pos;
+      int nsamples;
+      int op_pos;
+      nchannels=_of->links[_of->seekable?_of->cur_link:0].head.channel_count;
+      od_buffer_pos=_of->od_buffer_pos;
+      nsamples=_of->od_buffer_size-od_buffer_pos;
+      /*If we have buffered samples, return them.*/
+      if(OP_UNLIKELY(nsamples>0)){
+        if(OP_UNLIKELY(nsamples*nchannels>_buf_size)){
+          nsamples=_buf_size/nchannels;
+        }
+        memcpy(_pcm,_of->od_buffer+nchannels*od_buffer_pos,
+         sizeof(*_pcm)*nchannels*nsamples);
+        od_buffer_pos+=nsamples;
+        _of->od_buffer_pos=od_buffer_pos;
+        if(_li!=NULL)*_li=_of->cur_link;
+        return nsamples;
+      }
+      /*If we have buffered packets, decode one.*/
+      op_pos=_of->op_pos;
+      if(OP_LIKELY(op_pos<_of->op_count)){
+        ogg_packet  *pop;
+        ogg_int64_t  diff;
+        opus_int32   cur_discard_count;
+        int          duration;
+        int          trimmed_duration;
+        pop=_of->op+op_pos++;
+        _of->op_pos=op_pos;
+        cur_discard_count=_of->cur_discard_count;
+        duration=op_get_packet_duration(pop->packet,pop->bytes);
+        /*We don't buffer packets with an invalid TOC sequence.*/
+        OP_ASSERT(duration>0);
+        trimmed_duration=duration;
+        /*Perform end-trimming.*/
+        if(OP_UNLIKELY(pop->e_o_s)){
+          if(OP_UNLIKELY(op_granpos_cmp(pop->granulepos,
+           _of->prev_packet_gp)<=0)){
+            trimmed_duration=0;
+          }
+          else if(OP_LIKELY(!op_granpos_diff(&diff,
+           pop->granulepos,_of->prev_packet_gp))){
+            trimmed_duration=(int)OP_MIN(diff,trimmed_duration);
+          }
+        }
+        _of->prev_packet_gp=pop->granulepos;
+        if(OP_UNLIKELY(duration*nchannels>_buf_size)){
+          op_sample *buf;
+          /*If the user's buffer is too small, decode into a scratch buffer.*/
+          buf=_of->od_buffer;
+          if(OP_UNLIKELY(buf==NULL)){
+            ret=op_init_buffer(_of);
+            if(OP_UNLIKELY(ret<0))return ret;
+            buf=_of->od_buffer;
+          }
+#if defined(OP_FIXED_POINT)
+          ret=opus_multistream_decode(_of->od,
+           pop->packet,pop->bytes,buf,120*48,0);
+#else
+          ret=opus_multistream_decode_float(_of->od,
+           pop->packet,pop->bytes,buf,120*48,0);
+#endif
+          if(OP_UNLIKELY(ret<0))return OP_EBADPACKET;
+          OP_ASSERT(ret==duration);
+          /*Perform pre-skip/pre-roll.*/
+          od_buffer_pos=(int)OP_MIN(trimmed_duration,cur_discard_count);
+          cur_discard_count-=od_buffer_pos;
+          _of->cur_discard_count=cur_discard_count;
+          _of->od_buffer_pos=od_buffer_pos;
+          _of->od_buffer_size=trimmed_duration;
+          /*Update bitrate tracking based on the actual samples we used from
+             what was decoded.*/
+          _of->bytes_tracked+=pop->bytes;
+          _of->samples_tracked+=trimmed_duration-od_buffer_pos;
+          /*Don't grab another page yet.*/
+          if(OP_LIKELY(od_buffer_pos<trimmed_duration))continue;
+        }
+        else{
+          /*Otherwise decode directly into the user's buffer.*/
+#if defined(OP_FIXED_POINT)
+          ret=opus_multistream_decode(_of->od,pop->packet,pop->bytes,
+           _pcm,_buf_size/nchannels,0);
+#else
+          ret=opus_multistream_decode_float(_of->od,pop->packet,pop->bytes,
+           _pcm,_buf_size/nchannels,0);
+#endif
+          if(OP_UNLIKELY(ret<0))return OP_EBADPACKET;
+          OP_ASSERT(ret==duration);
+          if(OP_LIKELY(trimmed_duration>0)){
+            /*Perform pre-skip/pre-roll.*/
+            od_buffer_pos=(int)OP_MIN(trimmed_duration,cur_discard_count);
+            cur_discard_count-=od_buffer_pos;
+            _of->cur_discard_count=cur_discard_count;
+            if(OP_UNLIKELY(od_buffer_pos>0)
+             &&OP_LIKELY(od_buffer_pos<trimmed_duration)){
+              memmove(_pcm,_pcm+od_buffer_pos*nchannels,
+               sizeof(*_pcm)*(trimmed_duration-od_buffer_pos)*nchannels);
+            }
+            trimmed_duration-=od_buffer_pos;
+            /*Update bitrate tracking based on the actual samples we used from
+               what was decoded.*/
+            _of->bytes_tracked+=pop->bytes;
+            _of->samples_tracked+=trimmed_duration;
+            if(OP_LIKELY(trimmed_duration>0)){
+              if(_li!=NULL)*_li=_of->cur_link;
+              return trimmed_duration;
+            }
+          }
+        }
+      }
+    }
+    /*Suck in another page.*/
+    ret=op_fetch_and_process_page(_of,1,1);
+    if(OP_UNLIKELY(ret==OP_EOF)){
+      if(_li!=NULL)*_li=_of->cur_link;
+      return 0;
+    }
+    if(OP_UNLIKELY(ret<0))return ret;
+  }
+}
+
+#if defined(OP_FIXED_POINT)
+
+int op_read(OggOpusFile *_of,opus_int16 *_pcm,int _buf_size,int *_li){
+  return op_read_native(_of,_pcm,_buf_size,_li);
+}
+
+# if !defined(OP_DISABLE_FLOAT_API)
+int op_read_float(OggOpusFile *_of,float *_pcm,int _buf_size,int *_li){
+  int ret;
+  /*Ensure we have some decoded samples in our buffer.*/
+  ret=op_read_native(_of,NULL,0,_li);
+  /*Now convert them to float.*/
+  if(OP_LIKELY(ret>=0)&&OP_LIKELY(_of->ready_state>=OP_INITSET)){
+    int nchannels;
+    int od_buffer_pos;
+    nchannels=_of->links[_of->seekable?_of->cur_link:0].head.channel_count;
+    od_buffer_pos=_of->od_buffer_pos;
+    ret=_of->od_buffer_size-od_buffer_pos;
+    if(OP_LIKELY(ret>0)){
+      op_sample *buf;
+      int        i;
+      if(OP_UNLIKELY(ret*nchannels>_buf_size))ret=_buf_size/nchannels;
+      buf=_of->od_buffer+nchannels*od_buffer_pos;
+      _buf_size=ret*nchannels;
+      for(i=0;i<_buf_size;i++)_pcm[i]=(1.0F/32768)*buf[i];
+      od_buffer_pos+=ret;
+      _of->od_buffer_pos=od_buffer_pos;
+    }
+  }
+  return ret;
+}
+# endif
+
+#else
+
+# if defined(OP_HAVE_LRINTF)
+#  include <math.h>
+#  define op_float2int(_x) (lrintf(_x))
+# else
+#  define op_float2int(_x) ((int)((_x)+((_x)<0?-0.5F:0.5F)))
+# endif
+
+/*The dithering code here is adapted from opusdec, part of opus-tools.
+  It was originally written by Greg Maxwell.*/
+
+static opus_uint32 op_rand(opus_uint32 _seed){
+  return _seed*96314165+907633515&0xFFFFFFFFU;
+}
+
+/*This implements 16-bit quantization with full triangular dither and IIR noise
+   shaping.
+  The noise shaping filters were designed by Sebastian Gesemann, and are based
+   on the LAME ATH curves with flattening to limite their peak gain to 20 dB.
+  Everyone else's noise shaping filters are mildly crazy.
+  The 48 kHz version of this filter is just a warped version of the 44.1 kHz
+   filter and probably could be improved by shifting the HF shelf up in
+   frequency a little bit, since 48 kHz has a bit more room and being more
+   conservative against bat-ears is probably more important than more noise
+   suppression.
+  This process can increase the peak level of the signal (in theory by the peak
+   error of 1.5 +20 dB, though that is unobservably rare).
+  To avoid clipping, the signal is attenuated by a couple thousands of a dB.
+  Initially, the approach taken here was to only attenuate by the 99.9th
+   percentile, making clipping rare but not impossible (like SoX), but the
+   limited gain of the filter means that the worst case was only two
+   thousandths of a dB more, so this just uses the worst case.
+  The attenuation is probably also helpful to prevent clipping in the DAC
+   reconstruction filters or downstream resampling, in any case.*/
+
+#define OP_GAIN (32753.0F)
+
+#define OP_PRNG_GAIN (1.0F/0xFFFFFFFF)
+
+/*48 kHz noise shaping filter, sd=2.34.*/
+
+static const float OP_FCOEF_B[4]={
+  2.2374F,-0.7339F,-0.1251F,-0.6033F
+};
+
+static const float OP_FCOEF_A[4]={
+  0.9030F,0.0116F,-0.5853F,-0.2571F
+};
+
+static void op_shaped_dither16(OggOpusFile *_of,opus_int16 *_dst,float *_src,
+ int _nsamples,int _nchannels){
+  opus_uint32 seed;
+  int         mute;
+  int         i;
+  mute=_of->dither_mute;
+  seed=_of->dither_seed;
+  /*In order to avoid replacing digital silence with quiet dither noise, we
+     mute if the output has been silent for a while.*/
+  if(mute>64)memset(_of->dither_a,0,sizeof(*_of->dither_a)*4*_nchannels);
+  for(i=0;i<_nsamples;i++){
+    int silent;
+    int ci;
+    silent=1;
+    for(ci=0;ci<_nchannels;ci++){
+      float r;
+      float s;
+      float err;
+      int   si;
+      int   j;
+      s=_src[_nchannels*i+ci];
+      silent&=s==0;
+      s*=OP_GAIN;
+      err=0;
+      for(j=0;j<4;j++){
+        err+=OP_FCOEF_B[j]*_of->dither_b[ci*4+j]
+         -OP_FCOEF_A[j]*_of->dither_a[ci*4+j];
+      }
+      for(j=3;j-->0;)_of->dither_a[ci*4+j+1]=_of->dither_a[ci*4+j];
+      for(j=3;j-->0;)_of->dither_b[ci*4+j+1]=_of->dither_b[ci*4+j];
+      _of->dither_a[ci*4]=err;
+      s-=err;
+      if(mute>16)r=0;
+      else{
+        seed=op_rand(seed);
+        r=seed*OP_PRNG_GAIN;
+        seed=op_rand(seed);
+        r-=seed*OP_PRNG_GAIN;
+      }
+      /*Clamp in float out of paranoia that the input will be > 96 dBFS and
+         wrap if the integer is clamped.*/
+      si=op_float2int(OP_CLAMP(-32768,s+r,32767));
+      _dst[_nchannels*i+ci]=(opus_int16)si;
+      /*Including clipping in the noise shaping is generally disastrous: the
+         futile effort to restore the clipped energy results in more clipping.
+        However, small amounts---at the level which could normally be created
+         by dither and rounding---are harmless and can even reduce clipping
+         somewhat due to the clipping sometimes reducing the dither + rounding
+         error.*/
+      _of->dither_b[ci*4]=mute>16?0:OP_CLAMP(-1.5F,si-s,1.5F);
+    }
+    mute++;
+    if(!silent)mute=0;
+  }
+  _of->dither_mute=OP_MIN(mute,65);
+  _of->dither_seed=seed;
+}
+
+int op_read(OggOpusFile *_of,opus_int16 *_pcm,int _buf_size,int *_li){
+  int ret;
+  /*Ensure we have some decoded samples in our buffer.*/
+  ret=op_read_native(_of,NULL,0,_li);
+  /*Now convert them to shorts.*/
+  if(OP_LIKELY(ret>=0)&&OP_LIKELY(_of->ready_state>=OP_INITSET)){
+    int nchannels;
+    int od_buffer_pos;
+    nchannels=_of->links[_of->seekable?_of->cur_link:0].head.channel_count;
+    od_buffer_pos=_of->od_buffer_pos;
+    ret=_of->od_buffer_size-od_buffer_pos;
+    if(OP_LIKELY(ret>0)){
+      op_sample *buf;
+      if(OP_UNLIKELY(ret*nchannels>_buf_size))ret=_buf_size/nchannels;
+      buf=_of->od_buffer+nchannels*od_buffer_pos;
+      op_shaped_dither16(_of,_pcm,buf,ret,nchannels);
+      od_buffer_pos+=ret;
+      _of->od_buffer_pos=od_buffer_pos;
+    }
+  }
+  return ret;
+}
+
+int op_read_float(OggOpusFile *_of,float *_pcm,int _buf_size,int *_li){
+  return op_read_native(_of,_pcm,_buf_size,_li);
+}
+
+#endif
--- /dev/null
+++ b/src/stream.c
@@ -1,0 +1,174 @@
+/********************************************************************
+ *                                                                  *
+ * THIS FILE IS PART OF THE libopusfile SOFTWARE CODEC SOURCE CODE. *
+ * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
+ * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
+ * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
+ *                                                                  *
+ * THE libopusfile SOURCE CODE IS (C) COPYRIGHT 1994-2009           *
+ * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
+ *                                                                  *
+ ********************************************************************
+
+ function: stdio-based convenience library for opening/seeking/decoding
+ last mod: $Id: vorbisfile.c 17573 2010-10-27 14:53:59Z xiphmont $
+
+ ********************************************************************/
+#include "internal.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+
+typedef struct OpusMemStream OpusMemStream;
+
+#define OP_MEM_SIZE_MAX (~(size_t)0>>1)
+#define OP_MEM_DIFF_MAX ((ptrdiff_t)OP_MEM_SIZE_MAX)
+
+/*The context information needed to read from a block of memory as if it were a
+   file.*/
+struct OpusMemStream{
+  /*The block of memory to read from.*/
+  const unsigned char *data;
+  /*The total size of the block.
+    This must be at most OP_MEM_SIZE_MAX to prevent signed overflow while
+     seeking.*/
+  ptrdiff_t            size;
+  /*The current file position.
+    This is allowed to be set arbitrarily greater than size (i.e., past the end
+     of the block, though we will not read data past the end of the block), but
+     is not allowed to be negative (i.e., before the beginning of the block).*/
+  ptrdiff_t            pos;
+};
+
+static int op_fseek(void *_stream,opus_int64 _offset,int _whence){
+#if defined(_MSC_VER)
+  return _fseeki64((FILE *)_stream,_offset,_whence);
+#else
+  return fseeko((FILE *)_stream,(off_t)_offset,_whence);
+#endif
+}
+
+static opus_int64 op_ftell(void *_stream){
+#if defined(_MSC_VER)
+  return _ftelli64((FILE *)_stream);
+#else
+  return ftello((FILE *)_stream);
+#endif
+}
+
+static const OpusFileCallbacks OP_FILE_CALLBACKS={
+  (op_read_func)fread,
+  op_fseek,
+  op_ftell,
+  (op_close_func)fclose
+};
+
+void *op_fopen(OpusFileCallbacks *_cb,const char *_path,const char *_mode){
+  FILE *fp;
+  fp=fopen(_path,_mode);
+  if(fp!=NULL)*_cb=*&OP_FILE_CALLBACKS;
+  return fp;
+}
+
+void *op_fdopen(OpusFileCallbacks *_cb,int _fd,const char *_mode){
+  FILE *fp;
+  fp=fdopen(_fd,_mode);
+  if(fp!=NULL)*_cb=*&OP_FILE_CALLBACKS;
+  return fp;
+}
+
+void *op_freopen(OpusFileCallbacks *_cb,const char *_path,const char *_mode,
+ void *_stream){
+  FILE *fp;
+  fp=freopen(_path,_mode,(FILE *)_stream);
+  if(fp!=NULL)*_cb=*&OP_FILE_CALLBACKS;
+  return fp;
+}
+
+static size_t op_mem_read(void *_ptr,size_t _size,size_t _nmemb,void *_stream){
+  OpusMemStream *stream;
+  size_t         total;
+  ptrdiff_t      size;
+  ptrdiff_t      pos;
+  stream=(OpusMemStream *)_stream;
+  if(_size>OP_MEM_SIZE_MAX)return 0;
+  total=_size*_nmemb;
+  /*Check for overflow/empty read.*/
+  if(total==0||total/_size!=_nmemb)return 0;
+  size=stream->size;
+  pos=stream->pos;
+  /*Check for EOF.*/
+  if(pos>=size)return 0;
+  /*Check for a short read.*/
+  if(total>(size_t)(size-pos)){
+    _nmemb=(size-pos)/_size;
+    total=_size*_nmemb;
+  }
+  memcpy(_ptr,stream->data,total);
+  pos+=total;
+  stream->pos=pos;
+  return _nmemb;
+}
+
+static int op_mem_seek(void *_stream,opus_int64 _offset,int _whence){
+  OpusMemStream *stream;
+  ptrdiff_t      pos;
+  stream=(OpusMemStream *)_stream;
+  pos=stream->pos;
+  switch(_whence){
+    case SEEK_SET:{
+      /*Check for overflow:*/
+      if(_offset<0||_offset>OP_MEM_DIFF_MAX)return -1;
+      pos=(ptrdiff_t)_offset;
+    }break;
+    case SEEK_CUR:{
+      /*Check for overflow:*/
+      if(_offset<-pos||_offset>OP_MEM_DIFF_MAX-pos)return -1;
+      pos=(ptrdiff_t)(pos+_offset);
+    }break;
+    case SEEK_END:{
+      ptrdiff_t size;
+      size=stream->size;
+      OP_ASSERT(size>=0);
+      /*Check for overflow:*/
+      if(_offset>size||_offset<size-OP_MEM_DIFF_MAX)return -1;
+      pos=(ptrdiff_t)(size-_offset);
+    }break;
+    default:return -1;
+  }
+  stream->pos=pos;
+  return 0;
+}
+
+static opus_int64 op_mem_tell(void *_stream){
+  OpusMemStream *stream;
+  stream=(OpusMemStream *)_stream;
+  return (ogg_int64_t)stream->pos;
+}
+
+static int op_mem_close(void *_stream){
+  _ogg_free(_stream);
+  return 0;
+}
+
+static const OpusFileCallbacks OP_MEM_CALLBACKS={
+  op_mem_read,
+  op_mem_seek,
+  op_mem_tell,
+  op_mem_close
+};
+
+void *op_mem_stream_create(OpusFileCallbacks *_cb,
+ const unsigned char *_data,size_t _size){
+  OpusMemStream *stream;
+  if(_size>OP_MEM_SIZE_MAX)return NULL;
+  stream=(OpusMemStream *)_ogg_malloc(sizeof(*stream));
+  if(stream!=NULL){
+    *_cb=*&OP_MEM_CALLBACKS;
+    stream->data=_data;
+    stream->size=_size;
+    stream->pos=0;
+  }
+  return stream;
+}
--- /dev/null
+++ b/unix/Makefile
@@ -1,0 +1,182 @@
+# NOTE: This Makefile requires GNU make
+# Location to put the targets.
+TARGETBINDIR = .
+TESTBINDIR = tests
+TARGETLIBDIR = .
+# Name of the targets
+LIBOPUSFILE_TARGET = libopusfile.a
+OPUSFILE_EXAMPLE_TARGET = opusfile_example
+SEEKING_EXAMPLE_TARGET = seeking_example
+# Test targets
+#TODO: tests
+FOO_TARGET = foo
+# The command to use to generate dependency information
+MAKEDEPEND = ${CC} -MM
+#MAKEDEPEND = makedepend -f- -Y --
+# The location of include files.
+# Modify these to point to your Ogg and Vorbis include directories if they are
+#  not installed in a standard location.
+CINCLUDE = `pkg-config --cflags ogg opus`
+# These are gcc-only, but not actually critical.
+# Extra compilation flags.
+# You may get speed increases by including flags such as -O2 or -O3 or
+#  -ffast-math, or additional flags, depending on your system and compiler.
+# The -g flag will generally include debugging information.
+CFLAGS := -g $(CFLAGS)
+CFLAGS := -DOP_ENABLE_ASSERTIONS $(CFLAGS)
+CFLAGS := -fPIC $(CFLAGS)
+CFLAGS := -std=c89 -pedantic $(CFLAGS)
+CFLAGS := -fvisibility=hidden $(CFLAGS)
+CFLAGS := -Wextra -Wno-parentheses -Wno-long-long $(CFLAGS)
+CFLAGS := -Wall $(CFLAGS)
+
+# Libraries to link with, and the location of library files.
+LIBS = `pkg-config --libs ogg opus`
+
+#TODO: tests
+SEEKING_EXAMPLE_LIBS = -lm
+
+# ANYTHING BELOW THIS LINE PROBABLY DOES NOT NEED EDITING
+CINCLUDE := -I../include ${CINCLUDE}
+LIBSRCDIR = ../src
+BINSRCDIR = ../examples
+TESTSRCDIR = ${LIBSRCDIR}
+WORKDIR = objs
+
+# C source file lists
+LIBOPUSFILE_CSOURCES = \
+info.c \
+internal.c \
+opusfile.c \
+stream.c \
+
+LIBOPUSFILE_CHEADERS = \
+internal.h \
+
+OPUSFILE_EXAMPLE_CSOURCES = opusfile_example.c
+
+SEEKING_EXAMPLE_CSOURCES = seeking_example.c
+
+FOO_CSOURCES = tests/foo.c
+
+# Create object file list.
+LIBOPUSFILE_OBJS:= ${LIBOPUSFILE_CSOURCES:%.c=${WORKDIR}/%.o}
+LIBOPUSFILE_ASMS:= ${LIBOPUSFILE_OBJS:%.o=%.s}
+LIBOPUSFILE_DEPS:= ${LIBOPUSFILE_OBJS:%.o=%.d}
+OPUSFILE_EXAMPLE_OBJS:= ${OPUSFILE_EXAMPLE_CSOURCES:%.c=${WORKDIR}/%.o}
+SEEKING_EXAMPLE_OBJS:= ${SEEKING_EXAMPLE_CSOURCES:%.c=${WORKDIR}/%.o}
+#TODO: tests
+FOO_OBJS:= ${FOO_CSOURCES:%.c=${WORKDIR}/%.o}
+ALL_OBJS:= \
+ ${LIBOPUSFILE_OBJS} \
+ ${OPUSFILE_EXAMPLE_OBJS} \
+ ${SEEKING_EXAMPLE_OBJS} \
+
+#TODO: tests
+# ${FOO_OBJS}
+
+# Create the dependency file list
+ALL_DEPS:= ${ALL_OBJS:%.o=%.d}
+# Prepend source path to file names.
+LIBOPUSFILE_CSOURCES:= ${LIBOPUSFILE_CSOURCES:%=${LIBSRCDIR}/%}
+LIBOPUSFILE_CHEADERS:= ${LIBOPUSFILE_CHEADERS:%=${LIBSRCDIR}/%}
+OPUSFILE_EXAMPLE_CSOURCES:= ${OPUSFILE_EXAMPLE_CSOURCES:%=${BINSRCDIR}/%}
+SEEKING_EXAMPLE_CSOURCES:= ${SEEKING_EXAMPLE_CSOURCES:%=${BINSRCDIR}/%}
+#TODO: tests
+FOO_CSOURCES:= ${FOO_CSOURCES:%=${TESTSRCDIR}/%}
+ALL_CSOURCES:= \
+ ${LIBOPUSFILE_CSOURCES} \
+ ${OPUSFILE_EXAMPLE_CSOURCES} \
+ ${SEEKING_EXAMPLE_CSOURCES} \
+
+#TODO: tests
+# ${FOO_CSOURCES} \
+# Prepand target path to file names.
+LIBOPUSFILE_TARGET:= ${TARGETLIBDIR}/${LIBOPUSFILE_TARGET}
+OPUSFILE_EXAMPLE_TARGET:= ${TARGETBINDIR}/${OPUSFILE_EXAMPLE_TARGET}
+SEEKING_EXAMPLE_TARGET:= ${TARGETBINDIR}/${SEEKING_EXAMPLE_TARGET}
+# Prepend test path to file names.
+#TODO: tests
+FOO_TARGET:= ${TESTBINDIR}/${FOO_TARGET}
+# Complete set of targets
+ALL_TARGETS:= \
+ ${LIBOPUSFILE_TARGET} \
+ ${OPUSFILE_EXAMPLE_TARGET} \
+ ${SEEKING_EXAMPLE_TARGET} \
+
+#TODO: tests
+# ${FOO_TARGET} \
+
+# Targets:
+# Everything (default)
+all: ${ALL_TARGETS}
+
+# libopusfile
+${LIBOPUSFILE_TARGET}: ${LIBOPUSFILE_OBJS}
+	mkdir -p ${TARGETLIBDIR}
+	ar cqs $@ ${LIBOPUSFILE_OBJS}
+
+# opusfile_example
+${OPUSFILE_EXAMPLE_TARGET}: ${OPUSFILE_EXAMPLE_OBJS} ${LIBOPUSFILE_TARGET}
+	mkdir -p ${TARGETBINDIR}
+	${CC} ${CFLAGS} ${OPUSFILE_EXAMPLE_OBJS} ${LIBS} \
+         ${SEEKING_EXAMPLE_LIBS} ${LIBOPUSFILE_TARGET} -o $@
+
+# seeking_example
+${SEEKING_EXAMPLE_TARGET}: ${SEEKING_EXAMPLE_OBJS} ${LIBOPUSFILE_TARGET}
+	mkdir -p ${TARGETBINDIR}
+	${CC} ${CFLAGS} ${SEEKING_EXAMPLE_OBJS} ${LIBS} ${LIBOPUSFILE_TARGET} \
+         -o $@
+
+#TODO: tests
+# foo
+#${FOO_TARGET}: ${FOO_OBJS}
+#	mkdir -p ${TESTBINDIR}
+#	${CC} ${CFLAGS} ${FOO_OBJS} ${FOO_LIBS} -o $@
+
+# Assembly listing
+ALL_ASM := ${ALL_OBJS:%.o=%.s}
+asm: ${ALL_ASM}
+
+# Check that build is complete.
+check: all
+#TODO: tests
+#	${FOO_TARGET}
+
+# Remove all targets.
+clean:
+	${RM} ${ALL_ASM} ${ALL_OBJS} ${ALL_DEPS}
+	${RM} ${ALL_TARGETS}
+	-rmdir ${TESTBINDIR} ${WORKDIR}/tests ${WORKDIR}/x86 ${WORKDIR}
+
+# Make everything depend on changes in the Makefile
+${ALL_ASM} ${ALL_OBJS} ${ALL_DEPS} ${ALL_TARGETS} : Makefile
+
+# Specify which targets are phony for GNU make
+.PHONY : all clean check
+
+# Rules
+${WORKDIR}/%.d: ${LIBSRCDIR}/%.c
+	mkdir -p ${dir $@}
+	${MAKEDEPEND} ${CINCLUDE} ${CFLAGS} $< -MT ${@:%.d=%.o} > $@
+	${MAKEDEPEND} ${CINCLUDE} ${CFLAGS} $< -MT ${@:%.d=%.s} >> $@
+	${MAKEDEPEND} ${CINCLUDE} ${CFLAGS} $< -MT $@ >> $@
+${WORKDIR}/%.s: ${LIBSRCDIR}/%.c
+	mkdir -p ${dir $@}
+	${CC} ${CINCLUDE} ${CFLAGS} -S -o $@ $<
+${WORKDIR}/%.o: ${LIBSRCDIR}/%.c
+	mkdir -p ${dir $@}
+	${CC} ${CINCLUDE} ${CFLAGS} -c -o $@ $<
+
+${WORKDIR}/%.d : ${BINSRCDIR}/%.c
+	mkdir -p ${dir $@}
+	${MAKEDEPEND} ${CINCLUDE} ${CFLAGS} $< -MT ${@:%.d=%.o} > $@
+${WORKDIR}/%.s : ${BINSRCDIR}/%.c ${WORKDIR}/%.o
+	mkdir -p ${dir $@}
+	${CC} ${CINCLUDE} ${CFLAGS} -S -o $@ $<
+${WORKDIR}/%.o : ${BINSRCDIR}/%.c
+	mkdir -p ${dir $@}
+	${CC} ${CINCLUDE} ${CFLAGS} -c -o $@ $<
+
+# Include header file dependencies
+include ${ALL_DEPS}