826e281ca601e54fde154961331b3f0415bca999
[ffmpeg.git] / 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     asm volatile(
456         "movzbl (%1), %0                        \n\t"
457         "movl "RANGE    "(%2), %%ebx            \n\t"
458         "movl "RANGE    "(%2), %%edx            \n\t"
459         "andl $0xC0, %%ebx                      \n\t"
460         "movzbl "MANGLE(ff_h264_lps_range)"(%0, %%ebx, 2), %%esi\n\t"
461         "movl "LOW      "(%2), %%ebx            \n\t"
462 //eax:state ebx:low, edx:range, esi:RangeLPS
463         "subl %%esi, %%edx                      \n\t"
464 #if (defined CMOV_IS_FAST  && __CPU__ >= 686)
465         "movl %%edx, %%ecx                      \n\t"
466         "shl $17, %%edx                         \n\t"
467         "cmpl %%ebx, %%edx                      \n\t"
468         "cmova %%ecx, %%esi                     \n\t"
469         "sbbl %%ecx, %%ecx                      \n\t"
470         "andl %%ecx, %%edx                      \n\t"
471         "subl %%edx, %%ebx                      \n\t"
472         "xorl %%ecx, %0                         \n\t"
473 #else /* CMOV_IS_FAST */
474         "movl %%edx, %%ecx                      \n\t"
475         "shl $17, %%edx                         \n\t"
476         "subl %%ebx, %%edx                      \n\t"
477         "sarl $31, %%edx                        \n\t" //lps_mask
478         "subl %%ecx, %%esi                      \n\t" //RangeLPS - range
479         "andl %%edx, %%esi                      \n\t" //(RangeLPS - range)&lps_mask
480         "addl %%ecx, %%esi                      \n\t" //new range
481         "shl $17, %%ecx                         \n\t"
482         "andl %%edx, %%ecx                      \n\t"
483         "subl %%ecx, %%ebx                      \n\t"
484         "xorl %%edx, %0                         \n\t"
485 #endif /* CMOV_IS_FAST */
486
487 //eax:state ebx:low edx:mask esi:range
488
489 //eax:bit ebx:low esi:range
490
491         "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx   \n\t"
492         "shll %%cl, %%esi                       \n\t"
493         "movzbl "MANGLE(ff_h264_mlps_state)"+128(%0), %%edx   \n\t"
494         "movb %%dl, (%1)                        \n\t"
495         "movl %%esi, "RANGE    "(%2)            \n\t"
496         "shll %%cl, %%ebx                       \n\t"
497         "movl %%ebx, "LOW      "(%2)            \n\t"
498         "test %%bx, %%bx                        \n\t"
499         " jnz 1f                                \n\t"
500
501         "movl "BYTE     "(%2), %%ecx            \n\t"
502         "movzwl (%%ecx), %%esi                  \n\t"
503         "bswap %%esi                            \n\t"
504         "shrl $15, %%esi                        \n\t"
505         "subl $0xFFFF, %%esi                    \n\t"
506         "addl $2, %%ecx                         \n\t"
507         "movl %%ecx, "BYTE    "(%2)             \n\t"
508
509         "leal -1(%%ebx), %%ecx                  \n\t"
510         "xorl %%ebx, %%ecx                      \n\t"
511         "shrl $15, %%ecx                        \n\t"
512         "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx   \n\t"
513         "neg %%ecx                              \n\t"
514         "add $7, %%ecx                          \n\t"
515
516         "shll %%cl , %%esi                      \n\t"
517         "addl %%esi, %%ebx                      \n\t"
518         "movl %%ebx, "LOW      "(%2)            \n\t"
519         "1:                                     \n\t"
520         :"=&a"(bit)
521         :"r"(state), "r"(c)
522         : "%ecx", "%ebx", "%edx", "%esi", "memory"
523     );
524     bit&=1;
525 #endif /* BRANCHLESS_CABAC_DECODER */
526 #else /* defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__)) */
527     int s = *state;
528     int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
529     int bit, lps_mask attribute_unused;
530
531     c->range -= RangeLPS;
532 #ifndef BRANCHLESS_CABAC_DECODER
533     if(c->low < (c->range<<17)){
534         bit= s&1;
535         *state= ff_h264_mps_state[s];
536         renorm_cabac_decoder_once(c);
537     }else{
538         bit= ff_h264_norm_shift[RangeLPS];
539         c->low -= (c->range<<17);
540         *state= ff_h264_lps_state[s];
541         c->range = RangeLPS<<bit;
542         c->low <<= bit;
543         bit= (s&1)^1;
544
545         if(!(c->low & 0xFFFF)){
546             refill2(c);
547         }
548     }
549 #else /* BRANCHLESS_CABAC_DECODER */
550     lps_mask= ((c->range<<17) - c->low)>>31;
551
552     c->low -= (c->range<<17) & lps_mask;
553     c->range += (RangeLPS - c->range) & lps_mask;
554
555     s^=lps_mask;
556     *state= (ff_h264_mlps_state+128)[s];
557     bit= s&1;
558
559     lps_mask= ff_h264_norm_shift[c->range];
560     c->range<<= lps_mask;
561     c->low  <<= lps_mask;
562     if(!(c->low & CABAC_MASK))
563         refill2(c);
564 #endif /* BRANCHLESS_CABAC_DECODER */
565 #endif /* defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__)) */
566     return bit;
567 }
568
569 static int __attribute((noinline)) get_cabac_noinline(CABACContext *c, uint8_t * const state){
570     return get_cabac_inline(c,state);
571 }
572
573 static int get_cabac(CABACContext *c, uint8_t * const state){
574     return get_cabac_inline(c,state);
575 }
576
577 static int get_cabac_bypass(CABACContext *c){
578 #if 0 //not faster
579     int bit;
580     asm volatile(
581         "movl "RANGE    "(%1), %%ebx            \n\t"
582         "movl "LOW      "(%1), %%eax            \n\t"
583         "shl $17, %%ebx                         \n\t"
584         "add %%eax, %%eax                       \n\t"
585         "sub %%ebx, %%eax                       \n\t"
586         "cdq                                    \n\t"
587         "and %%edx, %%ebx                       \n\t"
588         "add %%ebx, %%eax                       \n\t"
589         "test %%ax, %%ax                        \n\t"
590         " jnz 1f                                \n\t"
591         "movl "BYTE     "(%1), %%ebx            \n\t"
592         "subl $0xFFFF, %%eax                    \n\t"
593         "movzwl (%%ebx), %%ecx                  \n\t"
594         "bswap %%ecx                            \n\t"
595         "shrl $15, %%ecx                        \n\t"
596         "addl $2, %%ebx                         \n\t"
597         "addl %%ecx, %%eax                      \n\t"
598         "movl %%ebx, "BYTE     "(%1)            \n\t"
599         "1:                                     \n\t"
600         "movl %%eax, "LOW      "(%1)            \n\t"
601
602         :"=&d"(bit)
603         :"r"(c)
604         : "%eax", "%ebx", "%ecx", "memory"
605     );
606     return bit+1;
607 #else
608     int range;
609     c->low += c->low;
610
611     if(!(c->low & CABAC_MASK))
612         refill(c);
613
614     range= c->range<<17;
615     if(c->low < range){
616         return 0;
617     }else{
618         c->low -= range;
619         return 1;
620     }
621 #endif
622 }
623
624
625 static always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
626 #ifdef ARCH_X86
627     asm volatile(
628         "movl "RANGE    "(%1), %%ebx            \n\t"
629         "movl "LOW      "(%1), %%eax            \n\t"
630         "shl $17, %%ebx                         \n\t"
631         "add %%eax, %%eax                       \n\t"
632         "sub %%ebx, %%eax                       \n\t"
633         "cdq                                    \n\t"
634         "and %%edx, %%ebx                       \n\t"
635         "add %%ebx, %%eax                       \n\t"
636         "xor %%edx, %%ecx                       \n\t"
637         "sub %%edx, %%ecx                       \n\t"
638         "test %%ax, %%ax                        \n\t"
639         " jnz 1f                                \n\t"
640         "movl "BYTE     "(%1), %%ebx            \n\t"
641         "subl $0xFFFF, %%eax                    \n\t"
642         "movzwl (%%ebx), %%edx                  \n\t"
643         "bswap %%edx                            \n\t"
644         "shrl $15, %%edx                        \n\t"
645         "addl $2, %%ebx                         \n\t"
646         "addl %%edx, %%eax                      \n\t"
647         "movl %%ebx, "BYTE     "(%1)            \n\t"
648         "1:                                     \n\t"
649         "movl %%eax, "LOW      "(%1)            \n\t"
650
651         :"+c"(val)
652         :"r"(c)
653         : "%eax", "%ebx", "%edx", "memory"
654     );
655     return val;
656 #else
657     int range, mask;
658     c->low += c->low;
659
660     if(!(c->low & CABAC_MASK))
661         refill(c);
662
663     range= c->range<<17;
664     c->low -= range;
665     mask= c->low >> 31;
666     range &= mask;
667     c->low += range;
668     return (val^mask)-mask;
669 #endif
670 }
671
672 //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!)
673 //FIXME use some macros to avoid duplicatin get_cabac (cant be done yet as that would make optimization work hard)
674 #ifdef ARCH_X86
675 static int decode_significance_x86(CABACContext *c, int max_coeff, uint8_t *significant_coeff_ctx_base, int *index){
676     void *end= significant_coeff_ctx_base + max_coeff - 1;
677     int minusstart= -(int)significant_coeff_ctx_base;
678     int minusindex= -(int)index;
679     int coeff_count;
680     asm volatile(
681         "movl "RANGE    "(%3), %%esi            \n\t"
682         "movl "LOW      "(%3), %%ebx            \n\t"
683
684         "2:                                     \n\t"
685
686         "movzbl (%1), %0                        \n\t"
687         "movl %%esi, %%edx                      \n\t"
688         "andl $0xC0, %%esi                      \n\t"
689         "movzbl "MANGLE(ff_h264_lps_range)"(%0, %%esi, 2), %%esi\n\t"
690 /*eax:state ebx:low, edx:range, esi:RangeLPS*/
691         "subl %%esi, %%edx                      \n\t"
692
693 #if (defined CMOV_IS_FAST  && __CPU__ >= 686)
694         "movl %%edx, %%ecx                      \n\t"
695         "shl $17, %%edx                         \n\t"
696         "cmpl %%ebx, %%edx                      \n\t"
697         "cmova %%ecx, %%esi                     \n\t"
698         "sbbl %%ecx, %%ecx                      \n\t"
699         "andl %%ecx, %%edx                      \n\t"
700         "subl %%edx, %%ebx                      \n\t"
701         "xorl %%ecx, %0                         \n\t"
702 #else /* CMOV_IS_FAST */
703         "movl %%edx, %%ecx                      \n\t"
704         "shl $17, %%edx                         \n\t"
705         "subl %%ebx, %%edx                      \n\t"
706         "sarl $31, %%edx                        \n\t" //lps_mask
707         "subl %%ecx, %%esi                      \n\t" //RangeLPS - range
708         "andl %%edx, %%esi                      \n\t" //(RangeLPS - range)&lps_mask
709         "addl %%ecx, %%esi                      \n\t" //new range
710         "shl $17, %%ecx                         \n\t"
711         "andl %%edx, %%ecx                      \n\t"
712         "subl %%ecx, %%ebx                      \n\t"
713         "xorl %%edx, %0                         \n\t"
714 #endif /* CMOV_IS_FAST */
715
716         "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx   \n\t"
717         "shll %%cl, %%esi                       \n\t"
718         "movzbl "MANGLE(ff_h264_mlps_state)"+128(%0), %%edx   \n\t"
719         "movb %%dl, (%1)                        \n\t"
720         "shll %%cl, %%ebx                       \n\t"
721         "test %%bx, %%bx                        \n\t"
722         " jnz 1f                                \n\t"
723
724         "movl "BYTE     "(%3), %%ecx            \n\t"
725         "movzwl (%%ecx), %%edx                  \n\t"
726         "bswap %%edx                            \n\t"
727         "shrl $15, %%edx                        \n\t"
728         "subl $0xFFFF, %%edx                    \n\t"
729         "addl $2, %%ecx                         \n\t"
730         "movl %%ecx, "BYTE    "(%3)             \n\t"
731
732         "leal -1(%%ebx), %%ecx                  \n\t"
733         "xorl %%ebx, %%ecx                      \n\t"
734         "shrl $15, %%ecx                        \n\t"
735         "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx   \n\t"
736         "neg %%ecx                              \n\t"
737         "add $7, %%ecx                          \n\t"
738
739         "shll %%cl , %%edx                      \n\t"
740         "addl %%edx, %%ebx                      \n\t"
741         "1:                                     \n\t"
742
743         "test $1, %0                            \n\t"
744         " jz 3f                                 \n\t"
745
746         "movl %2, %%eax                         \n\t"
747         "movl %4, %%ecx                         \n\t"
748         "addl %1, %%ecx                         \n\t"
749         "movl %%ecx, (%%eax)                    \n\t"
750         "addl $4, %%eax                         \n\t"
751         "movl %%eax, %2                         \n\t"
752
753         "movzbl 61(%1), %0                      \n\t"
754         "movl %%esi, %%edx                      \n\t"
755         "andl $0xC0, %%esi                      \n\t"
756         "movzbl "MANGLE(ff_h264_lps_range)"(%0, %%esi, 2), %%esi\n\t"
757 /*eax:state ebx:low, edx:range, esi:RangeLPS*/
758         "subl %%esi, %%edx                      \n\t"
759
760 #if (defined CMOV_IS_FAST  && __CPU__ >= 686)
761         "movl %%edx, %%ecx                      \n\t"
762         "shl $17, %%edx                         \n\t"
763         "cmpl %%ebx, %%edx                      \n\t"
764         "cmova %%ecx, %%esi                     \n\t"
765         "sbbl %%ecx, %%ecx                      \n\t"
766         "andl %%ecx, %%edx                      \n\t"
767         "subl %%edx, %%ebx                      \n\t"
768         "xorl %%ecx, %0                         \n\t"
769 #else /* CMOV_IS_FAST */
770         "movl %%edx, %%ecx                      \n\t"
771         "shl $17, %%edx                         \n\t"
772         "subl %%ebx, %%edx                      \n\t"
773         "sarl $31, %%edx                        \n\t" //lps_mask
774         "subl %%ecx, %%esi                      \n\t" //RangeLPS - range
775         "andl %%edx, %%esi                      \n\t" //(RangeLPS - range)&lps_mask
776         "addl %%ecx, %%esi                      \n\t" //new range
777         "shl $17, %%ecx                         \n\t"
778         "andl %%edx, %%ecx                      \n\t"
779         "subl %%ecx, %%ebx                      \n\t"
780         "xorl %%edx, %0                         \n\t"
781 #endif /* CMOV_IS_FAST */
782
783         "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx   \n\t"
784         "shll %%cl, %%esi                       \n\t"
785         "movzbl "MANGLE(ff_h264_mlps_state)"+128(%0), %%edx   \n\t"
786         "movb %%dl, 61(%1)                      \n\t"
787         "shll %%cl, %%ebx                       \n\t"
788         "test %%bx, %%bx                        \n\t"
789         " jnz 1f                                \n\t"
790
791         "movl "BYTE     "(%3), %%ecx            \n\t"
792         "movzwl (%%ecx), %%edx                  \n\t"
793         "bswap %%edx                            \n\t"
794         "shrl $15, %%edx                        \n\t"
795         "subl $0xFFFF, %%edx                    \n\t"
796         "addl $2, %%ecx                         \n\t"
797         "movl %%ecx, "BYTE    "(%3)             \n\t"
798
799         "leal -1(%%ebx), %%ecx                  \n\t"
800         "xorl %%ebx, %%ecx                      \n\t"
801         "shrl $15, %%ecx                        \n\t"
802         "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx   \n\t"
803         "neg %%ecx                              \n\t"
804         "add $7, %%ecx                          \n\t"
805
806         "shll %%cl , %%edx                      \n\t"
807         "addl %%edx, %%ebx                      \n\t"
808         "1:                                     \n\t"
809
810         "test $1, %%eax                         \n\t"
811         " jnz 4f                                \n\t"
812
813         "3:                                     \n\t"
814         "addl $1, %1                            \n\t"
815         "cmpl %5, %1                            \n\t"
816         " jb 2b                                 \n\t"
817         "movl %2, %%eax                         \n\t"
818         "movl %4, %%ecx                         \n\t"
819         "addl %1, %%ecx                         \n\t"
820         "movl %%ecx, (%%eax)                    \n\t"
821         "addl $4, %%eax                         \n\t"
822         "movl %%eax, %2                         \n\t"
823         "4:                                     \n\t"
824         "movl %2, %%eax                         \n\t"
825         "addl %6, %%eax                         \n\t"
826         "shr $2, %%eax                          \n\t"
827
828         "movl %%esi, "RANGE    "(%3)            \n\t"
829         "movl %%ebx, "LOW      "(%3)            \n\t"
830         :"=&a"(coeff_count), "+r"(significant_coeff_ctx_base), "+m"(index)\
831         :"r"(c), "m"(minusstart), "m"(end), "m"(minusindex)\
832         : "%ecx", "%ebx", "%edx", "%esi", "memory"\
833     );
834     return coeff_count;
835 }
836 #endif
837
838 /**
839  *
840  * @return the number of bytes read or 0 if no end
841  */
842 static int get_cabac_terminate(CABACContext *c){
843     c->range -= 2;
844     if(c->low < c->range<<17){
845         renorm_cabac_decoder_once(c);
846         return 0;
847     }else{
848         return c->bytestream - c->bytestream_start;
849     }
850 }
851
852 /**
853  * get (truncated) unnary binarization.
854  */
855 static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
856     int i;
857
858     for(i=0; i<max; i++){
859         if(get_cabac(c, state)==0)
860             return i;
861
862         if(i< max_index) state++;
863     }
864
865     return truncated ? max : -1;
866 }
867
868 /**
869  * get unary exp golomb k-th order binarization.
870  */
871 static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
872     int i, v;
873     int m= 1<<k;
874
875     if(get_cabac(c, state)==0)
876         return 0;
877
878     if(0 < max_index) state++;
879
880     for(i=1; i<max; i++){
881         if(get_cabac(c, state)==0){
882             if(is_signed && get_cabac_bypass(c)){
883                 return -i;
884             }else
885                 return i;
886         }
887
888         if(i < max_index) state++;
889     }
890
891     while(get_cabac_bypass(c)){
892         i+= m;
893         m+= m;
894     }
895
896     v=0;
897     while(m>>=1){
898         v+= v + get_cabac_bypass(c);
899     }
900     i += v;
901
902     if(is_signed && get_cabac_bypass(c)){
903         return -i;
904     }else
905         return i;
906 }