📄 h264.c
字号:
* write 1,10,100,1000,... for alignment, yes its exactly inverse to mpeg4 */static void encode_rbsp_trailing(PutBitContext *pb){ int length; put_bits(pb, 1, 1); length= (-put_bits_count(pb))&7; if(length) put_bits(pb, length, 0);}#endif/** * identifies the exact end of the bitstream * @return the length of the trailing, or 0 if damaged */static int decode_rbsp_trailing(uint8_t *src){ int v= *src; int r; tprintf("rbsp trailing %X\n", v); for(r=1; r<9; r++){ if(v&1) return r; v>>=1; } return 0;}/** * idct tranforms the 16 dc values and dequantize them. * @param qp quantization parameter */static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp){ const int qmul= dequant_coeff[qp][0];#define stride 16 int i; int temp[16]; //FIXME check if this is a good idea static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride}; static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};//memset(block, 64, 2*256);//return; for(i=0; i<4; i++){ const int offset= y_offset[i]; const int z0= block[offset+stride*0] + block[offset+stride*4]; const int z1= block[offset+stride*0] - block[offset+stride*4]; const int z2= block[offset+stride*1] - block[offset+stride*5]; const int z3= block[offset+stride*1] + block[offset+stride*5]; temp[4*i+0]= z0+z3; temp[4*i+1]= z1+z2; temp[4*i+2]= z1-z2; temp[4*i+3]= z0-z3; } for(i=0; i<4; i++){ const int offset= x_offset[i]; const int z0= temp[4*0+i] + temp[4*2+i]; 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)*qmul + 2)>>2; //FIXME think about merging this into decode_resdual block[stride*2 +offset]= ((z1 + z2)*qmul + 2)>>2; block[stride*8 +offset]= ((z1 - z2)*qmul + 2)>>2; block[stride*10+offset]= ((z0 - z3)*qmul + 2)>>2; }}#if 0/** * dct tranforms the 16 dc values. * @param qp quantization parameter ??? FIXME */static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){// const int qmul= dequant_coeff[qp][0]; int i; int temp[16]; //FIXME check if this is a good idea static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride}; static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride}; for(i=0; i<4; i++){ const int offset= y_offset[i]; const int z0= block[offset+stride*0] + block[offset+stride*4]; const int z1= block[offset+stride*0] - block[offset+stride*4]; const int z2= block[offset+stride*1] - block[offset+stride*5]; const int z3= block[offset+stride*1] + block[offset+stride*5]; temp[4*i+0]= z0+z3; temp[4*i+1]= z1+z2; temp[4*i+2]= z1-z2; temp[4*i+3]= z0-z3; } for(i=0; i<4; i++){ const int offset= x_offset[i]; const int z0= temp[4*0+i] + temp[4*2+i]; 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 stridestatic 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 0static 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 inline 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 0static 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 inline 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; }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; ((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; }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; ((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; }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];\
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -