📄 macroblock.c
字号:
}
/*
*************************************************************************
* Function:Residual Coding of an 8x8 Luma block (not for intra)
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
int // ==> coefficient cost
LumaResidualCoding8x8 (int *cbp, // --> cbp (updated according to processed 8x8 luminance block)
int *cbp_blk, // --> block cbp (updated according to processed 8x8 luminance block)
int block8x8, // <-- block number of 8x8 block
int fw_mode, // <-- forward prediction mode (1-7, 0=DIRECT)
int bw_mode, // <-- backward prediction mode (1-7, 0=DIRECT)
int fw_refframe, // <-- reference frame for forward prediction
int bw_refframe // <-- reference frame for backward prediction
)
{
int block_y, block_x, pic_pix_y, pic_pix_x, i, j, cbp_blk_mask;
int coeff_cost = 0;
int mb_y = (block8x8 / 2) << 3;
int mb_x = (block8x8 % 2) << 3;
int cbp_mask = 1 << block8x8;
int bxx, byy; // indexing curr_blk
int scrFlag = 0; // 0=noSCR, 1=strongSCR, 2=jmSCR
byte** imgY_original = imgY_org;
int pix_x = img->pix_x;
int pix_y = img->pix_y;
Macroblock* currMB = &img->mb_data[img->current_mb_nr];
int direct = (fw_mode == 0 && bw_mode == 0 && (img->type==B_IMG));
int skipped = (fw_mode == 0 && bw_mode == 0 && (img->type!=B_IMG));
short curr_blk[B8_SIZE][B8_SIZE]; // AVS 8x8 pred.error buffer
int incr_y=1,off_y=0; /*lgp*/
int IntraPrediction=IS_INTRA (currMB); //cjw 20060321
if (img->type==B_IMG)
scrFlag = 1;
//===== loop over 4x4 blocks =====
for (byy=0, block_y=mb_y; block_y<mb_y+8; byy+=4, block_y+=4)
{
//pic_pix_y = pix_y + block_y;/*lgp*/
pic_pix_y = pix_y + mb_y;/*lgp*/
for (bxx=0, block_x=mb_x; block_x<mb_x+8; bxx+=4, block_x+=4)
{
pic_pix_x = pix_x + block_x;
cbp_blk_mask = (block_x>>2) + block_y;
//===== prediction of 4x4 block =====
LumaPrediction4x4 (block_x, block_y, fw_mode, bw_mode, fw_refframe, bw_refframe);
// !! start shenyanfei
//if(((!direct)&&(!skipped)&&(img->LumVarFlag == 1) && (img->mb_weighting_flag == 0)&&(img->weighting_prediction == 1))
//if(((!skipped)&&(!direct)&&(img->LumVarFlag == 1) && (img->mb_weighting_flag == 1)&&(img->weighting_prediction == 1)) //cjw 20051230
if(((!skipped) && (img->LumVarFlag == 1) && (img->mb_weighting_flag == 1)&&(img->weighting_prediction == 1)) //cjw 20060321
||((img->LumVarFlag == 1) && (img->mb_weighting_flag == 0))){
for (j=0; j<4; j++)
for (i=0; i<4; i++)
img->mpr[i+block_x][j+block_y] = img->mpr_weight[i+block_x][j+block_y];
}
// !! end shenyanfei
//===== get displaced frame difference ======
for (j=0; j<4; j++)
for (i=0; i<4; i++)
{
img->m7[i][j] = curr_blk[byy+j][bxx+i] =
imgY_original[pic_pix_y+incr_y*(j+byy)+off_y/*lgp*/][pic_pix_x+i] - img->mpr[i+block_x][j+block_y];
}
}
}
if (!skipped)
{
transform_B8(curr_blk);
coeff_cost = scanquant_B8 (img->qp-MIN_QP, 0, block8x8, curr_blk, scrFlag, cbp, cbp_blk);
}
/*
The purpose of the action below is to prevent that single or 'expensive' coefficients are coded.
With 4x4 transform there is larger chance that a single coefficient in a 8x8 or 16x16 block may be nonzero.
A single small (level=1) coefficient in a 8x8 block will cost: 3 or more bits for the coefficient,
4 bits for EOBs for the 4x4 blocks,possibly also more bits for CBP. Hence the total 'cost' of that single
coefficient will typically be 10-12 bits which in a RD consideration is too much to justify the distortion improvement.
The action below is to watch such 'single' coefficients and set the reconstructed block equal to the prediction according
to a given criterium. The action is taken only for inter luma blocks.
Notice that this is a pure encoder issue and hence does not have any implication on the standard.
coeff_cost is a parameter set in dct_luma() and accumulated for each 8x8 block. If level=1 for a coefficient,
coeff_cost is increased by a number depending on RUN for that coefficient.The numbers are (see also dct_luma()): 3,2,2,1,1,1,0,0,...
when RUN equals 0,1,2,3,4,5,6, etc.
If level >1 coeff_cost is increased by 9 (or any number above 3). The threshold is set to 3. This means for example:
1: If there is one coefficient with (RUN,level)=(0,1) in a 8x8 block this coefficient is discarded.
2: If there are two coefficients with (RUN,level)=(1,1) and (4,1) the coefficients are also discarded
sum_cnt_nonz is the accumulation of coeff_cost over a whole macro block. If sum_cnt_nonz is 5 or less for the whole MB,
all nonzero coefficients are discarded for the MB and the reconstructed block is set equal to the prediction.
*///Lou
/*lgp*dct*/
if (!skipped && coeff_cost <= _LUMA_COEFF_COST_)
{
coeff_cost = 0;
(*cbp) &= (63 - cbp_mask);
(*cbp_blk) &= ~(51 << (4*block8x8-2*(block8x8%2)));
for (i=mb_x; i<mb_x+8; i++)
for (j=mb_y; j<mb_y+8; j++)
{
imgY[img->pix_y+j][img->pix_x+i] = img->mpr[i][j];
}
}
return coeff_cost;
}
void // ==> coefficient cost
LumaPrediction (int *cbp, // --> cbp (updated according to processed 8x8 luminance block)
int *cbp_blk, // --> block cbp (updated according to processed 8x8 luminance block)
int block8x8, // <-- block number of 8x8 block
int fw_mode, // <-- forward prediction mode (1-7, 0=DIRECT)
int bw_mode, // <-- backward prediction mode (1-7, 0=DIRECT)
int fw_refframe, // <-- reference frame for forward prediction
int bw_refframe // <-- reference frame for backward prediction
)
{
int block_y, block_x, pic_pix_y, pic_pix_x, cbp_blk_mask;
int coeff_cost = 0;
int mb_y = (block8x8 / 2) << 3;
int mb_x = (block8x8 % 2) << 3;
int cbp_mask = 1 << block8x8;
int bxx, byy; // indexing curr_blk
int scrFlag = 0; // 0=noSCR, 1=strongSCR, 2=jmSCR
byte** imgY_original = imgY_org;
int pix_x = img->pix_x;
int pix_y = img->pix_y;
Macroblock* currMB = &img->mb_data[img->current_mb_nr];
int skipped = (fw_mode == 0 && bw_mode == 0 && (img->type!=B_IMG));
int incr_y=1,off_y=0; /*lgp*/
if (img->type==B_IMG)
scrFlag = 1;
//===== loop over 4x4 blocks =====
for (byy=0, block_y=mb_y; block_y<mb_y+8; byy+=4, block_y+=4)
{
pic_pix_y = pix_y + mb_y;/*lgp*/
for (bxx=0, block_x=mb_x; block_x<mb_x+8; bxx+=4, block_x+=4)
{
pic_pix_x = pix_x + block_x;
cbp_blk_mask = (block_x>>2) + block_y;
//===== prediction of 4x4 block =====
LumaPrediction4x4 (block_x, block_y, fw_mode, bw_mode, fw_refframe, bw_refframe);
}
}
return ;
}
/*
*************************************************************************
* Function:Set mode parameters and reference frames for an 8x8 block
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
void
SetModesAndRefframe (int b8, int* fw_mode, int* bw_mode, int* fw_ref, int* bw_ref)
{
Macroblock* currMB = &img->mb_data[img->current_mb_nr];
int j = (b8/2);
int i = (b8%2);
int** frefarr = refFrArr; // For MB level field/frame coding
int** fw_refarr = fw_refFrArr; // For MB level field/frame coding
int** bw_refarr = bw_refFrArr; // For MB level field/frame coding
int block_x = img->block_x;
int block_y = img->block_y; // For MB level field/frame coding
*fw_mode = *bw_mode = *fw_ref = *bw_ref = -1;
if (img->type!=B_IMG)
{
*fw_ref = frefarr[(block_y>>1)+j][(block_x>>1)+i];
*bw_ref = 0;
*bw_mode = 0;
*fw_mode = currMB->b8mode[b8];
}
else
{
if (currMB->b8pdir[b8]==-1)
{
*fw_ref = -1;
*bw_ref = -1;
*fw_mode = 0;
*bw_mode = 0;
}
else if (currMB->b8pdir[b8]==0)
{
*fw_ref = fw_refarr[(block_y>>1)+j][(block_x>>1)+i];
*bw_ref = 0;
*fw_mode = currMB->b8mode[b8];
*bw_mode = 0;
}
else if (currMB->b8pdir[b8]==1)
{
*fw_ref = 0;
*bw_ref = bw_refarr[(block_y>>1)+j][(block_x>>1)+i];
*fw_mode = 0;
*bw_mode = currMB->b8mode[b8];
}
else
{
*fw_ref = fw_refarr[(block_y>>1)+j][(block_x>>1)+i];
*bw_ref = bw_refarr[(block_y>>1)+j][(block_x>>1)+i];
*fw_mode = currMB->b8mode[b8];
*bw_mode = currMB->b8mode[b8];
if (currMB->b8mode[b8]==0) // direct
{
if (img->type==B_IMG)
{
//sw
*fw_ref = 0;// max(0,frefarr[(block_y>>1)+j][(block_x>>1)+i]);
*bw_ref = 0;
}
else
{
*fw_ref = max(0,frefarr[(block_y>>1)+j][(block_x>>1)+i]);
*bw_ref = 0;
}
}
}
}
}
/*
*************************************************************************
* Function:Residual Coding of a Luma macroblock (not for intra)
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
void LumaResidualCoding ()
{
int i,j,block8x8;
int fw_mode, bw_mode, refframe;
int sum_cnt_nonz;
Macroblock *currMB = &img->mb_data[img->current_mb_nr];
int incr_y=1,off_y=0;/*lgp*/
int stage_block8x8_pos=0;/*lgp*/
int skipped;/*lgp*dct*/
int sad1 , sad2 ;
byte** imgY_original = imgY_org;
currMB->cbp = 0 ;
currMB->cbp_blk = 0 ;
sum_cnt_nonz = 0 ;
// !! start shenyanfei
if((img->LumVarFlag == 1) && (img->mb_weighting_flag == 1)){
for (block8x8=stage_block8x8_pos; block8x8<4; block8x8++){
int bw_ref;
SetModesAndRefframe (block8x8, &fw_mode, &bw_mode, &refframe, &bw_ref);
LumaPrediction(&(currMB->cbp), &(currMB->cbp_blk), block8x8,fw_mode, bw_mode, refframe, bw_ref);
}
sad1 = sad2 = 0 ;
for (j=0; j < MB_BLOCK_SIZE; j++)
{
for (i=0; i < MB_BLOCK_SIZE; i++){
sad1 += abs(imgY_original[img->pix_y+j][img->pix_x+i] -img->mpr[i][j]);
sad2 += abs(imgY_original[img->pix_y+j][img->pix_x+i] -img->mpr_weight[i][j]);
}
}
if(sad1 > sad2){
img->weighting_prediction = 1 ;
}
else{
img->weighting_prediction = 0 ;
}
}
// !! end shenyanfei
currMB->cbp = 0 ;
currMB->cbp_blk = 0 ;
sum_cnt_nonz = 0 ;
for (block8x8=stage_block8x8_pos/*lgp*/; block8x8<4; block8x8++)
{
int bw_ref;
SetModesAndRefframe (block8x8, &fw_mode, &bw_mode, &refframe, &bw_ref);
skipped = (fw_mode == 0 && bw_mode == 0 && (img->type!=B_IMG));
sum_cnt_nonz += LumaResidualCoding8x8 (&(currMB->cbp), &(currMB->cbp_blk), block8x8,fw_mode, bw_mode, refframe, bw_ref);
}
if (sum_cnt_nonz <= 5)
{
currMB->cbp &= 0xfffff0 ;
currMB->cbp_blk &= 0xff0000 ;
for (i=0; i < MB_BLOCK_SIZE; i++)
{
for (j=0; j < MB_BLOCK_SIZE; j++)
{
imgY[img->pix_y+j][img->pix_x+i]=img->mpr[i][j];
}
}
}
}
/*
*************************************************************************
* Function: Predict one component of a chroma 4x4 block
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
void
OneComponentChromaPrediction4x4 (int* mpred, // --> array to store prediction values
int pix_c_x, // <-- horizontal pixel coordinate of 4x4 block
int pix_c_y, // <-- vertical pixel coordinate of 4x4 block
int***** mv, // <-- motion vector array
int ref, // <-- reference frame parameter (0.../ -1: backward)
int blocktype, // <-- block type
int uv,
int directforword) // <-- chroma component
{
int i, j, ii, jj, ii0, jj0, ii1, jj1, if0, if1, jf0, jf1;
int incr;
int* mvb;
int refframe = (ref<0 ? 0 : ref);
pel_t** refimage;
int je = pix_c_y + 4;
int ie = pix_c_x + 4;
int f1 = 8 , f2=f1-1, f3=f1*f1, f4=f3>>1;
int s1 = 3;
int img_pic_c_x = img->pix_c_x;
int img_pic_c_y = img->pix_c_y;
int scale = 1;
int field_mode;
int fpred = ref < 0 ? 0: 1;
//xyji 11.27
if (!img->picture_structure) // field coding
{
if (img->type==B_IMG)
refframe = ref<0 ? ref+2 : ref;
}
field_mode = (!img->picture_structure);
incr = 1;
if(img->type==B_IMG && !img->picture_structure)
incr = 2;
ref = (img->type==B_IMG) ? ref+incr : ref;
refimage = mcef [ref][uv];
field_mode = (!img->picture_structure);
for (j=pix_c_y; j<je; j++)
for (i=pix_c_x; i<ie; i++)
{
mvb = mv [(i-img_pic_c_x)>>2][(j-img_pic_c_y)>>2][refframe][blocktype];
ii = (i<<s1) + mvb[0];
jj = (j<<s1) + mvb[1];
ii0 = max (0, min (img->width_cr -1, ii>>s1 ));
jj0 = max (0, min (img->height_cr/scale-1, jj>>s1 )); // For MB level field/frame -- scale chroma height by 2
ii1 = max (0, min (img->width_cr -1, (ii+f2)>>s1));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -