📄 mpeg12.c
字号:
run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6); UPDATE_CACHE(re, &s->gb); level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8); if (level == -128) { level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8); } else if (level == 0) { level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8); } i += run; j = scantable[i]; if(level<0){ level= -level; level= (level*qscale*quant_matrix[j])>>4; level= (level-1)|1; level= -level; }else{ level= (level*qscale*quant_matrix[j])>>4; level= (level-1)|1; } } if (i > 63){ av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); return -1; } block[j] = level; } CLOSE_READER(re, &s->gb); } s->block_last_index[n] = i; return 0;}static inline int mpeg1_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n){ int level, i, j, run; RLTable *rl = &rl_mpeg1; uint8_t * const scantable= s->intra_scantable.permutated; const uint16_t *quant_matrix= s->inter_matrix; const int qscale= s->qscale; { int v; OPEN_READER(re, &s->gb); i = -1; /* special case for the first coef. no need to add a second vlc table */ UPDATE_CACHE(re, &s->gb); v= SHOW_UBITS(re, &s->gb, 2); if (v & 2) { LAST_SKIP_BITS(re, &s->gb, 2); level= (3*qscale*quant_matrix[0])>>5; level= (level-1)|1; if(v&1) level= -level; block[0] = level; i++; } /* now quantify & encode AC coefs */ for(;;) { UPDATE_CACHE(re, &s->gb); GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2); if(level == 127){ break; } else if(level != 0) { i += run; j = scantable[i]; level= ((level*2+1)*qscale*quant_matrix[j])>>5; level= (level-1)|1; level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); LAST_SKIP_BITS(re, &s->gb, 1); } else { /* escape */ run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6); UPDATE_CACHE(re, &s->gb); level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8); if (level == -128) { level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8); } else if (level == 0) { level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8); } i += run; j = scantable[i]; if(level<0){ level= -level; level= ((level*2+1)*qscale*quant_matrix[j])>>5; level= (level-1)|1; level= -level; }else{ level= ((level*2+1)*qscale*quant_matrix[j])>>5; level= (level-1)|1; } } if (i > 63){ av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); return -1; } block[j] = level; } CLOSE_READER(re, &s->gb); } s->block_last_index[n] = i; return 0;}/* Also does unquantization here, since I will never support mpeg2 encoding */static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n){ int level, i, j, run; RLTable *rl = &rl_mpeg1; uint8_t * const scantable= s->intra_scantable.permutated; const uint16_t *quant_matrix; const int qscale= s->qscale; int mismatch; mismatch = 1; { int v; OPEN_READER(re, &s->gb); i = -1; if (n < 4) quant_matrix = s->inter_matrix; else quant_matrix = s->chroma_inter_matrix; /* special case for the first coef. no need to add a second vlc table */ UPDATE_CACHE(re, &s->gb); v= SHOW_UBITS(re, &s->gb, 2); if (v & 2) { LAST_SKIP_BITS(re, &s->gb, 2); level= (3*qscale*quant_matrix[0])>>5; if(v&1) level= -level; block[0] = level; mismatch ^= level; i++; } /* now quantify & encode AC coefs */ for(;;) { UPDATE_CACHE(re, &s->gb); GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2); if(level == 127){ break; } else if(level != 0) { i += run; j = scantable[i]; level= ((level*2+1)*qscale*quant_matrix[j])>>5; level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); LAST_SKIP_BITS(re, &s->gb, 1); } else { /* escape */ run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6); UPDATE_CACHE(re, &s->gb); level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12); i += run; j = scantable[i]; if(level<0){ level= ((-level*2+1)*qscale*quant_matrix[j])>>5; level= -level; }else{ level= ((level*2+1)*qscale*quant_matrix[j])>>5; } } if (i > 63){ av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); return -1; } mismatch ^= level; block[j] = level; } CLOSE_READER(re, &s->gb); } block[63] ^= (mismatch & 1); s->block_last_index[n] = i; return 0;}static inline int mpeg2_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n){ int level, dc, diff, i, j, run; int component; RLTable *rl; uint8_t * const scantable= s->intra_scantable.permutated; const uint16_t *quant_matrix; const int qscale= s->qscale; int mismatch; /* DC coef */ if (n < 4){ quant_matrix = s->intra_matrix; component = 0; }else{ quant_matrix = s->chroma_intra_matrix; component = (n&1) + 1; } diff = decode_dc(&s->gb, component); if (diff >= 0xffff) return -1; dc = s->last_dc[component]; dc += diff; s->last_dc[component] = dc; block[0] = dc << (3 - s->intra_dc_precision); dprintf("dc=%d\n", block[0]); mismatch = block[0] ^ 1; i = 0; if (s->intra_vlc_format) rl = &rl_mpeg2; else rl = &rl_mpeg1; { OPEN_READER(re, &s->gb); /* now quantify & encode AC coefs */ for(;;) { UPDATE_CACHE(re, &s->gb); GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2); if(level == 127){ break; } else if(level != 0) { i += run; j = scantable[i]; level= (level*qscale*quant_matrix[j])>>4; level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1); LAST_SKIP_BITS(re, &s->gb, 1); } else { /* escape */ run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6); UPDATE_CACHE(re, &s->gb); level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12); i += run; j = scantable[i]; if(level<0){ level= (-level*qscale*quant_matrix[j])>>4; level= -level; }else{ level= (level*qscale*quant_matrix[j])>>4; } } if (i > 63){ av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y); return -1; } mismatch^= level; block[j] = level; } CLOSE_READER(re, &s->gb); } block[63]^= mismatch&1; s->block_last_index[n] = i; return 0;}typedef struct Mpeg1Context { MpegEncContext mpeg_enc_ctx; int mpeg_enc_ctx_allocated; /* true if decoding context allocated */ int repeat_field; /* true if we must repeat the field */ AVPanScan pan_scan; /** some temporary storage for the panscan */ int slice_count; int swap_uv;//indicate VCR2 int save_aspect_info;} Mpeg1Context;static int mpeg_decode_init(AVCodecContext *avctx){ Mpeg1Context *s = avctx->priv_data; MpegEncContext *s2 = &s->mpeg_enc_ctx; MPV_decode_defaults(s2); s->mpeg_enc_ctx.avctx= avctx; s->mpeg_enc_ctx.flags= avctx->flags; s->mpeg_enc_ctx.flags2= avctx->flags2; common_init(&s->mpeg_enc_ctx); init_vlcs(); s->mpeg_enc_ctx_allocated = 0; s->mpeg_enc_ctx.picture_number = 0; s->repeat_field = 0; s->mpeg_enc_ctx.codec_id= avctx->codec->id; return 0;}static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm, const uint8_t *new_perm){uint16_t temp_matrix[64];int i; memcpy(temp_matrix,matrix,64*sizeof(uint16_t)); for(i=0;i<64;i++){ matrix[new_perm[i]] = temp_matrix[old_perm[i]]; } }//Call this function when we know all parameters//it may be called in different places for mpeg1 and mpeg2static int mpeg_decode_postinit(AVCodecContext *avctx){Mpeg1Context *s1 = avctx->priv_data;MpegEncContext *s = &s1->mpeg_enc_ctx;uint8_t old_permutation[64]; if ( (s1->mpeg_enc_ctx_allocated == 0)|| avctx->width != s->width || avctx->height != s->height||// s1->save_aspect_info != avctx->aspect_ratio_info|| 0) { if (s1->mpeg_enc_ctx_allocated) { MPV_common_end(s); } if( (s->width == 0 )||(s->height == 0)) return -2; avctx->width = s->width; avctx->height = s->height; avctx->bit_rate = s->bit_rate; s1->save_aspect_info = s->aspect_ratio_info; //low_delay may be forced, in this case we will have B frames //that behave like P frames avctx->has_b_frames = !(s->low_delay); if(avctx->sub_id==1){//s->codec_id==avctx->codec_id==CODEC_ID //mpeg1 fps avctx->frame_rate = frame_rate_tab[s->frame_rate_index].num; avctx->frame_rate_base= frame_rate_tab[s->frame_rate_index].den; //mpeg1 aspect avctx->sample_aspect_ratio= av_d2q( 1.0/mpeg1_aspect[s->aspect_ratio_info], 255); }else{//mpeg2 //mpeg2 fps av_reduce( &s->avctx->frame_rate, &s->avctx->frame_rate_base, frame_rate_tab[s->frame_rate_index].num * (s->frame_rate_ext_n+1), frame_rate_tab[s->frame_rate_index].den * (s->frame_rate_ext_d+1), 1<<30); //mpeg2 aspect if(s->aspect_ratio_info > 1){
#ifdef WINCE
AVRational rational;
if( (s1->pan_scan.width == 0 )||(s1->pan_scan.height == 0) )
{
rational.num = s->width;
rational.den = s->height;
s->avctx->sample_aspect_ratio=
av_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -