00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <limits.h>
00019
00020 #include "global.h"
00021
00022 #include "image.h"
00023 #include "macroblock.h"
00024 #include "mb_access.h"
00025 #include "rdopt_coding_state.h"
00026 #include "mode_decision.h"
00027 #include "rdopt.h"
00028 #include "rd_intra_jm.h"
00029 #include "q_around.h"
00030 #include "intra4x4.h"
00031
00032
00033
00034
00035
00036
00037
00038 int Mode_Decision_for_4x4IntraBlocks_JM_High444 (Macroblock *currMB, int b8, int b4, double lambda, double* min_cost)
00039 {
00040 ImageParameters *p_Img = currMB->p_Img;
00041 InputParameters *p_Inp = currMB->p_Inp;
00042 Slice *currSlice = currMB->p_slice;
00043 RDOPTStructure *p_RDO = currSlice->p_RDO;
00044
00045 int ipmode, best_ipmode = 0, i, j, y, dummy;
00046 int c_nz, nonzero = 0;
00047 int* ACLevel = currSlice->cofAC[b8][b4][0];
00048 int* ACRun = currSlice->cofAC[b8][b4][1];
00049 int uv;
00050
00051 double rdcost = 0.0;
00052 int block_x = ((b8 & 0x01) << 3) + ((b4 & 0x01) << 2);
00053 int block_y = ((b8 >> 1) << 3) + ((b4 >> 1) << 2);
00054 int pic_pix_x = currMB->pix_x + block_x;
00055 int pic_pix_y = currMB->pix_y + block_y;
00056 int pic_opix_x = currMB->pix_x + block_x;
00057 int pic_opix_y = currMB->opix_y + block_y;
00058 int pic_block_x = pic_pix_x >> 2;
00059 int pic_block_y = pic_pix_y >> 2;
00060 double min_rdcost = 1e30;
00061
00062 int left_available, up_available, all_available;
00063 int *mb_size = p_Img->mb_size[IS_LUMA];
00064
00065 char upMode, leftMode;
00066 int mostProbableMode;
00067
00068 PixelPos left_block, top_block;
00069
00070 int lrec4x4[4][4];
00071
00072 #ifdef BEST_NZ_COEFF
00073 int best_nz_coeff = 0;
00074 int best_coded_block_flag = 0;
00075 int bit_pos = 1 + ((((b8>>1)<<1)+(b4>>1))<<2) + (((b8&1)<<1)+(b4&1));
00076 int64 cbp_bits;
00077
00078 if (b8==0 && b4==0)
00079 cbp_bits = 0;
00080 #endif
00081
00082 get4x4Neighbour(currMB, block_x - 1, block_y , mb_size, &left_block);
00083 get4x4Neighbour(currMB, block_x, block_y - 1, mb_size, &top_block );
00084
00085
00086 if (p_Inp->UseConstrainedIntraPred)
00087 {
00088 left_block.available = left_block.available ? p_Img->intra_block[left_block.mb_addr] : 0;
00089 top_block.available = top_block.available ? p_Img->intra_block[top_block.mb_addr] : 0;
00090 }
00091
00092 upMode = top_block.available ? p_Img->ipredmode[top_block.pos_y ][top_block.pos_x ] : -1;
00093 leftMode = left_block.available ? p_Img->ipredmode[left_block.pos_y][left_block.pos_x] : -1;
00094
00095 mostProbableMode = (upMode < 0 || leftMode < 0) ? DC_PRED : upMode < leftMode ? upMode : leftMode;
00096
00097 *min_cost = INT_MAX;
00098
00099 currMB->ipmode_DPCM = NO_INTRA_PMODE;
00100
00101
00102
00103 set_intrapred_4x4(currMB, PLANE_Y, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
00104
00105 if (currSlice->P444_joined)
00106 {
00107 select_plane(p_Img, PLANE_U);
00108 set_intrapred_4x4(currMB, PLANE_U, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
00109 select_plane(p_Img, PLANE_V);
00110 set_intrapred_4x4(currMB, PLANE_V, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
00111 select_plane(p_Img, PLANE_Y);
00112 }
00113
00114
00115 for (ipmode = 0; ipmode < NO_INTRA_PMODE; ipmode++)
00116 {
00117 int available_mode = (all_available) || (ipmode==DC_PRED) ||
00118 (up_available && (ipmode==VERT_PRED||ipmode==VERT_LEFT_PRED||ipmode==DIAG_DOWN_LEFT_PRED)) ||
00119 (left_available && (ipmode==HOR_PRED||ipmode==HOR_UP_PRED));
00120
00121 if (valid_intra_mode(currSlice, ipmode) == 0)
00122 continue;
00123
00124 if( available_mode)
00125 {
00126
00127 get_intrapred_4x4(currMB, PLANE_Y, ipmode, block_x, block_y, left_available, up_available);
00128
00129
00130 generate_pred_error_4x4(&p_Img->pCurImg[pic_opix_y], currSlice->mpr_4x4[0][ipmode], &currSlice->mb_pred[0][block_y], &currSlice->mb_ores[0][block_y], pic_opix_x, block_x);
00131
00132 if (p_Img->yuv_format == YUV444)
00133 {
00134 currMB->ipmode_DPCM = (short) ipmode;
00135 if (!IS_INDEPENDENT(p_Inp))
00136 {
00137
00138 get_intrapred_4x4(currMB, PLANE_U, ipmode, block_x, block_y, left_available, up_available);
00139 generate_pred_error_4x4(&p_Img->pImgOrg[1][pic_opix_y], currSlice->mpr_4x4[1][ipmode], &currSlice->mb_pred[1][block_y], &currSlice->mb_ores[1][block_y], pic_opix_x, block_x);
00140
00141 get_intrapred_4x4(currMB, PLANE_V, ipmode, block_x, block_y, left_available, up_available);
00142 generate_pred_error_4x4(&p_Img->pImgOrg[2][pic_opix_y], currSlice->mpr_4x4[2][ipmode], &currSlice->mb_pred[2][block_y], &currSlice->mb_ores[2][block_y], pic_opix_x, block_x);
00143 }
00144 }
00145
00146
00147 #ifdef BEST_NZ_COEFF
00148 currMB->cbp_bits[0] = cbp_bits;
00149 #endif
00150
00151 if ((rdcost = currSlice->rdcost_for_4x4_intra_blocks (currMB, &c_nz, b8, b4, ipmode, lambda, mostProbableMode, min_rdcost)) < min_rdcost)
00152 {
00153
00154 memcpy(p_RDO->cofAC4x4[0], ACLevel, 18 * sizeof(int));
00155 memcpy(p_RDO->cofAC4x4[1], ACRun, 18 * sizeof(int));
00156
00157
00158 copy_4x4block(p_RDO->rec4x4[PLANE_Y], &p_Img->enc_picture->imgY[pic_pix_y], 0, pic_pix_x);
00159
00160
00161 if(currSlice->slice_type == SP_SLICE &&(!p_Img->si_frame_indicator && !p_Img->sp2_frame_indicator))
00162 {
00163 for (y=0; y<4; y++)
00164 {
00165 memcpy(lrec4x4[y],&p_Img->lrec[pic_pix_y+y][pic_pix_x], BLOCK_SIZE * sizeof(int));
00166 }
00167 }
00168
00169 if(currSlice->P444_joined)
00170 {
00171
00172 for (uv=0; uv < 2; uv++)
00173 {
00174 memcpy(p_RDO->cofAC4x4CbCr[uv][0],currSlice->cofAC[b8+4+uv*4][b4][0], 18 * sizeof(int));
00175 memcpy(p_RDO->cofAC4x4CbCr[uv][1],currSlice->cofAC[b8+4+uv*4][b4][1], 18 * sizeof(int));
00176 currMB->cr_cbp[uv + 1] = currMB->c_nzCbCr[uv + 1];
00177
00178
00179 copy_4x4block(p_RDO->rec4x4[uv + 1], &p_Img->enc_picture->imgUV[uv][pic_pix_y], 0, pic_pix_x);
00180 }
00181 }
00182
00183 nonzero = c_nz;
00184
00185
00186 *min_cost = rdcost;
00187 min_rdcost = rdcost;
00188 best_ipmode = ipmode;
00189 #ifdef BEST_NZ_COEFF
00190 best_nz_coeff = p_Img->nz_coeff [p_Img->current_mb_nr][block_x4][block_y4];
00191 best_coded_block_flag = (int)((currMB->cbp_bits[0] >> bit_pos)&(int64)(1));
00192 #endif
00193 if (p_Img->AdaptiveRounding)
00194 {
00195 store_adaptive_rounding_4x4 (p_Img, p_Img->ARCofAdj4x4, I4MB, block_y, block_x);
00196 }
00197 }
00198 }
00199 }
00200
00201 #ifdef BEST_NZ_COEFF
00202 p_Img->nz_coeff [p_Img->current_mb_nr][block_x4][block_y4] = best_nz_coeff;
00203 cbp_bits &= (~(int64)(1<<bit_pos));
00204 cbp_bits |= (int64)(best_coded_block_flag<<bit_pos);
00205 #endif
00206
00207
00208 p_Img->ipredmode[pic_block_y][pic_block_x] = (char) best_ipmode;
00209 currMB->intra_pred_modes[4*b8+b4] =
00210 (char) (mostProbableMode == best_ipmode ? -1 : (best_ipmode < mostProbableMode ? best_ipmode : best_ipmode-1));
00211
00212 if(currSlice->P444_joined)
00213 {
00214 ColorPlane k;
00215 for (k = PLANE_U; k <= PLANE_V; k++)
00216 {
00217 select_plane(p_Img, k);
00218
00219 copy_4x4block(&currSlice->mb_pred[k][block_y], currSlice->mpr_4x4[k][best_ipmode], block_x, 0);
00220 for (j=0; j<4; j++)
00221 {
00222 for (i=0; i<4; i++)
00223 {
00224 currSlice->mb_ores[k][block_y+j][block_x+i] = p_Img->pImgOrg[k][currMB->pix_y+block_y+j][currMB->pix_x+block_x+i] - currSlice->mpr_4x4[k][best_ipmode][j][i];
00225 }
00226 }
00227 currMB->cr_cbp[k] = currMB->trans_4x4(currMB, k, block_x,block_y,&dummy,1);
00228 }
00229 select_plane(p_Img, PLANE_Y);
00230 }
00231
00232
00233 memcpy (ACLevel, p_RDO->cofAC4x4[0], 18 * sizeof(int));
00234 memcpy (ACRun, p_RDO->cofAC4x4[1], 18 * sizeof(int));
00235
00236
00237 copy_4x4block(&p_Img->enc_picture->imgY[pic_pix_y], p_RDO->rec4x4[PLANE_Y], pic_pix_x, 0);
00238 copy_4x4block(&currSlice->mb_pred[0][block_y], currSlice->mpr_4x4[0][best_ipmode], block_x, 0);
00239
00240
00241 if(currSlice->slice_type == SP_SLICE &&(!p_Img->si_frame_indicator && !p_Img->sp2_frame_indicator))
00242 {
00243 for (y=0; y<BLOCK_SIZE; y++)
00244 {
00245 memcpy (&p_Img->lrec[pic_pix_y+y][pic_pix_x], lrec4x4[y], BLOCK_SIZE * sizeof(int));
00246 }
00247 }
00248 if (currSlice->P444_joined)
00249 {
00250 for (uv=0; uv < 2; uv++ )
00251 {
00252
00253 memcpy(currSlice->cofAC[b8+4+uv*4][b4][0], p_RDO->cofAC4x4CbCr[uv][0], 18 * sizeof(int));
00254 memcpy(currSlice->cofAC[b8+4+uv*4][b4][1], p_RDO->cofAC4x4CbCr[uv][1], 18 * sizeof(int));
00255
00256 copy_4x4block(&p_Img->enc_picture->imgUV[uv][pic_pix_y], p_RDO->rec4x4[uv + 1], pic_pix_x, 0);
00257 copy_4x4block(&currSlice->mb_pred[uv + 1][block_y], currSlice->mpr_4x4[uv + 1][best_ipmode], block_x, 0);
00258 }
00259 }
00260
00261 if (p_Img->AdaptiveRounding)
00262 {
00263 update_adaptive_rounding_4x4 (p_Img,p_Img->ARCofAdj4x4, I4MB, block_y, block_x);
00264 }
00265
00266 return nonzero;
00267 }
00268
00269
00270
00271
00272
00273
00274
00275 int Mode_Decision_for_4x4IntraBlocks_JM_Low444 (Macroblock *currMB, int b8, int b4, double lambda, double* min_cost)
00276 {
00277 ImageParameters *p_Img = currMB->p_Img;
00278 InputParameters *p_Inp = currMB->p_Inp;
00279 Slice *currSlice = currMB->p_slice;
00280
00281 int ipmode, best_ipmode = 0, i, j, cost, dummy;
00282 int nonzero = 0;
00283
00284 int block_x = ((b8 & 0x01) << 3) + ((b4 & 0x01) << 2);
00285 int block_y = ((b8 >> 1) << 3) + ((b4 >> 1) << 2);
00286 int pic_pix_x = currMB->pix_x + block_x;
00287 int pic_pix_y = currMB->pix_y + block_y;
00288 int pic_opix_x = currMB->pix_x + block_x;
00289 int pic_opix_y = currMB->opix_y + block_y;
00290 int pic_block_x = pic_pix_x >> 2;
00291 int pic_block_y = pic_pix_y >> 2;
00292
00293 int left_available, up_available, all_available;
00294
00295 char upMode, leftMode;
00296 int mostProbableMode;
00297
00298 PixelPos left_block;
00299 PixelPos top_block;
00300
00301 int fixedcost = (int) floor(4 * lambda );
00302 int *mb_size = p_Img->mb_size[IS_LUMA];
00303
00304
00305 #ifdef BEST_NZ_COEFF
00306 int best_nz_coeff = 0;
00307 int best_coded_block_flag = 0;
00308 int bit_pos = 1 + ((((b8>>1)<<1)+(b4>>1))<<2) + (((b8&1)<<1)+(b4&1));
00309 int64 cbp_bits;
00310
00311 if (b8==0 && b4==0)
00312 cbp_bits = 0;
00313 #endif
00314
00315 get4x4Neighbour(currMB, block_x - 1, block_y , mb_size, &left_block);
00316 get4x4Neighbour(currMB, block_x, block_y - 1, mb_size, &top_block );
00317
00318
00319 if (p_Inp->UseConstrainedIntraPred)
00320 {
00321 left_block.available = left_block.available ? p_Img->intra_block[left_block.mb_addr] : 0;
00322 top_block.available = top_block.available ? p_Img->intra_block[top_block.mb_addr] : 0;
00323 }
00324
00325 upMode = top_block.available ? p_Img->ipredmode[top_block.pos_y ][top_block.pos_x ] : -1;
00326 leftMode = left_block.available ? p_Img->ipredmode[left_block.pos_y][left_block.pos_x] : -1;
00327
00328 mostProbableMode = (upMode < 0 || leftMode < 0) ? DC_PRED : upMode < leftMode ? upMode : leftMode;
00329
00330 *min_cost = INT_MAX;
00331
00332 currMB->ipmode_DPCM = NO_INTRA_PMODE;
00333
00334
00335
00336 set_intrapred_4x4(currMB, PLANE_Y, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
00337
00338 if (currSlice->P444_joined)
00339 {
00340 select_plane(p_Img, PLANE_U);
00341 set_intrapred_4x4(currMB, PLANE_U, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
00342 select_plane(p_Img, PLANE_V);
00343 set_intrapred_4x4(currMB, PLANE_V, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
00344 select_plane(p_Img, PLANE_Y);
00345 }
00346
00347
00348 for (ipmode = 0; ipmode < NO_INTRA_PMODE; ipmode++)
00349 {
00350 int available_mode = (all_available) || (ipmode==DC_PRED) ||
00351 (up_available && (ipmode==VERT_PRED||ipmode==VERT_LEFT_PRED||ipmode==DIAG_DOWN_LEFT_PRED)) ||
00352 (left_available && (ipmode==HOR_PRED||ipmode==HOR_UP_PRED));
00353
00354 if (valid_intra_mode(currSlice, ipmode) == 0)
00355 continue;
00356
00357 if( available_mode)
00358 {
00359
00360
00361
00362 get_intrapred_4x4(currMB, PLANE_Y, ipmode, block_x, block_y, left_available, up_available);
00363 cost = (ipmode == mostProbableMode) ? 0 : fixedcost;
00364 currSlice->compute_cost4x4(p_Img, &p_Img->pCurImg[pic_opix_y], currSlice->mpr_4x4[0][ipmode], pic_opix_x, &cost, (int) *min_cost);
00365
00366 if (currSlice->P444_joined)
00367 {
00368 get_intrapred_4x4(currMB, PLANE_U, ipmode, block_x, block_y, left_available, up_available);
00369 currSlice->compute_cost4x4(p_Img, &p_Img->pImgOrg[1][pic_opix_y], currSlice->mpr_4x4[1][ipmode], pic_opix_x, &cost, (int) *min_cost);
00370 get_intrapred_4x4(currMB, PLANE_V, ipmode, block_x, block_y, left_available, up_available);
00371 currSlice->compute_cost4x4(p_Img, &p_Img->pImgOrg[2][pic_opix_y], currSlice->mpr_4x4[2][ipmode], pic_opix_x, &cost, (int) *min_cost);
00372 }
00373
00374 if (cost < *min_cost)
00375 {
00376 best_ipmode = ipmode;
00377 *min_cost = cost;
00378 }
00379 }
00380 }
00381
00382 #ifdef BEST_NZ_COEFF
00383 p_Img->nz_coeff [p_Img->current_mb_nr][block_x4][block_y4] = best_nz_coeff;
00384 cbp_bits &= (~(int64)(1<<bit_pos));
00385 cbp_bits |= (int64)(best_coded_block_flag<<bit_pos);
00386 #endif
00387
00388 p_Img->ipredmode[pic_block_y][pic_block_x] = (char) best_ipmode;
00389 currMB->intra_pred_modes[4*b8+b4] =
00390 (char) (mostProbableMode == best_ipmode ? -1 : (best_ipmode < mostProbableMode ? best_ipmode : best_ipmode-1));
00391
00392
00393 generate_pred_error_4x4(&p_Img->pCurImg[pic_opix_y], currSlice->mpr_4x4[0][best_ipmode], &currSlice->mb_pred[0][block_y], &currSlice->mb_ores[0][block_y], pic_opix_x, block_x);
00394
00395 currMB->ipmode_DPCM = (short) best_ipmode;
00396
00397 select_dct(currMB);
00398 nonzero = currMB->cr_cbp[0] = currMB->trans_4x4 (currMB, PLANE_Y, block_x, block_y, &dummy, 1);
00399
00400 if (currSlice->P444_joined)
00401 {
00402 ColorPlane k;
00403 for (k = PLANE_U; k <= PLANE_V; k++)
00404 {
00405 select_plane(p_Img, k);
00406 for (j=0; j<4; j++)
00407 {
00408 for (i=0; i<4; i++)
00409 {
00410 currSlice->mb_pred[k][block_y + j][block_x+i] = currSlice->mpr_4x4[k][best_ipmode][j][i];
00411 currSlice->mb_ores[k][block_y + j][block_x+i] = p_Img->pImgOrg[k][pic_opix_y+j][pic_opix_x+i] - currSlice->mpr_4x4[k][best_ipmode][j][i];
00412 }
00413 }
00414
00415 currMB->cr_cbp[k] = currMB->trans_4x4(currMB, k, block_x,block_y,&dummy,1);
00416 }
00417 select_plane(p_Img, PLANE_Y);
00418 }
00419
00420 return nonzero;
00421 }
00422
00423
00424
00425
00426
00427
00428
00429 void Intra16x16_Mode_Decision444 (Macroblock* currMB)
00430 {
00431 Slice *currSlice = currMB->p_slice;
00432 if (!currSlice->P444_joined)
00433 {
00434
00435 intrapred_16x16 (currMB, PLANE_Y);
00436 currSlice->find_sad_16x16 (currMB);
00437 currMB->cbp = currMB->trans_16x16 (currMB, PLANE_Y);
00438 }
00439 else
00440 {
00441 ImageParameters *p_Img = currSlice->p_Img;
00442
00443
00444 intrapred_16x16 (currMB, PLANE_Y);
00445 select_plane(p_Img, PLANE_U);
00446 intrapred_16x16 (currMB, PLANE_U);
00447 select_plane(p_Img, PLANE_V);
00448 intrapred_16x16 (currMB, PLANE_V);
00449 select_plane(p_Img, PLANE_Y);
00450
00451 currSlice->find_sad_16x16 (currMB);
00452
00453 currMB->cbp = currMB->trans_16x16 (currMB, PLANE_Y);
00454 select_plane(p_Img, PLANE_U);
00455 currSlice->cmp_cbp[1] = currMB->trans_16x16 (currMB, PLANE_U);
00456 select_plane(p_Img, PLANE_V);
00457 currSlice->cmp_cbp[2] = currMB->trans_16x16 (currMB, PLANE_V);
00458 select_plane(p_Img, PLANE_Y);
00459
00460 currMB->cbp |= (currSlice->cmp_cbp[1] | currSlice->cmp_cbp[2]);
00461 currSlice->cmp_cbp[1] = currMB->cbp;
00462 currSlice->cmp_cbp[2] = currMB->cbp;
00463 }
00464 }
00465