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

📄 rdopt.c

📁 H.264ITU-T 标准源码JM98 ITU推荐的免费解码器
💻 C
📖 第 1 页 / 共 5 页
字号:
    }     
    //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;
      if(!img->residue_transform_flag) // Residue Color Transform
      {
        for (j = 0; j < MB_BLOCK_SIZE; j++)
          memcpy(&enc_picture->imgY[img->pix_y+j][img->pix_x], tr8x8.mpr8x8[j], MB_BLOCK_SIZE * sizeof(imgpel));
      }
      else
      {
        for (j = 0; j < MB_BLOCK_SIZE; j++)
          memset(rec_resG[j], 0, MB_BLOCK_SIZE * sizeof(int));
      }                  
    }
    else
    {
      currMB->cbp     = cbp8_8x8ts;
      currMB->cbp_blk = cbp_blk8_8x8ts;
      if(!img->residue_transform_flag)                // Residue Color Transform
      {
        for (j = 0; j < MB_BLOCK_SIZE; j++)
          memcpy (&enc_picture->imgY[img->pix_y+j][img->pix_x],tr8x8.rec_mbY8x8[j], MB_BLOCK_SIZE * sizeof(imgpel));            
      }
      else
      {
        for (j = 0; j < MB_BLOCK_SIZE; j++)
          memcpy (rec_resG[j], tr8x8.rec_resG_8x8[j], MB_BLOCK_SIZE * sizeof(int));
      }
    }
    
    if(img->residue_transform_flag)                // Residue Color Transform
    {
      // Residue Color Transform
      for (j = 0; j < MB_BLOCK_SIZE; j++)
        for (i = 0; i < MB_BLOCK_SIZE; i++)
        {
          mprRGB[0][j][i]  = tr8x8.mprRGB_8x8[0][j][i];
          mprRGB[1][j][i]  = tr8x8.mprRGB_8x8[1][j][i];
          mprRGB[2][j][i]  = tr8x8.mprRGB_8x8[2][j][i];
          resTrans_R[j][i] = tr8x8.resTrans_R_8x8[j][i];
          resTrans_B[j][i] = tr8x8.resTrans_B_8x8[j][i];
        }
    }
  }
  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++)
          memcpy (img->cofAC[block][k][j],cofAC8x8[block][k][j], 65 * sizeof(int));            
    }
    
    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;
      if(!img->residue_transform_flag) // Residue Color Transform
      {
        for (j = 0; j < MB_BLOCK_SIZE; j++)
          memcpy (&enc_picture->imgY[img->pix_y+j][img->pix_x],tr4x4.mpr8x8[j], MB_BLOCK_SIZE * sizeof(imgpel));            
      }
      else
      {
        for (j = 0; j < MB_BLOCK_SIZE; j++)
          memset(rec_resG[j], 0, MB_BLOCK_SIZE * sizeof(int));
      }
    }
    else
    {
      currMB->cbp     = cbp8x8;
      currMB->cbp_blk = cbp_blk8x8;
      if(!img->residue_transform_flag)           // Residue Color Transform
      {
        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));            
      }
      else
      {
        for (j = 0; j < MB_BLOCK_SIZE; j++)
          memcpy(rec_resG[j], tr4x4.rec_resG_8x8[j], MB_BLOCK_SIZE * sizeof(int));
      }
    }
    
    if(img->residue_transform_flag)           // Residue Color Transform
    {
      // Residue Color Transform
      for (j = 0; j < MB_BLOCK_SIZE; j++)
      {
        memcpy(mprRGB[0][j], tr4x4.mprRGB_8x8[0][j], MB_BLOCK_SIZE * sizeof(int));
        memcpy(mprRGB[1][j], tr4x4.mprRGB_8x8[1][j], MB_BLOCK_SIZE * sizeof(int));
        memcpy(mprRGB[2][j], tr4x4.mprRGB_8x8[2][j], MB_BLOCK_SIZE * sizeof(int));
        memcpy(resTrans_R[j], tr4x4.resTrans_R_8x8[j], MB_BLOCK_SIZE * sizeof(int));
        memcpy(resTrans_B[j], tr4x4.resTrans_B_8x8[j], MB_BLOCK_SIZE * sizeof(int));
      }
    }
  }
}


/*!
 *************************************************************************************
 * \brief
 *    Sets motion vectors for a macroblock
 *************************************************************************************
 */
void SetMotionVectorsMB (Macroblock* currMB, int bframe)
{
  int i, j, k, l, m, mode8, pdir8, ref, by, bx, bxr;
  short ******all_mv  = img->all_mv;
  short ******pred_mv = img->pred_mv;
  int  bw_ref;
  int jdiv, jmod;
 
  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);
      
      bxr   = img->block_x+i;
      bx    = img->block_x+i+4;
      
      pdir8 = currMB->b8pdir[k];
      ref    = enc_picture->ref_idx[LIST_0][by][bxr];
      bw_ref = enc_picture->ref_idx[LIST_1][by][bxr];
      
      if (currMB->bi_pred_me && (pdir8 == 2) && currMB->mb_type==1)
      {
        all_mv  = currMB->bi_pred_me == 1 ? img->bipred_mv1 : img->bipred_mv2;
        ref = 0;
        bw_ref = 0;
      }
      
      if (!bframe)
      {
        if (pdir8>=0) //(mode8!=IBLOCK)&&(mode8!=I16MB))  // && ref != -1)
        {
          enc_picture->mv[LIST_0][by][bxr][0] = all_mv [j][i][LIST_0][ ref][mode8][0];
          enc_picture->mv[LIST_0][by][bxr][1] = all_mv [j][i][LIST_0][ ref][mode8][1];
        }
        else
        {
          enc_picture->mv[LIST_0][by][bxr][0] = 0;
          enc_picture->mv[LIST_0][by][bxr][1] = 0;
        }
      }
      else
      {
        if (pdir8==-1) // intra
        {
          enc_picture->mv[LIST_0][by][bxr][0] = 0;
          enc_picture->mv[LIST_0][by][bxr][1] = 0;
          enc_picture->mv[LIST_1][by][bxr][0] = 0;
          enc_picture->mv[LIST_1][by][bxr][1] = 0;
        }
        else if (pdir8==0) // list 0
        {
          enc_picture->mv[LIST_0][by][bxr][0] = all_mv [j][i][LIST_0][ ref][mode8][0];
          enc_picture->mv[LIST_0][by][bxr][1] = all_mv [j][i][LIST_0][ ref][mode8][1];
          enc_picture->mv[LIST_1][by][bxr][0] = 0;
          enc_picture->mv[LIST_1][by][bxr][1] = 0;
        }
        else if (pdir8==1) // list 1
        {
          enc_picture->mv[LIST_0][by][bxr][0] = 0;
          enc_picture->mv[LIST_0][by][bxr][1] = 0;          
          enc_picture->mv[LIST_1][by][bxr][0] = all_mv [j][i][LIST_1][bw_ref][mode8][0];
          enc_picture->mv[LIST_1][by][bxr][1] = all_mv [j][i][LIST_1][bw_ref][mode8][1];
        }
        else if (pdir8==2) // bipredictive
        {
          enc_picture->mv[LIST_0][by][bxr][0] = all_mv [j][i][LIST_0][ ref][mode8][0];
          enc_picture->mv[LIST_0][by][bxr][1] = all_mv [j][i][LIST_0][ ref][mode8][1];                   
          enc_picture->mv[LIST_1][by][bxr][0] = all_mv [j][i][LIST_1][bw_ref][mode8][0];
          enc_picture->mv[LIST_1][by][bxr][1] = all_mv [j][i][LIST_1][bw_ref][mode8][1];
        }
        else
        {
          error("invalid direction mode", 255);
        }
      }
    }
  }
  
  // copy all the motion vectors into rdopt structure
  // Can simplify this by copying the MV's of the best mode (TBD)
  if(img->MbaffFrameFlag)
  {
    for(i = 0;i<4;i++)
    {
      for(j = 0;j<4;j++)
      {
        for (k = 0;k<2;k++)
        {
          for(l = 0;l<img->max_num_references;l++)
          {
            for(m = 0;m<9;m++)
            {
              rdopt->all_mv [j][i][k][l][m][0]  = all_mv [j][i][k][l][m][0];
              rdopt->pred_mv[j][i][k][l][m][0]  = pred_mv[j][i][k][l][m][0];
              
              rdopt->all_mv [j][i][k][l][m][1]  = all_mv [j][i][k][l][m][1];
              rdopt->pred_mv[j][i][k][l][m][1]  = pred_mv[j][i][k][l][m][1];
            }
          }
        }
      }
    }
  }
}



/*!
 *************************************************************************************
 * \brief
 *    R-D Cost for a macroblock
 *************************************************************************************
 */
int RDCost_for_macroblocks (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_rate,     // --> bitrate of mode which has minimum rate-distortion cost. 
                            int i16mode )
{
  int         i, j, k; //, k, ****ip4;
  int         j1, j2;
  int         rate = 0, coeff_rate = 0;
  int64       distortion = 0;
  double      rdcost;
  Macroblock  *currMB   = &img->mb_data[img->current_mb_nr];
  Macroblock  *prevMB   = img->current_mb_nr ? &img->mb_data[img->current_mb_nr-1] : NULL;
  int         bframe    = (img->type==B_SLICE);
  int         tmp_cc;
  int         use_of_cc =  (img->type!=I_SLICE &&  input->symbol_mode!=CABAC);
  int         cc_rate, dummy;
  int         cr_cbp = 0, uv;
  
  //=====
  //=====  SET REFERENCE FRAMES AND BLOCK MODES
  //=====
  SetModesAndRefframeForBlocks (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 ( img->all_mv[0][0][0][0][0][0] < - 8192 
      || img->all_mv[0][0][0][0][0][0] > 8191 
      || img->all_mv[0][0][0][0][0][1] < LEVELMVLIMIT[img->LevelIndex][4] 
      || img->all_mv[0][0][0][0][0][1] > LEVELMVLIMIT[img->LevelIndex][4])
      return 0;
  }
  
  if (img->AdaptiveRounding)
  {
    for (j = 0;j < MB_BLOCK_SIZE;j++)
    {
      memset(img->fadjust4x4[0][j], 0, MB_BLOCK_SIZE * sizeof(int));;
      memset(img->fadjust8x8[0][j], 0, MB_BLOCK_SIZE * sizeof(int));
      memset(img->fadjust4x4Cr[0][0][j], 0, MB_BLOCK_SIZE * sizeof(int));
      memset(img->fadjust4x4Cr[0][1][j], 0, MB_BLOCK_SIZE * sizeof(int));
    }
  }
  
  if (mode<P8x8)
  {
    LumaResidualCoding ();
    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 (lambda, &dummy);
    
    // Residue Color Transform
    if(img->residue_transform_flag)
    {
      for(i = 0; i<2; i++)
      {
        for(j = 0; j<4; j++)
          for(k = 0; k<4; k++)
            if(cbp_chroma_block[i][j][k])
              cr_cbp = 2;
      }
      for(uv=0; uv<2; uv++)
        cr_cbp = dct_chroma_DC(uv, cr_cbp);
      
      currMB->cbp += (cr_cbp<<4);
    }
  }
  else if (mode==I16MB)
  {
    Intra16x16_Mode_Decision  (currMB, &i16mode);
  }
  else if(mode==I8MB)
  {
    currMB->cbp = Mode_Decision_for_new_Intra8x8Macroblock(lambda, &dummy);
    
    // Residue Color Transform
    if(img->residue_transform_flag)
    {
      for(i = 0; i<2; i++)
      {
        for(j = 0; j<4; j++)
          for(k = 0; k<4; k++)
            if(cbp_chroma_block[i][j][k])
              cr_cbp = 2;
      }     
      for(uv = 0; uv<2; uv++)
        cr_cbp = dct_chroma_DC(uv, cr_cbp);
      
      currMB->cbp += (cr_cbp<<4);
    }
  }
  else if(mode==IPCM)
  {
    for (j = 0; j < MB_BLOCK_SIZE; j++)
    {
      j1 = j + img->opix_y;
      j2 = j + img->pix_y;
      for (i=img->opix_x; i<img->opix_x+MB_BLOCK_SIZE; i++)        
        enc_picture->imgY[j2][i] = imgY_org[j1][i];
    }
    if (img->yuv_format != YUV400)
    {
      // CHROMA
      for (j = 0; j<img->mb_cr_size_y; j++)
      {
        j1 = j + img->opix_c_y;
        j2 = j + img->pix_c_y;
        for (i=img->opix_c_x; i<img->opix_c_x+img->mb_cr_size_x; i++)
        {
          enc_picture->imgUV[0][j2][i] = imgUV_org[0][j1][i];
          enc_picture->imgUV[1][j2][i] = imgUV_org[1][j1][i];
        }
      }
    }  
  }

  if (input->rdopt==3 && img->type!=B_SLICE)
  {
    // We need the reconstructed prediction residue for the simulated decoders.
    compute_residue_mb (mode==I16MB?i16mode:-1);
  }
  
  //Rate control
  if (input->RCEnable)
  {
    if (mode == I16MB)
      memcpy(pred,img->mprr_2[i16mode],MB_PIXELS * sizeof(imgpel));
    else
      memcpy(pred,img->mpr,MB_PIXELS * sizeof(imgpel));
  }

  img->i16offset = 0;
  dummy = 0;
  if ((!(img->residue_transform_flag && (mode==I4MB || mode==I16MB || mode==I8MB))) && img->yuv_format!=YUV400)
    ChromaResidualCoding (&dummy);
  if (mode==I16MB)     
    img->i16offset = I16Offset  (currMB->cbp, i16mode);
  
  //=====
  //=====   GET DISTORTION
  //=====
  // LUMA
  if (input->rdopt==3 && img->type!=B_SLICE)
  {
    for (k = 0; k<input->NoOfDecoders ;k++)
    {
      

⌨️ 快捷键说明

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