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

📄 rdopt.c

📁 h264标准的VC实现
💻 C
📖 第 1 页 / 共 5 页
字号:
        {
          if(!mode)
          {     //direct mode
            enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j] = direct_ref_idx[LIST_0][img->block_x+i][img->block_y+j];
            enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j] = direct_ref_idx[LIST_1][img->block_x+i][img->block_y+j];
          }
          else
          {   //intra
            enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j] = -1;
            enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j] = -1;

          }
        }
    }
    else
    {
      for (j=0;j<4;j++)
        for (i=0;i<4;i++)
        {
          enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j] = (mode==0?0:-1);
        }
    }
  }
  else
  {
    if (bframe)
    {
      for (j=0;j<4;j++)
        for (i=0;i<4;i++)
        {
          k = 2*(j/2)+(i/2);
          l = 2*(j%2)+(i%2);

          if(mode == P8x8 && best8x8mode[k]==0)
          {
            enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j] = direct_ref_idx[LIST_0][img->block_x+i][img->block_y+j];
            enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j] = direct_ref_idx[LIST_1][img->block_x+i][img->block_y+j];
          }
          else
          {
            enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j] = (IS_FW ? best8x8fwref[mode][k] : -1);
            enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j] = (IS_BW ? best8x8bwref[mode][k] : -1);
          }
        }
    }
    else
    {
      for (j=0;j<4;j++)
        for (i=0;i<4;i++)
        {
          k = 2*(j/2)+(i/2);
          l = 2*(j%2)+(i%2);
          enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j] = (IS_FW ? best8x8fwref[mode][k] : -1);
        }
    }
  }

  for (j=0;j<4;j++)
  {
    for (i=0;i<4;i++)
    {
      enc_picture->ref_pic_id [LIST_0][img->block_x+i][img->block_y+j] =
        (enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j]>=0 ?
         enc_picture->ref_pic_num[LIST_0 + list_offset][enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j]]:
         -1);
    }
  }
  if (bframe)
  {
    for (j=0;j<4;j++)
    {
      for (i=0;i<4;i++)
      {
        enc_picture->ref_pic_id [LIST_1][img->block_x+i][img->block_y+j] =
          (enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j]>=0 ?
           enc_picture->ref_pic_num[LIST_1 + list_offset][enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j]]:
           -1);
      }

    }
  }


#undef IS_FW
#undef IS_BW
}


/*!
 *************************************************************************************
 * \brief
 *    Intra 16x16 mode decision
 *************************************************************************************
 */
void
Intra16x16_Mode_Decision (Macroblock* currMB, int* i16mode)
{
  // Residue Color Transform
  int residue_R, residue_G, residue_B;
  int c_ipmode = img->mb_data[img->current_mb_nr].c_ipred_mode;
  int i, j, temp;
  int pic_pix_x   = img->pix_x;
  int pic_pix_y   = img->pix_y;
  pel_t   **imgY_orig  = imgY_org;
  pel_t   ***imgUV_orig  = imgUV_org;
  int cr_cbp;

  intrapred_luma_16x16 ();   /* make intra pred for all 4 new modes */
  find_sad_16x16 (i16mode);   /* get best new intra mode */

  // Residue Color Transform
  if(img->residue_transform_flag)
  {
    for (j=0; j<16; j++)
      for (i=0; i<16; i++)
      {
        residue_B = imgUV_orig[0][pic_pix_y+j][pic_pix_x+i] - img->mprr_c[0][c_ipmode][i][j];
        residue_G = imgY_orig[pic_pix_y+j][pic_pix_x+i] - img->mprr_2[*i16mode][j][i];
        residue_R = imgUV_orig[1][pic_pix_y+j][pic_pix_x+i] - img->mprr_c[1][c_ipmode][i][j];
        
        /* Forward Residue Transform */
        resTrans_R[i][j] = residue_R-residue_B;
        temp = residue_B+(resTrans_R[i][j]>>1);
        resTrans_B[i][j] = residue_G-temp;
        resTrans_G[i][j] = temp+(resTrans_B[i][j]>>1);
        
        img->m7[i][j]  = resTrans_G[i][j];
      }
  }

  currMB->cbp = dct_luma_16x16 (*i16mode);

  // Residue Color Transform
  if(img->residue_transform_flag)
  {
    for (j=0; j<16; j++)
      for (i=0; i<16; i++)
      {
        rec_resG[i][j] = img->m7[i][j];
        img->m7[i][j]  = resTrans_R[i][j];
      }

    cr_cbp = dct_chroma(0, 0);

    for (j=0; j<16; j++)
      for (i=0; i<16; i++)
      {
        rec_resR[i][j] = img->m7[i][j];
        img->m7[i][j]  = resTrans_B[i][j];
      }
      
    cr_cbp = dct_chroma(1, cr_cbp);

    for (j=0; j<16; j++)
      for (i=0; i<16; i++)
      {
        rec_resB[i][j] = img->m7[i][j];
      }

    currMB->cbp += (cr_cbp<<4);

    /* Inverse Residue Transform */
    for (j=0; j<16; j++)
      for (i=0; i<16; i++)
      {
        temp      = rec_resG[i][j]-(rec_resB[i][j]>>1);
        residue_G = rec_resB[i][j]+temp;
        residue_B = temp - (rec_resR[i][j]>>1);
        residue_R = residue_B+rec_resR[i][j];
        
        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][i][j]));
        enc_picture->imgY[pic_pix_y+j][pic_pix_x+i]     = min(img->max_imgpel_value,max(0,residue_G+(int)img->mprr_2[*i16mode][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][i][j]));
      }
  }
}



/*!
 *************************************************************************************
 * \brief
 *    Sets Coefficients and reconstruction for an 8x8 block
 *************************************************************************************
 */
void SetCoeffAndReconstruction8x8 (Macroblock* currMB)
{
  int block, k, j, i;


  //============= MIXED TRANSFORM SIZES FOR 8x8 PARTITION ==============
  //--------------------------------------------------------------------
  int l;
  int bframe = img->type==B_SLICE;
  int list_offset   = ((img->MbaffFrameFlag)&&(currMB->mb_field))? img->current_mb_nr%2 ? 4 : 2 : 0;

  if (currMB->luma_transform_size_8x8_flag)
  {
    assert(input->AllowTransform8x8);

    //============= set mode and ref. frames ==============
    for(i=0;i<4;i++)
    {
      currMB->b8mode[i]   = part8x8mode[i];
      currMB->b8pdir[i]   = part8x8pdir[i];
    }

    if (bframe)
    {
      for (j=0;j<4;j++)
        for (i=0;i<4;i++)
        {
          k = 2*(j/2)+(i/2);
          l = 2*(j%2)+(i%2);
          enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j] = part8x8fwref[k];
          enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j] = part8x8bwref[k];
        }
    }
    else
    {
      for (j=0;j<4;j++)
        for (i=0;i<4;i++)
        {
          k = 2*(j/2)+(i/2);
          l = 2*(j%2)+(i%2);
          enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j] = part8x8fwref[k];
        }
    }

    for (j=0;j<4;j++)
    {
      for (i=0;i<4;i++)
      {
        enc_picture->ref_pic_id [LIST_0][img->block_x+i][img->block_y+j] =
          (enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j]>=0 ?
          enc_picture->ref_pic_num[LIST_0 + list_offset][enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j]]:
        -1);
      }
    }
    if (bframe)
    {
      for (j=0;j<4;j++)
      {
        for (i=0;i<4;i++)
        {
          enc_picture->ref_pic_id [LIST_1][img->block_x+i][img->block_y+j] =
            (enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j]>=0 ?
            enc_picture->ref_pic_num[LIST_1 + list_offset][enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j]]:
          -1);
        }

      }
    }

    //====== set the mv's for 8x8 partition with transform size 8x8 ======
    //save the mv data for 4x4 transform
    StoreMV8x8(1);
    //set new mv data for 8x8 transform
    RestoreMV8x8(0);

    //============= get pre-calculated data ==============
    //restore coefficients from 8x8 transform
    for (block=0; block<4; block++)
    for (k=0; k<4; k++)
    for (j=0; j<2; j++)
    for (i=0; i<65; i++)
      img->cofAC[block][k][j][i] = cofAC_8x8ts[block][k][j][i];
    
    //restore reconstruction 
    if (cnt_nonz8_8x8ts <= _LUMA_8x8_COEFF_COST_ && 
        ((img->qp + img->bitdepth_luma_qp_scale)!=0 || img->lossless_qpprime_flag==0))
    {
      currMB->cbp     = 0;
      currMB->cbp_blk = 0;
      for (j=0; j<16; j++)
        for (i=0; i<16; i++)
          // Residue Color Transform
          if(!img->residue_transform_flag)
            enc_picture->imgY[img->pix_y+j][img->pix_x+i] = mpr_8x8ts[j][i];
          else
            rec_resG[i][j] = 0;
    }
    else
    {
      currMB->cbp     = cbp8_8x8ts;
      currMB->cbp_blk = cbp_blk8_8x8ts;
      for (j=0; j<16; j++)
        for (i=0; i<16; i++)
          // Residue Color Transform
          if(!img->residue_transform_flag)
            enc_picture->imgY[img->pix_y+j][img->pix_x+i] = rec_mbY_8x8ts[j][i];
          else
            rec_resG[i][j] =  rec_resG_8x8ts[i][j];
    }

    // Residue Color Transform
    for (j=0; j<16; j++)
    for (i=0; i<16; i++)
    {
      mprRGB[0][i][j] = mprRGB_8x8ts[0][i][j];
      mprRGB[1][i][j] = mprRGB_8x8ts[1][i][j];
      mprRGB[2][i][j] = mprRGB_8x8ts[2][i][j];
      resTrans_R[i][j] = resTrans_R_8x8ts[i][j];
      resTrans_B[i][j] = resTrans_B_8x8ts[i][j];
    }
  }
  else
  {
    //============= get pre-calculated data ==============
    //---------------------------------------------------
    //--- restore coefficients ---
    for (block=0; block<4+img->num_blk8x8_uv; block++)
    for (k=0; k<4; k++)
    for (j=0; j<2; j++)
    for (i=0; i<65; i++)
      img->cofAC[block][k][j][i] = cofAC8x8[block][k][j][i];

    if (cnt_nonz_8x8<=5 && img->type!=SP_SLICE &&
        ((img->qp + img->bitdepth_luma_qp_scale)!=0 || img->lossless_qpprime_flag==0))
    {
      currMB->cbp     = 0;
      currMB->cbp_blk = 0;
      for (j=0; j<16; j++)
      for (i=0; i<16; i++)
        // Residue Color Transform
        if(!img->residue_transform_flag)
          enc_picture->imgY[img->pix_y+j][img->pix_x+i] = mpr8x8[j][i];
        else
          rec_resG[i][j] = 0;
    }
    else
    {
      currMB->cbp     = cbp8x8;
      currMB->cbp_blk = cbp_blk8x8;
      for (j=0; j<16; j++)
      for (i=0; i<16; i++)
        // Residue Color Transform
        if(!img->residue_transform_flag)
          enc_picture->imgY[img->pix_y+j][img->pix_x+i] = rec_mbY8x8[j][i];
        else
          rec_resG[i][j] =  rec_resG_8x8[i][j];
    }

    // Residue Color Transform
    for (j=0; j<16; j++)
    for (i=0; i<16; i++)
    {
      mprRGB[0][i][j] = mprRGB_8x8[0][i][j];
      mprRGB[1][i][j] = mprRGB_8x8[1][i][j];
      mprRGB[2][i][j] = mprRGB_8x8[2][i][j];
      resTrans_R[i][j] = resTrans_R_8x8[i][j];
      resTrans_B[i][j] = resTrans_B_8x8[i][j];
    }
  }
}


/*!
 *************************************************************************************
 * \brief
 *    Sets motion vectors for an macroblock
 *************************************************************************************
 */
void SetMotionVectorsMB (Macroblock* currMB, int bframe)
{
  int i, j, k, l, m, mode8, pdir8, ref, by, bx, bxr;
  int ******all_mv  = img->all_mv;
  int ******pred_mv = img->pred_mv;
  int  bw_ref;

  for (j=0; j<4; j++)
    for (i=0; i<4; i++)
    {
      mode8 = currMB->b8mode[k=2*(j/2)+(i/2)];
      l     = 2*(j%2)+(i%2);
      by    = img->block_y+j;
      bxr   = img->block_x+i;
      bx    = img->block_x+i+4;


      
        pdir8 = currMB->b8pdir[k];
        ref    = enc_picture->ref_idx[LIST_0][bxr][by];
        bw_ref = enc_picture->ref_idx[LIST_1][bxr][by];

      
      if (!bframe)
      {
        if (pdir8>=0) //(mode8!=IBLOCK)&&(mode8!=I16MB))  // && ref != -1)
        {
          enc_picture->mv[LIST_0][bxr][by][0] = all_mv [i][j][LIST_0][ ref][mode8][0];
          enc_picture->mv[LIST_0][bxr][by][1] = all_mv [i][j][LIST_0][ ref][mode8][1];
        }
        else
        {
          enc_picture->mv[LIST_0][bxr][by][0] = 0;
          enc_picture->mv[LIST_0][bxr][by][1] = 0;
        }
      }
      else
      {
        if (pdir8==-1) // intra
        {
          enc_picture->mv[LIST_0][bxr][by][0] = 0;
          enc_picture->mv[LIST_0][bxr][by][1] = 0;
          enc_picture->mv[LIST_1][bxr][by][0] = 0;
          enc_picture->mv[LIST_1][bxr][by][1] = 0;
        }
        else if (pdir8==0) // forward
        {
          enc_picture->mv[LIST_0][bxr][by][0] = all_mv [i][j][LIST_0][ ref][mode8][0];
          enc_picture->mv[LIST_0][bxr][by][1] = all_mv [i][j][LIST_0][ ref][mode8][1];
          enc_picture->mv[LIST_1][bxr][by][0] = 0;

⌨️ 快捷键说明

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