📄 mpeg12.c
字号:
code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)); mpeg1_lum_dc_uni[i+255]= bits + (code<<8); bits= vlc_dc_chroma_bits[index] + index; code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)); mpeg1_chr_dc_uni[i+255]= bits + (code<<8); } mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) ); for(f_code=1; f_code<=MAX_FCODE; f_code++){ for(mv=-MAX_MV; mv<=MAX_MV; mv++){ int len; if(mv==0) len= mbMotionVectorTable[0][1]; else{ int val, bit_size, range, code; bit_size = s->f_code - 1; range = 1 << bit_size; val=mv; if (val < 0) val = -val; val--; code = (val >> bit_size) + 1; if(code<17){ len= mbMotionVectorTable[code][1] + 1 + bit_size; }else{ len= mbMotionVectorTable[16][1] + 2 + bit_size; } } mv_penalty[f_code][mv+MAX_MV]= len; } } for(f_code=MAX_FCODE; f_code>0; f_code--){ for(mv=-(8<<f_code); mv<(8<<f_code); mv++){ fcode_tab[mv+MAX_MV]= f_code; } } } s->me.mv_penalty= mv_penalty; s->fcode_tab= fcode_tab; if(s->codec_id == CODEC_ID_MPEG1VIDEO){ s->min_qcoeff=-255; s->max_qcoeff= 255; }else{ s->min_qcoeff=-2047; s->max_qcoeff= 2047; } s->intra_ac_vlc_length= s->inter_ac_vlc_length= s->intra_ac_vlc_last_length= s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;}static inline void encode_dc(MpegEncContext *s, int diff, int component){ if (component == 0) { put_bits( &s->pb, mpeg1_lum_dc_uni[diff+255]&0xFF, mpeg1_lum_dc_uni[diff+255]>>8); } else { put_bits( &s->pb, mpeg1_chr_dc_uni[diff+255]&0xFF, mpeg1_chr_dc_uni[diff+255]>>8); }}static void mpeg1_encode_block(MpegEncContext *s, DCTELEM *block, int n){ int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign; int code, component;// RLTable *rl = &rl_mpeg1; last_index = s->block_last_index[n]; /* DC coef */ if (s->mb_intra) { component = (n <= 3 ? 0 : n - 4 + 1); dc = block[0]; /* overflow is impossible */ diff = dc - s->last_dc[component]; encode_dc(s, diff, component); s->last_dc[component] = dc; i = 1;/* if (s->intra_vlc_format) rl = &rl_mpeg2; else rl = &rl_mpeg1;*/ } else { /* encode the first coefficient : needs to be done here because it is handled slightly differently */ level = block[0]; if (abs(level) == 1) { code = ((uint32_t)level >> 31); /* the sign bit */ put_bits(&s->pb, 2, code | 0x02); i = 1; } else { i = 0; last_non_zero = -1; goto next_coef; } } /* now quantify & encode AC coefs */ last_non_zero = i - 1; for(;i<=last_index;i++) { j = s->intra_scantable.permutated[i]; level = block[j]; next_coef:#if 0 if (level != 0) dprintf("level[%d]=%d\n", i, level);#endif /* encode using VLC */ if (level != 0) { run = i - last_non_zero - 1; alevel= level; MASK_ABS(sign, alevel) sign&=1;// code = get_rl_index(rl, 0, run, alevel); if (alevel <= mpeg1_max_level[0][run]){ code= mpeg1_index_run[0][run] + alevel - 1; /* store the vlc & sign at once */ put_bits(&s->pb, mpeg1_vlc[code][1]+1, (mpeg1_vlc[code][0]<<1) + sign); } else { /* escape seems to be pretty rare <5% so i dont optimize it */ put_bits(&s->pb, mpeg1_vlc[111/*rl->n*/][1], mpeg1_vlc[111/*rl->n*/][0]); /* escape: only clip in this case */ put_bits(&s->pb, 6, run); if(s->codec_id == CODEC_ID_MPEG1VIDEO){ if (alevel < 128) { put_bits(&s->pb, 8, level & 0xff); } else { if (level < 0) { put_bits(&s->pb, 16, 0x8001 + level + 255); } else { put_bits(&s->pb, 16, level & 0xffff); } } }else{ put_bits(&s->pb, 12, level & 0xfff); } } last_non_zero = i; } } /* end of block */ put_bits(&s->pb, 2, 0x2);}#endif //CONFIG_ENCODERS/******************************************//* decoding */static VLC dc_lum_vlc;static VLC dc_chroma_vlc;static VLC mv_vlc;static VLC mbincr_vlc;static VLC mb_ptype_vlc;static VLC mb_btype_vlc;static VLC mb_pat_vlc;static void init_vlcs(){ static int done = 0; if (!done) { done = 1; init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12, vlc_dc_lum_bits, 1, 1, vlc_dc_lum_code, 2, 2); init_vlc(&dc_chroma_vlc, DC_VLC_BITS, 12, vlc_dc_chroma_bits, 1, 1, vlc_dc_chroma_code, 2, 2); init_vlc(&mv_vlc, MV_VLC_BITS, 17, &mbMotionVectorTable[0][1], 2, 1, &mbMotionVectorTable[0][0], 2, 1); init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 36, &mbAddrIncrTable[0][1], 2, 1, &mbAddrIncrTable[0][0], 2, 1); init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 63, &mbPatTable[0][1], 2, 1, &mbPatTable[0][0], 2, 1); init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7, &table_mb_ptype[0][1], 2, 1, &table_mb_ptype[0][0], 2, 1); init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11, &table_mb_btype[0][1], 2, 1, &table_mb_btype[0][0], 2, 1); init_rl(&rl_mpeg1); init_rl(&rl_mpeg2); init_2d_vlc_rl(&rl_mpeg1); init_2d_vlc_rl(&rl_mpeg2); }}static inline int get_dmv(MpegEncContext *s){ if(get_bits1(&s->gb)) return 1 - (get_bits1(&s->gb) << 1); else return 0;}static inline int get_qscale(MpegEncContext *s){ int qscale = get_bits(&s->gb, 5); if (s->codec_id == CODEC_ID_MPEG2VIDEO) { if (s->q_scale_type) { return non_linear_qscale[qscale]; } else { return qscale << 1; } } return qscale;}/* motion type (for mpeg2) */#define MT_FIELD 1#define MT_FRAME 2#define MT_16X8 2#define MT_DMV 3static int mpeg_decode_mb(MpegEncContext *s, DCTELEM block[6][64]){ int i, j, k, cbp, val, mb_type, motion_type; dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y); assert(s->mb_skiped==0); if (s->mb_skip_run-- != 0) { if(s->pict_type == I_TYPE){ av_log(s->avctx, AV_LOG_ERROR, "skiped MB in I frame at %d %d\n", s->mb_x, s->mb_y); return -1; } /* skip mb */ s->mb_intra = 0; for(i=0;i<6;i++) s->block_last_index[i] = -1; s->mv_type = MV_TYPE_16X16; if (s->pict_type == P_TYPE) { /* if P type, zero motion vector is implied */ s->mv_dir = MV_DIR_FORWARD; s->mv[0][0][0] = s->mv[0][0][1] = 0; s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0; s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0; s->mb_skiped = 1; s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16; } else { /* if B type, reuse previous vectors and directions */ s->mv[0][0][0] = s->last_mv[0][0][0]; s->mv[0][0][1] = s->last_mv[0][0][1]; s->mv[1][0][0] = s->last_mv[1][0][0]; s->mv[1][0][1] = s->last_mv[1][0][1]; s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1] | MB_TYPE_SKIP;// assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8)); if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0) s->mb_skiped = 1; } return 0; } switch(s->pict_type) { default: case I_TYPE: if (get_bits1(&s->gb) == 0) { if (get_bits1(&s->gb) == 0){ av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y); return -1; } mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA; } else { mb_type = MB_TYPE_INTRA; } break; case P_TYPE: mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1); if (mb_type < 0){ av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y); return -1; } mb_type = ptype2mb_type[ mb_type ]; break; case B_TYPE: mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1); if (mb_type < 0){ av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y); return -1; } mb_type = btype2mb_type[ mb_type ]; break; } dprintf("mb_type=%x\n", mb_type);// motion_type = 0; /* avoid warning */ if (IS_INTRA(mb_type)) { /* compute dct type */ if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var? !s->frame_pred_frame_dct) { s->interlaced_dct = get_bits1(&s->gb); } if (IS_QUANT(mb_type)) s->qscale = get_qscale(s); if (s->concealment_motion_vectors) { /* just parse them */ if (s->picture_structure != PICT_FRAME) skip_bits1(&s->gb); /* field select */ s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] = mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]); s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] = mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]); skip_bits1(&s->gb); /* marker */ }else memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */ s->mb_intra = 1;#ifdef HAVE_XVMC //one 1 we memcpy blocks in xvmcvideo if(s->avctx->xvmc_acceleration > 1){ XVMC_pack_pblocks(s,-1);//inter are always full blocks if(s->swap_uv){ exchange_uv(s); } }#endif if (s->codec_id == CODEC_ID_MPEG2VIDEO) { for(i=0;i<6;i++) { if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0) return -1; } } else { for(i=0;i<6;i++) { if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0) return -1; } } } else { if (mb_type & MB_TYPE_ZERO_MV){ assert(mb_type & MB_TYPE_PAT); /* compute dct type */ if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var? !s->frame_pred_frame_dct) { s->interlaced_dct = get_bits1(&s->gb); } if (IS_QUANT(mb_type))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -