block.c
来自「the newest JM software by h.264 JVT offi」· C语言 代码 · 共 2,206 行 · 第 1/5 页
C
2,206 行
/*!
************************************************************************
* \brief
* Transform,quantization,inverse transform for chroma.
* The main reason why this is done in a separate routine is the
* additional 2x2 transform of DC-coeffs. This routine is called
* once for each of the chroma components.
*
* \par Input:
* uv : Make difference between the U and V chroma component \n
* cr_cbp: chroma coded block pattern
*
* \par Output:
* cr_cbp: Updated chroma coded block pattern.
************************************************************************
*/
int dct_chroma(Macroblock *currMB, int uv, int cr_cbp, int is_cavlc)
{
int i, j, n2, n1, coeff_ctr;
static int *m1;
int coeff_cost = 0;
int cr_cbp_tmp = 0;
int DCzero = FALSE;
int nonzero[4][4] = {{FALSE}};
int nonezero = FALSE;
const byte *c_cost = COEFF_COST4x4[params->disthres];
int b4;
int* DCLevel = img->cofDC[uv+1][0];
int* DCRun = img->cofDC[uv+1][1];
int* ACLevel;
int* ACRun;
int intra = IS_INTRA (currMB);
int uv_scale = uv * (img->num_blk8x8_uv >> 1);
//FRExt
static const int64 cbpblk_pattern[4]={0, 0xf0000, 0xff0000, 0xffff0000};
int yuv = img->yuv_format;
int b8;
const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
int cur_qp = currMB->qpc[uv] + img->bitdepth_chroma_qp_scale;
int qp_rem = qp_rem_matrix[cur_qp];
int max_imgpel_value_uv = img->max_imgpel_value_comp[uv + 1];
int **mb_rres = img->mb_rres[uv + 1];
int **mb_ores = img->mb_ores[uv + 1];
imgpel **mb_pred = img->mb_pred[uv + 1];
levelscale = LevelScale4x4Comp [uv + 1][intra][qp_rem];
leveloffset = LevelOffset4x4Comp [uv + 1][intra][cur_qp];
invlevelscale = InvLevelScale4x4Comp[uv + 1][intra][qp_rem];
if (currMB->mb_type == P8x8 && currMB->luma_transform_size_8x8_flag)
{
//P8x8, treansform 8x8 chroma adjustments must be stored in a different array to avoid conflict with tranform 4x4
fadjust4x4 = img->AdaptiveRounding ? img->ARCofAdj4x4[uv + 1][4] : NULL;
}
else
{
fadjust4x4 = img->AdaptiveRounding ? img->ARCofAdj4x4[uv + 1][currMB->ar_mode] : NULL;
}
img->is_v_block = uv;
//============= dct transform ===============
for (n2=0; n2 < img->mb_cr_size_y; n2 += BLOCK_SIZE)
{
for (n1=0; n1 < img->mb_cr_size_x; n1 += BLOCK_SIZE)
{
forward4x4(mb_ores, mb_rres, n2, n1);
}
}
if (yuv == YUV420)
{
m1 = M4[0];
//================== CHROMA DC YUV420 ===================
// forward 2x2 hadamard
hadamard2x2(mb_rres, m1);
// Quantization process of chroma 2X2 hadamard transformed DC coeffs.
DCzero = quant_dc_cr(&m1, cur_qp, DCLevel, DCRun, fadjust2x2, levelscale[0][0], invlevelscale[0][0], leveloffset[0][0], SCAN_YUV420, is_cavlc);
if (DCzero)
{
currMB->cbp_blk |= 0xf0000 << (uv << 2) ; // if one of the 2x2-DC levels is != 0 set the
cr_cbp=imax(1,cr_cbp); // coded-bit all 4 4x4 blocks (bit 16-19 or 20-23)
}
// Inverse transform of 2x2 DC levels
ihadamard2x2(m1, m1);
mb_rres[0][0] = m1[0] >> 5;
mb_rres[0][4] = m1[1] >> 5;
mb_rres[4][0] = m1[2] >> 5;
mb_rres[4][4] = m1[3] >> 5;
}
else if (yuv == YUV422)
{
//for YUV422 only
int cur_qp_dc = currMB->qpc[uv] + 3 + img->bitdepth_chroma_qp_scale;
int qp_rem_dc = qp_rem_matrix[cur_qp_dc];
invlevelscaleDC = InvLevelScale4x4Comp[uv + 1][intra][qp_rem_dc];
levelscaleDC = LevelScale4x4Comp [uv + 1][intra][qp_rem_dc];
leveloffsetDC = LevelOffset4x4Comp [uv + 1][intra][cur_qp_dc];
//================== CHROMA DC YUV422 ===================
//pick out DC coeff
for (j=0; j < img->mb_cr_size_y; j+=BLOCK_SIZE)
{
for (i=0; i < img->mb_cr_size_x; i+=BLOCK_SIZE)
M4[i>>2][j>>2]= mb_rres[j][i];
}
// forward hadamard transform. Note that coeffs have been transposed (4x2 instead of 2x4) which makes transform a bit faster
hadamard4x2(M4, M4);
// Quantization process of chroma transformed DC coeffs.
DCzero = quant_dc_cr(M4, cur_qp_dc, DCLevel, DCRun, fadjust4x2, levelscaleDC[0][0], invlevelscaleDC[0][0], leveloffsetDC[0][0], SCAN_YUV422, is_cavlc);
if (DCzero)
{
currMB->cbp_blk |= 0xff0000 << (uv << 3) ; // if one of the DC levels is != 0 set the
cr_cbp=imax(1,cr_cbp); // coded-bit all 4 4x4 blocks (bit 16-31 or 32-47) //YUV444
}
//inverse DC transform. Note that now M4 is transposed back
ihadamard4x2(M4, M4);
// This code assumes sizeof(int) > 16. Therefore, no need to have conditional
for (j = 0; j < 4; j++)
{
mb_rres[j << 2 ][0] = rshift_rnd_sf(M4[j][0], 6);
mb_rres[j << 2 ][4] = rshift_rnd_sf(M4[j][1], 6);
}
}
// Quant of chroma AC-coeffs.
for (b8=0; b8 < (img->num_blk8x8_uv >> 1); b8++)
{
for (b4=0; b4 < 4; b4++)
{
int64 uv_cbpblk = ((int64)1) << cbp_blk_chroma[b8 + uv_scale][b4];
n1 = hor_offset[yuv][b8][b4];
n2 = ver_offset[yuv][b8][b4];
ACLevel = img->cofAC[4 + b8 + uv_scale][b4][0];
ACRun = img->cofAC[4 + b8 + uv_scale][b4][1];
img->subblock_y = subblk_offset_y[img->yuv_format - 1][b8][b4]>>2;
img->subblock_x = subblk_offset_x[img->yuv_format - 1][b8][b4]>>2;
// Quantization process
nonzero[n2>>2][n1>>2] = quant_ac4x4cr(&mb_rres[n2], n2, n1, cur_qp, ACLevel, ACRun, &fadjust4x4[n2],
levelscale, invlevelscale, leveloffset, &coeff_cost, pos_scan, c_cost, CHROMA_AC, is_cavlc);
if (nonzero[n2>>2][n1>>2])
{
currMB->cbp_blk |= uv_cbpblk;
cr_cbp_tmp = 2;
nonezero = TRUE;
}
}
}
// Perform thresholding
// * reset chroma coeffs
if(nonezero && coeff_cost < _CHROMA_COEFF_COST_)
{
int64 uv_cbpblk = ((int64)cbpblk_pattern[yuv] << (uv << (1+yuv)));
cr_cbp_tmp = 0;
for (b8 = 0; b8 < (img->num_blk8x8_uv >> 1); b8++)
{
for (b4 = 0; b4 < 4; b4++)
{
n1 = hor_offset[yuv][b8][b4];
n2 = ver_offset[yuv][b8][b4];
if (nonzero[n2>>2][n1>>2] == TRUE)
{
nonzero[n2>>2][n1>>2] = FALSE;
ACLevel = img->cofAC[4 + b8 + uv_scale][b4][0];
ACRun = img->cofAC[4 + b8 + uv_scale][b4][1];
if (DCzero == 0)
currMB->cbp_blk &= ~(uv_cbpblk); // if no chroma DC's: then reset coded-bits of this chroma subblock
ACLevel[0] = 0;
for (coeff_ctr=1; coeff_ctr < 16; coeff_ctr++)// ac coeff
{
mb_rres[n2 + pos_scan[coeff_ctr][1]][n1 + pos_scan[coeff_ctr][0]] = 0;
ACLevel[coeff_ctr] = 0;
}
}
}
}
}
// IDCT.
// Horizontal.
if(cr_cbp_tmp == 2)
cr_cbp = 2;
nonezero = FALSE;
for (n2=0; n2 < img->mb_cr_size_y; n2 += BLOCK_SIZE)
{
for (n1=0; n1 < img->mb_cr_size_x; n1 += BLOCK_SIZE)
{
if (mb_rres[n2][n1] != 0 || nonzero[n2>>2][n1>>2] == TRUE)
{
inverse4x4(mb_rres, mb_rres, n2, n1);
nonezero = TRUE;
}
}
}
// Decoded block moved to memory
if (nonezero == TRUE)
{
SampleReconstruct (enc_picture->imgUV[uv], mb_pred, mb_rres, 0, 0, img->pix_c_y, img->pix_c_x, img->mb_cr_size_x, img->mb_cr_size_y, max_imgpel_value_uv, DQ_BITS);
}
else
{
for (j=0; j < img->mb_cr_size_y; j++)
{
memcpy(&enc_picture->imgUV[uv][img->pix_c_y + j][img->pix_c_x], mb_pred[j], img->mb_cr_size_x * sizeof(imgpel));
}
}
return cr_cbp;
}
/*!
************************************************************************
* \brief
* Transform,quantization,inverse transform for chroma.
* The main reason why this is done in a separate routine is the
* additional 2x2 transform of DC-coeffs. This routine is called
* once for each of the chroma components.
*
* \par Input:
* uv : Make difference between the U and V chroma component \n
* cr_cbp: chroma coded block pattern
*
* \par Output:
* cr_cbp: Updated chroma coded block pattern.
************************************************************************
*/
int dct_chroma_ls(Macroblock *currMB, int uv, int cr_cbp, int is_cavlc)
{
int i,j,n2,n1,coeff_ctr,level ,scan_pos,run;
static int m1[BLOCK_SIZE];
int coeff_cost;
int cr_cbp_tmp;
int nonzero = FALSE;
static imgpel *orig_img, *pred_img;
int b4;
int* DCLevel = img->cofDC[uv+1][0];
int* DCRun = img->cofDC[uv+1][1];
int* ACLevel;
int* ACRun;
int uv_scale = uv * (img->num_blk8x8_uv >> 1);
//FRExt
int yuv = img->yuv_format;
int b8;
static int *m7;
static int m3[4][4];
const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
int **mb_rres = img->mb_rres[uv + 1];
int **mb_ores = img->mb_ores[uv + 1];
imgpel **mb_pred = img->mb_pred[uv + 1];
if (currMB->mb_type == P8x8 && currMB->luma_transform_size_8x8_flag)
{
//P8x8, treansform 8x8 chroma adjustments must be stored in a different array to avoid conflict with tranform 4x4
fadjust4x4 = img->AdaptiveRounding ? img->ARCofAdj4x4[uv + 1][4] : NULL;
}
else
{
fadjust4x4 = img->AdaptiveRounding ? img->ARCofAdj4x4[uv + 1][currMB->ar_mode] : NULL;
}
if (yuv == YUV420)
{
//================== CHROMA DC YUV420 ===================
// 2X2 transform of DC coeffs.
run=-1;
scan_pos=0;
m1[0] = mb_rres[0][0] = mb_ores[0][0];
m1[1] = mb_rres[0][4] = mb_ores[0][4];
m1[2] = mb_rres[4][0] = mb_ores[4][0];
m1[3] = mb_rres[4][4] = mb_ores[4][4];
for (coeff_ctr=0; coeff_ctr < 4; coeff_ctr++)
{
run++;
level =iabs(m1[coeff_ctr]);
if (level != 0)
{
if (is_cavlc)
level = imin(level, CAVLC_LEVEL_LIMIT);
currMB->cbp_blk |= 0xf0000 << (uv << 2) ; // if one of the 2x2-DC levels is != 0 set the
cr_cbp=imax(1, cr_cbp); // coded-bit all 4 4x4 blocks (bit 16-19 or 20-23)
nonzero = TRUE;
level = isignab(level, m1[coeff_ctr]);
DCLevel[scan_pos ] = level;
DCRun [scan_pos++] = run;
run=-1;
}
}
DCLevel[scan_pos] = 0;
}
else if(yuv == YUV422)
{
//================== CHROMA DC YUV422 ===================
//transform DC coeff
//horizontal
//pick out DC coeff
for (j=0; j < img->mb_cr_size_y; j+=BLOCK_SIZE)
{
for (i=0; i < img->mb_cr_size_x; i+=BLOCK_SIZE)
{
m3[i>>2][j>>2] = mb_ores[j][i];
mb_rres[j][i] = mb_ores[j][i];
}
}
run=-1;
scan_pos=0;
//quant of chroma DC-coeffs
for (coeff_ctr=0;coeff_ctr<8;coeff_ctr++)
{
i=SCAN_YUV422[coeff_ctr][0];
j=SCAN_YUV422[coeff_ctr][1];
run++;
level = iabs(m3[i][j]);
M4[i][j]=m3[i][j];
if (level != 0)
{
//YUV422
currMB->cbp_blk |= 0xff0000 << (uv << 3) ; // if one of the DC levels is != 0 set the
cr_cbp=imax(1,cr_cbp); // coded-bit all 4 4x4 blocks (bit 16-31 or 32-47) //YUV444
nonzero = TRUE;
DCLevel[scan_pos ] = isignab(level,M4[i][j]);
DCRun [scan_pos++] = run;
run=-1;
}
}
DCLevel[scan_pos]=0;
//inverse DC transform
//horizontal
}
// Quant of chroma AC-coeffs.
coeff_cost=0;
cr_cbp_tmp=0;
for (b8=0; b8 < (img->num_blk8x8_uv >> 1); b8++)
{
for (b4=0; b4 < 4; b4++)
{
int64 uv_cbpblk = ((int64)1) << cbp_blk_chroma[b8 + uv_scale][b4];
n1 = hor_offset[yuv][b8][b4];
n2 = ver_offset[yuv][b8][b4];
ACLevel = img->cofAC[4 + b8 + uv_scale][b4][0];
ACRun = img->cofAC[4 + b8 + uv_scale][b4][1];
run=-1;
scan_pos=0;
for (coeff_ctr=1; coeff_ctr < 16; coeff_ctr++)// start change rd_quant
{
i=pos_scan[coeff_ctr][0];
j=pos_scan[coeff_ctr][1];
++run;
level = iabs(mb_ores[n2+j][n1+i]);
mb_rres[n2+j][n1+i] = mb_ores[n2+j][n1+i];
if (img->AdaptiveRounding)
{
fadjust4x4[n2+j][n1+i] = 0;
}
if (level != 0)
{
currMB->cbp_blk |= uv_cbpblk;
coeff_cost += MAX_VALUE; // set high cost, shall not be discarded
cr_cbp_tmp=2;
ACLevel[scan_pos ] = isignab(level, mb_ores[n2+j][n1+i]);
ACRun [scan_pos++] = run;
run=-1;
level = isignab(level, mb_ores[n2+j][n1+i]);
}
}
ACLevel[scan_pos] = 0;
}
}
for (j=0; j < img->mb_cr_size_y; j++)
{
orig_img = &enc_picture->imgUV[uv][img->pix_c_y + j][img->pix_c_x];
m7 = mb_rres[j];
pred_img = mb_pred[j];
for (i=0; i < img->mb_cr_size_x; i++)
{
orig_img[i] = (imgpel) m7[i] + pred_img[i];
}
}
return cr_cbp;
}
/*!
************************************************************************
* \brief
* The routine performs transform,quantization,inverse transform, adds the diff.
* to the prediction and writes the result to the decoded luma frame. Includes the
* RD constrained quantization also.
*
* \par Input:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?