[x265] [PATCH] Quant: modified rate cost calculation of last significant coefficient

ashok at multicorewareinc.com ashok at multicorewareinc.com
Wed Jan 7 11:25:38 CET 2015


# HG changeset patch
# User Ashok Kumar Mishra<ashok at multicorewareinc.com>
# Date 1420626307 -19800
#      Wed Jan 07 15:55:07 2015 +0530
# Node ID c4c8a11ad6c639927be6cd9239f773937384743c
# Parent  357ec738fb0ccaa678ab548629666b118f9f938f
Quant: modified rate cost calculation of last significant coefficient

diff -r 357ec738fb0c -r c4c8a11ad6c6 source/common/quant.cpp
--- a/source/common/quant.cpp	Tue Jan 06 15:39:58 2015 +0530
+++ b/source/common/quant.cpp	Wed Jan 07 15:55:07 2015 +0530
@@ -838,12 +838,23 @@
              * cost of signaling it as not-significant */
             uint32_t blkPos = codeParams.scan[scanPos];
             if (dstCoeff[blkPos])
-            {
-                /* Swap the cost of signaling its significant coeff bit with the cost of
-                 * signaling its lastNZ pos */
-                uint32_t posY = blkPos >> log2TrSize;
-                uint32_t posX = blkPos - (posY << log2TrSize);
-                uint32_t bitsLastNZ = codeParams.scanType == SCAN_VER ? getRateLast(posY, posX) : getRateLast(posX, posY);
+            {                
+                // Calculates the cost of signaling the last significant coefficient in the block 
+                int pos[2] = { (blkPos & (trSize - 1)), (blkPos >> log2TrSize) };
+                if (codeParams.scanType == SCAN_VER)
+                    std::swap(pos[0], pos[1]);
+                uint32_t bitsLastNZ = 0;
+
+                for (int i = 0; i < 2; i++)
+                {
+                    int temp = g_lastCoeffTable[pos[i]];
+                    int prefixOnes = temp & 15;
+                    int suffixLen = temp >> 4;
+
+                    bitsLastNZ += m_entropyCoder->m_estBitsSbac.lastBits[i][prefixOnes];
+                    bitsLastNZ += IEP_RATE * suffixLen;
+                }
+
                 int64_t costAsLast = totalRdCost - costSig[scanPos] + SIGCOST(bitsLastNZ);
 
                 if (costAsLast < bestCost)
@@ -1092,21 +1103,6 @@
     return (bIsLuma && (posX | posY) >= 4) ? 3 + offset : offset;
 }
 
-/* Calculates the cost of signaling the last significant coefficient in the block */
-inline uint32_t Quant::getRateLast(uint32_t posx, uint32_t posy) const
-{
-    uint32_t ctxX = getGroupIdx(posx);
-    uint32_t ctxY = getGroupIdx(posy);
-    uint32_t cost = m_entropyCoder->m_estBitsSbac.lastXBits[ctxX] + m_entropyCoder->m_estBitsSbac.lastYBits[ctxY];
-
-    int32_t maskX = (int32_t)(2 - posx) >> 31;
-    int32_t maskY = (int32_t)(2 - posy) >> 31;
-
-    cost += maskX & (IEP_RATE * ((ctxX - 2) >> 1));
-    cost += maskY & (IEP_RATE * ((ctxY - 2) >> 1));
-    return cost;
-}
-
 /* Context derivation process of coeff_abs_significant_flag */
 uint32_t Quant::getSigCoeffGroupCtxInc(uint64_t cgGroupMask, uint32_t cgPosX, uint32_t cgPosY, uint32_t log2TrSizeCG)
 {
diff -r 357ec738fb0c -r c4c8a11ad6c6 source/common/quant.h
--- a/source/common/quant.h	Tue Jan 06 15:39:58 2015 +0530
+++ b/source/common/quant.h	Wed Jan 07 15:55:07 2015 +0530
@@ -122,29 +122,7 @@
     uint32_t signBitHidingHDQ(int16_t* qcoeff, int32_t* deltaU, uint32_t numSig, const TUEntropyCodingParameters &codingParameters);
 
     uint32_t rdoQuant(const CUData& cu, int16_t* dstCoeff, uint32_t log2TrSize, TextType ttype, uint32_t absPartIdx, bool usePsy);
-    inline uint32_t getRateLast(uint32_t posx, uint32_t posy) const;
 };
-
-static inline uint32_t getGroupIdx(const uint32_t idx)
-{
-    // TODO: Why is this not a table lookup?
-
-    uint32_t group = (idx >> 3);
-
-    if (idx >= 24)
-        group = 2;
-    uint32_t groupIdx = ((idx >> (group + 1)) - 2) + 4 + (group << 1);
-    if (idx <= 3)
-        groupIdx = idx;
-
-#ifdef _DEBUG
-    static const uint8_t g_groupIdx[32] = { 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9 };
-    assert(groupIdx == g_groupIdx[idx]);
-#endif
-
-    return groupIdx;
-}
-
 }
 
 #endif // ifndef X265_QUANT_H
diff -r 357ec738fb0c -r c4c8a11ad6c6 source/encoder/entropy.cpp
--- a/source/encoder/entropy.cpp	Tue Jan 06 15:39:58 2015 +0530
+++ b/source/encoder/entropy.cpp	Wed Jan 07 15:55:07 2015 +0530
@@ -1715,32 +1715,26 @@
             for (uint32_t bin = 0; bin < 2; bin++)
                 estBitsSbac.significantBits[ctxIdx][bin] = sbacGetEntropyBits(m_contextState[OFF_SIG_FLAG_CTX + (NUM_SIG_FLAG_CTX_LUMA + ctxIdx)], bin);
     }
-    int bitsX = 0, bitsY = 0;
 
     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;
 
     uint32_t ctx;
-    const uint8_t *ctxX = &m_contextState[OFF_CTX_LAST_FLAG_X];
-    for (ctx = 0; ctx < maxGroupIdx; ctx++)
+    for (int i = 0, ctxIdx = 0; i < 2; i++, ctxIdx += NUM_CTX_LAST_FLAG_XY)
     {
-        int ctxOffset = blkSizeOffset + (ctx >> ctxShift);
-        estBitsSbac.lastXBits[ctx] = bitsX + sbacGetEntropyBits(ctxX[ctxOffset], 0);
-        bitsX += sbacGetEntropyBits(ctxX[ctxOffset], 1);
+        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][ctx] = bits;
     }
-
-    estBitsSbac.lastXBits[ctx] = bitsX;
-
-    const uint8_t *ctxY = &m_contextState[OFF_CTX_LAST_FLAG_Y];
-    for (ctx = 0; ctx < maxGroupIdx; ctx++)
-    {
-        int ctxOffset = blkSizeOffset + (ctx >> ctxShift);
-        estBitsSbac.lastYBits[ctx] = bitsY + sbacGetEntropyBits(ctxY[ctxOffset], 0);
-        bitsY += sbacGetEntropyBits(ctxY[ctxOffset], 1);
-    }
-
-    estBitsSbac.lastYBits[ctx] = bitsY;
 }
 
 /* estimate bit cost of significant coefficient */
diff -r 357ec738fb0c -r c4c8a11ad6c6 source/encoder/entropy.h
--- a/source/encoder/entropy.h	Tue Jan 06 15:39:58 2015 +0530
+++ b/source/encoder/entropy.h	Wed Jan 07 15:55:07 2015 +0530
@@ -88,8 +88,7 @@
 {
     int significantCoeffGroupBits[NUM_SIG_CG_FLAG_CTX][2];
     int significantBits[NUM_SIG_FLAG_CTX][2];
-    int lastXBits[10];
-    int lastYBits[10];
+    int lastBits[2][10];
     int greaterOneBits[NUM_ONE_FLAG_CTX][2];
     int levelAbsBits[NUM_ABS_FLAG_CTX][2];
     int blockCbpBits[NUM_QT_CBF_CTX][2];


More information about the x265-devel mailing list