// CabacContextModel头文件:
class H264AVCCOMMONLIB_API CabacContextModel { public: CabacContextModel(); ~CabacContextModel(); const UChar getState() { return m_ucState>>1; } const UChar getMps() { return m_ucState&1; } Void toggleMps() { m_ucState ^= 1; } Void setState( UChar ucState ) { m_ucState = (ucState<<1)+getMps(); } Void init( Short asCtxInit[], Int iQp ) { Int iState = ( ( asCtxInit[0] * iQp ) >> 4 ) + asCtxInit[1]; iState = min (max ( 1, iState), 126 ); if (iState>=64) { m_ucState = iState - 64; m_ucState += m_ucState + 1; } else { m_ucState = 63 - iState; m_ucState += m_ucState; } m_uiCount = 0; } Void initEqualProbability() { m_ucState = 0; m_uiCount = 0; } Void incrementCount() { m_uiCount++; } private: UChar m_ucState; UInt m_uiCount; static const Double m_afProbability[128]; static const Double m_afEntropy [128]; };
两个私有成员,m_uiCount似乎是上下文的编号,m_ucState最后一位表示MPS符号是什么,前面的位数表示
m_afProbability[128]和m_afEntropy [128] 是常数
构造函数和initEqualProbability都把m_ucState和m_uiCount初始化为零。
函数incrementCount:当输入的是MPS符号时,sigma右移一位,表现为m_uiCount自增1
toggleMps:交换MPS和LPS符号
类CabacContextModel2DBuffer:
成员变量
CabacContextModel* m_pcCContextModel;
const UInt m_uiSizeX;
const UInt m_uiSizeY;
事实上形成了一个 m_uiSizeX * m_uiSizeY 的CabacContextModel数组,m_uiSizeX 是列数
初始化:
Void init( Short asCtxInit[], Int iQp ) { Int iState = ( ( asCtxInit[0] * iQp ) >> 4 ) + asCtxInit[1]; iState = min (max ( 1, iState), 126 ); if (iState>=64) { m_ucState = iState - 64; m_ucState += m_ucState + 1; } else { m_ucState = 63 - iState; m_ucState += m_ucState; } m_uiCount = 0; }
函数:
CabacContextModel* get( UInt uiY ) 取得第uiY 的地址
CabacContextModel& get( UInt uiY, UInt uiX ) 取得第uiY行第uiX 列的元素
ErrVal initBuffer( Short* psCtxModel, Int iQp ) 初始化
/******************************************************************************************************************************/
CabacTables.h:记录了三张表格
g_aucLPSTable64x4 :64行4列的表格
g_aucACNextStateMPS64 :新来的符号是MPS时,状态下标的迁移
g_aucACNextStateLPS64 : 新来的符号时LPS时,状态下标的迁移
const UChar g_aucACNextStateMPS64[64] =
{
1, 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,62,63
};
const UChar g_aucACNextStateLPS64[64] =
{
0, 0, 1, 2, 2, 4, 4, 5, 6, 7,
8, 9, 9,11,11,12,13,13,15,15,
16,16,18,18,19,19,21,21,22,22,
23,24,24,25,26,26,27,27,28,29,
29,30,30,30,31,32,32,33,33,33,
34,34,35,35,35,36,36,36,37,37,
37,38,38,63
};