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

注:问号以及未注释部分 会在x265-1.9版本内更新

/*****************************************************************************
 * Copyright (C) 2013 x265 project
 *
 * Authors: Mandar Gurav 
 *          Deepthi Devaki Akkoorath 
 *          Mahesh Pittala 
 *          Rajesh Paulraj 
 *          Min Chen 
 *          Praveen Kumar Tiwari 
 *          Nabajit Deka 
 *
 * 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 "contexts.h"   // costCoeffNxN_c
#include "threading.h"  // CLZ

using namespace X265_NS;

#if _MSC_VER
#pragma warning(disable: 4127) // conditional expression is constant, typical for templated functions
#endif

// Fast DST Algorithm. Full matrix multiplication for DST and Fast DST algorithm
// give identical results
/** 函数功能       :对残差块进行4x4DST变换并对其转置
* \参数 block      :残差数据
* \参数 coeff      :存储行变换后的数据
* \参数 shift      :行变换移位个数
* \返回            :null
**/
static void fastForwardDst(const int16_t* block, int16_t* coeff, int shift)  // input block, output coeff
{
    //功能:行变换  X*DST'
    int c[4];
    int rnd_factor = 1 << (shift - 1);

    for (int i = 0; i < 4; i++)
    {
        // Intermediate Variables
        c[0] = block[4 * i + 0] + block[4 * i + 3];
        c[1] = block[4 * i + 1] + block[4 * i + 3];
        c[2] = block[4 * i + 0] - block[4 * i + 1];
        c[3] = 74 * block[4 * i + 2];

        coeff[i] =      (int16_t)((29 * c[0] + 55 * c[1]  + c[3] + rnd_factor) >> shift);
        coeff[4 + i] =  (int16_t)((74 * (block[4 * i + 0] + block[4 * i + 1] - block[4 * i + 3]) + rnd_factor) >> shift);
        coeff[8 + i] =  (int16_t)((29 * c[2] + 55 * c[0]  - c[3] + rnd_factor) >> shift);
        coeff[12 + i] = (int16_t)((55 * c[2] - 29 * c[1] + c[3] + rnd_factor) >> shift);
    }
}

static void inversedst(const int16_t* tmp, int16_t* block, int shift)  // input tmp, output block
{
    int i, c[4];
    int rnd_factor = 1 << (shift - 1);

    for (i = 0; i < 4; i++)
    {
        // Intermediate Variables
        c[0] = tmp[i] + tmp[8 + i];
        c[1] = tmp[8 + i] + tmp[12 + i];
        c[2] = tmp[i] - tmp[12 + i];
        c[3] = 74 * tmp[4 + i];

        block[4 * i + 0] = (int16_t)x265_clip3(-32768, 32767, (29 * c[0] + 55 * c[1]     + c[3]               + rnd_factor) >> shift);
        block[4 * i + 1] = (int16_t)x265_clip3(-32768, 32767, (55 * c[2] - 29 * c[1]     + c[3]               + rnd_factor) >> shift);
        block[4 * i + 2] = (int16_t)x265_clip3(-32768, 32767, (74 * (tmp[i] - tmp[8 + i]  + tmp[12 + i])      + rnd_factor) >> shift);
        block[4 * i + 3] = (int16_t)x265_clip3(-32768, 32767, (55 * c[0] + 29 * c[2]     - c[3]               + rnd_factor) >> shift);
    }
}
/** 函数功能       :对残差块进行16x16DCT变换并对其转置
* \参数 block      :残差数据
* \参数 coeff      :存储行变换后的数据
* \参数 shift      :行变换移位个数
* \返回            :null
**/
static void partialButterfly16(const int16_t* src, int16_t* dst, int shift, int line)
{
    //功能:行变换  X*DCT’
    int j, k;
    int E[8], O[8];
    int EE[4], EO[4];
    int EEE[2], EEO[2];
    int add = 1 << (shift - 1);

    for (j = 0; j < line; j++)
    {
        /* E and O */
        for (k = 0; k < 8; k++)
        {
            E[k] = src[k] + src[15 - k];
            O[k] = src[k] - src[15 - k];
        }

        /* EE and EO */
        for (k = 0; k < 4; k++)
        {
            EE[k] = E[k] + E[7 - k];
            EO[k] = E[k] - E[7 - k];
        }

        /* EEE and EEO */
        EEE[0] = EE[0] + EE[3];
        EEO[0] = EE[0] - EE[3];
        EEE[1] = EE[1] + EE[2];
        EEO[1] = EE[1] - EE[2];

        dst[0] = (int16_t)((g_t16[0][0] * EEE[0] + g_t16[0][1] * EEE[1] + add) >> shift);
        dst[8 * line] = (int16_t)((g_t16[8][0] * EEE[0] + g_t16[8][1] * EEE[1] + add) >> shift);
        dst[4 * line] = (int16_t)((g_t16[4][0] * EEO[0] + g_t16[4][1] * EEO[1] + add) >> shift);
        dst[12 * line] = (int16_t)((g_t16[12][0] * EEO[0] + g_t16[12][1] * EEO[1] + add) >> shift);

        for (k = 2; k < 16; k += 4)
        {
            dst[k * line] = (int16_t)((g_t16[k][0] * EO[0] + g_t16[k][1] * EO[1] + g_t16[k][2] * EO[2] +
                                       g_t16[k][3] * EO[3] + add) >> shift);
        }

        for (k = 1; k < 16; k += 2)
        {
            dst[k * line] =  (int16_t)((g_t16[k][0] * O[0] + g_t16[k][1] * O[1] + g_t16[k][2] * O[2] + g_t16[k][3] * O[3] +
                                        g_t16[k][4] * O[4] + g_t16[k][5] * O[5] + g_t16[k][6] * O[6] + g_t16[k][7] * O[7] +
                                        add) >> shift);
        }

        src += 16;
        dst++;
    }
}
/** 函数功能       :对残差块进行32x32DCT变换并对其转置
* \参数 block      :残差数据
* \参数 coeff      :存储行变换后的数据
* \参数 shift      :行变换移位个数
* \返回            :null
**/
static void partialButterfly32(const int16_t* src, int16_t* dst, int shift, int line)
{
    //功能:行变换  X*DCT’
    int j, k;
    int E[16], O[16];
    int EE[8], EO[8];
    int EEE[4], EEO[4];
    int EEEE[2], EEEO[2];
    int add = 1 << (shift - 1);

    for (j = 0; j < line; j++)
    {
        /* E and O*/
        for (k = 0; k < 16; k++)
        {
            E[k] = src[k] + src[31 - k];
            O[k] = src[k] - src[31 - k];
        }

        /* EE and EO */
        for (k = 0; k < 8; k++)
        {
            EE[k] = E[k] + E[15 - k];
            EO[k] = E[k] - E[15 - k];
        }

        /* EEE and EEO */
        for (k = 0; k < 4; k++)
        {
            EEE[k] = EE[k] + EE[7 - k];
            EEO[k] = EE[k] - EE[7 - k];
        }

        /* EEEE and EEEO */
        EEEE[0] = EEE[0] + EEE[3];
        EEEO[0] = EEE[0] - EEE[3];
        EEEE[1] = EEE[1] + EEE[2];
        EEEO[1] = EEE[1] - EEE[2];

        dst[0] = (int16_t)((g_t32[0][0] * EEEE[0] + g_t32[0][1] * EEEE[1] + add) >> shift);
        dst[16 * line] = (int16_t)((g_t32[16][0] * EEEE[0] + g_t32[16][1] * EEEE[1] + add) >> shift);
        dst[8 * line] = (int16_t)((g_t32[8][0] * EEEO[0] + g_t32[8][1] * EEEO[1] + add) >> shift);
        dst[24 * line] = (int16_t)((g_t32[24][0] * EEEO[0] + g_t32[24][1] * EEEO[1] + add) >> shift);
        for (k = 4; k < 32; k += 8)
        {
            dst[k * line] = (int16_t)((g_t32[k][0] * EEO[0] + g_t32[k][1] * EEO[1] + g_t32[k][2] * EEO[2] +
                                       g_t32[k][3] * EEO[3] + add) >> shift);
        }

        for (k = 2; k < 32; k += 4)
        {
            dst[k * line] = (int16_t)((g_t32[k][0] * EO[0] + g_t32[k][1] * EO[1] + g_t32[k][2] * EO[2] +
                                       g_t32[k][3] * EO[3] + g_t32[k][4] * EO[4] + g_t32[k][5] * EO[5] +
                                       g_t32[k][6] * EO[6] + g_t32[k][7] * EO[7] + add) >> shift);
        }

        for (k = 1; k < 32; k += 2)
        {
            dst[k * line] = (int16_t)((g_t32[k][0] * O[0] + g_t32[k][1] * O[1] + g_t32[k][2] * O[2] + g_t32[k][3] * O[3] +
                                       g_t32[k][4] * O[4] + g_t32[k][5] * O[5] + g_t32[k][6] * O[6] + g_t32[k][7] * O[7] +
                                       g_t32[k][8] * O[8] + g_t32[k][9] * O[9] + g_t32[k][10] * O[10] + g_t32[k][11] *
                                       O[11] + g_t32[k][12] * O[12] + g_t32[k][13] * O[13] + g_t32[k][14] * O[14] +
                                       g_t32[k][15] * O[15] + add) >> shift);
        }

        src += 32;
        dst++;
    }
}
/** 函数功能       :对残差块进行8x8DCT变换并对其转置
* \参数 block      :残差数据
* \参数 coeff      :存储行变换后的数据
* \参数 shift      :行变换移位个数
* \返回            :null
**/
static void partialButterfly8(const int16_t* src, int16_t* dst, int shift, int line)
{
    //功能:X*DCT'
    int j, k;
    int E[4], O[4];
    int EE[2], EO[2];
    int add = 1 << (shift - 1);

    for (j = 0; j < line; j++)
    {
        /* E and O*/
        for (k = 0; k < 4; k++)
        {
            E[k] = src[k] + src[7 - k];
            O[k] = src[k] - src[7 - k];
        }

        /* EE and EO */
        EE[0] = E[0] + E[3];
        EO[0] = E[0] - E[3];
        EE[1] = E[1] + E[2];
        EO[1] = E[1] - E[2];

        dst[0] = (int16_t)((g_t8[0][0] * EE[0] + g_t8[0][1] * EE[1] + add) >> shift);
        dst[4 * line] = (int16_t)((g_t8[4][0] * EE[0] + g_t8[4][1] * EE[1] + add) >> shift);
        dst[2 * line] = (int16_t)((g_t8[2][0] * EO[0] + g_t8[2][1] * EO[1] + add) >> shift);
        dst[6 * line] = (int16_t)((g_t8[6][0] * EO[0] + g_t8[6][1] * EO[1] + add) >> shift);

        dst[line] = (int16_t)((g_t8[1][0] * O[0] + g_t8[1][1] * O[1] + g_t8[1][2] * O[2] + g_t8[1][3] * O[3] + add) >> shift);
        dst[3 * line] = (int16_t)((g_t8[3][0] * O[0] + g_t8[3][1] * O[1] + g_t8[3][2] * O[2] + g_t8[3][3] * O[3] + add) >> shift);
        dst[5 * line] = (int16_t)((g_t8[5][0] * O[0] + g_t8[5][1] * O[1] + g_t8[5][2] * O[2] + g_t8[5][3] * O[3] + add) >> shift);
        dst[7 * line] = (int16_t)((g_t8[7][0] * O[0] + g_t8[7][1] * O[1] + g_t8[7][2] * O[2] + g_t8[7][3] * O[3] + add) >> shift);

        src += 8;
        dst++;
    }
}

static void partialButterflyInverse4(const int16_t* src, int16_t* dst, int shift, int line)
{
    int j;
    int E[2], O[2];
    int add = 1 << (shift - 1);

    for (j = 0; j < line; j++)
    {
        /* Utilizing symmetry properties to the maximum to minimize the number of multiplications */
        O[0] = g_t4[1][0] * src[line] + g_t4[3][0] * src[3 * line];
        O[1] = g_t4[1][1] * src[line] + g_t4[3][1] * src[3 * line];
        E[0] = g_t4[0][0] * src[0] + g_t4[2][0] * src[2 * line];
        E[1] = g_t4[0][1] * src[0] + g_t4[2][1] * src[2 * line];

        /* Combining even and odd terms at each hierarchy levels to calculate the final spatial domain vector */
        dst[0] = (int16_t)(x265_clip3(-32768, 32767, (E[0] + O[0] + add) >> shift));
        dst[1] = (int16_t)(x265_clip3(-32768, 32767, (E[1] + O[1] + add) >> shift));
        dst[2] = (int16_t)(x265_clip3(-32768, 32767, (E[1] - O[1] + add) >> shift));
        dst[3] = (int16_t)(x265_clip3(-32768, 32767, (E[0] - O[0] + add) >> shift));

        src++;
        dst += 4;
    }
}

static void partialButterflyInverse8(const int16_t* src, int16_t* dst, int shift, int line)
{
    int j, k;
    int E[4], O[4];
    int EE[2], EO[2];
    int add = 1 << (shift - 1);

    for (j = 0; j < line; j++)
    {
        /* Utilizing symmetry properties to the maximum to minimize the number of multiplications */
        for (k = 0; k < 4; k++)
        {
            O[k] = g_t8[1][k] * src[line] + g_t8[3][k] * src[3 * line] + g_t8[5][k] * src[5 * line] + g_t8[7][k] * src[7 * line];
        }

        EO[0] = g_t8[2][0] * src[2 * line] + g_t8[6][0] * src[6 * line];
        EO[1] = g_t8[2][1] * src[2 * line] + g_t8[6][1] * src[6 * line];
        EE[0] = g_t8[0][0] * src[0] + g_t8[4][0] * src[4 * line];
        EE[1] = g_t8[0][1] * src[0] + g_t8[4][1] * src[4 * line];

        /* Combining even and odd terms at each hierarchy levels to calculate the final spatial domain vector */
        E[0] = EE[0] + EO[0];
        E[3] = EE[0] - EO[0];
        E[1] = EE[1] + EO[1];
        E[2] = EE[1] - EO[1];
        for (k = 0; k < 4; k++)
        {
            dst[k] = (int16_t)x265_clip3(-32768, 32767, (E[k] + O[k] + add) >> shift);
            dst[k + 4] = (int16_t)x265_clip3(-32768, 32767, (E[3 - k] - O[3 - k] + add) >> shift);
        }

        src++;
        dst += 8;
    }
}

static void partialButterflyInverse16(const int16_t* src, int16_t* dst, int shift, int line)
{
    int j, k;
    int E[8], O[8];
    int EE[4], EO[4];
    int EEE[2], EEO[2];
    int add = 1 << (shift - 1);

    for (j = 0; j < line; j++)
    {
        /* Utilizing symmetry properties to the maximum to minimize the number of multiplications */
        for (k = 0; k < 8; k++)
        {
            O[k] = g_t16[1][k] * src[line] + g_t16[3][k] * src[3 * line] + g_t16[5][k] * src[5 * line] + g_t16[7][k] * src[7 * line] +
                g_t16[9][k] * src[9 * line] + g_t16[11][k] * src[11 * line] + g_t16[13][k] * src[13 * line] + g_t16[15][k] * src[15 * line];
        }

        for (k = 0; k < 4; k++)
        {
            EO[k] = g_t16[2][k] * src[2 * line] + g_t16[6][k] * src[6 * line] + g_t16[10][k] * src[10 * line] + g_t16[14][k] * src[14 * line];
        }

        EEO[0] = g_t16[4][0] * src[4 * line] + g_t16[12][0] * src[12 * line];
        EEE[0] = g_t16[0][0] * src[0] + g_t16[8][0] * src[8 * line];
        EEO[1] = g_t16[4][1] * src[4 * line] + g_t16[12][1] * src[12 * line];
        EEE[1] = g_t16[0][1] * src[0] + g_t16[8][1] * src[8 * line];

        /* Combining even and odd terms at each hierarchy levels to calculate the final spatial domain vector */
        for (k = 0; k < 2; k++)
        {
            EE[k] = EEE[k] + EEO[k];
            EE[k + 2] = EEE[1 - k] - EEO[1 - k];
        }

        for (k = 0; k < 4; k++)
        {
            E[k] = EE[k] + EO[k];
            E[k + 4] = EE[3 - k] - EO[3 - k];
        }

        for (k = 0; k < 8; k++)
        {
            dst[k]   = (int16_t)x265_clip3(-32768, 32767, (E[k] + O[k] + add) >> shift);
            dst[k + 8] = (int16_t)x265_clip3(-32768, 32767, (E[7 - k] - O[7 - k] + add) >> shift);
        }

        src++;
        dst += 16;
    }
}

static void partialButterflyInverse32(const int16_t* src, int16_t* dst, int shift, int line)
{
    int j, k;
    int E[16], O[16];
    int EE[8], EO[8];
    int EEE[4], EEO[4];
    int EEEE[2], EEEO[2];
    int add = 1 << (shift - 1);

    for (j = 0; j < line; j++)
    {
        /* Utilizing symmetry properties to the maximum to minimize the number of multiplications */
        for (k = 0; k < 16; k++)
        {
            O[k] = g_t32[1][k] * src[line] + g_t32[3][k] * src[3 * line] + g_t32[5][k] * src[5 * line] + g_t32[7][k] * src[7 * line] +
                g_t32[9][k] * src[9 * line] + g_t32[11][k] * src[11 * line] + g_t32[13][k] * src[13 * line] + g_t32[15][k] * src[15 * line] +
                g_t32[17][k] * src[17 * line] + g_t32[19][k] * src[19 * line] + g_t32[21][k] * src[21 * line] + g_t32[23][k] * src[23 * line] +
                g_t32[25][k] * src[25 * line] + g_t32[27][k] * src[27 * line] + g_t32[29][k] * src[29 * line] + g_t32[31][k] * src[31 * line];
        }

        for (k = 0; k < 8; k++)
        {
            EO[k] = g_t32[2][k] * src[2 * line] + g_t32[6][k] * src[6 * line] + g_t32[10][k] * src[10 * line] + g_t32[14][k] * src[14 * line] +
                g_t32[18][k] * src[18 * line] + g_t32[22][k] * src[22 * line] + g_t32[26][k] * src[26 * line] + g_t32[30][k] * src[30 * line];
        }

        for (k = 0; k < 4; k++)
        {
            EEO[k] = g_t32[4][k] * src[4 * line] + g_t32[12][k] * src[12 * line] + g_t32[20][k] * src[20 * line] + g_t32[28][k] * src[28 * line];
        }

        EEEO[0] = g_t32[8][0] * src[8 * line] + g_t32[24][0] * src[24 * line];
        EEEO[1] = g_t32[8][1] * src[8 * line] + g_t32[24][1] * src[24 * line];
        EEEE[0] = g_t32[0][0] * src[0] + g_t32[16][0] * src[16 * line];
        EEEE[1] = g_t32[0][1] * src[0] + g_t32[16][1] * src[16 * line];

        /* Combining even and odd terms at each hierarchy levels to calculate the final spatial domain vector */
        EEE[0] = EEEE[0] + EEEO[0];
        EEE[3] = EEEE[0] - EEEO[0];
        EEE[1] = EEEE[1] + EEEO[1];
        EEE[2] = EEEE[1] - EEEO[1];
        for (k = 0; k < 4; k++)
        {
            EE[k] = EEE[k] + EEO[k];
            EE[k + 4] = EEE[3 - k] - EEO[3 - k];
        }

        for (k = 0; k < 8; k++)
        {
            E[k] = EE[k] + EO[k];
            E[k + 8] = EE[7 - k] - EO[7 - k];
        }

        for (k = 0; k < 16; k++)
        {
            dst[k] = (int16_t)x265_clip3(-32768, 32767, (E[k] + O[k] + add) >> shift);
            dst[k + 16] = (int16_t)x265_clip3(-32768, 32767, (E[15 - k] - O[15 - k] + add) >> shift);
        }

        src++;
        dst += 32;
    }
}
/** 函数功能       :对残差块进行DCT变换并对其转置
* \参数 block      :残差数据
* \参数 coeff      :存储行变换后的数据
* \参数 shift      :行变换移位个数
* \返回            :null
**/
static void partialButterfly4(const int16_t* src, int16_t* dst, int shift, int line)
{
    //功能: X*DCT'
    int j;
    int E[2], O[2];
    int add = 1 << (shift - 1);

    for (j = 0; j < line; j++)
    {
        /* E and O */
        E[0] = src[0] + src[3];
        O[0] = src[0] - src[3];
        E[1] = src[1] + src[2];
        O[1] = src[1] - src[2];

        dst[0] = (int16_t)((g_t4[0][0] * E[0] + g_t4[0][1] * E[1] + add) >> shift);
        dst[2 * line] = (int16_t)((g_t4[2][0] * E[0] + g_t4[2][1] * E[1] + add) >> shift);
        dst[line] = (int16_t)((g_t4[1][0] * O[0] + g_t4[1][1] * O[1] + add) >> shift);
        dst[3 * line] = (int16_t)((g_t4[3][0] * O[0] + g_t4[3][1] * O[1] + add) >> shift);

        src += 4;
        dst++;
    }
}
/** 函数功能       :对残差系数作4x4DST变换
* \参数 src        :残差数据
* \参数 dst        :DST变换后存储地址(空间大小为4x4)
* \参数 srcStride  :残差块步长
* \返回            :null
**/
static void dst4_c(const int16_t* src, int16_t* dst, intptr_t srcStride)
{
    const int shift_1st = 1 + X265_DEPTH - 8;//行变换移位个数:1
    const int shift_2nd = 8;//列变换移位个数:8

    ALIGN_VAR_32(int16_t, coef[4 * 4]);//4x4空间用于存储行变换后的数据
    ALIGN_VAR_32(int16_t, block[4 * 4]);//4x4空间用于存储残差块数据

    for (int i = 0; i < 4; i++)
    {
        memcpy(&block[i * 4], &src[i * srcStride], 4 * sizeof(int16_t));//将残差块数据存储到block中
    }
    /*
    测试样例:
    当前残差 block
    -46  -39  -42  -42 
    -43  -39  -29  -27 
    -32  -31  -31  -28 
    -22  -20  -21  -17 
    **/
    fastForwardDst(block, coef, shift_1st);//对残差块进行变换并对其转置
    /*
    行变换后并移位后的数据
    -5057  -3903  -3639  -2360 
    -1591  -2035  -1295  -925 
    -967   -910   -517   -324 
    -572   -226   -319   -295
    DST =
    29  55  74  84
    74  74   0 -74
    84 -29 -74  55
    55 -84  74 -29
    X*DST' (X为前面的残差,DST'为DST的转置)
    =X* 29  55  74  84
        74  74   0 -74
        84 -29 -74  55
        55 -84  74 -29
    =
    -10115       -3182       -1935       -1144
    -7806       -4070       -1820        -452
    -7279       -2590       -1035        -638
    -4720       -1850        -649        -591
    对其移1位(四舍五入)
    -5057       -1591        -967        -572
    -3903       -2035        -910        -226
    -3639       -1295        -517        -319
    -2360        -925        -324        -295
    对其转置 为了重新复用fastForwardDst函数
    -5057  -3903  -3639  -2360 
    -1591  -2035  -1295  -925 
    -967   -910   -517   -324 
    -572   -226   -319   -295
    **/
    fastForwardDst(coef, dst, shift_2nd);//对残差块进行变换并对其转置(因为已经将其转置 在这里实质上做的是列变换)
    /*
    coeff=
    -5057  -3903  -3639  -2360 
    -1591  -2035  -1295  -925 
    -967   -910   -517   -324 
    -572   -226   -319   -295
    (coeff*DST’)'=
    -3238  -1295   -561   -302 
    -1908   -781   -449   -145 
    -672    -116   -134   -133 
    -590     56    -22    -108 
    最终结果 DST*X*DST’/512 右移9位(1+8)
    **/
}
/** 函数功能       :对残差系数作4x4DCT变换
* \参数 src        :残差数据
* \参数 dst        :DCT变换后存储地址(空间大小为4x4)
* \参数 srcStride  :残差块步长
* \返回            :null
**/
static void dct4_c(const int16_t* src, int16_t* dst, intptr_t srcStride)
{
    const int shift_1st = 1 + X265_DEPTH - 8;//行变换移位个数:1
    const int shift_2nd = 8;//列变换移位个数:8

    ALIGN_VAR_32(int16_t, coef[4 * 4]);//4x4空间用于存储行变换后的数据
    ALIGN_VAR_32(int16_t, block[4 * 4]);//4x4空间用于存储残差块数据

    for (int i = 0; i < 4; i++)
    {
        memcpy(&block[i * 4], &src[i * srcStride], 4 * sizeof(int16_t));//将残差块数据存储到block中
    }
    /*
    测试样例:
    当前残差 block
    -14  -17  -18  -20 
    -20  -18  -18  -19 
    -23  -25  -26  -25 
    -15  -19  -20  -19
    **/
    partialButterfly4(block, coef, shift_1st, 4);//对残差块进行变换并对其转置(因为已经将其转置 在这里实质上做的是列变换)
    /*
    行变换后并移位后的数据
    -2208  -2400  -3168  -2336 
    267    -41    101    184 
    32     -96     96    160 
    67     -18     -5     31 
    DCT =
    64   64   64   64  
    83   36  -36  -83  
    64  -64  -64   64  
    36  -83   83  -36  
    X*DCT'  (X为前面的残差,DCT'为DCT的转置)
    =X* 64   64   64   64
        83   36  -36  -83
        64  -64  -64   64
        36  -83   83  -36
    =
    -4416         534          64         133
    -4800         -83        -192         -36
    -6336         202         192         -11
    -4672         368         320          61
    对其移1位(四舍五入)
    -2208         267          32          67
    -2400         -41         -96         -18
    -3168         101          96          -5
    -2336         184         160          31
    对其转置 为了重新复用partialButterfly4函数
    -2208  -2400  -3168  -2336 
    267    -41    101    184 
    32     -96     96    160 
    67     -18     -5     31 
    **/
    partialButterfly4(coef, dst, shift_2nd, 4);//对残差块进行变换并对其转置(因为已经将其转置 在这里实质上做的是列变换)
    /*
    coeff=
    -2208  -2400  -3168  -2336 
    267    -41    101    184 
    32     -96     96    160 
    67     -18     -5     31 
    (coeff*DCT’)'=
    -2528    128     48     19 
      150      7    -68     10 
      256     98     48     30 
     -231     58     44      9 
    最终结果 DCT*X*DCT’/512 右移9位(1+8)
    **/
}
/** 函数功能       :对残差系数作8x8DCT变换 DCT*X*DCT’/2048
* \参数 src        :残差数据
* \参数 dst        :DCT变换后存储地址(空间大小为8x8)
* \参数 srcStride  :残差块步长
* \返回            :null
**/
static void dct8_c(const int16_t* src, int16_t* dst, intptr_t srcStride)
{
    const int shift_1st = 2 + X265_DEPTH - 8;//行变换移位个数:2
    const int shift_2nd = 9;//列变换移位个数:9

    ALIGN_VAR_32(int16_t, coef[8 * 8]);//8x8空间用于存储行变换后的数据
    ALIGN_VAR_32(int16_t, block[8 * 8]);//8x8空间用于存储残差块数据

    for (int i = 0; i < 8; i++)
    {
        memcpy(&block[i * 8], &src[i * srcStride], 8 * sizeof(int16_t));//将残差块数据存储到block中
    }
    /*
    DCT=
    { 64, 64, 64, 64, 64, 64, 64, 64},
    { 89, 75, 50, 18,-18,-50,-75,-89},
    { 83, 36,-36,-83,-83,-36, 36, 83},
    { 75,-18,-89,-50, 50, 89, 18,-75},
    { 64,-64,-64, 64, 64,-64,-64, 64},
    { 50,-89, 18, 75,-75,-18, 89,-50},
    { 36,-83, 83,-36,-36, 83,-83, 36},
    { 18,-50, 75,-89, 89,-75, 50,-18}
    **/
    partialButterfly8(block, coef, shift_1st, 8);//对残差块进行变换并对其转置(因为已经将其转置 在这里实质上做的是列变换)
    partialButterfly8(coef, dst, shift_2nd, 8);//对残差块进行变换并对其转置(因为已经将其转置 在这里实质上做的是列变换)
}
/** 函数功能       :对残差系数作16x16DCT变换 DCT*X*DCT’/8192
* \参数 src        :残差数据
* \参数 dst        :DCT变换后存储地址(空间大小为16x16)
* \参数 srcStride  :残差块步长
* \返回            :null
**/
static void dct16_c(const int16_t* src, int16_t* dst, intptr_t srcStride)
{
    const int shift_1st = 3 + X265_DEPTH - 8;//行变换移位个数:3
    const int shift_2nd = 10;//列变换移位个数:10

    ALIGN_VAR_32(int16_t, coef[16 * 16]);//16x16空间用于存储行变换后的数据
    ALIGN_VAR_32(int16_t, block[16 * 16]);//16x16空间用于存储残差块数据

    for (int i = 0; i < 16; i++)
    {
        memcpy(&block[i * 16], &src[i * srcStride], 16 * sizeof(int16_t));//将残差块数据存储到block中
    }
    /*
    { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64},
    { 90, 87, 80, 70, 57, 43, 25,  9, -9,-25,-43,-57,-70,-80,-87,-90},
    { 89, 75, 50, 18,-18,-50,-75,-89,-89,-75,-50,-18, 18, 50, 75, 89},
    { 87, 57,  9,-43,-80,-90,-70,-25, 25, 70, 90, 80, 43, -9,-57,-87},
    { 83, 36,-36,-83,-83,-36, 36, 83, 83, 36,-36,-83,-83,-36, 36, 83},
    { 80,  9,-70,-87,-25, 57, 90, 43,-43,-90,-57, 25, 87, 70, -9,-80},
    { 75,-18,-89,-50, 50, 89, 18,-75,-75, 18, 89, 50,-50,-89,-18, 75},
    { 70,-43,-87,  9, 90, 25,-80,-57, 57, 80,-25,-90, -9, 87, 43,-70},
    { 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64},
    { 57,-80,-25, 90, -9,-87, 43, 70,-70,-43, 87,  9,-90, 25, 80,-57},
    { 50,-89, 18, 75,-75,-18, 89,-50,-50, 89,-18,-75, 75, 18,-89, 50},
    { 43,-90, 57, 25,-87, 70,  9,-80, 80, -9,-70, 87,-25,-57, 90,-43},
    { 36,-83, 83,-36,-36, 83,-83, 36, 36,-83, 83,-36,-36, 83,-83, 36},
    { 25,-70, 90,-80, 43,  9,-57, 87,-87, 57, -9,-43, 80,-90, 70,-25},
    { 18,-50, 75,-89, 89,-75, 50,-18,-18, 50,-75, 89,-89, 75,-50, 18},
    {  9,-25, 43,-57, 70,-80, 87,-90, 90,-87, 80,-70, 57,-43, 25, -9}
    **/
    partialButterfly16(block, coef, shift_1st, 16);//对残差块进行变换并对其转置(因为已经将其转置 在这里实质上做的是列变换)
    partialButterfly16(coef, dst, shift_2nd, 16);//对残差块进行变换并对其转置(因为已经将其转置 在这里实质上做的是列变换)
}
/** 函数功能       :对残差系数作32x32DCT变换 DCT*X*DCT’/32768
* \参数 src        :残差数据
* \参数 dst        :DCT变换后存储地址(空间大小为32x32)
* \参数 srcStride  :残差块步长
* \返回            :null
**/
static void dct32_c(const int16_t* src, int16_t* dst, intptr_t srcStride)
{
    const int shift_1st = 4 + X265_DEPTH - 8;//行变换移位个数:4
    const int shift_2nd = 11;//列变换移位个数:11

    ALIGN_VAR_32(int16_t, coef[32 * 32]);//32x32空间用于存储行变换后的数据
    ALIGN_VAR_32(int16_t, block[32 * 32]);//32x32空间用于存储残差块数据

    for (int i = 0; i < 32; i++)
    {
        memcpy(&block[i * 32], &src[i * srcStride], 32 * sizeof(int16_t));//将残差块数据存储到block中
    }

    /*
    { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64},
    { 90, 90, 88, 85, 82, 78, 73, 67, 61, 54, 46, 38, 31, 22, 13,  4, -4,-13,-22,-31,-38,-46,-54,-61,-67,-73,-78,-82,-85,-88,-90,-90},
    { 90, 87, 80, 70, 57, 43, 25,  9, -9,-25,-43,-57,-70,-80,-87,-90,-90,-87,-80,-70,-57,-43,-25, -9,  9, 25, 43, 57, 70, 80, 87, 90},
    { 90, 82, 67, 46, 22, -4,-31,-54,-73,-85,-90,-88,-78,-61,-38,-13, 13, 38, 61, 78, 88, 90, 85, 73, 54, 31,  4,-22,-46,-67,-82,-90},
    { 89, 75, 50, 18,-18,-50,-75,-89,-89,-75,-50,-18, 18, 50, 75, 89, 89, 75, 50, 18,-18,-50,-75,-89,-89,-75,-50,-18, 18, 50, 75, 89},
    { 88, 67, 31,-13,-54,-82,-90,-78,-46, -4, 38, 73, 90, 85, 61, 22,-22,-61,-85,-90,-73,-38,  4, 46, 78, 90, 82, 54, 13,-31,-67,-88},
    { 87, 57,  9,-43,-80,-90,-70,-25, 25, 70, 90, 80, 43, -9,-57,-87,-87,-57, -9, 43, 80, 90, 70, 25,-25,-70,-90,-80,-43,  9, 57, 87},
    { 85, 46,-13,-67,-90,-73,-22, 38, 82, 88, 54, -4,-61,-90,-78,-31, 31, 78, 90, 61,  4,-54,-88,-82,-38, 22, 73, 90, 67, 13,-46,-85},
    { 83, 36,-36,-83,-83,-36, 36, 83, 83, 36,-36,-83,-83,-36, 36, 83, 83, 36,-36,-83,-83,-36, 36, 83, 83, 36,-36,-83,-83,-36, 36, 83},
    { 82, 22,-54,-90,-61, 13, 78, 85, 31,-46,-90,-67,  4, 73, 88, 38,-38,-88,-73, -4, 67, 90, 46,-31,-85,-78,-13, 61, 90, 54,-22,-82},
    { 80,  9,-70,-87,-25, 57, 90, 43,-43,-90,-57, 25, 87, 70, -9,-80,-80, -9, 70, 87, 25,-57,-90,-43, 43, 90, 57,-25,-87,-70,  9, 80},
    { 78, -4,-82,-73, 13, 85, 67,-22,-88,-61, 31, 90, 54,-38,-90,-46, 46, 90, 38,-54,-90,-31, 61, 88, 22,-67,-85,-13, 73, 82,  4,-78},
    { 75,-18,-89,-50, 50, 89, 18,-75,-75, 18, 89, 50,-50,-89,-18, 75, 75,-18,-89,-50, 50, 89, 18,-75,-75, 18, 89, 50,-50,-89,-18, 75},
    { 73,-31,-90,-22, 78, 67,-38,-90,-13, 82, 61,-46,-88, -4, 85, 54,-54,-85,  4, 88, 46,-61,-82, 13, 90, 38,-67,-78, 22, 90, 31,-73},
    { 70,-43,-87,  9, 90, 25,-80,-57, 57, 80,-25,-90, -9, 87, 43,-70,-70, 43, 87, -9,-90,-25, 80, 57,-57,-80, 25, 90,  9,-87,-43, 70},
    { 67,-54,-78, 38, 85,-22,-90,  4, 90, 13,-88,-31, 82, 46,-73,-61, 61, 73,-46,-82, 31, 88,-13,-90, -4, 90, 22,-85,-38, 78, 54,-67},
    { 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64, 64,-64,-64, 64},
    { 61,-73,-46, 82, 31,-88,-13, 90, -4,-90, 22, 85,-38,-78, 54, 67,-67,-54, 78, 38,-85,-22, 90,  4,-90, 13, 88,-31,-82, 46, 73,-61},
    { 57,-80,-25, 90, -9,-87, 43, 70,-70,-43, 87,  9,-90, 25, 80,-57,-57, 80, 25,-90,  9, 87,-43,-70, 70, 43,-87, -9, 90,-25,-80, 57},
    { 54,-85, -4, 88,-46,-61, 82, 13,-90, 38, 67,-78,-22, 90,-31,-73, 73, 31,-90, 22, 78,-67,-38, 90,-13,-82, 61, 46,-88,  4, 85,-54},
    { 50,-89, 18, 75,-75,-18, 89,-50,-50, 89,-18,-75, 75, 18,-89, 50, 50,-89, 18, 75,-75,-18, 89,-50,-50, 89,-18,-75, 75, 18,-89, 50},
    { 46,-90, 38, 54,-90, 31, 61,-88, 22, 67,-85, 13, 73,-82,  4, 78,-78, -4, 82,-73,-13, 85,-67,-22, 88,-61,-31, 90,-54,-38, 90,-46},
    { 43,-90, 57, 25,-87, 70,  9,-80, 80, -9,-70, 87,-25,-57, 90,-43,-43, 90,-57,-25, 87,-70, -9, 80,-80,  9, 70,-87, 25, 57,-90, 43},
    { 38,-88, 73, -4,-67, 90,-46,-31, 85,-78, 13, 61,-90, 54, 22,-82, 82,-22,-54, 90,-61,-13, 78,-85, 31, 46,-90, 67,  4,-73, 88,-38},
    { 36,-83, 83,-36,-36, 83,-83, 36, 36,-83, 83,-36,-36, 83,-83, 36, 36,-83, 83,-36,-36, 83,-83, 36, 36,-83, 83,-36,-36, 83,-83, 36},
    { 31,-78, 90,-61,  4, 54,-88, 82,-38,-22, 73,-90, 67,-13,-46, 85,-85, 46, 13,-67, 90,-73, 22, 38,-82, 88,-54, -4, 61,-90, 78,-31},
    { 25,-70, 90,-80, 43,  9,-57, 87,-87, 57, -9,-43, 80,-90, 70,-25,-25, 70,-90, 80,-43, -9, 57,-87, 87,-57,  9, 43,-80, 90,-70, 25},
    { 22,-61, 85,-90, 73,-38, -4, 46,-78, 90,-82, 54,-13,-31, 67,-88, 88,-67, 31, 13,-54, 82,-90, 78,-46,  4, 38,-73, 90,-85, 61,-22},
    { 18,-50, 75,-89, 89,-75, 50,-18,-18, 50,-75, 89,-89, 75,-50, 18, 18,-50, 75,-89, 89,-75, 50,-18,-18, 50,-75, 89,-89, 75,-50, 18},
    { 13,-38, 61,-78, 88,-90, 85,-73, 54,-31,  4, 22,-46, 67,-82, 90,-90, 82,-67, 46,-22, -4, 31,-54, 73,-85, 90,-88, 78,-61, 38,-13},
    {  9,-25, 43,-57, 70,-80, 87,-90, 90,-87, 80,-70, 57,-43, 25, -9, -9, 25,-43, 57,-70, 80,-87, 90,-90, 87,-80, 70,-57, 43,-25,  9},
    {  4,-13, 22,-31, 38,-46, 54,-61, 67,-73, 78,-82, 85,-88, 90,-90, 90,-90, 88,-85, 82,-78, 73,-67, 61,-54, 46,-38, 31,-22, 13, -4}
    **/
    partialButterfly32(block, coef, shift_1st, 32);//对残差块进行变换并对其转置(因为已经将其转置 在这里实质上做的是列变换)
    partialButterfly32(coef, dst, shift_2nd, 32);//对残差块进行变换并对其转置(因为已经将其转置 在这里实质上做的是列变换)
}

static void idst4_c(const int16_t* src, int16_t* dst, intptr_t dstStride)
{
    const int shift_1st = 7;
    const int shift_2nd = 12 - (X265_DEPTH - 8);

    ALIGN_VAR_32(int16_t, coef[4 * 4]);
    ALIGN_VAR_32(int16_t, block[4 * 4]);

    inversedst(src, coef, shift_1st); // Forward DST BY FAST ALGORITHM, block input, coef output
    inversedst(coef, block, shift_2nd); // Forward DST BY FAST ALGORITHM, coef input, coeff output

    for (int i = 0; i < 4; i++)
    {
        memcpy(&dst[i * dstStride], &block[i * 4], 4 * sizeof(int16_t));
    }
}

static void idct4_c(const int16_t* src, int16_t* dst, intptr_t dstStride)
{
    const int shift_1st = 7;
    const int shift_2nd = 12 - (X265_DEPTH - 8);

    ALIGN_VAR_32(int16_t, coef[4 * 4]);
    ALIGN_VAR_32(int16_t, block[4 * 4]);

    partialButterflyInverse4(src, coef, shift_1st, 4); // Forward DST BY FAST ALGORITHM, block input, coef output
    partialButterflyInverse4(coef, block, shift_2nd, 4); // Forward DST BY FAST ALGORITHM, coef input, coeff output

    for (int i = 0; i < 4; i++)
    {
        memcpy(&dst[i * dstStride], &block[i * 4], 4 * sizeof(int16_t));
    }
}

static void idct8_c(const int16_t* src, int16_t* dst, intptr_t dstStride)
{
    const int shift_1st = 7;
    const int shift_2nd = 12 - (X265_DEPTH - 8);

    ALIGN_VAR_32(int16_t, coef[8 * 8]);
    ALIGN_VAR_32(int16_t, block[8 * 8]);

    partialButterflyInverse8(src, coef, shift_1st, 8);
    partialButterflyInverse8(coef, block, shift_2nd, 8);

    for (int i = 0; i < 8; i++)
    {
        memcpy(&dst[i * dstStride], &block[i * 8], 8 * sizeof(int16_t));
    }
}

static void idct16_c(const int16_t* src, int16_t* dst, intptr_t dstStride)
{
    const int shift_1st = 7;
    const int shift_2nd = 12 - (X265_DEPTH - 8);

    ALIGN_VAR_32(int16_t, coef[16 * 16]);
    ALIGN_VAR_32(int16_t, block[16 * 16]);

    partialButterflyInverse16(src, coef, shift_1st, 16);
    partialButterflyInverse16(coef, block, shift_2nd, 16);

    for (int i = 0; i < 16; i++)
    {
        memcpy(&dst[i * dstStride], &block[i * 16], 16 * sizeof(int16_t));
    }
}

static void idct32_c(const int16_t* src, int16_t* dst, intptr_t dstStride)
{
    const int shift_1st = 7;
    const int shift_2nd = 12 - (X265_DEPTH - 8);

    ALIGN_VAR_32(int16_t, coef[32 * 32]);
    ALIGN_VAR_32(int16_t, block[32 * 32]);

    partialButterflyInverse32(src, coef, shift_1st, 32);
    partialButterflyInverse32(coef, block, shift_2nd, 32);

    for (int i = 0; i < 32; i++)
    {
        memcpy(&dst[i * dstStride], &block[i * 32], 32 * sizeof(int16_t));
    }
}

static void dequant_normal_c(const int16_t* quantCoef, int16_t* coef, int num, int scale, int shift)
{
#if HIGH_BIT_DEPTH
    X265_CHECK(scale < 32768 || ((scale & 3) == 0 && shift > (X265_DEPTH - 8)), "dequant invalid scale %d\n", scale);
#else
    // NOTE: maximum of scale is (72 * 256)
    X265_CHECK(scale < 32768, "dequant invalid scale %d\n", scale);
#endif
    X265_CHECK(num <= 32 * 32, "dequant num %d too large\n", num);
    X265_CHECK((num % 8) == 0, "dequant num %d not multiple of 8\n", num);
    X265_CHECK(shift <= 10, "shift too large %d\n", shift);
    X265_CHECK(((intptr_t)coef & 31) == 0, "dequant coef buffer not aligned\n");

    int add, coeffQ;

    add = 1 << (shift - 1);

    for (int n = 0; n < num; n++)
    {
        coeffQ = (quantCoef[n] * scale + add) >> shift;
        coef[n] = (int16_t)x265_clip3(-32768, 32767, coeffQ);
    }
}

static void dequant_scaling_c(const int16_t* quantCoef, const int32_t* deQuantCoef, int16_t* coef, int num, int per, int shift)
{
    X265_CHECK(num <= 32 * 32, "dequant num %d too large\n", num);

    int add, coeffQ;

    shift += 4;

    if (shift > per)
    {
        add = 1 << (shift - per - 1);

        for (int n = 0; n < num; n++)
        {
            coeffQ = ((quantCoef[n] * deQuantCoef[n]) + add) >> (shift - per);
            coef[n] = (int16_t)x265_clip3(-32768, 32767, coeffQ);
        }
    }
    else
    {
        for (int n = 0; n < num; n++)
        {
            coeffQ   = x265_clip3(-32768, 32767, quantCoef[n] * deQuantCoef[n]);
            coef[n] = (int16_t)x265_clip3(-32768, 32767, coeffQ << (per - shift));
        }
    }
}

/** 函数功能        : 常规量化,C语言版本
* \参数 coef        : 变换后的系数矩阵
* \参数 quantCoeff  : 前向量化表
* \参数 deltaU      : 存储每一个位置量化误差的buffer
* \参数 qCoef       : 输出的量化后的系数矩阵
* \参数 qBits       : 量化中需要右移的位数
* \参数 add         : 为了补偿量化中右移操作,右移操作前需要加上的补偿加数
* \参数 numCoeff    : 当前变换块中变换系数的个数
* \返回值 numSig    : 返回量化后非零系数的个数
*/
static uint32_t quant_c(const int16_t* coef, const int32_t* quantCoeff, int32_t* deltaU, int16_t* qCoef, int qBits, int add, int numCoeff)
{
    X265_CHECK(qBits >= 8, "qBits less than 8\n"); // 确保右移的位数大于8
    X265_CHECK((numCoeff % 16) == 0, "numCoeff must be multiple of 16\n"); // 确保系数个数是16的整数倍
    int qBits8 = qBits - 8; // 用于计算量化误差时右移的位数
    uint32_t numSig = 0;

    for (int blockpos = 0; blockpos < numCoeff; blockpos++)
    {
        int level = coef[blockpos]; // 得到一个变换后的系数
        int sign  = (level < 0 ? -1 : 1); // 判断符号

        int tmplevel = abs(level) * quantCoeff[blockpos]; // 变换系数的绝对值 * 前向量化值
        level = ((tmplevel + add) >> qBits); // 加上补偿加数后右移
        deltaU[blockpos] = ((tmplevel - (level << qBits)) >> qBits8); // 计算量化误差
        if (level) // 如果量化后的系数不为0,则非零系数个数加1
            ++numSig;
        level *= sign; // 恢复符号位
        qCoef[blockpos] = (int16_t)x265_clip3(-32768, 32767, level); // 进行clip操作,并存储在量化矩阵中。可以看出这里限定量化后的值是一个有符号16-bit的数(-2^15~2^15-1)
    }

    return numSig;//返回量化后非零系数的个数
}

/** 函数功能         : ?常规量化(与quant_c相比只是不用存储量化误差),C语言版本
 ** 调用范围         : 只在Quant::rdoQuant中被调用
* \参数 coef        : 变换后的系数矩阵
* \参数 quantCoeff  : 前向量化表
* \参数 qCoef       : 输出的量化后的系数矩阵
* \参数 qBits       : 量化中需要右移的位数
* \参数 add         : 为了补偿量化中右移操作,右移操作前需要加上的补偿加数
* \参数 numCoeff    : 当前变换块中变换系数的个数
* \返回值 numSig    : 返回量化后非零系数的个数
*/
static uint32_t nquant_c(const int16_t* coef, const int32_t* quantCoeff, int16_t* qCoef, int qBits, int add, int numCoeff)
{
    X265_CHECK((numCoeff % 16) == 0, "number of quant coeff is not multiple of 4x4\n"); // TU中的系数个数numCoeff需要是16的整数倍
    X265_CHECK((uint32_t)add < ((uint32_t)1 << qBits), "2 ^ qBits less than add\n"); // 加数需要小于右移的大小
    X265_CHECK(((intptr_t)quantCoeff & 31) == 0, "quantCoeff buffer not aligned\n"); // 前向量化表在内存中是对齐的

    uint32_t numSig = 0;

    for (int blockpos = 0; blockpos < numCoeff; blockpos++)
    {
        int level = coef[blockpos]; // 得到一个变换后的系数
        int sign  = (level < 0 ? -1 : 1); // 判断符号

        int tmplevel = abs(level) * quantCoeff[blockpos]; // 变换系数的绝对值 * 前向量化值
        level = ((tmplevel + add) >> qBits); // 加上补偿加数后右移
        if (level) // 如果量化后的系数不为0,则非零系数个数加1
            ++numSig;
        level *= sign; // 恢复符号位
        qCoef[blockpos] = (int16_t)x265_clip3(-32768, 32767, level); // 进行clip操作,并存储在量化矩阵中。可以看出这里限定量化后的值是一个有符号16-bit的数(-2^15~2^15-1)
    }

    return numSig;
}
template
int  count_nonzero_c(const int16_t* quantCoeff)
{
    X265_CHECK(((intptr_t)quantCoeff & 15) == 0, "quant buffer not aligned\n");
    int count = 0;
    int numCoeff = trSize * trSize;
    for (int i = 0; i < numCoeff; i++)
    {
        count += quantCoeff[i] != 0;
    }

    return count;
}

/** 函数功能       :拷贝残差块到变换系数块
* \参数 coeff      :输出的变换系数块
* \参数 residual   :输入的残差块
* \参数 resiStride :残差数据的步长
* \返回            :残差块中的非零系数的个数
**/
template
uint32_t copy_count(int16_t* coeff, const int16_t* residual, intptr_t resiStride)
{
    uint32_t numSig = 0;
    for (int k = 0; k < trSize; k++)
    {
        for (int j = 0; j < trSize; j++)
        {
            coeff[k * trSize + j] = residual[k * resiStride + j];
            numSig += (residual[k * resiStride + j] != 0);
        }
    }

    return numSig;//残差块中的非零系数的个数
}
/** 函数功能       :AC系数去噪:(AC系数的绝对值 - offset)*符号
* \参数 dctCoef    :输出的变换系数块
* \参数 resSum     :用于当前帧每个TU块中对应位置的绝对值和
* \参数 offset     :去噪偏移值
* \参数 numCoeff   :当前TU块的系数个数(一个TU块的全部系数包含零系数)
* \返回            :null
**/
static void denoiseDct_c(int16_t* dctCoef, uint32_t* resSum, const uint16_t* offset, int numCoeff)
{
    for (int i = 0; i < numCoeff; i++)//遍历所有dct系数
    {
        int level = dctCoef[i];//获取DCT系数值
        int sign = level >> 31;//获取符号位,整数为0,负数为-1
        level = (level + sign) ^ sign;//获取绝对值
        resSum[i] += level;//统计当前位置的系数绝对值和
        level -= offset[i];//(AC系数的绝对值 - offset)
        dctCoef[i] = (int16_t)(level < 0 ? 0 : (level ^ sign) - sign);//获取符号位
    }
}
/** 函数功能       :? 统计每个CG中的非零系数的符号、非零系数的标记(是否是非零系数)、非零系数个数,以及最后一个非零系数的位置
* \参数 scan       :编码参数扫描顺序表
* \参数 coeff      :经过常规量化后的系数
* \参数 coeffSign  :每一个CG中非零系数的符号,低位存储CG前面的非零系数,高位存储CG后面的非零系数
* \参数 coeffFlag  :每一个CG中非零系数的标记(可以对应语法元素sig_coeff_flag),高位存储CG前面的非零标记,低位存储CG后面的非零标记
* \参数 coeffNum   :每一个CG中非零系数的个数
* \参数 numSig     :非零系数的个数
* \参数 scanCG4x4  :CG的扫描顺序
* \参数 trSize     :TU尺寸
* \返回 int        : TU中最后一个非零系数的位置
**/
static int scanPosLast_c(const uint16_t *scan, const coeff_t *coeff, uint16_t *coeffSign, uint16_t *coeffFlag, uint8_t *coeffNum, int numSig, const uint16_t* /*scanCG4x4*/, const int /*trSize*/)
{
    memset(coeffNum, 0, MLS_GRP_NUM * sizeof(*coeffNum));
    memset(coeffFlag, 0, MLS_GRP_NUM * sizeof(*coeffFlag));
    memset(coeffSign, 0, MLS_GRP_NUM * sizeof(*coeffSign));

    int scanPosLast = 0;
    do // 从头到尾扫描每一个常规量化后的系数
    {
        const uint32_t cgIdx = (uint32_t)scanPosLast >> MLS_CG_SIZE; // 得到CG的下标index,由于一个CG是4x4,所以向右移4位,即可得到CG的下标
                               // 这样右移之后只能得到按照Z型(即先行后列)扫描顺序的CG下标,而不是当前扫描模式的CG下标,所以这里CG的相关信息是依据Z型扫描存储的,而系数还是按照扫描顺序遍历的
        const uint32_t posLast = scan[scanPosLast++]; // 得到当前扫描模式下的扫描位置

        const int curCoeff = coeff[posLast]; // 得到扫描位置上的系数值
        const uint32_t isNZCoeff = (curCoeff != 0); // 判断是否该系数为0
        // get L1 sig map
        // NOTE: the new algorithm is complicated, so I keep reference code here
        //uint32_t posy   = posLast >> log2TrSize;
        //uint32_t posx   = posLast - (posy << log2TrSize);
        //uint32_t blkIdx0 = ((posy >> MLS_CG_LOG2_SIZE) << codingParameters.log2TrSizeCG) + (posx >> MLS_CG_LOG2_SIZE);
        //const uint32_t blkIdx = ((posLast >> (2 * MLS_CG_LOG2_SIZE)) & ~maskPosXY) + ((posLast >> MLS_CG_LOG2_SIZE) & maskPosXY);
        //sigCoeffGroupFlag64 |= ((uint64_t)isNZCoeff << blkIdx);
        numSig -= isNZCoeff; // 如果当前系数是非零系数,则从非零系数的个数中减一

        // TODO: optimize by instruction BTS
        coeffSign[cgIdx] += (uint16_t)(((uint32_t)curCoeff >> 31) << coeffNum[cgIdx]); // 得到当前系数的符号位(向右移31位只剩下符号位),并移位累加到每一个CG的coeffSign[cgIdx]中。
                                       // 如果当前CG中第一个非零系数出现(coeffNum[cgIdx]=0),则它的符号位被存放在最低位;紧接着来了第二个非零系数(coeffNum[cgIdx]=1),则它的符号被存放在次低位,以此类推。
        coeffFlag[cgIdx] = (coeffFlag[cgIdx] << 1) + (uint16_t)isNZCoeff;
        coeffNum[cgIdx] += (uint8_t)isNZCoeff; // 对每一个CG累加其中的非零系数的个数
    }
    while (numSig > 0); // 知道遍历所有的非零系数,则结束循环
    return scanPosLast - 1; // 得到最后一个非零系数的扫描位置
}

static uint32_t findPosFirstLast_c(const int16_t *dstCoeff, const intptr_t trSize, const uint16_t scanTbl[16])
{
    int n;

    for (n = SCAN_SET_SIZE - 1; n >= 0; --n)
    {
        const uint32_t idx = scanTbl[n];
        const uint32_t idxY = idx / MLS_CG_SIZE;
        const uint32_t idxX = idx % MLS_CG_SIZE;
        if (dstCoeff[idxY * trSize + idxX])
            break;
    }

    X265_CHECK(n >= -1, "non-zero coeff scan failuare!\n");

    uint32_t lastNZPosInCG = (uint32_t)n;

    for (n = 0; n < SCAN_SET_SIZE; n++)
    {
        const uint32_t idx = scanTbl[n];
        const uint32_t idxY = idx / MLS_CG_SIZE;
        const uint32_t idxX = idx % MLS_CG_SIZE;
        if (dstCoeff[idxY * trSize + idxX])
            break;
    }

    uint32_t firstNZPosInCG = (uint32_t)n;

    // NOTE: when coeff block all ZERO, the lastNZPosInCG is undefined and firstNZPosInCG is 16
    return ((lastNZPosInCG << 16) | firstNZPosInCG);
}


static uint32_t costCoeffNxN_c(const uint16_t *scan, const coeff_t *coeff, intptr_t trSize, uint16_t *absCoeff, const uint8_t *tabSigCtx, uint32_t scanFlagMask, uint8_t *baseCtx, int offset, int scanPosSigOff, int subPosBase)
{
    ALIGN_VAR_32(uint16_t, tmpCoeff[SCAN_SET_SIZE]);
    uint32_t numNonZero = (scanPosSigOff < (SCAN_SET_SIZE - 1) ? 1 : 0);
    uint32_t sum = 0;

    // correct offset to match assembly
    absCoeff -= numNonZero;

    for (int i = 0; i < MLS_CG_SIZE; i++)
    {
        tmpCoeff[i * MLS_CG_SIZE + 0] = (uint16_t)abs(coeff[i * trSize + 0]);
        tmpCoeff[i * MLS_CG_SIZE + 1] = (uint16_t)abs(coeff[i * trSize + 1]);
        tmpCoeff[i * MLS_CG_SIZE + 2] = (uint16_t)abs(coeff[i * trSize + 2]);
        tmpCoeff[i * MLS_CG_SIZE + 3] = (uint16_t)abs(coeff[i * trSize + 3]);
    }

    do
    {
        uint32_t blkPos, sig, ctxSig;
        blkPos = scan[scanPosSigOff];
        const uint32_t posZeroMask = (subPosBase + scanPosSigOff) ? ~0 : 0;
        sig     = scanFlagMask & 1;
        scanFlagMask >>= 1;
        X265_CHECK((uint32_t)(tmpCoeff[blkPos] != 0) == sig, "sign bit mistake\n");
        if ((scanPosSigOff != 0) || (subPosBase == 0) || numNonZero)
        {
            const uint32_t cnt = tabSigCtx[blkPos] + offset;
            ctxSig = cnt & posZeroMask;

            //X265_CHECK(ctxSig == Quant::getSigCtxInc(patternSigCtx, log2TrSize, trSize, codingParameters.scan[subPosBase + scanPosSigOff], bIsLuma, codingParameters.firstSignificanceMapContext), "sigCtx mistake!\n");;
            //encodeBin(sig, baseCtx[ctxSig]);
            const uint32_t mstate = baseCtx[ctxSig];
            const uint32_t mps = mstate & 1;
            const uint32_t stateBits = PFX(entropyStateBits)[mstate ^ sig];
            uint32_t nextState = (stateBits >> 24) + mps;
            if ((mstate ^ sig) == 1)
                nextState = sig;
            X265_CHECK(sbacNext(mstate, sig) == nextState, "nextState check failure\n");
            X265_CHECK(sbacGetEntropyBits(mstate, sig) == (stateBits & 0xFFFFFF), "entropyBits check failure\n");
            baseCtx[ctxSig] = (uint8_t)nextState;
            sum += stateBits;
        }
        assert(numNonZero <= 15);
        assert(blkPos <= 15);
        absCoeff[numNonZero] = tmpCoeff[blkPos];
        numNonZero += sig;
        scanPosSigOff--;
    }
    while(scanPosSigOff >= 0);

    return (sum & 0xFFFFFF);
}

static uint32_t costCoeffRemain_c(uint16_t *absCoeff, int numNonZero, int idx)
{
    uint32_t goRiceParam = 0;

    uint32_t sum = 0;
    int baseLevel = 3;
    do
    {
        if (idx >= C1FLAG_NUMBER)
            baseLevel = 1;

        // TODO: the IDX is not really idx, so this check inactive
        //X265_CHECK(baseLevel == ((idx < C1FLAG_NUMBER) ? (2 + firstCoeff2) : 1), "baseLevel check failurr\n");
        int codeNumber = absCoeff[idx] - baseLevel;

        if (codeNumber >= 0)
        {
            //writeCoefRemainExGolomb(absCoeff[idx] - baseLevel, goRiceParam);
            uint32_t length = 0;

            codeNumber = ((uint32_t)codeNumber >> goRiceParam) - COEF_REMAIN_BIN_REDUCTION;
            if (codeNumber >= 0)
            {
                {
                    unsigned long cidx;
                    CLZ(cidx, codeNumber + 1);
                    length = cidx;
                }
                X265_CHECK((codeNumber != 0) || (length == 0), "length check failure\n");

                codeNumber = (length + length);
            }
            sum += (COEF_REMAIN_BIN_REDUCTION + 1 + goRiceParam + codeNumber);

            if (absCoeff[idx] > (COEF_REMAIN_BIN_REDUCTION << goRiceParam))
                goRiceParam = (goRiceParam + 1) - (goRiceParam >> 2);
            X265_CHECK(goRiceParam <= 4, "goRiceParam check failure\n");
        }
        baseLevel = 2;
        idx++;
    }
    while(idx < numNonZero);

    return sum;
}


static uint32_t costC1C2Flag_c(uint16_t *absCoeff, intptr_t numC1Flag, uint8_t *baseCtxMod, intptr_t ctxOffset)
{
    uint32_t sum = 0;
    uint32_t c1 = 1;
    uint32_t firstC2Idx = 8;
    uint32_t firstC2Flag = 2;
    uint32_t c1Next = 0xFFFFFFFE;

    int idx = 0;
    do
    {
        uint32_t symbol1 = absCoeff[idx] > 1;
        uint32_t symbol2 = absCoeff[idx] > 2;
        //encodeBin(symbol1, baseCtxMod[c1]);
        {
            const uint32_t mstate = baseCtxMod[c1];
            baseCtxMod[c1] = sbacNext(mstate, symbol1);
            sum += sbacGetEntropyBits(mstate, symbol1);
        }

        if (symbol1)
            c1Next = 0;

        if (symbol1 + firstC2Flag == 3)
            firstC2Flag = symbol2;

        if (symbol1 + firstC2Idx == 9)
            firstC2Idx  = idx;

        c1 = (c1Next & 3);
        c1Next >>= 2;
        X265_CHECK(c1 <= 3, "c1 check failure\n");
        idx++;
    }
    while(idx < numC1Flag);

    if (!c1)
    {
        X265_CHECK((firstC2Flag <= 1), "firstC2FlagIdx check failure\n");

        baseCtxMod += ctxOffset;

        //encodeBin(firstC2Flag, baseCtxMod[0]);
        {
            const uint32_t mstate = baseCtxMod[0];
            baseCtxMod[0] = sbacNext(mstate, firstC2Flag);
            sum += sbacGetEntropyBits(mstate, firstC2Flag);
        }
    }

    return (sum & 0x00FFFFFF) + (c1 << 26) + (firstC2Idx << 28);
}

namespace X265_NS {
// x265 private namespace

void setupDCTPrimitives_c(EncoderPrimitives& p)
{
    p.dequant_scaling = dequant_scaling_c;
    p.dequant_normal = dequant_normal_c;
    p.quant = quant_c;
    p.nquant = nquant_c;
    p.dst4x4 = dst4_c;
    p.cu[BLOCK_4x4].dct   = dct4_c;
    p.cu[BLOCK_8x8].dct   = dct8_c;
    p.cu[BLOCK_16x16].dct = dct16_c;
    p.cu[BLOCK_32x32].dct = dct32_c;
    p.idst4x4 = idst4_c;
    p.cu[BLOCK_4x4].idct   = idct4_c;
    p.cu[BLOCK_8x8].idct   = idct8_c;
    p.cu[BLOCK_16x16].idct = idct16_c;
    p.cu[BLOCK_32x32].idct = idct32_c;
    p.denoiseDct = denoiseDct_c;
    p.cu[BLOCK_4x4].count_nonzero = count_nonzero_c<4>;
    p.cu[BLOCK_8x8].count_nonzero = count_nonzero_c<8>;
    p.cu[BLOCK_16x16].count_nonzero = count_nonzero_c<16>;
    p.cu[BLOCK_32x32].count_nonzero = count_nonzero_c<32>;

    p.cu[BLOCK_4x4].copy_cnt   = copy_count<4>;
    p.cu[BLOCK_8x8].copy_cnt   = copy_count<8>;
    p.cu[BLOCK_16x16].copy_cnt = copy_count<16>;
    p.cu[BLOCK_32x32].copy_cnt = copy_count<32>;

    p.scanPosLast = scanPosLast_c;
    p.findPosFirstLast = findPosFirstLast_c;
    p.costCoeffNxN = costCoeffNxN_c;
    p.costCoeffRemain = costCoeffRemain_c;
    p.costC1C2Flag = costC1C2Flag_c;
}
}


 

你可能感兴趣的:(x265代码注释)