misc/libtremor/vorbisfile.c
author nemo
Sun, 12 Jun 2011 10:33:55 -0400
changeset 5236 ab044774e29d
parent 5170 f7e49eff3708
permissions -rw-r--r--
Make flakes respond to wind a bit more strongly
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
     1
/********************************************************************
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
     2
 *                                                                  *
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
     3
 * THIS FILE IS PART OF THE OggVorbis 'TREMOR' CODEC SOURCE CODE.   *
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
     4
 *                                                                  *
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
     5
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
     6
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
     7
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
     8
 *                                                                  *
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
     9
 * THE OggVorbis 'TREMOR' SOURCE CODE IS (C) COPYRIGHT 1994-2003    *
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    10
 * BY THE Xiph.Org FOUNDATION http://www.xiph.org/                  *
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    11
 *                                                                  *
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    12
 ********************************************************************
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    13
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    14
 function: stdio-based convenience library for opening/seeking/decoding
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    15
 last mod: $Id: vorbisfile.c,v 1.6 2003/03/30 23:40:56 xiphmont Exp $
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    16
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    17
 ********************************************************************/
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    18
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    19
#include <stdlib.h>
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    20
#include <stdio.h>
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    21
#include <errno.h>
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    22
#include <string.h>
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    23
#include <math.h>
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    24
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    25
#include "ivorbiscodec.h"
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    26
#include "ivorbisfile.h"
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    27
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    28
#include "misc.h"
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    29
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    30
/* A 'chained bitstream' is a Vorbis bitstream that contains more than
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    31
   one logical bitstream arranged end to end (the only form of Ogg
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    32
   multiplexing allowed in a Vorbis bitstream; grouping [parallel
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    33
   multiplexing] is not allowed in Vorbis) */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    34
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    35
/* A Vorbis file can be played beginning to end (streamed) without
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    36
   worrying ahead of time about chaining (see decoder_example.c).  If
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    37
   we have the whole file, however, and want random access
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    38
   (seeking/scrubbing) or desire to know the total length/time of a
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    39
   file, we need to account for the possibility of chaining. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    40
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    41
/* We can handle things a number of ways; we can determine the entire
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    42
   bitstream structure right off the bat, or find pieces on demand.
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    43
   This example determines and caches structure for the entire
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    44
   bitstream, but builds a virtual decoder on the fly when moving
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    45
   between links in the chain. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    46
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    47
/* There are also different ways to implement seeking.  Enough
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    48
   information exists in an Ogg bitstream to seek to
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    49
   sample-granularity positions in the output.  Or, one can seek by
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    50
   picking some portion of the stream roughly in the desired area if
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    51
   we only want coarse navigation through the stream. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    52
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    53
/*************************************************************************
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    54
 * Many, many internal helpers.  The intention is not to be confusing; 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    55
 * rampant duplication and monolithic function implementation would be 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    56
 * harder to understand anyway.  The high level functions are last.  Begin
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    57
 * grokking near the end of the file */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    58
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    59
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    60
/* read a little more data from the file/pipe into the ogg_sync framer */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    61
static long _get_data(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    62
  errno=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    63
  if(vf->datasource){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    64
    unsigned char *buffer=ogg_sync_bufferin(vf->oy,CHUNKSIZE);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    65
    long bytes=(vf->callbacks.read_func)(buffer,1,CHUNKSIZE,vf->datasource);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    66
    if(bytes>0)ogg_sync_wrote(vf->oy,bytes);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    67
    if(bytes==0 && errno)return(-1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    68
    return(bytes);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    69
  }else
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    70
    return(0);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    71
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    72
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    73
/* save a tiny smidge of verbosity to make the code more readable */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    74
static void _seek_helper(OggVorbis_File *vf,ogg_int64_t offset){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    75
  if(vf->datasource){ 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    76
    (vf->callbacks.seek_func)(vf->datasource, offset, SEEK_SET);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    77
    vf->offset=offset;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    78
    ogg_sync_reset(vf->oy);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    79
  }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    80
    /* shouldn't happen unless someone writes a broken callback */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    81
    return;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    82
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    83
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    84
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    85
/* The read/seek functions track absolute position within the stream */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    86
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    87
/* from the head of the stream, get the next page.  boundary specifies
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    88
   if the function is allowed to fetch more data from the stream (and
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    89
   how much) or only use internally buffered data.
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    90
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    91
   boundary: -1) unbounded search
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    92
              0) read no additional data; use cached only
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    93
	      n) search for a new page beginning for n bytes
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    94
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    95
   return:   <0) did not find a page (OV_FALSE, OV_EOF, OV_EREAD)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    96
              n) found a page at absolute offset n 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    97
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    98
              produces a refcounted page */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    99
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   100
static ogg_int64_t _get_next_page(OggVorbis_File *vf,ogg_page *og,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   101
				  ogg_int64_t boundary){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   102
  if(boundary>0)boundary+=vf->offset;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   103
  while(1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   104
    long more;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   105
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   106
    if(boundary>0 && vf->offset>=boundary)return(OV_FALSE);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   107
    more=ogg_sync_pageseek(vf->oy,og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   108
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   109
    if(more<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   110
      /* skipped n bytes */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   111
      vf->offset-=more;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   112
    }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   113
      if(more==0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   114
	/* send more paramedics */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   115
	if(!boundary)return(OV_FALSE);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   116
	{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   117
	  long ret=_get_data(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   118
	  if(ret==0)return(OV_EOF);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   119
	  if(ret<0)return(OV_EREAD);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   120
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   121
      }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   122
	/* got a page.  Return the offset at the page beginning,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   123
           advance the internal offset past the page end */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   124
	ogg_int64_t ret=vf->offset;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   125
	vf->offset+=more;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   126
	return(ret);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   127
	
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   128
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   129
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   130
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   131
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   132
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   133
/* find the latest page beginning before the current stream cursor
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   134
   position. Much dirtier than the above as Ogg doesn't have any
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   135
   backward search linkage.  no 'readp' as it will certainly have to
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   136
   read. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   137
/* returns offset or OV_EREAD, OV_FAULT and produces a refcounted page */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   138
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   139
static ogg_int64_t _get_prev_page(OggVorbis_File *vf,ogg_page *og){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   140
  ogg_int64_t begin=vf->offset;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   141
  ogg_int64_t end=begin;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   142
  ogg_int64_t ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   143
  ogg_int64_t offset=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   144
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   145
  while(offset==-1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   146
    begin-=CHUNKSIZE;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   147
    if(begin<0)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   148
      begin=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   149
    _seek_helper(vf,begin);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   150
    while(vf->offset<end){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   151
      ret=_get_next_page(vf,og,end-vf->offset);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   152
      if(ret==OV_EREAD)return(OV_EREAD);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   153
      if(ret<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   154
	break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   155
      }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   156
	offset=ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   157
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   158
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   159
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   160
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   161
  /* we have the offset.  Actually snork and hold the page now */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   162
  _seek_helper(vf,offset);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   163
  ret=_get_next_page(vf,og,CHUNKSIZE);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   164
  if(ret<0)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   165
    /* this shouldn't be possible */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   166
    return(OV_EFAULT);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   167
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   168
  return(offset);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   169
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   171
/* finds each bitstream link one at a time using a bisection search
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   172
   (has to begin by knowing the offset of the lb's initial page).
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   173
   Recurses for each link so it can alloc the link storage after
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   174
   finding them all, then unroll and fill the cache at the same time */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   175
static int _bisect_forward_serialno(OggVorbis_File *vf,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   176
				    ogg_int64_t begin,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   177
				    ogg_int64_t searched,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   178
				    ogg_int64_t end,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   179
				    ogg_uint32_t currentno,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   180
				    long m){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   181
  ogg_int64_t endsearched=end;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   182
  ogg_int64_t next=end;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   183
  ogg_page og={0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   184
  ogg_int64_t ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   185
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   186
  /* the below guards against garbage seperating the last and
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   187
     first pages of two links. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   188
  while(searched<endsearched){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   189
    ogg_int64_t bisect;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   190
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   191
    if(endsearched-searched<CHUNKSIZE){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   192
      bisect=searched;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   193
    }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   194
      bisect=(searched+endsearched)/2;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   195
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   196
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   197
    _seek_helper(vf,bisect);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   198
    ret=_get_next_page(vf,&og,-1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   199
    if(ret==OV_EREAD)return(OV_EREAD);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   200
    if(ret<0 || ogg_page_serialno(&og)!=currentno){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   201
      endsearched=bisect;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   202
      if(ret>=0)next=ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   203
    }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   204
      searched=ret+og.header_len+og.body_len;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   205
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   206
    ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   207
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   208
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   209
  _seek_helper(vf,next);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   210
  ret=_get_next_page(vf,&og,-1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   211
  if(ret==OV_EREAD)return(OV_EREAD);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   212
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   213
  if(searched>=end || ret<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   214
    ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   215
    vf->links=m+1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   216
    vf->offsets=_ogg_malloc((vf->links+1)*sizeof(*vf->offsets));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   217
    vf->serialnos=_ogg_malloc(vf->links*sizeof(*vf->serialnos));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   218
    vf->offsets[m+1]=searched;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   219
  }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   220
    ret=_bisect_forward_serialno(vf,next,vf->offset,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   221
				 end,ogg_page_serialno(&og),m+1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   222
    ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   223
    if(ret==OV_EREAD)return(OV_EREAD);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   224
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   225
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   226
  vf->offsets[m]=begin;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   227
  vf->serialnos[m]=currentno;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   228
  return(0);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   229
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   230
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   231
/* uses the local ogg_stream storage in vf; this is important for
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   232
   non-streaming input sources */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   233
/* consumes the page that's passed in (if any) */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   234
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   235
static int _fetch_headers(OggVorbis_File *vf,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   236
			  vorbis_info *vi,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   237
			  vorbis_comment *vc,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   238
			  ogg_uint32_t *serialno,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   239
			  ogg_page *og_ptr){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   240
  ogg_page og={0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   241
  ogg_packet op={0,0,0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   242
  int i,ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   243
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   244
  if(!og_ptr){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   245
    ogg_int64_t llret=_get_next_page(vf,&og,CHUNKSIZE);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   246
    if(llret==OV_EREAD)return(OV_EREAD);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   247
    if(llret<0)return OV_ENOTVORBIS;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   248
    og_ptr=&og;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   249
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   250
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   251
  ogg_stream_reset_serialno(vf->os,ogg_page_serialno(og_ptr));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   252
  if(serialno)*serialno=vf->os->serialno;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   253
  vf->ready_state=STREAMSET;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   254
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   255
  /* extract the initial header from the first page and verify that the
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   256
     Ogg bitstream is in fact Vorbis data */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   257
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   258
  vorbis_info_init(vi);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   259
  vorbis_comment_init(vc);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   260
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   261
  i=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   262
  while(i<3){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   263
    ogg_stream_pagein(vf->os,og_ptr);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   264
    while(i<3){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   265
      int result=ogg_stream_packetout(vf->os,&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   266
      if(result==0)break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   267
      if(result==-1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   268
	ret=OV_EBADHEADER;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   269
	goto bail_header;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   270
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   271
      if((ret=vorbis_synthesis_headerin(vi,vc,&op))){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   272
	goto bail_header;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   273
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   274
      i++;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   275
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   276
    if(i<3)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   277
      if(_get_next_page(vf,og_ptr,CHUNKSIZE)<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   278
	ret=OV_EBADHEADER;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   279
	goto bail_header;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   280
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   281
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   282
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   283
  ogg_packet_release(&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   284
  ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   285
  return 0; 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   286
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   287
 bail_header:
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   288
  ogg_packet_release(&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   289
  ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   290
  vorbis_info_clear(vi);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   291
  vorbis_comment_clear(vc);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   292
  vf->ready_state=OPENED;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   293
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   294
  return ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   295
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   296
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   297
/* last step of the OggVorbis_File initialization; get all the
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   298
   vorbis_info structs and PCM positions.  Only called by the seekable
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   299
   initialization (local stream storage is hacked slightly; pay
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   300
   attention to how that's done) */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   301
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   302
/* this is void and does not propogate errors up because we want to be
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   303
   able to open and use damaged bitstreams as well as we can.  Just
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   304
   watch out for missing information for links in the OggVorbis_File
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   305
   struct */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   306
static void _prefetch_all_headers(OggVorbis_File *vf, ogg_int64_t dataoffset){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   307
  ogg_page og={0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   308
  int i;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   309
  ogg_int64_t ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   310
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   311
  vf->vi=_ogg_realloc(vf->vi,vf->links*sizeof(*vf->vi));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   312
  vf->vc=_ogg_realloc(vf->vc,vf->links*sizeof(*vf->vc));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   313
  vf->dataoffsets=_ogg_malloc(vf->links*sizeof(*vf->dataoffsets));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   314
  vf->pcmlengths=_ogg_malloc(vf->links*2*sizeof(*vf->pcmlengths));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   315
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   316
  for(i=0;i<vf->links;i++){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   317
    if(i==0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   318
      /* we already grabbed the initial header earlier.  Just set the offset */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   319
      vf->dataoffsets[i]=dataoffset;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   320
      _seek_helper(vf,dataoffset);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   321
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   322
    }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   323
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   324
      /* seek to the location of the initial header */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   325
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   326
      _seek_helper(vf,vf->offsets[i]);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   327
      if(_fetch_headers(vf,vf->vi+i,vf->vc+i,NULL,NULL)<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   328
    	vf->dataoffsets[i]=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   329
      }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   330
	vf->dataoffsets[i]=vf->offset;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   331
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   332
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   333
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   334
    /* fetch beginning PCM offset */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   335
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   336
    if(vf->dataoffsets[i]!=-1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   337
      ogg_int64_t accumulated=0,pos;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   338
      long        lastblock=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   339
      int         result;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   340
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   341
      ogg_stream_reset_serialno(vf->os,vf->serialnos[i]);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   342
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   343
      while(1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   344
	ogg_packet op={0,0,0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   345
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   346
	ret=_get_next_page(vf,&og,-1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   347
	if(ret<0)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   348
	  /* this should not be possible unless the file is
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   349
             truncated/mangled */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   350
	  break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   351
       
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   352
	if(ogg_page_serialno(&og)!=vf->serialnos[i])
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   353
	  break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   354
	
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   355
	pos=ogg_page_granulepos(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   356
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   357
	/* count blocksizes of all frames in the page */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   358
	ogg_stream_pagein(vf->os,&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   359
	while((result=ogg_stream_packetout(vf->os,&op))){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   360
	  if(result>0){ /* ignore holes */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   361
	    long thisblock=vorbis_packet_blocksize(vf->vi+i,&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   362
	    if(lastblock!=-1)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   363
	      accumulated+=(lastblock+thisblock)>>2;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   364
	    lastblock=thisblock;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   365
	  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   366
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   367
	ogg_packet_release(&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   368
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   369
	if(pos!=-1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   370
	  /* pcm offset of last packet on the first audio page */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   371
	  accumulated= pos-accumulated;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   372
	  break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   373
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   374
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   375
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   376
      /* less than zero?  This is a stream with samples trimmed off
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   377
         the beginning, a normal occurrence; set the offset to zero */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   378
      if(accumulated<0)accumulated=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   379
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   380
      vf->pcmlengths[i*2]=accumulated;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   381
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   382
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   383
    /* get the PCM length of this link. To do this,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   384
       get the last page of the stream */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   385
    {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   386
      ogg_int64_t end=vf->offsets[i+1];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   387
      _seek_helper(vf,end);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   388
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   389
      while(1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   390
	ret=_get_prev_page(vf,&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   391
	if(ret<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   392
	  /* this should not be possible */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   393
	  vorbis_info_clear(vf->vi+i);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   394
	  vorbis_comment_clear(vf->vc+i);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   395
	  break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   396
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   397
	if(ogg_page_granulepos(&og)!=-1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   398
	  vf->pcmlengths[i*2+1]=ogg_page_granulepos(&og)-vf->pcmlengths[i*2];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   399
	  break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   400
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   401
	vf->offset=ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   402
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   403
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   404
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   405
  ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   406
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   407
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   408
static void _make_decode_ready(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   409
  if(vf->ready_state!=STREAMSET)return;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   410
  if(vf->seekable){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   411
    vorbis_synthesis_init(&vf->vd,vf->vi+vf->current_link);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   412
  }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   413
    vorbis_synthesis_init(&vf->vd,vf->vi);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   414
  }    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   415
  vorbis_block_init(&vf->vd,&vf->vb);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   416
  vf->ready_state=INITSET;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   417
  vf->bittrack=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   418
  vf->samptrack=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   419
  return;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   420
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   421
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   422
static int _open_seekable2(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   423
  ogg_uint32_t serialno=vf->current_serialno;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   424
  ogg_uint32_t tempserialno;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   425
  ogg_int64_t dataoffset=vf->offset, end;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   426
  ogg_page og={0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   427
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   428
  /* we're partially open and have a first link header state in
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   429
     storage in vf */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   430
  /* we can seek, so set out learning all about this file */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   431
  (vf->callbacks.seek_func)(vf->datasource,0,SEEK_END);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   432
  vf->offset=vf->end=(vf->callbacks.tell_func)(vf->datasource);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   433
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   434
  /* We get the offset for the last page of the physical bitstream.
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   435
     Most OggVorbis files will contain a single logical bitstream */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   436
  end=_get_prev_page(vf,&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   437
  if(end<0)return(end);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   438
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   439
  /* more than one logical bitstream? */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   440
  tempserialno=ogg_page_serialno(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   441
  ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   442
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   443
  if(tempserialno!=serialno){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   444
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   445
    /* Chained bitstream. Bisect-search each logical bitstream
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   446
       section.  Do so based on serial number only */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   447
    if(_bisect_forward_serialno(vf,0,0,end+1,serialno,0)<0)return(OV_EREAD);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   448
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   449
  }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   450
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   451
    /* Only one logical bitstream */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   452
    if(_bisect_forward_serialno(vf,0,end,end+1,serialno,0))return(OV_EREAD);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   453
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   454
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   455
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   456
  /* the initial header memory is referenced by vf after; don't free it */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   457
  _prefetch_all_headers(vf,dataoffset);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   458
  return(ov_raw_seek(vf,0));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   459
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   460
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   461
/* clear out the current logical bitstream decoder */ 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   462
static void _decode_clear(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   463
  vorbis_dsp_clear(&vf->vd);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   464
  vorbis_block_clear(&vf->vb);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   465
  vf->ready_state=OPENED;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   466
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   467
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   468
/* fetch and process a packet.  Handles the case where we're at a
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   469
   bitstream boundary and dumps the decoding machine.  If the decoding
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   470
   machine is unloaded, it loads it.  It also keeps pcm_offset up to
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   471
   date (seek and read both use this.  seek uses a special hack with
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   472
   readp). 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   473
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   474
   return: <0) error, OV_HOLE (lost packet) or OV_EOF
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   475
            0) need more data (only if readp==0)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   476
	    1) got a packet 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   477
*/
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   478
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   479
static int _fetch_and_process_packet(OggVorbis_File *vf,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   480
				     int readp,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   481
				     int spanp){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   482
  ogg_page og={0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   483
  ogg_packet op={0,0,0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   484
  int ret=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   485
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   486
  /* handle one packet.  Try to fetch it from current stream state */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   487
  /* extract packets from page */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   488
  while(1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   489
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   490
    /* process a packet if we can.  If the machine isn't loaded,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   491
       neither is a page */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   492
    if(vf->ready_state==INITSET){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   493
      while(1) {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   494
	int result=ogg_stream_packetout(vf->os,&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   495
	ogg_int64_t granulepos;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   496
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   497
	if(result<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   498
	  ret=OV_HOLE; /* hole in the data. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   499
	  goto cleanup;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   500
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   501
	if(result>0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   502
	  /* got a packet.  process it */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   503
	  granulepos=op.granulepos;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   504
	  if(!vorbis_synthesis(&vf->vb,&op,1)){ /* lazy check for lazy
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   505
						      header handling.  The
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   506
						      header packets aren't
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   507
						      audio, so if/when we
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   508
						      submit them,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   509
						      vorbis_synthesis will
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   510
						      reject them */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   511
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   512
	    /* suck in the synthesis data and track bitrate */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   513
	    {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   514
	      int oldsamples=vorbis_synthesis_pcmout(&vf->vd,NULL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   515
	      /* for proper use of libvorbis within libvorbisfile,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   516
                 oldsamples will always be zero. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   517
	      if(oldsamples){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   518
		ret=OV_EFAULT;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   519
		goto cleanup;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   520
	      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   521
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   522
	      vorbis_synthesis_blockin(&vf->vd,&vf->vb);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   523
	      vf->samptrack+=vorbis_synthesis_pcmout(&vf->vd,NULL)-oldsamples;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   524
	      vf->bittrack+=op.bytes*8;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   525
	    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   526
	  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   527
	    /* update the pcm offset. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   528
	    if(granulepos!=-1 && !op.e_o_s){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   529
	      int link=(vf->seekable?vf->current_link:0);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   530
	      int i,samples;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   531
	    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   532
	      /* this packet has a pcm_offset on it (the last packet
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   533
	         completed on a page carries the offset) After processing
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   534
	         (above), we know the pcm position of the *last* sample
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   535
	         ready to be returned. Find the offset of the *first*
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   536
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   537
	         As an aside, this trick is inaccurate if we begin
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   538
	         reading anew right at the last page; the end-of-stream
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   539
	         granulepos declares the last frame in the stream, and the
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   540
	         last packet of the last page may be a partial frame.
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   541
	         So, we need a previous granulepos from an in-sequence page
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   542
	         to have a reference point.  Thus the !op.e_o_s clause
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   543
	         above */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   544
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   545
	      if(vf->seekable && link>0)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   546
		granulepos-=vf->pcmlengths[link*2];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   547
	      if(granulepos<0)granulepos=0; /* actually, this
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   548
					       shouldn't be possible
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   549
					       here unless the stream
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   550
					       is very broken */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   551
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   552
	      samples=vorbis_synthesis_pcmout(&vf->vd,NULL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   553
	    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   554
	      granulepos-=samples;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   555
	      for(i=0;i<link;i++)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   556
	        granulepos+=vf->pcmlengths[i*2+1];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   557
	      vf->pcm_offset=granulepos;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   558
	    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   559
	    ret=1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   560
	    goto cleanup;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   561
	  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   562
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   563
	else 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   564
	  break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   565
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   566
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   567
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   568
    if(vf->ready_state>=OPENED){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   569
      int ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   570
      if(!readp){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   571
	ret=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   572
	goto cleanup;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   573
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   574
      if((ret=_get_next_page(vf,&og,-1))<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   575
	ret=OV_EOF; /* eof. leave unitialized */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   576
	goto cleanup;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   577
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   578
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   579
	/* bitrate tracking; add the header's bytes here, the body bytes
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   580
	   are done by packet above */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   581
      vf->bittrack+=og.header_len*8;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   582
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   583
      /* has our decoding just traversed a bitstream boundary? */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   584
      if(vf->ready_state==INITSET){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   585
	if(vf->current_serialno!=ogg_page_serialno(&og)){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   586
	  if(!spanp){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   587
	    ret=OV_EOF;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   588
	    goto cleanup;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   589
	  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   590
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   591
	  _decode_clear(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   592
	  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   593
	  if(!vf->seekable){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   594
	    vorbis_info_clear(vf->vi);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   595
	    vorbis_comment_clear(vf->vc);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   596
	  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   597
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   598
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   599
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   600
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   601
    /* Do we need to load a new machine before submitting the page? */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   602
    /* This is different in the seekable and non-seekable cases.  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   603
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   604
       In the seekable case, we already have all the header
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   605
       information loaded and cached; we just initialize the machine
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   606
       with it and continue on our merry way.
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   607
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   608
       In the non-seekable (streaming) case, we'll only be at a
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   609
       boundary if we just left the previous logical bitstream and
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   610
       we're now nominally at the header of the next bitstream
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   611
    */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   612
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   613
    if(vf->ready_state!=INITSET){ 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   614
      int link;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   615
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   616
      if(vf->ready_state<STREAMSET){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   617
	if(vf->seekable){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   618
	  vf->current_serialno=ogg_page_serialno(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   619
	  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   620
	  /* match the serialno to bitstream section.  We use this rather than
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   621
	     offset positions to avoid problems near logical bitstream
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   622
	     boundaries */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   623
	  for(link=0;link<vf->links;link++)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   624
	    if(vf->serialnos[link]==vf->current_serialno)break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   625
	  if(link==vf->links){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   626
	    ret=OV_EBADLINK; /* sign of a bogus stream.  error out,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   627
				leave machine uninitialized */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   628
	    goto cleanup;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   629
	  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   630
	  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   631
	  vf->current_link=link;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   632
	  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   633
	  ogg_stream_reset_serialno(vf->os,vf->current_serialno);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   634
	  vf->ready_state=STREAMSET;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   635
	  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   636
	}else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   637
	  /* we're streaming */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   638
	  /* fetch the three header packets, build the info struct */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   639
	  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   640
	  int ret=_fetch_headers(vf,vf->vi,vf->vc,&vf->current_serialno,&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   641
	  if(ret) goto cleanup;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   642
	  vf->current_link++;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   643
	  link=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   644
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   645
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   646
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   647
      _make_decode_ready(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   648
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   649
    ogg_stream_pagein(vf->os,&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   650
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   651
 cleanup:
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   652
  ogg_packet_release(&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   653
  ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   654
  return ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   655
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   656
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   657
/* if, eg, 64 bit stdio is configured by default, this will build with
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   658
   fseek64 */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   659
static int _fseek64_wrap(FILE *f,ogg_int64_t off,int whence){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   660
  if(f==NULL)return(-1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   661
  return fseek(f,off,whence);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   662
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   663
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   664
static int _ov_open1(void *f,OggVorbis_File *vf,char *initial,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   665
		     long ibytes, ov_callbacks callbacks){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   666
  int offsettest=(f?callbacks.seek_func(f,0,SEEK_CUR):-1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   667
  int ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   668
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   669
  memset(vf,0,sizeof(*vf));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   670
  vf->datasource=f;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   671
  vf->callbacks = callbacks;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   672
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   673
  /* init the framing state */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   674
  vf->oy=ogg_sync_create();
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   675
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   676
  /* perhaps some data was previously read into a buffer for testing
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   677
     against other stream types.  Allow initialization from this
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   678
     previously read data (as we may be reading from a non-seekable
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   679
     stream) */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   680
  if(initial){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   681
    unsigned char *buffer=ogg_sync_bufferin(vf->oy,ibytes);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   682
    memcpy(buffer,initial,ibytes);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   683
    ogg_sync_wrote(vf->oy,ibytes);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   684
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   685
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   686
  /* can we seek? Stevens suggests the seek test was portable */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   687
  if(offsettest!=-1)vf->seekable=1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   688
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   689
  /* No seeking yet; Set up a 'single' (current) logical bitstream
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   690
     entry for partial open */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   691
  vf->links=1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   692
  vf->vi=_ogg_calloc(vf->links,sizeof(*vf->vi));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   693
  vf->vc=_ogg_calloc(vf->links,sizeof(*vf->vc));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   694
  vf->os=ogg_stream_create(-1); /* fill in the serialno later */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   695
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   696
  /* Try to fetch the headers, maintaining all the storage */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   697
  if((ret=_fetch_headers(vf,vf->vi,vf->vc,&vf->current_serialno,NULL))<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   698
    vf->datasource=NULL;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   699
    ov_clear(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   700
  }else if(vf->ready_state < PARTOPEN)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   701
    vf->ready_state=PARTOPEN;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   702
  return(ret);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   703
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   704
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   705
static int _ov_open2(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   706
  if(vf->ready_state < OPENED)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   707
    vf->ready_state=OPENED;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   708
  if(vf->seekable){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   709
    int ret=_open_seekable2(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   710
    if(ret){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   711
      vf->datasource=NULL;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   712
      ov_clear(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   713
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   714
    return(ret);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   715
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   716
  return 0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   717
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   718
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   719
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   720
/* clear out the OggVorbis_File struct */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   721
int ov_clear(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   722
  if(vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   723
    vorbis_block_clear(&vf->vb);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   724
    vorbis_dsp_clear(&vf->vd);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   725
    ogg_stream_destroy(vf->os);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   726
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   727
    if(vf->vi && vf->links){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   728
      int i;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   729
      for(i=0;i<vf->links;i++){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   730
	vorbis_info_clear(vf->vi+i);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   731
	vorbis_comment_clear(vf->vc+i);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   732
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   733
      _ogg_free(vf->vi);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   734
      _ogg_free(vf->vc);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   735
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   736
    if(vf->dataoffsets)_ogg_free(vf->dataoffsets);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   737
    if(vf->pcmlengths)_ogg_free(vf->pcmlengths);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   738
    if(vf->serialnos)_ogg_free(vf->serialnos);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   739
    if(vf->offsets)_ogg_free(vf->offsets);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   740
    ogg_sync_destroy(vf->oy);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   741
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   742
    if(vf->datasource)(vf->callbacks.close_func)(vf->datasource);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   743
    memset(vf,0,sizeof(*vf));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   744
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   745
#ifdef DEBUG_LEAKS
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   746
  _VDBG_dump();
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   747
#endif
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   748
  return(0);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   749
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   750
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   751
/* inspects the OggVorbis file and finds/documents all the logical
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   752
   bitstreams contained in it.  Tries to be tolerant of logical
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   753
   bitstream sections that are truncated/woogie. 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   754
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   755
   return: -1) error
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   756
            0) OK
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   757
*/
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   758
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   759
int ov_open_callbacks(void *f,OggVorbis_File *vf,char *initial,long ibytes,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   760
    ov_callbacks callbacks){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   761
  int ret=_ov_open1(f,vf,initial,ibytes,callbacks);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   762
  if(ret)return ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   763
  return _ov_open2(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   764
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   765
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   766
int ov_open(FILE *f,OggVorbis_File *vf,char *initial,long ibytes){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   767
  ov_callbacks callbacks = {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   768
    (size_t (*)(void *, size_t, size_t, void *))  fread,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   769
    (int (*)(void *, ogg_int64_t, int))              _fseek64_wrap,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   770
    (int (*)(void *))                             fclose,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   771
    (long (*)(void *))                            ftell
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   772
  };
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   773
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   774
  return ov_open_callbacks((void *)f, vf, initial, ibytes, callbacks);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   775
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   776
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   777
/* Only partially open the vorbis file; test for Vorbisness, and load
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   778
   the headers for the first chain.  Do not seek (although test for
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   779
   seekability).  Use ov_test_open to finish opening the file, else
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   780
   ov_clear to close/free it. Same return codes as open. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   781
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   782
int ov_test_callbacks(void *f,OggVorbis_File *vf,char *initial,long ibytes,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   783
    ov_callbacks callbacks)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   784
{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   785
  return _ov_open1(f,vf,initial,ibytes,callbacks);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   786
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   787
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   788
int ov_test(FILE *f,OggVorbis_File *vf,char *initial,long ibytes){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   789
  ov_callbacks callbacks = {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   790
    (size_t (*)(void *, size_t, size_t, void *))  fread,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   791
    (int (*)(void *, ogg_int64_t, int))              _fseek64_wrap,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   792
    (int (*)(void *))                             fclose,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   793
    (long (*)(void *))                            ftell
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   794
  };
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   795
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   796
  return ov_test_callbacks((void *)f, vf, initial, ibytes, callbacks);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   797
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   798
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   799
int ov_test_open(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   800
  if(vf->ready_state!=PARTOPEN)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   801
  return _ov_open2(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   802
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   803
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   804
/* How many logical bitstreams in this physical bitstream? */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   805
long ov_streams(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   806
  return vf->links;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   807
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   808
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   809
/* Is the FILE * associated with vf seekable? */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   810
long ov_seekable(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   811
  return vf->seekable;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   812
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   813
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   814
/* returns the bitrate for a given logical bitstream or the entire
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   815
   physical bitstream.  If the file is open for random access, it will
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   816
   find the *actual* average bitrate.  If the file is streaming, it
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   817
   returns the nominal bitrate (if set) else the average of the
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   818
   upper/lower bounds (if set) else -1 (unset).
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   819
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   820
   If you want the actual bitrate field settings, get them from the
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   821
   vorbis_info structs */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   822
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   823
long ov_bitrate(OggVorbis_File *vf,int i){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   824
  if(vf->ready_state<OPENED)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   825
  if(i>=vf->links)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   826
  if(!vf->seekable && i!=0)return(ov_bitrate(vf,0));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   827
  if(i<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   828
    ogg_int64_t bits=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   829
    int i;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   830
    for(i=0;i<vf->links;i++)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   831
      bits+=(vf->offsets[i+1]-vf->dataoffsets[i])*8;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   832
    /* This once read: return(rint(bits/ov_time_total(vf,-1)));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   833
     * gcc 3.x on x86 miscompiled this at optimisation level 2 and above,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   834
     * so this is slightly transformed to make it work.
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   835
     */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   836
    return(bits*1000/ov_time_total(vf,-1));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   837
  }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   838
    if(vf->seekable){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   839
      /* return the actual bitrate */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   840
      return((vf->offsets[i+1]-vf->dataoffsets[i])*8000/ov_time_total(vf,i));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   841
    }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   842
      /* return nominal if set */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   843
      if(vf->vi[i].bitrate_nominal>0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   844
	return vf->vi[i].bitrate_nominal;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   845
      }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   846
	if(vf->vi[i].bitrate_upper>0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   847
	  if(vf->vi[i].bitrate_lower>0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   848
	    return (vf->vi[i].bitrate_upper+vf->vi[i].bitrate_lower)/2;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   849
	  }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   850
	    return vf->vi[i].bitrate_upper;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   851
	  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   852
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   853
	return(OV_FALSE);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   854
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   855
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   856
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   857
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   858
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   859
/* returns the actual bitrate since last call.  returns -1 if no
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   860
   additional data to offer since last call (or at beginning of stream),
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   861
   EINVAL if stream is only partially open 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   862
*/
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   863
long ov_bitrate_instant(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   864
  int link=(vf->seekable?vf->current_link:0);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   865
  long ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   866
  if(vf->ready_state<OPENED)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   867
  if(vf->samptrack==0)return(OV_FALSE);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   868
  ret=vf->bittrack/vf->samptrack*vf->vi[link].rate;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   869
  vf->bittrack=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   870
  vf->samptrack=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   871
  return(ret);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   872
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   873
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   874
/* Guess */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   875
long ov_serialnumber(OggVorbis_File *vf,int i){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   876
  if(i>=vf->links)return(ov_serialnumber(vf,vf->links-1));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   877
  if(!vf->seekable && i>=0)return(ov_serialnumber(vf,-1));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   878
  if(i<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   879
    return(vf->current_serialno);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   880
  }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   881
    return(vf->serialnos[i]);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   882
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   883
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   884
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   885
/* returns: total raw (compressed) length of content if i==-1
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   886
            raw (compressed) length of that logical bitstream for i==0 to n
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   887
	    OV_EINVAL if the stream is not seekable (we can't know the length)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   888
	    or if stream is only partially open
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   889
*/
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   890
ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   891
  if(vf->ready_state<OPENED)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   892
  if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   893
  if(i<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   894
    ogg_int64_t acc=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   895
    int i;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   896
    for(i=0;i<vf->links;i++)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   897
      acc+=ov_raw_total(vf,i);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   898
    return(acc);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   899
  }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   900
    return(vf->offsets[i+1]-vf->offsets[i]);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   901
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   902
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   903
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   904
/* returns: total PCM length (samples) of content if i==-1 PCM length
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   905
	    (samples) of that logical bitstream for i==0 to n
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   906
	    OV_EINVAL if the stream is not seekable (we can't know the
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   907
	    length) or only partially open 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   908
*/
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   909
ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   910
  if(vf->ready_state<OPENED)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   911
  if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   912
  if(i<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   913
    ogg_int64_t acc=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   914
    int i;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   915
    for(i=0;i<vf->links;i++)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   916
      acc+=ov_pcm_total(vf,i);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   917
    return(acc);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   918
  }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   919
    return(vf->pcmlengths[i*2+1]);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   920
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   921
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   922
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   923
/* returns: total milliseconds of content if i==-1
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   924
            milliseconds in that logical bitstream for i==0 to n
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   925
	    OV_EINVAL if the stream is not seekable (we can't know the
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   926
	    length) or only partially open 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   927
*/
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   928
ogg_int64_t ov_time_total(OggVorbis_File *vf,int i){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   929
  if(vf->ready_state<OPENED)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   930
  if(!vf->seekable || i>=vf->links)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   931
  if(i<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   932
    ogg_int64_t acc=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   933
    int i;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   934
    for(i=0;i<vf->links;i++)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   935
      acc+=ov_time_total(vf,i);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   936
    return(acc);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   937
  }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   938
    return(((ogg_int64_t)vf->pcmlengths[i*2+1])*1000/vf->vi[i].rate);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   939
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   940
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   941
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   942
/* seek to an offset relative to the *compressed* data. This also
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   943
   scans packets to update the PCM cursor. It will cross a logical
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   944
   bitstream boundary, but only if it can't get any packets out of the
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   945
   tail of the bitstream we seek to (so no surprises).
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   946
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   947
   returns zero on success, nonzero on failure */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   948
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   949
int ov_raw_seek(OggVorbis_File *vf,ogg_int64_t pos){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   950
  ogg_stream_state *work_os=NULL;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   951
  ogg_page og={0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   952
  ogg_packet op={0,0,0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   953
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   954
  if(vf->ready_state<OPENED)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   955
  if(!vf->seekable)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   956
    return(OV_ENOSEEK); /* don't dump machine if we can't seek */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   957
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   958
  if(pos<0 || pos>vf->end)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   959
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   960
  /* don't yet clear out decoding machine (if it's initialized), in
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   961
     the case we're in the same link.  Restart the decode lapping, and
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   962
     let _fetch_and_process_packet deal with a potential bitstream
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   963
     boundary */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   964
  vf->pcm_offset=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   965
  ogg_stream_reset_serialno(vf->os,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   966
			    vf->current_serialno); /* must set serialno */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   967
  vorbis_synthesis_restart(&vf->vd);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   968
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   969
  _seek_helper(vf,pos);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   970
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   971
  /* we need to make sure the pcm_offset is set, but we don't want to
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   972
     advance the raw cursor past good packets just to get to the first
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   973
     with a granulepos.  That's not equivalent behavior to beginning
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   974
     decoding as immediately after the seek position as possible.
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   975
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   976
     So, a hack.  We use two stream states; a local scratch state and
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   977
     the shared vf->os stream state.  We use the local state to
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   978
     scan, and the shared state as a buffer for later decode. 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   979
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   980
     Unfortuantely, on the last page we still advance to last packet
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   981
     because the granulepos on the last page is not necessarily on a
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   982
     packet boundary, and we need to make sure the granpos is
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   983
     correct. 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   984
  */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   985
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   986
  {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   987
    int lastblock=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   988
    int accblock=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   989
    int thisblock;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   990
    int eosflag=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   991
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   992
    work_os=ogg_stream_create(vf->current_serialno); /* get the memory ready */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   993
    while(1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   994
      if(vf->ready_state>=STREAMSET){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   995
	/* snarf/scan a packet if we can */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   996
	int result=ogg_stream_packetout(work_os,&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   997
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   998
	if(result>0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   999
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1000
	  if(vf->vi[vf->current_link].codec_setup){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1001
	    thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1002
	    if(thisblock<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1003
	      ogg_stream_packetout(vf->os,NULL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1004
	      thisblock=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1005
	    }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1006
	      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1007
	      if(eosflag)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1008
		ogg_stream_packetout(vf->os,NULL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1009
	      else
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1010
		if(lastblock)accblock+=(lastblock+thisblock)>>2;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1011
	    }	    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1012
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1013
	    if(op.granulepos!=-1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1014
	      int i,link=vf->current_link;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1015
	      ogg_int64_t granulepos=op.granulepos-vf->pcmlengths[link*2];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1016
	      if(granulepos<0)granulepos=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1017
	      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1018
	      for(i=0;i<link;i++)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1019
		granulepos+=vf->pcmlengths[i*2+1];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1020
	      vf->pcm_offset=granulepos-accblock;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1021
	      break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1022
	    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1023
	    lastblock=thisblock;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1024
	    continue;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1025
	  }else
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1026
	    ogg_stream_packetout(vf->os,NULL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1027
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1028
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1029
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1030
      if(!lastblock){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1031
	if(_get_next_page(vf,&og,-1)<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1032
	  vf->pcm_offset=ov_pcm_total(vf,-1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1033
	  break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1034
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1035
      }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1036
	/* huh?  Bogus stream with packets but no granulepos */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1037
	vf->pcm_offset=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1038
	break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1039
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1040
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1041
      /* has our decoding just traversed a bitstream boundary? */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1042
      if(vf->ready_state>=STREAMSET)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1043
	if(vf->current_serialno!=ogg_page_serialno(&og)){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1044
	  _decode_clear(vf); /* clear out stream state */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1045
	  ogg_stream_destroy(work_os);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1046
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1047
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1048
      if(vf->ready_state<STREAMSET){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1049
	int link;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1050
	
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1051
	vf->current_serialno=ogg_page_serialno(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1052
	for(link=0;link<vf->links;link++)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1053
	  if(vf->serialnos[link]==vf->current_serialno)break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1054
	if(link==vf->links)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1055
	  goto seek_error; /* sign of a bogus stream.  error out,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1056
			      leave machine uninitialized */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1057
 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1058
	vf->current_link=link;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1059
	
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1060
	ogg_stream_reset_serialno(vf->os,vf->current_serialno);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1061
	ogg_stream_reset_serialno(work_os,vf->current_serialno); 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1062
	vf->ready_state=STREAMSET;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1063
	
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1064
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1065
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1066
      {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1067
	ogg_page dup;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1068
	ogg_page_dup(&dup,&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1069
	eosflag=ogg_page_eos(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1070
	ogg_stream_pagein(vf->os,&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1071
	ogg_stream_pagein(work_os,&dup);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1072
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1073
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1074
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1075
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1076
  ogg_packet_release(&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1077
  ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1078
  ogg_stream_destroy(work_os);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1079
  vf->bittrack=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1080
  vf->samptrack=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1081
  return(0);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1082
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1083
 seek_error:
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1084
  ogg_packet_release(&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1085
  ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1086
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1087
  /* dump the machine so we're in a known state */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1088
  vf->pcm_offset=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1089
  ogg_stream_destroy(work_os);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1090
  _decode_clear(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1091
  return OV_EBADLINK;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1092
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1093
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1094
/* Page granularity seek (faster than sample granularity because we
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1095
   don't do the last bit of decode to find a specific sample).
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1096
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1097
   Seek to the last [granule marked] page preceeding the specified pos
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1098
   location, such that decoding past the returned point will quickly
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1099
   arrive at the requested position. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1100
int ov_pcm_seek_page(OggVorbis_File *vf,ogg_int64_t pos){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1101
  int link=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1102
  ogg_int64_t result=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1103
  ogg_int64_t total=ov_pcm_total(vf,-1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1104
  ogg_page og={0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1105
  ogg_packet op={0,0,0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1106
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1107
  if(vf->ready_state<OPENED)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1108
  if(!vf->seekable)return(OV_ENOSEEK);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1109
  if(pos<0 || pos>total)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1110
 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1111
  /* which bitstream section does this pcm offset occur in? */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1112
  for(link=vf->links-1;link>=0;link--){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1113
    total-=vf->pcmlengths[link*2+1];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1114
    if(pos>=total)break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1115
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1116
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1117
  /* search within the logical bitstream for the page with the highest
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1118
     pcm_pos preceeding (or equal to) pos.  There is a danger here;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1119
     missing pages or incorrect frame number information in the
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1120
     bitstream could make our task impossible.  Account for that (it
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1121
     would be an error condition) */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1122
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1123
  /* new search algorithm by HB (Nicholas Vinen) */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1124
  {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1125
    ogg_int64_t end=vf->offsets[link+1];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1126
    ogg_int64_t begin=vf->offsets[link];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1127
    ogg_int64_t begintime = vf->pcmlengths[link*2];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1128
    ogg_int64_t endtime = vf->pcmlengths[link*2+1]+begintime;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1129
    ogg_int64_t target=pos-total+begintime;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1130
    ogg_int64_t best=begin;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1131
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1132
    while(begin<end){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1133
      ogg_int64_t bisect;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1134
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1135
      if(end-begin<CHUNKSIZE){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1136
	bisect=begin;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1137
      }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1138
	/* take a (pretty decent) guess. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1139
	bisect=begin + 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1140
	  (target-begintime)*(end-begin)/(endtime-begintime) - CHUNKSIZE;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1141
	if(bisect<=begin)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1142
	  bisect=begin+1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1143
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1144
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1145
      _seek_helper(vf,bisect);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1146
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1147
      while(begin<end){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1148
	result=_get_next_page(vf,&og,end-vf->offset);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1149
	if(result==OV_EREAD) goto seek_error;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1150
	if(result<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1151
	  if(bisect<=begin+1)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1152
	    end=begin; /* found it */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1153
	  else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1154
	    if(bisect==0) goto seek_error;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1155
	    bisect-=CHUNKSIZE;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1156
	    if(bisect<=begin)bisect=begin+1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1157
	    _seek_helper(vf,bisect);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1158
	  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1159
	}else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1160
	  ogg_int64_t granulepos=ogg_page_granulepos(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1161
	  if(granulepos==-1)continue;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1162
	  if(granulepos<target){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1163
	    best=result;  /* raw offset of packet with granulepos */ 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1164
	    begin=vf->offset; /* raw offset of next page */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1165
	    begintime=granulepos;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1166
	    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1167
	    if(target-begintime>44100)break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1168
	    bisect=begin; /* *not* begin + 1 */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1169
	  }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1170
	    if(bisect<=begin+1)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1171
	      end=begin;  /* found it */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1172
	    else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1173
	      if(end==vf->offset){ /* we're pretty close - we'd be stuck in */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1174
		end=result;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1175
		bisect-=CHUNKSIZE; /* an endless loop otherwise. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1176
		if(bisect<=begin)bisect=begin+1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1177
		_seek_helper(vf,bisect);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1178
	      }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1179
		end=result;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1180
		endtime=granulepos;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1181
		break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1182
	      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1183
	    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1184
	  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1185
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1186
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1187
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1188
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1189
    /* found our page. seek to it, update pcm offset. Easier case than
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1190
       raw_seek, don't keep packets preceeding granulepos. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1191
    {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1192
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1193
      /* seek */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1194
      _seek_helper(vf,best);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1195
      vf->pcm_offset=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1196
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1197
      if(_get_next_page(vf,&og,-1)<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1198
	ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1199
	return(OV_EOF); /* shouldn't happen */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1200
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1201
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1202
      if(link!=vf->current_link){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1203
	/* Different link; dump entire decode machine */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1204
	_decode_clear(vf);  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1205
	
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1206
	vf->current_link=link;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1207
	vf->current_serialno=ogg_page_serialno(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1208
	vf->ready_state=STREAMSET;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1209
	
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1210
      }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1211
	vorbis_synthesis_restart(&vf->vd);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1212
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1213
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1214
      ogg_stream_reset_serialno(vf->os,vf->current_serialno);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1215
      ogg_stream_pagein(vf->os,&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1216
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1217
      /* pull out all but last packet; the one with granulepos */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1218
      while(1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1219
	result=ogg_stream_packetpeek(vf->os,&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1220
	if(result==0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1221
	  /* !!! the packet finishing this page originated on a
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1222
             preceeding page. Keep fetching previous pages until we
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1223
             get one with a granulepos or without the 'continued' flag
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1224
             set.  Then just use raw_seek for simplicity. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1225
	  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1226
	  _seek_helper(vf,best);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1227
	  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1228
	  while(1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1229
	    result=_get_prev_page(vf,&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1230
	    if(result<0) goto seek_error;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1231
	    if(ogg_page_granulepos(&og)>-1 ||
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1232
	       !ogg_page_continued(&og)){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1233
	      return ov_raw_seek(vf,result);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1234
	    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1235
	    vf->offset=result;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1236
	  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1237
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1238
	if(result<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1239
	  result = OV_EBADPACKET; 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1240
	  goto seek_error;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1241
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1242
	if(op.granulepos!=-1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1243
	  vf->pcm_offset=op.granulepos-vf->pcmlengths[vf->current_link*2];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1244
	  if(vf->pcm_offset<0)vf->pcm_offset=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1245
	  vf->pcm_offset+=total;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1246
	  break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1247
	}else
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1248
	  result=ogg_stream_packetout(vf->os,NULL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1249
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1250
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1251
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1252
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1253
  /* verify result */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1254
  if(vf->pcm_offset>pos || pos>ov_pcm_total(vf,-1)){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1255
    result=OV_EFAULT;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1256
    goto seek_error;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1257
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1258
  vf->bittrack=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1259
  vf->samptrack=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1260
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1261
  ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1262
  ogg_packet_release(&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1263
  return(0);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1264
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1265
 seek_error:
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1266
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1267
  ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1268
  ogg_packet_release(&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1269
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1270
  /* dump machine so we're in a known state */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1271
  vf->pcm_offset=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1272
  _decode_clear(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1273
  return (int)result;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1274
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1275
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1276
/* seek to a sample offset relative to the decompressed pcm stream 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1277
   returns zero on success, nonzero on failure */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1278
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1279
int ov_pcm_seek(OggVorbis_File *vf,ogg_int64_t pos){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1280
  ogg_packet op={0,0,0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1281
  ogg_page og={0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1282
  int thisblock,lastblock=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1283
  int ret=ov_pcm_seek_page(vf,pos);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1284
  if(ret<0)return(ret);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1285
  _make_decode_ready(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1286
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1287
  /* discard leading packets we don't need for the lapping of the
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1288
     position we want; don't decode them */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1289
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1290
  while(1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1291
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1292
    int ret=ogg_stream_packetpeek(vf->os,&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1293
    if(ret>0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1294
      thisblock=vorbis_packet_blocksize(vf->vi+vf->current_link,&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1295
      if(thisblock<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1296
	ogg_stream_packetout(vf->os,NULL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1297
	continue; /* non audio packet */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1298
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1299
      if(lastblock)vf->pcm_offset+=(lastblock+thisblock)>>2;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1300
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1301
      if(vf->pcm_offset+((thisblock+
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1302
			  vorbis_info_blocksize(vf->vi,1))>>2)>=pos)break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1303
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1304
      /* remove the packet from packet queue and track its granulepos */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1305
      ogg_stream_packetout(vf->os,NULL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1306
      vorbis_synthesis(&vf->vb,&op,0);  /* set up a vb with
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1307
					   only tracking, no
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1308
					   pcm_decode */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1309
      vorbis_synthesis_blockin(&vf->vd,&vf->vb); 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1310
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1311
      /* end of logical stream case is hard, especially with exact
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1312
	 length positioning. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1313
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1314
      if(op.granulepos>-1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1315
	int i;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1316
	/* always believe the stream markers */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1317
	vf->pcm_offset=op.granulepos-vf->pcmlengths[vf->current_link*2];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1318
	if(vf->pcm_offset<0)vf->pcm_offset=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1319
	for(i=0;i<vf->current_link;i++)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1320
	  vf->pcm_offset+=vf->pcmlengths[i*2+1];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1321
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1322
	
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1323
      lastblock=thisblock;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1324
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1325
    }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1326
      if(ret<0 && ret!=OV_HOLE)break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1327
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1328
      /* suck in a new page */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1329
      if(_get_next_page(vf,&og,-1)<0)break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1330
      if(vf->current_serialno!=ogg_page_serialno(&og))_decode_clear(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1331
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1332
      if(vf->ready_state<STREAMSET){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1333
	int link;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1334
	
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1335
	vf->current_serialno=ogg_page_serialno(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1336
	for(link=0;link<vf->links;link++)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1337
	  if(vf->serialnos[link]==vf->current_serialno)break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1338
	if(link==vf->links){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1339
	  ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1340
	  ogg_packet_release(&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1341
	  return(OV_EBADLINK);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1342
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1343
	vf->current_link=link;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1344
	
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1345
	ogg_stream_reset_serialno(vf->os,vf->current_serialno); 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1346
	vf->ready_state=STREAMSET;      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1347
	_make_decode_ready(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1348
	lastblock=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1349
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1350
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1351
      ogg_stream_pagein(vf->os,&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1352
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1353
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1354
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1355
  vf->bittrack=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1356
  vf->samptrack=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1357
  /* discard samples until we reach the desired position. Crossing a
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1358
     logical bitstream boundary with abandon is OK. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1359
  while(vf->pcm_offset<pos){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1360
    ogg_int64_t target=pos-vf->pcm_offset;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1361
    long samples=vorbis_synthesis_pcmout(&vf->vd,NULL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1362
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1363
    if(samples>target)samples=target;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1364
    vorbis_synthesis_read(&vf->vd,samples);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1365
    vf->pcm_offset+=samples;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1366
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1367
    if(samples<target)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1368
      if(_fetch_and_process_packet(vf,1,1)<=0)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1369
	vf->pcm_offset=ov_pcm_total(vf,-1); /* eof */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1370
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1371
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1372
  ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1373
  ogg_packet_release(&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1374
  return 0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1375
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1376
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1377
/* seek to a playback time relative to the decompressed pcm stream 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1378
   returns zero on success, nonzero on failure */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1379
int ov_time_seek(OggVorbis_File *vf,ogg_int64_t milliseconds){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1380
  /* translate time to PCM position and call ov_pcm_seek */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1381
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1382
  int link=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1383
  ogg_int64_t pcm_total=ov_pcm_total(vf,-1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1384
  ogg_int64_t time_total=ov_time_total(vf,-1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1385
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1386
  if(vf->ready_state<OPENED)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1387
  if(!vf->seekable)return(OV_ENOSEEK);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1388
  if(milliseconds<0 || milliseconds>time_total)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1389
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1390
  /* which bitstream section does this time offset occur in? */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1391
  for(link=vf->links-1;link>=0;link--){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1392
    pcm_total-=vf->pcmlengths[link*2+1];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1393
    time_total-=ov_time_total(vf,link);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1394
    if(milliseconds>=time_total)break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1395
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1396
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1397
  /* enough information to convert time offset to pcm offset */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1398
  {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1399
    ogg_int64_t target=pcm_total+(milliseconds-time_total)*vf->vi[link].rate/1000;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1400
    return(ov_pcm_seek(vf,target));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1401
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1402
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1403
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1404
/* page-granularity version of ov_time_seek 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1405
   returns zero on success, nonzero on failure */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1406
int ov_time_seek_page(OggVorbis_File *vf,ogg_int64_t milliseconds){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1407
  /* translate time to PCM position and call ov_pcm_seek */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1408
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1409
  int link=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1410
  ogg_int64_t pcm_total=ov_pcm_total(vf,-1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1411
  ogg_int64_t time_total=ov_time_total(vf,-1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1412
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1413
  if(vf->ready_state<OPENED)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1414
  if(!vf->seekable)return(OV_ENOSEEK);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1415
  if(milliseconds<0 || milliseconds>time_total)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1416
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1417
  /* which bitstream section does this time offset occur in? */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1418
  for(link=vf->links-1;link>=0;link--){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1419
    pcm_total-=vf->pcmlengths[link*2+1];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1420
    time_total-=ov_time_total(vf,link);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1421
    if(milliseconds>=time_total)break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1422
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1423
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1424
  /* enough information to convert time offset to pcm offset */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1425
  {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1426
    ogg_int64_t target=pcm_total+(milliseconds-time_total)*vf->vi[link].rate/1000;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1427
    return(ov_pcm_seek_page(vf,target));
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1428
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1429
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1430
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1431
/* tell the current stream offset cursor.  Note that seek followed by
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1432
   tell will likely not give the set offset due to caching */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1433
ogg_int64_t ov_raw_tell(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1434
  if(vf->ready_state<OPENED)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1435
  return(vf->offset);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1436
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1437
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1438
/* return PCM offset (sample) of next PCM sample to be read */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1439
ogg_int64_t ov_pcm_tell(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1440
  if(vf->ready_state<OPENED)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1441
  return(vf->pcm_offset);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1442
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1443
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1444
/* return time offset (milliseconds) of next PCM sample to be read */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1445
ogg_int64_t ov_time_tell(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1446
  int link=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1447
  ogg_int64_t pcm_total=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1448
  ogg_int64_t time_total=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1449
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1450
  if(vf->ready_state<OPENED)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1451
  if(vf->seekable){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1452
    pcm_total=ov_pcm_total(vf,-1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1453
    time_total=ov_time_total(vf,-1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1454
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1455
    /* which bitstream section does this time offset occur in? */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1456
    for(link=vf->links-1;link>=0;link--){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1457
      pcm_total-=vf->pcmlengths[link*2+1];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1458
      time_total-=ov_time_total(vf,link);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1459
      if(vf->pcm_offset>=pcm_total)break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1460
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1461
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1462
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1463
  return(time_total+(1000*vf->pcm_offset-pcm_total)/vf->vi[link].rate);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1464
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1465
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1466
/*  link:   -1) return the vorbis_info struct for the bitstream section
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1467
                currently being decoded
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1468
           0-n) to request information for a specific bitstream section
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1469
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1470
    In the case of a non-seekable bitstream, any call returns the
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1471
    current bitstream.  NULL in the case that the machine is not
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1472
    initialized */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1473
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1474
vorbis_info *ov_info(OggVorbis_File *vf,int link){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1475
  if(vf->seekable){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1476
    if(link<0)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1477
      if(vf->ready_state>=STREAMSET)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1478
	return vf->vi+vf->current_link;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1479
      else
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1480
      return vf->vi;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1481
    else
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1482
      if(link>=vf->links)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1483
	return NULL;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1484
      else
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1485
	return vf->vi+link;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1486
  }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1487
    return vf->vi;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1488
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1489
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1490
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1491
/* grr, strong typing, grr, no templates/inheritence, grr */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1492
vorbis_comment *ov_comment(OggVorbis_File *vf,int link){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1493
  if(vf->seekable){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1494
    if(link<0)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1495
      if(vf->ready_state>=STREAMSET)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1496
	return vf->vc+vf->current_link;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1497
      else
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1498
	return vf->vc;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1499
    else
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1500
      if(link>=vf->links)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1501
	return NULL;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1502
      else
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1503
	return vf->vc+link;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1504
  }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1505
    return vf->vc;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1506
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1507
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1508
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1509
/* up to this point, everything could more or less hide the multiple
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1510
   logical bitstream nature of chaining from the toplevel application
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1511
   if the toplevel application didn't particularly care.  However, at
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1512
   the point that we actually read audio back, the multiple-section
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1513
   nature must surface: Multiple bitstream sections do not necessarily
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1514
   have to have the same number of channels or sampling rate.
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1515
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1516
   ov_read returns the sequential logical bitstream number currently
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1517
   being decoded along with the PCM data in order that the toplevel
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1518
   application can take action on channel/sample rate changes.  This
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1519
   number will be incremented even for streamed (non-seekable) streams
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1520
   (for seekable streams, it represents the actual logical bitstream
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1521
   index within the physical bitstream.  Note that the accessor
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1522
   functions above are aware of this dichotomy).
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1523
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1524
   input values: buffer) a buffer to hold packed PCM data for return
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1525
		 length) the byte length requested to be placed into buffer
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1526
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1527
   return values: <0) error/hole in data (OV_HOLE), partial open (OV_EINVAL)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1528
                   0) EOF
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1529
		   n) number of bytes of PCM actually returned.  The
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1530
		   below works on a packet-by-packet basis, so the
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1531
		   return length is not related to the 'length' passed
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1532
		   in, just guaranteed to fit.
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1533
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1534
	    *section) set to the logical bitstream number */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1535
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1536
long ov_read(OggVorbis_File *vf,char *buffer,int bytes_req,int *bitstream){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1537
  int i,j;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1538
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1539
  ogg_int32_t **pcm;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1540
  long samples;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1541
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1542
  if(vf->ready_state<OPENED)return(OV_EINVAL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1543
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1544
  while(1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1545
    if(vf->ready_state==INITSET){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1546
      samples=vorbis_synthesis_pcmout(&vf->vd,&pcm);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1547
      if(samples)break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1548
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1549
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1550
    /* suck in another packet */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1551
    {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1552
      int ret=_fetch_and_process_packet(vf,1,1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1553
      if(ret==OV_EOF)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1554
	return(0);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1555
      if(ret<=0)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1556
	return(ret);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1557
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1558
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1559
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1560
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1561
  if(samples>0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1562
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1563
    /* yay! proceed to pack data into the byte buffer */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1564
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1565
    long channels=ov_info(vf,-1)->channels;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1566
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1567
    if(samples>(bytes_req/(2*channels)))
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1568
      samples=bytes_req/(2*channels);      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1569
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1570
    for(i=0;i<channels;i++) { /* It's faster in this order */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1571
      ogg_int32_t *src=pcm[i];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1572
      short *dest=((short *)buffer)+i;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1573
      for(j=0;j<samples;j++) {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1574
        *dest=CLIP_TO_15(src[j]>>9);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1575
        dest+=channels;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1576
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1577
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1578
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1579
    vorbis_synthesis_read(&vf->vd,samples);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1580
    vf->pcm_offset+=samples;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1581
    if(bitstream)*bitstream=vf->current_link;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1582
    return(samples*2*channels);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1583
  }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1584
    return(samples);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1585
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1586
}