]> git.sesse.net Git - ffmpeg/blob - libavcodec/cabac.h
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / 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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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     const uint8_t *bytestream_start;
45     const uint8_t *bytestream;
46     const uint8_t *bytestream_end;
47     PutBitContext pb;
48 }CABACContext;
49
50 extern uint8_t ff_h264_mlps_state[4*64];
51 extern uint8_t ff_h264_lps_range[4*2*64];  ///< rangeTabLPS
52 extern uint8_t ff_h264_mps_state[2*64];     ///< transIdxMPS
53 extern uint8_t ff_h264_lps_state[2*64];     ///< transIdxLPS
54 extern const uint8_t ff_h264_norm_shift[512];
55
56 #if ARCH_X86
57 #   include "x86/cabac.h"
58 #endif
59
60 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
61 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
62 void ff_init_cabac_states(CABACContext *c);
63
64
65 static inline void put_cabac_bit(CABACContext *c, int b){
66     put_bits(&c->pb, 1, b);
67     for(;c->outstanding_count; c->outstanding_count--){
68         put_bits(&c->pb, 1, 1-b);
69     }
70 }
71
72 static inline void renorm_cabac_encoder(CABACContext *c){
73     while(c->range < 0x100){
74         //FIXME optimize
75         if(c->low<0x100){
76             put_cabac_bit(c, 0);
77         }else if(c->low<0x200){
78             c->outstanding_count++;
79             c->low -= 0x100;
80         }else{
81             put_cabac_bit(c, 1);
82             c->low -= 0x200;
83         }
84
85         c->range+= c->range;
86         c->low += c->low;
87     }
88 }
89
90 static void refill(CABACContext *c){
91 #if CABAC_BITS == 16
92         c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
93 #else
94         c->low+= c->bytestream[0]<<1;
95 #endif
96     c->low -= CABAC_MASK;
97     c->bytestream+= CABAC_BITS/8;
98 }
99
100 static inline void renorm_cabac_decoder_once(CABACContext *c){
101     int shift= (uint32_t)(c->range - 0x100)>>31;
102     c->range<<= shift;
103     c->low  <<= shift;
104     if(!(c->low & CABAC_MASK))
105         refill(c);
106 }
107
108 #ifndef get_cabac_inline
109 static void refill2(CABACContext *c){
110     int i, x;
111
112     x= c->low ^ (c->low-1);
113     i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
114
115     x= -CABAC_MASK;
116
117 #if CABAC_BITS == 16
118         x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
119 #else
120         x+= c->bytestream[0]<<1;
121 #endif
122
123     c->low += x<<i;
124     c->bytestream+= CABAC_BITS/8;
125 }
126
127 static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
128     int s = *state;
129     int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
130     int bit, lps_mask;
131
132     c->range -= RangeLPS;
133     lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
134
135     c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
136     c->range += (RangeLPS - c->range) & lps_mask;
137
138     s^=lps_mask;
139     *state= (ff_h264_mlps_state+128)[s];
140     bit= s&1;
141
142     lps_mask= ff_h264_norm_shift[c->range];
143     c->range<<= lps_mask;
144     c->low  <<= lps_mask;
145     if(!(c->low & CABAC_MASK))
146         refill2(c);
147     return bit;
148 }
149 #endif
150
151 static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
152     return get_cabac_inline(c,state);
153 }
154
155 static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
156     return get_cabac_inline(c,state);
157 }
158
159 static int av_unused get_cabac_bypass(CABACContext *c){
160     int range;
161     c->low += c->low;
162
163     if(!(c->low & CABAC_MASK))
164         refill(c);
165
166     range= c->range<<(CABAC_BITS+1);
167     if(c->low < range){
168         return 0;
169     }else{
170         c->low -= range;
171         return 1;
172     }
173 }
174
175
176 #ifndef get_cabac_bypass_sign
177 static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
178     int range, mask;
179     c->low += c->low;
180
181     if(!(c->low & CABAC_MASK))
182         refill(c);
183
184     range= c->range<<(CABAC_BITS+1);
185     c->low -= range;
186     mask= c->low >> 31;
187     range &= mask;
188     c->low += range;
189     return (val^mask)-mask;
190 }
191 #endif
192
193 /**
194  *
195  * @return the number of bytes read or 0 if no end
196  */
197 static int av_unused get_cabac_terminate(CABACContext *c){
198     c->range -= 2;
199     if(c->low < c->range<<(CABAC_BITS+1)){
200         renorm_cabac_decoder_once(c);
201         return 0;
202     }else{
203         return c->bytestream - c->bytestream_start;
204     }
205 }
206
207 #endif /* AVCODEC_CABAC_H */