misc/libtremor/tremor/bitwise.c
author nemo
Fri, 30 Nov 2012 22:23:26 -0500
changeset 8165 9527839ad58b
parent 7849 a12155461b34
permissions -rw-r--r--
oups
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-2002    *
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: packing variable sized words into an octet stream
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    15
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
/* We're 'LSb' endian; if we write a word but read individual bits,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    19
   then we'll read the lsb first */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    20
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    21
#include <string.h>
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    22
#include <stdlib.h>
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    23
#include "ogg.h"
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    24
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    25
static unsigned long mask[]=
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    26
{0x00000000,0x00000001,0x00000003,0x00000007,0x0000000f,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    27
 0x0000001f,0x0000003f,0x0000007f,0x000000ff,0x000001ff,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    28
 0x000003ff,0x000007ff,0x00000fff,0x00001fff,0x00003fff,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    29
 0x00007fff,0x0000ffff,0x0001ffff,0x0003ffff,0x0007ffff,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    30
 0x000fffff,0x001fffff,0x003fffff,0x007fffff,0x00ffffff,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    31
 0x01ffffff,0x03ffffff,0x07ffffff,0x0fffffff,0x1fffffff,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    32
 0x3fffffff,0x7fffffff,0xffffffff };
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    33
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    34
/* mark read process as having run off the end */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    35
static void _adv_halt(oggpack_buffer *b){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    36
  b->headptr=b->head->buffer->data+b->head->begin+b->head->length;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    37
  b->headend=-1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    38
  b->headbit=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    39
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    40
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    41
/* spans forward, skipping as many bytes as headend is negative; if
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    42
   headend is zero, simply finds next byte.  If we're up to the end
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    43
   of the buffer, leaves headend at zero.  If we've read past the end,
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    44
   halt the decode process. */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    45
static void _span(oggpack_buffer *b){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    46
  while(b->headend<1){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    47
    if(b->head->next){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    48
      b->count+=b->head->length;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    49
      b->head=b->head->next;
7849
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
    50
      b->headptr=b->head->buffer->data+b->head->begin-b->headend;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
    51
      b->headend+=b->head->length;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    52
    }else{
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    53
      /* we've either met the end of decode, or gone past it. halt
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    54
         only if we're past */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    55
      if(b->headend<0 || b->headbit)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    56
        /* read has fallen off the end */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    57
        _adv_halt(b);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    58
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    59
      break;
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
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    63
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    64
void oggpack_readinit(oggpack_buffer *b,ogg_reference *r){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    65
  memset(b,0,sizeof(*b));
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
  b->tail=b->head=r;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    68
  b->count=0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    69
  b->headptr=b->head->buffer->data+b->head->begin;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    70
  b->headend=b->head->length;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    71
  _span(b);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    72
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    73
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    74
#define _lookspan()   while(!end){\
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    75
                        head=head->next;\
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    76
                        if(!head) return -1;\
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    77
                        ptr=head->buffer->data + head->begin;\
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    78
                        end=head->length;\
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
/* Read in bits without advancing the bitptr; bits <= 32 */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    82
long oggpack_look(oggpack_buffer *b,int bits){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    83
  unsigned long m=mask[bits];
7849
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
    84
  unsigned long ret=-1;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    85
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    86
  bits+=b->headbit;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    87
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    88
  if(bits >= b->headend<<3){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    89
    int            end=b->headend;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    90
    unsigned char *ptr=b->headptr;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    91
    ogg_reference *head=b->head;
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
    if(end<0)return -1;
7849
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
    94
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    95
    if(bits){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    96
      _lookspan();
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    97
      ret=*ptr++>>b->headbit;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    98
      if(bits>8){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
    99
        --end;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   100
        _lookspan();
7849
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   101
        ret|=*ptr++<<(8-b->headbit);
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   102
        if(bits>16){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   103
          --end;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   104
          _lookspan();
7849
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   105
          ret|=*ptr++<<(16-b->headbit);
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   106
          if(bits>24){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   107
            --end;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   108
            _lookspan();
7849
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   109
            ret|=*ptr++<<(24-b->headbit);
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   110
            if(bits>32 && b->headbit){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   111
              --end;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   112
              _lookspan();
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   113
              ret|=*ptr<<(32-b->headbit);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   114
            }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   115
          }
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
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   118
    }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   119
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
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   122
    /* make this a switch jump-table */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   123
    ret=b->headptr[0]>>b->headbit;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   124
    if(bits>8){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   125
      ret|=b->headptr[1]<<(8-b->headbit);  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   126
      if(bits>16){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   127
        ret|=b->headptr[2]<<(16-b->headbit);  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   128
        if(bits>24){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   129
          ret|=b->headptr[3]<<(24-b->headbit);  
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   130
          if(bits>32 && b->headbit)
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   131
            ret|=b->headptr[4]<<(32-b->headbit);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   132
        }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   133
      }
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   134
    }
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
  ret&=m;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   138
  return ret;
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
/* limited to 32 at a time */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   142
void oggpack_adv(oggpack_buffer *b,int bits){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   143
  bits+=b->headbit;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   144
  b->headbit=bits&7;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   145
  b->headptr+=bits/8;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   146
  if((b->headend-=bits/8)<1)_span(b);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   147
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   148
7849
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   149
/* spans forward and finds next byte.  Never halts */
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   150
static void _span_one(oggpack_buffer *b){
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   151
  while(b->headend<1){
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   152
    if(b->head->next){
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   153
      b->count+=b->head->length;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   154
      b->head=b->head->next;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   155
      b->headptr=b->head->buffer->data+b->head->begin;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   156
      b->headend=b->head->length;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   157
    }else
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   158
      break;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   159
  }
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   160
}
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   161
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   162
static int _halt_one(oggpack_buffer *b){
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   163
  if(b->headend<1){
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   164
    _adv_halt(b);
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   165
    return -1;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   166
  }
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   167
  return 0;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   168
}
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   169
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   170
int oggpack_eop(oggpack_buffer *b){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   171
  if(b->headend<0)return -1;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   172
  return 0;
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   173
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   174
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   175
/* bits <= 32 */
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   176
long oggpack_read(oggpack_buffer *b,int bits){
7849
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   177
  unsigned long m=mask[bits];
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   178
  ogg_uint32_t ret=-1;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   179
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   180
  bits+=b->headbit;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   181
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   182
  if(bits >= b->headend<<3){
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   183
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   184
    if(b->headend<0)return -1;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   185
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   186
    if(bits){
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   187
      if (_halt_one(b)) return -1;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   188
      ret=*b->headptr>>b->headbit;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   189
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   190
      if(bits>=8){
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   191
        ++b->headptr;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   192
        --b->headend;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   193
        _span_one(b);
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   194
        if(bits>8){
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   195
          if (_halt_one(b)) return -1;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   196
          ret|=*b->headptr<<(8-b->headbit);
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   197
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   198
          if(bits>=16){
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   199
            ++b->headptr;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   200
            --b->headend;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   201
            _span_one(b);
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   202
            if(bits>16){
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   203
              if (_halt_one(b)) return -1;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   204
              ret|=*b->headptr<<(16-b->headbit);
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   205
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   206
              if(bits>=24){
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   207
                ++b->headptr;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   208
                --b->headend;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   209
                _span_one(b);
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   210
                if(bits>24){
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   211
                  if (_halt_one(b)) return -1;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   212
                  ret|=*b->headptr<<(24-b->headbit);
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   213
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   214
                  if(bits>=32){
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   215
                    ++b->headptr;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   216
                    --b->headend;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   217
                    _span_one(b);
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   218
                    if(bits>32){
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   219
                      if (_halt_one(b)) return -1;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   220
                      if(b->headbit)ret|=*b->headptr<<(32-b->headbit);
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   221
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   222
                    }
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   223
                  }
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   224
                }
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   225
              }
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   226
            }
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   227
          }
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   228
        }
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   229
      }
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   230
    }
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   231
  }else{
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   232
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   233
    ret=b->headptr[0]>>b->headbit;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   234
    if(bits>8){
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   235
      ret|=b->headptr[1]<<(8-b->headbit);
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   236
      if(bits>16){
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   237
        ret|=b->headptr[2]<<(16-b->headbit);
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   238
        if(bits>24){
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   239
          ret|=b->headptr[3]<<(24-b->headbit);
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   240
          if(bits>32 && b->headbit){
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   241
            ret|=b->headptr[4]<<(32-b->headbit);
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   242
          }
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   243
        }
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   244
      }
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   245
    }
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   246
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   247
    b->headptr+=bits/8;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   248
    b->headend-=bits/8;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   249
  }
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   250
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   251
  ret&=m;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   252
  b->headbit=bits&7;
a12155461b34 revert r767d3c4153a1 this tremor implementation is buggy
koda
parents: 7697
diff changeset
   253
  return ret;
5170
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   254
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   255
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   256
long oggpack_bytes(oggpack_buffer *b){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   257
  return(b->count+b->headptr-b->head->buffer->data-b->head->begin+
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   258
         (b->headbit+7)/8);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   259
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   260
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   261
long oggpack_bits(oggpack_buffer *b){
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   262
  return((b->count+b->headptr-b->head->buffer->data-b->head->begin)*8+
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   263
         b->headbit);
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   264
}
f7e49eff3708 add libTremor sources (integer-only libvorbis implementation) to repo and ios project
koda
parents:
diff changeset
   265