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

📄 block.c

📁 AVS视频编解码器 能实现视频图像的高效率压缩 能在VC上高速运行
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
***********************************************************************
* COPYRIGHT AND WARRANTY INFORMATION
*
* Copyright 2003, Advanced Audio Video Coding Standard, Part II
*
* DISCLAIMER OF WARRANTY
*
* These software programs are available to the users without any
* license fee or royalty on an "as is" basis. The AVS disclaims
* any and all warranties, whether express, implied, or statutory,
* including any implied warranties of merchantability or of fitness
* for a particular purpose. In no event shall the contributors or 
* the AVS be liable for any incidental, punitive, or consequential
* damages of any kind whatsoever arising from the use of this program.
*
* This disclaimer of warranty extends to the user of this program
* and user's customers, employees, agents, transferees, successors,
* and assigns.
*
* The AVS does not represent or warrant that the program furnished
* hereunder are free of infringement of any third-party patents.
* Commercial implementations of AVS, including shareware, may be
* subject to royalty fees to patent holders. Information regarding
* the AVS patent policy is available from the AVS Web site at
* http://www.avs.org.cn
*
* THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE AVS PATENT POLICY.
************************************************************************
*/

/*
*************************************************************************************
* File name: block.c
* Function: Description
*
*************************************************************************************
*/



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

#include "defines.h"
#include "global.h"
#include "elements.h"
#include "block.h"
#include "vlc.h"
#include "golomb_dec.h"

#define EP (edgepixels+20)

#ifdef WIN32
	#define int16 __int16
#else
	#define int16 int16_t
#endif

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
		
};

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

/*
*************************************************************************
* Function:Inverse transform 8x8 block.
* Input:
* Output:
* Return: 
* Attention:input curr_blk has to be [lines][pixels]
*************************************************************************
*/
void inv_transform_B8(int curr_blk1[B8_SIZE][B8_SIZE]  // block to be inverse transformed.
   )
 {
   short int xx, yy;
   short int tmp[8];
   short int t;
   short  int b[8];
   short int curr_blk[8][8];
   
   for(yy=0; yy<8; yy++)
	   for(xx=0; xx<8; xx++)
	   {
		   curr_blk[yy][xx] = (short int)curr_blk1[yy][xx];
	   }
	     
   for(yy=0; yy<8; yy++)
   {
     // Horizontal inverse transform
     // Reorder
     tmp[0]=curr_blk[yy][0];
     tmp[1]=curr_blk[yy][4];
     tmp[2]=curr_blk[yy][2];
     tmp[3]=curr_blk[yy][6];
     tmp[4]=curr_blk[yy][1];
     tmp[5]=curr_blk[yy][3];
     tmp[6]=curr_blk[yy][5];
     tmp[7]=curr_blk[yy][7];
     
     // Downleft Butterfly
/*Lou Change*/
	b[0] = ((tmp[4] - tmp[7])<<1) + tmp[4];
	b[1] = ((tmp[5] + tmp[6])<<1) + tmp[5];
	b[2] = ((tmp[5] - tmp[6])<<1) - tmp[6];
	b[3] = ((tmp[4] + tmp[7])<<1) + tmp[7];

	b[4] = ((b[0] + b[1] + b[3])<<1) + b[1];
	b[5] = ((b[0] - b[1] + b[2])<<1) + b[0];
	b[6] = ((-b[1] - b[2] + b[3])<<1) + b[3];
	b[7] = ((b[0] - b[2] - b[3])<<1) - b[2];
/*Lou End*/
     
     // Upleft Butterfly
     /*Lou Change*/
	 t=((tmp[2]*10)+(tmp[3]<<2));
	 tmp[3]=((tmp[2]<<2)-(tmp[3]*10));
       	 tmp[2]=t;
     
     	 t=(tmp[0]+tmp[1])<<3;
     	 tmp[1]=(tmp[0]-tmp[1])<<3;
     	 tmp[0]=t;
     /*Lou End*/
     
    	 b[0]=tmp[0]+tmp[2];
     	 b[1]=tmp[1]+tmp[3];
     	 b[2]=tmp[1]-tmp[3];
     	 b[3]=tmp[0]-tmp[2];	 
     
     // Last Butterfly
	 /*Lou Change*/
	 //curr_blk[yy][0]=(short)(((b[0]+b[4])+(1<<2))>>3);
	 //curr_blk[yy][1]=(short)(((b[1]+b[5])+(1<<2))>>3);
	 //curr_blk[yy][2]=(short)(((b[2]+b[6])+(1<<2))>>3);
	 //curr_blk[yy][3]=(short)(((b[3]+b[7])+(1<<2))>>3);
	 //curr_blk[yy][7]=(short)(((b[0]-b[4])+(1<<2))>>3);
	 //curr_blk[yy][6]=(short)(((b[1]-b[5])+(1<<2))>>3);
	 //curr_blk[yy][5]=(short)(((b[2]-b[6])+(1<<2))>>3);
	 //curr_blk[yy][4]=(short)(((b[3]-b[7])+(1<<2))>>3);
	 /*Lou End*/
	 /*WANGJP CHANGE 20061226*/
	/*  Commented by cjw, 20070327
	 curr_blk[yy][0]=((int16)((int16)(b[0]+b[4])+4))>>3;
	 curr_blk[yy][1]=((int16)((int16)(b[1]+b[5])+4))>>3;
	 curr_blk[yy][2]=((int16)((int16)(b[2]+b[6])+4))>>3;
	 curr_blk[yy][3]=((int16)((int16)(b[3]+b[7])+4))>>3;
	 curr_blk[yy][7]=((int16)((int16)(b[0]-b[4])+4))>>3;
	 curr_blk[yy][6]=((int16)((int16)(b[1]-b[5])+4))>>3;
	 curr_blk[yy][5]=((int16)((int16)(b[2]-b[6])+4))>>3;
	 curr_blk[yy][4]=((int16)((int16)(b[3]-b[7])+4))>>3;
	 */
	 /*WANGJP END*/

	//070305 dingdandan
	curr_blk[yy][0]=(short)((Clip3(-32768,32767,((b[0]+b[4])+(1<<2))))>>3);
	curr_blk[yy][1]=(short)((Clip3(-32768,32767,((b[1]+b[5])+(1<<2))))>>3);
	curr_blk[yy][2]=(short)((Clip3(-32768,32767,((b[2]+b[6])+(1<<2))))>>3);
	curr_blk[yy][3]=(short)((Clip3(-32768,32767,((b[3]+b[7])+(1<<2))))>>3);
	curr_blk[yy][7]=(short)((Clip3(-32768,32767,((b[0]-b[4])+(1<<2))))>>3);
	curr_blk[yy][6]=(short)((Clip3(-32768,32767,((b[1]-b[5])+(1<<2))))>>3);
	curr_blk[yy][5]=(short)((Clip3(-32768,32767,((b[2]-b[6])+(1<<2))))>>3);
	curr_blk[yy][4]=(short)((Clip3(-32768,32767,((b[3]-b[7])+(1<<2))))>>3);
	//070305 dingdandan
	 
   }
   // Vertical inverse transform
   for(xx=0; xx<8; xx++)
   {
     
     // Reorder
     tmp[0]=curr_blk[0][xx];
     tmp[1]=curr_blk[4][xx];
     tmp[2]=curr_blk[2][xx];
     tmp[3]=curr_blk[6][xx];
     tmp[4]=curr_blk[1][xx];
     tmp[5]=curr_blk[3][xx];
     tmp[6]=curr_blk[5][xx];
     tmp[7]=curr_blk[7][xx];
     
     // Downleft Butterfly
/*Lou Change*/
	b[0] = ((tmp[4] - tmp[7])<<1) + tmp[4];
	b[1] = ((tmp[5] + tmp[6])<<1) + tmp[5];
	b[2] = ((tmp[5] - tmp[6])<<1) - tmp[6];
	b[3] = ((tmp[4] + tmp[7])<<1) + tmp[7];

	b[4] = ((b[0] + b[1] + b[3])<<1) + b[1];
	b[5] = ((b[0] - b[1] + b[2])<<1) + b[0];
	b[6] = ((-b[1] - b[2] + b[3])<<1) + b[3];
	b[7] = ((b[0] - b[2] - b[3])<<1) - b[2];
/*Lou End*/
     
     // Upleft Butterfly
     /*Lou Change*/
	 t=((tmp[2]*10)+(tmp[3]<<2));
	 tmp[3]=((tmp[2]<<2)-(tmp[3]*10));
   	 tmp[2]=t;
     
      t=(tmp[0]+tmp[1])<<3;
     tmp[1]=(tmp[0]-tmp[1])<<3;
     tmp[0]=t;
     /*Lou End*/
     
     b[0]=tmp[0]+tmp[2];
     b[1]=tmp[1]+tmp[3];
     b[2]=tmp[1]-tmp[3];
     b[3]=tmp[0]-tmp[2];
     
     // Last Butterfly
	 //curr_blk[0][xx]=(b[0]+b[4]+64)>>7;/*(Clip3(-32768,32703,b[0]+b[4])+64)>>7;*/
	 //curr_blk[1][xx]=(b[1]+b[5]+64)>>7;/*(Clip3(-32768,32703,b[1]+b[5])+64)>>7;*/
	 //curr_blk[2][xx]=(b[2]+b[6]+64)>>7;/*(Clip3(-32768,32703,b[2]+b[6])+64)>>7;*/
	 //curr_blk[3][xx]=(b[3]+b[7]+64)>>7;/*(Clip3(-32768,32703,b[3]+b[7])+64)>>7;*/
	 //curr_blk[7][xx]=(b[0]-b[4]+64)>>7;/*(Clip3(-32768,32703,b[0]-b[4])+64)>>7;*/
	 //curr_blk[6][xx]=(b[1]-b[5]+64)>>7;/*(Clip3(-32768,32703,b[1]-b[5])+64)>>7;*/
	 //curr_blk[5][xx]=(b[2]-b[6]+64)>>7;/*(Clip3(-32768,32703,b[2]-b[6])+64)>>7;*/
	 //curr_blk[4][xx]=(b[3]-b[7]+64)>>7;/*(Clip3(-32768,32703,b[3]-b[7])+64)>>7;*/

	 /*WANGJP CHANGE 20061226*/
	 /*  Commented by cjw, 20070327
	 curr_blk[0][xx]=((int16)((int16)(b[0]+b[4])+64))>>7;
	 curr_blk[1][xx]=((int16)((int16)(b[1]+b[5])+64))>>7;
	 curr_blk[2][xx]=((int16)((int16)(b[2]+b[6])+64))>>7;
	 curr_blk[3][xx]=((int16)((int16)(b[3]+b[7])+64))>>7;
	 curr_blk[7][xx]=((int16)((int16)(b[0]-b[4])+64))>>7;
	 curr_blk[6][xx]=((int16)((int16)(b[1]-b[5])+64))>>7;
	 curr_blk[5][xx]=((int16)((int16)(b[2]-b[6])+64))>>7;
	 curr_blk[4][xx]=((int16)((int16)(b[3]-b[7])+64))>>7;
	 */
	 /*WANGJP END*/

	 //070305 dingdandan
	curr_blk[0][xx]=(Clip3(-32768,32767,(b[0]+b[4])+64))>>7;
	curr_blk[1][xx]=(Clip3(-32768,32767,(b[1]+b[5])+64))>>7;
	curr_blk[2][xx]=(Clip3(-32768,32767,(b[2]+b[6])+64))>>7;
	curr_blk[3][xx]=(Clip3(-32768,32767,(b[3]+b[7])+64))>>7;
	curr_blk[7][xx]=(Clip3(-32768,32767,(b[0]-b[4])+64))>>7;
	curr_blk[6][xx]=(Clip3(-32768,32767,(b[1]-b[5])+64))>>7;
	curr_blk[5][xx]=(Clip3(-32768,32767,(b[2]-b[6])+64))>>7;
	curr_blk[4][xx]=(Clip3(-32768,32767,(b[3]-b[7])+64))>>7;
	//070305 dingdandan

	

	 //range checking
	 if((b[0]+b[4])<=-32768 || (b[0]+b[4])>=(32768-65 ))
		 printf("error\n");
	 if((b[1]+b[5])<=-32768 || (b[1]+b[5])>=(32768-65 ))
		 printf("error\n");
	 if((b[2]+b[6])<=-32768 || (b[2]+b[6])>=(32768-65 ))
		 printf("error\n");
	 if((b[3]+b[7])<=-32768 || (b[3]+b[7])>=(32768-65 ))
		 printf("error\n");
	 if((b[0]-b[4])<=-32768 || (b[0]-b[4])>=(32768-65 ))
		 printf("error\n");
	 if((b[1]-b[5])<=-32768 || (b[1]-b[5])>=(32768-65 ))
		 printf("error\n");
	 if((b[2]-b[6])<=-32768 || (b[2]-b[6])>=(32768-65 ))
		 printf("error\n"); 
	 if((b[3]-b[7])<=-32768 || (b[3]-b[7])>=(32768-65 ))
		 printf("error\n");

   }
   
   for(yy=0; yy<8; yy++)
	   for(xx=0; xx<8; xx++)
	   {
		   curr_blk1[yy][xx] = curr_blk[yy][xx];
	   }
	   
}

void idct_dequant_B8(int block8x8,
										 int qp,                         // Quantization parameter
										 int curr_blk[B8_SIZE][B8_SIZE],
										 struct img_par *img
										 )
{
  int  xx,yy;
  int  b8_y       = (block8x8 / 2) << 3;
  int  b8_x       = (block8x8 % 2) << 3;
  int  curr_val;

  // inverse transform
  inv_transform_B8(curr_blk);
  
  // normalize
  for(yy=0;yy<8;yy++)
    for(xx=0;xx<8;xx++)
    {
     if(block8x8 <= 3)
        curr_val = img->mpr[b8_x+xx][b8_y+yy] + curr_blk[yy][xx];
      else
	curr_val = img->mpr[xx][yy] + curr_blk[yy][xx];

      img->m7[xx][yy] = curr_blk[yy][xx] = clamp(curr_val,0,255);

      if(block8x8 <= 3)
        imgY[img->pix_y+b8_y+yy][img->pix_x+b8_x+xx] = curr_blk[yy][xx];
      else
        imgUV[block8x8-4][img->pix_c_y+yy][img->pix_c_x+xx] = curr_blk[yy][xx];

    }

}

/*
*************************************************************************
* Function:
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/
void reconstruct8x8(int curr_blk[8][8],int block8x8)
{
	int yy,xx,block_y, block_x;
	int incr_y=1,off_y=0; 
	Macroblock *currMB   = &mb_data[img->current_mb_nr];//GB current_mb_nr];

	if(block8x8<4)
	{
		block_y = (block8x8/2)*8;
		block_x = (block8x8%2)*8;
		
		for(yy=0;yy<8;yy++)
		{
			for(xx=0;xx<8;xx++)
			{				
				imgY[img->pix_y+block_y+incr_y*yy+off_y][img->pix_x+block_x+xx] = curr_blk[yy][xx];
			}
		}
	}
	else
	{	
		for(yy=0;yy<8;yy++)
		{
			for(xx=0;xx<8;xx++)			
			{
				imgUV[block8x8-4][img->pix_c_y+yy][img->pix_c_x+xx] = curr_blk[yy][xx];
			}
		}
	}
}


/*
*************************************************************************
* Function:Read coefficients of one 8x8 block
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/


void readLumaCoeff_B8(int block8x8, struct inp_par *inp, struct img_par *img)
{
  int i;
  int mb_nr          = img->current_mb_nr;
  Macroblock *currMB = &mb_data[mb_nr];
  const int cbp      = currMB->cbp;
  SyntaxElement currSE;
  int intra;
  int inumblk;                    /* number of blocks per CBP*/
  int inumcoeff;                  /* number of coeffs per block */
  int icoef;                      /* current coefficient */

⌨️ 快捷键说明

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