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

📄 rdopt.c

📁 JM 11.0 KTA 2.1 Source Code
💻 C
📖 第 1 页 / 共 5 页
字号:
            memcpy(&img->mpr[block_y+j][block_x], img->mprr[ipmode][j], BLOCK_SIZE * sizeof(imgpel));
            for (i=0; i<4; i++)
            {
              img->m7[j][i] = (int) (imgY_org[pic_opix_y+j][pic_opix_x+i] - img->mprr[ipmode][j][i]);
            }
          }
          
          //===== store the coding state =====
          //store_coding_state (cs_cm);
          // get and check rate-distortion cost
#ifdef BEST_NZ_COEFF
          img->mb_data[img->current_mb_nr].cbp_bits = cbp_bits;
#ifdef ADAPTIVE_FD_SD_CODING
          img->mb_data[img->current_mb_nr].FD_or_SD_bits = FD_or_SD_bits;
#endif
#endif
          if ((rdcost = RDCost_for_4x4IntraBlocks (&c_nz, b8, b4, ipmode, lambda, min_rdcost, mostProbableMode)) < min_rdcost)
          {
#ifdef USE_INTRA_MDDT
            if(input->UseIntraMDDT)
              memcpy(quant_stat_best, quant_stat_rd, 16*sizeof(long)); 
#endif
            //--- set coefficients ---
            memcpy(cofAC4x4[0],img->cofAC[b8][b4][0], 18 * sizeof(int));
            memcpy(cofAC4x4[1],img->cofAC[b8][b4][1], 18 * sizeof(int));
            
            //--- set reconstruction ---
            for (y=0; y<4; y++)
            {
              memcpy(rec4x4[y],&enc_picture->imgY[pic_pix_y+y][pic_pix_x], BLOCK_SIZE * sizeof(imgpel));
              if(img->type==SP_SLICE &&(!si_frame_indicator && !sp2_frame_indicator))
                memcpy(lrec4x4[y],&lrec[pic_pix_y+y][pic_pix_x], BLOCK_SIZE * sizeof(int));// stores the mode coefficients
            } 
            //--- flag if dct-coefficients must be coded ---
            nonzero = c_nz;
            
            //--- set best mode update minimum cost ---
            min_rdcost    = rdcost;
            best_ipmode   = ipmode;
#ifdef BEST_NZ_COEFF
            best_nz_coeff = img->nz_coeff [img->current_mb_nr][block_x4][block_y4];
            best_coded_block_flag = (int)((img->mb_data[img->current_mb_nr].cbp_bits>>bit_pos)&(int64)(1));
#ifdef ADAPTIVE_FD_SD_CODING
            best_SD_or_FD_flag    = (int)((img->mb_data[img->current_mb_nr].FD_or_SD_bits>>bit_pos)&(int64)(1));
#endif
#endif            
            //store_coding_state (cs_ib4);
            if (img->AdaptiveRounding)
            {
              for (j=0; j<4; j++)
                memcpy(&fadjust4x4[block_y+j][block_x],&img->fadjust4x4[1][block_y+j][block_x], BLOCK_SIZE * sizeof(int));
            }
          }
          
#ifndef RESET_STATE
          reset_coding_state (cs_cm);
#endif
        }
        else 
        {
          for (j=0; j<4; j++)
          {
            for (i=0; i<4; i++)
            {
              residue_B = imgUV_org[0][pic_opix_y+j][pic_opix_x+i] - img->mprr_c[0][c_ipmode][block_y+j][block_x+i];
              residue_G = imgY_org[pic_opix_y+j][pic_opix_x+i] - img->mprr[ipmode][j][i];
              residue_R = imgUV_org[1][pic_opix_y+j][pic_opix_x+i] - img->mprr_c[1][c_ipmode][block_y+j][block_x+i];
              
              /* Foward Residue Transform */
              resTrans_R[j][i] = residue_R-residue_B;
              temp = residue_B+(resTrans_R[j][i]>>1);
              resTrans_B[j][i] = residue_G-temp;
              resTrans_G[j][i] = temp+(resTrans_B[j][i]>>1);
            }
          }
          
          for (j=0; j<4; j++)
          {
            for (i=0; i<4; i++)
            {
              img->m7[j][i]  = resTrans_G[j][i];
            }
          }
          
          store_coding_state (cs_cm);
          // yuwen 2005.11.17 => should be an error here, requiring process for img->mb_data[img->current_mb_nr].cbp_bits
          rate = (int) RDCost_for_4x4IntraBlocks (&c_nz, b8, b4, ipmode, lambda, min_rdcost, mostProbableMode);
          reset_coding_state (cs_cm);
          
          for (j=0; j<4; j++)
          {
            for (i=0; i<4; i++)
            {
              rec_resG[j][i] = img->m7[j][i];
              img->m7[j][i]  = resTrans_B[j][i];
            }
          }
          //store_coding_state (cs_cm);
          // yuwen 2005.11.17 => should be an error here, requiring process for img->mb_data[img->current_mb_nr].cbp_bits
          rate += RDCost_for_4x4Blocks_Chroma (b8+4, b4, 0);
          for (j=0; j<4; j++)
          {
            for (i=0; i<4; i++)
            {
              rec_resB[j][i] = img->m7[j][i];
              img->m7[j][i]  = resTrans_R[j][i];
            }
          }
          // yuwen 2005.11.17 => should be an error here, requiring process for img->mb_data[img->current_mb_nr].cbp_bits
          rate += RDCost_for_4x4Blocks_Chroma (b8+8, b4, 1);
          
          reset_coding_state (cs_cm);
          for (j=0; j<4; j++)
          {
            for (i=0; i<4; i++)
            {
              rec_resR[j][i] = img->m7[j][i];
            }
          }
          
          for (j=0; j<4; j++)
          {
            for (i=0; i<4; i++)
            {
              /* Inverse Residue Transform */
              temp      = rec_resG[j][i]-(rec_resB[j][i]>>1);
              residue_G = rec_resB[j][i]+temp;
              residue_B = temp - (rec_resR[j][i]>>1);
              residue_R = residue_B+rec_resR[j][i];
              enc_picture->imgUV[0][pic_pix_y+j][pic_pix_x+i] = min(img->max_imgpel_value_uv,max(0,residue_B+(int)img->mprr_c[0][c_ipmode][block_y+j][block_x+i]));
              enc_picture->imgY[pic_pix_y+j][pic_pix_x+i]     = min(img->max_imgpel_value,max(0,residue_G+(int)img->mprr[ipmode][j][i]));
              enc_picture->imgUV[1][pic_pix_y+j][pic_pix_x+i] = min(img->max_imgpel_value_uv,max(0,residue_R+(int)img->mprr_c[1][c_ipmode][block_y+j][block_x+i]));
            }
          } 
          //===== get distortion (SSD) of 4x4 block =====
          distortion = 0;
          for (y=0; y<4; y++)
          {
            for (x=pic_pix_x; x<pic_pix_x+4; x++)
            {
#ifdef  INTERNAL_BIT_DEPTH_INCREASE
              distortion += SQR_DEPTH(imgY_org    [pic_pix_y+y][x], enc_picture->imgY    [pic_pix_y+y][x], input->BitDepthLuma,   img->BitDepthIncrease);
              distortion += SQR_DEPTH(imgUV_org[0][pic_pix_y+y][x], enc_picture->imgUV[0][pic_pix_y+y][x], input->BitDepthChroma, img->BitDepthIncreaseChroma);
              distortion += SQR_DEPTH(imgUV_org[1][pic_pix_y+y][x], enc_picture->imgUV[1][pic_pix_y+y][x], input->BitDepthChroma, img->BitDepthIncreaseChroma);
#else
              distortion += img->quad[imgY_org    [pic_pix_y+y][x] - enc_picture->imgY    [pic_pix_y+y][x]];
              distortion += img->quad[imgUV_org[0][pic_pix_y+y][x] - enc_picture->imgUV[0][pic_pix_y+y][x]];
              distortion += img->quad[imgUV_org[1][pic_pix_y+y][x] - enc_picture->imgUV[1][pic_pix_y+y][x]];
#endif
            }
          }
          rdcost = (double)distortion + lambda*(double)rate;
          
          if (rdcost < min_rdcost)
          {
            //--- set coefficients ---
            for (j=0; j<2; j++)
            {
              for (i=0; i<18;i++)  
                cofAC4x4[j][i]=img->cofAC[b8][b4][j][i];
              for (i=0; i<18;i++)  
                cofAC4x4_chroma[0][j][i]=img->cofAC[b8+4][b4][j][i];
              for (i=0; i<18;i++)
                cofAC4x4_chroma[1][j][i]=img->cofAC[b8+8][b4][j][i];
            }
            
            for (i=0; i<2; i++)
            { //uv
              dc_level        [i][2*(b8 & 0x01)+(b4 & 0x01)][2*(b8 >> 1)+(b4 >> 1)] = dc_level_temp        [i][2*(b8 & 0x01)+(b4 & 0x01)][2*(b8 >> 1)+(b4 >> 1)];
              cbp_chroma_block[i][2*(b8 & 0x01)+(b4 & 0x01)][2*(b8 >> 1)+(b4 >> 1)] = cbp_chroma_block_temp[i][2*(b8 & 0x01)+(b4 & 0x01)][2*(b8 >> 1)+(b4 >> 1)];
              //--- set reconstruction ---
              for (y=0; y<BLOCK_SIZE; y++)
              {
                memcpy(rec4x4_c[i][y],&enc_picture->imgUV[i][pic_pix_y+y][pic_pix_x], BLOCK_SIZE * sizeof(imgpel)); 
                if(img->type==SP_SLICE &&(!si_frame_indicator && !sp2_frame_indicator))
                  memcpy(lrec4x4[y],&lrec[pic_pix_y+y][pic_pix_x], BLOCK_SIZE * sizeof(int));//stores the coefficients for the mode
              }
            }
            
            //--- set reconstruction ---
            for (y=0; y<BLOCK_SIZE; y++)
            {
              memcpy(rec4x4[y],&enc_picture->imgY[pic_pix_y+y][pic_pix_x], BLOCK_SIZE * sizeof(imgpel));
              if(img->type==SP_SLICE &&(!si_frame_indicator && !sp2_frame_indicator))
                memcpy(lrec4x4[y],&lrec[pic_pix_y+y][pic_pix_x], BLOCK_SIZE * sizeof(int));
            }
            
            //--- flag if dct-coefficients must be coded ---
            nonzero = c_nz;
            
            //--- set best mode update minimum cost ---
            min_rdcost  = rdcost;
            best_ipmode = ipmode;
#ifdef BEST_NZ_COEFF
            best_nz_coeff = img->nz_coeff [img->current_mb_nr][block_x4][block_y4];
            // yuwen 2005.11.17 => should be an error here, requiring process for img->mb_data[img->current_mb_nr].cbp_bits
#endif
          }
        }
      }
    }
  }
  
#ifdef BEST_NZ_COEFF
  img->nz_coeff [img->current_mb_nr][block_x4][block_y4] = best_nz_coeff;
  cbp_bits &= (~(int64)(1<<bit_pos));
  cbp_bits |= (int64)(best_coded_block_flag<<bit_pos);
#ifdef ADAPTIVE_FD_SD_CODING
  FD_or_SD_bits &= (~(int64)(1<<bit_pos));
  FD_or_SD_bits |= (int64)(best_SD_or_FD_flag<<bit_pos);
#endif
  
#endif
  
#ifdef USE_INTRA_MDDT
  if(input->UseIntraMDDT)
    for (j=0; j<16; j++)
    {
      img->quant_stat[j]+=(quant_stat_best[j]>>5);
    }
#endif
    
    //===== set intra mode prediction =====
    img->ipredmode[pic_block_y][pic_block_x] = best_ipmode;
    img->mb_data[img->current_mb_nr].intra_pred_modes[4*b8+b4] = mostProbableMode == best_ipmode ? -1 : best_ipmode < mostProbableMode ? best_ipmode : best_ipmode-1;
    
    if (!input->rdopt)
    {
      // Residue Color Transform
      if(!img->residue_transform_flag)
      {
        // get prediction and prediction error
        for (j=0; j<4; j++)
        {
          int jj = pic_opix_y+j;
          for (i=0; i<4; i++)
          {
            img->mpr[block_y+j][block_x+i]  = img->mprr[best_ipmode][j][i];
            img->m7[j][i]                   = imgY_org[jj][pic_opix_x+i] - img->mprr[best_ipmode][j][i];
          }
        }
#ifdef USE_INTRA_MDDT
        if(input->UseIntraMDDT)
        {
          nonzero = (best_ipmode == 2) ? 
            dct_luma     (block_x, block_y, &dummy, 1, best_ipmode) : 
          klt_luma_sep (block_x, block_y, &dummy, best_ipmode);
        }
        else 
          nonzero = dct_luma     (block_x, block_y, &dummy, 1, best_ipmode);
#else
        nonzero = dct_luma (block_x, block_y, &dummy, 1);
#endif
      } 
      else 
      {
        int y_pos = 2*(b8 & 0x01)+(b4 & 0x01);
        int x_pos = 2*(b8 >> 1)+(b4 >> 1);
        for (j=0; j<4; j++)
        {
          for (i=0; i<4; i++)
          {
            residue_B = imgUV_org[0][pic_opix_y+j][pic_opix_x+i] - img->mprr_c[0][c_ipmode][block_y+j][block_x+i];
            residue_G = imgY_org[pic_opix_y+j][pic_opix_x+i] - img->mprr[best_ipmode][j][i];
            residue_R = imgUV_org[1][pic_opix_y+j][pic_opix_x+i] - img->mprr_c[1][c_ipmode][block_y+j][block_x+i];
            
            /* Forward Residue Transform */
            resTrans_R[j][i] = residue_R-residue_B;
            temp = residue_B+(resTrans_R[j][i]>>1);
            resTrans_B[j][i] = residue_G-temp;
            resTrans_G[j][i] = temp+(resTrans_B[j][i]>>1);
          }
        }
        
        for (j=0; j<4; j++)
        {
          for (i=0; i<4; i++)
          {
            img->m7[j][i]  = resTrans_G[j][i];
          }
        }
#ifdef USE_INTRA_MDDT
        nonzero = dct_luma (block_x, block_y, &dummy, 1, c_ipmode);
#else
        nonzero = dct_luma (block_x, block_y, &dummy, 1);
#endif
        for (j=0; j<4; j++)
        {
          for (i=0; i<4; i++)
          {
            rec_resG[j][i] = img->m7[j][i];
            img->m7[j][i]  = resTrans_B[j][i];
          }
        }
        cbp_chroma_block[0][y_pos][x_pos] = dct_chroma4x4 (0, b8+4, b4);
        dc_level        [0][y_pos][x_pos] = dc_level_temp[0][y_pos][x_pos];
        for (j=0; j<4; j++)
        {
          for (i=0; i<4; i++)
          {
            rec_resB[j][i] = img->m7[j][i];
            img->m7[j][i]  = resTrans_R[j][i];
          }
        }
        cbp_chroma_block[1][y_pos][x_pos] = dct_chroma4x4 (1, b8+8, b4);
        dc_level        [1][y_pos][x_pos] = dc_level_temp[1][y_pos][x_pos];
        for (j=0; j<4; j++)
        {
          for (i=0; i<4; i++)
          {
            rec_resR[j][i] = img->m7[j][i];
          }
        }
        for (j=0; j<4; j++)
        {
          for (i=0; i<4; i++)
          {
            /* Inverse Residue Transform */
            temp      = rec_resG[j][i]-(rec_resB[j][i]>>1);
            residue_G = rec_resB[j][i]+temp;
            residue_B = temp - (rec_resR[j][i]>>1);
            residue_R = residue_B+rec_resR[j][i];
            enc_picture->imgUV[0][pic_pix_y+j][pic_pix_x+i] = min(img->max_imgpel_value_uv,max(0,residue_B+(int)img->mprr_c[0][c_ipmode][block_y+j][block_x+i]));
            enc_picture->imgY[pic_pix_y+j][pic_pix_x+i]     = min(img->max_imgpel_value,max(0,residue_G+(int)img->mprr[best_ipmode][j][i]));
            enc_picture->imgUV[1][pic_pix_y+j][pic_pix_x+i] = min(img->max_imgpel_value_uv,max(0,residue_R+(int)img->mprr_c[1][c_ipmode][block_y+j][block_x+i]));
          }
        }
      }
  }
  else
  {
    //===== restore coefficients =====
    for (j=0; j<2; j++)
    {
      memcpy (img->cofAC[b8][b4][j],cofAC4x4[j], 18 * sizeof(int));
    } 
    // Residue Color Transform
    if(img->residue_transform_flag)
    {
      for (j=0; j<2; j++)
      {
        memcpy (img->cofAC[b8+4][b4][j],cofAC4x4_chroma[0][j], 18 * sizeof(int));            
        memcpy (img->cofAC[b8+8][b4][j],cofAC4x4_chroma[1][j], 18 * sizeof(int));            
      }
    }
    
    //===== restore reconstruction and prediction (needed if single coeffs are removed) =====
    for (y=0; y<BLOCK_SIZE; y++)
    {
      memcpy (&enc_picture->imgY[pic_pix_y+y][pic_pix_x],rec4x4[y],    BLOCK_SIZE * sizeof(imgpel));
      memcpy (&img->mpr[block_y+y][block_x],img->mprr[best_ipmode][y], BLOCK_SIZE * sizeof(imgpel));
      if(img->type==SP_SLICE &&(!si_frame_indicator && !sp2_frame_indicator))
        memcpy (&lrec[pic_pix_y+y][pic_pix_x],lrec4x4[y], BLOCK_SIZE * sizeof(int));//restore coefficients when encoding primary SP frame
    }
    
    if (img->AdaptiveRounding)
    {
      for (j=0; j<BLOCK_SIZE; j++)
        memcpy (&img->fadjust4x4[1][block_y+j][block_x],&fadjust4x4[block_y+j][block_x], BLOCK_SIZE * sizeof(int)); 
    }
    
    // Residue Color Transform
    if(img->residue_transform_flag)
    {
      for (i=0; i<2; i++)
      { //uv
        //--- set reconstruction ---
        for (y=0; y<4; y++)
        {
          memcpy(&enc_picture->imgUV[i][pic_pix_y+y][pic_pix_x],rec4x4_c[i][y], BLOCK_SIZE * sizeof(imgpel));
          if(img->type==SP_SLICE &&(!si_frame_indicator && !sp2_frame_indicator))
            memcpy (&lrec_uv[i][pic_pix_y+y][pic_pix_x],lrec4x4_c[i][y], BLOCK_SIZE * sizeof(int));//restore coefficients for primary SP frame encoding
        }
      }
    }    
  }  
  return nonzero;
}


/*!
*************************************************************************************
* \brief
*    Mode Decision for an 8x8 Intra block
*************************************************************************************
*/
int Mode_Decision_for_8x8IntraBlocks(int b8,double lambda,int *cost)
{
  int  nonzero=0, b4;
  int  cost4x4;
  
  *cost = (int)floor(6.0 * lambda + 0.4999);
  
  for (b4=0; b4<4; b4++)
  {

⌨️ 快捷键说明

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