The previous article describes the packaging format of the PES header, this article describes the packet format of the TS package
1.TS Baotou Format
The TS Stream, the transport stream, is a further encapsulation of the PES package, the base unit is TS Packet, fixed a packet size of 188 bytes (or 204 bytes, plus 16 bytes of CRC checksum data after 188 bytes), consisting of TS Header and payload, comprising the following figure:
The header consists of a 4-byte fixed head and a adaptation field data followed by the following byte order:
sync_byte: synchronization Code, the size of a fixed 8 bit, the value is 0x47 ;
transport_error_indicator: error flag bit, placeholder 1bit, set to 1 indicates that there is at least one uncorrectable error in this grouping;
payload_unit_start_indicator: load start flag bit, used to indicate the effective net load of TS package with PES package or PSI data, occupy bit 1bit, and if this value is 1, and the load is PSI data, after the TS header, The starting byte of the load will have 1 adjustment bytes Point_field;
When the TS package comes with PES packet data, the Payload_unit_start_indicator has the following features: set to 1, which identifies the effective payload of the TS package starting with the first byte of the PES package, that is, the TS package is the starter package for the PES package, And there is only one PES package starting field in this TS group, and set to 0, which means that the TS package is not the starter package for the PES package and is the subsequent packet.
When the TS Packet has PSI data, the Payload_unit_start_indicator has the following characteristics: Set to 1, indicating the first byte of the TS packet with a PSI data fragment, that is, the TS package is the starting package for the PSI section, The first byte of the load for this TS packet is Pointer_field, and set to 0, which means that the TS package does not have the first byte of the Psi section, that is, the TS package is not the starting packet of the PSI, that is, there is no point_field in the payload. The start of the payload is the data content of the PSI. The definition of Point_field will be described in the following Psi section; For packages with empty packages, Payload_unit_start_indicator should be set to 0.
For example, if the TS package load is pat, then when the payload_unit_start_indicator of the received TS packet is 1 o'clock, it indicates that the TS packet contains the PAT header information, which resolves the section_length and continuity_ from the package. Counter, and then continue to collect the TS package from the back of Payload_unit_start_indicator = 0, and judge the continuity of the continuity_counter, continuously read out the net load in the TS packet (that is, Pat data), with Section_ Length as the end condition for collecting TS packets.
transport_priority : send priority, 1 means that the package is higher than the other 0 of the same PID package has a high priority, occupying a bit of 1bit;
PID: Indicates the data type in the payload, the placeholder 13bit;0x0000 represents the pat,0x0001 on behalf of CAT,0X0002-0X000F reserved, 0X1FFF represents the empty packet;
Transport_scrambling_control: Payload encryption mode flag, placeholder 2bit,00 means unencrypted;
Adaptation_field_control: Adjusts the field flags to indicate whether this TS header follows the adjustment field or payload data, occupies a bit of 2bit, where 00 bits are reserved, 01 means no adjustment field, only payload data, and 10 indicates that only the adjustment field No payload, 11 indicates an adjustment field, followed by a payload; The field should be 01 for the empty group;
If Adaptation_field_control = = 1x, it is followed by adaptation field;
if Adaptation_field_control = = x1, it is followed by a data_bytes field;
Continuity_counter: Continuity count, with each of the same PID of the TS packet increase, the maximum value is then returned to 0, the placeholder 4bit, if the Adaptation_field_control value is 00 or 01, this value should not be increased If the flag bit discontinuity_indicator value of the adjustment field is 1, the value is also discontinuous;
adaptationfield: The following fields will exist only if Adaptation_field_control = = 1x, as follows:
adaptation_field_length: Adjust the length of the field, mark the length of the field after this byte, occupy a bit of 8bit; a value of 0 o'clock indicates that an adjustment byte is inserted in the TS group and there is no adjustment field followed by the payload ; Adaptation_field_control = = ' 11 ' When this value is between 0~182, Adaptation_field_control = = ' 10 ', this value is 183, if the field is not so long then fill the 0xFF field;
The following fields will appear in the adaptation_field_length>0, in the order below:
discontinuity_indicator: Discontinuous status indicator, placeholder 1bit, 1 indicates that the discontinuous state of this TS group is true;
random_access_indicator: Random access indicator, occupying bit 1bit;
elementary_stream_priority_indicator: Raw stream Data priority indicator, placeholder 1bit, bit 1 indicates that this raw stream data is higher than other original streams in the TS package of the same PID;
pcr_flag: PCR flag bit, placeholder 1bit, set 1 indicates that the adjustment field contains a PCR field, set 0 does not have a PCR field;
opcr_flag: OPCR flag bit, placeholder 1bit, set 1 indicates that the adjustment field contains the OPCR field, set 0 does not have a OPCR field;
splicing_point_flag: Splice_countdown flag bit, placeholder 1bit, set 1 indicates that the adjustment field contains the Splice_countdown field, and the position 0 is not Splice_ Countdown field;
transport_private_data_flag: transport_private_data flag bit, occupy bit 1bit, position 1 indicates that the adjustment field contains 1 or more private data bytes, set 0 is not this byte;
Adaptation_field_extension_flag: Adjust the field extension flag bit, occupy bit 1bit, set 1 to include the Adjustment field extension field, set bit 0 is no extension field;
The above 8 bits are identifiers, followed by the fields determined by the values of the identifiers, in the following order:
PCR field : when pcr_flag = = 1 o'clock, this field is present, occupying a bit of 48bit, sequentially:
program_clock_reference_base field : occupies a position of 33bit;
reserved Field : occupies a position of 6bit;
program_clock_reference_extension field : occupies a position of 9bit;
OPCR field : when opcr_flag = = 1 o'clock, this field exists, occupies a bit of 48bit, sequentially:
original_program_clock_reference_base field : occupies a position of 33bit;
reserved Field : occupies a position of 6bit;
original_program_clock_reference_extension field : occupies a position of 9bit;
splice_countdown field : when splicing_point_flag = = 1 o'clock This field exists, occupying a position of 8bit;
transport_private_data field : Private Data field, when transport_private_data_flag = = 1 o'clock This field exists, placeholder n*8bit, byte order:
transport_private_data_length: Indicates the byte length of the private data, occupying a bit of 8bit;
private_data_byte: private data, length determined by the previous length field;
adaptation_field_extension field : Adjust field extension field, occupation length is indeterminate, when adaptation_field_extension_flag = = 1 o'clock This field exists, the field also has 3 flag bits, To determine that some characters Gencun do not exist, their specific byte order is as follows:
adaptation_field_extension_length: Adjust the length of field extension field, occupy bit 8bit;
ltw_flag: LTW field flag bit, the position 1 indicates that this field exists, occupying a bit of 1bit;
piecewise_rate_flag: piecewise_rate field Flag bit, set 1 o'clock this field exists, occupying a position of 1bit;
seamless_splice_flag: Seamless_splice flag bit, set 1 o'clock this field exists, occupying a position of 1bit;
Reserved: Reserved field, occupy bit 5bit;
LTW field : when ltw_flag = = 1 o'clock This field exists, occupying 16bit, which consists of the following two fields
Ltw_valid_flag: Occupy Bit 1bit, when ltw_valid_flag = = 1 o'clock, Ltw_offset is effective;
Ltw_offset: Occupies a position of 15bit;
piecewise_rate field : when piecewise_rate_flag = = 1 o'clock This field exists, occupies a bit of 24bit, and its byte order is as follows:
Reserved field: Reserved field, placeholder 2bit;
Piecewise_rate field: Placeholder 22bit; This field is only defined when ltw_flag = = 1 and Ltw_valid_flag = = 1 o'clock, when defined, this field is a positive integer;
seamless_splice field : when seamless_splice_flag = = 1 o'clock This field exists, occupies a bit of 40bit; byte order is:
Splice_type field: Placeholder 4bit; identify delay and rate values;
DTS_NEXT_AU[32..30]: occupies a position of 3bit;
Marker_bit field: occupies a position of 1bit;
dts_next_au[29..15] Field: occupies a position of 15bit;
Marker_bit: Occupies a position of 1bit;
Dts_next_au[14..0]: occupies a position of 15bit;
Marker_bit: Occupies a position of 1bit;
stuffing_byte: Fill field, fixed to 0xFF;
payload_bytes: Payload field, bytes from PES package, psi part, etc.;
In short, these fields are a layer of layers, a field flag bit control a field, the flag bit is 1 o'clock, its flag field will exist;
2.PSI Program Special information table
TS header, is the content of the load payload, which can be PES packet data, can also be PSI information, PSI information mainly by PAT,PMT,CAT, etc., here mainly introduce Pat and PMT two kinds of information table, the information described above, The type of payload is determined by PID, General pid==0x0000 is payload pat,pid== 0x0001, then payload is cat, and PMT PID is specified in Pat;
The PSI may also have a special field:
point_field Field : Following the header, the placeholder 8bit, which is the payload, represents the number of bytes from the beginning of the field to the first byte of the PSI section in the payload; when payload_unit_start_indicator = = 1 o'clock, this field exists only, if Point_field = = 0x00, then this word is followed by the starting byte of the PSI section, this field is in the payload, the length of the payload is counted;
2.1.PAT: Program Association table
Pat contains the program number and the corresponding PMT PID number for each program, providing a correspondence between the program number and the PID of the TS Grouping (PMT grouping) that contains the program definition information (typically there is only one program (channel) in our TS stream, so there is usually only one PMT in Pat);
The entire table is made up of many fields, and the table may be transferred into multiple segmented sections, that is, Pat may be transferred over multiple TS packages, and Pat's data segment section consists of the following fields, in order:
table_id field: indicates the content of the PSI segment, occupying a bit of 8bit, when table_id = = 0x00, indicating that this segment is a pat fragment, when table_id = = 0x01, which indicates that this segment is a cat segment, when table_id = = 0x02, Indicates that this segment is a PMT fragment; in Pat, the value of ID is 0x00;
section_syntax_indicator field: occupies a position of 1bit, fixed position ' 1 ' ;
' 0 ' field: placeholder 1bit;
reserved field: reserved field, placeholder 2bit;
section_length field: segment length, placeholder 12bit, where the first 2 bits are fixed to ' 00 ' and the last 10 bits indicate the length of the following section field, including the length of the CRC;
transport_stream_id field: TS stream identification ID, which is used to identify this TS stream from other multiplexing in the network, and its value is defined by the user, occupying a bit of 16bit;
Reserved: reserved field, occupy bit 2bit;
version_number Field: The version number of the entire Pat, occupying 5bit; when Pat changes, its value is accumulated from 0 to 31, when current_next_indicator = = 1 o'clock, Version_ Number is the version of the current Pat, when current_next_indicator = = 0 o'clock, whose value is the version number of the next pat;
current_next_indicator field: indicator, placeholder 1bit, position 1 indicates that the current pat is valid, position 0 indicates that the current pat is invalid, the next pat is valid ;
section_number field: The ordinal of this segment section, the ordinal of the first segment section of the placeholder 8bit,pat should be 0x00, with each segment in Pat accumulating 1;
last_section_number field: The sequence number of the last segment section of Pat, which is the highest ordinal value , occupying a position of 8bit;
Loop:
program_number field: placeholder 16bit, indicating the number of programs available for PMT; if program_number = = 0x0000, then the next reference PID is the network PID, the other value is defined by the user In a pat version of Version_number, this value cannot take a single value more than once (that is, a Pat segment section can have only one program number and its PMT's PID correspondence);
Reserved: reserved field, occupy bit 3bit;
network_pid field: network PID, occupy bit 13bit, when program_number = = 0x0000, this field exists; indicates the PID value of TS packet containing the Network information table nit;
program_map_pid field: occupies a bit of 13bit when Program_number. = 0x0000 When this field exists, which represents the PID value of the PMT segmented TS packet available for the program indicated by Program_number; a program_number should not have multiple program_map_pid assignments, this PROGRAM_MAP_ The values of the PID are user-defined, but cannot be reserved for other purposes;
Loop end;
crc_32 field: CRC check value, placeholder