📄 rdopt.c
字号:
else currSE->writing = writeIntraPredMode2Buffer_CABAC;
//--- choose data partition ---
if (img->type!=B_IMG) dataPart = &(currSlice->partArr[partMap[SE_INTRAPREDMODE]]);
else dataPart = &(currSlice->partArr[partMap[SE_BFRAME]]);
//--- encode and update rate ---
dataPart->writeSyntaxElement (currSE, dataPart);
rate = currSE->len;
currSE++;
currMB->currSEnr++;
//===== RATE for LUMINANCE COEFFICIENTS =====
rate += writeLumaCoeff4x4 (b8, b4, 1);
rdcost = (double)distortion + lambda*(double)rate;
return rdcost;
}
/*!
*************************************************************************************
* \brief
* Mode Decision for an 4x4 Intra block
*************************************************************************************
*/
int
Mode_Decision_for_4x4IntraBlocks (int b8, int b4, double lambda, int* min_cost)
{
int ipmode, best_ipmode, i, j, k, x, y, cost, dummy;
int c_nz, nonzero, rec4x4[4][4], diff[16];
double rdcost;
int block_x = 8*(b8%2)+4*(b4%2);
int block_y = 8*(b8/2)+4*(b4/2);
int pic_pix_x = img->pix_x+block_x;
int pic_pix_y = img->pix_y+block_y;
int pic_block_x = pic_pix_x/4;
int pic_block_y = pic_pix_y/4;
double min_rdcost = 1e30;
*min_cost = (1<<20);
//===== INTRA PREDICTION FOR 4x4 BLOCK =====
intrapred_luma (pic_pix_x, pic_pix_y);
//===== LOOP OVER ALL 4x4 INTRA PREDICTION MODES =====
for (ipmode=0; ipmode<NO_INTRA_PMODE; ipmode++)
{
if ((ipmode==DC_PRED || ipmode==HOR_PRED || img->ipredmode[pic_block_x+1][pic_block_y] >= 0) &&
(ipmode==DC_PRED || ipmode==VERT_PRED || img->ipredmode[pic_block_x][pic_block_y+1] >= 0) )
{
if (!input->rdopt)
{
for (k=j=0; j<4; j++)
for (i=0; i<4; i++, k++)
{
diff[k] = imgY_org[pic_pix_y+j][pic_pix_x+i] - img->mprr[ipmode][j][i];
}
cost = (int)floor(2 * lambda * PRED_IPRED[img->ipredmode[pic_block_x+1][pic_block_y ]+1]
[img->ipredmode[pic_block_x ][pic_block_y+1]+1][ipmode] +0.4999);
cost += SATD (diff, input->hadamard);
if (cost < *min_cost)
{
best_ipmode = ipmode;
*min_cost = cost;
}
}
else
{
// get prediction and prediction error
for (j=0; j<4; j++)
for (i=0; i<4; i++)
{
img->mpr[block_x+i][block_y+j] = img->mprr[ipmode][j][i];
img->m7[i][j] = imgY_org[pic_pix_y+j][pic_pix_x+i] - img->mprr[ipmode][j][i];
}
//===== store the coding state =====
store_coding_state (cs_cm);
// get and check rate-distortion cost
if ((rdcost = RDCost_for_4x4IntraBlocks (&c_nz, b8, b4, ipmode, lambda, min_rdcost)) < min_rdcost)
{
//--- set coefficients ---
for (j=0; j<2; j++)
for (i=0; i<18;i++) cofAC4x4[j][i]=img->cofAC[b8][b4][j][i];
//--- set reconstruction ---
for (y=0; y<4; y++)
for (x=0; x<4; x++) rec4x4[y][x] = imgY[pic_pix_y+y][pic_pix_x+x];
//--- flag if dct-coefficients must be coded ---
nonzero = c_nz;
//--- set best mode update minimum cost ---
min_rdcost = rdcost;
best_ipmode = ipmode;
}
reset_coding_state (cs_cm);
}
}
}
//===== set intra mode prediction =====
img->ipredmode[pic_block_x+1][pic_block_y+1] = best_ipmode;
img->mb_data[img->current_mb_nr].intra_pred_modes[4*b8+b4]=PRED_IPRED[img->ipredmode[pic_block_x+1][pic_block_y ]+1]
[img->ipredmode[pic_block_x ][pic_block_y+1]+1]
[best_ipmode];
if (!input->rdopt)
{
// get prediction and prediction error
for (j=0; j<4; j++)
for (i=0; i<4; i++)
{
img->mpr[block_x+i][block_y+j] = img->mprr[best_ipmode][j][i];
img->m7[i][j] = imgY_org[pic_pix_y+j][pic_pix_x+i] - img->mprr[best_ipmode][j][i];
}
nonzero = dct_luma (block_x, block_y, &dummy, 1);
}
else
{
//===== restore coefficients =====
for (j=0; j<2; j++)
for (i=0; i<18;i++) img->cofAC[b8][b4][j][i]=cofAC4x4[j][i];
//===== restore reconstruction and prediction (needed if single coeffs are removed) =====
for (y=0; y<4; y++)
for (x=0; x<4; x++)
{
imgY[pic_pix_y+y][pic_pix_x+x] = rec4x4[y][x];
img->mpr[block_x+x][block_y+y] = img->mprr[best_ipmode][y][x];
}
}
return nonzero;
}
/*!
*************************************************************************************
* \brief
* Mode Decision for an 8x8 Intra block
*************************************************************************************
*/
int
Mode_Decision_for_8x8IntraBlocks (int b8,
double lambda,
int* cost)
{
int nonzero=0, b4;
int cost4x4;
*cost = (int)floor(6.0 * lambda + 0.4999);
for (b4=0; b4<4; b4++)
{
if (Mode_Decision_for_4x4IntraBlocks (b8, b4, lambda, &cost4x4))
{
nonzero = 1;
}
*cost += cost4x4;
}
return nonzero;
}
/*!
*************************************************************************************
* \brief
* 4x4 Intra mode decision for an macroblock
*************************************************************************************
*/
int
Mode_Decision_for_Intra4x4Macroblock (double lambda, int* cost)
{
int cbp=0, b8, cost8x8;
for (*cost=0, b8=0; b8<4; b8++)
{
if (Mode_Decision_for_8x8IntraBlocks (b8, lambda, &cost8x8))
{
cbp |= (1<<b8);
}
*cost += cost8x8;
}
return cbp;
}
/*!
*************************************************************************************
* \brief
* R-D Cost for an 8x8 Partition
*************************************************************************************
*/
double
RDCost_for_8x8blocks (int* cnt_nonz, // --> number of nonzero coefficients
int* cbp_blk, // --> cbp blk
double lambda, // <-- lagrange multiplier
int block, // <-- 8x8 block number
int mode, // <-- partitioning mode
int pdir, // <-- prediction direction
int ref) // <-- reference frame
{
int i, j, k;
int rate=0, distortion=0;
int dummy, mrate;
int fw_mode, bw_mode;
int cbp = 0;
int pax = 8*(block%2);
int pay = 8*(block/2);
int i0 = pax/4;
int j0 = pay/4;
int bframe = (img->type==B_IMG);
int intra = (mode==IBLOCK);
int direct = (bframe && mode==0);
int b8value = B8Mode2Value (mode, pdir);
Macroblock *currMB = &img->mb_data[img->current_mb_nr];
SyntaxElement *currSE = &img->MB_SyntaxElements[currMB->currSEnr];
Slice *currSlice = img->currentSlice;
DataPartition *dataPart;
const int *partMap = assignSE2partition[input->partition_mode];
EncodingEnvironmentPtr eep_dp;
//=====
//===== GET COEFFICIENTS, RECONSTRUCTIONS, CBP
//=====
if (!intra)
{
if (direct)
{
*cnt_nonz = LumaResidualCoding8x8 (&cbp, cbp_blk, block, 0, 0, max(0,refFrArr[img->block_y+j0][img->block_x+i0]));
}
else
{
fw_mode = (pdir==0||pdir==2 ? mode : 0);
bw_mode = (pdir==1||pdir==2 ? mode : 0);
*cnt_nonz = LumaResidualCoding8x8 (&cbp, cbp_blk, block, fw_mode, bw_mode, ref);
}
}
//===== get residue =====
if (input->rdopt==2 && !bframe)
{
// We need the reconstructed prediction residue for the simulated decoders.
compute_residue_b8block (block, -1);
}
//=====
//===== GET DISTORTION
//=====
if (input->rdopt==2 && !bframe)
{
for (k=0; k<input->NoOfDecoders ;k++)
{
decode_one_b8block (k, P8x8, block, mode, ref);
for (j=img->pix_y+pay; j<img->pix_y+pay+8; j++)
for (i=img->pix_x+pax; i<img->pix_x+pax+8; i++)
{
distortion += img->quad[imgY_org[j][i] - decs->decY[k][j][i]];
}
}
distortion /= input->NoOfDecoders;
}
else
{
for (j=img->pix_y+pay; j<img->pix_y+pay+8; j++)
for (i=img->pix_x+pax; i<img->pix_x+pax+8; i++)
{
distortion += img->quad [imgY_org[j][i] - imgY[j][i]];
}
}
//=====
//===== GET RATE
//=====
//----- block 8x8 mode -----
if (input->symbol_mode == UVLC)
{
n_linfo2 (b8value, dummy, &mrate, &dummy);
rate += mrate;
}
else
{
dataPart = &(currSlice->partArr[partMap[SE_BFRAME]]);
currSE->value1 = b8value;
currSE->writing = writeB8_typeInfo2Buffer_CABAC;
currSE->type = SE_MBTYPE;
dataPart->writeSyntaxElement (currSE, dataPart);
rate += currSE->len;
currSE++;
currMB->currSEnr++;
}
//----- intra 4x4 prediction modes -----
if (intra)
{
rate += writeIntra4x4Modes (block);
}
//----- motion information -----
if (!intra && !direct)
{
if ((input->no_multpred>1 || input->add_ref_frame>0) && (pdir==0 || pdir==2))
rate += writeReferenceFrame (mode, i0, j0, ref);
if (pdir==0 || pdir==2)
rate += writeMotionVector8x8 (i0, j0, i0+2, j0+2, ref, mode);
if (pdir==1 || pdir==2)
rate += writeMotionVector8x8 (i0, j0, i0+2, j0+2, -1, mode);
}
//----- coded block pattern (for CABAC only) -----
if (input->symbol_mode == CABAC)
{
dataPart = &(currSlice->partArr[partMap[SE_CBP_INTER]]);
eep_dp = &(dataPart->ee_cabac);
mrate = arienco_bits_written (eep_dp);
writeCBP_BIT_CABAC (block, ((*cnt_nonz>0)?1:0), cbp8x8, currMB, (intra?0:1), eep_dp);
mrate = arienco_bits_written (eep_dp) - mrate;
rate += mrate;
}
//----- luminance coefficients -----
if (*cnt_nonz)
{
rate += writeLumaCoeff8x8 (block, intra);
}
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 an macroblock
*************************************************************************************
*/
void
SetModesAndRefframeForBlocks (int mode)
{
int i,j,k,l;
Macroblock *currMB = &img->mb_data[img->current_mb_nr];
int bframe = (img->type==B_IMG);
//--- macroblock type ---
currMB->mb_type = mode;
//--- block 8x8 mode and prediction direction ---
switch (mode)
{
case 0:
for(i=0;i<4;i++)
{
currMB->b8mode[i] = 0;
currMB->b8pdir[i] = (bframe?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++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -