📄 block.c
字号:
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;i++)
{
for (j=0;j<4;j++)
M5[j]=M0[i][ii][j][jj];
M6[0]=(M5[0]+M5[2])*13;
M6[1]=(M5[0]-M5[2])*13;
M6[2]=M5[1]*7 -M5[3]*17;
M6[3]=M5[1]*17+M5[3]*7;
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];
}
}
}
}
for (j=0;j<16;j++)
{
for (i=0;i<16;i++)
{
M1[i][j]=M0[i%4][i/4][j%4][j/4];
}
}
for (j=0;j<16;j++)
for (i=0;i<16;i++)
imgY[img->pix_y+j][img->pix_x+i]=min(255,max(0,(M1[i][j]+img->mprr_2[new_intra_mode][j][i]*JQQ1+JQQ2)/JQQ1));
}
/************************************************************************
*
* Routine: intrapred_chroma()
*
* Description: Intra prediction for chroma. There is only one prediction mode,
* corresponding to 'DC prediction' for luma. However,since 2x2 transform
* of DC levels are used,all predictions are made from neighbouring MBs.
* Prediction also depends on whether the block is at a frame edge.
*
* Input: Starting point of current chroma macro block image posision
*
* Output: 8x8 array with DC intra chroma prediction and diff array
*
************************************************************************/
void intrapred_chroma(int img_c_x,int img_c_y,int uv)
{
int s[2][2],s0,s1,s2,s3;
int i,j;
int mb_nr = img->current_mb_nr;
int mb_width = img->width/16;
int mb_available_up = (img_c_y/BLOCK_SIZE == 0) ? 0 : (img->slice_numbers[mb_nr] == img->slice_numbers[mb_nr-mb_width]);
int mb_available_left = (img_c_x/BLOCK_SIZE == 0) ? 0 : (img->slice_numbers[mb_nr] == img->slice_numbers[mb_nr-1]);
s0=s1=s2=s3=0; /* reset counters */
for (i=0; i < BLOCK_SIZE; i++)
{
if(mb_available_up)
{
s0 += imgUV[uv][img_c_y-1][img_c_x+i];
s1 += imgUV[uv][img_c_y-1][img_c_x+i+BLOCK_SIZE];
}
if(mb_available_left)
{
s2 += imgUV[uv][img_c_y+i][img_c_x-1];
s3 += imgUV[uv][img_c_y+i+BLOCK_SIZE][img_c_x-1];
}
}
if(mb_available_up && mb_available_left)
{
s[0][0]=(s0+s2+4)/(2*BLOCK_SIZE);
s[1][0]=(s1+2)/BLOCK_SIZE;
s[0][1]=(s3+2)/BLOCK_SIZE;
s[1][1]=(s1+s3+4)/(2*BLOCK_SIZE);
}
else
if(mb_available_up && !mb_available_left)
{
s[0][0]=(s0+2)/BLOCK_SIZE;
s[1][0]=(s1+2)/BLOCK_SIZE;
s[0][1]=(s0+2)/BLOCK_SIZE;
s[1][1]=(s1+2)/BLOCK_SIZE;
}
else
if(!mb_available_up && mb_available_left)
{
s[0][0]=(s2+2)/BLOCK_SIZE;
s[1][0]=(s2+2)/BLOCK_SIZE;
s[0][1]=(s3+2)/BLOCK_SIZE;
s[1][1]=(s3+2)/BLOCK_SIZE;
}
else
if(!mb_available_up && !mb_available_left)
{
s[0][0]=128;
s[1][0]=128;
s[0][1]=128;
s[1][1]=128;
}
for (j=0; j < MB_BLOCK_SIZE/2; j++)
{
for (i=0; i < MB_BLOCK_SIZE/2; i++)
{
img->mpr[i][j]=s[i/BLOCK_SIZE][j/BLOCK_SIZE];
img->m7[i][j]=imgUV_org[uv][img_c_y+j][img_c_x+i]-img->mpr[i][j];
}
}
}
/************************************************************************
*
* Routine: dct_luma
*
* Description: 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.
*
*
* Input: block_x,block_y: Block position inside a macro block (0,4,8,12).
*
* Output: nonzero: 0 if no levels are nonzero. 1 if there are nonzero levels.
* coeff_cost: Counter for nonzero coefficients, used to discard expencive levels.
*
*
************************************************************************/
int dct_luma(int block_x,int block_y,int *coeff_cost)
{
int sign(int a,int b);
int i,j,i1,j1,ilev,m5[4],m6[4],coeff_ctr,scan_loop_ctr;
int qp_const,pos_x,pos_y,quant_set,level,scan_pos,run;
int nonzero;
int idx;
int scan_mode;
int loop_rep;
#ifndef NO_RDQUANT
int coeff[16];
#endif
if (img->type == INTRA_IMG)
qp_const=JQQ3; /* intra */
else
qp_const=JQQ4; /* inter */
pos_x=block_x/BLOCK_SIZE;
pos_y=block_y/BLOCK_SIZE;
/* Horizontal transform */
for (j=0; j < BLOCK_SIZE; 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])*13;
img->m7[2][j]=(m5[0]-m5[1])*13;
img->m7[1][j]=m5[3]*17+m5[2]*7;
img->m7[3][j]=m5[3]*7-m5[2]*17;
}
/* Vertival transform */
for (i=0; i < BLOCK_SIZE; 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])*13;
img->m7[i][2]=(m5[0]-m5[1])*13;
img->m7[i][1]=m5[3]*17+m5[2]*7;
img->m7[i][3]=m5[3]*7-m5[2]*17;
}
/* Quant */
quant_set=img->qp;
nonzero=FALSE;
if (img->imod == INTRA_MB_OLD && img->qp < 24)
{
scan_mode=DOUBLE_SCAN;
loop_rep=2;
idx=1;
}
else
{
scan_mode=SINGLE_SCAN;
loop_rep=1;
idx=0;
}
#ifndef NO_RDQUANT
for(scan_loop_ctr=0;scan_loop_ctr<loop_rep;scan_loop_ctr++) /* 2 times if double scan, 1 normal scan */
{
for (coeff_ctr=0;coeff_ctr < 16/loop_rep;coeff_ctr++) /* 8 times if double scan, 16 normal scan */
{
if (scan_mode==DOUBLE_SCAN)
{
i=DBL_SCAN[coeff_ctr][0][scan_loop_ctr];
j=DBL_SCAN[coeff_ctr][1][scan_loop_ctr];
}
else
{
i=SNGL_SCAN[coeff_ctr][0];
j=SNGL_SCAN[coeff_ctr][1];
}
coeff[coeff_ctr]=img->m7[i][j];
}
if (scan_mode==DOUBLE_SCAN)
rd_quant(QUANT_LUMA_DBL,coeff);
else
rd_quant(QUANT_LUMA_SNG,coeff);
run=-1;
scan_pos=scan_loop_ctr*9; /* for double scan; set first or second scan posision */
for (coeff_ctr=0; coeff_ctr<16/loop_rep; coeff_ctr++)
{
if (scan_mode==DOUBLE_SCAN)
{
i=DBL_SCAN[coeff_ctr][0][scan_loop_ctr];
j=DBL_SCAN[coeff_ctr][1][scan_loop_ctr];
}
else
{
i=SNGL_SCAN[coeff_ctr][0];
j=SNGL_SCAN[coeff_ctr][1];
}
run++;
ilev=0;
level= absm(coeff[coeff_ctr]);
if (level != 0)
{
nonzero=TRUE;
if (level > 1)
*coeff_cost += MAX_VALUE; /* set high cost, shall not be discarded */
else
*coeff_cost += COEFF_COST[run];
img->cof[pos_x][pos_y][scan_pos][0][scan_mode]=sign(level,img->m7[i][j]);
img->cof[pos_x][pos_y][scan_pos][1][scan_mode]=run;
++scan_pos;
run=-1; /* reset zero level counter */
ilev=level*JQ[quant_set][1];
}
img->m7[i][j]=sign(ilev,img->m7[i][j]);
}
img->cof[pos_x][pos_y][scan_pos][0][scan_mode]=0; /* end of block */
}
#endif
#ifdef NO_RDQUANT
for(scan_loop_ctr=0;scan_loop_ctr<loop_rep;scan_loop_ctr++) /* 2 times if double scan, 1 normal scan */
{
run=-1;
scan_pos=scan_loop_ctr*9;
for (coeff_ctr=0;coeff_ctr < 16/loop_rep;coeff_ctr++) /* 8 times if double scan, 16 normal scan */
{
if (scan_mode==DOUBLE_SCAN)
{
i=DBL_SCAN[coeff_ctr][0][scan_loop_ctr];
j=DBL_SCAN[coeff_ctr][1][scan_loop_ctr];
}
else
{
i=SNGL_SCAN[coeff_ctr][0];
j=SNGL_SCAN[coeff_ctr][1];
}
run++;
ilev=0;
level = (abs (img->m7[i][j]) * JQ[quant_set][0] +qp_const) / JQQ1;
if (level != 0)
{
nonzero=TRUE;
if (level > 1)
*coeff_cost += MAX_VALUE; /* set high cost, shall not be discarded */
else
*coeff_cost += COEFF_COST[run];
img->cof[pos_x][pos_y][scan_pos][0][scan_mode]=sign(level,img->m7[i][j]);
img->cof[pos_x][pos_y][scan_pos][1][scan_mode]=run;
++scan_pos;
run=-1; /* reset zero level counter */
ilev=level*JQ[quant_set][1];
}
img->m7[i][j]=sign(ilev,img->m7[i][j]);
}
img->cof[pos_x][pos_y][scan_pos][0][scan_mode]=0; /* end of block */
}
#endif
/* IDCT. */
/* horizontal */
for (j=0; j < BLOCK_SIZE; j++)
{
for (i=0; i < BLOCK_SIZE; i++)
{
m5[i]=img->m7[i][j];
}
m6[0]=(m5[0]+m5[2])*13;
m6[1]=(m5[0]-m5[2])*13;
m6[2]=m5[1]*7-m5[3]*17;
m6[3]=m5[1]*17+m5[3]*7;
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; i++)
{
for (j=0; j < BLOCK_SIZE; j++)
{
m5[j]=img->m7[i][j];
}
m6[0]=(m5[0]+m5[2])*13;
m6[1]=(m5[0]-m5[2])*13;
m6[2]=m5[1]*7-m5[3]*17;
m6[3]=m5[1]*17+m5[3]*7;
for (j=0; j < 2; j++)
{
j1=3-j;
img->m7[i][j] =min(255,max(0,(m6[j]+m6[j1]+img->mpr[i+block_x][j+block_y] *JQQ1+JQQ2)/JQQ1));
img->m7[i][j1]=min(255,max(0,(m6[j]-m6[j1]+img->mpr[i+block_x][j1+block_y] *JQQ1+JQQ2)/JQQ1));
}
}
/* Decoded block moved to frame memory */
for (j=0; j < BLOCK_SIZE; j++)
for (i=0; i < BLOCK_SIZE; i++)
imgY[img->pix_y+block_y+j][img->pix_x+block_x+i]=img->m7[i][j];
return nonzero;
}
/************************************************************************
*
* Routine : dct_chroma
*
* Description:
* 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.
*
* Input: uv : Make difference between the U and V chroma component
* cr_cbp: chroma coded block pattern
*
* Output: cr_cbp: Updated chroma coded block pattern.
*
*
************************************************************************/
#ifndef NO_RDQUANT
int dct_chroma(int uv,int cr_cbp)
{
int i,j,i1,j2,ilev,n2,n1,j1,mb_y,coeff_ctr,qp_const,pos_x,pos_y,quant_set,level ,scan_pos,run;
int m1[BLOCK_SIZE],m5[BLOCK_SIZE],m6[BLOCK_SIZE];
int coeff[16];
if (img->type == INTRA_IMG)
qp_const=JQQ3;
else
qp_const=JQQ4;
for (n2=0; n2 <= BLOCK_SIZE; n2 += BLOCK_SIZE)
{
for (n1=0; n1 <= BLOCK_SIZE; n1 += BLOCK_SIZE)
{
/* Horizontal transform. */
for (j=0; j < BLOCK_SIZE; 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])*13;
img->m7[n1+2][mb_y]=(m5[0]-m5[1])*13;
img->m7[n1+1][mb_y]=m5[3]*17+m5[2]*7;
img->m7[n1+3][mb_y]=m5[3]*7-m5[2]*17;
}
/* Vertical transform. */
for (i=0; i < BLOCK_SIZE; i++)
{
j1=n1+i;
for (j=0; j < 2; j++)
{
j2=3-j;
m5[j]=img->m7[j1][n2+j]+img->m7[j1][n2+j2];
m5[j2]=img->m7[j1][n2+j]-img->m7[j1][n2+j2];
}
img->m7[j1][n2+0]=(m5[0]+m5[1])*13;
img->m7[j1][n2+2]=(m5[0]-m5[1])*13;
img->m7[j1][n2+1]=m5[3]*17+m5[2]*7;
img->m7[j1][n2+3]=m5[3]*7-m5[2]*17;
}
}
}
/* 2X2 transform of DC coeffs. */
m1[0]=(img->m7[0][0]+img->m7[4][0]+img->m7[0][4]+img->m7[4][4])/2;
m1[1]=(img->m7[0][0]-img->m7[4][0]+img->m7[0][4]-img->m7[4][4])/2;
m1[2]=(img->m7[0][0]+img->m7[4][0]-img->m7[0][4]-img->m7[4][4])/2;
m1[3]=(img->m7[0][0]-img->m7[4][0]-img->m7[0][4]+img->m7[4][4])/2;
/* Quant of chroma 2X2 coeffs.*/
quant_set=QP_SCALE_CR[img->qp];
run=-1;
scan_pos=0;
for (coeff_ctr=0; coeff_ctr < 4; coeff_ctr++)
coeff[coeff_ctr]=m1[coeff_ctr];
rd_quant(QUANT_CHROMA_DC,coeff);
for (coeff_ctr=0; coeff_ctr < 4; coeff_ctr++)
{
run++;
ilev=0;
level =0;
level =(absm(coeff[coeff_ctr]));
if (level != 0)
{
cr_cbp=max(1,cr_cbp);
img->cofu[scan_pos][0][uv]=sign(level ,m1[coeff_ctr]);
img->cofu[scan_pos][1][uv]=run;
scan_pos++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -