TS Stream parsing-pat table Parsing

Source: Internet
Author: User
Tags crc32

The Pat table is defined as follows:

Typedef struct ts_pat_program
{
Unsigned program_number: 16; // program number
Unsigned program_map_pid: 13; // PID of the program ing table, which corresponds to a PID when the program number is greater than 0. Each program corresponds to one
} Ts_pat_program;

// Pat table structure
Typedef struct ts_pat
{
Unsigned table_id: 8; // fixed to 0x00, indicating that the table is Pat
Unsigned section_syntax_indicator: 1; // segment syntax flag, fixed to 1
Unsigned zero: 1; // 0
Unsigned reserved_1: 2; // Reserved Bit
Unsigned section_length: 12; // indicates the number of useful bytes after this byte, including CRC32
Unsigned transport_stream_id: 16; // the ID of the transmission stream, which is different from other multiplexing streams in a network.
Unsigned reserved_2: 2; // Reserved Bit
Unsigned version_number: 5; // The value ranges from 0 to 31, indicating the Pat version number.
Unsigned current_next_indicator: 1; // whether the sent Pat is currently valid or the next Pat is valid
Unsigned section_number: 8; // segment number. Pat may be divided into multiple segments for transmission. The first segment is 00. In the future, each segment is added with 1, and a maximum of 256 segments may exist.
Unsigned last_section_number: 8; // The number of the last segment
 
STD: vector <ts_pat_program> program;
Unsigned reserved_3: 3; // Reserved Bit
Unsigned network_pid: 13; // PID of the network information table (NIT). When the program number is 0, the corresponding PID is network_pid.

Unsigned crc_32: 32; // CRC32 Verification Code
} Ts_pat;

 

The parsing code is as follows:

Hresult cts_stream_parse: adjust_pat_table (ts_pat * packet, unsigned char * buffer)
{
Packet-> table_id = buffer [0];
Packet-> section_syntax_indicator = buffer [1]> 7;
Packet-> zero = buffer [1]> 6 & 0x1;
Packet-> reserved_1 = buffer [1]> 4 & 0x3;
Packet-> section_length = (buffer [1] & 0x0f) <8 | buffer [2];
 
Packet-> transport_stream_id = buffer [3] <8 | buffer [4];
 
Packet-> reserved_2 = buffer [5]> 6;
Packet-> version_number = buffer [5]> 1 & 0x1f;
Packet-> current_next_indicator = (buffer [5] <7)> 7;
Packet-> section_number = buffer [6];
Packet-> last_section_number = buffer [7];

Int Len = 0;
Len = 3 + packet-> section_length;
Packet-> crc_32 = (buffer [len-4] & 0x000000ff) <24
| (Buffer [len-3] & 0x000000ff) <16
| (Buffer [len-2] & 0x000000ff) <8
| (Buffer [len-1] & 0x000000ff );
 

Int n = 0;
For (n = 0; n <packet-> section_length-12; n + = 4)
{
Unsigned program_num = buffer [8 + N] <8 | buffer [9 + N];
Packet-> reserved_3 = buffer [10 + N]> 5;

Packet-> network_pid = 0x00;
If (program_num = 0x00)
{
Packet-> network_pid = (buffer [10 + N] & 0x1f) <8 | buffer [11 + N];

Ts_network_pid = packet-> network_pid; // record the Network PID of the TS Stream

Trace ("packet-> network_pid % 0x/n", packet-> network_pid );
}
Else
{
Ts_pat_program pat_program;
Pat_program.program_map_pid = (buffer [10 + N] & 0x1f) <8 | buffer [11 + N];
Pat_program.program_number = program_num;
Packet-> program. push_back (pat_program );

Ts_program.push_back (pat_program); // Add Pat program information to the global Pat program Array
}
}
Return 0;
}

Therefore, the Pat data parsing result is as follows:

Pat data

Table_id: 0x00 // 8
Section_syntax_indicator: 0x01 // 1
'0': 0x00 // 1
Reserved 0x03 // 2
Section_length: 0x00d // 12
Transport_stream_id: 0x0000 // 16
Reserved: 0x03 // 2
Version_number: 0x00 // 5
Current_next_indicator: 0x01 // 1
Section_number: 0x00 // 8
Last_section_number: 0x00 // 8
Program_number: 0x0001/16
Reserved: 0x07 // 3
Program_map_pid: 0x03e8 // 13
CRC: 0x F0 0b D7 79

The parsing structure shows that the Pat table contains no network information package information and only one program. Its PID is 0x03e8.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.