-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathavutils.h
More file actions
713 lines (613 loc) · 18.9 KB
/
Copy pathavutils.h
File metadata and controls
713 lines (613 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
#pragma once
#include "avcpp/avconfig.h"
#include "avcpp/avcpp_export.h"
#include <ranges>
#include <string>
#include <vector>
#include <deque>
#include <memory>
#include <mutex>
#include <sstream>
#include <algorithm>
#include <functional>
#include <type_traits>
#include <limits>
#if AVCPP_CXX_STANDARD >= 20
# include <span>
#endif
#include "ffmpeg.h"
#include "avtime.h"
#if AVCPP_HAS_AVFORMAT
extern "C" {
#include <libavformat/avformat.h>
}
#endif
//
// Functions
//
namespace av {
// Basic FFmpeg constants
constexpr auto NoPts = static_cast<int64_t>(AV_NOPTS_VALUE);
constexpr auto TimeBase = static_cast<int>(AV_TIME_BASE);
constexpr auto TimeBaseQ = AVRational{1, AV_TIME_BASE};
template<typename R, typename T>
R lexical_cast(const T& v)
{
R result;
std::stringstream ss;
ss << v;
ss >> result;
return result;
}
class AVCPP_EXPORT noncopyable
{
protected:
noncopyable() = default;
noncopyable( const noncopyable& ) = delete;
void operator=( const noncopyable& ) = delete;
};
/**
* This method can be used to turn up or down FFmpeg's logging level.
*
* @param level An integer value for level. Lower numbers
* mean less logging. A negative number tells FFmpeg to
* shut up.
*/
AVCPP_EXPORT void set_logging_level(int32_t level);
/**
* Like @see set_logging_level, but can assept logging level as string.
*
* @param level - string representation of loggin level:
* 'quiet', 'panic', 'fatal', 'error', 'warning', 'info', 'verbose' or 'debug',
* it also can be numeric (but in string representation), it it case
* boost::lexical_cast will be used and set_logging_level(int32_t) will be
* called.
*/
AVCPP_EXPORT void set_logging_level(const std::string& level);
// Compat code
#define setFFmpegLoggingLevel set_logging_level
/**
* @brief dump_binary_buffer
* Dump binary buffer to std out in HEX view
* @param buffer pointer to buffer start
* @param buffer_size buffer size
* @param width output width in hex values (real text screen with is: sw = width * 3 - 1)
*/
[[deprecated("Use av::hex_dump()")]]
AVCPP_EXPORT void dumpBinaryBuffer(uint8_t *buffer, int buffer_size, int width = 16);
/**
* C++ verstion of the av_err2str()
* @param error - error code to convert to string
* @return string representation of error code
*/
AVCPP_EXPORT std::string error2string(int error);
}
//
// Classes
//
namespace av {
struct AVCPP_EXPORT EmptyDeleter
{
void operator()(void *) {}
};
/**
* @brief The AvDeleter struct
* Unified delete functor for variois FFMPEG/libavformat/libavcodec and so on resource allocators.
* Can be used with shared_ptr<> and so on.
*/
namespace v1 {
struct AVCPP_EXPORT AvDeleter
{
bool operator() (struct SwsContext* &swsContext);
bool operator() (struct AVCodecContext* &codecContext);
#if AVCPP_HAS_AVFORMAT
bool operator() (struct AVOutputFormat* &format);
bool operator() (struct AVFormatContext* &formatContext);
#endif // if AVCPP_HAS_AVFORMAT
bool operator() (struct AVFrame* &frame);
bool operator() (struct AVPacket* &packet);
bool operator() (struct AVDictionary* &dictionary);
#if AVCPP_HAS_AVFILTER
bool operator ()(struct AVFilterInOut* &filterInOut);
#endif // if AVCPP_HAS_AVFILTER
bool operator() (struct AVBufferRef* &bufferRef);
};
} // ::v1
inline namespace v2 {
struct AVCPP_EXPORT SmartDeleter
{
template<typename T>
bool operator() (T *ptr) {
return v1::AvDeleter()(ptr);
}
};
}
template<typename T>
std::unique_ptr<T, void(*)(void*)> malloc(size_t size)
{
return {static_cast<T*>(av_malloc(size)), av_free};
}
template<typename T>
std::unique_ptr<T, void(*)(void*)> mallocz(size_t size)
{
return {static_cast<T*>(av_mallocz(size)), av_free};
}
template<typename T>
std::unique_ptr<T, void(*)(void*)> memdup(const void *p, size_t size)
{
return {static_cast<T*>(av_memdup(p, size)), av_free};
}
/**
* Functor to take next element in list/array
*/
#if 0
struct AVCPP_EXPORT AvNextElement
{
AVFilterInOut * operator()(AVFilterInOut * x) const
{
if (x)
return x->next;
else
return 0;
}
};
#endif
/**
* Teamplate class to set value for variable when out of scope will be occured. Use RAII idiom.
*
* By default type of variable and value is same, but it can be simple overriden, use
* @code
* ScopedValue<VariableType, OutValueType> scopedValue(variable, false);
* @endcode
* instead
* @code
* ScopedValue<VariableType> scopedValue(variable, false);
* @endcode
*/
template<typename T, typename V = T>
class ScopedValue
{
public:
/**
* Ctor. Store reference to variable and output value.
* @param var variable that must be set
* @param outValue value of variable
*/
ScopedValue(T &var, const V& outValue)
: var(var),
outValue(outValue)
{
}
/**
* Ctor. Like previous one but automaticaly can assign initial value to variable.
* @param var variable that must be set
* @param inValue initial value
* @param outValue output value
*/
ScopedValue(T &var, const V &inValue, const V &outValue)
: var(var),
outValue(outValue)
{
this->var = inValue;
}
~ScopedValue()
{
var = outValue;
}
private:
T& var;
V outValue;
};
/**
* @brief The ScopeOutAction class - guard-type class that allows points callback that will be called
* at the scope out
*
* Example:
* @code
* void foo()
* {
* int fd = open(...some args...);
* ScopeOutAction action([fd](){
* close(fd);
* });
*
* try
* {
* // some actions that can throw exception
* }
* catch(...)
* {
* throw; // Not best-practice, only for example
* }
* }
* @endcode
*
* In example above dtor of the action will be called before fd and we correctly close descriptor.
*
*/
class AVCPP_EXPORT ScopeOutAction
{
public:
template<typename Proc>
ScopeOutAction(const Proc& proc)
: m_proc(proc)
{}
template<typename Proc>
ScopeOutAction(Proc&& proc)
: m_proc(std::forward<Proc>(proc))
{}
~ScopeOutAction()
{
if (m_proc)
m_proc();
}
private:
std::function<void()> m_proc;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
#if AVCPP_CXX_STANDARD >= 20
/**
* Sentinel policy that defines well-known range end pointer
*/
template<class T>
struct SentinelEndPolicy
{
using value_type = std::remove_reference_t<T>;
value_type* end{};
constexpr bool isEnd(T* it) const noexcept {
return it == end;
}
};
/**
* Sentinel policy that defines well-known sequence end marker, like '\0' at the C-string end or special value like NULL
*/
template<class T>
struct SentinelUntilPolicy
{
using value_type = std::remove_cvref_t<T>;
value_type value{};
constexpr bool isEnd(T* it) const noexcept {
return it == nullptr || *it == value;
}
};
/**
* Policy based sentinel marker
*/
template<class T, class Policy>
struct ArraySentinel
{
Policy policy;
friend constexpr bool operator==(T* it, const ArraySentinel& s) noexcept {
return s.policy.isEnd(it);
}
};
template<class T, class U, bool UseProxy>
struct ReferenceSelector;
template<class T, class U>
struct ReferenceSelector<T, U, true>
{
using Type = T&;
};
template<class T, class U>
struct ReferenceSelector<T, U, false>
{
using Type = U;
};
/**
* Wrapped array iterator.
*
* Represents any element of the T* array as light-weight wrapper U
*
*/
template<typename T, typename U>
struct ArrayIterator
{
static constexpr bool UseProxy = !std::is_same_v<std::remove_cvref_t<T>, std::remove_cvref_t<U>>;
using difference_type = std::ptrdiff_t;
using value_type = std::remove_reference_t<U>;
using pointer = std::remove_reference_t<T>*; // or also value_type*
using reference = typename ReferenceSelector<T, U, !UseProxy>::Type;
using iterator_category =
std::conditional_t<
UseProxy,
std::random_access_iterator_tag,
std::contiguous_iterator_tag>;
ArrayIterator() noexcept = default; // semiregular
ArrayIterator(pointer ptr) noexcept : m_ptr(ptr) {}
reference operator*() const noexcept {
if constexpr (UseProxy)
return reference{*m_ptr};
else
return *m_ptr;
}
ArrayIterator& operator++() noexcept { m_ptr++; return *this; }
ArrayIterator operator++(int) noexcept { ArrayIterator tmp = *this; ++(*this); return tmp; }
ArrayIterator& operator--() noexcept { m_ptr--; return *this; }
ArrayIterator operator--(int) noexcept { ArrayIterator tmp = *this; --(*this); return tmp; }
ArrayIterator& operator+=(int n) noexcept { m_ptr+=n; return *this; }
ArrayIterator& operator-=(int n) noexcept { m_ptr-=n; return *this; }
// i[n]
reference operator[](int n) noexcept {
if constexpr (UseProxy)
return reference{m_ptr[n]};
else
return m_ptr[n];
}
const reference operator[](int n) const noexcept {
if constexpr (UseProxy)
return reference{m_ptr[n]};
else
return m_ptr[n];
}
//reference operator&() noexcept { return }
// a + n
// n + a
friend ArrayIterator operator+(const ArrayIterator& a, int n) noexcept { return {a.m_ptr + n}; }
friend ArrayIterator operator+(int n, const ArrayIterator& a) noexcept { return {a.m_ptr + n}; }
// i - n
friend ArrayIterator operator-(const ArrayIterator& a, int n) noexcept { return {a.m_ptr - n}; }
// b - a
friend difference_type operator-(const ArrayIterator& b, const ArrayIterator& a) noexcept { return b.m_ptr - a.m_ptr; }
// a<=>b
friend std::strong_ordering operator<=>(const ArrayIterator& a, const ArrayIterator& b) noexcept {
return a.m_ptr == b.m_ptr ? std::strong_ordering::equal
: a.m_ptr < b.m_ptr ? std::strong_ordering::less
: std::strong_ordering::greater;
}
friend bool operator== (const ArrayIterator& a, const ArrayIterator& b) { return a.m_ptr == b.m_ptr; };
friend bool operator!= (const ArrayIterator& a, const ArrayIterator& b) { return !(a == b); };
//
// Sentinels
//
friend bool operator==(ArrayIterator it, std::default_sentinel_t) = delete;
friend bool operator==(ArrayIterator it, auto sentinel) { return it.m_ptr == sentinel; }
friend bool operator!=(ArrayIterator it, auto sentinel) { return it.m_ptr != sentinel; }
friend bool operator==(auto sentinel, ArrayIterator it) { return it.m_ptr == sentinel; }
friend bool operator!=(auto sentinel, ArrayIterator it) { return it.m_ptr != sentinel; }
private:
pointer m_ptr{};
};
/**
* C-array (FFmpeg) wrapper witch can iterate over collection and represents any element via light-weight view U
*
* Policy maybe std::size_t that just defines input array size
*/
template<typename T, typename U, class Policy>
class ArrayView : public std::ranges::view_interface<ArrayView<T, U, Policy>>
{
public:
constexpr ArrayView() = default;
constexpr ArrayView(T *ptr, Policy policy)
: m_ptr(ptr), m_policy(std::move(policy))
{}
auto constexpr begin() const noexcept
{
return ArrayIterator<T, U>{m_ptr};
}
auto constexpr end() const noexcept
{
// special case to allow back()/size()/etc method for well-defined sizes
if constexpr (std::is_same_v<Policy, std::size_t>) {
return ArrayIterator<T, U>{m_ptr + m_policy};
} else {
return ArraySentinel<T, Policy>{m_policy};
}
}
private:
T *m_ptr{};
Policy m_policy{};
};
/**
* Helper to create array view with well-known size
* @param ptr
* @param size
*/
template<class U>
auto make_array_view_size(auto* ptr, std::size_t size)
{
using T = std::remove_reference_t<decltype(*ptr)>;
return ArrayView<T, U, std::size_t>(ptr, size);
}
/**
* Helper to create array view with end marker
* @param ptr
* @param value
*/
template<class U>
auto make_array_view_until(auto* ptr, auto value)
{
using T = std::remove_reference_t<decltype(*ptr)>;
return ArrayView<T, U, SentinelUntilPolicy<T>>(ptr, SentinelUntilPolicy<T>{value});
}
/**
* Select more approptiate value from given value list. Useful for AVCodec::supported_framerates,
* AVCodec::pix_fmts and so on.
*
* T - type of value and (by default) list elements
* L - type of list elements, range compatible
*
* If T and elements of L has different types, T must be have ctor from L-element.
*
* @param value value to set
* @param list list of allowed values
* @param endOfListValue end of list value, like
* @return value if list null or if it present in list, or more appropriate value from list
*/
template<typename T, typename L>
requires std::ranges::range<L> &&
requires(L& r) {
std::ranges::empty(r);
}
T guessValue(const T& value, L list)
{
if (list.empty())
return value;
T best = value;
T bestDistance = (std::numeric_limits<T>::max)();
for (auto&& cur : list) {
auto const distance = (std::max)(cur, value) - (std::min)(cur, value);
if (distance < bestDistance) {
bestDistance = distance;
best = cur;
}
}
return best;
}
#endif // AVCPP_CXX_STANDARD
template<typename T>
struct EqualComparator
{
EqualComparator(const T& value)
: value(value)
{}
bool operator() (const T& value) const
{
if (this->value == value)
return true;
return false;
}
const T& value;
};
/**
* Select more approptiate value from given value list. Useful for AVCodec::supported_framerates,
* AVCodec::pix_fmts and so on.
*
* T - type of value and (by default) list elements
* L - type of list elements
* C - end list comparator
*
* If T and L different types, T must be have ctor from L.
*
* @param value value to set
* @param list list of allowed values
* @param endOfListValue end of list value, like
* @return value if list null or if it present in list, or more appropriate value from list
*/
template<typename T, typename L, typename C>
T guessValue(const T& value, const L * list, C endListComparator)
{
if (!list)
return value;
T best = value;
T bestDistance = (std::numeric_limits<T>::max)();
for (const L * ptr = list; !endListComparator(*ptr); ++ptr) {
auto const cur = *ptr;
auto const distance = (std::max)(cur, value) - (std::min)(cur, value);
if (distance < bestDistance) {
bestDistance = distance;
best = cur;
}
}
return best;
}
template<typename T, typename Container>
void array_to_container(const T* array, std::size_t nelements, Container &container)
{
if (!array || nelements == 0)
return;
std::copy_n(array, array + nelements, std::back_inserter(container));
}
template<typename T, typename Container, typename Callable>
void array_to_container(const T* array, std::size_t nelements, Container &container, Callable convert)
{
if (!array || nelements == 0)
return;
// TBD: implement in more clean way
//std::copy_n(array, array + nelemnts, std::back_inserter(container));
for (auto i = 0u; i < nelements; ++i) {
container.push_back(convert(array[i]));
}
}
template<typename T, typename Container, typename Compare>
void array_to_container(const T* array, Container &container, Compare isEnd)
{
if (!array)
return;
T value;
while (!isEnd(value = *array++))
container.push_back(value);
}
template<typename T, typename Container, typename Compare, typename Callable>
void array_to_container(const T* array, Container &container, Compare isEnd, Callable convert)
{
if (!array)
return;
T value;
while (!isEnd(value = *array++))
container.push_back(convert(value));
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Allow to use without AVFORMAT library
//
AVCPP_EXPORT void hex_dump(FILE *f, const uint8_t *buf, std::size_t size);
AVCPP_EXPORT void hex_dump_log(void *avcl, int level, const uint8_t *buf, std::size_t size);
#if AVCPP_CXX_STANDARD >= 20
AVCPP_EXPORT void hex_dump(FILE *f, std::span<const uint8_t> buf);
AVCPP_EXPORT void hex_dump_log(void *avcl, int level, std::span<const uint8_t> buf);
#endif // if AVCPP_CXX_STANDARD >= 20
} // ::av
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef __cpp_lib_format
# include <format>
namespace av {
template <typename B>
concept has_name_method_with_ec = requires(const B& type, std::error_code ec) {
{ type.name(ec) } -> std::convertible_to<std::string_view>;
};
template <typename B>
concept has_name_method_without_ec = requires(const B& type) {
{ type.name() } -> std::convertible_to<std::string_view>;
};
template <typename B>
concept has_long_name_method_with_ec = requires(const B& type, std::error_code ec) {
{ type.longName(ec) } -> std::convertible_to<std::string_view>;
};
template <typename B>
concept has_long_name_method_without_ec = requires(const B& type) {
{ type.longName() } -> std::convertible_to<std::string_view>;
};
} // ::av
template <class T, class CharT>
requires av::has_name_method_with_ec<T> || av::has_name_method_without_ec<T>
struct std::formatter<T, CharT>
{
bool longName = false;
template<typename ParseContext>
constexpr ParseContext::iterator parse(ParseContext& ctx)
{
auto it = ctx.begin();
if constexpr (requires { requires av::has_long_name_method_with_ec<T> || av::has_long_name_method_without_ec<T>; }) {
if (it == ctx.end())
return it;
if (*it == 'l') {
longName = true;
++it;
}
if (it != ctx.end() && *it != '}')
throw std::format_error("Invalid format args");
}
return it;
}
template<typename ParseContext>
auto format(const T& value, ParseContext& ctx) const
{
if (longName) {
if constexpr (requires { requires av::has_long_name_method_with_ec<T>; }) {
std::error_code dummy;
return std::format_to(ctx.out(), "{}", value.longName(dummy));
} else if constexpr (requires { requires av::has_long_name_method_without_ec<T>; }) {
return std::format_to(ctx.out(), "{}", value.longName());
}
} else {
if constexpr (requires { requires av::has_name_method_with_ec<T>; }) {
std::error_code dummy;
return std::format_to(ctx.out(), "{}", value.name(dummy));
} else {
return std::format_to(ctx.out(), "{}", value.name());
}
}
return ctx.out();
}
};
#endif