?? h.264
字號:
/*!
*************************************************************************************
* \file loopFilter.c
*
* \brief
* Filter to reduce blocking artifacts on a macroblock level.
* The filter strengh is QP dependent.
*
* \author
* Contributors:
* - Peter List Peter.List@t-systems.de: Original code (13-Aug-2001)
* - Jani Lainema Jani.Lainema@nokia.com: Some bug fixing, removal of recusiveness (16-Aug-2001)
* - Peter List Peter.List@t-systems.de: inplace filtering and various simplifications (10-Jan-2002)
* - Anthony Joch anthony@ubvideo.com: Simplified switching between filters and
* non-recursive default filter. (08-Jul-2002)
* - Cristina Gomila cristina.gomila@thomson.net: Simplification of the chroma deblocking
* from JVT-E089 (21-Nov-2002)
*************************************************************************************
*/
#include <stdlib.h>
#include <string.h>
#include "global.h"
#include "image.h"
#include "mb_access.h"
extern const byte QP_SCALE_CR[52] ;
/*********************************************************************************************************/
#define IClip( Min, Max, Val) (((Val)<(Min))? (Min):(((Val)>(Max))? (Max):(Val)))
// NOTE: to change the tables below for instance when the QP doubling is changed from 6 to 8 values
// send an e-mail to Peter.List@t-systems.com to get a little programm that calculates them automatically
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}
} ;
void GetStrength(byte Strength[4],struct img_par *img,Macroblock* MbP,Macroblock* MbQ,int dir,int edge,int mb_y,int mb_x);
void EdgeLoop(byte* SrcPtr,byte Strength[4], int QP, int AlphaC0Offset, int BetaOffset, int dir,int width,int yuv);
void DeblockMb(ImageParameters *img, byte **imgY, byte ***imgUV, int mb_y, int mb_x) ;
/*!
*****************************************************************************************
* \brief
* Filter all macroblocks in order of increasing macroblock address.
*****************************************************************************************
*/
void DeblockFrame(ImageParameters *img, byte **imgY, byte ***imgUV)
{
unsigned i;
int mb_x, mb_y ;
for (i=0; i<img->PicSizeInMbs; i++)
{
get_mb_block_pos(i, &mb_x, &mb_y);
DeblockMb( img, imgY, imgUV, mb_y, mb_x ) ;
}
}
/*!
*****************************************************************************************
* \brief
* Deblocking filter for one macroblock.
*****************************************************************************************
*/
void DeblockMb(ImageParameters *img, byte **imgY, byte ***imgUV, int mb_y, int mb_x)
{
int EdgeCondition;
int dir,edge,QP;
byte Strength[4], *SrcY, *SrcU, *SrcV ;
Macroblock *MbP, *MbQ ;
int sizey;
int QPC;
int filterLeftMbEdgeFlag = (mb_x != 0);
int filterTopMbEdgeFlag = (mb_y != 0);
int fieldModeMbFlag;
SrcY = imgY [mb_y<<4] + (mb_x<<4) ; // pointers to source
SrcU = imgUV[0][mb_y<<3] + (mb_x<<3) ;
SrcV = imgUV[1][mb_y<<3] + (mb_x<<3) ;
MbQ = &img->mb_data[mb_y*img->PicWidthInMbs + mb_x] ; // current Mb
fieldModeMbFlag = img->field_pic_flag || (img->MbaffFrameFlag && MbQ->mb_field);
// return, if filter is disabled
if (MbQ->LFDisableIdc==1) return;
if (MbQ->LFDisableIdc==2)
{
// don't filter at slice boundaries
filterLeftMbEdgeFlag = MbQ->mbAvailA;
filterTopMbEdgeFlag = MbQ->mbAvailB;;
}
for( dir=0 ; dir<2 ; dir++ ) // vertical edges, than horicontal edges
{
EdgeCondition = (dir && filterTopMbEdgeFlag) || (!dir && filterLeftMbEdgeFlag); // can not filter beyond picture boundaries
for( edge=0 ; edge<4 ; edge++ ) // first 4 vertical strips of 16 pel
{ // then 4 horicontal
if( edge || EdgeCondition )
{
sizey = mb_y%2 ? 1:2*img->width/MB_BLOCK_SIZE-1;
MbP = (edge)? MbQ : ((dir)? (MbQ -(img->width>>4)) : (MbQ-1) ) ; // MbP = Mb of the remote 4x4 block
QP = ( MbP->qp + MbQ->qp + 1) >> 1 ; // Average QP of the two blocks
GetStrength(Strength,img,MbP,MbQ,dir,edge,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( SrcY + (edge<<2)* ((dir)? img->width:1 ), Strength, QP, MbQ->LFAlphaC0Offset, MbQ->LFBetaOffset, dir, img->width, 0) ;
if( (imgUV != NULL) && !(edge & 1) )
{
QPC = (QP_SCALE_CR[MbP->qp] + QP_SCALE_CR[MbQ->qp] + 1) >> 1;
EdgeLoop( SrcU + (edge<<1) * ((dir)? img->width_cr:1 ), Strength, QPC, MbQ->LFAlphaC0Offset, MbQ->LFBetaOffset, dir, img->width_cr, 1 ) ;
EdgeLoop( SrcV + (edge<<1) * ((dir)? img->width_cr:1 ), Strength, QPC, MbQ->LFAlphaC0Offset, MbQ->LFBetaOffset, dir, img->width_cr, 1 ) ;
}
}
}
}//end edge
}//end loop dir
}
/*!
*********************************************************************************************
* \brief
* returns a buffer of 4 Strength values for one stripe in a mb (for different Frame types)
*********************************************************************************************
*/
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 GetStrength(byte Strength[4],struct img_par *img,Macroblock* MbP,Macroblock* MbQ,int dir,int edge,int block_y,int block_x)
{
int blkP, blkQ, idx ;
int blk_x, blk_x2, blk_y, blk_y2 ;
int ***list0_mv = dec_picture->mv[LIST_0];
int ***list1_mv = dec_picture->mv[LIST_1];
int **list0_refIdxArr = dec_picture->ref_idx[LIST_0];
int **list1_refIdxArr = dec_picture->ref_idx[LIST_1];
int **list0_refPicIdArr = dec_picture->ref_pic_id[LIST_0];
int **list1_refPicIdArr = dec_picture->ref_pic_id[LIST_1];
if ((img->structure==FRAME) || !dir)
*((int*)Strength) = ININT_STRENGTH[edge] ; // Start with Strength=3. or Strength=4 for Mb-edge
else
*((int*)Strength) = ININT_STRENGTH[3] ; // Strength=3. for fields
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] ;
if( !(MbP->mb_type==I4MB || MbP->mb_type==I16MB)
&& !(MbQ->mb_type==I4MB || MbQ->mb_type==I16MB) )
{
if( ((MbQ->cbp_blk & (1 << blkQ )) != 0) || ((MbP->cbp_blk & (1 << blkP)) != 0) )
Strength[idx] = 2 ;
else
{ // if no coefs, but vector difference >= 1 set Strength=1
blk_y = block_y + (blkQ >> 2) ; blk_y2 = blk_y - dir ;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -