adaptive_filter.c

来自「JM 11.0 KTA 2.1 Source Code」· C语言 代码 · 共 1,655 行 · 第 1/5 页

C
1,655
字号
  0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  0.0, 0.0, 0.0, 0.0, 0.0, 0.0},       // n_pos
  {1.0 / 64.0, -5.0 / 64.0, 20.0 / 64.0, 52.0 / 64.0, -5.0 / 64.0, 1.0 / 64.0,
  0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  0.0, 0.0, 0.0, 0.0, 0.0, 0.0},       // o_pos
};
// separable aif (END)
int FILTER_NEW_SUB_POS[SQR_FILTER] = {        a_pos, b_pos, a_pos,
a_pos, e_pos, f_pos, e_pos,
b_pos, f_pos, j_pos, f_pos,
a_pos, e_pos, f_pos, e_pos };
int TwoDSymmetricPattern[15][SQR_FILTER]  =    // get one filter from another one, if symmetry properties are used (used e.g. in ExtendFilterCoefficients)
{
  { 0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,
  12, 13, 14, 15, 16, 17,        // a_pos  - not used actually
  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0  },
  { 0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,
  12, 13, 14, 14, 13, 12,        // b_pos
  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0  },
  { 0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,
  17, 16, 15, 14, 13, 12,        // c_pos
  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0,
  0,  0,  0,  0,  0,  0  },
  { 0,  0, 12,  0,  0,  0,
  0,  0, 13,  0,  0,  0,
  0,  0, 14,  0,  0,  0,        // d_pos
  0,  0, 15,  0,  0,  0,
  0,  0, 16,  0,  0,  0,
  0,  0, 17,  0,  0,  0  },
  { 0,  1,  2,  3,  4,  5,
  1,  7,  8,  9, 10, 11,
  2,  8, 14, 15, 16, 17,        // e_pos
  3,  9, 15, 21, 22, 23,
  4, 10, 16, 22, 28, 29,
  5, 11, 17, 23, 29, 35  },
  { 0,  1,  2,  2,  1,  0,
  6,  7,  8,  8,  7,  6,
  12, 13, 14, 14, 13, 12,        // f_pos
  18, 19, 20, 20, 19, 18,
  24, 25, 26, 26, 25, 24,
  30, 31, 32, 32, 31, 30  },
  { 5,  4,  3,  2,  1,  0,
  11, 10,  9,  8,  7,  1,
  17, 16, 15, 14,  8,  2,        // g_pos
  23, 22, 21, 15,  9,  3,
  29, 28, 22, 16, 10,  4,
  35, 29, 23, 17, 11,  5  },
  { 0,  0, 12,  0,  0,  0,
  0,  0, 13,  0,  0,  0,
  0,  0, 14,  0,  0,  0,        // h_pos
  0,  0, 14,  0,  0,  0,
  0,  0, 13,  0,  0,  0,
  0,  0, 12,  0,  0,  0  },
  { 0,  6, 12, 18, 24, 30,
  1,  7, 13, 19, 25, 31,
  2,  8, 14, 20, 26, 32,        // i_pos
  2,  8, 14, 20, 26, 32,
  1,  7, 13, 19, 25, 31,
  0,  6, 12, 18, 24, 30  },
  { 0,  1,  2,  2,  1,  0,
  1,  7,  8,  8,  7,  1,
  2,  8, 14, 14,  8,  2,         // j_pos
  2,  8, 14, 14,  8,  2,
  1,  7,  8,  8,  7,  1,
  0,  1,  2,  2,  1,  0  },
  {30, 24, 18, 12,  6,  0,
  31, 25, 19, 13,  7,  1,
  32, 26, 20, 14,  8,  2,         // k_pos
  32, 26, 20, 14,  8,  2,
  31, 25, 19, 13,  7,  1,
  30, 24, 18, 12,  6,  0  },
  { 0,  0, 17,  0,  0,  0,
  0,  0, 16,  0,  0,  0,
  0,  0, 15,  0,  0,  0,         // l_pos
  0,  0, 14,  0,  0,  0,
  0,  0, 13,  0,  0,  0,
  0,  0, 12,  0,  0,  0  },
  { 5, 11, 17, 23, 29, 35,
  4, 10, 16, 22, 28, 29,
  3,  9, 15, 21, 22, 23,        // m_pos
  2,  8, 14, 15, 16, 17,
  1,  7,  8,  9, 10, 11,
  0,  1,  2,  3,  4,  5  },
  {30, 31, 32, 32, 31, 30,
  24, 25, 26, 26, 25, 24,
  18, 19, 20, 20, 19, 18,         // n_pos
  12, 13, 14, 14, 13, 12,
  6,  7,  8,  8,  7,  6,
  0,  1,  2,  2,  1,  0  },
  {35, 29, 23, 17, 11,  5,
  29, 28, 22, 16, 10,  4,
  23, 22, 21, 15,  9,  3,         // o_pos
  17, 16, 15, 14,  8,  2,
  11, 10,  9,  8,  7,  1,
  5,  4,  3,  2,  1,  0  }
};

#ifdef E_DAIF
int numQBitsInt[SQR_FILTER_INT-1] = 
{
  12, 12, 12, 12, 12, 
  12, 11, 11, 11, 12, 
  12, 11, 10, 11, 12, 
  12, 11, 11, 11, 12,
  12, 12, 12, 12, 12, 
};   

int UseEquation[3][SQR_FILTER-1] = 
{
  {
    1, 0, 0, 0, 0, 0,
      0, 1, 0, 0, 0, 0,
      0, 0, 1, 0, 0, 0,
      0, 0, 0, 1, 0, 0,
      0, 0, 0, 0, 1, 0,
      0, 0, 0, 0, 0, 1,
  },
  {
    0, 0, 0, 0, 0, 1,
      0, 0, 0, 0, 1, 0,
      0, 0, 0, 1, 0, 0,
      0, 0, 1, 0, 0, 0,
      0, 1, 0, 0, 0, 0,
      1, 0, 0, 0, 0, 0,
    },
    {
      1, 0, 0, 0, 0, 1,
        0, 1, 0, 0, 1, 0,
        0, 0, 1, 1, 0, 0,
        0, 0, 1, 1, 0, 0,
        0, 1, 0, 0, 1, 0,
        1, 0, 0, 0, 0, 1,
    },
};
#endif

int DiffQFilterCoef[15][SQR_FILTER];        // differences to be transmitted
int POS_EQUATION_NUMBER[15];                // number of different coefficietns for each sub-pel position
int FilterFlag[15];                          // Flags defining if Filter at the particular position calculated
int  UseAllSubpelPositions;                  // 1 if FilterFlag for all independent positions is 1
int SubpelPositionsPattern;
int SymmetryPosition[15] = {1,1,0,0,1,1,0,0,0,1,0,0,0,0,0};  // if 0, the position is copied from another one
int Is1DPosition[15] = {1,1,1,1,0,0,0,1,0,0,0,1,0,0,0}; // if 0, the position is a 2D one
#ifdef DIRECTIONAL_FILTER
int IsDiagonal1D[15] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // if 1 - the filter alighned NW-SE, 2 - NE-SW
int nQBits[15] = {7,7,7,7,7,8,7,7,8,8,8,7,7,8,7};    // Number of bits for each coefficient representation, 16-bit DAIF

short int FilterCoef16bits[15][SQR_FILTER];                  // estimated filter coefficients
#endif
int FILTER_SIZE = 6;    // is not checked for other values
int FILTER_OFFSET;
int NumberOfQBits;                              // number of bits used for quantization of the filter coefficients



double NxN_Matrix[15][SQR_FILTER][SQR_FILTER];      // SQR(PEL)-1 X SQR(FILTER_SIZE) X SQR(FILTER_SIZE) - matrix
double N_Vector[15][SQR_FILTER];                    // SQR(PEL)-1 X SQR(FILTER_SIZE) - vector
double FilterCoef[15][SQR_FILTER];                  // estimated filter coefficients
int    QFilterCoef[15][SQR_FILTER];                 // SQR(PEL)-1 X SQR(FILTER_SIZE) - quantized filter coefficients
double PredFilterCoef[15][SQR_FILTER];              // predicted filter coefficients
double  DiffFilterCoef[15][SQR_FILTER];             // differences
double CalculatedFilter[15][SQR_FILTER];            // Filter for different positions
int **MBRateStorage;                                // storage for number of bits per macroblock for part. mode coded with standard filter
int *BitsPerFrameCounter;                           // storage for number of bits per macroblock for part. mode coded with standard filter
int number_of_macroblocks;
int current_poc;

double cost_frame[2];

#ifdef E_DAIF
double NxN_Matrix_Int[SQR_FILTER_INT][SQR_FILTER_INT];
double N_Vector_Int  [SQR_FILTER_INT];
double FilterCoefInt [SQR_FILTER_INT];
double CalculatedFilterInt [SQR_FILTER_INT];
int FilterFlagInt; 
int FILTER_OFFSET_INT;

int DiffQFilterOffsetI[15], DiffQFilterOffsetF[15];
int DiffQFilterOffsetIntI, DiffQFilterOffsetIntF; 
int DiffQFilterCoeffInt[SQR_FILTER_INT]; 
#endif

#ifdef DIRECTIONAL_FILTER
/*! 
*************************************************************************************
* \brief
*   Initialize the predefined AIF constants. 
*  
* \para <title>
*    <paragraph>
*
* \para
*    <another paragraph>
*
* \author
*    - Dmytro Rusanovskyy                   <dmytro.rusanovskyy@tut.fi>
*************************************************************************************
*/
void initFilterCustom(int filterID)
{
  UseAllSubpelPositions = 0;
  SubpelPositionsPattern = 0;

  memset(FilterFlag,0,MAX_NUM_SUBPELS_AIF*sizeof(int));
  memset(DiffQFilterCoef, 0, MAX_AIF_SUPPORT*MAX_NUM_SUBPELS_AIF*sizeof(int));
  memset(FilterCoef, 0, MAX_AIF_SUPPORT*MAX_NUM_SUBPELS_AIF*sizeof(double));

  if  ((filterID == FILTER_TYPE_2D_NS) || (filterID == FILTER_TYPE_2D_S))
  {
    memcpy(STANDARD_2D_FILTER,STANDARD_2D_FILTER_orig,sizeof(double)*MAX_NUM_SUBPELS_AIF*SQR_FILTER);
    memcpy(TwoDEquationPattern,TwoDEquationPattern_orig,sizeof(int)*MAX_NUM_SUBPELS_AIF*SQR_FILTER);
    memcpy(TwoDSymmetricPattern,TwoDSymmetricPattern_orig,sizeof(int)*MAX_NUM_SUBPELS_AIF*SQR_FILTER);

    memcpy(IsDiagonal1D,IsDiagonal1D_orig,sizeof(int)*15);
    memcpy(Is1DPosition,Is1DPosition_orig,sizeof(int)*15);
    memcpy(SymmetryPosition,SymmetryPosition_orig,sizeof(int)*15);
    memcpy(POS_EQUATION_NUMBER,POS_EQUATION_NUMBER_orig,sizeof(int)*15);
    memcpy(FILTER_NEW_SUB_POS,FILTER_NEW_SUB_POS_orig,sizeof(int)*SQR_FILTER);
  }
#ifdef E_DAIF 
  else if (filterID == FILTER_TYPE_1D || filterID == FILTER_TYPE_EDAIF)
#else
  else if (filterID == FILTER_TYPE_1D)
#endif
  {
    memcpy(&STANDARD_2D_FILTER[0][0],&STANDARD_2D_FILTER_v4[0][0],sizeof(double)*MAX_NUM_SUBPELS_AIF*SQR_FILTER);
    memcpy(&TwoDEquationPattern[0][0],&TwoDEquationPattern_v4[0][0],sizeof(int)*MAX_NUM_SUBPELS_AIF*SQR_FILTER);
    memcpy(&TwoDSymmetricPattern[0][0],&TwoDSymmetricPattern_v4[0][0],sizeof(int)*MAX_NUM_SUBPELS_AIF*SQR_FILTER);
    memcpy(&IsDiagonal1D[0],&IsDiagonal1D_v4[0],sizeof(int)*MAX_NUM_SUBPELS_AIF);
    memcpy(&Is1DPosition[0],&Is1DPosition_v4[0],sizeof(int)*MAX_NUM_SUBPELS_AIF);
    memcpy(&SymmetryPosition[0],&SymmetryPosition_v4[0],sizeof(int)*MAX_NUM_SUBPELS_AIF);
    memcpy(&POS_EQUATION_NUMBER[0],&POS_EQUATION_NUMBER_v4[0],sizeof(int)*MAX_NUM_SUBPELS_AIF);
    memcpy(&FILTER_NEW_SUB_POS[0],&FILTER_NEW_SUB_POS_v4[0],sizeof(int)*SQR_FILTER);
  }
}

#endif
/*!
************************************************************************
* \brief
*    init adaptive filter structures
************************************************************************
*/
void InitAdaptiveFilter(InputParameters *input)
{
  int block_x_nr;
  int block_y_nr;
  if(!input->UseAdaptiveFilter)
  {
    return;
  }
#ifdef DIRECTIONAL_FILTER
  if (input->UseAdaptiveFilter!= FILTER_TYPE_1D)
    input->ImpType = IMP_FLOAT32;
  if (input->ImpType == IMP_INT16)
    FILTCOEF_BITS = 7;
  else 
    FILTCOEF_BITS = DEFAULT_QUANT;
#endif
  NumberOfQBits = DEFAULT_QUANT;
  if(NumberOfQBits < 5 || NumberOfQBits > 16)
  {
    NumberOfQBits=DEFAULT_QUANT;
    printf("setting NumberOfQBits to default value...%d\n", NumberOfQBits);
  }
  FILTER_OFFSET = (FILTER_SIZE)/2-1;
#ifdef E_DAIF
  FILTER_OFFSET_INT = (FILTER_SIZE_INT)/2;
#endif

  POS_EQUATION_NUMBER[a_pos] =  6;
  POS_EQUATION_NUMBER[b_pos] =  3;
  POS_EQUATION_NUMBER[c_pos] =  6;

  POS_EQUATION_NUMBER[d_pos] =  6;
  POS_EQUATION_NUMBER[e_pos] = 21;
  POS_EQUATION_NUMBER[f_pos] = 18;
  POS_EQUATION_NUMBER[g_pos] = 21;

  POS_EQUATION_NUMBER[h_pos] =  3;
  POS_EQUATION_NUMBER[i_pos] = 18;
  POS_EQUATION_NUMBER[j_pos] =  6;
  POS_EQUATION_NUMBER[k_pos] = 18;

  POS_EQUATION_NUMBER[l_pos] =  6;
  POS_EQUATION_NUMBER[m_pos] = 21;
  POS_EQUATION_NUMBER[n_pos] = 18;
  POS_EQUATION_NUMBER[o_pos] = 21;

  number_of_macroblocks = img->width*img->height/256;
  BitsPerFrameCounter = calloc(number_of_macroblocks, sizeof(int));
  get_mem2Dint(&MBRateStorage, number_of_macroblocks, MAXMODE);
  block_y_nr = img->height/4;
  block_x_nr = img->width/4;
  BitsPerFrameCounter = calloc(number_of_macroblocks, sizeof(int));
  get_mem2Dint(&MBRateStorage, number_of_macroblocks, MAXMODE);
  current_poc=-1;

}

/*!
************************************************************************
* \brief
*    free adaptive filter structure
************************************************************************
*/
void FreeAdaptiveFilter()
{
  int block_x_nr;
  int block_y_nr;
  if(!input->UseAdaptiveFilter)
    return;
  if(MBRateStorage)
    free_mem2Dint(MBRateStorage);
  if(BitsPerFrameCounter)
    free(BitsPerFrameCounter);
  block_y_nr = img->height/4;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?