MPEG-2文件的构造格式

来源:互联网 发布:淘宝食品代理公司注 编辑:程序博客网 时间:2024/04/30 07:09
一个完整的MPEG-2文件就是一个PS流文件。

    MPEG-2文件的构造格式如下图:

    以一个实际的MPEG-2文件为例来分析一下PS流,MPEG-2文件:

    http://download.csdn.net/source/212077 

    打开文件,可以看到文件以0x0000 01B9结束,即MPEG_Program_end_code,之前有很多个0x0000 00BA字段,即为program pack的开始标志,program pack包的构造如下:

    可以看出program pack由3部分组成:pack_header+system_header+PES_packets,其中system_header当且仅当pack是第一个数据包时才存在。system_header和PES_packets构造如下:

    对照文件内容,各字段值如下:

            pack_header: 起始地址:0x0000 0000        

                Packet_start_code(4B):0x0000 01BA

                '01'

                SCR_base[32...30](3b):000

                marker:1

                SCR_base[29...15](15b):00 0000 0000 0010 1

                marker:1

                SCR_base[14...0](15b):00 0100 1000 0101 1

                marker:1

                SCR_externsion(9b):00 1111 101

                marker:1

                mutiplex_rate(22b):0000 0000 0101 0101 0001 01

                marker:1

                marker:1

                reserved(5b):1111 1

                stuffing_length(3b):000

           system_header: 起始地址:0x0000 000e

                   system_head_start_code(4B):0x0000 01BB

                head_length(2B):0x000C

                marker:1

                rate_bound(22b):000 0000 0010 1010 1000 101

                marker:1

                audio_bound(6b):0000 01

                fixed_flag(1b):0

                CSPS_flag(1b):0

                system_audio_local_flag(1b):0

                system_video_local_flag(1b):0

                marker:1

                video_bound(5b):0 0001

                packet_rate_restriction_flag(1b):0

                reserved(7b):111 1111

               

                stream_id(1B):0xE0(E0~EF为视频ID)

                '11'

                P-STD_buffer_bound_scale(1b):1

                P-STD_buffer_size_bound(13b):0 0000 0000 0001

 

                stream_id(1B):0xC0(C0~DF为音频ID)

                '11'

                P-STD_buffer_bound_scale(1b):0

                P-STD_buffer_size_bound(13b):0 0000 0100 1000

            PES packet:起始地址:0x0000 0020

                   packet_start_code_prefix(3B):0x 00 00 01

                stream_id(1B):0xBE(BE为Padding stream)

                PES_packet_length(2B):0x07 DA,跳过该长度的填充字节,刚好为下一个pack的起始标志:0x000001BA

      从0x0800地址开始为第二个pack包,该包的构造格式为pack_header+PES,分析如下:

            pack_header: 起始地址:0x0000 0800

                 Packet_start_code(4B):0x0000 01BA

                 '01'

                 program_clock_reference_base(3b):000

                 marker:1

                 SCR_base(15b):00 0000 0000 0000 0

                 marker:1

                 SCR_base(15b):00 0001 1001 1001 1

                 marker:1

                 SCR_externsion(9b):00 0111 100

                 marker:1

                 mutiplex_rate(22b):0000 0000 0100 0110 0101 00

                 marker:1

                 marker:1

                 reserved(5b):1111 1

                 stuffing_length(3b):000

             PES packet:起始地址:0x0000 080e

                   packet_start_code_prefix(3B):0x 00 00 01

                stream_id(1B):0xE0(视频)

                PES_packet_length(2B):0x07 EC,跳过该长度,到达0x1000地址单元,为下一个pack的起始标志:0x000001BA

                '10'

                scrambling_control(2b):00

                priority(1b):0

                alignment(1b):0

                copyright(1b):0

                original(1b):0

                PTS_DTS_flag(2b):11,该标志不能为01

                ESCR_flag(1b):0

                ES_rate_flag(1b):0

                DSM_trick_mode_flag(1b):0

                additional_copy_info_flag(1b):0

                CRC_flag(1b):0

                extension_flag(1b):0

                PES_header_data_length(1B):0x0A

                '0011'

                PTS[32...30](3b):000,PTS全称为presentation time stamp

               marker:1

                PTS[29...15](15b):0000 0000 0000 000

                marker:1

                PTS[14...0](15b):1010 1001 1111 001

                marker:1

                '0001'

                DTS[32...30](3b):000,DTS全称为dedoding time stamp

                marker:1

                DTS[29...15](15b):0000 0000 0000 000

                marker:1

                DTS[14...0](15b):1000 1100 1010 000

                marker:1

    因为除了PTS_DTS_flag为11外,其余6个标志都为0,所以直接进入视频序列:Video Sequence,其结构定义如下:

    图中sequence_header,sequence entension等子内容定义如下:

     

    对各字段分析如下:

             sequence header:起始地址:0x0000 0821

                sequence_header_code(4B):0x 0000 01B3

                horizontal_size(12b):0010 0100 0000

                vertical_size(12b):0001 1110 0000

                aspect_ratio_info(4b):0010

                frame_rate(4b):0001

                bit_rate(18b):0001 0110 1111 0011 00

                marker:1

                VBV_buffer_size(10b):0 0011 1000 0

                constrained(1b):0

                load_intra_Q_matrix(1b):0

             sequence extension:起始地址:0x0000 082d;如果有这一项说明是MPEG-2文件,否则为MPEG-1文件

                extension_start_code(4B):0x0000 01B5

                start_code_identifer(4b):0001

                profile_level_escape(1b):0

                profile_level(7b):100 1000

                progressive(1b):1

                chroma(2b):01

                horiz_extension(2b):00

                vertical_extension(2b):00

    接下来的数据是0x0000 01B8,说明该序列中没有extension&user data[i](extension data的开始符为0x0000 01B5,user data的开始符为0x0000 01B2),接下来的内容为Group of pictures header。

             Group_of_pictures_header:起始地址:0x0000 0837

                group_start_code(4B):0x0000 01B8

                time_code(25b):0000 0000 0000 1000 0000 0000 0

                closed_gop(1b):0

                broken_link(1b):0

             extension & user data[1]:无

             picture header: 起始地址:0x0000 083F

                picture_start_code(4B):0x0000 0100

                temporal_reference(10b):0000 0000 0000 00

                picture_coding_type(3b):00 1

                vbv_delay(16b):111 1111 1111 1111 1

                extra_bit_picture(1b):0

             picture coding extension:起始地址:0x0000 0847

                extension_start_code(4B):0x0000 01B5

                extension_start_code_identifier(4b):1000

                f_code[0][0](4b):1111,forward horizontal

                f_code[0][1](4b):1111,forward vertical

                f_code[1][0](4b):1111,backward horizontal

                f_code[1][1](4b):1111,backward vertical

                intra_dc_presison(2b):00

                picture_structure(2b):11

                top_field_first(1b):0

                frame_pred_frame_dct(1b):1

                concealment_motion_vectors(1b):0

                q_scale_type(1b):0

                intra_vlc_format(1b):0

                alternate_scan(1b):0

                repeat_first_field(1b):0

                chroma_420_type(1b):1

                progressive_frame(1b):1

                composite_display_flag(1b):0

             extension & user data[2]:无

             picture data: 起始地址:0x0000 0850,picture data分为多个slice片,而每个slice又分为多个Microblock,Microblock继续分为Block块。

             13818-2中定义的picture_data()如下:

                picture_data() { 

                      do {

                            slice()

                      } while ( nextbits() == slice_start_code )

                      next_start_code()

                 }              

                Slice
                slice() {

                        slice_start_code                                                32

                        if (vertical_size > 2800)

                               slice_vertical_position_extension           3

                        if (<sequence_scalable_extension() is present in the bitstream>) {

                               if (scalable_mode == “data partitioning” )

                                        priority_breakpoint                              7

                         }

                        quantiser_scale_code                                       5 

                        if ( nextbits() == ‘1’ ) {

                              intra_slice_flag                                              1 

                              intra_slice                                                       1

                             reserved_bits                                                  7 

                              while ( nextbits() == ‘1’ ) {

                                   extra_bit_slice /* with the value ‘1*/   1 

                                   extra_information_slice                          8 

                              }

                        }

                       extra_bit_slice /* with the value ‘0’ */          1 

                       do {

                            macroblock()

                       } while ( nextbits() != ‘000 0000 0000 0000 0000 0000’ )

                       next_start_code()

               }

          从文件内容分析,从0x0000 0850单元为第一个slice(0000 0101),共有7个slice,第7个slice的起始单元为0x0000  fe04,紧接着开始下一个packe,直到0x0000 3000才开始第一个音频pack包,后面的数据视频pack和音频数据成交叉状态。文件结尾标志为0x0000 01B9。