blob: 3dee569de623aaf6a7213b17ffda022bae549fd4 [file] [log] [blame]
Jean-Marc Valin02fa9132008-02-20 12:09:29 +11001#ifdef HAVE_CONFIG_H
2#include "config.h"
3#endif
4
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +11005#include <stddef.h>
6#include "entdec.h"
Jean-Marc Valina1bb9c72008-04-28 17:30:26 +10007#include "os_support.h"
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +11008
9
10void ec_byte_readinit(ec_byte_buffer *_b,unsigned char *_buf,long _bytes){
11 _b->buf=_b->ptr=_buf;
12 _b->storage=_bytes;
13}
14
15int 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
22int 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
46void ec_byte_adv1(ec_byte_buffer *_b){
47 _b->ptr++;
48}
49
50void ec_byte_adv4(ec_byte_buffer *_b){
51 _b->ptr+=4;
52}
53
54int 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
61int 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
85ec_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 Valinc2decd32008-03-22 22:58:45 +110091 s=ec_decode_bin(_this,EC_UNIT_BITS);
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110092 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 Valinc2decd32008-03-22 22:58:45 +110097 s=ec_decode_bin(_this,_ftb);
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +110098 ec_dec_update(_this,s,s+1,ft);
99 t=t<<_ftb|s;
100 return t;
101}
102
Timothy B. Terriberryf13fea72007-12-11 13:25:57 +1100103ec_uint64 ec_dec_bits64(ec_dec *_this,int _ftb){
tterribe649d5cc2008-01-17 07:51:18 +0000104 ec_uint32 t;
Timothy B. Terriberryf13fea72007-12-11 13:25:57 +1100105 if(_ftb>32){
tterribe649d5cc2008-01-17 07:51:18 +0000106 t=ec_dec_bits(_this,_ftb-32);
107 _ftb=32;
Timothy B. Terriberryf13fea72007-12-11 13:25:57 +1100108 }
109 else t=0;
tterribe649d5cc2008-01-17 07:51:18 +0000110 return (ec_uint64)t<<32|ec_dec_bits(_this,_ftb);
Timothy B. Terriberryf13fea72007-12-11 13:25:57 +1100111}
112
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +1100113ec_uint32 ec_dec_uint(ec_dec *_this,ec_uint32 _ft){
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +1100114 ec_uint32 t;
Timothy B. Terriberryf13fea72007-12-11 13:25:57 +1100115 unsigned ft;
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +1100116 unsigned s;
117 int ftb;
118 t=0;
119 _ft--;
120 ftb=EC_ILOG(_ft);
Jean-Marc Valindc767f62008-03-22 22:23:58 +1100121 if(ftb>EC_UNIT_BITS){
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +1100122 ftb-=EC_UNIT_BITS;
Timothy B. Terriberryf13fea72007-12-11 13:25:57 +1100123 ft=(unsigned)(_ft>>ftb)+1;
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +1100124 s=ec_decode(_this,ft);
125 ec_dec_update(_this,s,s+1,ft);
126 t=t<<EC_UNIT_BITS|s;
Jean-Marc Valina1bb9c72008-04-28 17:30:26 +1000127 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 Valindc767f62008-03-22 22:23:58 +1100134 } 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. Terriberry2ec8d9e2007-12-06 15:09:53 +1100140 }
Timothy B. Terriberryf13fea72007-12-11 13:25:57 +1100141}
142
143ec_uint64 ec_dec_uint64(ec_dec *_this,ec_uint64 _ft){
Timothy B. Terriberryf13fea72007-12-11 13:25:57 +1100144 ec_uint64 t;
145 unsigned ft;
146 unsigned s;
147 int ftb;
148 t=0;
149 _ft--;
150 ftb=EC_ILOG64(_ft);
Jean-Marc Valindc767f62008-03-22 22:23:58 +1100151 if(ftb>EC_UNIT_BITS){
Timothy B. Terriberryf13fea72007-12-11 13:25:57 +1100152 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 Valina1bb9c72008-04-28 17:30:26 +1000157 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 Valindc767f62008-03-22 22:23:58 +1100164 } 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. Terriberryf13fea72007-12-11 13:25:57 +1100170 }
Timothy B. Terriberry2ec8d9e2007-12-06 15:09:53 +1100171}