Jean-Marc Valin | 02fa913 | 2008-02-20 12:09:29 +1100 | [diff] [blame] | 1 | #ifdef HAVE_CONFIG_H |
| 2 | #include "config.h" |
| 3 | #endif |
| 4 | |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | #include "entdec.h" |
Jean-Marc Valin | a1bb9c7 | 2008-04-28 17:30:26 +1000 | [diff] [blame^] | 7 | #include "os_support.h" |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 8 | |
| 9 | |
| 10 | void ec_byte_readinit(ec_byte_buffer *_b,unsigned char *_buf,long _bytes){ |
| 11 | _b->buf=_b->ptr=_buf; |
| 12 | _b->storage=_bytes; |
| 13 | } |
| 14 | |
| 15 | int ec_byte_look1(ec_byte_buffer *_b){ |
| 16 | ptrdiff_t endbyte; |
| 17 | endbyte=_b->ptr-_b->buf; |
| 18 | if(endbyte>=_b->storage)return -1; |
| 19 | else return _b->ptr[0]; |
| 20 | } |
| 21 | |
| 22 | int ec_byte_look4(ec_byte_buffer *_b,ec_uint32 *_val){ |
| 23 | ptrdiff_t endbyte; |
| 24 | endbyte=_b->ptr-_b->buf; |
| 25 | if(endbyte+4>_b->storage){ |
| 26 | if(endbyte<_b->storage){ |
| 27 | *_val=_b->ptr[0]; |
| 28 | endbyte++; |
| 29 | if(endbyte<_b->storage){ |
| 30 | *_val|=(ec_uint32)_b->ptr[1]<<8; |
| 31 | endbyte++; |
| 32 | if(endbyte<_b->storage)*_val|=(ec_uint32)_b->ptr[2]<<16; |
| 33 | } |
| 34 | } |
| 35 | return -1; |
| 36 | } |
| 37 | else{ |
| 38 | *_val=_b->ptr[0]; |
| 39 | *_val|=(ec_uint32)_b->ptr[1]<<8; |
| 40 | *_val|=(ec_uint32)_b->ptr[2]<<16; |
| 41 | *_val|=(ec_uint32)_b->ptr[3]<<24; |
| 42 | } |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | void ec_byte_adv1(ec_byte_buffer *_b){ |
| 47 | _b->ptr++; |
| 48 | } |
| 49 | |
| 50 | void ec_byte_adv4(ec_byte_buffer *_b){ |
| 51 | _b->ptr+=4; |
| 52 | } |
| 53 | |
| 54 | int ec_byte_read1(ec_byte_buffer *_b){ |
| 55 | ptrdiff_t endbyte; |
| 56 | endbyte=_b->ptr-_b->buf; |
| 57 | if(endbyte>=_b->storage)return -1; |
| 58 | else return *(_b->ptr++); |
| 59 | } |
| 60 | |
| 61 | int ec_byte_read4(ec_byte_buffer *_b,ec_uint32 *_val){ |
| 62 | unsigned char *end; |
| 63 | end=_b->buf+_b->storage; |
| 64 | if(_b->ptr+4>end){ |
| 65 | if(_b->ptr<end){ |
| 66 | *_val=*(_b->ptr++); |
| 67 | if(_b->ptr<end){ |
| 68 | *_val|=(ec_uint32)*(_b->ptr++)<<8; |
| 69 | if(_b->ptr<end)*_val|=(ec_uint32)*(_b->ptr++)<<16; |
| 70 | } |
| 71 | } |
| 72 | return -1; |
| 73 | } |
| 74 | else{ |
| 75 | *_val=(*_b->ptr++); |
| 76 | *_val|=(ec_uint32)*(_b->ptr++)<<8; |
| 77 | *_val|=(ec_uint32)*(_b->ptr++)<<16; |
| 78 | *_val|=(ec_uint32)*(_b->ptr++)<<24; |
| 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | |
| 85 | ec_uint32 ec_dec_bits(ec_dec *_this,int _ftb){ |
| 86 | ec_uint32 t; |
| 87 | unsigned s; |
| 88 | unsigned ft; |
| 89 | t=0; |
| 90 | while(_ftb>EC_UNIT_BITS){ |
Jean-Marc Valin | c2decd3 | 2008-03-22 22:58:45 +1100 | [diff] [blame] | 91 | s=ec_decode_bin(_this,EC_UNIT_BITS); |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 92 | ec_dec_update(_this,s,s+1,EC_UNIT_MASK+1); |
| 93 | t=t<<EC_UNIT_BITS|s; |
| 94 | _ftb-=EC_UNIT_BITS; |
| 95 | } |
| 96 | ft=1U<<_ftb; |
Jean-Marc Valin | c2decd3 | 2008-03-22 22:58:45 +1100 | [diff] [blame] | 97 | s=ec_decode_bin(_this,_ftb); |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 98 | ec_dec_update(_this,s,s+1,ft); |
| 99 | t=t<<_ftb|s; |
| 100 | return t; |
| 101 | } |
| 102 | |
Timothy B. Terriberry | f13fea7 | 2007-12-11 13:25:57 +1100 | [diff] [blame] | 103 | ec_uint64 ec_dec_bits64(ec_dec *_this,int _ftb){ |
tterribe | 649d5cc | 2008-01-17 07:51:18 +0000 | [diff] [blame] | 104 | ec_uint32 t; |
Timothy B. Terriberry | f13fea7 | 2007-12-11 13:25:57 +1100 | [diff] [blame] | 105 | if(_ftb>32){ |
tterribe | 649d5cc | 2008-01-17 07:51:18 +0000 | [diff] [blame] | 106 | t=ec_dec_bits(_this,_ftb-32); |
| 107 | _ftb=32; |
Timothy B. Terriberry | f13fea7 | 2007-12-11 13:25:57 +1100 | [diff] [blame] | 108 | } |
| 109 | else t=0; |
tterribe | 649d5cc | 2008-01-17 07:51:18 +0000 | [diff] [blame] | 110 | return (ec_uint64)t<<32|ec_dec_bits(_this,_ftb); |
Timothy B. Terriberry | f13fea7 | 2007-12-11 13:25:57 +1100 | [diff] [blame] | 111 | } |
| 112 | |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 113 | ec_uint32 ec_dec_uint(ec_dec *_this,ec_uint32 _ft){ |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 114 | ec_uint32 t; |
Timothy B. Terriberry | f13fea7 | 2007-12-11 13:25:57 +1100 | [diff] [blame] | 115 | unsigned ft; |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 116 | unsigned s; |
| 117 | int ftb; |
| 118 | t=0; |
| 119 | _ft--; |
| 120 | ftb=EC_ILOG(_ft); |
Jean-Marc Valin | dc767f6 | 2008-03-22 22:23:58 +1100 | [diff] [blame] | 121 | if(ftb>EC_UNIT_BITS){ |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 122 | ftb-=EC_UNIT_BITS; |
Timothy B. Terriberry | f13fea7 | 2007-12-11 13:25:57 +1100 | [diff] [blame] | 123 | ft=(unsigned)(_ft>>ftb)+1; |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 124 | s=ec_decode(_this,ft); |
| 125 | ec_dec_update(_this,s,s+1,ft); |
| 126 | t=t<<EC_UNIT_BITS|s; |
Jean-Marc Valin | a1bb9c7 | 2008-04-28 17:30:26 +1000 | [diff] [blame^] | 127 | t = t<<ftb|ec_dec_bits(_this,ftb); |
| 128 | if (t>_ft) |
| 129 | { |
| 130 | celt_notify("uint decode error"); |
| 131 | t = _ft; |
| 132 | } |
| 133 | return t; |
Jean-Marc Valin | dc767f6 | 2008-03-22 22:23:58 +1100 | [diff] [blame] | 134 | } else { |
| 135 | _ft++; |
| 136 | s=ec_decode(_this,(unsigned)_ft); |
| 137 | ec_dec_update(_this,s,s+1,(unsigned)_ft); |
| 138 | t=t<<ftb|s; |
| 139 | return t; |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 140 | } |
Timothy B. Terriberry | f13fea7 | 2007-12-11 13:25:57 +1100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | ec_uint64 ec_dec_uint64(ec_dec *_this,ec_uint64 _ft){ |
Timothy B. Terriberry | f13fea7 | 2007-12-11 13:25:57 +1100 | [diff] [blame] | 144 | ec_uint64 t; |
| 145 | unsigned ft; |
| 146 | unsigned s; |
| 147 | int ftb; |
| 148 | t=0; |
| 149 | _ft--; |
| 150 | ftb=EC_ILOG64(_ft); |
Jean-Marc Valin | dc767f6 | 2008-03-22 22:23:58 +1100 | [diff] [blame] | 151 | if(ftb>EC_UNIT_BITS){ |
Timothy B. Terriberry | f13fea7 | 2007-12-11 13:25:57 +1100 | [diff] [blame] | 152 | ftb-=EC_UNIT_BITS; |
| 153 | ft=(unsigned)(_ft>>ftb)+1; |
| 154 | s=ec_decode(_this,ft); |
| 155 | ec_dec_update(_this,s,s+1,ft); |
| 156 | t=t<<EC_UNIT_BITS|s; |
Jean-Marc Valin | a1bb9c7 | 2008-04-28 17:30:26 +1000 | [diff] [blame^] | 157 | t = t<<ftb|ec_dec_bits64(_this,ftb); |
| 158 | if (t>_ft) |
| 159 | { |
| 160 | celt_notify("uint decode error"); |
| 161 | t = _ft; |
| 162 | } |
| 163 | return t; |
Jean-Marc Valin | dc767f6 | 2008-03-22 22:23:58 +1100 | [diff] [blame] | 164 | } else { |
| 165 | _ft++; |
| 166 | s=ec_decode(_this,(unsigned)_ft); |
| 167 | ec_dec_update(_this,s,s+1,(unsigned)_ft); |
| 168 | t=t<<ftb|s; |
| 169 | return t; |
Timothy B. Terriberry | f13fea7 | 2007-12-11 13:25:57 +1100 | [diff] [blame] | 170 | } |
Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 171 | } |