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

📄 rdopt.c

📁 avs-s最新代码,包括编码器和解码器源码
💻 C
📖 第 1 页 / 共 5 页
字号:
        all_mv = img->all_mv;
        imgmv = img->mv;
      }

      by    = block_y+j;
      bxr   = block_x+i;
      bx    = block_x+i+4;
      ref   = (bframe?frefar:refar)[by][bxr];
      bw_ref = (bframe?brefar:refar)[by][bxr];
      
      if (!bframe)
      {
        if (mode8!=IBLOCK && ref != -1)
        {
          mv   [0][by][bx] = all_mv [i][j][ ref][mode8][0];
          mv   [1][by][bx] = all_mv [i][j][ ref][mode8][1];
        }
        else
        {
          mv   [0][by][bx] = 0;
          mv   [1][by][bx] = 0;
        }
      }
      else
      {
        if (pdir8==-1) // intra
        {
          fwMV [0][by][bx]     = 0;
          fwMV [1][by][bx]     = 0;
          bwMV [0][by][bx]     = 0;
          bwMV [1][by][bx]     = 0;
          dfMV     [0][by][bx] = 0;
          dfMV     [1][by][bx] = 0;
          dbMV     [0][by][bx] = 0;
          dbMV     [1][by][bx] = 0;
        }
        else if (pdir8==0) // forward
        {
          fwMV [0][by][bx]     = all_mv [i][j][ ref][mode8][0];
          fwMV [1][by][bx]     = all_mv [i][j][ ref][mode8][1];
          bwMV [0][by][bx]     = 0;
          bwMV [1][by][bx]     = 0;
          dfMV     [0][by][bx] = 0;
          dfMV     [1][by][bx] = 0;
          dbMV     [0][by][bx] = 0;
          dbMV     [1][by][bx] = 0;
        }
        else if (pdir8==1) // backward
        {
          fwMV [0][by][bx] = 0;
          fwMV [1][by][bx] = 0;
          {
            bwMV [0][by][bx] = all_bmv[i][j][   bw_ref][mode8][0];/*lgp*13*/
            bwMV [1][by][bx] = all_bmv[i][j][   bw_ref][mode8][1];/*lgp*13*/
          }
          dfMV     [0][by][bx] = 0;
          dfMV     [1][by][bx] = 0;
          dbMV     [0][by][bx] = 0;
          dbMV     [1][by][bx] = 0;
        }
        else if (mode8!=0) // bidirect
        {
          fwMV [0][by][bx] = all_mv [i][j][ ref][mode8][0];
          fwMV [1][by][bx] = all_mv [i][j][ ref][mode8][1];
		  {
			  int delta_P,TRp,DistanceIndexFw,DistanceIndexBw,refframe,delta_PB;
			  refframe = ref;
			  delta_P = 2*(img->imgtr_next_P_frm - img->imgtr_last_P_frm);
			  delta_P = (delta_P + 512)%512;   // Added by Xiaozhen ZHENG, 2007.05.05
			  if(img->picture_structure)
				  TRp = (refframe+1)*delta_P;  //the lates backward reference
			  else
			  {
				  TRp = delta_P;//refframe == 0 ? delta_P-1 : delta_P+1;
			  }
			  delta_PB = 2*(picture_distance - img->imgtr_last_P_frm);		// Tsinghua 200701
			  TRp = (TRp + 512)%512;
			  delta_PB = (delta_PB + 512)%512;  // Added by Xiaozhen ZHENG, 2007.05.05
			  if(!img->picture_structure)
			  {
				  if(img->current_mb_nr_fld < img->total_number_mb) //top field
					  DistanceIndexFw =  refframe == 0 ? delta_PB-1:delta_PB;
				  else
					  DistanceIndexFw =  refframe == 0 ? delta_PB:delta_PB+1;
			  }
			  else
				  DistanceIndexFw = delta_PB;
			  
			  //DistanceIndexBw  = TRp - DistanceIndexFw;	  	   
			  DistanceIndexBw    = (TRp - DistanceIndexFw+512)%512; // Added by Zhijie Yang, 20070419, Broadcom

		 		 
		  bwMV [0][by][bx] = - ((all_mv[i][j][ref][mode8][0]*DistanceIndexBw*(512/DistanceIndexFw)+256)>>9); 
		  bwMV [1][by][bx] = - ((all_mv[i][j][ref][mode8][1]*DistanceIndexBw*(512/DistanceIndexFw)+256)>>9); 

#ifdef half_pixel_compensation
if(!img->picture_structure)
{
	int delt, delt2;
					MV_compensation_sym(&delt, &delt2, 1-refframe, refframe); 
					assert(delt2 == delt);
				 // bwMV [0][by][bx] = - ((all_mv[i][j][ref][mode8][0]*DistanceIndexBw*(512/DistanceIndexFw)+256)>>9); 
		  bwMV [1][by][bx] = - (((all_mv[i][j][ref][mode8][1]+delt)*DistanceIndexBw*(512/DistanceIndexFw)+256)>>9)-delt2; 
}
#endif
		  }

          dfMV     [0][by][bx] = 0;
          dfMV     [1][by][bx] = 0;
          dbMV     [0][by][bx] = 0;
          dbMV     [1][by][bx] = 0;
        }
        else // direct
        {
			if(!img->picture_structure)
			{
				dref = max(0,fw_refFrArr[by][bxr]);
				fwMV [0][by][bx] = dfMV     [0][by][bx] = all_mv [i][j][dref][    0][0];
				fwMV [1][by][bx] = dfMV     [1][by][bx] = all_mv [i][j][dref][    0][1];
				dref = max(0,bw_refFrArr[by][bxr]);
				bwMV [0][by][bx] = dbMV     [0][by][bx] = all_bmv[i][j][   dref][    0][0];
				bwMV [1][by][bx] = dbMV     [1][by][bx] = all_bmv[i][j][   dref][    0][1];
			}
			else
			{
				dref = 0;
			//sw
			/*
			fwMV [0][by][bx] = dfMV     [0][by][bx] = all_mv [i][j][dref][    0][0];
			fwMV [1][by][bx] = dfMV     [1][by][bx] = all_mv [i][j][dref][    0][1];*/       
			fwMV [0][by][bx] = dfMV     [0][by][bx] = all_mv [i][j][dref][    0][0];
			fwMV [1][by][bx] = dfMV     [1][by][bx] = all_mv [i][j][dref][    0][1];
			
			bwMV [0][by][bx] = dbMV     [0][by][bx] = all_bmv[i][j][   0][    0][0];
			bwMV [1][by][bx] = dbMV     [1][by][bx] = all_bmv[i][j][   0][    0][1];
			}
        }
      }
  }
}

/*
*************************************************************************
* Function:R-D Cost for a macroblock
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/

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
{
	int        i, j; //, k, ****ip4;
	int         rate=0, distortion=0;
	double      rdcost;
	Macroblock  *currMB       = &img->mb_data[img->current_mb_nr];
	int         bframe        = (img->type==B_IMG);
	int         tmp_cc;
	int         use_of_cc     =  (img->type!=INTRA_IMG);
	int         cc_rate, dummy;
	byte        **imgY_orig   = imgY_org;
	byte        ***imgUV_orig = imgUV_org;
	int         pix_y         = img->pix_y;
	int         pix_c_y       = img->pix_c_y;
	int         pix_x         = img->pix_x;
	int         pix_c_x       = img->pix_c_x;
	/*lgp*/
	int         rate_tmp,rate_top=0,rate_bot=0,distortion_blk,distortion_top=0,distortion_bot=0;
	double      rdcost_top,rdcost_bot;
	int         incr_y=1,off_y=0;
	int         block8x8,block_x,block_y;
	int         stage_block8x8_pos=0;
	int         c_distortion=0,lum_bits;
	int         cp_table[2][6] = {{1,1,1,1,1,1},{0,0,1,1,0,1}};

	//cjw 20060321 for MV limit
	mv_out_of_range=0; 

	//=====  SET REFERENCE FRAMES AND BLOCK MODES
	SetModesAndRefframeForBlocks (mode);

	//=====  GET COEFFICIENTS, RECONSTRUCTIONS, CBP
	if (mode==I4MB)
	{
		currMB->cbp = Mode_Decision_for_Intra4x4Macroblock (lambda, &dummy);
	}
	/*	else if (mode==P8x8)
	{
	SetCoeffAndReconstruction8x8 (currMB);
	}
	*/  else
	{
		LumaResidualCoding ();
	}

	//Rate control
	for(i=0; i<16; i++)
		for(j=0; j<16; j++)
			pred[j][i] = img->mpr[i][j];

	dummy = 0;
	ChromaResidualCoding (&dummy);


	//////////////////////////////////////////////////////////////////////////
	////////////////////////****cjw  qhg add 20060327*************////////////
	///Just for Direct mode MB's weighting prediction/////////////////////////
	///if the direct MB has a zero cbp, it will be coded as a skip MB ////////
	///then the weighting prediction is unnecessary //////////////////////////
	//////////////////////////////////////////////////////////////////////////
	if(img->LumVarFlag&&img->mb_weighting_flag&&img->weighting_prediction&&(IS_DIRECT(currMB))&&(currMB->cbp==0)) //qhg add
	{
		int bw_ref;
		int sum_cnt_nonz=0;
		int fw_mode, bw_mode, refframe,coeff_cost;
		int mb_x;
		int mb_y;

		img->weighting_prediction=0;
		for (block8x8=stage_block8x8_pos/*lgp*/; block8x8<4; block8x8++)
		{
			mb_x = (block8x8 % 2) << 3;
			mb_y = (block8x8 / 2) << 3;
			SetModesAndRefframe (block8x8, &fw_mode, &bw_mode, &refframe, &bw_ref);
			sum_cnt_nonz += LumaResidualCoding8x8 (&(currMB->cbp), &(currMB->cbp_blk), block8x8,fw_mode, bw_mode, refframe, bw_ref);
			if (sum_cnt_nonz <= _LUMA_COEFF_COST_)
			{
				coeff_cost  = 0;
				currMB->cbp&=  (63 - (1<<block8x8));
				for (i=mb_x; i<mb_x+8; i++)
					for (j=mb_y; j<mb_y+8; j++)
					{
						imgY[img->pix_y+j][img->pix_x+i] = img->mpr[i][j];
					}
			}
		}
		for(i=0; i<16; i++)
			for(j=0; j<16; j++)
				pred[j][i] = img->mpr[i][j];
		ChromaResidualCoding(&dummy);
	}
	//////////////////////////////////////////////////////////////////////////
	////////////////********************* end *****************************///
	//////////////////////////////////////////////////////////////////////////

	//=====   GET DISTORTION
	// LUMA
	/*lgp*/
	for(block8x8=stage_block8x8_pos; block8x8<4;block8x8++)
	{
		block_y = (block8x8/2)*8;
		block_x = (block8x8%2)*8;

		distortion_blk =0;

		for (j=0; j<8; j++)
		{
			for (i=0; i<8; i++)
			{
				distortion_blk += img->quad [imgY_orig[block_y+incr_y*j+off_y+pix_y][block_x+i+pix_x] - imgY[img->pix_y+block_y+incr_y*j+off_y][img->pix_x+block_x+i]];
			}
		}

		//4:0:0 WANGJP START
		if (input->chroma_format)
		{
			block_y = (block8x8/2)*4;
			block_x = (block8x8%2)*4;

			// CHROMA
			for (j=0; j<4; j++)
			{
				for (i=0; i<4; i++)
				{
					distortion_blk += img->quad [imgUV_orig[0][block_y+incr_y*j+off_y+pix_c_y][block_x+i+pix_c_x] - imgUV[0][img->pix_c_y+block_y+incr_y*j+off_y][img->pix_c_x+block_x+i]];
					distortion_blk += img->quad [imgUV_orig[1][block_y+incr_y*j+off_y+pix_c_y][block_x+i+pix_c_x] - imgUV[1][img->pix_c_y+block_y+incr_y*j+off_y][img->pix_c_x+block_x+i]];

					c_distortion += img->quad [imgUV_orig[0][block_y+incr_y*j+off_y+pix_c_y][block_x+i+pix_c_x] - imgUV[0][img->pix_c_y+block_y+incr_y*j+off_y][img->pix_c_x+block_x+i]];
					c_distortion += img->quad [imgUV_orig[1][block_y+incr_y*j+off_y+pix_c_y][block_x+i+pix_c_x] - imgUV[1][img->pix_c_y+block_y+incr_y*j+off_y][img->pix_c_x+block_x+i]];
				}
			}
		}
		//WANGJP END

		if(block8x8<2)
			distortion_top += distortion_blk;
		else
			distortion_bot += distortion_blk;

	}

	//4:0:0 WANGJP START
	if (input->chroma_format==0)
	{
		currMB->cbp &= 0xf;
	}
	//WANGJP END
	//=====   S T O R E   C O D I N G   S T A T E   =====
	store_coding_state (cs_cm);

	//=====   GET RATE
	//----- macroblock header -----
	if (use_of_cc)
	{
		if (currMB->mb_type!=0 || (bframe && currMB->cbp!=0))
		{
			// cod counter and macroblock mode are written ==> do not consider code counter
			tmp_cc = img->cod_counter;
			rate_top   = writeMBHeader (1); 
			ue_linfo (tmp_cc, dummy, &cc_rate, &dummy);
			rate_top  -= cc_rate;/*lgp*/
			img->cod_counter = tmp_cc;
		}
		else
		{
			// cod counter is just increased  ==> get additional rate
			ue_linfo (img->cod_counter+1, dummy, &rate_top,    &dummy);
			ue_linfo (img->cod_counter,   dummy, &cc_rate, &dummy);
			rate_top -= cc_rate;/*lgp*/
		}
	}
	else if (!(stage_block8x8_pos/2))/*lgp*/
	{
		rate_top = writeMBHeader (1); /*lgp*/
	}

	if (mode)
	{
		//----- motion information -----
		storeMotionInfo (stage_block8x8_pos/2);
		writeReferenceIndex(stage_block8x8_pos/2,&rate_top,&rate_bot);
		writeMVD(stage_block8x8_pos/2,&rate_top,&rate_bot);
		writeCBPandDqp(0,&rate_top,&rate_bot);
	}

	if (mode || (bframe && (currMB->cbp!=0)))
	{
		for(block8x8=stage_block8x8_pos; block8x8<6;block8x8++)
			if(cp_table[stage_block8x8_pos/2][block8x8])
			{
				rate_tmp = writeBlockCoeff (block8x8);

				if(block8x8 < 2)
					rate_top +=rate_tmp;
				else if(block8x8< 4)
					rate_bot +=rate_tmp;
				if(block8x8 == 4)
					rate_top +=rate_tmp;
				if(block8x8 == 5)
					rate_bot +=rate_tmp;

				if(block8x8 < 4)
					lum_bits = rate_bot+rate_top;
			}

	}

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

	rdcost_top = (double)distortion_top + lambda * (double)rate_top; 
	rdcost_bot = (double)distortion_bot + lambda * (double)rate_bot;

	rdcost = rdcost_top + rdcost_bot;

	if (rdcost >= *min_rdcost)
	{
		return 0;   
	}

	//cjw 20060321 for MV limit
	if ( mv_out_of_range == 1 )
	{
		return 0;   //return 0 means it is not the best mode
	}

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

	return 1;
}

/*
*************************************************************************
* Function:Store macroblock parameters
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/

void store_macroblock_parameters (int mode)
{
  int  i, j, k,l,****i4p, ***i3p;
  Macroblock *currMB  = &img->mb_data[img->current_mb_nr];
  int        bframe   = (img->type==B_IMG);
  int        **frefar = ((img->type==B_IMG)? fw_refFrArr : refFrArr);
  int        **brefar = bw_refFrArr;
  int    block_x  = img->block_x;
  int    pix_x    = img->pix_x;
  int    pix_c_x  = img->pix_c_x;
  int    block_y  = img->block_y;
  int    pix_y    = img->pix_y;
  int    pix_c_y  = img->pix_c_y;
	int    stage_block8x8_pos=0;/*lgp*/
	int    temp_chroma;

  //--- store best mode ---
  best_mode = mode;
  best_c_imode = currMB->c_ipred_mode;

	// !! shenyanfei 
  best_weight_flag = img->weighting_prediction ;

  for (i=/*0*/stage_block8x8_pos/*lgp*/; 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++)
    {

⌨️ 快捷键说明

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