📄 macroblock.c
字号:
************************************************************************
* \brief
* Predict (on-the-fly) one component of a chroma 4x4 block
************************************************************************
*/
void OneComponentChromaPrediction4x4_regenerate (
imgpel* 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
short ref, //!< reference index
int blocktype, //!< block type
int uv) //!< chroma component
{
int i, j, ii, jj, ii0, jj0, ii1, jj1, if0, if1, jf0, jf1;
short* mvb;
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 = img->mb_data[img->current_mb_nr].list_offset;
int max_y_cr = (int) (list_offset ? (img->height_cr >> 1) - 1 : img->height_cr - 1);
int max_x_cr = (int) (img->width_cr - 1);
int jjx, iix;
int mb_cr_y_div4 = img->mb_cr_size_y>>2;
int mb_cr_x_div4 = img->mb_cr_size_x>>2;
int jpos;
StorablePicture **list = listX[list_idx + list_offset];
imgpel** refimage = list[ref]->imgUV[uv];
for (j=block_c_y; j < block_c_y + BLOCK_SIZE; j++)
{
jjx = j/mb_cr_y_div4;
jpos = (j + img->opix_c_y)*f1_y;
for (i=block_c_x; i < block_c_x + BLOCK_SIZE; i++)
{
iix = i/mb_cr_x_div4;
mvb = mv [jjx][iix][list_idx][ref][blocktype];
ii = (i + img->opix_c_x)*f1_x + mvb[0];
jj = jpos + mvb[1];
if (active_sps->chroma_format_idc == 1)
jj += list[ref]->chroma_vector_adjustment;
ii0 = iClip3 (0, max_x_cr, ii/f1_x);
jj0 = iClip3 (0, max_y_cr, jj/f1_y);
ii1 = iClip3 (0, max_x_cr, (ii+f2_x)/f1_x);
jj1 = iClip3 (0, 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
* Retrieve one component of a chroma 4x4 block from the buffer
************************************************************************
*/
void OneComponentChromaPrediction4x4_retrieve (imgpel* 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
short ref, //!< reference index
int blocktype, //!< block type
int uv) //!< chroma component
{
int j, ii, jj;
short* mvb;
int list_offset = img->mb_data[img->current_mb_nr].list_offset;
int jjx;
int right_shift_x = 4 - chroma_shift_x;
int right_shift_y = 4 - chroma_shift_y;
int jpos;
int pos_x1 = block_c_x >> right_shift_x;
int pos_x2 = (block_c_x + 2) >> right_shift_x;
int ipos1 = ((block_c_x + img->opix_c_x) << chroma_shift_x) + IMG_PAD_SIZE_TIMES4;
int ipos2 = ((block_c_x + 2 + img->opix_c_x) << chroma_shift_x) + IMG_PAD_SIZE_TIMES4;
StorablePicture **list = listX[list_idx + list_offset];
imgpel**** refsubimage = list[ref]->imgUV_sub[uv];
imgpel *line_ptr;
int jj_chroma = ((active_sps->chroma_format_idc == 1) ? list[ref]->chroma_vector_adjustment : 0) + IMG_PAD_SIZE_TIMES4;
width_pad_cr = list[ref]->size_x_cr_pad;
height_pad_cr = list[ref]->size_y_cr_pad;
for (j=block_c_y; j < block_c_y + BLOCK_SIZE; j++)
{
jjx = j >> right_shift_y; // translate into absolute block (luma) coordinates
jpos = ( (j + img->opix_c_y) << chroma_shift_y ) + jj_chroma;
mvb = mv [jjx][pos_x1][list_idx][ref][blocktype];
ii = ipos1 + mvb[0];
jj = jpos + mvb[1];
line_ptr = UMVLine8X_chroma ( refsubimage, jj, ii);
*mpred++ = *line_ptr++;
*mpred++ = *line_ptr;
mvb = mv [jjx][pos_x2][list_idx][ref][blocktype];
ii = ipos2 + mvb[0];
jj = jpos + mvb[1];
line_ptr = UMVLine8X_chroma ( refsubimage, jj, ii);
*mpred++ = *line_ptr++;
*mpred++ = *line_ptr;
}
}
/*!
************************************************************************
* \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 j;
//===== prediction =====
for (j=block_y; j<block_y+4; j++)
memcpy(&img->mpr[j][block_x],&img->mprr_c[uv][mode][j][block_x], BLOCK_MULTIPLE * sizeof(imgpel));
}
/*!
************************************************************************
* \brief
* Predict one chroma 4x4 block
************************************************************************
*/
void ChromaPrediction4x4 ( 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 p_dir, // <-- prediction direction
int l0_mode, // <-- list0 prediction mode (1-7, 0=DIRECT if l1_mode=0)
int l1_mode, // <-- list1 prediction mode (1-7, 0=DIRECT if l0_mode=0)
short l0_ref_idx, // <-- reference frame for list0 prediction (if (<0) -> intra prediction)
short l1_ref_idx) // <-- reference frame for list1 prediction
{
static imgpel l0_pred[MB_BLOCK_SIZE];
static imgpel l1_pred[MB_BLOCK_SIZE];
int i, j;
int block_x4 = block_x+4;
int block_y4 = block_y+4;
imgpel* l0pred = l0_pred;
imgpel* l1pred = l1_pred;
short****** mv_array = img->all_mv;
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)));
if (currMB->bi_pred_me && l0_ref_idx == 0 && l1_ref_idx == 0 && p_dir == 2 && l0_mode==1 && l1_mode==1)
mv_array = currMB->bi_pred_me == 1? img->bipred_mv1 : img->bipred_mv2 ;
//===== INTRA PREDICTION =====
if (p_dir==-1)
{
IntraChromaPrediction4x4 (uv, block_x, block_y);
return;
}
//===== INTER PREDICTION =====
if ((p_dir==0) || (p_dir==2))
{
(*OneComponentChromaPrediction4x4) (l0_pred, block_x, block_y, mv_array, LIST_0, l0_ref_idx, l0_mode, uv);
}
if ((p_dir==1) || (p_dir==2))
{
(*OneComponentChromaPrediction4x4) (l1_pred, block_x, block_y, mv_array, LIST_1, l1_ref_idx, l1_mode, uv);
}
if (apply_weights)
{
if (p_dir==2)
{
int wbp0 = wbp_weight[0][l0_ref_idx][l1_ref_idx][uv+1];
int wbp1 = wbp_weight[1][l0_ref_idx][l1_ref_idx][uv+1];
int offset = (wp_offset[0][l0_ref_idx][uv+1] + wp_offset[1][l1_ref_idx][uv+1] + 1)>>1;
int wp_round = 2*wp_chroma_round;
int weight_denom = luma_log_weight_denom + 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_uv,
((wbp0 * *l0pred++ + wbp1 * *l1pred++ + wp_round) >> (weight_denom)) + (offset) );
}
else if (p_dir==0)
{
int wp = wp_weight[0][l0_ref_idx][uv+1];
int offset = wp_offset[0][l0_ref_idx][uv+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_uv, (( wp * *l0pred++ + wp_chroma_round) >> chroma_log_weight_denom) + offset);
}
else // (p_dir==1)
{
int wp = wp_weight[1][l1_ref_idx][uv+1];
int offset = wp_offset[1][l1_ref_idx][uv+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_uv, ((wp * *l1pred++ + wp_chroma_round) >> chroma_log_weight_denom) + offset);
}
}
else
{
if (p_dir==2)
{
for (j=block_y; j<block_y4; j++)
for (i=block_x; i<block_x4; i++)
img->mpr[j][i] = (*l0pred++ + *l1pred++ + 1) >> 1;
}
else if (p_dir==0)
{
for (j=block_y; j<block_y4; j++)
{
memcpy(&(img->mpr[j][block_x]), l0pred, BLOCK_SIZE * sizeof(imgpel));
l0pred += BLOCK_SIZE;
}
}
else // (p_dir==1)
{
for (j=block_y; j<block_y4; j++)
{
memcpy(&(img->mpr[j][block_x]), l1pred, BLOCK_SIZE * sizeof(imgpel));
l1pred += BLOCK_SIZE;
}
}
}
}
/*!
************************************************************************
* \brief
* Chroma residual coding for an macroblock
************************************************************************
*/
void ChromaResidualCoding (int* cr_cbp)
{
static const int block8x8_idx[3][4][4] = //ADD-VG-15052004
{
{ {0, 1, 0, 0},
{2, 3, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
},
{ {0, 1, 0, 0},
{0, 1, 0, 0},
{2, 3, 0, 0},
{2, 3, 0, 0},
},
{ {0, 0, 1, 1},
{0, 0, 1, 1},
{2, 2, 3, 3},
{2, 2, 3, 3}
}
};
int uv, block8, block_y, block_x, j, i;
int l0_mode, l1_mode;
short p_dir, refframe, l1_ref;
int skipped = (img->mb_data[img->current_mb_nr].mb_type == 0 && (img->type == P_SLICE || img->type == SP_SLICE));
int yuv = img->yuv_format - 1; //ADD-VG-15052004
if ( input->ChromaMCBuffer )
OneComponentChromaPrediction4x4 = OneComponentChromaPrediction4x4_retrieve;
else
OneComponentChromaPrediction4x4 = OneComponentChromaPrediction4x4_regenerate;
for (*cr_cbp=0, uv=0; uv<2; uv++)
{
//===== prediction of chrominance blocks ===d==
block8 = 0;
for (block_y=0; block_y < img->mb_cr_size_y; block_y+=4)
for (block_x=0; block_x < img->mb_cr_size_x; block_x+=4)
{
block8 = block8x8_idx[yuv][block_y>>2][block_x>>2];
SetModesAndRefframe (block8, &p_dir, &l0_mode, &l1_mode, &refframe, &l1_ref);
ChromaPrediction4x4 (uv, block_x, block_y, p_dir, l0_mode, l1_mode, refframe, l1_ref);
}
// ==== set chroma residue to zero for skip Mode in SP frames
if (img->NoResidueDirect)
{
for (j=0; j<img->mb_cr_size_y; j++)
memcpy(&enc_picture->imgUV[uv][img->pix_c_y+j][img->pix_c_x], img->mpr[j], img->mb_cr_size_x * sizeof(imgpel));
}
else if (skipped && img->type==SP_SLICE)
{
for (j=0; j<8; j++)
memset(img->m7[j], 0 , 8 * sizeof(int));
}
else
if (skipped)
{
for (j=0; j<img->mb_cr_size_y; j++)
memcpy(&enc_picture->imgUV[uv][img->pix_c_y+j][img->pix_c_x], img->mpr[j], img->mb_cr_size_x * sizeof(imgpel));
}
else
{
for (j=0; j<img->mb_cr_size_y; j++)
for (i=0; i<img->mb_cr_size_x; i++)
{
img->m7[j][i] = imgUV_org[uv][img->opix_c_y+j][img->opix_c_x+i] - img->mpr[j][i];
}
}
//===== DCT, Quantization, inverse Quantization, IDCT, and Reconstruction =====
//===== Call function for skip mode in SP frames to properly process frame ====
if (skipped && img->type==SP_SLICE)
{
if(si_frame_indicator || sp2_frame_indicator)
*cr_cbp=dct_chroma_sp2(uv,*cr_cbp);//modif ES added, calls the SI/switching SP encoding function
else
*cr_cbp=dct_chroma_sp(uv,*cr_cbp);
}
else
{
if (!img->NoResidueDirect && !skipped)
{
if (img->type!=SP_SLICE || (img->mb_data[img->current_mb_nr].mb_type==I16MB ))
{
//even if the block is intra it should still be treated as SP
*cr_cbp=dct_chroma (uv,*cr_cbp);
}
else
{
if(si_frame_indicator||sp2_frame_indicator)
*cr_cbp=dct_chroma_sp2(uv,*cr_cbp);// SI frames or switching SP frames
else
*cr_cbp=dct_chroma_sp(uv,*cr_cbp);
}
}
}
}
//===== update currMB->cbp =====
img->mb_data[img->curre
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -