CVB++ 15.0
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
31 class CodecConfig final
32 {
33
34 public:
36
45 {
46 return Internal::DoResCallObjectOut<CodecConfig>(
47 [&](void *&handle) { return CVB_CALL_CAPI(CVCBCreateCodecConfigTyped(provider.c_str(), handle)); });
48 }
49
50 static std::unique_ptr<CodecConfig> FromHandle(HandleGuard<CodecConfig> &&guard)
51 {
52 if (!guard.Handle())
53 throw std::runtime_error("handle must not be null");
54
55 return std::make_unique<CodecConfig>(std::move(guard));
56 }
57
58 explicit CodecConfig(HandleGuard<CodecConfig> &&guard) noexcept
59 : handle_(std::move(guard))
60 {
61 }
62
63 CodecConfig(const CodecConfig &other) = delete;
64 CodecConfig &operator=(const CodecConfig &other) = delete;
65 CodecConfig(CodecConfig &&other) = delete;
66 CodecConfig &operator=(CodecConfig &&other) = delete;
67
68 ~CodecConfig() = default;
69
70 void *Handle() const noexcept
71 {
72 return handle_.Handle();
73 }
74
76
80 Cvb::String Name() const;
81
83
87 Cvb::String ToJson() const;
88
90
94 int64_t BitRate() const
95 {
96 return Property<int64_t>(CExports::CVCBCP_Bitrate);
97 }
98
100
104 void SetBitRate(int64_t bitRate)
105 {
106 SetProperty(bitRate, CExports::CVCBCP_Bitrate);
107 }
108
110
115 {
116 return {Property<int>(CExports::CVCBCP_Width), Property<int>(CExports::CVCBCP_Height)};
117 }
118
120
127 int ID() const
128 {
129 return Property<int>(CExports::CVCBCP_ID);
130 }
131
133
138 {
139 SetProperty(size.Width(), CExports::CVCBCP_Width);
140 SetProperty(size.Height(), CExports::CVCBCP_Height);
141 }
142
144
149 {
150 auto timeBase = Property<CExports::CVCBRational>(CExports::CVCBCP_TimeBase);
151 return {timeBase.Numerator, timeBase.Denominator};
152 }
153
155
159 void SetTimeBase(Rational timeBase)
160 {
161 SetProperty(*reinterpret_cast<CExports::CVCBRational *>(&timeBase), CExports::CVCBCP_TimeBase);
162 }
163
165
169 int GOPSize() const
170 {
171 return Property<int>(CExports::CVCBCP_GOPSize);
172 }
173
175
179 void SetGOPSize(int gopSize)
180 {
181 SetProperty(gopSize, CExports::CVCBCP_GOPSize);
182 }
183
185
189 int MaxBFrames() const
190 {
191 return Property<int>(CExports::CVCBCP_MaxBFrames);
192 }
193
195
199 void SetMaxBFrames(int maxBFrames)
200 {
201 SetProperty(maxBFrames, CExports::CVCBCP_MaxBFrames);
202 }
203
205
212 {
213 return static_cast<CodecBridge::PixelFormat>(Property<CExports::CVCBPixelFormat>(CExports::CVCBCP_PixelFormat));
214 }
215
217
224 {
225 SetProperty(static_cast<CExports::CVCBPixelFormat>(pixelFormat), CExports::CVCBCP_PixelFormat);
226 }
227
229
235 template <class T>
236 T Option(const Cvb::String &name) const;
237
239
246 template <class T>
247 void AddOption(const Cvb::String &name, const T &value);
248
250
258
259 private:
260 template <class T>
261 T Property(CExports::CVCBCodecProperty property, const Cvb::String &optionName = Cvb::String()) const
262 {
263 T value = {};
264 size_t size = sizeof(T);
265 CVB_CALL_CAPI_CHECKED(CVCBCodecConfigGetProperty(Handle(), property, &value, size,
266 optionName.empty() ? nullptr : ToBytes(optionName).c_str()));
267 return value;
268 }
269
270 template <class T>
271 void SetProperty(const T &value, CExports::CVCBCodecProperty property,
272 const Cvb::String &optionName = Cvb::String())
273 {
274 CVB_CALL_CAPI_CHECKED(CVCBCodecConfigSetProperty(
275 Handle(), property, const_cast<T *>(&value), sizeof(T),
276 optionName.empty() ? nullptr
277 : 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,
300 const Cvb::String &optionName) const
301 {
302
303 size_t size = 0;
304 CVB_CALL_CAPI_CHECKED(CVCBCodecConfigGetProperty(Handle(), property, nullptr, size,
305 optionName.empty() ? nullptr : ToBytes(optionName).c_str()));
306
307 std::vector<char> buffer(size);
308 CVB_CALL_CAPI_CHECKED(CVCBCodecConfigGetProperty(Handle(), property, buffer.data(), size,
309 optionName.empty() ? nullptr : ToBytes(optionName).c_str()));
310
311 return {buffer.begin(), buffer.end() - 1};
312 }
313
314 template <>
315 inline void CodecConfig::SetProperty<Cvb::String>(const Cvb::String &value, CExports::CVCBCodecProperty property,
316 const Cvb::String &optionName)
317 {
318 CVB_CALL_CAPI_CHECKED(CVCBCodecConfigSetProperty(
319 Handle(), property, const_cast<char *>(ToBytes(value).c_str()), value.size() + 1,
320 optionName.empty() ? nullptr
321 : ToBytes(optionName).c_str())); // NOLINT(cppcoreguidelines-pro-type-const-cast)
322 }
323
325 {
326 return Property<Cvb::String>(CExports::CVCBCP_Name);
327 }
328
330 {
331 return Property<Cvb::String>(CExports::CVCBCP_JSON);
332 }
333
334 template <>
335 inline int64_t CodecConfig::Option<int64_t>(const Cvb::String &name) const
336 {
337 return Property<int64_t>(CExports::CVCBCP_IntOption, name);
338 }
339
340 template <>
341 inline Cvb::String CodecConfig::Option<Cvb::String>(const Cvb::String &name) const
342 {
343 return Property<Cvb::String>(CExports::CVCBCP_StringOption, name);
344 }
345
346 template <>
347 inline double CodecConfig::Option<double>(const Cvb::String &name) const
348 {
349 return Property<double>(CExports::CVCBCP_DoubleOption, name);
350 }
351
352 template <>
353 inline Rational CodecConfig::Option<Rational>(const Cvb::String &name) const
354 {
355 auto rational = Property<CExports::CVCBRational>(CExports::CVCBCP_DoubleOption, name);
356 return {rational.Numerator, rational.Denominator};
357 }
358
359 template <>
360 inline void CodecConfig::AddOption<Cvb::String>(const Cvb::String &name, const Cvb::String &value)
361 {
362 SetProperty(value, CExports::CVCBCP_StringOption, name);
363 }
364
365 template <>
366 inline void CodecConfig::AddOption<double>(const Cvb::String &name, const double &value)
367 {
368 SetProperty(value, CExports::CVCBCP_DoubleOption, name);
369 }
370
371 template <>
372 inline void CodecConfig::AddOption<Rational>(const Cvb::String &name, const Rational &value)
373 {
374 SetProperty(*reinterpret_cast<const CExports::CVCBRational *>(&value), CExports::CVCBCP_RationalOption, name);
375 }
376
377 template <>
378 inline void CodecConfig::AddOption<int64_t>(const Cvb::String &name, const int64_t &value)
379 {
380 SetProperty(value, CExports::CVCBCP_IntOption, name);
381 }
382
384 {
385 SetProperty(url, CExports::CVCBCP_URL);
386 return Property<Cvb::String>(CExports::CVCBCP_SDP);
387 }
388
389 } // namespace CodecBridge
390
391 CVB_END_INLINE_NS
392} // namespace Cvb
Cvb::String ToJson() const
Create a JSON string that defines this configuration.
Definition codec_config.hpp:329
Cvb::String Name() const
Get the name of the codec.
Definition codec_config.hpp:324
Size2D< int > Size() const
Get the size of the frames.
Definition codec_config.hpp:114
void SetGOPSize(int gopSize)
Set the GOP size.
Definition codec_config.hpp:179
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:189
CodecBridge::PixelFormat PixelFormat() const
Get the pixel format used by this codec.
Definition codec_config.hpp:211
void SetBitRate(int64_t bitRate)
Set the bit rate.
Definition codec_config.hpp:104
void SetMaxBFrames(int maxBFrames)
Set the maximum B frames.
Definition codec_config.hpp:199
static std::unique_ptr< CodecConfig > Create(const Cvb::String &provider)
Creates a codec config from given provider.
Definition codec_config.hpp:44
void SetTimeBase(Rational timeBase)
Set the time base used by the codec.
Definition codec_config.hpp:159
Rational TimeBase() const
The time base used by the codec.
Definition codec_config.hpp:148
int ID() const
Get the codec ID.
Definition codec_config.hpp:127
int64_t BitRate() const
Get the bit rate.
Definition codec_config.hpp:94
Cvb::String SessionDescription(const Cvb::String &url)
Get the contend of the session description protocol.
Definition codec_config.hpp:383
void SetSize(Size2D< int > size)
Set the size of the frames.
Definition codec_config.hpp:137
void SetPixelFormat(CodecBridge::PixelFormat pixelFormat)
Set the pixel format used by this codec.
Definition codec_config.hpp:223
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:169
A pair of rational numbers.
Definition codec_bridge.hpp:199
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::string String
String for wide characters or unicode characters.
Definition string.hpp:49