📄 rdopt.c
字号:
SyntaxElement se;
Slice *currSlice = img->currentSlice;
DataPartition *dataPart;
const int *partMap = assignSE2partition[input->partition_mode];
EncodingEnvironmentPtr eep_dp;
//=====
//===== GET COEFFICIENTS, RECONSTRUCTIONS, CBP
//=====
currMB->bi_pred_me=0;
if (direct)
{
if (direct_pdir[img->block_y+j0][img->block_x+i0]<0) // mode not allowed
return (1e20);
else
*cnt_nonz = LumaResidualCoding8x8 (&cbp, cbp_blk, block, direct_pdir[img->block_y+j0][img->block_x+i0], 0, 0,
(short)imax(0,direct_ref_idx[LIST_0][img->block_y+j0][img->block_x+i0]),
direct_ref_idx[LIST_1][img->block_y+j0][img->block_x+i0]);
}
else
{
if (pdir == 2 && active_pps->weighted_bipred_idc == 1)
{
int weight_sum = (active_pps->weighted_bipred_idc == 1)? wbp_weight[0][l0_ref][l1_ref][0] + wbp_weight[1][l0_ref][l1_ref][0] : 0;
if (weight_sum < -128 || weight_sum > 127)
{
return (1e20);
}
}
fw_mode = (pdir==0||pdir==2 ? mode : 0);
bw_mode = (pdir==1||pdir==2 ? mode : 0);
*cnt_nonz = LumaResidualCoding8x8 (&cbp, cbp_blk, block, pdir, fw_mode, bw_mode, l0_ref, l1_ref);
}
//===== get residue =====
if (input->rdopt==3 && img->type!=B_SLICE)
{
// We need the reconstructed prediction residue for the simulated decoders.
compute_residue_b8block (block, -1);
}
//=====
//===== GET DISTORTION
//=====
if (input->rdopt==3 && img->type!=B_SLICE)
{
for (k=0; k<input->NoOfDecoders ;k++)
{
decode_one_b8block (k, P8x8, block, mode, l0_ref);
for (j=img->opix_y+pay; j<img->opix_y+pay+8; j++)
for (i=img->opix_x+pax; i<img->opix_x+pax+8; i++)
{
distortion += iabs2( imgY_org[j][i] - decs->decY[k][j][i]);
}
}
distortion /= input->NoOfDecoders;
}
else
{
for (j=pay; j<pay+8; j++)
for (i=img->pix_x+pax; i<img->pix_x+pax+8; i++)
{
distortion += iabs2( imgY_org[img->opix_y+j][i] - enc_picture->imgY[img->pix_y+j][i]);
}
}
//=====
//===== GET RATE
//=====
//----- block 8x8 mode -----
if (input->symbol_mode == UVLC)
{
ue_linfo (b8value, dummy, &mrate, &dummy);
rate += mrate;
}
else
{
se.value1 = b8value;
se.type = SE_MBTYPE;
dataPart = &(currSlice->partArr[partMap[se.type]]);
writeB8_typeInfo(&se, dataPart);
rate += se.len;
}
//----- motion information -----
if (!direct)
{
if ((img->num_ref_idx_l0_active > 1 ) && (pdir==0 || pdir==2))
rate += writeReferenceFrame (mode, i0, j0, 1, l0_ref);
if(img->num_ref_idx_l1_active > 1 && img->type== B_SLICE)
{
if (pdir==1 || pdir==2)
{
rate += writeReferenceFrame (mode, i0, j0, 0, l1_ref);
}
}
if (pdir==0 || pdir==2)
{
rate += writeMotionVector8x8 (i0, j0, i0+2, j0+2, l0_ref,LIST_0, mode);
}
if (pdir==1 || pdir==2)
{
rate += writeMotionVector8x8 (i0, j0, i0+2, j0+2, l1_ref, LIST_1, mode);
}
}
//----- coded block pattern (for CABAC only) -----
if (input->symbol_mode == CABAC)
{
dataPart = &(currSlice->partArr[partMap[SE_CBP]]);
eep_dp = &(dataPart->ee_cabac);
mrate = arienco_bits_written (eep_dp);
writeCBP_BIT_CABAC (block, ((*cnt_nonz>0)?1:0), cbp8x8, currMB, 1, eep_dp);
mrate = arienco_bits_written (eep_dp) - mrate;
rate += mrate;
}
//----- luminance coefficients -----
if (*cnt_nonz)
{
rate += writeLumaCoeff8x8 (block, mode, currMB->luma_transform_size_8x8_flag);
}
return (double)distortion + lambda * (double)rate;
}
/*!
*************************************************************************************
* \brief
* Gets mode offset for intra16x16 mode
*************************************************************************************
*/
int I16Offset (int cbp, int i16mode)
{
return (cbp&15?13:1) + i16mode + ((cbp&0x30)>>2);
}
/*!
*************************************************************************************
* \brief
* Sets modes and reference frames for a macroblock
*************************************************************************************
*/
void SetModesAndRefframeForBlocks (int mode)
{
int i,j,k,l;
Macroblock *currMB = &img->mb_data[img->current_mb_nr];
int bframe = (img->type==B_SLICE);
int block_x, block_y;
int cur_ref[2];
//--- macroblock type ---
currMB->mb_type = mode;
currMB->bi_pred_me= (mode == 1 ? img->bi_pred_me[mode] : 0);
//--- block 8x8 mode and prediction direction ---
switch (mode)
{
case 0:
for(i=0;i<4;i++)
{
currMB->b8mode[i] = 0;
currMB->b8pdir[i] = (bframe ? direct_pdir[img->block_y + (i >> 1)*2][img->block_x + (i & 0x01)*2] : 0);
}
break;
case 1:
case 2:
case 3:
for(i=0;i<4;i++)
{
currMB->b8mode[i] = mode;
currMB->b8pdir[i] = best8x8pdir[mode][i];
}
break;
case P8x8:
for(i=0;i<4;i++)
{
currMB->b8mode[i] = best8x8mode[i];
currMB->b8pdir[i] = best8x8pdir[mode][i];
}
break;
case I4MB:
for(i=0;i<4;i++)
{
currMB->b8mode[i] = IBLOCK;
currMB->b8pdir[i] = -1;
}
break;
case I16MB:
for(i=0;i<4;i++)
{
currMB->b8mode[i] = 0;
currMB->b8pdir[i] = -1;
}
break;
case I8MB:
for(i=0;i<4;i++)
{
currMB->b8mode[i] = I8MB;
currMB->b8pdir[i] = -1;
}
//switch to 8x8 transform
currMB->luma_transform_size_8x8_flag = 1;
break;
case IPCM:
for(i=0;i<4;i++)
{
currMB->b8mode[i] = IPCM;
currMB->b8pdir[i] = -1;
}
currMB->luma_transform_size_8x8_flag = 0;
break;
default:
printf ("Unsupported mode in SetModesAndRefframeForBlocks!\n");
exit (1);
}
#define IS_FW ((best8x8pdir[mode][k]==0 || best8x8pdir[mode][k]==2) && (mode!=P8x8 || best8x8mode[k]!=0 || !bframe))
#define IS_BW ((best8x8pdir[mode][k]==1 || best8x8pdir[mode][k]==2) && (mode!=P8x8 || best8x8mode[k]!=0))
//--- reference frame arrays ---
if (mode==0 || mode==I4MB || mode==I16MB || mode==I8MB)
{
if (bframe)
{
if (!mode)
{
for (j = img->block_y; j < img->block_y + 4; j++)
{
memcpy(&enc_picture->ref_idx[LIST_0][j][img->block_x],&direct_ref_idx[LIST_0][j][img->block_x], 4 * sizeof(char));
memcpy(&enc_picture->ref_idx[LIST_1][j][img->block_x],&direct_ref_idx[LIST_1][j][img->block_x], 4 * sizeof(char));
}
}
else
{
for (j = img->block_y; j < img->block_y + 4; j++)
{
memset(&enc_picture->ref_idx[LIST_0][j][img->block_x],-1, 4 * sizeof(char));
memset(&enc_picture->ref_idx[LIST_1][j][img->block_x],-1, 4 * sizeof(char));
}
}
}
else
{
if (!mode)
{
for (j = img->block_y; j < img->block_y + 4; j++)
memset(&enc_picture->ref_idx[LIST_0][j][img->block_x],0, 4 * sizeof(char));
}
else
{
for (j = img->block_y; j < img->block_y + 4; j++)
memset(&enc_picture->ref_idx[LIST_0][j][img->block_x],-1, 4 * sizeof(char));
}
}
}
else
{
if (bframe)
{
for (j=0;j<4;j++)
{
block_y = img->block_y + j;
for (i=0;i<4;i++)
{
block_x = img->block_x + i;
k = 2*(j >> 1) + (i >> 1);
l = 2*(j & 0x01) + (i & 0x01);
if(mode == P8x8 && best8x8mode[k]==0)
{
enc_picture->ref_idx[LIST_0][block_y][block_x] = direct_ref_idx[LIST_0][block_y][block_x];
enc_picture->ref_idx[LIST_1][block_y][block_x] = direct_ref_idx[LIST_1][block_y][block_x];
}
else if (mode ==1 && currMB->bi_pred_me && IS_FW && IS_BW)
{
enc_picture->ref_idx[LIST_0][block_y][block_x] = 0;
enc_picture->ref_idx[LIST_1][block_y][block_x] = 0;
}
else
{
enc_picture->ref_idx[LIST_0][block_y][block_x] = (IS_FW ? best8x8l0ref[mode][k] : -1);
enc_picture->ref_idx[LIST_1][block_y][block_x] = (IS_BW ? best8x8l1ref[mode][k] : -1);
}
}
}
}
else
{
for (j=0;j<4;j++)
{
block_y = img->block_y + j;
for (i=0;i<4;i++)
{
block_x = img->block_x + i;
k = 2*(j >> 1) + (i >> 1);
l = 2*(j & 0x01) + (i & 0x01);
enc_picture->ref_idx[LIST_0][block_y][block_x] = (IS_FW ? best8x8l0ref[mode][k] : -1);
}
}
}
}
if (bframe)
{
for (j = img->block_y; j < img->block_y + 4; j++)
for (i = img->block_x; i < img->block_x + 4;i++)
{
cur_ref[LIST_0] = (int) enc_picture->ref_idx[LIST_0][j][i];
cur_ref[LIST_1] = (int) enc_picture->ref_idx[LIST_1][j][i];
enc_picture->ref_pic_id [LIST_0][j][i] = (cur_ref[LIST_0]>=0
? enc_picture->ref_pic_num[LIST_0 + currMB->list_offset][cur_ref[LIST_0]]
: -1);
enc_picture->ref_pic_id [LIST_1][j][i] = (cur_ref[LIST_1]>=0
? enc_picture->ref_pic_num[LIST_1 + currMB->list_offset][cur_ref[LIST_1]]
: -1);
}
}
else
{
for (j = img->block_y; j < img->block_y + 4; j++)
for (i = img->block_x; i < img->block_x + 4;i++)
{
cur_ref[LIST_0] = (int) enc_picture->ref_idx[LIST_0][j][i];
enc_picture->ref_pic_id [LIST_0][j][i] = (cur_ref[LIST_0]>=0
? enc_picture->ref_pic_num[LIST_0 + currMB->list_offset][cur_ref[LIST_0]]
: -1);
}
}
#undef IS_FW
#undef IS_BW
}
/*!
*************************************************************************************
* \brief
* Intra 16x16 mode decision
*************************************************************************************
*/
void Intra16x16_Mode_Decision (Macroblock* currMB, int* i16mode)
{
intrapred_luma_16x16 (); /* make intra pred for all 4 new modes */
find_sad_16x16 (i16mode); /* get best new intra mode */
currMB->cbp = dct_luma_16x16 (*i16mode);
}
/*!
*************************************************************************************
* \brief
* Sets Coefficients and reconstruction for an 8x8 block
*************************************************************************************
*/
void SetCoeffAndReconstruction8x8 (Macroblock* currMB)
{
int block, k, j, i;
int cur_ref[2];
//============= MIXED TRANSFORM SIZES FOR 8x8 PARTITION ==============
//--------------------------------------------------------------------
int l;
int bframe = img->type==B_SLICE;
if (currMB->luma_transform_size_8x8_flag)
{
//============= set mode and ref. frames ==============
for(i = 0;i<4;i++)
{
currMB->b8mode[i] = tr8x8.part8x8mode[i];
currMB->b8pdir[i] = tr8x8.part8x8pdir[i];
}
if (bframe)
{
for (j = 0;j<4;j++)
for (i = 0;i<4;i++)
{
k = 2*(j >> 1)+(i >> 1);
l = 2*(j & 0x01)+(i & 0x01);
enc_picture->ref_idx[LIST_0][img->block_y+j][img->block_x+i] = ((currMB->b8pdir[k] & 0x01) == 0) ? tr8x8.part8x8l0ref[k] : - 1;
enc_picture->ref_idx[LIST_1][img->block_y+j][img->block_x+i] = (currMB->b8pdir[k] > 0) ? tr8x8.part8x8l1ref[k] : - 1;
}
}
else
{
for (j = 0;j<4;j++)
for (i = 0;i<4;i++)
{
k = 2*(j >> 1)+(i >> 1);
l = 2*(j & 0x01)+(i & 0x01);
enc_picture->ref_idx[LIST_0][img->block_y+j][img->block_x+i] = tr8x8.part8x8l0ref[k];
}
}
for (j = img->block_y;j<img->block_y + BLOCK_MULTIPLE;j++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -