if (input->rdopt)
??? {
????? int mb_available_up;
????? int mb_available_left;
????? int mb_available_up_left;
?????
????? min_rdcost = max_rdcost;
?????
????? // precompute all new chroma intra prediction modes
?? //++ 對色度進行幀內預測
????? IntraChromaPrediction8x8(&mb_available_up, &mb_available_left, &mb_available_up_left);
?????
?? //++ 分別在四種色度模式下進行 RDO 計算,如果是 inter 模式,因為色度預測模式與 SSD 計算
?? //++ 無關,因此只需要計算一次(利用 currMB->c_ipred_mode == DC_PRED_8 條件限制來實現)
????? for (currMB->c_ipred_mode=DC_PRED_8; currMB->c_ipred_mode<=PLANE_8; currMB->c_ipred_mode++)
????? {
???????
??????? // bypass if c_ipred_mode is not allowed
??????? if ((currMB->c_ipred_mode==VERT_PRED_8 && !mb_available_up) ||
????????? (currMB->c_ipred_mode==HOR_PRED_8 && !mb_available_left) ||
????????? (currMB->c_ipred_mode==PLANE_8 && (!mb_available_left || !mb_available_up || !mb_available_up_left)))
????????? continue;
???????
???????
??????? //===== GET BEST MACROBLOCK MODE =====
??????? for (ctr16x16=0, index=0; index<7; index++)
??????? {
????????? mode = mb_mode_table[index];
?????????
????????? //--- for INTER16x16 check all prediction directions ---
????????? if (mode==1 && img->type==B_SLICE)
????????? {
??????????? best8x8pdir[1][0] = best8x8pdir[1][1] = best8x8pdir[1][2] = best8x8pdir[1][3] = ctr16x16;
??????????? if (ctr16x16 < 2) index--;
??????????? ctr16x16++;
????????? }
?????????
????????? img->NoResidueDirect = 0;
?????????
????????? if (valid[mode])
????????? {
??????????? // bypass if c_ipred_mode not used
??? //++ 設置當前宏塊類型以及其中每個8*8塊的分割方式和預測方向,每個4*4塊的參考幀索引
??? //++ 該函數在下面的 RDCost_for_macroblocks 函數內再次調用,進行了重復操作
??????????? SetModesAndRefframeForBlocks (mode);
??????????? if (currMB->c_ipred_mode == DC_PRED_8 || //++ 利用這個條件限制來實現 inter 模式時只計算一次 RDO
????????????? (IS_INTRA(currMB) ))
??????????? {
????????????? if (RDCost_for_macroblocks (lambda_mode, mode, &min_rdcost)) //++ 幀內模式時亮度存在重復計算情況:因為色度預測模式與亮度預測模式無關,所以在該色度模
????????????? {???????????????? //++ 式循環中每次計算得到的 intra16*16 和 intra4*4 宏塊類型的最佳亮度模式都是完全相同的
??????????????? //Rate control
??????????????? if(mode == P8x8)
??????????????? {
????????????????? for (i=0; i<16; i++)
??????????????????? for(j=0; j<16; j++)
????????????????????? diffy[j][i] = imgY_org[img->opix_y+j][img->opix_x+i] - mpr8x8[j][i];
??????????????? }else
??????????????? {
????????????????? for (i=0; i<16; i++)
??????????????????? for(j=0; j<16; j++)
????????????????????? diffy[j][i] = imgY_org[img->opix_y+j][img->opix_x+i] - pred[j][i];
??????????????? }
???????????????
??????????????? store_macroblock_parameters (mode);
????????????? }
??????????? }
????????? }
????????? if (valid[0] && bframe && mode == 0 && currMB->cbp && (currMB->cbp&15) != 15) //g050
????????? {
??????????? img->NoResidueDirect = 1;
??????????? if (RDCost_for_macroblocks (lambda_mode, mode, &min_rdcost))
??????????? {
????????????? //Rate control
????????????? for (i=0; i<16; i++)
??????????????? for(j=0; j<16; j++)
????????????????? diffy[j][i] = imgY_org[img->opix_y+j][img->opix_x+i] - pred[j][i];
??????????????? store_macroblock_parameters (mode);
??????????? }
????????? }
??????? }
????? }
??? }
??? else
??? {
????? if (valid[0] && bframe) // check DIRECT MODE
????? {
??????? cost = (have_direct?cost_direct:Get_Direct_CostMB (lambda_mode));
??????? cost -= (int)floor(16*lambda_motion+0.4999);
??????? if (cost <= min_cost)
??????? {
????????? //Rate control
????????? for (i=0; i<16; i++)
??????????? for(j=0; j<16; j++)
????????????? diffy[j][i] = imgY_org[img->pix_y+j][img->pix_x+i]-img->mpr[i][j];
???????????
??????????? min_cost = cost;
??????????? best_mode = 0;
??????? }
????? }
????? if (valid[I4MB]) // check INTRA4x4
????? {
??????? currMB->cbp = Mode_Decision_for_Intra4x4Macroblock (lambda_mode, &cost); //++ 計算當前宏塊采用intra4*4編碼時的代價和CBP
??????? if (cost <= min_cost)
??????? {
????????? //Rate control
????????? for (i=0; i<16; i++)
??????????? for(j=0; j<16; j++)
????????????? diffy[j][i] = imgY_org[img->pix_y+j][img->pix_x+i]-img->mpr[i][j]; //++ 保存采用intra4*4編碼時最佳模式的殘差塊
???????????
??????????? min_cost = cost;
??????????? best_mode = I4MB;
??????? }
????? }
????? if (valid[I16MB]) // check INTRA16x16
????? {
??????? intrapred_luma_16x16 (); //++ 分別計算當前宏塊在4種intra16*16幀內預測模式下的預測塊
??????? cost = find_sad_16x16 (&i16mode); //++ 計算intra16*16類型的代價(以SATD作為判斷標準,因為該段代碼是不采用RDO的處理過程)
??????? if (cost < min_cost) //++ 如果采用intra16*16類型編碼的代價小于采用intra4*4類型編碼的代價,則對當前宏塊采用intra16*16類型編碼
??????? {
????????? //Rate control
????????? for (i=0; i<16; i++)
??????????? for(j=0; j<16; j++)
????????????? diffy[j][i] = imgY_org[img->pix_y+j][img->pix_x+i]-img->mprr_2[i16mode][j][i];
???????????
??????????? best_mode?? = I16MB;
??????????? currMB->cbp = dct_luma_16x16 (i16mode); //++ 對當前宏塊采用intra16*16類型進行編碼,并計算CBP
??????? }
????? }
??? }
???
??? if (rerun==0)
??? {
????? intra1 = (currMB->mb_type==I16MB || currMB->mb_type==I4MB ? 1 : 0);
??? }
} // for (rerun=0; rerun<runs; rerun++)
if (input->rdopt)
{
???
??? if ((cbp!=0 || best_mode==I16MB ))
????? currMB->prev_cbp = 1;
??? else if (cbp==0 && !input->RCEnable)
??? {
????? currMB->delta_qp = 0;
????? currMB->qp = currMB->prev_qp;
????? img->qp = currMB->qp;
????? currMB->prev_cbp = 0;
??? }
??? set_stored_macroblock_parameters ();
}
else
{
??? //===== set parameters for chosen mode =====
??? SetModesAndRefframeForBlocks (best_mode); //++ 設置當前宏塊的參數,包括:宏塊類型(mb_type)、4個8*8塊的分割模式和預測方向(b8mode,b8pdir)、16個4*4塊的參考幀索引(ref_idx,ref_pic_id)
??? if (best_mode==P8x8)
??? {
????? SetCoeffAndReconstruction8x8 (currMB);
??? }
??? else
??? {
????? if (best_mode!=I4MB)
????? {
??????? for (k=0, j=img->block_y; j<img->block_y+4; j++)
????????? for (???? i=img->block_x; i<img->block_x+4; i++, k++)
????????? {
??????????? ipredmodes??? [i][j] = DC_PRED;
??????????? currMB->intra_pred_modes[k] = DC_PRED;
????????? }
????????? if (best_mode!=I16MB)
????????? {
??????????? LumaResidualCoding ();
??????????? //Rate control
??????????? for (i=0; i<16; i++)
????????????? for(j=0; j<16; j++)
??????????????? diffy[j][i] = imgY_org[img->pix_y+j][img->pix_x+i]-img->mpr[i][j];
????????? }
????? }
??? }
??? // precompute all chroma intra prediction modes
??? IntraChromaPrediction8x8(NULL, NULL, NULL); //++ 對色度塊進行處理,包括:分別計算4種色度幀內預測模式下的預測塊、代價(該分支未采用RDO,因此以SATD作為判斷標準)、最佳預測模式
??? img->i16offset = 0;
??? dummy = 0;
??? ChromaResidualCoding (&dummy);
??? if (best_mode==I16MB)
??? {
????? img->i16offset = I16Offset (currMB->cbp, i16mode);
??? }
??? SetMotionVectorsMB (currMB, bframe);
???
??? //===== check for SKIP mode =====
??? if ((img->type==P_SLICE || img->type==SP_SLICE) && best_mode==1 && currMB->cbp==0 &&
????? enc_picture->ref_idx[LIST_0][img->block_x][img->block_y]==0 &&
????? enc_picture->mv[LIST_0][img->block_x][img->block_y][0]==allmvs[0][0][0][0][0][0] &&
????? enc_picture->mv[LIST_0][img->block_x][img->block_y][1]==allmvs[0][0][0][0][0][1]?????????????? )
??? {
????? currMB->mb_type=currMB->b8mode[0]=currMB->b8mode[1]=currMB->b8mode[2]=currMB->b8mode[3]=0;
??? }
???
??? if(img->MbaffFrameFlag)
????? set_mbaff_parameters();
}
// Rate control
if(input->RCEnable)
{??
??? if(img->type==P_SLICE)
??? {
????? img->MADofMB[img->current_mb_nr] = calc_MAD();
?????
????? if(input->basicunit<img->Frame_Total_Number_MB)
????? {
??????? img->TotalMADBasicUnit +=img->MADofMB[img->current_mb_nr];
???????
??????? /* delta_qp is present only for non-skipped macroblocks*/
??????? if ((cbp!=0 || best_mode==I16MB))
????????? currMB->prev_cbp = 1;
??????? else
??????? {
????????? img->qp -= currMB->delta_qp;
????????? currMB->delta_qp = 0;
????????? currMB->qp = img->qp;
????????? currMB->prev_cbp = 0;
??????? }
??????? /* When MBAFF is used, delta_qp is only present for the first non-skipped macroblock of each
??????? macroblock pair*/
??????? if (input->MbInterlace)
??????? {
????????? if(!currMB->mb_field)
????????? {
??????????? DELTA_QP = currMB->delta_qp;
??????????? QP????? = currMB->qp;
????????? }
????????? else
????????? {
??????????? DELTA_QP2 = currMB->delta_qp;
??????????? QP2????? = currMB->qp;
????????? }
??????? }??????
????? }
??? }
}
???
if(input->rdopt)
??? rdopt->min_rdcost = min_rdcost;
else
??? rdopt->min_rdcost = min_cost;
if(img->MbaffFrameFlag)
{
??? if (img->current_mb_nr%2) //bottom
??? {
????? if ((currMB->mb_type ? 0:((img->type == B_SLICE) ? !currMB->cbp:1)) // bottom is skip
??????? &&(prevMB->mb_type ? 0:((img->type == B_SLICE) ? !prevMB->cbp:1))) // top is skip
????? {
??????? if (!(field_flag_inference() == curr_mb_field))
??????? {
????????? rdopt->min_rdcost = 1e30; // don't allow coding of an MB pair as skip if wrong inference
??????? }
????? }
??? }
}
//===== Decide if this MB will restrict the reference frames =====
if (input->RestrictRef==1)
{
??? if (input->rdopt==1)
??? {
????? refresh_map[2*img->mb_y ][2*img->mb_x ] = (intra ? 1 : 0);
????? refresh_map[2*img->mb_y ][2*img->mb_x+1] = (intra ? 1 : 0);
????? refresh_map[2*img->mb_y+1][2*img->mb_x ] = (intra ? 1 : 0);
????? refresh_map[2*img->mb_y+1][2*img->mb_x+1] = (intra ? 1 : 0);
??? }
??? else if (input->rdopt==2)
??? {
????? refresh_map[2*img->mb_y ][2*img->mb_x ] = (intra1==0 && (currMB->mb_type==I16MB || currMB->mb_type==I4MB) ? 1 : 0);
????? refresh_map[2*img->mb_y ][2*img->mb_x+1] = (intra1==0 && (currMB->mb_type==I16MB || currMB->mb_type==I4MB) ? 1 : 0);
????? refresh_map[2*img->mb_y+1][2*img->mb_x ] = (intra1==0 && (currMB->mb_type==I16MB || currMB->mb_type==I4MB) ? 1 : 0);
????? refresh_map[2*img->mb_y+1][2*img->mb_x+1] = (intra1==0 && (currMB->mb_type==I16MB || currMB->mb_type==I4MB) ? 1 : 0);
??? }
}
else if (input->RestrictRef==2)
{
??? refresh_map[2*img->mb_y ][2*img->mb_x ] = (currMB->mb_type==I16MB || currMB->mb_type==I4MB ? 1 : 0);
??? refresh_map[2*img->mb_y ][2*img->mb_x+1] = (currMB->mb_type==I16MB || currMB->mb_type==I4MB ? 1 : 0);
??? refresh_map[2*img->mb_y+1][2*img->mb_x ] = (currMB->mb_type==I16MB || currMB->mb_type==I4MB ? 1 : 0);
??? refresh_map[2*img->mb_y+1][2*img->mb_x+1] = (currMB->mb_type==I16MB || currMB->mb_type==I4MB ? 1 : 0);
}
if(input->FMEnable)
??? skip_intrabk_SAD(best_mode, listXsize[LIST_0+list_offset]);
}