📄 block.c
字号:
DCzero = quant_dc_cr(&m1, cur_qp, DCLevel, DCRun, fadjust2x2, levelscale[0][0], invlevelscale[0][0], leveloffset, 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, 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 intra = IS_INTRA (currMB);
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)[MB_BLOCK_SIZE] = img->mb_rres[uv + 1];
int (*mb_ores)[MB_BLOCK_SIZE] = img->mb_ores[uv + 1];
imgpel (*mb_pred)[MB_BLOCK_SIZE] = img->mb_pred[uv + 1];
fadjust4x4 = img->AdaptiveRounding ? img->fadjust4x4Cr[intra][uv] : 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:
* block_x,block_y: Block position inside a macro block (0,4,8,12).
*
* \par Output:
* nonzero: 0 if no levels are nonzero. 1 if there are nonzero levels. \n
* coeff_cost: Counter for nonzero coefficients, used to discard expensive levels.
*
*
************************************************************************
*/
int dct_4x4_sp(Macroblock *currMB, ColorPlane pl, int block_x,int block_y,int *coeff_cost, int intra, int is_cavlc)
{
int i,j,coeff_ctr;
int qp_const,ilev, level,scan_pos = 0,run = -1;
int nonzero = FALSE;
imgpel **img_enc = enc_picture->p_curr_img;
imgpel (*mb_pred)[MB_BLOCK_SIZE] = img->mb_pred[pl];
int (*mb_rres)[MB_BLOCK_SIZE] = img->mb_rres[pl];
int (*mb_ores)[MB_BLOCK_SIZE] = img->mb_ores[pl];
int c_err,qp_const2;
int qp = currMB->qp_scaled[pl];
int qp_sp = (currMB->qpsp);
const byte *c_cost = COEFF_COST4x4[params->disthres];
const byte (*pos_scan)[2] = currMB->is_field_mode ? FIELD_SCAN : SNGL_SCAN;
int pos_x = block_x >> BLOCK_SHIFT;
int pos_y = block_y >> BLOCK_SHIFT;
int b8 = 2*(pos_y >> 1) + (pos_x >> 1);
int b4 = 2*(pos_y & 0x01) + (pos_x & 0x01);
int* ACLevel = img->cofAC[b8][b4][0];
int* ACRun = img->cofAC[b8][b4][1];
// For encoding optimization
int c_err1, c_err2, level1, level2;
double D_dis1, D_dis2;
int len, info;
double lambda_mode = 0.85 * pow (2, (qp - SHIFT_QP)/3.0) * 4;
int qp_per = qp_per_matrix[qp];
int qp_rem = qp_rem_matrix[qp];
int q_bits = Q_BITS + qp_per;
int qp_per_sp = qp_per_matrix[qp_sp];
int qp_rem_sp = qp_rem_matrix[qp_sp];
int q_bits_sp = Q_BITS + qp_per_sp;
levelscale = LevelScale4x4Comp[pl][intra][qp_rem];
invlevelscale = InvLevelScale4x4Comp[pl][intra][qp_rem];
leveloffset = ptLevelOffset4x4[intra][qp];
levelscale_sp = LevelScale4x4Comp[pl][intra][qp_rem_sp];
invlevelscale_sp = InvLevelScale4x4Comp[pl][intra][qp_rem_sp];
leveloffset_sp = ptLevelOffset4x4[intra][qp_sp];
qp_const = (1<<q_bits)/6; // inter
qp_const2 = (1<<q_bits_sp)/2; //sp_pred
// Horizontal transform
for (j=block_y; j< block_x + BLOCK_SIZE; j++)
{
for (i=block_x; i< block_x + BLOCK_SIZE; i++)
{
mb_rres[j][i] = mb_ores[j][i];
mb_rres[j][i]+=mb_pred[j][i];
M1[j][i] = mb_pred[j][i];
}
}
// 4x4 transform
forward4x4(mb_rres, mb_rres, block_y, block_x);
forward4x4(M1, M1, block_y, block_x);
for (coeff_ctr = 0;coeff_ctr < 16;coeff_ctr++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -