00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <math.h>
00019 #include <limits.h>
00020
00021 #include "global.h"
00022
00023 #include "image.h"
00024 #include "mb_access.h"
00025 #include "elements.h"
00026 #include "vlc.h"
00027 #include "transform8x8.h"
00028 #include "transform.h"
00029 #include "macroblock.h"
00030 #include "symbol.h"
00031 #include "mc_prediction.h"
00032 #include "quant8x8.h"
00033 #include "rdoq.h"
00034 #include "q_matrix.h"
00035 #include "q_offsets.h"
00036 #include "rdopt.h"
00037 #include "md_common.h"
00038 #include "intra8x8.h"
00039 #include "rdopt_coding_state.h"
00040
00041
00042
00043
00044
00045
00046
00047
00048 int Mode_Decision_for_8x8IntraBlocks_JM_Low444 (Macroblock *currMB, int b8, double lambda, double *min_cost)
00049 {
00050 ImageParameters *p_Img = currMB->p_Img;
00051 InputParameters *p_Inp = currMB->p_Inp;
00052 Slice *currSlice = currMB->p_slice;
00053
00054 int ipmode, best_ipmode = 0, i, j, dummy;
00055 int cost;
00056 int nonzero = 0;
00057 int block_x = (b8 & 0x01) << 3;
00058 int block_y = (b8 >> 1) << 3;
00059 int pic_pix_x = currMB->pix_x + block_x;
00060 int pic_pix_y = currMB->pix_y + block_y;
00061 int pic_opix_x = currMB->pix_x + block_x;
00062 int pic_opix_y = currMB->opix_y + block_y;
00063 int pic_block_x = pic_pix_x >> 2;
00064 int pic_block_y = pic_pix_y >> 2;
00065 int mb_block_y = (currMB->block_y) + ((b8 >> 1) << 1);
00066 int mb_block_x = (currMB->block_x) + ((b8 & 0x01) << 1);
00067
00068 imgpel *img_org, *img_prd;
00069 int *residual;
00070 int left_available, up_available, all_available;
00071 int **mb_ores = currSlice->mb_ores[0];
00072 imgpel **mb_pred = currSlice->mb_pred[0];
00073 int *mb_size = p_Img->mb_size[IS_LUMA];
00074
00075 char upMode, leftMode;
00076 int mostProbableMode;
00077
00078 PixelPos left_block, top_block;
00079
00080 get4x4Neighbour(currMB, block_x - 1, block_y , mb_size, &left_block);
00081 get4x4Neighbour(currMB, block_x, block_y - 1, mb_size, &top_block );
00082
00083 if (p_Inp->UseConstrainedIntraPred)
00084 {
00085 top_block.available = top_block.available ? p_Img->intra_block [top_block.mb_addr] : 0;
00086 left_block.available = left_block.available ? p_Img->intra_block [left_block.mb_addr] : 0;
00087 }
00088
00089 if(b8 >> 1)
00090 upMode = top_block.available ? p_Img->ipredmode8x8[top_block.pos_y ][top_block.pos_x ] : -1;
00091 else
00092 upMode = top_block.available ? p_Img->ipredmode [top_block.pos_y ][top_block.pos_x ] : -1;
00093 if(b8 & 0x01)
00094 leftMode = left_block.available ? p_Img->ipredmode8x8[left_block.pos_y][left_block.pos_x] : -1;
00095 else
00096 leftMode = left_block.available ? p_Img->ipredmode[left_block.pos_y][left_block.pos_x] : -1;
00097
00098 mostProbableMode = (upMode < 0 || leftMode < 0) ? DC_PRED : upMode < leftMode ? upMode : leftMode;
00099
00100 *min_cost = INT_MAX;
00101
00102 currMB->ipmode_DPCM = NO_INTRA_PMODE;
00103
00104
00105 set_intrapred_8x8(currMB, PLANE_Y, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
00106
00107 if(currSlice->P444_joined)
00108 {
00109 select_plane(p_Img, PLANE_U);
00110 set_intrapred_8x8(currMB, PLANE_U, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
00111 select_plane(p_Img, PLANE_V);
00112 set_intrapred_8x8(currMB, PLANE_V, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
00113 select_plane(p_Img, PLANE_Y);
00114 }
00115
00116
00117 for (ipmode = 0; ipmode < NO_INTRA_PMODE; ipmode++)
00118 {
00119 if( (ipmode==DC_PRED) ||
00120 ((ipmode==VERT_PRED||ipmode==VERT_LEFT_PRED||ipmode==DIAG_DOWN_LEFT_PRED) && up_available ) ||
00121 ((ipmode==HOR_PRED||ipmode==HOR_UP_PRED) && left_available ) ||
00122 (all_available) )
00123 {
00124 get_intrapred_8x8(currMB, PLANE_Y, ipmode, left_available, up_available);
00125 cost = (ipmode == mostProbableMode) ? 0 : (int)floor(4 * lambda );
00126 currSlice->compute_cost8x8(p_Img, &p_Img->pImgOrg[0][pic_opix_y], currSlice->mpr_8x8[0][ipmode], pic_opix_x, &cost, (int) *min_cost);
00127
00128 if(currSlice->P444_joined)
00129 {
00130 get_intrapred_8x8(currMB, PLANE_U, ipmode, left_available, up_available);
00131 currSlice->compute_cost8x8(p_Img, &p_Img->pImgOrg[PLANE_U][pic_opix_y], currSlice->mpr_8x8[PLANE_U][ipmode], pic_opix_x, &cost, (int) *min_cost);
00132 get_intrapred_8x8(currMB, PLANE_V, ipmode, left_available, up_available);
00133 currSlice->compute_cost8x8(p_Img, &p_Img->pImgOrg[PLANE_V][pic_opix_y], currSlice->mpr_8x8[PLANE_V][ipmode], pic_opix_x, &cost, (int) *min_cost);
00134 }
00135
00136 if (cost < *min_cost)
00137 {
00138 best_ipmode = ipmode;
00139 *min_cost = cost;
00140 }
00141 }
00142 }
00143
00144
00145 p_Img->ipredmode8x8[pic_block_y][pic_block_x] = (char) best_ipmode;
00146 currMB->ipmode_DPCM = (char) best_ipmode;
00147
00148 if(currSlice->P444_joined)
00149 {
00150 ColorPlane k;
00151 p_Img->CbCr_predmode_8x8[b8] = best_ipmode;
00152 for (k = PLANE_U; k <= PLANE_V; k++)
00153 {
00154 currMB->cr_cbp[k] = 0;
00155 select_plane(p_Img, k);
00156 for (j=0; j<8; j++)
00157 {
00158 for (i=0; i<8; i++)
00159 {
00160 currSlice->mb_pred[k][block_y+j][block_x+i] = currSlice->mpr_8x8[k][best_ipmode][j][i];
00161 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_8x8[k][best_ipmode][j][i];
00162 }
00163 }
00164
00165 currMB->ipmode_DPCM = (short) best_ipmode;
00166 if (currMB->trans_8x8(currMB, k, b8, &dummy, 1))
00167 currMB->cr_cbp[k] = 1;
00168 }
00169 select_plane(p_Img, PLANE_Y);
00170 }
00171
00172 currMB->intra_pred_modes8x8[4*b8] = (char) ((mostProbableMode == best_ipmode)
00173 ? -1
00174 : (best_ipmode < mostProbableMode ? best_ipmode : best_ipmode-1));
00175
00176 for(j = mb_block_y; j < mb_block_y + 2; j++)
00177 memset(&p_Img->ipredmode8x8[j][mb_block_x], best_ipmode, 2 * sizeof(char));
00178
00179
00180 for (j = block_y; j < block_y + 8; j++)
00181 {
00182 memcpy(&mb_pred[j][block_x],currSlice->mpr_8x8[0][best_ipmode][j - block_y], 8 * sizeof(imgpel));
00183 img_org = &p_Img->pCurImg[currMB->opix_y + j][pic_opix_x];
00184 img_prd = &mb_pred[j][block_x];
00185 residual = &mb_ores[j][block_x];
00186 for (i=0; i<8; i++)
00187 {
00188 *residual++ = *img_org++ - *img_prd++;
00189 }
00190 }
00191
00192 currMB->ipmode_DPCM = (short) best_ipmode;
00193 nonzero = currMB->trans_8x8 (currMB, PLANE_Y, b8, &dummy, 1);
00194 return nonzero;
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204 int Mode_Decision_for_8x8IntraBlocks_JM_High444 (Macroblock *currMB, int b8, double lambda, double *min_cost)
00205 {
00206 ImageParameters *p_Img = currMB->p_Img;
00207 InputParameters *p_Inp = currMB->p_Inp;
00208 Slice *currSlice = currMB->p_slice;
00209 RDOPTStructure *p_RDO = currSlice->p_RDO;
00210
00211 int ipmode, best_ipmode = 0, i, j, dummy;
00212 int c_nz, nonzero = 0;
00213 double rdcost = 0.0;
00214 int block_x = (b8 & 0x01) << 3;
00215 int block_y = (b8 >> 1) << 3;
00216 int pic_pix_x = currMB->pix_x + block_x;
00217 int pic_pix_y = currMB->pix_y + block_y;
00218 int pic_opix_x = currMB->pix_x + block_x;
00219 int pic_opix_y = currMB->opix_y + block_y;
00220 int pic_block_x = pic_pix_x >> 2;
00221 int pic_block_y = pic_pix_y >> 2;
00222 int mb_block_y = (currMB->block_y) + ((b8 >> 1) << 1);
00223 int mb_block_x = (currMB->block_x) + ((b8 & 0x01) << 1);
00224
00225 double min_rdcost = 1e30;
00226
00227 int uv;
00228 int left_available, up_available, all_available;
00229
00230 char upMode, leftMode;
00231 int mostProbableMode;
00232
00233 PixelPos left_block, top_block;
00234
00235 int *mb_size = p_Img->mb_size[IS_LUMA];
00236
00237 get4x4Neighbour(currMB, block_x - 1, block_y , mb_size, &left_block);
00238 get4x4Neighbour(currMB, block_x, block_y - 1, mb_size, &top_block );
00239
00240 if (p_Inp->UseConstrainedIntraPred)
00241 {
00242 top_block.available = top_block.available ? p_Img->intra_block [top_block.mb_addr ] : 0;
00243 left_block.available = left_block.available ? p_Img->intra_block [left_block.mb_addr] : 0;
00244 }
00245
00246 if(b8 >> 1)
00247 upMode = top_block.available ? p_Img->ipredmode8x8[top_block.pos_y ][top_block.pos_x ] : -1;
00248 else
00249 upMode = top_block.available ? p_Img->ipredmode [top_block.pos_y ][top_block.pos_x ] : -1;
00250
00251 if(b8 & 0x01)
00252 leftMode = left_block.available ? p_Img->ipredmode8x8[left_block.pos_y][left_block.pos_x] : -1;
00253 else
00254 leftMode = left_block.available ? p_Img->ipredmode[left_block.pos_y][left_block.pos_x] : -1;
00255
00256 mostProbableMode = (upMode < 0 || leftMode < 0) ? DC_PRED : upMode < leftMode ? upMode : leftMode;
00257
00258 *min_cost = INT_MAX;
00259
00260 currMB->ipmode_DPCM = NO_INTRA_PMODE;
00261
00262
00263 set_intrapred_8x8(currMB, PLANE_Y, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
00264
00265 if(currSlice->P444_joined)
00266 {
00267 select_plane(p_Img, PLANE_U);
00268 set_intrapred_8x8(currMB, PLANE_U, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
00269 select_plane(p_Img, PLANE_V);
00270 set_intrapred_8x8(currMB, PLANE_V, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
00271 select_plane(p_Img, PLANE_Y);
00272 }
00273
00274
00275 for (ipmode = 0; ipmode < NO_INTRA_PMODE; ipmode++)
00276 {
00277 if( (ipmode==DC_PRED) ||
00278 ((ipmode==VERT_PRED||ipmode==VERT_LEFT_PRED||ipmode==DIAG_DOWN_LEFT_PRED) && up_available ) ||
00279 ((ipmode==HOR_PRED||ipmode==HOR_UP_PRED) && left_available ) ||
00280 (all_available) )
00281 {
00282 get_intrapred_8x8(currMB, PLANE_Y, ipmode, left_available, up_available);
00283
00284
00285 generate_pred_error_8x8(&p_Img->pImgOrg[0][pic_opix_y], currSlice->mpr_8x8[0][ipmode], &currSlice->mb_pred[0][block_y], &currSlice->mb_ores[0][block_y], pic_opix_x, block_x);
00286
00287 if(currSlice->P444_joined)
00288 {
00289 generate_pred_error_8x8(&p_Img->pImgOrg[1][pic_opix_y], currSlice->mpr_8x8[1][ipmode], &currSlice->mb_pred[1][block_y], &currSlice->mb_ores[1][block_y], pic_opix_x, block_x);
00290 generate_pred_error_8x8(&p_Img->pImgOrg[2][pic_opix_y], currSlice->mpr_8x8[2][ipmode], &currSlice->mb_pred[2][block_y], &currSlice->mb_ores[2][block_y], pic_opix_x, block_x);
00291 }
00292
00293 currMB->ipmode_DPCM = (short) ipmode;
00294
00295
00296
00297 if ((rdcost = currSlice->rdcost_for_8x8_intra_blocks (currMB, &c_nz, b8, ipmode, lambda, min_rdcost, mostProbableMode)) < min_rdcost)
00298 {
00299
00300 memcpy(p_RDO->coefAC8x8intra[b8][0][0][0],currSlice->cofAC[b8][0][0], 4 * 2 * 65 * sizeof(int));
00301
00302
00303 copy_image_data_8x8(p_RDO->rec8x8[PLANE_Y], &p_Img->enc_picture->imgY[pic_pix_y], 0, pic_pix_x);
00304
00305 if (p_Img->AdaptiveRounding)
00306 {
00307 for (j = block_y; j < block_y + 8; j++)
00308 memcpy(&p_Img->ARCofAdj8x8[0][DUMMY][j][block_x],&p_Img->ARCofAdj8x8[0][I8MB][j][block_x], 8 * sizeof(int));
00309
00310 if (currSlice->P444_joined)
00311 {
00312 for (j = block_y; j < block_y + 8; j++)
00313 {
00314 memcpy(&p_Img->ARCofAdj8x8[1][DUMMY][j][block_x],&p_Img->ARCofAdj8x8[1][I8MB][j][block_x], 8 * sizeof(int));
00315 memcpy(&p_Img->ARCofAdj8x8[2][DUMMY][j][block_x],&p_Img->ARCofAdj8x8[2][I8MB][j][block_x], 8 * sizeof(int));
00316 }
00317 }
00318 }
00319
00320 if (currSlice->P444_joined)
00321 {
00322
00323 for (uv=0; uv < 2; uv++)
00324 {
00325 memcpy(p_RDO->coefAC8x8intra[b8][uv + 1][0][0],currSlice->cofAC[4+b8+4*uv][0][0], 2 * 4 * 65 * sizeof(int));
00326
00327 currMB->cr_cbp[uv + 1] = currMB->c_nzCbCr[uv + 1];
00328
00329 copy_image_data_8x8(p_RDO->rec8x8[uv + 1], &p_Img->enc_picture->imgUV[uv][pic_pix_y], 0, pic_pix_x);
00330 }
00331 }
00332
00333
00334 nonzero = c_nz;
00335
00336
00337 *min_cost = rdcost;
00338 min_rdcost = rdcost;
00339 best_ipmode = ipmode;
00340 }
00341 }
00342 }
00343
00344
00345 p_Img->ipredmode8x8[pic_block_y][pic_block_x] = (char) best_ipmode;
00346 currMB->ipmode_DPCM = (short) best_ipmode;
00347
00348 if(currSlice->P444_joined)
00349 {
00350 ColorPlane k;
00351 p_Img->CbCr_predmode_8x8[b8] = best_ipmode;
00352 for (k = PLANE_U; k <= PLANE_V; k++)
00353 {
00354 currMB->cr_cbp[k] = 0;
00355 select_plane(p_Img, k);
00356 for (j=0; j<8; j++)
00357 {
00358 for (i=0; i<8; i++)
00359 {
00360 currSlice->mb_pred[k][block_y+j][block_x+i] = currSlice->mpr_8x8[k][best_ipmode][j][i];
00361 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_8x8[k][best_ipmode][j][i];
00362 }
00363 }
00364 currMB->ipmode_DPCM = (short) best_ipmode;
00365
00366 if (currMB->trans_8x8(currMB, k, b8, &dummy, 1))
00367 currMB->cr_cbp[k] = 1;
00368 }
00369 select_plane(p_Img, PLANE_Y);
00370 }
00371
00372 currMB->intra_pred_modes8x8[4*b8] = (char) ((mostProbableMode == best_ipmode)
00373 ? -1
00374 : (best_ipmode < mostProbableMode ? best_ipmode : best_ipmode-1));
00375
00376 memset(&p_Img->ipredmode8x8[mb_block_y ][mb_block_x], best_ipmode, 2 * sizeof(char));
00377 memset(&p_Img->ipredmode8x8[mb_block_y + 1][mb_block_x], best_ipmode, 2 * sizeof(char));
00378
00379
00380 memcpy(currSlice->cofAC[b8][0][0], p_RDO->coefAC8x8intra[b8][0][0][0], 4 * 2 * 65 * sizeof(int));
00381
00382 if (p_Img->AdaptiveRounding)
00383 {
00384 for (j=block_y; j< block_y + 8; j++)
00385 memcpy(&p_Img->ARCofAdj8x8[0][I8MB][j][block_x], &p_Img->ARCofAdj8x8[0][DUMMY][j][block_x], 8 * sizeof(int));
00386
00387 if (currSlice->P444_joined)
00388 {
00389 for (j=0; j<8; j++)
00390 {
00391 memcpy(&p_Img->ARCofAdj8x8[1][I8MB][block_y + j][block_x], &p_Img->ARCofAdj8x8[1][DUMMY][block_y + j][block_x], 8 * sizeof(int));
00392 memcpy(&p_Img->ARCofAdj8x8[2][I8MB][block_y + j][block_x], &p_Img->ARCofAdj8x8[2][DUMMY][block_y + j][block_x], 8 * sizeof(int));
00393 }
00394 }
00395 }
00396
00397
00398 copy_image_data_8x8(&p_Img->enc_picture->imgY[pic_pix_y], p_RDO->rec8x8[0], pic_pix_x, 0);
00399 copy_image_data_8x8(&currSlice->mb_pred[0][block_y], currSlice->mpr_8x8[0][best_ipmode], block_x, 0);
00400
00401 if (currSlice->P444_joined)
00402 {
00403
00404 memcpy(currSlice->cofAC[4 + b8 + 4*0][0][0], p_RDO->coefAC8x8intra[b8][1][0][0], 4 * 2 * 65 * sizeof(int));
00405 memcpy(currSlice->cofAC[4 + b8 + 4*1][0][0], p_RDO->coefAC8x8intra[b8][2][0][0], 4 * 2 * 65 * sizeof(int));
00406
00407
00408 copy_image_data_8x8(&p_Img->enc_picture->imgUV[0][pic_pix_y], p_RDO->rec8x8[1], pic_pix_x, 0);
00409 copy_image_data_8x8(&p_Img->enc_picture->imgUV[1][pic_pix_y], p_RDO->rec8x8[2], pic_pix_x, 0);
00410 copy_image_data_8x8(&currSlice->mb_pred[1][block_y], currSlice->mpr_8x8[1][best_ipmode], block_x, 0);
00411 copy_image_data_8x8(&currSlice->mb_pred[2][block_y], currSlice->mpr_8x8[2][best_ipmode], block_x, 0);
00412 }
00413
00414 return nonzero;
00415 }
00416