1 
2 #ifndef _SCHRO_ARITH_H_
3 #define _SCHRO_ARITH_H_
4 
5 #include <schroedinger/schroutils.h>
6 #include <schroedinger/schrobuffer.h>
7 
8 SCHRO_BEGIN_DECLS
9 
10 #ifdef SCHRO_ENABLE_UNSTABLE_API
11 
12 enum {
13   SCHRO_CTX_ZERO_CODEBLOCK = 0,
14   SCHRO_CTX_QUANTISER_CONT,
15   SCHRO_CTX_QUANTISER_VALUE,
16   SCHRO_CTX_QUANTISER_SIGN,
17   SCHRO_CTX_ZPZN_F1,
18   SCHRO_CTX_ZPNN_F1,
19   SCHRO_CTX_ZP_F2,
20   SCHRO_CTX_ZP_F3,
21   SCHRO_CTX_ZP_F4,
22   SCHRO_CTX_ZP_F5,
23   SCHRO_CTX_ZP_F6p,
24   SCHRO_CTX_NPZN_F1,
25   SCHRO_CTX_NPNN_F1,
26   SCHRO_CTX_NP_F2,
27   SCHRO_CTX_NP_F3,
28   SCHRO_CTX_NP_F4,
29   SCHRO_CTX_NP_F5,
30   SCHRO_CTX_NP_F6p,
31   SCHRO_CTX_SIGN_POS,
32   SCHRO_CTX_SIGN_NEG,
33   SCHRO_CTX_SIGN_ZERO,
34   SCHRO_CTX_COEFF_DATA,
35 
36   SCHRO_CTX_SB_F1,
37   SCHRO_CTX_SB_F2,
38   SCHRO_CTX_SB_DATA,
39   SCHRO_CTX_BLOCK_MODE_REF1,
40   SCHRO_CTX_BLOCK_MODE_REF2,
41   SCHRO_CTX_GLOBAL_BLOCK,
42   SCHRO_CTX_LUMA_DC_CONT_BIN1,
43   SCHRO_CTX_LUMA_DC_CONT_BIN2,
44   SCHRO_CTX_LUMA_DC_VALUE,
45   SCHRO_CTX_LUMA_DC_SIGN,
46   SCHRO_CTX_CHROMA1_DC_CONT_BIN1,
47   SCHRO_CTX_CHROMA1_DC_CONT_BIN2,
48   SCHRO_CTX_CHROMA1_DC_VALUE,
49   SCHRO_CTX_CHROMA1_DC_SIGN,
50   SCHRO_CTX_CHROMA2_DC_CONT_BIN1,
51   SCHRO_CTX_CHROMA2_DC_CONT_BIN2,
52   SCHRO_CTX_CHROMA2_DC_VALUE,
53   SCHRO_CTX_CHROMA2_DC_SIGN,
54   SCHRO_CTX_MV_REF1_H_CONT_BIN1,
55   SCHRO_CTX_MV_REF1_H_CONT_BIN2,
56   SCHRO_CTX_MV_REF1_H_CONT_BIN3,
57   SCHRO_CTX_MV_REF1_H_CONT_BIN4,
58   SCHRO_CTX_MV_REF1_H_CONT_BIN5,
59   SCHRO_CTX_MV_REF1_H_VALUE,
60   SCHRO_CTX_MV_REF1_H_SIGN,
61   SCHRO_CTX_MV_REF1_V_CONT_BIN1,
62   SCHRO_CTX_MV_REF1_V_CONT_BIN2,
63   SCHRO_CTX_MV_REF1_V_CONT_BIN3,
64   SCHRO_CTX_MV_REF1_V_CONT_BIN4,
65   SCHRO_CTX_MV_REF1_V_CONT_BIN5,
66   SCHRO_CTX_MV_REF1_V_VALUE,
67   SCHRO_CTX_MV_REF1_V_SIGN,
68   SCHRO_CTX_MV_REF2_H_CONT_BIN1,
69   SCHRO_CTX_MV_REF2_H_CONT_BIN2,
70   SCHRO_CTX_MV_REF2_H_CONT_BIN3,
71   SCHRO_CTX_MV_REF2_H_CONT_BIN4,
72   SCHRO_CTX_MV_REF2_H_CONT_BIN5,
73   SCHRO_CTX_MV_REF2_H_VALUE,
74   SCHRO_CTX_MV_REF2_H_SIGN,
75   SCHRO_CTX_MV_REF2_V_CONT_BIN1,
76   SCHRO_CTX_MV_REF2_V_CONT_BIN2,
77   SCHRO_CTX_MV_REF2_V_CONT_BIN3,
78   SCHRO_CTX_MV_REF2_V_CONT_BIN4,
79   SCHRO_CTX_MV_REF2_V_CONT_BIN5,
80   SCHRO_CTX_MV_REF2_V_VALUE,
81   SCHRO_CTX_MV_REF2_V_SIGN,
82 
83   SCHRO_CTX_LAST
84 };
85 
86 
87 typedef struct _SchroArith SchroArith;
88 typedef struct _SchroArithContext SchroArithContext;
89 
90 struct _SchroArithContext {
91   unsigned int next;
92 #ifdef unused
93   int stat_range;
94   int n_bits;
95   int n_symbols;
96   int ones;
97 #endif
98 };
99 
100 struct _SchroArith {
101   SchroBuffer *buffer;
102   uint8_t *dataptr;
103   uintptr_t offset;
104 
105   uint32_t range[2];
106   uint32_t code;
107   uint32_t range_size;
108   int cntr;
109   int carry;
110 
111   uint16_t probabilities[SCHRO_CTX_LAST];
112   uint16_t lut[512];
113   SchroArithContext contexts[SCHRO_CTX_LAST];
114 };
115 
116 SchroArith * schro_arith_new (void);
117 void schro_arith_free (SchroArith *arith);
118 void schro_arith_decode_init (SchroArith *arith, SchroBuffer *buffer);
119 void schro_arith_encode_init (SchroArith *arith, SchroBuffer *buffer);
120 void schro_arith_estimate_init (SchroArith *arith);
121 void schro_arith_flush (SchroArith *arith);
122 void schro_arith_decode_flush (SchroArith *arith);
123 
124 void schro_arith_encode_bit (SchroArith *arith, int context, int value);
125 void schro_arith_encode_uint (SchroArith *arith, int cont_context,
126     int value_context, int value);
127 void schro_arith_encode_sint (SchroArith *arith, int cont_context,
128     int value_context, int sign_context, int value);
129 
130 int schro_arith_decode_bit (SchroArith *arith, unsigned int context);
131 int schro_arith_decode_uint (SchroArith *arith, unsigned int cont_context,
132     unsigned int value_context);
133 int schro_arith_decode_sint (SchroArith *arith, unsigned int cont_context,
134     unsigned int value_context, unsigned int sign_context);
135 
136 int _schro_arith_decode_sint (SchroArith *arith, unsigned int cont_context,
137     unsigned int value_context, unsigned int sign_context) SCHRO_INTERNAL;
138 
139 void schro_arith_estimate_bit (SchroArith *arith, int i, int value);
140 void schro_arith_estimate_uint (SchroArith *arith, int cont_context,
141     int value_context, int value);
142 void schro_arith_estimate_sint (SchroArith *arith, int cont_context,
143     int value_context, int sign_context, int value);
144 
145 #ifdef SCHRO_ARITH_DEFINE_INLINE
146 static inline int
_schro_arith_decode_bit(SchroArith * arith,unsigned int i)147 _schro_arith_decode_bit (SchroArith *arith, unsigned int i)
148 {
149   unsigned int range_x_prob;
150   unsigned int value;
151   unsigned int lut_index;
152   register unsigned int range = arith->range[1];
153   register unsigned int code_minus_low = arith->code;
154 
155   while (range <= 0x40000000) {
156 
157     range <<= 1;
158     code_minus_low <<= 1;
159 
160     if (!--arith->cntr) {
161       arith->offset++;
162       if (arith->offset < arith->buffer->length) {
163         code_minus_low |= arith->dataptr[arith->offset] << 8;
164       } else {
165         code_minus_low |= 0xff00;
166       }
167 
168       arith->offset++;
169       if (arith->offset < arith->buffer->length) {
170         code_minus_low |= arith->dataptr[arith->offset];
171       } else {
172         code_minus_low |= 0xff;
173       }
174 
175       arith->cntr = 16;
176     }
177   }
178 
179   range_x_prob = ((range >> 16) * arith->probabilities[i]) & 0xFFFF0000;
180   lut_index = arith->probabilities[i]>>7 & ~1;
181 
182   value = (code_minus_low  >= range_x_prob);
183   arith->probabilities[i] += arith->lut[lut_index | value];
184 
185   if (value) {
186     code_minus_low -= range_x_prob;
187     range -= range_x_prob;
188   } else {
189     range = range_x_prob;
190   }
191 
192   arith->range[1] = range;
193   arith->code = code_minus_low;
194 
195   return value;
196 }
197 
198 static inline int
_schro_arith_decode_uint(SchroArith * arith,unsigned int cont_context,unsigned int value_context)199 _schro_arith_decode_uint (SchroArith *arith, unsigned int cont_context,
200     unsigned int value_context)
201 {
202   unsigned int bits=1;
203 
204   while(!_schro_arith_decode_bit (arith, cont_context)) {
205     bits <<= 1;
206     bits |= _schro_arith_decode_bit (arith, value_context);
207     cont_context = arith->contexts[cont_context].next;
208   }
209   return bits - 1;
210 }
211 
212 static inline void
_schro_arith_encode_bit(SchroArith * arith,int i,int value)213 _schro_arith_encode_bit (SchroArith *arith, int i, int value)
214 {
215   unsigned int range;
216   unsigned int probability0;
217   unsigned int range_x_prob;
218 
219   probability0 = arith->probabilities[i];
220   range = arith->range[1];
221   range_x_prob = (range * probability0) >> 16;
222 
223   if (value) {
224     arith->range[0] = arith->range[0] + range_x_prob;
225     arith->range[1] -= range_x_prob;
226     arith->probabilities[i] -= arith->lut[arith->probabilities[i]>>8];
227   } else {
228     arith->range[1] = range_x_prob;
229     arith->probabilities[i] += arith->lut[255-(arith->probabilities[i]>>8)];
230   }
231 
232   while (arith->range[1] <= 0x4000) {
233     arith->range[0] <<= 1;
234     arith->range[1] <<= 1;
235     arith->cntr++;
236 
237     if (arith->cntr == 8) {
238       if (arith->range[0] < (1<<24) &&
239           (arith->range[0] + arith->range[1]) >= (1<<24)) {
240         arith->carry++;
241       } else {
242         if (arith->range[0] >= (1<<24)) {
243           arith->dataptr[arith->offset-1]++;
244           while (arith->carry) {
245             arith->dataptr[arith->offset] = 0x00;
246             arith->carry--;
247             arith->offset++;
248           }
249         } else {
250           while (arith->carry) {
251             arith->dataptr[arith->offset] = 0xff;
252             arith->carry--;
253             arith->offset++;
254           }
255         }
256         arith->dataptr[arith->offset] = arith->range[0] >> 16;
257         arith->offset++;
258       }
259 
260       arith->range[0] &= 0xffff;
261       arith->cntr = 0;
262     }
263   }
264 }
265 
266 static inline int
maxbit(unsigned int x)267 maxbit (unsigned int x)
268 {
269 #if 0
270   int i;
271   for(i=0;x;i++){
272     x >>= 1;
273   }
274   return i;
275 #else
276   int i = 0;
277   if (x == 0) return 0;
278   if (x > 0x00ff) {
279     i += 8;
280     x >>= 8;
281   }
282   if (x > 0x000f) {
283     i += 4;
284     x >>= 4;
285   }
286   if (x > 0x0003) {
287     i += 2;
288     x >>= 2;
289   }
290   if (x > 0x0001) {
291     i += 1;
292     x >>= 1;
293   }
294   if (x > 0x0000) {
295     i += 1;
296   }
297   return i;
298 #endif
299 }
300 
301 static inline void
_schro_arith_encode_uint(SchroArith * arith,int cont_context,int value_context,int value)302 _schro_arith_encode_uint (SchroArith *arith, int cont_context,
303     int value_context, int value)
304 {
305   int i;
306   int n_bits;
307 
308   value++;
309   n_bits = maxbit(value);
310   for(i=0;i<n_bits - 1;i++){
311     _schro_arith_encode_bit (arith, cont_context, 0);
312     _schro_arith_encode_bit (arith, value_context,
313         (value>>(n_bits - 2 - i))&1);
314     cont_context = arith->contexts[cont_context].next;
315   }
316   _schro_arith_encode_bit (arith, cont_context, 1);
317 }
318 
319 static inline void
_schro_arith_encode_sint(SchroArith * arith,int cont_context,int value_context,int sign_context,int value)320 _schro_arith_encode_sint (SchroArith *arith, int cont_context,
321     int value_context, int sign_context, int value)
322 {
323   int sign;
324 
325   if (value < 0) {
326     sign = 1;
327     value = -value;
328   } else {
329     sign = 0;
330   }
331   _schro_arith_encode_uint (arith, cont_context, value_context, value);
332   if (value) {
333     _schro_arith_encode_bit (arith, sign_context, sign);
334   }
335 }
336 
337 #else /* SCHRO_ARITH_DEFINE_INLINE */
338 int _schro_arith_decode_bit (SchroArith *arith, unsigned int context);
339 int _schro_arith_decode_uint (SchroArith *arith, unsigned int cont_context,
340     unsigned int value_context);
341 void _schro_arith_encode_bit (SchroArith *arith, int context, int
342     value) SCHRO_INTERNAL;
343 void _schro_arith_encode_uint (SchroArith *arith, int cont_context,
344     int value_context, int value) SCHRO_INTERNAL;
345 void _schro_arith_encode_sint (SchroArith *arith, int cont_context,
346     int value_context, int sign_context, int value) SCHRO_INTERNAL;
347 
348 #endif /* SCHRO_ARITH_DEFINE_INLINE */
349 
350 #endif
351 
352 SCHRO_END_DECLS
353 
354 #endif
355 
356 
357