avcpp 3.2.1
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
av::Packet Class Reference

Compressed data and its metadata. More...

#include <packet.h>

Inheritance diagram for av::Packet:

Classes

struct  wrap_data
 Wrap data and take owning. More...
struct  wrap_data_static
 Wrap static data, do not owning and free. More...

Public Member Functions

 Packet (std::nullptr_t)
 Create an empty packet without allocating.
 Packet ()
 Create an empty, valid packet with an allocated FFmpeg structure.
 Packet (const Packet &packet, OptionalErrorCode ec)
 Copy a packet as a reference to the same data.
 Packet (const Packet &packet)
 Copy a packet as a reference to the same data.
 Packet (Packet &&packet)
 Move a packet.
 Packet (const AVPacket *packet, OptionalErrorCode ec=throws())
 Wrap an FFmpeg packet.
 Packet (const std::vector< uint8_t > &data)
 Copy a byte vector into the packet payload.
 Packet (const uint8_t *data, size_t size, bool doAllign=true)
 Copy a memory block into the packet payload.
 Packet (uint8_t *data, size_t size, wrap_data, OptionalErrorCode ec=throws())
 Take ownership of a buffer allocated with the FFmpeg allocator.
 Packet (uint8_t *data, size_t size, wrap_data_static, OptionalErrorCode ec=throws())
 Wrap static data without taking ownership.
 Packet (std::span< const uint8_t > data, bool doAllign=true)
 Copy a span into the packet payload.
 Packet (std::span< uint8_t > data, wrap_data, OptionalErrorCode ec=throws())
 Take ownership of a span allocated with the FFmpeg allocator.
 Packet (std::span< uint8_t > data, wrap_data_static, OptionalErrorCode ec=throws())
 Wrap static data from a span without taking ownership.
bool setData (const std::vector< uint8_t > &newData, OptionalErrorCode ec=throws())
 Replace the payload with a copy of a byte vector.
bool setData (const uint8_t *newData, size_t size, OptionalErrorCode ec=throws())
 Replace the payload with a copy of a memory block.
const uint8_t * data () const
 Read access to the payload.
uint8_t * data ()
 Write access to the payload.
bool setData (std::span< const uint8_t > newData, OptionalErrorCode ec=throws())
 Replace the payload with a copy of a span.
std::span< const uint8_t > span () const
 Read-only view of the payload.
std::span< uint8_t > span ()
 Mutable view of the payload.
Timestamp pts () const
 Presentation timestamp.
Timestamp dts () const
 Decoding timestamp.
Timestamp ts () const
 Timestamp FFmpeg would use for display, falling back to the decoding one.
size_t size () const
 Payload size.
attribute_deprecated void setPts (int64_t pts, const Rational &tsTimeBase=Rational(0, 0))
 Set the presentation timestamp.
attribute_deprecated void setDts (int64_t dts, const Rational &tsTimeBase=Rational(0, 0))
 Set the decoding timestamp.
void setPts (const Timestamp &pts)
 Set the presentation timestamp.
void setDts (const Timestamp &dts)
 Set the decoding timestamp.
int streamIndex () const
 Index of the stream the packet belongs to.
bool isKeyPacket () const
 Whether the packet is a key frame.
int duration () const
 Duration of the packet, in time base units.
bool isComplete () const
 Whether the packet is marked complete.
bool isNull () const
 Whether the packet has no payload.
void setStreamIndex (int idx)
 Set the stream index.
void setKeyPacket (bool keyPacket)
 Mark the packet as a key frame.
void setDuration (int duration, const Rational &durationTimeBase=Rational(0, 0))
 Set the packet duration.
void setComplete (bool complete)
 Mark the packet as complete.
int flags () const
 Raw AV_PKT_FLAG_* mask.
void setFlags (int flags)
 Replace the AV_PKT_FLAG_* mask.
void addFlags (int flags)
 Add flags.
void clearFlags (int flags)
 Remove flags.
void dump (const Stream &st, bool dumpPayload=false) const
 Print the packet through the FFmpeg log system.
const Rational & timeBase () const
 Time base of pts() and dts().
void setTimeBase (const Rational &value)
 Set the time base used by pts() and dts().
std::span< const uint8_t > sideData (AVPacketSideDataType type) const
 Read-only access to the side data of the given type.
std::span< uint8_t > sideData (AVPacketSideDataType type)
 Mutable access to the side data of the given type.
std::size_t sideDataCount () const noexcept
 Return count of the side data elements.
PacketSideData sideDataIndex (std::size_t index) noexcept
 Get side data element by index.
ArrayView< AVPacketSideData, PacketSideData, std::size_t > sideData () noexcept
 Observe all packet side data via iterators.
ArrayView< const AVPacketSideData, PacketSideData, std::size_t > sideData () const noexcept
 Read-only iteration over all side data entries.
void freeSideData () noexcept
 Remove and free all side data entries.
void addSideData (AVPacketSideDataType type, std::span< const uint8_t > data, OptionalErrorCode ec=throws())
 Add side data of the given type into packet.
void addSideData (AVPacketSideDataType type, std::span< uint8_t > data, wrap_data, OptionalErrorCode ec=throws())
 Add side data of the given type into packet.
std::span< uint8_t > allocateSideData (AVPacketSideDataType type, std::size_t size, OptionalErrorCode ec=throws())
 Allocate storage for the packet side data of the given type and return reference to it.
bool isReferenced () const
 Whether the payload is reference counted.
int refCount () const
 Number of references to the payload.
AVPacket * makeRef (OptionalErrorCode ec) const
 Create an independent reference to the packet.
Packet clone (OptionalErrorCode ec=throws()) const
 Deep copy of the packet, payload included.
Packet & operator= (const Packet &rhs)
 Assign by referencing the other packet's payload.
Packet & operator= (Packet &&rhs)
 Move assignment.
Packet & operator= (const AVPacket *rhs)
 Assign from an FFmpeg packet.
void swap (Packet &other)
 Exchange the contents of two packets.
 operator bool () const
 Whether the packet is complete.
Public Member Functions inherited from FFWrapperPtr< AVPacket >
 FFWrapperPtr ()=default
 Create an empty wrapper; isNull() is true.
const AVPacket * raw () const
 Read access to the wrapped object.
void reset (AVPacket *raw=nullptr)
 Replace the stored pointer.
bool isNull () const
 Whether the wrapper is empty.
void _log (int level, const char *fmt) const
 Log through the FFmpeg log system, using this wrapper as the context.

Additional Inherited Members

Protected Attributes inherited from FFWrapperPtr< AVPacket >
AVPacket * m_raw
 The wrapped pointer; owned by the derived class, not by this template.

Detailed Description

A packet belongs to exactly one stream: it carries the stream index, the presentation and decoding timestamps, the duration and the flags of one compressed unit. Reading gives packets, writing consumes them; decoding turns them into frames and encoding turns frames back into packets.

Buffers are allocated with the FFmpeg allocator and always carry the zeroed AV_INPUT_BUFFER_PADDING_SIZE bytes after the payload. This is not optional: some muxers and parsers read payloads as C strings, and the ASS muxer does exactly that.

Examples
api2-decode-audio.cpp, api2-decode-encode-audio.cpp, api2-decode-encode-video.cpp, api2-decode-filter-encode.cpp, api2-decode-overlay-encode.cpp, api2-decode-rasample-audio.cpp, api2-decode-raw-h264.cpp, api2-decode.cpp, api2-generate-subtitles.cpp, api2-hw-decode.cpp, api2-hw-encode.cpp, api2-hw-scale.cpp, api2-hw-transcode.cpp, api2-scale-video.cpp, api2-send-receive.cpp, and api2-subtitles.cpp.

Constructor & Destructor Documentation

◆ Packet() [1/12]

av::Packet::Packet ( std::nullptr_t )
explicit
Note
No checks are performed, unlike Packet(); the packet is invalid until data is set.

◆ Packet() [2/12]

av::Packet::Packet ( const Packet & packet,
OptionalErrorCode ec )
Parameters
packetthe packet to reference
echolder for the error code

◆ Packet() [3/12]

av::Packet::Packet ( const Packet & packet)
Parameters
packetthe packet to reference

◆ Packet() [4/12]

av::Packet::Packet ( Packet && packet)
Parameters
packetthe packet to move from; left empty

◆ Packet() [5/12]

av::Packet::Packet ( const AVPacket * packet,
OptionalErrorCode ec = throws() )
explicit

The data is referenced, not copied.

Parameters
packetthe packet to wrap; null produces an empty packet
echolder for the error code

◆ Packet() [6/12]

av::Packet::Packet ( const std::vector< uint8_t > & data)
explicit
Parameters
datathe payload to copy

◆ Packet() [7/12]

av::Packet::Packet ( const uint8_t * data,
size_t size,
bool doAllign = true )
Parameters
datathe payload
sizepayload size in bytes
doAlligntrue appends the zeroed padding FFmpeg requires after the payload; disabling it is only safe when the buffer already provides it

◆ Packet() [8/12]

av::Packet::Packet ( uint8_t * data,
size_t size,
wrap_data ,
OptionalErrorCode ec = throws() )
Parameters
datathe buffer; it must come from av_malloc() or av::malloc()
sizepayload size in bytes
echolder for the error code
Note
The buffer must contain the zeroed padding after the payload, otherwise allocate size + AV_INPUT_BUFFER_PADDING_SIZE bytes.

◆ Packet() [9/12]

av::Packet::Packet ( uint8_t * data,
size_t size,
wrap_data_static ,
OptionalErrorCode ec = throws() )
Parameters
datathe buffer; it must outlive the packet
sizepayload size in bytes
echolder for the error code
Note
The buffer must be size + AV_INPUT_BUFFER_PADDING_SIZE bytes and the padding must be zeroed.

◆ Packet() [10/12]

av::Packet::Packet ( std::span< const uint8_t > data,
bool doAllign = true )
explicit
Parameters
datathe payload
doAlligntrue appends the zeroed padding FFmpeg requires

◆ Packet() [11/12]

av::Packet::Packet ( std::span< uint8_t > data,
wrap_data ,
OptionalErrorCode ec = throws() )
Parameters
datathe buffer; it must come from av_malloc() or av::malloc() and must contain the zeroed AV_INPUT_BUFFER_PADDING_SIZE bytes after the payload
echolder for the error code

◆ Packet() [12/12]

av::Packet::Packet ( std::span< uint8_t > data,
wrap_data_static ,
OptionalErrorCode ec = throws() )
Parameters
datathe buffer; it must outlive the packet and include the zeroed FFmpeg padding
echolder for the error code

Member Function Documentation

◆ addFlags()

void av::Packet::addFlags ( int flags)
Parameters
flagsthe bits to set

◆ addSideData() [1/2]

void av::Packet::addSideData ( AVPacketSideDataType type,
std::span< const uint8_t > data,
OptionalErrorCode ec = throws() )

Data will be cloned.

Parameters
type
data
ec

◆ addSideData() [2/2]

void av::Packet::addSideData ( AVPacketSideDataType type,
std::span< uint8_t > data,
wrap_data ,
OptionalErrorCode ec = throws() )

Data will be wrapped and owner-shipping taken. Data must be allocated via av_malloc()/av::malloc() family functions.

Parameters
type
data
ec

◆ allocateSideData()

std::span< uint8_t > av::Packet::allocateSideData ( AVPacketSideDataType type,
std::size_t size,
OptionalErrorCode ec = throws() )

Data owned by the packet.

Parameters
type
size
ec
Returns

◆ clearFlags()

void av::Packet::clearFlags ( int flags)
Parameters
flagsthe bits to clear

◆ clone()

Packet av::Packet::clone ( OptionalErrorCode ec = throws()) const
Parameters
echolder for the error code
Returns
an independent packet, or an empty one on failure

◆ data() [1/2]

uint8_t * av::Packet::data ( )
Returns
the payload pointer, or nullptr for an empty packet
Note
Writing into the buffer of a shared packet changes the data seen by every reference to it; call clone() first when that matters.

◆ data() [2/2]

const uint8_t * av::Packet::data ( ) const
Returns
the payload pointer, or nullptr for an empty packet
Examples
api2-decode-encode-audio.cpp.

◆ dts()

◆ dump()

void av::Packet::dump ( const Stream & st,
bool dumpPayload = false ) const
Parameters
stthe stream the packet belongs to; used to convert timestamps to seconds
dumpPayloadtrue also dumps the payload as hex

◆ duration()

int av::Packet::duration ( ) const
Returns
the duration

◆ flags()

int av::Packet::flags ( ) const
Returns
the flags of the packet
Examples
api2-decode-encode-audio.cpp.

◆ isComplete()

bool av::Packet::isComplete ( ) const
Returns
the completeness flag, set by the reader or by setComplete()

◆ isKeyPacket()

bool av::Packet::isKeyPacket ( ) const
Returns
the AV_PKT_FLAG_KEY state

◆ isNull()

bool av::Packet::isNull ( ) const
Returns
true when the data pointer is null or the size is zero

◆ isReferenced()

bool av::Packet::isReferenced ( ) const
Returns
true when the data is shared through AVBufferRef

◆ makeRef()

AVPacket * av::Packet::makeRef ( OptionalErrorCode ec) const
Parameters
echolder for the error code
Returns
a new AVPacket that shares the payload and must be freed by the caller, or nullptr on failure

◆ operator bool()

av::Packet::operator bool ( ) const
inline
Returns
the completeness flag, as returned by isComplete()

◆ operator=() [1/3]

Packet & av::Packet::operator= ( const AVPacket * rhs)
Parameters
rhsthe packet to reference; null empties this one
Returns
this packet

◆ operator=() [2/3]

Packet & av::Packet::operator= ( const Packet & rhs)
Parameters
rhsthe packet to assign from
Returns
this packet

◆ operator=() [3/3]

Packet & av::Packet::operator= ( Packet && rhs)
Parameters
rhsthe packet to move from; left empty
Returns
this packet

◆ pts()

◆ refCount()

int av::Packet::refCount ( ) const
Returns
the reference count, or zero when the packet is not referenced

◆ setComplete()

void av::Packet::setComplete ( bool complete)
Parameters
completetrue when the payload holds a whole compressed unit

◆ setData() [1/3]

bool av::Packet::setData ( const std::vector< uint8_t > & newData,
OptionalErrorCode ec = throws() )
Parameters
newDatathe new payload
echolder for the error code
Returns
true when the payload was replaced

◆ setData() [2/3]

bool av::Packet::setData ( const uint8_t * newData,
size_t size,
OptionalErrorCode ec = throws() )

The packet allocates size + AV_INPUT_BUFFER_PADDING_SIZE bytes and zeroes the padding itself.

Parameters
newDatathe new payload
sizepayload size in bytes
echolder for the error code
Returns
true when the payload was replaced

◆ setData() [3/3]

bool av::Packet::setData ( std::span< const uint8_t > newData,
OptionalErrorCode ec = throws() )
Parameters
newDatathe new payload
echolder for the error code
Returns
true when the payload was replaced

◆ setDts() [1/2]

void av::Packet::setDts ( const Timestamp & dts)
Parameters
dtsthe timestamp; its time base replaces the stored one

◆ setDts() [2/2]

attribute_deprecated void av::Packet::setDts ( int64_t dts,
const Rational & tsTimeBase = Rational(0, 0) )
Deprecated
Pass a Timestamp instead, so the time base travels with the value.
Parameters
dtsnew decoding timestamp value
tsTimeBasetime base of the value; the zero value means the packet time base

◆ setDuration()

void av::Packet::setDuration ( int duration,
const Rational & durationTimeBase = Rational(0, 0) )
Parameters
durationduration in durationTimeBase units
durationTimeBasetime base of the value; the default zero value means "the packet time base"

◆ setFlags()

void av::Packet::setFlags ( int flags)
Parameters
flagsthe new mask

◆ setKeyPacket()

void av::Packet::setKeyPacket ( bool keyPacket)
Parameters
keyPacketthe new value of the flag

◆ setPts() [1/2]

void av::Packet::setPts ( const Timestamp & pts)
Parameters
ptsthe timestamp; its time base replaces the stored one

◆ setPts() [2/2]

attribute_deprecated void av::Packet::setPts ( int64_t pts,
const Rational & tsTimeBase = Rational(0, 0) )
Deprecated
Pass a Timestamp instead, so the time base travels with the value.
Parameters
ptsnew presentation timestamp value
tsTimeBasetime base of the value; the zero value means the packet time base

◆ setStreamIndex()

void av::Packet::setStreamIndex ( int idx)

Must be set before writing the packet into a container, and remapped when input and output streams differ.

Parameters
idxindex of the stream
Examples
api2-decode-encode-audio.cpp, api2-decode-encode-video.cpp, api2-decode-filter-encode.cpp, api2-decode-overlay-encode.cpp, api2-generate-subtitles.cpp, api2-hw-encode.cpp, api2-hw-scale.cpp, api2-hw-transcode.cpp, and api2-scale-video.cpp.

◆ setTimeBase()

void av::Packet::setTimeBase ( const Rational & value)
Parameters
valuetimestamp units per second

◆ sideData() [1/4]

ArrayView< const AVPacketSideData, PacketSideData, std::size_t > av::Packet::sideData ( ) const
noexcept
Returns
a non-owning range valid while the packet is alive

◆ sideData() [2/4]

ArrayView< AVPacketSideData, PacketSideData, std::size_t > av::Packet::sideData ( )
noexcept

Applicable for range-based for and std::range:xxx algos.

Returns

◆ sideData() [3/4]

std::span< uint8_t > av::Packet::sideData ( AVPacketSideDataType type)
Parameters
typethe AV_PKT_DATA_* value to look for
Returns
the payload, empty when the packet has none

◆ sideData() [4/4]

std::span< const uint8_t > av::Packet::sideData ( AVPacketSideDataType type) const
Parameters
typethe AV_PKT_DATA_* value to look for
Returns
the payload, empty when the packet has none

◆ sideDataCount()

std::size_t av::Packet::sideDataCount ( ) const
noexcept
Returns

◆ sideDataIndex()

PacketSideData av::Packet::sideDataIndex ( std::size_t index)
noexcept
Parameters
index
Returns

◆ size()

size_t av::Packet::size ( ) const
Returns
the number of payload bytes, without the padding
Examples
api2-decode-encode-audio.cpp.

◆ span() [1/2]

std::span< uint8_t > av::Packet::span ( )
Returns
the bytes, without the padding

◆ span() [2/2]

std::span< const uint8_t > av::Packet::span ( ) const
Returns
the bytes, without the padding

◆ streamIndex()

◆ swap()

void av::Packet::swap ( Packet & other)
Parameters
otherthe packet to swap with

◆ timeBase()

◆ ts()

Timestamp av::Packet::ts ( ) const
Returns
pts when it is set, otherwise dts

The documentation for this class was generated from the following file: