misc/libtremor/tremor/vorbisfile.c
author koda
Fri, 21 Sep 2012 00:50:04 +0200
changeset 7697 767d3c4153a1
parent 6551 a2f39cb9af62
child 7849 a12155461b34
permissions -rw-r--r--
switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
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
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
    15
 last mod: $Id: vorbisfile.c,v 1.6.2.4 2003/04/29 04:03:27 xiphmont Exp $
5170
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
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
    25
#include "codec_internal.h"
5170
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
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
    28
#include "os.h"
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    29
#include "misc.h"
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    30
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
    31
#define  NOTOPEN   0
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
    32
#define  PARTOPEN  1
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
    33
#define  OPENED    2
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
    34
#define  STREAMSET 3 /* serialno and link set, but not to current link */
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
    35
#define  LINKSET   4 /* serialno and link set to current link */
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
    36
#define  INITSET   5
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
    37
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    38
/* 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
    39
   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
    40
   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
    41
   multiplexing] is not allowed in Vorbis) */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    42
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    43
/* 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
    44
   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
    45
   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
    46
   (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
    47
   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
    48
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    49
/* 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
    50
   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
    51
   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
    52
   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
    53
   between links in the chain. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    54
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    55
/* 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
    56
   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
    57
   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
    58
   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
    59
   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
    60
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    61
/*************************************************************************
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    62
 * 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
    63
 * 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
    64
 * 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
    65
 * grokking near the end of the file */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    66
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    67
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    68
/* 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
    69
static long _get_data(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    70
  errno=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    71
  if(vf->datasource){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    72
    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
    73
    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
    74
    if(bytes>0)ogg_sync_wrote(vf->oy,bytes);
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
    75
    if(bytes==0 && errno)return -1;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
    76
    return bytes;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    77
  }else
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
    78
    return 0;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    79
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    80
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    81
/* 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
    82
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
    83
  if(vf->datasource){ 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    84
    (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
    85
    vf->offset=offset;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    86
    ogg_sync_reset(vf->oy);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    87
  }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    88
    /* 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
    89
    return;
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
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    92
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    93
/* 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
    94
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    95
/* 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
    96
   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
    97
   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
    98
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    99
   boundary: -1) unbounded search
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   100
              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
   101
	      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
   102
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   103
   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
   104
              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
   105
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   106
              produces a refcounted page */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   107
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   108
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
   109
				  ogg_int64_t boundary){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   110
  if(boundary>0)boundary+=vf->offset;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   111
  while(1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   112
    long more;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   113
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   114
    if(boundary>0 && vf->offset>=boundary)return OV_FALSE;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   115
    more=ogg_sync_pageseek(vf->oy,og);
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
    if(more<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   118
      /* skipped n bytes */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   119
      vf->offset-=more;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   120
    }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   121
      if(more==0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   122
	/* send more paramedics */
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   123
	if(!boundary)return OV_FALSE;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   124
	{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   125
	  long ret=_get_data(vf);
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   126
	  if(ret==0)return OV_EOF;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   127
	  if(ret<0)return OV_EREAD;
5170
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
      }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   130
	/* 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
   131
           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
   132
	ogg_int64_t ret=vf->offset;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   133
	vf->offset+=more;
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   134
	return ret;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   135
	
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   136
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   137
    }
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
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   140
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   141
/* 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
   142
   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
   143
   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
   144
   read. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   145
/* 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
   146
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   147
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
   148
  ogg_int64_t begin=vf->offset;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   149
  ogg_int64_t end=begin;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   150
  ogg_int64_t ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   151
  ogg_int64_t offset=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   152
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   153
  while(offset==-1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   154
    begin-=CHUNKSIZE;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   155
    if(begin<0)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   156
      begin=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   157
    _seek_helper(vf,begin);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   158
    while(vf->offset<end){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   159
      ret=_get_next_page(vf,og,end-vf->offset);
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   160
      if(ret==OV_EREAD)return OV_EREAD;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   161
      if(ret<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   162
	break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   163
      }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   164
	offset=ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   165
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   166
    }
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
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   169
  /* 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
   170
  _seek_helper(vf,offset);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   171
  ret=_get_next_page(vf,og,CHUNKSIZE);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   172
  if(ret<0)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   173
    /* this shouldn't be possible */
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   174
    return OV_EFAULT;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   175
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   176
  return offset;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   177
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   178
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   179
/* 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
   180
   (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
   181
   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
   182
   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
   183
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
   184
				    ogg_int64_t begin,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   185
				    ogg_int64_t searched,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   186
				    ogg_int64_t end,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   187
				    ogg_uint32_t currentno,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   188
				    long m){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   189
  ogg_int64_t endsearched=end;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   190
  ogg_int64_t next=end;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   191
  ogg_page og={0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   192
  ogg_int64_t ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   193
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   194
  /* 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
   195
     first pages of two links. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   196
  while(searched<endsearched){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   197
    ogg_int64_t bisect;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   198
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   199
    if(endsearched-searched<CHUNKSIZE){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   200
      bisect=searched;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   201
    }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   202
      bisect=(searched+endsearched)/2;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   203
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   204
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   205
    _seek_helper(vf,bisect);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   206
    ret=_get_next_page(vf,&og,-1);
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   207
    if(ret==OV_EREAD)return OV_EREAD;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   208
    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
   209
      endsearched=bisect;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   210
      if(ret>=0)next=ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   211
    }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   212
      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
   213
    }
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
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   216
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   217
  _seek_helper(vf,next);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   218
  ret=_get_next_page(vf,&og,-1);
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   219
  if(ret==OV_EREAD)return OV_EREAD;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   220
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   221
  if(searched>=end || ret<0){
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
    vf->links=m+1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   224
    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
   225
    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
   226
    vf->offsets[m+1]=searched;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   227
  }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   228
    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
   229
				 end,ogg_page_serialno(&og),m+1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   230
    ogg_page_release(&og);
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   231
    if(ret==OV_EREAD)return OV_EREAD;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   232
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   233
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   234
  vf->offsets[m]=begin;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   235
  vf->serialnos[m]=currentno;
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   236
  return 0;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   237
}
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   238
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   239
static int _decode_clear(OggVorbis_File *vf){
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   240
  if(vf->ready_state==INITSET){
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   241
    vorbis_dsp_destroy(vf->vd);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   242
    vf->vd=0;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   243
    vf->ready_state=STREAMSET;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   244
  }
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   245
  
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   246
  if(vf->ready_state>=STREAMSET){
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   247
    vorbis_info_clear(&vf->vi);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   248
    vorbis_comment_clear(&vf->vc);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   249
    vf->ready_state=OPENED;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   250
  }
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   251
  return 0;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   252
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   253
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   254
/* 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
   255
   non-streaming input sources */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   256
/* consumes the page that's passed in (if any) */
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   257
/* state is LINKSET upon successful return */
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   258
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   259
static int _fetch_headers(OggVorbis_File *vf,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   260
			  vorbis_info *vi,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   261
			  vorbis_comment *vc,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   262
			  ogg_uint32_t *serialno,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   263
			  ogg_page *og_ptr){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   264
  ogg_page og={0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   265
  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
   266
  int i,ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   267
  
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   268
  if(vf->ready_state>OPENED)_decode_clear(vf);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   269
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   270
  if(!og_ptr){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   271
    ogg_int64_t llret=_get_next_page(vf,&og,CHUNKSIZE);
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   272
    if(llret==OV_EREAD)return OV_EREAD;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   273
    if(llret<0)return OV_ENOTVORBIS;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   274
    og_ptr=&og;
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
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   277
  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
   278
  if(serialno)*serialno=vf->os->serialno;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   279
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   280
  /* 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
   281
     Ogg bitstream is in fact Vorbis data */
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
  vorbis_info_init(vi);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   284
  vorbis_comment_init(vc);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   285
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   286
  i=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   287
  while(i<3){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   288
    ogg_stream_pagein(vf->os,og_ptr);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   289
    while(i<3){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   290
      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
   291
      if(result==0)break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   292
      if(result==-1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   293
	ret=OV_EBADHEADER;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   294
	goto bail_header;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   295
      }
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   296
      if((ret=vorbis_dsp_headerin(vi,vc,&op))){
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   297
	goto bail_header;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   298
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   299
      i++;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   300
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   301
    if(i<3)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   302
      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
   303
	ret=OV_EBADHEADER;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   304
	goto bail_header;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   305
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   306
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   307
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   308
  ogg_packet_release(&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   309
  ogg_page_release(&og);
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   310
  vf->ready_state=LINKSET;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   311
  return 0; 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   312
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   313
 bail_header:
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   314
  ogg_packet_release(&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   315
  ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   316
  vorbis_info_clear(vi);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   317
  vorbis_comment_clear(vc);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   318
  vf->ready_state=OPENED;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   319
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   320
  return ret;
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
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   323
/* we no longer preload all vorbis_info (and the associated
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   324
   codec_setup) structs.  Call this to seek and fetch the info from
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   325
   the bitstream, if needed */
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   326
static int _set_link_number(OggVorbis_File *vf,int link){
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   327
  if(link != vf->current_link) _decode_clear(vf);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   328
  if(vf->ready_state<STREAMSET){
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   329
    _seek_helper(vf,vf->offsets[link]);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   330
    ogg_stream_reset_serialno(vf->os,vf->serialnos[link]);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   331
    vf->current_serialno=vf->serialnos[link];
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   332
    vf->current_link=link;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   333
    return _fetch_headers(vf,&vf->vi,&vf->vc,&vf->current_serialno,NULL);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   334
  }
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   335
  return 0;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   336
}
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   337
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   338
static int _set_link_number_preserve_pos(OggVorbis_File *vf,int link){
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   339
  ogg_int64_t pos=vf->offset;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   340
  int ret=_set_link_number(vf,link);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   341
  if(ret)return ret;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   342
  _seek_helper(vf,pos);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   343
  if(pos<vf->offsets[link] || pos>=vf->offsets[link+1])
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   344
    vf->ready_state=STREAMSET;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   345
  return 0;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   346
}
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   347
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   348
/* last step of the OggVorbis_File initialization; get all the offset
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   349
   positions.  Only called by the seekable initialization (local
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   350
   stream storage is hacked slightly; pay attention to how that's
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   351
   done) */
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   352
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   353
/* 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
   354
   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
   355
   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
   356
   struct */
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   357
static void _prefetch_all_offsets(OggVorbis_File *vf, ogg_int64_t dataoffset){
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   358
  ogg_page og={0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   359
  int i;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   360
  ogg_int64_t ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   361
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   362
  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
   363
  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
   364
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   365
  for(i=0;i<vf->links;i++){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   366
    if(i==0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   367
      /* 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
   368
      vf->dataoffsets[i]=dataoffset;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   369
      _seek_helper(vf,dataoffset);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   370
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   371
    }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   372
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   373
      /* 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
   374
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   375
      _seek_helper(vf,vf->offsets[i]);
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   376
      if(_fetch_headers(vf,&vf->vi,&vf->vc,NULL,NULL)<0){
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   377
    	vf->dataoffsets[i]=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   378
      }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   379
	vf->dataoffsets[i]=vf->offset;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   380
      }
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
    /* fetch beginning PCM offset */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   384
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   385
    if(vf->dataoffsets[i]!=-1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   386
      ogg_int64_t accumulated=0,pos;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   387
      long        lastblock=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   388
      int         result;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   389
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   390
      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
   391
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   392
      while(1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   393
	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
   394
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   395
	ret=_get_next_page(vf,&og,-1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   396
	if(ret<0)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   397
	  /* 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
   398
             truncated/mangled */
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
	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
   402
	  break;
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
	pos=ogg_page_granulepos(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   405
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   406
	/* 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
   407
	ogg_stream_pagein(vf->os,&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   408
	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
   409
	  if(result>0){ /* ignore holes */
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   410
	    long thisblock=vorbis_packet_blocksize(&vf->vi,&op);
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   411
	    if(lastblock!=-1)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   412
	      accumulated+=(lastblock+thisblock)>>2;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   413
	    lastblock=thisblock;
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
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   416
	ogg_packet_release(&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   417
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   418
	if(pos!=-1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   419
	  /* 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
   420
	  accumulated= pos-accumulated;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   421
	  break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   422
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   423
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   424
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   425
      /* 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
   426
         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
   427
      if(accumulated<0)accumulated=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   428
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   429
      vf->pcmlengths[i*2]=accumulated;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   430
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   431
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   432
    /* 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
   433
       get the last page of the stream */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   434
    {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   435
      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
   436
      _seek_helper(vf,end);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   437
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   438
      while(1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   439
	ret=_get_prev_page(vf,&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   440
	if(ret<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   441
	  /* this should not be possible */
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   442
	  vorbis_info_clear(&vf->vi);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   443
	  vorbis_comment_clear(&vf->vc);
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   444
	  break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   445
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   446
	if(ogg_page_granulepos(&og)!=-1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   447
	  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
   448
	  break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   449
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   450
	vf->offset=ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   451
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   452
    }
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
  ogg_page_release(&og);
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
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   457
static int _make_decode_ready(OggVorbis_File *vf){
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   458
  int i;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   459
  switch(vf->ready_state){
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   460
  case OPENED:
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   461
  case STREAMSET:
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   462
    for(i=0;i<vf->links;i++)
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   463
      if(vf->offsets[i+1]>=vf->offset)break;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   464
    if(i==vf->links)return -1;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   465
    i=_set_link_number_preserve_pos(vf,i);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   466
    if(i)return i;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   467
    /* fall through */
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   468
  case LINKSET:
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   469
    vf->vd=vorbis_dsp_create(&vf->vi);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   470
    vf->ready_state=INITSET;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   471
    vf->bittrack=0;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   472
    vf->samptrack=0;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   473
  case INITSET:
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   474
    return 0;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   475
  default:
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   476
    return -1;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   477
  }
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   478
  
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   479
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   480
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   481
static int _open_seekable2(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   482
  ogg_uint32_t serialno=vf->current_serialno;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   483
  ogg_uint32_t tempserialno;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   484
  ogg_int64_t dataoffset=vf->offset, end;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   485
  ogg_page og={0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   486
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   487
  /* 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
   488
     storage in vf */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   489
  /* 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
   490
  (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
   491
  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
   492
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   493
  /* 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
   494
     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
   495
  end=_get_prev_page(vf,&og);
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   496
  if(end<0)return end;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   497
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   498
  /* more than one logical bitstream? */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   499
  tempserialno=ogg_page_serialno(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   500
  ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   501
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   502
  if(tempserialno!=serialno){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   503
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   504
    /* Chained bitstream. Bisect-search each logical bitstream
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   505
       section.  Do so based on serial number only */
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   506
    if(_bisect_forward_serialno(vf,0,0,end+1,serialno,0)<0)return OV_EREAD;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   507
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   508
  }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   509
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   510
    /* Only one logical bitstream */
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   511
    if(_bisect_forward_serialno(vf,0,end,end+1,serialno,0))return OV_EREAD;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   512
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
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   515
  /* the initial header memory is referenced by vf after; don't free it */
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   516
  _prefetch_all_offsets(vf,dataoffset);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   517
  return ov_raw_seek(vf,0);
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   518
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   519
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   520
/* 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
   521
   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
   522
   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
   523
   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
   524
   readp). 
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
   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
   527
            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
   528
	    1) got a packet 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   529
*/
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   530
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   531
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
   532
				     int readp,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   533
				     int spanp){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   534
  ogg_page og={0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   535
  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
   536
  int ret=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   537
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   538
  /* 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
   539
  /* extract packets from page */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   540
  while(1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   541
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   542
    /* 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
   543
       neither is a page */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   544
    if(vf->ready_state==INITSET){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   545
      while(1) {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   546
	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
   547
	ogg_int64_t granulepos;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   548
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   549
	if(result<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   550
	  ret=OV_HOLE; /* hole in the data. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   551
	  goto cleanup;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   552
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   553
	if(result>0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   554
	  /* got a packet.  process it */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   555
	  granulepos=op.granulepos;
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   556
	  if(!vorbis_dsp_synthesis(vf->vd,&op,1)){ /* lazy check for lazy
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   557
						      header handling.  The
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   558
						      header packets aren't
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   559
						      audio, so if/when we
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   560
						      submit them,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   561
						      vorbis_synthesis will
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   562
						      reject them */
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   563
	    
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   564
	    vf->samptrack+=vorbis_dsp_pcmout(vf->vd,NULL,0);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   565
	    vf->bittrack+=op.bytes*8;
5170
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
	    /* update the pcm offset. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   568
	    if(granulepos!=-1 && !op.e_o_s){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   569
	      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
   570
	      int i,samples;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   571
	    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   572
	      /* 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
   573
	         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
   574
	         (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
   575
	         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
   576
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   577
	         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
   578
	         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
   579
	         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
   580
	         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
   581
	         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
   582
	         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
   583
	         above */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   584
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   585
	      if(vf->seekable && link>0)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   586
		granulepos-=vf->pcmlengths[link*2];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   587
	      if(granulepos<0)granulepos=0; /* actually, this
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   588
					       shouldn't be possible
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   589
					       here unless the stream
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   590
					       is very broken */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   591
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   592
	      samples=vorbis_dsp_pcmout(vf->vd,NULL,0);
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   593
	    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   594
	      granulepos-=samples;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   595
	      for(i=0;i<link;i++)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   596
	        granulepos+=vf->pcmlengths[i*2+1];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   597
	      vf->pcm_offset=granulepos;
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
	    ret=1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   600
	    goto cleanup;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   601
	  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   602
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   603
	else 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   604
	  break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   605
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   606
    }
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
    if(vf->ready_state>=OPENED){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   609
      int ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   610
      if(!readp){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   611
	ret=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   612
	goto cleanup;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   613
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   614
      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
   615
	ret=OV_EOF; /* eof. leave unitialized */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   616
	goto cleanup;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   617
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   618
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   619
	/* 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
   620
	   are done by packet above */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   621
      vf->bittrack+=og.header_len*8;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   622
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   623
      /* 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
   624
      if(vf->ready_state==INITSET){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   625
	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
   626
	  if(!spanp){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   627
	    ret=OV_EOF;
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
	  _decode_clear(vf);
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
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   634
    }
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
    /* 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
   637
    /* 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
   638
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   639
       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
   640
       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
   641
       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
   642
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   643
       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
   644
       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
   645
       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
   646
    */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   647
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   648
    if(vf->ready_state!=INITSET){ 
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   649
      int link,ret;
5170
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
      if(vf->ready_state<STREAMSET){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   652
	if(vf->seekable){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   653
	  vf->current_serialno=ogg_page_serialno(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   654
	  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   655
	  /* 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
   656
	     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
   657
	     boundaries */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   658
	  for(link=0;link<vf->links;link++)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   659
	    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
   660
	  if(link==vf->links){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   661
	    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
   662
				leave machine uninitialized */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   663
	    goto cleanup;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   664
	  }
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   665
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   666
	  vf->current_link=link;
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   667
	  ret=_fetch_headers(vf,&vf->vi,&vf->vc,&vf->current_serialno,&og);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   668
	  if(ret) goto cleanup;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   669
	  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   670
	}else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   671
	  /* we're streaming */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   672
	  /* 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
   673
	  
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   674
	  int ret=_fetch_headers(vf,&vf->vi,&vf->vc,&vf->current_serialno,&og);
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   675
	  if(ret) goto cleanup;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   676
	  vf->current_link++;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   677
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   678
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   679
      
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   680
      if(_make_decode_ready(vf)) return OV_EBADLINK;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   681
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   682
    ogg_stream_pagein(vf->os,&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   683
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   684
 cleanup:
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   685
  ogg_packet_release(&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   686
  ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   687
  return ret;
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
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   690
/* 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
   691
   fseek64 */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   692
static int _fseek64_wrap(FILE *f,ogg_int64_t off,int whence){
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   693
  if(f==NULL)return -1;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   694
  return fseek(f,off,whence);
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
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   697
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
   698
		     long ibytes, ov_callbacks callbacks){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   699
  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
   700
  int ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   701
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   702
  memset(vf,0,sizeof(*vf));
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   703
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   704
  /* Tremor assumes in multiple places that right shift of a signed
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   705
     integer is an arithmetic shift */
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   706
  if( (-1>>1) != -1) return OV_EIMPL;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   707
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   708
  vf->datasource=f;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   709
  vf->callbacks = callbacks;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   710
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   711
  /* init the framing state */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   712
  vf->oy=ogg_sync_create();
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
  /* 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
   715
     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
   716
     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
   717
     stream) */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   718
  if(initial){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   719
    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
   720
    memcpy(buffer,initial,ibytes);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   721
    ogg_sync_wrote(vf->oy,ibytes);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   722
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   723
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   724
  /* 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
   725
  if(offsettest!=-1)vf->seekable=1;
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
  /* 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
   728
     entry for partial open */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   729
  vf->links=1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   730
  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
   731
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   732
  /* Try to fetch the headers, maintaining all the storage */
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   733
  if((ret=_fetch_headers(vf,&vf->vi,&vf->vc,&vf->current_serialno,NULL))<0){
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   734
    vf->datasource=NULL;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   735
    ov_clear(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   736
  }else if(vf->ready_state < PARTOPEN)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   737
    vf->ready_state=PARTOPEN;
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   738
  return ret;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   739
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   740
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   741
static int _ov_open2(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   742
  if(vf->ready_state < OPENED)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   743
    vf->ready_state=OPENED;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   744
  if(vf->seekable){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   745
    int ret=_open_seekable2(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   746
    if(ret){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   747
      vf->datasource=NULL;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   748
      ov_clear(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   749
    }
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   750
    return ret;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   751
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   752
  return 0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   753
}
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
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   756
/* clear out the OggVorbis_File struct */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   757
int ov_clear(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   758
  if(vf){
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   759
    vorbis_dsp_destroy(vf->vd);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   760
    vf->vd=0;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   761
    ogg_stream_destroy(vf->os);
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   762
    vorbis_info_clear(&vf->vi);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   763
    vorbis_comment_clear(&vf->vc);
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   764
    if(vf->dataoffsets)_ogg_free(vf->dataoffsets);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   765
    if(vf->pcmlengths)_ogg_free(vf->pcmlengths);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   766
    if(vf->serialnos)_ogg_free(vf->serialnos);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   767
    if(vf->offsets)_ogg_free(vf->offsets);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   768
    ogg_sync_destroy(vf->oy);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   769
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   770
    if(vf->datasource)(vf->callbacks.close_func)(vf->datasource);
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   771
    memset(vf,0,sizeof(*vf));
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
#ifdef DEBUG_LEAKS
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   774
  _VDBG_dump();
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   775
#endif
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   776
  return 0;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   777
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   778
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   779
/* 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
   780
   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
   781
   bitstream sections that are truncated/woogie. 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   782
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   783
   return: -1) error
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   784
            0) OK
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   785
*/
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
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
   788
    ov_callbacks callbacks){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   789
  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
   790
  if(ret)return ret;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   791
  return _ov_open2(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   792
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   793
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   794
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
   795
  ov_callbacks callbacks = {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   796
    (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
   797
    (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
   798
    (int (*)(void *))                             fclose,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   799
    (long (*)(void *))                            ftell
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   800
  };
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   801
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   802
  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
   803
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   804
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   805
/* 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
   806
   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
   807
   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
   808
   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
   809
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   810
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
   811
    ov_callbacks callbacks)
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
  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
   814
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   815
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   816
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
   817
  ov_callbacks callbacks = {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   818
    (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
   819
    (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
   820
    (int (*)(void *))                             fclose,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   821
    (long (*)(void *))                            ftell
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
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   824
  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
   825
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   826
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   827
int ov_test_open(OggVorbis_File *vf){
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   828
  if(vf->ready_state!=PARTOPEN)return OV_EINVAL;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   829
  return _ov_open2(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   830
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   831
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   832
/* 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
   833
long ov_streams(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   834
  return vf->links;
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
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   837
/* Is the FILE * associated with vf seekable? */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   838
long ov_seekable(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   839
  return vf->seekable;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   840
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   841
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   842
/* 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
   843
   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
   844
   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
   845
   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
   846
   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
   847
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   848
   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
   849
   vorbis_info structs */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   850
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   851
long ov_bitrate(OggVorbis_File *vf,int i){
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   852
  if(vf->ready_state<OPENED)return OV_EINVAL;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   853
  if(i>=vf->links)return OV_EINVAL;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   854
  if(!vf->seekable && i!=0)return ov_bitrate(vf,0);
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   855
  if(i<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   856
    ogg_int64_t bits=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   857
    int i;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   858
    for(i=0;i<vf->links;i++)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   859
      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
   860
    /* 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
   861
     * 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
   862
     * 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
   863
     */
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   864
    return bits*1000/ov_time_total(vf,-1);
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   865
  }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   866
    if(vf->seekable){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   867
      /* return the actual bitrate */
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   868
      return (vf->offsets[i+1]-vf->dataoffsets[i])*8000/ov_time_total(vf,i);
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   869
    }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   870
      /* return nominal if set */
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   871
      if(vf->vi.bitrate_nominal>0){
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   872
	return vf->vi.bitrate_nominal;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   873
      }else{
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   874
	if(vf->vi.bitrate_upper>0){
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   875
	  if(vf->vi.bitrate_lower>0){
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   876
	    return (vf->vi.bitrate_upper+vf->vi.bitrate_lower)/2;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   877
	  }else{
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   878
	    return vf->vi.bitrate_upper;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   879
	  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   880
	}
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   881
	return OV_FALSE;
5170
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
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   886
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   887
/* 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
   888
   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
   889
   EINVAL if stream is only partially open 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   890
*/
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   891
long ov_bitrate_instant(OggVorbis_File *vf){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   892
  long ret;
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   893
  if(vf->ready_state<OPENED)return OV_EINVAL;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   894
  if(vf->samptrack==0)return OV_FALSE;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   895
  ret=vf->bittrack/vf->samptrack*vf->vi.rate;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   896
  vf->bittrack=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   897
  vf->samptrack=0;
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   898
  return ret;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   899
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   900
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   901
/* Guess */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   902
long ov_serialnumber(OggVorbis_File *vf,int i){
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   903
  if(i>=vf->links)return ov_serialnumber(vf,vf->links-1);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   904
  if(!vf->seekable && i>=0)return ov_serialnumber(vf,-1);
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   905
  if(i<0){
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   906
    return vf->current_serialno;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   907
  }else{
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   908
    return vf->serialnos[i];
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   909
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   910
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   911
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   912
/* 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
   913
            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
   914
	    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
   915
	    or if stream is only partially open
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   916
*/
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   917
ogg_int64_t ov_raw_total(OggVorbis_File *vf,int i){
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   918
  if(vf->ready_state<OPENED)return OV_EINVAL;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   919
  if(!vf->seekable || i>=vf->links)return OV_EINVAL;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   920
  if(i<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   921
    ogg_int64_t acc=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   922
    int i;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   923
    for(i=0;i<vf->links;i++)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   924
      acc+=ov_raw_total(vf,i);
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   925
    return acc;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   926
  }else{
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   927
    return vf->offsets[i+1]-vf->offsets[i];
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   928
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   929
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   930
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   931
/* 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
   932
	    (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
   933
	    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
   934
	    length) or only partially open 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   935
*/
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   936
ogg_int64_t ov_pcm_total(OggVorbis_File *vf,int i){
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   937
  if(vf->ready_state<OPENED)return OV_EINVAL;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   938
  if(!vf->seekable || i>=vf->links)return OV_EINVAL;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   939
  if(i<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   940
    ogg_int64_t acc=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   941
    int i;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   942
    for(i=0;i<vf->links;i++)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   943
      acc+=ov_pcm_total(vf,i);
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   944
    return acc;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   945
  }else{
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   946
    return vf->pcmlengths[i*2+1];
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   947
  }
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
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   950
/* 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
   951
            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
   952
	    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
   953
	    length) or only partially open 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   954
*/
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   955
ogg_int64_t ov_time_total(OggVorbis_File *vf,int i){
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   956
  if(vf->ready_state<OPENED)return OV_EINVAL;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   957
  if(!vf->seekable || i>=vf->links)return OV_EINVAL;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   958
  if(i<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   959
    ogg_int64_t acc=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   960
    int i;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   961
    for(i=0;i<vf->links;i++)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   962
      acc+=ov_time_total(vf,i);
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   963
    return acc;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   964
  }else{
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   965
    return ((ogg_int64_t)vf->pcmlengths[i*2+1])*1000/vf->vi.rate;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   966
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   967
}
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 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
   970
   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
   971
   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
   972
   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
   973
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   974
   returns zero on success, nonzero on failure */
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
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
   977
  ogg_stream_state *work_os=NULL;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   978
  ogg_page og={0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   979
  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
   980
  
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   981
  if(vf->ready_state<OPENED)return OV_EINVAL;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   982
  if(!vf->seekable)
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   983
    return OV_ENOSEEK; /* don't dump machine if we can't seek */
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   984
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   985
  if(pos<0 || pos>vf->end)return OV_EINVAL;
5170
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
  /* 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
   988
     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
   989
     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
   990
     boundary */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   991
  vf->pcm_offset=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   992
  ogg_stream_reset_serialno(vf->os,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   993
			    vf->current_serialno); /* must set serialno */
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
   994
  vorbis_dsp_restart(vf->vd);
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   995
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   996
  _seek_helper(vf,pos);
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
  /* 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
   999
     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
  1000
     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
  1001
     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
  1002
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1003
     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
  1004
     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
  1005
     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
  1006
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1007
     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
  1008
     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
  1009
     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
  1010
     correct. 
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
  {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1014
    int lastblock=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1015
    int accblock=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1016
    int thisblock;
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1017
    int eosflag;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1018
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1019
    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
  1020
    while(1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1021
      if(vf->ready_state>=STREAMSET){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1022
	/* snarf/scan a packet if we can */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1023
	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
  1024
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1025
	if(result>0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1026
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1027
	  if(vf->vi.codec_setup){
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1028
	    thisblock=vorbis_packet_blocksize(&vf->vi,&op);
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1029
	    if(thisblock<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1030
	      ogg_stream_packetout(vf->os,NULL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1031
	      thisblock=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1032
	    }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1033
	      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1034
	      if(eosflag)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1035
		ogg_stream_packetout(vf->os,NULL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1036
	      else
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1037
		if(lastblock)accblock+=(lastblock+thisblock)>>2;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1038
	    }	    
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
	    if(op.granulepos!=-1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1041
	      int i,link=vf->current_link;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1042
	      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
  1043
	      if(granulepos<0)granulepos=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1044
	      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1045
	      for(i=0;i<link;i++)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1046
		granulepos+=vf->pcmlengths[i*2+1];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1047
	      vf->pcm_offset=granulepos-accblock;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1048
	      break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1049
	    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1050
	    lastblock=thisblock;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1051
	    continue;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1052
	  }else
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1053
	    ogg_stream_packetout(vf->os,NULL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1054
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1055
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1056
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1057
      if(!lastblock){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1058
	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
  1059
	  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
  1060
	  break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1061
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1062
      }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1063
	/* 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
  1064
	vf->pcm_offset=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1065
	break;
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
      
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1068
      /* did we just grab a page from other than current link? */
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1069
      if(vf->ready_state>=STREAMSET)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1070
	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
  1071
	  _decode_clear(vf); /* clear out stream state */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1072
	  ogg_stream_destroy(work_os);
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
      if(vf->ready_state<STREAMSET){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1076
	int link;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1077
	
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1078
	vf->current_serialno=ogg_page_serialno(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1079
	for(link=0;link<vf->links;link++)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1080
	  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
  1081
	if(link==vf->links)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1082
	  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
  1083
			      leave machine uninitialized */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1084
 
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1085
	/* need to initialize machine to this link */
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1086
	{
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1087
	  int ret=_set_link_number_preserve_pos(vf,link);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1088
	  if(ret) goto seek_error;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1089
	}
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1090
	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
  1091
	ogg_stream_reset_serialno(work_os,vf->current_serialno); 
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1092
	
5170
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
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1095
    
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
	ogg_page dup;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1098
	ogg_page_dup(&dup,&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1099
	eosflag=ogg_page_eos(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1100
	ogg_stream_pagein(vf->os,&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1101
	ogg_stream_pagein(work_os,&dup);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1102
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1103
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1104
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1105
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1106
  ogg_packet_release(&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1107
  ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1108
  ogg_stream_destroy(work_os);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1109
  vf->bittrack=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1110
  vf->samptrack=0;
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1111
  return 0;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1112
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1113
 seek_error:
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1114
  ogg_packet_release(&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1115
  ogg_page_release(&og);
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
  /* 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
  1118
  vf->pcm_offset=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1119
  ogg_stream_destroy(work_os);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1120
  _decode_clear(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1121
  return OV_EBADLINK;
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
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1124
/* 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
  1125
   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
  1126
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1127
   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
  1128
   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
  1129
   arrive at the requested position. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1130
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
  1131
  int link=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1132
  ogg_int64_t result=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1133
  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
  1134
  ogg_page og={0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1135
  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
  1136
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1137
  if(vf->ready_state<OPENED)return OV_EINVAL;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1138
  if(!vf->seekable)return OV_ENOSEEK;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1139
  if(pos<0 || pos>total)return OV_EINVAL;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1140
 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1141
  /* 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
  1142
  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
  1143
    total-=vf->pcmlengths[link*2+1];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1144
    if(pos>=total)break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1145
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1146
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1147
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1148
  if(link!=vf->current_link){
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1149
    int ret=_set_link_number(vf,link);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1150
    if(ret) goto seek_error;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1151
  }else{
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1152
    vorbis_dsp_restart(vf->vd);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1153
  }
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1154
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1155
  ogg_stream_reset_serialno(vf->os,vf->serialnos[link]);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1156
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1157
  /* 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
  1158
     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
  1159
     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
  1160
     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
  1161
     would be an error condition) */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1162
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1163
  /* new search algorithm by HB (Nicholas Vinen) */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1164
  {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1165
    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
  1166
    ogg_int64_t begin=vf->offsets[link];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1167
    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
  1168
    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
  1169
    ogg_int64_t target=pos-total+begintime;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1170
    ogg_int64_t best=begin;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1171
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1172
    while(begin<end){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1173
      ogg_int64_t bisect;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1174
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1175
      if(end-begin<CHUNKSIZE){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1176
	bisect=begin;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1177
      }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1178
	/* take a (pretty decent) guess. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1179
	bisect=begin + 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1180
	  (target-begintime)*(end-begin)/(endtime-begintime) - CHUNKSIZE;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1181
	if(bisect<=begin)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1182
	  bisect=begin+1;
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
      _seek_helper(vf,bisect);
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
      while(begin<end){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1188
	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
  1189
	if(result==OV_EREAD) goto seek_error;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1190
	if(result<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1191
	  if(bisect<=begin+1)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1192
	    end=begin; /* found it */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1193
	  else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1194
	    if(bisect==0) goto seek_error;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1195
	    bisect-=CHUNKSIZE;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1196
	    if(bisect<=begin)bisect=begin+1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1197
	    _seek_helper(vf,bisect);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1198
	  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1199
	}else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1200
	  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
  1201
	  if(granulepos==-1)continue;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1202
	  if(granulepos<target){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1203
	    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
  1204
	    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
  1205
	    begintime=granulepos;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1206
	    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1207
	    if(target-begintime>44100)break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1208
	    bisect=begin; /* *not* begin + 1 */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1209
	  }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1210
	    if(bisect<=begin+1)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1211
	      end=begin;  /* found it */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1212
	    else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1213
	      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
  1214
		end=result;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1215
		bisect-=CHUNKSIZE; /* an endless loop otherwise. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1216
		if(bisect<=begin)bisect=begin+1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1217
		_seek_helper(vf,bisect);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1218
	      }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1219
		end=result;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1220
		endtime=granulepos;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1221
		break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1222
	      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1223
	    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1224
	  }
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
      }
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
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1229
    /* 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
  1230
       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
  1231
    {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1232
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1233
      /* seek */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1234
      _seek_helper(vf,best);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1235
      vf->pcm_offset=-1;
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
      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
  1238
	ogg_page_release(&og);
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1239
	return OV_EOF; /* shouldn't happen */
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1240
      }
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
      ogg_stream_pagein(vf->os,&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1243
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1244
      /* 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
  1245
      while(1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1246
	result=ogg_stream_packetpeek(vf->os,&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1247
	if(result==0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1248
	  /* !!! 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
  1249
             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
  1250
             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
  1251
             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
  1252
	  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1253
	  _seek_helper(vf,best);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1254
	  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1255
	  while(1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1256
	    result=_get_prev_page(vf,&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1257
	    if(result<0) goto seek_error;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1258
	    if(ogg_page_granulepos(&og)>-1 ||
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1259
	       !ogg_page_continued(&og)){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1260
	      return ov_raw_seek(vf,result);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1261
	    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1262
	    vf->offset=result;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1263
	  }
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
	if(result<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1266
	  result = OV_EBADPACKET; 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1267
	  goto seek_error;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1268
	}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1269
	if(op.granulepos!=-1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1270
	  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
  1271
	  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
  1272
	  vf->pcm_offset+=total;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1273
	  break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1274
	}else
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1275
	  result=ogg_stream_packetout(vf->os,NULL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1276
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1277
    }
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
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1280
  /* verify result */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1281
  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
  1282
    result=OV_EFAULT;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1283
    goto seek_error;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1284
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1285
  vf->bittrack=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1286
  vf->samptrack=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1287
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1288
  ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1289
  ogg_packet_release(&op);
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1290
  return 0;
5170
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
 seek_error:
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1293
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1294
  ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1295
  ogg_packet_release(&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1296
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1297
  /* 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
  1298
  vf->pcm_offset=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1299
  _decode_clear(vf);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1300
  return (int)result;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1301
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1302
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1303
/* 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
  1304
   returns zero on success, nonzero on failure */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1305
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1306
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
  1307
  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
  1308
  ogg_page og={0,0,0,0};
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1309
  int thisblock,lastblock=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1310
  int ret=ov_pcm_seek_page(vf,pos);
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1311
  if(ret<0)return ret;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1312
  if(_make_decode_ready(vf))return OV_EBADLINK;
5170
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
  /* 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
  1315
     position we want; don't decode them */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1316
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1317
  while(1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1318
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1319
    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
  1320
    if(ret>0){
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1321
      thisblock=vorbis_packet_blocksize(&vf->vi,&op);
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1322
      if(thisblock<0){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1323
	ogg_stream_packetout(vf->os,NULL);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1324
	continue; /* non audio packet */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1325
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1326
      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
  1327
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1328
      if(vf->pcm_offset+((thisblock+
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1329
			  vorbis_info_blocksize(&vf->vi,1))>>2)>=pos)break;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1330
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1331
      /* 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
  1332
      ogg_stream_packetout(vf->os,NULL);
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1333
      vorbis_dsp_synthesis(vf->vd,&op,0);  /* set up a vb with
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1334
					      only tracking, no
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1335
					      pcm_decode */
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1336
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1337
      /* 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
  1338
	 length positioning. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1339
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1340
      if(op.granulepos>-1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1341
	int i;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1342
	/* always believe the stream markers */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1343
	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
  1344
	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
  1345
	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
  1346
	  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
  1347
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1348
	
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1349
      lastblock=thisblock;
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
    }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1352
      if(ret<0 && ret!=OV_HOLE)break;
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
      /* suck in a new page */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1355
      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
  1356
      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
  1357
      
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1358
      if(vf->ready_state<STREAMSET){
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1359
	int link,ret;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1360
	
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1361
	vf->current_serialno=ogg_page_serialno(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1362
	for(link=0;link<vf->links;link++)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1363
	  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
  1364
	if(link==vf->links){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1365
	  ogg_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1366
	  ogg_packet_release(&op);
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1367
	  return OV_EBADLINK;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1368
	}
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1369
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1370
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1371
	vf->current_link=link;
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1372
	ret=_fetch_headers(vf,&vf->vi,&vf->vc,&vf->current_serialno,&og);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1373
	if(ret) return ret;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1374
	if(_make_decode_ready(vf))return OV_EBADLINK;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1375
	lastblock=0;
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
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1378
      ogg_stream_pagein(vf->os,&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1379
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1380
  }
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
  vf->bittrack=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1383
  vf->samptrack=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1384
  /* 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
  1385
     logical bitstream boundary with abandon is OK. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1386
  while(vf->pcm_offset<pos){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1387
    ogg_int64_t target=pos-vf->pcm_offset;
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1388
    long samples=vorbis_dsp_pcmout(vf->vd,NULL,0);
5170
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
    if(samples>target)samples=target;
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1391
    vorbis_dsp_read(vf->vd,samples);
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1392
    vf->pcm_offset+=samples;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1393
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1394
    if(samples<target)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1395
      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
  1396
	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
  1397
  }
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_page_release(&og);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1400
  ogg_packet_release(&op);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1401
  return 0;
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
/* 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
  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(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
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1413
  if(vf->ready_state<OPENED)return OV_EINVAL;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1414
  if(!vf->seekable)return OV_ENOSEEK;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1415
  if(milliseconds<0 || milliseconds>time_total)return OV_EINVAL;
5170
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
  {
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1426
    int ret=_set_link_number(vf,link);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1427
    if(ret)return ret;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1428
    return 
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1429
      ov_pcm_seek(vf,pcm_total+(milliseconds-time_total)*
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1430
		  vf->vi.rate/1000);
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1431
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1432
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1433
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1434
/* page-granularity version of ov_time_seek 
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1435
   returns zero on success, nonzero on failure */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1436
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
  1437
  /* 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
  1438
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1439
  int link=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1440
  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
  1441
  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
  1442
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1443
  if(vf->ready_state<OPENED)return OV_EINVAL;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1444
  if(!vf->seekable)return OV_ENOSEEK;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1445
  if(milliseconds<0 || milliseconds>time_total)return OV_EINVAL;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1446
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1447
  /* 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
  1448
  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
  1449
    pcm_total-=vf->pcmlengths[link*2+1];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1450
    time_total-=ov_time_total(vf,link);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1451
    if(milliseconds>=time_total)break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1452
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1453
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1454
  /* 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
  1455
  {
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1456
    int ret=_set_link_number(vf,link);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1457
    if(ret)return ret;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1458
    return 
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1459
      ov_pcm_seek_page(vf,pcm_total+(milliseconds-time_total)*
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1460
		       vf->vi.rate/1000);
5170
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
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1464
/* 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
  1465
   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
  1466
ogg_int64_t ov_raw_tell(OggVorbis_File *vf){
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1467
  if(vf->ready_state<OPENED)return OV_EINVAL;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1468
  return vf->offset;
5170
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
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1471
/* 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
  1472
ogg_int64_t ov_pcm_tell(OggVorbis_File *vf){
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1473
  if(vf->ready_state<OPENED)return OV_EINVAL;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1474
  return vf->pcm_offset;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1475
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1476
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1477
/* 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
  1478
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
  1479
  int link=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1480
  ogg_int64_t pcm_total=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1481
  ogg_int64_t time_total=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1482
  
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1483
  if(vf->ready_state<OPENED)return OV_EINVAL;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1484
  if(vf->seekable){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1485
    pcm_total=ov_pcm_total(vf,-1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1486
    time_total=ov_time_total(vf,-1);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1487
  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1488
    /* 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
  1489
    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
  1490
      pcm_total-=vf->pcmlengths[link*2+1];
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1491
      time_total-=ov_time_total(vf,link);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1492
      if(vf->pcm_offset>=pcm_total)break;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1493
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1494
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1495
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1496
  return time_total+(1000*vf->pcm_offset-pcm_total)/vf->vi.rate;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1497
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1498
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1499
/*  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
  1500
                currently being decoded
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1501
           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
  1502
    
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1503
    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
  1504
    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
  1505
    initialized */
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
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
  1508
  if(vf->seekable){
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1509
    if(link>=vf->links)return NULL;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1510
    if(link>=0){
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1511
      int ret=_set_link_number_preserve_pos(vf,link);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1512
      if(ret)return NULL;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1513
    }
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1514
  }
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1515
  return &vf->vi;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1516
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1517
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1518
/* 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
  1519
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
  1520
  if(vf->seekable){
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1521
    if(link>=vf->links)return NULL;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1522
    if(link>=0){
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1523
      int ret=_set_link_number_preserve_pos(vf,link);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1524
      if(ret)return NULL;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1525
    }
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1526
  }
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1527
  return &vf->vc;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1528
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1529
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1530
/* 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
  1531
   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
  1532
   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
  1533
   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
  1534
   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
  1535
   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
  1536
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1537
   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
  1538
   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
  1539
   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
  1540
   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
  1541
   (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
  1542
   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
  1543
   functions above are aware of this dichotomy).
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1544
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1545
   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
  1546
		 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
  1547
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1548
   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
  1549
                   0) EOF
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1550
		   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
  1551
		   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
  1552
		   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
  1553
		   in, just guaranteed to fit.
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1554
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1555
	    *section) set to the logical bitstream number */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1556
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1557
long ov_read(OggVorbis_File *vf,void *buffer,int bytes_req,int *bitstream){
5170
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
  long samples;
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1560
  long channels;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1561
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1562
  if(vf->ready_state<OPENED)return OV_EINVAL;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1563
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1564
  while(1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1565
    if(vf->ready_state==INITSET){
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1566
      channels=vf->vi.channels;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1567
      samples=vorbis_dsp_pcmout(vf->vd,buffer,(bytes_req>>1)/channels);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1568
      if(samples){
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1569
	if(samples>0){
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1570
	  vorbis_dsp_read(vf->vd,samples);
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1571
	  vf->pcm_offset+=samples;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1572
	  if(bitstream)*bitstream=vf->current_link;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1573
	  return samples*2*channels;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1574
	}
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1575
	return samples;
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1576
      }
5170
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
    /* suck in another packet */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1580
    {
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1581
      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
  1582
      if(ret==OV_EOF)
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1583
	return 0;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1584
      if(ret<=0)
7697
767d3c4153a1 switch the Tremor implementation from the Cocos2D one to the lowmem-no-byte branch and let's see what happens
koda
parents: 6551
diff changeset
  1585
	return ret;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1586
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1587
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1588
  }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
  1589
}