📄 macroblock.c
字号:
//! UVLC
if (input->symbol_mode == UVLC)
{
for (i=0; i<currSlice->max_part_nr; i++)
{
dataPart = &(currSlice->partArr[i]);
currStream = dataPart->bitstream;
size_in_bytes = currStream->byte_pos /*- currStream->tmp_byte_pos*/;
if (currStream->bits_to_go < 8)
size_in_bytes++;
if (currStream->bits_to_go < rlc_bits)
size_in_bytes++;
if(size_in_bytes > input->slice_argument)
return TRUE;
}
}
//! CABAC
if (input->symbol_mode ==CABAC)
{
for (i=0; i<currSlice->max_part_nr; i++)
{
dataPart= &(currSlice->partArr[i]);
eep = &(dataPart->ee_cabac);
if( arienco_bits_written(eep) > (input->slice_argument*8))
return TRUE;
}
}
return FALSE;
}
/*!
************************************************************************
* \brief
* Predict one component of a 4x4 Luma block
************************************************************************
*/
void
OneComponentLumaPrediction4x4 (int* mpred, // --> array of prediction values (row by row)
int pic_pix_x, // <-- absolute horizontal coordinate of 4x4 block
int pic_pix_y, // <-- absolute vertical coordinate of 4x4 block
int* mv, // <-- motion vector
int ref, // <-- reference frame
StorablePicture **list)
{
pel_t** ref_pic;
int pix_add = 4;
int j0 = (pic_pix_y << 2) + mv[1], j1=j0+pix_add, j2=j1+pix_add, j3=j2+pix_add;
int i0 = (pic_pix_x << 2) + mv[0], i1=i0+pix_add, i2=i1+pix_add, i3=i2+pix_add;
pel_t (*get_pel) (pel_t**, int, int, int, int) = UMVPelY_14;
int img_width =list[ref]->size_x;
int img_height=list[ref]->size_y;
ref_pic = list[ref]->imgY_ups;
*mpred++ = get_pel (ref_pic, j0, i0, img_height, img_width);
*mpred++ = get_pel (ref_pic, j0, i1, img_height, img_width);
*mpred++ = get_pel (ref_pic, j0, i2, img_height, img_width);
*mpred++ = get_pel (ref_pic, j0, i3, img_height, img_width);
*mpred++ = get_pel (ref_pic, j1, i0, img_height, img_width);
*mpred++ = get_pel (ref_pic, j1, i1, img_height, img_width);
*mpred++ = get_pel (ref_pic, j1, i2, img_height, img_width);
*mpred++ = get_pel (ref_pic, j1, i3, img_height, img_width);
*mpred++ = get_pel (ref_pic, j2, i0, img_height, img_width);
*mpred++ = get_pel (ref_pic, j2, i1, img_height, img_width);
*mpred++ = get_pel (ref_pic, j2, i2, img_height, img_width);
*mpred++ = get_pel (ref_pic, j2, i3, img_height, img_width);
*mpred++ = get_pel (ref_pic, j3, i0, img_height, img_width);
*mpred++ = get_pel (ref_pic, j3, i1, img_height, img_width);
*mpred++ = get_pel (ref_pic, j3, i2, img_height, img_width);
*mpred++ = get_pel (ref_pic, j3, i3, img_height, img_width);
}
/*!
************************************************************************
* \brief
* copy foward/backward prediction values of one component of a 4x4 Luma block
************************************************************************
*/
void
copyblock4x4 (int* mpred, // --> array of prediction values (row by row)
int block[BLOCK_SIZE][BLOCK_SIZE])
{
*mpred++ = block[0][0];
*mpred++ = block[1][0];
*mpred++ = block[2][0];
*mpred++ = block[3][0];
*mpred++ = block[0][1];
*mpred++ = block[1][1];
*mpred++ = block[2][1];
*mpred++ = block[3][1];
*mpred++ = block[0][2];
*mpred++ = block[1][2];
*mpred++ = block[2][2];
*mpred++ = block[3][2];
*mpred++ = block[0][3];
*mpred++ = block[1][3];
*mpred++ = block[2][3];
*mpred++ = block[3][3];
}
/*!
************************************************************************
* \brief
* Predict one 4x4 Luma block
************************************************************************
*/
void
LumaPrediction4x4 (int block_x, // <-- relative horizontal block coordinate of 4x4 block
int block_y, // <-- relative vertical block coordinate of 4x4 block
int p_dir, // <-- prediction direction (0=forward, 1=backward, 2=bidir)
int fw_mode, // <-- forward prediction mode (1-7, 0=DIRECT if bw_mode=0)
int bw_mode, // <-- backward prediction mode (1-7, 0=DIRECT if fw_mode=0)
int fw_ref_idx, // <-- reference frame for forward prediction (-1: Intra4x4 pred. with fw_mode)
int bw_ref_idx )
{
static int fw_pred[16];
static int bw_pred[16];
int i, j;
int block_x4 = block_x+4;
int block_y4 = block_y+4;
int pic_opix_x = img->opix_x + block_x;
int pic_opix_y = img->opix_y + block_y;
int bx = block_x >> 2;
int by = block_y >> 2;
int* fpred = fw_pred;
int* bpred = bw_pred;
// int direct = (fw_mode == 0 && bw_mode == 0 && (img->type == B_SLICE));
// int skipped = (fw_mode == 0 && bw_mode == 0 && (img->type != B_SLICE));
// int apply_weights = ( (input->WeightedPrediction && (img->type == P_SLICE || img->type == SP_SLICE)) ||
// (input->WeightedBiprediction && (img->type ==B_SLICE)));
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)));
int list_offset = ((img->MbaffFrameFlag)&&(img->mb_data[img->current_mb_nr].mb_field))? img->current_mb_nr%2 ? 4 : 2 : 0;
if ((p_dir==0)||(p_dir==2))
{
OneComponentLumaPrediction4x4 (fw_pred, pic_opix_x, pic_opix_y, img->all_mv[bx][by][LIST_0][fw_ref_idx][fw_mode], fw_ref_idx, listX[0+list_offset]);
}
if ((p_dir==1)||(p_dir==2))
{
OneComponentLumaPrediction4x4 (bw_pred, pic_opix_x, pic_opix_y, img->all_mv[bx][by][LIST_1][bw_ref_idx][bw_mode], bw_ref_idx, listX[1+list_offset]);
}
if (apply_weights)
{
if (p_dir==2)
{
for (j=block_y; j<block_y4; j++)
for (i=block_x; i<block_x4; i++)
img->mpr[i][j] = clip1a(((wbp_weight[0][fw_ref_idx][bw_ref_idx][0] * *fpred++ +
wbp_weight[1][fw_ref_idx][bw_ref_idx][0] * *bpred++ +
2*wp_luma_round) >> (luma_log_weight_denom + 1)) +
((wp_offset[0][fw_ref_idx][0] + wp_offset[1][bw_ref_idx][0] + 1)>>1));
}
else if (p_dir==0)
{
for (j=block_y; j<block_y4; j++)
for (i=block_x; i<block_x4; i++)
img->mpr[i][j] = clip1a(((wp_weight[0][fw_ref_idx][0] * *fpred++ + wp_luma_round) >> luma_log_weight_denom) +
+ wp_offset[0][fw_ref_idx][0] );
}
else // p_dir==1
{
for (j=block_y; j<block_y4; j++)
for (i=block_x; i<block_x4; i++)
img->mpr[i][j] = clip1a(((wp_weight[1][bw_ref_idx][0] * *bpred++ + wp_luma_round) >> luma_log_weight_denom) +
wp_offset[1][bw_ref_idx][0] );
}
}
else
{
if (p_dir==2)
{
for (j=block_y; j<block_y4; j++)
for (i=block_x; i<block_x4; i++)
img->mpr[i][j] = (*fpred++ + *bpred++ + 1) / 2;
}
else if (p_dir==0)
{
for (j=block_y; j<block_y4; j++)
for (i=block_x; i<block_x4; i++) img->mpr[i][j] = *fpred++;
}
else // p_dir==1
{
for (j=block_y; j<block_y4; j++)
for (i=block_x; i<block_x4; i++) img->mpr[i][j] = *bpred++;
}
}
}
/*!
************************************************************************
* \brief
* Residual Coding of an 8x8 Luma block (not for intra)
************************************************************************
*/
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 p_dir, // <-- prediction direction
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, nonzero, 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
int skipped = (fw_mode == 0 && bw_mode == 0 && (img->type != B_SLICE));
if (img->type==B_SLICE)
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 = 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, fw_mode, bw_mode, fw_refframe, bw_refframe);
//===== get displaced frame difference ======
for (j=0; j<4; j++)
for (i=0; i<4; i++)
{
img->m7[i][j] = imgY_org[pic_pix_y+j][pic_pix_x+i] - img->mpr[i+block_x][j+block_y];
}
//===== DCT, Quantization, inverse Quantization, IDCT, Reconstruction =====
if (img->NoResidueDirect != 1 && !skipped )
{
//===== DCT, Quantization, inverse Quantization, IDCT, Reconstruction =====
if (img->type!=SP_SLICE) nonzero = dct_luma (block_x, block_y, &coeff_cost, 0);
else nonzero = dct_luma_sp(block_x, block_y, &coeff_cost);
if (nonzero)
{
(*cbp_blk) |= 1 << cbp_blk_mask; // one bit for every 4x4 block
(*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_)
{
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++)
{
enc_picture->imgY[img->pix_y+j][img->pix_x+i] = img->mpr[i][j];
}
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 ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -