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

📄 rdopt.c

📁 H.264编码实现
💻 C
📖 第 1 页 / 共 5 页
字号:
        for (j = 0; j < MB_BLOCK_SIZE; j++)
        {
          memcpy (&lrec[img->pix_y+j][img->pix_x],tr4x4.lrec[j], MB_BLOCK_SIZE * sizeof(int)); // restore coeff. SP frame
        }
      }

      memset( img->cofAC[0][0][0], 0, 4 * 4 * 2 * 65 * sizeof(int));

      if (img->P444_joined)
      {
        for (j = 0; j < MB_BLOCK_SIZE; j++)
        {
          memcpy (&enc_picture->imgUV[0][img->pix_y+j][img->pix_x],tr4x4.mpr8x8CbCr[0][j], MB_BLOCK_SIZE * sizeof(imgpel));    
          memcpy (&enc_picture->imgUV[1][img->pix_y+j][img->pix_x],tr4x4.mpr8x8CbCr[1][j], MB_BLOCK_SIZE * sizeof(imgpel));  
        }
        for (uv=0; uv<2; uv++)
        {
          for (block = 0; block<4; block++)
          {
            memset( img->cofAC[4+block+uv*4][0][0], 0, 4 * 2 * 65 * sizeof(int));
          }
        }
      }
    }
    else
    {
      currMB->cbp     = cbp8x8;
      currMB->cbp_blk = cbp_blk8x8;
      for (j = 0; j < MB_BLOCK_SIZE; j++)
      {
        memcpy (&enc_picture->imgY[img->pix_y+j][img->pix_x],tr4x4.rec_mbY8x8[j], MB_BLOCK_SIZE * sizeof(imgpel));
      }

      if (params->rdopt == 3)
      {
        errdo_get_best_block(img, enc_picture->p_dec_img[0], decs->dec_mbY8x8, 0, MB_BLOCK_SIZE);
      }

      if(img->type==SP_SLICE &&(!si_frame_indicator && !sp2_frame_indicator))
      {
        for (j = 0; j < MB_BLOCK_SIZE; j++)
        {
          memcpy (&lrec[img->pix_y+j][img->pix_x],tr4x4.lrec[j], MB_BLOCK_SIZE * sizeof(int));
        }
      }
      if (img->P444_joined)
      {
        cmp_cbp[1] = cmp_cbp[2] = cbp8x8;
        for (j = 0; j < MB_BLOCK_SIZE; j++)
        {
          memcpy (&enc_picture->imgUV[0][img->pix_y+j][img->pix_x],tr4x4.rec_mb8x8_cr[0][j], MB_BLOCK_SIZE * sizeof(imgpel));
          memcpy (&enc_picture->imgUV[1][img->pix_y+j][img->pix_x],tr4x4.rec_mb8x8_cr[1][j], MB_BLOCK_SIZE * sizeof(imgpel));
        }
      }
    }
  }
}


/*!
 *************************************************************************************
 * \brief
 *    Sets motion vectors for a macroblock
 *************************************************************************************
 */
void SetMotionVectorsMB (Macroblock* currMB, int bframe)
{
  int i, j, k, l, mode8, pdir8, ref, by, bx;
  short ******all_mv  = img->all_mv;
  short ******pred_mv = img->pred_mv;
  short bipred_me8;
  int  l1_ref;
  int jdiv, jmod;

  // copy all the motion vectors into rdopt structure
  // Can simplify this by copying the MV's of the best mode (TBD)
  // Should maybe add code to check for Intra only profiles
  if (img->MbaffFrameFlag || (params->UseRDOQuant && params->RDOQ_QP_Num > 1))
  {
    memcpy(&rdopt->pred_mv [0][0][0][0][0][0], &pred_mv [0][0][0][0][0][0], 2 * img->max_num_references * 9 * 4 * 4 * 2 * sizeof(short));
    memcpy(&rdopt->all_mv  [0][0][0][0][0][0], &all_mv  [0][0][0][0][0][0], 2 * img->max_num_references * 9 * 4 * 4 * 2 * sizeof(short));
  }

  if (!bframe)
  {
    for (j = 0; j<4; j++)
    {
      jmod = j & 0x01;
      jdiv = j >>   1;
      by    = img->block_y+j;
      for (i = 0; i<4; i++)
      {
        mode8 = currMB->b8mode[k=2*jdiv+(i>>1)];
        l     = 2*jmod + (i & 0x01);

        bx   = img->block_x+i;

        pdir8 = currMB->b8pdir[k];
        ref    = enc_picture->motion.ref_idx[LIST_0][by][bx];

        if (pdir8>=0)
        {
          enc_picture->motion.mv[LIST_0][by][bx][0] = all_mv [LIST_0][ ref][mode8][j][i][0];
          enc_picture->motion.mv[LIST_0][by][bx][1] = all_mv [LIST_0][ ref][mode8][j][i][1];
        }
        else
        {
          enc_picture->motion.mv[LIST_0][by][bx][0] = 0;
          enc_picture->motion.mv[LIST_0][by][bx][1] = 0;
        }
      }
    }
  }
  else
  {
    for (j = 0; j<4; j++)
    {
      jmod = j & 0x01;
      jdiv = j >>   1;
      by    = img->block_y+j;
      for (i = 0; i<4; i++)
      {
        mode8 = currMB->b8mode[k=2*jdiv+(i>>1)];
        bipred_me8  = currMB->bipred_me[k]; 
        l     = 2*jmod + (i & 0x01);

        bx    = img->block_x+i;

        pdir8 = currMB->b8pdir[k];
        ref    = enc_picture->motion.ref_idx[LIST_0][by][bx];
        l1_ref = enc_picture->motion.ref_idx[LIST_1][by][bx];

        if ( bipred_me8 && (pdir8 == 2) && is_bipred_enabled(currMB->mb_type))
        {
          all_mv  = img->bipred_mv[bipred_me8 - 1]; 
          ref = 0;
          l1_ref = 0;
        }
        else
        {
          all_mv  = img->all_mv;
        }

        if (pdir8==-1) // intra
        {
          enc_picture->motion.mv[LIST_0][by][bx][0] = 0;
          enc_picture->motion.mv[LIST_0][by][bx][1] = 0;
          enc_picture->motion.mv[LIST_1][by][bx][0] = 0;
          enc_picture->motion.mv[LIST_1][by][bx][1] = 0;
        }
        else if (pdir8==0) // list 0
        {
          enc_picture->motion.mv[LIST_0][by][bx][0]   = all_mv [LIST_0][ ref][mode8][j][i][0];
          enc_picture->motion.mv[LIST_0][by][bx][1]   = all_mv [LIST_0][ ref][mode8][j][i][1];
          enc_picture->motion.mv[LIST_1][by][bx][0]   = 0;
          enc_picture->motion.mv[LIST_1][by][bx][1]   = 0;
          enc_picture->motion.ref_idx[LIST_1][by][bx] = -1;
        }
        else if (pdir8==1) // list 1
        {
          enc_picture->motion.mv[LIST_0][by][bx][0]   = 0;
          enc_picture->motion.mv[LIST_0][by][bx][1]   = 0;
          enc_picture->motion.ref_idx[LIST_0][by][bx] = -1;
          enc_picture->motion.mv[LIST_1][by][bx][0]   = all_mv [LIST_1][l1_ref][mode8][j][i][0];
          enc_picture->motion.mv[LIST_1][by][bx][1]   = all_mv [LIST_1][l1_ref][mode8][j][i][1];
        }
        else if (pdir8==2) // bipredictive
        {
          enc_picture->motion.mv[LIST_0][by][bx][0] = all_mv [LIST_0][   ref][mode8][j][i][0];
          enc_picture->motion.mv[LIST_0][by][bx][1] = all_mv [LIST_0][   ref][mode8][j][i][1];
          enc_picture->motion.mv[LIST_1][by][bx][0] = all_mv [LIST_1][l1_ref][mode8][j][i][0];
          enc_picture->motion.mv[LIST_1][by][bx][1] = all_mv [LIST_1][l1_ref][mode8][j][i][1];

          // copy all the motion vectors into rdopt structure
          // Can simplify this by copying the MV's of the best mode (TBD)
          // Should maybe add code to check for Intra only profiles
          if (img->MbaffFrameFlag || (params->UseRDOQuant && params->RDOQ_QP_Num > 1))
          {
            rdopt->all_mv [LIST_0][   ref][mode8][j][i][0] = all_mv [LIST_0][   ref][mode8][j][i][0];
            rdopt->all_mv [LIST_0][   ref][mode8][j][i][1] = all_mv [LIST_0][   ref][mode8][j][i][1];
            rdopt->all_mv [LIST_1][l1_ref][mode8][j][i][0] = all_mv [LIST_1][l1_ref][mode8][j][i][0];
            rdopt->all_mv [LIST_1][l1_ref][mode8][j][i][1] = all_mv [LIST_1][l1_ref][mode8][j][i][1];
          }
        }
        else
        {
          error("invalid direction mode", 255);
        }
      }
    }
  }
}



/*!
 *************************************************************************************
 * \brief
 *    R-D Cost for a macroblock
 *************************************************************************************
 */
int RDCost_for_macroblocks (Macroblock  *currMB,   // <-- Current Macroblock to code
                            double   lambda,       // <-- lagrange multiplier
                            int      mode,         // <-- modus (0-COPY/DIRECT, 1-16x16, 2-16x8, 3-8x16, 4-8x8(+), 5-Intra4x4, 6-Intra16x16)
                            double*  min_rdcost,   // <-> minimum rate-distortion cost
                            double*  min_dcost,   // <-> distortion of mode which has minimum rate-distortion cost.
                            double*  min_rate,     // --> bitrate of mode which has minimum rate-distortion cost.
                            int i16mode,
                            int is_cavlc)
{
  int         i, j, k; //, k, ****ip4;
  int         j1, j2;
  int         rate = 0, coeff_rate = 0;
  int64       distortion = 0;
  double      rdcost;
  int         prev_mb_nr  = FmoGetPreviousMBNr(img->current_mb_nr);  
  Macroblock  *prevMB   = (prev_mb_nr >= 0) ? &img->mb_data[prev_mb_nr] : NULL;
  int         bframe    = (img->type==B_SLICE);
  int         tmp_cc;
  int         use_of_cc =  (img->type!=I_SLICE &&  is_cavlc);
  int         cc_rate, dummy;
  double      dummy_d;
  imgpel     (*mb_pred)[16] = img->mb_pred[0];
  imgpel     (*curr_mpr_16x16)[16][16] = img->mpr_16x16[0];

  //=====
  //=====  SET REFERENCE FRAMES AND BLOCK MODES
  //=====
  SetModesAndRefframeForBlocks (currMB, mode);
  
  //=====
  //=====  GET COEFFICIENTS, RECONSTRUCTIONS, CBP
  //=====
  if (bframe && mode==0)
  {
    int block_x = (img->pix_x >> 2);
    int block_y = (img->pix_y >> 2);
    for (j = block_y; j < block_y + 4;j++)
      for (i = block_x; i < block_x + 4;i++)
        if (direct_pdir[j][i] < 0)
          return 0;
  }

  // Test MV limits for Skip Mode. This could be necessary for MBAFF case Frame MBs.
  if ((img->MbaffFrameFlag) && (!currMB->mb_field) && (img->type==P_SLICE) && (mode==0) )
  {
    if (out_of_bounds_mvs(img, img->all_mv[0][0][0][0][0], Q_PEL))
      return 0;
  }

  if (img->AdaptiveRounding)
  {
    memset(&(img->fadjust4x4[0][0][0]), 0, MB_PIXELS * sizeof(int));
    memset(&(img->fadjust8x8[0][0][0]), 0, MB_PIXELS * sizeof(int));
    if (img->yuv_format != 0)
    {
      memset(&(img->fadjust4x4Cr[0][0][0][0]), 0, img->mb_cr_size_y * img->mb_cr_size_x * sizeof(int));
      memset(&(img->fadjust4x4Cr[1][0][0][0]), 0, img->mb_cr_size_y * img->mb_cr_size_x  * sizeof(int));
      memset(&(img->fadjust8x8Cr[0][0][0][0]), 0, img->mb_cr_size_y * img->mb_cr_size_x  * sizeof(int));
      memset(&(img->fadjust8x8Cr[1][0][0][0]), 0, img->mb_cr_size_y * img->mb_cr_size_x  * sizeof(int));
    }
  }

  if (mode<P8x8)
  {
    LumaResidualCoding (currMB, is_cavlc);

    // This code seems unnecessary 
    if(mode==0 && currMB->cbp!=0 && (img->type != B_SLICE || img->NoResidueDirect==1))
      return 0;
    if (img->P444_joined)      
    {
      if(mode==0 && (currMB->cbp==0 && cmp_cbp[1] == 0 && cmp_cbp[2] == 0)&& currMB->luma_transform_size_8x8_flag == 1) //for B_skip, luma_transform_size_8x8_flag=0 only
        return 0;
    }
    else
    {
      if(mode==0 && currMB->cbp==0 && currMB->luma_transform_size_8x8_flag == 1) //for B_skip, luma_transform_size_8x8_flag=0 only        
        return 0;
    }
  }
  else if (mode==P8x8)
  {
    SetCoeffAndReconstruction8x8 (currMB);
  }
  else if (mode==I4MB)
  {
    currMB->cbp = Mode_Decision_for_Intra4x4Macroblock (currMB, lambda, &dummy_d, is_cavlc);
  }
  else if (mode==I16MB)
  {
    Intra16x16_Mode_Decision  (currMB, &i16mode, is_cavlc);
  }
  else if(mode==I8MB)
  {
    currMB->cbp = Mode_Decision_for_new_Intra8x8Macroblock(currMB, lambda, &dummy_d);
  }
  else if(mode==IPCM)
  {
    for (j = 0; j < MB_BLOCK_SIZE; j++)
    {
      memcpy(&enc_picture->imgY[j + img->pix_y][img->opix_x], &pCurImg[j + img->opix_y][img->opix_x], MB_BLOCK_SIZE * sizeof(imgpel));
    }
    if ((img->yuv_format != YUV400) && !IS_INDEPENDENT(params))
    {
      // CHROMA
      for (j = 0; j<img->mb_cr_size_y; j++)
      {
        j1 = j + img->opix_c_y;
        j2 = j + img->pix_c_y;
        memcpy(&enc_picture->imgUV[0][j2][img->opix_c_x], &pImgOrg[1][j1][img->opix_c_x], img->mb_cr_size_x * sizeof(imgpel));
        memcpy(&enc_picture->imgUV[1][j2][img->opix_c_x], &pImgOrg[2][j1][img->opix_c_x], img->mb_cr_size_x * sizeof(imgpel));
      }
    }
    for (j=0;j<4;j++)
      for (i=0; i<(4+img->num_blk8x8_uv); i++)
        img->nz_coeff[img->current_mb_nr][j][i] = 16;

  }

  if (params->rdopt==3 && img->type!=B_SLICE)
  {
    // We need the reconstructed prediction residue for the simulated decoders.
    // Should we be handing imgY here or p_curr_img instead of p_img[0]? This could make it more generic
    ////compute_residue_mb (img, &enc_picture->p_img[0][img->pix_y], decs->res_img[0], mode == I16MB ? i16mode : -1);
    compute_residue_block (img, &enc_picture->p_curr_img[img->pix_y], decs->res_img[0], mode == I16MB ? img->mpr_16x16[0][i16mode] : img->mb_pred[0], 0, 16);
  }

  //Rate control
  if (params->RCEnable)
  {
    if (mode == I16MB)
      memcpy(pred, curr_mpr_16x16[i16mode], MB_PIXELS * sizeof(imgpel));
    else
      memcpy(pred, mb_pred, MB_PIXELS * sizeof(imgpel));
  }

  img->i16offset = 0;
  dummy = 0;

  if (((img->yuv_format!=YUV400) && (active_sps->chroma_format_idc != YUV444)) && (mode != IPCM))
    ChromaResidualCoding (currMB, is_cavlc);

  if (mode==I16MB)
    img->i16offset = I16Offset  (currMB->cbp, i16mode);

  //=====
  //=====   GET DISTORTION
  //=====
  // LUMA
  if (params->rdopt == 3 && img->type!=B_SLICE)
  {
    if (mode != P8x8)
    {
      for (k = 0; k<params->NoOfDecoders ;k++)
      {
        decode_one_mb (img, enc_picture, k, currMB);
        distortion += compute_SSE(&pCurImg[img->opix_y], &enc_picture->p_dec_img[0][k][img->opix_y], img->opix_x, img->opix_x, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
      }
    }
    else
    {
      for (k = 0; k<params->NoOfDecoders ;k++)
      {
        distortion += compute_SSE(&pCurImg[img->opix_y], &enc_picture->p_dec_img[0][k][img->opix_y], img->opix_x, img->opix_x, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
      }
    }
    distortion /= params->NoOfDecoders;

    if ((img->yuv_format != YUV400) && (active_sps->chroma_format_idc != YUV444))
    {
      // CHROMA
      distortion += compute_SSE(&pImgOrg[1][img->opix_c_y], &enc_picture->imgUV[0][img->pix_c_y], img->opix_c_x, img->pix_c_x, img->mb_cr_size_y, img->mb_cr_size_x);
      distortion += compute_SSE(&pImgOrg[2][img->opix_c_y], &enc_picture->imgUV[1][img->pix_c_y], img->opix_c_x, img->pix_c_x, img->mb_cr_size_y, img->mb_cr_size_x);
    }
  }
  else
  {
    distortion = getDistortion(currMB);
  }

  //=====   S T O R E   C O D I N G   S T A T E   =====
  //---------------------------------------------------
  store_coding_state (currMB, cs_cm);

  //=====
  //=====   GET RATE
  //=====
  //----- macroblock header -----
  if (use_of_cc)
  {
    if (currMB->mb_type!=0 || (bframe && currMB->cbp!=0))

⌨️ 快捷键说明

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