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

📄 rdopt.c

📁 JM 11.0 KTA 2.1 Source Code
💻 C
📖 第 1 页 / 共 5 页
字号:
        }
        else if (dx == 2)
        {
          for (y = y_pos ; y < y_pos + BLOCK_SIZE ; y++)
            for (x = x_pos ; x < x_pos + BLOCK_SIZE ; x++)
            {
              for(yy=-2;yy<4;yy++) 
              {
                pres_y = max(0,min(maxold_y, yy + y));
                for(xx=-2;xx<4;xx++) 
                {
                  pres_x = max(0,min(maxold_x, xx + x));
                  if (pixel_map[pres_y][pres_x] < ref_frame)
                    return 0;
                }
              }
            }
        }
        else if (dy == 2)
        {
          for (y = y_pos ; y < y_pos + BLOCK_SIZE ; y++)
            for (x = x_pos ; x < x_pos + BLOCK_SIZE ; x++)
            {
              for(xx=-2;xx<4;xx++) 
              {
                pres_x = max(0,min(maxold_x, xx + x));
                for(yy=-2;yy<4;yy++) 
                {
                  pres_y = max(0,min(maxold_y, yy + y));
                  if (pixel_map[pres_y][pres_x] < ref_frame)
                    return 0;
                }
              }
            }
        }
        else
        {
          for (y = y_pos ; y < y_pos + BLOCK_SIZE ; y++)
          {
            for (x = x_pos ; x < x_pos + BLOCK_SIZE ; x++)
            {
              pres_y = dy == 1 ? y : y + 1;
              pres_y = max(0,min(maxold_y,pres_y));
              
              for(xx=-2;xx<4;xx++) 
              {
                pres_x = max(0,min(maxold_x,xx + x));
                if (pixel_map[pres_y][pres_x] < ref_frame)
                  return 0;
              }
              
              pres_x = dx == 1 ? x : x + 1;
              pres_x = max(0,min(maxold_x,pres_x));
              
              for(yy=-2;yy<4;yy++) 
              {
                pres_y = max(0,min(maxold_y, yy + y));
                if (pixel_map[pres_y][pres_x] < ref_frame)
                  return 0;
              }
            }
          }
        }        
      }
    }
  }
  return 1;
}



/*!
*************************************************************************************
* \brief
*    R-D Cost for an 4x4 Intra block
*************************************************************************************
*/
double RDCost_for_4x4IntraBlocks (int*    nonzero,
                                  int     b8,
                                  int     b4,
                                  int     ipmode,
                                  double  lambda,
                                  double  min_rdcost,
                                  int mostProbableMode)
{
  double  rdcost;
  int     dummy, x, y, rate;
  int64   distortion  = 0;
  int     block_x     = 8*(b8 & 0x01)+4*(b4 & 0x01);
  int     block_y     = 8*(b8 >> 1)+4*(b4 >> 1);
  int     pic_pix_x   = img->pix_x+block_x;
  int     pic_pix_y   = img->pix_y+block_y;
  int     pic_opix_y  = img->opix_y+block_y;
  imgpel  **imgY      = enc_picture->imgY;
  
  Slice          *currSlice    =  img->currentSlice;
  Macroblock     *currMB       = &img->mb_data[img->current_mb_nr];
  SyntaxElement  *currSE       = &img->MB_SyntaxElements[currMB->currSEnr];
  const int      *partMap      = assignSE2partition[input->partition_mode];
  DataPartition  *dataPart;
  
  //===== perform DCT, Q, IQ, IDCT, Reconstruction =====
  dummy = 0;
  
  if(img->type!=SP_SLICE)
#ifdef USE_INTRA_MDDT
    if(input->UseIntraMDDT)
    {
      *nonzero = (ipmode == 2) ? 
        dct_luma (block_x, block_y, &dummy, 1, ipmode): 
      klt_luma_sep (block_x, block_y, &dummy, ipmode);
    }
    else 
      *nonzero = dct_luma (block_x, block_y, &dummy, 1, ipmode);
#else
    *nonzero = dct_luma (block_x, block_y, &dummy, 1);
#endif
    else if(!si_frame_indicator && !sp2_frame_indicator)
    {
      *nonzero = dct_luma_sp(block_x, block_y, &dummy);
    }
    else
    {   
      *nonzero = dct_luma_sp2(block_x, block_y, &dummy);
    }
    
    //===== get distortion (SSD) of 4x4 block =====
    if(!img->residue_transform_flag)
    {
      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_opix_y+y][x], imgY[pic_pix_y+y][x], input->BitDepthLuma, img->BitDepthIncrease);
#else
          distortion += img->quad [imgY_org[pic_opix_y+y][x] - imgY[pic_pix_y+y][x]];
#endif
        }
      }
    }
    
    //===== 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 = 4*b8 + b4;
    currSE->type    = SE_INTRAPREDMODE;
    
    //--- choose data partition ---
    dataPart = &(currSlice->partArr[partMap[SE_INTRAPREDMODE]]);
    //--- encode and update rate ---
    if (input->symbol_mode == UVLC)    
      writeSyntaxElement_Intra4x4PredictionMode(currSE, dataPart);
    else
    {
      currSE->writing = writeIntraPredMode_CABAC;
      dataPart->writeSyntaxElement (currSE, dataPart);
    }
    
    rate = currSE->len;
    currSE++;
    currMB->currSEnr++;
    
    //===== RATE for LUMINANCE COEFFICIENTS =====
    if (input->symbol_mode == UVLC)
    {
      rate  += writeCoeff4x4_CAVLC (LUMA, b8, b4, 0);
    }
    else
    {
      rate  += writeLumaCoeff4x4_CABAC (b8, b4, 1);
    }
    //reset_coding_state (cs_cm);
    rdcost = (double)distortion + lambda*(double)rate;
    
    if(img->residue_transform_flag)
      return (double)rate;
    else
      return rdcost;
                                  }
                                  
                                  
                                  // Residue Color Transform
int RDCost_for_4x4Blocks_Chroma (int     b8,
                                 int     b4,
                                 int  chroma)
{
  int     rate=0;
  
  Slice          *currSlice    =  img->currentSlice;
  Macroblock     *currMB       = &img->mb_data[img->current_mb_nr];
  SyntaxElement  *currSE       = &img->MB_SyntaxElements[currMB->currSEnr];
  const int      *partMap      = assignSE2partition[input->partition_mode];
  int uv;
  
  //===== perform DCT, Q, IQ, IDCT, Reconstruction =====
  if(b8 > 7) 
    uv = 1;
  else 
    uv = 0;
  
  cbp_chroma_block_temp[uv][2*((b8-4*(uv+1)) & 0x01)+(b4 & 0x01)][2*((b8-4*(uv+1)) >> 1)+(b4 >> 1)] = dct_chroma4x4 (chroma, b8, b4);
  
  //===== RATE for LUMINANCE COEFFICIENTS =====
  if (input->symbol_mode == UVLC)
  {
    rate  = writeCoeff4x4_CAVLC (CHROMA_AC, b8, b4, ((2*(b8 & 0x01)+ (b4 & 0x01))<<4) | (2*(b8 >> 1)+(b4 >> 1)));
  }
  else
  {
    int * ACLevel, * ACRun;
    int level, run, k;
    DataPartition*  dataPart;
    int*            bitCount  = currMB->bitcounter;
    ACLevel = img->cofAC[b8][b4][0];
    ACRun   = img->cofAC[b8][b4][1];
    
    level=1;
    
    img->subblock_y = b4 >> 1;
    img->subblock_x = b4 & 0x01;
    
    for (k=0; k < 17 && level != 0; k++)
    {
      level = currSE->value1 = ACLevel[k]; // level
      run   = currSE->value2 = ACRun  [k]; // run
      
      if (input->symbol_mode == UVLC)   
        currSE->mapping = levrun_linfo_inter;
      else                              
        currSE->writing = writeRunLevel_CABAC;
      
      currSE->context     = CHROMA_AC;
      currSE->type        = SE_CHR_AC_INTRA;
      
      img->is_intra_block =  IS_INTRA(currMB);
      img->is_v_block     = uv;
      
      // choose the appropriate data partition
      dataPart = &(currSlice->partArr[partMap[currSE->type]]);
      dataPart->writeSyntaxElement (currSE, dataPart);
      bitCount[BITS_COEFF_UV_MB] += currSE->len;
      rate                       += currSE->len;
      
      // proceed to next SE
      currSE++;
      currMB->currSEnr++;
    }
  }
  reset_coding_state (cs_cm);
  
  return rate;
}


/*!
*************************************************************************************
* \brief
*    Mode Decision for an 4x4 Intra block
*************************************************************************************
*/
int Mode_Decision_for_4x4IntraBlocks (int  b8,  int  b4,  double  lambda,  int*  min_cost)
{
  int     ipmode, best_ipmode = 0, i, j, k, x, y, cost, dummy;
  int     c_nz, nonzero = 0, diff[16];
  imgpel  rec4x4[4][4];
  double  rdcost;
  int     block_x     = 8*(b8 & 0x01)+4*(b4 & 0x01);
  int     block_y     = 8*(b8 >> 1)+4*(b4 >> 1);
  int     block_x4    = block_x >> 2;
  int     block_y4    = block_y >> 2;
  int     pic_pix_x   = img->pix_x+block_x;
  int     pic_pix_y   = img->pix_y+block_y;
  int     pic_opix_x   = img->opix_x+block_x;
  int     pic_opix_y   = img->opix_y+block_y;
  int     pic_block_x = pic_pix_x >> 2;
  int     pic_block_y = pic_pix_y >> 2;
  double  min_rdcost  = 1e30;
  
  int left_available, up_available, all_available;
  
  char   upMode;
  char   leftMode;
  int     mostProbableMode;
  
  PixelPos left_block;
  PixelPos top_block;
  
  int     lrec4x4[4][4]; 
  int     lrec4x4_c[2][4][4];
  // Residue Color Transform
  int residue_R, residue_G, residue_B;
  int rate, temp;
  int64 distortion;
  int c_ipmode = img->mb_data[img->current_mb_nr].c_ipred_mode;
  int fixedcost = (int) floor(4 * lambda );
  imgpel rec4x4_c[2][4][4];
#ifdef USE_INTRA_MDDT
  long quant_stat_best[16];
#endif
#ifdef BEST_NZ_COEFF
  int best_nz_coeff = 0;
  int best_coded_block_flag = 0;
#ifdef ADAPTIVE_FD_SD_CODING
  int best_SD_or_FD_flag = 0;
#endif
  int bit_pos = 1 + ((((b8>>1)<<1)+(b4>>1))<<2) + (((b8&1)<<1)+(b4&1));
  static int64 cbp_bits;
#ifdef ADAPTIVE_FD_SD_CODING
  static int64 FD_or_SD_bits;
#endif
  
#ifdef ADAPTIVE_FD_SD_CODING
  if (b8==0 && b4==0)
  {
#else
    if (b8==0 && b4==0)
#endif
      cbp_bits = 0;
#ifdef ADAPTIVE_FD_SD_CODING
    FD_or_SD_bits = 0;
  }
#endif
  
#endif
  
  getLuma4x4Neighbour(img->current_mb_nr, block_x4, block_y4, -1,  0, &left_block);
  getLuma4x4Neighbour(img->current_mb_nr, block_x4, block_y4,  0, -1, &top_block);
  
  // constrained intra pred
  if (input->UseConstrainedIntraPred)
  {
    left_block.available = left_block.available ? img->intra_block[left_block.mb_addr] : 0;
    top_block.available  = top_block.available  ? img->intra_block[top_block.mb_addr]  : 0;
  }
  
  upMode            =  top_block.available ? img->ipredmode[top_block.pos_y ][top_block.pos_x ] : -1;
  leftMode          = left_block.available ? img->ipredmode[left_block.pos_y][left_block.pos_x] : -1;
  
  mostProbableMode  = (upMode < 0 || leftMode < 0) ? DC_PRED : upMode < leftMode ? upMode : leftMode;
  
  *min_cost = INT_MAX;
  
  //===== INTRA PREDICTION FOR 4x4 BLOCK =====
  intrapred_luma (pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
  
  //===== LOOP OVER ALL 4x4 INTRA PREDICTION MODES =====
  for (ipmode=0; ipmode<NO_INTRA_PMODE; ipmode++)
  {
    int available_mode =  (ipmode==DC_PRED) ||
      ((ipmode==VERT_PRED||ipmode==VERT_LEFT_PRED||ipmode==DIAG_DOWN_LEFT_PRED) && up_available ) ||
      ((ipmode==HOR_PRED||ipmode==HOR_UP_PRED) && left_available ) ||(all_available);
    
    if (input->IntraDisableInterOnly==0 || img->type != I_SLICE)
    {
      if (input->Intra4x4ParDisable && (ipmode==VERT_PRED||ipmode==HOR_PRED))
        continue;      
      
      if (input->Intra4x4DiagDisable && (ipmode==DIAG_DOWN_LEFT_PRED||ipmode==DIAG_DOWN_RIGHT_PRED))
        continue;      
      
      if (input->Intra4x4DirDisable && ipmode>=VERT_RIGHT_PRED)
        continue;
    }
    
    if( available_mode)
    {
      if (!input->rdopt)
      {
        for (k=j=0; j<4; j++)
        {
          int jj = pic_opix_y+j;
          for (i=0; i<4; i++, k++)
          {
            diff[k] = imgY_org[jj][pic_opix_x+i] - img->mprr[ipmode][j][i];
          }
        }
        //cost  = (ipmode == mostProbableMode) ? 0 : (int)floor(4 * lambda );
        cost  = (ipmode == mostProbableMode) ? 0 : fixedcost;
        cost += SATD (diff, input->hadamard);
        if (cost < *min_cost)
        {
          best_ipmode = ipmode;
          *min_cost   = cost;
        }
      }
      else
      {
        // Residue Color Transform
        if(!img->residue_transform_flag)
        {
          // get prediction and prediction error
          for (j=0; j<4; j++)
          {

⌨️ 快捷键说明

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