📄 predict00.c
字号:
//*************************************************
//File name: predict.c
//Author: Anna
//Date:
//************************************************
#define DEC_MBC 45
#define DEC_MBR 36
#define TOP 1
#define LEFT 0
#define _div_div(a, b) (a>0) ? (a+(b>>1))/b : (a-(b>>1))/b
#define abs(a) ((a)>0 ? (a) : -(a))
int dc_store_lum[2*DEC_MBR+1][2*DEC_MBC+1];
int ac_left_lum[2*DEC_MBR+1][2*DEC_MBC+1][7];
int ac_top_lum[2*DEC_MBR+1][2*DEC_MBC+1][7];
int dc_store_chr[2][DEC_MBR+1][DEC_MBC+1];
int ac_left_chr[2][DEC_MBR+1][DEC_MBC+1][7];
int ac_top_chr[2][DEC_MBR+1][DEC_MBC+1][7];
int quant_store[DEC_MBR+1][DEC_MBC+1]; // [Review]
int predict_dir;
int ac_pred_flag;
int quantizer;
int mb_xpos;
int mb_ypos;
extern int dc_scaler;
void dc_recon(int block_num,short *dc_value)
{
if (block_num<4)
{
int b_xpos=(mb_xpos << 1) + (block_num & 1);
int b_ypos=(mb_ypos << 1) + ((block_num & 2) >> 1);
int dc_pred;
// set prediction direction
if (abs(dc_store_lum[b_ypos+1-1][b_xpos+1-1] -
dc_store_lum[b_ypos+1][b_xpos+1-1]) < // Fa - Fb
abs(dc_store_lum[b_ypos+1-1][b_xpos+1-1] -
dc_store_lum[b_ypos+1-1][b_xpos+1])) // Fb - Fc
{
predict_dir = TOP;
dc_pred =dc_store_lum[b_ypos+1-1][b_xpos+1];
}
else
{
predict_dir = LEFT;
dc_pred = dc_store_lum[b_ypos+1][b_xpos+1-1];
}
(* dc_value) += _div_div(dc_pred,dc_scaler);
(* dc_value) *=dc_scaler;
// store dc value
dc_store_lum[b_ypos+1][b_xpos+1] = (* dc_value);
}
else // chrominance blocks
{
int b_xpos = mb_xpos;
int b_ypos = mb_ypos;
int chr_num = block_num - 4;
int dc_pred;
// set prediction direction
if (abs(dc_store_chr[chr_num][b_ypos+1-1][b_xpos+1-1] -
dc_store_chr[chr_num][b_ypos+1][b_xpos+1-1]) < // Fa - Fb
abs(dc_store_chr[chr_num][b_ypos+1-1][b_xpos+1-1] -
dc_store_chr[chr_num][b_ypos+1-1][b_xpos+1])) // Fb - Fc
{
predict_dir = TOP;
dc_pred = dc_store_chr[chr_num][b_ypos+1-1][b_xpos+1];
}
else
{
predict_dir = LEFT;
dc_pred = dc_store_chr[chr_num][b_ypos+1][b_xpos+1-1];
}
(* dc_value) += _div_div(dc_pred,dc_scaler);
(* dc_value) *= dc_scaler;
// store dc value
dc_store_chr[chr_num][b_ypos+1][b_xpos+1] = (* dc_value);
}
}
/***/
static int saiAcLeftIndex[8]=
{
0,8,16,24,32,40,48,56
};
void ac_recon(int block_num, short * psBlock)
{
int b_xpos, b_ypos;
int i;
if (block_num < 4) {
b_xpos = (mb_xpos << 1) + (block_num & 1);
b_ypos = (mb_ypos << 1) + ((block_num & 2) >> 1);
}
else {
b_xpos = mb_xpos;
b_ypos = mb_ypos;
}
// predict coefficients
if (ac_pred_flag)
{
if (block_num < 4)
{
if (predict_dir == TOP)
{
for (i = 1; i < 8; i++) // [Review] index can become more efficient [0..7]
psBlock[i] +=ac_top_lum[b_ypos+1-1][b_xpos+1][i-1];
}
else // left prediction
{
for (i = 1; i < 8; i++)
psBlock[saiAcLeftIndex[i]] +=ac_left_lum[b_ypos+1][b_xpos+1-1][i-1];
}
}
else
{
int chr_num = block_num - 4;
if (predict_dir == TOP)
{
for (i = 1; i < 8; i++)
psBlock[i] += ac_top_chr[chr_num][b_ypos+1-1][b_xpos+1][i-1];
}
else // left prediction
{
for (i = 1; i < 8; i++)
psBlock[saiAcLeftIndex[i]] += ac_left_chr[chr_num][b_ypos+1][b_xpos+1-1][i-1];
}
}
}
}
/***/
void ac_store(int block_num, short * psBlock)
{
int b_xpos, b_ypos;
int i;
// [Review] This lines of code are repeated frequently
if (block_num < 4) {
b_xpos = (mb_xpos << 1) + (block_num & 1);
b_ypos = (mb_ypos << 1) + ((block_num & 2) >> 1);
}
else {
b_xpos = mb_xpos;
b_ypos = mb_ypos;
}
// store coefficients
if (block_num < 4)
{
for (i = 1; i < 8; i++) {
ac_top_lum[b_ypos+1][b_xpos+1][i-1] = psBlock[i];
ac_left_lum[b_ypos+1][b_xpos+1][i-1] = psBlock[saiAcLeftIndex[i]];
}
}
else
{
int chr_num = block_num - 4;
for (i = 1; i < 8; i++) {
ac_top_chr[chr_num][b_ypos+1][b_xpos+1][i-1] = psBlock[i];
ac_left_chr[chr_num][b_ypos+1][b_xpos+1][i-1] = psBlock[saiAcLeftIndex[i]];
}
}
}
/***/
#define _rescale(predict_quant, current_quant, coeff) (coeff != 0) ? \
_div_div((coeff) * (predict_quant), (current_quant)) : 0
int ac_rescaling(int block_num, short * psBlock)
{
int mb_xpos_ac = mb_xpos;
int mb_ypos_ac = mb_ypos;
int current_quant = quantizer;
int predict_quant = (predict_dir == TOP) ?
quant_store[mb_ypos][mb_xpos+1] : quant_store[mb_ypos+1][mb_xpos];
int b_xpos, b_ypos; // index for stored coeff matrix
int i;
if ((! ac_pred_flag) || (current_quant == predict_quant) || (block_num == 3))
return 0;
if ((mb_ypos_ac == 0) && (predict_dir == TOP))
return 0;
if ((mb_xpos_ac == 0) && (predict_dir == LEFT))
return 0;
if ((mb_xpos_ac == 0) && (mb_ypos_ac == 0))
return 0;
// [Review] This lines of code are repeated frequently
if (block_num < 4) {
b_xpos = (mb_xpos << 1) + (block_num & 1);
b_ypos = (mb_ypos << 1) + ((block_num & 2) >> 1);
}
else {
b_xpos =mb_xpos;
b_ypos =mb_ypos;
}
if (predict_dir == TOP) // rescale only if really needed
{
switch (block_num)
{
case 0: case 1:
for (i = 1; i < 8; i++)
psBlock[i] += _rescale(predict_quant, current_quant, ac_top_lum[b_ypos][b_xpos+1][i-1]);
return 1;
break;
case 4:
for (i = 1; i < 8; i++)
psBlock[i] += _rescale(predict_quant, current_quant, ac_top_chr[0][b_ypos][b_xpos+1][i-1]);
return 1;
break;
case 5:
for (i = 1; i < 8; i++)
psBlock[i] += _rescale(predict_quant, current_quant, ac_top_chr[1][b_ypos][b_xpos+1][i-1]);
return 1;
break;
}
}
else
{
switch (block_num)
{
case 0: case 2:
for (i = 1; i < 8; i++)
psBlock[saiAcLeftIndex[i]] += _rescale(predict_quant, current_quant, ac_left_lum[b_ypos+1][b_xpos][i-1]);
return 1;
break;
case 4:
for (i = 1; i < 8; i++)
psBlock[saiAcLeftIndex[i]] += _rescale(predict_quant, current_quant, ac_left_chr[0][b_ypos+1][b_xpos][i-1]);
return 1;
break;
case 5:
for (i = 1; i < 8; i++)
psBlock[saiAcLeftIndex[i]] += _rescale(predict_quant, current_quant, ac_left_chr[1][b_ypos+1][b_xpos][i-1]);
return 1;
break;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -