📄 h264.c
字号:
const int z1= temp[4*0+i] - temp[4*2+i];
const int z2= temp[4*1+i] - temp[4*3+i];
const int z3= temp[4*1+i] + temp[4*3+i];
block[stride*0 +offset]= (z0 + z3)>>1;
block[stride*2 +offset]= (z1 + z2)>>1;
block[stride*8 +offset]= (z1 - z2)>>1;
block[stride*10+offset]= (z0 - z3)>>1;
}
}
#endif
#undef xStride
#undef stride
static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp){
const int qmul= dequant_coeff[qp][0];
const int stride= 16*2;
const int xStride= 16;
int a,b,c,d,e;
a= block[stride*0 + xStride*0];
b= block[stride*0 + xStride*1];
c= block[stride*1 + xStride*0];
d= block[stride*1 + xStride*1];
e= a-b;
a= a+b;
b= c-d;
c= c+d;
block[stride*0 + xStride*0]= ((a+c)*qmul + 0)>>1;
block[stride*0 + xStride*1]= ((e+b)*qmul + 0)>>1;
block[stride*1 + xStride*0]= ((a-c)*qmul + 0)>>1;
block[stride*1 + xStride*1]= ((e-b)*qmul + 0)>>1;
}
#if 0
static void chroma_dc_dct_c(DCTELEM *block){
const int stride= 16*2;
const int xStride= 16;
int a,b,c,d,e;
a= block[stride*0 + xStride*0];
b= block[stride*0 + xStride*1];
c= block[stride*1 + xStride*0];
d= block[stride*1 + xStride*1];
e= a-b;
a= a+b;
b= c-d;
c= c+d;
block[stride*0 + xStride*0]= (a+c);
block[stride*0 + xStride*1]= (e+b);
block[stride*1 + xStride*0]= (a-c);
block[stride*1 + xStride*1]= (e-b);
}
#endif
/**
* gets the chroma qp.
*/
static int get_chroma_qp(H264Context *h, int qscale){
return chroma_qp[clip(qscale + h->pps.chroma_qp_index_offset, 0, 51)];
}
/**
*
*/
static void h264_add_idct_c(uint8_t *dst, DCTELEM *block, int stride){
int i;
uint8_t *cm = cropTbl + MAX_NEG_CROP;
block[0] += 32;
for(i=0; i<4; i++){
const int z0= block[0 + 4*i] + block[2 + 4*i];
const int z1= block[0 + 4*i] - block[2 + 4*i];
const int z2= (block[1 + 4*i]>>1) - block[3 + 4*i];
const int z3= block[1 + 4*i] + (block[3 + 4*i]>>1);
block[0 + 4*i]= z0 + z3;
block[1 + 4*i]= z1 + z2;
block[2 + 4*i]= z1 - z2;
block[3 + 4*i]= z0 - z3;
}
for(i=0; i<4; i++){
const int z0= block[i + 4*0] + block[i + 4*2];
const int z1= block[i + 4*0] - block[i + 4*2];
const int z2= (block[i + 4*1]>>1) - block[i + 4*3];
const int z3= block[i + 4*1] + (block[i + 4*3]>>1);
dst[i + 0*stride]= cm[ dst[i + 0*stride] + ((z0 + z3) >> 6) ];
dst[i + 1*stride]= cm[ dst[i + 1*stride] + ((z1 + z2) >> 6) ];
dst[i + 2*stride]= cm[ dst[i + 2*stride] + ((z1 - z2) >> 6) ];
dst[i + 3*stride]= cm[ dst[i + 3*stride] + ((z0 - z3) >> 6) ];
}
}
#if 0
static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){
int i;
//FIXME try int temp instead of block
for(i=0; i<4; i++){
const int d0= src1[0 + i*stride] - src2[0 + i*stride];
const int d1= src1[1 + i*stride] - src2[1 + i*stride];
const int d2= src1[2 + i*stride] - src2[2 + i*stride];
const int d3= src1[3 + i*stride] - src2[3 + i*stride];
const int z0= d0 + d3;
const int z3= d0 - d3;
const int z1= d1 + d2;
const int z2= d1 - d2;
block[0 + 4*i]= z0 + z1;
block[1 + 4*i]= 2*z3 + z2;
block[2 + 4*i]= z0 - z1;
block[3 + 4*i]= z3 - 2*z2;
}
for(i=0; i<4; i++){
const int z0= block[0*4 + i] + block[3*4 + i];
const int z3= block[0*4 + i] - block[3*4 + i];
const int z1= block[1*4 + i] + block[2*4 + i];
const int z2= block[1*4 + i] - block[2*4 + i];
block[0*4 + i]= z0 + z1;
block[1*4 + i]= 2*z3 + z2;
block[2*4 + i]= z0 - z1;
block[3*4 + i]= z3 - 2*z2;
}
}
#endif
//FIXME need to check that this doesnt overflow signed 32 bit for low qp, iam not sure, its very close
//FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
static int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
int i;
const int * const quant_table= quant_coeff[qscale];
const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
const unsigned int threshold2= (threshold1<<1);
int last_non_zero;
if(seperate_dc){
if(qscale<=18){
//avoid overflows
const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
const unsigned int dc_threshold2= (dc_threshold1<<1);
int level= block[0]*quant_coeff[qscale+18][0];
if(((unsigned)(level+dc_threshold1))>dc_threshold2){
if(level>0){
level= (dc_bias + level)>>(QUANT_SHIFT-2);
block[0]= level;
}else{
level= (dc_bias - level)>>(QUANT_SHIFT-2);
block[0]= -level;
}
// last_non_zero = i;
}else{
block[0]=0;
}
}else{
const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
const unsigned int dc_threshold2= (dc_threshold1<<1);
int level= block[0]*quant_table[0];
if(((unsigned)(level+dc_threshold1))>dc_threshold2){
if(level>0){
level= (dc_bias + level)>>(QUANT_SHIFT+1);
block[0]= level;
}else{
level= (dc_bias - level)>>(QUANT_SHIFT+1);
block[0]= -level;
}
// last_non_zero = i;
}else{
block[0]=0;
}
}
last_non_zero= 0;
i=1;
}else{
last_non_zero= -1;
i=0;
}
for(; i<16; i++){
const int j= scantable[i];
int level= block[j]*quant_table[j];
// if( bias+level >= (1<<(QMAT_SHIFT - 3))
// || bias-level >= (1<<(QMAT_SHIFT - 3))){
if(((unsigned)(level+threshold1))>threshold2){
if(level>0){
level= (bias + level)>>QUANT_SHIFT;
block[j]= level;
}else{
level= (bias - level)>>QUANT_SHIFT;
block[j]= -level;
}
last_non_zero = i;
}else{
block[j]=0;
}
}
return last_non_zero;
}
static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
const uint32_t a= ((uint32_t*)(src-stride))[0];
((uint32_t*)(src+0*stride))[0]= a;
((uint32_t*)(src+1*stride))[0]= a;
((uint32_t*)(src+2*stride))[0]= a;
((uint32_t*)(src+3*stride))[0]= a;
}
static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
}
static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride)
{
const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
+ src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
/*((uint32_t*)(src+0*stride))[0]=
((uint32_t*)(src+1*stride))[0]=
((uint32_t*)(src+2*stride))[0]=
((uint32_t*)(src+3*stride))[0]= dc* 0x01010101; */
int dc1=dc*0x01010101;
memcpy(src+0*stride,&dc1,sizeof(uint32_t));
memcpy(src+1*stride,&dc1,sizeof(uint32_t));
memcpy(src+2*stride,&dc1,sizeof(uint32_t));
memcpy(src+3*stride,&dc1,sizeof(uint32_t));
}
static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride)
{
const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
int dc1=dc*0x01010101;
/*((uint32_t*)(src+0*stride))[0]=
((uint32_t*)(src+1*stride))[0]=
((uint32_t*)(src+2*stride))[0]=
((uint32_t*)(src+3*stride))[0]= dc* 0x01010101; */
memcpy(src+0*stride,&dc1,sizeof(uint32_t));
memcpy(src+1*stride,&dc1,sizeof(uint32_t));
memcpy(src+2*stride,&dc1,sizeof(uint32_t));
memcpy(src+3*stride,&dc1,sizeof(uint32_t));
}
static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
int dc1=dc*0x01010101;
/*((uint32_t*)(src+0*stride))[0]=
((uint32_t*)(src+1*stride))[0]=
((uint32_t*)(src+2*stride))[0]=
((uint32_t*)(src+3*stride))[0]= dc* 0x01010101; */
memcpy(src+0*stride,&dc1,sizeof(uint32_t));
memcpy(src+1*stride,&dc1,sizeof(uint32_t));
memcpy(src+2*stride,&dc1,sizeof(uint32_t));
memcpy(src+3*stride,&dc1,sizeof(uint32_t));
}
static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
((uint32_t*)(src+0*stride))[0]=
((uint32_t*)(src+1*stride))[0]=
((uint32_t*)(src+2*stride))[0]=
((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
}
#define LOAD_TOP_RIGHT_EDGE\
const int t4= topright[0];\
const int t5= topright[1];\
const int t6= topright[2];\
const int t7= topright[3];\
#define LOAD_LEFT_EDGE\
const int l0= src[-1+0*stride];\
const int l1= src[-1+1*stride];\
const int l2= src[-1+2*stride];\
const int l3= src[-1+3*stride];\
#define LOAD_TOP_EDGE\
const int t0= src[ 0-1*stride];\
const int t1= src[ 1-1*stride];\
const int t2= src[ 2-1*stride];\
const int t3= src[ 3-1*stride];\
static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
const int lt= src[-1-1*stride];
LOAD_TOP_EDGE
LOAD_LEFT_EDGE
src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
src[0+2*stride]=
src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
src[0+1*stride]=
src[1+2*stride]=
src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
src[0+0*stride]=
src[1+1*stride]=
src[2+2*stride]=
src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
src[1+0*stride]=
src[2+1*stride]=
src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
src[2+0*stride]=
src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
}
static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
LOAD_TOP_EDGE
LOAD_TOP_RIGHT_EDGE
// LOAD_LEFT_EDGE
src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
src[1+0*stride]=
src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
src[2+0*stride]=
src[1+1*stride]=
src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
src[3+0*stride]=
src[2+1*stride]=
src[1+2*stride]=
src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
src[3+1*stride]=
src[2+2*stride]=
src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
src[3+2*stride]=
src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
}
static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
const int lt= src[-1-1*stride];
LOAD_TOP_EDGE
LOAD_LEFT_EDGE
//const __attribute__((unused)) int unu= l3;
src[0+0*stride]=
src[1+2*stride]=(lt + t0 + 1)>>1;
src[1+0*stride]=
src[2+2*stride]=(t0 + t1 + 1)>>1;
src[2+0*stride]=
src[3+2*stride]=(t1 + t2 + 1)>>1;
src[3+0*stride]=(t2 + t3 + 1)>>1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -