📄 macroblock.c
字号:
for (i=0; i<4; i++)
{
residue_B = imgUV_org[0][pic_pix_y+j][pic_pix_x+i] - mprRGB[1][i+block_x][j+block_y];
residue_G = imgY_org[pic_pix_y+j][pic_pix_x+i] - mprRGB[0][i+block_x][j+block_y];
residue_R = imgUV_org[1][pic_pix_y+j][pic_pix_x+i] - mprRGB[2][i+block_x][j+block_y];
resTrans_R[i+block_x][j+block_y] = residue_R-residue_B;
temp = residue_B+(resTrans_R[i+block_x][j+block_y]>>1);
resTrans_B[i+block_x][j+block_y] = residue_G-temp;
resTrans_G[i+block_x][j+block_y] = temp+(resTrans_B[i+block_x][j+block_y]>>1);
if(!need_8x8_transform)
img->m7[i][j] = resTrans_G[i+block_x][j+block_y];
else
img->m7[i+bxx][j+byy] = resTrans_G[i+block_x][j+block_y];
}
}
}
}
if(need_8x8_transform)
{
if (img->NoResidueDirect != 1 && !skipped)
{
if (img->type!=SP_SLICE)
nonzero = dct_luma8x8 (block8x8, &coeff_cost, 0);
// Residue Color Transform
if(img->residue_transform_flag)
{
for (j=0; j<8; j++)
for (i=0; i<8; i++)
rec_resG[mb_x+i][mb_y+j] = img->m7[i][j];
}
if (nonzero)
{
(*cbp_blk) |= 51 << (4*block8x8-2*(block8x8%2)); // 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))
{
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++)
{
// Residue Color Transform
if(!img->residue_transform_flag)
enc_picture->imgY[img->pix_y+j][img->pix_x+i] = img->mpr[i][j];
else
rec_resG[i][j] = 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);
}
}
return coeff_cost;
}
/*!
************************************************************************
* \brief
* Set mode parameters and reference frames for an 8x8 block
************************************************************************
*/
void
SetModesAndRefframe (int b8, int* p_dir, int* fw_mode, int* bw_mode, int* fw_ref, int* bw_ref)
{
Macroblock* currMB = &img->mb_data[img->current_mb_nr];
int j = 2*(b8/2);
int i = 2*(b8%2);
*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_x+i][img->block_y+j];
*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_x+i][img->block_y+j];
*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_x+i][img->block_y+j];
*fw_mode = 0;
*bw_mode = currMB->b8mode[b8];
}
else
{
*fw_ref = enc_picture->ref_idx[LIST_0][img->block_x+i][img->block_y+j];
*bw_ref = enc_picture->ref_idx[LIST_1][img->block_x+i][img->block_y+j];
*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 p_dir, fw_mode, bw_mode, 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++)
{
int 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))
{
currMB->cbp &= 0xfffff0 ;
currMB->cbp_blk &= 0xff0000 ;
for (i=0; i < MB_BLOCK_SIZE; i++)
{
for (j=0; j < MB_BLOCK_SIZE; j++)
{
if(!img->residue_transform_flag)
enc_picture->imgY[img->pix_y+j][img->pix_x+i]=img->mpr[i][j];
else
rec_resG[i][j] = 0;
}
}
if (img->type==SP_SLICE)
{
for(block8x8=0;block8x8<4;block8x8++)
{
b8_x=(block8x8&1)<<3;
b8_y=(block8x8&2)<<2;
for (i=0;i<8;i+=4)
for (j=0;j<8;j+=4)
copyblock_sp(b8_x+i,b8_y+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 p_dir, fw_mode, bw_mode, 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 / 2) << 3;
mb_x = (block8x8 % 2) << 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[i+block_x][j+block_y];
}
cost4x4 += SATD (diff_ptr, input->hadamard);
}
}
cost8x8 += SATD8X8 (diff, input->hadamard);
}
if(input->AllowTransform8x8==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
int****** mv, //!< motion vector array
int list_idx, //!< reference picture list
int ref, //!< reference index
int blocktype, //!< block type
int uv) //!< chroma component
{
int i, j, ii, jj, ii0, jj0, ii1, jj1, if0, if1, jf0, jf1;
int* mvb;
pel_t** refimage;
int f1_x = 64/img->mb_cr_size_x;
int f2_x=f1_x-1;
int f1_y = 64/img->mb_cr_size_y;
int f2_y=f1_y-1;
int f3=f1_x*f1_y, f4=f3>>1;
int list_offset;
int max_y_cr;
StorablePicture **list;
int curr_mb_field = ((img->MbaffFrameFlag)&&(img->mb_data[img->current_mb_nr].mb_field));
// find out the correct list offsets
if (curr_mb_field)
{
if(img->current_mb_nr%2)
list_offset = 4; // top field mb
else
list_offset = 2; // bottom field mb
max_y_cr = img->height_cr/2-1;
}
else
{
list_offset = 0; // no mb aff or frame mb
max_y_cr = img->height_cr-1;
}
list = listX[list_idx + list_offset];
refimage = list[ref]->imgUV[uv];
for (j=0; j<4; j++)
for (i=0; i<4; i++)
{
mvb = mv [(i+block_c_x)/(img->mb_cr_size_x/4)][(j+block_c_y)/(img->mb_cr_size_y/4)][list_idx][ref][blocktype];
ii = (i+block_c_x+img->opix_c_x)*f1_x + mvb[0];
jj = (j+block_c_y+img->opix_c_y)*f1_y + mvb[1];
jj += list[ref]->chroma_vector_adjustment;
ii0 = max (0, min (img->width_cr -1, ii/f1_x));
jj0 = max (0, min (max_y_cr, jj/f1_y));
ii1 = max (0, min (img->width_cr -1, (ii+f2_x)/f1_x));
jj1 = max (0, min (max_y_cr, (jj+f2_y)/f1_y));
if1 = (ii&f2_x); if0 = f1_x-if1;
jf1 = (jj&f2_y); jf0 = f1_y-jf1;
*mpred++ = (if0 * jf0 * refimage[jj0][ii0] +
if1 * jf0 * refimage[jj0][ii1] +
if0 * jf1 * refimage[jj1][ii0] +
if1 * jf1 * refimage[jj1][ii1] + f4) / f3;
}
}
/*!
************************************************************************
* \brief
* Predict an intra chroma 4x4 block
************************************************************************
*/
void IntraChromaPrediction4x4 (int uv, // <-- colour component
int block_x, // <-- relative horizontal block coordinate of 4x4 block
int block_y) // <-- relative vertical block coordinate of 4x4 block
{
int mode = img->mb_data[img->current_mb_nr].c_ipred_mode;
int i, j;
//===== prediction =====
for (j=block_y; j<block_y+4; j++)
for (i=block_x; i<block_x+4; i++)
{
img->mpr[i][j] = img->mprr_c[uv][mode][i][j];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -