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

📄 block.c

📁 davinci avs.......................................................
💻 C
📖 第 1 页 / 共 3 页
字号:
/*!
 *************************************************************************************
 * \file 
 *      block.c
 * \brief
 *      block based action such as transform, quantization, re-constructions
 *
 * \date: updating <April 1st, 2005>
 *     
 *************************************************************************************
 */
#include <assert.h>
#include <stdlib.h>
#include "block.h"
#include "global.h"
#include "defines.h"
//#include "compensation.h"


  //!< Global Tables for Inverse Quantization
  const unsigned short IQ_TAB[64] = 
  {
		32768,36061,38968,42495,46341,50535,55437,60424,
		32932,35734,38968,42495,46177,50535,55109,59933,
		65535,35734,38968,42577,46341,50617,55027,60097,
		32809,35734,38968,42454,46382,50576,55109,60056,
		65535,35734,38968,42495,46320,50515,55109,60076,
		65535,35744,38968,42495,46341,50535,55099,60087,
		65535,35734,38973,42500,46341,50535,55109,60097,
		32771,35734,38965,42497,46341,50535,55109,60099
  };

  const short IQ_SHIFT[64] = 
  {
	  14,14,14,14,14,14,14,14,
		13,13,13,13,13,13,13,13,
		13,12,12,12,12,12,12,12,
		11,11,11,11,11,11,11,11,
		11,10,10,10,10,10,10,10,
		10,9,9,9,9,9,9,9,
		9,8,8,8,8,8,8,8,
		7,7,7,7,7,7,7,7
	};

  const unsigned char QP_SCALE_CR[64]=
  {
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
   10,11,12,13,14,15,16,17,18,19,
   20,21,22,23,24,25,26,27,28,29,
   30,31,32,33,34,35,36,37,38,39,
   40,41,42,42,43,43,44,44,45,45,
   46,46,47,47,48,48,48,49,49,49,
   50,50,50,51
  };


  /*! 
 *************************************************************************************
 * \brief : Dequantization of 4x4 block.
 *************************************************************************************
 */
void Dequant_B4(int qp, int b8, int b4)    
{
  int i, j;
  int val;
  int shift, QPI;

  assert((b8 >= 0) && (b8 <= 5));

  if(b8 >= 4)
    qp =  QP_SCALE_CR[qp];

  for (j=0; j<4; j++)
	  for (i=0; i<4; i++)
	  {
      CheckQCoeffValid(pgImage->current_mb_nr,b8,b4,pgcurrMB->pred_residual[b8][b4][j][i]); /* check quantized coefficient */

		  val  = pgcurrMB->pred_residual[b8][b4][j][i];   //VLD's residual before IQ
		  shift = IQ_SHIFT[qp];
		  QPI   = IQ_TAB[qp]; 
		  
		  pgcurrMB->pred_residual[b8][b4][j][i] = (val*QPI+(1<<(shift-1)))>>shift; //IQ coefficients after Dequantization

      CheckDCTCoeffValid(pgImage->current_mb_nr,b8,b4,pgcurrMB->pred_residual[b8][b4][j][i]);  /* Inverse Quantization DCT coefficient Check */      
		  
#if TRACE
		  if(b8<4)
			  MB_IQ_Y[((b8>>1)<<3)+((b4>>1)<<2)+j][((b8 &1)<<3)+((b4 &1)<<2)+i] = pgcurrMB->pred_residual[b8][b4][j][i];
		  else
			  MB_IQ_UV[b8-4][((b4>>1)<<2)+j][((b4 &1)<<2)+i] = pgcurrMB->pred_residual[b8][b4][j][i];   
#endif
	  }

}

 /*! 
 *************************************************************************************
 * \brief : Inverse transform 4x4 block.
 *************************************************************************************
 */
void Inv_Transform_B4(int b8,int b4)    
{
  int i, j, i1, j1;
	int tmp[4];
	int tmp_blk[BLOCK_SIZE][BLOCK_SIZE];
	
	//using intermediate array tmp_blk[4][4] as transform unit.
	//4x4 image data swapping
	for(j=0; j<4; j++)
		for(i=0; i<4; i++)
			tmp_blk[j][i] = pgcurrMB->pred_residual[b8][b4][j][i];
		
	//horizontal transform
	for (j=0; j<4; j++)
	{
		
		tmp[0] = (tmp_blk[j][0]+tmp_blk[j][2])<<1;              
		tmp[1] = (tmp_blk[j][0]-tmp_blk[j][2])<<1;                                       
		tmp[3] = (tmp_blk[j][1]*3)+tmp_blk[j][3];       
		tmp[2] = tmp_blk[j][1]-(tmp_blk[j][3]*3);      
		
		for (i=0; i<2; i++)
		{
			i1=3-i;
			tmp_blk[j][i] = tmp[i]+tmp[i1];
			tmp_blk[j][i1]= tmp[i]-tmp[i1];
		}
	}

	//vertical transform
	for (i=0; i<4; i++)
	{
		tmp[0] =(tmp_blk[0][i]+tmp_blk[2][i])<<1;              
		tmp[1] =(tmp_blk[0][i]-tmp_blk[2][i])<<1;              
		
		tmp[3]= (tmp_blk[1][i]*3)+tmp_blk[3][i];       
		tmp[2]= tmp_blk[1][i]- (tmp_blk[3][i]*3);       
		
		for (j=0; j<2; j++)
		{
			j1=3-j;
      tmp_blk[j][i] = tmp[j]+tmp[j1];
			tmp_blk[j1][i]= tmp[j]-tmp[j1];   //
      
			//tmp_blk[j][i] =(tmp[j]+tmp[j1]+16)>>5;
			//tmp_blk[j1][i]=(tmp[j]-tmp[j1]+16)>>5;   //
		}
	}
  
  //added by zhan ma
  for(j=0; j<4; j++)   
	  for(i=0; i<4; i++)
    {
     
       CheckHCoeffValid(pgImage->current_mb_nr,b8,b4,tmp_blk[j][i]);  /* IDCT coefficient check */

#if TRACE 
		if(b8<4)
			MB_H_coeff_Y[((b8>>1)<<3)+((b4>>1)<<2)+j][((b8 &1)<<3)+((b4 &1)<<2)+i] = tmp_blk[j][i];
		else
			MB_H_coeff_UV[b8-4][((b4>>1)<<2)+j][((b4 &1)<<2)+i] = tmp_blk[j][i];
#endif

     tmp_blk[j][i] = (tmp_blk[j][i] +16)>>5;
    }
		
	//swapping
	for(j=0; j<4; j++)
	  for(i=0; i<4; i++)
		{
      pgcurrMB->pred_residual[b8][b4][j][i] = tmp_blk[j][i];
	
		//Just as a intermediate variable for decoding trace
#if TRACE 
		if(b8<4)
			MB_IDCT_Y[((b8>>1)<<3)+((b4>>1)<<2)+j][((b8 &1)<<3)+((b4 &1)<<2)+i] = pgcurrMB->pred_residual[b8][b4][j][i];
		else
			MB_IDCT_UV[b8-4][((b4>>1)<<2)+j][((b4 &1)<<2)+i] = pgcurrMB->pred_residual[b8][b4][j][i];
#endif
		}

}

 /*! 
 *************************************************************************************
 * \brief : Reconstruction of a 4x4 block.
 *************************************************************************************
 */
void Recon_B4(int b8, int b4)    
{
	int i,j;
	short temp;
	int blk_off_x, blk_off_y;
	int Mb_off_x, Mb_off_y;
	
	blk_off_x = ((b4 &1)<<2);
	blk_off_y = ((b4>>1)<<2);
	Mb_off_x  = ((b8 &1)<<3) + blk_off_x;
	Mb_off_y  = ((b8>>1)<<3) + blk_off_y;
	
 
	for(j=0; j<4; j++)
	{
		for(i=0; i<4; i++)
		{
			if(b8 < 4)   //luma
			{
				
				temp = (pgcurrMB->pred_sample[b8][b4][i][j] + pgcurrMB->pred_residual[b8][b4][j][i]);
				pgImage->m7[Mb_off_x+i][Mb_off_y+j] =  pgcurrMB->pred_residual[b8][b4][j][i] = clamp(temp,0,255);
				imgY[pgImage->pix_y+Mb_off_y+j][pgImage->pix_x+Mb_off_x+i] = pgcurrMB->pred_residual[b8][b4][j][i];
#if TRACE			
				MB_Y[Mb_off_y+j][Mb_off_x+i] = pgcurrMB->pred_residual[b8][b4][j][i];
#endif
			}
			else        //chroma
			{
				temp = (pgcurrMB->pred_sample[b8][b4][i][j] + pgcurrMB->pred_residual[b8][b4][j][i] );
				pgcurrMB->pred_residual[b8][b4][j][i] = clamp(temp,0,255);
				imgUV[b8-4][pgImage->pix_c_y+blk_off_y+j][pgImage->pix_c_x+blk_off_x+i] = pgcurrMB->pred_residual[b8][b4][j][i];
#if TRACE       
				MB_UV[b8-4][blk_off_y+j][blk_off_x+i] = pgcurrMB->pred_residual[b8][b4][j][i];
#endif
				
			}
		}
	}
}

/*! 
 *************************************************************************************
 * \brief : 
 *     Get predition residual. VLD
 *************************************************************************************
 */
void Get_residual(int b8,int b4)  
{
	int  xx, yy;
	int  mb_y  = (b8 / 2) << 3;
	int  mb_x  = (b8 % 2) << 3;

  assert(b8<=5&&b8>=0);
	
	if (b8<=3)
	{
		for (yy=0; yy<BLOCK_SIZE; yy++)
			for (xx=0; xx<BLOCK_SIZE; xx++)
			    pgcurrMB->pred_residual[b8][b4][yy][xx] = pgImage->m7[mb_x+((b4 &1)<<2)+xx][mb_y+((b4>>1)<<2)+yy]; 
	}
	else
	{	  
		for (yy=0; yy<BLOCK_SIZE; yy++)
			for (xx=0; xx<BLOCK_SIZE; xx++)
			    pgcurrMB->pred_residual[b8][b4][yy][xx] = pgImage->m8[b8-4][((b4 &1)<<2)+xx][((b4>>1)<<2)+yy]; 
	}
}


// Notation for comments regarding prediction and predictors.
// The pels of the 4x4 block are labelled a..p. The predictor pels above
// are labelled A..H, from the left I..P, and from above left X, as follows:
//
//  X A B C D E F G H
//  I a b c d
//  J e f g h
//  K i j k l
//  L m n o p
//  M
//  N
//  O
//  P
//

// Predictor array index definitions
#define P_X (PredPel[0])
#define P_A (PredPel[1])
#define P_B (PredPel[2])
#define P_C (PredPel[3])
#define P_D (PredPel[4])
#define P_E (PredPel[5])
#define P_F (PredPel[6])
#define P_G (PredPel[7])
#define P_H (PredPel[8])
#define P_I (PredPel[9])
#define P_J (PredPel[10])
#define P_K (PredPel[11])
#define P_L (PredPel[12])
#define P_M (PredPel[13])
#define P_N (PredPel[14])
#define P_O (PredPel[15])
#define P_P (PredPel[16])

/*!
 ***********************************************************************
 * \brief
 *    makes and returns 4x4 blocks with all 5 intra prediction modes
 *
 * \return
 *    DECODING_OK   decoding of intraprediction mode was sucessfull            
 *    SEARCH_SYNC   search next sync element as errors while decoding occured
 * \notes checke by Zhan MA June 20th 2005
 ***********************************************************************
 */
int intrapred(int b8, 
              int b4, 
              int ioff,                         //!< pixel offset X within MB
	            int joff,				//!< pixel offset Y within MB
	            int img_block_x,			//!< location of block X, multiples of 4
	            int img_block_y)			//!< location of block Y, multiples of 4
{
  int i,j;
  int s0;
  int img_y,img_x;
  int PredPel[17];  // array of predictor pels
  
  int block_available_up;
  int block_available_up_right;
  int block_available_left;
  int block_available_left_down;
  int block_available_up_left;
  //one 4x4 block one mode
  byte predmode = pgImage->ipredmode[img_block_x+1][img_block_y+1];   //accordinate based on this
  
  img_x=img_block_x*4;

⌨️ 快捷键说明

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