[x265] [PATCH] optimize: rewrite TEncBinCABAC::encodeBin

Min Chen chenm003 at 163.com
Tue Mar 18 02:12:50 CET 2014


# HG changeset patch
# User Min Chen <chenm003 at 163.com>
# Date 1395105163 25200
# Node ID a56d6e0b44894e9ab81bfa971ec070bd2569ed72
# Parent  8d5deb7cafd83ac554489deb8577f905a0cda6b3
optimize: rewrite TEncBinCABAC::encodeBin

diff -r 8d5deb7cafd8 -r a56d6e0b4489 source/Lib/TLibCommon/ContextTables.h
--- a/source/Lib/TLibCommon/ContextTables.h	Mon Mar 17 00:47:24 2014 -0500
+++ b/source/Lib/TLibCommon/ContextTables.h	Mon Mar 17 18:12:43 2014 -0700
@@ -136,7 +136,7 @@
     uint8_t bBinsCoded;
 } ContextModel;
 
-extern const int     g_entropyBits[128];
+extern const uint32_t g_entropyBits[128];
 extern const uint8_t g_nextState[128][2];
 uint8_t sbacInit(int qp, int initValue);   ///< initialize state with initial probability
 
diff -r 8d5deb7cafd8 -r a56d6e0b4489 source/Lib/TLibCommon/TComRom.cpp
--- a/source/Lib/TLibCommon/TComRom.cpp	Mon Mar 17 00:47:24 2014 -0500
+++ b/source/Lib/TLibCommon/TComRom.cpp	Mon Mar 17 18:12:43 2014 -0700
@@ -573,18 +573,6 @@
     {   2,   2,   2,   2 }
 };
 
-const uint8_t g_renormTable[32] =
-{
-    6,  5,  4,  4,
-    3,  3,  3,  3,
-    2,  2,  2,  2,
-    2,  2,  2,  2,
-    1,  1,  1,  1,
-    1,  1,  1,  1,
-    1,  1,  1,  1,
-    1,  1,  1,  1
-};
-
 const uint8_t x265_exp2_lut[64] =
 {
     0,  3,  6,  8,  11, 14,  17,  20,  23,  26,  29,  32,  36,  39,  42,  45,
diff -r 8d5deb7cafd8 -r a56d6e0b4489 source/Lib/TLibCommon/TComRom.h
--- a/source/Lib/TLibCommon/TComRom.h	Mon Mar 17 00:47:24 2014 -0500
+++ b/source/Lib/TLibCommon/TComRom.h	Mon Mar 17 18:12:43 2014 -0700
@@ -272,7 +272,6 @@
 extern const double x265_lambda2_non_I[MAX_MAX_QP + 1];
 // CABAC tables
 extern const uint8_t g_lpsTable[64][4];
-extern const uint8_t g_renormTable[32];
 extern const uint8_t x265_exp2_lut[64];
 }
 
diff -r 8d5deb7cafd8 -r a56d6e0b4489 source/Lib/TLibEncoder/TEncBinCoderCABAC.cpp
--- a/source/Lib/TLibEncoder/TEncBinCoderCABAC.cpp	Mon Mar 17 00:47:24 2014 -0500
+++ b/source/Lib/TLibEncoder/TEncBinCoderCABAC.cpp	Mon Mar 17 18:12:43 2014 -0700
@@ -37,6 +37,7 @@
 
 #include "TEncBinCoderCABAC.h"
 #include "TLibCommon/TComRom.h"
+#include "threading.h"  // CLZ32
 
 using namespace x265;
 
@@ -180,7 +181,7 @@
 
     uint32_t mstate = ctxModel.m_state;
 
-    ctxModel.m_state = sbacNext(ctxModel.m_state, binValue);
+    ctxModel.m_state = sbacNext(mstate, binValue);
 
     if (m_bIsCounter)
     {
@@ -194,25 +195,32 @@
     uint32_t lps = g_lpsTable[state][(m_range >> 6) & 3];
     m_range -= lps;
 
-    int numBits = g_renormTable[lps >> 3];
+    assert(lps != 0);
+
+    int numBits = (uint32_t)(m_range - 256) >> 31;
+    uint32_t low = m_low;
+    uint32_t range = m_range;
     if (binValue != mps)
     {
-        m_low     = (m_low + m_range) << numBits;
-        m_range   = lps << numBits;
+        // NOTE: lps is non-zero and the maximum of idx is 8 because lps less than 256
+        //numBits   = g_renormTable[lps >> 3];
+        unsigned long idx;
+        CLZ32(idx, lps);
+        numBits = 8 - idx;
+        if (numBits >= 6)
+            numBits = 6;
+
+        low    += range;
+        range   = lps;
     }
-    else
-    {
-        if (m_range >= 256)
-        {
-            return;
-        }
-        numBits = 1;
-        m_low <<= 1;
-        m_range <<= 1;
-    }
+    m_low = (low << numBits);
+    m_range = (range << numBits);
     m_bitsLeft += numBits;
 
-    testAndWriteOut();
+    if (m_bitsLeft >= 0)
+    {
+        writeOut();
+    }
 }
 
 /**
@@ -240,7 +248,10 @@
     }
     m_bitsLeft++;
 
-    testAndWriteOut();
+    if (m_bitsLeft >= 0)
+    {
+        writeOut();
+    }
 }
 
 /**
@@ -274,14 +285,20 @@
         binValues -= pattern << numBins;
         m_bitsLeft += 8;
 
-        testAndWriteOut();
+        if (m_bitsLeft >= 0)
+        {
+            writeOut();
+        }
     }
 
     m_low <<= numBins;
     m_low += m_range * binValues;
     m_bitsLeft += numBins;
 
-    testAndWriteOut();
+    if (m_bitsLeft >= 0)
+    {
+        writeOut();
+    }
 }
 
 /**
@@ -316,11 +333,6 @@
         m_bitsLeft++;
     }
 
-    testAndWriteOut();
-}
-
-void TEncBinCABAC::testAndWriteOut()
-{
     if (m_bitsLeft >= 0)
     {
         writeOut();
diff -r 8d5deb7cafd8 -r a56d6e0b4489 source/Lib/TLibEncoder/TEncBinCoderCABAC.h
--- a/source/Lib/TLibEncoder/TEncBinCoderCABAC.h	Mon Mar 17 00:47:24 2014 -0500
+++ b/source/Lib/TLibEncoder/TEncBinCoderCABAC.h	Mon Mar 17 18:12:43 2014 -0700
@@ -85,7 +85,6 @@
 
 protected:
 
-    void testAndWriteOut();
     void writeOut();
 
 public:
diff -r 8d5deb7cafd8 -r a56d6e0b4489 source/Lib/TLibEncoder/TEncSbac.cpp
--- a/source/Lib/TLibEncoder/TEncSbac.cpp	Mon Mar 17 00:47:24 2014 -0500
+++ b/source/Lib/TLibEncoder/TEncSbac.cpp	Mon Mar 17 18:12:43 2014 -0700
@@ -61,7 +61,7 @@
 
 #endif // if ENC_DEC_TRACE
 
-const int g_entropyBits[128] =
+const uint32_t g_entropyBits[128] =
 {
     // Corrected table, most notably for last state
     0x07b23, 0x085f9, 0x074a0, 0x08cbc, 0x06ee4, 0x09354, 0x067f4, 0x09c1b, 0x060b0, 0x0a62a, 0x05a9c, 0x0af5b, 0x0548d, 0x0b955, 0x04f56, 0x0c2a9,
diff -r 8d5deb7cafd8 -r a56d6e0b4489 source/Lib/TLibEncoder/TEncSbac.h
--- a/source/Lib/TLibEncoder/TEncSbac.h	Mon Mar 17 00:47:24 2014 -0500
+++ b/source/Lib/TLibEncoder/TEncSbac.h	Mon Mar 17 18:12:43 2014 -0700
@@ -58,6 +58,8 @@
 class TEncSbac : public SyntaxElementWriter, public TEncEntropyIf
 {
 public:
+    uint64_t pad;
+    ContextModel m_contextModels[MAX_OFF_CTX_MOD];
 
     TComSlice*    m_slice;
     TEncBinCABAC* m_binIf;
@@ -153,8 +155,6 @@
     void xCopyContextsFrom(TEncSbac* src);
     void xCodePredWeightTable(TComSlice* slice);
     void xCodeScalingList(TComScalingList* scalingList, uint32_t sizeId, uint32_t listId);
-
-    ContextModel         m_contextModels[MAX_OFF_CTX_MOD];
 };
 }
 //! \}



More information about the x265-devel mailing list