📄 transform8x8.c
字号:
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/4;
int pic_block_y = pic_pix_y/4;
double min_rdcost = 1e30;
imgpel **imgY_orig = imgY_org;
extern int ****cofAC8x8;
int fadjust8x8[2][16][16];
int left_available, up_available, all_available;
char upMode;
char leftMode;
int mostProbableMode;
PixelPos left_block;
PixelPos top_block;
// Residue Color Transform
int residue_R, residue_G, residue_B;
int rate, temp, b4;
int64 distortion;
Macroblock *currMB = &img->mb_data[img->current_mb_nr];
int c_ipmode = currMB->c_ipred_mode;
int rec8x8_c[2][4][4][4];
#ifdef USE_INTRA_MDDT
long quant_stat_best8x8[64];
#endif
getLuma4x4Neighbour(img->current_mb_nr, block_x/4, block_y/4, -1, 0, &left_block);
getLuma4x4Neighbour(img->current_mb_nr, block_x/4, block_y/4, 0, -1, &top_block);
if (input->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;
//===== INTRA PREDICTION FOR 8x8 BLOCK =====
intrapred_luma8x8 (pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
//===== 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) )
{
if (!input->rdopt)
{
for (k=j=0; j<8; j++)
for (i=0; i<8; i++, k++)
{
diff[k] = imgY_orig[pic_opix_y+j][pic_opix_x+i] - img->mprr_3[ipmode][j][i];
}
cost = (ipmode == mostProbableMode) ? 0 : (int)floor(4 * lambda );
cost += SATD8X8 (diff, input->hadamard);
if (cost < *min_cost)
{
best_ipmode = ipmode;
*min_cost = cost;
}
}
else
{
// Residue Color Transform
if(!img->residue_transform_flag)
{
// get prediction and prediction error
for (j=0; j<8; j++)
{
memcpy(&img->mpr[block_y+j][block_x],img->mprr_3[ipmode][j], 8 * sizeof(imgpel));
for (i=0; i<8; i++)
{
img->m7[j][i] = imgY_orig[pic_opix_y+j][pic_opix_x+i] - img->mprr_3[ipmode][j][i];
}
}
//===== store the coding state =====
//store_coding_state_cs_cm();
// get and check rate-distortion cost
if ((rdcost = RDCost_for_8x8IntraBlocks (&c_nz, b8, ipmode, lambda, min_rdcost, mostProbableMode)) < min_rdcost)
{
#ifdef USE_INTRA_MDDT
if(input->UseIntraMDDT)
memcpy(quant_stat_best8x8, quant_stat_rd8x8, 64*sizeof(long));
#endif
//--- set coefficients ---
for(k=0; k<4; k++) // do 4x now
{
for (j=0; j<2; j++)
memcpy(cofAC8x8[b8][k][j],img->cofAC[b8][k][j], 65 * sizeof(int));
}
//--- set reconstruction ---
for (y=0; y<8; y++)
{
memcpy(rec8x8[y],&enc_picture->imgY[pic_pix_y+y][pic_pix_x], 8 * sizeof(imgpel));
}
if (img->AdaptiveRounding)
{
for (j=block_y; j<block_y + 8; j++)
memcpy(&fadjust8x8[1][j][block_x],&img->fadjust8x8[1][j][block_x], 8 * sizeof(int));
}
//--- flag if dct-coefficients must be coded ---
nonzero = c_nz;
//--- set best mode update minimum cost ---
min_rdcost = rdcost;
best_ipmode = ipmode;
}
reset_coding_state_cs_cm();
}
else
{
for (j=0; j<8; j++)
for (i=0; i<8; i++)
{
residue_B = imgUV_org[0][pic_opix_y+j][pic_opix_x+i] - img->mprr_c[0][c_ipmode][block_y+j][block_x+i];
residue_G = imgY_org[pic_opix_y+j][pic_opix_x+i] - img->mprr_3[ipmode][j][i];
residue_R = imgUV_org[1][pic_opix_y+j][pic_opix_x+i] - img->mprr_c[1][c_ipmode][block_y+j][block_x+i];
/* Forward Residue Transform */
resTrans_R[j][i] = residue_R-residue_B;
temp = residue_B+(resTrans_R[j][i]>>1);
resTrans_B[j][i] = residue_G-temp;
resTrans_G[j][i] = temp+(resTrans_B[j][i]>>1);
}
for (j=0; j<8; j++)
for (i=0; i<8; i++)
{
img->m7[j][i] = resTrans_G[j][i];
}
//store_coding_state_cs_cm();
rate = (int) RDCost_for_8x8IntraBlocks (&c_nz, b8, ipmode, lambda, min_rdcost, mostProbableMode);
reset_coding_state_cs_cm();
for (j=0; j<8; j++)
for (i=0; i<8; i++)
{
rec_resG[j][i] = img->m7[j][i];
}
for(b4=0;b4<4;b4++)
{
block4x4_x = 4*(b4 & 0x01);
block4x4_y = 4*(b4 >> 1);
for (j=0; j<4; j++)
for (i=0; i<4; i++)
{
img->m7[j][i] = resTrans_B[j+block4x4_y][i+block4x4_x];
}
rate += RDCost_for_4x4Blocks_Chroma (b8+4, b4, 0);
for (j=0; j<4; j++)
for (i=0; i<4; i++)
{
rec_resB[j+block4x4_y][i+block4x4_x] = img->m7[j][i];
img->m7[j][i] = resTrans_R[j+block4x4_y][i+block4x4_x];
}
rate += RDCost_for_4x4Blocks_Chroma (b8+8, b4, 1);
for (j=0; j<4; j++)
for (i=0; i<4; i++)
{
rec_resR[j+block4x4_y][i+block4x4_x] = img->m7[j][i];
}
}
reset_coding_state_cs_cm();
for (j=0; j<8; j++)
{
for (i=0; i<8; i++)
{
/* Inverse Residue Transform */
temp = rec_resG[j][i]-(rec_resB[j][i]>>1);
residue_G = rec_resB[j][i]+temp;
residue_B = temp - (rec_resR[j][i]>>1);
residue_R = residue_B+rec_resR[j][i];
enc_picture->imgUV[0][pic_pix_y+j][pic_pix_x+i] = min(img->max_imgpel_value_uv,max(0,residue_B+(int)img->mprr_c[0][c_ipmode][block_y+j][block_x+i]));
enc_picture->imgY[pic_pix_y+j][pic_pix_x+i] = min(img->max_imgpel_value,max(0,residue_G+(int)img->mprr_3[ipmode][j][i]));
enc_picture->imgUV[1][pic_pix_y+j][pic_pix_x+i] = min(img->max_imgpel_value_uv,max(0,residue_R+(int)img->mprr_c[1][c_ipmode][block_y+j][block_x+i]));
}
}
//===== get distortion (SSD) of 8x8 block =====
distortion = 0;
for (y=0; y<8; y++)
for (x=pic_pix_x; x<pic_pix_x+8; x++)
{
#ifdef INTERNAL_BIT_DEPTH_INCREASE
distortion += SQR_DEPTH(imgY_org [pic_pix_y+y][x], enc_picture->imgY [pic_pix_y+y][x], input->InputBitDepth, img->BitDepthIncrease);
distortion += SQR_DEPTH(imgUV_org[0][pic_pix_y+y][x], enc_picture->imgUV[0][pic_pix_y+y][x], input->InputBitDepth, img->BitDepthIncreaseChroma);
distortion += SQR_DEPTH(imgUV_org[1][pic_pix_y+y][x], enc_picture->imgUV[1][pic_pix_y+y][x], input->InputBitDepth, img->BitDepthIncreaseChroma);
#else
distortion += img->quad[imgY_org [pic_pix_y+y][x] - enc_picture->imgY [pic_pix_y+y][x]];
distortion += img->quad[imgUV_org[0][pic_pix_y+y][x] - enc_picture->imgUV[0][pic_pix_y+y][x]];
distortion += img->quad[imgUV_org[1][pic_pix_y+y][x] - enc_picture->imgUV[1][pic_pix_y+y][x]];
#endif
}
rdcost = (double)distortion + lambda*(double)rate;
if (rdcost < min_rdcost)
{
//--- set coefficients ---
for (j=0; j<2; j++)
for (i=0; i<65;i++)
for(k=0; k<4; k++) //do 4x now
cofAC8x8[b8][k][j][i]=img->cofAC[b8][k][j][i]; //k vs 0
for(b4=0; b4<4; b4++)
{
block4x4_x = 4*(b4 & 0x01);
block4x4_y = 4*(b4 >> 1);
for (j=0; j<2; j++)
for (i=0; i<18;i++) cofAC8x8_chroma[0][b4][j][i]=img->cofAC[b8+4][b4][j][i];
for (j=0; j<2; j++)
for (i=0; i<18;i++) cofAC8x8_chroma[1][b4][j][i]=img->cofAC[b8+8][b4][j][i];
for (i=0; i<2; i++)
{ //uv
dc_level[i][2*(b8 & 0x01)+(b4 & 0x01)][2*(b8 >> 1)+(b4 >> 1)] = dc_level_temp[i][2*(b8 & 0x01)+(b4 & 0x01)][2*(b8 >> 1)+(b4 >> 1)];
cbp_chroma_block[i][2*(b8 & 0x01)+(b4 & 0x01)][2*(b8 >> 1)+(b4 >> 1)] = cbp_chroma_block_temp[i][2*(b8 & 0x01)+(b4 & 0x01)][2*(b8 >> 1)+(b4 >> 1)];
//--- set reconstruction ---
for (y=0; y<4; y++)
for (x=0; x<4; x++) rec8x8_c[i][b4][y][x] = enc_picture->imgUV[i][pic_pix_y+y+block4x4_y][pic_pix_x+x+block4x4_x];
}
}
//--- set reconstruction ---
for (y=0; y<8; y++)
for (x=0; x<8; x++)
rec8x8[y][x] = enc_picture->imgY[pic_pix_y+y][pic_pix_x+x];
//--- flag if dct-coefficients must be coded ---
nonzero = c_nz;
//--- set best mode update minimum cost ---
min_rdcost = rdcost;
best_ipmode = ipmode;
}
}
}
}
}
#ifdef USE_INTRA_MDDT
if(input->UseIntraMDDT)
for (j=0; j<64; j++)
{
img->quant_stat8x8[j]+=quant_stat_best8x8[j];
}
#endif
//===== set intra mode prediction =====
img->ipredmode8x8[pic_block_y][pic_block_x] = best_ipmode;
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));
if (!input->rdopt)
{
// Residue Color Transform
if(!img->residue_transform_flag)
{
// get prediction and prediction error
for (j=0; j<8; j++)
{
memcpy(&img->mpr[block_y+j][block_x],img->mprr_3[best_ipmode][j], 8 * sizeof(imgpel));
for (i=0; i<8; i++)
{
img->m7[j][i] = imgY_orig[pic_opix_y+j][pic_opix_x+i] - img->mprr_3[best_ipmode][j][i];
}
}
#ifdef USE_INTRA_MDDT
if(input->UseIntraMDDT)
{
nonzero = (best_ipmode == 2) ?
dct_luma8x8 (b8, &dummy, 1, best_ipmode) :
klt_luma8x8_sep_fast (b8, &dummy, best_ipmode);
}
else
nonzero = dct_luma8x8 (b8, &dummy, 1, best_ipmode);
#else
nonzero = dct_luma8x8 (b8, &dummy, 1);
#endif
}
else
{
for (j=0; j<8; j++)
{
for (i=0; i<8; i++)
{
img->mpr[block_y+j][block_x+i] = img->mprr_3[best_ipmode][j][i];
residue_B = imgUV_org[0][pic_opix_y+j][pic_opix_x+i] - img->mprr_c[0][c_ipmode][block_y+j][block_x+i];
residue_G = imgY_org[pic_opix_y+j][pic_opix_x+i] - img->mprr_3[best_ipmode][j][i];
residue_R = imgUV_org[1][pic_opix_y+j][pic_opix_x+i] - img->mprr_c[1][c_ipmode][block_y+j][block_x+i];
/* Forward Residue Transform */
resTrans_R[j][i] = residue_R-residue_B;
temp = residue_B+(resTrans_R[j][i]>>1);
resTrans_B[j][i] = residue_G-temp;
resTrans_G[j][i] = temp+(resTrans_B[j][i]>>1);
}
}
for (j=0; j<8; j++)
{
for (i=0; i<8; i++)
img->m7[j][i] = resTrans_G[j][i];
}
#ifdef USE_INTRA_MDDT
nonzero = dct_luma8x8 (b8, &dummy, 1, c_ipmode);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -