CVB++ 15.0
frame.hpp
1#pragma once
2
3#include <map>
4
5#include "codec_bridge.hpp"
6#include "../global.hpp"
7#include "../size_2d.hpp"
8#include "../image.hpp"
9#include "../plane.hpp"
10
11namespace Cvb
12{
13
14 CVB_BEGIN_INLINE_NS
15
16 template <>
17 inline HandleGuard<CodecBridge::Frame>::HandleGuard(void *handle) noexcept
18 : HandleGuard<CodecBridge::Frame>(handle, [](void *handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
19 {
20 }
21
22 namespace CodecBridge
23 {
24
29 class Frame final
30 {
31
32 public:
34
44 {
45 return Internal::DoResCallObjectOut<Frame>([&](void *&handle) {
46 return CVB_CALL_CAPI(CVCBCreateFrame(size.Width(), size.Height(),
47 static_cast<CExports::CVCBPixelFormat>(format), buffer, handle));
48 });
49 }
50
52
61 {
62 return Create(size, format, nullptr);
63 }
64
65 static std::unique_ptr<Frame> FromHandle(HandleGuard<Frame> &&guard)
66 {
67 if (!guard.Handle())
68 throw std::runtime_error("handle must not be null");
69
70 return std::make_unique<Frame>(std::move(guard));
71 }
72
73 explicit Frame(HandleGuard<Frame> &&guard) noexcept
74 : handle_(std::move(guard))
75 {
76 }
77
78 Frame(const Frame &other) = delete;
79 Frame &operator=(const Frame &other) = delete;
80 Frame(Frame &&other) = delete;
81 Frame &operator=(Frame &&other) = delete;
82
83 ~Frame() = default;
84
85 void *Handle() const noexcept
86 {
87 return handle_.Handle();
88 }
89
91
95 int Width() const
96 {
97 return static_cast<int>(Property<CExports::cvbdim_t>(CExports::CVCBFP_Width));
98 }
99
101
105 int Height() const
106 {
107 return static_cast<int>(Property<CExports::cvbdim_t>(CExports::CVCBFP_Height));
108 }
109
111
116 {
117 return {Width(), Height()};
118 }
119
121
127 int64_t PresentationTimeStamp() const
128 {
129 return static_cast<int64_t>(Property<CExports::cvbint64_t>(CExports::CVCBFP_PresentationTimestamp));
130 }
131
133
139 void SetPresentationTimeStamp(int64_t value)
140 {
141 SetProperty(static_cast<CExports::cvbint64_t>(value), CExports::CVCBFP_PresentationTimestamp);
142 }
143
145
151 {
152 return static_cast<CodecBridge::PixelFormat>(Property<CExports::CVCBPixelFormat>(CExports::CVCBFP_PixelFormat));
153 }
154
156
163 void UserData(void *buffer, size_t &size) const
164 {
165 CVB_CALL_CAPI_CHECKED(CVCBFrameGetProperty(Handle(), CExports::CVCBFP_SEIUserData, buffer, size));
166 }
167
169
177 {
178 auto timeBase = Property<CExports::CVCBRational>(CExports::CVCBFP_TimeBase);
179 return {timeBase.Numerator, timeBase.Denominator};
180 }
181
183
191 void SetUserData(void *buffer, size_t size)
192 {
193 CVB_CALL_CAPI_CHECKED(CVCBFrameSetProperty(Handle(), CExports::CVCBFP_SEIUserData, buffer, size));
194 }
195
197
203 {
204 CVB_CALL_CAPI_CHECKED(CVCBFrameSetProperty(Handle(), CExports::CVCBFP_SEIUserData, nullptr, 0));
205 }
206
208
216 {
217 if (!CExports::IsImage(Handle()))
218 throw std::runtime_error("frame is not an image");
219
220 if (!CExports::ShareObject(Handle()))
221 throw std::runtime_error("failed to share frame handle");
222
223 HandleGuard<Image> guard(Handle());
224 return Image::FromHandle(std::move(guard));
225 }
226
228
232 int PlanesCount() const noexcept
233 {
234 CExports::cvbdim_t numElements = 0;
235 CExports::CVCPlaneEnumGetCount(Handle(), numElements);
236 return static_cast<int>(numElements);
237 }
238
240
246 PlanePtr Plane(int index) const
247 {
248 std::unique_lock<std::mutex> guard(planesMtx_);
249 auto candidate = planes_.find(index);
250 if (candidate != planes_.end())
251 if (auto plane = candidate->second.lock())
252 return plane;
253
254 auto plane = Internal::DoResCallShareOut<Cvb::Plane>([&](void *&handle) {
255 return CVB_CALL_CAPI(CVCPlaneEnumGetAt(Handle(), static_cast<CExports::cvbdim_t>(index), handle));
256 });
257
258 planes_[index] = plane;
259 return plane;
260 }
261
262 private:
263 template <class T>
264 T Property(CExports::CVCBFrameProperty property) const
265 {
266 T value = {};
267 size_t size = sizeof(T);
268 CVB_CALL_CAPI_CHECKED(CVCBFrameGetProperty(Handle(), property, &value, size));
269 return value;
270 }
271
272 template <class T>
273 void SetProperty(T value, CExports::CVCBFrameProperty property) const
274 {
275 CVB_CALL_CAPI_CHECKED(CVCBFrameSetProperty(Handle(), property, &value, sizeof(T)));
276 }
277
278 HandleGuard<Frame> handle_;
279 mutable std::mutex planesMtx_;
280 mutable std::map<int, std::weak_ptr<Cvb::Plane>> planes_;
281 };
282
283 } // namespace CodecBridge
284
285 CVB_END_INLINE_NS
286} // namespace Cvb
PlanePtr Plane(int index) const
Get a plane of this frame at the given index.
Definition frame.hpp:246
Size2D< int > Size() const
Get the frame size.
Definition frame.hpp:115
void UserData(void *buffer, size_t &size) const
Get the user data if available.
Definition frame.hpp:163
void SetUserData(void *buffer, size_t size)
Set the user data for this frame.
Definition frame.hpp:191
int PlanesCount() const noexcept
Get the number of planes in this frame.
Definition frame.hpp:232
CodecBridge::PixelFormat PixelFormat() const
Get the frame pixel format.
Definition frame.hpp:150
int64_t PresentationTimeStamp() const
Get the frame presentation time stamp.
Definition frame.hpp:127
int Height() const
Get the frame height.
Definition frame.hpp:105
Rational TimeBase() const
Get the time base for this frame.
Definition frame.hpp:176
int Width() const
Get the frame width.
Definition frame.hpp:95
std::unique_ptr< Image > AsImage() const
Get this frame as image.
Definition frame.hpp:215
static std::unique_ptr< Frame > Create(Size2D< int > size, CodecBridge::PixelFormat format, uint8_t *buffer)
Creates a frame as view on external memory.
Definition frame.hpp:43
void DeleteUserData()
Delete user data.
Definition frame.hpp:202
void SetPresentationTimeStamp(int64_t value)
Set the presentation time stamp for this frame.
Definition frame.hpp:139
static std::unique_ptr< Frame > Create(Size2D< int > size, CodecBridge::PixelFormat format)
Creates a frame.
Definition frame.hpp:60
A pair of rational numbers.
Definition codec_bridge.hpp:199
static std::unique_ptr< Image > FromHandle(HandleGuard< Image > &&guard)
Creates an image from a classic API handle.
Definition decl_image.hpp:155
Stores a pair of numbers that represents the width and the height of a subject, typically a rectangle...
Definition size_2d.hpp:20
T Height() const noexcept
Gets the vertical component of the size.
Definition size_2d.hpp:77
T Width() const noexcept
Gets the horizontal component of the size.
Definition size_2d.hpp:57
cvbbool_t ReleaseObject(OBJ &Object)
T move(T... args)
Namespace for encoding and decoding videos.
Definition codec_bridge.hpp:24
PixelFormat
Subset of FFmpeg pixel formats.
Definition codec_bridge.hpp:91
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
std::shared_ptr< Plane > PlanePtr
Convenience shared pointer for Plane.
Definition global.hpp:78