📄 macroblock.c
字号:
************************************************************************
* \brief
* Read motion info
************************************************************************
*/
void readMotionInfoFromNAL (struct img_par *img, struct inp_par *inp)
{
int i,j,k;
int step_h,step_v;
int curr_mvd;
Macroblock *currMB = &img->mb_data[img->current_mb_nr];
SyntaxElement currSE;
Slice *currSlice = img->currentSlice;
DataPartition *dP;
int *partMap = assignSE2partition[currSlice->dp_mode];
int bframe = (img->type==B_SLICE);
int partmode = (IS_P8x8(currMB)?4:currMB->mb_type);
int step_h0 = BLOCK_STEP [partmode][0];
int step_v0 = BLOCK_STEP [partmode][1];
int mv_mode, i0, j0;
char refframe;
short pmv[2];
int j4, i4, ii,jj;
int motion_vector;
int mv_scale = 0;
int flag_mode;
int list_offset = ((img->MbaffFrameFlag)&&(currMB->mb_field))? img->current_mb_nr%2 ? 4 : 2 : 0;
byte ** moving_block;
short **** co_located_mv;
char *** co_located_ref_idx;
int64 *** co_located_ref_id;
if ((img->MbaffFrameFlag)&&(currMB->mb_field))
{
if(img->current_mb_nr%2)
{
moving_block = Co_located->bottom_moving_block;
co_located_mv = Co_located->bottom_mv;
co_located_ref_idx = Co_located->bottom_ref_idx;
co_located_ref_id = Co_located->bottom_ref_pic_id;
}
else
{
moving_block = Co_located->top_moving_block;
co_located_mv = Co_located->top_mv;
co_located_ref_idx = Co_located->top_ref_idx;
co_located_ref_id = Co_located->top_ref_pic_id;
}
}
else
{
moving_block = Co_located->moving_block;
co_located_mv = Co_located->mv;
co_located_ref_idx = Co_located->ref_idx;
co_located_ref_id = Co_located->ref_pic_id;
}
if (bframe && IS_P8x8 (currMB))
{
if (img->direct_spatial_mv_pred_flag)
{
int imgblock_y= ((img->MbaffFrameFlag)&&(currMB->mb_field))? (img->current_mb_nr%2) ? (img->block_y-4)/2:img->block_y/2: img->block_y;
int fw_rFrameL, fw_rFrameU, fw_rFrameUL, fw_rFrameUR;
int bw_rFrameL, bw_rFrameU, bw_rFrameUL, bw_rFrameUR;
PixelPos mb_left, mb_up, mb_upleft, mb_upright;
char fw_rFrame,bw_rFrame;
short pmvfw[2]={0,0},
pmvbw[2]={0,0};
getLuma4x4Neighbour(img->current_mb_nr, 0, 0, -1, 0, &mb_left);
getLuma4x4Neighbour(img->current_mb_nr, 0, 0, 0, -1, &mb_up);
getLuma4x4Neighbour(img->current_mb_nr, 0, 0, 16, -1, &mb_upright);
getLuma4x4Neighbour(img->current_mb_nr, 0, 0, -1, -1, &mb_upleft);
if (!img->MbaffFrameFlag)
{
fw_rFrameL = mb_left.available ? dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x] : -1;
fw_rFrameU = mb_up.available ? dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x] : -1;
fw_rFrameUL = mb_upleft.available ? dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x] : -1;
fw_rFrameUR = mb_upright.available ? dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x] : fw_rFrameUL;
bw_rFrameL = mb_left.available ? dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x] : -1;
bw_rFrameU = mb_up.available ? dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x] : -1;
bw_rFrameUL = mb_upleft.available ? dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x] : -1;
bw_rFrameUR = mb_upright.available ? dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x] : bw_rFrameUL;
}
else
{
if (img->mb_data[img->current_mb_nr].mb_field)
{
fw_rFrameL = mb_left.available ?
img->mb_data[mb_left.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x] < 0?
dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x] :
dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x] * 2: -1;
fw_rFrameU = mb_up.available ?
img->mb_data[mb_up.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x] < 0?
dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x] :
dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x] * 2: -1;
fw_rFrameUL = mb_upleft.available ?
img->mb_data[mb_upleft.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x] < 0?
dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x] :
dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x] *2: -1;
fw_rFrameUR = mb_upright.available ?
img->mb_data[mb_upright.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x] < 0 ?
dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x] :
dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x] * 2: fw_rFrameUL;
bw_rFrameL = mb_left.available ?
img->mb_data[mb_left.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x] < 0 ?
dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x] :
dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x] * 2: -1;
bw_rFrameU = mb_up.available ?
img->mb_data[mb_up.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x] < 0 ?
dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x] :
dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x] * 2: -1;
bw_rFrameUL = mb_upleft.available ?
img->mb_data[mb_upleft.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x] < 0 ?
dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x] :
dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x] *2: -1;
bw_rFrameUR = mb_upright.available ?
img->mb_data[mb_upright.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x] < 0 ?
dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x] :
dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x] * 2: bw_rFrameUL;
}
else
{
fw_rFrameL = mb_left.available ?
img->mb_data[mb_left.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x] < 0 ?
dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x] >> 1 :
dec_picture->ref_idx[LIST_0][mb_left.pos_y][mb_left.pos_x]: -1;
fw_rFrameU = mb_up.available ?
img->mb_data[mb_up.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x] < 0 ?
dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x] >> 1 :
dec_picture->ref_idx[LIST_0][mb_up.pos_y][mb_up.pos_x] : -1;
fw_rFrameUL = mb_upleft.available ?
img->mb_data[mb_upleft.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x] < 0 ?
dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x]>> 1 :
dec_picture->ref_idx[LIST_0][mb_upleft.pos_y][mb_upleft.pos_x] : -1;
fw_rFrameUR = mb_upright.available ?
img->mb_data[mb_upright.mb_addr].mb_field || dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x] < 0 ?
dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x] >> 1 :
dec_picture->ref_idx[LIST_0][mb_upright.pos_y][mb_upright.pos_x] : fw_rFrameUL;
bw_rFrameL = mb_left.available ?
img->mb_data[mb_left.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x] < 0 ?
dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x] >> 1 :
dec_picture->ref_idx[LIST_1][mb_left.pos_y][mb_left.pos_x] : -1;
bw_rFrameU = mb_up.available ?
img->mb_data[mb_up.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x] < 0 ?
dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x] >> 1 :
dec_picture->ref_idx[LIST_1][mb_up.pos_y][mb_up.pos_x] : -1;
bw_rFrameUL = mb_upleft.available ?
img->mb_data[mb_upleft.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x] < 0 ?
dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x] >> 1 :
dec_picture->ref_idx[LIST_1][mb_upleft.pos_y][mb_upleft.pos_x] : -1;
bw_rFrameUR = mb_upright.available ?
img->mb_data[mb_upright.mb_addr].mb_field || dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x] < 0 ?
dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x] >> 1:
dec_picture->ref_idx[LIST_1][mb_upright.pos_y][mb_upright.pos_x] : bw_rFrameUL;
}
}
fw_rFrame = (fw_rFrameL >= 0 && fw_rFrameU >= 0) ? min(fw_rFrameL,fw_rFrameU): max(fw_rFrameL,fw_rFrameU);
fw_rFrame = (fw_rFrame >= 0 && fw_rFrameUR >= 0) ? min(fw_rFrame,fw_rFrameUR): max(fw_rFrame,fw_rFrameUR);
bw_rFrame = (bw_rFrameL >= 0 && bw_rFrameU >= 0) ? min(bw_rFrameL,bw_rFrameU): max(bw_rFrameL,bw_rFrameU);
bw_rFrame = (bw_rFrame >= 0 && bw_rFrameUR >= 0) ? min(bw_rFrame,bw_rFrameUR): max(bw_rFrame,bw_rFrameUR);
if (fw_rFrame >=0)
SetMotionVectorPredictor (img, pmvfw, pmvfw+1, fw_rFrame, LIST_0, dec_picture->ref_idx, dec_picture->mv, 0, 0, 16, 16);
if (bw_rFrame >=0)
SetMotionVectorPredictor (img, pmvbw, pmvbw+1, bw_rFrame, LIST_1, dec_picture->ref_idx, dec_picture->mv, 0, 0, 16, 16);
for (i=0;i<4;i++)
{
if (currMB->b8mode[i] == 0)
for(j=2*(i/2);j<2*(i/2)+2;j++)
for(k=2*(i%2);k<2*(i%2)+2;k++)
{
int j6 = imgblock_y+j;
j4 = img->block_y+j;
i4 = img->block_x+k;
if (fw_rFrame >= 0)
{
if (!fw_rFrame && ((!moving_block[j6][i4]) && (!listX[1+list_offset][0]->is_long_term)))
{
dec_picture->mv [LIST_0][j4][i4][0] = 0;
dec_picture->mv [LIST_0][j4][i4][1] = 0;
dec_picture->ref_idx[LIST_0][j4][i4] = 0;
}
else
{
dec_picture->mv [LIST_0][j4][i4][0] = pmvfw[0];
dec_picture->mv [LIST_0][j4][i4][1] = pmvfw[1];
dec_picture->ref_idx[LIST_0][j4][i4] = fw_rFrame;
}
}
else
{
dec_picture->mv [LIST_0][j4][i4][0] = 0;
dec_picture->mv [LIST_0][j4][i4][1] = 0;
dec_picture->ref_idx[LIST_0][j4][i4] = -1;
}
if (bw_rFrame >= 0)
{
if (bw_rFrame==0 && ((!moving_block[j6][i4])&& (!listX[1+list_offset][0]->is_long_term)))
{
dec_picture->mv [LIST_1][j4][i4][0] = 0;
dec_picture->mv [LIST_1][j4][i4][1] = 0;
dec_picture->ref_idx[LIST_1][j4][i4] = 0;
}
else
{
dec_picture->mv [LIST_1][j4][i4][0] = pmvbw[0];
dec_picture->mv [LIST_1][j4][i4][1] = pmvbw[1];
dec_picture->ref_idx[LIST_1][j4][i4] = bw_rFrame;
}
}
else
{
dec_picture->mv [LIST_1][j4][i4][0] = 0;
dec_picture->mv [LIST_1][j4][i4][1] = 0;
dec_picture->ref_idx[LIST_1][j4][i4] = -1;
}
if (fw_rFrame <0 && bw_rFrame <0)
{
dec_picture->ref_idx[LIST_0][j4][i4] = 0;
dec_picture->ref_idx[LIST_1][j4][i4] = 0;
}
}
}
}
else
{
for (i=0;i<4;i++)
{
if (currMB->b8mode[i] == 0)
{
for(j=2*(i/2);j<2*(i/2)+2;j++)
{
for(k=2*(i%2);k<2*(i%2)+2;k++)
{
int list_offset = ((img->MbaffFrameFlag)&&(currMB->mb_field))? img->current_mb_nr%2 ? 4 : 2 : 0;
int imgblock_y= ((img->MbaffFrameFlag)&&(currMB->mb_field))? (img->current_mb_nr%2) ? (img->block_y-4)/2 : img->block_y/2 : img->block_y;
int refList = co_located_ref_idx[LIST_0 ][imgblock_y+j][img->block_x+k]== -1 ? LIST_1 : LIST_0;
int ref_idx = co_located_ref_idx[refList][imgblock_y + j][img->block_x + k];
int mapped_idx=-1, iref;
if (ref_idx == -1)
{
dec_picture->ref_idx [LIST_0][img->block_y + j][img->block_x + k] = 0;
dec_picture->ref_idx [LIST_1][img->block_y + j][img->block_x + k] = 0;
}
else
{
for (iref=0;iref<min(img->num_ref_idx_l0_active,listXsize[LIST_0 + list_offset]);iref++)
{
#if 1
int curr_mb_field = ((img->MbaffFrameFlag)&&(currMB->mb_field));
if(img->structure==0 && curr_mb_field==0)
{
// If the current MB is a frame MB and the colocated is from a field picture,
// then the co_located_ref_id may have been generated from the wrong value of
// frame_poc if it references it's complementary field, so test both POC values
if(listX[0][iref]->top_poc*2 == co_located_ref_id[refList][imgblock_y + j][img->block_x + k]
|| listX[0][iref]->bottom_poc*2 == co_located_ref_id[refList][imgblock_y + j][img->block_x + k])
{
mapped_idx=iref;
break;
}
else //! invalid index. Default to zero even though this case should not happen
mapped_idx=INVALIDINDEX;
continue;
}
#endif
if (dec_picture->ref_pic_num[img->current_slice_nr][LIST_0 + list_offset][iref]==co_located_ref_id[refList][imgblock_y + j][img->block_x + k])
{
mapped_idx=iref;
break;
}
else //! invalid index. Default to zero even though this case should not happen
mapped_idx=INVALIDINDEX;
}
if (INVALIDINDEX == mapped_idx)
{
error("temporal direct error\ncolocated block has ref that is unavailable",-1111);
}
dec_picture->ref_idx [LIST_0][img->block_y + j][img->block_x + k] = mapped_idx;
dec_picture->ref_idx [LIST_1][img->block_y + j][img->block_x + k] = 0;
}
}
}
}
}
}
}
// If multiple ref. frames, read reference frame for the MB *********************************
if(img->num_ref_idx_l0_active>1)
{
flag_mode = ( img->num_ref_idx_l0_active == 2 ? 1 : 0);
currSE.type = SE_REFFRAME;
dP = &(currSlice->partArr[partMap[SE_REFFRAME]]);
if (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag) currSE.mapping = linfo_ue;
else currSE.reading = readRefFrame_CABAC;
for (j0=0; j0<4; j0+=step_v0)
{
for (i0=0; i0<4; i0+=step_h0)
{
k=2*(j0/2)+(i0/2);
if ((currMB->b8pdir[k]==0 || currMB->b8pdir[k]==2) && currMB->b8mode[k]!=0)
{
TRACE_STRING("ref_idx_l0");
img->subblock_x = i0;
img->subblock_y = j0;
if (!IS_P8x8 (currMB) || bframe || (!bframe && !img->allrefzero))
{
currSE.context = BType2CtxRef (currMB->b8mode[k]);
if( (active_pps->entropy_coding_mode_flag == UVLC || dP->bitstream->ei_flag) && flag_mode )
{
currSE.len = 1;
readSyntaxElement_FLC(&currSE, dP->bitstream);
currSE.value1 = 1 - currSE.value1;
}
else
{
currSE.value2 = LIST_0;
dP->readSyntaxElement (&currSE,img,inp,dP);
}
refframe = currSE.value1;
}
else
{
refframe = 0;
}
/*
if (bframe && refframe>img->buf_cycle) // img->buf_cycle should be correct for field MBs now
{
set_ec_flag(SE_REFFRAME);
refframe = 1;
}
*/
for (j=j0; j<j0+step_v0;j++)
for (i=i0; i<i0+step_h0;i++)
{
dec_picture->ref_idx[LIST_0][img->block_y + j][img->block_x + i] = refframe;
}
}
}
}
}
else
{
for (j0=0; j0<4; j0+=step_v0)
{
for (i0=0; i0<4; i0+=step_h0)
{
k=2*(j0/2)+(i0/2);
if ((currMB->b8pdir[k]==0 || currMB->b8pdir[k]==2) && currMB->b8mode[k]!=0)
{
for (j=j0; j<j0+step_v0;j++)
for (i=i0; i<i0+step_h0;i++)
{
dec_picture->ref_idx[LIST_0][img->block_y + j][img->block_x + i] = 0;
}
}
}
}
}
// If backward multiple ref. frames, read backward reference frame for the MB ******************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -