From: http://www.cnblogs.com/hjj801006/p/3837435.html
A ts Stream is a packet-based bitstream format. Each packet contains 188 bytes or 204 characters (generally 188 bytes, the 204-byte format is to add 16 bytes of CRC data after the 188-byte packet. Other formats are the same.) parse the TS stream, parse each packet, and then from a packet, parse the Pat PID, find the Pat package based on the PID, then parse the pmt pid from the Pat package, and find the PMT package based on the PID, parse the video and audio (also including teletext and EPG) PID from the PMT package. Then find the corresponding package based on the PID.
All packages are in the same format, including packet header and packet datas.
The packet header format is as follows:
Typedef struct
{
Unsigned sync_byte: 8-bit;/* 8 bits Synchronization Byte, fixed to 0x47, indicating that it is followed by a TS group. */
Unsigned transport_error_indicator: 1-bit/* 1-Bit Error Indication. 1 indicates that the current packet has at least 1-bit transmission error, 0 indicates that all data is correct. If the data is transmitted incorrectly, the packet will not be processed. */
Unsigned payload_unit_start_indicator: 1-bit/* indicates the start of the load unit ,*/
Unsigned transport_priority: indicates the transmission priority of 1-bit/* 1bit. 1 indicates the high priority, 0 indicates the low priority, and the transmission mechanism may be used. decoding seems to be unavailable */
Unsigned PID: 13 BITs;/* 13 BITs packet ID number. The unique number corresponds to different packages and indicates the type of the payload data of this package, tell us what the package transmits */
Unsigned transport_scrambling_control: 2-bit;/* 2 bits encryption flag. 00 indicates no encryption. Others indicate encrypted, indicating the encryption mode of the TS group's server load. TS header (32 bits in front) should not be encrypted */
Unsigned adaptation_field_control: 2-bit/* 2bits additional region control, indicating whether there are adjustment fields and payload following the header of the TS group. 01 only contains payload, 10 only contains adjustment fields, and 11 contains adjustment fields and payload. If 00, the decoder will not be processed. The empty group does not have any adjustment fields. */
Unsigned continuity_counter: 4-bit/* 4bits package increment counter. The value range is 0-15. For Ts groups with the same PID, 1 is added each time for transmission, and 0 is cleared after 15. but sometimes it is not counted. 1. The TS group does not have a valid load. 2. The value of the copied TS group is the same as that of the original group. 3. the discontinuity mark mentioned later -- When indicator is 1 */
} Packet_header;
Pat Data Structure
Program_association_section ()
{
Table_id // The 8-bit value is fixed to 0x00, indicating that the table is a pat table.
Section_syntax_indicator // 1-segment syntax flag, fixed to 1
'0' // 1 is fixed to 1 set to prevent control word conflicts with iso13818video stream format
Reserved // 2 reserved bits, usually 0
Section_length // the size of 12 segments, indicating the number of useful bytes after the byte, including CRC32. If the number of bytes in front of the byte is less than 188, it will be filled with 0xff, if this value is relatively large, Pat will divide it into several parts for transmission.
Transport_stream_id // 16 the ID of the transmission stream, which is different from other multiplexing streams in a network.
Reserved // 2
Version_number // The value range of 5 is 0-31, indicating the Pat version number and marking the current program version. This is a very useful parameter. When this field is detected to change, it indicates that the program in the TS stream has changed, and the program must collect the program again.
Current_next_indicator // 1 indicates whether the sent Pat is currently valid or the next Pat is valid.
Section_number // The number of the eight segments. Pat may be divided into multiple segments for transmission. The first segment is 00, and 1 is added for each segment in the future. A maximum of 256 segments may exist.
Last_section_number // The number of the last segment of 8
For (I = 0; I <n; I ++)
{
Program_number // program number 16
Reserved // 3
If (program_number = '0 ')
{
Network_pid // 13 PID of the network information table (NIT). The network information table provides some information about the physical network, which is related to the TV station. When the program number is 0, the corresponding PID is network_pid.
}
Else
{
Program_map_pid // PID of the 13 program ing table, which corresponds to the PID when the program number is greater than 0. Each program corresponds to one
}
Crc_32 // 32 CRC32 Verification Code
}
Example 47 40 00 1C 00 00 B0 15 13 F6 E7 00 00 00 E0 10 00 01 E0 20 00 02 E0 21 1A 34 B4 77 FF ...... FF
The first four bytes are the header of the TS group, which is 01000111 01000000 00000000 0001 1100. The pid = (BUF [1] & 0x0f) can be parsed) <8 | Buf [2] = 0x00, indicating that the group's payload is Pat.
The fifth byte 00 is called the "pointer field", indicating an offset, that is, the Pat part starting from the last few bytes. If it is 00, PAT: 00 B0 15 13 F6 E7 00 00 00 00 E0 10 00 01 E0 20 00 02 E0 21 1A 34 B4 77 is followed.
(00000000 10110000 00010101 00010011 11110110 11100111 00000000 00000000 00000000 00000000 11100000 00010000 00000000 00000001 11100000 00100000 00000000 00000010 11100000 00100001 00011010 00110100 10110100 01110111)
The following information can be obtained by parsing the Pat data structure:
Table_id: 0x00
Section_syntax_indicator 0x01
Section_length: 0x015 indicates that the last 21 bytes are useful.
Transport_stream_id: 0x13f6
Version_number: 0x13
Current_next_indicator: 0x01
Section_number: 0x00
Last_section_number: 0x00 indicates that only one segment exists.
Program_number: 0x0000
Network_pid: 0010
Program_number: 0001
Program_map_pid: 0020
Program_number: 0002
Program_map_pid: 0021
Crc_32: 1a34b477
This Pat has only one program, including three programs. Program No. 0000 corresponds to network_pid = 0010, program No. 0001 corresponds to program_map_pid = 0020, and program No. 0002 corresponds to program_map_pid = 0021, from the actual point of view, we should regard the three program numbers as three channels. The content of the first channel is network information, and the Second and Third Channels contain program information. In digital TV, a channel can have multiple programs, and the PMT behind it tells us the PID corresponding to all programs in a channel.
So now we can search for the TS group with PID = 0x0020, that is, the PMT information corresponding to Channel 2.
PMT data structure:
Ts_program_map_section ()
{
Table_id // 8 is fixed to 0x02, indicating that the table is PMT
Section_syntax_indicator // 1-segment syntax flag, fixed to 1
'0' // 1
Reserved // 2
Section_length // 12 indicates the number of useful bytes after this byte, including CRC32. If the number of the first byte is less than 188, it will be filled with 0xff. If this value is relatively large, Pat will divide it into several parts for transmission.
Program_number // program number 16, indicating the program corresponding to the PMT
Reserved // 2
Version_number // The value range of 5 is 0-31, indicating the Pat version number and marking the current program version. This is a very useful parameter. When this field is detected to change, it indicates that the program in the TS stream has changed, and the program must collect the program again.
Current_next_indicator // 1 indicates whether the sent Pat is currently valid or the next Pat is valid.
Section_number // The 8 segment number is fixed to 0x00
Last_section_number // The number of the last segment of 8 is fixed to 0x00
Reserved // 3
The PID of the TS group where pcr_pid // 13 PCR (program clock reference) is located. You can search the corresponding TS group based on the PID to obtain the PCR information.
Reserved 4
Program_info_length // 12 length of the program information, which may contain some bytes to describe the program information.
For (I = 0; I <n; I ++ ){
Descriptor ()
}
For (I = 0; I <N1; I ++ ){
Stream_type // 8 indicates the type of the original stream in the PES group whose PID is elementary_pid, such as video stream and audio stream.
Reserved // 3
Elementary_pid // 13 the PID of the TS group corresponding to the video stream and audio stream included in the program
Reserved // 4
Es_info_length // 12 length of the descriptor of the program-related original stream
For (I = 0; I <N2; I ++ ){
Descriptor ()
}
}
Crc_32 // 32
}
}
Example PMT package
47 40 20 1C 00 02 B0 1f 00 01 E7 00 00 E1 00 F0 00 02 E1 00 F0 05 02 03 B2 44 5f 04 E1 10 F0 03 01 01 67 C9 AB C8 D2
The first four bytes are the header of the TS group, and the fifth byte 00 is the pointer field. The following is the content of PMT.
Table_id: 02
Section_syntax_indicator: 01
Section_length: 01f
Program_number: 0001
Version_number: 13
Current_next_indicator: 01
Section_number: 00
Last_section_number: 00
Pcr_pid: 0100
Program_info_length: 000
Descriptor:
Steam_type: 00
Elementary_pid: 0001
Es_info_length: 000
Descriptor:
Steam_type: 02
Elementary_pid: 0001
Es_info_length: 005
Descriptor: 02 03 B2 44 5f
Steam_type: 04
Elementary_pid: 0011
Es_info_length: 003
Descriptor: 03 01 67
Crc_32: c9abc8d2
It can be seen that the program No. 0001 contains the information of three streams. The streams with the stream types of and are reserved values, which can be ignored. 02 indicates that the original stream is a video stream, its elementary_pid is 0001,04, which indicates that the original stream is an audio stream, its elementary_pid is 0011, and the two streams also carry the descriptor (descriptor), indicating some information about the original stream.
Most of them are reprinted by others, and some understanding is added in the middle. Thank you very much for helping me! Thank you.
TS data stream Pat and PMT analysis (reproduced)