]> git.sesse.net Git - ffmpeg/blob - libavcodec/cabac.h
mpegvideo: claim ownership of referenced pictures
[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 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     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 void refill(CABACContext *c){
66 #if CABAC_BITS == 16
67         c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
68 #else
69         c->low+= c->bytestream[0]<<1;
70 #endif
71     c->low -= CABAC_MASK;
72     c->bytestream+= CABAC_BITS/8;
73 }
74
75 static inline void renorm_cabac_decoder_once(CABACContext *c){
76     int shift= (uint32_t)(c->range - 0x100)>>31;
77     c->range<<= shift;
78     c->low  <<= shift;
79     if(!(c->low & CABAC_MASK))
80         refill(c);
81 }
82
83 #ifndef get_cabac_inline
84 static void refill2(CABACContext *c){
85     int i, x;
86
87     x= c->low ^ (c->low-1);
88     i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
89
90     x= -CABAC_MASK;
91
92 #if CABAC_BITS == 16
93         x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
94 #else
95         x+= c->bytestream[0]<<1;
96 #endif
97
98     c->low += x<<i;
99     c->bytestream+= CABAC_BITS/8;
100 }
101
102 static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
103     int s = *state;
104     int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
105     int bit, lps_mask;
106
107     c->range -= RangeLPS;
108     lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
109
110     c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
111     c->range += (RangeLPS - c->range) & lps_mask;
112
113     s^=lps_mask;
114     *state= (ff_h264_mlps_state+128)[s];
115     bit= s&1;
116
117     lps_mask= ff_h264_norm_shift[c->range];
118     c->range<<= lps_mask;
119     c->low  <<= lps_mask;
120     if(!(c->low & CABAC_MASK))
121         refill2(c);
122     return bit;
123 }
124 #endif
125
126 static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
127     return get_cabac_inline(c,state);
128 }
129
130 static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
131     return get_cabac_inline(c,state);
132 }
133
134 static int av_unused get_cabac_bypass(CABACContext *c){
135     int range;
136     c->low += c->low;
137
138     if(!(c->low & CABAC_MASK))
139         refill(c);
140
141     range= c->range<<(CABAC_BITS+1);
142     if(c->low < range){
143         return 0;
144     }else{
145         c->low -= range;
146         return 1;
147     }
148 }
149
150
151 #ifndef get_cabac_bypass_sign
152 static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
153     int range, mask;
154     c->low += c->low;
155
156     if(!(c->low & CABAC_MASK))
157         refill(c);
158
159     range= c->range<<(CABAC_BITS+1);
160     c->low -= range;
161     mask= c->low >> 31;
162     range &= mask;
163     c->low += range;
164     return (val^mask)-mask;
165 }
166 #endif
167
168 /**
169  *
170  * @return the number of bytes read or 0 if no end
171  */
172 static int av_unused get_cabac_terminate(CABACContext *c){
173     c->range -= 2;
174     if(c->low < c->range<<(CABAC_BITS+1)){
175         renorm_cabac_decoder_once(c);
176         return 0;
177     }else{
178         return c->bytestream - c->bytestream_start;
179     }
180 }
181
182 #endif /* AVCODEC_CABAC_H */