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

📄 rdopt.c

📁 H.264编码解码器源码(c语言).zip
💻 C
📖 第 1 页 / 共 5 页
字号:
  }
  else
  {
    rate = writeMBHeader (1); 
  }
  if (mode)
  {
    //----- motion information -----
    rate  += writeMotionInfo2NAL  ();
  }
  if (mode || (bframe && (currMB->cbp!=0 || input->symbol_mode==CABAC)))
  {
    rate  += writeCBPandLumaCoeff ();

    rate  += writeChromaCoeff     ();
  }


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


  rdcost = (double)distortion + lambda * (double)rate;
 
  if (rdcost >= *min_rdcost)
  {
    return 0;
  }


  if(input->InterlaceCodingOption >= MB_CODING && mb_adaptive && img->field_mode && mode == 0)
  {
    if(!bframe && TopFieldIsSkipped)
      return 0;
    else if(bframe && TopFieldIsSkipped && currMB->cbp == 0)
      return 0;
  }


  //=====   U P D A T E   M I N I M U M   C O S T   =====
  //-----------------------------------------------------
  *min_rdcost = rdcost;
  return 1;
}





/*! 
 *************************************************************************************
 * \brief
 *    Store macroblock parameters
 *************************************************************************************
 */
void store_macroblock_parameters (int mode)
{
  int  i, j, k, ****i4p, ***i3p;
  Macroblock *currMB  = &img->mb_data[img->current_mb_nr];
  //int        bframe   = (img->type==B_SLICE || img->type==BS_IMG);
	int        bframe   = (img->type==B_SLICE);
//  int        **frefar = ((img->type==B_SLICE || img->type==BS_IMG)? fw_refFrArr : refFrArr);
  int        **frefar = ((img->type==B_SLICE)? fw_refFrArr : refFrArr);
  int        **abp_type_arr = abp_type_FrArr;
  int        **brefar = bw_refFrArr;
  int    block_y  = img->block_y;
  int    pix_y    = img->pix_y;
  int    pix_c_y  = img->pix_c_y;

  if(input->InterlaceCodingOption >= MB_CODING && mb_adaptive && img->field_mode)
  {
    block_y = img->field_block_y;
    pix_y   = img->field_pix_y;
    pix_c_y = img->field_pix_c_y;
    if(img->top_field)
    {
//      frefar = ((img->type==B_SLICE || img->type==BS_IMG) ? fw_refFrArr_top : refFrArr_top);
			frefar = ((img->type==B_SLICE) ? fw_refFrArr_top : refFrArr_top);
      abp_type_arr = abp_type_FrArr_top;
      brefar = bw_refFrArr_top;
    }
    else
    {
//      frefar = ((img->type==B_SLICE || img->type==BS_IMG) ? fw_refFrArr_bot : refFrArr_bot);
			frefar = ((img->type==B_SLICE) ? fw_refFrArr_bot : refFrArr_bot);
      abp_type_arr = abp_type_FrArr_bot;
      brefar = bw_refFrArr_bot;
    }

  }

  //--- store best mode ---
  best_mode = mode;
  best_c_imode = currMB->c_ipred_mode;
  best_i16offset = img->i16offset;
  for (i=0; i<4; i++)
  {
    b8mode[i]   = currMB->b8mode[i];
    b8pdir[i]   = currMB->b8pdir[i];
  }

  //--- reconstructed blocks ----
  for (j=0; j<16; j++)
  for (i=0; i<16; i++)
  {
    rec_mbY[j][i] = enc_picture->imgY[img->pix_y+j][img->pix_x+i];
  }
  for (j=0; j<8; j++)
  for (i=0; i<8; i++)
  {
    rec_mbU[j][i] = enc_picture->imgUV[0][img->pix_c_y+j][img->pix_c_x+i];
    rec_mbV[j][i] = enc_picture->imgUV[1][img->pix_c_y+j][img->pix_c_x+i];
  }


  //--- store results of decoders ---
  if (input->rdopt==2 && img->type!=B_SLICE)
  {
    for (k=0; k<input->NoOfDecoders; k++)
    {
      for (j=img->pix_y; j<img->pix_y+16; j++)
      for (i=img->pix_x; i<img->pix_x+16; i++)
      {
        // Keep the decoded values of each MB for updating the ref frames
        decs->decY_best[k][j][i] = decs->decY[k][j][i];
      }
    }
  }

  //--- coeff, cbp, kac ---
  if (mode || bframe)
  {
    i4p=cofAC; cofAC=img->cofAC; img->cofAC=i4p;
    i3p=cofDC; cofDC=img->cofDC; img->cofDC=i3p;
    cbp     = currMB->cbp;
    cbp_blk = currMB->cbp_blk;
  }
  else
  {
    cbp = cbp_blk = 0;
  }

  for (j=0; j<4; j++)
  for (i=0; i<4; i++)
  {  
    frefframe[j][i] = frefar[block_y+j  ][img->block_x +i ];
    enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j] = frefframe[j][i];
	  if ((b8mode[i]==0) && (frefframe[j][i]<0) && (!input->direct_type))
    {
	    enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j] = 0;
    }

    enc_picture->ref_pic_id [LIST_0][img->block_x+i][img->block_y+j] = enc_picture->ref_pic_num[LIST_0][enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j]];
						
    if (bframe)
    {
      brefframe[j][i] = brefar[block_y +j ][img->block_x+i  ];
			enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j] = brefframe[j][i];
			if ((b8mode[i]==0)&&(!input->direct_type))
      {
				enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j] = 0;
      }
      abp_typeframe[j][i] = abp_type_arr[block_y + j][img->block_x + i];     
      enc_picture->ref_pic_id [LIST_1][img->block_x+i][img->block_y+j] = enc_picture->ref_pic_num[LIST_1][enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j]];
			
      if (input->direct_type && (b8mode[i] == 0))
      {
        if (fwdir_refFrArr[img->block_y+j][img->block_x+i]== -1) 
        {
          enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j] = -1;
          enc_picture->mv[LIST_0][img->block_x+i][img->block_y+j][0] = 0;
          enc_picture->mv[LIST_0][img->block_x+i][img->block_y+j][1] = 0;
        }
        else if (bwdir_refFrArr[img->block_y+j][img->block_x+i] == -1) 
        {
          enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j] = -1;
          enc_picture->mv[LIST_1][img->block_x+i][img->block_y+j][0] = 0;
          enc_picture->mv[LIST_1][img->block_x+i][img->block_y+j][1] = 0;
        }
      }
    }
  }

/*
  //--- references ---
  frefframe[0] = frefar[block_y  ][img->block_x  ];
  frefframe[1] = frefar[block_y  ][img->block_x+2];
  frefframe[2] = frefar[block_y+2][img->block_x  ];
  frefframe[3] = frefar[block_y+2][img->block_x+2];
  if (bframe)
  {
    brefframe[0] = brefar[block_y  ][img->block_x  ]; 
    brefframe[1] = brefar[block_y  ][img->block_x+2];
    brefframe[2] = brefar[block_y+2][img->block_x  ];
    brefframe[3] = brefar[block_y+2][img->block_x+2];
  }
  */
}


/*! 
 *************************************************************************************
 * \brief
 *    Set stored macroblock parameters
 *************************************************************************************
 */
void set_stored_macroblock_parameters ()
{
  int  i, j, k, ****i4p, ***i3p,l;
  Macroblock  *currMB  = &img->mb_data[img->current_mb_nr];
  int         mode     = best_mode;
  //int         bframe   = (img->type==B_SLICE || img->type==BS_IMG);
	int         bframe   = (img->type==B_SLICE);
//  int         **frefar = ((img->type==B_SLICE || img->type==BS_IMG)? fw_refFrArr : refFrArr);
	int         **frefar = ((img->type==B_SLICE)? fw_refFrArr : refFrArr);
  int         **abp_type_arr = abp_type_FrArr;
  int         **brefar = bw_refFrArr;
  int     **ipredmodes = img->ipredmode;
  int     block_y = img->block_y; 
  
  byte    **imgY=enc_picture->imgY;
  byte    ***imgUV=enc_picture->imgUV;

  if(input->InterlaceCodingOption >= MB_CODING && mb_adaptive && img->field_mode)
  {
    block_y = img->field_block_y;
    if(img->top_field)
    {
      //frefar = (img->type==B_SLICE || img->type==BS_IMG) ? fw_refFrArr_top : refFrArr_top;
			frefar = (img->type==B_SLICE) ? fw_refFrArr_top : refFrArr_top;
      abp_type_arr = abp_type_FrArr_top;
      brefar = bw_refFrArr_top;
    }
    else
    {
//      frefar = (img->type==B_SLICE || img->type==BS_IMG) ? fw_refFrArr_bot : refFrArr_bot;
		  frefar = (img->type==B_SLICE) ? fw_refFrArr_bot : refFrArr_bot;
      abp_type_arr = abp_type_FrArr_bot;
      brefar = bw_refFrArr_bot;

    }
  }

  //===== reconstruction values =====
  for (j=0; j<16; j++)
  for (i=0; i<16; i++)
  {
    imgY[img->pix_y+j][img->pix_x+i] = rec_mbY[j][i];
    if(input->InterlaceCodingOption >= MB_CODING && mb_adaptive)
      rdopt->rec_mbY[j][i]       = rec_mbY[j][i]; 
  }

  for (j=0; j<8; j++)
  for (i=0; i<8; i++)
  {
    imgUV[0][img->pix_c_y+j][img->pix_c_x+i] = rec_mbU[j][i];
    imgUV[1][img->pix_c_y+j][img->pix_c_x+i] = rec_mbV[j][i];
    if(input->InterlaceCodingOption >= MB_CODING && mb_adaptive)
    {
      rdopt->rec_mbU[j][i]           = rec_mbU[j][i];   
      rdopt->rec_mbV[j][i]           = rec_mbV[j][i];   
    }
  }

  //===== coefficients and cbp =====
  i4p=cofAC; cofAC=img->cofAC; img->cofAC=i4p;
  i3p=cofDC; cofDC=img->cofDC; img->cofDC=i3p;
  currMB->cbp      = cbp;
  currMB->cbp_blk = cbp_blk;
  //==== macroblock type ====
  currMB->mb_type = mode;
  if(input->InterlaceCodingOption >= MB_CODING && mb_adaptive)
  {
    rdopt->mode = mode;
    rdopt->i16offset = img->i16offset;  // For MBINTLC  -Rajeev
    rdopt->cbp = cbp;
    rdopt->cbp_blk = cbp_blk;
    rdopt->mb_type  = mode;


    for(i=0;i<6;i++)
      for(j=0;j<4;j++)
        for(k=0;k<2;k++)
          for(l=0;l<18;l++)
            rdopt->cofAC[i][j][k][l] = img->cofAC[i][j][k][l];

    for(i=0;i<3;i++)
        for(k=0;k<2;k++)
          for(l=0;l<18;l++)
            rdopt->cofDC[i][k][l] = img->cofDC[i][k][l];
  }


  for (i=0; i<4; i++)
  {
    currMB->b8mode[i]   = b8mode[i];
    currMB->b8pdir[i]   = b8pdir[i];
    if(input->InterlaceCodingOption >= MB_CODING && mb_adaptive)
    {
      rdopt->b8mode[i]  = b8mode[i];                  
      rdopt->b8pdir[i]  = b8pdir[i];                  
    }
  }
  if (input->rdopt==2 && img->type!=B_SLICE)
  {
    //! save the MB Mode of every macroblock
    decs->dec_mb_mode[img->mb_x][img->mb_y] = mode;
  }

  //==== reference frames =====
  for (j=0; j<4; j++)
  for (i=0; i<4; i++)
  {
    frefar[block_y+j][img->block_x+i] = frefframe[j][i];	
		enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j] = frefframe[j][i];

		// temporal direct mode 
		if ((currMB->b8mode[i/2+(j/2)*2] == 0) && (frefframe[j][i] < 0) && !input->direct_type)
    {
			enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j] = 0;
    }
		// backward prediction or intra
		if ((currMB->b8pdir[i/2+(j/2)*2] == 1) || IS_INTRA(currMB))
		{
			enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j] = -1;
			enc_picture->mv[LIST_0][img->block_x+i][img->block_y+j][0] =0;
			enc_picture->mv[LIST_0][img->block_x+i][img->block_y+j][1] = 0;
		}

    enc_picture->ref_pic_id [LIST_0][img->block_x+i][img->block_y+j] = enc_picture->ref_pic_num[LIST_0][enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j]];
		// forward prediction or intra
		if ((currMB->b8pdir[i/2+(j/2)*2] == 0) || IS_INTRA(currMB))
		{
			enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j] = -1;
      enc_picture->ref_pic_id [LIST_1][img->block_x+i][img->block_y+j] = enc_picture->ref_pic_num[LIST_1][enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j]];	
			enc_picture->mv[LIST_1][img->block_x+i][img->block_y+j][0] =0;
			enc_picture->mv[LIST_1][img->block_x+i][img->block_y+j][1] = 0;
		}
		
		if ((img->type!=I_SLICE) && (enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j] >= 0) && (frefframe[j][i] >= 0))
		{
			enc_picture->mv[LIST_0][img->block_x+i][img->block_y+j][0] = img->all_mv[i][j][frefframe[j][i]][currMB->b8mode[i/2+(j/2)*2]][0];
			enc_picture->mv[LIST_0][img->block_x+i][img->block_y+j][1] = img->all_mv[i][j][frefframe[j][i]][currMB->b8mode[i/2+(j/2)*2]][1];
		}
		//    frefar[block_y+j][img->block_x+i] = frefframe[2*(j/2) + (i/2)];
    if(input->InterlaceCodingOption >= MB_CODING && mb_adaptive)
      rdopt->frefar[j][i]            = frefar[block_y+j][img->block_x+i];
  }
  if (bframe)
  {
    for (j=0; j<4; j++)
			for (i=0; i<4; i++)
			{
				//      brefar[block_y+j][img->block_x+i] = brefframe[2*(j/2) + (i/2)];
				brefar[block_y+j][img->block_x+i] = brefframe[j][i];
				
				enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j] = brefframe[j][i];
				
				// temporal direct mode
				if ((currMB->b8mode[i/2+(j/2)*2] == 0) && (!input->direct_type) && (enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j]<0))
        {
					enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j] = 0;
        }
		
				if ((currMB->b8pdir[i/2+(j/2)*2]==1) || ((currMB->b8pdir[i/2+(j/2)*2]==2) && (enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j]<0)))
					enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j] = 0;
				else if ((currMB->b8pdir[i/2+(j/2)*2]==0) || IS_INTRA(currMB))
				{
					enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j] = -1;
					enc_picture->mv[LIST_1][img->block_x+i][img->block_y+j][0] = 0;
					enc_picture->mv[LIST_1][img->block_x+i][img->block_y+j][1] = 0;
				}

				if ((enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j] >= 0) && (brefframe[j][i] >= 0))
				{
					enc_picture->mv[LIST_1][img->block_x+i][img->block_y+j][0] = img->all_bmv[i][j][0][currMB->b8mode[i/2+(j/2)*2]][0];
					enc_picture->mv[LIST_1][img->block_x+i][img->block_y+j][1] = img->all_bmv[i][j][0][currMB->b8mode[i/2+(j/2)*2]][1];
				}
				
        enc_picture->ref_pic_id [LIST_1][img->block_x+i][img->block_y+j] = enc_picture->ref_pic_num[LIST_1][enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j]];

        if(input->InterlaceCodingOption >= MB_CODING && mb_adaptive)

⌨️ 快捷键说明

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