📄 macroblock.c
字号:
imgpel* l0pred = l0_pred;
imgpel* l1pred = l1_pred;
Macroblock* currMB = &img->mb_data[img->current_mb_nr];
int apply_weights = ( (active_pps->weighted_pred_flag && (img->type == P_SLICE || img->type == SP_SLICE)) ||
(active_pps->weighted_bipred_idc && (img->type == B_SLICE)));
short ****mv_array = list ? img->bipred_mv1[by][bx] : img->bipred_mv2[by][bx];
OneComponentLumaPrediction4x4 (l0_pred, pic_opix_x, pic_opix_y, mv_array[LIST_0][l0_ref_idx][l0_mode], l0_ref_idx, listX[0+currMB->list_offset]);
OneComponentLumaPrediction4x4 (l1_pred, pic_opix_x, pic_opix_y, mv_array[LIST_1][l1_ref_idx][l1_mode], l1_ref_idx, listX[1+currMB->list_offset]);
if (apply_weights)
{
int wbp0 = wbp_weight[0][l0_ref_idx][l1_ref_idx][0];
int wbp1 = wbp_weight[1][l0_ref_idx][l1_ref_idx][0];
int offset = (wp_offset[0][l0_ref_idx][0] + wp_offset[1][l1_ref_idx][0] + 1)>>1;
for (j=block_y; j<block_y4; j++)
for (i=block_x; i<block_x4; i++)
img->mpr[j][i] = iClip1( img->max_imgpel_value,
((wbp0 * *l0pred++ + wbp1 * *l1pred++ + 2*wp_luma_round) >> (luma_log_weight_denom + 1)) + offset);
}
else
{
for (j=block_y; j<block_y4; j++)
for (i=block_x; i<block_x4; i++)
img->mpr[j][i] = (*l0pred++ + *l1pred++ + 1) >> 1;
}
}
/*!
************************************************************************
* \brief
* Residual Coding of an 8x8 Luma block (not for intra)
*
* \return
* coefficient cost
************************************************************************
*/
int LumaResidualCoding8x8 ( int *cbp, //!< Output: cbp (updated according to processed 8x8 luminance block)
int64 *cbp_blk, //!< Output: block cbp (updated according to processed 8x8 luminance block)
int block8x8, //!< block number of 8x8 block
short p_dir, //!< prediction direction
int l0_mode, //!< list0 prediction mode (1-7, 0=DIRECT)
int l1_mode, //!< list1 prediction mode (1-7, 0=DIRECT)
short l0_ref_idx, //!< reference picture for list0 prediction
short l1_ref_idx //!< reference picture for list0 prediction
)
{
int block_y, block_x, pic_pix_y, pic_pix_x, i, j, nonzero = 0, cbp_blk_mask;
int coeff_cost = 0;
int mb_y = (block8x8 >> 1) << 3;
int mb_x = (block8x8 & 0x01) << 3;
int pix_y;
int cbp_mask = 1 << block8x8;
int bxx, byy; // indexing curr_blk
int skipped = (l0_mode == 0 && l1_mode == 0 && (img->type != B_SLICE));
Macroblock* currMB = &img->mb_data[img->current_mb_nr];
//set transform size
int need_8x8_transform = currMB->luma_transform_size_8x8_flag;
if ( input->ChromaMCBuffer )
OneComponentChromaPrediction4x4 = OneComponentChromaPrediction4x4_retrieve;
else
OneComponentChromaPrediction4x4 = OneComponentChromaPrediction4x4_regenerate;
//===== loop over 4x4 blocks =====
if(!need_8x8_transform)
{
for (byy=0, block_y=mb_y; block_y<mb_y+8; byy+=4, block_y+=4)
{
pic_pix_y = img->opix_y + block_y;
for (bxx=0, block_x=mb_x; block_x<mb_x+8; bxx+=4, block_x+=4)
{
pic_pix_x = img->opix_x + block_x;
cbp_blk_mask = (block_x>>2) + block_y;
//===== prediction of 4x4 block =====
LumaPrediction4x4 (block_x, block_y, p_dir, l0_mode, l1_mode, l0_ref_idx, l1_ref_idx);
//===== get displaced frame difference ======
for (j=0; j<4; j++)
{
pix_y = pic_pix_y + j;
for (i=0; i<4; i++)
{
img->m7[j][i] = imgY_org[pix_y][pic_pix_x + i] - img->mpr[j+block_y][i+block_x];
}
}
//===== DCT, Quantization, inverse Quantization, IDCT, Reconstruction =====
if ( (img->NoResidueDirect != 1 && !skipped ) ||
((img->qp_scaled)==0 && img->lossless_qpprime_flag==1) )
{
//===== DCT, Quantization, inverse Quantization, IDCT, Reconstruction =====
if (img->type!=SP_SLICE)
nonzero = dct_luma (block_x, block_y, &coeff_cost, 0);
else if(!si_frame_indicator && !sp2_frame_indicator)
nonzero = dct_luma_sp(block_x, block_y, &coeff_cost);// SP frame encoding
else
nonzero = dct_luma_sp2(block_x, block_y, &coeff_cost);//switching SP/SI encoding
if (nonzero)
{
(*cbp_blk) |= (int64)1 << cbp_blk_mask; // one bit for every 4x4 block
(*cbp) |= cbp_mask; // one bit for the 4x4 blocks of an 8x8 block
}
}
}
}
}
else
{
for (byy=0, block_y=mb_y; block_y<mb_y+8; byy+=4, block_y+=4)
{
pic_pix_y = img->opix_y + block_y;
for (bxx=0, block_x=mb_x; block_x<mb_x+8; bxx+=4, block_x+=4)
{
pic_pix_x = img->opix_x + block_x;
cbp_blk_mask = (block_x>>2) + block_y;
//===== prediction of 4x4 block =====
LumaPrediction4x4 (block_x, block_y, p_dir, l0_mode, l1_mode, l0_ref_idx, l1_ref_idx);
//===== get displaced frame difference ======
for (j=0; j<4; j++)
{
pix_y = pic_pix_y + j;
for (i=0; i<4; i++)
{
img->m7[j+byy][i+bxx] = imgY_org[pix_y][pic_pix_x+i] - img->mpr[j+block_y][i+block_x];
}
}
}
}
if (img->NoResidueDirect != 1 && !skipped)
{
if (img->type!=SP_SLICE)
nonzero = dct_luma8x8 (block8x8, &coeff_cost, 0);
if (nonzero)
{
(*cbp_blk) |= 51 << (4*block8x8-2*(block8x8 & 0x01)); // corresponds to 110011, as if all four 4x4 blocks contain coeff, shifted to block position
(*cbp) |= cbp_mask; // one bit for the 4x4 blocks of an 8x8 block
}
}
}
/*
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.
*/
if (img->NoResidueDirect != 1 && !skipped && coeff_cost <= _LUMA_COEFF_COST_ &&
((img->qp_scaled)!=0 || img->lossless_qpprime_flag==0)&&
!(img->type==SP_SLICE && (si_frame_indicator==1 || sp2_frame_indicator==1 )))// last set of conditions
// cannot skip when perfect reconstruction is as in switching pictures or SI pictures
{
coeff_cost = 0;
(*cbp) &= (63 - cbp_mask);
(*cbp_blk) &= ~(51 << (4*block8x8-2*(block8x8 & 0x01)));
for (j=mb_y; j<mb_y+8; j++)
memcpy(&enc_picture->imgY[img->pix_y + j][img->pix_x + mb_x], &img->mpr[j][mb_x], 2 * BLOCK_SIZE * sizeof(imgpel));
if (img->type==SP_SLICE)
{
for (i=mb_x; i < mb_x+BLOCK_SIZE*2; i+=BLOCK_SIZE)
for (j=mb_y; j < mb_y+BLOCK_SIZE*2; j+=BLOCK_SIZE)
copyblock_sp(i,j);
}
}
return coeff_cost;
}
/*!
************************************************************************
* \brief
* Set mode parameters and reference frames for an 8x8 block
************************************************************************
*/
void SetModesAndRefframe (int b8, short* p_dir, int* l0_mode, int* l1_mode, short* l0_ref, short* l1_ref)
{
Macroblock* currMB = &img->mb_data[img->current_mb_nr];
int j = 2*(b8>>1);
int i = 2*(b8 & 0x01);
*l0_mode = *l1_mode = *l0_ref = *l1_ref = -1;
*p_dir = currMB->b8pdir[b8];
if (img->type!=B_SLICE)
{
*l0_ref = enc_picture->ref_idx[LIST_0][img->block_y+j][img->block_x+i];
*l1_ref = 0;
*l0_mode = currMB->b8mode[b8];
*l1_mode = 0;
}
else
{
if (currMB->b8pdir[b8]==-1)
{
*l0_ref = -1;
*l1_ref = -1;
*l0_mode = 0;
*l1_mode = 0;
}
else if (currMB->b8pdir[b8]==0)
{
*l0_ref = enc_picture->ref_idx[LIST_0][img->block_y+j][img->block_x+i];
*l1_ref = 0;
*l0_mode = currMB->b8mode[b8];
*l1_mode = 0;
}
else if (currMB->b8pdir[b8]==1)
{
*l0_ref = 0;
*l1_ref = enc_picture->ref_idx[LIST_1][img->block_y+j][img->block_x+i];
*l0_mode = 0;
*l1_mode = currMB->b8mode[b8];
}
else
{
*l0_ref = enc_picture->ref_idx[LIST_0][img->block_y+j][img->block_x+i];
*l1_ref = enc_picture->ref_idx[LIST_1][img->block_y+j][img->block_x+i];
*l0_mode = currMB->b8mode[b8];
*l1_mode = currMB->b8mode[b8];
}
}
}
/*!
************************************************************************
* \brief
* Residual Coding of a Luma macroblock (not for intra)
************************************************************************
*/
void LumaResidualCoding (void)
{
int i,j,block8x8,b8_x,b8_y;
int l0_mode, l1_mode;
short p_dir, refframe;
int sum_cnt_nonz;
Macroblock *currMB = &img->mb_data[img->current_mb_nr];
currMB->cbp = 0 ;
currMB->cbp_blk = 0 ;
sum_cnt_nonz = 0 ;
for (block8x8=0; block8x8<4; block8x8++)
{
short l1_ref;
SetModesAndRefframe (block8x8, &p_dir, &l0_mode, &l1_mode, &refframe, &l1_ref);
sum_cnt_nonz += LumaResidualCoding8x8 (&(currMB->cbp), &(currMB->cbp_blk), block8x8,
p_dir, l0_mode, l1_mode, refframe, l1_ref);
}
if (sum_cnt_nonz <= _LUMA_MB_COEFF_COST_ &&
((img->qp_scaled)!=0 || img->lossless_qpprime_flag==0) &&
!(img->type==SP_SLICE && (si_frame_indicator==1 || sp2_frame_indicator==1)))// modif ES added last set of conditions
//cannot skip if SI or switching SP frame perfect reconstruction is needed
{
currMB->cbp &= 0xfffff0 ;
currMB->cbp_blk &= 0xff0000 ;
for (j=0; j < MB_BLOCK_SIZE; j++)
memcpy(&enc_picture->imgY[img->pix_y+j][img->pix_x], img->mpr[j], MB_BLOCK_SIZE * sizeof (imgpel));
if (img->type==SP_SLICE)
{
for(block8x8=0;block8x8<4;block8x8++)
{
b8_x=(block8x8&1)<<3;
b8_y=(block8x8&2)<<2;
for (i=b8_x;i<b8_x+8;i+=4)
for (j=b8_y;j<b8_y+8;j+=4)
copyblock_sp(i,j);
}
}
}
}
/*!
************************************************************************
* \brief
* Makes the decision if 8x8 tranform will be used (for RD-off)
************************************************************************
*/
int TransformDecision (int block_check, int *cost)
{
int block_y, block_x, pic_pix_y, pic_pix_x, i, j, k;
int mb_y, mb_x, block8x8;
int l0_mode, l1_mode;
short p_dir, l0_ref, l1_ref;
int num_blks;
int cost8x8=0, cost4x4=0;
int *diff_ptr;
if(block_check==-1)
{
block8x8=0;
num_blks=4;
}
else
{
block8x8=block_check;
num_blks=block_check+1;
}
for (; block8x8<num_blks; block8x8++)
{
SetModesAndRefframe (block8x8, &p_dir, &l0_mode, &l1_mode, &l0_ref, &l1_ref);
mb_y = (block8x8 >> 1) << 3;
mb_x = (block8x8 & 0x01) << 3;
//===== loop over 4x4 blocks =====
k=0;
for (block_y=mb_y; block_y<mb_y+8; block_y+=4)
{
pic_pix_y = img->opix_y + block_y;
for (block_x=mb_x; block_x<mb_x+8; block_x+=4)
{
pic_pix_x = img->opix_x + block_x;
//===== prediction of 4x4 block =====
LumaPrediction4x4 (block_x, block_y, p_dir, l0_mode, l1_mode, l0_ref, l1_ref);
//===== get displaced frame difference ======
diff_ptr=&diff64[k];
for (j=0; j<4; j++)
{
for (i=0; i<4; i++, k++)
diff64[k] = imgY_org[pic_pix_y+j][pic_pix_x+i] - img->mpr[j+block_y][i+block_x];
}
cost4x4 += distortion4x4 (diff_ptr);
}
}
cost8x8 += distortion8x8 (diff64);
}
if(input->Transform8x8Mode==2) //always allow 8x8 transform
return 1;
else if(cost8x8<cost4x4)
return 1;
else
{
*cost = (*cost-cost8x8+cost4x4);
return 0;
}
}
/*!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -