注:问号以及未注释部分 会在x265-1.9版本内更新
/*****************************************************************************
* Copyright (C) 2015 x265 project
*
* Authors: Steve Borho <steve@borho.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
*
* This program is also available under a commercial proprietary license.
* For more information, contact us at license @ x265.com.
*****************************************************************************/
#ifndef X265_CONTEXTS_H
#define X265_CONTEXTS_H
#include "common.h"
#define NUM_SPLIT_FLAG_CTX 3 // CU划分标志上下文模型个数 number of context models for split flag
#define NUM_SKIP_FLAG_CTX 3 // CUskip标志上下文模型个数number of context models for skip flag
#define NUM_MERGE_FLAG_EXT_CTX 1 // Merge标志上下文模型个数number of context models for merge flag of merge extended
#define NUM_MERGE_IDX_EXT_CTX 1 // Merge idx标志上下文模型个数number of context models for merge index of merge extended
#define NUM_PART_SIZE_CTX 4 // Part size 标志上下文模型个数 number of context models for partition size
#define NUM_PRED_MODE_CTX 1 // pred mode 标志上下文模型个数number of context models for prediction mode
#define NUM_ADI_CTX 1 // intra亮度预测 标志上下文模型个数number of context models for intra prediction
#define NUM_CHROMA_PRED_CTX 2 // intra色度预测 标志上下文模型个数number of context models for intra prediction (chroma)
#define NUM_INTER_DIR_CTX 5 // interdir 标志上下文模型个数number of context models for inter prediction direction
#define NUM_MV_RES_CTX 2 // MVD 标志上下文模型个数number of context models for motion vector difference
#define NUM_REF_NO_CTX 2 // 参考帧 标志上下文模型个数number of context models for reference index
#define NUM_TRANS_SUBDIV_FLAG_CTX 3 // TU划分 标志上下文模型个数number of context models for transform subdivision flags
#define NUM_QT_CBF_CTX 7 // CBF 标志上下文模型个数number of context models for QT CBF
#define NUM_QT_ROOT_CBF_CTX 1 // RQT ROOT CBF 标志上下文模型个数number of context models for QT ROOT CBF
#define NUM_DELTA_QP_CTX 3 // CU delta QP 标志上下文模型个数number of context models for dQP
#define NUM_SIG_CG_FLAG_CTX 2 //子块 标志上下文模型个数 number of context models for MULTI_LEVEL_SIGNIFICANCE
#define NUM_SIG_FLAG_CTX 42 //SIG coeff标志上下文模型个数 number of context models for sig flag
#define NUM_SIG_FLAG_CTX_LUMA 27 // number of context models for luma sig flag
#define NUM_SIG_FLAG_CTX_CHROMA 15 // number of context models for chroma sig flag
#define NUM_CTX_LAST_FLAG_XY 18 // SIG flag xy标志上下文模型个数number of context models for last coefficient position
#define NUM_CTX_LAST_FLAG_XY_LUMA 15 // number of context models for last coefficient position of luma
#define NUM_CTX_LAST_FLAG_XY_CHROMA 3 // number of context models for last coefficient position of chroma
#define NUM_CTX_LAST_FLAG_XY 18 // coeff abs l标志上下文模型个数number of context models for last coefficient position
#define NUM_ONE_FLAG_CTX 24 // number of context models for greater than 1 flag
#define NUM_ONE_FLAG_CTX_LUMA 16 // number of context models for greater than 1 flag of luma
#define NUM_ONE_FLAG_CTX_CHROMA 8 // number of context models for greater than 1 flag of chroma
#define NUM_ABS_FLAG_CTX 6 // coeff abs 2标志上下文模型个数number of context models for greater than 2 flag
#define NUM_ABS_FLAG_CTX_LUMA 4 // number of context models for greater than 2 flag of luma
#define NUM_ABS_FLAG_CTX_CHROMA 2 // number of context models for greater than 2 flag of chroma
#define NUM_MVP_IDX_CTX 1 // MVP index标志上下文模型个数number of context models for MVP index
#define NUM_SAO_MERGE_FLAG_CTX 1 // SAO Merge标志上下文模型个数number of context models for SAO merge flags
#define NUM_SAO_TYPE_IDX_CTX 1 // SAO type index标志上下文模型个数number of context models for SAO type index
#define NUM_TRANSFORMSKIP_FLAG_CTX 1 // cu transform skip flag标志上下文模型个数number of context models for transform skipping
#define NUM_TQUANT_BYPASS_FLAG_CTX 1 // cu transquant bypass flag标志上下文模型个数
#define CNU 154 //表示此处不用上下文模型值 dummy initialization value for unused context models 'Context model Not Used'
// Offset for context
#define OFF_SPLIT_FLAG_CTX (0) //CU划分标记的上下文信息存储位置
#define OFF_SKIP_FLAG_CTX (OFF_SPLIT_FLAG_CTX + NUM_SPLIT_FLAG_CTX) //CUskip标记的上下文信息存储位置
#define OFF_MERGE_FLAG_EXT_CTX (OFF_SKIP_FLAG_CTX + NUM_SKIP_FLAG_CTX) //Merge标记的上下文信息存储位置
#define OFF_MERGE_IDX_EXT_CTX (OFF_MERGE_FLAG_EXT_CTX + NUM_MERGE_FLAG_EXT_CTX) //Merge idx标记的上下文信息存储位置
#define OFF_PART_SIZE_CTX (OFF_MERGE_IDX_EXT_CTX + NUM_MERGE_IDX_EXT_CTX) //Part size 标记的上下文信息存储位置
#define OFF_PRED_MODE_CTX (OFF_PART_SIZE_CTX + NUM_PART_SIZE_CTX) //Pred mode 标记的上下文信息存储位置
#define OFF_ADI_CTX (OFF_PRED_MODE_CTX + NUM_PRED_MODE_CTX) //intra Pred luma mode 标记的上下文信息存储位置
#define OFF_CHROMA_PRED_CTX (OFF_ADI_CTX + NUM_ADI_CTX) //intra Pred chroma mode 标记的上下文信息存储位置
#define OFF_DELTA_QP_CTX (OFF_CHROMA_PRED_CTX + NUM_CHROMA_PRED_CTX) //CU delta QP 标记的上下文信息存储位置
#define OFF_INTER_DIR_CTX (OFF_DELTA_QP_CTX + NUM_DELTA_QP_CTX) //inter dir 标记的上下文信息存储位置
#define OFF_REF_NO_CTX (OFF_INTER_DIR_CTX + NUM_INTER_DIR_CTX) //参考帧 标记的上下文信息存储位置
#define OFF_MV_RES_CTX (OFF_REF_NO_CTX + NUM_REF_NO_CTX) //MVD 标记的上下文信息存储位置
#define OFF_QT_CBF_CTX (OFF_MV_RES_CTX + NUM_MV_RES_CTX) //CBF 标记的上下文信息存储位置
#define OFF_TRANS_SUBDIV_FLAG_CTX (OFF_QT_CBF_CTX + NUM_QT_CBF_CTX) //TU划分 标记的上下文信息存储位置
#define OFF_QT_ROOT_CBF_CTX (OFF_TRANS_SUBDIV_FLAG_CTX + NUM_TRANS_SUBDIV_FLAG_CTX)//RQT root CBF 标记的上下文信息存储位置
#define OFF_SIG_CG_FLAG_CTX (OFF_QT_ROOT_CBF_CTX + NUM_QT_ROOT_CBF_CTX) //子块 标记的上下文信息存储位置
#define OFF_SIG_FLAG_CTX (OFF_SIG_CG_FLAG_CTX + 2 * NUM_SIG_CG_FLAG_CTX) //SIG coeff 标记的上下文信息存储位置
#define OFF_CTX_LAST_FLAG_X (OFF_SIG_FLAG_CTX + NUM_SIG_FLAG_CTX) //SIG flag x 标记的上下文信息存储位置
#define OFF_CTX_LAST_FLAG_Y (OFF_CTX_LAST_FLAG_X + NUM_CTX_LAST_FLAG_XY)//SIG flag y 标记的上下文信息存储位置
#define OFF_ONE_FLAG_CTX (OFF_CTX_LAST_FLAG_Y + NUM_CTX_LAST_FLAG_XY)//coeff abs level greater1 标记的上下文信息存储位置
#define OFF_ABS_FLAG_CTX (OFF_ONE_FLAG_CTX + NUM_ONE_FLAG_CTX)//coeff abs level greater2 标记的上下文信息存储位置
#define OFF_MVP_IDX_CTX (OFF_ABS_FLAG_CTX + NUM_ABS_FLAG_CTX)//MVP index 标记的上下文信息存储位置
#define OFF_SAO_MERGE_FLAG_CTX (OFF_MVP_IDX_CTX + NUM_MVP_IDX_CTX)//SAO Merge 标记的上下文信息存储位置
#define OFF_SAO_TYPE_IDX_CTX (OFF_SAO_MERGE_FLAG_CTX + NUM_SAO_MERGE_FLAG_CTX)//SAO type index 标记的上下文信息存储位置
#define OFF_TRANSFORMSKIP_FLAG_CTX (OFF_SAO_TYPE_IDX_CTX + NUM_SAO_TYPE_IDX_CTX)//cu transform skip flag 标记的上下文信息存储位置
#define OFF_TQUANT_BYPASS_FLAG_CTX (OFF_TRANSFORMSKIP_FLAG_CTX + 2 * NUM_TRANSFORMSKIP_FLAG_CTX)//cu transquant bypass flag 标记的上下文信息存储位置
#define MAX_OFF_CTX_MOD (OFF_TQUANT_BYPASS_FLAG_CTX + NUM_TQUANT_BYPASS_FLAG_CTX)//最后位置
extern "C" const uint32_t PFX(entropyStateBits)[128];
namespace X265_NS {
// private namespace
extern const uint32_t g_entropyBits[128];
extern const uint8_t g_nextState[128][2];
#define sbacGetMps(S) ((S) & 1)
#define sbacGetState(S) ((S) >> 1)
#define sbacNext(S, V) (g_nextState[(S)][(V)])
#define sbacGetEntropyBits(S, V) (g_entropyBits[(S) ^ (V)])
#define sbacGetEntropyBitsTrm(V) (g_entropyBits[126 ^ (V)])
#define MAX_NUM_CHANNEL_TYPE 2
static const uint32_t ctxCbf[3][5] = { { 1, 0, 0, 0, 0 }, { 2, 3, 4, 5, 6 }, { 2, 3, 4, 5, 6 } };
static const uint32_t significanceMapContextSetStart[MAX_NUM_CHANNEL_TYPE][3] = { { 0, 9, 21 }, { 0, 9, 12 } };
static const uint32_t significanceMapContextSetSize[MAX_NUM_CHANNEL_TYPE][3] = { { 9, 12, 6 }, { 9, 3, 3 } };
static const uint32_t nonDiagonalScan8x8ContextOffset[MAX_NUM_CHANNEL_TYPE] = { 6, 0 };
static const uint32_t notFirstGroupNeighbourhoodContextOffset[MAX_NUM_CHANNEL_TYPE] = { 3, 0 };
// initial probability for cu_transquant_bypass flag
//cu transquant bypass flag下文模型初始值
static const uint8_t INIT_CU_TRANSQUANT_BYPASS_FLAG[3][NUM_TQUANT_BYPASS_FLAG_CTX] =
{
{ 154 },//B帧
{ 154 },//P帧
{ 154 },//I帧
};
// initial probability for split flag
// CU划分上下文模型初始值
static const uint8_t INIT_SPLIT_FLAG[3][NUM_SPLIT_FLAG_CTX] =
{
{ 107, 139, 126, },//B帧
{ 107, 139, 126, },//P帧
{ 139, 141, 157, },//I帧
};
// CUskip上下文模型初始值
static const uint8_t INIT_SKIP_FLAG[3][NUM_SKIP_FLAG_CTX] =
{
{ 197, 185, 201, },//B帧
{ 197, 185, 201, },//P帧
{ CNU, CNU, CNU, },
};
// Merge上下文模型初始值
static const uint8_t INIT_MERGE_FLAG_EXT[3][NUM_MERGE_FLAG_EXT_CTX] =
{
{ 154, },//B帧
{ 110, },//P帧
{ CNU, },
};
// Merge idx上下文模型初始值
static const uint8_t INIT_MERGE_IDX_EXT[3][NUM_MERGE_IDX_EXT_CTX] =
{
{ 137, },//B帧
{ 122, },//P帧
{ CNU, },
};
// Part size 上下文模型初始值
static const uint8_t INIT_PART_SIZE[3][NUM_PART_SIZE_CTX] =
{
{ 154, 139, 154, 154 },//B帧
{ 154, 139, 154, 154 },//P帧
{ 184, CNU, CNU, CNU },//I帧
};
// Pred size 上下文模型初始值
static const uint8_t INIT_PRED_MODE[3][NUM_PRED_MODE_CTX] =
{
{ 134, },//B帧
{ 149, },//P帧
{ CNU, },
};
// intra Pred 亮度 上下文模型初始值
static const uint8_t INIT_INTRA_PRED_MODE[3][NUM_ADI_CTX] =
{
{ 183, },//B帧
{ 154, },//P帧
{ 184, },//I帧
};
// intra Pred 色度 上下文模型初始值
static const uint8_t INIT_CHROMA_PRED_MODE[3][NUM_CHROMA_PRED_CTX] =
{
{ 152, 139, },//B帧
{ 152, 139, },//P帧
{ 63, 139, },//I帧
};
// inter dir 上下文模型初始值
static const uint8_t INIT_INTER_DIR[3][NUM_INTER_DIR_CTX] =
{
{ 95, 79, 63, 31, 31, },//B帧
{ 95, 79, 63, 31, 31, },//P帧
{ CNU, CNU, CNU, CNU, CNU, },
};
// MVD 上下文模型初始值
static const uint8_t INIT_MVD[3][NUM_MV_RES_CTX] =
{
{ 169, 198, },//B帧
{ 140, 198, },//P帧
{ CNU, CNU, },
};
// 参考帧 上下文模型初始值
static const uint8_t INIT_REF_PIC[3][NUM_REF_NO_CTX] =
{
{ 153, 153 },//B帧
{ 153, 153 },//P帧
{ CNU, CNU },
};
// CU delta QP 上下文模型初始值
static const uint8_t INIT_DQP[3][NUM_DELTA_QP_CTX] =
{
{ 154, 154, 154, },//B帧
{ 154, 154, 154, },//P帧
{ 154, 154, 154, },//I帧
};
// CBF 亮度、色度 上下文模型初始值
static const uint8_t INIT_QT_CBF[3][NUM_QT_CBF_CTX] =
{
{ 153, 111, 149, 92, 167, 154, 154 },//B帧
{ 153, 111, 149, 107, 167, 154, 154 },//P帧
{ 111, 141, 94, 138, 182, 154, 154 },//I帧
};
// RQT root CBF 上下文模型初始值
static const uint8_t INIT_QT_ROOT_CBF[3][NUM_QT_ROOT_CBF_CTX] =
{
{ 79, },//B帧
{ 79, },//P帧
{ CNU, },
};
// SIG LAST XY 上下文模型初始值
static const uint8_t INIT_LAST[3][NUM_CTX_LAST_FLAG_XY] =
{
{ 125, 110, 124, 110, 95, 94, 125, 111, 111, 79, 125, 126, 111, 111, 79,
108, 123, 93 },//B帧
{ 125, 110, 94, 110, 95, 79, 125, 111, 110, 78, 110, 111, 111, 95, 94,
108, 123, 108 },//P帧
{ 110, 110, 124, 125, 140, 153, 125, 127, 140, 109, 111, 143, 127, 111, 79,
108, 123, 63 },//I帧
};
// 子块上下文模型初始值
static const uint8_t INIT_SIG_CG_FLAG[3][2 * NUM_SIG_CG_FLAG_CTX] =
{
{ 121, 140,
61, 154, },//B帧
{ 121, 140,
61, 154, },//P帧
{ 91, 171,
134, 141, },//I帧
};
// SIG coeff上下文模型初始值
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, },//B帧
{ 155, 154, 139, 153, 139, 123, 123, 63, 153, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 166, 183, 140, 136, 153, 154, 170, 153, 123, 123, 107, 121, 107, 121, 167, 151, 183, 140, 151, 183, 140, },//P帧
{ 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, },//I帧
};
// coeff abs level greater1上下文模型初始值
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, },//B帧
{ 154, 196, 196, 167, 154, 152, 167, 182, 182, 134, 149, 136, 153, 121, 136, 137, 169, 194, 166, 167, 154, 167, 137, 182, },//P帧
{ 140, 92, 137, 138, 140, 152, 138, 139, 153, 74, 149, 92, 139, 107, 122, 152, 140, 179, 166, 182, 140, 227, 122, 197, },//I帧
};
// coeff abs level greater2上下文模型初始值
static const uint8_t INIT_ABS_FLAG[3][NUM_ABS_FLAG_CTX] =
{
{ 107, 167, 91, 107, 107, 167, },//B帧
{ 107, 167, 91, 122, 107, 167, },//P帧
{ 138, 153, 136, 167, 152, 152, },//I帧
};
// MVP index上下文模型初始值
static const uint8_t INIT_MVP_IDX[3][NUM_MVP_IDX_CTX] =
{
{ 168 },//B帧
{ 168 },//P帧
{ CNU },
};
// SAO merge flag上下文模型初始值
static const uint8_t INIT_SAO_MERGE_FLAG[3][NUM_SAO_MERGE_FLAG_CTX] =
{
{ 153, },//B帧
{ 153, },//P帧
{ 153, },//I帧
};
// SAO type index上下文模型初始值
static const uint8_t INIT_SAO_TYPE_IDX[3][NUM_SAO_TYPE_IDX_CTX] =
{
{ 160, },//B帧
{ 185, },//P帧
{ 200, },//I帧
};
// TU划分 上下文模型初始值
static const uint8_t INIT_TRANS_SUBDIV_FLAG[3][NUM_TRANS_SUBDIV_FLAG_CTX] =
{
{ 224, 167, 122, },//B帧
{ 124, 138, 94, },//P帧
{ 153, 138, 138, },//I帧
};
// cu transquant bypass flag 上下文模型初始值
static const uint8_t INIT_TRANSFORMSKIP_FLAG[3][2 * NUM_TRANSFORMSKIP_FLAG_CTX] =
{
{ 139, 139 },//B帧
{ 139, 139 },//P帧
{ 139, 139 },//I帧
};
}
#endif // ifndef X265_CONTEXTS_H