During the internship did a TS code flow analysis of the project, summed up some knowledge, share to everyone, may have written errors and omissions, welcome correction 1. The relationship between TS 1.1 ts Flow and other flows
ES (Elementary Stream): Basic code flow, not segmented audio, video or other information of the continuous stream.
pes (packetized Elementary Stream): a packet of basic code flow, the Basic code flow ES stream according to the need to be divided into different lengths of data packets, and with Baotou to form a packaged basic code flow PES flow. is a data structure used to transmit ES.
TS (Transport Stream): transmission flow, is composed of fixed-length packets, contains independent time benchmark of one or more programs, suitable for more error-free environment, and from any part of the stream can be independently decoded. in MPEG-2 systems, the standard flow of information generated by video, audio es streams, and secondary data multiplexing is called the MPEG-2 transport stream. personally, TS streams are the original PES stream (audio and video) that inserts psi/si and some identifiers (secondary data) information at a certain frequency and then packs the resulting transport stream in a fixed length. It is noteworthy that the PSI/SI information in the TS stream is not only sent once, but at a certain frequency to insert the code stream, is repeatedly sent.
PS (Program stream): programme stream, The difference between PS flow and TS flow is that the packet structure of PS flow is variable length, and the packet structure of TS flow is fixed length. 1.2 TS Pack
Length of TS packet: 188 B or 204 b,204 B length is increased by B's CRC checksum data after 188B.
sync_byte: 1B, fixed value 0x47,ts packet identifier, the normal TS package is still 0x47 "next TS Packet identifier" After the 0x47 header identifier 188/204b
transport_error_indicator: 1bit, when it is 1 o'clock, indicates that there is at least one error bit in the TS packet that cannot be corrected, and only after the error has been corrected can the bit be reset by 0 "After the TS package is actually obtained, the bit 1 packet discards"
payload_unit_start_indicator: 1bit, for the PSI packet, the bit is 1 o'clock, indicating that the TS package is the first package of a section, and that it contains Pointer_field, the value of the variable is In addition to adjusting the field, the next Pointer_field bytes Start, is the valid data. For empty packets, the value is 0.
transport_priority: 1bit, which represents transmission priority, and for TS packages with the same PID, this field has a higher priority for TS packets of 1.
PID:13bit,pid can identify the type of data stored in the TS packet for effective net load. The PID is used to identify various psi/si information tables, TV programs, and the PES packets that distinguish audio and video, which is the key to identify the character of code flow information.
Transport_scrambling_control:2bit, used to indicate the payload of the transmission flow packet. "The header of the transfer flow packet includes the adjustment field and should not be disturbed; empty packets are not disturbed." 】
Transport_scrambling_control |
Describe |
00 |
Not scrambling |
01 |
User-defined |
10 |
User-defined |
11 |
User-defined |
Adaption_field_control:2bit, indicating whether the header of the transfer flow follows the adjustment field/payload "If all is the adjustment field does not contain Payload"
Adaption_field_control |
Describe |
00 |
Future use of reservations for ISO/IEC |
01 |
Field not adjusted |
10 |
No payload, all adjustment fields |
11 |
0-182B adjusted field for effective net load |
continuity_counter: 4bit, increases with TS packets with the same PID, when it reaches the maximum (31), restores to 0, if Adaption_field_control = 00/10, the continuous counter does not increase Because it does not contain payload. 1.3 Analysis TS Packet 1.3.1 Get packet length
TS packets have two kinds of--188b or 204B of packet length, before parsing TS packets, it is necessary to determine the TS packet length for subsequent analysis.
I design to get the package length method is more clumsy, after getting the first 0x47 data, let the file pointer back 188B, if it is 0x47, let the pointer continue downward, so that the cycle 10 times (can be more), the result is still 0x47, judge the length of the package as 188 B, otherwise the same way to judge 204B, Through the packet length of 204, are not through the file to continue to search, the same method to determine the length of the package.
The detailed process is shown below (take 188 B as an example):
After analysis of the code flow found that most of the TS packets are 188 b for the specified length.
1.3.2 Analysis TS Baotou
After obtaining the packet length, it is necessary to parse the header information and obtain valid data, and define a structure body to store the data:
The structure body of/*TS packet Baotou
/typedef struct CSTSPACKETHEADER_S
{
BYTE ucsyncbyte; TS Packet identifier
BYTE uctransport_error_indicator; The transmission error indicator, the value last, indicating that the packet was incorrectly
BYTE ucpayload_unit_start_indicator; The active net load start tag bit, the value last, indicating that the package is the beginning of a section with the Pointer_field field
BYTE uctransport_priority; The priority of the transfer
WORD wpid; The ID of the TS packet, used to differentiate between the different section
BYTE Uctransport_scrambling_control; The scrambling method indicating the effective net load of TS transfer packets
BYTE Ucadaptation_field_control; Indicates whether there are adjustment fields and valid net load
BYTE uccontinuity_counter; Increasing}tspacketheader with the increase of the same PID TS packet
;
Note: If you want to conserve storage space, you can define the structure by using a bit-field approach. byte--unsigned Char,word--unsignedshort int
Assuming that the packet length is 188, we load a TS packet into a byte array of length 188, the first 4 byte is the data of the header, the data can be logically with, move left and right, logical or method, the specific examples are as follows:
Psttsheader->wpid = ((puctsbuffer[1]& 0x1f) << 8) | PUCTSBUFFER[2]; 1.3.3 to judge the validity of TS packets
In one stream, not all of them are valid TS packets, and some invalid TS packets need to be removed
Invalid TS packets are divided into five categories:
(1) The TS packet back 188B is not 0x47 header identifier (TS packets are continuously sent, if the packet is not contiguous, the packet data transmission error);
(2) TS packet error, that is, the value of Transport_error_indicator is 1;
(3) TS package is all adjusted field (empty packet), that is, the value of Adaption_field_control is 10 (binary);
(4) The Adjustment field of TS packet is reserved, that is, the value of Adaption_field_control is 00 (binary);
(5) TS packets are added, that is, the Transport_scrambling_control is not 00, (if there is a disturbance can be removed this situation);
We discard all TS packets for each of these five cases and get the next TS package directly. 1.3.4 determine the starting position of the payload
TS Packet, the starting position of the payload is not fixed, will be affected by the adjustment field and Pointer_field, after parsing the header information can be determined to determine the starting position of the payload payloadposition.
The first thing to determine is whether there are adjustment fields, if there are payloadposition = 5 + Adjust the length of the field.
If there is no payloadposition= 4.
The second is to judge whether there are pointer_field,payload_unit_start_indicator= 1, at this time payloadposition+= 1 + pointer_field;
Note: 1. The subscript of an array is calculated here, starting with the payloadposition (including the payloadposition subscript) as valid data.
All the lengths in the 2.TS stream are calculated from the next byte of the length data, such as Section_length 5, which is the 5 byte length from the next byte of the Section_length, So the first case is added to adjust the length of the field itself 1 bytes, and Baotou 4 bytes, Total is 5.
Start Reference Blog http://blog.chinaaet.com/yuwoo/p/5100018406 thanks.