An efficient TSDuck-specific TS packets encapsulation in a PID.
An instance of this class encapsulates several PID's from the input transport stream into one single output PID. Functionally, this is a subset of the features of T2-MI but much more lightweight and significantly faster to process.
Encapsulation format (plain)
In the output elementary stream (ES), all input TS packets are contiguous, without encapsulation. The initial 0x47 synchronization byte is removed. Only the remaining 187 bytes are encapsulated.
In the output PID, the packetization is similar to sections, with 187-bytes packets instead of sections. The Payload Unit Start Indicator (PUSI) bit is set in the header of TS packets containing the start of an encapsulated packet. When the PUSI bit is set, the first byte of the payload is a "pointer field" to the beginning of the first encapsulated packet.
Due to the overhead of the TS header, the number of output packets is slightly larger than the input packets. The input streams must contain a few null packets to absorb the extra output packets. For this reason, null packets (PID 0x1FFF) are never encapsulated.
Encapsulation format (PES)
When selecting the PES encapsulation the same plain elementary stream is used, but with a PES envelope. This reduces the payload size, but makes the outer encapsulation more transparent. The full overhead is around 14-20% of additional data.
The PES envelope uses a KLVA SMPTE-336M encapsulation to insert the inner payload into one private (testing) key. Each TS packet contains only one key, with a size no larger than the payload of one TS packet. So each PES packet fits into a single TS packet.
The SMPTE-336M encapsulation implemented can be either the asynchronous (without timestamps) or the synchronous (with PTS). The latter consumes more space (+10 bytes), and it's only useful when it's needed to remux the encapsulated stream with an external tool that requires to use PTS marks. No other advantages are provided.
Two variant strategies are implemented. The FIXED mode uses the short (7-bit) BER encoding. This limits the PES payload to a maximum of 127 bytes. And the adaptation field of the outer packet is enlarged with some stuff. However, the advantage is that the PES is sufficient small to include more data in the outer TS packet. This reduces the possibility than some external processing will split the outer packet in two to accommodate the entire PES data.
The VARIABLE mode does not impose this restriction and outer packets are filled to the maximum. The drawback is that sometimes the long form of BER encoding is used with two bytes and others the short form with one byte. Furthermore, this increases the chances that some external processing occupies two outer packets for the same inner PES packet. Still, support for those split PES packets is included. The only requirement is that the 26|27 PES+KLVA header is inserted in the first packet (with PUSI on). The remaining payload can be distributed in the following TS packets.
The PES envelope has an overhead of 26|27|36|37 bytes based on:
- 9 bytes for the PES header.
- 0|5 bytes for the PTS (only in synchronous mode)
- 0|5 bytes for the Metadata AU Header (only in synchronous mode)
- 16 bytes for the UL key
- 1|2 bytes for the payload size (BER short or long format)
To enable the use of the Synchronous encapsulation is required to use PCRs and provide one offset. This value (positive or negative) will be added to the PCR to compute the PTS. Recommended values are between -90000 and +90000 (-1,+1 second). If you use negative values then you can restore in advance the encapsulated stream after remuxing. However, this will be valid only if you use an external tool to remux. If you're unsure, then don't enable it.
A warning about the Synchronous mode: At start the PTS marks can't be in synch with the target pcr-pid. This is because the PCR value isn't read at start. But the PTS is required to be in all PES packets of the encapsulation. So, outer packets are delayed until valid PTS values can be computed. If too many initial packets need to be delayed, the first ones are discarded.
In order to correctly identify the encapsulated PES stream, it is recommended to include in the PMT table a format identifier descriptor for "KLVA" (0x4B4C5641); and use the associated metadata for the stream type based on the selected Sync/Async mode:
- Asynchronous mode: Private Type (0x06)
- Synchronous mode: Metadata Type (0x15)
Example (Asynchronous):
tsp ...
-P encap -o 7777 --pes-mode ...
-P pmt -s 100 -a 7777/0x06 --add-programinfo-id 0x4B4C5641
...
where the outer PID is 7777 and the attached service is 100.
Example (Synchronous):
tsp ...
-P encap -o 7777 --pes-mode ... --pcr-pid 101 --pes-offset=-50000
-P pmt -s 100 -a 7777/0x15 --add-programinfo-id 0x4B4C5641
...
where the outer PID is 7777, the pid 101 carries the PCR of the service, the attached service is 100 and the PTS is advanced around half second.
- See also
- https://impleotv.com/2017/02/17/klv-encoded-metadata-in-stanag-4609-streams/