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

📄 deblocking.cpp

📁 h264编解码.用C++实现了图像的编解码功能。
💻 CPP
字号:
#include "global.h"
#include <math.h>

#define  IClip( Min, Max, Val) (((Val)<(Min))? (Min):(((Val)>(Max))? (Max):(Val)))

byte ALPHA_TABLE[52]  = {0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,4,4,5,6,  7,8,9,10,12,13,15,17,  20,22,25,28,32,36,40,45,  50,56,63,71,80,90,101,113,  127,144,162,182,203,226,255,255} ;
byte  BETA_TABLE[52]  = {0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,2,2,2,3,  3,3,3, 4, 4, 4, 6, 6,   7, 7, 8, 8, 9, 9,10,10,  11,11,12,12,13,13, 14, 14,   15, 15, 16, 16, 17, 17, 18, 18} ;
byte CLIP_TAB[52][5]  =
{
  { 0, 0, 0, 0, 0},{ 0, 0, 0, 0, 0},{ 0, 0, 0, 0, 0},{ 0, 0, 0, 0, 0},{ 0, 0, 0, 0, 0},{ 0, 0, 0, 0, 0},{ 0, 0, 0, 0, 0},{ 0, 0, 0, 0, 0},
  { 0, 0, 0, 0, 0},{ 0, 0, 0, 0, 0},{ 0, 0, 0, 0, 0},{ 0, 0, 0, 0, 0},{ 0, 0, 0, 0, 0},{ 0, 0, 0, 0, 0},{ 0, 0, 0, 0, 0},{ 0, 0, 0, 0, 0},
  { 0, 0, 0, 0, 0},{ 0, 0, 0, 1, 1},{ 0, 0, 0, 1, 1},{ 0, 0, 0, 1, 1},{ 0, 0, 0, 1, 1},{ 0, 0, 1, 1, 1},{ 0, 0, 1, 1, 1},{ 0, 1, 1, 1, 1},
  { 0, 1, 1, 1, 1},{ 0, 1, 1, 1, 1},{ 0, 1, 1, 1, 1},{ 0, 1, 1, 2, 2},{ 0, 1, 1, 2, 2},{ 0, 1, 1, 2, 2},{ 0, 1, 1, 2, 2},{ 0, 1, 2, 3, 3},
  { 0, 1, 2, 3, 3},{ 0, 2, 2, 3, 3},{ 0, 2, 2, 4, 4},{ 0, 2, 3, 4, 4},{ 0, 2, 3, 4, 4},{ 0, 3, 3, 5, 5},{ 0, 3, 4, 6, 6},{ 0, 3, 4, 6, 6},
  { 0, 4, 5, 7, 7},{ 0, 4, 5, 8, 8},{ 0, 4, 6, 9, 9},{ 0, 5, 7,10,10},{ 0, 6, 8,11,11},{ 0, 6, 8,13,13},{ 0, 7,10,14,14},{ 0, 8,11,16,16},
  { 0, 9,12,18,18},{ 0,10,13,20,20},{ 0,11,15,23,23},{ 0,13,17,25,25}
} ;
int  ININT_STRENGTH[4] = {0x04040404, 0x03030303, 0x03030303, 0x03030303} ; 
byte BLK_NUM[2][4][4]  = {{{0,4,8,12},{1,5,9,13},{2,6,10,14},{3,7,11,15}},{{0,1,2,3},{4,5,6,7},{8,9,10,11},{12,13,14,15}}} ;
byte BLK_4_TO_8[16]    = {0,0,1,1,0,0,1,1,2,2,3,3,2,2,3,3} ;

void DeblockMb_I(struct img_par *img, int mb_y, int mb_x);
void DeblockMb(struct img_par *img, int mb_y, int mb_x);
void GetStrength(byte Strength[4],struct img_par *img,int dir,int edge,int block_y,int block_x);
void EdgeLoop_Y(byte* SrcPtr,byte Strength[4],int QP,int dir,int width);
void EdgeLoop_UV(byte* SrcPtr,byte Strength[4],int QP,int dir,int width);
extern const byte QP_SCALE_CR[52];
extern int tmp_mv[72][92][2];
extern char intra_mb_pos[72][92];
extern int cbp_blk[72][88];
extern byte *imgY;
extern byte *imgUV[2];

void DeblockFrame(struct img_par *img)
{
  int mb_x, mb_y ;
  int width=img->width>>4;
  int height=img->height>>4;

  if(img->type != INTRA_IMG)
  {
    for(mb_y=0;mb_y<height;mb_y++)
      for(mb_x=0;mb_x<width;mb_x++)
      {
      	DeblockMb( img, mb_y, mb_x ) ;
      }
    return;
  }
  else
  {
    for(mb_y=0;mb_y<height;mb_y++)
      for(mb_x=0;mb_x<width;mb_x++)
      {
      	DeblockMb_I( img, mb_y, mb_x ) ;
      }
  }
}

void DeblockMb_I(struct img_par *img, int mb_y, int mb_x)
{
  int           QP,QPC;
  byte          Strength[4], *SrcY, *SrcU, *SrcV ;
  int width=img->width+IMG_PAD_SIZE;
  int width_cr=img->width_cr+IMG_PAD_SIZE;

  int           filterLeftMbEdgeFlag  = (mb_x != 0);
  int           filterTopMbEdgeFlag   = (mb_y != 0);
  
  SrcY = imgY + (mb_y<<4)*width + (mb_x<<4) ;                                                      // pointers to source
  SrcU = imgUV[0] + (mb_y<<3)*width_cr + (mb_x<<3) ;
  SrcV = imgUV[1] + (mb_y<<3)*width_cr + (mb_x<<3) ;

  QP  = img->qp;                   // Average QP of the two blocks
  QPC  = QP_SCALE_CR[QP];

  //dir ==0, vertical
  //block 0
  if(filterLeftMbEdgeFlag )
  {
        *((int*)Strength) = ININT_STRENGTH[0] ;
        EdgeLoop_Y( SrcY, Strength, QP, 0, width) ; 
        EdgeLoop_UV( SrcU, Strength, QPC,  0, width_cr) ; 
        EdgeLoop_UV( SrcV, Strength, QPC,  0, width_cr) ; 
  }
  //block 1
  *((int*)Strength) = ININT_STRENGTH[1] ;
  EdgeLoop_Y( SrcY + 4, Strength, QP, 0, width) ; 
  //block 2
  EdgeLoop_Y( SrcY + 8, Strength, QP, 0, width) ; 
  EdgeLoop_UV( SrcU + 4, Strength, QPC,  0, width_cr) ; 
  EdgeLoop_UV( SrcV + 4, Strength, QPC,  0, width_cr) ; 
  //block 3
  EdgeLoop_Y( SrcY + 12, Strength, QP, 0, width) ; 

  //dir == 1; horizontal
  //block 0
  if(filterTopMbEdgeFlag)
  {
  	*((int*)Strength) = ININT_STRENGTH[0] ;
    if( *((int*)Strength) )                      // only if one of the 4 Strength bytes is != 0
    {
       EdgeLoop_Y( SrcY, Strength, QP, 1, width) ; 
       EdgeLoop_UV( SrcU, Strength, QPC,  1, width_cr) ; 
       EdgeLoop_UV( SrcV, Strength, QPC,  1, width_cr) ; 
    }
  }
  //block 1
  *((int*)Strength) = ININT_STRENGTH[1] ;
  EdgeLoop_Y( SrcY + 4*width, Strength, QP, 1, width) ; 
  //block 2
  EdgeLoop_Y( SrcY + 8*width, Strength, QP, 1, width) ; 
  EdgeLoop_UV( SrcU + 4*width_cr, Strength, QPC,  1, width_cr) ; 
  EdgeLoop_UV( SrcV + 4*width_cr, Strength, QPC,  1, width_cr) ; 
  //block 3
  EdgeLoop_Y( SrcY + 12*width, Strength, QP, 1, width) ; 
}

void DeblockMb(struct img_par *img, int mb_y, int mb_x)
{
  int           QP,QPC;
  byte          Strength[4], *SrcY, *SrcU, *SrcV ;
  int width=img->width+IMG_PAD_SIZE;
  int width_cr=img->width_cr+IMG_PAD_SIZE;

  int           filterLeftMbEdgeFlag  = (mb_x != 0);
  int           filterTopMbEdgeFlag   = (mb_y != 0);
  
  SrcY = imgY + (mb_y<<4)*width + (mb_x<<4) ;                                                      // pointers to source
  SrcU = imgUV[0] + (mb_y<<3)*width_cr + (mb_x<<3) ;
  SrcV = imgUV[1] + (mb_y<<3)*width_cr + (mb_x<<3) ;

  QP  = img->qp;                   // Average QP of the two blocks
  QPC  = QP_SCALE_CR[QP];

  //dir ==0, vertical
  //block 0
  if(filterLeftMbEdgeFlag )
  {
        GetStrength(Strength,img,0,0,mb_y<<2,mb_x<<2); // Strength for 4 blks in 1 stripe
        if( *((int*)Strength) )                      // only if one of the 4 Strength bytes is != 0
        {
          EdgeLoop_Y( SrcY, Strength, QP, 0, width) ; 
          EdgeLoop_UV( SrcU, Strength, QPC,  0, width_cr) ; 
          EdgeLoop_UV( SrcV, Strength, QPC,  0, width_cr) ; 
        }
  }
  //block 1
  GetStrength(Strength,img,0,1,mb_y<<2,mb_x<<2); // Strength for 4 blks in 1 stripe
  if( *((int*)Strength) )                      // only if one of the 4 Strength bytes is != 0
  {
      EdgeLoop_Y( SrcY + 4, Strength, QP, 0, width) ; 
  }
  //block 2
  GetStrength(Strength,img,0,2,mb_y<<2,mb_x<<2); // Strength for 4 blks in 1 stripe
  if( *((int*)Strength) )                      // only if one of the 4 Strength bytes is != 0
  {
      EdgeLoop_Y( SrcY + 8, Strength, QP, 0, width) ; 
      EdgeLoop_UV( SrcU + 4, Strength, QPC,  0, width_cr) ; 
      EdgeLoop_UV( SrcV + 4, Strength, QPC,  0, width_cr) ; 
  }
  //block 3
  GetStrength(Strength,img,0,3,mb_y<<2,mb_x<<2); // Strength for 4 blks in 1 stripe
  if( *((int*)Strength) )                      // only if one of the 4 Strength bytes is != 0
  {
      EdgeLoop_Y( SrcY + 12, Strength, QP, 0, width) ; 
  }

	
  //dir == 1; horizontal
  //block 0
  if(filterTopMbEdgeFlag)
  {
  	GetStrength(Strength,img,1,0,mb_y<<2,mb_x<<2); // Strength for 4 blks in 1 stripe
    if( *((int*)Strength) )                      // only if one of the 4 Strength bytes is != 0
    {
       EdgeLoop_Y( SrcY, Strength, QP, 1, width) ; 
       EdgeLoop_UV( SrcU, Strength, QPC,  1, width_cr) ; 
       EdgeLoop_UV( SrcV, Strength, QPC,  1, width_cr) ; 
    }
  }  
  //block 1
  GetStrength(Strength,img,1,1,mb_y<<2,mb_x<<2); // Strength for 4 blks in 1 stripe
  if( *((int*)Strength) )                      // only if one of the 4 Strength bytes is != 0
  {
      EdgeLoop_Y( SrcY + width*4, Strength, QP, 1, width) ; 
  }
  //block 2
  GetStrength(Strength,img,1,2,mb_y<<2,mb_x<<2); // Strength for 4 blks in 1 stripe
  if( *((int*)Strength) )                      // only if one of the 4 Strength bytes is != 0
  {
      EdgeLoop_Y( SrcY + width*8, Strength, QP, 1, width) ; 
      EdgeLoop_UV( SrcU + width_cr*4, Strength, QPC,  1, width_cr) ; 
      EdgeLoop_UV( SrcV + width_cr*4, Strength, QPC,  1, width_cr) ; 
  }
  //block 3
  GetStrength(Strength,img,1,3,mb_y<<2,mb_x<<2); // Strength for 4 blks in 1 stripe
  if( *((int*)Strength) )                      // only if one of the 4 Strength bytes is != 0
  {
      EdgeLoop_Y( SrcY + width*12, Strength, QP, 1, width) ; 
  }
}

void GetStrength(byte Strength[4],struct img_par *img,int dir,int edge,int block_y,int block_x)
{
  int    blkP, blkQ, idx ;
  int    blk_x, blk_x2, blk_y, blk_y2 ;

  *((int*)Strength) = ININT_STRENGTH[edge] ;                     // Start with Strength=3. or Strength=4 for Mb-edge


  for( idx=0 ; idx<4 ; idx++ )
  {                                                                                       // if not intra or SP-frame
    blkQ = BLK_NUM[dir][ edge       ][idx] ;                 // if one of the 4x4 blocks has coefs.    set Strength=2
    blkP = BLK_NUM[dir][(edge-1) & 3][idx] ;
    blk_y  = block_y + (blkQ >> 2) ;   blk_y2 = blk_y -  dir ;
    blk_x  = block_x + (blkQ  & 3) ;   blk_x2 = blk_x - !dir ; 
    
    if((!intra_mb_pos[blk_y][blk_x+4]) && (!intra_mb_pos[blk_y2][blk_x2+4]))
    {
      if( (cbp_blk[blk_y][blk_x] != 0) || (cbp_blk[blk_y2][blk_x2] != 0) )
        Strength[idx] = 2 ;
      else
      {
         Strength[idx] =  //(ref_p0 != ref_q0 ) |
            (abs( tmp_mv[blk_y][blk_x+4][0] - tmp_mv[blk_y2][blk_x2+4][0]) >= 4 ) |
            (abs( tmp_mv[blk_y][blk_x+4][1] - tmp_mv[blk_y2][blk_x2+4][1]) >= 4 );
        
      }
    }
  }
}

void EdgeLoop_Y(byte* SrcPtr,byte Strength[4],int QP,int dir,int width)
{
  int      pel, ap = 0, aq = 0, PtrInc, Strng ;
  int      inc, inc2, inc3, inc4 ;
  int      C0, c0, Delta, dif, AbsDelta,AbsDelta1,AbsDelta2 ;
  int      L2 = 0, L1, L0, R0, R1, R2 = 0, RL0 ;
  int      Alpha = 0, Beta = 0 ;
  byte*    ClipTab = NULL;   
  int      small_gap;


  PtrInc  = dir?  1 : width ;
  inc     = dir?  width : 1 ;                     // vertical filtering increment to next pixel is 1 else width
  inc2    = inc<<1 ;    
  inc3    = inc + inc2 ;    
  inc4    = inc<<2 ;

  Alpha=ALPHA_TABLE[QP];
  Beta=BETA_TABLE[QP];  
  ClipTab=CLIP_TAB[QP];
  
  for( pel=0 ; pel<16 ; pel++ )
  {
    if( (Strng = Strength[pel >> 2]) )
    {
      L0  = SrcPtr [-inc ] ;
      R0  = SrcPtr [    0] ;
      AbsDelta  = abs( Delta = R0 - L0 )  ;

      if( AbsDelta < Alpha )
      {
        C0  = ClipTab[ Strng ] ;
        L1  = SrcPtr[-inc2] ;
        R1  = SrcPtr[ inc ] ;
        AbsDelta1=abs( R0 - R1);
        AbsDelta2=abs(L0 - L1);
        if( ((AbsDelta1 - Beta )  & (AbsDelta2 - Beta )) < 0  ) 
        {

          L2  = SrcPtr[-inc3] ;
          R2  = SrcPtr[ inc2] ;
          AbsDelta1=abs( R0 - R2);
          AbsDelta2=abs(L0 - L2);
          aq  = (AbsDelta1 - Beta ) < 0  ;
          ap  = (AbsDelta2 - Beta ) < 0  ;

          RL0             = L0 + R0 ;

          if(Strng == 4 )    // INTRA strong filtering
          {
            small_gap = (AbsDelta < ((Alpha >> 2) + 2));
            aq &= small_gap;
            ap &= small_gap;
            if(aq)
            {  
            	SrcPtr[   0 ] =( L1 + ((R1 + RL0) << 1) +  SrcPtr[ inc2] + 4) >> 3;
            	SrcPtr[ inc ] =( SrcPtr[ inc2] + R0 + R1 + L0 + 2) >> 2 ;
			    SrcPtr[ inc2] =(((SrcPtr[ inc3] + SrcPtr[ inc2]) <<1) + SrcPtr[ inc2] + R1 + RL0 + 4) >> 3;
            }
            else
            {
                SrcPtr[   0 ] =((R1 << 1) + R0 + L1 + 2) >> 2 ;
			}
         	
         	if(ap)
         	{
         	    SrcPtr[-inc ] = ( R1 + ((L1 + RL0) << 1) +  SrcPtr[-inc3] + 4) >> 3 ;
         	    SrcPtr[-inc2] = ( SrcPtr[-inc3] + L1 + L0 + R0 + 2) >> 2 ;
         	    SrcPtr[-inc3] = (((SrcPtr[-inc4] + SrcPtr[-inc3]) <<1) + SrcPtr[-inc3] + L1 + RL0 + 4) >> 3 ;
         	}
         	else
         	{
         		SrcPtr[-inc ] = ((L1 << 1) + L0 + R1 + 2) >> 2 ;
         	}
          }
          else                                                                                   // normal filtering
          {
            c0               = C0 + ap + aq ;
            dif              = IClip( -c0, c0, ( (Delta << 2) + (L1 - R1) + 4) >> 3 ) ;
            SrcPtr[  -inc ]  = IClip(0, 255, L0 + dif) ;
            SrcPtr[     0 ]  = IClip(0, 255, R0 - dif) ;

            SrcPtr[-inc2] +=ap? IClip( -C0,  C0, ( L2 + ((RL0 + 1) >> 1) - (L1<<1)) >> 1 ) :0;
            SrcPtr[  inc] +=aq? IClip( -C0,  C0, ( R2 + ((RL0 + 1) >> 1) - (R1<<1)) >> 1 ) :0;
          } ;
        } ; 
      } ;
      SrcPtr += PtrInc ;      // Increment to next set of pixel
    } 
    else
    {
      SrcPtr += PtrInc << 2 ;
      pel    += 3 ;
    }  ;
  }
}

void EdgeLoop_UV(byte* SrcPtr,byte Strength[4],int QP,int dir,int width)
{
  int      pel, ap = 0, aq = 0, PtrInc, Strng ;
  int      inc, inc2, inc3, inc4 ;
  int      C0, c0, Delta, dif, AbsDelta,AbsDelta1,AbsDelta2 ;
  int      L2 = 0, L1, L0, R0, R1, R2 = 0;
  int      Alpha = 0, Beta = 0 ;
  byte*    ClipTab = NULL;   

  PtrInc  = dir?      1 : width ;
  inc     = dir?  width : 1 ;                     // vertical filtering increment to next pixel is 1 else width
  inc2    = inc<<1 ;    
  inc3    = inc + inc2 ;    
  inc4    = inc<<2 ;

  Alpha=ALPHA_TABLE[QP];
  Beta=BETA_TABLE[QP];  
  ClipTab=CLIP_TAB[QP];
  
  for( pel=0 ; pel<8 ; pel++ )
  {
    if( (Strng = Strength[pel >> 1]) )
    {
      L0  = SrcPtr [-inc ] ;
      R0  = SrcPtr [    0] ;
      AbsDelta  = abs( Delta = R0 - L0 )  ;

      if( AbsDelta < Alpha )
      {
        C0  = ClipTab[ Strng ] ;
        L1  = SrcPtr[-inc2] ;
        R1  = SrcPtr[ inc ] ;
        AbsDelta1=abs( R0 - R1);
        AbsDelta2=abs(L0 - L1);
        if( ((AbsDelta1 - Beta )  & (AbsDelta2 - Beta )) < 0  ) 
        {
          if(Strng == 4 )    // INTRA strong filtering
          {
            SrcPtr[   0 ] = ((R1 << 1) + R0 + L1 + 2) >> 2; 
            SrcPtr[-inc ] = ((L1 << 1) + L0 + R1 + 2) >> 2;                                           
          }
          else                                                                                   // normal filtering
          {
            c0               = C0+1 ;
            dif              = IClip( -c0, c0, ( (Delta << 2) + (L1 - R1) + 4) >> 3 ) ;
            SrcPtr[  -inc ]  = IClip(0,255,L0 + dif);
            SrcPtr[     0 ]  = IClip(0,255,R0 - dif);
          } ;
        } ; 
      } ;
      SrcPtr += PtrInc ;      // Increment to next set of pixel
    } 
    else
    {
      SrcPtr += PtrInc << 1 ;
      pel    ++ ;
    }  ;
  }
}

⌨️ 快捷键说明

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