📄 block.c
字号:
b8 = 2*(jj/2) + (ii/2);
b4 = 2*(jj%2) + (ii%2);
ACLevel = img->cofAC [b8][b4][0];
ACRun = img->cofAC [b8][b4][1];
for (coeff_ctr=1;coeff_ctr<16;coeff_ctr++) // set in AC coeff
{
if (img->field_picture || ( mb_adaptive && img->field_mode ))
{ // Alternate scan for field coding
i=FIELD_SCAN[coeff_ctr][0];
j=FIELD_SCAN[coeff_ctr][1];
}
else
{
i=SNGL_SCAN[coeff_ctr][0];
j=SNGL_SCAN[coeff_ctr][1];
}
run++;
if(lossless_qpprime)
level= abs( M0[i][ii][j][jj]);
else
level= ( abs( M0[i][ii][j][jj]) * LevelScale4x4Luma_Intra[qp_rem][i][j] + qp_const) >> q_bits;
if (level != 0)
{
ac_coef = 15;
ACLevel[scan_pos] = sign(level,M0[i][ii][j][jj]);
ACRun [scan_pos] = run;
++scan_pos;
run=-1;
}
if(!lossless_qpprime)
{
level=sign(level, M0[i][ii][j][jj]);
if(qp_per<4)
M0[i][ii][j][jj]=(level*InvLevelScale4x4Luma_Intra[qp_rem][i][j]+(1<<(3-qp_per)))>>(4-qp_per);
else
M0[i][ii][j][jj]=(level*InvLevelScale4x4Luma_Intra[qp_rem][i][j])<<(qp_per-4);
}
}
ACLevel[scan_pos] = 0;
// IDCT horizontal
for (j=0;j<4 && !lossless_qpprime;j++)
{
for (i=0;i<4;i++)
{
M5[i]=M0[i][ii][j][jj];
}
M6[0]= M5[0]+M5[2];
M6[1]= M5[0]-M5[2];
M6[2]=(M5[1]>>1) -M5[3];
M6[3]= M5[1]+(M5[3]>>1);
for (i=0;i<2;i++)
{
i1=3-i;
M0[i][ii][j][jj] =M6[i]+M6[i1];
M0[i1][ii][j][jj]=M6[i]-M6[i1];
}
}
// vert
for (i=0;i<4 && !lossless_qpprime;i++)
{
for (j=0;j<4;j++)
M5[j]=M0[i][ii][j][jj];
M6[0]= M5[0]+M5[2];
M6[1]= M5[0]-M5[2];
M6[2]=(M5[1]>>1) -M5[3];
M6[3]= M5[1]+(M5[3]>>1);
for (j=0;j<2;j++)
{
j1=3-j;
M0[i][ii][ j][jj]=M6[j]+M6[j1];
M0[i][ii][j1][jj]=M6[j]-M6[j1];
}
}
}
}
// Residue Color Transform
if(!img->residue_transform_flag)
{
for (j=0;j<16;j++)
{
for (i=0;i<16;i++)
{
M1[i][j]=M0[i%4][i/4][j%4][j/4];
}
}
}
else
{
for (j=0;j<16;j++)
{
for (i=0;i<16;i++)
{
img->m7[i][j]=((M0[i%4][i/4][j%4][j/4]+DQ_ROUND)>>DQ_BITS);
}
}
}
if(!img->residue_transform_flag)
{
for (j=0;j<16;j++)
{
for (i=0;i<16;i++)
{
if(lossless_qpprime)
enc_picture->imgY[img->pix_y+j][img->pix_x+i]=(imgpel)(M1[i][j]+img->mprr_2[new_intra_mode][j][i]);
else
enc_picture->imgY[img->pix_y+j][img->pix_x+i]=(imgpel)min(img->max_imgpel_value, max(0,(M1[i][j]+((long)img->mprr_2[new_intra_mode][j][i]<<DQ_BITS)+DQ_ROUND)>>DQ_BITS));
}
}
}
return ac_coef;
}
/*!
************************************************************************
* \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_luma(int block_x,int block_y,int *coeff_cost, int intra)
{
int sign(int a,int b);
int i,j,i1,j1,ilev,m5[4],m6[4],coeff_ctr;
int qp_const,level,scan_pos,run;
int nonzero;
int qp_per,qp_rem,q_bits;
int pos_x = block_x/BLOCK_SIZE;
int pos_y = block_y/BLOCK_SIZE;
int b8 = 2*(pos_y/2) + (pos_x/2);
int b4 = 2*(pos_y%2) + (pos_x%2);
int* ACLevel = img->cofAC[b8][b4][0];
int* ACRun = img->cofAC[b8][b4][1];
Macroblock *currMB = &img->mb_data[img->current_mb_nr];
Boolean lossless_qpprime = ((currMB->qp + img->bitdepth_luma_qp_scale)==0 && img->lossless_qpprime_flag==1);
qp_per = (currMB->qp + img->bitdepth_luma_qp_scale - MIN_QP)/6;
qp_rem = (currMB->qp + img->bitdepth_luma_qp_scale - MIN_QP)%6;
q_bits = Q_BITS+qp_per;
if (img->type == I_SLICE)
qp_const=(1<<q_bits)/3; // intra
else
qp_const=(1<<q_bits)/6; // inter
// Horizontal transform
for (j=0; j < BLOCK_SIZE && !lossless_qpprime; j++)
{
for (i=0; i < 2; i++)
{
i1=3-i;
m5[i]=img->m7[i][j]+img->m7[i1][j];
m5[i1]=img->m7[i][j]-img->m7[i1][j];
}
img->m7[0][j]=(m5[0]+m5[1]);
img->m7[2][j]=(m5[0]-m5[1]);
img->m7[1][j]=m5[3]*2+m5[2];
img->m7[3][j]=m5[3]-m5[2]*2;
}
// Vertical transform
for (i=0; i < BLOCK_SIZE && !lossless_qpprime; i++)
{
for (j=0; j < 2; j++)
{
j1=3-j;
m5[j]=img->m7[i][j]+img->m7[i][j1];
m5[j1]=img->m7[i][j]-img->m7[i][j1];
}
img->m7[i][0]=(m5[0]+m5[1]);
img->m7[i][2]=(m5[0]-m5[1]);
img->m7[i][1]=m5[3]*2+m5[2];
img->m7[i][3]=m5[3]-m5[2]*2;
}
// Quant
nonzero=FALSE;
run=-1;
scan_pos=0;
for (coeff_ctr=0;coeff_ctr < 16;coeff_ctr++)
{
if (img->field_picture || ( img->MbaffFrameFlag && currMB->mb_field ))
{ // Alternate scan for field coding
i=FIELD_SCAN[coeff_ctr][0];
j=FIELD_SCAN[coeff_ctr][1];
}
else
{
i=SNGL_SCAN[coeff_ctr][0];
j=SNGL_SCAN[coeff_ctr][1];
}
run++;
ilev=0;
if(lossless_qpprime)
level = abs (img->m7[i][j]);
else if(intra == 1)
level = (abs (img->m7[i][j]) * LevelScale4x4Luma_Intra[qp_rem][i][j] + qp_const) >> q_bits;
else
level = (abs (img->m7[i][j]) * LevelScale4x4Luma_Inter[qp_rem][i][j] + qp_const) >> q_bits;
if (level != 0)
{
nonzero=TRUE;
if (level > 1 || lossless_qpprime)
*coeff_cost += MAX_VALUE; // set high cost, shall not be discarded
else
*coeff_cost += COEFF_COST[input->disthres][run];
ACLevel[scan_pos] = sign(level,img->m7[i][j]);
ACRun [scan_pos] = run;
++scan_pos;
run=-1; // reset zero level counter
level=sign(level, img->m7[i][j]);
if(lossless_qpprime)
{
ilev=level;
}
else if(qp_per<4)
{
if(intra == 1)
ilev=(level*InvLevelScale4x4Luma_Intra[qp_rem][i][j]+(1<<(3-qp_per)))>>(4-qp_per);
else
ilev=(level*InvLevelScale4x4Luma_Inter[qp_rem][i][j]+(1<<(3-qp_per)))>>(4-qp_per);
}
else
{
if(intra == 1)
ilev=(level*InvLevelScale4x4Luma_Intra[qp_rem][i][j])<<(qp_per-4);
else
ilev=(level*InvLevelScale4x4Luma_Inter[qp_rem][i][j])<<(qp_per-4);
}
}
if(!lossless_qpprime)
img->m7[i][j]=ilev;
}
ACLevel[scan_pos] = 0;
// IDCT.
// horizontal
for (j=0; j < BLOCK_SIZE && !lossless_qpprime; j++)
{
for (i=0; i < BLOCK_SIZE; i++)
{
m5[i]=img->m7[i][j];
}
m6[0]=(m5[0]+m5[2]);
m6[1]=(m5[0]-m5[2]);
m6[2]=(m5[1]>>1)-m5[3];
m6[3]=m5[1]+(m5[3]>>1);
for (i=0; i < 2; i++)
{
i1=3-i;
img->m7[i][j]=m6[i]+m6[i1];
img->m7[i1][j]=m6[i]-m6[i1];
}
}
// vertical
for (i=0; i < BLOCK_SIZE && !lossless_qpprime; i++)
{
for (j=0; j < BLOCK_SIZE; j++)
{
m5[j]=img->m7[i][j];
}
m6[0]=(m5[0]+m5[2]);
m6[1]=(m5[0]-m5[2]);
m6[2]=(m5[1]>>1)-m5[3];
m6[3]=m5[1]+(m5[3]>>1);
for (j=0; j < 2; j++)
{
j1=3-j;
// Residue Color Transform
if (!img->residue_transform_flag)
{
img->m7[i][j] =min(img->max_imgpel_value,max(0,(m6[j]+m6[j1]+((long)img->mpr[i+block_x][j+block_y] <<DQ_BITS)+DQ_ROUND)>>DQ_BITS));
img->m7[i][j1]=min(img->max_imgpel_value,max(0,(m6[j]-m6[j1]+((long)img->mpr[i+block_x][j1+block_y]<<DQ_BITS)+DQ_ROUND)>>DQ_BITS));
}
else
{
img->m7[i][j] =(m6[j]+m6[j1]+DQ_ROUND)>>DQ_BITS;
img->m7[i][j1]=(m6[j]-m6[j1]+DQ_ROUND)>>DQ_BITS;
}
}
}
// Decoded block moved to frame memory
if (!img->residue_transform_flag)
{
for (j=0; j < BLOCK_SIZE; j++)
{
for (i=0; i < BLOCK_SIZE; i++)
{
if(lossless_qpprime)
enc_picture->imgY[img->pix_y+block_y+j][img->pix_x+block_x+i]=img->m7[i][j]+img->mpr[i+block_x][j+block_y];
else
enc_picture->imgY[img->pix_y+block_y+j][img->pix_x+block_x+i]=img->m7[i][j];
}
}
}
return nonzero;
}
/*!
************************************************************************
* \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
* ones 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(int uv,int cr_cbp)
{
int i,j,i1,j2,ilev,n2,n1,j1,mb_y,coeff_ctr,level ,scan_pos,run;
int m1[BLOCK_SIZE],m5[BLOCK_SIZE],m6[BLOCK_SIZE];
int coeff_cost;
int cr_cbp_tmp;
int DCcoded=0 ;
Macroblock *currMB = &img->mb_data[img->current_mb_nr];
int qp_per,qp_rem,q_bits;
int qp_c;
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/2);
//FRExt
int64 cbpblk_pattern[4]={0, 0xf0000, 0xff0000, 0xffff0000};
int yuv = img->yuv_format;
int b8;
int m3[4][4];
int m4[4][4];
int qp_per_dc = 0;
int qp_rem_dc = 0;
int qp_const;
int q_bits_422 = 0;
int qp_const_422 = 0;
Boolean lossless_qpprime = ((currMB->qp + img->bitdepth_luma_qp_scale)==0 && img->lossless_qpprime_flag==1);
qp_c = currMB->qp + img->chroma_qp_offset[uv];
qp_c = Clip3(-img->bitdepth_chroma_qp_scale,51,qp_c);
qp_c = (qp_c < 0)? qp_c : QP_SCALE_CR[qp_c - MIN_QP];
qp_per = (qp_c + img->bitdepth_chroma_qp_scale)/6;
qp_rem = (qp_c + img->bitdepth_chroma_qp_scale)%6;
q_bits = Q_BITS+qp_per;
if (img->type == I_SLICE)
qp_const=(1<<q_bits)/3; // intra
else
qp_const=(1<<q_bits)/6; // inter
if (img->yuv_format == YUV422)
{
//for YUV422 only
qp_per_dc = (qp_c + 3 + img->bitdepth_chroma_qp_scale)/6;
qp_rem_dc = (qp_c + 3 + img->bitdepth_chroma_qp_scale)%6;
q_bits_422 = Q_BITS+qp_per_dc;
if (img->type == I_SLICE)
qp_const_422=(1<<q_bits_422)/3; // intra
else
qp_const_422=(1<<q_bits_422)/6; // inter
}
//============= 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)
{
// Horizontal transform.
for (j=0; j < BLOCK_SIZE && !lossless_qpprime; j++)
{
mb_y=n2+j;
for (i=0; i < 2; i++)
{
i1=3-i;
m5[i]=img->m7[i+n1][mb_y]+img->m7[i1+n1][mb_y];
m5[i1]=img->m7[i+n1][mb_y]-img->m7[i1+n1][mb_y];
}
img->m7[n1][mb_y] =(m5[0]+m5[1]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -