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 #include "intra8x8.h"
00032
00033
00034
00035
00036
00037
00038
00039 int Mode_Decision_for_4x4IntraBlocks_JM_Low (Macroblock *currMB, int b8, int b4, double lambda, double* min_cost)
00040 {
00041 ImageParameters *p_Img = currMB->p_Img;
00042 InputParameters *p_Inp = currMB->p_Inp;
00043 Slice *currSlice = currMB->p_slice;
00044
00045 int ipmode, best_ipmode = 0, cost, dummy;
00046 int nonzero = 0;
00047
00048 int block_x = ((b8 & 0x01) << 3) + ((b4 & 0x01) << 2);
00049 int block_y = ((b8 >> 1) << 3) + ((b4 >> 1) << 2);
00050 int pic_pix_x = currMB->pix_x + block_x;
00051 int pic_pix_y = currMB->pix_y + block_y;
00052 int pic_opix_x = currMB->pix_x + block_x;
00053 int pic_opix_y = currMB->opix_y + block_y;
00054 int pic_block_x = pic_pix_x >> 2;
00055 int pic_block_y = pic_pix_y >> 2;
00056
00057 int left_available, up_available, all_available;
00058
00059 char upMode, leftMode;
00060 int mostProbableMode;
00061
00062 PixelPos left_block, top_block;
00063
00064 int fixedcost = (int) floor(4 * lambda );
00065 int *mb_size = p_Img->mb_size[IS_LUMA];
00066
00067
00068 #ifdef BEST_NZ_COEFF
00069 int best_nz_coeff = 0;
00070 int best_coded_block_flag = 0;
00071 int bit_pos = 1 + ((((b8>>1)<<1)+(b4>>1))<<2) + (((b8&1)<<1)+(b4&1));
00072 int64 cbp_bits;
00073
00074 if (b8==0 && b4==0)
00075 cbp_bits = 0;
00076 #endif
00077
00078 get4x4Neighbour(currMB, block_x - 1, block_y , mb_size, &left_block);
00079 get4x4Neighbour(currMB, block_x, block_y - 1, mb_size, &top_block );
00080
00081
00082 if (p_Inp->UseConstrainedIntraPred)
00083 {
00084 left_block.available = left_block.available ? p_Img->intra_block[left_block.mb_addr] : 0;
00085 top_block.available = top_block.available ? p_Img->intra_block[top_block.mb_addr] : 0;
00086 }
00087
00088 upMode = top_block.available ? p_Img->ipredmode[top_block.pos_y ][top_block.pos_x ] : -1;
00089 leftMode = left_block.available ? p_Img->ipredmode[left_block.pos_y][left_block.pos_x] : -1;
00090
00091 mostProbableMode = (upMode < 0 || leftMode < 0) ? DC_PRED : upMode < leftMode ? upMode : leftMode;
00092
00093 *min_cost = INT_MAX;
00094
00095 currMB->ipmode_DPCM = NO_INTRA_PMODE;
00096
00097
00098
00099 set_intrapred_4x4(currMB, PLANE_Y, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
00100
00101
00102 for (ipmode = 0; ipmode < NO_INTRA_PMODE; ipmode++)
00103 {
00104 int available_mode = (all_available) || (ipmode==DC_PRED) ||
00105 (up_available && (ipmode==VERT_PRED||ipmode==VERT_LEFT_PRED||ipmode==DIAG_DOWN_LEFT_PRED)) ||
00106 (left_available && (ipmode==HOR_PRED||ipmode==HOR_UP_PRED));
00107
00108 if (valid_intra_mode(currSlice, ipmode) == 0)
00109 continue;
00110
00111 if( available_mode)
00112 {
00113
00114
00115
00116 get_intrapred_4x4(currMB, PLANE_Y, ipmode, block_x, block_y, left_available, up_available);
00117 cost = (ipmode == mostProbableMode) ? 0 : fixedcost;
00118 currSlice->compute_cost4x4(p_Img, &p_Img->pCurImg[pic_opix_y], currSlice->mpr_4x4[0][ipmode], pic_opix_x, &cost, (int) *min_cost);
00119
00120 if (cost < *min_cost)
00121 {
00122 best_ipmode = ipmode;
00123 *min_cost = cost;
00124 }
00125 }
00126 }
00127
00128 #ifdef BEST_NZ_COEFF
00129 p_Img->nz_coeff [p_Img->current_mb_nr][block_x4][block_y4] = best_nz_coeff;
00130 cbp_bits &= (~(int64)(1<<bit_pos));
00131 cbp_bits |= (int64)(best_coded_block_flag<<bit_pos);
00132 #endif
00133
00134 p_Img->ipredmode[pic_block_y][pic_block_x] = (char) best_ipmode;
00135 currMB->intra_pred_modes[4*b8+b4] =
00136 (char) (mostProbableMode == best_ipmode ? -1 : (best_ipmode < mostProbableMode ? best_ipmode : best_ipmode-1));
00137
00138
00139 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);
00140
00141 currMB->ipmode_DPCM = (short) best_ipmode;
00142
00143 select_dct(currMB);
00144 nonzero = currMB->cr_cbp[0] = currMB->trans_4x4 (currMB, PLANE_Y, block_x, block_y, &dummy, 1);
00145
00146
00147 return nonzero;
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00157 int Mode_Decision_for_8x8IntraBlocks_JM_Low (Macroblock *currMB, int b8, double lambda, double *min_cost)
00158 {
00159 ImageParameters *p_Img = currMB->p_Img;
00160 InputParameters *p_Inp = currMB->p_Inp;
00161 Slice *currSlice = currMB->p_slice;
00162
00163 int ipmode, best_ipmode = 0, i, j, dummy;
00164 int cost;
00165 int nonzero = 0;
00166 int block_x = (b8 & 0x01) << 3;
00167 int block_y = (b8 >> 1) << 3;
00168 int pic_pix_x = currMB->pix_x + block_x;
00169 int pic_pix_y = currMB->pix_y + block_y;
00170 int pic_opix_x = currMB->pix_x + block_x;
00171 int pic_opix_y = currMB->opix_y + block_y;
00172 int pic_block_x = pic_pix_x >> 2;
00173 int pic_block_y = pic_pix_y >> 2;
00174 int mb_block_y = (currMB->block_y) + ((b8 >> 1) << 1);
00175 int mb_block_x = (currMB->block_x) + ((b8 & 0x01) << 1);
00176
00177 imgpel *img_org, *img_prd;
00178 int *residual;
00179 int left_available, up_available, all_available;
00180 int **mb_ores = currSlice->mb_ores[0];
00181 imgpel **mb_pred = currSlice->mb_pred[0];
00182 int *mb_size = p_Img->mb_size[IS_LUMA];
00183
00184 char upMode;
00185 char leftMode;
00186 int mostProbableMode;
00187
00188 PixelPos left_block;
00189 PixelPos top_block;
00190
00191 get4x4Neighbour(currMB, block_x - 1, block_y , mb_size, &left_block);
00192 get4x4Neighbour(currMB, block_x, block_y - 1, mb_size, &top_block );
00193
00194 if (p_Inp->UseConstrainedIntraPred)
00195 {
00196 top_block.available = top_block.available ? p_Img->intra_block [top_block.mb_addr] : 0;
00197 left_block.available = left_block.available ? p_Img->intra_block [left_block.mb_addr] : 0;
00198 }
00199
00200 if(b8 >> 1)
00201 upMode = top_block.available ? p_Img->ipredmode8x8[top_block.pos_y ][top_block.pos_x ] : -1;
00202 else
00203 upMode = top_block.available ? p_Img->ipredmode [top_block.pos_y ][top_block.pos_x ] : -1;
00204 if(b8 & 0x01)
00205 leftMode = left_block.available ? p_Img->ipredmode8x8[left_block.pos_y][left_block.pos_x] : -1;
00206 else
00207 leftMode = left_block.available ? p_Img->ipredmode[left_block.pos_y][left_block.pos_x] : -1;
00208
00209 mostProbableMode = (upMode < 0 || leftMode < 0) ? DC_PRED : upMode < leftMode ? upMode : leftMode;
00210
00211 *min_cost = INT_MAX;
00212
00213 currMB->ipmode_DPCM = NO_INTRA_PMODE;
00214
00215
00216 set_intrapred_8x8(currMB, PLANE_Y, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
00217
00218
00219 for (ipmode = 0; ipmode < NO_INTRA_PMODE; ipmode++)
00220 {
00221 if( (ipmode==DC_PRED) ||
00222 ((ipmode==VERT_PRED||ipmode==VERT_LEFT_PRED||ipmode==DIAG_DOWN_LEFT_PRED) && up_available ) ||
00223 ((ipmode==HOR_PRED||ipmode==HOR_UP_PRED) && left_available ) ||
00224 (all_available) )
00225 {
00226 get_intrapred_8x8(currMB, PLANE_Y, ipmode, left_available, up_available);
00227 cost = (ipmode == mostProbableMode) ? 0 : (int)floor(4 * lambda );
00228 currSlice->compute_cost8x8(p_Img, &p_Img->pImgOrg[0][pic_opix_y], currSlice->mpr_8x8[0][ipmode], pic_opix_x, &cost, (int) *min_cost);
00229
00230 if (cost < *min_cost)
00231 {
00232 best_ipmode = ipmode;
00233 *min_cost = cost;
00234 }
00235 }
00236 }
00237
00238
00239 p_Img->ipredmode8x8[pic_block_y][pic_block_x] = (char) best_ipmode;
00240 currMB->ipmode_DPCM = (short) best_ipmode;
00241
00242 currMB->intra_pred_modes8x8[4*b8] = (char)((mostProbableMode == best_ipmode)
00243 ? -1
00244 : (best_ipmode < mostProbableMode ? best_ipmode : best_ipmode-1));
00245
00246 for(j = mb_block_y; j < mb_block_y + 2; j++)
00247 memset(&p_Img->ipredmode8x8[j][mb_block_x], best_ipmode, 2 * sizeof(char));
00248
00249
00250 for (j = block_y; j < block_y + 8; j++)
00251 {
00252 memcpy(&mb_pred[j][block_x],currSlice->mpr_8x8[0][best_ipmode][j - block_y], 8 * sizeof(imgpel));
00253 img_org = &p_Img->pCurImg[currMB->opix_y + j][pic_opix_x];
00254 img_prd = &mb_pred[j][block_x];
00255 residual = &mb_ores[j][block_x];
00256 for (i=0; i<8; i++)
00257 {
00258 *residual++ = *img_org++ - *img_prd++;
00259 }
00260 }
00261
00262 currMB->ipmode_DPCM = (short) best_ipmode;
00263
00264 nonzero = currMB->trans_8x8 (currMB, PLANE_Y, b8, &dummy, 1);
00265 return nonzero;
00266 }
00267