diff options
Diffstat (limited to 'src/libffmpeg/libavcodec/mpeg12.c')
-rw-r--r-- | src/libffmpeg/libavcodec/mpeg12.c | 142 |
1 files changed, 92 insertions, 50 deletions
diff --git a/src/libffmpeg/libavcodec/mpeg12.c b/src/libffmpeg/libavcodec/mpeg12.c index 9b917ce27..fab6d1aef 100644 --- a/src/libffmpeg/libavcodec/mpeg12.c +++ b/src/libffmpeg/libavcodec/mpeg12.c @@ -33,6 +33,14 @@ #define EXT_START_CODE 0x000001b5 #define USER_START_CODE 0x000001b2 +#define DC_VLC_BITS 9 +#define MV_VLC_BITS 9 +#define MBINCR_VLC_BITS 9 +#define MB_PAT_VLC_BITS 9 +#define MB_PTYPE_VLC_BITS 6 +#define MB_BTYPE_VLC_BITS 6 +#define TEX_VLC_BITS 9 + static void mpeg1_encode_block(MpegEncContext *s, DCTELEM *block, int component); @@ -52,6 +60,46 @@ static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred); static UINT16 mv_penalty[MAX_FCODE+1][MAX_MV*2+1]; static UINT8 fcode_tab[MAX_MV*2+1]; +static void init_2d_vlc_rl(RLTable *rl) +{ + int i, q; + + init_vlc(&rl->vlc, TEX_VLC_BITS, rl->n + 2, + &rl->table_vlc[0][1], 4, 2, + &rl->table_vlc[0][0], 4, 2); + + + rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM)); + for(i=0; i<rl->vlc.table_size; i++){ + int code= rl->vlc.table[i][0]; + int len = rl->vlc.table[i][1]; + int level, run; + + if(len==0){ // illegal code + run= 65; + level= MAX_LEVEL; + }else if(len<0){ //more bits needed + run= 0; + level= code; + }else{ + if(code==rl->n){ //esc + run= 65; + level= 0; + }else if(code==rl->n+1){ //eob + run= 192; + level= 1; + }else{ + run= rl->table_run [code] + 1; + level= rl->table_level[code]; + } + } + rl->rl_vlc[0][i].len= len; + rl->rl_vlc[0][i].level= level; + rl->rl_vlc[0][i].run= run; + } +} + + static void put_header(MpegEncContext *s, int header) { align_put_bits(&s->pb); @@ -540,37 +588,33 @@ void mpeg1_init_vlc(MpegEncContext *s) if (!done) { done = 1; - init_vlc(&dc_lum_vlc, 9, 12, + init_vlc(&dc_lum_vlc, DC_VLC_BITS, 10/*12*/, vlc_dc_lum_bits, 1, 1, vlc_dc_lum_code, 2, 2); - init_vlc(&dc_chroma_vlc, 9, 12, + init_vlc(&dc_chroma_vlc, DC_VLC_BITS, 10/*12*/, vlc_dc_chroma_bits, 1, 1, vlc_dc_chroma_code, 2, 2); - init_vlc(&mv_vlc, 9, 17, + init_vlc(&mv_vlc, MV_VLC_BITS, 17, &mbMotionVectorTable[0][1], 2, 1, &mbMotionVectorTable[0][0], 2, 1); - init_vlc(&mbincr_vlc, 9, 35, + init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 35, &mbAddrIncrTable[0][1], 2, 1, &mbAddrIncrTable[0][0], 2, 1); - init_vlc(&mb_pat_vlc, 9, 63, + 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, 6, 32, + init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 32, &table_mb_ptype[0][1], 2, 1, &table_mb_ptype[0][0], 2, 1); - init_vlc(&mb_btype_vlc, 6, 32, + init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 32, &table_mb_btype[0][1], 2, 1, &table_mb_btype[0][0], 2, 1); init_rl(&rl_mpeg1); init_rl(&rl_mpeg2); - /* cannot use generic init because we must add the EOB code */ - init_vlc(&rl_mpeg1.vlc, 9, rl_mpeg1.n + 2, - &rl_mpeg1.table_vlc[0][1], 4, 2, - &rl_mpeg1.table_vlc[0][0], 4, 2); - init_vlc(&rl_mpeg2.vlc, 9, rl_mpeg2.n + 2, - &rl_mpeg2.table_vlc[0][1], 4, 2, - &rl_mpeg2.table_vlc[0][0], 4, 2); + + init_2d_vlc_rl(&rl_mpeg1); + init_2d_vlc_rl(&rl_mpeg2); } } @@ -614,7 +658,7 @@ static int mpeg_decode_mb(MpegEncContext *s, /* read again increment */ s->mb_incr = 1; for(;;) { - code = get_vlc(&s->gb, &mbincr_vlc); + code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2); if (code < 0) return 1; /* error = end of slice */ if (code >= 33) { @@ -671,12 +715,12 @@ static int mpeg_decode_mb(MpegEncContext *s, } break; case P_TYPE: - mb_type = get_vlc(&s->gb, &mb_ptype_vlc); + mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1); if (mb_type < 0) return -1; break; case B_TYPE: - mb_type = get_vlc(&s->gb, &mb_btype_vlc); + mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1); if (mb_type < 0) return -1; break; @@ -846,7 +890,7 @@ static int mpeg_decode_mb(MpegEncContext *s, } if (mb_type & MB_PAT) { - cbp = get_vlc(&s->gb, &mb_pat_vlc); + cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1); if (cbp < 0) return -1; cbp++; @@ -891,7 +935,7 @@ static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred) { int code, sign, val, m, l, shift; - code = get_vlc(&s->gb, &mv_vlc); + code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2); if (code < 0) { return 0xffff; } @@ -924,9 +968,9 @@ static inline int decode_dc(MpegEncContext *s, int component) int code, diff; if (component == 0) { - code = get_vlc(&s->gb, &dc_lum_vlc); + code = get_vlc2(&s->gb, dc_lum_vlc.table, DC_VLC_BITS, 1); } else { - code = get_vlc(&s->gb, &dc_chroma_vlc); + code = get_vlc2(&s->gb, dc_chroma_vlc.table, DC_VLC_BITS, 1); } if (code < 0) return 0xffff; @@ -961,26 +1005,25 @@ static int mpeg1_decode_block(MpegEncContext *s, dprintf("dc=%d diff=%d\n", dc, diff); i = 1; } else { - int bit_cnt, v; - UINT32 bit_buf; - UINT8 *buf_ptr; + int v; + OPEN_READER(re, &s->gb); i = 0; /* special case for the first coef. no need to add a second vlc table */ - SAVE_BITS(&s->gb); - SHOW_BITS(&s->gb, v, 2); + UPDATE_CACHE(re, &s->gb); + v= SHOW_UBITS(re, &s->gb, 2); if (v & 2) { run = 0; level = 1 - ((v & 1) << 1); - FLUSH_BITS(2); - RESTORE_BITS(&s->gb); + SKIP_BITS(re, &s->gb, 2); + CLOSE_READER(re, &s->gb); goto add_coef; } - RESTORE_BITS(&s->gb); + CLOSE_READER(re, &s->gb); } /* now quantify & encode AC coefs */ for(;;) { - code = get_vlc(&s->gb, &rl->vlc); + code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2); if (code < 0) { return -1; } @@ -1035,31 +1078,30 @@ static int mpeg2_decode_block_non_intra(MpegEncContext *s, mismatch = 1; { - int bit_cnt, v; - UINT32 bit_buf; - UINT8 *buf_ptr; + int v; + OPEN_READER(re, &s->gb); i = 0; - if (n < 4) + if (n < 4) matrix = s->inter_matrix; else matrix = s->chroma_inter_matrix; - + /* special case for the first coef. no need to add a second vlc table */ - SAVE_BITS(&s->gb); - SHOW_BITS(&s->gb, v, 2); + UPDATE_CACHE(re, &s->gb); + v= SHOW_UBITS(re, &s->gb, 2); if (v & 2) { run = 0; level = 1 - ((v & 1) << 1); - FLUSH_BITS(2); - RESTORE_BITS(&s->gb); + SKIP_BITS(re, &s->gb, 2); + CLOSE_READER(re, &s->gb); goto add_coef; } - RESTORE_BITS(&s->gb); + CLOSE_READER(re, &s->gb); } /* now quantify & encode AC coefs */ for(;;) { - code = get_vlc(&s->gb, &rl->vlc); + code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2); if (code < 0) return -1; if (code == 112) { @@ -1138,7 +1180,7 @@ static int mpeg2_decode_block_intra(MpegEncContext *s, /* now quantify & encode AC coefs */ for(;;) { - code = get_vlc(&s->gb, &rl->vlc); + code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2); if (code < 0) return -1; if (code == 112) { @@ -1410,7 +1452,7 @@ static int mpeg_decode_slice(AVCodecContext *avctx, /* start frame decoding */ if (s->first_slice) { s->first_slice = 0; - MPV_frame_start(s); + MPV_frame_start(s, avctx); } init_get_bits(&s->gb, buf, buf_size); @@ -1463,8 +1505,8 @@ static int mpeg_decode_slice(AVCodecContext *avctx, pict->data[1] = picture[1]; pict->data[2] = picture[2]; pict->linesize[0] = s->linesize; - pict->linesize[1] = s->linesize / 2; - pict->linesize[2] = s->linesize / 2; + pict->linesize[1] = s->uvlinesize; + pict->linesize[2] = s->uvlinesize; return 1; } else { return 0; @@ -1504,7 +1546,7 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx, } s->width = width; s->height = height; - s->has_b_frames = 1; + avctx->has_b_frames= s->has_b_frames = 1; s->avctx = avctx; avctx->width = width; avctx->height = height; @@ -1542,7 +1584,7 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx, #endif } else { for(i=0;i<64;i++) { - v = default_intra_matrix[i]; + v = ff_mpeg1_default_intra_matrix[i]; s->intra_matrix[i] = v; s->chroma_intra_matrix[i] = v; } @@ -1562,7 +1604,7 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx, #endif } else { for(i=0;i<64;i++) { - v = default_non_intra_matrix[i]; + v = ff_mpeg1_default_non_intra_matrix[i]; s->inter_matrix[i] = v; s->chroma_inter_matrix[i] = v; } @@ -1600,8 +1642,8 @@ static int mpeg_decode_frame(AVCodecContext *avctx, picture->data[1] = s2->next_picture[1]; picture->data[2] = s2->next_picture[2]; picture->linesize[0] = s2->linesize; - picture->linesize[1] = s2->linesize / 2; - picture->linesize[2] = s2->linesize / 2; + picture->linesize[1] = s2->uvlinesize; + picture->linesize[2] = s2->uvlinesize; *data_size = sizeof(AVPicture); } return 0; |