CVB++ 14.1
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
14CVB_BEGIN_INLINE_NS
15
16template <>
17inline HandleGuard<CodecBridge::Frame>::HandleGuard(void* handle) noexcept
18 : HandleGuard<CodecBridge::Frame>(handle, [](void* handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
19{
20}
21
22namespace CodecBridge
23{
24
29class Frame final
30{
31
32public:
33
35
45 {
46 return Internal::DoResCallObjectOut<Frame>([&](void*& handle)
47 {
48 return CVB_CALL_CAPI(CVCBCreateFrame(size.Width(), size.Height(), static_cast<CExports::CVCBPixelFormat>(format), buffer, handle));
49 });
50 }
51
53
62 {
63 return Create(size, format, nullptr);
64 }
65
66 static std::unique_ptr<Frame> FromHandle(HandleGuard<Frame>&& guard)
67 {
68 if (!guard.Handle())
69 throw std::runtime_error("handle must not be null");
70
71 return std::make_unique<Frame>(std::move(guard));
72 }
73
74 explicit Frame(HandleGuard<Frame>&& guard) noexcept
75 : handle_(std::move(guard))
76 {
77
78 }
79
80 Frame(const Frame& other) = delete;
81 Frame& operator=(const Frame& other) = delete;
82 Frame(Frame&& other) = delete;
83 Frame& operator=(Frame&& other) = delete;
84
85 ~Frame() = default;
86
87 void* Handle() const noexcept
88 {
89 return handle_.Handle();
90 }
91
93
97 int Width() const
98 {
99 return static_cast<int>(Property<CExports::cvbdim_t>(CExports::CVCBFP_Width));
100 }
101
103
107 int Height() const
108 {
109 return static_cast<int>(Property<CExports::cvbdim_t>(CExports::CVCBFP_Height));
110 }
111
113
118 {
119 return { Width(), Height() };
120 }
121
123
129 int64_t PresentationTimeStamp() const
130 {
131 return static_cast<int64_t>(Property<CExports::cvbint64_t>(CExports::CVCBFP_PresentationTimestamp));
132 }
133
135
141 void SetPresentationTimeStamp(int64_t value)
142 {
143 SetProperty(static_cast<CExports::cvbint64_t>(value), CExports::CVCBFP_PresentationTimestamp);
144 }
145
147
153 {
154 return static_cast<CodecBridge::PixelFormat>(Property<CExports::CVCBPixelFormat>(CExports::CVCBFP_PixelFormat));
155 }
156
158
164 void UserData(void* buffer, size_t& size) const
165 {
166 CVB_CALL_CAPI_CHECKED(CVCBFrameGetProperty(Handle(), CExports::CVCBFP_SEIUserData, buffer, size));
167 }
168
170
178 {
179 auto timeBase = Property<CExports::CVCBRational>(CExports::CVCBFP_TimeBase);
180 return { timeBase.Numerator , timeBase.Denominator };
181 }
182
184
192 void SetUserData(void* buffer, size_t size)
193 {
194 CVB_CALL_CAPI_CHECKED(CVCBFrameSetProperty(Handle(), CExports::CVCBFP_SEIUserData, buffer, size));
195 }
196
198
204 {
205 CVB_CALL_CAPI_CHECKED(CVCBFrameSetProperty(Handle(), CExports::CVCBFP_SEIUserData, nullptr, 0));
206 }
207
209
217 {
218 if (!CExports::IsImage(Handle()))
219 throw std::runtime_error("frame is not an image");
220
221 if (!CExports::ShareObject(Handle()))
222 throw std::runtime_error("failed to share frame handle");
223
224 HandleGuard<Image> guard(Handle());
225 return Image::FromHandle(std::move(guard));
226
227 }
228
230
234 int PlanesCount() const noexcept
235 {
236 CExports::cvbdim_t numElements = 0;
237 CExports::CVCPlaneEnumGetCount(Handle(), numElements);
238 return static_cast<int>(numElements);
239 }
240
242
248 PlanePtr Plane(int index) const
249 {
250 std::unique_lock<std::mutex> guard(planesMtx_);
251 auto candidate = planes_.find(index);
252 if(candidate != planes_.end())
253 if (auto plane = candidate->second.lock())
254 return plane;
255
256 auto plane = Internal::DoResCallShareOut<Cvb::Plane>([&](void*& handle)
257 {
258 return CVB_CALL_CAPI(CVCPlaneEnumGetAt(Handle(), static_cast<CExports::cvbdim_t>(index), handle));
259 });
260
261 planes_[index] = plane;
262 return plane;
263 }
264
265private:
266
267 template<class T>
268 T Property(CExports::CVCBFrameProperty property) const
269 {
270 T value = {};
271 size_t size = sizeof(T);
272 CVB_CALL_CAPI_CHECKED(CVCBFrameGetProperty(Handle(), property, &value, size));
273 return value;
274 }
275
276 template<class T>
277 void SetProperty( T value, CExports::CVCBFrameProperty property) const
278 {
279 CVB_CALL_CAPI_CHECKED(CVCBFrameSetProperty(Handle(), property, &value, sizeof(T)));
280 }
281
282 HandleGuard<Frame> handle_;
283 mutable std::mutex planesMtx_;
285};
286
287}
288
289CVB_END_INLINE_NS
290}
A video frame for encoding and decoding.
Definition: frame.hpp:30
PlanePtr Plane(int index) const
Get a plane of this frame at the given index.
Definition: frame.hpp:248
Size2D< int > Size() const
Get the frame size.
Definition: frame.hpp:117
void UserData(void *buffer, size_t &size) const
Get the user data if available.
Definition: frame.hpp:164
void SetUserData(void *buffer, size_t size)
Set the user data for this frame.
Definition: frame.hpp:192
int PlanesCount() const noexcept
Get the number of planes in this frame.
Definition: frame.hpp:234
CodecBridge::PixelFormat PixelFormat() const
Get the frame pixel format.
Definition: frame.hpp:152
int64_t PresentationTimeStamp() const
Get the frame presentation time stamp.
Definition: frame.hpp:129
int Height() const
Get the frame height.
Definition: frame.hpp:107
Rational TimeBase() const
Get the time base for this frame.
Definition: frame.hpp:177
int Width() const
Get the frame width.
Definition: frame.hpp:97
std::unique_ptr< Image > AsImage() const
Get this frame as image.
Definition: frame.hpp:216
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:44
void DeleteUserData()
Delete user data.
Definition: frame.hpp:203
void SetPresentationTimeStamp(int64_t value)
Set the presentation time stamp for this frame.
Definition: frame.hpp:141
static std::unique_ptr< Frame > Create(Size2D< int > size, CodecBridge::PixelFormat format)
Creates a frame.
Definition: frame.hpp:61
A pair of rational numbers.
Definition: codec_bridge.hpp:200
static std::unique_ptr< Image > FromHandle(HandleGuard< Image > &&guard)
Creates an image from a classic API handle.
Definition: decl_image.hpp:153
T Height() const noexcept
Gets the vertical component of the size.
Definition: size_2d.hpp:79
T Width() const noexcept
Gets the horizontal component of the size.
Definition: size_2d.hpp:59
PixelFormat
Subset of FFmpeg pixel formats.
Definition: codec_bridge.hpp:92
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24