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

📄 macroblock.c

📁 Mobile IP VCEG的信道模拟程序
💻 C
📖 第 1 页 / 共 3 页
字号:
/*!
 *	\file
 *			Macroblock.c
 *	\brief
 *			Decode a Macroblock
 *	\author
 *			Main contributors (see contributors.h for copyright, address and affiliation details)
 *			Inge Lille-Lang鴜               <inge.lille-langoy@telenor.com>
 *			Rickard Sjoberg                 <rickard.sjoberg@era.ericsson.se>
 *			Jani Lainema                    <jani.lainema@nokia.com>
 *			Sebastian Purreiter             <sebastian.purreiter@mch.siemens.de>
 *			Thomas Wedi						<wedi@tnt.uni-hannover.de>
 *      Detlev Marpe                    <marpe@hhi.de>
 *      Gabi Blaettermann               <blaetter@hhi.de>
 */
#include "contributors.h"

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>

#include "global.h"
#include "elements.h"
#include "macroblock.h"

/************************************************************************
*
*  Name :       SetLoopfilterStrength_P()
*
*  Description: Set the filter strength for a macroblock of a I- or P-frame
*
************************************************************************/
void SetLoopfilterStrength_P(struct img_par *img)
{
	int i,j;
	int ii,jj;
	int i3,j3,mvDiffX,mvDiffY;

	if (img->imod == INTRA_MB_OLD || img->imod == INTRA_MB_NEW) 
	{
		for (i=0;i<BLOCK_MULTIPLE;i++)
		{
			ii=img->block_x+i;
			i3=ii/2;
			for (j=0;j<BLOCK_MULTIPLE;j++)
			{
				jj=img->block_y+j;
				j3=jj/2;
				loopb[ii+1][jj+1]=3;
				loopb[ii  ][jj+1]=max(loopb[ii  ][jj+1],2);
				loopb[ii+1][jj  ]=max(loopb[ii+1][jj  ],2);
				loopb[ii+2][jj+1]=max(loopb[ii+2][jj+1],2);
				loopb[ii+1][jj+2]=max(loopb[ii+1][jj+2],2);
				
				loopc[i3+1][j3+1]=2;
				loopc[i3  ][j3+1]=max(loopc[i3  ][j3+1],1);
				loopc[i3+1][j3  ]=max(loopc[i3+1][j3  ],1);
				loopc[i3+2][j3+1]=max(loopc[i3+2][j3+1],1);
				loopc[i3+1][j3+2]=max(loopc[i3+1][j3+2],1);
			}
		}
	}
	else
	{
		for (i=0;i<4;i++)
		{
			ii=img->block_x+i;
			i3=ii/2;
			for (j=0;j<4;j++)
			{
				jj=img->block_y+j;
				j3=jj/2;
			
				mvDiffX = img->mv[ii+4][jj][0] - img->mv[ii-1+4][jj][0];
				mvDiffY = img->mv[ii+4][jj][1] - img->mv[ii-1+4][jj][1];
				if((mvDiffX*mvDiffX >= 16 || mvDiffY*mvDiffY >= 16) && ii > 0)
				{ 
					loopb[ii  ][jj+1]=max(loopb[ii  ][jj+1],1);
					loopb[ii+1][jj+1]=max(loopb[ii+1][jj+1],1); 
					loopc[i3  ][j3+1]=max(loopc[i3  ][j3+1],1);
					loopc[i3+1][j3+1]=max(loopc[i3+1][j3+1],1);
				}
				
				if(jj > 0) /*GH: bug fix to avoid img->mv[][-1][ii+4]*/
				{			
				  mvDiffX = img->mv[ii+4][jj][0] - img->mv[ii+4][jj-1][0];
				  mvDiffY = img->mv[ii+4][jj][1] - img->mv[ii+4][jj-1][1];
				  if(mvDiffX*mvDiffX >= 16 || mvDiffY*mvDiffY >= 16)
				  {
					loopb[ii+1][jj  ]=max(loopb[ii+1][jj  ],1);
					loopb[ii+1][jj+1]=max(loopb[ii+1][jj+1],1);
					loopc[i3+1][j3  ]=max(loopc[i3+1][j3  ],1);
					loopc[i3+1][j3+1]=max(loopc[i3+1][j3+1],1);
				  }
				}
			}
		}		
	}
}

/************************************************************************
*
 *  Name :       CheckAvailabilityOfNeighbors(struct img_par *img)
*
*  Description: Checks the availability of neighboring macroblocks of 
*								the current macroblock for prediction and context determination;
*								marks the unavailable MBs for intra prediction in the 
*								ipredmode-array by -1. Only neighboring MBs in the causal 
*								past of the current MB are checked.
*
************************************************************************/
void CheckAvailabilityOfNeighbors(struct img_par *img)
{
	int i,j;
	const int mb_width = img->width/MB_BLOCK_SIZE;
	const int mb_nr = img->current_mb_nr;
	Macroblock *currMB = &img->mb_data[mb_nr];

	/* mark all neighbors as unavailable */
	for (i=0; i<3; i++)
		for (j=0; j<3; j++)
			img->mb_data[mb_nr].mb_available[i][j]=NULL;
	img->mb_data[mb_nr].mb_available[1][1]=currMB; /* current MB */

	/* Check MB to the left */
	if(img->pix_x >= MB_BLOCK_SIZE)
	{
		if(currMB->slice_nr != img->mb_data[mb_nr-1].slice_nr)
		{
			img->ipredmode[img->block_x][img->block_y+1] = -1;
			img->ipredmode[img->block_x][img->block_y+2] = -1;
			img->ipredmode[img->block_x][img->block_y+3] = -1;
			img->ipredmode[img->block_x][img->block_y+4] = -1;
		} else
			currMB->mb_available[1][0]=&(img->mb_data[mb_nr-1]);
	} 


	/* Check MB above */
	if(img->pix_y >= MB_BLOCK_SIZE)
	{
		if(currMB->slice_nr != img->mb_data[mb_nr-mb_width].slice_nr)
		{
			img->ipredmode[img->block_x+1][img->block_y] = -1;
			img->ipredmode[img->block_x+2][img->block_y] = -1;
			img->ipredmode[img->block_x+3][img->block_y] = -1;
			img->ipredmode[img->block_x+4][img->block_y] = -1;
		} else
		    currMB->mb_available[0][1]=&(img->mb_data[mb_nr-mb_width]);
	}

	/* Check MB left above */
	if(img->pix_x >= MB_BLOCK_SIZE && img->pix_y  >= MB_BLOCK_SIZE )
	{
		if(currMB->slice_nr == img->mb_data[mb_nr-mb_width-1].slice_nr)
			img->mb_data[mb_nr].mb_available[0][0]=&(img->mb_data[mb_nr-mb_width-1]);
	}

	/* Check MB right above */
	if(img->pix_y >= MB_BLOCK_SIZE && img->pix_x < (img->width-MB_BLOCK_SIZE ))
	{
		if(currMB->slice_nr == img->mb_data[mb_nr-mb_width+1].slice_nr)
			currMB->mb_available[0][2]=&(img->mb_data[mb_nr-mb_width+1]);
	}
}

/************************************************************************
*
*  Name :       start_macroblock()
*
*  Description: initializes the current macroblock
*
************************************************************************/
void start_macroblock(struct img_par *img,struct inp_par *inp)
{
	int i,j,k,l;
	Macroblock *currMB = &img->mb_data[img->current_mb_nr];
	//	Slice *curr_slice = img->currentSlice;
	
	/* Save the slice number of this macroblock. When the macroblock below     */
	/* is coded it will use this to decide if prediction for above is possible */
	img->slice_numbers[img->current_mb_nr] = img->current_slice_nr;

	/* Save the slice and macroblock number of the current MB */
	currMB->slice_nr = img->current_slice_nr;

	/* If MB is next to a slice boundary, mark neighboring blocks unavailable for prediction */
	CheckAvailabilityOfNeighbors(img);

	/* Reset syntax element entries in MB struct */
	currMB->mb_type = 0;
	currMB->ref_frame = 0;
    currMB->predframe_no = 0;

	currMB->cbp = 0;

	for (l=0; l < 2; l++)
		for (j=0; j < BLOCK_MULTIPLE; j++)
			for (i=0; i < BLOCK_MULTIPLE; i++)
				for (k=0; k < 2; k++)
					currMB->mvd[l][j][i][k] = 0;

	for (i=0; i < (BLOCK_MULTIPLE*BLOCK_MULTIPLE); i++)
		currMB->intra_pred_modes[i] = 0;
}

/************************************************************************
*
*  Name :       exit_macroblock()
*
*  Description: set coordinates of the next macroblock
*               check end_of_slice condition (have to implement)
*
************************************************************************/
int exit_macroblock(struct img_par *img,struct inp_par *inp)
{   
    const int number_mb_per_row = img->width / MB_BLOCK_SIZE ;
	Slice *currSlice = img->currentSlice;

    /* Update coordinates of the next macroblock */
	img->mb_x++;
	if (img->mb_x == number_mb_per_row) /* next row of MBs */
	{
		img->mb_x = 0; /* start processing of next row */
		img->mb_y++;
	}
 	img->current_mb_nr++;

	/* Define vertical positions */
	img->block_y = img->mb_y * BLOCK_SIZE;      /* luma block position */
	img->pix_y   = img->mb_y * MB_BLOCK_SIZE;   /* luma macroblock position */
	img->pix_c_y = img->mb_y * MB_BLOCK_SIZE/2; /* chroma macroblock position */
	
	/* Define horizontal positions */
	img->block_x = img->mb_x * BLOCK_SIZE;      /* luma block position */
	img->pix_x   = img->mb_x * MB_BLOCK_SIZE;   /* luma pixel position */
	img->pix_c_x = img->mb_x * MB_BLOCK_SIZE/2; /* chroma pixel position */

    if (img->current_mb_nr == img->max_mb_nr) 
	{
		if (currSlice->next_header != EOS)
			currSlice->next_header = SOP;		
        return TRUE;
	}
	/* ask for last mb in the slice  UVLC*/
    else if(nal_startcode_follows(img, inp)==TRUE)
        return TRUE;
    else
        return FALSE;
}
/************************************************************************
*
*  Name :       interpret_mb_mode_P()
*
*  Description: Interpret the mb mode for P-Frames
*
************************************************************************/
void interpret_mb_mode_P(struct img_par *img)
{
    const int ICBPTAB[6] = {0,16,32,15,31,47};
    Macroblock *currMB = &img->mb_data[img->current_mb_nr];

    if (img->mb_mode == INTRA_MB)   /* 4x4 intra */
		img->imod = currMB->mb_imode = INTRA_MB_OLD;
	if (img->mb_mode > INTRA_MB)    /* 16x16 intra */
	{
		img->imod = currMB->mb_imode = INTRA_MB_NEW;     //mod0=img->mb_mode-1;kmod=mod0 & 3;cbp = ICBPTAB[mod0/4];
		currMB->intra_pred_modes[0] = (img->mb_mode - INTRA_MB-1) & 3; 
		currMB->cbp = ICBPTAB[(img->mb_mode - INTRA_MB-1)>>2]; 
	}
	if (img->mb_mode < INTRA_MB)    /* inter prediction mode (block shape) */
		img->imod = currMB->mb_imode = INTRA_MB_INTER;   /* intra in inter frame */ 
}
/************************************************************************
*
*  Name :       interpret_mb_mode_I()
*
*  Description: Interpret the mb mode for I-Frames
*
************************************************************************/
void interpret_mb_mode_I(struct img_par *img)
{
    const int ICBPTAB[6] = {0,16,32,15,31,47};
    Macroblock *currMB = &img->mb_data[img->current_mb_nr];

	if (img->mb_mode == 0)
		img->imod = currMB->mb_imode = INTRA_MB_OLD;     /* 4x4 intra */
	else
	{
		img->imod = currMB->mb_imode = INTRA_MB_NEW;     /* 16x16 intra */ //mod0=img->mb_mode-1;kmod=mod0 & 3;cbp = ICBPTAB[mod0/4];
        currMB->intra_pred_modes[0] = (img->mb_mode - 1) & 3; 
	    currMB->cbp = ICBPTAB[(img->mb_mode - 1)>>2]; 
	}	
    
}
/************************************************************************
*
*  Name :       interpret_mb_mode_B()
*
*  Description: Interpret the mb mode for B-Frames
*
************************************************************************/
void interpret_mb_mode_B(struct img_par *img)
{
    const int ICBPTAB[6] = {0,16,32,15,31,47};
    Macroblock *currMB = &img->mb_data[img->current_mb_nr];

   
	if (img->mb_mode == INTRA_MB_B) /* 4x4 intra */
		img->imod = currMB->mb_imode = INTRA_MB_OLD;
	if (img->mb_mode > INTRA_MB_B)  /* 16x16 intra */
	{
		img->imod = currMB->mb_imode = INTRA_MB_NEW;
  		currMB->intra_pred_modes[0] = (img->mb_mode - INTRA_MB_B-1) & 3; 
		currMB->cbp = ICBPTAB[(img->mb_mode - INTRA_MB_B-1)>>2];  
	}
	if (img->mb_mode < INTRA_MB_B)  /* intra in inter frame */
	  {
		if(img->mb_mode == 0) 
			img->imod = currMB->mb_imode = B_Direct;	
		else if(img->mb_mode == 3) 
            img->imod = currMB->mb_imode = B_Bidirect;
  		else if(img->mb_mode==1 || (img->mb_mode>3 && img->mb_mode%2==0)) 
            img->imod = currMB->mb_imode = B_Forward;
		else if(img->mb_mode==2 || (img->mb_mode>4 && img->mb_mode%2==1)) 
            img->imod = currMB->mb_imode = B_Backward;
        else img->imod = 3/img->mb_mode;
	  }
}
/************************************************************************
*
*  Name :       init_macroblock()
*
*  Description: init macroblock I and P frames
*
************************************************************************/
void init_macroblock(struct img_par *img)
{
    int i,j;
    int predframe_no;
    Macroblock *currMB = &img->mb_data[img->current_mb_nr];

    img->mv[img->block_x+4][img->block_y][2]=img->number;

	for (i=0;i<BLOCK_SIZE;i++)
	{                           /* reset vectors and pred. modes  */
		for(j=0;j<BLOCK_SIZE;j++)
		{
			img->mv[img->block_x+i+4][img->block_y+j][0] = 0;
			img->mv[img->block_x+i+4][img->block_y+j][1] = 0;
			img->ipredmode[img->block_x+i+1][img->block_y+j+1] = 0;
		}
	}
 
	currMB->ref_frame = img->frame_cycle;
	currMB->predframe_no = predframe_no = 0;//g.b.1;

	/* Set the reference frame information for motion vector prediction */
	if (img->imod == INTRA_MB_OLD || img->imod == INTRA_MB_NEW)
		for (j = 0;j < BLOCK_SIZE;j++)
			for (i = 0;i < BLOCK_SIZE;i++)
				refFrArr[img->block_y+j][img->block_x+i] = -1;
    else
        for (j = 0;j < BLOCK_SIZE;j++)
			for (i = 0;i < BLOCK_SIZE;i++)
				refFrArr[img->block_y+j][img->block_x+i] = predframe_no;

  
}
/************************************************************************
*
*  Name :       read_one_macroblock()
*
*  Description: Get the syntax elements from the NAL
*
************************************************************************/
int read_one_macroblock(struct img_par *img,struct inp_par *inp)
{
	int i, i1, j1;
	
	SyntaxElement currSE;
	Macroblock *currMB = &img->mb_data[img->current_mb_nr];
	
	Slice *currSlice = img->currentSlice;
	DataPartition *dP;
	int *partMap = assignSE2partition[inp->partition_mode];
	
	int dbl_ipred_word;
	
	/*  read MB mode ******************************************************************/
	if (inp->symbol_mode == UVLC)
		currSE.mapping = linfo;
	else
		currSE.reading = readMB_typeInfoFromBuffer_CABAC;
	currSE.type = SE_MBTYPE;
	dP = &(currSlice->partArr[partMap[currSE.type]]);
	
#if TRACE
	strcpy(currSE.tracestring, "MB Type");
#endif
	
	dP->readSyntaxElement(&currSE,img,inp,dP);
	img->mb_mode = currMB->mb_type = currSE.value1;   
	
	if ((img->type==INTER_IMG_1) || (img->type==INTER_IMG_MULT))    /* inter frame */       
		interpret_mb_mode_P(img);
	else if (img->type==INTRA_IMG)                                  /* intra frame */
		interpret_mb_mode_I(img);
	else if ((img->type==B_IMG_1) || (img->type==B_IMG_MULT))       /* B frame */
		interpret_mb_mode_B(img);
	
	if ((img->type==B_IMG_1) || (img->type==B_IMG_MULT)) 
		init_macroblock_Bframe(img);
	else
		init_macroblock(img);
	
	if (img->imod==INTRA_MB_INTER && img->mb_mode==COPY_MB) /*keep last macroblock*/
	{
		return DECODE_COPY_MB;
	}
	
	/* intra prediction modes for a macroblock 4x4 ***********************************************/
	if (img->imod==INTRA_MB_OLD)              
	{
		if (inp->symbol_mode == UVLC)
		    currSE.mapping = linfo;
		else
		    currSE.reading = readIntraPredModeFromBuffer_CABAC; 
		
		currSE.type = SE_INTRAPREDMODE;
		dP = &(currSlice->partArr[partMap[currSE.type]]);
    
		for(i=0;i<MB_BLOCK_SIZE/2;i++)
		{
#if TRACE
			sprintf(currSE.tracestring, "Intra mode ");
#endif
			dP->readSyntaxElement(&currSE,img,inp,dP);
			
			i1=img->block_x + 2*(i&0x01);
			j1=img->block_y + i/2;
			
			if (inp->symbol_mode == UVLC)
			{
				dbl_ipred_word = currSE.value1;
				/* find intra prediction mode for two blocks */
				img->ipredmode[i1+1][j1+1] = PRED_IPRED[img->ipredmode[i1+1][j1]+1][img->ipredmode[i1][j1+1]+1][IPRED_ORDER[dbl_ipred_word][0]];
				img->ipredmode[i1+2][j1+1] = PRED_IPRED[img->ipredmode[i1+2][j1]+1][img->ipredmode[i1+1][j1+1]+1][IPRED_ORDER[dbl_ipred_word][1]];
			}
			else
			{
				img->ipredmode[i1+1][j1+1] = PRED_IPRED[img->ipredmode[i1+1][j1]+1][img->ipredmode[i1][j1+1]+1][currSE.value1];
				img->ipredmode[i1+2][j1+1] = PRED_IPRED[img->ipredmode[i1+2][j1]+1][img->ipredmode[i1+1][j1+1]+1][currSE.value2];
			}
		}
	}
	
	/* read inter frame vector data *********************************************************/
	if ((img->type==B_IMG_1) || (img->type==B_IMG_MULT)) 
		readMotionInfoFromNAL_Bframe(img,inp);
	else if(img->imod==INTRA_MB_INTER)              
		readMotionInfoFromNAL_Pframe(img,inp);
	
	/* read CBP and Coeffs  ****************************************************************/

⌨️ 快捷键说明

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