OSDN Git Service

cmdutils: add support for programs in check_stream_specifier()
[coroid/libav_saccubus.git] / libavcodec / cabac.h
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * Context Adaptive Binary Arithmetic Coder.
25  */
26
27 #ifndef AVCODEC_CABAC_H
28 #define AVCODEC_CABAC_H
29
30 #include <stddef.h>
31
32 #include "put_bits.h"
33
34 //#undef NDEBUG
35 #include <assert.h>
36
37 #define CABAC_BITS 16
38 #define CABAC_MASK ((1<<CABAC_BITS)-1)
39
40 typedef struct CABACContext{
41     int low;
42     int range;
43     int outstanding_count;
44 #ifdef STRICT_LIMITS
45     int symCount;
46 #endif
47     const uint8_t *bytestream_start;
48     const uint8_t *bytestream;
49     const uint8_t *bytestream_end;
50     PutBitContext pb;
51 }CABACContext;
52
53 extern uint8_t ff_h264_mlps_state[4*64];
54 extern uint8_t ff_h264_lps_range[4*2*64];  ///< rangeTabLPS
55 extern uint8_t ff_h264_mps_state[2*64];     ///< transIdxMPS
56 extern uint8_t ff_h264_lps_state[2*64];     ///< transIdxLPS
57 extern const uint8_t ff_h264_norm_shift[512];
58
59 #if ARCH_X86
60 #   include "x86/cabac.h"
61 #endif
62
63 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
64 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
65 void ff_init_cabac_states(CABACContext *c);
66
67
68 static inline void put_cabac_bit(CABACContext *c, int b){
69     put_bits(&c->pb, 1, b);
70     for(;c->outstanding_count; c->outstanding_count--){
71         put_bits(&c->pb, 1, 1-b);
72     }
73 }
74
75 static inline void renorm_cabac_encoder(CABACContext *c){
76     while(c->range < 0x100){
77         //FIXME optimize
78         if(c->low<0x100){
79             put_cabac_bit(c, 0);
80         }else if(c->low<0x200){
81             c->outstanding_count++;
82             c->low -= 0x100;
83         }else{
84             put_cabac_bit(c, 1);
85             c->low -= 0x200;
86         }
87
88         c->range+= c->range;
89         c->low += c->low;
90     }
91 }
92
93 static void refill(CABACContext *c){
94 #if CABAC_BITS == 16
95         c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
96 #else
97         c->low+= c->bytestream[0]<<1;
98 #endif
99     c->low -= CABAC_MASK;
100     c->bytestream+= CABAC_BITS/8;
101 }
102
103 static inline void renorm_cabac_decoder(CABACContext *c){
104     while(c->range < 0x100){
105         c->range+= c->range;
106         c->low+= c->low;
107         if(!(c->low & CABAC_MASK))
108             refill(c);
109     }
110 }
111
112 static inline void renorm_cabac_decoder_once(CABACContext *c){
113     int shift= (uint32_t)(c->range - 0x100)>>31;
114     c->range<<= shift;
115     c->low  <<= shift;
116     if(!(c->low & CABAC_MASK))
117         refill(c);
118 }
119
120 #ifndef get_cabac_inline
121 static void refill2(CABACContext *c){
122     int i, x;
123
124     x= c->low ^ (c->low-1);
125     i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
126
127     x= -CABAC_MASK;
128
129 #if CABAC_BITS == 16
130         x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
131 #else
132         x+= c->bytestream[0]<<1;
133 #endif
134
135     c->low += x<<i;
136     c->bytestream+= CABAC_BITS/8;
137 }
138
139 static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
140     int s = *state;
141     int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
142     int bit, lps_mask;
143
144     c->range -= RangeLPS;
145     lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
146
147     c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
148     c->range += (RangeLPS - c->range) & lps_mask;
149
150     s^=lps_mask;
151     *state= (ff_h264_mlps_state+128)[s];
152     bit= s&1;
153
154     lps_mask= ff_h264_norm_shift[c->range];
155     c->range<<= lps_mask;
156     c->low  <<= lps_mask;
157     if(!(c->low & CABAC_MASK))
158         refill2(c);
159     return bit;
160 }
161 #endif
162
163 static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
164     return get_cabac_inline(c,state);
165 }
166
167 static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
168     return get_cabac_inline(c,state);
169 }
170
171 static int av_unused get_cabac_bypass(CABACContext *c){
172     int range;
173     c->low += c->low;
174
175     if(!(c->low & CABAC_MASK))
176         refill(c);
177
178     range= c->range<<(CABAC_BITS+1);
179     if(c->low < range){
180         return 0;
181     }else{
182         c->low -= range;
183         return 1;
184     }
185 }
186
187
188 #ifndef get_cabac_bypass_sign
189 static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
190     int range, mask;
191     c->low += c->low;
192
193     if(!(c->low & CABAC_MASK))
194         refill(c);
195
196     range= c->range<<(CABAC_BITS+1);
197     c->low -= range;
198     mask= c->low >> 31;
199     range &= mask;
200     c->low += range;
201     return (val^mask)-mask;
202 }
203 #endif
204
205 /**
206  *
207  * @return the number of bytes read or 0 if no end
208  */
209 static int av_unused get_cabac_terminate(CABACContext *c){
210     c->range -= 2;
211     if(c->low < c->range<<(CABAC_BITS+1)){
212         renorm_cabac_decoder_once(c);
213         return 0;
214     }else{
215         return c->bytestream - c->bytestream_start;
216     }
217 }
218
219 #if 0
220 /**
221  * Get (truncated) unary binarization.
222  */
223 static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
224     int i;
225
226     for(i=0; i<max; i++){
227         if(get_cabac(c, state)==0)
228             return i;
229
230         if(i< max_index) state++;
231     }
232
233     return truncated ? max : -1;
234 }
235
236 /**
237  * get unary exp golomb k-th order binarization.
238  */
239 static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
240     int i, v;
241     int m= 1<<k;
242
243     if(get_cabac(c, state)==0)
244         return 0;
245
246     if(0 < max_index) state++;
247
248     for(i=1; i<max; i++){
249         if(get_cabac(c, state)==0){
250             if(is_signed && get_cabac_bypass(c)){
251                 return -i;
252             }else
253                 return i;
254         }
255
256         if(i < max_index) state++;
257     }
258
259     while(get_cabac_bypass(c)){
260         i+= m;
261         m+= m;
262     }
263
264     v=0;
265     while(m>>=1){
266         v+= v + get_cabac_bypass(c);
267     }
268     i += v;
269
270     if(is_signed && get_cabac_bypass(c)){
271         return -i;
272     }else
273         return i;
274 }
275 #endif /* 0 */
276
277 #endif /* AVCODEC_CABAC_H */