Mercurial > libavcodec.hg
annotate cabac.h @ 3948:3edbf131ee44 libavcodec
refill cabac variables in 16bit steps, 3% faster get_cabac()
| author | michael |
|---|---|
| date | Sat, 07 Oct 2006 15:44:14 +0000 |
| parents | c8c591fe26f8 |
| children | 900d21b85dd6 |
| 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 | |
| 2323 | 42 uint8_t lps_range[2*65][4]; ///< rangeTabLPS |
| 1287 | 43 uint8_t lps_state[2*64]; ///< transIdxLPS |
| 44 uint8_t mps_state[2*64]; ///< 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]; | |
| 2323 | 54 extern const uint8_t ff_h264_norm_shift[256]; |
| 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){ |
| 2736 | 259 if(c->bytestream <= c->bytestream_end) |
| 2323 | 260 #if CABAC_BITS == 16 |
| 3946 | 261 c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1); |
| 2323 | 262 #else |
| 263 c->low+= c->bytestream[0]<<1; | |
| 264 #endif | |
| 265 c->low -= CABAC_MASK; | |
| 266 c->bytestream+= CABAC_BITS/8; | |
| 267 } | |
| 268 | |
|
2522
e25782262d7d
kill warnings patch by (M?ns Rullg?rd <mru inprovide com>)
michael
parents:
2323
diff
changeset
|
269 #if 0 /* all use commented */ |
| 2323 | 270 static void refill2(CABACContext *c){ |
| 271 int i, x; | |
| 272 | |
| 273 x= c->low ^ (c->low-1); | |
| 274 i= 8 - ff_h264_norm_shift[x>>(CABAC_BITS+1)]; | |
| 275 | |
| 276 x= -CABAC_MASK; | |
| 2967 | 277 |
| 2323 | 278 if(c->bytestream < c->bytestream_end) |
| 279 #if CABAC_BITS == 16 | |
| 280 x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1); | |
| 281 #else | |
| 282 x+= c->bytestream[0]<<1; | |
| 283 #endif | |
| 2967 | 284 |
| 2323 | 285 c->low += x<<i; |
| 286 c->bytestream+= CABAC_BITS/8; | |
| 287 } | |
|
2522
e25782262d7d
kill warnings patch by (M?ns Rullg?rd <mru inprovide com>)
michael
parents:
2323
diff
changeset
|
288 #endif |
| 2323 | 289 |
| 1287 | 290 static inline void renorm_cabac_decoder(CABACContext *c){ |
| 2323 | 291 while(c->range < (0x200 << CABAC_BITS)){ |
| 1287 | 292 c->range+= c->range; |
| 293 c->low+= c->low; | |
| 2323 | 294 if(!(c->low & CABAC_MASK)) |
| 295 refill(c); | |
| 1287 | 296 } |
| 297 } | |
| 298 | |
| 2323 | 299 static inline void renorm_cabac_decoder_once(CABACContext *c){ |
|
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
300 #ifdef ARCH_X86 |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
301 int temp; |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
302 #if 0 |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
303 //P3:683 |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
304 asm( |
|
3948
3edbf131ee44
refill cabac variables in 16bit steps, 3% faster get_cabac()
michael
parents:
3947
diff
changeset
|
305 "lea -0x2000000(%0), %2 \n\t" |
|
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
306 "shr $31, %2 \n\t" //FIXME 31->63 for x86-64 |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
307 "shl %%cl, %0 \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
308 "shl %%cl, %1 \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
309 : "+r"(c->range), "+r"(c->low), "+c"(temp) |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
310 ); |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
311 #elif 0 |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
312 //P3:680 |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
313 asm( |
|
3948
3edbf131ee44
refill cabac variables in 16bit steps, 3% faster get_cabac()
michael
parents:
3947
diff
changeset
|
314 "cmp $0x2000000, %0 \n\t" |
|
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
315 "setb %%cl \n\t" //FIXME 31->63 for x86-64 |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
316 "shl %%cl, %0 \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
317 "shl %%cl, %1 \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
318 : "+r"(c->range), "+r"(c->low), "+c"(temp) |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
319 ); |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
320 #elif 1 |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
321 int temp2; |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
322 //P3:665 |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
323 asm( |
|
3948
3edbf131ee44
refill cabac variables in 16bit steps, 3% faster get_cabac()
michael
parents:
3947
diff
changeset
|
324 "lea -0x2000000(%0), %%eax \n\t" |
|
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
325 "cdq \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
326 "mov %0, %%eax \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
327 "and %%edx, %0 \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
328 "and %1, %%edx \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
329 "add %%eax, %0 \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
330 "add %%edx, %1 \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
331 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2) |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
332 ); |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
333 #elif 0 |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
334 int temp2; |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
335 //P3:673 |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
336 asm( |
|
3948
3edbf131ee44
refill cabac variables in 16bit steps, 3% faster get_cabac()
michael
parents:
3947
diff
changeset
|
337 "cmp $0x2000000, %0 \n\t" |
|
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
338 "sbb %%edx, %%edx \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
339 "mov %0, %%eax \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
340 "and %%edx, %0 \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
341 "and %1, %%edx \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
342 "add %%eax, %0 \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
343 "add %%edx, %1 \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
344 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2) |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
345 ); |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
346 #else |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
347 int temp2; |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
348 //P3:677 |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
349 asm( |
|
3948
3edbf131ee44
refill cabac variables in 16bit steps, 3% faster get_cabac()
michael
parents:
3947
diff
changeset
|
350 "cmp $0x2000000, %0 \n\t" |
|
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
351 "lea (%0, %0), %%eax \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
352 "lea (%1, %1), %%edx \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
353 "cmovb %%eax, %0 \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
354 "cmovb %%edx, %1 \n\t" |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
355 : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2) |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
356 ); |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
357 #endif |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
358 #else |
|
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
359 //P3:675 |
| 3642 | 360 int shift= (uint32_t)(c->range - (0x200 << CABAC_BITS))>>31; |
| 361 c->range<<= shift; | |
| 362 c->low <<= shift; | |
|
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
363 #endif |
| 2323 | 364 if(!(c->low & CABAC_MASK)) |
| 365 refill(c); | |
| 366 } | |
| 367 | |
|
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
|
368 static int get_cabac(CABACContext *c, uint8_t * const state){ |
| 3642 | 369 //FIXME gcc generates duplicate load/stores for c->low and c->range |
|
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
370 START_TIMER |
| 3642 | 371 int s = *state; |
| 372 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
|
373 int bit, lps_mask attribute_unused; |
| 2967 | 374 |
| 1287 | 375 c->range -= RangeLPS; |
| 2323 | 376 #if 1 |
| 1287 | 377 if(c->low < c->range){ |
| 3642 | 378 bit= s&1; |
| 379 *state= c->mps_state[s]; | |
| 2323 | 380 renorm_cabac_decoder_once(c); |
| 1287 | 381 }else{ |
| 2323 | 382 // int shift= ff_h264_norm_shift[RangeLPS>>17]; |
| 3642 | 383 bit= (s&1)^1; |
| 1287 | 384 c->low -= c->range; |
| 3642 | 385 *state= c->lps_state[s]; |
| 1287 | 386 c->range = RangeLPS; |
| 2323 | 387 renorm_cabac_decoder(c); |
| 388 /* c->range = RangeLPS<<shift; | |
| 389 c->low <<= shift; | |
| 390 if(!(c->low & 0xFFFF)){ | |
| 391 refill2(c); | |
| 392 }*/ | |
| 1287 | 393 } |
| 2323 | 394 #else |
| 395 lps_mask= (c->range - c->low)>>31; | |
| 2967 | 396 |
| 2323 | 397 c->low -= c->range & lps_mask; |
| 398 c->range += (RangeLPS - c->range) & lps_mask; | |
| 2967 | 399 |
| 3642 | 400 bit= (s^lps_mask)&1; |
| 401 *state= c->mps_state[s - (128&lps_mask)]; | |
| 2967 | 402 |
| 2323 | 403 lps_mask= ff_h264_norm_shift[c->range>>(CABAC_BITS+2)]; |
| 404 c->range<<= lps_mask; | |
| 405 c->low <<= lps_mask; | |
| 406 if(!(c->low & CABAC_MASK)) | |
| 407 refill2(c); | |
| 408 #endif | |
|
3943
811a9b0d9f32
several x86 renorm_cabac_decoder_once optimizations
michael
parents:
3928
diff
changeset
|
409 STOP_TIMER("get_cabac") |
| 2967 | 410 return bit; |
| 1287 | 411 } |
| 412 | |
|
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
|
413 static int get_cabac_bypass(CABACContext *c){ |
| 1287 | 414 c->low += c->low; |
| 415 | |
| 2323 | 416 if(!(c->low & CABAC_MASK)) |
| 417 refill(c); | |
| 2967 | 418 |
| 1287 | 419 if(c->low < c->range){ |
| 420 return 0; | |
| 421 }else{ | |
| 422 c->low -= c->range; | |
| 423 return 1; | |
| 424 } | |
| 425 } | |
| 426 | |
|
1300
e18667d1e94d
FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents:
1298
diff
changeset
|
427 /** |
|
e18667d1e94d
FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents:
1298
diff
changeset
|
428 * |
|
e18667d1e94d
FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents:
1298
diff
changeset
|
429 * @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
|
430 */ |
|
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
|
431 static int get_cabac_terminate(CABACContext *c){ |
| 2323 | 432 c->range -= 4<<CABAC_BITS; |
| 1287 | 433 if(c->low < c->range){ |
| 2323 | 434 renorm_cabac_decoder_once(c); |
| 1287 | 435 return 0; |
| 436 }else{ | |
|
1300
e18667d1e94d
FFV1 codec (our very simple lossless intra only codec, compresses much better then huffyuv)
michaelni
parents:
1298
diff
changeset
|
437 return c->bytestream - c->bytestream_start; |
| 2967 | 438 } |
| 1287 | 439 } |
| 440 | |
| 1290 | 441 /** |
| 442 * get (truncated) unnary binarization. | |
| 443 */ | |
|
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
|
444 static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){ |
| 1290 | 445 int i; |
| 2967 | 446 |
| 447 for(i=0; i<max; i++){ | |
| 1290 | 448 if(get_cabac(c, state)==0) |
| 449 return i; | |
| 2967 | 450 |
| 1290 | 451 if(i< max_index) state++; |
| 452 } | |
| 453 | |
| 454 return truncated ? max : -1; | |
| 455 } | |
| 456 | |
| 457 /** | |
| 458 * get unary exp golomb k-th order binarization. | |
| 459 */ | |
|
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
|
460 static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){ |
| 1290 | 461 int i, v; |
| 462 int m= 1<<k; | |
| 2967 | 463 |
| 464 if(get_cabac(c, state)==0) | |
| 1290 | 465 return 0; |
| 2967 | 466 |
| 1290 | 467 if(0 < max_index) state++; |
| 2967 | 468 |
| 469 for(i=1; i<max; i++){ | |
| 1290 | 470 if(get_cabac(c, state)==0){ |
| 471 if(is_signed && get_cabac_bypass(c)){ | |
| 472 return -i; | |
| 473 }else | |
| 474 return i; | |
| 475 } | |
| 476 | |
| 477 if(i < max_index) state++; | |
| 478 } | |
| 2967 | 479 |
| 1290 | 480 while(get_cabac_bypass(c)){ |
| 481 i+= m; | |
| 482 m+= m; | |
| 483 } | |
| 2967 | 484 |
| 1290 | 485 v=0; |
| 486 while(m>>=1){ | |
| 487 v+= v + get_cabac_bypass(c); | |
| 488 } | |
| 489 i += v; | |
| 490 | |
| 491 if(is_signed && get_cabac_bypass(c)){ | |
| 492 return -i; | |
| 493 }else | |
| 494 return i; | |
| 495 } |
