[x265] [PATCH] improvement TComTrQuant::calcPatternSigCtx

Min Chen chenm003 at 163.com
Thu Oct 24 13:33:38 CEST 2013


# HG changeset patch
# User Min Chen <chenm003 at 163.com>
# Date 1382614395 -28800
# Node ID d088c6af083c273b91b5b50e4db1cf5e367f9adc
# Parent  5e3bc274a414cb346e7591bf01209604dc092060
improvement TComTrQuant::calcPatternSigCtx

1. replace width/height by size, since there are only NxN
2. use mask operatior to avoid condition and branch

diff -r 5e3bc274a414 -r d088c6af083c source/Lib/TLibCommon/TComTrQuant.cpp
--- a/source/Lib/TLibCommon/TComTrQuant.cpp	Thu Oct 24 17:07:33 2013 +0800
+++ b/source/Lib/TLibCommon/TComTrQuant.cpp	Thu Oct 24 19:33:15 2013 +0800
@@ -259,6 +259,8 @@
     int add = 0;
     bool useRDOQ = cu->getTransformSkip(absPartIdx, ttype) ? m_useRDOQTS : m_useRDOQ;
 
+    assert(width == height);
+
     if (useRDOQ && (ttype == TEXT_LUMA || RDOQ_CHROMA))
     {
         acSum = xRateDistOptQuant(cu, coef, qCoef, width, height, ttype, absPartIdx, lastPos);
@@ -590,7 +592,7 @@
         UInt cgPosX   = cgBlkPos - (cgPosY * numBlkSide);
         ::memset(&rdStats, 0, sizeof(coeffGroupRDStats));
 
-        const int patternSigCtx = TComTrQuant::calcPatternSigCtx(sigCoeffGroupFlag, cgPosX, cgPosY, width, height);
+        const int patternSigCtx = TComTrQuant::calcPatternSigCtx(sigCoeffGroupFlag, cgPosX, cgPosY, log2BlkSize);
         for (int scanPosinCG = cgSize - 1; scanPosinCG >= 0; scanPosinCG--)
         {
             scanPos = cgScanPos * cgSize + scanPosinCG;
@@ -1020,23 +1022,27 @@
  * \param height height of the block
  * \returns pattern for current coefficient group
  */
-int TComTrQuant::calcPatternSigCtx(const UInt* sigCoeffGroupFlag, UInt posXCG, UInt posYCG, int width, int height)
+int TComTrQuant::calcPatternSigCtx(const UInt* sigCoeffGroupFlag, UInt posXCG, UInt posYCG, int log2BlockSize)
 {
-    if (width == 4 && height == 4) return -1;
+    if (log2BlockSize == 2)
+        return -1;
 
-    UInt sigRight = 0;
-    UInt sigLower = 0;
+    log2BlockSize -= 2;
 
-    width >>= 2;
-    height >>= 2;
-    if (posXCG < width - 1)
-    {
-        sigRight = (sigCoeffGroupFlag[posYCG * width + posXCG + 1] != 0);
-    }
-    if (posYCG < height - 1)
-    {
-        sigLower = (sigCoeffGroupFlag[(posYCG  + 1) * width + posXCG] != 0);
-    }
+    const int size = (1 << log2BlockSize);
+    const UInt* sigPos = &sigCoeffGroupFlag[(posYCG << log2BlockSize) + posXCG];
+
+    assert(sigPos[1] <= 1);
+    assert(sigPos[size] <= 1);
+
+    UInt sigRight = (sigPos[1]);
+    UInt sigLower = (sigPos[size]);
+    int maskRight = ((int)(posXCG - size + 1)) >> 31;
+    int maskLower = ((int)(posYCG - size + 1)) >> 31;
+
+    sigRight &= maskRight;
+    sigLower &= maskLower;
+
     return sigRight + (sigLower << 1);
 }
 
diff -r 5e3bc274a414 -r d088c6af083c source/Lib/TLibCommon/TComTrQuant.h
--- a/source/Lib/TLibCommon/TComTrQuant.h	Thu Oct 24 17:07:33 2013 +0800
+++ b/source/Lib/TLibCommon/TComTrQuant.h	Thu Oct 24 19:33:15 2013 +0800
@@ -160,7 +160,7 @@
     void processScalingListEnc(int *coeff, int *quantcoeff, int quantScales, UInt height, UInt width, UInt ratio, int sizuNum, UInt dc);
     void processScalingListDec(int *coeff, int *dequantcoeff, int invQuantScales, UInt height, UInt width, UInt ratio, int sizuNum, UInt dc);
 
-    static int  calcPatternSigCtx(const UInt* sigCoeffGroupFlag, UInt posXCG, UInt posYCG, int width, int height);
+    static int  calcPatternSigCtx(const UInt* sigCoeffGroupFlag, UInt posXCG, UInt posYCG, int log2BlockSize);
 
     static int  getSigCtxInc(int patternSigCtx, UInt scanIdx, int posX, int posY, int log2BlkSize, TextType ttype);
 
diff -r 5e3bc274a414 -r d088c6af083c source/Lib/TLibEncoder/TEncSbac.cpp
--- a/source/Lib/TLibEncoder/TEncSbac.cpp	Thu Oct 24 17:07:33 2013 +0800
+++ b/source/Lib/TLibEncoder/TEncSbac.cpp	Thu Oct 24 19:33:15 2013 +0800
@@ -2328,7 +2328,7 @@
         // encode significant_coeff_flag
         if (sigCoeffGroupFlag[cgBlkPos])
         {
-            int patternSigCtx = TComTrQuant::calcPatternSigCtx(sigCoeffGroupFlag, cgPosX, cgPosY, width, height);
+            int patternSigCtx = TComTrQuant::calcPatternSigCtx(sigCoeffGroupFlag, cgPosX, cgPosY, log2BlockSize);
             UInt blkPos, posy, posx, sig, ctxSig;
             for (; scanPosSig >= subPos; scanPosSig--)
             {



More information about the x265-devel mailing list