留下批注自己体会
#define CU_SET_FLAG(bitfield, flag, value) (bitfield) = ((bitfield) & (~(flag))) | ((~((value) - 1)) & (flag))
void CUData::calcCTUGeoms(uint32_t ctuWidth, uint32_t ctuHeight, uint32_t maxCUSize, uint32_t minCUSize, CUGeom cuDataArray[CUGeom::MAX_GEOMS])
{
uint32_t num4x4Partition = (1U << ((g_log2Size[maxCUSize] - LOG2_UNIT_SIZE) << 1));
// Initialize the coding blocks inside the CTB
for (uint32_t log2CUSize = g_log2Size[maxCUSize], rangeCUIdx = 0; log2CUSize >= g_log2Size[minCUSize]; log2CUSize--)
{
// log2CUSize CU 大小从 g_log2Size[maxCUSize] 开始减小到 g_log2Size[minCUSize]
// 也就是从 64 * 64 减小到 4 * 4
uint32_t blockSize = 1 << log2CUSize; // blockSize 大小
uint32_t sbWidth = 1 << (g_log2Size[maxCUSize] - log2CUSize); // 一行有几个子CTU
int32_t lastLevelFlag = log2CUSize == g_log2Size[minCUSize]; // 是否是最后一层,也就是 CU 大小为 4 * 4
// 以下循环可以这么理解
// 对于第一层,4 个块划分的idx是
// { 0 1
// 2 3 }
// 对于第二层,4 个块的起止就变了,因为第二层,划分的总数是16个
// { 0, 1, 4, 5,
// 2, 3, 6, 7,
// 8, 9, 12, 13,
// 10, 11, 14, 15,}
// 上一层的 0 的孩子 -> 0*4 = 0
// 上一层的 1 的孩子 -> 1*4 = 4
// 上一层的 2 的孩子 -> 2*4 = 8
// 上一层的 3 的孩子 -> 3*4 = 12
// 这就是 Z order 的好处
for (uint32_t sbY = 0; sbY < sbWidth; sbY++)
{
for (uint32_t sbX = 0; sbX < sbWidth; sbX++)
{
uint32_t depthIdx = g_depthScanIdx[sbY][sbX];
uint32_t cuIdx = rangeCUIdx + depthIdx;
uint32_t childIdx = rangeCUIdx + sbWidth * sbWidth + (depthIdx << 2);
// rangeCUIdx 是这层的起始地址 sbWidth * sbWidth 是这层分割的数量
// (depthIdx << 2) 是对应的下层地址
// 因为 m_CUGeom 是 1 + 4 + 16 + 64 每层挨着存放的
// 所以孩子节点是 rangeCUIdx + sbWidth * sbWidth + (depthIdx << 2)
uint32_t px = sbX * blockSize;
uint32_t py = sbY * blockSize;
int32_t presentFlag = px < ctuWidth && py < ctuHeight;
int32_t splitMandatoryFlag = presentFlag && !lastLevelFlag && (px + blockSize > ctuWidth || py + blockSize > ctuHeight);
/* Offset of the luma CU in the X, Y direction in terms of pixels from the CTU origin */
uint32_t xOffset = (sbX * blockSize) >> 3;
uint32_t yOffset = (sbY * blockSize) >> 3;
X265_CHECK(cuIdx < CUGeom::MAX_GEOMS, "CU geom index bug\n");
CUGeom *cu = cuDataArray + cuIdx;
cu->log2CUSize = log2CUSize;
cu->childOffset = childIdx - cuIdx;
// 所以这里就知道了 childOffset 是一个偏移
// 当前 CU 的 Idx + childOffset 是儿子的 Idx
cu->absPartIdx = g_depthScanIdx[yOffset][xOffset] * 4;
// 这里会提问,为啥上面 先各 >> 3, 这里再乘以4,上面直接 >> 2 不就好了
// 不是哦,g_depthScanIdx是 8 * 8 的,所以必须先映射到 8 * 8,再直接 * 4 得到对应的 4 * 4划分的地址
cu->numPartitions = (num4x4Partition >> ((g_log2Size[maxCUSize] - cu->log2CUSize) * 2));
cu->depth = g_log2Size[maxCUSize] - log2CUSize;
cu->geomRecurId = cuIdx;
cu->flags = 0;
// 震惊我
// 卧槽
// 原来这个 枚举类型 这样定义,有这样的含义
// enum {
// INTRA = 1 << 0, // CU is intra predicted
// PRESENT = 1 << 1, // CU is not completely outside the frame
// SPLIT_MANDATORY = 1 << 2, // CU split is mandatory if CU is inside frame and can be split
// LEAF = 1 << 3, // CU is a leaf node of the CTU
// SPLIT = 1 << 4, // CU is currently split in four child CUs.
// };
// 比如:
// (cu->flags) = ((cu->flags) & (~(CUGeom::PRESENT))) | ((~((presentFlag)-1)) & (CUGeom::PRESENT));
// cu->flags = cu->flags & (~(0010)) | ((~((presentFlag)-1)) & (0010)
// cu->flags = (cu->flags & 1101) | ((~((presentFlag)-1)) & (0010)
// 如果 presentFlag == 1 则 ((~((presentFlag)-1)) & (0010) -> 0010
// 如果 presentFlag == 0 则 ((~((presentFlag)-1)) & (0010) -> 0000
//
// 再举个例子
// (cu->flags) = ((cu->flags) & (~(CUGeom::SPLIT_MANDATORY | CUGeom::SPLIT))) | ((~((splitMandatoryFlag)-1)) & (CUGeom::SPLIT_MANDATORY | CUGeom::SPLIT));
// CUGeom::SPLIT_MANDATORY | CUGeom::SPLIT -> 1010
// (cu->flags) & (~(1010)) -> cu->flags & 0101
// (~((splitMandatoryFlag)-1)) & 0101
// 如果 splitMandatoryFlag == 1 则 0101 也就是 splitMandatoryFlag == 1 SPLIT_MANDATORY,SPLIT两个标志位都打开
// 如果 splitMandatoryFlag == 0 则 SPLIT_MANDATORY,SPLIT两个标志位都打开
CU_SET_FLAG(cu->flags, CUGeom::PRESENT, presentFlag);
CU_SET_FLAG(cu->flags, CUGeom::SPLIT_MANDATORY | CUGeom::SPLIT, splitMandatoryFlag);
CU_SET_FLAG(cu->flags, CUGeom::LEAF, lastLevelFlag);
}
}
// rangeCUIdx 是每一层的 CU 的 起始 Geom 的位置
rangeCUIdx += sbWidth * sbWidth;
}
}