[x265] residual coding unit is always square

Satoshi Nakagawa nakagawa424 at oki.com
Mon Mar 3 03:48:45 CET 2014


# HG changeset patch
# User Satoshi Nakagawa <nakagawa424 at oki.com>
# Date 1393814793 -32400
#      Mon Mar 03 11:46:33 2014 +0900
# Node ID 504d9373c3ff93f17f18b229826637933580d9ea
# Parent  288a83d7e28999798859eba6b2f38c952cac7547
residual coding unit is always square

diff -r 288a83d7e289 -r 504d9373c3ff source/Lib/TLibCommon/CommonDef.h
--- a/source/Lib/TLibCommon/CommonDef.h	Sun Mar 02 18:57:46 2014 -0600
+++ b/source/Lib/TLibCommon/CommonDef.h	Mon Mar 03 11:46:33 2014 +0900
@@ -88,8 +88,7 @@
 
 #define MLS_GRP_NUM                 64 ///< G644 : Max number of coefficient groups, max(16, 64)
 #define MLS_CG_SIZE                 4 ///< G644 : Coefficient group size of 4x4
-#define MLS_CG_LOG2_WIDTH           2
-#define MLS_CG_LOG2_HEIGHT          2
+#define MLS_CG_LOG2_SIZE            2
 
 #define ARL_C_PRECISION             7 ///< G382: 7-bit arithmetic precision
 #define LEVEL_RANGE                 30 ///< G382: max coefficient level in statistics collection
diff -r 288a83d7e289 -r 504d9373c3ff source/Lib/TLibCommon/TComDataCU.cpp
--- a/source/Lib/TLibCommon/TComDataCU.cpp	Sun Mar 02 18:57:46 2014 -0600
+++ b/source/Lib/TLibCommon/TComDataCU.cpp	Mon Mar 03 11:46:33 2014 +0900
@@ -2833,7 +2833,7 @@
                                        + (partWidth / m_pic->getMinCUWidth()) / 2];
 }
 
-uint32_t TComDataCU::getCoefScanIdx(uint32_t absPartIdx, uint32_t width, uint32_t height, bool bIsLuma, bool bIsIntra)
+uint32_t TComDataCU::getCoefScanIdx(uint32_t absPartIdx, uint32_t log2TrSize, bool bIsLuma, bool bIsIntra)
 {
     uint32_t scanIdx;
     uint32_t dirMode;
@@ -2846,57 +2846,28 @@
 
     if (bIsLuma)
     {
-        const uint32_t maximumWidth  = MDCS_MAXIMUM_WIDTH;
-        const uint32_t maximumHeight = MDCS_MAXIMUM_HEIGHT;
-
-        if ((width > maximumWidth) || (height > maximumHeight)) return SCAN_DIAG;
+        if (log2TrSize > MDCS_LOG2_MAX_SIZE) return SCAN_DIAG;
 
         dirMode = getLumaIntraDir(absPartIdx);
     }
     else
     {
-        const uint32_t maximumWidth  = MDCS_MAXIMUM_WIDTH  >> m_hChromaShift;
-        const uint32_t maximumHeight = MDCS_MAXIMUM_HEIGHT >> m_vChromaShift;
-
-        if ((width > maximumWidth) || (height > maximumHeight)) return SCAN_DIAG;
+        if (log2TrSize > (MDCS_LOG2_MAX_SIZE - m_hChromaShift)) return SCAN_DIAG;
 
         dirMode  = getChromaIntraDir(absPartIdx);
         if (dirMode == DM_CHROMA_IDX)
         {
             dirMode = getLumaIntraDir(absPartIdx);
         }
+        // TODO: 4:2:2
     }
 
-    switch (MDCS_MODE)
-    {
-    case MDCS_BOTH_DIRECTIONS:
-        if (abs((int)dirMode - VER_IDX) <= MDCS_ANGLE_LIMIT)
-            scanIdx = SCAN_HOR;
-        else if (abs((int)dirMode - HOR_IDX) <= MDCS_ANGLE_LIMIT)
-            scanIdx = SCAN_VER;
-        else
-            scanIdx = SCAN_DIAG;
-        break;
-
-    case MDCS_VERTICAL_ONLY:
-        if (abs((int)dirMode - HOR_IDX) <= MDCS_ANGLE_LIMIT)
-            scanIdx = SCAN_VER;
-        break;
-
-    case MDCS_HORIZONTAL_ONLY:
-        if (abs((int)dirMode - VER_IDX) <= MDCS_ANGLE_LIMIT)
-            scanIdx = SCAN_HOR;
-        break;
-
-    case MDCS_DISABLED:
-        scanIdx = 0;
-        break;
-
-    default:
-        assert(false);
-        x265_log(NULL, X265_LOG_ERROR, "Unrecognised MDCS mode\n");
-        break;
-    }
+    if (abs((int)dirMode - VER_IDX) <= MDCS_ANGLE_LIMIT)
+        scanIdx = SCAN_HOR;
+    else if (abs((int)dirMode - HOR_IDX) <= MDCS_ANGLE_LIMIT)
+        scanIdx = SCAN_VER;
+    else
+        scanIdx = SCAN_DIAG;
 
     return scanIdx;
 }
diff -r 288a83d7e289 -r 504d9373c3ff source/Lib/TLibCommon/TComDataCU.h
--- a/source/Lib/TLibCommon/TComDataCU.h	Sun Mar 02 18:57:46 2014 -0600
+++ b/source/Lib/TLibCommon/TComDataCU.h	Mon Mar 03 11:46:33 2014 +0900
@@ -472,7 +472,7 @@
 
     uint32_t&     getTotalNumPart()               { return m_numPartitions; }
 
-    uint32_t      getCoefScanIdx(uint32_t absPartIdx, uint32_t width, uint32_t height, bool bIsLuma, bool bIsIntra);
+    uint32_t      getCoefScanIdx(uint32_t absPartIdx, uint32_t log2TrSize, bool bIsLuma, bool bIsIntra);
 
     // -------------------------------------------------------------------------------------------------------------------
     // member functions to support multiple color space formats
diff -r 288a83d7e289 -r 504d9373c3ff source/Lib/TLibCommon/TComRom.cpp
--- a/source/Lib/TLibCommon/TComRom.cpp	Sun Mar 02 18:57:46 2014 -0600
+++ b/source/Lib/TLibCommon/TComRom.cpp	Mon Mar 03 11:46:33 2014 +0900
@@ -45,7 +45,7 @@
 //! \ingroup TLibCommon
 //! \{
 // scanning order table
-uint32_t* g_scanOrder[SCAN_NUMBER_OF_GROUP_TYPES][SCAN_NUMBER_OF_TYPES][MAX_CU_DEPTH][MAX_CU_DEPTH];
+uint32_t* g_scanOrder[SCAN_NUMBER_OF_GROUP_TYPES][SCAN_NUMBER_OF_TYPES][MAX_CU_DEPTH];
 
 class ScanGenerator
 {
@@ -148,64 +148,61 @@
     }
 
     // initialise scan orders
-    for (uint32_t log2BlockHeight = 0; log2BlockHeight < MAX_CU_DEPTH; log2BlockHeight++)
+    for (uint32_t log2BlockSize = 0; log2BlockSize < MAX_CU_DEPTH; log2BlockSize++)
     {
-        for (uint32_t log2BlockWidth = 0; log2BlockWidth < MAX_CU_DEPTH; log2BlockWidth++)
+        const uint32_t blockWidth  = 1 << log2BlockSize;
+        const uint32_t blockHeight = 1 << log2BlockSize;
+        const uint32_t totalValues = blockWidth * blockHeight;
+        //non-grouped scan orders
+        for (uint32_t scanTypeIndex = 0; scanTypeIndex < SCAN_NUMBER_OF_TYPES; scanTypeIndex++)
         {
-            const uint32_t blockWidth  = 1 << log2BlockWidth;
-            const uint32_t blockHeight = 1 << log2BlockHeight;
-            const uint32_t totalValues = blockWidth * blockHeight;
-            //non-grouped scan orders
-            for (uint32_t scanTypeIndex = 0; scanTypeIndex < SCAN_NUMBER_OF_TYPES; scanTypeIndex++)
+            const COEFF_SCAN_TYPE scanType = COEFF_SCAN_TYPE(scanTypeIndex);
+            g_scanOrder[SCAN_UNGROUPED][scanType][log2BlockSize] = X265_MALLOC(uint32_t, totalValues);
+            ScanGenerator fullBlockScan(blockWidth, blockHeight, blockWidth, scanType);
+
+            for (uint32_t scanPosition = 0; scanPosition < totalValues; scanPosition++)
             {
-                const COEFF_SCAN_TYPE scanType = COEFF_SCAN_TYPE(scanTypeIndex);
-                g_scanOrder[SCAN_UNGROUPED][scanType][log2BlockWidth][log2BlockHeight] = X265_MALLOC(uint32_t, totalValues);
-                ScanGenerator fullBlockScan(blockWidth, blockHeight, blockWidth, scanType);
+                g_scanOrder[SCAN_UNGROUPED][scanType][log2BlockSize][scanPosition] = fullBlockScan.GetNextIndex(0, 0);
+            }
+        }
 
-                for (uint32_t scanPosition = 0; scanPosition < totalValues; scanPosition++)
+        //grouped scan orders
+        const uint32_t  groupWidth           = 1 << MLS_CG_LOG2_SIZE;
+        const uint32_t  groupHeight          = 1 << MLS_CG_LOG2_SIZE;
+        const uint32_t  widthInGroups        = blockWidth  >> MLS_CG_LOG2_SIZE;
+        const uint32_t  heightInGroups       = blockHeight >> MLS_CG_LOG2_SIZE;
+
+        const uint32_t  groupSize            = groupWidth    * groupHeight;
+        const uint32_t  totalGroups          = widthInGroups * heightInGroups;
+
+        for (uint32_t scanTypeIndex = 0; scanTypeIndex < SCAN_NUMBER_OF_TYPES; scanTypeIndex++)
+        {
+            const COEFF_SCAN_TYPE scanType = COEFF_SCAN_TYPE(scanTypeIndex);
+
+            g_scanOrder[SCAN_GROUPED_4x4][scanType][log2BlockSize] = X265_MALLOC(uint32_t, totalValues);
+
+            ScanGenerator fullBlockScan(widthInGroups, heightInGroups, groupWidth, scanType);
+
+            for (uint32_t groupIndex = 0; groupIndex < totalGroups; groupIndex++)
+            {
+                const uint32_t groupPositionY  = fullBlockScan.GetCurrentY();
+                const uint32_t groupPositionX  = fullBlockScan.GetCurrentX();
+                const uint32_t groupOffsetX    = groupPositionX * groupWidth;
+                const uint32_t groupOffsetY    = groupPositionY * groupHeight;
+                const uint32_t groupOffsetScan = groupIndex     * groupSize;
+
+                ScanGenerator groupScan(groupWidth, groupHeight, blockWidth, scanType);
+
+                for (uint32_t scanPosition = 0; scanPosition < groupSize; scanPosition++)
                 {
-                    g_scanOrder[SCAN_UNGROUPED][scanType][log2BlockWidth][log2BlockHeight][scanPosition] = fullBlockScan.GetNextIndex(0, 0);
+                    g_scanOrder[SCAN_GROUPED_4x4][scanType][log2BlockSize][groupOffsetScan + scanPosition] = groupScan.GetNextIndex(groupOffsetX, groupOffsetY);
                 }
+
+                fullBlockScan.GetNextIndex(0, 0);
             }
+        }
 
-            //grouped scan orders
-            const uint32_t  groupWidth           = 1 << MLS_CG_LOG2_WIDTH;
-            const uint32_t  groupHeight          = 1 << MLS_CG_LOG2_HEIGHT;
-            const uint32_t  widthInGroups        = blockWidth  >> MLS_CG_LOG2_WIDTH;
-            const uint32_t  heightInGroups       = blockHeight >> MLS_CG_LOG2_HEIGHT;
-
-            const uint32_t  groupSize            = groupWidth    * groupHeight;
-            const uint32_t  totalGroups          = widthInGroups * heightInGroups;
-
-            for (uint32_t scanTypeIndex = 0; scanTypeIndex < SCAN_NUMBER_OF_TYPES; scanTypeIndex++)
-            {
-                const COEFF_SCAN_TYPE scanType = COEFF_SCAN_TYPE(scanTypeIndex);
-
-                g_scanOrder[SCAN_GROUPED_4x4][scanType][log2BlockWidth][log2BlockHeight] = X265_MALLOC(uint32_t, totalValues);
-
-                ScanGenerator fullBlockScan(widthInGroups, heightInGroups, groupWidth, scanType);
-
-                for (uint32_t groupIndex = 0; groupIndex < totalGroups; groupIndex++)
-                {
-                    const uint32_t groupPositionY  = fullBlockScan.GetCurrentY();
-                    const uint32_t groupPositionX  = fullBlockScan.GetCurrentX();
-                    const uint32_t groupOffsetX    = groupPositionX * groupWidth;
-                    const uint32_t groupOffsetY    = groupPositionY * groupHeight;
-                    const uint32_t groupOffsetScan = groupIndex     * groupSize;
-
-                    ScanGenerator groupScan(groupWidth, groupHeight, blockWidth, scanType);
-
-                    for (uint32_t scanPosition = 0; scanPosition < groupSize; scanPosition++)
-                    {
-                        g_scanOrder[SCAN_GROUPED_4x4][scanType][log2BlockWidth][log2BlockHeight][groupOffsetScan + scanPosition] = groupScan.GetNextIndex(groupOffsetX, groupOffsetY);
-                    }
-
-                    fullBlockScan.GetNextIndex(0, 0);
-                }
-            }
-
-            //--------------------------------------------------------------------------------------------------
-        }
+        //--------------------------------------------------------------------------------------------------
     }
 }
 
@@ -218,12 +215,9 @@
     {
         for (uint32_t scanOrderIndex = 0; scanOrderIndex < SCAN_NUMBER_OF_TYPES; scanOrderIndex++)
         {
-            for (uint32_t log2BlockWidth = 0; log2BlockWidth < MAX_CU_DEPTH; log2BlockWidth++)
+            for (uint32_t log2BlockSize = 0; log2BlockSize < MAX_CU_DEPTH; log2BlockSize++)
             {
-                for (uint32_t log2BlockHeight = 0; log2BlockHeight < MAX_CU_DEPTH; log2BlockHeight++)
-                {
-                    X265_FREE(g_scanOrder[groupTypeIndex][scanOrderIndex][log2BlockWidth][log2BlockHeight]);
-                }
+                X265_FREE(g_scanOrder[groupTypeIndex][scanOrderIndex][log2BlockSize]);
             }
         }
     }
diff -r 288a83d7e289 -r 504d9373c3ff source/Lib/TLibCommon/TComRom.h
--- a/source/Lib/TLibCommon/TComRom.h	Sun Mar 02 18:57:46 2014 -0600
+++ b/source/Lib/TLibCommon/TComRom.h	Mon Mar 03 11:46:33 2014 +0900
@@ -78,7 +78,7 @@
 // flexible conversion from relative to absolute index
 extern uint32_t g_zscanToRaster[MAX_NUM_SPU_W * MAX_NUM_SPU_W];
 extern uint32_t g_rasterToZscan[MAX_NUM_SPU_W * MAX_NUM_SPU_W];
-extern uint32_t*  g_scanOrder[SCAN_NUMBER_OF_GROUP_TYPES][SCAN_NUMBER_OF_TYPES][MAX_CU_DEPTH][MAX_CU_DEPTH];
+extern uint32_t*  g_scanOrder[SCAN_NUMBER_OF_GROUP_TYPES][SCAN_NUMBER_OF_TYPES][MAX_CU_DEPTH];
 void initZscanToRaster(int maxDepth, int depth, uint32_t startVal, uint32_t*& curIdx);
 void initRasterToZscan(uint32_t maxCUWidth, uint32_t maxCUHeight, uint32_t maxCUDepth);
 
diff -r 288a83d7e289 -r 504d9373c3ff source/Lib/TLibCommon/TComTrQuant.cpp
--- a/source/Lib/TLibCommon/TComTrQuant.cpp	Sun Mar 02 18:57:46 2014 -0600
+++ b/source/Lib/TLibCommon/TComTrQuant.cpp	Mon Mar 03 11:46:33 2014 +0900
@@ -131,13 +131,13 @@
 // To minimize the distortion only. No rate is considered.
 void TComTrQuant::signBitHidingHDQ(TCoeff* qCoef, TCoeff* coef, int32_t* deltaU, const TUEntropyCodingParameters &codingParameters)
 {
-    const uint32_t trSize = codingParameters.widthInGroups << MLS_CG_LOG2_WIDTH;
+    const uint32_t log2TrSizeCG = codingParameters.log2TrSizeCG;
 
     int lastCG = -1;
     int absSum = 0;
     int n;
 
-    for (int subSet = (trSize * trSize - 1) >> LOG2_SCAN_SET_SIZE; subSet >= 0; subSet--)
+    for (int subSet = (1 << log2TrSizeCG * 2) - 1; subSet >= 0; subSet--)
     {
         int  subPos = subSet << LOG2_SCAN_SET_SIZE;
         int  firstNZPosInCG = SCAN_SET_SIZE, lastNZPosInCG = -1;
@@ -270,11 +270,11 @@
     }
     else
     {
+        const uint32_t log2TrSize = g_convertToBit[trSize] + 2;
         TUEntropyCodingParameters codingParameters;
-        getTUEntropyCodingParameters(cu, codingParameters, absPartIdx,  trSize, trSize, ttype);
+        getTUEntropyCodingParameters(cu, codingParameters, absPartIdx, log2TrSize, ttype);
         int deltaU[32 * 32];
 
-        const uint32_t log2TrSize = g_convertToBit[trSize] + 2;
         int scalingListType = (cu->isIntra(absPartIdx) ? 0 : 3) + ttype;
         assert(scalingListType < 6);
         int32_t *quantCoeff = 0;
@@ -285,7 +285,7 @@
         int qbits = QUANT_SHIFT + m_qpParam.m_per + transformShift;
         add = (cu->getSlice()->getSliceType() == I_SLICE ? 171 : 85) << (qbits - 9);
 
-        int numCoeff = trSize * trSize;
+        int numCoeff = 1 << log2TrSize * 2;
         acSum += primitives.quant(coef, quantCoeff, deltaU, qCoef, qbits, add, numCoeff, lastPos);
 
         if (cu->getSlice()->getPPS()->getSignHideFlag() && acSum >= 2)
@@ -487,34 +487,25 @@
 void TComTrQuant::getTUEntropyCodingParameters(TComDataCU*                cu,
                                                TUEntropyCodingParameters &result,
                                                uint32_t                   absPartIdx,
-                                               uint32_t                   width,
-                                               uint32_t                   height,
+                                               uint32_t                   log2TrSize,
                                                TextType                   ttype)
 {
-    //set the local parameters
-    const uint32_t                 log2BlockWidth  = g_convertToBit[width]  + 2;
-    const uint32_t                 log2BlockHeight = g_convertToBit[height] + 2;
-
-    result.scanType = COEFF_SCAN_TYPE(cu->getCoefScanIdx(absPartIdx, width, height, ttype == TEXT_LUMA, cu->isIntra(absPartIdx)));
-
     //set the group layout
-    result.widthInGroups  = width  >> MLS_CG_LOG2_WIDTH;
-    result.heightInGroups = height >> MLS_CG_LOG2_HEIGHT;
+    const uint32_t log2TrSizeCG = log2TrSize - MLS_CG_LOG2_SIZE;
+    result.log2TrSizeCG = log2TrSizeCG;
 
     //set the scan orders
-    const uint32_t log2WidthInGroups  = g_convertToBit[result.widthInGroups  * 4];
-    const uint32_t log2HeightInGroups = g_convertToBit[result.heightInGroups * 4];
-
-    result.scan   = g_scanOrder[SCAN_GROUPED_4x4][result.scanType][log2BlockWidth][log2BlockHeight];
-    result.scanCG = g_scanOrder[SCAN_UNGROUPED][result.scanType][log2WidthInGroups][log2HeightInGroups];
+    result.scanType = COEFF_SCAN_TYPE(cu->getCoefScanIdx(absPartIdx, log2TrSize, ttype == TEXT_LUMA, cu->isIntra(absPartIdx)));
+    result.scan   = g_scanOrder[SCAN_GROUPED_4x4][result.scanType][log2TrSize];
+    result.scanCG = g_scanOrder[SCAN_UNGROUPED][result.scanType][log2TrSizeCG];
 
     //set the significance map context selection parameters
     TextType ctype = ttype == TEXT_LUMA ? TEXT_LUMA : TEXT_CHROMA;
-    if ((width == 4) && (height == 4))
+    if (log2TrSize == 2)
     {
         result.firstSignificanceMapContext = significanceMapContextSetStart[ctype][CONTEXT_TYPE_4x4];
     }
-    else if ((width == 8) && (height == 8))
+    else if (log2TrSize == 3)
     {
         result.firstSignificanceMapContext = significanceMapContextSetStart[ctype][CONTEXT_TYPE_8x8];
         if (result.scanType != SCAN_DIAG)
@@ -566,13 +557,11 @@
     int sigRateDelta[32 * 32];
     int deltaU[32 * 32];
     TUEntropyCodingParameters codingParameters;
-    getTUEntropyCodingParameters(cu, codingParameters, absPartIdx,  trSize, trSize, ttype);
+    getTUEntropyCodingParameters(cu, codingParameters, absPartIdx, log2TrSize, ttype);
 
     const uint32_t cgSize = (1 << MLS_CG_SIZE); // 16
     double costCoeffGroupSig[MLS_GRP_NUM];
     uint32_t sigCoeffGroupFlag[MLS_GRP_NUM];
-    const uint32_t log2BlockWidth    = log2TrSize;
-    const uint32_t log2BlockHeight   = log2TrSize;
     uint32_t   ctxSet    = 0;
     int    c1            = 1;
     int    c2            = 0;
@@ -582,19 +571,19 @@
     uint32_t   c2Idx     = 0;
     int    cgLastScanPos = -1;
     int    baseLevel;
-    ::memset(sigCoeffGroupFlag, 0, sizeof(uint32_t) * MLS_GRP_NUM);
+    uint32_t cgNum = 1 << codingParameters.log2TrSizeCG * 2;
+    memset(sigCoeffGroupFlag, 0, sizeof(uint32_t) * cgNum);
 
-    uint32_t cgNum = trSize * trSize >> MLS_CG_SIZE;
     int scanPos;
     coeffGroupRDStats rdStats;
 
     for (int cgScanPos = cgNum - 1; cgScanPos >= 0; cgScanPos--)
     {
         uint32_t cgBlkPos = codingParameters.scanCG[cgScanPos];
-        uint32_t cgPosY   = cgBlkPos / codingParameters.widthInGroups;
-        uint32_t cgPosX   = cgBlkPos - (cgPosY * codingParameters.widthInGroups);
-        ::memset(&rdStats, 0, sizeof(coeffGroupRDStats));
-        const int patternSigCtx = TComTrQuant::calcPatternSigCtx(sigCoeffGroupFlag, cgPosX, cgPosY, codingParameters.widthInGroups, codingParameters.heightInGroups);
+        uint32_t cgPosY   = cgBlkPos >> codingParameters.log2TrSizeCG;
+        uint32_t cgPosX   = cgBlkPos - (cgPosY << codingParameters.log2TrSizeCG);
+        memset(&rdStats, 0, sizeof(coeffGroupRDStats));
+        const int patternSigCtx = TComTrQuant::calcPatternSigCtx(sigCoeffGroupFlag, cgPosX, cgPosY, codingParameters.log2TrSizeCG);
         for (int scanPosinCG = cgSize - 1; scanPosinCG >= 0; scanPosinCG--)
         {
             scanPos = cgScanPos * cgSize + scanPosinCG;
@@ -640,7 +629,7 @@
                 }
                 else
                 {
-                    uint16_t ctxSig = getSigCtxInc(patternSigCtx, codingParameters, scanPos, log2BlockWidth, log2BlockHeight, ttype);
+                    uint16_t ctxSig = getSigCtxInc(patternSigCtx, codingParameters, scanPos, log2TrSize, ttype);
                     level           = xGetCodedLevel(costCoeff[scanPos], costCoeff0[scanPos], costSig[scanPos],
                                                      levelDouble, maxAbsLevel, ctxSig, oneCtx, absCtx, goRiceParam,
                                                      c1Idx, c2Idx, qbits, scaleFactor, 0);
@@ -730,7 +719,7 @@
             {
                 if (sigCoeffGroupFlag[cgBlkPos] == 0)
                 {
-                    uint32_t  ctxSig = getSigCoeffGroupCtxInc(sigCoeffGroupFlag, cgPosX, cgPosY, codingParameters.widthInGroups, codingParameters.heightInGroups);
+                    uint32_t ctxSig = getSigCoeffGroupCtxInc(sigCoeffGroupFlag, cgPosX, cgPosY, codingParameters.log2TrSizeCG);
                     baseCost += xGetRateSigCoeffGroup(0, ctxSig) - rdStats.sigCost;
                     costCoeffGroupSig[cgScanPos] = xGetRateSigCoeffGroup(0, ctxSig);
                 }
@@ -747,7 +736,7 @@
                         double costZeroCG = baseCost;
 
                         // add SigCoeffGroupFlag cost to total cost
-                        uint32_t  ctxSig = getSigCoeffGroupCtxInc(sigCoeffGroupFlag, cgPosX, cgPosY, codingParameters.widthInGroups, codingParameters.heightInGroups);
+                        uint32_t ctxSig = getSigCoeffGroupCtxInc(sigCoeffGroupFlag, cgPosX, cgPosY, codingParameters.log2TrSizeCG);
                         if (cgScanPos < cgLastScanPos)
                         {
                             baseCost  += xGetRateSigCoeffGroup(1, ctxSig);
@@ -828,8 +817,8 @@
                 uint32_t blkPos = codingParameters.scan[scanPos];
                 if (dstCoeff[blkPos])
                 {
-                    uint32_t posY = blkPos >> log2BlockWidth;
-                    uint32_t posX = blkPos - (posY << log2BlockWidth);
+                    uint32_t posY = blkPos >> log2TrSize;
+                    uint32_t posX = blkPos - (posY << log2TrSize);
                     double costLast = codingParameters.scanType == SCAN_VER ? xGetRateLast(posY, posX) : xGetRateLast(posX, posY);
                     double totalCost = baseCost + costLast - costSig[scanPos];
 
@@ -1011,23 +1000,21 @@
 
 /** Pattern decision for context derivation process of significant_coeff_flag
  * \param sigCoeffGroupFlag pointer to prior coded significant coeff group
- * \param posXCG column of current coefficient group
- * \param posYCG row of current coefficient group
+ * \param cgPosX column of current coefficient group
+ * \param cgPosY row of current coefficient group
  * \param width width of the block
  * \param height height of the block
  * \returns pattern for current coefficient group
  */
-int TComTrQuant::calcPatternSigCtx(const uint32_t* sigCoeffGroupFlag, uint32_t posXCG, uint32_t posYCG, uint32_t widthInGroups, uint32_t heightInGroups)
+int TComTrQuant::calcPatternSigCtx(const uint32_t* sigCoeffGroupFlag, uint32_t cgPosX, uint32_t cgPosY, uint32_t log2TrSizeCG)
 {
-    if ((widthInGroups <= 1) && (heightInGroups <= 1)) return 0;
-    const bool rightAvailable = posXCG < (widthInGroups  - 1);
-    const bool belowAvailable = posYCG < (heightInGroups - 1);
-    uint32_t sigRight = 0;
-    uint32_t sigLower = 0;
-    if (rightAvailable)
-        sigRight = ((sigCoeffGroupFlag[(posYCG * widthInGroups) + posXCG + 1] != 0) ? 1 : 0);
-    if (belowAvailable)
-        sigLower = ((sigCoeffGroupFlag[(posYCG + 1) * widthInGroups + posXCG] != 0) ? 1 : 0);
+    if (log2TrSizeCG == 0) return 0;
+
+    const uint32_t trSizeCG = 1 << log2TrSizeCG;
+    const uint32_t* sigPos = &sigCoeffGroupFlag[(cgPosY << log2TrSizeCG) + cgPosX];
+    uint32_t sigRight = (cgPosX < (trSizeCG - 1)) && (sigPos[1] != 0);
+    uint32_t sigLower = (cgPosY < (trSizeCG - 1)) && (sigPos[trSizeCG] != 0);
+
     return sigRight + (sigLower << 1);
 }
 
@@ -1044,8 +1031,7 @@
 int TComTrQuant::getSigCtxInc(int                              patternSigCtx,
                               const TUEntropyCodingParameters &codingParameters,
                               const int                        scanPosition,
-                              const int                        log2BlockWidth,
-                              const int                        log2BlockHeight,
+                              const int                        log2TrSize,
                               const TextType                   ttype)
 {
     static const int ctxIndMap[16] =
@@ -1056,14 +1042,14 @@
         7, 7, 8, 8
     };
     const uint32_t rasterPosition = codingParameters.scan[scanPosition];
-    const uint32_t posY           = rasterPosition >> log2BlockWidth;
-    const uint32_t posX           = rasterPosition - (posY << log2BlockWidth);
+    const uint32_t posY           = rasterPosition >> log2TrSize;
+    const uint32_t posX           = rasterPosition - (posY << log2TrSize);
 
     if ((posX + posY) == 0) return 0; //special case for the DC context variable
 
     int offset = MAX_INT;
 
-    if ((log2BlockWidth == 2) && (log2BlockHeight == 2)) //4x4
+    if (log2TrSize == 2) //4x4
     {
         offset = ctxIndMap[(4 * posY) + posX];
     }
@@ -1075,8 +1061,8 @@
         {
         case 0: //neither neighbouring group is significant
         {
-            const int posXinSubset     = posX & ((1 << MLS_CG_LOG2_WIDTH)  - 1);
-            const int posYinSubset     = posY & ((1 << MLS_CG_LOG2_HEIGHT) - 1);
+            const int posXinSubset     = posX & ((1 << MLS_CG_LOG2_SIZE) - 1);
+            const int posYinSubset     = posY & ((1 << MLS_CG_LOG2_SIZE) - 1);
             const int posTotalInSubset = posXinSubset + posYinSubset;
 
             //first N coefficients in scan order use 2; the next few use 1; the rest use 0.
@@ -1089,8 +1075,8 @@
 
         case 1: //right group is significant, below is not
         {
-            const int posYinSubset = posY & ((1 << MLS_CG_LOG2_HEIGHT) - 1);
-            const int groupHeight  = 1 << MLS_CG_LOG2_HEIGHT;
+            const int posYinSubset = posY & ((1 << MLS_CG_LOG2_SIZE) - 1);
+            const int groupHeight  = 1 << MLS_CG_LOG2_SIZE;
 
             cnt = (posYinSubset >= (groupHeight >> 1)) ? 0 : ((posYinSubset >= (groupHeight >> 2)) ? 1 : 2); //top quarter uses 2; second-from-top quarter uses 1; bottom half uses 0
         }
@@ -1098,8 +1084,8 @@
 
         case 2: //below group is significant, right is not
         {
-            const int posXinSubset = posX & ((1 << MLS_CG_LOG2_WIDTH)  - 1);
-            const int groupWidth   = 1 << MLS_CG_LOG2_WIDTH;
+            const int posXinSubset = posX & ((1 << MLS_CG_LOG2_SIZE)  - 1);
+            const int groupWidth   = 1 << MLS_CG_LOG2_SIZE;
 
             cnt = (posXinSubset >= (groupWidth >> 1)) ? 0 : ((posXinSubset >= (groupWidth >> 2)) ? 1 : 2); //left quarter uses 2; second-from-left quarter uses 1; right half uses 0
         }
@@ -1117,7 +1103,7 @@
             break;
         }
 
-        const bool notFirstGroup = ((posX >> MLS_CG_LOG2_WIDTH) + (posY >> MLS_CG_LOG2_HEIGHT)) > 0;
+        const bool notFirstGroup = ((posX >> MLS_CG_LOG2_SIZE) + (posY >> MLS_CG_LOG2_SIZE)) > 0;
         TextType ctype = ttype == TEXT_LUMA ? TEXT_LUMA : TEXT_CHROMA;
         offset = (notFirstGroup ? notFirstGroupNeighbourhoodContextOffset[ctype] : 0) + cnt;
     }
@@ -1353,21 +1339,14 @@
 uint32_t TComTrQuant::getSigCoeffGroupCtxInc(const uint32_t* sigCoeffGroupFlag,
                                              const uint32_t  cgPosX,
                                              const uint32_t  cgPosY,
-                                             const uint32_t  widthInGroups,
-                                             const uint32_t  heightInGroups)
+                                             const uint32_t  log2TrSizeCG)
 {
-    uint32_t sigRight = 0;
-    uint32_t sigLower = 0;
+    const uint32_t trSizeCG = 1 << log2TrSizeCG;
+    const uint32_t* sigPos = &sigCoeffGroupFlag[(cgPosY << log2TrSizeCG) + cgPosX];
+    uint32_t sigRight = (cgPosX < (trSizeCG - 1)) && (sigPos[1] != 0);
+    uint32_t sigLower = (cgPosY < (trSizeCG - 1)) && (sigPos[trSizeCG] != 0);
 
-    if (cgPosX < (widthInGroups  - 1))
-    {
-        sigRight = ((sigCoeffGroupFlag[(cgPosY * widthInGroups) + cgPosX + 1] != 0) ? 1 : 0);
-    }
-    if (cgPosY < (heightInGroups - 1))
-    {
-        sigLower = ((sigCoeffGroupFlag[(cgPosY + 1) * widthInGroups + cgPosX] != 0) ? 1 : 0);
-    }
-    return ((sigRight + sigLower) != 0) ? 1 : 0;
+    return sigRight | sigLower;
 }
 
 /** set quantized matrix coefficient for encode
diff -r 288a83d7e289 -r 504d9373c3ff source/Lib/TLibCommon/TComTrQuant.h
--- a/source/Lib/TLibCommon/TComTrQuant.h	Sun Mar 02 18:57:46 2014 -0600
+++ b/source/Lib/TLibCommon/TComTrQuant.h	Mon Mar 03 11:46:33 2014 +0900
@@ -158,10 +158,10 @@
     void setScalingList(TComScalingList *scalingList);
     void processScalingListEnc(int32_t *coeff, int32_t *quantcoeff, int quantScales, uint32_t height, uint32_t width, uint32_t ratio, int sizuNum, uint32_t dc);
     void processScalingListDec(int32_t *coeff, int32_t *dequantcoeff, int invQuantScales, uint32_t height, uint32_t width, uint32_t ratio, int sizuNum, uint32_t dc);
-    static int  calcPatternSigCtx(const uint32_t* sigCoeffGroupFlag, uint32_t posXCG, uint32_t posYCG, uint32_t widthInGroups, uint32_t heightInGroups);
-    static int getSigCtxInc(int patternSigCtx, const TUEntropyCodingParameters &codingParameters, const int scanPosition, const int log2BlockWidth, const int log2BlockHeight, const TextType ttype);
-    static uint32_t getSigCoeffGroupCtxInc(const uint32_t* sigCoeffGroupFlag, uint32_t cGPosX, uint32_t cGPosY, const uint32_t widthInGroups, const uint32_t heightInGroups);
-    static void getTUEntropyCodingParameters(TComDataCU* cu, TUEntropyCodingParameters &result, uint32_t absPartIdx, uint32_t width, uint32_t height, TextType ttype);
+    static int calcPatternSigCtx(const uint32_t* sigCoeffGroupFlag, uint32_t cgPosX, uint32_t cgPosY, uint32_t log2TrSizeCG);
+    static int getSigCtxInc(int patternSigCtx, const TUEntropyCodingParameters &codingParameters, const int scanPosition, const int log2TrSize, const TextType ttype);
+    static uint32_t getSigCoeffGroupCtxInc(const uint32_t* sigCoeffGroupFlag, uint32_t cgPosX, uint32_t cgPosY, const uint32_t log2TrSizeCG);
+    static void getTUEntropyCodingParameters(TComDataCU* cu, TUEntropyCodingParameters &result, uint32_t absPartIdx, uint32_t log2TrSize, TextType ttype);
     estBitsSbacStruct* m_estBitsSbac;
 
 protected:
diff -r 288a83d7e289 -r 504d9373c3ff source/Lib/TLibCommon/TypeDef.h
--- a/source/Lib/TLibCommon/TypeDef.h	Sun Mar 02 18:57:46 2014 -0600
+++ b/source/Lib/TLibCommon/TypeDef.h	Mon Mar 03 11:46:33 2014 +0900
@@ -69,10 +69,9 @@
 // ====================================================================================================================
 // Enumeration
 // ====================================================================================================================
-#define MDCS_MODE                       MDCS_BOTH_DIRECTIONS        ///< Name taken from definition of MDCSMode enumeration below
 #define MDCS_ANGLE_LIMIT                                  4         ///< (default 4) 0 = Horizontal/vertical only, 1 = Horizontal/vertical +/- 1, 2 = Horizontal/vertical +/- 2 etc...
-#define MDCS_MAXIMUM_WIDTH                                8         ///< (default 8) (measured in pixels) TUs with width greater than this can only use diagonal scan
-#define MDCS_MAXIMUM_HEIGHT                               8         ///< (default 8) (measured in pixels) TUs with height greater than this can only use diagonal scan
+#define MDCS_LOG2_MAX_SIZE                                3         ///< (default 3) TUs with log2 of size greater than this can only use diagonal scan
+
 /// supported slice type
 enum SliceType
 {
@@ -91,16 +90,6 @@
     NUM_CHROMA_FORMAT = 4
 };
 
-///MDCS modes
-enum MDCSMode
-{
-    MDCS_DISABLED        = 0,
-    MDCS_HORIZONTAL_ONLY = 1,
-    MDCS_VERTICAL_ONLY   = 2,
-    MDCS_BOTH_DIRECTIONS = 3,
-    MDCS_NUMBER_OF_MODES = 4
-};
-
 #define CHROMA_H_SHIFT(x) (x == X265_CSP_I420 || x == X265_CSP_I422)
 #define CHROMA_V_SHIFT(x) (x == X265_CSP_I420)
 
@@ -187,8 +176,7 @@
     const uint32_t            *scan;
     const uint32_t            *scanCG;
     COEFF_SCAN_TYPE      scanType;
-    uint32_t             widthInGroups;
-    uint32_t             heightInGroups;
+    uint32_t             log2TrSizeCG;
     uint32_t             firstSignificanceMapContext;
 };
 
diff -r 288a83d7e289 -r 504d9373c3ff source/Lib/TLibEncoder/TEncEntropy.cpp
--- a/source/Lib/TLibEncoder/TEncEntropy.cpp	Sun Mar 02 18:57:46 2014 -0600
+++ b/source/Lib/TLibEncoder/TEncEntropy.cpp	Mon Mar 03 11:46:33 2014 +0900
@@ -365,7 +365,7 @@
         }
         if (cbfY)
         {
-            m_entropyCoderIf->codeCoeffNxN(cu, (cu->getCoeffY() + offsetLuma), absPartIdx, width, height, depth, TEXT_LUMA);
+            m_entropyCoderIf->codeCoeffNxN(cu, (cu->getCoeffY() + offsetLuma), absPartIdx, width, depth, TEXT_LUMA);
         }
 
         if ((log2TrafoSize == 2) && !(cu->getChromaFormat() == CHROMA_444))
@@ -373,13 +373,15 @@
             uint32_t partNum = cu->getPic()->getNumPartInCU() >> ((depth - 1) << 1);
             if ((absPartIdx % partNum) == (partNum - 1))
             {
+                // TODO: 4:2:2
+                assert(width == height);
                 if (cbfU)
                 {
-                    m_entropyCoderIf->codeCoeffNxN(cu, (cu->getCoeffCb() + m_bakChromaOffset), m_bakAbsPartIdx, width, height, depth, TEXT_CHROMA_U);
+                    m_entropyCoderIf->codeCoeffNxN(cu, (cu->getCoeffCb() + m_bakChromaOffset), m_bakAbsPartIdx, width, depth, TEXT_CHROMA_U);
                 }
                 if (cbfV)
                 {
-                    m_entropyCoderIf->codeCoeffNxN(cu, (cu->getCoeffCr() + m_bakChromaOffset), m_bakAbsPartIdx, width, height, depth, TEXT_CHROMA_V);
+                    m_entropyCoderIf->codeCoeffNxN(cu, (cu->getCoeffCr() + m_bakChromaOffset), m_bakAbsPartIdx, width, depth, TEXT_CHROMA_V);
                 }
             }
         }
@@ -387,13 +389,15 @@
         {
             int trWidth  = width >> cu->getHorzChromaShift();
             int trHeight = height >> cu->getVertChromaShift();
+            // TODO: 4:2:2
+            assert(trWidth == trHeight);
             if (cbfU)
             {
-                m_entropyCoderIf->codeCoeffNxN(cu, (cu->getCoeffCb() + offsetChroma), absPartIdx, trWidth, trHeight, depth, TEXT_CHROMA_U);
+                m_entropyCoderIf->codeCoeffNxN(cu, (cu->getCoeffCb() + offsetChroma), absPartIdx, trWidth, depth, TEXT_CHROMA_U);
             }
             if (cbfV)
             {
-                m_entropyCoderIf->codeCoeffNxN(cu, (cu->getCoeffCr() + offsetChroma), absPartIdx, trWidth, trHeight, depth, TEXT_CHROMA_V);
+                m_entropyCoderIf->codeCoeffNxN(cu, (cu->getCoeffCr() + offsetChroma), absPartIdx, trWidth, depth, TEXT_CHROMA_V);
             }
         }
     }
@@ -620,14 +624,16 @@
 void TEncEntropy::encodeCoeffNxN(TComDataCU* cu, TCoeff* coeff, uint32_t absPartIdx, uint32_t trWidth, uint32_t trHeight, uint32_t depth, TextType ttype)
 {
     // This is for Transform unit processing. This may be used at mode selection stage for Inter.
-    m_entropyCoderIf->codeCoeffNxN(cu, coeff, absPartIdx, trWidth, trHeight, depth, ttype);
+    // TODO: 4:2:2
+    assert(trWidth == trHeight);
+    m_entropyCoderIf->codeCoeffNxN(cu, coeff, absPartIdx, trWidth, depth, ttype);
 }
 
-void TEncEntropy::estimateBit(estBitsSbacStruct* estBitsSBac, int width, int height, TextType ttype)
+void TEncEntropy::estimateBit(estBitsSbacStruct* estBitsSBac, int trSize, TextType ttype)
 {
     ttype = ttype == TEXT_LUMA ? TEXT_LUMA : TEXT_CHROMA;
 
-    m_entropyCoderIf->estBit(estBitsSBac, width, height, ttype);
+    m_entropyCoderIf->estBit(estBitsSBac, trSize, ttype);
 }
 
 /** Encode SAO Offset
diff -r 288a83d7e289 -r 504d9373c3ff source/Lib/TLibEncoder/TEncEntropy.h
--- a/source/Lib/TLibEncoder/TEncEntropy.h	Sun Mar 02 18:57:46 2014 -0600
+++ b/source/Lib/TLibEncoder/TEncEntropy.h	Mon Mar 03 11:46:33 2014 +0900
@@ -104,13 +104,13 @@
     virtual void codeRefFrmIdx(TComDataCU* cu, uint32_t absPartIdx, int eRefList) = 0;
     virtual void codeMvd(TComDataCU* cu, uint32_t absPartIdx, int eRefList) = 0;
     virtual void codeDeltaQP(TComDataCU* cu, uint32_t absPartIdx) = 0;
-    virtual void codeCoeffNxN(TComDataCU* cu, TCoeff* pcCoef, uint32_t absPartIdx, uint32_t width, uint32_t height, uint32_t depth, TextType ttype) = 0;
+    virtual void codeCoeffNxN(TComDataCU* cu, TCoeff* pcCoef, uint32_t absPartIdx, uint32_t trSize, uint32_t depth, TextType ttype) = 0;
     virtual void codeSAOSign(uint32_t code) = 0;
     virtual void codeSaoMaxUvlc(uint32_t code, uint32_t maxSymbol) = 0;
     virtual void codeSaoMerge(uint32_t code) = 0;
     virtual void codeSaoTypeIdx(uint32_t code) = 0;
     virtual void codeSaoUflc(uint32_t length, uint32_t code) = 0;
-    virtual void estBit(estBitsSbacStruct* estBitsSbac, int width, int height, TextType ttype) = 0;
+    virtual void estBit(estBitsSbacStruct* estBitsSbac, int trSize, TextType ttype) = 0;
 
     virtual ~TEncEntropyIf() {}
 };
@@ -186,7 +186,7 @@
 
     void encodeCoeffNxN(TComDataCU* cu, TCoeff* pcCoeff, uint32_t absPartIdx, uint32_t trWidth, uint32_t trHeight, uint32_t depth, TextType ttype);
 
-    void estimateBit(estBitsSbacStruct* estBitsSbac, int width, int height, TextType ttype);
+    void estimateBit(estBitsSbacStruct* estBitsSbac, int trSize, TextType ttype);
     void encodeSaoOffset(SaoLcuParam* saoLcuParam, uint32_t compIdx);
     void encodeSaoUnitInterleaving(int compIdx, bool saoFlag, int rx, int ry, SaoLcuParam* saoLcuParam, int cuAddrInSlice, int cuAddrUpInSlice, int allowMergeLeft, int allowMergeUp);
 }; // END CLASS DEFINITION TEncEntropy
diff -r 288a83d7e289 -r 504d9373c3ff source/Lib/TLibEncoder/TEncSbac.cpp
--- a/source/Lib/TLibEncoder/TEncSbac.cpp	Sun Mar 02 18:57:46 2014 -0600
+++ b/source/Lib/TLibEncoder/TEncSbac.cpp	Mon Mar 03 11:46:33 2014 +0900
@@ -923,7 +923,7 @@
 void TEncSbac::xCodeScalingList(TComScalingList* scalingList, uint32_t sizeId, uint32_t listId)
 {
     int coefNum = X265_MIN(MAX_MATRIX_COEF_NUM, (int)g_scalingListSize[sizeId]);
-    const uint32_t* scan = g_scanOrder[SCAN_UNGROUPED][SCAN_DIAG][sizeId == 0 ? 2 : 3][sizeId == 0 ? 2 : 3];
+    const uint32_t* scan = g_scanOrder[SCAN_UNGROUPED][SCAN_DIAG][sizeId == 0 ? 2 : 3];
     int nextCoef = SCALING_LIST_START_VALUE;
     int data;
     int32_t *src = scalingList->getScalingListAddress(sizeId, listId);
@@ -1851,13 +1851,13 @@
     DTRACE_CABAC_T("\n")
 }
 
-void TEncSbac::codeTransformSkipFlags(TComDataCU* cu, uint32_t absPartIdx, uint32_t width, TextType ttype)
+void TEncSbac::codeTransformSkipFlags(TComDataCU* cu, uint32_t absPartIdx, uint32_t trSize, TextType ttype)
 {
     if (cu->getCUTransquantBypass(absPartIdx))
     {
         return;
     }
-    if (width != 4)
+    if (trSize != 4)
     {
         return;
     }
@@ -2000,7 +2000,7 @@
  * \param uiScanIdx scan type (zig-zag, hor, ver)
  * This method encodes the X and Y component within a block of the last significant coefficient.
  */
-void TEncSbac::codeLastSignificantXY(uint32_t posx, uint32_t posy, int width, int height, TextType ttype, uint32_t scanIdx)
+void TEncSbac::codeLastSignificantXY(uint32_t posx, uint32_t posy, uint32_t log2TrSize, TextType ttype, uint32_t scanIdx)
 {
     assert((ttype == TEXT_LUMA) || (ttype == TEXT_CHROMA));
     // swap
@@ -2010,37 +2010,34 @@
     }
 
     uint32_t ctxLast;
-    ContextModel *ctxX = &m_contextModels[OFF_CTX_LAST_FLAG_X + (ttype ? NUM_CTX_LAST_FLAG_XY_LUMA : 0)];
-    ContextModel *ctxY = &m_contextModels[OFF_CTX_LAST_FLAG_Y + (ttype ? NUM_CTX_LAST_FLAG_XY_LUMA : 0)];
     uint32_t groupIdxX = g_groupIdx[posx];
     uint32_t groupIdxY = g_groupIdx[posy];
 
-    int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY;
-    blkSizeOffsetX = ttype ? 0 : (g_convertToBit[width] * 3 + ((g_convertToBit[width] + 1) >> 2));
-    blkSizeOffsetY = ttype ? 0 : (g_convertToBit[height] * 3 + ((g_convertToBit[height] + 1) >> 2));
-    shiftX = ttype ? g_convertToBit[width] : ((g_convertToBit[width] + 3) >> 2);
-    shiftY = ttype ? g_convertToBit[height] : ((g_convertToBit[height] + 3) >> 2);
+    int blkSizeOffset = ttype ? NUM_CTX_LAST_FLAG_XY_LUMA : ((log2TrSize - 2) * 3 + ((log2TrSize - 1) >> 2));
+    int ctxShift = ttype ? log2TrSize - 2 : ((log2TrSize + 1) >> 2);
+    uint32_t maxGroupIdx = log2TrSize * 2 - 1;
     // posX
+    ContextModel *ctxX = &m_contextModels[OFF_CTX_LAST_FLAG_X];
     for (ctxLast = 0; ctxLast < groupIdxX; ctxLast++)
     {
-        m_binIf->encodeBin(1, *(ctxX + blkSizeOffsetX + (ctxLast >> shiftX)));
+        m_binIf->encodeBin(1, *(ctxX + blkSizeOffset + (ctxLast >> ctxShift)));
     }
-
-    if (groupIdxX < g_groupIdx[width - 1])
+    if (groupIdxX < maxGroupIdx)
     {
-        m_binIf->encodeBin(0, *(ctxX + blkSizeOffsetX + (ctxLast >> shiftX)));
+        m_binIf->encodeBin(0, *(ctxX + blkSizeOffset + (ctxLast >> ctxShift)));
     }
 
     // posY
+    ContextModel *ctxY = &m_contextModels[OFF_CTX_LAST_FLAG_Y];
     for (ctxLast = 0; ctxLast < groupIdxY; ctxLast++)
     {
-        m_binIf->encodeBin(1, *(ctxY + blkSizeOffsetY + (ctxLast >> shiftY)));
+        m_binIf->encodeBin(1, *(ctxY + blkSizeOffset + (ctxLast >> ctxShift)));
+    }
+    if (groupIdxY < maxGroupIdx)
+    {
+        m_binIf->encodeBin(0, *(ctxY + blkSizeOffset + (ctxLast >> ctxShift)));
     }
 
-    if (groupIdxY < g_groupIdx[height - 1])
-    {
-        m_binIf->encodeBin(0, *(ctxY + blkSizeOffsetY + (ctxLast >> shiftY)));
-    }
     if (groupIdxX > 3)
     {
         uint32_t count = (groupIdxX - 2) >> 1;
@@ -2055,16 +2052,16 @@
     }
 }
 
-void TEncSbac::codeCoeffNxN(TComDataCU* cu, TCoeff* coeff, uint32_t absPartIdx, uint32_t width, uint32_t height, uint32_t depth, TextType ttype)
+void TEncSbac::codeCoeffNxN(TComDataCU* cu, TCoeff* coeff, uint32_t absPartIdx, uint32_t trSize, uint32_t depth, TextType ttype)
 {
 #if ENC_DEC_TRACE
     DTRACE_CABAC_VL(g_nSymbolCounter++)
     DTRACE_CABAC_T("\tparseCoeffNxN()\teType=")
     DTRACE_CABAC_V(ttype)
     DTRACE_CABAC_T("\twidth=")
-    DTRACE_CABAC_V(width)
+    DTRACE_CABAC_V(trSize)
     DTRACE_CABAC_T("\theight=")
-    DTRACE_CABAC_V(height)
+    DTRACE_CABAC_V(trSize)
     DTRACE_CABAC_T("\tdepth=")
     DTRACE_CABAC_V(depth)
     DTRACE_CABAC_T("\tabspartidx=")
@@ -2086,10 +2083,10 @@
     (void)depth;
 #endif // if ENC_DEC_TRACE
 
-    assert(width <= m_slice->getSPS()->getMaxTrSize());
+    assert(trSize <= m_slice->getSPS()->getMaxTrSize());
 
     // compute number of significant coefficients
-    uint32_t numSig = primitives.count_nonzero(coeff, width * height);
+    uint32_t numSig = primitives.count_nonzero(coeff, trSize * trSize);
 
     if (numSig == 0)
         return;
@@ -2105,16 +2102,15 @@
     }
     if (cu->getSlice()->getPPS()->getUseTransformSkip())
     {
-        codeTransformSkipFlags(cu, absPartIdx, width, ttype);
+        codeTransformSkipFlags(cu, absPartIdx, trSize, ttype);
     }
 
     ttype = ttype == TEXT_LUMA ? TEXT_LUMA : TEXT_CHROMA;
-    const uint32_t log2BlockWidth  = g_convertToBit[width] + 2;
-    const uint32_t log2BlockHeight = g_convertToBit[height] + 2;
+    const uint32_t log2TrSize = g_convertToBit[trSize] + 2;
 
     //select scans
     TUEntropyCodingParameters codingParameters;
-    TComTrQuant::getTUEntropyCodingParameters(cu, codingParameters, absPartIdx,  width, height, ttype);
+    TComTrQuant::getTUEntropyCodingParameters(cu, codingParameters, absPartIdx, log2TrSize, ttype);
 
     //----- encode significance map -----
 
@@ -2122,16 +2118,17 @@
     int scanPosLast = -1;
     int posLast;
     uint32_t sigCoeffGroupFlag[MLS_GRP_NUM];
-    memset(sigCoeffGroupFlag, 0, sizeof(uint32_t) * MLS_GRP_NUM);
+    uint32_t cgNum = 1 << codingParameters.log2TrSizeCG * 2;
+    memset(sigCoeffGroupFlag, 0, sizeof(uint32_t) * cgNum);
     do
     {
         posLast = codingParameters.scan[++scanPosLast];
         if (coeff[posLast] != 0)
         {
             // get L1 sig map
-            uint32_t posy   = posLast >> log2BlockWidth;
-            uint32_t posx   = posLast - (posy << log2BlockWidth);
-            uint32_t blkIdx = codingParameters.widthInGroups * (posy >> MLS_CG_LOG2_HEIGHT) + (posx >> MLS_CG_LOG2_WIDTH);
+            uint32_t posy   = posLast >> log2TrSize;
+            uint32_t posx   = posLast - (posy << log2TrSize);
+            uint32_t blkIdx = ((posy >> MLS_CG_LOG2_SIZE) << codingParameters.log2TrSizeCG) + (posx >> MLS_CG_LOG2_SIZE);
             sigCoeffGroupFlag[blkIdx] = 1;
 
             numSig--;
@@ -2140,9 +2137,9 @@
     while (numSig > 0);
 
     // Code position of last coefficient
-    int posLastY = posLast >> log2BlockWidth;
-    int posLastX = posLast - (posLastY << log2BlockWidth);
-    codeLastSignificantXY(posLastX, posLastY, width, height, ttype, codingParameters.scanType);
+    int posLastY = posLast >> log2TrSize;
+    int posLastX = posLast - (posLastY << log2TrSize);
+    codeLastSignificantXY(posLastX, posLastY, log2TrSize, ttype, codingParameters.scanType);
     //===== code significance flag =====
     ContextModel * const baseCoeffGroupCtx = &m_contextModels[OFF_SIG_CG_FLAG_CTX + (ttype ? NUM_SIG_CG_FLAG_CTX : 0)];
     ContextModel * const baseCtx = (ttype == TEXT_LUMA) ? &m_contextModels[OFF_SIG_FLAG_CTX] : &m_contextModels[OFF_SIG_FLAG_CTX + NUM_SIG_FLAG_CTX_LUMA];
@@ -2171,8 +2168,8 @@
         }
         // encode significant_coeffgroup_flag
         int cgBlkPos = codingParameters.scanCG[subSet];
-        int cgPosY   = cgBlkPos / codingParameters.widthInGroups;
-        int cgPosX   = cgBlkPos - (cgPosY * codingParameters.widthInGroups);
+        int cgPosY   = cgBlkPos >> codingParameters.log2TrSizeCG;
+        int cgPosX   = cgBlkPos - (cgPosY << codingParameters.log2TrSizeCG);
 
         if (subSet == lastScanSet || subSet == 0)
         {
@@ -2181,13 +2178,13 @@
         else
         {
             uint32_t sigCoeffGroup = (sigCoeffGroupFlag[cgBlkPos] != 0);
-            uint32_t ctxSig = TComTrQuant::getSigCoeffGroupCtxInc(sigCoeffGroupFlag, cgPosX, cgPosY, codingParameters.widthInGroups, codingParameters.heightInGroups);
+            uint32_t ctxSig = TComTrQuant::getSigCoeffGroupCtxInc(sigCoeffGroupFlag, cgPosX, cgPosY, codingParameters.log2TrSizeCG);
             m_binIf->encodeBin(sigCoeffGroup, baseCoeffGroupCtx[ctxSig]);
         }
         // encode significant_coeff_flag
         if (sigCoeffGroupFlag[cgBlkPos])
         {
-            const int patternSigCtx = TComTrQuant::calcPatternSigCtx(sigCoeffGroupFlag, cgPosX, cgPosY, codingParameters.widthInGroups, codingParameters.heightInGroups);
+            const int patternSigCtx = TComTrQuant::calcPatternSigCtx(sigCoeffGroupFlag, cgPosX, cgPosY, codingParameters.log2TrSizeCG);
             uint32_t blkPos, sig, ctxSig;
             for (; scanPosSig >= subPos; scanPosSig--)
             {
@@ -2195,7 +2192,7 @@
                 sig     = (coeff[blkPos] != 0);
                 if (scanPosSig > subPos || subSet == 0 || numNonZero)
                 {
-                    ctxSig  = TComTrQuant::getSigCtxInc(patternSigCtx, codingParameters, scanPosSig, log2BlockWidth, log2BlockHeight, ttype);
+                    ctxSig  = TComTrQuant::getSigCtxInc(patternSigCtx, codingParameters, scanPosSig, log2TrSize, ttype);
                     m_binIf->encodeBin(sig, baseCtx[ctxSig]);
                 }
                 if (sig)
@@ -2348,14 +2345,14 @@
  *   estimate bit cost for CBP, significant map and significant coefficients
  ****************************************************************************
  */
-void TEncSbac::estBit(estBitsSbacStruct* estBitsSbac, int width, int height, TextType ttype)
+void TEncSbac::estBit(estBitsSbacStruct* estBitsSbac, int trSize, TextType ttype)
 {
     estCBFBit(estBitsSbac);
 
     estSignificantCoeffGroupMapBit(estBitsSbac, ttype);
 
     // encode significance map
-    estSignificantMapBit(estBitsSbac, width, height, ttype);
+    estSignificantMapBit(estBitsSbac, trSize, ttype);
 
     // encode significant coefficients
     estSignificantCoefficientsBit(estBitsSbac, ttype);
@@ -2412,16 +2409,16 @@
  *    estimate SAMBAC bit cost for significant coefficient map
  ****************************************************************************
  */
-void TEncSbac::estSignificantMapBit(estBitsSbacStruct* estBitsSbac, int width, int height, TextType ttype)
+void TEncSbac::estSignificantMapBit(estBitsSbacStruct* estBitsSbac, int trSize, TextType ttype)
 {
     int firstCtx = 1, numCtx = 8;
 
-    if (width >= 16)
+    if (trSize >= 16)
     {
         firstCtx = (ttype == TEXT_LUMA) ? 21 : 12;
         numCtx = (ttype == TEXT_LUMA) ? 6 : 3;
     }
-    else if (width == 8)
+    else if (trSize == 8)
     {
         firstCtx = 9;
         numCtx = (ttype == TEXT_LUMA) ? 12 : 3;
@@ -2458,30 +2455,30 @@
         }
     }
     int bitsX = 0, bitsY = 0;
-    int blkSizeOffsetX, blkSizeOffsetY, shiftX, shiftY;
 
-    blkSizeOffsetX = ttype ? 0 : (g_convertToBit[width] * 3 + ((g_convertToBit[width] + 1) >> 2));
-    blkSizeOffsetY = ttype ? 0 : (g_convertToBit[height] * 3 + ((g_convertToBit[height] + 1) >> 2));
-    shiftX = ttype ? g_convertToBit[width] : ((g_convertToBit[width] + 3) >> 2);
-    shiftY = ttype ? g_convertToBit[height] : ((g_convertToBit[height] + 3) >> 2);
+    uint32_t log2TrSize = g_convertToBit[trSize] + 2;
+    int blkSizeOffset = ttype ? NUM_CTX_LAST_FLAG_XY_LUMA : ((log2TrSize - 2) * 3 + ((log2TrSize - 1) >> 2));
+    int ctxShift = ttype ? log2TrSize - 2 : ((log2TrSize + 1) >> 2);
+    uint32_t maxGroupIdx = log2TrSize * 2 - 1;
 
     assert((ttype == TEXT_LUMA) || (ttype == TEXT_CHROMA));
     int ctx;
-    for (ctx = 0; ctx < g_groupIdx[width - 1]; ctx++)
+    const ContextModel *ctxX = &m_contextModels[OFF_CTX_LAST_FLAG_X];
+    for (ctx = 0; ctx < maxGroupIdx; ctx++)
     {
-        int ctxOffset = blkSizeOffsetX + (ctx >> shiftX);
-        estBitsSbac->lastXBits[ctx] = bitsX + sbacGetEntropyBits(m_contextModels[OFF_CTX_LAST_FLAG_X + ((ttype ? NUM_CTX_LAST_FLAG_XY_LUMA : 0) + ctxOffset)].m_state, 0);
-        bitsX += sbacGetEntropyBits(m_contextModels[OFF_CTX_LAST_FLAG_X + ((ttype ? NUM_CTX_LAST_FLAG_XY_LUMA : 0) + ctxOffset)].m_state, 1);
+        int ctxOffset = blkSizeOffset + (ctx >> ctxShift);
+        estBitsSbac->lastXBits[ctx] = bitsX + sbacGetEntropyBits(ctxX[ctxOffset].m_state, 0);
+        bitsX += sbacGetEntropyBits(ctxX[ctxOffset].m_state, 1);
     }
+    estBitsSbac->lastXBits[ctx] = bitsX;
 
-    estBitsSbac->lastXBits[ctx] = bitsX;
-    for (ctx = 0; ctx < g_groupIdx[height - 1]; ctx++)
+    const ContextModel *ctxY = &m_contextModels[OFF_CTX_LAST_FLAG_Y];
+    for (ctx = 0; ctx < maxGroupIdx; ctx++)
     {
-        int ctxOffset = blkSizeOffsetY + (ctx >> shiftY);
-        estBitsSbac->lastYBits[ctx] = bitsY + sbacGetEntropyBits(m_contextModels[OFF_CTX_LAST_FLAG_Y + ((ttype ? NUM_CTX_LAST_FLAG_XY_LUMA : 0) + ctxOffset)].m_state, 0);
-        bitsY += sbacGetEntropyBits(m_contextModels[OFF_CTX_LAST_FLAG_Y + ((ttype ? NUM_CTX_LAST_FLAG_XY_LUMA : 0) + ctxOffset)].m_state, 1);
+        int ctxOffset = blkSizeOffset + (ctx >> ctxShift);
+        estBitsSbac->lastYBits[ctx] = bitsY + sbacGetEntropyBits(ctxY[ctxOffset].m_state, 0);
+        bitsY += sbacGetEntropyBits(ctxY[ctxOffset].m_state, 1);
     }
-
     estBitsSbac->lastYBits[ctx] = bitsY;
 }
 
diff -r 288a83d7e289 -r 504d9373c3ff source/Lib/TLibEncoder/TEncSbac.h
--- a/source/Lib/TLibEncoder/TEncSbac.h	Sun Mar 02 18:57:46 2014 -0600
+++ b/source/Lib/TLibEncoder/TEncSbac.h	Mon Mar 03 11:46:33 2014 +0900
@@ -127,18 +127,18 @@
 
     void codeDeltaQP(TComDataCU* cu, uint32_t absPartIdx);
 
-    void codeLastSignificantXY(uint32_t posx, uint32_t posy, int width, int height, TextType ttype, uint32_t scanIdx);
-    void codeCoeffNxN(TComDataCU* cu, TCoeff* coef, uint32_t absPartIdx, uint32_t width, uint32_t height, uint32_t depth, TextType ttype);
-    void codeTransformSkipFlags(TComDataCU* cu, uint32_t absPartIdx, uint32_t width, TextType ttype);
+    void codeLastSignificantXY(uint32_t posx, uint32_t posy, uint32_t log2TrSize, TextType ttype, uint32_t scanIdx);
+    void codeCoeffNxN(TComDataCU* cu, TCoeff* coef, uint32_t absPartIdx, uint32_t trSize, uint32_t depth, TextType ttype);
+    void codeTransformSkipFlags(TComDataCU* cu, uint32_t absPartIdx, uint32_t trSize, TextType ttype);
 
     // -------------------------------------------------------------------------------------------------------------------
     // for RD-optimizatioon
     // -------------------------------------------------------------------------------------------------------------------
 
-    void estBit(estBitsSbacStruct* estBitsSbac, int width, int height, TextType ttype);
+    void estBit(estBitsSbacStruct* estBitsSbac, int trSize, TextType ttype);
     void estCBFBit(estBitsSbacStruct* estBitsSbac);
     void estSignificantCoeffGroupMapBit(estBitsSbacStruct* estBitsSbac, TextType ttype);
-    void estSignificantMapBit(estBitsSbacStruct* estBitsSbac, int width, int height, TextType ttype);
+    void estSignificantMapBit(estBitsSbacStruct* estBitsSbac, int trSize, TextType ttype);
     void estSignificantCoefficientsBit(estBitsSbacStruct* estBitsSbac, TextType ttype);
 
     TEncBinCABAC* getEncBinIf()  { return m_binIf; }
diff -r 288a83d7e289 -r 504d9373c3ff source/Lib/TLibEncoder/TEncSearch.cpp
--- a/source/Lib/TLibEncoder/TEncSearch.cpp	Sun Mar 02 18:57:46 2014 -0600
+++ b/source/Lib/TLibEncoder/TEncSearch.cpp	Mon Mar 03 11:46:33 2014 +0900
@@ -460,7 +460,7 @@
     //--- init rate estimation arrays for RDOQ ---
     if (useTransformSkip ? m_cfg->bEnableRDOQTS : m_cfg->bEnableRDOQ)
     {
-        m_entropyCoder->estimateBit(m_trQuant->m_estBitsSbac, width, width, TEXT_LUMA);
+        m_entropyCoder->estimateBit(m_trQuant->m_estBitsSbac, width, TEXT_LUMA);
     }
 
     //--- transform and quantization ---
@@ -588,7 +588,7 @@
         //--- init rate estimation arrays for RDOQ ---
         if (useTransformSkipChroma ? m_cfg->bEnableRDOQTS : m_cfg->bEnableRDOQ)
         {
-            m_entropyCoder->estimateBit(m_trQuant->m_estBitsSbac, width, width, ttype);
+            m_entropyCoder->estimateBit(m_trQuant->m_estBitsSbac, width, ttype);
         }
         //--- transform and quantization ---
         uint32_t absSum = 0;
@@ -3482,7 +3482,7 @@
 
         if (m_cfg->bEnableRDOQ && curuseRDOQ)
         {
-            m_entropyCoder->estimateBit(m_trQuant->m_estBitsSbac, trWidth, trHeight, TEXT_LUMA);
+            m_entropyCoder->estimateBit(m_trQuant->m_estBitsSbac, trWidth, TEXT_LUMA);
         }
 
         m_trQuant->setQPforQuant(cu->getQP(0), TEXT_LUMA, cu->getSlice()->getSPS()->getQpBDOffsetY(), 0, chFmt);
@@ -3497,7 +3497,7 @@
         {
             if (m_cfg->bEnableRDOQ && curuseRDOQ)
             {
-                m_entropyCoder->estimateBit(m_trQuant->m_estBitsSbac, trWidthC, trHeightC, TEXT_CHROMA);
+                m_entropyCoder->estimateBit(m_trQuant->m_estBitsSbac, trWidthC, TEXT_CHROMA);
             }
             //Cb transform
             int curChromaQpOffset = cu->getSlice()->getPPS()->getChromaCbQpOffset() + cu->getSlice()->getSliceQpDeltaCb();
@@ -3773,7 +3773,7 @@
 
             if (m_cfg->bEnableRDOQTS)
             {
-                m_entropyCoder->estimateBit(m_trQuant->m_estBitsSbac, trWidth, trHeight, TEXT_LUMA);
+                m_entropyCoder->estimateBit(m_trQuant->m_estBitsSbac, trWidth, TEXT_LUMA);
             }
 
             m_trQuant->setQPforQuant(cu->getQP(0), TEXT_LUMA, cu->getSlice()->getSPS()->getQpBDOffsetY(), 0, chFmt);
@@ -3852,7 +3852,7 @@
 
             if (m_cfg->bEnableRDOQTS)
             {
-                m_entropyCoder->estimateBit(m_trQuant->m_estBitsSbac, trWidthC, trHeightC, TEXT_CHROMA);
+                m_entropyCoder->estimateBit(m_trQuant->m_estBitsSbac, trWidthC, TEXT_CHROMA);
             }
 
             int curChromaQpOffset = cu->getSlice()->getPPS()->getChromaCbQpOffset() + cu->getSlice()->getSliceQpDeltaCb();


More information about the x265-devel mailing list