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

📄 rdopt.c

📁 h.264 影像壓縮 必須在 .net 的環境 下操作
💻 C
📖 第 1 页 / 共 5 页
字号:
        {
          for (y = y_pos ; y < y_pos + BLOCK_SIZE ; y++)
          {
            pres_y = iClip3(0, maxold_y, y);
            for (x = x_pos ; x < x_pos + BLOCK_SIZE ; x++)
            {
              for(xx = -2 ; xx < 4 ; xx++) {
                pres_x = iClip3(0, maxold_x, x + xx);
                if (pixel_map[pres_y][pres_x] < ref_frame)
                  return 0;
              }
            }
          }
        }
        else if (dx == 0)
        {
          for (y = y_pos ; y < y_pos + BLOCK_SIZE ; y++)
            for (x=x_pos ; x < x_pos + BLOCK_SIZE ; x++)
            {
              pres_x = iClip3(0,maxold_x,x);
              for(yy=-2;yy<4;yy++) {
                pres_y = iClip3(0,maxold_y, yy + y);
                if (pixel_map[pres_y][pres_x] < ref_frame)
                  return 0;
              }
            }
        }
        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 = iClip3(0,maxold_y, yy + y);
                for(xx=-2;xx<4;xx++) {
                  pres_x = iClip3(0,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 = iClip3(0,maxold_x, xx + x);
                for(yy=-2;yy<4;yy++) {
                  pres_y = iClip3(0,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 = iClip3(0,maxold_y,pres_y);

              for(xx=-2;xx<4;xx++)
              {
                pres_x = iClip3(0,maxold_x,xx + x);
                if (pixel_map[pres_y][pres_x] < ref_frame)
                  return 0;
              }

              pres_x = dx == 1 ? x : x + 1;
              pres_x = iClip3(0,maxold_x,pres_x);

              for(yy=-2;yy<4;yy++)
              {
                pres_y = iClip3(0,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 (Macroblock    *currMB,
                                  int*    nonzero,
                                  int     b8,
                                  int     b4,
                                  int     ipmode,
                                  double  lambda,
                                  int mostProbableMode,
                                  int c_nzCbCr[3])
{
  double  rdcost;
  int     dummy = 0, x, y, rate;
  int64   distortion  = 0;
  int     block_x     = ((b8 & 0x01) << 3) + ((b4 & 0x01) << 2);
  int     block_y     = ((b8 >> 1) << 3) + ((b4 >> 1) << 2);
  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  *img_org, *img_enc;

  Slice          *currSlice = img->currentSlice;
  SyntaxElement  se;
  const int      *partMap   = assignSE2partition[input->partition_mode];
  DataPartition  *dataPart;

  //===== perform DCT, Q, IQ, IDCT, Reconstruction =====
  select_dct(currMB);
  *nonzero = pDCT_4x4 (currMB, PLANE_Y, block_x, block_y, &dummy, 1);

  //===== get distortion (SSD) of 4x4 block =====
  for (y=0; y<4; y++)
  {
    img_org = pCurImg[pic_opix_y+y];
    img_enc = enc_picture->imgY[pic_pix_y+y];
    for (x=pic_pix_x; x<pic_pix_x+4; x++)
    {
      distortion += iabs2( img_org[x] - img_enc[x] );
    }
  }
  if((active_sps->chroma_format_idc == YUV444) && (IS_INDEPENDENT(input)==0))
  {
    ColorPlane k;
    for (k = PLANE_U; k <= PLANE_V; k++)
    {
      select_plane(k);
      c_nzCbCr[k] = pDCT_4x4(currMB, k, block_x, block_y, &dummy, 1);

      for( y = 0; y < 4; y++ )
      {
        for (x=pic_pix_x; x<pic_pix_x+4; x++)
        {
          distortion +=iabs2( pCurImg[pic_opix_y+y][x] - enc_picture->p_curr_img[pic_pix_y+y][x] );
        }
      }
    }
    ipmode_DPCM=NO_INTRA_PMODE;  //For residual DPCM
    select_plane(PLANE_Y);
  }
  else if((active_sps->chroma_format_idc == YUV444) && IS_INDEPENDENT(input))   //For residual DPCM
  {
    ipmode_DPCM=NO_INTRA_PMODE;  
  }


  //===== RATE for INTRA PREDICTION MODE  (SYMBOL MODE MUST BE SET TO CAVLC) =====
  se.value1 = (mostProbableMode == ipmode) ? -1 : ipmode < mostProbableMode ? ipmode : ipmode - 1;

  //--- set position and type ---
  se.context = (b8 << 2) + b4;
  se.type    = SE_INTRAPREDMODE;

  //--- choose data partition ---
  dataPart = &(currSlice->partArr[partMap[SE_INTRAPREDMODE]]);
  //--- encode and update rate ---
  writeIntraPredMode (&se, dataPart);
  rate = se.len;

  //===== RATE for LUMINANCE COEFFICIENTS =====
  if (input->symbol_mode == CAVLC)
  {
    rate  += writeCoeff4x4_CAVLC (currMB, LUMA, b8, b4, 0);
    if((active_sps->chroma_format_idc == YUV444) && (IS_INDEPENDENT(input)==0) )  
    {
      rate  += writeCoeff4x4_CAVLC (currMB, CB, b8, b4, 0);
      rate  += writeCoeff4x4_CAVLC (currMB, CR, b8, b4, 0);
    }
  }
  else
  {
    rate  += writeCoeff4x4_CABAC (currMB, PLANE_Y, b8, b4, 1);
    if((active_sps->chroma_format_idc == YUV444) && (IS_INDEPENDENT(input)==0) )  
    {
      rate  += writeCoeff4x4_CABAC (currMB, PLANE_U, b8, b4, 1);
      rate  += writeCoeff4x4_CABAC (currMB, PLANE_V, b8, b4, 1);
    }
  }
  //reset_coding_state (currMB, cs_cm);
  rdcost = (double)distortion + lambda * (double) rate;

  return rdcost;
}


/*!
 *************************************************************************************
 * \brief
 *    Mode Decision for an 4x4 Intra block
 *************************************************************************************
 */
int Mode_Decision_for_4x4IntraBlocks (Macroblock *currMB, int  b8,  int  b4,  double  lambda,  double*  min_cost, int cr_cbp[3])
{
  int     ipmode, best_ipmode = 0, i, j, y, cost, dummy;
  int     c_nz, nonzero = 0;
  int*    ACLevel = img->cofAC[b8][b4][0];
  int*    ACRun   = img->cofAC[b8][b4][1];
  static imgpel *org_img, *prd_img;
  static int *m7;
  int    c_nzCbCr[3]= {999,999, 999};
  static imgpel  rec4x4[4][4];
  static imgpel  rec4x4CbCr[2][4][4];
  int    uv;
  
  double rdcost;
  int    block_x     = ((b8 & 0x01) << 3) + ((b4 & 0x01) << 2);
  int    block_y     = ((b8 >> 1) << 3)  + ((b4 >> 1) << 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 *d;

  int left_available, up_available, all_available;
  imgpel (*curr_mpr)[16] = img->mpr[0];

  char   upMode;
  char   leftMode;
  int    mostProbableMode;

  PixelPos left_block;
  PixelPos top_block;

  int  lrec4x4[4][4];
  int  fixedcost = (int) floor(4 * lambda );

  //For residual DPCM
  Boolean lossless_qpprime = ((currMB->qp + img->bitdepth_luma_qp_scale)==0 && img->lossless_qpprime_flag==1);  

#ifdef BEST_NZ_COEFF
  int best_nz_coeff = 0;
  int best_coded_block_flag = 0;
  int bit_pos = 1 + ((((b8>>1)<<1)+(b4>>1))<<2) + (((b8&1)<<1)+(b4&1));
  static int64 cbp_bits;

  if (b8==0 && b4==0)
   cbp_bits = 0;
#endif

  getLuma4x4Neighbour(currMB, block_x - 1, block_y,     &left_block);
  getLuma4x4Neighbour(currMB, block_x,     block_y - 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;

  ipmode_DPCM = NO_INTRA_PMODE; ////For residual DPCM

  //===== INTRA PREDICTION FOR 4x4 BLOCK =====
  intrapred_4x4 (currMB, PLANE_Y, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);

  if ((img->yuv_format == YUV444) && !IS_INDEPENDENT(input))
  {
    select_plane(PLANE_U);
    intrapred_4x4 (currMB, PLANE_U, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
    select_plane(PLANE_V);
    intrapred_4x4 (currMB, PLANE_V, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
    select_plane(PLANE_Y);
  }

  //===== LOOP OVER ALL 4x4 INTRA PREDICTION MODES =====
  for (ipmode = 0; ipmode < NO_INTRA_PMODE; ipmode++)
  {
    int available_mode =  (all_available) || (ipmode==DC_PRED) ||
      (up_available && (ipmode==VERT_PRED||ipmode==VERT_LEFT_PRED||ipmode==DIAG_DOWN_LEFT_PRED)) ||
      (left_available && (ipmode==HOR_PRED||ipmode==HOR_UP_PRED));

    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)
      {
        d = &diff[0];
        for (j = 0; j<4; j++)
        {
          org_img = &pCurImg[pic_opix_y+j][pic_opix_x];
          prd_img = img->mpr_4x4[0][ipmode][j];
          
          for (i=0; i<4; i++)
          {
            *d++ = *org_img++ - *prd_img++;
          }
        }
        cost  = (ipmode == mostProbableMode) ? 0 : fixedcost;
        cost += distortion4x4 (diff);
        if ((img->yuv_format == YUV444) && !IS_INDEPENDENT(input))
        {
          int k;
          for (k=j=0; j<4; j++)
          {
            for (i=0; i<4; i++, k++)
            {
              diff[k] = imgUV_org[0][pic_opix_y+j][pic_opix_x+i] - img->mpr_4x4[1][ipmode][j][i]; 
            }
          }
          
          cost += distortion4x4 (diff);
          for (k=j=0; j<4; j++)
          {
            for (i=0; i<4; i++, k++)
            {
              diff[k] = imgUV_org[1][pic_opix_y+j][pic_opix_x+i] - img->mpr_4x4[2][ipmode][j][i]; 
            }
          }
          cost += distortion4x4 (diff);
        }
        if (cost < *min_cost)
        {
          best_ipmode = ipmode;
          *min_cost   = cost;
        }
      }
      else
      {
        // get prediction and prediction error
        for (j=0; j<4; j++)
        {
          org_img = &pCurImg[pic_opix_y+j][pic_opix_x];
          prd_img = img->mpr_4x4[0][ipmode][j];
          m7 = &img->m7[0][block_y+j][block_x];
          
          memcpy(&curr_mpr[block_y+j][block_x], prd_img, BLOCK_SIZE * sizeof(imgpel));
          for (i=0; i<4; i++)
          {
            *m7++ = (int) (*org_img++ - *prd_img++);
          }
        }
        if ((img->yuv_format == YUV444) && !IS_INDEPENDENT(input))
        {
          for (uv = 1; uv < 3; uv++)
          {
            for (j=0; j<4; j++)
            {
              org_img = &imgUV_org[uv - 1][pic_opix_y+j][pic_opix_x];
              prd_img = img->mpr_4x4[uv][ipmode][j];
              m7 = &img->m7[uv][block_y+j][block_x];

              memcpy(&curr_mpr[block_y+j][block_x], prd_img, BLOCK_SIZE * sizeof(imgpel));
              for (i=0; i<4; i++)
              {
                *m7++ = (int) (*org_img++ - *prd_img++);
              }
            }
          }
        }

          if ((img->yuv_format == YUV444) && !IS_INDEPENDENT(input))  //For residual DPCM
          {
            if((lossless_qpprime)&&(ipmode<2))  
            {
              Residual_DPCM_4x4(ipmode, 0, block_y, block_x);
              Residual_DPCM_4x4(ipmode, 1, block_y, block_x);
              Residual_DPCM_4x4(ipmode, 2, block_y, block_x);

⌨️ 快捷键说明

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