00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <math.h>
00012 #include <limits.h>
00013 #include <float.h>
00014
00015 #include "global.h"
00016 #include "rdopt_coding_state.h"
00017 #include "intrarefresh.h"
00018 #include "image.h"
00019 #include "ratectl.h"
00020 #include "mode_decision.h"
00021 #include "fmo.h"
00022 #include "me_umhex.h"
00023 #include "me_umhexsmp.h"
00024 #include "macroblock.h"
00025 #include "conformance.h"
00026 #include "vlc.h"
00027 #include "rdopt.h"
00028
00029
00030
00031
00032
00033
00034
00035 void encode_one_macroblock_high (Slice *currSlice, Macroblock *currMB)
00036 {
00037 int max_index = 9;
00038 int block, index, mode, i, j;
00039 char best_pdir;
00040 RD_PARAMS enc_mb;
00041 double min_rdcost = 1e30;
00042 double min_dcost = 1e30;
00043 char best_ref[2] = {0, -1};
00044 int bmcost[5] = {INT_MAX};
00045 int cost=0;
00046 int min_cost = INT_MAX, cost_direct=0, have_direct=0, i16mode=0;
00047 int intra1 = 0;
00048 int cost8x8_direct = 0;
00049 int mb_available_up;
00050 int mb_available_left;
00051 int mb_available_up_left;
00052
00053 short islice = (short) (img->type==I_SLICE);
00054 short bslice = (short) (img->type==B_SLICE);
00055 short pslice = (short) ((img->type==P_SLICE) || (img->type==SP_SLICE));
00056 short intra = (short) (islice || (pslice && img->mb_y==img->mb_y_upd && img->mb_y_upd!=img->mb_y_intra));
00057 int lambda_mf[3];
00058 int is_cavlc = (img->currentSlice->symbol_mode == CAVLC);
00059
00060 Macroblock* prevMB = currMB->PrevMB;
00061 imgpel **mb_pred = img->mb_pred[0];
00062 Block8x8Info *b8x8info = img->b8x8info;
00063
00064 short min_chroma_pred_mode, max_chroma_pred_mode;
00065
00066 short inter_skip = 0;
00067 short bipred_me = 0;
00068 double min_rate = 0;
00069
00070 if(params->SearchMode == UM_HEX)
00071 {
00072 UMHEX_decide_intrabk_SAD();
00073 }
00074 else if (params->SearchMode == UM_HEX_SIMPLE)
00075 {
00076 smpUMHEX_decide_intrabk_SAD();
00077 }
00078
00079 if (params->AdaptiveRounding)
00080 {
00081 reset_adaptive_rounding();
00082 }
00083
00084 if (img->MbaffFrameFlag)
00085 {
00086 reset_mb_nz_coeff(currMB->mb_nr);
00087 }
00088
00089 intra |= RandomIntra (currMB->mb_nr);
00090
00091
00092 init_enc_mb_params(currMB, &enc_mb, intra, bslice);
00093
00094
00095
00096 store_coding_state (currSlice, currMB, cs_cm);
00097
00098 if (!intra)
00099 {
00100
00101 best_mode = 1;
00102 if (bslice)
00103 {
00104 Get_Direct_Motion_Vectors (currMB);
00105 }
00106
00107 if (params->CtxAdptLagrangeMult == 1)
00108 {
00109 get_initial_mb16x16_cost(currMB);
00110 }
00111
00112
00113 for (min_cost=INT_MAX, mode=1; mode<4; mode++)
00114 {
00115 bipred_me = 0;
00116 b8x8info->bipred8x8me[mode][0] = 0;
00117 if (enc_mb.valid[mode])
00118 {
00119 for (cost=0, block=0; block<(mode==1?1:2); block++)
00120 {
00121 update_lambda_costs(&enc_mb, lambda_mf);
00122 PartitionMotionSearch (currMB, mode, block, lambda_mf);
00123
00124
00125 j = (block==1 && mode==2 ? 2 : 0);
00126 i = (block==1 && mode==3 ? 2 : 0);
00127
00128
00129 bmcost[LIST_0] = INT_MAX;
00130 list_prediction_cost(currMB, LIST_0, block, mode, &enc_mb, bmcost, best_ref);
00131
00132 if (bslice)
00133 {
00134
00135 bmcost[LIST_1] = INT_MAX;
00136 list_prediction_cost(currMB, LIST_1, block, mode, &enc_mb, bmcost, best_ref);
00137
00138
00139 list_prediction_cost(currMB, BI_PRED, block, mode, &enc_mb, bmcost, best_ref);
00140
00141
00142 if (is_bipred_enabled(mode))
00143 {
00144 list_prediction_cost(currMB, BI_PRED_L0, block, mode, &enc_mb, bmcost, 0);
00145 list_prediction_cost(currMB, BI_PRED_L1, block, mode, &enc_mb, bmcost, 0);
00146 }
00147 else
00148 {
00149 bmcost[BI_PRED_L0] = INT_MAX;
00150 bmcost[BI_PRED_L1] = INT_MAX;
00151 }
00152
00153
00154 determine_prediction_list(mode, bmcost, best_ref, &best_pdir, &cost, &bipred_me);
00155 }
00156 else
00157 {
00158 best_pdir = 0;
00159 cost += bmcost[LIST_0];
00160 }
00161
00162 assign_enc_picture_params(mode, best_pdir, 2 * block, enc_mb.list_offset[LIST_0], best_ref[LIST_0], best_ref[LIST_1], bslice, bipred_me);
00163
00164
00165 set_block8x8_info(b8x8info, mode, block, best_ref, best_pdir, bipred_me);
00166
00167
00168 if (mode>1 && block == 0)
00169 SetRefAndMotionVectors (&enc_picture->motion, currMB, block, mode, best_pdir, best_ref[LIST_0], best_ref[LIST_1], bipred_me);
00170 }
00171
00172 if (cost < min_cost)
00173 {
00174 best_mode = mode;
00175 min_cost = cost;
00176 if (params->CtxAdptLagrangeMult == 1)
00177 {
00178 adjust_mb16x16_cost(cost);
00179 }
00180 }
00181 }
00182 }
00183
00184 if (enc_mb.valid[P8x8])
00185 {
00186 giRDOpt_B8OnlyFlag = 1;
00187
00188 tr8x8.mb_p8x8_cost = INT_MAX;
00189 tr4x4.mb_p8x8_cost = INT_MAX;
00190
00191 store_coding_state (currSlice, currMB, cs_mb);
00192 memset( img->cofAC[0][0][0], 0, 4 * 4 * 2 * 65 * sizeof(int));
00193
00194 currMB->all_blk_8x8 = -1;
00195 currMB->valid_8x8 = FALSE;
00196
00197 if (params->Transform8x8Mode)
00198 {
00199 tr8x8.mb_p8x8_cost = 0;
00200
00201
00202
00203
00204 for (cost_direct=cbp8x8=cbp_blk8x8=cnt_nonz_8x8=0, block = 0; block < 4; block++)
00205 {
00206 submacroblock_mode_decision(currSlice, &enc_mb, &tr8x8, currMB, cofAC8x8ts[block],
00207 &have_direct, bslice, block, &cost_direct, &cost, &cost8x8_direct, 1, is_cavlc);
00208 set_subblock8x8_info(b8x8info, P8x8, block, &tr8x8);
00209 }
00210
00211
00212 cbp8_8x8ts = cbp8x8;
00213 cbp_blk8_8x8ts = cbp_blk8x8;
00214 cnt_nonz8_8x8ts = cnt_nonz_8x8;
00215 currMB->luma_transform_size_8x8_flag = 0;
00216
00217
00218 reset_coding_state (currSlice, currMB, cs_mb);
00219 }
00220
00221
00222 if (params->Transform8x8Mode != 2)
00223 {
00224 tr4x4.mb_p8x8_cost = 0;
00225
00226
00227
00228
00229 for (cost_direct = cbp8x8 = cbp_blk8x8 = cnt_nonz_8x8 = 0, block = 0; block < 4; block++)
00230 {
00231 submacroblock_mode_decision(currSlice, &enc_mb, &tr4x4, currMB, coefAC8x8[block],
00232 &have_direct, bslice, block, &cost_direct, &cost, &cost8x8_direct, 0, is_cavlc);
00233 set_subblock8x8_info(b8x8info, P8x8, block, &tr4x4);
00234 }
00235
00236
00237 }
00238
00239
00240 reset_coding_state (currSlice, currMB, cs_mb);
00241
00242 if (params->RCEnable)
00243 rc_store_diff(img->opix_x, img->opix_y, mb_pred);
00244
00245 giRDOpt_B8OnlyFlag = 0;
00246 }
00247 else
00248 {
00249 tr4x4.mb_p8x8_cost = INT_MAX;
00250 }
00251
00252
00253 if(pslice)
00254 FindSkipModeMotionVector (currMB);
00255 }
00256 else
00257 {
00258 min_cost = INT_MAX;
00259 }
00260
00261
00262
00263 if ((img->yuv_format != YUV400) && !IS_INDEPENDENT(params))
00264 {
00265
00266 IntraChromaPrediction(currMB, &mb_available_up, &mb_available_left, &mb_available_up_left);
00267
00268 if (params->FastCrIntraDecision)
00269 {
00270 IntraChromaRDDecision(currMB, enc_mb);
00271
00272 min_chroma_pred_mode = (short) currMB->c_ipred_mode;
00273 max_chroma_pred_mode = (short) currMB->c_ipred_mode;
00274 }
00275 else
00276 {
00277 min_chroma_pred_mode = DC_PRED_8;
00278 max_chroma_pred_mode = PLANE_8;
00279 }
00280 }
00281 else
00282 {
00283 min_chroma_pred_mode = DC_PRED_8;
00284 max_chroma_pred_mode = DC_PRED_8;
00285 }
00286
00287 for (currMB->c_ipred_mode=min_chroma_pred_mode; currMB->c_ipred_mode<=max_chroma_pred_mode; currMB->c_ipred_mode++)
00288 {
00289
00290 if ( (img->yuv_format != YUV400) &&
00291 ( ((!intra || !params->IntraDisableInterOnly) && params->ChromaIntraDisable == 1 && currMB->c_ipred_mode!=DC_PRED_8)
00292 || (currMB->c_ipred_mode == VERT_PRED_8 && !mb_available_up)
00293 || (currMB->c_ipred_mode == HOR_PRED_8 && !mb_available_left)
00294 || (currMB->c_ipred_mode == PLANE_8 && (!mb_available_left || !mb_available_up || !mb_available_up_left))))
00295 continue;
00296
00297
00298 for (index=0; index < max_index; index++)
00299 {
00300 mode = mb_mode_table[index];
00301 if (img->yuv_format != YUV400)
00302 {
00303 i16mode = 0;
00304 }
00305
00306
00307 if (img->P444_joined)
00308 {
00309 if (params->SkipIntraInInterSlices && !intra && mode >= I16MB
00310 && best_mode <=3 && currMB->cbp == 0 && cmp_cbp[1] == 0 && cmp_cbp[2] == 0)
00311 continue;
00312 }
00313 else
00314 {
00315 if (params->SkipIntraInInterSlices && !intra && mode >= I4MB && best_mode <=3 && currMB->cbp == 0)
00316 continue;
00317 }
00318
00319
00320 if (CheckPredictionParams(active_pps, b8x8info, mode, bslice) == 0)
00321 continue;
00322
00323 if (enc_mb.valid[mode])
00324 compute_mode_RD_cost(currSlice, currMB, &enc_mb, mode, &min_rdcost, &min_dcost, &min_rate, i16mode, bslice, &inter_skip, is_cavlc);
00325 }
00326 }
00327
00328 #ifdef BEST_NZ_COEFF
00329 for (j=0;j<4;j++)
00330 for (i=0; i<(4+img->num_blk8x8_uv); i++)
00331 img->nz_coeff[currMB->mb_nr][j][i] = gaaiMBAFF_NZCoeff[j][i];
00332 #endif
00333
00334 intra1 = IS_INTRA(currMB);
00335
00336
00337
00338 update_qp_cbp_tmp(currMB, cbp, best_mode);
00339 set_stored_macroblock_parameters (currMB);
00340
00341
00342 if(params->RCEnable && params->RCUpdateMode <= MAX_RC_MODE)
00343 rc_store_mad(currMB);
00344 update_qp_cbp(currMB, best_mode);
00345
00346 rdopt->min_rdcost = min_rdcost;
00347 rdopt->min_dcost = min_dcost;
00348
00349 if ( (img->MbaffFrameFlag)
00350 && (currMB->mb_nr & 0x01)
00351 && (currMB->mb_type ? 0:((bslice) ? !currMB->cbp:1))
00352 && (prevMB->mb_type ? 0:((bslice) ? !prevMB->cbp:1))
00353 && !(field_flag_inference(currMB) == enc_mb.curr_mb_field))
00354 {
00355 rdopt->min_rdcost = 1e30;
00356 }
00357
00358
00359 if (params->RestrictRef)
00360 update_refresh_map(intra, intra1, currMB);
00361
00362 if(params->SearchMode == UM_HEX)
00363 {
00364 UMHEX_skip_intrabk_SAD(best_mode, listXsize[enc_mb.list_offset[LIST_0]]);
00365 }
00366 else if(params->SearchMode == UM_HEX_SIMPLE)
00367 {
00368 smpUMHEX_skip_intrabk_SAD(best_mode, listXsize[enc_mb.list_offset[LIST_0]]);
00369 }
00370
00371
00372 if(params->UseConstrainedIntraPred && (img->type==P_SLICE || img->type==B_SLICE))
00373 {
00374 img->intra_block[currMB->mb_nr] = IS_INTRA(currMB);
00375 }
00376 }
00377
00378
00379
00380