Statistics
| Branch: | Revision:

ffmpeg / libavcodec / cabac.h @ df3a80b5

History | View | Annotate | Download (28.7 KB)

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
 * @file cabac.h
24
 * Context Adaptive Binary Arithmetic Coder.
25
 */
26

    
27
#ifndef CABAC_H
28
#define CABAC_H
29

    
30
#include "bitstream.h"
31

    
32
//#undef NDEBUG
33
#include <assert.h>
34
#ifdef ARCH_X86
35
#include "x86_cpu.h"
36
#endif
37

    
38
#define CABAC_BITS 16
39
#define CABAC_MASK ((1<<CABAC_BITS)-1)
40
#define BRANCHLESS_CABAC_DECODER 1
41
//#define ARCH_X86_DISABLED 1
42

    
43
typedef struct CABACContext{
44
    int low;
45
    int range;
46
    int outstanding_count;
47
#ifdef STRICT_LIMITS
48
    int symCount;
49
#endif
50
    const uint8_t *bytestream_start;
51
    const uint8_t *bytestream;
52
    const uint8_t *bytestream_end;
53
    PutBitContext pb;
54
}CABACContext;
55

    
56
extern uint8_t ff_h264_mlps_state[4*64];
57
extern uint8_t ff_h264_lps_range[4*2*64];  ///< rangeTabLPS
58
extern uint8_t ff_h264_mps_state[2*64];     ///< transIdxMPS
59
extern uint8_t ff_h264_lps_state[2*64];     ///< transIdxLPS
60
extern const uint8_t ff_h264_norm_shift[512];
61

    
62

    
63
void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
64
void ff_init_cabac_decoder(CABACContext *c, const uint8_t *buf, int buf_size);
65
void ff_init_cabac_states(CABACContext *c);
66

    
67

    
68
static inline void put_cabac_bit(CABACContext *c, int b){
69
    put_bits(&c->pb, 1, b);
70
    for(;c->outstanding_count; c->outstanding_count--){
71
        put_bits(&c->pb, 1, 1-b);
72
    }
73
}
74

    
75
static inline void renorm_cabac_encoder(CABACContext *c){
76
    while(c->range < 0x100){
77
        //FIXME optimize
78
        if(c->low<0x100){
79
            put_cabac_bit(c, 0);
80
        }else if(c->low<0x200){
81
            c->outstanding_count++;
82
            c->low -= 0x100;
83
        }else{
84
            put_cabac_bit(c, 1);
85
            c->low -= 0x200;
86
        }
87

    
88
        c->range+= c->range;
89
        c->low += c->low;
90
    }
91
}
92

    
93
static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
94
    int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
95

    
96
    if(bit == ((*state)&1)){
97
        c->range -= RangeLPS;
98
        *state= ff_h264_mps_state[*state];
99
    }else{
100
        c->low += c->range - RangeLPS;
101
        c->range = RangeLPS;
102
        *state= ff_h264_lps_state[*state];
103
    }
104

    
105
    renorm_cabac_encoder(c);
106

    
107
#ifdef STRICT_LIMITS
108
    c->symCount++;
109
#endif
110
}
111

    
112
static void put_cabac_static(CABACContext *c, int RangeLPS, int bit){
113
    assert(c->range > RangeLPS);
114

    
115
    if(!bit){
116
        c->range -= RangeLPS;
117
    }else{
118
        c->low += c->range - RangeLPS;
119
        c->range = RangeLPS;
120
    }
121

    
122
    renorm_cabac_encoder(c);
123

    
124
#ifdef STRICT_LIMITS
125
    c->symCount++;
126
#endif
127
}
128

    
129
/**
130
 * @param bit 0 -> write zero bit, !=0 write one bit
131
 */
132
static void put_cabac_bypass(CABACContext *c, int bit){
133
    c->low += c->low;
134

    
135
    if(bit){
136
        c->low += c->range;
137
    }
138
//FIXME optimize
139
    if(c->low<0x200){
140
        put_cabac_bit(c, 0);
141
    }else if(c->low<0x400){
142
        c->outstanding_count++;
143
        c->low -= 0x200;
144
    }else{
145
        put_cabac_bit(c, 1);
146
        c->low -= 0x400;
147
    }
148

    
149
#ifdef STRICT_LIMITS
150
    c->symCount++;
151
#endif
152
}
153

    
154
/**
155
 *
156
 * @return the number of bytes written
157
 */
158
static int put_cabac_terminate(CABACContext *c, int bit){
159
    c->range -= 2;
160

    
161
    if(!bit){
162
        renorm_cabac_encoder(c);
163
    }else{
164
        c->low += c->range;
165
        c->range= 2;
166

    
167
        renorm_cabac_encoder(c);
168

    
169
        assert(c->low <= 0x1FF);
170
        put_cabac_bit(c, c->low>>9);
171
        put_bits(&c->pb, 2, ((c->low>>7)&3)|1);
172

    
173
        flush_put_bits(&c->pb); //FIXME FIXME FIXME XXX wrong
174
    }
175

    
176
#ifdef STRICT_LIMITS
177
    c->symCount++;
178
#endif
179

    
180
    return (put_bits_count(&c->pb)+7)>>3;
181
}
182

    
183
/**
184
 * put (truncated) unary binarization.
185
 */
186
static void put_cabac_u(CABACContext *c, uint8_t * state, int v, int max, int max_index, int truncated){
187
    int i;
188

    
189
    assert(v <= max);
190

    
191
#if 1
192
    for(i=0; i<v; i++){
193
        put_cabac(c, state, 1);
194
        if(i < max_index) state++;
195
    }
196
    if(truncated==0 || v<max)
197
        put_cabac(c, state, 0);
198
#else
199
    if(v <= max_index){
200
        for(i=0; i<v; i++){
201
            put_cabac(c, state+i, 1);
202
        }
203
        if(truncated==0 || v<max)
204
            put_cabac(c, state+i, 0);
205
    }else{
206
        for(i=0; i<=max_index; i++){
207
            put_cabac(c, state+i, 1);
208
        }
209
        for(; i<v; i++){
210
            put_cabac(c, state+max_index, 1);
211
        }
212
        if(truncated==0 || v<max)
213
            put_cabac(c, state+max_index, 0);
214
    }
215
#endif
216
}
217

    
218
/**
219
 * put unary exp golomb k-th order binarization.
220
 */
221
static void put_cabac_ueg(CABACContext *c, uint8_t * state, int v, int max, int is_signed, int k, int max_index){
222
    int i;
223

    
224
    if(v==0)
225
        put_cabac(c, state, 0);
226
    else{
227
        const int sign= v < 0;
228

    
229
        if(is_signed) v= FFABS(v);
230

    
231
        if(v<max){
232
            for(i=0; i<v; i++){
233
                put_cabac(c, state, 1);
234
                if(i < max_index) state++;
235
            }
236

    
237
            put_cabac(c, state, 0);
238
        }else{
239
            int m= 1<<k;
240

    
241
            for(i=0; i<max; i++){
242
                put_cabac(c, state, 1);
243
                if(i < max_index) state++;
244
            }
245

    
246
            v -= max;
247
            while(v >= m){ //FIXME optimize
248
                put_cabac_bypass(c, 1);
249
                v-= m;
250
                m+= m;
251
            }
252
            put_cabac_bypass(c, 0);
253
            while(m>>=1){
254
                put_cabac_bypass(c, v&m);
255
            }
256
        }
257

    
258
        if(is_signed)
259
            put_cabac_bypass(c, sign);
260
    }
261
}
262

    
263
static void refill(CABACContext *c){
264
#if CABAC_BITS == 16
265
        c->low+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
266
#else
267
        c->low+= c->bytestream[0]<<1;
268
#endif
269
    c->low -= CABAC_MASK;
270
    c->bytestream+= CABAC_BITS/8;
271
}
272

    
273
static void refill2(CABACContext *c){
274
    int i, x;
275

    
276
    x= c->low ^ (c->low-1);
277
    i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
278

    
279
    x= -CABAC_MASK;
280

    
281
#if CABAC_BITS == 16
282
        x+= (c->bytestream[0]<<9) + (c->bytestream[1]<<1);
283
#else
284
        x+= c->bytestream[0]<<1;
285
#endif
286

    
287
    c->low += x<<i;
288
    c->bytestream+= CABAC_BITS/8;
289
}
290

    
291
static inline void renorm_cabac_decoder(CABACContext *c){
292
    while(c->range < 0x100){
293
        c->range+= c->range;
294
        c->low+= c->low;
295
        if(!(c->low & CABAC_MASK))
296
            refill(c);
297
    }
298
}
299

    
300
static inline void renorm_cabac_decoder_once(CABACContext *c){
301
#ifdef ARCH_X86_DISABLED
302
    int temp;
303
#if 0
304
    //P3:683    athlon:475
305
    asm(
306
        "lea -0x100(%0), %2         \n\t"
307
        "shr $31, %2                \n\t"  //FIXME 31->63 for x86-64
308
        "shl %%cl, %0               \n\t"
309
        "shl %%cl, %1               \n\t"
310
        : "+r"(c->range), "+r"(c->low), "+c"(temp)
311
    );
312
#elif 0
313
    //P3:680    athlon:474
314
    asm(
315
        "cmp $0x100, %0             \n\t"
316
        "setb %%cl                  \n\t"  //FIXME 31->63 for x86-64
317
        "shl %%cl, %0               \n\t"
318
        "shl %%cl, %1               \n\t"
319
        : "+r"(c->range), "+r"(c->low), "+c"(temp)
320
    );
321
#elif 1
322
    int temp2;
323
    //P3:665    athlon:517
324
    asm(
325
        "lea -0x100(%0), %%eax      \n\t"
326
        "cdq                        \n\t"
327
        "mov %0, %%eax              \n\t"
328
        "and %%edx, %0              \n\t"
329
        "and %1, %%edx              \n\t"
330
        "add %%eax, %0              \n\t"
331
        "add %%edx, %1              \n\t"
332
        : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
333
    );
334
#elif 0
335
    int temp2;
336
    //P3:673    athlon:509
337
    asm(
338
        "cmp $0x100, %0             \n\t"
339
        "sbb %%edx, %%edx           \n\t"
340
        "mov %0, %%eax              \n\t"
341
        "and %%edx, %0              \n\t"
342
        "and %1, %%edx              \n\t"
343
        "add %%eax, %0              \n\t"
344
        "add %%edx, %1              \n\t"
345
        : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
346
    );
347
#else
348
    int temp2;
349
    //P3:677    athlon:511
350
    asm(
351
        "cmp $0x100, %0             \n\t"
352
        "lea (%0, %0), %%eax        \n\t"
353
        "lea (%1, %1), %%edx        \n\t"
354
        "cmovb %%eax, %0            \n\t"
355
        "cmovb %%edx, %1            \n\t"
356
        : "+r"(c->range), "+r"(c->low), "+a"(temp), "+d"(temp2)
357
    );
358
#endif
359
#else
360
    //P3:675    athlon:476
361
    int shift= (uint32_t)(c->range - 0x100)>>31;
362
    c->range<<= shift;
363
    c->low  <<= shift;
364
#endif
365
    if(!(c->low & CABAC_MASK))
366
        refill(c);
367
}
368

    
369
static av_always_inline int get_cabac_inline(CABACContext *c, uint8_t * const state){
370
    //FIXME gcc generates duplicate load/stores for c->low and c->range
371
#define LOW          "0"
372
#define RANGE        "4"
373
#ifdef ARCH_X86_64
374
#define BYTESTART   "16"
375
#define BYTE        "24"
376
#define BYTEEND     "32"
377
#else
378
#define BYTESTART   "12"
379
#define BYTE        "16"
380
#define BYTEEND     "20"
381
#endif
382
#if defined(ARCH_X86) && defined(CONFIG_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS)
383
    int bit;
384

    
385
#ifndef BRANCHLESS_CABAC_DECODER
386
    asm volatile(
387
        "movzbl (%1), %0                        \n\t"
388
        "movl "RANGE    "(%2), %%ebx            \n\t"
389
        "movl "RANGE    "(%2), %%edx            \n\t"
390
        "andl $0xC0, %%ebx                      \n\t"
391
        "movzbl "MANGLE(ff_h264_lps_range)"(%0, %%ebx, 2), %%esi\n\t"
392
        "movl "LOW      "(%2), %%ebx            \n\t"
393
//eax:state ebx:low, edx:range, esi:RangeLPS
394
        "subl %%esi, %%edx                      \n\t"
395
        "movl %%edx, %%ecx                      \n\t"
396
        "shll $17, %%ecx                        \n\t"
397
        "cmpl %%ecx, %%ebx                      \n\t"
398
        " ja 1f                                 \n\t"
399

    
400
#if 1
401
        //athlon:4067 P3:4110
402
        "lea -0x100(%%edx), %%ecx               \n\t"
403
        "shr $31, %%ecx                         \n\t"
404
        "shl %%cl, %%edx                        \n\t"
405
        "shl %%cl, %%ebx                        \n\t"
406
#else
407
        //athlon:4057 P3:4130
408
        "cmp $0x100, %%edx                      \n\t" //FIXME avoidable
409
        "setb %%cl                              \n\t"
410
        "shl %%cl, %%edx                        \n\t"
411
        "shl %%cl, %%ebx                        \n\t"
412
#endif
413
        "movzbl "MANGLE(ff_h264_mps_state)"(%0), %%ecx   \n\t"
414
        "movb %%cl, (%1)                        \n\t"
415
//eax:state ebx:low, edx:range, esi:RangeLPS
416
        "test %%bx, %%bx                        \n\t"
417
        " jnz 2f                                \n\t"
418
        "mov  "BYTE     "(%2), %%"REG_S"        \n\t"
419
        "subl $0xFFFF, %%ebx                    \n\t"
420
        "movzwl (%%"REG_S"), %%ecx              \n\t"
421
        "bswap %%ecx                            \n\t"
422
        "shrl $15, %%ecx                        \n\t"
423
        "add  $2, %%"REG_S"                     \n\t"
424
        "addl %%ecx, %%ebx                      \n\t"
425
        "mov  %%"REG_S", "BYTE    "(%2)         \n\t"
426
        "jmp 2f                                 \n\t"
427
        "1:                                     \n\t"
428
//eax:state ebx:low, edx:range, esi:RangeLPS
429
        "subl %%ecx, %%ebx                      \n\t"
430
        "movl %%esi, %%edx                      \n\t"
431
        "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx   \n\t"
432
        "shll %%cl, %%ebx                       \n\t"
433
        "shll %%cl, %%edx                       \n\t"
434
        "movzbl "MANGLE(ff_h264_lps_state)"(%0), %%ecx   \n\t"
435
        "movb %%cl, (%1)                        \n\t"
436
        "add  $1, %0                            \n\t"
437
        "test %%bx, %%bx                        \n\t"
438
        " jnz 2f                                \n\t"
439

    
440
        "mov  "BYTE     "(%2), %%"REG_c"        \n\t"
441
        "movzwl (%%"REG_c"), %%esi              \n\t"
442
        "bswap %%esi                            \n\t"
443
        "shrl $15, %%esi                        \n\t"
444
        "subl $0xFFFF, %%esi                    \n\t"
445
        "add  $2, %%"REG_c"                     \n\t"
446
        "mov  %%"REG_c", "BYTE    "(%2)         \n\t"
447

    
448
        "leal -1(%%ebx), %%ecx                  \n\t"
449
        "xorl %%ebx, %%ecx                      \n\t"
450
        "shrl $15, %%ecx                        \n\t"
451
        "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx   \n\t"
452
        "neg %%ecx                              \n\t"
453
        "add $7, %%ecx                          \n\t"
454

    
455
        "shll %%cl , %%esi                      \n\t"
456
        "addl %%esi, %%ebx                      \n\t"
457
        "2:                                     \n\t"
458
        "movl %%edx, "RANGE    "(%2)            \n\t"
459
        "movl %%ebx, "LOW      "(%2)            \n\t"
460
        :"=&a"(bit) //FIXME this is fragile gcc either runs out of registers or miscompiles it (for example if "+a"(bit) or "+m"(*state) is used
461
        :"r"(state), "r"(c)
462
        : "%"REG_c, "%ebx", "%edx", "%"REG_S, "memory"
463
    );
464
    bit&=1;
465
#else /* BRANCHLESS_CABAC_DECODER */
466

    
467

    
468
#if defined HAVE_FAST_CMOV
469
#define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
470
        "mov    "tmp"       , %%ecx                                     \n\t"\
471
        "shl    $17         , "tmp"                                     \n\t"\
472
        "cmp    "low"       , "tmp"                                     \n\t"\
473
        "cmova  %%ecx       , "range"                                   \n\t"\
474
        "sbb    %%ecx       , %%ecx                                     \n\t"\
475
        "and    %%ecx       , "tmp"                                     \n\t"\
476
        "sub    "tmp"       , "low"                                     \n\t"\
477
        "xor    %%ecx       , "ret"                                     \n\t"
478
#else /* HAVE_FAST_CMOV */
479
#define BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
480
        "mov    "tmp"       , %%ecx                                     \n\t"\
481
        "shl    $17         , "tmp"                                     \n\t"\
482
        "sub    "low"       , "tmp"                                     \n\t"\
483
        "sar    $31         , "tmp"                                     \n\t" /*lps_mask*/\
484
        "sub    %%ecx       , "range"                                   \n\t" /*RangeLPS - range*/\
485
        "and    "tmp"       , "range"                                   \n\t" /*(RangeLPS - range)&lps_mask*/\
486
        "add    %%ecx       , "range"                                   \n\t" /*new range*/\
487
        "shl    $17         , %%ecx                                     \n\t"\
488
        "and    "tmp"       , %%ecx                                     \n\t"\
489
        "sub    %%ecx       , "low"                                     \n\t"\
490
        "xor    "tmp"       , "ret"                                     \n\t"
491
#endif /* HAVE_FAST_CMOV */
492

    
493

    
494
#define BRANCHLESS_GET_CABAC(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
495
        "movzbl "statep"    , "ret"                                     \n\t"\
496
        "mov    "range"     , "tmp"                                     \n\t"\
497
        "and    $0xC0       , "range"                                   \n\t"\
498
        "movzbl "MANGLE(ff_h264_lps_range)"("ret", "range", 2), "range" \n\t"\
499
        "sub    "range"     , "tmp"                                     \n\t"\
500
        BRANCHLESS_GET_CABAC_UPDATE(ret, cabac, statep, low, lowword, range, tmp, tmpbyte)\
501
        "movzbl " MANGLE(ff_h264_norm_shift) "("range"), %%ecx          \n\t"\
502
        "shl    %%cl        , "range"                                   \n\t"\
503
        "movzbl "MANGLE(ff_h264_mlps_state)"+128("ret"), "tmp"          \n\t"\
504
        "mov    "tmpbyte"   , "statep"                                  \n\t"\
505
        "shl    %%cl        , "low"                                     \n\t"\
506
        "test   "lowword"   , "lowword"                                 \n\t"\
507
        " jnz   1f                                                      \n\t"\
508
        "mov "BYTE"("cabac"), %%"REG_c"                                 \n\t"\
509
        "movzwl (%%"REG_c")     , "tmp"                                 \n\t"\
510
        "bswap  "tmp"                                                   \n\t"\
511
        "shr    $15         , "tmp"                                     \n\t"\
512
        "sub    $0xFFFF     , "tmp"                                     \n\t"\
513
        "add    $2          , %%"REG_c"                                 \n\t"\
514
        "mov    %%"REG_c"   , "BYTE    "("cabac")                       \n\t"\
515
        "lea    -1("low")   , %%ecx                                     \n\t"\
516
        "xor    "low"       , %%ecx                                     \n\t"\
517
        "shr    $15         , %%ecx                                     \n\t"\
518
        "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx            \n\t"\
519
        "neg    %%ecx                                                   \n\t"\
520
        "add    $7          , %%ecx                                     \n\t"\
521
        "shl    %%cl        , "tmp"                                     \n\t"\
522
        "add    "tmp"       , "low"                                     \n\t"\
523
        "1:                                                             \n\t"
524

    
525
    asm volatile(
526
        "movl "RANGE    "(%2), %%esi            \n\t"
527
        "movl "LOW      "(%2), %%ebx            \n\t"
528
        BRANCHLESS_GET_CABAC("%0", "%2", "(%1)", "%%ebx", "%%bx", "%%esi", "%%edx", "%%dl")
529
        "movl %%esi, "RANGE    "(%2)            \n\t"
530
        "movl %%ebx, "LOW      "(%2)            \n\t"
531

    
532
        :"=&a"(bit)
533
        :"r"(state), "r"(c)
534
        : "%"REG_c, "%ebx", "%edx", "%esi", "memory"
535
    );
536
    bit&=1;
537
#endif /* BRANCHLESS_CABAC_DECODER */
538
#else /* defined(ARCH_X86) && defined(CONFIG_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS) */
539
    int s = *state;
540
    int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + s];
541
    int bit, lps_mask av_unused;
542

    
543
    c->range -= RangeLPS;
544
#ifndef BRANCHLESS_CABAC_DECODER
545
    if(c->low < (c->range<<(CABAC_BITS+1))){
546
        bit= s&1;
547
        *state= ff_h264_mps_state[s];
548
        renorm_cabac_decoder_once(c);
549
    }else{
550
        bit= ff_h264_norm_shift[RangeLPS];
551
        c->low -= (c->range<<(CABAC_BITS+1));
552
        *state= ff_h264_lps_state[s];
553
        c->range = RangeLPS<<bit;
554
        c->low <<= bit;
555
        bit= (s&1)^1;
556

    
557
        if(!(c->low & CABAC_MASK)){
558
            refill2(c);
559
        }
560
    }
561
#else /* BRANCHLESS_CABAC_DECODER */
562
    lps_mask= ((c->range<<(CABAC_BITS+1)) - c->low)>>31;
563

    
564
    c->low -= (c->range<<(CABAC_BITS+1)) & lps_mask;
565
    c->range += (RangeLPS - c->range) & lps_mask;
566

    
567
    s^=lps_mask;
568
    *state= (ff_h264_mlps_state+128)[s];
569
    bit= s&1;
570

    
571
    lps_mask= ff_h264_norm_shift[c->range];
572
    c->range<<= lps_mask;
573
    c->low  <<= lps_mask;
574
    if(!(c->low & CABAC_MASK))
575
        refill2(c);
576
#endif /* BRANCHLESS_CABAC_DECODER */
577
#endif /* defined(ARCH_X86) && defined(CONFIG_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS) */
578
    return bit;
579
}
580

    
581
static int av_noinline get_cabac_noinline(CABACContext *c, uint8_t * const state){
582
    return get_cabac_inline(c,state);
583
}
584

    
585
static int get_cabac(CABACContext *c, uint8_t * const state){
586
    return get_cabac_inline(c,state);
587
}
588

    
589
static int get_cabac_bypass(CABACContext *c){
590
#if 0 //not faster
591
    int bit;
592
    asm volatile(
593
        "movl "RANGE    "(%1), %%ebx            \n\t"
594
        "movl "LOW      "(%1), %%eax            \n\t"
595
        "shl $17, %%ebx                         \n\t"
596
        "add %%eax, %%eax                       \n\t"
597
        "sub %%ebx, %%eax                       \n\t"
598
        "cdq                                    \n\t"
599
        "and %%edx, %%ebx                       \n\t"
600
        "add %%ebx, %%eax                       \n\t"
601
        "test %%ax, %%ax                        \n\t"
602
        " jnz 1f                                \n\t"
603
        "movl "BYTE     "(%1), %%"REG_b"        \n\t"
604
        "subl $0xFFFF, %%eax                    \n\t"
605
        "movzwl (%%"REG_b"), %%ecx              \n\t"
606
        "bswap %%ecx                            \n\t"
607
        "shrl $15, %%ecx                        \n\t"
608
        "addl $2, %%"REG_b"                     \n\t"
609
        "addl %%ecx, %%eax                      \n\t"
610
        "movl %%"REG_b", "BYTE     "(%1)        \n\t"
611
        "1:                                     \n\t"
612
        "movl %%eax, "LOW      "(%1)            \n\t"
613

614
        :"=&d"(bit)
615
        :"r"(c)
616
        : "%eax", "%"REG_b, "%ecx", "memory"
617
    );
618
    return bit+1;
619
#else
620
    int range;
621
    c->low += c->low;
622

    
623
    if(!(c->low & CABAC_MASK))
624
        refill(c);
625

    
626
    range= c->range<<(CABAC_BITS+1);
627
    if(c->low < range){
628
        return 0;
629
    }else{
630
        c->low -= range;
631
        return 1;
632
    }
633
#endif
634
}
635

    
636

    
637
static av_always_inline int get_cabac_bypass_sign(CABACContext *c, int val){
638
#if defined(ARCH_X86) && !(defined(PIC) && defined(__GNUC__))
639
    asm volatile(
640
        "movl "RANGE    "(%1), %%ebx            \n\t"
641
        "movl "LOW      "(%1), %%eax            \n\t"
642
        "shl $17, %%ebx                         \n\t"
643
        "add %%eax, %%eax                       \n\t"
644
        "sub %%ebx, %%eax                       \n\t"
645
        "cdq                                    \n\t"
646
        "and %%edx, %%ebx                       \n\t"
647
        "add %%ebx, %%eax                       \n\t"
648
        "xor %%edx, %%ecx                       \n\t"
649
        "sub %%edx, %%ecx                       \n\t"
650
        "test %%ax, %%ax                        \n\t"
651
        " jnz 1f                                \n\t"
652
        "mov  "BYTE     "(%1), %%"REG_b"        \n\t"
653
        "subl $0xFFFF, %%eax                    \n\t"
654
        "movzwl (%%"REG_b"), %%edx              \n\t"
655
        "bswap %%edx                            \n\t"
656
        "shrl $15, %%edx                        \n\t"
657
        "add  $2, %%"REG_b"                     \n\t"
658
        "addl %%edx, %%eax                      \n\t"
659
        "mov  %%"REG_b", "BYTE     "(%1)        \n\t"
660
        "1:                                     \n\t"
661
        "movl %%eax, "LOW      "(%1)            \n\t"
662

    
663
        :"+c"(val)
664
        :"r"(c)
665
        : "%eax", "%"REG_b, "%edx", "memory"
666
    );
667
    return val;
668
#else
669
    int range, mask;
670
    c->low += c->low;
671

    
672
    if(!(c->low & CABAC_MASK))
673
        refill(c);
674

    
675
    range= c->range<<(CABAC_BITS+1);
676
    c->low -= range;
677
    mask= c->low >> 31;
678
    range &= mask;
679
    c->low += range;
680
    return (val^mask)-mask;
681
#endif
682
}
683

    
684
//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!)
685
//FIXME use some macros to avoid duplicatin get_cabac (cannot be done yet as that would make optimization work hard)
686
#if defined(ARCH_X86) && defined(CONFIG_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS)
687
static int decode_significance_x86(CABACContext *c, int max_coeff, uint8_t *significant_coeff_ctx_base, int *index){
688
    void *end= significant_coeff_ctx_base + max_coeff - 1;
689
    int minusstart= -(int)significant_coeff_ctx_base;
690
    int minusindex= 4-(int)index;
691
    int coeff_count;
692
    asm volatile(
693
        "movl "RANGE    "(%3), %%esi            \n\t"
694
        "movl "LOW      "(%3), %%ebx            \n\t"
695

    
696
        "2:                                     \n\t"
697

    
698
        BRANCHLESS_GET_CABAC("%%edx", "%3", "(%1)", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
699

    
700
        "test $1, %%edx                         \n\t"
701
        " jz 3f                                 \n\t"
702

    
703
        BRANCHLESS_GET_CABAC("%%edx", "%3", "61(%1)", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
704

    
705
        "mov  %2, %%"REG_a"                     \n\t"
706
        "movl %4, %%ecx                         \n\t"
707
        "add  %1, %%"REG_c"                     \n\t"
708
        "movl %%ecx, (%%"REG_a")                \n\t"
709

    
710
        "test $1, %%edx                         \n\t"
711
        " jnz 4f                                \n\t"
712

    
713
        "add  $4, %%"REG_a"                     \n\t"
714
        "mov  %%"REG_a", %2                     \n\t"
715

    
716
        "3:                                     \n\t"
717
        "add  $1, %1                            \n\t"
718
        "cmp  %5, %1                            \n\t"
719
        " jb 2b                                 \n\t"
720
        "mov  %2, %%"REG_a"                     \n\t"
721
        "movl %4, %%ecx                         \n\t"
722
        "add  %1, %%"REG_c"                     \n\t"
723
        "movl %%ecx, (%%"REG_a")                \n\t"
724
        "4:                                     \n\t"
725
        "add  %6, %%eax                         \n\t"
726
        "shr $2, %%eax                          \n\t"
727

    
728
        "movl %%esi, "RANGE    "(%3)            \n\t"
729
        "movl %%ebx, "LOW      "(%3)            \n\t"
730
        :"=&a"(coeff_count), "+r"(significant_coeff_ctx_base), "+m"(index)\
731
        :"r"(c), "m"(minusstart), "m"(end), "m"(minusindex)\
732
        : "%"REG_c, "%ebx", "%edx", "%esi", "memory"\
733
    );
734
    return coeff_count;
735
}
736

    
737
static int decode_significance_8x8_x86(CABACContext *c, uint8_t *significant_coeff_ctx_base, int *index, uint8_t *sig_off){
738
    int minusindex= 4-(int)index;
739
    int coeff_count;
740
    long last=0;
741
    asm volatile(
742
        "movl "RANGE    "(%3), %%esi            \n\t"
743
        "movl "LOW      "(%3), %%ebx            \n\t"
744

    
745
        "mov %1, %%"REG_D"                      \n\t"
746
        "2:                                     \n\t"
747

    
748
        "mov %6, %%"REG_a"                      \n\t"
749
        "movzbl (%%"REG_a", %%"REG_D"), %%edi   \n\t"
750
        "add %5, %%"REG_D"                      \n\t"
751

    
752
        BRANCHLESS_GET_CABAC("%%edx", "%3", "(%%"REG_D")", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
753

    
754
        "mov %1, %%edi                          \n\t"
755
        "test $1, %%edx                         \n\t"
756
        " jz 3f                                 \n\t"
757

    
758
        "movzbl "MANGLE(last_coeff_flag_offset_8x8)"(%%edi), %%edi\n\t"
759
        "add %5, %%"REG_D"                      \n\t"
760

    
761
        BRANCHLESS_GET_CABAC("%%edx", "%3", "15(%%"REG_D")", "%%ebx", "%%bx", "%%esi", "%%eax", "%%al")
762

    
763
        "mov %2, %%"REG_a"                      \n\t"
764
        "mov %1, %%edi                          \n\t"
765
        "movl %%edi, (%%"REG_a")                \n\t"
766

    
767
        "test $1, %%edx                         \n\t"
768
        " jnz 4f                                \n\t"
769

    
770
        "add $4, %%"REG_a"                      \n\t"
771
        "mov %%"REG_a", %2                      \n\t"
772

    
773
        "3:                                     \n\t"
774
        "addl $1, %%edi                         \n\t"
775
        "mov %%edi, %1                          \n\t"
776
        "cmpl $63, %%edi                        \n\t"
777
        " jb 2b                                 \n\t"
778
        "mov %2, %%"REG_a"                      \n\t"
779
        "movl %%edi, (%%"REG_a")                \n\t"
780
        "4:                                     \n\t"
781
        "addl %4, %%eax                         \n\t"
782
        "shr $2, %%eax                          \n\t"
783

    
784
        "movl %%esi, "RANGE    "(%3)            \n\t"
785
        "movl %%ebx, "LOW      "(%3)            \n\t"
786
        :"=&a"(coeff_count),"+m"(last), "+m"(index)\
787
        :"r"(c), "m"(minusindex), "m"(significant_coeff_ctx_base), "m"(sig_off)\
788
        : "%"REG_c, "%ebx", "%edx", "%esi", "%"REG_D, "memory"\
789
    );
790
    return coeff_count;
791
}
792
#endif /* defined(ARCH_X86) && && defined(CONFIG_7REGS) && defined(HAVE_EBX_AVAILABLE) && !defined(BROKEN_RELOCATIONS) */
793

    
794
/**
795
 *
796
 * @return the number of bytes read or 0 if no end
797
 */
798
static int get_cabac_terminate(CABACContext *c){
799
    c->range -= 2;
800
    if(c->low < c->range<<(CABAC_BITS+1)){
801
        renorm_cabac_decoder_once(c);
802
        return 0;
803
    }else{
804
        return c->bytestream - c->bytestream_start;
805
    }
806
}
807

    
808
/**
809
 * get (truncated) unnary binarization.
810
 */
811
static int get_cabac_u(CABACContext *c, uint8_t * state, int max, int max_index, int truncated){
812
    int i;
813

    
814
    for(i=0; i<max; i++){
815
        if(get_cabac(c, state)==0)
816
            return i;
817

    
818
        if(i< max_index) state++;
819
    }
820

    
821
    return truncated ? max : -1;
822
}
823

    
824
/**
825
 * get unary exp golomb k-th order binarization.
826
 */
827
static int get_cabac_ueg(CABACContext *c, uint8_t * state, int max, int is_signed, int k, int max_index){
828
    int i, v;
829
    int m= 1<<k;
830

    
831
    if(get_cabac(c, state)==0)
832
        return 0;
833

    
834
    if(0 < max_index) state++;
835

    
836
    for(i=1; i<max; i++){
837
        if(get_cabac(c, state)==0){
838
            if(is_signed && get_cabac_bypass(c)){
839
                return -i;
840
            }else
841
                return i;
842
        }
843

    
844
        if(i < max_index) state++;
845
    }
846

    
847
    while(get_cabac_bypass(c)){
848
        i+= m;
849
        m+= m;
850
    }
851

    
852
    v=0;
853
    while(m>>=1){
854
        v+= v + get_cabac_bypass(c);
855
    }
856
    i += v;
857

    
858
    if(is_signed && get_cabac_bypass(c)){
859
        return -i;
860
    }else
861
        return i;
862
}
863

    
864
#endif /* CABAC_H */