IDS peak IPL v1.17.0
Loading...
Searching...
No Matches
peak_ipl_video_writer.hpp
Go to the documentation of this file.
1
10
11#pragma once
12
18
19#include <cstdint>
20#include <memory>
21#include <string>
22#include <utility>
23
24namespace peak
25{
26namespace ipl
27{
28
29class VideoWriter;
30
31namespace encoder
32{
33
38{
40
41protected:
43
45 {
46 this->m_handle = handle;
47 }
48
49public:
50 IEncoder() = default;
51 virtual ~IEncoder() = default;
53
65 void SetOption(PEAK_IPL_VIDEO_ENCODER_OPTION option, const void* value, size_t count)
66 {
68 [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_VideoWriter_Encoder_SetOption(m_handle, option, value, count); });
69 }
70
83 void GetOption(PEAK_IPL_VIDEO_ENCODER_OPTION option, void* value, size_t count, size_t* outCount)
84 {
86 return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_VideoWriter_Encoder_GetOption(m_handle, option, value, count, outCount);
87 });
88 }
89
102 void OptionRange(PEAK_IPL_VIDEO_ENCODER_OPTION option, void* min, void* max, void* inc)
103 {
106 });
107 }
108};
109
110static std::unique_ptr<IEncoder> Create(PEAK_IPL_VIDEO_ENCODER encoder);
111
116class MJPEGEncoder : public IEncoder
117{
118 friend std::unique_ptr<IEncoder> Create(PEAK_IPL_VIDEO_ENCODER encoder);
119
120 MJPEGEncoder() = default;
121
122public:
123 MJPEGEncoder(const MJPEGEncoder&) = delete;
124 MJPEGEncoder(MJPEGEncoder&&) = default;
125
126 MJPEGEncoder& operator=(const MJPEGEncoder&) = delete;
127 MJPEGEncoder& operator=(MJPEGEncoder&&) = default;
128
134 {
136 }
137
147 void SetQuality(uint32_t quality)
148 {
149 SetOption(PEAK_IPL_VIDEO_ENCODER_OPTION_QUALITY, &quality, sizeof(quality));
150 }
151
159 uint32_t GetQuality()
160 {
161 uint32_t quality = 0;
162 GetOption(PEAK_IPL_VIDEO_ENCODER_OPTION_QUALITY, &quality, sizeof(quality), nullptr);
163 return quality;
164 }
165
174 {
175 double min = 0, max = 0, inc = 0;
177 return { min, max, inc };
178 }
179};
180
189static inline std::vector<PEAK_IPL_VIDEO_ENCODER> GetSupportedEncodersForAContainer(PEAK_IPL_VIDEO_CONTAINER container)
190{
191 size_t length{};
194 });
195 std::vector<PEAK_IPL_VIDEO_ENCODER> encoders(length);
198 container, encoders.data(), &length);
199 });
200 return encoders;
201}
202
211static inline std::vector<PixelFormatName> GetSupportedPixelFormats(PEAK_IPL_VIDEO_ENCODER encoder)
212{
213 size_t sizeOfData{};
216 });
217 std::vector<PixelFormatName> formats(sizeOfData);
220 encoder, reinterpret_cast<PEAK_IPL_PIXEL_FORMAT*>(formats.data()), &sizeOfData);
221 });
222 return formats;
223}
224
234static inline std::unique_ptr<IEncoder> Create(PEAK_IPL_VIDEO_ENCODER encoder)
235{
236 switch (encoder)
237 {
239 return std::unique_ptr<MJPEGEncoder>(new MJPEGEncoder());
240 default:
241 throw InvalidArgumentException("The supplied encoder is not a valid value!");
242 }
243
244}
245
246} // namespace encoder
247
248namespace container
249{
250
255{
257
258protected:
261 {
262 this->m_handle = handle;
263 }
264
265public:
266 IContainer() = default;
267 virtual ~IContainer() = default;
269
281 void SetOption(PEAK_IPL_VIDEO_CONTAINER_OPTION option, const void* value, size_t count)
282 {
285 });
286 }
287
300 void GetOption(PEAK_IPL_VIDEO_CONTAINER_OPTION option, void* value, size_t count, size_t* outCount)
301 {
304 m_handle, option, value, count, outCount);
305 });
306 }
307
321 void OptionRange(PEAK_IPL_VIDEO_CONTAINER_OPTION option, void* min, void* max, void* inc)
322 {
325 });
326 }
327};
328
329static std::unique_ptr<IContainer> Create(PEAK_IPL_VIDEO_CONTAINER container);
330
335class AVIContainer : public IContainer
336{
337 friend std::unique_ptr<IContainer> Create(PEAK_IPL_VIDEO_CONTAINER container);
338 AVIContainer() = default;
339
340public:
341 AVIContainer(const AVIContainer&) = delete;
342 AVIContainer(AVIContainer&&) = default;
343
344 AVIContainer& operator=(const AVIContainer&) = delete;
345 AVIContainer& operator=(AVIContainer&&) = default;
346
352 {
354 }
355
365 void SetFramerate(double fps)
366 {
368 }
369
378 {
379 double fps = 0;
380 GetOption(PEAK_IPL_VIDEO_CONTAINER_OPTION_FRAMERATE, &fps, sizeof(fps), nullptr);
381 return fps;
382 }
383};
384
389static inline std::vector<PEAK_IPL_VIDEO_CONTAINER> GetSupportedContainersForAnEncoder(PEAK_IPL_VIDEO_ENCODER encoder)
390{
391 size_t length{};
394 });
395 std::vector<PEAK_IPL_VIDEO_CONTAINER> containers(length);
398 encoder, containers.data(), &length);
399 });
400 return containers;
401}
402
414static std::unique_ptr<IContainer> Create(PEAK_IPL_VIDEO_CONTAINER container)
415{
416 switch (container)
417 {
419 return std::unique_ptr<AVIContainer>(new AVIContainer());
420 default:
421 throw InvalidArgumentException("The supplied container is not a valid value!");
422 }
423}
424
425} // namespace container
426
427class Image;
428
432class VideoWriter final
433{
434public:
435 VideoWriter() = default;
437 {
438 if(IsOpen())
439 {
440 Close();
441 }
442 }
443 VideoWriter(const VideoWriter&) = delete;
444 VideoWriter(VideoWriter&& other) noexcept
445 {
446 m_handle = other.m_handle;
447 other.m_handle = nullptr;
448 m_encoder = std::move(other.m_encoder);
449 m_container = std::move(other.m_container);
450 }
453 {
454 m_handle = other.m_handle;
455 other.m_handle = nullptr;
456 m_encoder = std::move(other.m_encoder);
457 m_container = std::move(other.m_container);
458
459 return *this;
460 }
461
475 void Open(const std::string& fileName, std::shared_ptr<container::IContainer> container,
476 std::shared_ptr<encoder::IEncoder> encoder)
477 {
478 if (!container || !encoder)
479 {
480 throw InvalidArgumentException("The given pointer is a null pointer!");
481 }
482
485 &m_handle, fileName.c_str(), container->Id(), encoder->Id());
486 });
487 m_encoder = encoder;
488 m_container = container;
489
490 m_encoder->SetHandle(m_handle);
491 m_container->SetHandle(m_handle);
492 }
493
504 void Open(const std::string& fileName)
505 {
506 auto container = container::Create(PEAK_IPL_VIDEO_CONTAINER_AVI);
507 auto encoder = encoder::Create(PEAK_IPL_VIDEO_ENCODER_MJPEG);
508 Open(fileName, std::move(container), std::move(encoder));
509 }
510
521 {
522 PEAK_IPL_BOOL8 isOpen = 0;
524 [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_VideoWriter_Video_IsOpen(m_handle, &isOpen); });
525 return isOpen != 0;
526 }
527
535 void Close()
536 {
538
539 m_handle = nullptr;
540 }
541
555 void Append(const Image& image)
556 {
557 if (m_encoder == nullptr)
558 {
559 throw InvalidArgumentException("The open method must be called before append!");
560 }
563 m_handle, ImageBackendAccessor::BackendHandle(image));
564 });
565 }
566
569 {
570 Append(image);
571 return *this;
572 }
573
578 PEAK_IPL_NO_DISCARD std::shared_ptr<encoder::IEncoder> Encoder() const
579 {
580 return m_encoder;
581 }
582
587 PEAK_IPL_NO_DISCARD std::shared_ptr<container::IContainer> Container() const
588 {
589 return m_container;
590 }
591
597 {
598 uint64_t numFramesEncoded{};
600 [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_VideoWriter_Video_GetEncodedFrames(m_handle, &numFramesEncoded); });
601 return numFramesEncoded;
602 }
603
609 {
610 uint64_t numFramesDropped{};
612 [&] { return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_VideoWriter_Video_GetDroppedFrames(m_handle, &numFramesDropped); });
613 return numFramesDropped;
614 }
615
621 {
622 uint64_t s{};
625 return s;
626 }
627
637
642 void SetQueueSize(int32_t queueSize)
643 {
646 });
647 }
648
654 {
655 int32_t queueSize{};
658 });
659
660 return queueSize;
661 }
662
668 {
669 int32_t minQueueSize{}, maxQueueSize{};
671 return PEAK_IPL_C_ABI_PREFIX PEAK_IPL_VideoWriter_Queue_GetRange(m_handle, &minQueueSize, &maxQueueSize);
672 });
673
674 return {minQueueSize, maxQueueSize, 1};
675 }
676
682 {
683 int32_t queued{};
686 });
687
688 return queued;
689 }
690
695 void WaitUntilFrameDone(int32_t timeout_ms)
696 {
699 });
700 }
701
706 void WaitUntilQueueEmpty(int32_t timeout_ms)
707 {
710 });
711 }
712
713private:
714 PEAK_IPL_VIDEO_HANDLE m_handle{nullptr};
715 std::shared_ptr<encoder::IEncoder> m_encoder{};
716 std::shared_ptr<container::IContainer> m_container{};
717};
718
719} /* namespace ipl */
720} /* namespace peak */
Stores the pixel format, width and height of an image and the pointer to the image buffer.
Definition peak_ipl_image.hpp:46
The exception thrown when passing an invalid parameter to a function.
Definition peak_ipl_exception.hpp:80
Range support and convenience class.
Definition peak_ipl_range.hpp:33
Writes images to a file in the form of a video.
Definition peak_ipl_video_writer.hpp:433
void Open(const std::string &fileName, std::shared_ptr< container::IContainer > container, std::shared_ptr< encoder::IEncoder > encoder)
Open the VideoWriter and sets up a container and an encoder. The file is created upon receiving the f...
Definition peak_ipl_video_writer.hpp:475
VideoWriter(const VideoWriter &)=delete
VideoWriter(VideoWriter &&other) noexcept
Definition peak_ipl_video_writer.hpp:444
Range< int32_t > QueueSizeRange()
Get the range of the queue size.
Definition peak_ipl_video_writer.hpp:667
void Open(const std::string &fileName)
Open the VideoWriter with AVI container and mjpeg encoder. The file is created upon receiving the fir...
Definition peak_ipl_video_writer.hpp:504
void WaitUntilFrameDone(int32_t timeout_ms)
Wait until frame processing is done.
Definition peak_ipl_video_writer.hpp:695
std::shared_ptr< encoder::IEncoder > Encoder() const
Get the used encoder.
Definition peak_ipl_video_writer.hpp:578
void SetQueueSize(int32_t queueSize)
Set the internal queue size of the video writer.
Definition peak_ipl_video_writer.hpp:642
std::shared_ptr< container::IContainer > Container() const
Get the used container.
Definition peak_ipl_video_writer.hpp:587
void WaitUntilQueueEmpty(int32_t timeout_ms)
Wait until the queue is empty.
Definition peak_ipl_video_writer.hpp:706
VideoWriter & operator=(VideoWriter &&other) noexcept
Definition peak_ipl_video_writer.hpp:452
int32_t NumBuffersQueued()
Get the count of the currently queued buffers, which are waiting to be processed.
Definition peak_ipl_video_writer.hpp:681
void ResetFrameCounters()
Reset compressed and dropped frame counters.
Definition peak_ipl_video_writer.hpp:631
void Append(const Image &image)
Append an image to the opened video.
Definition peak_ipl_video_writer.hpp:555
bool IsOpen() const
Check if the VideoWriter is already open.
Definition peak_ipl_video_writer.hpp:520
uint64_t NumFramesDropped() const
Get the dropped frames in the video stream.
Definition peak_ipl_video_writer.hpp:608
void Close()
Close the opened VideoWriter.
Definition peak_ipl_video_writer.hpp:535
VideoWriter & operator=(const VideoWriter &)=delete
uint64_t FileSize() const
Get the file size of the video in bytes.
Definition peak_ipl_video_writer.hpp:620
uint64_t NumFramesEncoded() const
Get the encoded frames in the video stream.
Definition peak_ipl_video_writer.hpp:596
~VideoWriter()
Definition peak_ipl_video_writer.hpp:436
int32_t QueueSize()
Get the internal queue size of the video writer.
Definition peak_ipl_video_writer.hpp:653
VideoWriter & operator<<(const Image &image)
Append an image to the opened video.
Definition peak_ipl_video_writer.hpp:568
AVI container specialization of IContainer. Create the container with the according Create function.
Definition peak_ipl_video_writer.hpp:336
friend std::unique_ptr< IContainer > Create(PEAK_IPL_VIDEO_CONTAINER container)
Factory to create an encoder.
Definition peak_ipl_video_writer.hpp:234
AVIContainer(const AVIContainer &)=delete
PEAK_IPL_VIDEO_CONTAINER Id() override
Get the current type of the container.
Definition peak_ipl_video_writer.hpp:351
void SetFramerate(double fps)
Set the frame rate for the opened video.
Definition peak_ipl_video_writer.hpp:365
AVIContainer & operator=(AVIContainer &&)=default
double GetFramerate()
Get the frame rate for the opened video.
Definition peak_ipl_video_writer.hpp:377
AVIContainer & operator=(const AVIContainer &)=delete
AVIContainer(AVIContainer &&)=default
PEAK_IPL_VIDEO_HANDLE m_handle
Definition peak_ipl_video_writer.hpp:259
void GetOption(PEAK_IPL_VIDEO_CONTAINER_OPTION option, void *value, size_t count, size_t *outCount)
Gets an option of the container.
Definition peak_ipl_video_writer.hpp:300
void OptionRange(PEAK_IPL_VIDEO_CONTAINER_OPTION option, void *min, void *max, void *inc)
Receive the range for an option of the container.
Definition peak_ipl_video_writer.hpp:321
virtual PEAK_IPL_VIDEO_CONTAINER Id()=0
void SetOption(PEAK_IPL_VIDEO_CONTAINER_OPTION option, const void *value, size_t count)
Sets an option on the container.
Definition peak_ipl_video_writer.hpp:281
void SetHandle(PEAK_IPL_VIDEO_HANDLE handle)
Definition peak_ipl_video_writer.hpp:260
virtual ~IEncoder()=default
void SetHandle(PEAK_IPL_VIDEO_HANDLE handle)
Definition peak_ipl_video_writer.hpp:44
void OptionRange(PEAK_IPL_VIDEO_ENCODER_OPTION option, void *min, void *max, void *inc)
Receive the range for an option of the encoder.
Definition peak_ipl_video_writer.hpp:102
PEAK_IPL_VIDEO_HANDLE m_handle
Definition peak_ipl_video_writer.hpp:42
virtual PEAK_IPL_VIDEO_ENCODER Id()=0
void GetOption(PEAK_IPL_VIDEO_ENCODER_OPTION option, void *value, size_t count, size_t *outCount)
Gets an option of the encoder.
Definition peak_ipl_video_writer.hpp:83
void SetOption(PEAK_IPL_VIDEO_ENCODER_OPTION option, const void *value, size_t count)
Sets an option on the encoder.
Definition peak_ipl_video_writer.hpp:65
friend std::unique_ptr< IEncoder > Create(PEAK_IPL_VIDEO_ENCODER encoder)
Factory to create an encoder.
Definition peak_ipl_video_writer.hpp:234
MJPEGEncoder & operator=(const MJPEGEncoder &)=delete
void SetQuality(uint32_t quality)
Sets the quality for the jpeg encoder.
Definition peak_ipl_video_writer.hpp:147
MJPEGEncoder(MJPEGEncoder &&)=default
MJPEGEncoder(const MJPEGEncoder &)=delete
PEAK_IPL_VIDEO_ENCODER Id() override
Get the current type of the encoder.
Definition peak_ipl_video_writer.hpp:133
Range< double > QualityRange()
Gets the quality range for the jpeg encoder.
Definition peak_ipl_video_writer.hpp:173
MJPEGEncoder & operator=(MJPEGEncoder &&)=default
uint32_t GetQuality()
Gets the quality for the jpeg encoder.
Definition peak_ipl_video_writer.hpp:159
Definition peak_ipl_video_writer.hpp:249
Definition peak_ipl_video_writer.hpp:32
The "peak::ipl" namespace contains the whole image processing library.
void ExecuteAndMapReturnCodes(const CallableType &callableObject)
Definition peak_ipl_exception.hpp:271
#define PEAK_IPL_NO_DISCARD
Definition peak_ipl_attribute_defines.h:25
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Queue_SetSize(PEAK_IPL_VIDEO_HANDLE videoHandle, int32_t size)
Set the internal queue size of the video writer.
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Container_GetOption(PEAK_IPL_VIDEO_HANDLE videoHandle, PEAK_IPL_VIDEO_ENCODER_OPTION option, void *value, size_t count, size_t *outCount)
Gets an option of the container.
uint16_t PEAK_IPL_VIDEO_ENCODER
Definition peak_ipl_backend.h:243
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Encoder_GetOption(PEAK_IPL_VIDEO_HANDLE videoHandle, PEAK_IPL_VIDEO_ENCODER_OPTION option, void *value, size_t count, size_t *outCount)
Sets an option of the encoder.
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Queue_GetQueued(PEAK_IPL_VIDEO_HANDLE videoHandle, int32_t *queued)
Get the count of the currently queued buffers.
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Video_GetEncodedFrames(PEAK_IPL_VIDEO_HANDLE videoHandle, uint64_t *numFrames)
Gets the number of encoded frames in the video stream.
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Encoder_OptionRange(PEAK_IPL_VIDEO_HANDLE videoHandle, PEAK_IPL_VIDEO_ENCODER_OPTION option, void *min, void *max, void *inc)
Receive the range for an option of the encoder.
@ PEAK_IPL_VIDEO_CONTAINER_AVI
Definition peak_ipl_backend.h:234
uint16_t PEAK_IPL_VIDEO_CONTAINER_OPTION
Definition peak_ipl_backend.h:255
uint16_t PEAK_IPL_VIDEO_CONTAINER
Definition peak_ipl_backend.h:236
PEAK_IPL_C_API PEAK_IPL_VideoWriter_GetSupportedEncodersForAContainer(PEAK_IPL_VIDEO_CONTAINER container, PEAK_IPL_VIDEO_ENCODER *encoders, size_t *size)
Get the supported encoders for a container.
@ PEAK_IPL_VIDEO_ENCODER_OPTION_QUALITY
Definition peak_ipl_backend.h:247
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Video_Close(PEAK_IPL_VIDEO_HANDLE videoHandle)
Close the video stream.
uint16_t PEAK_IPL_VIDEO_ENCODER_OPTION
Definition peak_ipl_backend.h:249
@ PEAK_IPL_VIDEO_CONTAINER_OPTION_FRAMERATE
Definition peak_ipl_backend.h:253
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Video_WaitUntilFrameDone(PEAK_IPL_VIDEO_HANDLE videoHandle, int32_t timeout_ms)
Wait until frame processing is done.
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Queue_GetSize(PEAK_IPL_VIDEO_HANDLE videoHandle, int32_t *size)
Get the internal queue size of the video writer.
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Video_GetFileSize(PEAK_IPL_VIDEO_HANDLE videoHandle, uint64_t *fileSize)
Gets the file size of the video.
struct PEAK_IPL_VIDEO * PEAK_IPL_VIDEO_HANDLE
Definition peak_ipl_backend.h:417
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Video_Open(PEAK_IPL_VIDEO_HANDLE *videoHandle, const char *filePath, PEAK_IPL_VIDEO_CONTAINER container, PEAK_IPL_VIDEO_ENCODER encoder)
Open a video stream.
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Video_WaitUntilQueueEmpty(PEAK_IPL_VIDEO_HANDLE videoHandle, int32_t timeout_ms)
Wait until the queue is empty.
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Video_GetDroppedFrames(PEAK_IPL_VIDEO_HANDLE videoHandle, uint64_t *numFrames)
Gets the number of dropped frames in the video stream.
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Container_SetOption(PEAK_IPL_VIDEO_HANDLE videoHandle, PEAK_IPL_VIDEO_CONTAINER_OPTION option, const void *value, size_t count)
Sets an option on the container.
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Encoder_SetOption(PEAK_IPL_VIDEO_HANDLE videoHandle, PEAK_IPL_VIDEO_ENCODER_OPTION option, const void *value, size_t count)
Sets an option on the encoder.
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Container_OptionRange(PEAK_IPL_VIDEO_HANDLE videoHandle, PEAK_IPL_VIDEO_ENCODER_OPTION option, void *min, void *max, void *inc)
Receive the range for an option of the container.
int8_t PEAK_IPL_BOOL8
Definition peak_ipl_backend.h:26
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Video_ResetFrameCounters(PEAK_IPL_VIDEO_HANDLE videoHandle)
Reset compressed and dropped frame counters.
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Encoder_GetSupportedPixelFormats(PEAK_IPL_VIDEO_ENCODER encoder, PEAK_IPL_PIXEL_FORMAT *pixelFormatList, size_t *pixelFormatListSize)
Get the supported pixelformats for an encoder.
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Video_Append(PEAK_IPL_VIDEO_HANDLE videoHandle, PEAK_IPL_IMAGE_HANDLE imageHandle)
Append an image to the video stream.
int32_t PEAK_IPL_PIXEL_FORMAT
Definition peak_ipl_backend.h:173
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Queue_GetRange(PEAK_IPL_VIDEO_HANDLE videoHandle, int32_t *minSize, int32_t *maxSize)
Get the possible range of the internal queue size.
PEAK_IPL_C_API PEAK_IPL_VideoWriter_Video_IsOpen(PEAK_IPL_VIDEO_HANDLE videoHandle, PEAK_IPL_BOOL8 *isOpen)
Check if the video stream is open.
@ PEAK_IPL_VIDEO_ENCODER_MJPEG
Definition peak_ipl_backend.h:241
PEAK_IPL_C_API PEAK_IPL_VideoWriter_GetSupportedContainersForAnEncoder(PEAK_IPL_VIDEO_ENCODER encoder, PEAK_IPL_VIDEO_CONTAINER *containers, size_t *size)
Get the supported containers for an encoder.
#define PEAK_IPL_C_ABI_PREFIX
Definition peak_ipl_dll_defines.h:16