15#include <system_error>
26#include <libavutil/hwcontext.h>
60 constexpr DeviceType(AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE) noexcept
70 operator AVHWDeviceType() const noexcept {
return m_type; }
77 AVHWDeviceType
get() const noexcept {
return m_type; }
84 bool isNone() const noexcept {
return m_type == AV_HWDEVICE_TYPE_NONE; }
91 const char*
name() const noexcept;
116 AVHWDeviceType m_type = AV_HWDEVICE_TYPE_NONE;
127 friend class FramesContext;
130 DeviceContext() =
default;
148 const char* device =
nullptr,
163 const std::string& device,
181 static DeviceContext
derive(
const DeviceContext& source,
197 static DeviceContext
wrap(AVBufferRef* ref)
noexcept;
219 AVHWDeviceContext*
raw() const noexcept;
231 explicit DeviceContext(
BufferRef&& ref) noexcept
232 : m_ref(
std::move(ref))
244 FramesContext() =
default;
270 int initialPoolSize = 0,
280 static FramesContext
wrap(AVBufferRef* ref)
noexcept;
342 AVHWFramesContext*
raw() const noexcept;
354 explicit FramesContext(
BufferRef&& ref) noexcept
355 : m_ref(
std::move(ref))
388 : m_data(data), m_size(
size)
396 T*
begin() const noexcept {
return m_data; }
402 T*
end() const noexcept {
return m_data + m_size; }
408 size_t size() const noexcept {
return m_size; }
414 bool empty() const noexcept {
return m_size == 0; }
422 T&
operator[](
size_t index)
const noexcept {
return m_data[index]; }
431 return m_data ? std::vector<T>(m_data, m_data + m_size) : std::vector<T>();
486 m_type =
DeviceType(av_hwdevice_iterate_types(m_type.get()));
623 : m_type(type), m_device(device)
648 if (!m_type.isNone())
649 m_type =
DeviceType(av_hwdevice_iterate_types(m_type.get()));
682 void probe() noexcept
685 m_probe.
type = m_type;
689 AVBufferRef* ref =
nullptr;
690 int ret = av_hwdevice_ctx_create(&ref, m_type.get(), m_device,
nullptr, 0);
695 m_probe.available =
true;
696 av_buffer_unref(&ref);
700 const char* m_device =
nullptr;
723 : m_owned(device), m_device(m_owned->c_str())
732 : m_owned(other.m_owned),
733 m_device(other.m_owned ? m_owned->c_str() : other.m_device)
742 : m_owned(std::move(other.m_owned)),
743 m_device(m_owned ? m_owned->c_str() : other.m_device)
745 other.m_device =
nullptr;
769 swap(m_owned, other.m_owned);
770 swap(m_device, other.m_device);
772 m_device = m_owned->c_str();
774 other.m_device = other.m_owned->c_str();
784 return iterator(
DeviceType(av_hwdevice_iterate_types(AV_HWDEVICE_TYPE_NONE)), m_device);
794 std::optional<std::string> m_owned;
795 const char* m_device =
nullptr;
871 explicit iterator(
const AVPixelFormat* ptr) noexcept : m_ptr(ptr) {}
918 const AVPixelFormat* m_ptr =
nullptr;
952 size_t size() const noexcept {
return static_cast<size_t>(m_end - m_begin); }
958 bool empty() const noexcept {
return m_begin == m_end; }
976 const AVPixelFormat* m_begin =
nullptr;
977 const AVPixelFormat* m_end =
nullptr;
1005 : m_formats(formats)
1018 : m_formats(other.m_formats)
1020 other.m_formats =
nullptr;
1078 AVPixelFormat* m_formats =
nullptr;
1113 bool isValid() const noexcept {
return static_cast<bool>(m_raw); }
1154 void operator()(AVHWFramesConstraints* ptr)
const noexcept;
1157 std::unique_ptr<AVHWFramesConstraints, Deleter> m_raw;
1208 : m_codec(codec), m_index(index)
1258 if (m_codec != other.m_codec)
1260 if (!m_valid || !other.m_valid)
1261 return m_valid == other.m_valid;
1262 return m_index == other.m_index;
1274 void refresh() noexcept
1277 if (!m_codec || m_index < 0)
1279 const AVCodecHWConfig* cfg = avcodec_get_hw_config(m_codec, m_index);
1286 const AVCodec* m_codec =
nullptr;
1288 bool m_valid =
false;
1289 AVCodecHWConfig m_value{};
1302 : m_codec(codec.raw())
1326 const AVCodec* m_codec =
nullptr;
1352 int methodsMask = AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX |
1353 AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX);
1390 : m_id(id), m_type(type), m_encoders(encoders)
1440 void advance() noexcept
1443 m_codec = av_codec_iterate(&m_opaque);
1446 if (m_encoders ? !av_codec_is_encoder(m_codec) : !av_codec_is_decoder(m_codec))
1448 if (m_id != AV_CODEC_ID_NONE && m_codec->id != m_id)
1450 if (!codecSupportsDevice(m_codec, m_type.get(), m_encoders))
1456 static bool codecSupportsDevice(
const AVCodec* codec, AVHWDeviceType type,
bool encoders)
noexcept;
1458 AVCodecID m_id = AV_CODEC_ID_NONE;
1460 bool m_encoders =
false;
1461 void* m_opaque =
nullptr;
1462 const AVCodec* m_codec =
nullptr;
1477 : m_id(
id), m_type(type), m_encoders(encoders)
1494 AVCodecID m_id = AV_CODEC_ID_NONE;
1496 bool m_encoders =
false;
FFmpeg version and API compatibility switches.
Error codes, error categories and the OptionalErrorCode convention.
Reference counted buffers: BufferRef and Buffer over AVBufferRef.
Light weight wrapper for the FFmpeg AVBufferRef functionality.
Definition buffer.h:136
Codec descriptor.
Definition codec.h:32
Implements interface to access to the AVDictionary entity.
Definition dictionary.h:38
Carries the caller's choice between exceptions and error codes.
Definition averror.h:96
Forward iterator over the hardware configurations of a codec.
Definition hwcontext.h:1184
const AVCodecHWConfig & reference
Reference to the configuration entry.
Definition hwcontext.h:1195
pointer operator->() const noexcept
Configuration at the current position.
Definition hwcontext.h:1224
std::ptrdiff_t difference_type
Distance type required by the iterator traits.
Definition hwcontext.h:1191
iterator(const AVCodec *codec, int index) noexcept
Point the iterator at a configuration index.
Definition hwcontext.h:1207
std::forward_iterator_tag iterator_category
Standard iterator category tag.
Definition hwcontext.h:1187
iterator operator++(int) noexcept
Advance, returning the previous position.
Definition hwcontext.h:1242
iterator & operator++() noexcept
Advance to the next configuration.
Definition hwcontext.h:1231
const AVCodecHWConfig * pointer
Pointer to the configuration entry.
Definition hwcontext.h:1193
reference operator*() const noexcept
Configuration at the current position.
Definition hwcontext.h:1218
bool operator==(const iterator &other) const noexcept
Compare positions.
Definition hwcontext.h:1256
iterator()=default
End iterator.
AVCodecHWConfig value_type
Type produced by dereferencing.
Definition hwcontext.h:1189
bool operator!=(const iterator &other) const noexcept
Compare positions.
Definition hwcontext.h:1271
Non-owning iterator over AVCodecHWConfig entries of a codec.
Definition hwcontext.h:1178
iterator begin() const
First configuration.
Definition hwcontext.h:1310
bool empty() const
Whether the codec has no hardware configuration.
Definition hwcontext.h:1323
CodecConfigRange(const Codec &codec) noexcept
Iterate the hardware configurations of a codec.
Definition hwcontext.h:1301
iterator end() const
End of the range.
Definition hwcontext.h:1316
CodecConfigRange()=default
Create an empty range.
Forward iterator over the codecs FFmpeg was built with.
Definition hwcontext.h:1365
Codec value_type
Type produced by dereferencing.
Definition hwcontext.h:1370
iterator()=default
End iterator.
iterator & operator++() noexcept
Advance to the next matching codec.
Definition hwcontext.h:1406
bool operator==(const iterator &other) const noexcept
Compare positions.
Definition hwcontext.h:1429
Codec reference
Dereference result type.
Definition hwcontext.h:1376
Codec operator*() const noexcept
Codec at the current position.
Definition hwcontext.h:1400
bool operator!=(const iterator &other) const noexcept
Compare positions.
Definition hwcontext.h:1437
iterator(AVCodecID id, DeviceType type, bool encoders)
Start the iteration and skip everything that does not match.
Definition hwcontext.h:1389
std::ptrdiff_t difference_type
Distance type required by the iterator traits.
Definition hwcontext.h:1372
iterator operator++(int) noexcept
Advance, returning the previous position.
Definition hwcontext.h:1416
std::forward_iterator_tag iterator_category
Standard iterator category tag.
Definition hwcontext.h:1368
void pointer
No pointer-to-element type: the value is produced on dereference.
Definition hwcontext.h:1374
Non-owning range over codecs found with av_codec_iterate().
Definition hwcontext.h:1359
iterator begin() const
First matching codec.
Definition hwcontext.h:1485
CodecRange()=default
Create an empty range.
CodecRange(AVCodecID id, DeviceType type, bool encoders) noexcept
Describe the codecs to iterate.
Definition hwcontext.h:1476
iterator end() const
End of the range.
Definition hwcontext.h:1491
Owner of an AVHWDeviceContext.
Definition hwcontext.h:126
static DeviceContext derive(const DeviceContext &source, DeviceType type, const Dictionary &options={}, OptionalErrorCode ec=throws())
Create a device derived from an existing one.
static DeviceContext wrap(AVBufferRef *ref) noexcept
Take a reference to an existing AVBufferRef.
AVBufferRef * bufferRef() const noexcept
The reference-counted buffer that owns the context.
DeviceType type() const noexcept
The type of the device.
AVHWDeviceContext * raw() const noexcept
The underlying FFmpeg context.
static DeviceContext create(DeviceType type, const std::string &device, const Dictionary &options={}, OptionalErrorCode ec=throws())
Create and open a hardware device from a std::string selector.
bool isValid() const noexcept
Whether the wrapper holds a device.
static DeviceContext create(DeviceType type, const char *device=nullptr, const Dictionary &options={}, OptionalErrorCode ec=throws())
Create and open a hardware device.
Forward iterator that creates and destroys one device per step.
Definition hwcontext.h:599
iterator operator++(int) noexcept
Advance, returning the previous position.
Definition hwcontext.h:658
reference operator*() const noexcept
Result of the current probe.
Definition hwcontext.h:633
iterator(DeviceType type, const char *device)
Start at the given type and probe it immediately.
Definition hwcontext.h:622
const DeviceProbe & reference
Reference to the probe result.
Definition hwcontext.h:610
bool operator!=(const iterator &other) const noexcept
Compare positions.
Definition hwcontext.h:679
pointer operator->() const noexcept
Result of the current probe.
Definition hwcontext.h:639
iterator()=default
End iterator.
const DeviceProbe * pointer
Pointer to the probe result.
Definition hwcontext.h:608
std::ptrdiff_t difference_type
Distance type required by the iterator traits.
Definition hwcontext.h:606
iterator & operator++() noexcept
Move to the next type and probe it.
Definition hwcontext.h:646
bool operator==(const iterator &other) const noexcept
Compare positions.
Definition hwcontext.h:671
std::forward_iterator_tag iterator_category
Standard iterator category tag.
Definition hwcontext.h:602
DeviceProbe value_type
Type produced by dereferencing.
Definition hwcontext.h:604
Lazy range that probes every compiled device type.
Definition hwcontext.h:593
DeviceProbeRange & operator=(DeviceProbeRange other)
Assign through copy-and-swap.
Definition hwcontext.h:755
DeviceProbeRange(DeviceProbeRange &&other) noexcept
Move the range.
Definition hwcontext.h:741
DeviceProbeRange(const char *device) noexcept
Range over all types with a borrowed device selector.
Definition hwcontext.h:713
iterator begin() const
Start probing.
Definition hwcontext.h:782
DeviceProbeRange(const std::string &device)
Range over all types with a device selector stored by the range.
Definition hwcontext.h:722
void swap(DeviceProbeRange &other) noexcept
Exchange the state of two ranges, fixing up the stored selectors.
Definition hwcontext.h:766
DeviceProbeRange(const DeviceProbeRange &other)
Copy the range, keeping the stored selector valid.
Definition hwcontext.h:731
DeviceProbeRange()=default
Empty range, used as the past-the-end value.
iterator end() const
End of the range.
Definition hwcontext.h:791
Forward iterator over the device types FFmpeg was built with.
Definition hwcontext.h:449
void pointer
No pointer-to-element type: the value is produced on dereference.
Definition hwcontext.h:458
DeviceType value_type
Type produced by dereferencing.
Definition hwcontext.h:454
iterator()=default
End iterator.
std::ptrdiff_t difference_type
Distance type required by the iterator traits.
Definition hwcontext.h:456
iterator operator++(int) noexcept
Advance, returning the previous position.
Definition hwcontext.h:494
iterator & operator++() noexcept
Advance to the next type.
Definition hwcontext.h:484
DeviceType reference
Dereference result type.
Definition hwcontext.h:460
DeviceType operator*() const noexcept
Current type.
Definition hwcontext.h:478
iterator(DeviceType type) noexcept
Start the iteration at the given type.
Definition hwcontext.h:471
bool operator==(const iterator &other) const noexcept
Compare positions.
Definition hwcontext.h:507
bool operator!=(const iterator &other) const noexcept
Compare positions.
Definition hwcontext.h:515
std::forward_iterator_tag iterator_category
Standard iterator category tag.
Definition hwcontext.h:452
Non-owning iterator over AVHWDeviceType values.
Definition hwcontext.h:443
iterator begin() const noexcept
First compiled device type.
Definition hwcontext.h:526
iterator end() const noexcept
End of the range.
Definition hwcontext.h:535
Wrapper for AVHWDeviceType.
Definition hwcontext.h:53
static DeviceType fromName(const char *name) noexcept
Look a device type up by its FFmpeg name.
const char * name() const noexcept
FFmpeg name of the device type, or "none".
bool isNone() const noexcept
Whether the wrapper holds no device type.
Definition hwcontext.h:84
constexpr DeviceType(AVHWDeviceType type=AV_HWDEVICE_TYPE_NONE) noexcept
Wrap an FFmpeg device type.
Definition hwcontext.h:60
AVHWDeviceType get() const noexcept
The wrapped FFmpeg type.
Definition hwcontext.h:77
Owner of AVHWFramesConstraints.
Definition hwcontext.h:1087
int minWidth() const noexcept
Smallest supported width.
int maxWidth() const noexcept
Largest supported width.
PixelFormatRange hwFormats() const noexcept
Hardware surface formats the device supports.
static FrameConstraints query(const DeviceContext &device, OptionalErrorCode ec=throws())
Ask the device for its frame constraints.
PixelFormatRange swFormats() const noexcept
System-memory formats that can be transferred to the device.
FrameConstraints()=default
Create an empty value; isValid() is false.
bool isValid() const noexcept
Whether constraints were obtained.
Definition hwcontext.h:1113
int maxHeight() const noexcept
Largest supported height.
int minHeight() const noexcept
Smallest supported height.
static FramesContext create(const DeviceContext &device, PixelFormat format, PixelFormat swFormat, int width, int height, int initialPoolSize=0, OptionalErrorCode ec=throws())
Create and initialize a hardware frames context (a pool of surfaces).
PixelFormat format() const noexcept
The hardware format of the surfaces in the pool.
AVHWFramesContext * raw() const noexcept
The underlying FFmpeg frames context.
DeviceContext device() const noexcept
The device this pool belongs to.
PixelFormatList transferFormats(AVHWFrameTransferDirection direction, OptionalErrorCode ec=throws()) const
Formats that frames can be transferred to or from this pool.
AVBufferRef * bufferRef() const noexcept
The reference-counted buffer that owns the frames context.
static FramesContext wrap(AVBufferRef *ref) noexcept
Take a reference to an existing AVHWFrameContext buffer.
int width() const noexcept
Surface width.
bool isValid() const noexcept
Whether the wrapper holds a pool.
PixelFormat swFormat() const noexcept
The system-memory format used to transfer frames into and out of the pool.
int height() const noexcept
Surface height.
T * begin() const noexcept
First element.
Definition hwcontext.h:396
RangeView()=default
Create an empty view.
std::vector< T > toVector() const
Copy the view into a vector.
Definition hwcontext.h:429
T * end() const noexcept
One past the last element.
Definition hwcontext.h:402
T * iterator
Iterator type of the view.
Definition hwcontext.h:371
RangeView(T *data, size_t size) noexcept
Wrap an existing array.
Definition hwcontext.h:387
bool empty() const noexcept
Whether the view has no elements.
Definition hwcontext.h:414
T value_type
Element type of the view.
Definition hwcontext.h:369
const T * const_iterator
Const iterator type of the view.
Definition hwcontext.h:373
size_t size() const noexcept
Number of elements.
Definition hwcontext.h:408
T & operator[](size_t index) const noexcept
Element access.
Definition hwcontext.h:422
Codec descriptors and lookup by name or id.
AVDictionary wrappers: Dictionary and DictionaryArray.
std::optional< AVCodecHWConfig > findCodecConfig(const Codec &codec, DeviceType type, int methodsMask=AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX|AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX)
The first hardware configuration that matches a device type.
CodecRange findDecoders(AVCodecID id, DeviceType type) noexcept
All decoders for a codec identifier that support a device type.
bool isDeviceAvailable(DeviceType type, const char *device=nullptr)
Check whether one device type can be created.
DeviceTypeRange allDeviceTypes() noexcept
All hardware device types compiled into FFmpeg.
CodecConfigRange codecConfigs(const Codec &codec) noexcept
All hardware configurations of a codec.
FrameConstraints frameConstraints(const DeviceContext &device, OptionalErrorCode ec=throws())
Query hardware frames constraints for a device.
CodecRange findEncoders(AVCodecID id, DeviceType type) noexcept
All encoders for a codec identifier that support a device type.
Codec findEncoder(AVCodecID id, DeviceType type) noexcept
The first encoder for a codec identifier that supports a device type.
EncodingMode
How an encoder receives hardware frames.
Definition hwcontext.h:36
@ Auto
Prefer a hardware frames context, fall back to a device context.
Definition hwcontext.h:37
@ Frames
Use AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX only.
Definition hwcontext.h:38
@ Device
Use AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX only.
Definition hwcontext.h:39
DeviceProbeRange probeDevices(const char *device=nullptr)
Probe every compiled device type with a borrowed selector.
PixelFormat hwPixelFormat(DeviceType type) noexcept
Hardware pixel format used by a device type.
Codec findDecoder(AVCodecID id, DeviceType type) noexcept
The first decoder for a codec identifier that supports a device type.
Root namespace of the library.
Definition audioresampler.h:19
OptionalErrorCode throws()
Helper to construct null OptionalErrorCode object.
Definition averror.h:310
std::error_code make_ffmpeg_error(int code)
Build an error code from an FFmpeg return value.
Definition averror.h:262
Result of probing one device type: whether it could be created, and why not.
Definition hwcontext.h:552
bool available
Whether a device of this type could be created.
Definition hwcontext.h:560
std::error_code error
Why the probe failed; empty when available is true.
Definition hwcontext.h:564
DeviceType type
The device type that was probed.
Definition hwcontext.h:556