⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transform8x8.c

📁 包含FRExt部分的JM源码
💻 C
📖 第 1 页 / 共 5 页
字号:
  dummy = 0;

  *nonzero = dct_luma8x8 (b8, &dummy, 1);

  //===== get distortion (SSD) of 8x8 block =====
  for (y=0; y<8; y++)
    for (x=pic_pix_x; x<pic_pix_x+8; x++)  
      distortion += img->quad [imgY_orig[pic_opix_y+y][x] - imgY[pic_pix_y+y][x]];

  //===== RATE for INTRA PREDICTION MODE  (SYMBOL MODE MUST BE SET TO UVLC) =====
  currSE->value1 = (mostProbableMode == ipmode) ? -1 : ipmode < mostProbableMode ? ipmode : ipmode-1;

  //--- set position and type ---
  currSE->context = b8;
  currSE->type    = SE_INTRAPREDMODE;

  //--- set function pointer ----
  if (input->symbol_mode != UVLC)    
    currSE->writing = writeIntraPredMode_CABAC;

  //--- choose data partition ---
//  if (img->type!=B_SLICE && img->type!=BS_IMG)   dataPart = &(currSlice->partArr[partMap[SE_INTRAPREDMODE]]);
  if (img->type!=B_SLICE)
    dataPart = &(currSlice->partArr[partMap[SE_INTRAPREDMODE]]);
  else
    dataPart = &(currSlice->partArr[partMap[SE_BFRAME]]);

  //--- encode and update rate ---
  if (input->symbol_mode == UVLC)
    writeSyntaxElement_Intra4x4PredictionMode(currSE, dataPart); //LW
  else
    dataPart->writeSyntaxElement (currSE, dataPart);

  rate = currSE->len;
  currSE++;
  currMB->currSEnr++;

  //===== RATE for LUMINANCE COEFFICIENTS =====

  if (input->symbol_mode == UVLC)
  {
	int b4; //LW new variable
	for(b4=0; b4<4; b4++) //LW: do 4x now
		rate  += writeCoeff4x4_CAVLC (LUMA, b8, b4, 0); //LW
  }
  else
  {
    rate  += writeLumaCoeff8x8_CABAC (b8, 1);
  }


  rdcost = (double)distortion + lambda*(double)rate;

  if(img->residue_transform_flag) // WSKIM SAMSUNG AIT
    return (double)rate;
  else
    return rdcost;
}

/*!
 ************************************************************************
 * \brief
 *    Calculate the quantisation and inverse quantisation parameters
 *
 * \para Input:
 *    none
 *
 * \para Output_
 *    none
 ************************************************************************
 */
void CalculateQuant8Param(void)
{
  int i, j, k, temp;
  // HB 10062004 start
  int present[2];
  int no_q_matrix=FALSE;

  if(!active_sps->seq_scaling_matrix_present_flag && !active_pps->pic_scaling_matrix_present_flag) //set to default matrix
    no_q_matrix=TRUE;
  else
  {
    memset(present, 0, sizeof(int)*2);

    if(active_sps->seq_scaling_matrix_present_flag)
      for(i=0; i<2; i++)
        present[i] = active_sps->seq_scaling_list_present_flag[i+6];

    if(active_pps->pic_scaling_matrix_present_flag)
      for(i=0; i<2; i++)
        present[i] |= active_pps->pic_scaling_list_present_flag[i+6];
  }

  if(no_q_matrix==TRUE)
  {
    for(k=0; k<6; k++)
      for(j=0; j<8; j++)
        for(i=0; i<8; i++)
        {
          LevelScale8x8Luma_Intra[k][j][i]         = quant_coef8[k][j][i];
          InvLevelScale8x8Luma_Intra[k][j][i]      = dequant_coef8[k][j][i]<<4;

          LevelScale8x8Luma_Inter[k][j][i]         = quant_coef8[k][j][i];
          InvLevelScale8x8Luma_Inter[k][j][i]      = dequant_coef8[k][j][i]<<4;
        }
  }
  else
  {
    for(k=0; k<6; k++)
      for(j=0; j<8; j++)
        for(i=0; i<8; i++)
        {
          temp = (i<<3)+j;
          if((!present[0]) || UseDefaultScalingMatrix8x8Flag[0])
          {
            LevelScale8x8Luma_Intra[k][j][i]    = (quant_coef8[k][j][i]<<4)/Quant8_intra_default[temp];
            InvLevelScale8x8Luma_Intra[k][j][i] = dequant_coef8[k][j][i]*Quant8_intra_default[temp];
          }
          else
          {
            LevelScale8x8Luma_Intra[k][j][i]    = (quant_coef8[k][j][i]<<4)/ScalingList8x8[0][temp];
            InvLevelScale8x8Luma_Intra[k][j][i] = dequant_coef8[k][j][i]*ScalingList8x8[0][temp];
          }

          if((!present[1]) || UseDefaultScalingMatrix8x8Flag[1])
          {
            LevelScale8x8Luma_Inter[k][j][i]    = (quant_coef8[k][j][i]<<4)/Quant8_inter_default[temp];
            InvLevelScale8x8Luma_Inter[k][j][i] = dequant_coef8[k][j][i]*Quant8_inter_default[temp];
          }
          else
          {
            LevelScale8x8Luma_Inter[k][j][i]    = (quant_coef8[k][j][i]<<4)/ScalingList8x8[1][temp];
            InvLevelScale8x8Luma_Inter[k][j][i] = dequant_coef8[k][j][i]*ScalingList8x8[1][temp];
          }
        }
  }
  // HB 10062004 end
}

/*!
 ************************************************************************
 * \brief
 *    The routine performs transform,quantization,inverse transform, adds the diff.
 *    to the prediction and writes the result to the decoded luma frame. Includes the
 *    RD constrained quantization also.
 *
 * \para Input:
 *    b8: Block position inside a macro block (0,1,2,3).
 *
 * \para Output_
 *    nonzero: 0 if no levels are nonzero.  1 if there are nonzero levels.  
 *    coeff_cost: Counter for nonzero coefficients, used to discard expencive levels.
 ************************************************************************
 */

#define MC(coeff) ((coeff)&3) //LW

int dct_luma8x8(int b8,int *coeff_cost, int intra)
{
  int sign(int a,int b);

  int i,j,ilev,coeff_ctr;
  int qp_const,level,scan_pos,run;
  int nonzero;
  int qp_per,qp_rem,q_bits;
  int dq_lshift, dq_rshift, dq_round;

  int block_x = 8*(b8%2);
  int block_y = 8*(b8/2);
  int*  ACLevel = img->cofAC[b8][0][0];
  int*  ACRun   = img->cofAC[b8][0][1];
  int m6[8][8];
  int scan_poss[4],runs[4]; //LW

  Macroblock *currMB = &img->mb_data[img->current_mb_nr];
  Boolean lossless_qpprime = ((img->qp + img->bitdepth_luma_qp_scale)==0 && img->lossless_qpprime_flag==1); // S.SUN 06072004
  
  qp_per    = (img->qp + img->bitdepth_luma_qp_scale - MIN_QP)/6;  //CHA-VG01
  qp_rem    = (img->qp + img->bitdepth_luma_qp_scale - MIN_QP)%6; //CHA-VG01
  q_bits    = Q_BITS_8+qp_per;

  if (qp_per < 6)
  {
    dq_rshift = 6 - qp_per;
    dq_round  = 1<<(5-qp_per);
  }
  else
    dq_lshift = qp_per - 6;

/*
  if (intra == 1)
    qp_const=(1<<q_bits)/3;
  else 
    qp_const=(1<<q_bits)/6;
*/
  if (img->type == I_SLICE)
    qp_const=(1<<q_bits)/3;    // intra
  else
    qp_const=(1<<q_bits)/6;    // inter
  

  // horizontal transform
  for( i=0; i<8 && !lossless_qpprime; i++)  // S.SUN 06072004
  {
    int a[8], b[8];
    a[0] = img->m7[0][i] + img->m7[7][i];
    a[1] = img->m7[1][i] + img->m7[6][i];
    a[2] = img->m7[2][i] + img->m7[5][i];
    a[3] = img->m7[3][i] + img->m7[4][i];

    b[0] = a[0] + a[3];
    b[1] = a[1] + a[2];
    b[2] = a[0] - a[3];
    b[3] = a[1] - a[2];

    a[4] = img->m7[0][i] - img->m7[7][i];
    a[5] = img->m7[1][i] - img->m7[6][i];
    a[6] = img->m7[2][i] - img->m7[5][i];
    a[7] = img->m7[3][i] - img->m7[4][i];

    b[4]= a[5] + a[6] + ((a[4]>>1) + a[4]);
    b[5]= a[4] - a[7] - ((a[6]>>1) + a[6]);
    b[6]= a[4] + a[7] - ((a[5]>>1) + a[5]);
    b[7]= a[5] - a[6] + ((a[7]>>1) + a[7]);

    m6[0][i] = b[0] + b[1];
    m6[2][i] = b[2] + (b[3]>>1);
    m6[4][i] = b[0] - b[1];
    m6[6][i] = (b[2]>>1) - b[3];
    m6[1][i] =   b[4] + (b[7]>>2);
    m6[3][i] =   b[5] + (b[6]>>2);
    m6[5][i] =   b[6] - (b[5]>>2);
    m6[7][i] = - b[7] + (b[4]>>2);

  }
  // vertical transform
  for( i=0; i<8 && !lossless_qpprime; i++)  // S.SUN 06072004
  {
    int a[8], b[8];
    a[0] = m6[i][0] + m6[i][7];
    a[1] = m6[i][1] + m6[i][6];
    a[2] = m6[i][2] + m6[i][5];
    a[3] = m6[i][3] + m6[i][4];
    
    b[0] = a[0] + a[3];
    b[1] = a[1] + a[2];
    b[2] = a[0] - a[3];
    b[3] = a[1] - a[2];
    
    a[4] = m6[i][0] - m6[i][7];
    a[5] = m6[i][1] - m6[i][6];
    a[6] = m6[i][2] - m6[i][5];
    a[7] = m6[i][3] - m6[i][4];
    
    b[4]= a[5] + a[6] + ((a[4]>>1) + a[4]);
    b[5]= a[4] - a[7] - ((a[6]>>1) + a[6]);
    b[6]= a[4] + a[7] - ((a[5]>>1) + a[5]);
    b[7]= a[5] - a[6] + ((a[7]>>1) + a[7]);
    
    img->m7[i][0] = b[0] + b[1];
    img->m7[i][2] = b[2] + (b[3]>>1);
    img->m7[i][4] = b[0] - b[1];
    img->m7[i][6] = (b[2]>>1) - b[3];
    img->m7[i][1] =   b[4] + (b[7]>>2);
    img->m7[i][3] =   b[5] + (b[6]>>2);
    img->m7[i][5] =   b[6] - (b[5]>>2);
    img->m7[i][7] = - b[7] + (b[4]>>2);
  }


  // Quant
  
  nonzero=FALSE;
  
  run=-1;
  scan_pos=0;
  
  runs[0]=runs[1]=runs[2]=runs[3]=-1; //LW
  scan_poss[0]=scan_poss[1]=scan_poss[2]=scan_poss[3]=0; //LW 
  
  for (coeff_ctr=0;coeff_ctr < 64;coeff_ctr++)
  {
    
    if (img->field_picture || ( img->MbaffFrameFlag && currMB->mb_field )) 
    {  // Alternate scan for field coding
      i=FIELD_SCAN8x8[coeff_ctr][0];
      j=FIELD_SCAN8x8[coeff_ctr][1];
    }
    else 
    {
      i=SNGL_SCAN8x8[coeff_ctr][0];
      j=SNGL_SCAN8x8[coeff_ctr][1];
    }
    
    run++;
    ilev=0;
    
    runs[MC(coeff_ctr)]++; //LW
    
    if(lossless_qpprime) // S.SUN 06072004
      level = abs (img->m7[i][j]);
    else if(intra == 1)
      level = (abs (img->m7[i][j]) * LevelScale8x8Luma_Intra[qp_rem][i][j] + qp_const) >> q_bits;
    else
      level = (abs (img->m7[i][j]) * LevelScale8x8Luma_Inter[qp_rem][i][j] + qp_const) >> q_bits;
    
    if (level != 0)
    {
      nonzero=TRUE;
      
      if (currMB->luma_transform_size_8x8_flag && input->symbol_mode == UVLC) //LW -start
      {
        if (level > 1)
          *coeff_cost += MAX_VALUE;                // set high cost, shall not be discarded
        else
          *coeff_cost += COEFF_COST8x8[runs[MC(coeff_ctr)]];
        
        img->cofAC[b8][MC(coeff_ctr)][0][scan_poss[MC(coeff_ctr)]] = sign(level,img->m7[i][j]);
        img->cofAC[b8][MC(coeff_ctr)][1][scan_poss[MC(coeff_ctr)]] = runs[MC(coeff_ctr)];
        ++scan_poss[MC(coeff_ctr)];
        runs[MC(coeff_ctr)]=-1;
      }
      else  //LW	-end
      {
        if (level > 1)
          *coeff_cost += MAX_VALUE;                // set high cost, shall not be discarded
        else
          *coeff_cost += COEFF_COST8x8[run];
        ACLevel[scan_pos] = sign(level,img->m7[i][j]);
        ACRun  [scan_pos] = run;
        ++scan_pos;
        run=-1;                     // reset zero level counter
      }      
      level = sign(level, img->m7[i][j]); // HB 10062004
      if(lossless_qpprime)  // S.SUN 06072004
      {
        ilev = level;
      }
      else if(intra == 1)
      {
        if (qp_per>=6)
          ilev = level*InvLevelScale8x8Luma_Intra[qp_rem][i][j]<<dq_lshift; // dequantization
        else
          ilev = (level*InvLevelScale8x8Luma_Intra[qp_rem][i][j] + dq_round)>>dq_rshift; // dequantization
      }
      else
      {
        if (qp_per>=6)
          ilev = level*InvLevelScale8x8Luma_Inter[qp_rem][i][j]<<dq_lshift; // dequantization
        e

⌨️ 快捷键说明

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