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