📄 transform8x8.c
字号:
/*!
***************************************************************************
* \file transform8x8.c
*
* \brief
* 8x8 transform functions
*
* \author
* Main contributors (see contributors.h for copyright, address and affiliation details)
* - Yuri Vatis
* - Jan Muenster
* - Lowell Winger <lwinger@lsil.com>
* \date
* 12. October 2003
**************************************************************************
*/
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include "global.h"
#include "image.h"
#include "mb_access.h"
#include "elements.h"
#include "cabac.h"
#include "vlc.h"
#include "transform8x8.h"
#include "transform.h"
#include "macroblock.h"
#include "symbol.h"
int cofAC8x8_chroma[2][4][2][18];
static int diff64[64];
const int quant_coef8[6][8][8] =
{
{
{13107, 12222, 16777, 12222, 13107, 12222, 16777, 12222},
{12222, 11428, 15481, 11428, 12222, 11428, 15481, 11428},
{16777, 15481, 20972, 15481, 16777, 15481, 20972, 15481},
{12222, 11428, 15481, 11428, 12222, 11428, 15481, 11428},
{13107, 12222, 16777, 12222, 13107, 12222, 16777, 12222},
{12222, 11428, 15481, 11428, 12222, 11428, 15481, 11428},
{16777, 15481, 20972, 15481, 16777, 15481, 20972, 15481},
{12222, 11428, 15481, 11428, 12222, 11428, 15481, 11428}
},
{
{11916, 11058, 14980, 11058, 11916, 11058, 14980, 11058},
{11058, 10826, 14290, 10826, 11058, 10826, 14290, 10826},
{14980, 14290, 19174, 14290, 14980, 14290, 19174, 14290},
{11058, 10826, 14290, 10826, 11058, 10826, 14290, 10826},
{11916, 11058, 14980, 11058, 11916, 11058, 14980, 11058},
{11058, 10826, 14290, 10826, 11058, 10826, 14290, 10826},
{14980, 14290, 19174, 14290, 14980, 14290, 19174, 14290},
{11058, 10826, 14290, 10826, 11058, 10826, 14290, 10826}
},
{
{10082, 9675, 12710, 9675, 10082, 9675, 12710, 9675},
{9675, 8943, 11985, 8943, 9675, 8943, 11985, 8943},
{12710, 11985, 15978, 11985, 12710, 11985, 15978, 11985},
{9675, 8943, 11985, 8943, 9675, 8943, 11985, 8943},
{10082, 9675, 12710, 9675, 10082, 9675, 12710, 9675},
{9675, 8943, 11985, 8943, 9675, 8943, 11985, 8943},
{12710, 11985, 15978, 11985, 12710, 11985, 15978, 11985},
{9675, 8943, 11985, 8943, 9675, 8943, 11985, 8943}
},
{
{9362, 8931, 11984, 8931, 9362, 8931, 11984, 8931},
{8931, 8228, 11259, 8228, 8931, 8228, 11259, 8228},
{11984, 11259, 14913, 11259, 11984, 11259, 14913, 11259},
{8931, 8228, 11259, 8228, 8931, 8228, 11259, 8228},
{9362, 8931, 11984, 8931, 9362, 8931, 11984, 8931},
{8931, 8228, 11259, 8228, 8931, 8228, 11259, 8228},
{11984, 11259, 14913, 11259, 11984, 11259, 14913, 11259},
{8931, 8228, 11259, 8228, 8931, 8228, 11259, 8228}
},
{
{8192, 7740, 10486, 7740, 8192, 7740, 10486, 7740},
{7740, 7346, 9777, 7346, 7740, 7346, 9777, 7346},
{10486, 9777, 13159, 9777, 10486, 9777, 13159, 9777},
{7740, 7346, 9777, 7346, 7740, 7346, 9777, 7346},
{8192, 7740, 10486, 7740, 8192, 7740, 10486, 7740},
{7740, 7346, 9777, 7346, 7740, 7346, 9777, 7346},
{10486, 9777, 13159, 9777, 10486, 9777, 13159, 9777},
{7740, 7346, 9777, 7346, 7740, 7346, 9777, 7346}
},
{
{7282, 6830, 9118, 6830, 7282, 6830, 9118, 6830},
{6830, 6428, 8640, 6428, 6830, 6428, 8640, 6428},
{9118, 8640, 11570, 8640, 9118, 8640, 11570, 8640},
{6830, 6428, 8640, 6428, 6830, 6428, 8640, 6428},
{7282, 6830, 9118, 6830, 7282, 6830, 9118, 6830},
{6830, 6428, 8640, 6428, 6830, 6428, 8640, 6428},
{9118, 8640, 11570, 8640, 9118, 8640, 11570, 8640},
{6830, 6428, 8640, 6428, 6830, 6428, 8640, 6428}
}
};
const int dequant_coef8[6][8][8] =
{
{
{20, 19, 25, 19, 20, 19, 25, 19},
{19, 18, 24, 18, 19, 18, 24, 18},
{25, 24, 32, 24, 25, 24, 32, 24},
{19, 18, 24, 18, 19, 18, 24, 18},
{20, 19, 25, 19, 20, 19, 25, 19},
{19, 18, 24, 18, 19, 18, 24, 18},
{25, 24, 32, 24, 25, 24, 32, 24},
{19, 18, 24, 18, 19, 18, 24, 18}
},
{
{22, 21, 28, 21, 22, 21, 28, 21},
{21, 19, 26, 19, 21, 19, 26, 19},
{28, 26, 35, 26, 28, 26, 35, 26},
{21, 19, 26, 19, 21, 19, 26, 19},
{22, 21, 28, 21, 22, 21, 28, 21},
{21, 19, 26, 19, 21, 19, 26, 19},
{28, 26, 35, 26, 28, 26, 35, 26},
{21, 19, 26, 19, 21, 19, 26, 19}
},
{
{26, 24, 33, 24, 26, 24, 33, 24},
{24, 23, 31, 23, 24, 23, 31, 23},
{33, 31, 42, 31, 33, 31, 42, 31},
{24, 23, 31, 23, 24, 23, 31, 23},
{26, 24, 33, 24, 26, 24, 33, 24},
{24, 23, 31, 23, 24, 23, 31, 23},
{33, 31, 42, 31, 33, 31, 42, 31},
{24, 23, 31, 23, 24, 23, 31, 23}
},
{
{28, 26, 35, 26, 28, 26, 35, 26},
{26, 25, 33, 25, 26, 25, 33, 25},
{35, 33, 45, 33, 35, 33, 45, 33},
{26, 25, 33, 25, 26, 25, 33, 25},
{28, 26, 35, 26, 28, 26, 35, 26},
{26, 25, 33, 25, 26, 25, 33, 25},
{35, 33, 45, 33, 35, 33, 45, 33},
{26, 25, 33, 25, 26, 25, 33, 25}
},
{
{32, 30, 40, 30, 32, 30, 40, 30},
{30, 28, 38, 28, 30, 28, 38, 28},
{40, 38, 51, 38, 40, 38, 51, 38},
{30, 28, 38, 28, 30, 28, 38, 28},
{32, 30, 40, 30, 32, 30, 40, 30},
{30, 28, 38, 28, 30, 28, 38, 28},
{40, 38, 51, 38, 40, 38, 51, 38},
{30, 28, 38, 28, 30, 28, 38, 28}
},
{
{36, 34, 46, 34, 36, 34, 46, 34},
{34, 32, 43, 32, 34, 32, 43, 32},
{46, 43, 58, 43, 46, 43, 58, 43},
{34, 32, 43, 32, 34, 32, 43, 32},
{36, 34, 46, 34, 36, 34, 46, 34},
{34, 32, 43, 32, 34, 32, 43, 32},
{46, 43, 58, 43, 46, 43, 58, 43},
{34, 32, 43, 32, 34, 32, 43, 32}
}
};
//! single scan pattern
static const byte SNGL_SCAN8x8[64][2] = {
{0,0}, {1,0}, {0,1}, {0,2}, {1,1}, {2,0}, {3,0}, {2,1},
{1,2}, {0,3}, {0,4}, {1,3}, {2,2}, {3,1}, {4,0}, {5,0},
{4,1}, {3,2}, {2,3}, {1,4}, {0,5}, {0,6}, {1,5}, {2,4},
{3,3}, {4,2}, {5,1}, {6,0}, {7,0}, {6,1}, {5,2}, {4,3},
{3,4}, {2,5}, {1,6}, {0,7}, {1,7}, {2,6}, {3,5}, {4,4},
{5,3}, {6,2}, {7,1}, {7,2}, {6,3}, {5,4}, {4,5}, {3,6},
{2,7}, {3,7}, {4,6}, {5,5}, {6,4}, {7,3}, {7,4}, {6,5},
{5,6}, {4,7}, {5,7}, {6,6}, {7,5}, {7,6}, {6,7}, {7,7}
};
//! field scan pattern
static const byte FIELD_SCAN8x8[64][2] = { // 8x8
{0,0}, {0,1}, {0,2}, {1,0}, {1,1}, {0,3}, {0,4}, {1,2},
{2,0}, {1,3}, {0,5}, {0,6}, {0,7}, {1,4}, {2,1}, {3,0},
{2,2}, {1,5}, {1,6}, {1,7}, {2,3}, {3,1}, {4,0}, {3,2},
{2,4}, {2,5}, {2,6}, {2,7}, {3,3}, {4,1}, {5,0}, {4,2},
{3,4}, {3,5}, {3,6}, {3,7}, {4,3}, {5,1}, {6,0}, {5,2},
{4,4}, {4,5}, {4,6}, {4,7}, {5,3}, {6,1}, {6,2}, {5,4},
{5,5}, {5,6}, {5,7}, {6,3}, {7,0}, {7,1}, {6,4}, {6,5},
{6,6}, {6,7}, {7,2}, {7,3}, {7,4}, {7,5}, {7,6}, {7,7}
};
//! array used to find expensive coefficients
static const byte COEFF_COST8x8[2][64] =
{
{3,3,3,3,2,2,2,2,2,2,2,2,1,1,1,1,
1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9}
};
/*!
*************************************************************************************
* \brief
* 8x8 Intra mode decision for a macroblock
*************************************************************************************
*/
int Mode_Decision_for_new_Intra8x8Macroblock (Macroblock *currMB, double lambda, double *min_cost)
{
int cur_cbp = 0, b8;
double cost8x8;
int cr_cbp[3] = { 0, 0, 0};
*min_cost = (int)floor(6.0 * lambda + 0.4999);
cmp_cbp[1] = cmp_cbp[2] = 0;
for (b8=0; b8<4; b8++)
{
if (Mode_Decision_for_new_8x8IntraBlocks (currMB, b8, lambda, &cost8x8, cr_cbp))
{
cur_cbp |= (1<<b8);
}
*min_cost += cost8x8;
if(active_sps->chroma_format_idc==YUV444 && !IS_INDEPENDENT(input))
{
int k;
for (k = 1; k < 3; k++)
{
if (cr_cbp[k])
{
cmp_cbp[k] |= (1<<b8);
cur_cbp |= cmp_cbp[k];
cmp_cbp[k] = cur_cbp;
}
}
}
}
return cur_cbp;
}
/*!
*************************************************************************************
* \brief
* 8x8 Intra mode decision for a macroblock
*************************************************************************************
*/
int Mode_Decision_for_new_8x8IntraBlocks (Macroblock *currMB, int b8, double lambda, double *min_cost, int cr_cbp[3])
{
int ipmode, best_ipmode = 0, i, j, k, y, dummy;
double cost;
int c_nz, nonzero = 0;
static imgpel rec8x8[3][8][8];
double rdcost = 0.0;
int block_x = (b8 & 0x01) << 3;
int block_y = (b8 >> 1) << 3;
int pic_pix_x = img->pix_x + block_x;
int pic_pix_y = img->pix_y + block_y;
int pic_opix_x = img->opix_x + block_x;
int pic_opix_y = img->opix_y + block_y;
int pic_block_x = pic_pix_x >> 2;
int pic_block_y = pic_pix_y >> 2;
double min_rdcost = 1e30;
imgpel *img_org, *img_prd;
int *residual;
extern int ****cofAC8x8;
static int fadjust8x8[2][16][16];
static int fadjust8x8Cr[2][2][16][16];
extern int ****cofAC8x8CbCr[2];
int uv, c_nzCbCr[3];
int left_available, up_available, all_available;
int (*curr_res)[16] = img->m7[0];
imgpel (*curr_mpr)[16] = img->mpr[0];
char upMode;
char leftMode;
int mostProbableMode;
PixelPos left_block;
PixelPos top_block;
//For residual DPCM
Boolean lossless_qpprime = ((currMB->qp + img->bitdepth_luma_qp_scale)==0 && img->lossless_qpprime_flag==1);
getLuma4x4Neighbour(currMB, block_x - 1, block_y, &left_block);
getLuma4x4Neighbour(currMB, block_x, block_y - 1, &top_block);
if (input->UseConstrainedIntraPred)
{
top_block.available = top_block.available ? img->intra_block [top_block.mb_addr] : 0;
left_block.available = left_block.available ? img->intra_block [left_block.mb_addr] : 0;
}
if(b8 >> 1)
upMode = top_block.available ? img->ipredmode8x8[top_block.pos_y ][top_block.pos_x ] : -1;
else
upMode = top_block.available ? img->ipredmode [top_block.pos_y ][top_block.pos_x ] : -1;
if(b8 & 0x01)
leftMode = left_block.available ? img->ipredmode8x8[left_block.pos_y][left_block.pos_x] : -1;
else
leftMode = left_block.available ? img->ipredmode[left_block.pos_y][left_block.pos_x] : -1;
mostProbableMode = (upMode < 0 || leftMode < 0) ? DC_PRED : upMode < leftMode ? upMode : leftMode;
*min_cost = INT_MAX;
ipmode_DPCM = NO_INTRA_PMODE; //For residual DPCM
//===== INTRA PREDICTION FOR 8x8 BLOCK =====
intrapred_8x8 (currMB, PLANE_Y, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
if( (active_sps->chroma_format_idc ==YUV444) && (IS_INDEPENDENT(input)==0) )
{
select_plane(PLANE_U);
intrapred_8x8(currMB, PLANE_U, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
select_plane(PLANE_V);
intrapred_8x8(currMB, PLANE_V, pic_pix_x, pic_pix_y, &left_available, &up_available, &all_available);
select_plane(PLANE_Y);
}
//===== LOOP OVER ALL 8x8 INTRA PREDICTION MODES =====
for (ipmode = 0; ipmode < NO_INTRA_PMODE; ipmode++)
{
if( (ipmode==DC_PRED) ||
((ipmode==VERT_PRED||ipmode==VERT_LEFT_PRED||ipmode==DIAG_DOWN_LEFT_PRED) && up_available ) ||
((ipmode==HOR_PRED||ipmode==HOR_UP_PRED) && left_available ) ||
(all_available) )
{
if (!input->rdopt)
{
for (k=j=0; j<8; j++)
{
for (i=0; i<8; i++, k++)
{
diff64[k] = pCurImg[pic_opix_y+j][pic_opix_x+i] - img->mpr_8x8[0][ipmode][j][i];
}
}
cost = (ipmode == mostProbableMode) ? 0 : (int)floor(4 * lambda );
cost += distortion8x8 (diff64);
if( (active_sps->chroma_format_idc == YUV444) && (IS_INDEPENDENT(input)==0) )
{
int m;
for (m = PLANE_U; m <= PLANE_V; m++)
{
for (k=j=0; j<8; j++)
for (i=0; i<8; i++, k++)
{
diff64[k] = pImgOrg[m][pic_opix_y+j][pic_opix_x+i] - img->mpr_8x8[m][ipmode][j][i];
}
cost += distortion8x8 (diff64);
}
}
if (cost < *min_cost)
{
best_ipmode = ipmode;
*min_cost = cost;
}
}
else
{
// get prediction and prediction error
for (j=block_y; j < block_y + 8; j++)
{
memcpy(&curr_mpr[j][block_x],img->mpr_8x8[0][ipmode][j - block_y], 8 * sizeof(imgpel));
img_org = &pCurImg[img->opix_y+j][pic_opix_x];
img_prd = &curr_mpr[j][block_x];
residual = &curr_res[j][block_x];
for (i=0; i<8; i++)
{
*residual++ = *img_org++ - *img_prd++;
}
}
if( (img->yuv_format == YUV444) && (!IS_INDEPENDENT(input)))
{
for (k = PLANE_U; k <= PLANE_V; k++)
{
for (j=0; j<8; j++)
{
memcpy(&img->mpr[k][block_y+j][block_x],img->mpr_8x8[k][ipmode][j], 8 * sizeof(imgpel));
for (i=0; i<8; i++)
{
img->m7[k][block_y+j][block_x+i] = (int) (pImgOrg[k][pic_opix_y+j][pic_opix_x+i] - img->mpr_8x8[k][ipmode][j][i]);
}
}
}
}
if( (img->yuv_format == YUV444) && (!IS_INDEPENDENT(input)))
{
if((lossless_qpprime)&&(ipmode<2)) //For residual DPCM
{
Residual_DPCM_8x8(ipmode, 0, block_y, block_x);
Residual_DPCM_8x8(ipmode, 1, block_y, block_x);
Residual_DPCM_8x8(ipmode, 2, block_y, block_x);
ipmode_DPCM=ipmode;
}
}
else if ( (img->yuv_format == YUV444) && (IS_INDEPENDENT(input)))
{
if((lossless_qpprime)&&(ipmode<2)) //For residual DPCM
{
Residual_DPCM_8x8(ipmode, 0, block_y, block_x);
ipmode_DPCM=ipmode;
}
}
//===== store the coding state =====
// store_coding_state_cs_cm(currMB);
// get and check rate-distortion cost
if ((rdcost = RDCost_for_8x8IntraBlocks (currMB, &c_nz, b8, ipmode, lambda, min_rdcost, mostProbableMode, c_nzCbCr)) < min_rdcost)
{
//--- set coefficients ---
for(k=0; k<4; k++) // do 4x now
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -