📄 h264.c
字号:
if(ABS(mv_col[0]) <= 1 && ABS(mv_col[1]) <= 1){ if(ref[0] == 0) *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0; if(ref[1] == 0) *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0; } } } } } }else{ /* direct temporal mv pred */ if(IS_16X16(*mb_type)){ fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1); if(IS_INTRA(mb_type_col)){ fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1); fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, 0, 4); fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, 0, 4); }else{ const int ref0 = l1ref0[0] >= 0 ? h->map_col_to_list0[0][l1ref0[0]] : h->map_col_to_list0[1][l1ref1[0]]; const int dist_scale_factor = h->dist_scale_factor[ref0]; const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0]; int mv_l0[2]; mv_l0[0] = (dist_scale_factor * mv_col[0] + 128) >> 8; mv_l0[1] = (dist_scale_factor * mv_col[1] + 128) >> 8; fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref0, 1); fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0],mv_l0[1]), 4); fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]), 4); } }else{ for(i8=0; i8<4; i8++){ const int x8 = i8&1; const int y8 = i8>>1; int ref0, dist_scale_factor; const int16_t (*l1mv)[2]= l1mv0; if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8])) continue; h->sub_mb_type[i8] = sub_mb_type; if(IS_INTRA(mb_type_col)){ fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1); fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1); fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4); fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4); continue; } ref0 = l1ref0[x8 + y8*h->b8_stride]; if(ref0 >= 0) ref0 = h->map_col_to_list0[0][ref0]; else{ ref0 = h->map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]]; l1mv= l1mv1; } dist_scale_factor = h->dist_scale_factor[ref0]; fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1); fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1); if(IS_SUB_8X8(sub_mb_type)){ const int16_t *mv_col = l1mv[x8*3 + y8*3*h->b_stride]; int mx = (dist_scale_factor * mv_col[0] + 128) >> 8; int my = (dist_scale_factor * mv_col[1] + 128) >> 8; fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4); fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4); }else for(i4=0; i4<4; i4++){ const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride]; int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]]; mv_l0[0] = (dist_scale_factor * mv_col[0] + 128) >> 8; mv_l0[1] = (dist_scale_factor * mv_col[1] + 128) >> 8; *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]); } } } }}static inline void write_back_motion(H264Context *h, int mb_type){ MpegEncContext * const s = &h->s; const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride; const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride; int list; if(!USES_LIST(mb_type, 0)) fill_rectangle(&s->current_picture.ref_index[0][b8_xy], 2, 2, h->b8_stride, (uint8_t)LIST_NOT_USED, 1); for(list=0; list<2; list++){ int y; if(!USES_LIST(mb_type, list)) continue; for(y=0; y<4; y++){ *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+0 + 8*y]; *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+2 + 8*y]; } if( h->pps.cabac ) { for(y=0; y<4; y++){ *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y]; *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y]; } } { uint8_t *ref_index = &s->current_picture.ref_index[list][b8_xy]; ref_index[0+0*h->b8_stride]= h->ref_cache[list][scan8[0]]; ref_index[1+0*h->b8_stride]= h->ref_cache[list][scan8[4]]; ref_index[0+1*h->b8_stride]= h->ref_cache[list][scan8[8]]; ref_index[1+1*h->b8_stride]= h->ref_cache[list][scan8[12]]; } } if(h->slice_type == B_TYPE && h->pps.cabac){ if(IS_8X8(mb_type)){ uint8_t *direct_table = &h->direct_table[b8_xy]; direct_table[1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0; direct_table[0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0; direct_table[1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0; } }}/** * Decodes a network abstraction layer unit. * @param consumed is the number of bytes used as input * @param length is the length of the array * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp tailing? * @returns decoded bytes, might be src+1 if no escapes */static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){ int i, si, di; uint8_t *dst;// src[0]&0x80; //forbidden bit h->nal_ref_idc= src[0]>>5; h->nal_unit_type= src[0]&0x1F; src++; length--;#if 0 for(i=0; i<length; i++) printf("%2X ", src[i]);#endif for(i=0; i+1<length; i+=2){ if(src[i]) continue; if(i>0 && src[i-1]==0) i--; if(i+2<length && src[i+1]==0 && src[i+2]<=3){ if(src[i+2]!=3){ /* startcode, so we must be past the end */ length=i; } break; } } if(i>=length-1){ //no escaped 0 *dst_length= length; *consumed= length+1; //+1 for the header return src; } h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length); dst= h->rbsp_buffer;//printf("decoding esc\n"); si=di=0; while(si<length){ //remove escapes (very rare 1:2^22) if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){ if(src[si+2]==3){ //escape dst[di++]= 0; dst[di++]= 0; si+=3; continue; }else //next start code break; } dst[di++]= src[si++]; } *dst_length= di; *consumed= si + 1;//+1 for the header//FIXME store exact number of bits in the getbitcontext (its needed for decoding) return dst;}#if 0/** * @param src the data which should be escaped * @param dst the target buffer, dst+1 == src is allowed as a special case * @param length the length of the src data * @param dst_length the length of the dst array * @returns length of escaped data in bytes or -1 if an error occured */static int encode_nal(H264Context *h, uint8_t *dst, uint8_t *src, int length, int dst_length){ int i, escape_count, si, di; uint8_t *temp; assert(length>=0); assert(dst_length>0); dst[0]= (h->nal_ref_idc<<5) + h->nal_unit_type; if(length==0) return 1; escape_count= 0; for(i=0; i<length; i+=2){ if(src[i]) continue; if(i>0 && src[i-1]==0) i--; if(i+2<length && src[i+1]==0 && src[i+2]<=3){ escape_count++; i+=2; } } if(escape_count==0){ if(dst+1 != src) memcpy(dst+1, src, length); return length + 1; } if(length + escape_count + 1> dst_length) return -1; //this should be damn rare (hopefully) h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length + escape_count); temp= h->rbsp_buffer;//printf("encoding esc\n"); si= 0; di= 0; while(si < length){ if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){ temp[di++]= 0; si++; temp[di++]= 0; si++; temp[di++]= 3; temp[di++]= src[si++]; } else temp[di++]= src[si++]; } memcpy(dst+1, temp, length+escape_count); assert(di == length+escape_count); return di + 1;}/** * 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, int qmul){#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 + 128 ) >> 8)); //FIXME think about merging this into decode_resdual block[stride*2 +offset]= ((((z1 + z2)*qmul + 128 ) >> 8)); block[stride*8 +offset]= ((((z1 - z2)*qmul + 128 ) >> 8)); block[stride*10+offset]= ((((z0 - z3)*qmul + 128 ) >> 8)); }}#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[of
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -