CVB++ 14.1
encoder.hpp
1#pragma once
2
3
4#include "codec_bridge.hpp"
5#include "../global.hpp"
6#include "../utilities/system_info.hpp"
7
8#include "codec_config.hpp"
9#include "frame.hpp"
10#include "packet.hpp"
11
12namespace Cvb
13{
14
15 CVB_BEGIN_INLINE_NS
16
17 template <>
18 inline HandleGuard<CodecBridge::Encoder>::HandleGuard(void* handle) noexcept
19 : HandleGuard<CodecBridge::Encoder>(handle, [](void* handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
20 {
21 }
22
23 namespace CodecBridge
24 {
25
32 class Encoder final
33 {
34
35 public:
36
38
46 static std::unique_ptr<Encoder> Create(const CodecConfig& codecConfig)
47 {
48 return Internal::DoResCallObjectOut<Encoder>([&](void*& handle)
49 {
50 return CVB_CALL_CAPI(CVCBCreateEncoder(codecConfig.Handle(), handle));
51 });
52 }
53
54 static std::unique_ptr<Encoder> FromHandle(HandleGuard<Encoder>&& guard)
55 {
56 if (!guard.Handle())
57 throw std::runtime_error("handle must not be null");
58
59 return std::make_unique<Encoder>(std::move(guard));
60 }
61
62 explicit Encoder(HandleGuard<Encoder>&& guard) noexcept
63 : handle_(std::move(guard))
64 {
65
66 }
67
68 Encoder(const Encoder& other) = delete;
69 Encoder& operator=(const Encoder& other) = delete;
70 Encoder(Encoder&& other) = delete;
71 Encoder& operator=(Encoder&& other) = delete;
72
73 ~Encoder() = default;
74
75 void* Handle() const noexcept
76 {
77 return handle_.Handle();
78 }
79
81
89 CodecStatus Encode(const Frame& frame)
90 {
91 CExports::CVCBCodecStatus status = CExports::CVCBCS_ReadoutRequired;
92 CVB_CALL_CAPI_CHECKED(CVCBEncoderSendFrame(Handle(), frame.Handle(), status));
93 return static_cast<CodecStatus>(status);
94 }
95
97
103 void Flush()
104 {
105 CExports::CVCBCodecStatus status = CExports::CVCBCS_ReadoutRequired;
106 CVB_CALL_CAPI_CHECKED(CVCBEncoderSendFrame(Handle(), nullptr, status));
107 }
108
110
118 {
119 CExports::CVCBPACKET handle = nullptr;
120 CVB_CALL_CAPI_CHECKED(CVCBEncoderReceivePacket(Handle(), handle));
121 if (!handle)
122 return {};
123 HandleGuard<Packet> guard(handle);
124 return Packet::FromHandle(std::move(guard));
125 }
126
127
128 private:
129
130 HandleGuard<Encoder> handle_;
131 };
132
133}
134
135CVB_END_INLINE_NS
136}
A configuration defining a codec.
Definition: codec_config.hpp:33
An encoder for video frames.
Definition: encoder.hpp:33
void Flush()
Flush the encoder and finish encoding.
Definition: encoder.hpp:103
static std::unique_ptr< Encoder > Create(const CodecConfig &codecConfig)
Creates an encoder with a given configuration.
Definition: encoder.hpp:46
CodecStatus Encode(const Frame &frame)
Encode a video frame.
Definition: encoder.hpp:89
std::unique_ptr< Packet > ReceivePacket()
Receive an encoded packet.
Definition: encoder.hpp:117
A video frame for encoding and decoding.
Definition: frame.hpp:30
CodecStatus
Status of the encoder/decoder after sending data.
Definition: codec_bridge.hpp:187
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24