Mercurial > libavcodec.hg
annotate cabac.h @ 3967:01e1a12aa380 libavcodec
remove bytestream_end checks, seems to work fine without them and the bitstream reader doesnt check for the end either
author | michael |
---|---|
date | Mon, 09 Oct 2006 12:25:24 +0000 |
parents | 162bea01a5d4 |
children | fc6e0942353b |
rev | line source |
---|---|
1287 | 1 /* |
2 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder | |
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at> | |
4 * | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3946
diff
changeset
|
5 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3946
diff
changeset
|
6 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3946
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
1287 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3946
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
1287 | 11 * |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3946
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
1287 | 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 | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3946
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2967
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
1287 | 20 * |
21 */ | |
2967 | 22 |
1287 | 23 /** |
24 * @file cabac.h | |
25 * Context Adaptive Binary Arithmetic Coder. | |
26 */ | |
27 | |
28 | |
3284
a224d9752912
don't force asserts in release builds. 2% faster h264.
lorenm
parents:
3036
diff
changeset
|
29 //#undef NDEBUG |
1287 | 30 #include <assert.h> |
31 | |
3948
3edbf131ee44
refill cabac variables in 16bit steps, 3% faster get_cabac()
michael
parents:
3947
diff
changeset
|
32 #define CABAC_BITS 16 |
2323 | 33 #define CABAC_MASK ((1<<CABAC_BITS)-1) |
34 | |
1287 | 35 typedef struct CABACContext{ |
36 int low; | |
37 int range; | |
38 int outstanding_count; | |
39 #ifdef STRICT_LIMITS | |
40 int symCount; | |
41 #endif | |
3955 | 42 uint8_t lps_range[2*66][4]; ///< rangeTabLPS |
43 uint8_t lps_state[2*65]; ///< transIdxLPS | |
44 uint8_t mps_state[2*65]; ///< transIdxMPS | |
2024
f65d87bfdd5a
some of the warning fixes by (Michael Roitzsch <mroi at users dot sourceforge dot net>)
michael
parents:
1787
diff
changeset
|
45 const uint8_t *bytestream_start; |
f65d87bfdd5a
some of the warning fixes by (Michael Roitzsch <mroi at users dot sourceforge dot net>)
michael
parents:
1787
diff
changeset
|
46 const uint8_t *bytestream; |
2116 | 47 const uint8_t *bytestream_end; |
1287 | 48 PutBitContext pb; |
49 }CABACContext; | |
50 | |
1301 | 51 extern const uint8_t ff_h264_lps_range[64][4]; |
52 extern const uint8_t ff_h264_mps_state[64]; | |
53 extern const uint8_t ff_h264_lps_state[64]; | |
3964 | 54 extern const uint8_t ff_h264_norm_shift[128]; |
2323 | 55 |
1287 | 56 |
57 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size); | |
2024
f65d87bfdd5a
some of the warning fixes by (Michael Roitzsch <mroi at users dot sourceforge dot net>)
michael
parents:
1787
diff
changeset
|
58 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size); |
2967 | 59 void ff_init_cabac_states(CABACContext *c, uint8_t const (*lps_range)[4], |
1287 | 60 uint8_t const *mps_state, uint8_t const *lps_state, int state_count); |
61 | |
62 | |
63 static inline void put_cabac_bit(CABACContext *c, int b){ | |
2967 | 64 put_bits(&c->pb, 1, b); |
65 for(;c->outstanding_count; c->outstanding_count--){ | |
1287 | 66 put_bits(&c->pb, 1, 1-b); |
67 } | |
68 } | |
69 | |
70 static inline void renorm_cabac_encoder(CABACContext *c){ | |
71 while(c->range < 0x100){ | |
72 //FIXME optimize | |
73 if(c->low<0x100){ | |
74 put_cabac_bit(c, 0); | |
75 }else if(c->low<0x200){ | |
76 c->outstanding_count++; | |
77 c->low -= 0x100; | |
78 }else{ | |
79 put_cabac_bit(c, 1); | |
80 c->low -= 0x200; | |
81 } | |
2967 | 82 |
1287 | 83 c->range+= c->range; |
84 c->low += c->low; | |
85 } | |
86 } | |
87 | |
3928
987fffdf6ae7
don't try to inline cabac functions. gcc ignored the hint anyway, and forcing it would make h264 slower.
lorenm
parents:
3642
diff
changeset
|
88 static void put_cabac(CABACContext *c, uint8_t * const state, int bit){ |
2323 | 89 int RangeLPS= c->lps_range[*state][c->range>>6]; |
2967 | 90 |
1287 | 91 if(bit == ((*state)&1)){ |
92 c->range -= RangeLPS; | |
93 *state= c->mps_state[*state]; | |
94 }else{ | |
95 c->low += c->range - RangeLPS; | |
96 c->range = RangeLPS; | |
97 *state= c->lps_state[*state]; | |
98 } | |
2967 | 99 |
1287 | 100 renorm_cabac_encoder(c); |
101 | |
102 #ifdef STRICT_LIMITS | |
103 c->symCount++; | |
104 #endif | |
105 } | |
106 | |
3928
987fffdf6ae7
don't try to inline cabac functions. gcc ignored the hint anyway, and forcing it would make h264 slower.
lorenm
parents:
3642
diff
changeset
|
107 static void put_cabac_static(CABACContext *c, int RangeLPS, int bit){ |
1287 | 108 assert(c->range > RangeLPS); |
109 | |
110 if(!bit){ | |
111 c->range -= RangeLPS; | |
112 }else{ | |
113 c->low += c->range - RangeLPS; | |
114 c->range = RangeLPS; | |
115 } | |
116 | |
117 renorm_cabac_encoder(c); | |
118 | |
119 #ifdef STRICT_LIMITS | |
120 c->symCount++; | |
121 #endif | |
122 } | |
123 | |
1290 | 124 /** |
125 * @param bit 0 -> write zero bit, !=0 write one bit | |
126 */ | |
3928
987fffdf6ae7
don't try to inline cabac functions. gcc ignored the hint anyway, and forcing it would make h264 slower.
lorenm
parents:
3642
diff
changeset
|
127 static void put_cabac_bypass(CABACContext *c, int bit){ |
1287 | 128 c->low += c->low; |
129 | |
130 if(bit){ | |
131 c->low += c->range; | |
132 } | |
133 //FIXME optimize | |
134 if(c->low<0x200){ | |
135 put_cabac_bit(c, 0); | |
136 }else if(c->low<0x400){ | |
137 c->outstanding_count++; | |
138 c->low -= 0x200; | |
139 }else{ | |
140 put_cabac_bit(c, 1); | |
141 c->low -= 0x400; | |
142 } | |
2967 | 143 |
1287 | 144 #ifdef STRICT_LIMITS |
145 c->symCount++; | |
146 #endif | |
147 } | |
148 | |
1300
e18667d1e94d
FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents:
1298
diff
changeset
|
149 /** |
e18667d1e94d
FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents:
1298
diff
changeset
|
150 * |
e18667d1e94d
FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents:
1298
diff
changeset
|
151 * @return the number of bytes written |
e18667d1e94d
FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents:
1298
diff
changeset
|
152 */ |
3928
987fffdf6ae7
don't try to inline cabac functions. gcc ignored the hint anyway, and forcing it would make h264 slower.
lorenm
parents:
3642
diff
changeset
|
153 static int put_cabac_terminate(CABACContext *c, int bit){ |
1287 | 154 c->range -= 2; |
155 | |
156 if(!bit){ | |
157 renorm_cabac_encoder(c); | |
158 }else{ | |
159 c->low += c->range; | |
160 c->range= 2; | |
2967 | 161 |
1287 | 162 renorm_cabac_encoder(c); |
163 | |
164 assert(c->low <= 0x1FF); | |
165 put_cabac_bit(c, c->low>>9); | |
166 put_bits(&c->pb, 2, ((c->low>>7)&3)|1); | |
2967 | 167 |
1287 | 168 flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong |
169 } | |
2967 | 170 |
1287 | 171 #ifdef STRICT_LIMITS |
172 c->symCount++; | |
173 #endif | |
1300
e18667d1e94d
FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents:
1298
diff
changeset
|
174 |
1787 | 175 return (put_bits_count(&c->pb)+7)>>3; |
1287 | 176 } |
177 | |
1290 | 178 /** |
179 * put (truncated) unary binarization. | |
180 */ | |
3928
987fffdf6ae7
don't try to inline cabac functions. gcc ignored the hint anyway, and forcing it would make h264 slower.
lorenm
parents:
3642
diff
changeset
|
181 static void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){ |
1290 | 182 int i; |
2967 | 183 |
1290 | 184 assert(v <= max); |
2967 | 185 |
1290 | 186 #if 1 |
187 for(i=0; i<v; i++){ | |
188 put_cabac(c, state, 1); | |
189 if(i < max_index) state++; | |
190 } | |
191 if(truncated==0 || v<max) | |
192 put_cabac(c, state, 0); | |
193 #else | |
194 if(v <= max_index){ | |
195 for(i=0; i<v; i++){ | |
196 put_cabac(c, state+i, 1); | |
197 } | |
198 if(truncated==0 || v<max) | |
199 put_cabac(c, state+i, 0); | |
200 }else{ | |
201 for(i=0; i<=max_index; i++){ | |
202 put_cabac(c, state+i, 1); | |
203 } | |
204 for(; i<v; i++){ | |
205 put_cabac(c, state+max_index, 1); | |
206 } | |
207 if(truncated==0 || v<max) | |
208 put_cabac(c, state+max_index, 0); | |
209 } | |
210 #endif | |
211 } | |
212 | |
213 /** | |
214 * put unary exp golomb k-th order binarization. | |
215 */ | |
3928
987fffdf6ae7
don't try to inline cabac functions. gcc ignored the hint anyway, and forcing it would make h264 slower.
lorenm
parents:
3642
diff
changeset
|
216 static void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){ |
1290 | 217 int i; |
2967 | 218 |
1290 | 219 if(v==0) |
220 put_cabac(c, state, 0); | |
221 else{ | |
1298 | 222 const int sign= v < 0; |
2967 | 223 |
1298 | 224 if(is_signed) v= ABS(v); |
2967 | 225 |
1290 | 226 if(v<max){ |
227 for(i=0; i<v; i++){ | |
228 put_cabac(c, state, 1); | |
229 if(i < max_index) state++; | |
230 } | |
231 | |
232 put_cabac(c, state, 0); | |
233 }else{ | |
234 int m= 1<<k; | |
235 | |
236 for(i=0; i<max; i++){ | |
237 put_cabac(c, state, 1); | |
238 if(i < max_index) state++; | |
239 } | |
240 | |
241 v -= max; | |
242 while(v >= m){ //FIXME optimize | |
243 put_cabac_bypass(c, 1); | |
244 v-= m; | |
245 m+= m; | |
246 } | |
247 put_cabac_bypass(c, 0); | |
248 while(m>>=1){ | |
249 put_cabac_bypass(c, v&m); | |
250 } | |
251 } | |
252 | |
253 if(is_signed) | |
254 put_cabac_bypass(c, sign); | |
255 } | |
256 } | |
257 | |
2323 | 258 static void refill(CABACContext *c){ |
259 #if CABAC_BITS == 16 | |
3946 | 260 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1); |
2323 | 261 #else |
262 c->low+= c->bytestream[0]<<1; | |
263 #endif | |
264 c->low -= CABAC_MASK; | |
265 c->bytestream+= CABAC_BITS/8; | |
266 } | |
267 | |
268 static void refill2(CABACContext *c){ | |
269 int i, x; | |
270 | |
271 x= c->low ^ (c->low-1); | |
3964 | 272 i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS+1)]; |
2323 | 273 |
274 x= -CABAC_MASK; | |
2967 | 275 |
2323 | 276 #if CABAC_BITS == 16 |
277 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1); | |
278 #else | |
279 x+= c->bytestream[0]<<1; | |
280 #endif | |
2967 | 281 |
2323 | 282 c->low += x<<i; |
283 c->bytestream+= CABAC_BITS/8; | |
284 } | |
285 | |
1287 | 286 static inline void renorm_cabac_decoder(CABACContext *c){ |
2323 | 287 while(c->range < (0x200 << CABAC_BITS)){ |
1287 | 288 c->range+= c->range; |
289 c->low+= c->low; | |
2323 | 290 if(!(c->low & CABAC_MASK)) |
291 refill(c); | |
1287 | 292 } |
293 } | |
294 | |
2323 | 295 static inline void renorm_cabac_decoder_once(CABACContext *c){ |
3951 | 296 #ifdef ARCH_X86_DISABLED |
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
297 int temp; |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
298 #if 0 |
3950
900d21b85dd6
renorm_cabac_decoder_once START/STOP_TIMER scores for athlon
michael
parents:
3948
diff
changeset
|
299 //P3:683 athlon:475 |
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
300 asm( |
3948
3edbf131ee44
refill cabac variables in 16bit steps, 3% faster get_cabac()
michael
parents:
3947
diff
changeset
|
301 "lea -0x2000000(%0), %2 \n\t" |
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
302 "shr $31, %2 \n\t" //FIXME 31->63 for x86-64 |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
303 "shl %%cl, %0 \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
304 "shl %%cl, %1 \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
305 : "+r"(c->range), "+r"(c->low), "+c"(temp) |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
306 ); |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
307 #elif 0 |
3950
900d21b85dd6
renorm_cabac_decoder_once START/STOP_TIMER scores for athlon
michael
parents:
3948
diff
changeset
|
308 //P3:680 athlon:474 |
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
309 asm( |
3948
3edbf131ee44
refill cabac variables in 16bit steps, 3% faster get_cabac()
michael
parents:
3947
diff
changeset
|
310 "cmp $0x2000000, %0 \n\t" |
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
311 "setb %%cl \n\t" //FIXME 31->63 for x86-64 |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
312 "shl %%cl, %0 \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
313 "shl %%cl, %1 \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
314 : "+r"(c->range), "+r"(c->low), "+c"(temp) |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
315 ); |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
316 #elif 1 |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
317 int temp2; |
3950
900d21b85dd6
renorm_cabac_decoder_once START/STOP_TIMER scores for athlon
michael
parents:
3948
diff
changeset
|
318 //P3:665 athlon:517 |
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
319 asm( |
3948
3edbf131ee44
refill cabac variables in 16bit steps, 3% faster get_cabac()
michael
parents:
3947
diff
changeset
|
320 "lea -0x2000000(%0), %%eax \n\t" |
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
321 "cdq \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
322 "mov %0, %%eax \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
323 "and %%edx, %0 \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
324 "and %1, %%edx \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
325 "add %%eax, %0 \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
326 "add %%edx, %1 \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
327 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2) |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
328 ); |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
329 #elif 0 |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
330 int temp2; |
3950
900d21b85dd6
renorm_cabac_decoder_once START/STOP_TIMER scores for athlon
michael
parents:
3948
diff
changeset
|
331 //P3:673 athlon:509 |
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
332 asm( |
3948
3edbf131ee44
refill cabac variables in 16bit steps, 3% faster get_cabac()
michael
parents:
3947
diff
changeset
|
333 "cmp $0x2000000, %0 \n\t" |
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
334 "sbb %%edx, %%edx \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
335 "mov %0, %%eax \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
336 "and %%edx, %0 \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
337 "and %1, %%edx \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
338 "add %%eax, %0 \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
339 "add %%edx, %1 \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
340 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2) |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
341 ); |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
342 #else |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
343 int temp2; |
3950
900d21b85dd6
renorm_cabac_decoder_once START/STOP_TIMER scores for athlon
michael
parents:
3948
diff
changeset
|
344 //P3:677 athlon:511 |
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
345 asm( |
3948
3edbf131ee44
refill cabac variables in 16bit steps, 3% faster get_cabac()
michael
parents:
3947
diff
changeset
|
346 "cmp $0x2000000, %0 \n\t" |
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
347 "lea (%0, %0), %%eax \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
348 "lea (%1, %1), %%edx \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
349 "cmovb %%eax, %0 \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
350 "cmovb %%edx, %1 \n\t" |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
351 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2) |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
352 ); |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
353 #endif |
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
354 #else |
3950
900d21b85dd6
renorm_cabac_decoder_once START/STOP_TIMER scores for athlon
michael
parents:
3948
diff
changeset
|
355 //P3:675 athlon:476 |
3642 | 356 int shift= (uint32_t)(c->range - (0x200 << CABAC_BITS))>>31; |
357 c->range<<= shift; | |
358 c->low <<= shift; | |
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
359 #endif |
2323 | 360 if(!(c->low & CABAC_MASK)) |
361 refill(c); | |
362 } | |
363 | |
3928
987fffdf6ae7
don't try to inline cabac functions. gcc ignored the hint anyway, and forcing it would make h264 slower.
lorenm
parents:
3642
diff
changeset
|
364 static int get_cabac(CABACContext *c, uint8_t * const state){ |
3642 | 365 //FIXME gcc generates duplicate load/stores for c->low and c->range |
3951 | 366 //START_TIMER |
3642 | 367 int s = *state; |
368 int RangeLPS= c->lps_range[s][c->range>>(CABAC_BITS+7)]<<(CABAC_BITS+1); | |
2522
e25782262d7d
kill warnings patch by (M«©ns Rullg«©rd <mru inprovide com>)
michael
parents:
2323
diff
changeset
|
369 int bit, lps_mask attribute_unused; |
2967 | 370 |
1287 | 371 c->range -= RangeLPS; |
2323 | 372 #if 1 |
1287 | 373 if(c->low < c->range){ |
3642 | 374 bit= s&1; |
3955 | 375 #ifdef ARCH_X86 |
376 //P3:627 | |
377 asm( | |
378 "addb $2, %b0 \n\t" | |
379 " js 1f \n\t" | |
380 "movb %b0, %1 \n\t" | |
381 "1: \n\t" | |
382 : "+q"(s), "=m"(*state) | |
383 ); | |
384 #else | |
385 *state= c->mps_state[s]; //P3:655 | |
386 /* if(s<126) //P3:657 | |
387 *state= s+2;*/ | |
388 s+=2; //P3:631 | |
389 if(s<128) | |
390 *state= s; | |
391 #endif | |
2323 | 392 renorm_cabac_decoder_once(c); |
1287 | 393 }else{ |
3964 | 394 bit= ff_h264_norm_shift[RangeLPS>>19]; |
1287 | 395 c->low -= c->range; |
3642 | 396 *state= c->lps_state[s]; |
3956
0910f2844f9a
branchless renormalization (1% faster get_cabac) old branchless renormalization wasnt faster because gcc was scared of the shift variable (missusing bit variable now)
michael
parents:
3955
diff
changeset
|
397 c->range = RangeLPS<<bit; |
0910f2844f9a
branchless renormalization (1% faster get_cabac) old branchless renormalization wasnt faster because gcc was scared of the shift variable (missusing bit variable now)
michael
parents:
3955
diff
changeset
|
398 c->low <<= bit; |
0910f2844f9a
branchless renormalization (1% faster get_cabac) old branchless renormalization wasnt faster because gcc was scared of the shift variable (missusing bit variable now)
michael
parents:
3955
diff
changeset
|
399 bit= (s&1)^1; |
0910f2844f9a
branchless renormalization (1% faster get_cabac) old branchless renormalization wasnt faster because gcc was scared of the shift variable (missusing bit variable now)
michael
parents:
3955
diff
changeset
|
400 |
2323 | 401 if(!(c->low & 0xFFFF)){ |
402 refill2(c); | |
3956
0910f2844f9a
branchless renormalization (1% faster get_cabac) old branchless renormalization wasnt faster because gcc was scared of the shift variable (missusing bit variable now)
michael
parents:
3955
diff
changeset
|
403 } |
1287 | 404 } |
2323 | 405 #else |
406 lps_mask= (c->range - c->low)>>31; | |
2967 | 407 |
2323 | 408 c->low -= c->range & lps_mask; |
409 c->range += (RangeLPS - c->range) & lps_mask; | |
2967 | 410 |
3642 | 411 bit= (s^lps_mask)&1; |
412 *state= c->mps_state[s - (128&lps_mask)]; | |
2967 | 413 |
2323 | 414 lps_mask= ff_h264_norm_shift[c->range>>(CABAC_BITS+2)]; |
415 c->range<<= lps_mask; | |
416 c->low <<= lps_mask; | |
417 if(!(c->low & CABAC_MASK)) | |
418 refill2(c); | |
419 #endif | |
3951 | 420 //STOP_TIMER("get_cabac") |
2967 | 421 return bit; |
1287 | 422 } |
423 | |
3928
987fffdf6ae7
don't try to inline cabac functions. gcc ignored the hint anyway, and forcing it would make h264 slower.
lorenm
parents:
3642
diff
changeset
|
424 static int get_cabac_bypass(CABACContext *c){ |
1287 | 425 c->low += c->low; |
426 | |
2323 | 427 if(!(c->low & CABAC_MASK)) |
428 refill(c); | |
2967 | 429 |
1287 | 430 if(c->low < c->range){ |
431 return 0; | |
432 }else{ | |
433 c->low -= c->range; | |
434 return 1; | |
435 } | |
436 } | |
437 | |
1300
e18667d1e94d
FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents:
1298
diff
changeset
|
438 /** |
e18667d1e94d
FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents:
1298
diff
changeset
|
439 * |
e18667d1e94d
FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents:
1298
diff
changeset
|
440 * @return the number of bytes read or 0 if no end |
e18667d1e94d
FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents:
1298
diff
changeset
|
441 */ |
3928
987fffdf6ae7
don't try to inline cabac functions. gcc ignored the hint anyway, and forcing it would make h264 slower.
lorenm
parents:
3642
diff
changeset
|
442 static int get_cabac_terminate(CABACContext *c){ |
2323 | 443 c->range -= 4<<CABAC_BITS; |
1287 | 444 if(c->low < c->range){ |
2323 | 445 renorm_cabac_decoder_once(c); |
1287 | 446 return 0; |
447 }else{ | |
1300
e18667d1e94d
FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents:
1298
diff
changeset
|
448 return c->bytestream - c->bytestream_start; |
2967 | 449 } |
1287 | 450 } |
451 | |
1290 | 452 /** |
453 * get (truncated) unnary binarization. | |
454 */ | |
3928
987fffdf6ae7
don't try to inline cabac functions. gcc ignored the hint anyway, and forcing it would make h264 slower.
lorenm
parents:
3642
diff
changeset
|
455 static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){ |
1290 | 456 int i; |
2967 | 457 |
458 for(i=0; i<max; i++){ | |
1290 | 459 if(get_cabac(c, state)==0) |
460 return i; | |
2967 | 461 |
1290 | 462 if(i< max_index) state++; |
463 } | |
464 | |
465 return truncated ? max : -1; | |
466 } | |
467 | |
468 /** | |
469 * get unary exp golomb k-th order binarization. | |
470 */ | |
3928
987fffdf6ae7
don't try to inline cabac functions. gcc ignored the hint anyway, and forcing it would make h264 slower.
lorenm
parents:
3642
diff
changeset
|
471 static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){ |
1290 | 472 int i, v; |
473 int m= 1<<k; | |
2967 | 474 |
475 if(get_cabac(c, state)==0) | |
1290 | 476 return 0; |
2967 | 477 |
1290 | 478 if(0 < max_index) state++; |
2967 | 479 |
480 for(i=1; i<max; i++){ | |
1290 | 481 if(get_cabac(c, state)==0){ |
482 if(is_signed && get_cabac_bypass(c)){ | |
483 return -i; | |
484 }else | |
485 return i; | |
486 } | |
487 | |
488 if(i < max_index) state++; | |
489 } | |
2967 | 490 |
1290 | 491 while(get_cabac_bypass(c)){ |
492 i+= m; | |
493 m+= m; | |
494 } | |
2967 | 495 |
1290 | 496 v=0; |
497 while(m>>=1){ | |
498 v+= v + get_cabac_bypass(c); | |
499 } | |
500 i += v; | |
501 | |
502 if(is_signed && get_cabac_bypass(c)){ | |
503 return -i; | |
504 }else | |
505 return i; | |
506 } |