[x265] [PATCH 4 of 4] cabac: cleanup and convert class ContextModel to struct

Min Chen chenm003 at 163.com
Wed Oct 23 14:10:38 CEST 2013


# HG changeset patch
# User Min Chen <chenm003 at 163.com>
# Date 1382530173 -28800
# Node ID adcbd22c15029679e82f36bf9a6c253f46530db8
# Parent  f5f71d7f3bf383f2ac0545a5fecd4607a0a43f08
cabac: cleanup and convert class ContextModel to struct

diff -r f5f71d7f3bf3 -r adcbd22c1502 source/Lib/TLibCommon/ContextModel.cpp
--- a/source/Lib/TLibCommon/ContextModel.cpp	Wed Oct 23 20:08:55 2013 +0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,117 +0,0 @@
-/* The copyright in this software is being made available under the BSD
- * License, included below. This software may be subject to other third party
- * and contributor rights, including patent rights, and no such rights are
- * granted under this license.
- *
- * Copyright (c) 2010-2013, ITU/ISO/IEC
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *  * Redistributions of source code must retain the above copyright notice,
- *    this list of conditions and the following disclaimer.
- *  * Redistributions in binary form must reproduce the above copyright notice,
- *    this list of conditions and the following disclaimer in the documentation
- *    and/or other materials provided with the distribution.
- *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
- *    be used to endorse or promote products derived from this software without
- *    specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** \file     ContextModel.cpp
-    \brief    context model class
-*/
-
-#include "ContextModel.h"
-#include "TComRom.h"
-#include "common.h"
-
-using namespace x265;
-
-//! \ingroup TLibCommon
-//! \{
-
-// ====================================================================================================================
-// Public member functions
-// ====================================================================================================================
-
-/**
- - initialize context model with respect to QP and initialization value
- .
- \param  qp         input QP value
- \param  initValue  8 bit initialization value
- */
-void ContextModel::init(int qp, int initValue)
-{
-    qp = Clip3(0, 51, qp);
-
-    int  slope      = (initValue >> 4) * 5 - 45;
-    int  offset     = ((initValue & 15) << 3) - 16;
-    int  initState  =  X265_MIN(X265_MAX(1, (((slope * qp) >> 4) + offset)), 126);
-    UInt mpState    = (initState >= 64);
-    m_state       = ((mpState ? (initState - 64) : (63 - initState)) << 1) + mpState;
-}
-
-const UChar g_nextStateMPS[128] =
-{
-    2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
-    18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
-    34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
-    50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
-    66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
-    82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
-    98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
-    114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 124, 125, 126, 127
-};
-
-const UChar g_nextStateLPS[128] =
-{
-    1, 0, 0, 1, 2, 3, 4, 5, 4, 5, 8, 9, 8, 9, 10, 11,
-    12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 22, 23, 22, 23, 24, 25,
-    26, 27, 26, 27, 30, 31, 30, 31, 32, 33, 32, 33, 36, 37, 36, 37,
-    38, 39, 38, 39, 42, 43, 42, 43, 44, 45, 44, 45, 46, 47, 48, 49,
-    48, 49, 50, 51, 52, 53, 52, 53, 54, 55, 54, 55, 56, 57, 58, 59,
-    58, 59, 60, 61, 60, 61, 60, 61, 62, 63, 64, 65, 64, 65, 66, 67,
-    66, 67, 66, 67, 68, 69, 68, 69, 70, 71, 70, 71, 70, 71, 72, 73,
-    72, 73, 72, 73, 74, 75, 74, 75, 74, 75, 76, 77, 76, 77, 126, 127
-};
-
-UChar g_nextState[128][2];
-
-void ContextModel::buildNextStateTable()
-{
-    for (int i = 0; i < 128; i++)
-    {
-        for (int j = 0; j < 2; j++)
-        {
-            g_nextState[i][j] = ((i & 1) == j) ? g_nextStateMPS[i] : g_nextStateLPS[i];
-        }
-    }
-}
-
-const int 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,
-    0x04a87, 0x0cbf7, 0x045d6, 0x0d5c3, 0x04144, 0x0e01b, 0x03d88, 0x0e937, 0x039e0, 0x0f2cd, 0x03663, 0x0fc9e, 0x03347, 0x10600, 0x03050, 0x10f95,
-    0x02d4d, 0x11a02, 0x02ad3, 0x12333, 0x0286e, 0x12cad, 0x02604, 0x136df, 0x02425, 0x13f48, 0x021f4, 0x149c4, 0x0203e, 0x1527b, 0x01e4d, 0x15d00,
-    0x01c99, 0x166de, 0x01b18, 0x17017, 0x019a5, 0x17988, 0x01841, 0x18327, 0x016df, 0x18d50, 0x015d9, 0x19547, 0x0147c, 0x1a083, 0x0138e, 0x1a8a3,
-    0x01251, 0x1b418, 0x01166, 0x1bd27, 0x01068, 0x1c77b, 0x00f7f, 0x1d18e, 0x00eda, 0x1d91a, 0x00e19, 0x1e254, 0x00d4f, 0x1ec9a, 0x00c90, 0x1f6e0,
-    0x00c01, 0x1fef8, 0x00b5f, 0x208b1, 0x00ab6, 0x21362, 0x00a15, 0x21e46, 0x00988, 0x2285d, 0x00934, 0x22ea8, 0x008a8, 0x239b2, 0x0081d, 0x24577,
-    0x007c9, 0x24ce6, 0x00763, 0x25663, 0x00710, 0x25e8f, 0x006a0, 0x26a26, 0x00672, 0x26f23, 0x005e8, 0x27ef8, 0x005ba, 0x284b5, 0x0055e, 0x29057,
-    0x0050c, 0x29bab, 0x004c1, 0x2a674, 0x004a7, 0x2aa5e, 0x0046f, 0x2b32f, 0x0041f, 0x2c0ad, 0x003e7, 0x2ca8d, 0x003ba, 0x2d323, 0x0010c, 0x3bfbb
-};
-//! \}
diff -r f5f71d7f3bf3 -r adcbd22c1502 source/Lib/TLibCommon/ContextModel.h
--- a/source/Lib/TLibCommon/ContextModel.h	Wed Oct 23 20:08:55 2013 +0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,108 +0,0 @@
-/* The copyright in this software is being made available under the BSD
- * License, included below. This software may be subject to other third party
- * and contributor rights, including patent rights, and no such rights are
- * granted under this license.
- *
- * Copyright (c) 2010-2013, ITU/ISO/IEC
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *  * Redistributions of source code must retain the above copyright notice,
- *    this list of conditions and the following disclaimer.
- *  * Redistributions in binary form must reproduce the above copyright notice,
- *    this list of conditions and the following disclaimer in the documentation
- *    and/or other materials provided with the distribution.
- *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
- *    be used to endorse or promote products derived from this software without
- *    specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/** \file     ContextModel.h
-    \brief    context model class (header)
-*/
-
-#ifndef X265_CONTEXTMODEL_H
-#define X265_CONTEXTMODEL_H
-
-#include "CommonDef.h"
-
-//! \ingroup TLibCommon
-//! \{
-
-using namespace x265;
-
-extern const UChar g_nextStateMPS[128];
-extern const UChar g_nextStateLPS[128];
-extern const int   g_entropyBits[128];
-extern       UChar g_nextState[128][2];
-
-namespace x265 {
-// private namespace
-
-// ====================================================================================================================
-// Class definition
-// ====================================================================================================================
-
-/// context model class
-class ContextModel
-{
-public:
-
-    ContextModel()   { m_state = 0; bBinsCoded = 0; }
-
-    ~ContextModel()  {}
-
-    UChar getState() { return m_state >> 1; } ///< get current state
-
-    UChar getMps()   { return m_state  & 1; } ///< get curret MPS
-
-    void init(int qp, int initValue);   ///< initialize state with initial probability
-
-    void updateLPS()
-    {
-        m_state = g_nextStateLPS[m_state];
-    }
-
-    void updateMPS()
-    {
-        m_state = g_nextStateMPS[m_state];
-    }
-
-    int getEntropyBits(UInt val) { return g_entropyBits[m_state ^ val]; }
-    int getEntropyBits(UInt state, UInt val) { return g_entropyBits[state ^ val]; }
-
-    void update(int binVal)
-    {
-        m_state = g_nextState[m_state][binVal];
-    }
-
-    static void buildNextStateTable();
-    static int getEntropyBitsTrm(int val) { return g_entropyBits[126 ^ val]; }
-
-    void setBinsCoded(UInt val)   { bBinsCoded = (UChar)val;  }
-
-    UInt getBinsCoded()           { return bBinsCoded;   }
-
-public:
-
-    UChar         m_state;  ///< internal state variable
-    UChar         bBinsCoded;
-};
-}
-//! \}
-
-#endif // ifndef X265_CONTEXTMODEL_H
diff -r f5f71d7f3bf3 -r adcbd22c1502 source/Lib/TLibCommon/ContextTables.h
--- a/source/Lib/TLibCommon/ContextTables.h	Wed Oct 23 20:08:55 2013 +0800
+++ b/source/Lib/TLibCommon/ContextTables.h	Wed Oct 23 20:09:33 2013 +0800
@@ -39,6 +39,8 @@
 #ifndef X265_CONTEXTTABLES_H
 #define X265_CONTEXTTABLES_H
 
+#include <stdint.h>
+
 //! \ingroup TLibCommon
 //! \{
 
@@ -125,6 +127,29 @@
 #define MAX_OFF_CTX_MOD                     (OFF_CU_TRANSQUANT_BYPASS_FLAG_CTX + NUM_CU_TRANSQUANT_BYPASS_FLAG_CTX)
 
 // ====================================================================================================================
+// Sbac interface
+// ====================================================================================================================
+typedef struct ContextModel {
+    uint8_t m_state;  ///< internal state variable
+    uint8_t bBinsCoded;
+} ContextModel;
+
+extern const uint8_t g_nextStateMPS[128];
+extern const uint8_t g_nextStateLPS[128];
+extern const int     g_entropyBits[128];
+extern       uint8_t g_nextState[128][2];
+extern void buildNextStateTable();
+extern uint8_t sbacInit(int qp, int initValue);   ///< initialize state with initial probability
+
+#define sbacGetMps(S)               ((S) & 1)
+#define sbacGetState(S)             ((S) >> 1)
+#define sbacNextLPS(S)              (g_nextStateLPS[(S)])
+#define sbacNextMPS(S)              (g_nextStateMPS[(S)])
+#define sbacNext(S, V)              (g_nextState[(S)][(V)])
+#define sbacGetEntropyBits(S, V)    (g_entropyBits[(S) ^ (V)])
+#define sbacGetEntropyBitsTrm(V)    (g_entropyBits[126 ^ (V)])
+
+// ====================================================================================================================
 // Tables
 // ====================================================================================================================
 
@@ -132,7 +157,7 @@
 // private namespace
 
 // initial probability for cu_transquant_bypass flag
-static const UChar
+static const uint8_t
     INIT_CU_TRANSQUANT_BYPASS_FLAG[3][NUM_CU_TRANSQUANT_BYPASS_FLAG_CTX] =
 {
     { 154 },
@@ -141,7 +166,7 @@
 };
 
 // initial probability for split flag
-static const UChar
+static const uint8_t
     INIT_SPLIT_FLAG[3][NUM_SPLIT_FLAG_CTX] =
 {
     { 107,  139,  126, },
@@ -149,7 +174,7 @@
     { 139,  141,  157, },
 };
 
-static const UChar
+static const uint8_t
     INIT_SKIP_FLAG[3][NUM_SKIP_FLAG_CTX] =
 {
     { 197,  185,  201, },
@@ -157,7 +182,7 @@
     { CNU,  CNU,  CNU, },
 };
 
-static const UChar
+static const uint8_t
     INIT_MERGE_FLAG_EXT[3][NUM_MERGE_FLAG_EXT_CTX] =
 {
     { 154, },
@@ -165,7 +190,7 @@
     { CNU, },
 };
 
-static const UChar
+static const uint8_t
     INIT_MERGE_IDX_EXT[3][NUM_MERGE_IDX_EXT_CTX] =
 {
     { 137, },
@@ -173,7 +198,7 @@
     { CNU, },
 };
 
-static const UChar
+static const uint8_t
     INIT_PART_SIZE[3][NUM_PART_SIZE_CTX] =
 {
     { 154,  139,  CNU,  CNU, },
@@ -181,7 +206,7 @@
     { 184,  CNU,  CNU,  CNU, },
 };
 
-static const UChar
+static const uint8_t
     INIT_CU_AMP_POS[3][NUM_CU_AMP_CTX] =
 {
     { 154, },
@@ -189,7 +214,7 @@
     { CNU, },
 };
 
-static const UChar
+static const uint8_t
     INIT_PRED_MODE[3][NUM_PRED_MODE_CTX] =
 {
     { 134, },
@@ -197,7 +222,7 @@
     { CNU, },
 };
 
-static const UChar
+static const uint8_t
     INIT_INTRA_PRED_MODE[3][NUM_ADI_CTX] =
 {
     { 183, },
@@ -205,7 +230,7 @@
     { 184, },
 };
 
-static const UChar
+static const uint8_t
     INIT_CHROMA_PRED_MODE[3][NUM_CHROMA_PRED_CTX] =
 {
     { 152,  139, },
@@ -213,7 +238,7 @@
     {  63,  139, },
 };
 
-static const UChar
+static const uint8_t
     INIT_INTER_DIR[3][NUM_INTER_DIR_CTX] =
 {
     {  95,   79,   63,   31,  31, },
@@ -221,7 +246,7 @@
     { CNU,  CNU,  CNU,  CNU, CNU, },
 };
 
-static const UChar
+static const uint8_t
     INIT_MVD[3][NUM_MV_RES_CTX] =
 {
     { 169,  198, },
@@ -229,7 +254,7 @@
     { CNU,  CNU, },
 };
 
-static const UChar
+static const uint8_t
     INIT_REF_PIC[3][NUM_REF_NO_CTX] =
 {
     { 153,  153 },
@@ -237,7 +262,7 @@
     { CNU,  CNU },
 };
 
-static const UChar
+static const uint8_t
     INIT_DQP[3][NUM_DELTA_QP_CTX] =
 {
     { 154,  154,  154, },
@@ -245,7 +270,7 @@
     { 154,  154,  154, },
 };
 
-static const UChar
+static const uint8_t
     INIT_QT_CBF[3][2 * NUM_QT_CBF_CTX] =
 {
     { 153,  111,  CNU,  CNU,  CNU,  149,   92,  167,  CNU,  CNU, },
@@ -253,7 +278,7 @@
     { 111,  141,  CNU,  CNU,  CNU,   94,  138,  182,  CNU,  CNU, },
 };
 
-static const UChar
+static const uint8_t
     INIT_QT_ROOT_CBF[3][NUM_QT_ROOT_CBF_CTX] =
 {
     {  79, },
@@ -261,7 +286,7 @@
     { CNU, },
 };
 
-static const UChar
+static const uint8_t
     INIT_LAST[3][2 * NUM_CTX_LAST_FLAG_XY] =
 {
     { 125,  110,  124,  110,   95,   94,  125,  111,  111,   79,  125,  126,  111,  111,   79,
@@ -272,7 +297,7 @@
       108,  123,   63,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU,  CNU, },
 };
 
-static const UChar
+static const uint8_t
     INIT_SIG_CG_FLAG[3][2 * NUM_SIG_CG_FLAG_CTX] =
 {
     { 121,  140,
@@ -283,7 +308,7 @@
        134,  141, },
 };
 
-static const UChar
+static const uint8_t
     INIT_SIG_FLAG[3][NUM_SIG_FLAG_CTX] =
 {
     { 170,  154,  139,  153,  139,  123,  123,   63,  124,  166,  183,  140,  136,  153,  154,  166,  183,  140,  136,  153,  154,  166,  183,  140,  136,  153,  154,  170,  153,  138,  138,  122,  121,  122,  121,  167,  151,  183,  140,  151,  183,  140,  },
@@ -291,7 +316,7 @@
     { 111,  111,  125,  110,  110,   94,  124,  108,  124,  107,  125,  141,  179,  153,  125,  107,  125,  141,  179,  153,  125,  107,  125,  141,  179,  153,  125,  140,  139,  182,  182,  152,  136,  152,  136,  153,  136,  139,  111,  136,  139,  111,  },
 };
 
-static const UChar
+static const uint8_t
     INIT_ONE_FLAG[3][NUM_ONE_FLAG_CTX] =
 {
     { 154,  196,  167,  167,  154,  152,  167,  182,  182,  134,  149,  136,  153,  121,  136,  122,  169,  208,  166,  167,  154,  152,  167,  182, },
@@ -299,7 +324,7 @@
     { 140,   92,  137,  138,  140,  152,  138,  139,  153,   74,  149,   92,  139,  107,  122,  152,  140,  179,  166,  182,  140,  227,  122,  197, },
 };
 
-static const UChar
+static const uint8_t
     INIT_ABS_FLAG[3][NUM_ABS_FLAG_CTX] =
 {
     { 107,  167,   91,  107,  107,  167, },
@@ -307,7 +332,7 @@
     { 138,  153,  136,  167,  152,  152, },
 };
 
-static const UChar
+static const uint8_t
     INIT_MVP_IDX[3][NUM_MVP_IDX_CTX] =
 {
     { 168,  CNU, },
@@ -315,7 +340,7 @@
     { CNU,  CNU, },
 };
 
-static const UChar
+static const uint8_t
     INIT_SAO_MERGE_FLAG[3][NUM_SAO_MERGE_FLAG_CTX] =
 {
     { 153,  },
@@ -323,7 +348,7 @@
     { 153,  },
 };
 
-static const UChar
+static const uint8_t
     INIT_SAO_TYPE_IDX[3][NUM_SAO_TYPE_IDX_CTX] =
 {
     { 160, },
@@ -331,7 +356,7 @@
     { 200, },
 };
 
-static const UChar
+static const uint8_t
     INIT_TRANS_SUBDIV_FLAG[3][NUM_TRANS_SUBDIV_FLAG_CTX] =
 {
     { 224,  167,  122, },
@@ -339,7 +364,7 @@
     { 153,  138,  138, },
 };
 
-static const UChar
+static const uint8_t
     INIT_TRANSFORMSKIP_FLAG[3][2 * NUM_TRANSFORMSKIP_FLAG_CTX] =
 {
     { 139,  139 },
diff -r f5f71d7f3bf3 -r adcbd22c1502 source/Lib/TLibEncoder/TEncBinCoderCABAC.cpp
--- a/source/Lib/TLibEncoder/TEncBinCoderCABAC.cpp	Wed Oct 23 20:08:55 2013 +0800
+++ b/source/Lib/TLibEncoder/TEncBinCoderCABAC.cpp	Wed Oct 23 20:09:33 2013 +0800
@@ -184,18 +184,17 @@
     }
 
     UInt mstate = ctxModel.m_state;
-    ctxModel.update(binValue);
+    ctxModel.m_state = sbacNext(ctxModel.m_state, binValue);
 
     if (bIsCounter)
     {
-        m_fracBits += ctxModel.getEntropyBits(mstate, binValue);
+        m_fracBits += sbacGetEntropyBits(mstate, binValue);
         return;
     }
-    ctxModel.setBinsCoded(1);
+    ctxModel.bBinsCoded = 1;
 
-    // TODO: Sync encode proto of cabac status
-    UInt mps = mstate & 1;
-    UInt state = mstate >> 1;
+    UInt mps = sbacGetMps(mstate);
+    UInt state = sbacGetState(mstate);
     UInt lps = g_lpsTable[state][(m_range >> 6) & 3];
     m_range -= lps;
 
@@ -298,7 +297,7 @@
 {
     if (bIsCounter)
     {
-        m_fracBits += ContextModel::getEntropyBitsTrm(binValue);
+        m_fracBits += sbacGetEntropyBitsTrm(binValue);
         return;
     }
 
diff -r f5f71d7f3bf3 -r adcbd22c1502 source/Lib/TLibEncoder/TEncBinCoderCABAC.h
--- a/source/Lib/TLibEncoder/TEncBinCoderCABAC.h	Wed Oct 23 20:08:55 2013 +0800
+++ b/source/Lib/TLibEncoder/TEncBinCoderCABAC.h	Wed Oct 23 20:09:33 2013 +0800
@@ -38,7 +38,7 @@
 #ifndef X265_TENCBINCODERCABAC_H
 #define X265_TENCBINCODERCABAC_H
 
-#include "TLibCommon/ContextModel.h"
+#include "TLibCommon/ContextTables.h"
 #include "TLibCommon/TComBitStream.h"
 
 //! \ingroup TLibEncoder
diff -r f5f71d7f3bf3 -r adcbd22c1502 source/Lib/TLibEncoder/TEncEntropy.h
--- a/source/Lib/TLibEncoder/TEncEntropy.h	Wed Oct 23 20:08:55 2013 +0800
+++ b/source/Lib/TLibEncoder/TEncEntropy.h	Wed Oct 23 20:09:33 2013 +0800
@@ -41,7 +41,6 @@
 #include "TLibCommon/TComSlice.h"
 #include "TLibCommon/TComDataCU.h"
 #include "TLibCommon/TComBitStream.h"
-#include "TLibCommon/ContextModel.h"
 #include "TLibCommon/TComPic.h"
 #include "TLibCommon/TComTrQuant.h"
 #include "TLibCommon/TComSampleAdaptiveOffset.h"
diff -r f5f71d7f3bf3 -r adcbd22c1502 source/Lib/TLibEncoder/TEncSbac.cpp
--- a/source/Lib/TLibEncoder/TEncSbac.cpp	Wed Oct 23 20:08:55 2013 +0800
+++ b/source/Lib/TLibEncoder/TEncSbac.cpp	Wed Oct 23 20:09:33 2013 +0800
@@ -61,14 +61,83 @@
 
 #endif // if ENC_DEC_TRACE
 
+const int 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,
+    0x04a87, 0x0cbf7, 0x045d6, 0x0d5c3, 0x04144, 0x0e01b, 0x03d88, 0x0e937, 0x039e0, 0x0f2cd, 0x03663, 0x0fc9e, 0x03347, 0x10600, 0x03050, 0x10f95,
+    0x02d4d, 0x11a02, 0x02ad3, 0x12333, 0x0286e, 0x12cad, 0x02604, 0x136df, 0x02425, 0x13f48, 0x021f4, 0x149c4, 0x0203e, 0x1527b, 0x01e4d, 0x15d00,
+    0x01c99, 0x166de, 0x01b18, 0x17017, 0x019a5, 0x17988, 0x01841, 0x18327, 0x016df, 0x18d50, 0x015d9, 0x19547, 0x0147c, 0x1a083, 0x0138e, 0x1a8a3,
+    0x01251, 0x1b418, 0x01166, 0x1bd27, 0x01068, 0x1c77b, 0x00f7f, 0x1d18e, 0x00eda, 0x1d91a, 0x00e19, 0x1e254, 0x00d4f, 0x1ec9a, 0x00c90, 0x1f6e0,
+    0x00c01, 0x1fef8, 0x00b5f, 0x208b1, 0x00ab6, 0x21362, 0x00a15, 0x21e46, 0x00988, 0x2285d, 0x00934, 0x22ea8, 0x008a8, 0x239b2, 0x0081d, 0x24577,
+    0x007c9, 0x24ce6, 0x00763, 0x25663, 0x00710, 0x25e8f, 0x006a0, 0x26a26, 0x00672, 0x26f23, 0x005e8, 0x27ef8, 0x005ba, 0x284b5, 0x0055e, 0x29057,
+    0x0050c, 0x29bab, 0x004c1, 0x2a674, 0x004a7, 0x2aa5e, 0x0046f, 0x2b32f, 0x0041f, 0x2c0ad, 0x003e7, 0x2ca8d, 0x003ba, 0x2d323, 0x0010c, 0x3bfbb
+};
+
+const uint8_t g_nextStateMPS[128] =
+{
+    2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
+    18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
+    34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
+    50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
+    66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
+    82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
+    98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
+    114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 124, 125, 126, 127
+};
+
+const uint8_t g_nextStateLPS[128] =
+{
+    1, 0, 0, 1, 2, 3, 4, 5, 4, 5, 8, 9, 8, 9, 10, 11,
+    12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 22, 23, 22, 23, 24, 25,
+    26, 27, 26, 27, 30, 31, 30, 31, 32, 33, 32, 33, 36, 37, 36, 37,
+    38, 39, 38, 39, 42, 43, 42, 43, 44, 45, 44, 45, 46, 47, 48, 49,
+    48, 49, 50, 51, 52, 53, 52, 53, 54, 55, 54, 55, 56, 57, 58, 59,
+    58, 59, 60, 61, 60, 61, 60, 61, 62, 63, 64, 65, 64, 65, 66, 67,
+    66, 67, 66, 67, 68, 69, 68, 69, 70, 71, 70, 71, 70, 71, 72, 73,
+    72, 73, 72, 73, 74, 75, 74, 75, 74, 75, 76, 77, 76, 77, 126, 127
+};
+
+UChar g_nextState[128][2];
+
+void buildNextStateTable()
+{
+    for (int i = 0; i < 128; i++)
+    {
+        for (int j = 0; j < 2; j++)
+        {
+            g_nextState[i][j] = ((i & 1) == j) ? g_nextStateMPS[i] : g_nextStateLPS[i];
+        }
+    }
+}
+
+/**
+ - initialize context model with respect to QP and initialization value
+ .
+ \param  qp         input QP value
+ \param  initValue  8 bit initialization value
+ */
+uint8_t sbacInit(int qp, int initValue)
+{
+    qp = Clip3(0, 51, qp);
+
+    int  slope      = (initValue >> 4) * 5 - 45;
+    int  offset     = ((initValue & 15) << 3) - 16;
+    int  initState  =  X265_MIN(X265_MAX(1, (((slope * qp) >> 4) + offset)), 126);
+    UInt mpState    = (initState >= 64);
+    uint8_t m_state = ((mpState ? (initState - 64) : (63 - initState)) << 1) + mpState;
+
+    return m_state;
+}
+
 static void initBuffer(ContextModel* contextModel, SliceType sliceType, int qp, UChar* ctxModel, int size)
 {
     ctxModel += sliceType * size;
 
     for (int n = 0; n < size; n++)
     {
-        contextModel[n].init(qp, ctxModel[n]);
-        contextModel[n].setBinsCoded(0);
+        contextModel[n].m_state = sbacInit(qp, ctxModel[n]);
+        contextModel[n].bBinsCoded = 0;
     }
 }
 
@@ -81,14 +150,14 @@
     for (int n = 0; n < size; n++)
     {
         ContextModel tmpContextModel;
-        tmpContextModel.init(qp, ctxModel[n]);
+        tmpContextModel.m_state = sbacInit(qp, ctxModel[n]);
 
         // Map the 64 CABAC states to their corresponding probability values
         static double aStateToProbLPS[] = { 0.50000000, 0.47460857, 0.45050660, 0.42762859, 0.40591239, 0.38529900, 0.36573242, 0.34715948, 0.32952974, 0.31279528, 0.29691064, 0.28183267, 0.26752040, 0.25393496, 0.24103941, 0.22879875, 0.21717969, 0.20615069, 0.19568177, 0.18574449, 0.17631186, 0.16735824, 0.15885931, 0.15079198, 0.14313433, 0.13586556, 0.12896592, 0.12241667, 0.11620000, 0.11029903, 0.10469773, 0.09938088, 0.09433404, 0.08954349, 0.08499621, 0.08067986, 0.07658271, 0.07269362, 0.06900203, 0.06549791, 0.06217174, 0.05901448, 0.05601756, 0.05317283, 0.05047256, 0.04790942, 0.04547644, 0.04316702, 0.04097487, 0.03889405, 0.03691890, 0.03504406, 0.03326442, 0.03157516, 0.02997168, 0.02844963, 0.02700488, 0.02563349, 0.02433175, 0.02309612, 0.02192323, 0.02080991, 0.01975312, 0.01875000 };
 
-        double probLPS          = aStateToProbLPS[contextModel[n].getState()];
+        double probLPS          = aStateToProbLPS[sbacGetState(contextModel[n].m_state)];
         double prob0, prob1;
-        if (contextModel[n].getMps() == 1)
+        if (sbacGetMps(contextModel[n].m_state) == 1)
         {
             prob0 = probLPS;
             prob1 = 1.0 - prob0;
@@ -99,9 +168,9 @@
             prob0 = 1.0 - prob1;
         }
 
-        if (contextModel[n].getBinsCoded() > 0)
+        if (contextModel[n].bBinsCoded > 0)
         {
-            cost += (UInt)(prob0 * tmpContextModel.getEntropyBits(0) + prob1 * tmpContextModel.getEntropyBits(1));
+            cost += (UInt)(prob0 * sbacGetEntropyBits(tmpContextModel.m_state, 0) + prob1 * sbacGetEntropyBits(tmpContextModel.m_state, 1));
         }
     }
 
@@ -119,6 +188,8 @@
     , m_coeffCost(0)
 {
     assert(MAX_OFF_CTX_MOD <= MAX_NUM_CTX_MOD);
+
+    memset(m_contextModels, 0, sizeof(m_contextModels));
 }
 
 TEncSbac::~TEncSbac()
@@ -2442,16 +2513,16 @@
 
     for (UInt uiCtxInc = 0; uiCtxInc < 3 * NUM_QT_CBF_CTX; uiCtxInc++)
     {
-        estBitsSbac->blockCbpBits[uiCtxInc][0] = ctx[uiCtxInc].getEntropyBits(0);
-        estBitsSbac->blockCbpBits[uiCtxInc][1] = ctx[uiCtxInc].getEntropyBits(1);
+        estBitsSbac->blockCbpBits[uiCtxInc][0] = sbacGetEntropyBits(ctx[uiCtxInc].m_state, 0);
+        estBitsSbac->blockCbpBits[uiCtxInc][1] = sbacGetEntropyBits(ctx[uiCtxInc].m_state, 1);
     }
 
     ctx = &m_contextModels[OFF_QT_ROOT_CBF_CTX];
 
     for (UInt uiCtxInc = 0; uiCtxInc < 4; uiCtxInc++)
     {
-        estBitsSbac->blockRootCbpBits[uiCtxInc][0] = ctx[uiCtxInc].getEntropyBits(0);
-        estBitsSbac->blockRootCbpBits[uiCtxInc][1] = ctx[uiCtxInc].getEntropyBits(1);
+        estBitsSbac->blockRootCbpBits[uiCtxInc][0] = sbacGetEntropyBits(ctx[uiCtxInc].m_state, 0);
+        estBitsSbac->blockRootCbpBits[uiCtxInc][1] = sbacGetEntropyBits(ctx[uiCtxInc].m_state, 1);
     }
 }
 
@@ -2470,7 +2541,7 @@
     {
         for (UInt bin = 0; bin < 2; bin++)
         {
-            estBitsSbac->significantCoeffGroupBits[ctxIdx][bin] = m_contextModels[OFF_SIG_CG_FLAG_CTX + ((ttype ? NUM_SIG_CG_FLAG_CTX : 0) + ctxIdx)].getEntropyBits(bin);
+            estBitsSbac->significantCoeffGroupBits[ctxIdx][bin] = sbacGetEntropyBits(m_contextModels[OFF_SIG_CG_FLAG_CTX + ((ttype ? NUM_SIG_CG_FLAG_CTX : 0) + ctxIdx)].m_state, bin);
         }
     }
 }
@@ -2500,14 +2571,14 @@
     {
         for (UInt bin = 0; bin < 2; bin++)
         {
-            estBitsSbac->significantBits[0][bin] = m_contextModels[OFF_SIG_FLAG_CTX].getEntropyBits(bin);
+            estBitsSbac->significantBits[0][bin] = sbacGetEntropyBits(m_contextModels[OFF_SIG_FLAG_CTX].m_state, bin);
         }
 
         for (int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++)
         {
             for (UInt bin = 0; bin < 2; bin++)
             {
-                estBitsSbac->significantBits[ctxIdx][bin] = m_contextModels[OFF_SIG_FLAG_CTX + ctxIdx].getEntropyBits(bin);
+                estBitsSbac->significantBits[ctxIdx][bin] = sbacGetEntropyBits(m_contextModels[OFF_SIG_FLAG_CTX + ctxIdx].m_state, bin);
             }
         }
     }
@@ -2515,14 +2586,14 @@
     {
         for (UInt bin = 0; bin < 2; bin++)
         {
-            estBitsSbac->significantBits[0][bin] = m_contextModels[OFF_SIG_FLAG_CTX + (NUM_SIG_FLAG_CTX_LUMA + 0)].getEntropyBits(bin);
+            estBitsSbac->significantBits[0][bin] = sbacGetEntropyBits(m_contextModels[OFF_SIG_FLAG_CTX + (NUM_SIG_FLAG_CTX_LUMA + 0)].m_state, bin);
         }
 
         for (int ctxIdx = firstCtx; ctxIdx < firstCtx + numCtx; ctxIdx++)
         {
             for (UInt bin = 0; bin < 2; bin++)
             {
-                estBitsSbac->significantBits[ctxIdx][bin] = m_contextModels[OFF_SIG_FLAG_CTX + (NUM_SIG_FLAG_CTX_LUMA + ctxIdx)].getEntropyBits(bin);
+                estBitsSbac->significantBits[ctxIdx][bin] = sbacGetEntropyBits(m_contextModels[OFF_SIG_FLAG_CTX + (NUM_SIG_FLAG_CTX_LUMA + ctxIdx)].m_state, bin);
             }
         }
     }
@@ -2539,16 +2610,16 @@
     for (ctx = 0; ctx < g_groupIdx[width - 1]; ctx++)
     {
         int ctxOffset = blkSizeOffsetX + (ctx >> shiftX);
-        estBitsSbac->lastXBits[ctx] = bitsX + m_contextModels[OFF_CTX_LAST_FLAG_X + ((ttype ? NUM_CTX_LAST_FLAG_XY : 0) + ctxOffset)].getEntropyBits(0);
-        bitsX += m_contextModels[OFF_CTX_LAST_FLAG_X + ((ttype ? NUM_CTX_LAST_FLAG_XY : 0) + ctxOffset)].getEntropyBits(1);
+        estBitsSbac->lastXBits[ctx] = bitsX + sbacGetEntropyBits(m_contextModels[OFF_CTX_LAST_FLAG_X + ((ttype ? NUM_CTX_LAST_FLAG_XY : 0) + ctxOffset)].m_state, 0);
+        bitsX += sbacGetEntropyBits(m_contextModels[OFF_CTX_LAST_FLAG_X + ((ttype ? NUM_CTX_LAST_FLAG_XY : 0) + ctxOffset)].m_state, 1);
     }
 
     estBitsSbac->lastXBits[ctx] = bitsX;
     for (ctx = 0; ctx < g_groupIdx[height - 1]; ctx++)
     {
         int ctxOffset = blkSizeOffsetY + (ctx >> shiftY);
-        estBitsSbac->lastYBits[ctx] = bitsY + m_contextModels[OFF_CTX_LAST_FLAG_Y + ((ttype ? NUM_CTX_LAST_FLAG_XY : 0) + ctxOffset)].getEntropyBits(0);
-        bitsY += m_contextModels[OFF_CTX_LAST_FLAG_Y + ((ttype ? NUM_CTX_LAST_FLAG_XY : 0) + ctxOffset)].getEntropyBits(1);
+        estBitsSbac->lastYBits[ctx] = bitsY + sbacGetEntropyBits(m_contextModels[OFF_CTX_LAST_FLAG_Y + ((ttype ? NUM_CTX_LAST_FLAG_XY : 0) + ctxOffset)].m_state, 0);
+        bitsY += sbacGetEntropyBits(m_contextModels[OFF_CTX_LAST_FLAG_Y + ((ttype ? NUM_CTX_LAST_FLAG_XY : 0) + ctxOffset)].m_state, 1);
     }
 
     estBitsSbac->lastYBits[ctx] = bitsY;
@@ -2569,14 +2640,14 @@
 
         for (int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_LUMA; ctxIdx++)
         {
-            estBitsSbac->greaterOneBits[ctxIdx][0] = ctxOne[ctxIdx].getEntropyBits(0);
-            estBitsSbac->greaterOneBits[ctxIdx][1] = ctxOne[ctxIdx].getEntropyBits(1);
+            estBitsSbac->greaterOneBits[ctxIdx][0] = sbacGetEntropyBits(ctxOne[ctxIdx].m_state, 0);
+            estBitsSbac->greaterOneBits[ctxIdx][1] = sbacGetEntropyBits(ctxOne[ctxIdx].m_state, 1);
         }
 
         for (int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_LUMA; ctxIdx++)
         {
-            estBitsSbac->levelAbsBits[ctxIdx][0] = ctxAbs[ctxIdx].getEntropyBits(0);
-            estBitsSbac->levelAbsBits[ctxIdx][1] = ctxAbs[ctxIdx].getEntropyBits(1);
+            estBitsSbac->levelAbsBits[ctxIdx][0] = sbacGetEntropyBits(ctxAbs[ctxIdx].m_state, 0);
+            estBitsSbac->levelAbsBits[ctxIdx][1] = sbacGetEntropyBits(ctxAbs[ctxIdx].m_state, 1);
         }
     }
     else
@@ -2586,14 +2657,14 @@
 
         for (int ctxIdx = 0; ctxIdx < NUM_ONE_FLAG_CTX_CHROMA; ctxIdx++)
         {
-            estBitsSbac->greaterOneBits[ctxIdx][0] = ctxOne[ctxIdx].getEntropyBits(0);
-            estBitsSbac->greaterOneBits[ctxIdx][1] = ctxOne[ctxIdx].getEntropyBits(1);
+            estBitsSbac->greaterOneBits[ctxIdx][0] = sbacGetEntropyBits(ctxOne[ctxIdx].m_state, 0);
+            estBitsSbac->greaterOneBits[ctxIdx][1] = sbacGetEntropyBits(ctxOne[ctxIdx].m_state, 1);
         }
 
         for (int ctxIdx = 0; ctxIdx < NUM_ABS_FLAG_CTX_CHROMA; ctxIdx++)
         {
-            estBitsSbac->levelAbsBits[ctxIdx][0] = ctxAbs[ctxIdx].getEntropyBits(0);
-            estBitsSbac->levelAbsBits[ctxIdx][1] = ctxAbs[ctxIdx].getEntropyBits(1);
+            estBitsSbac->levelAbsBits[ctxIdx][0] = sbacGetEntropyBits(ctxAbs[ctxIdx].m_state, 0);
+            estBitsSbac->levelAbsBits[ctxIdx][1] = sbacGetEntropyBits(ctxAbs[ctxIdx].m_state, 1);
         }
     }
 }
diff -r f5f71d7f3bf3 -r adcbd22c1502 source/Lib/TLibEncoder/TEncSbac.h
--- a/source/Lib/TLibEncoder/TEncSbac.h	Wed Oct 23 20:08:55 2013 +0800
+++ b/source/Lib/TLibEncoder/TEncSbac.h	Wed Oct 23 20:09:33 2013 +0800
@@ -40,7 +40,6 @@
 
 #include "TLibCommon/TComBitStream.h"
 #include "TLibCommon/ContextTables.h"
-#include "TLibCommon/ContextModel.h"
 #include "TEncEntropy.h"
 #include "TEncBinCoderCABAC.h"
 #include "SyntaxElementWriter.h"
diff -r f5f71d7f3bf3 -r adcbd22c1502 source/common/CMakeLists.txt
--- a/source/common/CMakeLists.txt	Wed Oct 23 20:08:55 2013 +0800
+++ b/source/common/CMakeLists.txt	Wed Oct 23 20:09:33 2013 +0800
@@ -8,7 +8,6 @@
 
 set(LIBCOMMON_HDR
     ../Lib/TLibCommon/CommonDef.h
-    ../Lib/TLibCommon/ContextModel.h
     ../Lib/TLibCommon/ContextTables.h
     ../Lib/TLibCommon/NAL.h
     ../Lib/TLibCommon/SEI.h
@@ -31,7 +30,6 @@
     ../Lib/TLibCommon/TComYuv.h
     ../Lib/TLibCommon/TypeDef.h)
 set(LIBCOMMON_SRC
-    ../Lib/TLibCommon/ContextModel.cpp
     ../Lib/TLibCommon/TComBitStream.cpp
     ../Lib/TLibCommon/TComDataCU.cpp
     ../Lib/TLibCommon/TComLoopFilter.cpp
diff -r f5f71d7f3bf3 -r adcbd22c1502 source/common/primitives.cpp
--- a/source/common/primitives.cpp	Wed Oct 23 20:08:55 2013 +0800
+++ b/source/common/primitives.cpp	Wed Oct 23 20:09:33 2013 +0800
@@ -22,7 +22,7 @@
  *****************************************************************************/
 
 #include "TLibCommon/TComRom.h"
-#include "TLibCommon/ContextModel.h"
+#include "TLibEncoder/TEncSbac.h"
 #include "primitives.h"
 #include "common.h"
 
@@ -95,7 +95,7 @@
 {
     // initialize global variables
     initROM();
-    ContextModel::buildNextStateTable();
+    buildNextStateTable();
 
     if (cpuid < 0)
     {



More information about the x265-devel mailing list