📄 mpeg12.c
字号:
}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); if (s->intra_vlc_format) rl = &ff_rl_mpeg2; else rl = &ff_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, 0); if(level == 127){ break; } else if(level != 0) { scantable += run; j = *scantable; 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); scantable += run; j = *scantable; if(level<0){ level= (-level*qscale*quant_matrix[j])>>4; level= -level; }else{ level= (level*qscale*quant_matrix[j])>>4; } } block[j] = level; } CLOSE_READER(re, &s->gb); } s->block_last_index[n] = scantable - s->intra_scantable.permutated; 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; int save_width, save_height; AVRational frame_rate_ext; ///< MPEG-2 specific framerate modificator} Mpeg1Context;static int mpeg_decode_init(AVCodecContext *avctx){ Mpeg1Context *s = avctx->priv_data; MpegEncContext *s2 = &s->mpeg_enc_ctx; int i; //we need some parmutation to store //matrixes, until MPV_common_init() //set the real permutatuon for(i=0;i<64;i++) s2->dsp.idct_permutation[i]=i; MPV_decode_defaults(s2); s->mpeg_enc_ctx.avctx= avctx; s->mpeg_enc_ctx.flags= avctx->flags; s->mpeg_enc_ctx.flags2= avctx->flags2; ff_mpeg12_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->coded_width != s->width || avctx->coded_height != s->height|| s1->save_width != s->width || s1->save_height != s->height || s1->save_aspect_info != s->aspect_ratio_info|| 0) { if (s1->mpeg_enc_ctx_allocated) { ParseContext pc= s->parse_context; s->parse_context.buffer=0; MPV_common_end(s); s->parse_context= pc; } if( (s->width == 0 )||(s->height == 0)) return -2; avcodec_set_dimensions(avctx, s->width, s->height); avctx->bit_rate = s->bit_rate; s1->save_aspect_info = s->aspect_ratio_info; s1->save_width = s->width; s1->save_height = s->height; //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->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num; avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den; //mpeg1 aspect avctx->sample_aspect_ratio= av_d2q( 1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255); }else{//mpeg2 //mpeg2 fps av_reduce( &s->avctx->time_base.den, &s->avctx->time_base.num, ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num, ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den, 1<<30); //mpeg2 aspect if(s->aspect_ratio_info > 1){ if( (s1->pan_scan.width == 0 )||(s1->pan_scan.height == 0) ){ s->avctx->sample_aspect_ratio= av_div_q( ff_mpeg2_aspect[s->aspect_ratio_info], (AVRational){s->width, s->height} ); }else{ s->avctx->sample_aspect_ratio= av_div_q( ff_mpeg2_aspect[s->aspect_ratio_info], (AVRational){s1->pan_scan.width, s1->pan_scan.height} ); } }else{ s->avctx->sample_aspect_ratio= ff_mpeg2_aspect[s->aspect_ratio_info]; } }//mpeg2 if(avctx->xvmc_acceleration){ avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420); }else{ if(s->chroma_format < 2){ avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420); }else if(s->chroma_format == 2){ avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_422); }else if(s->chroma_format > 2){ avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_444); } } //until then pix_fmt may be changed right after codec init if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ) if( avctx->idct_algo == FF_IDCT_AUTO ) avctx->idct_algo = FF_IDCT_SIMPLE; //quantization matrixes may need reordering //if dct permutation is changed memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t)); if (MPV_common_init(s) < 0) return -2; quant_matrix_rebuild(s->intra_matrix, old_permutation,s->dsp.idct_permutation); quant_matrix_rebuild(s->inter_matrix, old_permutation,s->dsp.idct_permutation); quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation); quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation); s1->mpeg_enc_ctx_allocated = 1; } return 0;}static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf, int buf_size){ Mpeg1Context *s1 = avctx->priv_data; MpegEncContext *s = &s1->mpeg_enc_ctx; int ref, f_code, vbv_delay; if(mpeg_decode_postinit(s->avctx) < 0) return -2; init_get_bits(&s->gb, buf, buf_size*8); ref = get_bits(&s->gb, 10); /* temporal ref */ s->pict_type = get_bits(&s->gb, 3); if(s->pict_type == 0 || s->pict_type > 3) return -1; vbv_delay= get_bits(&s->gb, 16); if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) { s->full_pel[0] = get_bits1(&s->gb); f_code = get_bits(&s->gb, 3); if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT) return -1; s->mpeg_f_code[0][0] = f_code; s->mpeg_f_code[0][1] = f_code; } if (s->pict_type == B_TYPE) { s->full_pel[1] = get_bits1(&s->gb); f_code = get_bits(&s->gb, 3); if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT) return -1; s->mpeg_f_code[1][0] = f_code; s->mpeg_f_code[1][1] = f_code; } s->current_picture.pict_type= s->pict_type; s->current_picture.key_frame= s->pict_type == I_TYPE; if(avctx->debug & FF_DEBUG_PICT_INFO) av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type); s->y_dc_scale = 8; s->c_dc_scale = 8; s->first_slice = 1; return 0;}static void mpeg_decode_sequence_extension(Mpeg1Context *s1){ MpegEncContext *s= &s1->mpeg_enc_ctx; int horiz_size_ext, vert_size_ext; int bit_rate_ext; skip_bits(&s->gb, 1); /* profil and level esc*/ s->avctx->profile= get_bits(&s->gb, 3); s->avctx->level= get_bits(&s->gb, 4); s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */ s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */ horiz_size_ext = get_bits(&s->gb, 2); vert_size_ext = get_bits(&s->gb, 2); s->width |= (horiz_size_ext << 12); s->height |= (vert_size_ext << 12); bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */ s->bit_rate += (bit_rate_ext << 18) * 400; skip_bits1(&s->gb); /* marker */ s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10; s->low_delay = get_bits1(&s->gb); if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1; s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1; s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1; dprintf(s->avctx, "sequence extension\n"); s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO; s->avctx->sub_id = 2; /* indicates mpeg2 found */ if(s->avctx->debug & FF_DEBUG_PICT_INFO) av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n", s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);}static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1){ MpegEncContext *s= &s1->mpeg_enc_ctx; int color_description, w, h; skip_bits(&s->gb, 3); /* video format */ color_description= get_bits1(&s->gb); if(color_description){ skip_bits(&s->gb, 8); /* color primaries */ skip_bits(&s->gb, 8); /* transfer_characteristics */ skip_bits(&s->gb, 8); /* matrix_coefficients */ } w= get_bits(&s->gb, 14); skip_bits(&s->gb, 1); //marker h= get_bits(&s->gb, 14); skip_bits(&s->gb, 1); //marker s1->pan_scan.width= 16*w; s1->pan_scan.height=16*h; if(s->avctx->debug & FF_DEBUG_PICT_INFO) av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);}static void mpeg_decode_picture_display_extension(Mpeg1Context *s1){ MpegEncContext *s= &s1->mpeg_enc_ctx; int i,nofco; nofco = 1; if(s->progressive_sequence){ if(s->repeat_first_field){ nofco++; if(s->top_field_first) nofco++; } }else{ if(s->picture_structure == PICT_FRAME){ nofco++; if(s->repeat_first_field) nofco++; } } for(i=0; i<nofco; i++){ s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16); skip_bits(&s->gb, 1); //marker s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16); skip_bits(&s->gb, 1); //marker } if(s->avctx->debug & FF_DEBUG_PICT_INFO) av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n", s1->pan_scan.position[0][0], s1->pan_scan.position[0][1], s1->pan_scan.position[1][0], s1->pan_scan.position[1][1], s1->pan_scan.position[2][0], s1->pan_scan.position[2][1] );}static void mpeg_decode_quant_matrix_extension(MpegEncContext *s){ int i, v, j; dprintf(s->avctx, "matrix extension\n"); if (get_bits1(&s->gb)) { for(i=0;i<64;i++) { v = get_bits(&s->gb, 8); j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ]; s->intra_matrix[j] = v; s->chroma_intra_matrix[j] = v; } } if (get_bits1(&s->gb)) { for(i=0;i<64;i++) { v = get_bits(&s->gb, 8); j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ]; s->inter_matrix[j] = v; s->chroma_inter_matrix[j] = v; } } if (get_bits1(&s->gb)) { for(i=0;i<64;i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -