📄 dsp_vop.c
字号:
//@ BitstreamPutBits( (UInt) 0, 1L);
//@ if ((Mode == MODE_GMC) || (Mode == MODE_GMC_Q))
//@ BitstreamPutBits( (UInt) 1, 1L);
//@ }
/* ACpred_flag */
if ((Mode == MODE_INTRA || Mode==MODE_INTRA_Q) && ACpred_flag != -1)
{
BitstreamPutBits((UInt)ACpred_flag, 1L);
}
/* CBPY */
PutCBPY (cbpy, (Char)(Mode==MODE_INTRA||Mode==MODE_INTRA_Q));
/* DQUANT */
//@ if ((Mode == MODE_INTER_Q) || (Mode == MODE_INTRA_Q)|| (Mode == MODE_GMC_Q))
//@ {
//@ switch (DQUANT)
//@ {
//@ case -1:
//@ BitstreamPutBits( 0L, 2L);
//@ break;
//@ case -2:
//@ BitstreamPutBits( 1L, 2L);
//@ break;
//@ case 1:
//@ BitstreamPutBits( 2L, 2L);
//@ break;
//@ case 2:
//@ BitstreamPutBits( 3L, 2L);
//@ break;
//@ default:
//@ fprintf(stderr,"Invalid DQUANT\n");
//@ exit(1);
//@ }
//@ }
}
//宏块系数编码
void MB_CodeCoeff(short *qcoeff,char Mode, Int CBP, char ncoeffs,char direction[])
{
register char i, m;
short coeff[64];
char *zz = zigzag;
//如果是帧内
if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
{
//@ if (intra_dcpred_disable == 0)
//@ {
for (i = 0; i < 6; i++)//4个8*8亮度块和2个8*8色度块
{
// if (i>3 || transp_pattern[i]!=1) /* Not transparent */
//@ {
//@ if (!alternate_scan)
//@ {
switch (direction[i])//判断方向进行不同的Z扫描
{
case 1: zz = zigzag_v; break;//垂直
case 2: zz = zigzag_h; break;//水平
case 0: break;
//@ default: fprintf(stderr, "MB_CodeCoeff(): Error in zigzag direction\n");
//@ exit(-1);
}
//@ }
/* Do the zigzag scanning of coefficients */
//将量化以后的系数以Z扫描的形式一一传给coeff
for (m = 0; m < 64; m++)
{
*(coeff + zz[m]) = qcoeff[i*ncoeffs+m];
}
//@ if (switched==0)
//@ {
//@ if (error_res_disable)
//@ {//对DC系数进行编码,包括Y,C两种
if (i < 4)
// bits->Y += 亮度
IntraDC_dpcm(coeff[0],1);
else
// bits->C += 色度
IntraDC_dpcm(coeff[0],0);
//@ }
//@ }
//对每个8*8块剩余的63个AC系数进行编码
/* Code AC coeffs. dep. on block pattern MW 15-NOV-1996 */
if ((i==0 && CBP&32) ||
(i==1 && CBP&16) ||
(i==2 && CBP&8) ||
(i==3 && CBP&4) ||
(i==4 && CBP&2) ||
(i==5 && CBP&1))
{
//@ if (error_res_disable || ((!error_res_disable) && (!reverse_vlc)))
//@ {
// if (i < 4)
// bits->Y +=
CodeCoeff(1,Mode, coeff,ncoeffs);//从1开始
// else
// bits->C +=
// CodeCoeff(1,Mode, coeff,ncoeffs);
//@ }
}
//@ }
}
//@ }
}
else // inter block encoding帧间编码
{
for (i = 0; i < 6; i++)
{
// Do the zigzag scanning of coefficients 通用Z形扫描
for (m = 0; m < 64; m++)
*(coeff + zz[m]) = qcoeff[i*ncoeffs+m];
//对于帧间的误差图像不进行DC预测,全部直接进行VLC编码
if ((i==0 && CBP&32) ||
(i==1 && CBP&16) ||
(i==2 && CBP&8) ||
(i==3 && CBP&4) ||
(i==4 && CBP&2) ||
(i==5 && CBP&1))
{
//@ if (error_res_disable || ((!error_res_disable) && (!reverse_vlc)))
//@ {
// if (i < 4)
// bits->Y +=
CodeCoeff(0,Mode, coeff, ncoeffs);//直接从0开始
// else
// bits->C +=
// CodeCoeff(0,Mode, coeff, ncoeffs);
//@ }
}
}
}
}
//帧内DC预测
char IntraDC_dpcm(short val, char lum)//val为系数值
{
char n_bits;
short absval,exclu;
char size = 0;
exclu =0;
absval = ( val <0)?-val:val; /* abs(val) */
/* compute dct_dc_size */
size = 0;
while(absval)
{
absval>>=1;
size++;
exclu= exclu<<1;
exclu |=1;
}
if (lum)
{ /* luminance亮度 */
n_bits = PutDCsize_lum (size);
}
else
{ /* chrominance色度 */
n_bits = PutDCsize_chrom (size);
}
if ( size != 0 )
{
if (val<0)
{
absval = -val; /* set to "-val" MW 14-NOV-1996 */
//@ val = (absval ^( (int)pow(2.0,(double)size)-1) );
val = (absval ^ exclu);//ldm
}
BitstreamPutBits( (UInt)(val), (UChar)(size));//size为系数值的二进制比特殊
n_bits += size;
if (size > 8)
BitstreamPutBits( 1, 1);
}
return n_bits; /* # bits for intra_dc dpcm */
}
short CodeCoeff(char j_start, char Mode, short qcoeff[], char ncoeffs)
{
char j, first, s, prev_s, run, prev_run;
UChar length;
short level, prev_level;
short bits;
//@ Int prev_ind, ind;// seem useless
run = 0;
bits = 0;
first = 1;
//@ prev_ind = ind = 0;
prev_level = level = 0;
prev_run = s = prev_s = 0;
// prev_run = prev_level = prev_ind = level = s = prev_s = ind = 0;
for (j = j_start; j< ncoeffs; j++)
{
/* encode AC coeff */
s = 0;
/* Increment run if coeff is zero */
if ((level = qcoeff[j]) == 0)//判断0的长度
{
run++;
}
else
{
/* code run & level and count bits 对行程和像素值编码并计算比特数*/
if (level < 0)
{
s = 1;
level = -level;
}
//@ ind = level | run<<4;
//@ ind = ind | 0<<12; /* Not last coeff */
if (!first)
{
/* Encode the previous ind */
if ((prev_run < 64) &&
(((prev_level < 13) && (Mode != MODE_INTRA &&
Mode != MODE_INTRA_Q))
|| ((prev_level < 28) && (Mode == MODE_INTRA ||
Mode == MODE_INTRA_Q))))
{
/* Separate tables for Intra luminance blocks */
if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
{
length = PutCoeff_Intra(prev_run, prev_level,0);
}
else
{
length = PutCoeff_Inter(prev_run, prev_level,0);
}
}
else
length = 0;
/* First escape mode. Level offset */
if (length == 0)
{
if ( prev_run < 64 )
{
/* subtraction of Max level, last = 0 */
short level_minus_max;
if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
level_minus_max = prev_level -
intra_max_level[0][prev_run];
else
level_minus_max = prev_level -
inter_max_level[0][prev_run];
if ( ( (level_minus_max < 13) && (Mode != MODE_INTRA && Mode != MODE_INTRA_Q) ) ||
( (level_minus_max < 28) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q) ) )
{
/* Separate tables for Intra luminance blocks */
if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
{
length = PutLevelCoeff_Intra(prev_run, level_minus_max, 0);
}
else
{
length = PutLevelCoeff_Inter(prev_run, level_minus_max, 0);
}
} else
length = 0;
}
else length = 0;
}
/* Second escape mode. Run offset */
if (length == 0)
{
if ( ((prev_level < 13) && (Mode != MODE_INTRA && Mode != MODE_INTRA_Q)) ||
((prev_level < 28) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)) )
{
/* subtraction of Max Run, last = 0 */
short run_minus_max;
//@ if (prev_level == 0)
//@ {
//@ fprintf (stdout, "ERROR(CodeCoeff-second esc): level is %d\n", prev_level);
//@ exit(-1);
//@ }
if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
run_minus_max = prev_run - (intra_max_run0[prev_level]+1);
else
run_minus_max = prev_run - (inter_max_run0[prev_level]+1);
/* boon 120697 */
if (run_minus_max < 64)
{
/* Separate tables for Intra luminance blocks */
if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
{
length = PutRunCoeff_Intra(run_minus_max, prev_level, 0);
}
else
{
length = PutRunCoeff_Inter(run_minus_max, prev_level, 0);
}
} else
length = 0;
}
else length = 0;
}
/* Third escape mode. FLC */
if (length == 0)
{ /* Escape coding */
if (prev_s == 1)
{
prev_level = (prev_level^0xfff)+1;
}
BitstreamPutBits( 3L, 7L);
BitstreamPutBits( 3L, 2L);
/* last */
BitstreamPutBits( 0L, 1L);
/* run */
BitstreamPutBits( (UInt)(prev_run), 6L);
BitstreamPutBits( MARKER_BIT, 1);
/* level */
BitstreamPutBits( (UInt)(prev_level), 12L);
BitstreamPutBits( MARKER_BIT, 1);
bits += 30;
}
else
{
BitstreamPutBits( (UInt)(prev_s), 1L);
bits += length + 1;
}
}
prev_run = run; prev_s = s;
prev_level = level;
//@ prev_ind = ind;
run = first = 0;
}
}
/* Encode the last coeff */
if (!first)
{
//@ prev_ind = prev_ind | 1<<12; /* last coeff */
if ((prev_run < 64) &&
(((prev_level < 4) && (Mode != MODE_INTRA &&
Mode != MODE_INTRA_Q))
|| ((prev_level < 9) && ((Mode == MODE_INTRA) ||
(Mode == MODE_INTRA_Q)))))
{
/* Separate tables for Intra luminance blocks */
if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
{
length = PutCoeff_Intra(prev_run, prev_level, 1);
}
else
{
length = PutCoeff_Inter(prev_run, prev_level, 1);
}
}
else
length = 0;
/* First escape mode. Level offset */
if (length == 0)
{
if ( prev_run < 64 )
{
/* subtraction of Max level, last = 0 */
int level_minus_max;
if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
level_minus_max = prev_level - intra_max_level[1][prev_run];
else
level_minus_max = prev_level - inter_max_level[1][prev_run];
if ( ( (level_minus_max < 4) && (Mode != MODE_INTRA && Mode != MODE_INTRA_Q) ) ||
( (level_minus_max < 9) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q) ) )
{
/* Separate tables for Intra luminance blocks */
if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
{
length = PutLevelCoeff_Intra(prev_run, level_minus_max, 1);
}
else
{
length = PutLevelCoeff_Inter(prev_run, level_minus_max, 1);
}
} else
length = 0;
}
else length = 0;
}
/* Second escape mode. Run offset */
if (length == 0)
{
if (((prev_level < 4) && (Mode != MODE_INTRA && Mode != MODE_INTRA_Q))||
((prev_level < 9) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)) )
{
/* subtraction of Max Run, last = 1 */
int run_minus_max;
//@ if (prev_level == 0)
//@ {
//@ fprintf (stdout, "ERROR(CodeCoeff-second esc): level is %d\n", prev_level);
//@ exit(-1);
//@ }
if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
run_minus_max = prev_run - (intra_max_run1[prev_level]+1);
else
run_minus_max = prev_run - (inter_max_run1[prev_level]+1);
if (run_minus_max < 64) /* boon 120697 */
{
/* Separate tables for Intra luminance blocks */
if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
{
length = PutRunCoeff_Intra(run_minus_max, prev_level, 1);
}
else
{
length = PutRunCoeff_Inter(run_minus_max, prev_level, 1);
}
} else
length = 0;
}
else length = 0;
}
/* Third escape mode. FLC */
if (length == 0)
{ /* Escape coding */
if (prev_s == 1)
{
prev_level = (prev_level^0xfff)+1;
}
BitstreamPutBits( 3L, 7L);
BitstreamPutBits( 3L, 2L); /* boon */
BitstreamPutBits( 1L, 1L); /* last */
BitstreamPutBits( (UInt)(prev_run), 6L);
BitstreamPutBits( MARKER_BIT, 1);
/* level */
BitstreamPutBits( (UInt)(prev_level), 12L);
BitstreamPutBits( MARKER_BIT, 1);
bits += 30;
}
else
{
BitstreamPutBits( (UInt)(prev_s), 1L);
bits += length + 1;
}
}
return bits;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -