Code implementation: MPEG-2 file Parser Code implementation
A full MPEG-2 file is a PS stream file.
The MPEG-2 file is constructed in such a format as:
Find yourself an MPG file to open, you can see the file to 0x0000 01b9 end (some files are not standard), that is Mpeg_program_end_code, before there are many 0x0000 00BA fields, that is, program pack start flag, The program Pack package is constructed as follows:
You can see that program pack consists of 3 parts: Pack_header+system_header+pes_packets, where System_header exists only if the pack is the first packet. The System_header and pes_packets are constructed as follows:
Control the contents of the file, the order of each field, the length is as follows:
Pack_header: start Address: 0x0000 0000 (This address may not be the same because the file is different)
Logo: 0x00 0x 0x0xBA (4B)
struct packheader{unsigned paddingbyte:2;unsigned program_clock_reference_base:3;unsigned marker1:1;unsigned SCR_ base1:15;unsigned marker2:1;unsigned scr_base2:15;unsigned marker3:1;unsigned scr_externsion:9;unsigned marker4:1;u nsigned mutiplex_rate:22;unsigned marker5:1;unsigned marker6:1;unsigned reserved:5;unsigned stuffing_length:3;};
System_header: start Address: 0x0000 000e
logo (4B): 0x00 0x00 0x01 0xBB
struct systemheader//112bit{unsigned head_length:16;unsigned marker1:1;unsigned rate_bound:22;unsigned marker2:1; unsigned audio_bound:6;unsigned fixed_flag:1;unsigned csps_flag:1;unsigned system_audio_local_flag:1;unsigned system_video_local_flag:1;unsigned marker3:1;unsigned video_bound:5;unsigned Packet_rate_restriction_flag<span Style= "White-space:pre" ></SPAN>: 1;unsigned reserved:7;unsigned stream_id1:8;unsigned paddingbyte1:2; unsigned p_std_buffer_bound_scale1:1;unsigned p_std_buffer_size_bound1:13;unsigned stream_id2:8;unsigned paddingbyte2:2;unsigned p_std_buffer_bound_scale2:1;unsigned p_std_buffer_size_bound2:13;};
PES Packet: starting address: 0x0000 0020
flag bit (3B): 0x 00 00 01
STREAM_ID (1B): 0xBE (be padding stream)
Pes_packet_length (2B): 0x07 DA, skipping the padding byte of that length, just the start flag for the next pack: 0X000001BA
Starting with the 0x0800 address as the second pack package, the package is constructed in Pack_header+pes and is analyzed as follows:
Pack_header: start Address: 0x0000 0800
(This package has already been mentioned, here no longer repeat)
PES Packet: start address: 0x0000 080e
flag bit (3B): 0x 00 00 01
STREAM_ID (1B): 0xE0 (VIDEO)
struct pespacket{unsigned pes_packet_length:16;unsigned paddingbyte1:2;unsigned scrambling_control:2;unsigned priority:1;unsigned alignment:1;unsigned copyright:1;unsigned original:1;unsigned pts_dts_flag:2;unsigned ESCR_flag : 1;unsigned es_rate_flag:1;unsigned dsm_trick_mode_flag<span style= "White-space:pre" ></SPAN>: 1; unsigned additional_copy_info_flag<span style= "White-space:pre" ></SPAN>: 1;unsigned crc_flag:1; unsigned extension_flag:1;unsigned pes_header_data_length<span style= "White-space:pre" ></SPAN>: 8; unsigned paddingbyte2:4;unsigned pts_32_30:3;//pts 第32-30位 unsigned marker1:1;unsigned pts_29_15:15;// PTS of 第29-15位 unsigned marker2:1;unsigned pts_14_0:15;//pts 第14-0位 (total 33 bits) unsigned marker3:1;unsigned paddingbyte3:4; unsigned dts_32_30:3;unsigned marker4:1;unsigned dts_29_15:15;unsigned marker5:1;unsigned dts_14_0:15;unsigned Marke R6:1;};
Video Sequence, whose structure is defined as follows:
The Sequence_header,sequence entension and other sub-contents in the figure are defined as follows:
The order and length of each field are analyzed as follows:
Sequence Header: start address: 0x0000 0821
flag bit (4B): 0x00 0x00 0x01 0xb3
struct sequenceheader{unsigned horizontal_size:12;unsigned vertical_size:12;unsigned aspect_ratio_info:4;unsigned frame_rate:4;unsigned bit_rate:18;unsigned marker:1;unsigned vbv_buffer_size:10;unsigned constrained:1;unsigned Load_intra_q_matrix<span style= "White-space:pre" ></SPAN>: 1;unsigned paddingbit:1;};
sequence Extension: start address: 0x0000 082d; If there is a MPEG-2 file for this description, the MPEG-1 file
Flag bit (4B): 0x0000 01b5
struct sequenceextension{unsigned start_code_identifer:4;unsigned profile_level_escape:1;unsigned profile_level:7; unsigned progressive:1;unsigned chroma:2;unsigned horiz_extension:2;unsigned vertical_extension:2;};
The next data is 0x0000 01b8, stating that there is no extension&user data[i in the sequence (extension data starts with 0x0000 01b5,user data start with 0x0000 01b2), The next content is the group of pictures header.
Group_of_pictures_header: start Address: 0x0000 0837
Flag bit (4B): 0x0000 01b8
struct groupofpicturesheader{unsigned time_code:25;unsigned closed_gop:1;unsigned broken_link<span style= " White-space:pre "></SPAN>: 1;};
Extension & user Data[1]: None
Picture Header: start address: 0x0000 083F
Flag bit (4B): 0x0000 0100
struct pictureheader{unsigned temporal_reference:10;unsigned picture_coding_type<span style= "White-space:pre" ></span>: 3;unsigned vbv_delay:16;unsigned extra_bit_piture:1;};
Picture Coding Extension: start address: 0x0000 0847
Extension_start_code (4B): 0x0000 01b5
struct picturecodingextension{unsigned extension_start_code_identifier:4;unsigned f_code_0_0:4;unsigned f_code_0_1 : 4;unsigned f_code_1_0:4;unsigned f_code_1_1:4;unsigned intra_dc_presison:2;unsigned picture_structure:2;unsigned top_field_first:1;unsigned frame_pred_frame_dct:1;unsigned concealment_motion_vectors:1;unsigned q_scale_type:1; unsigned intra_vlc_format:1;unsigned alternate_scan:1;unsigned repeat_first_field:1;unsigned chroma_420_type:1; unsigned progressive_frame:2;unsigned composite_display_flag:2;};
extension & user Data[2]: none
Picture Data: start Address: 0x0000 0850 (temporarily not analyzed, later on)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
MPEG-2 File Structure parsing