📄 transform8x8.c
字号:
double rdcost = 0.0;
int block_x = (b8 & 0x01) << 3;
int block_y = (b8 >> 1) << 3;
int pic_pix_x = img->pix_x + block_x;
int pic_pix_y = img->pix_y + block_y;
int pic_opix_x = img->opix_x + block_x;
int pic_opix_y = img->opix_y + block_y;
int pic_block_x = pic_pix_x >> 2;
int pic_block_y = pic_pix_y >> 2;
double min_rdcost = 1e30;
imgpel *img_org, *img_prd;
int *residual;
int uv, c_nzCbCr[3];
int left_available, up_available, all_available;
int **mb_ores = img->mb_ores[0];
imgpel **mb_pred = img->mb_pred[0];
int *mb_size = img->mb_size[IS_LUMA];
char upMode;
char leftMode;
int mostProbableMode;
PixelPos left_block, top_block;
get4x4Neighbour(currMB, block_x - 1, block_y , mb_size, &left_block);
get4x4Neighbour(currMB, block_x, block_y - 1, mb_size, &top_block );
if (params->UseConstrainedIntraPred)
{
top_block.available = top_block.available ? img->intra_block [top_block.mb_addr] : 0;
left_block.available = left_block.available ? img->intra_block [left_block.mb_addr] : 0;
}
if(b8 >> 1)
upMode = top_block.available ? img->ipredmode8x8[top_block.pos_y ][top_block.pos_x ] : -1;
else
upMode = top_block.available ? img->ipredmode [top_block.pos_y ][top_block.pos_x ] : -1;
if(b8 & 0x01)
leftMode = left_block.available ? img->ipredmode8x8[left_block.pos_y][left_block.pos_x] : -1;
else
leftMode = left_block.available ? img->ipredmode[left_block.pos_y][left_block.pos_x] : -1;
mostProbableMode = (upMode < 0 || leftMode < 0) ? DC_PRED : upMode < leftMode ? upMode : leftMode;
*min_cost = INT_MAX;
ipmode_DPCM = NO_INTRA_PMODE; //For residual DPCM
//===== INTRA PREDICTION FOR 8x8 BLOCK =====
intrapred_8x8 (currMB, PLANE_Y, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
if(img->P444_joined)
{
select_plane(PLANE_U);
intrapred_8x8(currMB, PLANE_U, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
select_plane(PLANE_V);
intrapred_8x8(currMB, PLANE_V, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
select_plane(PLANE_Y);
}
//===== LOOP OVER ALL 8x8 INTRA PREDICTION MODES =====
for (ipmode = 0; ipmode < NO_INTRA_PMODE; ipmode++)
{
if( (ipmode==DC_PRED) ||
((ipmode==VERT_PRED||ipmode==VERT_LEFT_PRED||ipmode==DIAG_DOWN_LEFT_PRED) && up_available ) ||
((ipmode==HOR_PRED||ipmode==HOR_UP_PRED) && left_available ) ||
(all_available) )
{
// get prediction and prediction error
for (j=block_y; j < block_y + 8; j++)
{
memcpy(&mb_pred[j][block_x],img->mpr_8x8[0][ipmode][j - block_y], 8 * sizeof(imgpel));
img_org = &pCurImg[img->opix_y+j][pic_opix_x];
img_prd = &mb_pred[j][block_x];
residual = &mb_ores[j][block_x];
for (i=0; i<8; i++)
{
*residual++ = *img_org++ - *img_prd++;
}
}
if(img->P444_joined)
{
for (k = PLANE_U; k <= PLANE_V; k++)
{
for (j=0; j<8; j++)
{
memcpy(&img->mb_pred[k][block_y+j][block_x],img->mpr_8x8[k][ipmode][j], 8 * sizeof(imgpel));
for (i=0; i<8; i++)
{
img->mb_ores[k][block_y+j][block_x+i] = (int) (pImgOrg[k][pic_opix_y+j][pic_opix_x+i] - img->mpr_8x8[k][ipmode][j][i]);
//img->mb_ores[k][block_y+j][block_x+i] = img->mb_ores[k][block_y+j][block_x+i]; // line is temporary until we fix code to use img->mb_ores
}
}
}
}
ipmode_DPCM=ipmode;
//===== store the coding state =====
// store_coding_state_cs_cm(currSlice, currMB);
// get and check rate-distortion cost
if ((rdcost = RDCost_for_8x8IntraBlocks (currSlice, currMB, &c_nz, b8, ipmode, lambda, min_rdcost, mostProbableMode, c_nzCbCr)) < min_rdcost)
{
//--- set coefficients ---
memcpy(coefAC8x8[b8][0][0][0],img->cofAC[b8][0][0], 4 * 2 * 65 * sizeof(int));
//--- set reconstruction ---
copy_8x8block(rec8x8[PLANE_Y], &enc_picture->imgY[pic_pix_y], 0, pic_pix_x);
if (img->AdaptiveRounding)
{
for (j = block_y; j < block_y + 8; j++)
memcpy(&img->ARCofAdj8x8[0][DUMMY][j][block_x],&img->ARCofAdj8x8[0][I8MB][j][block_x], 8 * sizeof(int));
if (img->P444_joined)
{
for (j = block_y; j < block_y + 8; j++)
{
memcpy(&img->ARCofAdj8x8[1][DUMMY][j][block_x],&img->ARCofAdj8x8[1][I8MB][j][block_x], 8 * sizeof(int));
memcpy(&img->ARCofAdj8x8[2][DUMMY][j][block_x],&img->ARCofAdj8x8[2][I8MB][j][block_x], 8 * sizeof(int));
}
}
}
if (img->P444_joined)
{
//--- set coefficients ---
for (uv=0; uv < 2; uv++)
{
memcpy(coefAC8x8[b8][uv + 1][0][0],img->cofAC[4+b8+4*uv][0][0], 2 * 4 * 65 * sizeof(int));
cr_cbp[uv + 1] = c_nzCbCr[uv + 1];
//--- set reconstruction ---
copy_8x8block(rec8x8[uv + 1], &enc_picture->imgUV[uv][pic_pix_y], 0, pic_pix_x);
}
}
//--- flag if dct-coefficients must be coded ---
nonzero = c_nz;
//--- set best mode update minimum cost ---
*min_cost = rdcost;
min_rdcost = rdcost;
best_ipmode = ipmode;
}
reset_coding_state_cs_cm(currSlice, currMB);
}
}
//===== set intra mode prediction =====
img->ipredmode8x8[pic_block_y][pic_block_x] = (char) best_ipmode;
ipmode_DPCM = best_ipmode; //For residual DPCM
if(img->P444_joined)
{
ColorPlane k;
CbCr_predmode_8x8[b8] = best_ipmode;
for (k = PLANE_U; k <= PLANE_V; k++)
{
cr_cbp[k] = 0;
select_plane(k);
for (j=0; j<8; j++)
{
for (i=0; i<8; i++)
{
img->mb_pred[k][block_y+j][block_x+i] = img->mpr_8x8[k][best_ipmode][j][i];
img->mb_ores[k][block_y+j][block_x+i] = pImgOrg[k][img->pix_y+block_y+j][img->pix_x+block_x+i] - img->mpr_8x8[k][best_ipmode][j][i];
}
}
ipmode_DPCM = best_ipmode;
if (pDCT_8x8(currMB, k, b8, &dummy, 1))
cr_cbp[k] = 1;
}
select_plane(PLANE_Y);
}
currMB->intra_pred_modes8x8[4*b8] = (mostProbableMode == best_ipmode)
? -1
: (best_ipmode < mostProbableMode ? best_ipmode : best_ipmode-1);
for(j = img->mb_y*4+(b8 >> 1)*2; j < img->mb_y*4+(b8 >> 1)*2 + 2; j++) //loop 4x4s in the subblock for 8x8 prediction setting
memset(&img->ipredmode8x8[j][img->mb_x*4+(b8 & 0x01)*2], best_ipmode, 2 * sizeof(char));
//===== restore coefficients =====
memcpy(img->cofAC[b8][0][0],coefAC8x8[b8][0][0][0], 4 * 2 * 65 * sizeof(int));
if (img->AdaptiveRounding)
{
for (j=block_y; j< block_y + 8; j++)
memcpy(&img->ARCofAdj8x8[0][I8MB][j][block_x], &img->ARCofAdj8x8[0][DUMMY][j][block_x], 8 * sizeof(int));
if (img->P444_joined)
{
for (j=0; j<8; j++)
{
memcpy(&img->ARCofAdj8x8[1][I8MB][block_y + j][block_x], &img->ARCofAdj8x8[1][DUMMY][block_y + j][block_x], 8 * sizeof(int));
memcpy(&img->ARCofAdj8x8[2][I8MB][block_y + j][block_x], &img->ARCofAdj8x8[2][DUMMY][block_y + j][block_x], 8 * sizeof(int));
}
}
}
//===== restore reconstruction and prediction (needed if single coeffs are removed) =====
copy_8x8block(&enc_picture->imgY[pic_pix_y], rec8x8[0], pic_pix_x, 0);
copy_8x8block(&mb_pred[block_y], img->mpr_8x8[0][best_ipmode], block_x, 0);
if (img->P444_joined)
{
//===== restore coefficients =====
memcpy(img->cofAC[4+b8+4*0][0][0], coefAC8x8[b8][1][0][0], 4 * 2 * 65 * sizeof(int));
memcpy(img->cofAC[4+b8+4*1][0][0], coefAC8x8[b8][2][0][0], 4 * 2 * 65 * sizeof(int));
//===== restore reconstruction and prediction (needed if single coeffs are removed) =====
copy_8x8block(&enc_picture->imgUV[0][pic_pix_y], rec8x8[1], pic_pix_x, 0);
copy_8x8block(&enc_picture->imgUV[1][pic_pix_y], rec8x8[2], pic_pix_x, 0);
copy_8x8block(&img->mb_pred[1][block_y], img->mpr_8x8[1][best_ipmode], block_x, 0);
copy_8x8block(&img->mb_pred[2][block_y], img->mpr_8x8[2][best_ipmode], block_x, 0);
}
return nonzero;
}
// Notation for comments regarding prediction and predictors.
// The pels of the 4x4 block are labelled a..p. The predictor pels above
// are labelled A..H, from the left I..P, and from above left X, as follows:
//
// Z A B C D E F G H I J K L M N O P
// Q a1 b1 c1 d1 e1 f1 g1 h1
// R a2 b2 c2 d2 e2 f2 g2 h2
// S a3 b3 c3 d3 e3 f3 g3 h3
// T a4 b4 c4 d4 e4 f4 g4 h4
// U a5 b5 c5 d5 e5 f5 g5 h5
// V a6 b6 c6 d6 e6 f6 g6 h6
// W a7 b7 c7 d7 e7 f7 g7 h7
// X a8 b8 c8 d8 e8 f8 g8 h8
static inline void get_diag_down_left(imgpel **cur_pred, imgpel *PredPel)
{
cur_pred[0][0] = (imgpel) ((P_A + P_C + (P_B << 1) + 2) >> 2);
cur_pred[0][1] =
cur_pred[1][0] = (imgpel) ((P_B + P_D + (P_C << 1) + 2) >> 2);
cur_pred[0][2] =
cur_pred[1][1] =
cur_pred[2][0] = (imgpel) ((P_C + P_E + (P_D << 1) + 2) >> 2);
cur_pred[0][3] =
cur_pred[1][2] =
cur_pred[2][1] =
cur_pred[3][0] = (imgpel) ((P_D + P_F + (P_E << 1) + 2) >> 2);
cur_pred[0][4] =
cur_pred[1][3] =
cur_pred[2][2] =
cur_pred[3][1] =
cur_pred[4][0] = (imgpel) ((P_E + P_G + (P_F << 1) + 2) >> 2);
cur_pred[0][5] =
cur_pred[1][4] =
cur_pred[2][3] =
cur_pred[3][2] =
cur_pred[4][1] =
cur_pred[5][0] = (imgpel) ((P_F + P_H + (P_G << 1) + 2) >> 2);
cur_pred[0][6] =
cur_pred[1][5] =
cur_pred[2][4] =
cur_pred[3][3] =
cur_pred[4][2] =
cur_pred[5][1] =
cur_pred[6][0] = (imgpel) ((P_G + P_I + (P_H << 1) + 2) >> 2);
cur_pred[0][7] =
cur_pred[1][6] =
cur_pred[2][5] =
cur_pred[3][4] =
cur_pred[4][3] =
cur_pred[5][2] =
cur_pred[6][1] =
cur_pred[7][0] = (imgpel) ((P_H + P_J + (P_I << 1) + 2) >> 2);
cur_pred[1][7] =
cur_pred[2][6] =
cur_pred[3][5] =
cur_pred[4][4] =
cur_pred[5][3] =
cur_pred[6][2] =
cur_pred[7][1] = (imgpel) ((P_I + P_K + (P_J << 1) + 2) >> 2);
cur_pred[2][7] =
cur_pred[3][6] =
cur_pred[4][5] =
cur_pred[5][4] =
cur_pred[6][3] =
cur_pred[7][2] = (imgpel) ((P_J + P_L + (P_K << 1) + 2) >> 2);
cur_pred[3][7] =
cur_pred[4][6] =
cur_pred[5][5] =
cur_pred[6][4] =
cur_pred[7][3] = (imgpel) ((P_K + P_M + (P_L << 1) + 2) >> 2);
cur_pred[4][7] =
cur_pred[5][6] =
cur_pred[6][5] =
cur_pred[7][4] = (imgpel) ((P_L + P_N + (P_M << 1) + 2) >> 2);
cur_pred[5][7] =
cur_pred[6][6] =
cur_pred[7][5] = (imgpel) ((P_M + P_O + (P_N << 1) + 2) >> 2);
cur_pred[6][7] =
cur_pred[7][6] = (imgpel) ((P_N + P_P + (P_O << 1) + 2) >> 2);
cur_pred[7][7] = (imgpel) ((P_O + P_P + (P_P << 1) + 2) >> 2);
}
static inline void get_vert_left(imgpel **cur_pred, imgpel *PredPel)
{
cur_pred[0][0] = (imgpel) ((P_A + P_B + 1) >> 1);
cur_pred[0][1] =
cur_pred[2][0] = (imgpel) ((P_B + P_C + 1) >> 1);
cur_pred[0][2] =
cur_pred[2][1] =
cur_pred[4][0] = (imgpel) ((P_C + P_D + 1) >> 1);
cur_pred[0][3] =
cur_pred[2][2] =
cur_pred[4][1] =
cur_pred[6][0] = (imgpel) ((P_D + P_E + 1) >> 1);
cur_pred[0][4] =
cur_pred[2][3] =
cur_pred[4][2] =
cur_pred[6][1] = (imgpel) ((P_E + P_F + 1) >> 1);
cur_pred[0][5] =
cur_pred[2][4] =
cur_pred[4][3] =
cur_pred[6][2] = (imgpel) ((P_F + P_G + 1) >> 1);
cur_pred[0][6] =
cur_pred[2][5] =
cur_pred[4][4] =
cur_pred[6][3] = (imgpel) ((P_G + P_H + 1) >> 1);
cur_pred[0][7] =
cur_pred[2][6] =
cur_pred[4][5] =
cur_pred[6][4] = (imgpel) ((P_H + P_I + 1) >> 1);
cur_pred[2][7] =
cur_pred[4][6] =
cur_pred[6][5] = (imgpel) ((P_I + P_J + 1) >> 1);
cur_pred[4][7] =
cur_pred[6][6] = (imgpel) ((P_J + P_K + 1) >> 1);
cur_pred[6][7] = (imgpel) ((P_K + P_L + 1) >> 1);
cur_pred[1][0] = (imgpel) ((P_A + P_C + 2*P_B + 2) >> 2);
cur_pred[1][1] =
cur_pred[3][0] = (imgpel) ((P_B + P_D + 2*P_C + 2) >> 2);
cur_pred[1][2] =
cur_pred[3][1] =
cur_pred[5][0] = (imgpel) ((P_C + P_E + 2*P_D + 2) >> 2);
cur_pred[1][3] =
cur_pred[3][2] =
cur_pred[5][1] =
cur_pred[7][0] = (imgpel) ((P_D + P_F + 2*P_E + 2) >> 2);
cur_pred[1][4] =
cur_pred[3][3] =
cur_pred[5][2] =
cur_pred[7][1] = (imgpel) ((P_E + P_G + 2*P_F + 2) >> 2);
cur_pred[1][5] =
cur_pred[3][4] =
cur_pred[5][3] =
cur_pred[7][2] = (imgpel) ((P_F + P_H + 2*P_G + 2) >> 2);
cur_pred[1][6] =
cur_pred[3][5] =
cur_pred[5][4] =
cur_pred[7][3] = (imgpel) ((P_G + P_I + 2*P_H + 2) >> 2);
cur_pred[1][7] =
cur_pred[3][6] =
cur_pred[5][5] =
cur_pred[7][4] = (imgpel) ((P_H + P_J + 2*P_I + 2) >> 2);
cur_pred[3][7] =
cur_pred[5][6] =
cur_pred[7][5] = (imgpel) ((P_I + P_K + 2*P_J + 2) >> 2);
cur_pred[5][7] =
cur_pred[7][6] = (imgpel) ((P_J + P_L + 2*P_K + 2) >> 2);
cur_pred[7][7] = (imgpel) ((P_K + P_M + 2*P_L + 2) >> 2);
}
static inline void get_diag_down_right(imgpel **cur_pred, imgpel *PredPel)
{
cur_pred[7][0] = (imgpel) ((P_X + P_V + (P_W << 1) + 2) >> 2);
cur_pred[6][0] =
cur_pred[7][1] = (imgpel) ((P_W + P_U + (P_V << 1) + 2) >> 2);
cur_pred[5][0] =
cur_pred[6][1] =
cur_pred[7][2] = (imgpel) ((P_V + P_T + (P_U << 1) + 2) >> 2);
cur_pred[4][0] =
cur_pred[5][1] =
cur_pred[6][2] =
cur_pred[7][3] = (imgpel) ((P_U + P_S + (P_T << 1) + 2) >> 2);
cur_pred[3][0] =
cur_pred[4][1] =
cur_pred[5][2] =
cur_pred[6][3] =
cur_pred[7][4] = (imgpel) ((P_T + P_R + (P_S << 1) + 2) >> 2);
cur_pred[2][0] =
cur_pred[3][1] =
cur_pred[4][2] =
cur_pred[5][3] =
cur_pred[6][4] =
cur_pred[7][5] = (imgpel) ((P_S + P_Q + (P_R << 1) + 2) >> 2);
cur_pred[1][0] =
cur_pred[2][1] =
cur_pred[3][2] =
cur_pred[4][3] =
cur_pred[5][4] =
cur_pred[6][5] =
cur_pred[7][6] = (imgpel) ((P_R + P_Z + (P_Q << 1) + 2) >> 2);
cur_pred[0][0] =
cur_pred[1][1] =
cur_pred[2][2] =
cur_pred[3][3] =
cur_pred[4][4] =
cur_pred[5][5] =
cur_pred[6][6] =
cur_pred[7][7] = (imgpel) ((P_Q + P_A + (P_Z << 1) + 2) >> 2);
cur_pred[0][1] =
cur_pred[1][2] =
cur_pred[2][3] =
cur_pred[3][4] =
cur_pred[4][5] =
cur_pred[5][6] =
cur_pred[6][7] = (imgpel) ((P_Z + P_B + (P_A << 1) + 2) >> 2);
cur_pred[0][2] =
cur_pred[1][3] =
cur_pred[2][4] =
cur_pred[3][5] =
cur_pred[4][6] =
cur_pred[5][7] = (imgpel) ((P_A + P_C + (P_B << 1) + 2) >> 2);
cur_pred[0][3] =
cur_pred[1][4] =
cur_pred[2][5] =
cur_pred[3][6] =
cur_pred[4][7] = (imgpel) ((P_B + P_D + (P_C << 1) + 2) >> 2);
cur_pred[0][4] =
cur_pred[1][5] =
cur_pred[2][6] =
cur_pred[3][7] = (imgpel) ((P_C + P_E + (P_D << 1) + 2) >> 2);
cur_pred[0][5] =
cur_pred[1][6] =
cur_pred[2][7] = (imgpel) ((P_D + P_F + (P_E << 1) + 2) >> 2);
cur_pred[0][6] =
cur_pred[1][7] = (imgpel) ((P_E + P_G + (P_F << 1) + 2) >> 2);
cur_pred[0][7] = (imgpel) ((P_F + P_H + (P_G << 1) + 2) >> 2);
}
static inline void get_vert_right(imgpel **cur_pred, imgpel *PredPel)
{
cur_pred[0][0] =
cur_pred[2][1] =
cur_pred[4][2] =
cur_pred[6][3] = (imgpel) ((P_Z + P_A + 1) >> 1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -