x265-1.8版本-common/scalinglist.cpp注释

来源:互联网 发布:sqlalchemy sql语句 编辑:程序博客网 时间:2024/06/03 02:26

注:问号以及未注释部分 会在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. *****************************************************************************/#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",    },};//DC量化矩阵文件中的矩阵类型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",    },};//4x4TU的量化矩阵 应用于4x4intraYUV、4x4inter YUVstatic int quantTSDefault4x4[16] ={    16, 16, 16, 16,    16, 16, 16, 16,    16, 16, 16, 16,    16, 16, 16, 16};//8x8TU的量化矩阵 应用于8x8intraYUV 16x16intraYUV 32x32intraYstatic 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};//8x8TU的量化矩阵 应用于8x8interYUV 16x16interYUV 32x32intraUV 32x32interYUVstatic 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_NS {// private namespaceconst 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)/** 函数功能         : 构造函数,初始化为0/*  调用范围         : 随encoder类创建而构造*/ScalingList::ScalingList(){    memset(m_quantCoef, 0, sizeof(m_quantCoef));    memset(m_dequantCoef, 0, sizeof(m_dequantCoef));    memset(m_scalingListCoef, 0, sizeof(m_scalingListCoef));}/** 函数功能         :为量化参数申请空间,申请成功返回true 否则返回false*                      前向量化表"m_quantCoef"/反量化表"m_dequantCoef"/量化矩阵表"m_scalingListCoef"/*  调用范围         : 只在Encoder::create()函数中被调用* \返回值 ok         : 初始化成功则返回true,否则返回false*/bool ScalingList::init(){    bool ok = true;    for (int sizeId = 0; sizeId < NUM_SIZES; sizeId++)//遍历4x4, 8x8, 16x16, 32x32    {        for (int listId = 0; listId < NUM_LISTS; listId++)//遍历 intra: Y U V  inter Y U V        {            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;//返回申请空间是否成功}/** 函数功能         : 析构函数,释放内存/*  调用范围         : 随encoder类析构而析构*/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 *//** 函数功能        : 判断读取的量化矩阵是否与默认量化矩阵相同(相同false 不同 true) ** 调用范围        : 仅在ScalingList::parseScalingList中被调用 ** 返回值          :  相同false 不同 true*/bool ScalingList::checkDefaultScalingList() const{    int defaultCounter = 0;    for (int s = 0; s < NUM_SIZES; s++)//遍历所有的块大小个数:4x4, 8x8, 16x16, 32x32        for (int l = 0; l < NUM_LISTS; l++)//遍历所有类型 intra: Y U V  inter Y U V            if (!memcmp(m_scalingListCoef[s][l], getScalingListDefaultAddress(s, l),//比较内存区域buf1和buf2的前count个字节 如果相同计数                        sizeof(int32_t) * X265_MIN(MAX_MATRIX_COEF_NUM, s_numCoefPerSize[s])) &&                ((s < BLOCK_16x16) || (m_scalingListDC[s][l] == 16)))//查看DC量化是否为16 计数1                defaultCounter++;//相同计数1    return defaultCounter != (NUM_LISTS * NUM_SIZES - 4); //查看是否读取的量化矩阵与默认值全部相同  因为32x32只有2个所以需要减去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_scalingListCoef,用于之后的量化    m_bEnabled = true; // 使能量化矩阵    m_bDataPresent = false; // 使用默认的量化矩阵}/** 函数功能        : 解析用户自定义的量化矩阵文件 ** 调用范围        : 仅在Encoder::create()中被调用 ** 返回值          :  失败返回true 成功返回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++)//遍历所有的块大小个数:4x4, 8x8, 16x16, 32x32    {        int size = X265_MIN(MAX_MATRIX_COEF_NUM, s_numCoefPerSize[sizeIdc]);//每个量化矩阵的最大系数个数        for (int listIdc = 0; listIdc < NUM_LISTS; listIdc++)//遍历所有类型 intra: Y U V  inter Y U V  number of quantization matrix lists (YUV * inter/intra)        {            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];//设置DC系数量化矩阵  取量化矩阵的第一个系数            if (sizeIdc > BLOCK_8x8)//如果块大小大于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;//设置DC系数量化矩阵            }        }    }    fclose(fp);//关闭文件    m_bEnabled = true;//标记使用量化矩阵    m_bDataPresent = !checkDefaultScalingList();//判断读取的量化矩阵是否与默认量化矩阵相同(相同false 不同 true) (此处似乎存在bug!!!! 前面不应该有!)    return false;//成功返回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;}}


 

1 0
原创粉丝点击