注:问号以及未注释部分 会在x265-1.8版本内更新
/*****************************************************************************
* 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.
*****************************************************************************/
#include "common.h"
#include "primitives.h"
#include "scalinglist.h"
namespace {
// file-anonymous namespace
/* Strings for scaling list file parsing */
/* 不同的量化表类型
* 这个表对量化非常重要,量化表是根据这个表中 不同的TU尺寸(4x4、8x8、16x16、32x32)、不同的图像分量(Y、U、V)分别进行建立的
*/
const char MatrixType[4][6][20] =
{
{ // Intra 4x4 + Inter 4x4
"INTRA4X4_LUMA",
"INTRA4X4_CHROMAU",
"INTRA4X4_CHROMAV",
"INTER4X4_LUMA",
"INTER4X4_CHROMAU",
"INTER4X4_CHROMAV"
},
{ // Intra 8x8 + Inter 8x8
"INTRA8X8_LUMA",
"INTRA8X8_CHROMAU",
"INTRA8X8_CHROMAV",
"INTER8X8_LUMA",
"INTER8X8_CHROMAU",
"INTER8X8_CHROMAV"
},
{ // Intra 16x16 + Inter 16x16
"INTRA16X16_LUMA",
"INTRA16X16_CHROMAU",
"INTRA16X16_CHROMAV",
"INTER16X16_LUMA",
"INTER16X16_CHROMAU",
"INTER16X16_CHROMAV"
},
{ // Intra 32x32 Luma + Inter 32x32 Luma
"INTRA32X32_LUMA",
"INTER32X32_LUMA",
},
};
const char MatrixType_DC[4][12][22] =
{
{
},
{
},
{
"INTRA16X16_LUMA_DC",
"INTRA16X16_CHROMAU_DC",
"INTRA16X16_CHROMAV_DC",
"INTER16X16_LUMA_DC",
"INTER16X16_CHROMAU_DC",
"INTER16X16_CHROMAV_DC"
},
{
"INTRA32X32_LUMA_DC",
"INTER32X32_LUMA_DC",
},
};
int quantTSDefault4x4[16] =
{
16, 16, 16, 16,
16, 16, 16, 16,
16, 16, 16, 16,
16, 16, 16, 16
};
int quantIntraDefault8x8[64] =
{
16, 16, 16, 16, 17, 18, 21, 24,
16, 16, 16, 16, 17, 19, 22, 25,
16, 16, 17, 18, 20, 22, 25, 29,
16, 16, 18, 21, 24, 27, 31, 36,
17, 17, 20, 24, 30, 35, 41, 47,
18, 19, 22, 27, 35, 44, 54, 65,
21, 22, 25, 31, 41, 54, 70, 88,
24, 25, 29, 36, 47, 65, 88, 115
};
int quantInterDefault8x8[64] =
{
16, 16, 16, 16, 17, 18, 20, 24,
16, 16, 16, 17, 18, 20, 24, 25,
16, 16, 17, 18, 20, 24, 25, 28,
16, 17, 18, 20, 24, 25, 28, 33,
17, 18, 20, 24, 25, 28, 33, 41,
18, 20, 24, 25, 28, 33, 41, 54,
20, 24, 25, 28, 33, 41, 54, 71,
24, 25, 28, 33, 41, 54, 71, 91
};
}
namespace x265 {
// private namespace
const int ScalingList::s_numCoefPerSize[NUM_SIZES] = { 16, 64, 256, 1024 }; // 不同变换块大小,对应的变换系数个数,(4x4:16,8x8:64,16x16:265,32x32:1024)
const int32_t ScalingList::s_quantScales[NUM_REM] = { 26214, 23302, 20560, 18396, 16384, 14564 }; // 前向量化系数表的值,分别对应不同的Qp余数(Qp%6)
const int32_t ScalingList::s_invQuantScales[NUM_REM] = { 40, 45, 51, 57, 64, 72 }; // 反量化系数表的值,分别对应不同的Qp余数(Qp%6)
ScalingList::ScalingList()
{
memset(m_quantCoef, 0, sizeof(m_quantCoef));
memset(m_dequantCoef, 0, sizeof(m_dequantCoef));
memset(m_scalingListCoef, 0, sizeof(m_scalingListCoef));
}
/** 函数功能 : 初始化量化中所需要的几个表格
* 前向量化表"m_quantCoef"/反量化表"m_dequantCoef"/量化矩阵表"m_scalingListCoef"
* \返回值 numSig : 初始化成功则返回true,否则返回false
*/
bool ScalingList::init()
{
bool ok = true;
for (int sizeId = 0; sizeId < NUM_SIZES; sizeId++)
{
for (int listId = 0; listId < NUM_LISTS; listId++)
{
m_scalingListCoef[sizeId][listId] = X265_MALLOC(int32_t, X265_MIN(MAX_MATRIX_COEF_NUM, s_numCoefPerSize[sizeId])); // 为m_scalingListCoef分配空间,最大为8x8=64
ok &= !!m_scalingListCoef[sizeId][listId];
for (int rem = 0; rem < NUM_REM; rem++)
{
m_quantCoef[sizeId][listId][rem] = X265_MALLOC(int32_t, s_numCoefPerSize[sizeId]); // 为m_quantCoef分配空间,空间大小是s_numCoefPerSize[sizeId],即根据TU尺寸分配
m_dequantCoef[sizeId][listId][rem] = X265_MALLOC(int32_t, s_numCoefPerSize[sizeId]);// 为m_dequantCoef分配空间,空间大小是s_numCoefPerSize[sizeId],即根据TU尺寸分配
ok &= m_quantCoef[sizeId][listId][rem] && m_dequantCoef[sizeId][listId][rem];
}
}
}
return ok;
}
ScalingList::~ScalingList()
{
for (int sizeId = 0; sizeId < NUM_SIZES; sizeId++)
{
for (int listId = 0; listId < NUM_LISTS; listId++)
{
X265_FREE(m_scalingListCoef[sizeId][listId]);
for (int rem = 0; rem < NUM_REM; rem++)
{
X265_FREE(m_quantCoef[sizeId][listId][rem]);
X265_FREE(m_dequantCoef[sizeId][listId][rem]);
}
}
}
}
/* returns predicted list index if a match is found, else -1 */
int ScalingList::checkPredMode(int size, int list) const
{
for (int predList = list; predList >= 0; predList--)
{
// check DC value
if (size < BLOCK_16x16 && m_scalingListDC[size][list] != m_scalingListDC[size][predList])
continue;
// check value of matrix
if (!memcmp(m_scalingListCoef[size][list],
list == predList ? getScalingListDefaultAddress(size, predList) : m_scalingListCoef[size][predList],
sizeof(int32_t) * X265_MIN(MAX_MATRIX_COEF_NUM, s_numCoefPerSize[size])))
return predList;
}
return -1;
}
/* check if use default quantization matrix
* returns true if default quantization matrix is used in all sizes */
bool ScalingList::checkDefaultScalingList() const
{
int defaultCounter = 0;
for (int s = 0; s < NUM_SIZES; s++)
for (int l = 0; l < NUM_LISTS; l++)
if (!memcmp(m_scalingListCoef[s][l], getScalingListDefaultAddress(s, l),
sizeof(int32_t) * X265_MIN(MAX_MATRIX_COEF_NUM, s_numCoefPerSize[s])) &&
((s < BLOCK_16x16) || (m_scalingListDC[s][l] == 16)))
defaultCounter++;
return defaultCounter != (NUM_LISTS * NUM_SIZES - 4); // -4 for 32x32
}
/* get address of default quantization matrix */
/** 函数功能 : 根据不同的TU尺寸,不同的list类型得到不同的默认量化矩阵
* \参数 sizeId : TU尺寸的ID
* \参数 listId : list类型的ID
*/
const int32_t* ScalingList::getScalingListDefaultAddress(int sizeId, int listId) const
{
switch (sizeId)
{
case BLOCK_4x4: // 4x4 量化矩阵
return quantTSDefault4x4;
case BLOCK_8x8: // 8x8 量化矩阵
return (listId < 3) ? quantIntraDefault8x8 : quantInterDefault8x8; // listId:0~2为Intra的量化矩阵(0~2分别对应Y、U、V),3~5为Inter的量化矩阵(3~5分别对应Y、U、V)
case BLOCK_16x16: // 16x16 量化矩阵
return (listId < 3) ? quantIntraDefault8x8 : quantInterDefault8x8; // listId:0~2为Intra的量化矩阵(0~2分别对应Y、U、V),3~5为Inter的量化矩阵(3~5分别对应Y、U、V)
case BLOCK_32x32:
return (listId < 1) ? quantIntraDefault8x8 : quantInterDefault8x8; // 对于32x32,0为Intra Luma的量化矩阵,1为Inter Luma的量化矩阵
default:
break;
}
X265_CHECK(0, "invalid scaling list size\n");
return NULL;
}
/** 函数功能 : 将默认量化矩阵中拷贝到m_scalingListCoef,用于之后的量化
* \参数 sizeId : TU尺寸的ID
* \参数 listId : list类型的ID
*/
void ScalingList::processDefaultMarix(int sizeId, int listId)
{ // 根据不同的TU尺寸,不同的list类型来拷贝对应的默认量化矩阵
memcpy(m_scalingListCoef[sizeId][listId], getScalingListDefaultAddress(sizeId, listId), sizeof(int) * X265_MIN(MAX_MATRIX_COEF_NUM, s_numCoefPerSize[sizeId]));
m_scalingListDC[sizeId][listId] = SCALING_LIST_DC; // 设置DC系数的量化矩阵值
}
/** 函数功能 : 设置默认的量化矩阵
** 调用范围 : 仅在Encoder::create()中被调用
*/
void ScalingList::setDefaultScalingList()
{
for (int sizeId = 0; sizeId < NUM_SIZES; sizeId++) // 设置不同TU尺寸的量化表
for (int listId = 0; listId < NUM_LISTS; listId++) // 设置不同list类型的量化表
processDefaultMarix(sizeId, listId);
m_bEnabled = true; // 使能量化矩阵
m_bDataPresent = false; // 使用默认的量化矩阵
}
bool ScalingList::parseScalingList(const char* filename)
{
FILE *fp = fopen(filename, "r");
if (!fp)
{
x265_log(NULL, X265_LOG_ERROR, "can't open scaling list file %s\n", filename);
return true;
}
char line[1024];
int32_t *src = NULL;
for (int sizeIdc = 0; sizeIdc < NUM_SIZES; sizeIdc++)
{
int size = X265_MIN(MAX_MATRIX_COEF_NUM, s_numCoefPerSize[sizeIdc]);
for (int listIdc = 0; listIdc < NUM_LISTS; listIdc++)
{
src = m_scalingListCoef[sizeIdc][listIdc];
fseek(fp, 0, 0);
do
{
char *ret = fgets(line, 1024, fp);
if (!ret || (!strstr(line, MatrixType[sizeIdc][listIdc]) && feof(fp)))
{
x265_log(NULL, X265_LOG_ERROR, "can't read matrix from %s\n", filename);
return true;
}
}
while (!strstr(line, MatrixType[sizeIdc][listIdc]));
for (int i = 0; i < size; i++)
{
int data;
if (fscanf(fp, "%d,", &data) != 1)
{
x265_log(NULL, X265_LOG_ERROR, "can't read matrix from %s\n", filename);
return true;
}
src[i] = data;
}
// set DC value for default matrix check
m_scalingListDC[sizeIdc][listIdc] = src[0];
if (sizeIdc > BLOCK_8x8)
{
fseek(fp, 0, 0);
do
{
char *ret = fgets(line, 1024, fp);
if (!ret || (!strstr(line, MatrixType_DC[sizeIdc][listIdc]) && feof(fp)))
{
x265_log(NULL, X265_LOG_ERROR, "can't read DC from %s\n", filename);
return true;
}
}
while (!strstr(line, MatrixType_DC[sizeIdc][listIdc]));
int data;
if (fscanf(fp, "%d,", &data) != 1)
{
x265_log(NULL, X265_LOG_ERROR, "can't read matrix from %s\n", filename);
return true;
}
// overwrite DC value when size of matrix is larger than 16x16
m_scalingListDC[sizeIdc][listIdc] = data;
}
}
}
fclose(fp);
m_bEnabled = true;
m_bDataPresent = !checkDefaultScalingList();
return false;
}
/** set quantized matrix coefficient for encode */
/** 函数功能 : 根据不同的TU尺寸,不同的list类型,不同的量化矩阵,生成不同的量化矩阵。
** 如果m_bEnabled为true,则使用默认的量化矩阵或者从文件中读取的量化矩阵,生成新的非均匀量化矩阵。
** 如果m_bEnabled为false,则直接使用默认的量化系数得到均匀量化矩阵。
** 调用范围 : 仅在Encoder::create()中被调用
*/
void ScalingList::setupQuantMatrices()
{
for (int size = 0; size < NUM_SIZES; size++) // 对不同的TU尺寸设置量化矩阵表
{
int width = 1 << (size + 2); // 得到TU的宽度
int ratio = width / X265_MIN(MAX_MATRIX_SIZE_NUM, width); // ratio = width / min(8, width); 假如width <= 8,ratio = 1; 假如width > 8,ratio = width/8
int stride = X265_MIN(MAX_MATRIX_SIZE_NUM, width); // stride = min(8, width)
int count = s_numCoefPerSize[size]; // 得到TU中变换系数的个数
for (int list = 0; list < NUM_LISTS; list++) // 对每个list type设置量化矩阵表
{
int32_t *coeff = m_scalingListCoef[size][list]; // 得到的量化矩阵(HEVC中规定的默认量化矩阵或者是从文件中读取的量化矩阵)
int32_t dc = m_scalingListDC[size][list]; // 得到的DC量化系数(HEVC中规定的默认DC量化系数或者是从文件中读取的DC量化系数)
for (int rem = 0; rem < NUM_REM; rem++) // 对于不同的Qp余数,设置不同的量化矩阵
{
int32_t *quantCoeff = m_quantCoef[size][list][rem]; // 得到均匀量化的量化系数
int32_t *dequantCoeff = m_dequantCoef[size][list][rem]; // 得到均匀量化的量化系数
if (m_bEnabled) // 如果使能非均匀量化
{
processScalingListEnc(coeff, quantCoeff, s_quantScales[rem] << 4, width, width, ratio, stride, dc); // 生成新的非均匀量化矩阵
processScalingListDec(coeff, dequantCoeff, s_invQuantScales[rem], width, width, ratio, stride, dc); // 生成新的非均匀反量化矩阵
}
else // 如果不支持非均匀量化,则只能使用均匀量化
{
/* flat quant and dequant coefficients */
for (int i = 0; i < count; i++) // 均匀量化表中的每一个值都设置为默认的量化系数
{ // 使用的量化系数只与Qp余数相关
quantCoeff[i] = s_quantScales[rem];
dequantCoeff[i] = s_invQuantScales[rem];
}
}
}
}
}
}
/** 函数功能 : 生成非均匀量化矩阵
** 调用范围 : 仅在ScalingList::setupQuantMatrices()中被调用
* \参数 coeff : 输入的非均匀量化矩阵
* \参数 quantcoeff : 输出的新建立的非均匀量化矩阵
* \参数 quantScales: 默认的均匀量化系数
* \参数 height : TU的高度
* \参数 width : TU的宽度
* \参数 ratio : TU尺寸与量化矩阵尺寸的比例 = TU width / min(8, TU width). 假如TU width<=8, 则ratio = 1; 假如TU height>8, ratio = TU width/8
* \参数 stride : TU的步长, = width
* \参数 dc : 输入的非均匀DC量化系数
*/
void ScalingList::processScalingListEnc(int32_t *coeff, int32_t *quantcoeff, int32_t quantScales, int height, int width,
int ratio, int stride, int32_t dc)
{
// 如果width<= 8, ratio = 1, 量化矩阵 quantcoeff = quantscale*16/coeff[stride*j+i]
// 如果width==16, ratio = 2, 量化矩阵 quantcoeff = quantscale*16/coeff[stride*j/2+i/2],由于TU过大,而量化矩阵只有8x8,所以需要对TU长和宽进行2倍的下采样,来匹配8x8的量化矩阵
// 如果width==32, ratio = 4, 量化矩阵 quantcoeff = quantscale*16/coeff[stride*j/4+i/4],对TU长和宽进行4倍的下采样,来匹配8x8的量化矩阵
for (int j = 0; j < height; j++)
for (int i = 0; i < width; i++)
quantcoeff[j * width + i] = quantScales / coeff[stride * (j / ratio) + i / ratio];
if (ratio > 1) // 对于ratio>1,也就是TU尺寸大于量化矩阵的情况,可以对DC量化系数进行单独的设置
quantcoeff[0] = quantScales / dc; // = quantscale*16/ dc
}
/** 函数功能 : 生成非均匀反量化矩阵
** 调用范围 : 仅在ScalingList::setupQuantMatrices()中被调用
* \参数 coeff : 输入的非均匀量化矩阵
* \参数 dequantcoeff : 输出的新建立的非均匀反量化矩阵
* \参数 invQuantScales : 默认的均匀反量化系数
* \参数 height : TU的高度
* \参数 width : TU的宽度
* \参数 ratio : TU尺寸与量化矩阵尺寸的比例 = TU width / min(8, TU width). 假如TU width<=8, 则ratio = 1; 假如TU height>8, ratio = TU width/8
* \参数 stride : TU的步长, = width
* \参数 dc : 输入的非均匀DC量化系数
*/
void ScalingList::processScalingListDec(int32_t *coeff, int32_t *dequantcoeff, int32_t invQuantScales, int height, int width,
int ratio, int stride, int32_t dc)
{
// 如果width<= 8, ratio = 1, 反量化矩阵 dequantcoeff = invQuantScales * coeff[stride*j+i]
// 如果width==16, ratio = 2, 反量化矩阵 dequantcoeff = invQuantScales * coeff[stride*j/2+i/2],由于TU过大,而量化矩阵只有8x8,所以需要对TU长和宽进行2倍的下采样,来匹配8x8的量化矩阵
// 如果width==32, ratio = 4, 反量化矩阵 dequantcoeff = invQuantScales * coeff[stride*j/4+i/4],对TU长和宽进行4倍的下采样,来匹配8x8的量化矩阵
for (int j = 0; j < height; j++)
for (int i = 0; i < width; i++)
dequantcoeff[j * width + i] = invQuantScales * coeff[stride * (j / ratio) + i / ratio];
if (ratio > 1) // 对于ratio>1,也就是TU尺寸大于量化矩阵的情况,可以对DC反量化系数进行单独的设置
dequantcoeff[0] = invQuantScales * dc;
}
}
本文详细介绍了HEVC编码中量化矩阵的解析过程及如何根据不同变换块大小和量化参数生成非均匀量化矩阵,包括矩阵类型定义、默认量化表初始化、文件解析逻辑和量化矩阵的设置。
2397

被折叠的 条评论
为什么被折叠?



