📄 mpvd.java
字号:
/* based on ISO/IEC 13818-2 section 6.3.12
(November 1994) Picture display extensions */
/* derive number_of_frame_center_offsets */
if (progressive_sequence>0){
if (repeat_first_field>0) {
if (top_field_first>0)
number_of_frame_center_offsets = 3;
else
number_of_frame_center_offsets = 2;
}else
number_of_frame_center_offsets = 1;
}else{
if (picture_structure!=FRAME_PICTURE)
number_of_frame_center_offsets = 1;
else{
if (repeat_first_field>0)
number_of_frame_center_offsets = 3;
else
number_of_frame_center_offsets = 2;
}
}
/* now parse */
for (i=0; i<number_of_frame_center_offsets; i++){
frame_center_horizontal_offset[i] = Get_Bits(16);
Flush_Bits(1); // marker bit
frame_center_vertical_offset[i] = Get_Bits(16);
Flush_Bits(1); // marker bit
//DM24062004 081.7 int05 add
info_3 += ", (" + frame_center_horizontal_offset[i] + "," + frame_center_vertical_offset[i] + ")";
}
}
/* decode picture coding extension */
public void picture_coding_extension(){
int chroma_420_type;
int composite_display_flag;
int v_axis;
int field_sequence;
int sub_carrier;
int burst_amplitude;
int sub_carrier_phase;
f_code[0][0] = Get_Bits(4);
f_code[0][1] = Get_Bits(4);
f_code[1][0] = Get_Bits(4);
f_code[1][1] = Get_Bits(4);
intra_dc_precision = Get_Bits(2);
picture_structure = Get_Bits(2);
top_field_first = Get_Bits(1);
frame_pred_frame_dct = Get_Bits(1);
concealment_motion_vectors = Get_Bits(1);
q_scale_type = Get_Bits(1);
intra_vlc_format = Get_Bits(1);
alternate_scan = Get_Bits(1);
repeat_first_field = Get_Bits(1);
chroma_420_type = Get_Bits(1);
progressive_frame = Get_Bits(1);
composite_display_flag = Get_Bits(1);
if (composite_display_flag>0){
v_axis = Get_Bits(1);
field_sequence = Get_Bits(3);
sub_carrier = Get_Bits(1);
burst_amplitude = Get_Bits(7);
sub_carrier_phase = Get_Bits(8);
//DM29082004 081.7 int10 add
info_3 += ", cdf";
}
}
/* Copyright extension */
/* ISO/IEC 13818-2 section 6.2.3.6. */
/* (header added in November, 1994 to the IS document) */
public void copyright_extension(){
int copyright_flag;
int copyright_identifier;
int original_or_copy;
int copyright_number_1;
int copyright_number_2;
int copyright_number_3;
int reserved_data;
copyright_flag = Get_Bits(1);
copyright_identifier = Get_Bits(8);
original_or_copy = Get_Bits(1);
/* reserved */
reserved_data = Get_Bits(7);
Flush_Bits(1); // marker bit
copyright_number_1 = Get_Bits(20);
Flush_Bits(1); // marker bit
copyright_number_2 = Get_Bits(22);
Flush_Bits(1); // marker bit
copyright_number_3 = Get_Bits(22);
}
/* set std for lower profiles as mpeg1 */
public void resetDecoder(){
Fault_Flag=0; //DM14052004 081.7 int02 add,fix
picture_coding_type=0; //DM14052004 081.7 int02 add,fix
SequenceHeader=1;
video_format=5;
progressive_sequence=1;
chroma_format=1;
profile_and_level_indication=0;
Second_Field=0;
intra_dc_precision=0;
picture_structure=FRAME_PICTURE;
top_field_first=0;
frame_pred_frame_dct=1;
concealment_motion_vectors=0;
intra_vlc_format=0;
repeat_first_field=0;
progressive_frame=1;
q_scale_type=0;
quantizer_scale=0;
alternate_scan=0;
}
public void InitialDecoder(){
mb_width = (horizontal_size+15)/16;
mb_height = (progressive_sequence>0) ? (vertical_size+15)/16 : 2*((vertical_size+31)/32);
Coded_Picture_Width = 16 * mb_width;
Coded_Picture_Height = 16 * mb_height;
Chroma_Width = (chroma_format==CHROMA444) ? Coded_Picture_Width : Coded_Picture_Width>>1;
Chroma_Height = (chroma_format!=CHROMA420) ? Coded_Picture_Height : Coded_Picture_Height>>1;
block_count = ChromaFormat[chroma_format];
if (picture_coding_type==I_TYPE)
pixels = new int[Coded_Picture_Width*Coded_Picture_Height]; //DM30112003 081.5++ fix
}
//public void Decode_Picture(int ref, byte dst, int pitch){
public void Decode_Picture(){
if (picture_structure==FRAME_PICTURE && Second_Field>0)
Second_Field = 0;
if (picture_coding_type!=B_TYPE){
pf_forward = pf_backward;
pf_backward = pf_current;
}
//moved
String SH[] = { "G","S" };
String cf[] = { "res.","4:2:0","4:2:2","4:4:4" };
String fieldorder[] = {"B","T"}; //<==TheHorse 221003
String picture_struc[] = {"-","T","B","F"}; //DM08022004 081.6 int16 add
//DM26022004 081.6 int18 changed
//DM06052004 081.7 int02 changed
info_2 = "" + gop_hour + ":" + gop_minute + ":" + gop_sec + ":" + gop_frame + " ";
info_2 += ", " + drop_flag + "/" + closed_gop + "/" + broken_link + " ";
info_2 += ", " + (Math.round(frame_rate * 1000) / 1000.0f) + "fps "; //DM06022004 081.6 int15 change
info_2 += ", " + SH[SequenceHeader] + " ";
info_2 += ", " + picture_struc[picture_structure]; //DM08022004 081.6 int16 add
info_2 += ", " + ((progressive_sequence==0) ? fieldorder[top_field_first] : "-") + " "; //<==TheHorse 221003
info_2 += ", " + cf[chroma_format];
info_2 += info_3;
SequenceHeader=0;
Update_Picture_Buffers();
picture_data();
scale_Picture();
repaint();
/**
if (ref>0 && (picture_structure==FRAME_PICTURE || Second_Field>0)){
if (picture_coding_type==B_TYPE)
FrametoRGB(auxframe, pf_current, dst, pitch);
else
FrametoRGB(forward_reference_frame, pf_forward, dst, pitch);
}
**/
if (picture_structure!=FRAME_PICTURE)
Second_Field ^= Second_Field;
}
/* reuse old picture buffers as soon as they are no longer needed */
public void Update_Picture_Buffers(){
int cc; /* color component index */
byte tmp; /* temporary swap pointer */
for (cc=0; cc<3; cc++) {
/* B pictures do not need to be save for future reference */
if (picture_coding_type==B_TYPE)
current_frame[cc] = auxframe[cc];
else{
if (Second_Field<1){
/* only update at the beginning of the coded frame */
tmp = forward_reference_frame[cc];
/* the previously decoded reference frame is stored coincident with the
location where the backward reference frame is stored (backwards
prediction is not needed in P pictures) */
forward_reference_frame[cc] = backward_reference_frame[cc];
/* update pointer for potential future B pictures */
backward_reference_frame[cc] = tmp;
}
/* can erase over old backward reference frame since it is not used
in a P picture, and since any subsequent B pictures will use the
previously decoded I or P frame as the backward_reference_frame */
current_frame[cc] = backward_reference_frame[cc];
}
if (picture_structure==BOTTOM_FIELD)
current_frame[cc] += (cc==0) ? Coded_Picture_Width : Chroma_Width;
}
}
/* decode all macroblocks of the current picture */
/* stages described in ISO/IEC 13818-2 section 7 */
public void picture_data(){
int MBAmax;
int err=0;
/* number of macroblocks per picture */
MBAmax = mb_width*mb_height;
if (picture_structure!=FRAME_PICTURE)
MBAmax>>=1;
for (;;)
if (slice(MBAmax)<0)
return;
}
/* decode slice header */
/* ISO/IEC 13818-2 section 6.2.4 */
public int slice_header(){
int slice_picture_id_enable = 0;
int slice_picture_id = 0;
int extra_information_slice = 0;
int slice_vertical_position_extension = vertical_size>2800 ? Get_Bits(3) : 0;
int quantizer_scale_code = Get_Bits(5);
quantizer_scale = (q_scale_type>0) ? Non_Linear_quantizer_scale[quantizer_scale_code] : quantizer_scale_code<<1;
/* slice_id introduced in March 1995 as part of the video corridendum
(after the IS was drafted in November 1994) */
if (Get_Bits(1)>0){
Get_Bits(1); // intra slice
slice_picture_id_enable = Get_Bits(1);
slice_picture_id = Get_Bits(6);
extra_information_slice = extra_bit_information();
}
return slice_vertical_position_extension;
}
/* decode extra bit information */
/* ISO/IEC 13818-2 section 6.2.3.4. */
public int extra_bit_information(){
int Byte_Count = 0;
while (Get_Bits(1)>0){
Flush_Bits(8);
Byte_Count ++;
}
return Byte_Count;
}
/* decode all macroblocks of the current picture */
/* ISO/IEC 13818-2 section 6.3.16 */
/* return 0 : go to next slice */
/* return -1: go to next picture */
public int slice(int MBAmax){
int MBA[] = {0}, MBAinc[] ={0}, macroblock_type[]={0}, motion_type[]={0}, dct_type[]={0}, ret=0;
int dc_dct_pred[] = new int[3], PMV[][][] = new int[2][2][2],
motion_vertical_field_select[][] = new int[2][2], dmvector[] = new int[2];
if ((ret=start_of_slice(MBA, MBAinc, dc_dct_pred, PMV))!=1) return ret;
for (;;){
/* this is how we properly exit out of picture */
if (MBA[0]>=MBAmax) return -1; // all macroblocks decoded
if (MBAinc[0]==0) {
if (Show_Bits(23)<1 || Fault_Flag>0){ // next_start_code or fault
Fault_Flag = 0;
return 0; // trigger: go to next slice
}else{ /* neither next_start_code nor Fault_Flag */
/* decode macroblock address increment */
MBAinc[0] = Get_macroblock_address_increment();
if (Fault_Flag>0) {
Fault_Flag = 0;
return 0; // trigger: go to next slice
}
}
}
//test
//System.out.println("mba " + MBA[0]);
if (MBAinc[0]==1) { /* not skipped */
if (decode_macroblock(macroblock_type, motion_type, dct_type, PMV,
dc_dct_pred, motion_vertical_field_select, dmvector)<1) {
Fault_Flag = 0;
//DM14052004 081.7 int02 changed
return 0; //return -1; // return 0; // trigger: go to next slice
}
}else{ /* MBAinc[0]!=1: skipped macroblock */ /* ISO/IEC 13818-2 section 7.6.6 */
skipped_macroblock(dc_dct_pred, PMV, motion_type,
motion_vertical_field_select, macroblock_type);
}
/* ISO/IEC 13818-2 section 7.6 */
motion_compensation(MBA, macroblock_type, motion_type, PMV,
motion_vertical_field_select, dmvector, dct_type);
/* advance to next macroblock */
MBA[0]++; MBAinc[0]--;
if (MBA[0]>=MBAmax) return -1; // all macroblocks decoded
}
}
/* ISO/IEC 13818-2 section 7.6.6 */
public void skipped_macroblock(int dc_dct_pred[], int PMV[][][], int motion_type[],
int motion_vertical_field_select[][], int macroblock_type[]){
int comp;
for (comp=0; comp<block_count; comp++) Clear_Block(comp);
/* reset intra_dc predictors */
/* ISO/IEC 13818-2 section 7.2.1: DC coefficients in intra blocks */
dc_dct_pred[0]=dc_dct_pred[1]=dc_dct_pred[2]=0;
/* reset motion vector predictors */
/* ISO/IEC 13818-2 section 7.6.3.4: Resetting motion vector predictors */
if (picture_coding_type==P_TYPE) PMV[0][0][0]=PMV[0][0][1]=PMV[1][0][0]=PMV[1][0][1]=0;
/* derive motion_type */
if (picture_structure==FRAME_PICTURE) motion_type[0] = MC_FRAME;
else{
motion_type[0] = MC_FIELD;
motion_vertical_field_select[0][0] = motion_vertical_field_select[0][1] =
((picture_structure==BOTTOM_FIELD)?1:0);
}
/* clear MACROBLOCK_INTRA */
macroblock_type[0] &= ~MACROBLOCK_INTRA;
}
/* ISO/IEC 13818-2 sections 7.2 through 7.5 */
public int decode_macroblock(int macroblock_type[], int motion_type[], int dct_type[],
int PMV[][][], int dc_dct_pred[],
int motion_vertical_field_select[][], int dmvector[]){
int quantizer_scale_code, comp, motion_vector_count[]={0}, mv_format[]={0};
int dmv[]={0}, mvscale[]={0}, coded_block_pattern;
/* ISO/IEC 13818-2 section 6.3.17.1: Macroblock modes */
macroblock_modes(macroblock_type, motion_type, motion_vector_count, mv_format,
dmv, mvscale, dct_type);
if (Fault_Flag>0) return 0; // trigger: go to next slice
if ( (macroblock_type[0] & MACROBLOCK_QUANT)>0 ){
quantizer_scale_code = Get_Bits(5);
/* ISO/IEC 13818-2 section 7.4.2.2: Quantizer scale factor */
quantizer_scale = (q_scale_type>0) ?
Non_Linear_quantizer_scale[quantizer_scale_code] : (quantizer_scale_code << 1);
}
/* ISO/IEC 13818-2 section 6.3.17.2: Motion vectors */
/* decode forward motion vectors */
if ( ((macroblock_type[0] & MACROBLOCK_MOTION_FORWARD)>0)
|| (((macroblock_type[0] & MACROBLOCK_INTRA)>0) && (concealment_motion_vectors>0)))
motion_vectors(PMV, dmvector, motion_vertical_field_select, 0,
motion_vector_count, mv_format, f_code[0][0]-1, f_code[0][1]-1, dmv, mvscale);
if (Fault_Flag>0) return 0; // trigger: go to next slice
/* decode backward motion vectors */
if ((macroblock_type[0] & MACROBLOCK_MOTION_BACKWARD)>0)
motion_vectors(PMV, dmvector, motion_vertical_field_select, 1,
motion_vector_count,mv_format, f_code[1][0]-1, f_code[1][1]-1, new int[1], mvscale);
if (Fault_Flag>0) return 0; // trigger: go to next slice
if (((macroblock_type[0] & MACROBLOCK_INTRA)>0) && (concealment_motion_vectors>0))
Flush_Bits(1); // marker bit
/* macroblock_pattern */
/* ISO/IEC 13818-2 section 6.3.17.4: Coded block pattern */
if ((macroblock_type[0] & MACROBLOCK_PATTERN)>0) {
coded_block_pattern = Get_coded_block_pattern();
if (chroma_format==CHROMA422)
coded_block_pattern = (coded_block_pattern<<2) | Get_Bits(2);
else if (chroma_format==CHROMA444)
coded_block_pattern = (coded_block_pattern<<6) | Get_Bits(6);
}else
coded_block_pattern = ((macroblock_type[0] & MACROBLOCK_INTRA)>0) ? (1<<block_count)-1 : 0;
if (Fault_Flag>0) return 0; // trigger: go to next slice
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -