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

📄 b_frame.c

📁 Mobile IP VCEG的信道模拟程序
💻 C
📖 第 1 页 / 共 4 页
字号:
							 if0*jf1*mcef[img->fw_multframe_no][uv][jj1][ii0]+
							 if1*jf1*mcef[img->fw_multframe_no][uv][jj1][ii1]+f4)/f3;
					
					ii=(img->pix_c_x+i)*f1+tmp_bwMV[0][pic_block_y][pic_block_x+4];
					jj=(img->pix_c_y+j)*f1+tmp_bwMV[1][pic_block_y][pic_block_x+4];
#ifndef UMV
					ii0=ii/f1;
					jj0=jj/f1;
					ii1=(ii+f2)/f1;
					jj1=(jj+f2)/f1;
#endif
#ifdef UMV
					ii0= max (0, min (img->width_cr-1, ii/f1));
					jj0= max (0, min (img->height_cr-1, jj/f1));
					ii1= max (0, min (img->width_cr-1, (ii+f2)/f1));
					jj1= max (0, min (img->height_cr-1, (jj+f2)/f1));
#endif

					if1=(ii & f2);
					jf1=(jj & f2);
					if0=f1-if1;
					jf0=f1-jf1;
					bw_pred=(if0*jf0*mcef_P[uv][jj0][ii0]+if1*jf0*mcef_P[uv][jj0][ii1]+
							 if0*jf1*mcef_P[uv][jj1][ii0]+if1*jf1*mcef_P[uv][jj1][ii1]+f4)/f3;

					img->mpr[i][j]=(int)((fw_pred+bw_pred)/2.0+0.5);
					img->m7[i][j]=imgUV_org[uv][img->pix_c_y+j][img->pix_c_x+i]-img->mpr[i][j]; 
				}
			}
		}
		else // img->imod == B_Direct  
		{
			for (j=0; j < MB_BLOCK_SIZE/2; j++) 
			{
				pic_block_y=(img->pix_c_y+j)/2;
				for (i=0; i < MB_BLOCK_SIZE/2; i++) 
				{
					pic_block_x=(img->pix_c_x+i)/2;

					ii=(img->pix_c_x+i)*f1+dfMV[0][pic_block_y][pic_block_x+4];
					jj=(img->pix_c_y+j)*f1+dfMV[1][pic_block_y][pic_block_x+4];
#ifndef UMV
					ii0=ii/f1;
					jj0=jj/f1;
					ii1=(ii+f2)/f1;
					jj1=(jj+f2)/f1;
#endif
#ifdef UMV
					ii0= max (0, min (img->width_cr-1, ii/f1));
					jj0= max (0, min (img->height_cr-1, jj/f1));
					ii1= max (0, min (img->width_cr-1, (ii+f2)/f1));
					jj1= max (0, min (img->height_cr-1, (jj+f2)/f1));
#endif

					if1=(ii & f2);
					jf1=(jj & f2);
					if0=f1-if1;
					jf0=f1-jf1;
					
					// next P is intra mode
					if(refFrArr[pic_block_y][pic_block_x]==-1)
						ref_inx=(img->number-1)%img->buf_cycle;
					// next P is skip or inter mode
					else
						ref_inx=(img->number-refFrArr[pic_block_y][pic_block_x]-1)%img->buf_cycle;
					
					fw_pred=(if0*jf0*mcef[ref_inx][uv][jj0][ii0]+
							 if1*jf0*mcef[ref_inx][uv][jj0][ii1]+
							 if0*jf1*mcef[ref_inx][uv][jj1][ii0]+
							 if1*jf1*mcef[ref_inx][uv][jj1][ii1]+f4)/f3;
					
					ii=(img->pix_c_x+i)*f1+dbMV[0][pic_block_y][pic_block_x+4];
					jj=(img->pix_c_y+j)*f1+dbMV[1][pic_block_y][pic_block_x+4];
#ifndef UMV
					ii0=ii/f1;
					jj0=jj/f1;
					ii1=(ii+f2)/f1;
					jj1=(jj+f2)/f1;
#endif
#ifdef UMV
					ii0= max (0, min (img->width_cr-1, ii/f1));
					jj0= max (0, min (img->height_cr-1, jj/f1));
					ii1= max (0, min (img->width_cr-1, (ii+f2)/f1));
					jj1= max (0, min (img->height_cr-1, (jj+f2)/f1));
#endif

					if1=(ii & f2);
					jf1=(jj & f2);
					if0=f1-if1;
					jf0=f1-jf1;
					
					bw_pred=(if0*jf0*mcef_P[uv][jj0][ii0]+if1*jf0*mcef_P[uv][jj0][ii1]+
							 if0*jf1*mcef_P[uv][jj1][ii0]+if1*jf1*mcef_P[uv][jj1][ii1]+f4)/f3;

					img->mpr[i][j]=(int)((fw_pred+bw_pred)/2.0+0.5);
					
					// LG : direct residual coding
					img->m7[i][j]=imgUV_org[uv][img->pix_c_y+j][img->pix_c_x+i]-img->mpr[i][j]; 
				}
			}
		}
		*cr_cbp=dct_chroma(uv, *cr_cbp);
	}
	// LG : direct residual coding
	currMB->cbp += *cr_cbp*16;
}


/************************************************************************
*
*  Name :       writeMotionInfo2NAL_Bframe()
*
*  Description: Passes for a given MB of a B picture the reference frame 
*								parameter and motion parameters to the NAL 
*
************************************************************************/
#ifdef _RD_OPT_
int
#else
void
#endif
writeMotionInfo2NAL_Bframe()
{
	int i,j,k,l,m;
	int step_h,step_v;
	int curr_mvd=0, fw_blk_shape=0, bw_blk_shape=0;
	int mb_nr = img->current_mb_nr;
	Macroblock *currMB = &img->mb_data[mb_nr];
	const int fw_predframe_no=currMB->ref_frame;
	SyntaxElement *currSE = &img->MB_SyntaxElements[currMB->currSEnr];
	int *bitCount = currMB->bitcounter;
	Slice *currSlice = img->currentSlice;
	DataPartition *dataPart;
	int *partMap = assignSE2partition[input->partition_mode];
#ifdef _RD_OPT_
	int no_bits = 0;
#endif


	/* Write fw_predframe_no(Forward, Bidirect) */ 
	if(img->imod==B_Forward || img->imod==B_Bidirect) 
	{
#ifdef _ADDITIONAL_REFERENCE_FRAME_
		if (img->no_multpred > 1 || input->add_ref_frame > 0) 
#else
	    if (img->no_multpred > 1)
#endif
		{		
			currSE->value1 = currMB->ref_frame;
			currSE->type = SE_REFFRAME;
			if (input->symbol_mode == UVLC)
				currSE->mapping = n_linfo2;
			else
				currSE->writing = writeRefFrame2Buffer_CABAC;

			dataPart = &(currSlice->partArr[partMap[SE_BFRAME]]);
			dataPart->writeSyntaxElement(	currSE, dataPart);
			bitCount[BITS_INTER_MB]+=currSE->len;
#ifdef _RD_OPT_
			no_bits += currSE->len;
#endif
#if TRACE
			sprintf(currSE->tracestring, "B_fw_Reference frame no %3d ", currMB->ref_frame);
#endif				
			/* proceed to next SE */
			currSE++;	
			currMB->currSEnr++;			
		}
	}
			
	/* Write Blk_size(Bidirect) */					  
	if(img->imod==B_Bidirect) 
	{
		/* Write blockshape for forward pred */
		fw_blk_shape = BlkSize2CodeNumber(img->fw_blc_size_h, img->fw_blc_size_v);
		
		currSE->value1 = fw_blk_shape;
		currSE->type = SE_BFRAME;
		if (input->symbol_mode == UVLC)
			currSE->mapping = n_linfo2;
		else
			currSE->writing = writeBiDirBlkSize2Buffer_CABAC;
				
		dataPart = &(currSlice->partArr[partMap[SE_BFRAME]]);
		dataPart->writeSyntaxElement(	currSE, dataPart);
		bitCount[BITS_INTER_MB]+=currSE->len;
#ifdef _RD_OPT_
		no_bits += currSE->len;
#endif
#if TRACE
		sprintf(currSE->tracestring, "B_bidiret_fw_blk %3d x %3d ", img->fw_blc_size_h, img->fw_blc_size_v);
#endif
				
		/* proceed to next SE */
		currSE++;			
		currMB->currSEnr++;			

		/* Write blockshape for backward pred */
		bw_blk_shape = BlkSize2CodeNumber(img->bw_blc_size_h, img->bw_blc_size_v);
		
		currSE->value1 = bw_blk_shape;
		currSE->type = SE_BFRAME;
		if (input->symbol_mode == UVLC)
			currSE->mapping = n_linfo2;
		else
			currSE->writing = writeBiDirBlkSize2Buffer_CABAC;
				
		dataPart = &(currSlice->partArr[partMap[SE_BFRAME]]);
		dataPart->writeSyntaxElement(	currSE, dataPart);
		bitCount[BITS_INTER_MB]+=currSE->len;
#ifdef _RD_OPT_
		no_bits += currSE->len;
#endif
#if TRACE
		sprintf(currSE->tracestring, "B_bidiret_bw_blk %3d x %3d ", img->bw_blc_size_h, img->bw_blc_size_v);
#endif
				
		/* proceed to next SE */
		currSE++;			
		currMB->currSEnr++;			
		
	}

	/* Write MVDFW(Forward, Bidirect) */
	if(img->imod==B_Forward || img->imod==B_Bidirect) 
	{
		step_h=img->fw_blc_size_h/BLOCK_SIZE;  // horizontal stepsize 
		step_v=img->fw_blc_size_v/BLOCK_SIZE;  // vertical stepsize 

		for (j=0; j < BLOCK_SIZE; j += step_v) 
		{            
			for (i=0;i < BLOCK_SIZE; i += step_h) 
			{
				for (k=0; k < 2; k++) 
				{
					if(img->mb_mode==1) // fw 16x16
						curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][1][k]);
					else 
						if(img->mb_mode==3) // bidirectinal
						{
							switch(fw_blk_shape)
							{
								case 0:
									curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][1][k]);
									break;
								case 1:
									curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][2][k]);
									break;
								case 2:
									curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][3][k]);
									break;
								case 3:
									curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][4][k]);
									break;
								case 4:
									curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][5][k]);
									break;
								case 5:
									curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][6][k]);
									break;
								case 6:
									curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][7][k]);
									break;
								default:
									break;
							}
						}
						else
							curr_mvd=(tmp_fwMV[k][img->block_y+j][img->block_x+i+4]-img->p_fwMV[i][j][fw_predframe_no][(img->mb_mode)/2][k]);
				
					/* store (oversampled) mvd */
					for (l=0; l < step_v; l++) 
						for (m=0; m < step_h; m++)	
							currMB->mvd[0][j+l][i+m][k] = curr_mvd;
				
					currSE->value1 = curr_mvd;
					currSE->type = SE_MVD;
					if (input->symbol_mode == UVLC)
						currSE->mapping = mvd_linfo2;
					else
					{
						img->subblock_x = i; // position used for context determination
						img->subblock_y = j; // position used for context determination
						currSE->value2 = 2*k; // identifies the component and the direction; only used for context determination
						currSE->writing = writeBiMVD2Buffer_CABAC;
					}	
					dataPart = &(currSlice->partArr[partMap[SE_BFRAME]]);
					dataPart->writeSyntaxElement(	currSE, dataPart);
					bitCount[BITS_INTER_MB]+=currSE->len;
#ifdef _RD_OPT_
					no_bits += currSE->len;
#endif
#if TRACE
					sprintf(currSE->tracestring, " MVD(%d) = %3d",k, curr_mvd);
#endif
							
					/* proceed to next SE */
					currSE++;	
					currMB->currSEnr++;
				}
			}
		}
	}
			
	/* Write MVDBW(Backward, Bidirect) */
	if(img->imod==B_Backward || img->imod==B_Bidirect) 
	{
		step_h=img->bw_blc_size_h/BLOCK_SIZE;  // horizontal stepsize 
		step_v=img->bw_blc_size_v/BLOCK_SIZE;  // vertical stepsize 

		for (j=0; j < BLOCK_SIZE; j += step_v) 
		{
			for (i=0;i < BLOCK_SIZE; i += step_h) 
			{
				for (k=0; k < 2; k++) 
				{
					if(img->mb_mode==2) // bw 16x16
						curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][1][k]);
					else 
						if(img->mb_mode==3) // bidirectional
						{
							switch(bw_blk_shape)
							{
								case 0:
									curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][1][k]);
									break;
								case 1:
									curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][2][k]);
									break;
								case 2:
									curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][3][k]);
									break;
								case 3:
									curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][4][k]);
									break;
								case 4:
									curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][5][k]);
									break;
								case 5:
									curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][6][k]);
									break;
								case 6:
									curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][7][k]);
									break;
								default:
									break;
							}
						}
						else // other bw
							curr_mvd=(tmp_bwMV[k][img->block_y+j][img->block_x+i+4]-img->p_bwMV[i][j][0][(img->mb_mode-1)/2][k]);						  				


					/* store (oversampled) mvd */
					for (l=0; l < step_v; l++) 
						for (m=0; m < step_h; m++)	
							currMB->mvd[1][j+l][i+m][k] = curr_mvd;
				
					currSE->value1 = curr_mvd;
					currSE->type = SE_MVD;

					if (input->symbol_mode == UVLC)
						currSE->mapping = mvd_linfo2;
					else
					{
						img->subblock_x = i; // position used for context determination
						img->subblock_y = j; // position used for context determination
						currSE->value2 = 2*k+1; // identifies the component and the direction; only used for context determination
						currSE->writing = writeBiMVD2Buffer_CABAC;
					}	

					dataPart = &(currSlice->partArr[partMap[SE_BFRAME]]);
					dataPart->writeSyntaxElement(	currSE, dataPart);
					bitCount[BITS_INTER_MB]+=currSE->len;
#ifdef _RD_OPT_
					no_bits += currSE->len;
#endif
#if TRACE
					sprintf(currSE->tracestring, " MVD(%d) = %3d",k, curr_mvd);
#endif
						
					/* proceed to next SE */
					currSE++;		
					currMB->currSEnr++;
					
				}
			}
		}
	}	
#ifdef _RD_OPT_
	return no_bits;
#endif
}

/************************************************************************
*
*  Name :       BlkSize2CodeNumber()
*
*  Description: Passes back the code number given the blocksize width and
*								height (should be replaced by an appropriate table lookup)
*
************************************************************************/
int BlkSize2CodeNumber(int blc_size_h, int blc_size_v)
{

	if(blc_size_h==16 && blc_size_v==16)  // 16x16 : code_number 0			
		return 0;
	else 
		if(blc_size_h==16 && blc_size_v==8)  // 16x8 : code_number 1
			return 1;
		else 
			if(blc_size_h==8 && blc_size_v==16) // 8x16 : code_number 2
				return 2;
			else 
				if(blc_size_h==8 && blc_size_v==8) // 8x8 : code_number 3
					return 3;
				else 
					if(blc_size_h==8 && blc_size_v==4)  // 8x4 : code_number 4
						return 4;
					else 
						if(blc_size_h==4 && blc_size_v==8) // 4x8 : code_number 5
							return 5;
						else  // 4x4 : code_number 6
							return 6;

}

////////////////////////////////////////////////////////////////////
// select intra, forward, backward, bidirectional, direct mode
////////////////////////////////////////////////////////////////////
int motion_search_Bframe(int tot_intra_sad)
{
	int fw_sad, bw_sad, bid_sad, dir_sad;
	int fw_predframe_no;

	fw_predframe_no=get_fwMV(&fw_sad, tot_intra_sad); 

	get_bwMV(&bw_sad); 
	//bw_sad =MAX_VALUE;

	get_bid(&bid_sad, fw_predframe_no); 
	//bid_sad=MAX_VALUE;

	get_dir(&dir_sad); 
	//dir_sad=MAX_VALUE;


	compare_sad(tot_intra_sad, fw_sad, bw_sad, bid_sad, dir_sad); 

	return fw_predframe_no;	
}


void get_bid(int *bid_sad, int fw_predframe_no)
{
	int mb_y,mb_x, block_y, block_x, pic_pix_y, pic_pix_x, pic_block_y, pic_block_x;
	int i, j, ii4, jj4, iii4, jjj4, i2, j2; 

⌨️ 快捷键说明

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