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

Base class for audio and video codec contexts. More...

#include <codeccontext.h>

Inheritance diagram for av::CodecContext2:

Public Member Functions

void open (OptionalErrorCode ec=throws())
 Open the codec with the parameters set so far.
void open (const Codec &codec, OptionalErrorCode ec=throws())
 Open the codec, choosing it explicitly.
void open (class Dictionary &options, OptionalErrorCode ec=throws())
 Open the codec with options.
void open (class Dictionary &&options, OptionalErrorCode ec=throws())
 Open the codec with options, taking ownership of the dictionary.
void open (class Dictionary &options, const Codec &codec, OptionalErrorCode ec=throws())
 Open an explicitly chosen codec with options.
void open (class Dictionary &&options, const Codec &codec, OptionalErrorCode ec=throws())
 Open an explicitly chosen codec with options, moving the dictionary.
void close (OptionalErrorCode ec=throws())
 Close the codec context.
bool isOpened () const noexcept
 Whether the codec has been opened.
bool isValid () const noexcept
 Whether the context owns an FFmpeg codec context.
Rational timeBase () const noexcept
 Time base of the codec.
void setTimeBase (const Rational &value) noexcept
 Set the time base.
Rational frameRate () const noexcept
 Frame rate advertised by the codec.
void setFrameRate (const Rational &value) noexcept
 Set the frame rate.
bool isEof () const noexcept
 Returns true if the codec has been flushed and all output is drained.
void flushBuffers () noexcept
 Resets the codec internal state and the end-of-stream flag.
const Stream & stream () const noexcept
 The stream this context is attached to.
Codec codec () const noexcept
 The codec in use.
void setOption (const std::string &key, const std::string &val, OptionalErrorCode ec=throws())
 Set a private codec option.
void setOption (const std::string &key, const std::string &val, int flags, OptionalErrorCode ec=throws())
 Set a private codec option with AVDictionary flags.
int frameSize () const noexcept
 Number of samples per frame the codec works with.
int64_t frameNumber () const noexcept
 Counter of frames passed through the context.
bool isRefCountedFrames () const noexcept
 Whether the codec hands out reference-counted frames.
void setRefCountedFrames (bool refcounted) const noexcept
 Ask the codec for reference-counted frames.
int strict () const noexcept
 Current strictness level of the codec.
void setStrict (int strict) noexcept
 Set the strictness level.
int64_t bitRate () const noexcept
 Target bit rate.
std::pair< int64_t, int64_t > bitRateRange () const noexcept
 Allowed bit rate range.
void setBitRate (int64_t bitRate) noexcept
 Set the target bit rate.
void setBitRateRange (const std::pair< int64_t, int64_t > &bitRateRange) noexcept
 Set the allowed bit rate range.
template<typename T>
std::pair< int, const std::error_category * > decodeCommon (T &outFrame, const class Packet &inPacket, size_t offset, int &frameFinished, int(*decodeProc)(AVCodecContext *, AVFrame *, int *, const AVPacket *))
 Decode one packet into a typed frame.
template<typename T>
std::pair< int, const std::error_category * > encodeCommon (class Packet &outPacket, const T &inFrame, int &gotPacket, int(*encodeProc)(AVCodecContext *, AVPacket *, const AVFrame *, int *))
 Encode one typed frame into a packet.
bool send (const Packet &packet, OptionalErrorCode ec=throws())
 Submit a packet to a decoder.
Packet receive (OptionalErrorCode ec=throws())
 Read one decoded packet from an encoder.
void _log (int level, const char *fmt) const
 Log through the FFmpeg log system, using this wrapper as the context.
void _log (int level, const char *fmt, const Args &... args) const
 Log through the FFmpeg log system with arguments.
void copyContextFrom (const CodecContext2 &other, OptionalErrorCode ec=throws())
 Copy codec context from codec context associated with given stream or other codec context.
void setFlags (int flags) noexcept
 Access to CODEC_FLAG_* flags.
void addFlags (int flags) noexcept
 Add AV_CODEC_FLAG_* flags.
void clearFlags (int flags) noexcept
 Remove AV_CODEC_FLAG_* flags.
int flags () noexcept
 Current AV_CODEC_FLAG_* mask.
bool isFlags (int flags) noexcept
 Whether all given flags are set.
void setFlags2 (int flags) noexcept
 Access to CODEC_FLAG2_* flags.
void addFlags2 (int flags) noexcept
 Add AV_CODEC_FLAG2_* flags.
void clearFlags2 (int flags) noexcept
 Remove AV_CODEC_FLAG2_* flags.
int flags2 () noexcept
 Current AV_CODEC_FLAG2_* mask.
bool isFlags2 (int flags) noexcept
 Whether all given flags are set.
Public Member Functions inherited from FFWrapperPtr< AVCodecContext >
 FFWrapperPtr ()=default
 Create an empty wrapper; isNull() is true.
 FFWrapperPtr (AVCodecContext *raw)
 Wrap an existing object.
const AVCodecContext * raw () const
 Read access to the wrapped object.
AVCodecContext * raw ()
 Write access to the wrapped object.
void reset (AVCodecContext *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.
void _log (int level, const char *fmt, const Args &... args) const
 Log through the FFmpeg log system with arguments.

Protected Types

using BaseWrapper = FFWrapperPtr<AVCodecContext>
 /// The reference-counting wrapper this context builds on.

Protected Member Functions

void swap (CodecContext2 &other)
 Exchange the state of two contexts.
 CodecContext2 (const class Stream &st, const class Codec &codec, Direction direction, AVMediaType type)
 Create a context attached to a stream.
 CodecContext2 (const class Codec &codec, Direction direction, AVMediaType type)
 Create a context for a codec without a stream.
void setCodec (const class Codec &codec, bool resetDefaults, Direction direction, AVMediaType type, OptionalErrorCode ec=throws())
 Attach a codec and validate it against the direction and media type.
AVMediaType codecType (AVMediaType contextType) const noexcept
 Map a context media type to the effective media type.
bool isValidForEncode (Direction direction, AVMediaType type) const noexcept
 Whether the configured codec can encode the given media type.
bool checkCodec (const Codec &codec, Direction direction, AVMediaType type, OptionalErrorCode ec)
 Validate a codec against a direction and media type, reporting the reason.
void open (const Codec &codec, AVDictionary **options, OptionalErrorCode ec)
 Open the codec with raw FFmpeg options.
std::pair< int, const std::error_category * > decodeCommon (AVFrame *outFrame, const class Packet &inPacket, size_t offset, int &frameFinished, int(*decodeProc)(AVCodecContext *, AVFrame *, int *, const AVPacket *)) noexcept
 Decode one packet into an FFmpeg frame.
std::pair< int, const std::error_category * > encodeCommon (class Packet &outPacket, const AVFrame *inFrame, int &gotPacket, int(*encodeProc)(AVCodecContext *, AVPacket *, const AVFrame *, int *)) noexcept
 Encode one typed frame into a packet.
std::error_code sendCommon (const class Packet &pkt)
 Submit a packet to a decoder.
std::error_code receiveCommon (AVFrame *frame)
 Read one frame from a decode context.
template<typename T>
std::error_code receiveCommon (T &frame)
 Read one typed output object from a decode context.
std::error_code sendCommon (const AVFrame *frame)
 Submit a frame to an encoder.
std::error_code receiveCommon (class Packet &pkt)
 Read one packet from an encode context.
template<typename T>
std::error_code sendCommon (const T &frame)
 Submit a typed frame to an encoder.
Protected Member Functions inherited from av::noncopyable
 noncopyable (const noncopyable &)=delete
void operator= (const noncopyable &)=delete

Protected Attributes

hw::DeviceContext m_hwDevice
 Device attached by setHwDeviceContext() or configureHwEncoding().
hw::FramesContext m_hwFrames
 Pool attached by setHwFramesContext() or configureHwEncoding().
bool m_hwTransferToSystem = false
 Whether decoded hardware frames are copied to system memory automatically.
bool m_hwAutoUpload = true
 Whether software frames are uploaded into the pool before encoding.
hw::EncodingMode m_hwEncodingMode = hw::EncodingMode::Auto
 How the encoder receives hardware frames.
Protected Attributes inherited from FFWrapperPtr< AVCodecContext >
AVCodecContext * m_raw
 The wrapped pointer; owned by the derived class, not by this template.

Detailed Description

Send/receive contract
The send/receive API wraps the FFmpeg send/receive model:
  • send() submits one packet (decoding) or one frame (encoding) and returns false when the codec cannot accept more input until the output is drained. This EAGAIN condition is not an error.
  • receive() returns one output object. An empty (invalid) result means that no output is available yet (EAGAIN) or that the stream has ended.
  • After the last input, call flush() once and keep calling receive() until it returns an empty result. At that point isEof() becomes true.
  • An empty result is never marked as complete. Callers must check the object with operator bool() or isNull()/isValid().
  • flushBuffers() resets the codec state and the EOF flag, for example after a seek back to a key frame.

Constructor & Destructor Documentation

◆ CodecContext2() [1/2]

av::CodecContext2::CodecContext2 ( const class Stream & st,
const class Codec & codec,
Direction direction,
AVMediaType type )
protected

Used by the typed subclasses, which know the direction and media type; not part of the public construction path.

Parameters
st
codec
direction
type

◆ CodecContext2() [2/2]

av::CodecContext2::CodecContext2 ( const class Codec & codec,
Direction direction,
AVMediaType type )
protected

Used by the typed subclasses; not part of the public construction path.

Parameters
codec
direction
type

Member Function Documentation

◆ _log() [1/2]

void FFWrapperPtr< AVCodecContext >::_log ( int level,
const char * fmt ) const
inline
Parameters
levelan AV_LOG_* level
fmtprintf-style format string without arguments

◆ _log() [2/2]

void FFWrapperPtr< AVCodecContext >::_log ( int level,
const char * fmt,
const Args &... args ) const
inline
Parameters
levelan AV_LOG_* level
fmtprintf-style format string
argsarguments for the format string

◆ addFlags()

void av::CodecContext2::addFlags ( int flags)
noexcept
Parameters
flagsflags to set
Examples
api2-hw-scale.cpp, api2-hw-transcode.cpp, and api2-scale-video.cpp.

◆ addFlags2()

void av::CodecContext2::addFlags2 ( int flags)
noexcept
Parameters
flagsflags to set

◆ bitRate()

◆ bitRateRange()

std::pair< int64_t, int64_t > av::CodecContext2::bitRateRange ( ) const
noexcept
Returns
minimum and maximum bit rate in bits per second

◆ checkCodec()

bool av::CodecContext2::checkCodec ( const Codec & codec,
Direction direction,
AVMediaType type,
OptionalErrorCode ec )
protected
Parameters
codec
direction
type
ec
Returns
true when the codec is usable

◆ clearFlags()

void av::CodecContext2::clearFlags ( int flags)
noexcept
Parameters
flagsflags to clear

◆ clearFlags2()

void av::CodecContext2::clearFlags2 ( int flags)
noexcept
Parameters
flagsflags to clear

◆ close()

void av::CodecContext2::close ( OptionalErrorCode ec = throws())
Deprecated
Since FFmpeg 4.0 the recommendation is to destroy and recreate the context instead of closing it: close() does not free the context and leaves it in a half-initialized state.
Parameters
echolder for the error code; when it is not throws() an error is reported through it instead of an exception
Deprecated
Start from FFmpeg 4.0 it is recommended to destroy and recreate codec context insted of close

◆ codec()

Codec av::CodecContext2::codec ( ) const
noexcept
Returns
the codec descriptor, or an invalid one when no codec is set

◆ codecType()

AVMediaType av::CodecContext2::codecType ( AVMediaType contextType) const
protectednoexcept

Codec contexts can be created for a media type that the codec overrides; this resolves the final value.

Parameters
contextType
Returns
the effective media type

◆ copyContextFrom()

void av::CodecContext2::copyContextFrom ( const CodecContext2 & other,
OptionalErrorCode ec = throws() )

This functionality useful for remuxing without deconding/encoding. In this case you need not open codecs, only copy context.

Parameters
otherstream or codec context
echolder for the error code

◆ decodeCommon() [1/2]

std::pair< int, const std::error_category * > av::CodecContext2::decodeCommon ( AVFrame * outFrame,
const class Packet & inPacket,
size_t offset,
int & frameFinished,
int(* decodeProc )(AVCodecContext *, AVFrame *, int *, const AVPacket *) )
protectednoexcept

The typed contexts wrap this and convert the result.

Parameters
outFrame
inPacket
offset
frameFinished
decodeProc
Returns
the FFmpeg error code and the category it belongs to

◆ decodeCommon() [2/2]

template<typename T>
std::pair< int, const std::error_category * > av::CodecContext2::decodeCommon ( T & outFrame,
const class Packet & inPacket,
size_t offset,
int & frameFinished,
int(* decodeProc )(AVCodecContext *, AVFrame *, int *, const AVPacket *) )

Shared implementation of the public decode() overloads.

Parameters
outFrame
inPacket
offset
frameFinished
decodeProc
Returns
the FFmpeg error code and the category it belongs to

◆ encodeCommon() [1/2]

std::pair< int, const std::error_category * > av::CodecContext2::encodeCommon ( class Packet & outPacket,
const AVFrame * inFrame,
int & gotPacket,
int(* encodeProc )(AVCodecContext *, AVPacket *, const AVFrame *, int *) )
protectednoexcept

Shared implementation of the public encode() overloads.

Parameters
outPacket
inFrame
gotPacket
encodeProc
Returns
the FFmpeg error code and the category it belongs to

◆ encodeCommon() [2/2]

template<typename T>
std::pair< int, const std::error_category * > av::CodecContext2::encodeCommon ( class Packet & outPacket,
const T & inFrame,
int & gotPacket,
int(* encodeProc )(AVCodecContext *, AVPacket *, const AVFrame *, int *) )

Shared implementation of the public encode() overloads.

Parameters
outPacket
inFrame
gotPacket
encodeProc
Returns
the FFmpeg error code and the category it belongs to

◆ flags()

int av::CodecContext2::flags ( )
noexcept
Returns
the flags in use

◆ flags2()

int av::CodecContext2::flags2 ( )
noexcept
Returns
the flags in use

◆ flushBuffers()

void av::CodecContext2::flushBuffers ( )
inlinenoexcept

Wraps avcodec_flush_buffers(). Use it to reuse the context after EOF or to seek back to a key frame.

◆ frameNumber()

int64_t av::CodecContext2::frameNumber ( ) const
noexcept
Returns
the frame number reported by FFmpeg, or zero when unavailable

◆ frameRate()

Rational av::CodecContext2::frameRate ( ) const
noexcept

Hardware encoders (for example h264_qsv) read this value and need it to be set before open().

Returns
the frame rate, or an empty value when it is not set

◆ frameSize()

int av::CodecContext2::frameSize ( ) const
noexcept
Returns
the frame size in samples; zero for codecs that do not use it
Examples
api2-decode-encode-audio.cpp.

◆ isEof()

bool av::CodecContext2::isEof ( ) const
inlinenoexcept

An empty result of receive() with isEof() == false means that more input is required (EAGAIN), for example during the initial delay of a reordering decoder.

Returns
true when the codec is flushed and fully drained
Examples
api2-send-receive.cpp.

◆ isFlags()

bool av::CodecContext2::isFlags ( int flags)
noexcept
Parameters
flagsflags to test
Returns
true when every flag in the mask is present

◆ isFlags2()

bool av::CodecContext2::isFlags2 ( int flags)
noexcept
Parameters
flagsflags to test
Returns
true when every flag in the mask is present

◆ isOpened()

bool av::CodecContext2::isOpened ( ) const
noexcept
Returns
true after a successful open() and before the context dies

◆ isRefCountedFrames()

bool av::CodecContext2::isRefCountedFrames ( ) const
noexcept
Returns
true when frames can outlive the next receive() call

◆ isValid()

bool av::CodecContext2::isValid ( ) const
noexcept
Returns
true when the underlying AVCodecContext exists

◆ isValidForEncode()

bool av::CodecContext2::isValidForEncode ( Direction direction,
AVMediaType type ) const
protectednoexcept

Validation helper used by setCodec() and open().

Parameters
direction
type
Returns
true when the codec supports this combination

◆ open() [1/7]

void av::CodecContext2::open ( class Dictionary && options,
const Codec & codec,
OptionalErrorCode ec = throws() )
Parameters
optionscodec options, moved into the call
codecthe codec to open
echolder for the error code

◆ open() [2/7]

void av::CodecContext2::open ( class Dictionary && options,
OptionalErrorCode ec = throws() )
Parameters
optionscodec options, moved into the call
echolder for the error code

◆ open() [3/7]

void av::CodecContext2::open ( class Dictionary & options,
const Codec & codec,
OptionalErrorCode ec = throws() )
Parameters
optionscodec options; the object stays usable, but FFmpeg removes the options it recognized
codecthe codec to open
echolder for the error code

◆ open() [4/7]

void av::CodecContext2::open ( class Dictionary & options,
OptionalErrorCode ec = throws() )
Parameters
optionscodec options, for example {"threads", "4"}; the dictionary stays usable, but FFmpeg removes the options it recognized
echolder for the error code

◆ open() [5/7]

void av::CodecContext2::open ( const Codec & codec,
AVDictionary ** options,
OptionalErrorCode ec )
protected

The public open() overloads convert a Dictionary and call this one.

Parameters
codec
options
ec

◆ open() [6/7]

void av::CodecContext2::open ( const Codec & codec,
OptionalErrorCode ec = throws() )
Parameters
codecthe codec to open; it must match the direction and media type of the context
echolder for the error code

◆ open() [7/7]

void av::CodecContext2::open ( OptionalErrorCode ec = throws())

The codec is either the one passed to the constructor or the one set with setCodec(). Everything that has to be known at open time — size, sample rate, time base, hardware device or frames context — must be set before this call.

Parameters
echolder for the error code; throws() raises av::Exception
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-scale.cpp, api2-hw-transcode.cpp, api2-scale-video.cpp, api2-send-receive.cpp, and api2-subtitles.cpp.

◆ receive()

Packet av::CodecContext2::receive ( OptionalErrorCode ec = throws())
Parameters
echolder for the error code
Returns
an empty packet when nothing is available yet (EAGAIN) or the stream has ended; use isEof() to tell the two apart

◆ receiveCommon() [1/3]

std::error_code av::CodecContext2::receiveCommon ( AVFrame * frame)
protected

Shared implementation of receive() for video decoders.

Parameters
frame
Returns
an FFmpeg error code, empty on success

◆ receiveCommon() [2/3]

std::error_code av::CodecContext2::receiveCommon ( class Packet & pkt)
protected

Shared implementation of receive() for encoders.

Parameters
pkt
Returns
an FFmpeg error code, empty on success

◆ receiveCommon() [3/3]

template<typename T>
std::error_code av::CodecContext2::receiveCommon ( T & frame)
protected

Shared implementation of receive() for the typed decoders.

Parameters
frame
Returns
an FFmpeg error code, empty on success

◆ send()

bool av::CodecContext2::send ( const Packet & packet,
OptionalErrorCode ec = throws() )
Parameters
packetthe compressed packet
echolder for the error code
Returns
false when the decoder cannot accept more input until its output is drained; this EAGAIN condition is not an error

◆ sendCommon() [1/3]

std::error_code av::CodecContext2::sendCommon ( const AVFrame * frame)
protected

Shared implementation of send() for video encoders.

Parameters
frame
Returns
an FFmpeg error code, empty on success

◆ sendCommon() [2/3]

std::error_code av::CodecContext2::sendCommon ( const class Packet & pkt)
protected

Shared implementation of send() for decoding contexts.

Parameters
pkt
Returns
an FFmpeg error code, empty on success

◆ sendCommon() [3/3]

template<typename T>
std::error_code av::CodecContext2::sendCommon ( const T & frame)
protected

Shared implementation of send() for the typed encoders.

Parameters
frame
Returns
an FFmpeg error code, empty on success

◆ setBitRate()

void av::CodecContext2::setBitRate ( int64_t bitRate)
noexcept

◆ setBitRateRange()

void av::CodecContext2::setBitRateRange ( const std::pair< int64_t, int64_t > & bitRateRange)
noexcept
Parameters
bitRateRangeminimum and maximum in bits per second

◆ setCodec()

void av::CodecContext2::setCodec ( const class Codec & codec,
bool resetDefaults,
Direction direction,
AVMediaType type,
OptionalErrorCode ec = throws() )
protected

The typed subclasses bind the direction and media type themselves.

Parameters
codec
resetDefaults
direction
type
ec

◆ setFlags()

void av::CodecContext2::setFlags ( int flags)
noexcept

Replace the AV_CODEC_FLAG_* set.

Parameters
flagsthe new flag mask

◆ setFlags2()

void av::CodecContext2::setFlags2 ( int flags)
noexcept

Replace the AV_CODEC_FLAG2_* set.

Parameters
flagsthe new flag mask

◆ setFrameRate()

void av::CodecContext2::setFrameRate ( const Rational & value)
noexcept
Parameters
valueframes per second; some encoders (QSV, for one) require it before open(), others use it as a hint
Examples
api2-hw-scale.cpp, and api2-hw-transcode.cpp.

◆ setOption() [1/2]

void av::CodecContext2::setOption ( const std::string & key,
const std::string & val,
int flags,
OptionalErrorCode ec = throws() )
Parameters
keyoption name
valoption value
flagscombination of AV_DICT_* flags, for example AV_DICT_DONT_OVERWRITE
echolder for the error code

◆ setOption() [2/2]

void av::CodecContext2::setOption ( const std::string & key,
const std::string & val,
OptionalErrorCode ec = throws() )
Parameters
keyoption name, as accepted by ffmpeg -h
valoption value
echolder for the error code; an unknown key reports Errors::InvalidArgument

◆ setRefCountedFrames()

void av::CodecContext2::setRefCountedFrames ( bool refcounted) const
noexcept
Parameters
refcountedtrue makes frame copies cheap and keeps a frame valid after the next decode call, which is required for multithreaded processing
Examples
api2-decode-audio.cpp, api2-decode-encode-video.cpp, api2-decode-filter-encode.cpp, api2-decode-overlay-encode.cpp, api2-decode-rasample-audio.cpp, and api2-scale-video.cpp.

◆ setStrict()

void av::CodecContext2::setStrict ( int strict)
noexcept
Parameters
strictan FF_COMPLIANCE_* value; higher levels make the codec reject streams that are not fully standard compliant

◆ setTimeBase()

void av::CodecContext2::setTimeBase ( const Rational & value)
noexcept

◆ stream()

const Stream & av::CodecContext2::stream ( ) const
noexcept
Returns
the stream passed to the constructor, or an empty stream when the context was created from a codec alone

◆ strict()

int av::CodecContext2::strict ( ) const
noexcept
Returns
the FF_COMPLIANCE_* value in use

◆ swap()

void av::CodecContext2::swap ( CodecContext2 & other)
protected
Parameters
other

◆ timeBase()

Rational av::CodecContext2::timeBase ( ) const
noexcept

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