TSDuck v3.40-4034
MPEG Transport Stream Toolkit
|
Basic definition of an MPEG-2 transport packet. More...
#include <tsTSPacket.h>
Public Types | |
enum | { DUMP_RAW = 0x00010000 , DUMP_TS_HEADER = 0x00020000 , DUMP_PES_HEADER = 0x00040000 , DUMP_PAYLOAD = 0x00080000 , DUMP_AF = 0x00100000 } |
Options for packet display. More... | |
Public Member Functions | |
void | clearDiscontinuityIndicator () |
Clear discontinuity_indicator - 1 bit. | |
void | clearESPI () |
Clear elementary_stream_priority_indicator (ESPI) - 1 bit. | |
void | clearPriority () |
Clear transport_priority - 1 bit. | |
void | clearPUSI () |
Clear payload_unit_start_indicator (PUSI) - 1 bit. | |
void | clearRandomAccessIndicator () |
Clear random_access_indicator - 1 bit. | |
void | clearTEI () |
Clear transport_error_indicator (TEI) - 1 bit. | |
void | copyFrom (const void *source) |
Init packet from a memory area. | |
void | copyTo (void *dest) const |
Copy packet content to a memory area. | |
std::ostream & | display (std::ostream &strm, uint32_t flags=0, size_t indent=0, size_t size=PKT_SIZE) const |
This method displays the content of a transport packet. | |
size_t | getAFSize () const |
Compute adaptation_field (AF) size. | |
size_t | getAFStuffingSize () const |
Compute the size of the stuffing part in the adaptation_field. | |
uint8_t | getCC () const |
Extract continuity_counter (CC) - 4 bits. | |
bool | getDiscontinuityIndicator () const |
Check if packet has a discontinuity_indicator set - 1 bit. | |
uint64_t | getDTS () const |
Get the DTS - 33 bits. | |
bool | getESPI () const |
Check if packet has a elementary_stream_priority_indicator (ESPI) set - 1 bit. | |
size_t | getHeaderSize () const |
Compute the size of the TS header. | |
uint64_t | getOPCR () const |
Get the OPCR - 42 bits. | |
uint8_t * | getPayload () |
Get payload start address. | |
const uint8_t * | getPayload () const |
Get payload start address. | |
size_t | getPayloadSize () const |
Compute payload size. | |
uint64_t | getPCR () const |
Get the PCR - 42 bits. | |
size_t | getPESHeaderSize () const |
Get the size of the PES header in the packet, if one is present. | |
bool | getPESHeaderStuffingArea (const uint8_t *&addr, size_t &pes_size, size_t &ts_size) const |
Get the address and size of the stuffing area of the PES header in the TS packet. | |
bool | getPESHeaderStuffingArea (uint8_t *&addr, size_t &pes_size, size_t &ts_size) |
Get the address and size of the stuffing area of the PES header in the TS packet. | |
PID | getPID () const |
Extract PID - 13 bits. | |
bool | getPriority () const |
Extract transport_priority - 1 bit. | |
uint8_t * | getPrivateData () |
Get address of private data in adaptation field. | |
const uint8_t * | getPrivateData () const |
Get address of private data in adaptation field. | |
void | getPrivateData (ByteBlock &data) const |
Get private data from adaptation field. | |
size_t | getPrivateDataSize () const |
Get size in bytes of private data from adaptation field. | |
uint64_t | getPTS () const |
Get the PTS - 33 bits. | |
bool | getPUSI () const |
Extract payload_unit_start_indicator (PUSI) - 1 bit. | |
bool | getRandomAccessIndicator () const |
Check if packet has a random_access_indicator set - 1 bit. | |
uint8_t | getScrambling () const |
Extract transport_scrambling_control - 2 bits. | |
int8_t | getSpliceCountdown () const |
Get the splicing point countdown - 8 bits (signed). | |
bool | getTEI () const |
Extract transport_error_indicator (TEI) - 1 bit. | |
bool | hasAF () const |
Check if packet has an adaptation_field (AF) | |
bool | hasDTS () const |
Check if the TS packet contains a Decoding Time Stamp (DTS). | |
bool | hasOPCR () const |
Check if packet has an Original Program Clock Reference (OPCR) | |
bool | hasPayload () const |
Check if packet has a payload. | |
bool | hasPCR () const |
Check if packet has a Program Clock Reference (PCR) | |
bool | hasPrivateData () const |
Check if packet has private data in adaptation field. | |
bool | hasPTS () const |
Check if the TS packet contains a Presentation Time Stamp (PTS). | |
bool | hasSpliceCountdown () const |
Check if packet has splicing point countdown. | |
bool | hasValidSync () const |
Check if the sync byte is valid. | |
void | init (PID pid=PID_NULL, uint8_t cc=0, uint8_t data=0xFF) |
Initialize a TS packet. | |
bool | isClear () const |
Check if the packet is clear (ie not scrambled). | |
bool | isDuplicate (const TSPacket &other) const |
Check if this packet is a duplicate as another one. | |
bool | isScrambled () const |
Check if the packet is scrambled. | |
bool | operator== (const TSPacket &p) const |
Equality operator. | |
std::istream & | read (std::istream &strm, bool check_sync=true, Report &report=(ts::CerrReport::Instance())) |
Read a packet from standard streams (binary mode). | |
void | removeOPCR () |
Remove the Original Program Clock Reference (OPCR) from the packet, if there is one. | |
void | removePCR () |
Remove the Program Clock Reference (PCR) from the packet, if there is one. | |
void | removePrivateData () |
Remove the private data from the adaptation field, if there is one. | |
void | removeSpliceCountdown () |
Remove the splicing point countdown from the packet, if there is one. | |
bool | reserveStuffing (size_t size, bool shift_payload=false, bool enforce_af=false) |
Reserve some given space in the stuffing part of the adaptation field. | |
bool | samePayload (const TSPacket &other) const |
Check if this packet has the same payload as another one. | |
void | setCC (uint8_t cc) |
Set continuity_counter (CC) - 4 bits. | |
bool | setDiscontinuityIndicator (bool shift_payload=false) |
Set discontinuity_indicator - 1 bit. | |
void | setDTS (const uint64_t &dts) |
Replace the DTS value - 33 bits. | |
bool | setESPI (bool shift_payload=false) |
Set elementary_stream_priority_indicator (ESPI) - 1 bit. | |
bool | setOPCR (const uint64_t &opcr, bool shift_payload=false) |
Create or replace the OPCR value - 42 bits. | |
bool | setPayloadSize (size_t size, bool shift_payload=false, uint8_t pad=0xFF) |
Set the payload size. | |
bool | setPCR (const uint64_t &pcr, bool shift_payload=false) |
Create or replace the PCR value - 42 bits. | |
void | setPID (PID pid) |
Set PID - 13 bits. | |
void | setPriority () |
Set transport_priority - 1 bit. | |
void | setPriority (bool on) |
Set transport_priority - 1 bit. | |
bool | setPrivateData (const ByteBlock &data, bool shift_payload=false) |
Set private data in adaptation field. | |
bool | setPrivateData (const void *data, size_t size, bool shift_payload=false) |
Set private data in adaptation field. | |
void | setPTS (const uint64_t &pts) |
Replace the PTS value - 33 bits. | |
void | setPUSI () |
Set payload_unit_start_indicator (PUSI) - 1 bit. | |
void | setPUSI (bool on) |
Set payload_unit_start_indicator (PUSI) - 1 bit. | |
bool | setRandomAccessIndicator (bool shift_payload=false) |
Set random_access_indicator - 1 bit. | |
void | setScrambling (uint8_t sc) |
Set transport_scrambling_control - 2 bits. | |
bool | setSpliceCountdown (int8_t count, bool shift_payload=false) |
Create or replace the splicing point countdown - 8 bits. | |
void | setTEI () |
Set transport_error_indicator (TEI) - 1 bit. | |
void | setTEI (bool on) |
Set transport_error_indicator (TEI) - 1 bit. | |
bool | startPES () const |
Check if the packet contains the start of a clear PES header. | |
std::ostream & | write (std::ostream &strm, Report &report=(ts::CerrReport::Instance())) const |
Write a packet to standard streams (binary mode). | |
Static Public Member Functions | |
static void | Copy (TSPacket *dest, const TSPacket *source, size_t count=1) |
Copy contiguous TS packets. | |
static void | Copy (TSPacket *dest, const uint8_t *source, size_t count=1, size_t packet_size=PKT_SIZE) |
Copy contiguous TS packets from raw memory. | |
static void | Copy (uint8_t *dest, const TSPacket *source, size_t count=1, size_t packet_size=PKT_SIZE) |
Copy contiguous TS packets into raw memory. | |
static uint64_t | GetPCR (const uint8_t *b) |
This static method extracts a PCR from a stream. | |
static bool | Locate (const uint8_t *buffer, size_t buffer_size, size_t &start_index, size_t &packet_count, size_t &packet_size) |
Locate contiguous TS packets into a buffer. | |
static void | PutPCR (uint8_t *b, const uint64_t &pcr) |
This routine inserts a PCR in a stream. | |
static void | SanityCheck () |
Sanity check routine. | |
Public Attributes | |
uint8_t | b [PKT_SIZE] |
The public content is the 188-byte array representing the TS packet. | |
Static Public Attributes | |
static constexpr size_t | PCR_BYTES = 6 |
Size in bytes of a Program Clock Reference (PCR) as stored in a TS packet. | |
Basic definition of an MPEG-2 transport packet.
Physically, an object of this class is exactly implemented as a 188-byte TS packets. It is safe to consider that arrays or vectors of this class have the physical layout of a transport stream.
For performance reason, there is no constructor. Uninitialized packets have undefined binary content.
anonymous enum |
|
inline |
Equality operator.
[in] | p | Other packet to compare. |
Initialize a TS packet.
This method should be used when initializing with NullPacket or EmptyPacket is not appropriate. The packet payload is 184 bytes long and filled with the data byte.
[in] | pid | PID value. |
[in] | cc | Continuity counter. |
[in] | data | Byte value to fill the payload with. |
|
inline |
Check if the sync byte is valid.
|
inline |
Extract PID - 13 bits.
|
inline |
Set PID - 13 bits.
[in] | pid | The new PID. |
|
inline |
Extract payload_unit_start_indicator (PUSI) - 1 bit.
|
inline |
Set payload_unit_start_indicator (PUSI) - 1 bit.
[in] | on | The value to set. |
|
inline |
Extract transport_error_indicator (TEI) - 1 bit.
|
inline |
Set transport_error_indicator (TEI) - 1 bit.
[in] | on | The value to set. |
|
inline |
Extract transport_priority - 1 bit.
|
inline |
Set transport_priority - 1 bit.
[in] | on | The value to set. |
|
inline |
Extract transport_scrambling_control - 2 bits.
|
inline |
Check if the packet is clear (ie not scrambled).
|
inline |
Check if the packet is scrambled.
|
inline |
Set transport_scrambling_control - 2 bits.
[in] | sc | New transport_scrambling_control value. |
|
inline |
Extract continuity_counter (CC) - 4 bits.
|
inline |
Set continuity_counter (CC) - 4 bits.
[in] | cc | New continuity_counter value. |
|
inline |
Check if packet has an adaptation_field (AF)
|
inline |
Compute adaptation_field (AF) size.
size_t ts::TSPacket::getAFStuffingSize | ( | ) | const |
Compute the size of the stuffing part in the adaptation_field.
|
inline |
Compute the size of the TS header.
|
inline |
Check if packet has a payload.
|
inline |
Get payload start address.
|
inline |
Get payload start address.
|
inline |
Compute payload size.
bool ts::TSPacket::setPayloadSize | ( | size_t | size, |
bool | shift_payload = false , |
||
uint8_t | pad = 0xFF |
||
) |
Set the payload size.
If the payload shall be shrunk, the adaptation field is enlarged with stuffing. If the payload shall be enlarged, reduce the amount of stuffing in the adaptation field. This method should be used only when creating a packet from scratch, before filling the payload.
[in] | size | The requested payload size. |
[in] | shift_payload | If true, the payload is shifted so that the start of its content remains the same. When the payload is shrunk, its end is truncated. When the paylaod is enlarged, it is padded with pad values. When shift_payload is false, the data in the memory area of the payload is not modified. In that case, the memory is silently overwritten, losing the payload content. |
[in] | pad | Byte value to use when padding the adaptation field or payload. |
|
inline |
Check if packet has a discontinuity_indicator set - 1 bit.
|
inline |
Set discontinuity_indicator - 1 bit.
[in] | shift_payload | If true, the payload can be shifted and truncated when the adaptation field needs to be created. |
|
inline |
Check if packet has a random_access_indicator set - 1 bit.
|
inline |
Set random_access_indicator - 1 bit.
[in] | shift_payload | If true, the payload can be shifted and truncated when the adaptation field needs to be created. |
|
inline |
Check if packet has a elementary_stream_priority_indicator (ESPI) set - 1 bit.
|
inline |
Set elementary_stream_priority_indicator (ESPI) - 1 bit.
[in] | shift_payload | If true, the payload can be shifted and truncated when the adaptation field needs to be created. |
|
inline |
Check if packet has a Program Clock Reference (PCR)
uint64_t ts::TSPacket::getPCR | ( | ) | const |
Get the PCR - 42 bits.
bool ts::TSPacket::setPCR | ( | const uint64_t & | pcr, |
bool | shift_payload = false |
||
) |
Create or replace the PCR value - 42 bits.
[in] | pcr | The new PCR value. |
[in] | shift_payload | If true, the payload can be shifted and truncated when the adaptation field needs to be enlarged. |
|
inline |
Remove the Program Clock Reference (PCR) from the packet, if there is one.
The adaptation field size is unchanged, its stuffing part is enlarged.
|
inline |
Check if packet has an Original Program Clock Reference (OPCR)
uint64_t ts::TSPacket::getOPCR | ( | ) | const |
Get the OPCR - 42 bits.
bool ts::TSPacket::setOPCR | ( | const uint64_t & | opcr, |
bool | shift_payload = false |
||
) |
Create or replace the OPCR value - 42 bits.
[in] | opcr | The new OPCR value. |
[in] | shift_payload | If true, the payload can be shifted and truncated when the adaptation field needs to be enlarged. |
|
inline |
Remove the Original Program Clock Reference (OPCR) from the packet, if there is one.
The adaptation field size is unchanged, its stuffing part is enlarged.
|
static |
This static method extracts a PCR from a stream.
[in] | b | Address of a 6-byte memory area containing a PCR binary value. |
|
static |
This routine inserts a PCR in a stream.
[out] | b | Address of a 6-byte memory area to write the PCR binary value. |
[in] | pcr | A 42-bit PCR value. |
|
inline |
Check if packet has splicing point countdown.
int8_t ts::TSPacket::getSpliceCountdown | ( | ) | const |
Get the splicing point countdown - 8 bits (signed).
bool ts::TSPacket::setSpliceCountdown | ( | int8_t | count, |
bool | shift_payload = false |
||
) |
Create or replace the splicing point countdown - 8 bits.
[in] | count | The new splicing point countdown value. |
[in] | shift_payload | If true, the payload can be shifted and truncated when the adaptation field needs to be enlarged. |
|
inline |
Remove the splicing point countdown from the packet, if there is one.
The adaptation field size is unchanged, its stuffing part is enlarged.
|
inline |
Check if packet has private data in adaptation field.
size_t ts::TSPacket::getPrivateDataSize | ( | ) | const |
Get size in bytes of private data from adaptation field.
const uint8_t * ts::TSPacket::getPrivateData | ( | ) | const |
Get address of private data in adaptation field.
uint8_t * ts::TSPacket::getPrivateData | ( | ) |
Get address of private data in adaptation field.
void ts::TSPacket::getPrivateData | ( | ByteBlock & | data | ) | const |
Get private data from adaptation field.
[out] | data | Private data from adaptation field. |
bool ts::TSPacket::setPrivateData | ( | const void * | data, |
size_t | size, | ||
bool | shift_payload = false |
||
) |
Set private data in adaptation field.
[in] | data | Address of private data to set in the packet. |
[in] | size | Size in bytes of private data to set in the packet. |
[in] | shift_payload | If true, the payload can be shifted and truncated when the adaptation field needs to be created or enlarged. |
bool ts::TSPacket::setPrivateData | ( | const ByteBlock & | data, |
bool | shift_payload = false |
||
) |
Set private data in adaptation field.
[in] | data | Private data to set in the packet. |
[in] | shift_payload | If true, the payload can be shifted and truncated when the adaptation field needs to be created or enlarged. |
void ts::TSPacket::removePrivateData | ( | ) |
Remove the private data from the adaptation field, if there is one.
The adaptation field size is unchanged, its stuffing part is enlarged.
bool ts::TSPacket::reserveStuffing | ( | size_t | size, |
bool | shift_payload = false , |
||
bool | enforce_af = false |
||
) |
Reserve some given space in the stuffing part of the adaptation field.
[in] | size | The expected stuffing size in bytes, in the adaptation field. If the AF stuffing is already that size or larger, do nothing. Otherwise, attempt to increase the AF size by shifting and truncating the payload. |
[in] | shift_payload | If true, the payload can be shifted and truncated when the adaptation field needs to be enlarged. |
[in] | enforce_af | When true, try to create the AF anyway, even if size is zero, making sure that the flags field of the AF is present. |
bool ts::TSPacket::startPES | ( | ) | const |
Check if the packet contains the start of a clear PES header.
size_t ts::TSPacket::getPESHeaderSize | ( | ) | const |
Get the size of the PES header in the packet, if one is present.
bool ts::TSPacket::getPESHeaderStuffingArea | ( | const uint8_t *& | addr, |
size_t & | pes_size, | ||
size_t & | ts_size | ||
) | const |
Get the address and size of the stuffing area of the PES header in the TS packet.
[out] | addr | Address of the PES header stuffing area. This address points inside the TS packet payload. |
[out] | pes_size | Size in bytes of the PES header stuffing area. This is the complete size, some of which can be outside the TS packet. |
[out] | ts_size | Size in bytes of the PES header stuffing area which is in the TS packet. This size can be lower than the returned pes_size if the stuffing area continues in another TS packet. |
bool ts::TSPacket::getPESHeaderStuffingArea | ( | uint8_t *& | addr, |
size_t & | pes_size, | ||
size_t & | ts_size | ||
) |
Get the address and size of the stuffing area of the PES header in the TS packet.
[out] | addr | Address of the PES header stuffing area. This address points inside the TS packet payload. |
[out] | pes_size | Size in bytes of the PES header stuffing area. This is the complete size, some of which can be outside the TS packet. |
[out] | ts_size | Size in bytes of the PES header stuffing area which is in the TS packet. This size can be lower than the returned pes_size if the stuffing area continues in another TS packet. |
|
inline |
Check if the TS packet contains a Presentation Time Stamp (PTS).
Technically, the PTS and DTS are part of the PES packet, not the TS packet. If the TS packet is the first TS packet of a PES packet, it is possible that the PTS and/or DTS are present in the PES packet but outside the first TS packet. This is possible but rare. So, we provide here a fast way of getting PTS and/or DTS from the TS packet if available.
|
inline |
|
inline |
Get the PTS - 33 bits.
|
inline |
Get the DTS - 33 bits.
|
inline |
Replace the PTS value - 33 bits.
[in] | pts | The new PTS value. |
|
inline |
Replace the DTS value - 33 bits.
[in] | dts | The new DTS value. |
bool ts::TSPacket::samePayload | ( | const TSPacket & | other | ) | const |
Check if this packet has the same payload as another one.
[in] | other | The other packet to compare. |
bool ts::TSPacket::isDuplicate | ( | const TSPacket & | other | ) | const |
Check if this packet is a duplicate as another one.
A valid "true" pair of duplicate packets is made of two consecutive packets with same continuity counter and same payload. It must also have the same adaptation field, with the exception of the PCR which can (should?) be different.
[in] | other | The other packet to compare. |
std::istream & ts::TSPacket::read | ( | std::istream & | strm, |
bool | check_sync = true , |
||
Report & | report = (ts::CerrReport::Instance()) |
||
) |
Read a packet from standard streams (binary mode).
[in,out] | strm | A standard stream in input mode. |
[in] | check_sync | If true, the sync byte of the input packet is checked. If it is not valid, set the failbit of the stream. |
[in,out] | report | Where to report errors. |
std::ostream & ts::TSPacket::write | ( | std::ostream & | strm, |
Report & | report = (ts::CerrReport::Instance()) |
||
) | const |
Write a packet to standard streams (binary mode).
[in,out] | strm | A standard stream in output mode. |
[in,out] | report | Where to report errors. |
std::ostream & ts::TSPacket::display | ( | std::ostream & | strm, |
uint32_t | flags = 0 , |
||
size_t | indent = 0 , |
||
size_t | size = PKT_SIZE |
||
) | const |
This method displays the content of a transport packet.
[in,out] | strm | A standard stream in output mode (text mode). |
[in] | flags | Indicate which part must be dumped. If DUMP_RAW or DUMP_PAYLOAD is specified, flags from ts::UString::HexaFlags may also be used. |
[in] | indent | Indicates the base indentation of lines. |
[in] | size | Maximum size to display in the packet. |
void ts::TSPacket::copyFrom | ( | const void * | source | ) |
Init packet from a memory area.
[in] | source | Address of the memory area to read. Must contain at least PKT_SIZE bytes. |
void ts::TSPacket::copyTo | ( | void * | dest | ) | const |
Copy packet content to a memory area.
[out] | dest | Address of the memory area to write. Must contain at least PKT_SIZE bytes. |
|
static |
Copy contiguous TS packets.
[out] | dest | Address of the first contiguous TS packet to write. |
[in] | source | Address of the first contiguous TS packet to read. |
[in] | count | Number of TS packets to copy. |
|
static |
Copy contiguous TS packets from raw memory.
[out] | dest | Address of the first contiguous TS packet to write. |
[in] | source | Address of the memory area to read. |
[in] | count | Number of TS packets to copy. |
[in] | packet_size | Individual packet size in the source memory area. |
|
static |
Copy contiguous TS packets into raw memory.
[out] | dest | Address of the memory area to write. |
[in] | source | Address of the first contiguous TS packet to read. |
[in] | count | Number of TS packets to copy. |
[in] | packet_size | Individual packet size in the destination memory area. |
|
static |
Locate contiguous TS packets into a buffer.
This static method is typically used to locate useful packets in a UDP datagram. Basically, we expect the message to contain only TS packets. However, we also face the following situations:
To face the first situation, we look backward from the end of the message, looking for a 0x47 sync byte every 188 or 204 bytes, going backward.
If no TS packet is found using the first method, we restart from the beginning of the message, looking for a 0x47 sync byte every 188 or 204 bytes, going forward. If we find this pattern, followed by less than 188 or 204 bytes, then we have found a sequence of TS packets.
[in] | buffer | Address of a message buffer containing TS packets. |
[in] | buffer_size | Size in bytes of the buffer. |
[out] | start_index | Start index in bytes of the first TS packet in the buffer. |
[out] | packet_count | Number of TS packets in the buffer. |
[in,out] | packet_size | On input, indicate the previous packet size from the same source. If different from PKT_SIZE (188) and PKT_RS_SIZE (204), assume an unknown packet size. On output, if packets were found, it is set to the packet size. |
|
static |
Sanity check routine.
Ensure that the TSPacket structure can be used in contiguous memory array and array of packets. Can be used once at startup time in paranoid applications. Abort application on error.