[x265] [PATCH] improve loop in the Entropy::estSignificantMapBit()

Min Chen chenm003 at 163.com
Wed Nov 4 02:57:31 CET 2015


# HG changeset patch
# User Min Chen <chenm003 at 163.com>
# Date 1446578858 21600
# Node ID 1e120ec968d0e8d5b9882df416fe42e0a7325a2f
# Parent  f755f2624101ac3efc28113884b61d64268bed44
improve loop in the Entropy::estSignificantMapBit()
---
 source/encoder/entropy.cpp |   89 ++++++++++++++++++++++++++++++-------------
 1 files changed, 62 insertions(+), 27 deletions(-)

diff -r f755f2624101 -r 1e120ec968d0 source/encoder/entropy.cpp
--- a/source/encoder/entropy.cpp	Tue Nov 03 13:27:36 2015 -0600
+++ b/source/encoder/entropy.cpp	Tue Nov 03 13:27:38 2015 -0600
@@ -1920,43 +1920,78 @@
         numCtx = bIsLuma ? 12 : 3;
     }
 
+    const int ctxSigOffset = OFF_SIG_FLAG_CTX + (bIsLuma ? 0 : NUM_SIG_FLAG_CTX_LUMA);
+
+    estBitsSbac.significantBits[0][0] = sbacGetEntropyBits(m_contextState[ctxSigOffset], 0);
+    estBitsSbac.significantBits[1][0] = sbacGetEntropyBits(m_contextState[ctxSigOffset], 1);
+
+    for (int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++)
+    {
+        estBitsSbac.significantBits[0][ctxIdx] = sbacGetEntropyBits(m_contextState[ctxSigOffset + ctxIdx], 0);
+        estBitsSbac.significantBits[1][ctxIdx] = sbacGetEntropyBits(m_contextState[ctxSigOffset + ctxIdx], 1);
+    }
+
+    const uint32_t maxGroupIdx = log2TrSize * 2 - 1;
     if (bIsLuma)
     {
-        for (uint32_t bin = 0; bin < 2; bin++)
-            estBitsSbac.significantBits[bin][0] = sbacGetEntropyBits(m_contextState[OFF_SIG_FLAG_CTX], bin);
+        if (log2TrSize == 2)
+        {
+            for (int i = 0, ctxIdx = 0; i < 2; i++, ctxIdx += NUM_CTX_LAST_FLAG_XY)
+            {
+                int bits = 0;
+                const uint8_t *ctxState = &m_contextState[OFF_CTX_LAST_FLAG_X + ctxIdx];
 
-        for (int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++)
-            for (uint32_t bin = 0; bin < 2; bin++)
-                estBitsSbac.significantBits[bin][ctxIdx] = sbacGetEntropyBits(m_contextState[OFF_SIG_FLAG_CTX + ctxIdx], bin);
+                for (uint32_t ctx = 0; ctx < 3; ctx++)
+                {
+                    estBitsSbac.lastBits[i][ctx] = bits + sbacGetEntropyBits(ctxState[ctx], 0);
+                    bits += sbacGetEntropyBits(ctxState[ctx], 1);
+                }
+
+                estBitsSbac.lastBits[i][maxGroupIdx] = bits;
+            }
+        }
+        else
+        {
+            const int blkSizeOffset = ((log2TrSize - 2) * 3 + (log2TrSize == 5));
+
+            for (int i = 0, ctxIdx = 0; i < 2; i++, ctxIdx += NUM_CTX_LAST_FLAG_XY)
+            {
+                int bits = 0;
+                const uint8_t *ctxState = &m_contextState[OFF_CTX_LAST_FLAG_X + ctxIdx];
+                X265_CHECK(maxGroupIdx & 1, "maxGroupIdx check failure\n");
+
+                for (uint32_t ctx = 0; ctx < (maxGroupIdx >> 1) + 1; ctx++)
+                {
+                    const int cost0 = sbacGetEntropyBits(ctxState[blkSizeOffset + ctx], 0);
+                    const int cost1 = sbacGetEntropyBits(ctxState[blkSizeOffset + ctx], 1);
+                    estBitsSbac.lastBits[i][ctx * 2 + 0] = bits + cost0;
+                    estBitsSbac.lastBits[i][ctx * 2 + 1] = bits + cost1 + cost0;
+                    bits += 2 * cost1;
+                }
+                // correct latest bit cost, it didn't include cost0
+                estBitsSbac.lastBits[i][maxGroupIdx] -= sbacGetEntropyBits(ctxState[blkSizeOffset + (maxGroupIdx >> 1)], 0);
+            }
+        }
     }
     else
     {
-        for (uint32_t bin = 0; bin < 2; bin++)
-            estBitsSbac.significantBits[bin][0] = sbacGetEntropyBits(m_contextState[OFF_SIG_FLAG_CTX + (NUM_SIG_FLAG_CTX_LUMA + 0)], bin);
+        const int blkSizeOffset = NUM_CTX_LAST_FLAG_XY_LUMA;
+        const int ctxShift = log2TrSize - 2;
 
-        for (int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++)
-            for (uint32_t bin = 0; bin < 2; bin++)
-                estBitsSbac.significantBits[bin][ctxIdx] = sbacGetEntropyBits(m_contextState[OFF_SIG_FLAG_CTX + (NUM_SIG_FLAG_CTX_LUMA + ctxIdx)], bin);
-    }
+        for (int i = 0, ctxIdx = 0; i < 2; i++, ctxIdx += NUM_CTX_LAST_FLAG_XY)
+        {
+            int bits = 0;
+            const uint8_t *ctxState = &m_contextState[OFF_CTX_LAST_FLAG_X + ctxIdx];
 
-    int blkSizeOffset = bIsLuma ? ((log2TrSize - 2) * 3 + ((log2TrSize - 1) >> 2)) : NUM_CTX_LAST_FLAG_XY_LUMA;
-    int ctxShift = bIsLuma ? ((log2TrSize + 1) >> 2) : log2TrSize - 2;
-    uint32_t maxGroupIdx = log2TrSize * 2 - 1;
+            for (uint32_t ctx = 0; ctx < maxGroupIdx; ctx++)
+            {
+                int ctxOffset = blkSizeOffset + (ctx >> ctxShift);
+                estBitsSbac.lastBits[i][ctx] = bits + sbacGetEntropyBits(ctxState[ctxOffset], 0);
+                bits += sbacGetEntropyBits(ctxState[ctxOffset], 1);
+            }
 
-    uint32_t ctx;
-    for (int i = 0, ctxIdx = 0; i < 2; i++, ctxIdx += NUM_CTX_LAST_FLAG_XY)
-    {
-        int bits = 0;
-        const uint8_t *ctxState = &m_contextState[OFF_CTX_LAST_FLAG_X + ctxIdx];
-
-        for (ctx = 0; ctx < maxGroupIdx; ctx++)
-        {
-            int ctxOffset = blkSizeOffset + (ctx >> ctxShift);
-            estBitsSbac.lastBits[i][ctx] = bits + sbacGetEntropyBits(ctxState[ctxOffset], 0);
-            bits += sbacGetEntropyBits(ctxState[ctxOffset], 1);
+            estBitsSbac.lastBits[i][maxGroupIdx] = bits;
         }
-
-        estBitsSbac.lastBits[i][ctx] = bits;
     }
 }
 



More information about the x265-devel mailing list