Browse code

shift CABACContext.range right, this reduces the number of shifts needed in get_cabac() and is slightly faster on P3 (and should be much faster on P4 as the P4 except the more recent variants lacks an integer shifter and so shifts have ~10 times longer latency then simple operations like adds)

Originally committed as revision 6702 to svn://svn.ffmpeg.org/ffmpeg/trunk

Michael Niedermayer authored on 2006/10/16 05:40:50
Showing 2 changed files
... ...
@@ -51,7 +51,7 @@ static const uint8_t lps_range[64][4]= {
51 51
 };
52 52
 
53 53
 uint8_t ff_h264_mlps_state[4*64];
54
-uint8_t ff_h264_lps_range[2*65][4];
54
+uint8_t ff_h264_lps_range[4][2*64];
55 55
 uint8_t ff_h264_lps_state[2*64];
56 56
 uint8_t ff_h264_mps_state[2*64];
57 57
 
... ...
@@ -76,8 +76,8 @@ static const uint8_t lps_state[64]= {
76 76
  33,33,34,34,35,35,35,36,
77 77
  36,36,37,37,37,38,38,63,
78 78
 };
79
-
80
-const uint8_t ff_h264_norm_shift[128]= {
79
+#if 0
80
+const uint8_t ff_h264_norm_shift_old[128]= {
81 81
  7,6,5,5,4,4,4,4,3,3,3,3,3,3,3,3,
82 82
  2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
83 83
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
... ...
@@ -87,6 +87,29 @@ const uint8_t ff_h264_norm_shift[128]= {
87 87
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
88 88
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
89 89
 };
90
+#endif
91
+const uint8_t ff_h264_norm_shift[512]= {
92
+ 9,8,7,7,6,6,6,6,5,5,5,5,5,5,5,5,
93
+ 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
94
+ 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
95
+ 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
96
+ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
97
+ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
98
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122 122
 #endif
123 123
     c->low+= ((*c->bytestream++)<<2) + 2;
124
-    c->range= 0x1FE<<(CABAC_BITS + 1);
124
+    c->range= 0x1FE;
125 125
 }
126 126
 
127 127
 void ff_init_cabac_states(CABACContext *c){
... ...
@@ -129,8 +152,8 @@ void ff_init_cabac_states(CABACContext *c){
129 129
 
130 130
     for(i=0; i<64; i++){
131 131
         for(j=0; j<4; j++){ //FIXME check if this is worth the 1 shift we save
132
-            ff_h264_lps_range[2*i+0][j+4]=
133
-            ff_h264_lps_range[2*i+1][j+4]= lps_range[i][j];
132
+            ff_h264_lps_range[j][2*i+0]=
133
+            ff_h264_lps_range[j][2*i+1]= lps_range[i][j];
134 134
         }
135 135
 
136 136
         ff_h264_mlps_state[128+2*i+0]=
... ...
@@ -48,10 +48,10 @@ typedef struct CABACContext{
48 48
 }CABACContext;
49 49
 
50 50
 extern uint8_t ff_h264_mlps_state[4*64];
51
-extern uint8_t ff_h264_lps_range[2*65][4];  ///< rangeTabLPS
51
+extern uint8_t ff_h264_lps_range[4][2*64];  ///< rangeTabLPS
52 52
 extern uint8_t ff_h264_mps_state[2*64];     ///< transIdxMPS
53 53
 extern uint8_t ff_h264_lps_state[2*64];     ///< transIdxLPS
54
-extern const uint8_t ff_h264_norm_shift[128];
54
+extern const uint8_t ff_h264_norm_shift[512];
55 55
 
56 56
 
57 57
 void ff_init_cabac_encoder(CABACContext *c, uint8_t *buf, int buf_size);
... ...
@@ -85,7 +85,7 @@ static inline void renorm_cabac_encoder(CABACContext *c){
85 85
 }
86 86
 
87 87
 static void put_cabac(CABACContext *c, uint8_t * const state, int bit){
88
-    int RangeLPS= ff_h264_lps_range[*state][c->range>>6];
88
+    int RangeLPS= ff_h264_lps_range[2*(c->range&0xC0) + *state];
89 89
 
90 90
     if(bit == ((*state)&1)){
91 91
         c->range -= RangeLPS;
... ...
@@ -268,7 +268,7 @@ static void refill2(CABACContext *c){
268 268
     int i, x;
269 269
 
270 270
     x= c->low ^ (c->low-1);
271
-    i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS+1)];
271
+    i= 7 - ff_h264_norm_shift[x>>(CABAC_BITS-1)];
272 272
 
273 273
     x= -CABAC_MASK;
274 274
 
... ...
@@ -283,7 +283,7 @@ static void refill2(CABACContext *c){
283 283
 }
284 284
 
285 285
 static inline void renorm_cabac_decoder(CABACContext *c){
286
-    while(c->range < (0x200 << CABAC_BITS)){
286
+    while(c->range < 0x100){
287 287
         c->range+= c->range;
288 288
         c->low+= c->low;
289 289
         if(!(c->low & CABAC_MASK))
... ...
@@ -297,7 +297,7 @@ static inline void renorm_cabac_decoder_once(CABACContext *c){
297 297
 #if 0
298 298
     //P3:683    athlon:475
299 299
     asm(
300
-        "lea -0x2000000(%0), %2     \n\t"
300
+        "lea -0x100(%0), %2         \n\t"
301 301
         "shr $31, %2                \n\t"  //FIXME 31->63 for x86-64
302 302
         "shl %%cl, %0               \n\t"
303 303
         "shl %%cl, %1               \n\t"
... ...
@@ -306,7 +306,7 @@ static inline void renorm_cabac_decoder_once(CABACContext *c){
306 306
 #elif 0
307 307
     //P3:680    athlon:474
308 308
     asm(
309
-        "cmp $0x2000000, %0         \n\t"
309
+        "cmp $0x100, %0             \n\t"
310 310
         "setb %%cl                  \n\t"  //FIXME 31->63 for x86-64
311 311
         "shl %%cl, %0               \n\t"
312 312
         "shl %%cl, %1               \n\t"
... ...
@@ -316,7 +316,7 @@ static inline void renorm_cabac_decoder_once(CABACContext *c){
316 316
     int temp2;
317 317
     //P3:665    athlon:517
318 318
     asm(
319
-        "lea -0x2000000(%0), %%eax  \n\t"
319
+        "lea -0x100(%0), %%eax      \n\t"
320 320
         "cdq                        \n\t"
321 321
         "mov %0, %%eax              \n\t"
322 322
         "and %%edx, %0              \n\t"
... ...
@@ -329,7 +329,7 @@ static inline void renorm_cabac_decoder_once(CABACContext *c){
329 329
     int temp2;
330 330
     //P3:673    athlon:509
331 331
     asm(
332
-        "cmp $0x2000000, %0         \n\t"
332
+        "cmp $0x100, %0             \n\t"
333 333
         "sbb %%edx, %%edx           \n\t"
334 334
         "mov %0, %%eax              \n\t"
335 335
         "and %%edx, %0              \n\t"
... ...
@@ -342,7 +342,7 @@ static inline void renorm_cabac_decoder_once(CABACContext *c){
342 342
     int temp2;
343 343
     //P3:677    athlon:511
344 344
     asm(
345
-        "cmp $0x2000000, %0         \n\t"
345
+        "cmp $0x100, %0             \n\t"
346 346
         "lea (%0, %0), %%eax        \n\t"
347 347
         "lea (%1, %1), %%edx        \n\t"
348 348
         "cmovb %%eax, %0            \n\t"
... ...
@@ -352,7 +352,7 @@ static inline void renorm_cabac_decoder_once(CABACContext *c){
352 352
 #endif
353 353
 #else
354 354
     //P3:675    athlon:476
355
-    int shift= (uint32_t)(c->range - (0x200 << CABAC_BITS))>>31;
355
+    int shift= (uint32_t)(c->range - 0x100)>>31;
356 356
     c->range<<= shift;
357 357
     c->low  <<= shift;
358 358
 #endif
... ...
@@ -375,24 +375,25 @@ static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state
375 375
         "movzbl (%1), %%eax                     \n\t"
376 376
         "movl "RANGE    "(%2), %%ebx            \n\t"
377 377
         "movl "RANGE    "(%2), %%edx            \n\t"
378
-        "shrl $23, %%ebx                        \n\t"
379
-        "movzbl "MANGLE(ff_h264_lps_range)"(%%ebx, %%eax, 4), %%esi\n\t"
380
-        "shll $17, %%esi                        \n\t"
378
+        "andl $0xC0, %%ebx                      \n\t"
379
+        "movzbl "MANGLE(ff_h264_lps_range)"(%%eax, %%ebx, 2), %%esi\n\t"
381 380
         "movl "LOW      "(%2), %%ebx            \n\t"
382 381
 //eax:state ebx:low, edx:range, esi:RangeLPS
383 382
         "subl %%esi, %%edx                      \n\t"
384
-        "cmpl %%edx, %%ebx                      \n\t"
383
+        "movl %%edx, %%ecx                      \n\t"
384
+        "shll $17, %%ecx                        \n\t"
385
+        "cmpl %%ecx, %%ebx                      \n\t"
385 386
         " ja 1f                                 \n\t"
386 387
 
387 388
 #if 1
388 389
         //athlon:4067 P3:4110
389
-        "lea -0x2000000(%%edx), %%ecx           \n\t"
390
+        "lea -0x100(%%edx), %%ecx               \n\t"
390 391
         "shr $31, %%ecx                         \n\t"
391 392
         "shl %%cl, %%edx                        \n\t"
392 393
         "shl %%cl, %%ebx                        \n\t"
393 394
 #else
394 395
         //athlon:4057 P3:4130
395
-        "cmp $0x2000000, %%edx                  \n\t" //FIXME avoidable
396
+        "cmp $0x100, %%edx                      \n\t" //FIXME avoidable
396 397
         "setb %%cl                              \n\t"
397 398
         "shl %%cl, %%edx                        \n\t"
398 399
         "shl %%cl, %%ebx                        \n\t"
... ...
@@ -413,14 +414,13 @@ static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state
413 413
         "jmp 2f                                 \n\t"
414 414
         "1:                                     \n\t"
415 415
 //eax:state ebx:low, edx:range, esi:RangeLPS
416
-        "subl %%edx, %%ebx                      \n\t"
416
+        "subl %%ecx, %%ebx                      \n\t"
417 417
         "movl %%esi, %%edx                      \n\t"
418
-        "shr $19, %%esi                         \n\t"
419
-        "movzbl "MANGLE(ff_h264_lps_state)"(%%eax), %%ecx   \n\t"
420
-        "movb %%cl, (%1)                        \n\t"
421 418
         "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx   \n\t"
422 419
         "shll %%cl, %%ebx                       \n\t"
423 420
         "shll %%cl, %%edx                       \n\t"
421
+        "movzbl "MANGLE(ff_h264_lps_state)"(%%eax), %%ecx   \n\t"
422
+        "movb %%cl, (%1)                        \n\t"
424 423
         "addl $1, %%eax                         \n\t"
425 424
         "test %%bx, %%bx                        \n\t"
426 425
         " jnz 2f                                \n\t"
... ...
@@ -435,7 +435,7 @@ static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state
435 435
 
436 436
         "leal -1(%%ebx), %%ecx                  \n\t"
437 437
         "xorl %%ebx, %%ecx                      \n\t"
438
-        "shrl $17, %%ecx                        \n\t"
438
+        "shrl $15, %%ecx                        \n\t"
439 439
         "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx   \n\t"
440 440
         "neg %%ecx                              \n\t"
441 441
         "add $7, %%ecx                          \n\t"
... ...
@@ -455,20 +455,22 @@ static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state
455 455
         "movzbl (%1), %%eax                     \n\t"
456 456
         "movl "RANGE    "(%2), %%ebx            \n\t"
457 457
         "movl "RANGE    "(%2), %%edx            \n\t"
458
-        "shrl $23, %%ebx                        \n\t"
459
-        "movzbl "MANGLE(ff_h264_lps_range)"(%%ebx, %%eax, 4), %%esi\n\t"
460
-        "shll $17, %%esi                        \n\t"
458
+        "andl $0xC0, %%ebx                      \n\t"
459
+        "movzbl "MANGLE(ff_h264_lps_range)"(%%eax, %%ebx, 2), %%esi\n\t"
461 460
         "movl "LOW      "(%2), %%ebx            \n\t"
462 461
 //eax:state ebx:low, edx:range, esi:RangeLPS
463 462
         "subl %%esi, %%edx                      \n\t"
464
-#ifdef CMOV_IS_FAST //FIXME actually define this somewhere
463
+#ifdef CMOV_IS_FAST
464
+        "movl %%edx, %%ecx                      \n\t"
465
+        "shl $17, %%edx                         \n\t"
465 466
         "cmpl %%ebx, %%edx                      \n\t"
466
-        "cmova %%edx, %%esi                     \n\t"
467
+        "cmova %%ecx, %%esi                     \n\t"
467 468
         "sbbl %%ecx, %%ecx                      \n\t"
468 469
         "andl %%ecx, %%edx                      \n\t"
469 470
         "subl %%edx, %%ebx                      \n\t"
470 471
         "xorl %%ecx, %%eax                      \n\t"
471 472
 #else /* CMOV_IS_FAST */
473
+FIXTHIS
472 474
         "movl %%edx, %%ecx                      \n\t"
473 475
         "subl %%ebx, %%edx                      \n\t"
474 476
         "sarl $31, %%edx                        \n\t" //lps_mask
... ...
@@ -481,16 +483,14 @@ static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state
481 481
 #endif /* CMOV_IS_FAST */
482 482
 
483 483
 //eax:state ebx:low edx:mask esi:range
484
-        "movzbl "MANGLE(ff_h264_mlps_state)"+128(%%eax), %%ecx   \n\t"
485
-        "movb %%cl, (%1)                        \n\t"
486 484
 
487
-        "movl %%esi, %%edx                      \n\t"
488
-//eax:bit ebx:low edx:range esi:range
485
+//eax:bit ebx:low esi:range
489 486
 
490
-        "shr $19, %%esi                         \n\t"
491 487
         "movzbl " MANGLE(ff_h264_norm_shift) "(%%esi), %%ecx   \n\t"
492
-        "shll %%cl, %%edx                       \n\t"
493
-        "movl %%edx, "RANGE    "(%2)            \n\t"
488
+        "shll %%cl, %%esi                       \n\t"
489
+        "movzbl "MANGLE(ff_h264_mlps_state)"+128(%%eax), %%edx   \n\t"
490
+        "movb %%dl, (%1)                        \n\t"
491
+        "movl %%esi, "RANGE    "(%2)            \n\t"
494 492
         "shll %%cl, %%ebx                       \n\t"
495 493
         "movl %%ebx, "LOW      "(%2)            \n\t"
496 494
         "test %%bx, %%bx                        \n\t"
... ...
@@ -506,7 +506,7 @@ static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state
506 506
 
507 507
         "leal -1(%%ebx), %%ecx                  \n\t"
508 508
         "xorl %%ebx, %%ecx                      \n\t"
509
-        "shrl $17, %%ecx                        \n\t"
509
+        "shrl $15, %%ecx                        \n\t"
510 510
         "movzbl " MANGLE(ff_h264_norm_shift) "(%%ecx), %%ecx   \n\t"
511 511
         "neg %%ecx                              \n\t"
512 512
         "add $7, %%ecx                          \n\t"
... ...
@@ -523,18 +523,18 @@ static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state
523 523
 #endif /* BRANCHLESS_CABAC_DECODER */
524 524
 #else /* ARCH_X86 */
525 525
     int s = *state;
526
-    int RangeLPS= ff_h264_lps_range[s][c->range>>(CABAC_BITS+7)]<<(CABAC_BITS+1);
526
+    int RangeLPS= ff_h264_lps_range[0][2*(c->range&0xC0) + s];
527 527
     int bit, lps_mask attribute_unused;
528 528
 
529 529
     c->range -= RangeLPS;
530 530
 #ifndef BRANCHLESS_CABAC_DECODER
531
-    if(c->low < c->range){
531
+    if(c->low < (c->range<<17)){
532 532
         bit= s&1;
533 533
         *state= ff_h264_mps_state[s];
534 534
         renorm_cabac_decoder_once(c);
535 535
     }else{
536
-        bit= ff_h264_norm_shift[RangeLPS>>19];
537
-        c->low -= c->range;
536
+        bit= ff_h264_norm_shift[RangeLPS];
537
+        c->low -= (c->range<<17);
538 538
         *state= ff_h264_lps_state[s];
539 539
         c->range = RangeLPS<<bit;
540 540
         c->low <<= bit;
... ...
@@ -545,16 +545,16 @@ static int always_inline get_cabac_inline(CABACContext *c, uint8_t * const state
545 545
         }
546 546
     }
547 547
 #else /* BRANCHLESS_CABAC_DECODER */
548
-    lps_mask= (c->range - c->low)>>31;
548
+    lps_mask= ((c->range<<17) - c->low)>>31;
549 549
 
550
-    c->low -= c->range & lps_mask;
550
+    c->low -= (c->range<<17) & lps_mask;
551 551
     c->range += (RangeLPS - c->range) & lps_mask;
552 552
 
553 553
     s^=lps_mask;
554 554
     *state= (ff_h264_mlps_state+128)[s];
555 555
     bit= s&1;
556 556
 
557
-    lps_mask= ff_h264_norm_shift[c->range>>(CABAC_BITS+3)];
557
+    lps_mask= ff_h264_norm_shift[c->range];
558 558
     c->range<<= lps_mask;
559 559
     c->low  <<= lps_mask;
560 560
     if(!(c->low & CABAC_MASK))
... ...
@@ -573,15 +573,17 @@ static int get_cabac(CABACContext *c, uint8_t * const state){
573 573
 }
574 574
 
575 575
 static int get_cabac_bypass(CABACContext *c){
576
+    int range;
576 577
     c->low += c->low;
577 578
 
578 579
     if(!(c->low & CABAC_MASK))
579 580
         refill(c);
580 581
 
581
-    if(c->low < c->range){
582
+    range= c->range<<17;
583
+    if(c->low < range){
582 584
         return 0;
583 585
     }else{
584
-        c->low -= c->range;
586
+        c->low -= range;
585 587
         return 1;
586 588
     }
587 589
 }
... ...
@@ -591,8 +593,8 @@ static int get_cabac_bypass(CABACContext *c){
591 591
  * @return the number of bytes read or 0 if no end
592 592
  */
593 593
 static int get_cabac_terminate(CABACContext *c){
594
-    c->range -= 4<<CABAC_BITS;
595
-    if(c->low < c->range){
594
+    c->range -= 2;
595
+    if(c->low < c->range<<17){
596 596
         renorm_cabac_decoder_once(c);
597 597
         return 0;
598 598
     }else{