avcpp 3.1.0
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
hwcontext.h
Go to the documentation of this file.
1#pragma once
2
9
10#include <cstddef>
11#include <iterator>
12#include <memory>
13#include <optional>
14#include <string>
15#include <system_error>
16#include <vector>
17
18#include "avcompat.h"
19#include "averror.h"
20#include "buffer.h"
21#include "codec.h"
22#include "dictionary.h"
23#include "pixelformat.h"
24
25extern "C" {
26#include <libavutil/hwcontext.h>
27}
28
29namespace av {
30namespace hw {
31
35enum class EncodingMode
36{
40};
41
42class DeviceContext;
43class FramesContext;
44class PixelFormatList;
45
53{
54public:
60 constexpr DeviceType(AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE) noexcept
61 : m_type(type)
62 {}
63
70 operator AVHWDeviceType() const noexcept { return m_type; }
71
77 AVHWDeviceType get() const noexcept { return m_type; }
78
84 bool isNone() const noexcept { return m_type == AV_HWDEVICE_TYPE_NONE; }
85
91 const char* name() const noexcept;
92
104 static DeviceType fromName(const char* name) noexcept;
113 static DeviceType fromName(const std::string& name) noexcept;
114
115private:
116 AVHWDeviceType m_type = AV_HWDEVICE_TYPE_NONE;
117};
118
125class DeviceContext
126{
127 friend class FramesContext;
128
129public:
130 DeviceContext() = default;
131
147 static DeviceContext create(DeviceType type,
148 const char* device = nullptr,
149 const Dictionary& options = {},
150 OptionalErrorCode ec = throws());
151
162 static DeviceContext create(DeviceType type,
163 const std::string& device,
164 const Dictionary& options = {},
165 OptionalErrorCode ec = throws());
166
181 static DeviceContext derive(const DeviceContext& source,
183 const Dictionary& options = {},
184 OptionalErrorCode ec = throws());
185
197 static DeviceContext wrap(AVBufferRef* ref) noexcept;
198
204 bool isValid() const noexcept;
210 DeviceType type() const noexcept;
211
219 AVHWDeviceContext* raw() const noexcept;
228 AVBufferRef* bufferRef() const noexcept;
229
230private:
231 explicit DeviceContext(BufferRef&& ref) noexcept
232 : m_ref(std::move(ref))
233 {}
234
235 BufferRef m_ref;
236};
237
241class FramesContext
242{
243public:
244 FramesContext() = default;
245
265 static FramesContext create(const DeviceContext& device,
268 int width,
269 int height,
270 int initialPoolSize = 0,
271 OptionalErrorCode ec = throws());
272
280 static FramesContext wrap(AVBufferRef* ref) noexcept;
281
287 bool isValid() const noexcept;
293 DeviceContext device() const noexcept;
299 PixelFormat format() const noexcept;
305 PixelFormat swFormat() const noexcept;
311 int width() const noexcept;
317 int height() const noexcept;
318
332 PixelFormatList transferFormats(AVHWFrameTransferDirection direction,
333 OptionalErrorCode ec = throws()) const;
334
342 AVHWFramesContext* raw() const noexcept;
351 AVBufferRef* bufferRef() const noexcept;
352
353private:
354 explicit FramesContext(BufferRef&& ref) noexcept
355 : m_ref(std::move(ref))
356 {}
357
358 BufferRef m_ref;
359};
360
364template<typename T>
366{
367public:
369 using value_type = T;
371 using iterator = T*;
373 using const_iterator = const T*;
374
378 RangeView() = default;
387 RangeView(T* data, size_t size) noexcept
388 : m_data(data), m_size(size)
389 {}
390
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]; }
423
429 std::vector<T> toVector() const
430 {
431 return m_data ? std::vector<T>(m_data, m_data + m_size) : std::vector<T>();
432 }
433
434private:
435 T* m_data = nullptr;
436 size_t m_size = 0;
437};
438
443{
444public:
449 {
450 public:
452 using iterator_category = std::forward_iterator_tag;
456 using difference_type = std::ptrdiff_t;
458 using pointer = void;
461
465 iterator() = default;
471 explicit iterator(DeviceType type) noexcept : m_type(type) {}
472
478 DeviceType operator*() const noexcept { return m_type; }
485 {
486 m_type = DeviceType(av_hwdevice_iterate_types(m_type.get()));
487 return *this;
488 }
489
494 iterator operator++(int) noexcept
495 {
496 iterator tmp = *this;
497 ++(*this);
498 return tmp;
499 }
500
507 bool operator==(const iterator& other) const noexcept { return m_type.get() == other.m_type.get(); }
515 bool operator!=(const iterator& other) const noexcept { return !(*this == other); }
516
517 private:
518 DeviceType m_type;
519 };
520
526 iterator begin() const noexcept
527 {
528 return iterator(DeviceType(av_hwdevice_iterate_types(AV_HWDEVICE_TYPE_NONE)));
529 }
530
535 iterator end() const noexcept { return iterator(DeviceType(AV_HWDEVICE_TYPE_NONE)); }
536};
537
547
552{
560 bool available = false;
564 std::error_code error;
565};
566
593{
594public:
599 {
600 public:
602 using iterator_category = std::forward_iterator_tag;
606 using difference_type = std::ptrdiff_t;
608 using pointer = const DeviceProbe*;
610 using reference = const DeviceProbe&;
611
615 iterator() = default;
622 iterator(DeviceType type, const char* device)
623 : m_type(type), m_device(device)
624 {
625 probe();
626 }
627
633 reference operator*() const noexcept { return m_probe; }
639 pointer operator->() const noexcept { return &m_probe; }
640
647 {
648 if (!m_type.isNone())
649 m_type = DeviceType(av_hwdevice_iterate_types(m_type.get()));
650 probe();
651 return *this;
652 }
653
658 iterator operator++(int) noexcept
659 {
660 iterator tmp = *this;
661 ++(*this);
662 return tmp;
663 }
664
671 bool operator==(const iterator& other) const noexcept { return m_type.get() == other.m_type.get(); }
679 bool operator!=(const iterator& other) const noexcept { return !(*this == other); }
680
681 private:
682 void probe() noexcept
683 {
684 m_probe = DeviceProbe{};
685 m_probe.type = m_type;
686 if (m_type.isNone())
687 return;
688
689 AVBufferRef* ref = nullptr;
690 int ret = av_hwdevice_ctx_create(&ref, m_type.get(), m_device, nullptr, 0);
691 if (ret < 0) {
692 m_probe.error = make_ffmpeg_error(ret);
693 return;
694 }
695 m_probe.available = true;
696 av_buffer_unref(&ref);
697 }
698
699 DeviceType m_type;
700 const char* m_device = nullptr;
701 DeviceProbe m_probe;
702 };
703
707 DeviceProbeRange() = default;
713 explicit DeviceProbeRange(const char* device) noexcept
714 : m_device(device)
715 {}
716
722 explicit DeviceProbeRange(const std::string& device)
723 : m_owned(device), m_device(m_owned->c_str())
724 {}
725
732 : m_owned(other.m_owned),
733 m_device(other.m_owned ? m_owned->c_str() : other.m_device)
734 {}
735
742 : m_owned(std::move(other.m_owned)),
743 m_device(m_owned ? m_owned->c_str() : other.m_device)
744 {
745 other.m_device = nullptr;
746 }
747
756 {
757 swap(other);
758 return *this;
759 }
760
766 void swap(DeviceProbeRange& other) noexcept
767 {
768 using std::swap;
769 swap(m_owned, other.m_owned);
770 swap(m_device, other.m_device);
771 if (m_owned)
772 m_device = m_owned->c_str();
773 if (other.m_owned)
774 other.m_device = other.m_owned->c_str();
775 }
776
783 {
784 return iterator(DeviceType(av_hwdevice_iterate_types(AV_HWDEVICE_TYPE_NONE)), m_device);
785 }
786
791 iterator end() const { return iterator(); }
792
793private:
794 std::optional<std::string> m_owned;
795 const char* m_device = nullptr;
796};
797
809DeviceProbeRange probeDevices(const char* device = nullptr);
817DeviceProbeRange probeDevices(const std::string& device);
818
828bool isDeviceAvailable(DeviceType type, const char* device = nullptr);
837bool isDeviceAvailable(DeviceType type, const std::string& device);
838
843{
844public:
849 {
850 public:
852 using iterator_category = std::forward_iterator_tag;
856 using difference_type = std::ptrdiff_t;
858 using pointer = void;
861
865 iterator() = default;
871 explicit iterator(const AVPixelFormat* ptr) noexcept : m_ptr(ptr) {}
872
878 PixelFormat operator*() const noexcept { return PixelFormat(*m_ptr); }
885 {
886 ++m_ptr;
887 return *this;
888 }
889
894 iterator operator++(int) noexcept
895 {
896 iterator tmp = *this;
897 ++(*this);
898 return tmp;
899 }
900
907 bool operator==(const iterator& other) const noexcept { return m_ptr == other.m_ptr; }
915 bool operator!=(const iterator& other) const noexcept { return m_ptr != other.m_ptr; }
916
917 private:
918 const AVPixelFormat* m_ptr = nullptr;
919 };
920
924 PixelFormatRange() = default;
933 explicit PixelFormatRange(const AVPixelFormat* formats) noexcept;
934
940 iterator begin() const noexcept { return iterator(m_begin); }
946 iterator end() const noexcept { return iterator(m_end); }
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; }
966 PixelFormat operator[](size_t index) const noexcept { return PixelFormat(m_begin[index]); }
967
973 std::vector<PixelFormat> toVector() const;
974
975private:
976 const AVPixelFormat* m_begin = nullptr;
977 const AVPixelFormat* m_end = nullptr;
978};
979
986{
987public:
990
994 PixelFormatList() = default;
1004 explicit PixelFormatList(AVPixelFormat* formats) noexcept
1005 : m_formats(formats)
1006 {}
1007
1011
1018 : m_formats(other.m_formats)
1019 {
1020 other.m_formats = nullptr;
1021 }
1022
1030
1039
1045 PixelFormatRange range() const noexcept { return PixelFormatRange(m_formats); }
1051 iterator begin() const noexcept { return range().begin(); }
1057 iterator end() const noexcept { return range().end(); }
1063 size_t size() const noexcept { return range().size(); }
1069 bool empty() const noexcept { return range().empty(); }
1075 std::vector<PixelFormat> toVector() const { return range().toVector(); }
1076
1077private:
1078 AVPixelFormat* m_formats = nullptr;
1079};
1080
1087{
1088public:
1092 FrameConstraints() = default;
1093
1106 OptionalErrorCode ec = throws());
1107
1113 bool isValid() const noexcept { return static_cast<bool>(m_raw); }
1119 PixelFormatRange hwFormats() const noexcept;
1125 PixelFormatRange swFormats() const noexcept;
1131 int minWidth() const noexcept;
1137 int maxWidth() const noexcept;
1143 int minHeight() const noexcept;
1149 int maxHeight() const noexcept;
1150
1151private:
1152 struct Deleter
1153 {
1154 void operator()(AVHWFramesConstraints* ptr) const noexcept;
1155 };
1156
1157 std::unique_ptr<AVHWFramesConstraints, Deleter> m_raw;
1158};
1159
1172 OptionalErrorCode ec = throws());
1173
1178{
1179public:
1184 {
1185 public:
1187 using iterator_category = std::forward_iterator_tag;
1189 using value_type = AVCodecHWConfig;
1191 using difference_type = std::ptrdiff_t;
1193 using pointer = const AVCodecHWConfig*;
1195 using reference = const AVCodecHWConfig&;
1196
1200 iterator() = default;
1207 iterator(const AVCodec* codec, int index) noexcept
1208 : m_codec(codec), m_index(index)
1209 {
1210 refresh();
1211 }
1212
1218 reference operator*() const noexcept { return m_value; }
1224 pointer operator->() const noexcept { return &m_value; }
1225
1232 {
1233 ++m_index;
1234 refresh();
1235 return *this;
1236 }
1237
1242 iterator operator++(int) noexcept
1243 {
1244 iterator tmp = *this;
1245 ++(*this);
1246 return tmp;
1247 }
1248
1256 bool operator==(const iterator& other) const noexcept
1257 {
1258 if (m_codec != other.m_codec)
1259 return false;
1260 if (!m_valid || !other.m_valid)
1261 return m_valid == other.m_valid;
1262 return m_index == other.m_index;
1263 }
1264
1271 bool operator!=(const iterator& other) const noexcept { return !(*this == other); }
1272
1273 private:
1274 void refresh() noexcept
1275 {
1276 m_valid = false;
1277 if (!m_codec || m_index < 0)
1278 return;
1279 const AVCodecHWConfig* cfg = avcodec_get_hw_config(m_codec, m_index);
1280 if (!cfg)
1281 return;
1282 m_value = *cfg;
1283 m_valid = true;
1284 }
1285
1286 const AVCodec* m_codec = nullptr;
1287 int m_index = 0;
1288 bool m_valid = false;
1289 AVCodecHWConfig m_value{};
1290 };
1291
1295 CodecConfigRange() = default;
1301 explicit CodecConfigRange(const Codec& codec) noexcept
1302 : m_codec(codec.raw())
1303 {}
1304
1310 iterator begin() const { return iterator(m_codec, 0); }
1316 iterator end() const { return iterator(m_codec, -1); }
1317
1323 bool empty() const { return begin() == end(); }
1324
1325private:
1326 const AVCodec* m_codec = nullptr;
1327};
1328
1336CodecConfigRange codecConfigs(const Codec& codec) noexcept;
1337
1349std::optional<AVCodecHWConfig> findCodecConfig(
1350 const Codec& codec,
1351 DeviceType type,
1352 int methodsMask = AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX |
1353 AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX);
1354
1359{
1360public:
1365 {
1366 public:
1368 using iterator_category = std::forward_iterator_tag;
1372 using difference_type = std::ptrdiff_t;
1374 using pointer = void;
1377
1381 iterator() = default;
1389 iterator(AVCodecID id, DeviceType type, bool encoders)
1390 : m_id(id), m_type(type), m_encoders(encoders)
1391 {
1392 advance();
1393 }
1394
1400 Codec operator*() const noexcept { return Codec(m_codec); }
1407 {
1408 advance();
1409 return *this;
1410 }
1411
1416 iterator operator++(int) noexcept
1417 {
1418 iterator tmp = *this;
1419 ++(*this);
1420 return tmp;
1421 }
1422
1429 bool operator==(const iterator& other) const noexcept { return m_codec == other.m_codec; }
1437 bool operator!=(const iterator& other) const noexcept { return !(*this == other); }
1438
1439 private:
1440 void advance() noexcept
1441 {
1442 for (;;) {
1443 m_codec = av_codec_iterate(&m_opaque);
1444 if (!m_codec)
1445 return;
1446 if (m_encoders ? !av_codec_is_encoder(m_codec) : !av_codec_is_decoder(m_codec))
1447 continue;
1448 if (m_id != AV_CODEC_ID_NONE && m_codec->id != m_id)
1449 continue;
1450 if (!codecSupportsDevice(m_codec, m_type.get(), m_encoders))
1451 continue;
1452 return;
1453 }
1454 }
1455
1456 static bool codecSupportsDevice(const AVCodec* codec, AVHWDeviceType type, bool encoders) noexcept;
1457
1458 AVCodecID m_id = AV_CODEC_ID_NONE;
1459 DeviceType m_type;
1460 bool m_encoders = false;
1461 void* m_opaque = nullptr;
1462 const AVCodec* m_codec = nullptr;
1463 };
1464
1468 CodecRange() = default;
1476 CodecRange(AVCodecID id, DeviceType type, bool encoders) noexcept
1477 : m_id(id), m_type(type), m_encoders(encoders)
1478 {}
1479
1485 iterator begin() const { return iterator(m_id, m_type, m_encoders); }
1491 iterator end() const { return iterator(); }
1492
1493private:
1494 AVCodecID m_id = AV_CODEC_ID_NONE;
1495 DeviceType m_type;
1496 bool m_encoders = false;
1497};
1498
1510CodecRange findDecoders(AVCodecID id, DeviceType type) noexcept;
1519CodecRange findEncoders(AVCodecID id, DeviceType type) noexcept;
1528Codec findDecoder(AVCodecID id, DeviceType type) noexcept;
1537Codec findEncoder(AVCodecID id, DeviceType type) noexcept;
1538
1551
1552} // namespace hw
1553} // namespace av
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
The PixelFormat class is a simple wrapper for AVPixelFormat that allow to acces it it properties.
Definition pixelformat.h:41
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.
Owner of an AVPixelFormat array allocated by FFmpeg.
Definition hwcontext.h:986
PixelFormatList & operator=(PixelFormatList &&other) noexcept
Move assignment: frees the current array and takes the other one.
bool empty() const noexcept
Whether the list has no formats.
Definition hwcontext.h:1069
std::vector< PixelFormat > toVector() const
Copy the list into a vector.
Definition hwcontext.h:1075
PixelFormatList(AVPixelFormat *formats) noexcept
Take ownership of a format array allocated by FFmpeg.
Definition hwcontext.h:1004
PixelFormatRange range() const noexcept
Non-owning view over the array.
Definition hwcontext.h:1045
~PixelFormatList()
Free the owned array.
iterator end() const noexcept
One past the last format.
Definition hwcontext.h:1057
iterator begin() const noexcept
First format.
Definition hwcontext.h:1051
PixelFormatList()=default
Create an empty list.
PixelFormatList(const PixelFormatList &)=delete
Copying is disabled: the list owns its array.
size_t size() const noexcept
Number of formats.
Definition hwcontext.h:1063
PixelFormatRange::iterator iterator
Iterator type of the owned array.
Definition hwcontext.h:989
PixelFormatList & operator=(const PixelFormatList &)=delete
Copy assignment is disabled: the list owns its array.
PixelFormatList(PixelFormatList &&other) noexcept
Move the array out of another list.
Definition hwcontext.h:1017
Forward iterator over a null-terminated AVPixelFormat array.
Definition hwcontext.h:849
PixelFormat value_type
Type produced by dereferencing.
Definition hwcontext.h:854
iterator & operator++() noexcept
Advance to the next format.
Definition hwcontext.h:884
PixelFormat operator*() const noexcept
Format at the current position.
Definition hwcontext.h:878
PixelFormat reference
Dereference result type.
Definition hwcontext.h:860
iterator()=default
End iterator.
std::forward_iterator_tag iterator_category
Standard iterator category tag.
Definition hwcontext.h:852
std::ptrdiff_t difference_type
Distance type required by the iterator traits.
Definition hwcontext.h:856
bool operator!=(const iterator &other) const noexcept
Compare positions.
Definition hwcontext.h:915
void pointer
No pointer-to-element type: the value is produced on dereference.
Definition hwcontext.h:858
bool operator==(const iterator &other) const noexcept
Compare positions.
Definition hwcontext.h:907
iterator(const AVPixelFormat *ptr) noexcept
Point the iterator at an array element.
Definition hwcontext.h:871
iterator operator++(int) noexcept
Advance, returning the previous position.
Definition hwcontext.h:894
Non-owning view over a null-terminated AVPixelFormat array.
Definition hwcontext.h:843
iterator end() const noexcept
One past the last format.
Definition hwcontext.h:946
size_t size() const noexcept
Number of formats.
Definition hwcontext.h:952
PixelFormatRange(const AVPixelFormat *formats) noexcept
Wrap a null-terminated format array.
bool empty() const noexcept
Whether the range has no formats.
Definition hwcontext.h:958
std::vector< PixelFormat > toVector() const
Copy the range into a vector.
PixelFormat operator[](size_t index) const noexcept
Format access.
Definition hwcontext.h:966
iterator begin() const noexcept
First format.
Definition hwcontext.h:940
PixelFormatRange()=default
Create an empty range.
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
STL namespace.
Pixel formats.
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