view libavcodec/cabac.h @ 2:897f711a7157

rearrange to work with autoconf
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Tue, 25 Sep 2012 15:55:33 +0200
parents
children
line source
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 */
22 /**
23 * @file
24 * Context Adaptive Binary Arithmetic Coder.
25 */
27 #ifndef AVCODEC_CABAC_H
28 #define AVCODEC_CABAC_H
30 //#undef NDEBUG
31 #include <assert.h>
32 #include "libavutil/x86_cpu.h"
33 #include "libavutil/attributes.h"
35 #define CABAC_BITS 16
36 #define CABAC_MASK ((1<<CABAC_BITS)-1)
37 #define BRANCHLESS_CABAC_DECODER 1
39 typedef struct CABACContext{
40 int low;
41 int range;
42 int outstanding_count;
43 #ifdef STRICT_LIMITS
44 int symCount;
45 #endif
46 const uint8_t *bytestream_start;
47 const uint8_t *bytestream;
48 const uint8_t *bytestream_end;
49 uint8_t cabac_state[460];
50 }CABACContext;
52 extern uint8_t ff_h264_mlps_state[4*64];
53 extern uint8_t ff_h264_lps_range[4*2*64]; ///< rangeTabLPS
54 extern uint8_t ff_h264_mps_state[2*64]; ///< transIdxMPS
55 extern uint8_t ff_h264_lps_state[2*64]; ///< transIdxLPS
56 extern const uint8_t ff_h264_norm_shift[512];
58 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
59 void ff_init_cabac_states(void);
61 static void refill(CABACContext *c){
62 #if CABAC_BITS == 16
63 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
64 #else
65 c->low+= c->bytestream[0]<<1;
66 #endif
67 c->low -= CABAC_MASK;
68 c->bytestream+= CABAC_BITS/8;
69 }
71 static void refill2(CABACContext *c){
72 int i, x;
74 x= c->low ^ (c->low-1);
75 i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
77 x= -CABAC_MASK;
79 #if CABAC_BITS == 16
80 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
81 #else
82 x+= c->bytestream[0]<<1;
83 #endif
85 c->low += x<<i;
86 c->bytestream+= CABAC_BITS/8;
87 }
89 static inline void renorm_cabac_decoder(CABACContext *c){
90 while(c->range < 0x100){
91 c->range+= c->range;
92 c->low+= c->low;
93 if(!(c->low & CABAC_MASK))
94 refill(c);
95 }
96 }
98 static inline void renorm_cabac_decoder_once(CABACContext *c){
100 int shift= (uint32_t)(c->range - 0x100)>>31;
101 c->range<<= shift;
102 c->low <<= shift;
104 if(!(c->low & CABAC_MASK))
105 refill(c);
106 }
108 static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
110 int s = *state;
111 int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
112 int bit, lps_mask av_unused;
114 c->range -= RangeLPS;
115 #ifndef BRANCHLESS_CABAC_DECODER
116 if(c->low < (c->range<<(CABAC_BITS+1))){
117 bit= s&1;
118 *state= ff_h264_mps_state[s];
119 renorm_cabac_decoder_once(c);
120 }else{
121 bit= ff_h264_norm_shift[RangeLPS];
122 c->low -= (c->range<<(CABAC_BITS+1));
123 *state= ff_h264_lps_state[s];
124 c->range = RangeLPS<<bit;
125 c->low <<= bit;
126 bit= (s&1)^1;
128 if(!(c->low & CABAC_MASK)){
129 refill2(c);
130 }
131 }
132 #else /* BRANCHLESS_CABAC_DECODER */
133 lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
135 c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
136 c->range += (RangeLPS - c->range) & lps_mask;
138 s^=lps_mask;
139 *state= (ff_h264_mlps_state+128)[s];
140 bit= s&1;
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 #endif /* BRANCHLESS_CABAC_DECODER */
149 return bit;
150 }
152 static int av_noinline av_unused get_cabac_noinline(CABACContext *c, uint8_t * const state){
153 return get_cabac_inline(c, state);
154 }
156 static int av_unused get_cabac(CABACContext *c, uint8_t * const state){
157 return get_cabac_inline(c, state);
158 }
160 static int av_unused get_cabac_bypass(CABACContext *c){
162 int range;
163 c->low += c->low;
165 if(!(c->low & CABAC_MASK))
166 refill(c);
168 range= c->range<<(CABAC_BITS+1);
169 if(c->low < range){
170 return 0;
171 }else{
172 c->low -= range;
173 return 1;
174 }
175 }
177 static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
178 int range, mask;
179 c->low += c->low;
181 if(!(c->low & CABAC_MASK))
182 refill(c);
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 }
192 /**
193 *
194 * @return the number of bytes read or 0 if no end
195 */
196 static int av_unused get_cabac_terminate(CABACContext *c){
197 c->range -= 2;
198 if(c->low < c->range<<(CABAC_BITS+1)){
199 renorm_cabac_decoder_once(c);
200 return 0;
201 }else{
202 return c->bytestream - c->bytestream_start;
203 }
204 }
206 #endif /* AVCODEC_CABAC_H */