annotate cabac.h @ 2268:5b3dfc4103b9 libavcodec

10l (segfault)
author michael
date Mon, 27 Sep 2004 03:19:24 +0000
parents 48d9f86fb047
children 1c39d9786efd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1287
michaelni
parents:
diff changeset
1 /*
michaelni
parents:
diff changeset
2 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
michaelni
parents:
diff changeset
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
michaelni
parents:
diff changeset
4 *
michaelni
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
michaelni
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
michaelni
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
michaelni
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
michaelni
parents:
diff changeset
9 *
michaelni
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
michaelni
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
michaelni
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
michaelni
parents:
diff changeset
13 * Lesser General Public License for more details.
michaelni
parents:
diff changeset
14 *
michaelni
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
michaelni
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
michaelni
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
michaelni
parents:
diff changeset
18 *
michaelni
parents:
diff changeset
19 */
michaelni
parents:
diff changeset
20
michaelni
parents:
diff changeset
21 /**
michaelni
parents:
diff changeset
22 * @file cabac.h
michaelni
parents:
diff changeset
23 * Context Adaptive Binary Arithmetic Coder.
michaelni
parents:
diff changeset
24 */
michaelni
parents:
diff changeset
25
michaelni
parents:
diff changeset
26
michaelni
parents:
diff changeset
27 #undef NDEBUG
michaelni
parents:
diff changeset
28 #include <assert.h>
michaelni
parents:
diff changeset
29
michaelni
parents:
diff changeset
30 typedef struct CABACContext{
michaelni
parents:
diff changeset
31 int low;
michaelni
parents:
diff changeset
32 int range;
michaelni
parents:
diff changeset
33 int outstanding_count;
michaelni
parents:
diff changeset
34 #ifdef STRICT_LIMITS
michaelni
parents:
diff changeset
35 int symCount;
michaelni
parents:
diff changeset
36 #endif
michaelni
parents:
diff changeset
37 uint8_t lps_range[2*64][4]; ///< rangeTabLPS
michaelni
parents:
diff changeset
38 uint8_t lps_state[2*64]; ///< transIdxLPS
michaelni
parents:
diff changeset
39 uint8_t mps_state[2*64]; ///< transIdxMPS
2024
f65d87bfdd5a some of the warning fixes by (Michael Roitzsch <mroi at users dot sourceforge dot net>)
michael
parents: 1787
diff changeset
40 const uint8_t *bytestream_start;
f65d87bfdd5a some of the warning fixes by (Michael Roitzsch <mroi at users dot sourceforge dot net>)
michael
parents: 1787
diff changeset
41 const uint8_t *bytestream;
2116
48d9f86fb047 overread fix
michael
parents: 2024
diff changeset
42 const uint8_t *bytestream_end;
1287
michaelni
parents:
diff changeset
43 int bits_left; ///<
michaelni
parents:
diff changeset
44 PutBitContext pb;
michaelni
parents:
diff changeset
45 }CABACContext;
michaelni
parents:
diff changeset
46
1301
michaelni
parents: 1300
diff changeset
47 extern const uint8_t ff_h264_lps_range[64][4];
michaelni
parents: 1300
diff changeset
48 extern const uint8_t ff_h264_mps_state[64];
michaelni
parents: 1300
diff changeset
49 extern const uint8_t ff_h264_lps_state[64];
1287
michaelni
parents:
diff changeset
50
michaelni
parents:
diff changeset
51 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
2024
f65d87bfdd5a some of the warning fixes by (Michael Roitzsch <mroi at users dot sourceforge dot net>)
michael
parents: 1787
diff changeset
52 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
1287
michaelni
parents:
diff changeset
53 void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4],
michaelni
parents:
diff changeset
54 uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
michaelni
parents:
diff changeset
55
michaelni
parents:
diff changeset
56
michaelni
parents:
diff changeset
57 static inline void put_cabac_bit(CABACContext *c, int b){
michaelni
parents:
diff changeset
58 put_bits(&c->pb, 1, b);
michaelni
parents:
diff changeset
59 for(;c->outstanding_count; c->outstanding_count--){
michaelni
parents:
diff changeset
60 put_bits(&c->pb, 1, 1-b);
michaelni
parents:
diff changeset
61 }
michaelni
parents:
diff changeset
62 }
michaelni
parents:
diff changeset
63
michaelni
parents:
diff changeset
64 static inline void renorm_cabac_encoder(CABACContext *c){
michaelni
parents:
diff changeset
65 while(c->range < 0x100){
michaelni
parents:
diff changeset
66 //FIXME optimize
michaelni
parents:
diff changeset
67 if(c->low<0x100){
michaelni
parents:
diff changeset
68 put_cabac_bit(c, 0);
michaelni
parents:
diff changeset
69 }else if(c->low<0x200){
michaelni
parents:
diff changeset
70 c->outstanding_count++;
michaelni
parents:
diff changeset
71 c->low -= 0x100;
michaelni
parents:
diff changeset
72 }else{
michaelni
parents:
diff changeset
73 put_cabac_bit(c, 1);
michaelni
parents:
diff changeset
74 c->low -= 0x200;
michaelni
parents:
diff changeset
75 }
michaelni
parents:
diff changeset
76
michaelni
parents:
diff changeset
77 c->range+= c->range;
michaelni
parents:
diff changeset
78 c->low += c->low;
michaelni
parents:
diff changeset
79 }
michaelni
parents:
diff changeset
80 }
michaelni
parents:
diff changeset
81
michaelni
parents:
diff changeset
82 static inline void put_cabac(CABACContext *c, uint8_t * const state, int bit){
michaelni
parents:
diff changeset
83 int RangeLPS= c->lps_range[*state][((c->range)>>6)&3];
michaelni
parents:
diff changeset
84
michaelni
parents:
diff changeset
85 if(bit == ((*state)&1)){
michaelni
parents:
diff changeset
86 c->range -= RangeLPS;
michaelni
parents:
diff changeset
87 *state= c->mps_state[*state];
michaelni
parents:
diff changeset
88 }else{
michaelni
parents:
diff changeset
89 c->low += c->range - RangeLPS;
michaelni
parents:
diff changeset
90 c->range = RangeLPS;
michaelni
parents:
diff changeset
91 *state= c->lps_state[*state];
michaelni
parents:
diff changeset
92 }
michaelni
parents:
diff changeset
93
michaelni
parents:
diff changeset
94 renorm_cabac_encoder(c);
michaelni
parents:
diff changeset
95
michaelni
parents:
diff changeset
96 #ifdef STRICT_LIMITS
michaelni
parents:
diff changeset
97 c->symCount++;
michaelni
parents:
diff changeset
98 #endif
michaelni
parents:
diff changeset
99 }
michaelni
parents:
diff changeset
100
michaelni
parents:
diff changeset
101 static inline void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
michaelni
parents:
diff changeset
102 assert(c->range > RangeLPS);
michaelni
parents:
diff changeset
103
michaelni
parents:
diff changeset
104 if(!bit){
michaelni
parents:
diff changeset
105 c->range -= RangeLPS;
michaelni
parents:
diff changeset
106 }else{
michaelni
parents:
diff changeset
107 c->low += c->range - RangeLPS;
michaelni
parents:
diff changeset
108 c->range = RangeLPS;
michaelni
parents:
diff changeset
109 }
michaelni
parents:
diff changeset
110
michaelni
parents:
diff changeset
111 renorm_cabac_encoder(c);
michaelni
parents:
diff changeset
112
michaelni
parents:
diff changeset
113 #ifdef STRICT_LIMITS
michaelni
parents:
diff changeset
114 c->symCount++;
michaelni
parents:
diff changeset
115 #endif
michaelni
parents:
diff changeset
116 }
michaelni
parents:
diff changeset
117
1290
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
118 /**
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
119 * @param bit 0 -> write zero bit, !=0 write one bit
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
120 */
1287
michaelni
parents:
diff changeset
121 static inline void put_cabac_bypass(CABACContext *c, int bit){
michaelni
parents:
diff changeset
122 c->low += c->low;
michaelni
parents:
diff changeset
123
michaelni
parents:
diff changeset
124 if(bit){
michaelni
parents:
diff changeset
125 c->low += c->range;
michaelni
parents:
diff changeset
126 }
michaelni
parents:
diff changeset
127 //FIXME optimize
michaelni
parents:
diff changeset
128 if(c->low<0x200){
michaelni
parents:
diff changeset
129 put_cabac_bit(c, 0);
michaelni
parents:
diff changeset
130 }else if(c->low<0x400){
michaelni
parents:
diff changeset
131 c->outstanding_count++;
michaelni
parents:
diff changeset
132 c->low -= 0x200;
michaelni
parents:
diff changeset
133 }else{
michaelni
parents:
diff changeset
134 put_cabac_bit(c, 1);
michaelni
parents:
diff changeset
135 c->low -= 0x400;
michaelni
parents:
diff changeset
136 }
michaelni
parents:
diff changeset
137
michaelni
parents:
diff changeset
138 #ifdef STRICT_LIMITS
michaelni
parents:
diff changeset
139 c->symCount++;
michaelni
parents:
diff changeset
140 #endif
michaelni
parents:
diff changeset
141 }
michaelni
parents:
diff changeset
142
1300
e18667d1e94d FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents: 1298
diff changeset
143 /**
e18667d1e94d FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents: 1298
diff changeset
144 *
e18667d1e94d FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents: 1298
diff changeset
145 * @return the number of bytes written
e18667d1e94d FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents: 1298
diff changeset
146 */
e18667d1e94d FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents: 1298
diff changeset
147 static inline int put_cabac_terminate(CABACContext *c, int bit){
1287
michaelni
parents:
diff changeset
148 c->range -= 2;
michaelni
parents:
diff changeset
149
michaelni
parents:
diff changeset
150 if(!bit){
michaelni
parents:
diff changeset
151 renorm_cabac_encoder(c);
michaelni
parents:
diff changeset
152 }else{
michaelni
parents:
diff changeset
153 c->low += c->range;
michaelni
parents:
diff changeset
154 c->range= 2;
michaelni
parents:
diff changeset
155
michaelni
parents:
diff changeset
156 renorm_cabac_encoder(c);
michaelni
parents:
diff changeset
157
michaelni
parents:
diff changeset
158 assert(c->low <= 0x1FF);
michaelni
parents:
diff changeset
159 put_cabac_bit(c, c->low>>9);
michaelni
parents:
diff changeset
160 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
michaelni
parents:
diff changeset
161
michaelni
parents:
diff changeset
162 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
michaelni
parents:
diff changeset
163 }
michaelni
parents:
diff changeset
164
michaelni
parents:
diff changeset
165 #ifdef STRICT_LIMITS
michaelni
parents:
diff changeset
166 c->symCount++;
michaelni
parents:
diff changeset
167 #endif
1300
e18667d1e94d FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents: 1298
diff changeset
168
1787
752b51a3c8ed get_bit_count -> put_bits_count
alex
parents: 1301
diff changeset
169 return (put_bits_count(&c->pb)+7)>>3;
1287
michaelni
parents:
diff changeset
170 }
michaelni
parents:
diff changeset
171
1290
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
172 /**
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
173 * put (truncated) unary binarization.
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
174 */
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
175 static inline void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
176 int i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
177
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
178 assert(v <= max);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
179
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
180 #if 1
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
181 for(i=0; i<v; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
182 put_cabac(c, state, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
183 if(i < max_index) state++;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
184 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
185 if(truncated==0 || v<max)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
186 put_cabac(c, state, 0);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
187 #else
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
188 if(v <= max_index){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
189 for(i=0; i<v; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
190 put_cabac(c, state+i, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
191 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
192 if(truncated==0 || v<max)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
193 put_cabac(c, state+i, 0);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
194 }else{
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
195 for(i=0; i<=max_index; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
196 put_cabac(c, state+i, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
197 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
198 for(; i<v; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
199 put_cabac(c, state+max_index, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
200 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
201 if(truncated==0 || v<max)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
202 put_cabac(c, state+max_index, 0);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
203 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
204 #endif
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
205 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
206
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
207 /**
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
208 * put unary exp golomb k-th order binarization.
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
209 */
1298
5bc3184810dc cleanup
michaelni
parents: 1290
diff changeset
210 static inline void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
1290
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
211 int i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
212
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
213 if(v==0)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
214 put_cabac(c, state, 0);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
215 else{
1298
5bc3184810dc cleanup
michaelni
parents: 1290
diff changeset
216 const int sign= v < 0;
5bc3184810dc cleanup
michaelni
parents: 1290
diff changeset
217
5bc3184810dc cleanup
michaelni
parents: 1290
diff changeset
218 if(is_signed) v= ABS(v);
5bc3184810dc cleanup
michaelni
parents: 1290
diff changeset
219
1290
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
220 if(v<max){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
221 for(i=0; i<v; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
222 put_cabac(c, state, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
223 if(i < max_index) state++;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
224 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
225
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
226 put_cabac(c, state, 0);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
227 }else{
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
228 int m= 1<<k;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
229
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
230 for(i=0; i<max; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
231 put_cabac(c, state, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
232 if(i < max_index) state++;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
233 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
234
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
235 v -= max;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
236 while(v >= m){ //FIXME optimize
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
237 put_cabac_bypass(c, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
238 v-= m;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
239 m+= m;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
240 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
241 put_cabac_bypass(c, 0);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
242 while(m>>=1){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
243 put_cabac_bypass(c, v&m);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
244 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
245 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
246
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
247 if(is_signed)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
248 put_cabac_bypass(c, sign);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
249 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
250 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
251
1287
michaelni
parents:
diff changeset
252 static inline void renorm_cabac_decoder(CABACContext *c){
michaelni
parents:
diff changeset
253 while(c->range < 0x10000){
michaelni
parents:
diff changeset
254 c->range+= c->range;
michaelni
parents:
diff changeset
255 c->low+= c->low;
michaelni
parents:
diff changeset
256 if(--c->bits_left == 0){
2116
48d9f86fb047 overread fix
michael
parents: 2024
diff changeset
257 if(c->bytestream < c->bytestream_end)
48d9f86fb047 overread fix
michael
parents: 2024
diff changeset
258 c->low+= *c->bytestream;
48d9f86fb047 overread fix
michael
parents: 2024
diff changeset
259 c->bytestream++;
1287
michaelni
parents:
diff changeset
260 c->bits_left= 8;
michaelni
parents:
diff changeset
261 }
michaelni
parents:
diff changeset
262 }
michaelni
parents:
diff changeset
263 }
michaelni
parents:
diff changeset
264
michaelni
parents:
diff changeset
265 static inline int get_cabac(CABACContext *c, uint8_t * const state){
michaelni
parents:
diff changeset
266 int RangeLPS= c->lps_range[*state][((c->range)>>14)&3]<<8;
michaelni
parents:
diff changeset
267 int bit;
michaelni
parents:
diff changeset
268
michaelni
parents:
diff changeset
269 c->range -= RangeLPS;
michaelni
parents:
diff changeset
270 if(c->low < c->range){
michaelni
parents:
diff changeset
271 bit= (*state)&1;
michaelni
parents:
diff changeset
272 *state= c->mps_state[*state];
michaelni
parents:
diff changeset
273 }else{
michaelni
parents:
diff changeset
274 bit= ((*state)&1)^1;
michaelni
parents:
diff changeset
275 c->low -= c->range;
michaelni
parents:
diff changeset
276 c->range = RangeLPS;
michaelni
parents:
diff changeset
277 *state= c->lps_state[*state];
michaelni
parents:
diff changeset
278 }
michaelni
parents:
diff changeset
279 renorm_cabac_decoder(c);
michaelni
parents:
diff changeset
280
michaelni
parents:
diff changeset
281 return bit;
michaelni
parents:
diff changeset
282 }
michaelni
parents:
diff changeset
283
michaelni
parents:
diff changeset
284 static inline int get_cabac_static(CABACContext *c, int RangeLPS){
michaelni
parents:
diff changeset
285 int bit;
michaelni
parents:
diff changeset
286
michaelni
parents:
diff changeset
287 c->range -= RangeLPS;
michaelni
parents:
diff changeset
288 if(c->low < c->range){
michaelni
parents:
diff changeset
289 bit= 0;
michaelni
parents:
diff changeset
290 }else{
michaelni
parents:
diff changeset
291 bit= 1;
michaelni
parents:
diff changeset
292 c->low -= c->range;
michaelni
parents:
diff changeset
293 c->range = RangeLPS;
michaelni
parents:
diff changeset
294 }
michaelni
parents:
diff changeset
295 renorm_cabac_decoder(c);
michaelni
parents:
diff changeset
296
michaelni
parents:
diff changeset
297 return bit;
michaelni
parents:
diff changeset
298 }
michaelni
parents:
diff changeset
299
michaelni
parents:
diff changeset
300 static inline int get_cabac_bypass(CABACContext *c){
michaelni
parents:
diff changeset
301 c->low += c->low;
michaelni
parents:
diff changeset
302
michaelni
parents:
diff changeset
303 if(--c->bits_left == 0){
2116
48d9f86fb047 overread fix
michael
parents: 2024
diff changeset
304 if(c->bytestream < c->bytestream_end)
48d9f86fb047 overread fix
michael
parents: 2024
diff changeset
305 c->low+= *c->bytestream;
48d9f86fb047 overread fix
michael
parents: 2024
diff changeset
306 c->bytestream++;
1287
michaelni
parents:
diff changeset
307 c->bits_left= 8;
michaelni
parents:
diff changeset
308 }
michaelni
parents:
diff changeset
309
michaelni
parents:
diff changeset
310 if(c->low < c->range){
michaelni
parents:
diff changeset
311 return 0;
michaelni
parents:
diff changeset
312 }else{
michaelni
parents:
diff changeset
313 c->low -= c->range;
michaelni
parents:
diff changeset
314 return 1;
michaelni
parents:
diff changeset
315 }
michaelni
parents:
diff changeset
316 }
michaelni
parents:
diff changeset
317
1300
e18667d1e94d FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents: 1298
diff changeset
318 /**
e18667d1e94d FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents: 1298
diff changeset
319 *
e18667d1e94d FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents: 1298
diff changeset
320 * @return the number of bytes read or 0 if no end
e18667d1e94d FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents: 1298
diff changeset
321 */
1287
michaelni
parents:
diff changeset
322 static inline int get_cabac_terminate(CABACContext *c){
michaelni
parents:
diff changeset
323 c->range -= 2<<8;
michaelni
parents:
diff changeset
324 if(c->low < c->range){
michaelni
parents:
diff changeset
325 renorm_cabac_decoder(c);
michaelni
parents:
diff changeset
326 return 0;
michaelni
parents:
diff changeset
327 }else{
1300
e18667d1e94d FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents: 1298
diff changeset
328 return c->bytestream - c->bytestream_start;
1287
michaelni
parents:
diff changeset
329 }
michaelni
parents:
diff changeset
330 }
michaelni
parents:
diff changeset
331
1290
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
332 /**
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
333 * get (truncated) unnary binarization.
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
334 */
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
335 static inline int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
336 int i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
337
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
338 for(i=0; i<max; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
339 if(get_cabac(c, state)==0)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
340 return i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
341
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
342 if(i< max_index) state++;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
343 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
344
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
345 return truncated ? max : -1;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
346 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
347
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
348 /**
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
349 * get unary exp golomb k-th order binarization.
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
350 */
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
351 static inline int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
352 int i, v;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
353 int m= 1<<k;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
354
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
355 if(get_cabac(c, state)==0)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
356 return 0;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
357
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
358 if(0 < max_index) state++;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
359
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
360 for(i=1; i<max; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
361 if(get_cabac(c, state)==0){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
362 if(is_signed && get_cabac_bypass(c)){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
363 return -i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
364 }else
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
365 return i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
366 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
367
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
368 if(i < max_index) state++;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
369 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
370
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
371 while(get_cabac_bypass(c)){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
372 i+= m;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
373 m+= m;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
374 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
375
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
376 v=0;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
377 while(m>>=1){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
378 v+= v + get_cabac_bypass(c);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
379 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
380 i += v;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
381
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
382 if(is_signed && get_cabac_bypass(c)){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
383 return -i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
384 }else
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
385 return i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
386 }