📄 macroblock.c
字号:
rec_resG[j+block_y][i+block_x] = img->m7[j][i];
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
}
}
}
}
}
if(need_8x8_transform)
{
if (img->NoResidueDirect != 1 && !skipped)
{
if (img->type!=SP_SLICE)
#ifdef USE_INTRA_MDDT
nonzero = dct_luma8x8 (block8x8, &coeff_cost, 0, -1);
#else
nonzero = dct_luma8x8 (block8x8, &coeff_cost, 0);
#endif
// Residue Color Transform
if(img->residue_transform_flag)
{
for (j=0; j<8; j++)
for (i=0; i<8; i++)
rec_resG[mb_y+j][mb_x+i] = img->m7[j][i];
}
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 + img->bitdepth_luma_qp_scale)!=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)));
#ifdef ADAPTIVE_FD_SD_CODING
currMB->SD_or_FD [block8x8/2][block8x8%2]=0;
set_bit (&(currMB->SD_or_FD_t8x8),block8x8,0 );
for (j=0;j<4;j++)
{
memset(img->cofAC[block8x8][j][0],0,65 * sizeof(int));
memset(img->cofAC[block8x8][j][1],0,65 * sizeof(int));
}
#endif
if(!img->residue_transform_flag)
{
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));
}
else // Residue Color Transform
{
for (i=mb_x; i<mb_x+8; i++)
for (j=mb_y; j<mb_y+8; j++)
{
rec_resG[j][i] = 0;
}
}
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);
}
}
#ifdef ADAPTIVE_FD_SD_CODING
if (img->APEC_in_FD_and_SD && currMB->SD_Coding_on_off==1 && !skipped && currMB->b8mode[block8x8] != IBLOCK)
{
if (!need_8x8_transform)
{
encode_in_spatial_domain(block8x8,cbp_blk, cbp, &coeff_cost);
}
else
{
encode_in_spatial_domain_8x8(block8x8,cbp_blk, cbp, &coeff_cost);
}
}
if (img->NoResidueDirect != 1 && !skipped && coeff_cost <= _LUMA_COEFF_COST_ &&
((img->qp + img->bitdepth_luma_qp_scale)!=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)));
currMB->SD_or_FD [block8x8/2][block8x8%2]=0;
set_bit (&(currMB->SD_or_FD_t8x8),block8x8,0 );
for (j=0;j<4;j++)
{
memset(img->cofAC[block8x8][j][0],0,65 * sizeof(int));
memset(img->cofAC[block8x8][j][1],0,65 * sizeof(int));
}
if(!img->residue_transform_flag)
{
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));
}
else // Residue Color Transform
{
for (i=mb_x; i<mb_x+8; i++)
for (j=mb_y; j<mb_y+8; j++)
{
rec_resG[j][i] = 0;
}
}
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);
}
}
#endif
return coeff_cost;
}
/*!
************************************************************************
* \brief
* Set mode parameters and reference frames for an 8x8 block
************************************************************************
*/
void SetModesAndRefframe (int b8, short* p_dir, int* fw_mode, int* bw_mode, short* fw_ref, short* bw_ref)
{
Macroblock* currMB = &img->mb_data[img->current_mb_nr];
int j = 2*(b8>>1);
int i = 2*(b8 & 0x01);
*fw_mode = *bw_mode = *fw_ref = *bw_ref = -1;
*p_dir = currMB->b8pdir[b8];
if (img->type!=B_SLICE)
{
*fw_ref = enc_picture->ref_idx[LIST_0][img->block_y+j][img->block_x+i];
*bw_ref = 0;
*fw_mode = currMB->b8mode[b8];
*bw_mode = 0;
}
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 = enc_picture->ref_idx[LIST_0][img->block_y+j][img->block_x+i];
*bw_ref = 0;
*fw_mode = currMB->b8mode[b8];
*bw_mode = 0;
}
else if (currMB->b8pdir[b8]==1)
{
*fw_ref = 0;
*bw_ref = enc_picture->ref_idx[LIST_1][img->block_y+j][img->block_x+i];
*fw_mode = 0;
*bw_mode = currMB->b8mode[b8];
}
else
{
*fw_ref = enc_picture->ref_idx[LIST_0][img->block_y+j][img->block_x+i];
*bw_ref = enc_picture->ref_idx[LIST_1][img->block_y+j][img->block_x+i];
*fw_mode = currMB->b8mode[b8];
*bw_mode = currMB->b8mode[b8];
}
}
}
/*!
************************************************************************
* \brief
* Residual Coding of a Luma macroblock (not for intra)
************************************************************************
*/
void LumaResidualCoding ()
{
int i,j,block8x8,b8_x,b8_y;
int fw_mode, bw_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 bw_ref;
SetModesAndRefframe (block8x8, &p_dir, &fw_mode, &bw_mode, &refframe, &bw_ref);
sum_cnt_nonz += LumaResidualCoding8x8 (&(currMB->cbp), &(currMB->cbp_blk), block8x8,
p_dir, fw_mode, bw_mode, refframe, bw_ref);
}
if (sum_cnt_nonz <= _LUMA_MB_COEFF_COST_ &&
((img->qp + img->bitdepth_luma_qp_scale)!=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 ;
if(!img->residue_transform_flag)
{
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));
}
else
{
for (i=0; i < MB_BLOCK_SIZE; i++)
for (j=0; j < MB_BLOCK_SIZE; j++)
{
rec_resG[j][i] = 0;
}
}
#ifdef ADAPTIVE_FD_SD_CODING
for (i=0;i<4;i++)
{
for (j=0;j<64;j++)
{
img->cofAC[i][0][0][j]=0;
img->cofAC[i][0][1][j]=0;
img->cofAC[i][1][0][j]=0;
img->cofAC[i][1][1][j]=0;
img->cofAC[i][2][0][j]=0;
img->cofAC[i][2][1][j]=0;
img->cofAC[i][3][0][j]=0;
img->cofAC[i][3][1][j]=0;
}
}
#endif
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 fw_mode, bw_mode;
short p_dir, fw_ref, bw_ref;
int num_blks;
int cost8x8=0, cost4x4=0;
int diff[64], *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, &fw_mode, &bw_mode, &fw_ref, &bw_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, fw_mode, bw_mode, fw_ref, bw_ref);
//===== get displaced frame difference ======
diff_ptr=&diff[k];
for (j=0; j<4; j++)
{
for (i=0; i<4; i++, k++)
diff[k] = imgY_org[pic_pix_y+j][pic_pix_x+i] - img->mpr[j+block_y][i+block_x];
}
cost4x4 += SATD (diff_ptr, input->hadamard);
}
}
cost8x8 += SATD8X8 (diff, input->hadamard);
}
if(input->Transform8x8Mode==2) //always allow 8x8 transform
return 1;
else if(cost8x8<cost4x4)
return 1;
else
{
*cost = (*cost-cost8x8+cost4x4);
return 0;
}
}
/*!
************************************************************************
* \brief
* Predict one component of a chroma 4x4 block
************************************************************************
*/
void OneComponentChromaPrediction4x4 (int* mpred, //!< array to store prediction values
int block_c_x, //!< horizontal pixel coordinate of 4x4 block
int block_c_y, //!< vertical pixel coordinate of 4x4 block
short****** mv, //!< motion vector array
int list_idx, //!< reference picture list
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -