]> git.sesse.net Git - ffmpeg/blob - libavcodec/cabac.h
e191a621247d17a9795d7efacbb822ad709ada1f
[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 /**
24  * @file cabac.h
25  * Context Adaptive Binary Arithmetic Coder.
26  */
27
28
29 //#undef NDEBUG
30 #include <assert.h>
31
32 #define CABAC_BITS 16
33 #define CABAC_MASK ((1<<CABAC_BITS)-1)
34 #define BRANCHLESS_CABAC_DECODER 1
35 #define CMOV_IS_FAST 1
36 //#define ARCH_X86_DISABLED 1
37
38 typedef struct CABACContext{
39     int low;
40     int range;
41     int outstanding_count;
42 #ifdef STRICT_LIMITS
43     int symCount;
44 #endif
45     const uint8_t *bytestream_start;
46     const uint8_t *bytestream;
47     const uint8_t *bytestream_end;
48     PutBitContext pb;
49 }CABACContext;
50
51 extern uint8_t ff_h264_mlps_state[4*64];
52 extern uint8_t ff_h264_lps_range[4*2*64];  ///< rangeTabLPS
53 extern uint8_t ff_h264_mps_state[2*64];     ///< transIdxMPS
54 extern uint8_t ff_h264_lps_state[2*64];     ///< transIdxLPS
55 extern const uint8_t ff_h264_norm_shift[512];
56
57
58 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
59 void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
60 void ff_init_cabac_states(CABACContext *c);
61
62
63 static inline void put_cabac_bit(CABACContext *c, int b){
64     put_bits(&c->pb, 1, b);
65     for(;c->outstanding_count; c->outstanding_count--){
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         }
82
83         c->range+= c->range;
84         c->low += c->low;
85     }
86 }
87
88 static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
89     int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
90
91     if(bit == ((*state)&1)){
92         c->range -= RangeLPS;
93         *state= ff_h264_mps_state[*state];
94     }else{
95         c->low += c->range - RangeLPS;
96         c->range = RangeLPS;
97         *state= ff_h264_lps_state[*state];
98     }
99
100     renorm_cabac_encoder(c);
101
102 #ifdef STRICT_LIMITS
103     c->symCount++;
104 #endif
105 }
106
107 static void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
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
124 /**
125  * @param bit 0 -> write zero bit, !=0 write one bit
126  */
127 static void put_cabac_bypass(CABACContext *c, int bit){
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     }
143
144 #ifdef STRICT_LIMITS
145     c->symCount++;
146 #endif
147 }
148
149 /**
150  *
151  * @return the number of bytes written
152  */
153 static int put_cabac_terminate(CABACContext *c, int bit){
154     c->range -= 2;
155
156     if(!bit){
157         renorm_cabac_encoder(c);
158     }else{
159         c->low += c->range;
160         c->range= 2;
161
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);
167
168         flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
169     }
170
171 #ifdef STRICT_LIMITS
172     c->symCount++;
173 #endif
174
175     return (put_bits_count(&c->pb)+7)>>3;
176 }
177
178 /**
179  * put (truncated) unary binarization.
180  */
181 static void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
182     int i;
183
184     assert(v <= max);
185
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  */
216 static void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
217     int i;
218
219     if(v==0)
220         put_cabac(c, state, 0);
221     else{
222         const int sign= v < 0;
223
224         if(is_signed) v= FFABS(v);
225
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
258 static void refill(CABACContext *c){
259 #if CABAC_BITS == 16
260         c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
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);
272     i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
273
274     x= -CABAC_MASK;
275
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
281
282     c->low += x<<i;
283     c->bytestream+= CABAC_BITS/8;
284 }
285
286 static inline void renorm_cabac_decoder(CABACContext *c){
287     while(c->range < 0x100){
288         c->range+= c->range;
289         c->low+= c->low;
290         if(!(c->low & CABAC_MASK))
291             refill(c);
292     }
293 }
294
295 static inline void renorm_cabac_decoder_once(CABACContext *c){
296 #ifdef ARCH_X86_DISABLED
297     int temp;
298 #if 0
299     //P3:683    athlon:475
300     asm(
301         "lea -0x100(%0), %2         \n\t"
302         "shr $31, %2                \n\t"  //FIXME 31->63 for x86-64
303         "shl %%cl, %0               \n\t"
304         "shl %%cl, %1               \n\t"
305         : "+r"(c->range), "+r"(c->low), "+c"(temp)
306     );
307 #elif 0
308     //P3:680    athlon:474
309     asm(
310         "cmp $0x100, %0             \n\t"
311         "setb %%cl                  \n\t"  //FIXME 31->63 for x86-64
312         "shl %%cl, %0               \n\t"
313         "shl %%cl, %1               \n\t"
314         : "+r"(c->range), "+r"(c->low), "+c"(temp)
315     );
316 #elif 1
317     int temp2;
318     //P3:665    athlon:517
319     asm(
320         "lea -0x100(%0), %%eax      \n\t"
321         "cdq                        \n\t"
322         "mov %0, %%eax              \n\t"
323         "and %%edx, %0              \n\t"
324         "and %1, %%edx              \n\t"
325         "add %%eax, %0              \n\t"
326         "add %%edx, %1              \n\t"
327         : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
328     );
329 #elif 0
330     int temp2;
331     //P3:673    athlon:509
332     asm(
333         "cmp $0x100, %0             \n\t"
334         "sbb %%edx, %%edx           \n\t"
335         "mov %0, %%eax              \n\t"
336         "and %%edx, %0              \n\t"
337         "and %1, %%edx              \n\t"
338         "add %%eax, %0              \n\t"
339         "add %%edx, %1              \n\t"
340         : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
341     );
342 #else
343     int temp2;
344     //P3:677    athlon:511
345     asm(
346         "cmp $0x100, %0             \n\t"
347         "lea (%0, %0), %%eax        \n\t"
348         "lea (%1, %1), %%edx        \n\t"
349         "cmovb %%eax, %0            \n\t"
350         "cmovb %%edx, %1            \n\t"
351         : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
352     );
353 #endif
354 #else
355     //P3:675    athlon:476
356     int shift= (uint32_t)(c->range - 0x100)>>31;
357     c->range<<= shift;
358     c->low  <<= shift;
359 #endif
360     if(!(c->low & CABAC_MASK))
361         refill(c);
362 }
363
364 static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state){
365     //FIXME gcc generates duplicate load/stores for c->low and c->range
366 #define LOW          "0"
367 #define RANGE        "4"
368 #define BYTESTART   "12"
369 #define BYTE        "16"
370 #define BYTEEND     "20"
371 #if defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__))
372     int bit;
373
374 #ifndef BRANCHLESS_CABAC_DECODER
375     asm volatile(
376         "movzbl (%1), %0                        \n\t"
377         "movl "RANGE    "(%2), %%ebx            \n\t"
378         "movl "RANGE    "(%2), %%edx            \n\t"
379         "andl $0xC0, %%ebx                      \n\t"
380         "movzbl "MANGLE(ff_h264_lps_range)"(%0, %%ebx, 2), %%esi\n\t"
381         "movl "LOW      "(%2), %%ebx            \n\t"
382 //eax:state ebx:low, edx:range, esi:RangeLPS
383         "subl %%esi, %%edx                      \n\t"
384         "movl %%edx, %%ecx                      \n\t"
385         "shll $17, %%ecx                        \n\t"
386         "cmpl %%ecx, %%ebx                      \n\t"
387         " ja 1f                                 \n\t"
388
389 #if 1
390         //athlon:4067 P3:4110
391         "lea -0x100(%%edx), %%ecx               \n\t"
392         "shr $31, %%ecx                         \n\t"
393         "shl %%cl, %%edx                        \n\t"
394         "shl %%cl, %%ebx                        \n\t"
395 #else
396         //athlon:4057 P3:4130
397         "cmp $0x100, %%edx                      \n\t" //FIXME avoidable
398         "setb %%cl                              \n\t"
399         "shl %%cl, %%edx                        \n\t"
400         "shl %%cl, %%ebx                        \n\t"
401 #endif
402         "movzbl "MANGLE(ff_h264_mps_state)"(%0), %%ecx   \n\t"
403         "movb %%cl, (%1)                        \n\t"
404 //eax:state ebx:low, edx:range, esi:RangeLPS
405         "test %%bx, %%bx                        \n\t"
406         " jnz 2f                                \n\t"
407         "movl "BYTE     "(%2), %%esi            \n\t"
408         "subl $0xFFFF, %%ebx                    \n\t"
409         "movzwl (%%esi), %%ecx                  \n\t"
410         "bswap %%ecx                            \n\t"
411         "shrl $15, %%ecx                        \n\t"
412         "addl $2, %%esi                         \n\t"
413         "addl %%ecx, %%ebx                      \n\t"
414         "movl %%esi, "BYTE    "(%2)             \n\t"
415         "jmp 2f                                 \n\t"
416         "1:                                     \n\t"
417 //eax:state ebx:low, edx:range, esi:RangeLPS
418         "subl %%ecx, %%ebx                      \n\t"
419         "movl %%esi, %%edx                      \n\t"
420         "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx   \n\t"
421         "shll %%cl, %%ebx                       \n\t"
422         "shll %%cl, %%edx                       \n\t"
423         "movzbl "MANGLE(ff_h264_lps_state)"(%0), %%ecx   \n\t"
424         "movb %%cl, (%1)                        \n\t"
425         "addl $1, %0                            \n\t"
426         "test %%bx, %%bx                        \n\t"
427         " jnz 2f                                \n\t"
428
429         "movl "BYTE     "(%2), %%ecx            \n\t"
430         "movzwl (%%ecx), %%esi                  \n\t"
431         "bswap %%esi                            \n\t"
432         "shrl $15, %%esi                        \n\t"
433         "subl $0xFFFF, %%esi                    \n\t"
434         "addl $2, %%ecx                         \n\t"
435         "movl %%ecx, "BYTE    "(%2)             \n\t"
436
437         "leal -1(%%ebx), %%ecx                  \n\t"
438         "xorl %%ebx, %%ecx                      \n\t"
439         "shrl $15, %%ecx                        \n\t"
440         "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx   \n\t"
441         "neg %%ecx                              \n\t"
442         "add $7, %%ecx                          \n\t"
443
444         "shll %%cl , %%esi                      \n\t"
445         "addl %%esi, %%ebx                      \n\t"
446         "2:                                     \n\t"
447         "movl %%edx, "RANGE    "(%2)            \n\t"
448         "movl %%ebx, "LOW      "(%2)            \n\t"
449         :"=&a"(bit) //FIXME this is fragile gcc either runs out of registers or misscompiles it (for example if "+a"(bit) or "+m"(*state) is used
450         :"r"(state), "r"(c)
451         : "%ecx", "%ebx", "%edx", "%esi", "memory"
452     );
453     bit&=1;
454 #else /* BRANCHLESS_CABAC_DECODER */
455
456
457 #if (defined CMOV_IS_FAST  && __CPU__ >= 686)
458 #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
459         "mov    "tmp"       , %%ecx                                     \n\t"\
460         "shl    $17         , "tmp"                                     \n\t"\
461         "cmp    "low"       , "tmp"                                     \n\t"\
462         "cmova  %%ecx       , "range"                                   \n\t"\
463         "sbb    %%ecx       , %%ecx                                     \n\t"\
464         "and    %%ecx       , "tmp"                                     \n\t"\
465         "sub    "tmp"       , "low"                                     \n\t"\
466         "xor    %%ecx       , "ret"                                     \n\t"
467 #else /* CMOV_IS_FAST */
468 #define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
469         "mov    "tmp"       , %%ecx                                     \n\t"\
470         "shl    $17         , "tmp"                                     \n\t"\
471         "sub    "low"       , "tmp"                                     \n\t"\
472         "sar    $31         , "tmp"                                     \n\t" /*lps_mask*/\
473         "sub    %%ecx       , "range"                                   \n\t" /*RangeLPS - range*/\
474         "and    "tmp"       , "range"                                   \n\t" /*(RangeLPS - range)&lps_mask*/\
475         "add    %%ecx       , "range"                                   \n\t" /*new range*/\
476         "shl    $17         , %%ecx                                     \n\t"\
477         "and    "tmp"       , %%ecx                                     \n\t"\
478         "sub    %%ecx       , "low"                                     \n\t"\
479         "xor    "tmp"       , "ret"                                     \n\t"
480 #endif /* CMOV_IS_FAST */
481
482
483 #define BRANCHLESS_GET_CABAC(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
484         "movzbl "statep"    , "ret"                                     \n\t"\
485         "mov    "range"     , "tmp"                                     \n\t"\
486         "and    $0xC0       , "range"                                   \n\t"\
487         "movzbl "MANGLE(ff_h264_lps_range)"("ret", "range", 2), "range" \n\t"\
488         "sub    "range"     , "tmp"                                     \n\t"\
489         BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
490         "movzbl " MANGLE(ff_h264_norm_shift) "("range"), %%ecx          \n\t"\
491         "shl    %%cl        , "range"                                   \n\t"\
492         "movzbl "MANGLE(ff_h264_mlps_state)"+128("ret"), "tmp"          \n\t"\
493         "mov    "tmpbyte"   , "statep"                                  \n\t"\
494         "shl    %%cl        , "low"                                     \n\t"\
495         "test   "lowword"   , "lowword"                                 \n\t"\
496         " jnz   1f                                                      \n\t"\
497         "mov "BYTE"("cabac"), %%ecx                                     \n\t"\
498         "movzwl (%%ecx)     , "tmp"                                     \n\t"\
499         "bswap  "tmp"                                                   \n\t"\
500         "shr    $15         , "tmp"                                     \n\t"\
501         "sub    $0xFFFF     , "tmp"                                     \n\t"\
502         "add    $2          , %%ecx                                     \n\t"\
503         "mov    %%ecx       , "BYTE    "("cabac")                       \n\t"\
504         "lea    -1("low")   , %%ecx                                     \n\t"\
505         "xor    "low"       , %%ecx                                     \n\t"\
506         "shr    $15         , %%ecx                                     \n\t"\
507         "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx            \n\t"\
508         "neg    %%ecx                                                   \n\t"\
509         "add    $7          , %%ecx                                     \n\t"\
510         "shl    %%cl        , "tmp"                                     \n\t"\
511         "add    "tmp"       , "low"                                     \n\t"\
512         "1:                                                             \n\t"
513
514     asm volatile(
515         "movl "RANGE    "(%2), %%esi            \n\t"
516         "movl "LOW      "(%2), %%ebx            \n\t"
517         BRANCHLESS_GET_CABAC("%0", "%2", "(%1)", "%%ebx", "%%bx", "%%esi", "%%edx", "%%dl")
518         "movl %%esi, "RANGE    "(%2)            \n\t"
519         "movl %%ebx, "LOW      "(%2)            \n\t"
520
521         :"=&a"(bit)
522         :"r"(state), "r"(c)
523         : "%ecx", "%ebx", "%edx", "%esi", "memory"
524     );
525     bit&=1;
526 #endif /* BRANCHLESS_CABAC_DECODER */
527 #else /* defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__)) */
528     int s = *state;
529     int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
530     int bit, lps_mask attribute_unused;
531
532     c->range -= RangeLPS;
533 #ifndef BRANCHLESS_CABAC_DECODER
534     if(c->low < (c->range<<17)){
535         bit= s&1;
536         *state= ff_h264_mps_state[s];
537         renorm_cabac_decoder_once(c);
538     }else{
539         bit= ff_h264_norm_shift[RangeLPS];
540         c->low -= (c->range<<17);
541         *state= ff_h264_lps_state[s];
542         c->range = RangeLPS<<bit;
543         c->low <<= bit;
544         bit= (s&1)^1;
545
546         if(!(c->low & 0xFFFF)){
547             refill2(c);
548         }
549     }
550 #else /* BRANCHLESS_CABAC_DECODER */
551     lps_mask= ((c->range<<17) - c->low)>>31;
552
553     c->low -= (c->range<<17) & lps_mask;
554     c->range += (RangeLPS - c->range) & lps_mask;
555
556     s^=lps_mask;
557     *state= (ff_h264_mlps_state+128)[s];
558     bit= s&1;
559
560     lps_mask= ff_h264_norm_shift[c->range];
561     c->range<<= lps_mask;
562     c->low  <<= lps_mask;
563     if(!(c->low & CABAC_MASK))
564         refill2(c);
565 #endif /* BRANCHLESS_CABAC_DECODER */
566 #endif /* defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__)) */
567     return bit;
568 }
569
570 static int __attribute((noinline)) get_cabac_noinline(CABACContext *c, uint8_t * const state){
571     return get_cabac_inline(c,state);
572 }
573
574 static int get_cabac(CABACContext *c, uint8_t * const state){
575     return get_cabac_inline(c,state);
576 }
577
578 static int get_cabac_bypass(CABACContext *c){
579 #if 0 //not faster
580     int bit;
581     asm volatile(
582         "movl "RANGE    "(%1), %%ebx            \n\t"
583         "movl "LOW      "(%1), %%eax            \n\t"
584         "shl $17, %%ebx                         \n\t"
585         "add %%eax, %%eax                       \n\t"
586         "sub %%ebx, %%eax                       \n\t"
587         "cdq                                    \n\t"
588         "and %%edx, %%ebx                       \n\t"
589         "add %%ebx, %%eax                       \n\t"
590         "test %%ax, %%ax                        \n\t"
591         " jnz 1f                                \n\t"
592         "movl "BYTE     "(%1), %%ebx            \n\t"
593         "subl $0xFFFF, %%eax                    \n\t"
594         "movzwl (%%ebx), %%ecx                  \n\t"
595         "bswap %%ecx                            \n\t"
596         "shrl $15, %%ecx                        \n\t"
597         "addl $2, %%ebx                         \n\t"
598         "addl %%ecx, %%eax                      \n\t"
599         "movl %%ebx, "BYTE     "(%1)            \n\t"
600         "1:                                     \n\t"
601         "movl %%eax, "LOW      "(%1)            \n\t"
602
603         :"=&d"(bit)
604         :"r"(c)
605         : "%eax", "%ebx", "%ecx", "memory"
606     );
607     return bit+1;
608 #else
609     int range;
610     c->low += c->low;
611
612     if(!(c->low & CABAC_MASK))
613         refill(c);
614
615     range= c->range<<17;
616     if(c->low < range){
617         return 0;
618     }else{
619         c->low -= range;
620         return 1;
621     }
622 #endif
623 }
624
625
626 static always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
627 #ifdef ARCH_X86
628     asm volatile(
629         "movl "RANGE    "(%1), %%ebx            \n\t"
630         "movl "LOW      "(%1), %%eax            \n\t"
631         "shl $17, %%ebx                         \n\t"
632         "add %%eax, %%eax                       \n\t"
633         "sub %%ebx, %%eax                       \n\t"
634         "cdq                                    \n\t"
635         "and %%edx, %%ebx                       \n\t"
636         "add %%ebx, %%eax                       \n\t"
637         "xor %%edx, %%ecx                       \n\t"
638         "sub %%edx, %%ecx                       \n\t"
639         "test %%ax, %%ax                        \n\t"
640         " jnz 1f                                \n\t"
641         "movl "BYTE     "(%1), %%ebx            \n\t"
642         "subl $0xFFFF, %%eax                    \n\t"
643         "movzwl (%%ebx), %%edx                  \n\t"
644         "bswap %%edx                            \n\t"
645         "shrl $15, %%edx                        \n\t"
646         "addl $2, %%ebx                         \n\t"
647         "addl %%edx, %%eax                      \n\t"
648         "movl %%ebx, "BYTE     "(%1)            \n\t"
649         "1:                                     \n\t"
650         "movl %%eax, "LOW      "(%1)            \n\t"
651
652         :"+c"(val)
653         :"r"(c)
654         : "%eax", "%ebx", "%edx", "memory"
655     );
656     return val;
657 #else
658     int range, mask;
659     c->low += c->low;
660
661     if(!(c->low & CABAC_MASK))
662         refill(c);
663
664     range= c->range<<17;
665     c->low -= range;
666     mask= c->low >> 31;
667     range &= mask;
668     c->low += range;
669     return (val^mask)-mask;
670 #endif
671 }
672
673 //FIXME the x86 code from this file should be moved into i386/h264 or cabac something.c/h (note ill kill you if you move my code away from under my fingers before iam finished with it!)
674 //FIXME use some macros to avoid duplicatin get_cabac (cant be done yet as that would make optimization work hard)
675 #ifdef ARCH_X86
676 static int decode_significance_x86(CABACContext *c, int max_coeff, uint8_t *significant_coeff_ctx_base, int *index){
677     void *end= significant_coeff_ctx_base + max_coeff - 1;
678     int minusstart= -(int)significant_coeff_ctx_base;
679     int minusindex= 4-(int)index;
680     int coeff_count;
681     asm volatile(
682         "movl "RANGE    "(%3), %%esi            \n\t"
683         "movl "LOW      "(%3), %%ebx            \n\t"
684
685         "2:                                     \n\t"
686
687         BRANCHLESS_GET_CABAC("%%edx", "%3", "(%1)", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
688
689         "test $1, %%edx                         \n\t"
690         " jz 3f                                 \n\t"
691
692         BRANCHLESS_GET_CABAC("%%edx", "%3", "61(%1)", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
693
694         "movl %2, %%eax                         \n\t"
695         "movl %4, %%ecx                         \n\t"
696         "addl %1, %%ecx                         \n\t"
697         "movl %%ecx, (%%eax)                    \n\t"
698
699         "test $1, %%edx                         \n\t"
700         " jnz 4f                                \n\t"
701
702         "addl $4, %%eax                         \n\t"
703         "movl %%eax, %2                         \n\t"
704
705         "3:                                     \n\t"
706         "addl $1, %1                            \n\t"
707         "cmpl %5, %1                            \n\t"
708         " jb 2b                                 \n\t"
709         "movl %2, %%eax                         \n\t"
710         "movl %4, %%ecx                         \n\t"
711         "addl %1, %%ecx                         \n\t"
712         "movl %%ecx, (%%eax)                    \n\t"
713         "4:                                     \n\t"
714         "addl %6, %%eax                         \n\t"
715         "shr $2, %%eax                          \n\t"
716
717         "movl %%esi, "RANGE    "(%3)            \n\t"
718         "movl %%ebx, "LOW      "(%3)            \n\t"
719         :"=&a"(coeff_count), "+r"(significant_coeff_ctx_base), "+m"(index)\
720         :"r"(c), "m"(minusstart), "m"(end), "m"(minusindex)\
721         : "%ecx", "%ebx", "%edx", "%esi", "memory"\
722     );
723     return coeff_count;
724 }
725 #endif
726
727 /**
728  *
729  * @return the number of bytes read or 0 if no end
730  */
731 static int get_cabac_terminate(CABACContext *c){
732     c->range -= 2;
733     if(c->low < c->range<<17){
734         renorm_cabac_decoder_once(c);
735         return 0;
736     }else{
737         return c->bytestream - c->bytestream_start;
738     }
739 }
740
741 /**
742  * get (truncated) unnary binarization.
743  */
744 static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
745     int i;
746
747     for(i=0; i<max; i++){
748         if(get_cabac(c, state)==0)
749             return i;
750
751         if(i< max_index) state++;
752     }
753
754     return truncated ? max : -1;
755 }
756
757 /**
758  * get unary exp golomb k-th order binarization.
759  */
760 static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
761     int i, v;
762     int m= 1<<k;
763
764     if(get_cabac(c, state)==0)
765         return 0;
766
767     if(0 < max_index) state++;
768
769     for(i=1; i<max; i++){
770         if(get_cabac(c, state)==0){
771             if(is_signed && get_cabac_bypass(c)){
772                 return -i;
773             }else
774                 return i;
775         }
776
777         if(i < max_index) state++;
778     }
779
780     while(get_cabac_bypass(c)){
781         i+= m;
782         m+= m;
783     }
784
785     v=0;
786     while(m>>=1){
787         v+= v + get_cabac_bypass(c);
788     }
789     i += v;
790
791     if(is_signed && get_cabac_bypass(c)){
792         return -i;
793     }else
794         return i;
795 }