CVB++ 14.1
codec_config.hpp
1#pragma once
2
3#include <map>
4
5#include "codec_bridge.hpp"
6#include "../size_2d.hpp"
7#include "../string.hpp"
8#include "../shims/stdvariant.hpp"
9#include "../utilities/system_info.hpp"
10
11namespace Cvb
12{
13
14 CVB_BEGIN_INLINE_NS
15
16 template <>
17 inline HandleGuard<CodecBridge::CodecConfig>::HandleGuard(void* handle) noexcept
18 : HandleGuard<CodecBridge::CodecConfig>(handle, [](void* handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
19 {
20 }
21
22 namespace CodecBridge
23 {
24
25
32 class CodecConfig final
33 {
34
35 public:
36
38
46 {
47 return Internal::DoResCallObjectOut<CodecConfig>([&](void*& handle)
48 {
49 return CVB_CALL_CAPI(CVCBCreateCodecConfigTyped(provider.c_str(), handle));
50 });
51 }
52
53 static std::unique_ptr<CodecConfig> FromHandle(HandleGuard<CodecConfig>&& guard)
54 {
55 if (!guard.Handle())
56 throw std::runtime_error("handle must not be null");
57
58 return std::make_unique<CodecConfig>(std::move(guard));
59 }
60
61 explicit CodecConfig(HandleGuard<CodecConfig>&& guard) noexcept
62 : handle_(std::move(guard))
63 {
64
65 }
66
67 CodecConfig(const CodecConfig& other) = delete;
68 CodecConfig& operator=(const CodecConfig& other) = delete;
69 CodecConfig(CodecConfig&& other) = delete;
70 CodecConfig& operator=(CodecConfig&& other) = delete;
71
72 ~CodecConfig() = default;
73
74 void* Handle() const noexcept
75 {
76 return handle_.Handle();
77 }
78
80
84 Cvb::String Name() const;
85
87
91 Cvb::String ToJson() const;
92
94
98 int64_t BitRate() const
99 {
100 return Property<int64_t>(CExports::CVCBCP_Bitrate);
101 }
102
104
108 void SetBitRate(int64_t bitRate)
109 {
110 SetProperty(bitRate, CExports::CVCBCP_Bitrate);
111 }
112
114
119 {
120 return { Property<int>(CExports::CVCBCP_Width), Property<int>(CExports::CVCBCP_Height) };
121 }
122
124
131 int ID() const
132 {
133 return Property<int>(CExports::CVCBCP_ID);
134 }
135
137
142 {
143 SetProperty(size.Width(), CExports::CVCBCP_Width);
144 SetProperty(size.Height(), CExports::CVCBCP_Height);
145 }
146
148
153 {
154 auto timeBase = Property<CExports::CVCBRational>(CExports::CVCBCP_TimeBase);
155 return { timeBase.Numerator, timeBase.Denominator };
156 }
157
159
163 void SetTimeBase(Rational timeBase)
164 {
165 SetProperty(*reinterpret_cast<CExports::CVCBRational*>(&timeBase), CExports::CVCBCP_TimeBase);
166 }
167
169
173 int GOPSize() const
174 {
175 return Property<int>(CExports::CVCBCP_GOPSize);
176 }
177
179
183 void SetGOPSize(int gopSize)
184 {
185 SetProperty(gopSize, CExports::CVCBCP_GOPSize);
186 }
187
189
193 int MaxBFrames() const
194 {
195 return Property<int>(CExports::CVCBCP_MaxBFrames);
196 }
197
199
203 void SetMaxBFrames(int maxBFrames)
204 {
205 SetProperty(maxBFrames, CExports::CVCBCP_MaxBFrames);
206 }
207
209
216 {
217 return static_cast<CodecBridge::PixelFormat>(Property<CExports::CVCBPixelFormat>(CExports::CVCBCP_PixelFormat));
218 }
219
221
228 {
229 SetProperty(static_cast<CExports::CVCBPixelFormat>(pixelFormat), CExports::CVCBCP_PixelFormat);
230 }
231
233
239 template<class T>
240 T Option(const Cvb::String& name) const;
241
243
250 template<class T>
251 void AddOption(const Cvb::String& name, const T& value);
252
254
262
263 private:
264
265 template<class T>
266 T Property(CExports::CVCBCodecProperty property, const Cvb::String& optionName = Cvb::String()) const
267 {
268 T value = {};
269 size_t size = sizeof(T);
270 CVB_CALL_CAPI_CHECKED(CVCBCodecConfigGetProperty(Handle(), property, &value, size, optionName.empty() ? nullptr : ToBytes(optionName).c_str()));
271 return value;
272 }
273
274 template<class T>
275 void SetProperty(const T& value, CExports::CVCBCodecProperty property, const Cvb::String& optionName = Cvb::String())
276 {
277 CVB_CALL_CAPI_CHECKED(CVCBCodecConfigSetProperty(Handle(), property, const_cast<T*>(&value), sizeof(T), optionName.empty() ? nullptr : ToBytes(optionName).c_str())); // NOLINT(cppcoreguidelines-pro-type-const-cast)
278 }
279
280 static std::string ToBytes(const std::wstring& text)
281 {
282 // we can be sure that options are ASCII only
283 std::string asciiText;
284 asciiText.reserve(text.size());
285 for (const auto& c : text)
286 asciiText.push_back(static_cast<char>(c));
287 return asciiText;
288 }
289
290 static std::string ToBytes(const std::string& text)
291 {
292 return text;
293 }
294
295 HandleGuard<CodecConfig> handle_;
296 };
297
298 template<>
299 inline Cvb::String CodecConfig::Property<Cvb::String>(CExports::CVCBCodecProperty property, const Cvb::String& optionName) const
300 {
301
302 size_t size = 0;
303 CVB_CALL_CAPI_CHECKED(CVCBCodecConfigGetProperty(Handle(), property, nullptr, size, optionName.empty() ? nullptr : ToBytes(optionName).c_str()));
304
305 std::vector<char> buffer(size);
306 CVB_CALL_CAPI_CHECKED(CVCBCodecConfigGetProperty(Handle(), property, buffer.data(), size, optionName.empty() ? nullptr : ToBytes(optionName).c_str()));
307
308 return { buffer.begin(), buffer.end() - 1 };
309 }
310
311 template<>
312 inline void CodecConfig::SetProperty<Cvb::String>(const Cvb::String& value, CExports::CVCBCodecProperty property, const Cvb::String& optionName)
313 {
314 CVB_CALL_CAPI_CHECKED(CVCBCodecConfigSetProperty(Handle(), property, const_cast<char*>(ToBytes(value).c_str()), value.size() + 1, optionName.empty() ? nullptr : ToBytes(optionName).c_str())); // NOLINT(cppcoreguidelines-pro-type-const-cast)
315 }
316
318 {
319 return Property<Cvb::String>(CExports::CVCBCP_Name);
320 }
321
323 {
324 return Property<Cvb::String>(CExports::CVCBCP_JSON);
325 }
326
327 template<>
328 inline int64_t CodecConfig::Option<int64_t>(const Cvb::String& name) const
329 {
330 return Property<int64_t>(CExports::CVCBCP_IntOption, name);
331 }
332
333 template<>
334 inline Cvb::String CodecConfig::Option<Cvb::String>(const Cvb::String& name) const
335 {
336 return Property<Cvb::String>(CExports::CVCBCP_StringOption, name);
337 }
338
339 template<>
340 inline double CodecConfig::Option<double>(const Cvb::String& name) const
341 {
342 return Property<double>(CExports::CVCBCP_DoubleOption, name);
343 }
344
345 template<>
346 inline Rational CodecConfig::Option<Rational>(const Cvb::String& name) const
347 {
348 auto rational = Property<CExports::CVCBRational>(CExports::CVCBCP_DoubleOption, name);
349 return { rational.Numerator, rational.Denominator };
350 }
351
352 template<>
353 inline void CodecConfig::AddOption<Cvb::String>(const Cvb::String& name, const Cvb::String& value)
354 {
355 SetProperty(value, CExports::CVCBCP_StringOption, name);
356 }
357
358 template<>
359 inline void CodecConfig::AddOption<double>(const Cvb::String& name, const double& value)
360 {
361 SetProperty(value, CExports::CVCBCP_DoubleOption, name);
362 }
363
364 template<>
365 inline void CodecConfig::AddOption<Rational>(const Cvb::String& name, const Rational& value)
366 {
367 SetProperty(*reinterpret_cast<const CExports::CVCBRational*>(&value), CExports::CVCBCP_RationalOption, name);
368 }
369
370 template<>
371 inline void CodecConfig::AddOption<int64_t>(const Cvb::String& name, const int64_t& value)
372 {
373 SetProperty(value, CExports::CVCBCP_IntOption, name);
374 }
375
377 {
378 SetProperty(url, CExports::CVCBCP_URL);
379 return Property<Cvb::String>(CExports::CVCBCP_SDP);
380 }
381
382 }
383
384 CVB_END_INLINE_NS
385}
A configuration defining a codec.
Definition: codec_config.hpp:33
Cvb::String ToJson() const
Create a JSON string that defines this configuration.
Definition: codec_config.hpp:322
Cvb::String Name() const
Get the name of the codec.
Definition: codec_config.hpp:317
Size2D< int > Size() const
Get the size of the frames.
Definition: codec_config.hpp:118
void SetGOPSize(int gopSize)
Set the GOP size.
Definition: codec_config.hpp:183
void AddOption(const Cvb::String &name, const T &value)
Add a generic option from the codec.
int MaxBFrames() const
Get the maximum B frames.
Definition: codec_config.hpp:193
CodecBridge::PixelFormat PixelFormat() const
Get the pixel format used by this codec.
Definition: codec_config.hpp:215
void SetBitRate(int64_t bitRate)
Set the bit rate.
Definition: codec_config.hpp:108
void SetMaxBFrames(int maxBFrames)
Set the maximum B frames.
Definition: codec_config.hpp:203
static std::unique_ptr< CodecConfig > Create(const Cvb::String &provider)
Creates a codec config from given provider.
Definition: codec_config.hpp:45
void SetTimeBase(Rational timeBase)
Set the time base used by the codec.
Definition: codec_config.hpp:163
Rational TimeBase() const
The time base used by the codec.
Definition: codec_config.hpp:152
int ID() const
Get the codec ID.
Definition: codec_config.hpp:131
int64_t BitRate() const
Get the bit rate.
Definition: codec_config.hpp:98
Cvb::String SessionDescription(const Cvb::String &url)
Get the contend of the session description protocol.
Definition: codec_config.hpp:376
void SetSize(Size2D< int > size)
Set the size of the frames.
Definition: codec_config.hpp:141
void SetPixelFormat(CodecBridge::PixelFormat pixelFormat)
Set the pixel format used by this codec.
Definition: codec_config.hpp:227
T Option(const Cvb::String &name) const
Get a generic option from the codec.
int GOPSize() const
Get the GOP size.
Definition: codec_config.hpp:173
A pair of rational numbers.
Definition: codec_bridge.hpp:200
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