rdopt.c
来自「the newest JM software by h.264 JVT offi」· C语言 代码 · 共 1,966 行 · 第 1/5 页
C
1,966 行
if(img->type==SP_SLICE &&(!si_frame_indicator && !sp2_frame_indicator))
{
for (j = 0; j < MB_BLOCK_SIZE; j++)
{
memcpy (&lrec[img->pix_y+j][img->pix_x],tr8x8.lrec[j], MB_BLOCK_SIZE * sizeof(int));
}
}
if (img->P444_joined)
{
cmp_cbp[1] = cmp_cbp[2] = cbp8_8x8ts;
for (j = 0; j < MB_BLOCK_SIZE; j++)
{
memcpy (&enc_picture->imgUV[0][img->pix_y+j][img->pix_x],tr8x8.rec_mb8x8_cr[0][j], MB_BLOCK_SIZE * sizeof(imgpel));
memcpy (&enc_picture->imgUV[1][img->pix_y+j][img->pix_x],tr8x8.rec_mb8x8_cr[1][j], MB_BLOCK_SIZE * sizeof(imgpel));
}
}
}
}
else
{
if (bslice && currMB->valid_8x8)
{
StoreMV8x8(1);
}
//============= get pre-calculated data ==============
//---------------------------------------------------
//--- restore coefficients ---
//memcpy (img->cofAC[0][0][0],coefAC8x8[0][0][0][0], (4+img->num_blk8x8_uv) * 4 * 2 * 65 * sizeof(int));
for (block = 0; block < 4; block ++)
{
memcpy (img->cofAC[block][0][0],coefAC8x8[block][0][0][0], 4 * 2 * 65 * sizeof(int));
}
if (img->P444_joined)
{
for (block = 0; block<4; block++)
{
memcpy (img->cofAC[block+4][0][0],coefAC8x8[block][1][0][0], 4 * 2 * 65 * sizeof(int));
memcpy (img->cofAC[block+8][0][0],coefAC8x8[block][2][0][0], 4 * 2 * 65 * sizeof(int));
}
}
if (cnt_nonz_8x8<=5 && img->type!=SP_SLICE &&
((currMB->qp_scaled[0])!=0 || img->lossless_qpprime_flag==0))
{
currMB->cbp = 0;
currMB->cbp_blk = 0;
for (j = 0; j < MB_BLOCK_SIZE; j++)
{
memcpy (&enc_picture->imgY[img->pix_y+j][img->pix_x],tr4x4.mpr8x8[j], MB_BLOCK_SIZE * sizeof(imgpel));
}
if (params->rdopt == 3)
{
errdo_get_best_block(img, enc_picture->p_dec_img[0], decs->dec_mb_pred_best8x8[0], 0, MB_BLOCK_SIZE);
}
if(img->type ==SP_SLICE &&(!si_frame_indicator && !sp2_frame_indicator))
{
for (j = 0; j < MB_BLOCK_SIZE; j++)
{
memcpy (&lrec[img->pix_y+j][img->pix_x],tr4x4.lrec[j], MB_BLOCK_SIZE * sizeof(int)); // restore coeff. SP frame
}
}
memset( img->cofAC[0][0][0], 0, 4 * 4 * 2 * 65 * sizeof(int));
if (img->P444_joined)
{
cmp_cbp[1] = cmp_cbp[2] = 0;
for (j = 0; j < MB_BLOCK_SIZE; j++)
{
memcpy (&enc_picture->imgUV[0][img->pix_y+j][img->pix_x],tr4x4.mpr8x8CbCr[0][j], MB_BLOCK_SIZE * sizeof(imgpel));
memcpy (&enc_picture->imgUV[1][img->pix_y+j][img->pix_x],tr4x4.mpr8x8CbCr[1][j], MB_BLOCK_SIZE * sizeof(imgpel));
}
for (uv=0; uv<2; uv++)
{
for (block = 0; block<4; block++)
{
memset( img->cofAC[4+block+uv*4][0][0], 0, 4 * 2 * 65 * sizeof(int));
}
}
}
}
else
{
currMB->cbp = cbp8x8;
currMB->cbp_blk = cbp_blk8x8;
for (j = 0; j < MB_BLOCK_SIZE; j++)
{
memcpy (&enc_picture->imgY[img->pix_y+j][img->pix_x],tr4x4.rec_mbY8x8[j], MB_BLOCK_SIZE * sizeof(imgpel));
}
if (params->rdopt == 3)
{
errdo_get_best_block(img, enc_picture->p_dec_img[0], decs->dec_mbY_best8x8[0], 0, MB_BLOCK_SIZE);
}
if(img->type==SP_SLICE &&(!si_frame_indicator && !sp2_frame_indicator))
{
for (j = 0; j < MB_BLOCK_SIZE; j++)
{
memcpy (&lrec[img->pix_y+j][img->pix_x],tr4x4.lrec[j], MB_BLOCK_SIZE * sizeof(int));
}
}
if (img->P444_joined)
{
cmp_cbp[1] = cmp_cbp[2] = cbp8x8;
for (j = 0; j < MB_BLOCK_SIZE; j++)
{
memcpy (&enc_picture->imgUV[0][img->pix_y+j][img->pix_x],tr4x4.rec_mb8x8_cr[0][j], MB_BLOCK_SIZE * sizeof(imgpel));
memcpy (&enc_picture->imgUV[1][img->pix_y+j][img->pix_x],tr4x4.rec_mb8x8_cr[1][j], MB_BLOCK_SIZE * sizeof(imgpel));
}
}
}
}
}
/*!
*************************************************************************************
* \brief
* R-D Cost for a macroblock
*************************************************************************************
*/
int RDCost_for_macroblocks (Slice *currSlice, // <-- Current Slice to code
Macroblock *currMB, // <-- Current Macroblock to code
double lambda, // <-- lagrange multiplier
int mode, // <-- modus (0-COPY/DIRECT, 1-16x16, 2-16x8, 3-8x16, 4-8x8(+), 5-Intra4x4, 6-Intra16x16)
double* min_rdcost, // <-> minimum rate-distortion cost
double* min_dcost, // <-> distortion of mode which has minimum rate-distortion cost.
double* min_rate, // --> bitrate of mode which has minimum rate-distortion cost.
int i16mode,
int is_cavlc)
{
int i, j, k; //, k, ****ip4;
int j1, j2;
int rate = 0, coeff_rate = 0;
int64 distortion = 0;
double rdcost;
Macroblock *prevMB = currMB->PrevMB;
int bslice = (img->type==B_SLICE);
int tmp_cc;
int use_of_cc = (img->type!=I_SLICE && is_cavlc);
int cc_rate, dummy;
double dummy_d;
imgpel **mb_pred = img->mb_pred[0];
imgpel ***curr_mpr_16x16 = img->mpr_16x16[0];
// Check if direct mode can be utilized for this partition
if (bslice && mode==0)
{
for (j = img->block_y; j < img->block_y + 4;j++)
{
for (i = img->block_x; i < img->block_x + 4;i++)
{
if (direct_pdir[j][i] < 0)
return 0;
}
}
}
// Test MV limits for Skip Mode. This could be necessary for MBAFF case Frame MBs.
if ((img->MbaffFrameFlag) && (!currMB->mb_field) && (img->type==P_SLICE) && (mode==0) )
{
if (out_of_bounds_mvs(img, img->all_mv[LIST_0][0][0][0][0]))
return 0;
}
//=====
//===== Set Reference Pictures and Block modes
//=====
SetModesAndRefframeForBlocks (currMB, mode);
//=====
//===== Set Motion Vectors
//=====
SetMotionVectorsMB (img, &enc_picture->motion, currMB);
//=====
//===== Get coefficients, reconstruction values, CBP etc
//=====
if (mode<P8x8)
{
LumaResidualCoding (currMB, is_cavlc);
}
else if (mode == P8x8)
{
SetCoeffAndReconstruction8x8 (currMB);
}
else if (mode==I4MB)
{
currMB->cbp = Mode_Decision_for_Intra4x4Macroblock (currSlice, currMB, lambda, &dummy_d, is_cavlc);
}
else if (mode==I16MB)
{
Intra16x16_Mode_Decision (currMB, &i16mode, is_cavlc);
}
else if(mode==I8MB)
{
currMB->cbp = Mode_Decision_for_Intra8x8Macroblock(currSlice, currMB, lambda, &dummy_d);
}
else if(mode==IPCM)
{
for (j = 0; j < MB_BLOCK_SIZE; j++)
{
memcpy(&enc_picture->imgY[j + img->pix_y][img->opix_x], &pCurImg[j + img->opix_y][img->opix_x], MB_BLOCK_SIZE * sizeof(imgpel));
}
if ((img->yuv_format != YUV400) && !IS_INDEPENDENT(params))
{
// CHROMA
for (j = 0; j<img->mb_cr_size_y; j++)
{
j1 = j + img->opix_c_y;
j2 = j + img->pix_c_y;
memcpy(&enc_picture->imgUV[0][j2][img->opix_c_x], &pImgOrg[1][j1][img->opix_c_x], img->mb_cr_size_x * sizeof(imgpel));
memcpy(&enc_picture->imgUV[1][j2][img->opix_c_x], &pImgOrg[2][j1][img->opix_c_x], img->mb_cr_size_x * sizeof(imgpel));
}
}
for (j=0;j<4;j++)
for (i=0; i<(4+img->num_blk8x8_uv); i++)
img->nz_coeff[currMB->mb_nr][j][i] = 16;
}
if (params->rdopt == 3)
{
// We need the reconstructed prediction residue for the simulated decoders.
compute_residue_block (img, &enc_picture->p_curr_img[img->pix_y], decs->res_img[0], mode == I16MB ? img->mpr_16x16[0][i16mode] : img->mb_pred[0], 0, 16);
}
//Rate control
if (params->RCEnable)
{
if (mode == I16MB)
memcpy(pred[0], curr_mpr_16x16[i16mode][0], MB_PIXELS * sizeof(imgpel));
else
memcpy(pred[0], mb_pred[0], MB_PIXELS * sizeof(imgpel));
}
img->i16offset = 0;
dummy = 0;
if (((img->yuv_format != YUV400) && (active_sps->chroma_format_idc != YUV444)) && (mode != IPCM))
ChromaResidualCoding (currMB, is_cavlc);
if (mode==I16MB)
img->i16offset = I16Offset (currMB->cbp, i16mode);
//=====
//===== GET DISTORTION
//=====
// LUMA
if (params->rdopt == 3)
{
if (mode != P8x8)
{
for (k = 0; k<params->NoOfDecoders ;k++)
{
decode_one_mb (img, enc_picture, k, currMB);
distortion += compute_SSE(&pCurImg[img->opix_y], &enc_picture->p_dec_img[0][k][img->opix_y], img->opix_x, img->opix_x, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
}
}
else
{
for (k = 0; k<params->NoOfDecoders ;k++)
{
distortion += compute_SSE(&pCurImg[img->opix_y], &enc_picture->p_dec_img[0][k][img->opix_y], img->opix_x, img->opix_x, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
}
}
distortion /= params->NoOfDecoders;
if ((img->yuv_format != YUV400) && (active_sps->chroma_format_idc != YUV444))
{
// CHROMA
distortion += compute_SSE(&pImgOrg[1][img->opix_c_y], &enc_picture->imgUV[0][img->pix_c_y], img->opix_c_x, img->pix_c_x, img->mb_cr_size_y, img->mb_cr_size_x);
distortion += compute_SSE(&pImgOrg[2][img->opix_c_y], &enc_picture->imgUV[1][img->pix_c_y], img->opix_c_x, img->pix_c_x, img->mb_cr_size_y, img->mb_cr_size_x);
}
}
else
{
distortion = getDistortion(currMB);
}
if (currMB->qp_scaled[0] == 0 && img->lossless_qpprime_flag == 1 && distortion != 0)
return 0;
//===== S T O R E C O D I N G S T A T E =====
//---------------------------------------------------
store_coding_state (currSlice, currMB, cs_cm);
//=====
//===== GET RATE
//=====
//----- macroblock header -----
if (use_of_cc)
{
if (currMB->mb_type!=0 || (bslice && currMB->cbp!=0))
{
// cod counter and macroblock mode are written ==> do not consider code counter
tmp_cc = img->cod_counter;
rate = writeMBLayer (currSlice, currMB, 1, &coeff_rate);
ue_linfo (tmp_cc, dummy, &cc_rate, &dummy);
rate -= cc_rate;
img->cod_counter = tmp_cc;
}
else
{
// cod counter is just increased ==> get additional rate
ue_linfo (img->cod_counter + 1, dummy, &rate, &dummy);
ue_linfo (img->cod_counter , dummy, &cc_rate, &dummy);
rate -= cc_rate;
}
}
else
{
rate = writeMBLayer (currSlice, currMB, 1, &coeff_rate);
}
//===== R E S T O R E C O D I N G S T A T E =====
//-------------------------------------------------------
reset_coding_state (currSlice, currMB, cs_cm);
rdcost = (double)distortion + lambda * dmax(0.5,(double)rate);
if (rdcost >= *min_rdcost ||
((currMB->qp_scaled[0]) == 0 && img->lossless_qpprime_flag == 1 && distortion != 0))
{
#if FASTMODE
// Reordering RDCost comparison order of mode 0 and mode 1 in P_SLICE
// if RDcost of mode 0 and mode 1 is same, we choose best_mode is 0
// This might not always be good since mode 0 is more biased towards rate than quality.
if((img->type!=P_SLICE || mode != 0 || rdcost != *min_rdcost) || IS_FREXT_PROFILE(params->ProfileIDC))
#endif
return 0;
}
if ((img->MbaffFrameFlag) && (mode ? 0: ((img->type == B_SLICE) ? !currMB->cbp:1))) // AFF and current is skip
{
if (currMB->mb_nr & 0x01) //bottom
{
if (prevMB->mb_type ? 0:((img->type == B_SLICE) ? !prevMB->cbp:1)) //top is skip
{
if (!(field_flag_inference(currMB) == currMB->mb_field)) //skip only allowed when correct inference
return 0;
}
}
}
//===== U P D A T E M I N I M U M C O S T =====
//-----------------------------------------------------
*min_rdcost = rdcost;
*min_dcost = (double) distortion;
*min_rate = lambda * (double)coeff_rate;
#ifdef BEST_NZ_COEFF
for (j=0;j<4;j++)
memcpy(&gaaiMBAFF_NZCoeff[j][0], &img->nz_coeff[currMB->mb_nr][j][0], (4 + img->num_blk8x8_uv) * sizeof(int));
#endif
return 1;
}
/*!
*************************************************************************************
* \brief
* Store macroblock parameters
*************************************************************************************
*/
void store_macroblock_parameters (Macroblock *currMB, int mode)
{
int j, ****i4p, ***i3p;
int bslice = (img->type==B_SLICE);
//--- store best mode ---
best_mode = mode;
best_c_imode = currMB->c_ipred_mode;
best_i16offset = img->i16offset;
memcpy(b8mode, currMB->b8mode, BLOCK_MULTIPLE * sizeof(short));
memcpy(b8bipred_me, currMB->bipred_me, BLOCK_MULTIPLE * sizeof(short));
memcpy(b8pdir, currMB->b8pdir, BLOCK_MULTIPLE * sizeof(short));
memcpy(b4_intra_pred_modes, currMB->intra_pred_modes, MB_BLOCK_PARTITIONS * sizeof(char));
memcpy(b8_intra_pred_modes8x8,currMB->intra_pred_modes8x8, MB_BLOCK_PARTITIONS * sizeof(char));
for (j = 0 ; j < BLOCK_MULTIPLE; j++)
{
memcpy(&b4_ipredmode[j * BLOCK_MULTIPLE],&img->ipredmode [img->block_y + j][img->block_x],BLOCK_MULTIPLE * sizeof(char));
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?