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

📄 rdopt.c

📁 压缩JM12.3d的完整的全部C语言的代码文档,用于嵌入式系统的压缩编解码
💻 C
📖 第 1 页 / 共 5 页
字号:
      for (i = img->block_x;i<img->block_x + BLOCK_MULTIPLE;i++)
      {
        cur_ref[LIST_0] = (int) enc_picture->ref_idx[LIST_0][j][i];

        enc_picture->ref_pic_id [LIST_0][j][i] =(cur_ref[LIST_0]>=0
        ? enc_picture->ref_pic_num[LIST_0 + currMB->list_offset][cur_ref[LIST_0]]
        : -1);
      }
    }

    if (bframe)
    {
      for (j = img->block_y; j < img->block_y + BLOCK_MULTIPLE; j++)
      {
        for (i = img->block_x;i<img->block_x + BLOCK_MULTIPLE;i++)
        {
          cur_ref[LIST_1] = (int) enc_picture->ref_idx[LIST_1][j][i];

          enc_picture->ref_pic_id [LIST_1][j][i] = (cur_ref[LIST_1]>=0
            ? enc_picture->ref_pic_num[LIST_1 + currMB->list_offset][cur_ref[LIST_1]]
            : -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++)
          memcpy (img->cofAC[block][k][j],cofAC_8x8ts[block][k][j], 65 * sizeof(int));
    }
    //restore reconstruction
    if (cnt_nonz8_8x8ts <= _LUMA_8x8_COEFF_COST_ &&
      ((img->qp_scaled)!=0 || img->lossless_qpprime_flag==0) &&
      (img->type!=SP_SLICE))// modif ES added last condition (we probably never go there so is the next modification useful ? check)
    {
      currMB->cbp     = 0;
      currMB->cbp_blk = 0;
      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));
        if(img->type==SP_SLICE &&(!si_frame_indicator && !sp2_frame_indicator ))
          memcpy(&lrec[img->pix_y+j][img->pix_x],tr8x8.lrec[j], MB_BLOCK_SIZE * sizeof(int));
      }
    }
    else
    {
      currMB->cbp     = cbp8_8x8ts;
      currMB->cbp_blk = cbp_blk8_8x8ts;
      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));
        if(img->type==SP_SLICE &&(!si_frame_indicator && !sp2_frame_indicator))
          memcpy (&lrec[img->pix_y+j][img->pix_x],tr8x8.lrec[j], MB_BLOCK_SIZE * sizeof(int));
      }
    }
  }
  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_scaled)!=0 || img->lossless_qpprime_flag==0))
    {
      currMB->cbp     = 0;
      currMB->cbp_blk = 0;
      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));
        if(img->type ==SP_SLICE &&(!si_frame_indicator && !sp2_frame_indicator))
          memcpy (&lrec[img->pix_y+j][img->pix_x],tr4x4.lrec[j], MB_BLOCK_SIZE * sizeof(int)); // restore coeff. SP frame
      }
    }
    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(img->type==SP_SLICE &&(!si_frame_indicator && !sp2_frame_indicator))
          memcpy (&lrec[img->pix_y+j][img->pix_x],tr4x4.lrec[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;
  short ******all_mv  = img->all_mv;
  short ******pred_mv = img->pred_mv;
  int  l1_ref;
  int jdiv, jmod;

  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->ref_idx[LIST_0][by][bx];

        if (pdir8>=0)
        {
          enc_picture->mv[LIST_0][by][bx][0] = all_mv [j][i][LIST_0][ ref][mode8][0];
          enc_picture->mv[LIST_0][by][bx][1] = all_mv [j][i][LIST_0][ ref][mode8][1];
        }
        else
        {
          enc_picture->mv[LIST_0][by][bx][0] = 0;
          enc_picture->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)];
      l     = 2*jmod + (i & 0x01);

      bx    = img->block_x+i;

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

      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;
        l1_ref = 0;
      }

      if (pdir8==-1) // intra
      {
        enc_picture->mv[LIST_0][by][bx][0] = 0;
        enc_picture->mv[LIST_0][by][bx][1] = 0;
        enc_picture->mv[LIST_1][by][bx][0] = 0;
        enc_picture->mv[LIST_1][by][bx][1] = 0;
      }
      else if (pdir8==0) // list 0
      {
        enc_picture->mv[LIST_0][by][bx][0] = all_mv [j][i][LIST_0][ ref][mode8][0];
        enc_picture->mv[LIST_0][by][bx][1] = all_mv [j][i][LIST_0][ ref][mode8][1];
        enc_picture->mv[LIST_1][by][bx][0] = 0;
        enc_picture->mv[LIST_1][by][bx][1] = 0;
        enc_picture->ref_idx[LIST_1][by][bx] = -1;
      }
      else if (pdir8==1) // list 1
      {
        enc_picture->mv[LIST_0][by][bx][0] = 0;
        enc_picture->mv[LIST_0][by][bx][1] = 0;
        enc_picture->ref_idx[LIST_0][by][bx] = -1;
        enc_picture->mv[LIST_1][by][bx][0] = all_mv [j][i][LIST_1][l1_ref][mode8][0];
        enc_picture->mv[LIST_1][by][bx][1] = all_mv [j][i][LIST_1][l1_ref][mode8][1];
      }
      else if (pdir8==2) // bipredictive
      {
        enc_picture->mv[LIST_0][by][bx][0] = all_mv [j][i][LIST_0][ ref][mode8][0];
        enc_picture->mv[LIST_0][by][bx][1] = all_mv [j][i][LIST_0][ ref][mode8][1];
        enc_picture->mv[LIST_1][by][bx][0] = all_mv [j][i][LIST_1][l1_ref][mode8][0];
        enc_picture->mv[LIST_1][by][bx][1] = all_mv [j][i][LIST_1][l1_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_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         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  *currMB   = &img->mb_data[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 &&  input->symbol_mode!=CABAC);
  int         cc_rate, dummy;

  //=====
  //=====  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][5])
      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[0][1][0][0]), 0, img->mb_cr_size_y * img->mb_cr_size_x  * sizeof(int));
    }
  }
  if (mode<P8x8)
  {
    LumaResidualCoding ();

// This code seems unnecessary 
    if(mode==0 && currMB->cbp!=0 && (img->type != B_SLICE || img->NoResidueDirect==1))
      return 0;
    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);
  }
  else if (mode==I16MB)
  {
    Intra16x16_Mode_Decision  (currMB, &i16mode);
  }
  else if(mode==I8MB)
  {
    currMB->cbp = Mode_Decision_for_new_Intra8x8Macroblock(lambda, &dummy);
  }
  else if(mode==IPCM)
  {
    for (j = 0; j < MB_BLOCK_SIZE; j++)
    {
      memcpy(&enc_picture->imgY[j + img->pix_y][img->opix_x], &imgY_org[j + img->opix_y][img->opix_x], MB_BLOCK_SIZE * sizeof(imgpel));
    }
    if ((img->yuv_format != YUV400) && !IS_INDEPENDENT(input))
    {
      // 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], &imgUV_org[0][j1][img->opix_c_x], img->mb_cr_size_x * sizeof(imgpel));
        memcpy(&enc_picture->imgUV[1][j2][img->opix_c_x], &imgUV_org[1][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 (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->yuv_format!=YUV400) && !IS_INDEPENDENT(input)) && (mode != IPCM))
    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++)
    {
      decode_one_mb (k, currMB);
      for (j = 0; j<MB_BLOCK_SIZE; j++)
      {
        for (i=img->opix_x; i<img->opix_x+MB_BLOCK_SIZE; i++)
          distortion += iabs2( imgY_org[img->opix_y+j][i] - decs->decY[k][img->opix_y+j][i] );
      }
    }
    distortion /= input->NoOfDecoders;

    if ((img->yuv_format != YUV400) && !IS_INDEPENDENT(input))
    {
      // 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++)
        {
          distortion += iabs2( imgUV_org[0][j1][i] - enc_picture->imgUV[0][j2][i] );
          distortion += iabs2( imgUV_org[1][j1][i] - enc_picture->imgUV[1][j2][i] );
        }
      }

⌨️ 快捷键说明

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