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 "ratectl.h"
00022 #include "mode_decision.h"
00023 #include "fmo.h"
00024 #include "me_umhex.h"
00025 #include "me_umhexsmp.h"
00026 #include "macroblock.h"
00027 #include "conformance.h"
00028 #include "vlc.h"
00029 #include "rdopt.h"
00030
00031
00032
00033
00034
00035
00036
00037
00038 static void fast_mode_intra_decision(Macroblock *currMB, short *intra_skip, double min_rate)
00039 {
00040 int i;
00041 int mb_available_up, mb_available_left, mb_available_up_left;
00042 long SBE;
00043 double AR = 0, ABE = 0;
00044 PixelPos up;
00045 PixelPos left[2];
00046
00047 getNeighbour(currMB, 0 , -1 , img->mb_size[IS_LUMA], &up);
00048 getNeighbour(currMB, -1 , -1 , img->mb_size[IS_LUMA], &left[0]);
00049 getNeighbour(currMB, -1 , 0 , img->mb_size[IS_LUMA], &left[1]);
00050
00051 mb_available_up = up.available;
00052 mb_available_up_left = left[0].available;
00053 mb_available_left = left[1].available;
00054
00055 AR=(1.0/384)*min_rate;
00056
00057 SBE = 0;
00058
00059 if( (img->mb_y != (int)img->FrameHeightInMbs-1) && (img->mb_x != (int)img->PicWidthInMbs-1) && mb_available_left && mb_available_up)
00060 {
00061 for(i = 0; i < MB_BLOCK_SIZE; i++)
00062 {
00063 SBE += iabs(pCurImg[img->opix_y][img->opix_x+i] - enc_picture->imgY[img->pix_y-1][img->pix_x+i]);
00064 SBE += iabs(pCurImg[img->opix_y+i][img->opix_x] - enc_picture->imgY[img->pix_y+i][img->pix_x-1]);
00065 }
00066
00067 for(i = 0; i < 8; i++)
00068 {
00069 SBE += iabs(pImgOrg[1][img->opix_c_y][img->opix_c_x+i] - enc_picture->imgUV[0][img->pix_c_y-1][img->pix_c_x+i]);
00070 SBE += iabs(pImgOrg[1][img->opix_c_y+i][img->opix_c_x] - enc_picture->imgUV[0][img->pix_c_y+i][img->pix_c_x-1]);
00071 SBE += iabs(pImgOrg[2][img->opix_c_y][img->opix_c_x+i] - enc_picture->imgUV[1][img->pix_c_y-1][img->pix_c_x+i]);
00072 SBE += iabs(pImgOrg[2][img->opix_c_y+i][img->opix_c_x] - enc_picture->imgUV[1][img->pix_c_y+i][img->pix_c_x-1]);
00073 }
00074 ABE = 1.0/64 * SBE;
00075 }
00076 else
00077 {
00078 ABE = 0;
00079 }
00080
00081 if(AR <= ABE)
00082 {
00083 *intra_skip = 1;
00084 }
00085 }
00086
00087
00088
00089
00090
00091
00092
00093 void encode_one_macroblock_highfast (Slice *currSlice, Macroblock *currMB)
00094 {
00095 int max_index;
00096
00097 int block, index, mode, i, j, ctr16x16;
00098 char best_pdir;
00099 RD_PARAMS enc_mb;
00100 double min_rdcost = 1e30, max_rdcost = 1e30;
00101 double min_dcost = 1e30;
00102 char best_ref[2] = {0, -1};
00103 int bmcost[5] = {INT_MAX};
00104 int cost=0;
00105 int min_cost = INT_MAX, cost_direct=0, have_direct=0, i16mode=0;
00106 int intra1 = 0;
00107 int lambda_mf[3];
00108 int cost8x8_direct = 0;
00109 short islice = (img->type==I_SLICE);
00110 short bslice = (img->type==B_SLICE);
00111 short pslice = (img->type==P_SLICE) || (img->type==SP_SLICE);
00112 short intra = (islice || (pslice && img->mb_y==img->mb_y_upd && img->mb_y_upd!=img->mb_y_intra));
00113 int is_cavlc = (img->currentSlice->symbol_mode == CAVLC);
00114 Macroblock *prevMB = currMB->PrevMB;
00115 Block8x8Info *b8x8info = img->b8x8info;
00116
00117 short *allmvs = img->all_mv[0][0][0][0][0];
00118 short min_chroma_pred_mode, max_chroma_pred_mode;
00119 imgpel **mb_pred = img->mb_pred[0];
00120
00121
00122 short inter_skip = 0, intra_skip = 0;
00123 short bipred_me;
00124 int cost16 = 0, mode16 = 0;
00125 double min_rate = 0, RDCost16 = DBL_MAX;
00126
00127
00128 if(params->SearchMode == UM_HEX)
00129 {
00130 UMHEX_decide_intrabk_SAD();
00131 }
00132 else if (params->SearchMode == UM_HEX_SIMPLE)
00133 {
00134 smpUMHEX_decide_intrabk_SAD();
00135 }
00136
00137 if (params->AdaptiveRounding)
00138 {
00139 reset_adaptive_rounding();
00140 }
00141
00142 if (img->MbaffFrameFlag)
00143 {
00144 reset_mb_nz_coeff(img->current_mb_nr);
00145 }
00146
00147 intra |= RandomIntra (img->current_mb_nr);
00148
00149
00150 init_enc_mb_params(currMB, &enc_mb, intra, bslice);
00151
00152
00153
00154 store_coding_state (currSlice, currMB, cs_cm);
00155
00156 if (!intra)
00157 {
00158
00159 best_mode = 1;
00160 if (bslice)
00161 {
00162 Get_Direct_Motion_Vectors (currMB);
00163 if (enc_mb.valid[0])
00164 {
00165 best_mode = 0;
00166 currMB->c_ipred_mode=DC_PRED_8;
00167 min_rdcost = max_rdcost;
00168 compute_mode_RD_cost(currSlice, currMB, &enc_mb, 0, &min_rdcost, &min_dcost, &min_rate, i16mode, bslice, &inter_skip, is_cavlc);
00169 }
00170 }
00171
00172 if (params->CtxAdptLagrangeMult == 1)
00173 {
00174 get_initial_mb16x16_cost(currMB);
00175 }
00176
00177
00178 for (min_cost=INT_MAX, mode=1; mode<4; mode++)
00179 {
00180 bipred_me = 0;
00181 b8x8info->bipred8x8me[mode][0] = 0;
00182 if (enc_mb.valid[mode] && !inter_skip)
00183 {
00184 for (cost=0, block=0; block<(mode==1?1:2); block++)
00185 {
00186 update_lambda_costs(&enc_mb, lambda_mf);
00187 PartitionMotionSearch (currMB, mode, block, lambda_mf);
00188
00189
00190 j = (block==1 && mode==2 ? 2 : 0);
00191 i = (block==1 && mode==3 ? 2 : 0);
00192
00193
00194 bmcost[LIST_0] = INT_MAX;
00195 list_prediction_cost(currMB, LIST_0, block, mode, &enc_mb, bmcost, best_ref);
00196
00197 if (bslice)
00198 {
00199
00200 bmcost[LIST_1] = INT_MAX;
00201 list_prediction_cost(currMB, LIST_1, block, mode, &enc_mb, bmcost, best_ref);
00202
00203
00204 list_prediction_cost(currMB, BI_PRED, block, mode, &enc_mb, bmcost, best_ref);
00205
00206
00207 if (is_bipred_enabled(mode))
00208 {
00209 list_prediction_cost(currMB, BI_PRED_L0, block, mode, &enc_mb, bmcost, 0);
00210 list_prediction_cost(currMB, BI_PRED_L1, block, mode, &enc_mb, bmcost, 0);
00211 }
00212 else
00213 {
00214 bmcost[BI_PRED_L0] = INT_MAX;
00215 bmcost[BI_PRED_L1] = INT_MAX;
00216 }
00217
00218
00219 determine_prediction_list(mode, bmcost, best_ref, &best_pdir, &cost, &bipred_me);
00220 }
00221 else
00222 {
00223 best_pdir = 0;
00224 cost += bmcost[LIST_0];
00225 }
00226
00227 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);
00228
00229
00230 set_block8x8_info(b8x8info, mode, block, best_ref, best_pdir, bipred_me);
00231
00232
00233 if (mode>1 && block==0)
00234 SetRefAndMotionVectors (&enc_picture->motion, currMB, block, mode, best_pdir, best_ref[LIST_0], best_ref[LIST_1], bipred_me);
00235 }
00236
00237
00238 if(mode == 1)
00239 {
00240 if(pslice)
00241 min_rdcost = max_rdcost;
00242
00243
00244
00245
00246
00247
00248 currMB->c_ipred_mode=DC_PRED_8;
00249 compute_mode_RD_cost(currSlice, currMB, &enc_mb, mode, &min_rdcost, &min_dcost, &min_rate, i16mode, bslice, &inter_skip, is_cavlc);
00250
00251 if(pslice)
00252 {
00253
00254 FindSkipModeMotionVector (currMB);
00255 if(params->EarlySkipEnable)
00256 {
00257
00258 if ( currMB->cbp==0 && enc_picture->motion.ref_idx[LIST_0][img->block_y][img->block_x]==0 &&
00259 enc_picture->motion.mv[LIST_0][img->block_y][img->block_x][0]==allmvs[0] &&
00260 enc_picture->motion.mv[LIST_0][img->block_y][img->block_x][1]==allmvs[1] )
00261 {
00262 inter_skip = 1;
00263 best_mode = 0;
00264 }
00265 }
00266 }
00267
00268
00269 RDCost16 = min_rdcost;
00270 mode16 = best_mode;
00271 cost16 = cost;
00272 }
00273
00274 if ((!inter_skip) && (cost < min_cost))
00275 {
00276 best_mode = mode;
00277 min_cost = cost;
00278 if (params->CtxAdptLagrangeMult == 1)
00279 {
00280 adjust_mb16x16_cost(cost);
00281 }
00282 }
00283 }
00284 }
00285
00286 if ((!inter_skip) && enc_mb.valid[P8x8])
00287 {
00288 giRDOpt_B8OnlyFlag = 1;
00289
00290 tr8x8.mb_p8x8_cost = INT_MAX;
00291 tr4x4.mb_p8x8_cost = INT_MAX;
00292
00293 store_coding_state (currSlice, currMB, cs_mb);
00294
00295 currMB->all_blk_8x8 = -1;
00296
00297 if (params->Transform8x8Mode)
00298 {
00299 tr8x8.mb_p8x8_cost = 0;
00300
00301
00302
00303
00304 for (cost_direct=cbp8x8=cbp_blk8x8=cnt_nonz_8x8=0, block=0; block<4; block++)
00305 {
00306 submacroblock_mode_decision(currSlice, &enc_mb, &tr8x8, currMB, cofAC8x8ts[block],
00307 &have_direct, bslice, block, &cost_direct, &cost, &cost8x8_direct, 1, is_cavlc);
00308 set_subblock8x8_info(b8x8info, P8x8, block, &tr8x8);
00309 }
00310
00311
00312 cbp8_8x8ts = cbp8x8;
00313 cbp_blk8_8x8ts = cbp_blk8x8;
00314 cnt_nonz8_8x8ts = cnt_nonz_8x8;
00315 currMB->luma_transform_size_8x8_flag = 0;
00316
00317
00318 reset_coding_state (currSlice, currMB, cs_mb);
00319 }
00320
00321
00322 if (params->Transform8x8Mode != 2)
00323 {
00324 tr4x4.mb_p8x8_cost = 0;
00325
00326
00327
00328
00329 for (cost_direct=cbp8x8=cbp_blk8x8=cnt_nonz_8x8=0, block=0; block<4; block++)
00330 {
00331 submacroblock_mode_decision(currSlice, &enc_mb, &tr4x4, currMB, coefAC8x8[block],
00332 &have_direct, bslice, block, &cost_direct, &cost, &cost8x8_direct, 0, is_cavlc);
00333 set_subblock8x8_info(b8x8info, P8x8, block, &tr4x4);
00334 }
00335
00336
00337 }
00338
00339
00340 reset_coding_state (currSlice, currMB, cs_mb);
00341
00342
00343 if (params->RCEnable)
00344 rc_store_diff(img->opix_x, img->opix_y, mb_pred);
00345
00346
00347 giRDOpt_B8OnlyFlag = 0;
00348 }
00349 else
00350 {
00351 tr4x4.mb_p8x8_cost = INT_MAX;
00352 }
00353
00354 }
00355 else
00356 {
00357 min_cost = INT_MAX;
00358 }
00359
00360
00361
00362 {
00363
00364 if (!inter_skip)
00365 {
00366 int mb_available_up;
00367 int mb_available_left;
00368 int mb_available_up_left;
00369
00370 if(img->type!=I_SLICE)
00371 {
00372 min_rdcost = RDCost16;
00373 best_mode = mode16;
00374 }
00375 else
00376 min_rdcost = max_rdcost;
00377
00378
00379 max_index = ((!intra && params->SelectiveIntraEnable ) ? 5 : 9);
00380
00381 if (((img->yuv_format != YUV400) && !IS_INDEPENDENT(params)) && max_index != 5)
00382 {
00383
00384 IntraChromaPrediction(currMB, &mb_available_up, &mb_available_left, &mb_available_up_left);
00385
00386 if (params->FastCrIntraDecision)
00387 {
00388 IntraChromaRDDecision(currMB, enc_mb);
00389
00390 min_chroma_pred_mode = (short) currMB->c_ipred_mode;
00391 max_chroma_pred_mode = (short) currMB->c_ipred_mode;
00392 }
00393 else
00394 {
00395 min_chroma_pred_mode = DC_PRED_8;
00396 max_chroma_pred_mode = PLANE_8;
00397 }
00398 }
00399 else
00400 {
00401 min_chroma_pred_mode = DC_PRED_8;
00402 max_chroma_pred_mode = DC_PRED_8;
00403 }
00404
00405 for (currMB->c_ipred_mode=min_chroma_pred_mode; currMB->c_ipred_mode<=max_chroma_pred_mode; currMB->c_ipred_mode++)
00406 {
00407
00408 if ( (img->yuv_format != YUV400) &&
00409 ( ((!intra || !params->IntraDisableInterOnly) && params->ChromaIntraDisable == 1 && currMB->c_ipred_mode!=DC_PRED_8)
00410 || (currMB->c_ipred_mode == VERT_PRED_8 && !mb_available_up)
00411 || (currMB->c_ipred_mode == HOR_PRED_8 && !mb_available_left)
00412 || (currMB->c_ipred_mode == PLANE_8 && (!mb_available_left || !mb_available_up || !mb_available_up_left))))
00413 continue;
00414
00415
00416 for (ctr16x16=0, index=0; index < max_index; index++)
00417 {
00418 mode = mb_mode_table[index];
00419 if (img->yuv_format != YUV400)
00420 {
00421 i16mode = 0;
00422 }
00423
00424
00425 if (img->P444_joined)
00426 {
00427 if (params->SkipIntraInInterSlices && !intra && mode >= I16MB
00428 && best_mode <=3 && currMB->cbp == 0 && cmp_cbp[1] == 0 && cmp_cbp[2] == 0)
00429 continue;
00430 }
00431 else
00432 {
00433 if (params->SkipIntraInInterSlices && !intra && mode >= I4MB && best_mode <=3 && currMB->cbp == 0)
00434 continue;
00435 }
00436
00437
00438 if (CheckPredictionParams(active_pps, b8x8info, mode, bslice) == 0)
00439 continue;
00440
00441 if (enc_mb.valid[mode])
00442 compute_mode_RD_cost(currSlice, currMB, &enc_mb, mode, &min_rdcost, &min_dcost, &min_rate, i16mode, bslice, &inter_skip, is_cavlc);
00443 }
00444 }
00445
00446
00447
00448 if(img->type!=I_SLICE && params->SelectiveIntraEnable && !IS_FREXT_PROFILE(params->ProfileIDC))
00449 {
00450 fast_mode_intra_decision(currMB, &intra_skip, min_rate);
00451
00452 if(!intra_skip)
00453 {
00454
00455 if ((img->yuv_format != YUV400) && !IS_INDEPENDENT(params))
00456 {
00457
00458 IntraChromaPrediction(currMB, &mb_available_up, &mb_available_left, &mb_available_up_left);
00459
00460 if (params->FastCrIntraDecision)
00461 {
00462 IntraChromaRDDecision(currMB, enc_mb);
00463 min_chroma_pred_mode = currMB->c_ipred_mode;
00464 max_chroma_pred_mode = currMB->c_ipred_mode;
00465 }
00466 else
00467 {
00468 min_chroma_pred_mode = DC_PRED_8;
00469 max_chroma_pred_mode = PLANE_8;
00470 }
00471 }
00472 else
00473 {
00474 min_chroma_pred_mode = DC_PRED_8;
00475 max_chroma_pred_mode = DC_PRED_8;
00476 }
00477
00478 max_index = 9;
00479
00480 for (currMB->c_ipred_mode=min_chroma_pred_mode; currMB->c_ipred_mode<=max_chroma_pred_mode; currMB->c_ipred_mode++)
00481 {
00482
00483
00484 if ( (img->yuv_format != YUV400) &&
00485 ( ((!intra || !params->IntraDisableInterOnly) && params->ChromaIntraDisable == 1 && currMB->c_ipred_mode!=DC_PRED_8)
00486 || (currMB->c_ipred_mode == VERT_PRED_8 && !mb_available_up)
00487 || (currMB->c_ipred_mode == HOR_PRED_8 && !mb_available_left)
00488 || (currMB->c_ipred_mode == PLANE_8 && (!mb_available_left || !mb_available_up || !mb_available_up_left))))
00489 continue;
00490
00491
00492 for (index = 5; index < max_index; index++)
00493 {
00494 mode = mb_mode_table[index];
00495
00496
00497 if (params->SkipIntraInInterSlices && !intra && mode >= I4MB && best_mode <=3 && currMB->cbp == 0)
00498 continue;
00499
00500 if (img->yuv_format != YUV400)
00501 {
00502 i16mode = 0;
00503
00504 if(((bslice && mode == 0) || (!islice && mode == 1)))
00505 continue;
00506 }
00507
00508 if (enc_mb.valid[mode])
00509 compute_mode_RD_cost(currSlice, currMB, &enc_mb, mode, &min_rdcost, &min_dcost, &min_rate, i16mode, bslice, &inter_skip, is_cavlc);
00510 }
00511 }
00512 }
00513 }
00514 }
00515 #ifdef BEST_NZ_COEFF
00516 for (j=0;j<4;j++)
00517 for (i=0; i<(4+img->num_blk8x8_uv); i++)
00518 img->nz_coeff[img->current_mb_nr][j][i] = gaaiMBAFF_NZCoeff[j][i];
00519 #endif
00520 }
00521
00522 intra1 = IS_INTRA(currMB);
00523
00524
00525
00526 update_qp_cbp_tmp(currMB, cbp, best_mode);
00527 set_stored_macroblock_parameters (currMB);
00528
00529
00530 if(params->RCEnable && params->RCUpdateMode <= MAX_RC_MODE)
00531 rc_store_mad(currMB);
00532 update_qp_cbp(currMB, best_mode);
00533
00534 rdopt->min_rdcost = min_rdcost;
00535 rdopt->min_dcost = min_dcost;
00536
00537 if ( (img->MbaffFrameFlag)
00538 && (img->current_mb_nr%2)
00539 && (currMB->mb_type ? 0:((bslice) ? !currMB->cbp:1))
00540 && (prevMB->mb_type ? 0:((bslice) ? !prevMB->cbp:1))
00541 && !(field_flag_inference(currMB) == enc_mb.curr_mb_field))
00542 {
00543 rdopt->min_rdcost = 1e30;
00544 }
00545
00546
00547 if (params->RestrictRef)
00548 update_refresh_map(intra, intra1, currMB);
00549
00550 if(params->SearchMode == UM_HEX)
00551 {
00552 UMHEX_skip_intrabk_SAD(best_mode, listXsize[enc_mb.list_offset[LIST_0]]);
00553 }
00554 else if(params->SearchMode == UM_HEX_SIMPLE)
00555 {
00556 smpUMHEX_skip_intrabk_SAD(best_mode, listXsize[enc_mb.list_offset[LIST_0]]);
00557 }
00558
00559
00560 if(params->UseConstrainedIntraPred && (img->type==P_SLICE || img->type==B_SLICE))
00561 {
00562 img->intra_block[img->current_mb_nr] = IS_INTRA(currMB);
00563 }
00564 }
00565