annotate cabac.h @ 2115:4ea05f23730b libavcodec

dc_precission=11 fix
author michael
date Tue, 06 Jul 2004 12:27:36 +0000
parents f65d87bfdd5a
children 48d9f86fb047
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;
1287
michaelni
parents:
diff changeset
42 int bits_left; ///<
michaelni
parents:
diff changeset
43 PutBitContext pb;
michaelni
parents:
diff changeset
44 }CABACContext;
michaelni
parents:
diff changeset
45
1301
michaelni
parents: 1300
diff changeset
46 extern const uint8_t ff_h264_lps_range[64][4];
michaelni
parents: 1300
diff changeset
47 extern const uint8_t ff_h264_mps_state[64];
michaelni
parents: 1300
diff changeset
48 extern const uint8_t ff_h264_lps_state[64];
1287
michaelni
parents:
diff changeset
49
michaelni
parents:
diff changeset
50 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
51 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
1287
michaelni
parents:
diff changeset
52 void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4],
michaelni
parents:
diff changeset
53 uint8_t const *mps_state, uint8_t const *lps_state, int state_count);
michaelni
parents:
diff changeset
54
michaelni
parents:
diff changeset
55
michaelni
parents:
diff changeset
56 static inline void put_cabac_bit(CABACContext *c, int b){
michaelni
parents:
diff changeset
57 put_bits(&c->pb, 1, b);
michaelni
parents:
diff changeset
58 for(;c->outstanding_count; c->outstanding_count--){
michaelni
parents:
diff changeset
59 put_bits(&c->pb, 1, 1-b);
michaelni
parents:
diff changeset
60 }
michaelni
parents:
diff changeset
61 }
michaelni
parents:
diff changeset
62
michaelni
parents:
diff changeset
63 static inline void renorm_cabac_encoder(CABACContext *c){
michaelni
parents:
diff changeset
64 while(c->range < 0x100){
michaelni
parents:
diff changeset
65 //FIXME optimize
michaelni
parents:
diff changeset
66 if(c->low<0x100){
michaelni
parents:
diff changeset
67 put_cabac_bit(c, 0);
michaelni
parents:
diff changeset
68 }else if(c->low<0x200){
michaelni
parents:
diff changeset
69 c->outstanding_count++;
michaelni
parents:
diff changeset
70 c->low -= 0x100;
michaelni
parents:
diff changeset
71 }else{
michaelni
parents:
diff changeset
72 put_cabac_bit(c, 1);
michaelni
parents:
diff changeset
73 c->low -= 0x200;
michaelni
parents:
diff changeset
74 }
michaelni
parents:
diff changeset
75
michaelni
parents:
diff changeset
76 c->range+= c->range;
michaelni
parents:
diff changeset
77 c->low += c->low;
michaelni
parents:
diff changeset
78 }
michaelni
parents:
diff changeset
79 }
michaelni
parents:
diff changeset
80
michaelni
parents:
diff changeset
81 static inline void put_cabac(CABACContext *c, uint8_t * const state, int bit){
michaelni
parents:
diff changeset
82 int RangeLPS= c->lps_range[*state][((c->range)>>6)&3];
michaelni
parents:
diff changeset
83
michaelni
parents:
diff changeset
84 if(bit == ((*state)&1)){
michaelni
parents:
diff changeset
85 c->range -= RangeLPS;
michaelni
parents:
diff changeset
86 *state= c->mps_state[*state];
michaelni
parents:
diff changeset
87 }else{
michaelni
parents:
diff changeset
88 c->low += c->range - RangeLPS;
michaelni
parents:
diff changeset
89 c->range = RangeLPS;
michaelni
parents:
diff changeset
90 *state= c->lps_state[*state];
michaelni
parents:
diff changeset
91 }
michaelni
parents:
diff changeset
92
michaelni
parents:
diff changeset
93 renorm_cabac_encoder(c);
michaelni
parents:
diff changeset
94
michaelni
parents:
diff changeset
95 #ifdef STRICT_LIMITS
michaelni
parents:
diff changeset
96 c->symCount++;
michaelni
parents:
diff changeset
97 #endif
michaelni
parents:
diff changeset
98 }
michaelni
parents:
diff changeset
99
michaelni
parents:
diff changeset
100 static inline void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
michaelni
parents:
diff changeset
101 assert(c->range > RangeLPS);
michaelni
parents:
diff changeset
102
michaelni
parents:
diff changeset
103 if(!bit){
michaelni
parents:
diff changeset
104 c->range -= RangeLPS;
michaelni
parents:
diff changeset
105 }else{
michaelni
parents:
diff changeset
106 c->low += c->range - RangeLPS;
michaelni
parents:
diff changeset
107 c->range = RangeLPS;
michaelni
parents:
diff changeset
108 }
michaelni
parents:
diff changeset
109
michaelni
parents:
diff changeset
110 renorm_cabac_encoder(c);
michaelni
parents:
diff changeset
111
michaelni
parents:
diff changeset
112 #ifdef STRICT_LIMITS
michaelni
parents:
diff changeset
113 c->symCount++;
michaelni
parents:
diff changeset
114 #endif
michaelni
parents:
diff changeset
115 }
michaelni
parents:
diff changeset
116
1290
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
117 /**
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
118 * @param bit 0 -> write zero bit, !=0 write one bit
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
119 */
1287
michaelni
parents:
diff changeset
120 static inline void put_cabac_bypass(CABACContext *c, int bit){
michaelni
parents:
diff changeset
121 c->low += c->low;
michaelni
parents:
diff changeset
122
michaelni
parents:
diff changeset
123 if(bit){
michaelni
parents:
diff changeset
124 c->low += c->range;
michaelni
parents:
diff changeset
125 }
michaelni
parents:
diff changeset
126 //FIXME optimize
michaelni
parents:
diff changeset
127 if(c->low<0x200){
michaelni
parents:
diff changeset
128 put_cabac_bit(c, 0);
michaelni
parents:
diff changeset
129 }else if(c->low<0x400){
michaelni
parents:
diff changeset
130 c->outstanding_count++;
michaelni
parents:
diff changeset
131 c->low -= 0x200;
michaelni
parents:
diff changeset
132 }else{
michaelni
parents:
diff changeset
133 put_cabac_bit(c, 1);
michaelni
parents:
diff changeset
134 c->low -= 0x400;
michaelni
parents:
diff changeset
135 }
michaelni
parents:
diff changeset
136
michaelni
parents:
diff changeset
137 #ifdef STRICT_LIMITS
michaelni
parents:
diff changeset
138 c->symCount++;
michaelni
parents:
diff changeset
139 #endif
michaelni
parents:
diff changeset
140 }
michaelni
parents:
diff changeset
141
1300
e18667d1e94d FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents: 1298
diff changeset
142 /**
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 * @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
145 */
e18667d1e94d FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents: 1298
diff changeset
146 static inline int put_cabac_terminate(CABACContext *c, int bit){
1287
michaelni
parents:
diff changeset
147 c->range -= 2;
michaelni
parents:
diff changeset
148
michaelni
parents:
diff changeset
149 if(!bit){
michaelni
parents:
diff changeset
150 renorm_cabac_encoder(c);
michaelni
parents:
diff changeset
151 }else{
michaelni
parents:
diff changeset
152 c->low += c->range;
michaelni
parents:
diff changeset
153 c->range= 2;
michaelni
parents:
diff changeset
154
michaelni
parents:
diff changeset
155 renorm_cabac_encoder(c);
michaelni
parents:
diff changeset
156
michaelni
parents:
diff changeset
157 assert(c->low <= 0x1FF);
michaelni
parents:
diff changeset
158 put_cabac_bit(c, c->low>>9);
michaelni
parents:
diff changeset
159 put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
michaelni
parents:
diff changeset
160
michaelni
parents:
diff changeset
161 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
michaelni
parents:
diff changeset
162 }
michaelni
parents:
diff changeset
163
michaelni
parents:
diff changeset
164 #ifdef STRICT_LIMITS
michaelni
parents:
diff changeset
165 c->symCount++;
michaelni
parents:
diff changeset
166 #endif
1300
e18667d1e94d FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents: 1298
diff changeset
167
1787
752b51a3c8ed get_bit_count -> put_bits_count
alex
parents: 1301
diff changeset
168 return (put_bits_count(&c->pb)+7)>>3;
1287
michaelni
parents:
diff changeset
169 }
michaelni
parents:
diff changeset
170
1290
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
171 /**
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
172 * put (truncated) unary binarization.
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
173 */
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
174 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
175 int i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
176
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
177 assert(v <= max);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
178
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
179 #if 1
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
180 for(i=0; i<v; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
181 put_cabac(c, state, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
182 if(i < max_index) state++;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
183 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
184 if(truncated==0 || v<max)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
185 put_cabac(c, state, 0);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
186 #else
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
187 if(v <= max_index){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
188 for(i=0; i<v; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
189 put_cabac(c, state+i, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
190 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
191 if(truncated==0 || v<max)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
192 put_cabac(c, state+i, 0);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
193 }else{
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
194 for(i=0; i<=max_index; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
195 put_cabac(c, state+i, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
196 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
197 for(; i<v; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
198 put_cabac(c, state+max_index, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
199 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
200 if(truncated==0 || v<max)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
201 put_cabac(c, state+max_index, 0);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
202 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
203 #endif
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
204 }
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 * put unary exp golomb k-th order binarization.
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
208 */
1298
5bc3184810dc cleanup
michaelni
parents: 1290
diff changeset
209 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
210 int i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
211
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
212 if(v==0)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
213 put_cabac(c, state, 0);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
214 else{
1298
5bc3184810dc cleanup
michaelni
parents: 1290
diff changeset
215 const int sign= v < 0;
5bc3184810dc cleanup
michaelni
parents: 1290
diff changeset
216
5bc3184810dc cleanup
michaelni
parents: 1290
diff changeset
217 if(is_signed) v= ABS(v);
5bc3184810dc cleanup
michaelni
parents: 1290
diff changeset
218
1290
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
219 if(v<max){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
220 for(i=0; i<v; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
221 put_cabac(c, state, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
222 if(i < max_index) state++;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
223 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
224
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
225 put_cabac(c, state, 0);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
226 }else{
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
227 int m= 1<<k;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
228
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
229 for(i=0; i<max; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
230 put_cabac(c, state, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
231 if(i < max_index) state++;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
232 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
233
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
234 v -= max;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
235 while(v >= m){ //FIXME optimize
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
236 put_cabac_bypass(c, 1);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
237 v-= m;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
238 m+= m;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
239 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
240 put_cabac_bypass(c, 0);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
241 while(m>>=1){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
242 put_cabac_bypass(c, v&m);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
243 }
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 if(is_signed)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
247 put_cabac_bypass(c, sign);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
248 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
249 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
250
1287
michaelni
parents:
diff changeset
251 static inline void renorm_cabac_decoder(CABACContext *c){
michaelni
parents:
diff changeset
252 while(c->range < 0x10000){
michaelni
parents:
diff changeset
253 c->range+= c->range;
michaelni
parents:
diff changeset
254 c->low+= c->low;
michaelni
parents:
diff changeset
255 if(--c->bits_left == 0){
michaelni
parents:
diff changeset
256 c->low+= *c->bytestream++;
michaelni
parents:
diff changeset
257 c->bits_left= 8;
michaelni
parents:
diff changeset
258 }
michaelni
parents:
diff changeset
259 }
michaelni
parents:
diff changeset
260 }
michaelni
parents:
diff changeset
261
michaelni
parents:
diff changeset
262 static inline int get_cabac(CABACContext *c, uint8_t * const state){
michaelni
parents:
diff changeset
263 int RangeLPS= c->lps_range[*state][((c->range)>>14)&3]<<8;
michaelni
parents:
diff changeset
264 int bit;
michaelni
parents:
diff changeset
265
michaelni
parents:
diff changeset
266 c->range -= RangeLPS;
michaelni
parents:
diff changeset
267 if(c->low < c->range){
michaelni
parents:
diff changeset
268 bit= (*state)&1;
michaelni
parents:
diff changeset
269 *state= c->mps_state[*state];
michaelni
parents:
diff changeset
270 }else{
michaelni
parents:
diff changeset
271 bit= ((*state)&1)^1;
michaelni
parents:
diff changeset
272 c->low -= c->range;
michaelni
parents:
diff changeset
273 c->range = RangeLPS;
michaelni
parents:
diff changeset
274 *state= c->lps_state[*state];
michaelni
parents:
diff changeset
275 }
michaelni
parents:
diff changeset
276 renorm_cabac_decoder(c);
michaelni
parents:
diff changeset
277
michaelni
parents:
diff changeset
278 return bit;
michaelni
parents:
diff changeset
279 }
michaelni
parents:
diff changeset
280
michaelni
parents:
diff changeset
281 static inline int get_cabac_static(CABACContext *c, int RangeLPS){
michaelni
parents:
diff changeset
282 int bit;
michaelni
parents:
diff changeset
283
michaelni
parents:
diff changeset
284 c->range -= RangeLPS;
michaelni
parents:
diff changeset
285 if(c->low < c->range){
michaelni
parents:
diff changeset
286 bit= 0;
michaelni
parents:
diff changeset
287 }else{
michaelni
parents:
diff changeset
288 bit= 1;
michaelni
parents:
diff changeset
289 c->low -= c->range;
michaelni
parents:
diff changeset
290 c->range = RangeLPS;
michaelni
parents:
diff changeset
291 }
michaelni
parents:
diff changeset
292 renorm_cabac_decoder(c);
michaelni
parents:
diff changeset
293
michaelni
parents:
diff changeset
294 return bit;
michaelni
parents:
diff changeset
295 }
michaelni
parents:
diff changeset
296
michaelni
parents:
diff changeset
297 static inline int get_cabac_bypass(CABACContext *c){
michaelni
parents:
diff changeset
298 c->low += c->low;
michaelni
parents:
diff changeset
299
michaelni
parents:
diff changeset
300 if(--c->bits_left == 0){
michaelni
parents:
diff changeset
301 c->low+= *c->bytestream++;
michaelni
parents:
diff changeset
302 c->bits_left= 8;
michaelni
parents:
diff changeset
303 }
michaelni
parents:
diff changeset
304
michaelni
parents:
diff changeset
305 if(c->low < c->range){
michaelni
parents:
diff changeset
306 return 0;
michaelni
parents:
diff changeset
307 }else{
michaelni
parents:
diff changeset
308 c->low -= c->range;
michaelni
parents:
diff changeset
309 return 1;
michaelni
parents:
diff changeset
310 }
michaelni
parents:
diff changeset
311 }
michaelni
parents:
diff changeset
312
1300
e18667d1e94d FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents: 1298
diff changeset
313 /**
e18667d1e94d FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents: 1298
diff changeset
314 *
e18667d1e94d FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents: 1298
diff changeset
315 * @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
316 */
1287
michaelni
parents:
diff changeset
317 static inline int get_cabac_terminate(CABACContext *c){
michaelni
parents:
diff changeset
318 c->range -= 2<<8;
michaelni
parents:
diff changeset
319 if(c->low < c->range){
michaelni
parents:
diff changeset
320 renorm_cabac_decoder(c);
michaelni
parents:
diff changeset
321 return 0;
michaelni
parents:
diff changeset
322 }else{
1300
e18667d1e94d FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents: 1298
diff changeset
323 return c->bytestream - c->bytestream_start;
1287
michaelni
parents:
diff changeset
324 }
michaelni
parents:
diff changeset
325 }
michaelni
parents:
diff changeset
326
1290
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
327 /**
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
328 * get (truncated) unnary binarization.
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
329 */
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
330 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
331 int i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
332
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
333 for(i=0; i<max; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
334 if(get_cabac(c, state)==0)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
335 return i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
336
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
337 if(i< max_index) state++;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
338 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
339
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
340 return truncated ? max : -1;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
341 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
342
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
343 /**
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
344 * get unary exp golomb k-th order binarization.
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
345 */
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
346 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
347 int i, v;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
348 int m= 1<<k;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
349
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
350 if(get_cabac(c, state)==0)
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
351 return 0;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
352
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
353 if(0 < max_index) state++;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
354
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
355 for(i=1; i<max; i++){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
356 if(get_cabac(c, state)==0){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
357 if(is_signed && get_cabac_bypass(c)){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
358 return -i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
359 }else
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
360 return i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
361 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
362
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
363 if(i < max_index) state++;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
364 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
365
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
366 while(get_cabac_bypass(c)){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
367 i+= m;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
368 m+= m;
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 v=0;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
372 while(m>>=1){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
373 v+= v + get_cabac_bypass(c);
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
374 }
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
375 i += v;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
376
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
377 if(is_signed && get_cabac_bypass(c)){
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
378 return -i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
379 }else
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
380 return i;
dae280b939ca (truncated) unary binerization
michaelni
parents: 1287
diff changeset
381 }