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

One video frame. More...

#include <frame.h>

Inheritance diagram for av::VideoFrame:

Public Member Functions

 VideoFrame ()=default
 Create an invalid frame.
 VideoFrame (PixelFormat pixelFormat, int width, int height, int align=1)
 Allocate an empty frame for the given format and size.
 VideoFrame (const uint8_t *data, size_t size, PixelFormat pixelFormat, int width, int height, int align=1)
 Copy external image data into a frame.
 VideoFrame (const VideoFrame &other)
 Copy constructor: references the same frame buffer.
 VideoFrame (VideoFrame &&other)
 Move constructor.
VideoFrame & operator= (const VideoFrame &rhs)
 Copy assignment: references the buffer of the other frame.
VideoFrame & operator= (VideoFrame &&rhs)
 Move assignment.
PixelFormat pixelFormat () const
 Pixel format of the frame.
int width () const
 Frame width.
int height () const
 Frame height.
PixelFormat adjustFromJpegPixelFormat ()
 Convert JPEG pixel format to the compatible non-JPEG one.
PixelFormat adjustToJpegPixelFormat ()
 Convert non-JPEG pixel format to the compatible JPEG one.
bool isKeyFrame () const
 Whether the frame is a key frame.
void setKeyFrame (bool isKey)
 Mark the frame as a key frame.
int quality () const
 Quality value attached to the frame.
void setQuality (int quality)
 Attach a quality value to the frame.
AVPictureType pictureType () const
 Picture type assigned by the decoder or expected by the encoder.
void setPictureType (AVPictureType type=AV_PICTURE_TYPE_NONE)
 Set the picture type.
Rational sampleAspectRatio () const
 Pixel aspect ratio of the frame.
void setSampleAspectRatio (const Rational &sampleAspectRatio)
 Set the pixel aspect ratio.
size_t bufferSize (int align=1, OptionalErrorCode ec=throws()) const
 Size of the buffer needed to hold the frame in one contiguous block.
bool copyToBuffer (uint8_t *dst, size_t size, int align=1, OptionalErrorCode ec=throws())
 Copy the image into a caller-provided buffer.
bool copyToBuffer (std::vector< uint8_t > &dst, int align=1, OptionalErrorCode ec=throws())
 Copy the image into a vector, resizing it as needed.
VideoFrame clone (size_t align=1, OptionalErrorCode ec=throws()) const
 Deep copy of a software frame.
VideoFrame toSystemMemory (PixelFormat format=AV_PIX_FMT_NONE, OptionalErrorCode ec=throws()) const
 Transfer a hardware frame into system memory.
VideoFrame toHwFrame (const hw::FramesContext &frames, OptionalErrorCode ec=throws()) const
 Upload a software frame into hardware memory.
VideoFrame mapTo (const hw::FramesContext &frames, int flags=AV_HWFRAME_MAP_READ, OptionalErrorCode ec=throws()) const
 Map this hardware frame into another hardware frames context.
hw::FramesContext hwFramesContext () const noexcept
 Frames context of this hardware frame, or an invalid context.
hw::DeviceContext hwDeviceContext () const noexcept
 Device context used by this hardware frame, or an invalid context.
Public Member Functions inherited from av::Frame< VideoFrame >
 FrameCommon ()
 Create an invalid frame.
 FrameCommon (const AVFrame *frame)
 Copy the frame metadata into a new wrapper.
 FrameCommon (const FrameCommon &other)
 Copy constructor: references the same buffer.
 FrameCommon (FrameCommon &&other)
 Move constructor.
VideoFrame clone (size_t align=1) const
 Deep copy of the frame.
Public Member Functions inherited from av::FrameCommon
 FrameCommon ()
 Create an invalid frame.
 FrameCommon (const AVFrame *frame)
 Copy the frame metadata into a new wrapper.
 FrameCommon (const FrameCommon &other)
 Copy constructor: references the same buffer.
 FrameCommon (FrameCommon &&other)
 Move constructor.
bool isReferenced () const
 Whether the frame data is reference counted.
int refCount () const
 Number of references to the frame buffer.
AVFrame * makeRef () const
 Create an independent reference to the frame.
Timestamp pts () const
 Presentation timestamp.
attribute_deprecated void setPts (int64_t pts, Rational ptsTimeBase)
 Set the timestamp and its time base at once.
void setPts (const Timestamp &ts)
 Set the timestamp.
const Rational & timeBase () const
 Time base of the frame.
void setTimeBase (const Rational &value)
 Set the time base.
int streamIndex () const
 Index of the stream the frame belongs to.
void setStreamIndex (int streamIndex)
 Set the stream index.
void setComplete (bool isComplete)
 Mark the frame as complete or incomplete.
bool isComplete () const
 Whether the frame is marked complete.
bool isValid () const
 Whether the wrapper holds a frame.
 operator bool () const
 Whether the wrapper holds a frame.
bool isHwFrame () const
 Whether the frame lives in device memory.
uint8_t * data (size_t plane=0)
 Pointer to one image or audio plane.
const uint8_t * data (size_t plane=0) const
 Pointer to one plane, read-only.
size_t size (size_t plane) const
 Size of one plane.
size_t size () const
 Total size of all planes.
void dump () const
 Print the frame layout through the FFmpeg log system.
void operator= (const FrameCommon &)=delete
 Assignment is deleted: derived classes implement it to avoid slicing.
void swap (FrameCommon &other)
 Exchange the contents of two frames.
const FrameSideData sideData (AVFrameSideDataType type) const
 Get packet side data of the given type.
FrameSideData sideData (AVFrameSideDataType type)
 Mutable access to the side data of the given type.
std::size_t sideDataCount () const noexcept
 Return count of the side data elements.
FrameSideData sideDataIndex (std::size_t index) noexcept
 Get side data element by index.
ArrayView< AVFrameSideData *, FrameSideData, std::size_t > sideData () noexcept
 Observe all packet side data via iterators.
ArrayView< const AVFrameSideData *, FrameSideData, std::size_t > sideData () const noexcept
 Read-only iteration over all side data entries.
void sideDataRemove (AVFrameSideDataType type) noexcept
 Remove and free all side data instances of the given type.
FrameSideData addSideData (AVFrameSideDataType type, std::span< const uint8_t > data, OptionalErrorCode ec=throws())
 Add side data of the given type into packet.
FrameSideData addSideData (AVFrameSideDataType type, std::span< uint8_t > data, wrap_data, OptionalErrorCode ec=throws())
 Add side data of the given type into packet.
FrameSideData addSideData (AVFrameSideDataType type, std::span< uint8_t > data, wrap_data_static, OptionalErrorCode ec=throws())
 Add side data of the given type into packet.
FrameSideData addSideData (AVFrameSideDataType type, BufferRef buf, OptionalErrorCode ec=throws())
 Add side data of the given type into packet.
FrameSideData allocateSideData (AVFrameSideDataType type, std::size_t size, OptionalErrorCode ec=throws())
 Allocate storage for the packet side data of the given type and return reference to it.
Public Member Functions inherited from FFWrapperPtr< AVFrame >
 FFWrapperPtr ()=default
 Create an empty wrapper; isNull() is true.
const AVFrame * raw () const
 Read access to the wrapped object.
void reset (AVFrame *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.

Static Public Member Functions

static VideoFrame wrap (const void *data, size_t size, PixelFormat pixelFormat, int width, int height, int align=1)
 Wrap external data into VideoFrame object ready to use with FFmpeg/AvCpp.
static VideoFrame wrap (std::span< const std::byte > data, PixelFormat pixelFormat, int width, int height, int align=1)
 Wrap external data into VideoFrame object ready to use with FFmpeg/AvCpp.
static VideoFrame wrap (std::span< const std::byte > data, void(*deleter)(void *opaque, uint8_t *data), void *opaque, PixelFormat pixelFormat, int width, int height, int align=1)
 Wrap external data into VideoFrame object ready to use with FFmpeg/AvCpp.
Static Public Member Functions inherited from av::Frame< VideoFrame >
static VideoFrame null ()
 An invalid frame of this type.

Additional Inherited Members

Protected Member Functions inherited from av::Frame< VideoFrame >
VideoFrame & assignOperator (const VideoFrame &rhs)
 Copy the other frame into this one.
VideoFrame & moveOperator (VideoFrame &&rhs)
 Move the other frame into this one.
Protected Member Functions inherited from av::FrameCommon
void copyInfoFrom (const FrameCommon &other)
 Copy the avcpp bookkeeping (time base, stream index, completeness).
void clone (FrameCommon &dst, size_t align=1) const
 Make a deep copy of the frame data into another frame object.
Protected Attributes inherited from av::FrameCommon
Rational m_timeBase {}
 Time base of the timestamps in this frame.
int m_streamIndex {-1}
 Index of the stream the frame belongs to; -1 when unset.
bool m_isComplete {false}
 Completeness flag, set through setComplete().
Protected Attributes inherited from FFWrapperPtr< AVFrame >
AVFrame * m_raw
 The wrapped pointer; owned by the derived class, not by this template.

Detailed Description

The frame can live in system memory, where data[] is readable, or in device memory, where it is only a descriptor for a hardware surface; isHwFrame() tells the two apart and toSystemMemory() moves between them.

Examples
api2-decode-filter-encode.cpp, api2-decode-overlay-encode.cpp, api2-decode-raw-h264.cpp, api2-decode.cpp, api2-hw-decode.cpp, api2-hw-encode.cpp, api2-hw-scale.cpp, and api2-hw-transcode.cpp.

Constructor & Destructor Documentation

◆ VideoFrame() [1/4]

av::VideoFrame::VideoFrame ( PixelFormat pixelFormat,
int width,
int height,
int align = 1 )
Parameters
pixelFormatpixel format of the frame
widthwidth in pixels
heightheight in pixels
alignalignment in bytes for each plane

◆ VideoFrame() [2/4]

av::VideoFrame::VideoFrame ( const uint8_t * data,
size_t size,
PixelFormat pixelFormat,
int width,
int height,
int align = 1 )
Parameters
databuffer holding the image
sizesize of the buffer in bytes
pixelFormatpixel format of the data
widthwidth in pixels
heightheight in pixels
alignalignment in bytes for each plane

◆ VideoFrame() [3/4]

av::VideoFrame::VideoFrame ( const VideoFrame & other)
Parameters
otherthe frame to copy

◆ VideoFrame() [4/4]

av::VideoFrame::VideoFrame ( VideoFrame && other)
Parameters
otherthe frame to move from; left invalid

Member Function Documentation

◆ adjustFromJpegPixelFormat()

PixelFormat av::VideoFrame::adjustFromJpegPixelFormat ( )

YUV Pixel formats with J in the naming (AV_PIX_FMT_YUVJxxx) mostly deprecated and some components warn about it. Allow to adjust internal pixel format into compatible one.

Returns
previous pixel format

◆ adjustToJpegPixelFormat()

PixelFormat av::VideoFrame::adjustToJpegPixelFormat ( )

YUV Pixel formats with J in the naming (AV_PIX_FMT_YUVJxxx) mostly deprecated and some components warn about it. Allow to adjust internal pixel format into compatible one.

Returns
previous pixel format

◆ bufferSize()

size_t av::VideoFrame::bufferSize ( int align = 1,
OptionalErrorCode ec = throws() ) const
Parameters
alignalignment in bytes for each plane
echolder for the error code
Returns
the required size in bytes

◆ clone()

VideoFrame av::VideoFrame::clone ( size_t align = 1,
OptionalErrorCode ec = throws() ) const

Hardware frames cannot be copied implicitly: the function reports Errors::HardwareTransferUnsupported. Use toSystemMemory() to transfer a hardware frame into system memory first. An invalid frame produces an invalid copy without an error.

Parameters
align
ec
Returns
an independent frame, or an invalid frame when this one is invalid

◆ copyToBuffer() [1/2]

bool av::VideoFrame::copyToBuffer ( std::vector< uint8_t > & dst,
int align = 1,
OptionalErrorCode ec = throws() )
Parameters
dstdestination vector; resized to bufferSize()
alignalignment in bytes for each plane
echolder for the error code
Returns
true when the frame was copied

◆ copyToBuffer() [2/2]

bool av::VideoFrame::copyToBuffer ( uint8_t * dst,
size_t size,
int align = 1,
OptionalErrorCode ec = throws() )
Parameters
dstdestination buffer
sizesize of the destination in bytes
alignalignment in bytes for each plane
echolder for the error code
Returns
true when the frame was copied

◆ height()

int av::VideoFrame::height ( ) const
Returns
the height in pixels, or zero for an invalid frame
Examples
api2-decode-filter-encode.cpp, api2-decode-overlay-encode.cpp, and api2-hw-scale.cpp.

◆ hwDeviceContext()

hw::DeviceContext av::VideoFrame::hwDeviceContext ( ) const
noexcept
Returns
the device of this frame, or an invalid value

◆ hwFramesContext()

hw::FramesContext av::VideoFrame::hwFramesContext ( ) const
noexcept
Returns
the pool of this frame, or an invalid value

◆ isKeyFrame()

bool av::VideoFrame::isKeyFrame ( ) const
Returns
the AV_FRAME_FLAG_KEY state

◆ mapTo()

VideoFrame av::VideoFrame::mapTo ( const hw::FramesContext & frames,
int flags = AV_HWFRAME_MAP_READ,
OptionalErrorCode ec = throws() ) const

Uses av_hwframe_map(). The operation depends on the backend and may fail with ENOSYS. Use toHwFrame() with a transfer for a portable path.

Parameters
frames
flags
ec
Returns
a frame mapped into the given pool

◆ operator=() [1/2]

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

◆ operator=() [2/2]

VideoFrame & av::VideoFrame::operator= ( VideoFrame && rhs)
Parameters
rhsthe frame to move from; left invalid
Returns
this frame

◆ pictureType()

AVPictureType av::VideoFrame::pictureType ( ) const
Returns
the AV_PICTURE_TYPE_* value
Examples
api2-decode-overlay-encode.cpp.

◆ pixelFormat()

PixelFormat av::VideoFrame::pixelFormat ( ) const
Returns
the format, or AV_PIX_FMT_NONE for an invalid frame
Examples
api2-hw-scale.cpp.

◆ quality()

int av::VideoFrame::quality ( ) const
Returns
the quality in the FF_LAMBDA_* scale

◆ sampleAspectRatio()

Rational av::VideoFrame::sampleAspectRatio ( ) const
Returns
the ratio, or an empty value when it is not set

◆ setKeyFrame()

void av::VideoFrame::setKeyFrame ( bool isKey)
Parameters
isKeythe new state of the key-frame flag

◆ setPictureType()

void av::VideoFrame::setPictureType ( AVPictureType type = AV_PICTURE_TYPE_NONE)
Parameters
typethe AV_PICTURE_TYPE_* value; the default lets the encoder decide
Examples
api2-decode-overlay-encode.cpp.

◆ setQuality()

void av::VideoFrame::setQuality ( int quality)
Parameters
qualityquality in the FF_LAMBDA_* scale

◆ setSampleAspectRatio()

void av::VideoFrame::setSampleAspectRatio ( const Rational & sampleAspectRatio)
Parameters
sampleAspectRatiowidth divided by height of a single pixel

◆ toHwFrame()

VideoFrame av::VideoFrame::toHwFrame ( const hw::FramesContext & frames,
OptionalErrorCode ec = throws() ) const

The destination buffer is allocated with av_hwframe_get_buffer() and then filled with av_hwframe_transfer_data(). A hardware frame that already belongs to frames is returned as a reference copy.

Parameters
frames
ec
Returns
a frame in the given hardware pool
Examples
api2-hw-scale.cpp.

◆ toSystemMemory()

VideoFrame av::VideoFrame::toSystemMemory ( PixelFormat format = AV_PIX_FMT_NONE,
OptionalErrorCode ec = throws() ) const

The function allocates a software frame and copies the hardware surface into it. When format is not AV_PIX_FMT_NONE, the transfer requests that format. The format must be supported by the hardware backend; see FramesContext::transferFormats().

A software frame is returned as a reference copy when no format conversion is requested.

Parameters
format
ec
Returns
a frame in system memory
Examples
api2-hw-scale.cpp.

◆ width()

int av::VideoFrame::width ( ) const
Returns
the width in pixels, or zero for an invalid frame
Examples
api2-decode-filter-encode.cpp, api2-decode-overlay-encode.cpp, and api2-hw-scale.cpp.

◆ wrap() [1/3]

VideoFrame av::VideoFrame::wrap ( const void * data,
size_t size,
PixelFormat pixelFormat,
int width,
int height,
int align = 1 )
static
Note
Ownershipping does not acquired.
Parameters
datadata pointer to wrap
sizewhole data buffer size with alignment and real data
pixelFormatpixel format of the data
widthimage width
heightimage height
alignimage data alignemnt
Returns
image data wrapped into VideoFrame object

◆ wrap() [2/3]

VideoFrame av::VideoFrame::wrap ( std::span< const std::byte > data,
PixelFormat pixelFormat,
int width,
int height,
int align = 1 )
static
Note
Ownershipping does not acquired.
Parameters
dataexternal image data wrapped into std::span
pixelFormatpixel format of the data
widthimage width
heightimage height
alignimage data alignment
Returns

◆ wrap() [3/3]

VideoFrame av::VideoFrame::wrap ( std::span< const std::byte > data,
void(* deleter )(void *opaque, uint8_t *data),
void * opaque,
PixelFormat pixelFormat,
int width,
int height,
int align = 1 )
static
Note
Ownershipping does not acquired. When data wants to be deleted, passed deleter function will be called.
Parameters
dataexternal image data wrapped into std::span
deleteruser defined deleter
opaqueopaque data passed to the deleter, maybe nullptr
pixelFormatpixel format of the data
widthimage width
heightimage height
alignimage data alignment
Returns

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