14 #include <type_traits>
16 #if AVCPP_CXX_STANDARD >= 20
23 #if AVCPP_HAS_AVFORMAT
25 #include <libavformat/avformat.h>
35 constexpr
auto NoPts =
static_cast<int64_t
>(AV_NOPTS_VALUE);
36 constexpr
auto TimeBase =
static_cast<int>(AV_TIME_BASE);
37 constexpr
auto TimeBaseQ = AVRational{1, AV_TIME_BASE};
40 template<
typename R,
typename T>
80 #define setFFmpegLoggingLevel set_logging_level
89 [[deprecated(
"Use av::hex_dump()")]]
123 bool operator() (
struct SwsContext* &swsContext);
124 bool operator() (
struct AVCodecContext* &codecContext);
125 #if AVCPP_HAS_AVFORMAT
126 bool operator() (
struct AVOutputFormat* &format);
127 bool operator() (
struct AVFormatContext* &formatContext);
131 bool operator() (
struct AVDictionary* &dictionary);
132 #if AVCPP_HAS_AVFILTER
133 bool operator ()(
struct AVFilterInOut* &filterInOut);
138 inline namespace v2 {
149 std::unique_ptr<T, void(*)(
void*)>
malloc(
size_t size)
151 return {
static_cast<T*
>(av_malloc(size)), av_free};
155 std::unique_ptr<T, void(*)(
void*)>
mallocz(
size_t size)
157 return {
static_cast<T*
>(av_mallocz(size)), av_free};
161 std::unique_ptr<T, void(*)(
void*)>
memdup(
const void *p,
size_t size)
163 return {
static_cast<T*
>(av_memdup(p, size)), av_free};
172 AVFilterInOut * operator()(AVFilterInOut * x)
const
196 template<
typename T,
typename V = T>
265 template<
typename Proc>
270 template<
typename Proc>
272 : m_proc(
std::forward<Proc>(proc))
282 std::function<void()> m_proc;
288 #if AVCPP_CXX_STANDARD >= 20
294 struct SentinelEndPolicy
296 using value_type = std::remove_reference_t<T>;
298 constexpr
bool isEnd(T* it)
const noexcept {
307 struct SentinelUntilPolicy
309 using value_type = std::remove_cvref_t<T>;
311 constexpr
bool isEnd(T* it)
const noexcept {
312 return it ==
nullptr || *it == value;
320 template<
class T,
class Policy>
324 friend constexpr
bool operator==(T* it,
const ArraySentinel& s) noexcept {
325 return s.policy.isEnd(it);
329 template<
class T,
class U,
bool UseProxy>
330 struct ReferenceSelector;
332 template<
class T,
class U>
333 struct ReferenceSelector<T, U, true>
338 template<
class T,
class U>
339 struct ReferenceSelector<T, U, false>
350 template<
typename T,
typename U>
353 static constexpr
bool UseProxy = !std::is_same_v<std::remove_cvref_t<T>, std::remove_cvref_t<U>>;
355 using difference_type = std::ptrdiff_t;
356 using value_type = std::remove_reference_t<U>;
357 using pointer = std::remove_reference_t<T>*;
359 using reference =
typename ReferenceSelector<T, U, !UseProxy>::Type;
360 using iterator_category =
363 std::random_access_iterator_tag,
364 std::contiguous_iterator_tag>;
366 ArrayIterator() noexcept = default;
367 ArrayIterator(pointer ptr) noexcept : m_ptr(ptr) {}
370 if constexpr (UseProxy)
371 return reference{*m_ptr};
376 ArrayIterator& operator++() noexcept { m_ptr++;
return *
this; }
377 ArrayIterator operator++(
int) noexcept { ArrayIterator tmp = *
this; ++(*this);
return tmp; }
379 ArrayIterator& operator--() noexcept { m_ptr--;
return *
this; }
380 ArrayIterator operator--(
int) noexcept { ArrayIterator tmp = *
this; --(*this);
return tmp; }
382 ArrayIterator& operator+=(
int n) noexcept { m_ptr+=n;
return *
this; }
383 ArrayIterator& operator-=(
int n) noexcept { m_ptr-=n;
return *
this; }
386 reference operator[](
int n) noexcept {
387 if constexpr (UseProxy)
388 return reference{m_ptr[n]};
392 const reference operator[](
int n)
const noexcept {
393 if constexpr (UseProxy)
394 return reference{m_ptr[n]};
401 friend ArrayIterator
operator+(
const ArrayIterator& a,
int n) noexcept {
return {a.m_ptr + n}; }
402 friend ArrayIterator
operator+(
int n,
const ArrayIterator& a) noexcept {
return {a.m_ptr + n}; }
404 friend ArrayIterator
operator-(
const ArrayIterator& a,
int n) noexcept {
return {a.m_ptr - n}; }
406 friend difference_type
operator-(
const ArrayIterator& b,
const ArrayIterator& a) noexcept {
return b.m_ptr - a.m_ptr; }
409 friend std::strong_ordering operator<=>(
const ArrayIterator& a,
const ArrayIterator& b) noexcept {
410 return a.m_ptr == b.m_ptr ? std::strong_ordering::equal
411 : a.m_ptr < b.m_ptr ? std::strong_ordering::less
412 : std::strong_ordering::greater;
414 friend bool operator== (
const ArrayIterator& a,
const ArrayIterator& b) {
return a.m_ptr == b.m_ptr; };
415 friend bool operator!= (
const ArrayIterator& a,
const ArrayIterator& b) {
return !(a == b); };
420 friend bool operator==(ArrayIterator it, std::default_sentinel_t) =
delete;
422 friend bool operator==(ArrayIterator it,
auto sentinel) {
return it.m_ptr == sentinel; }
423 friend bool operator!=(ArrayIterator it,
auto sentinel) {
return it.m_ptr != sentinel; }
424 friend bool operator==(
auto sentinel, ArrayIterator it) {
return it.m_ptr == sentinel; }
425 friend bool operator!=(
auto sentinel, ArrayIterator it) {
return it.m_ptr != sentinel; }
436 template<
typename T,
typename U,
class Policy>
437 class ArrayView :
public std::ranges::view_interface<ArrayView<T, U, Policy>>
440 constexpr ArrayView(T *ptr, Policy policy)
441 : m_ptr(ptr), m_policy(
std::move(policy))
444 auto constexpr begin() const noexcept
446 return ArrayIterator<T, U>{m_ptr};
449 auto constexpr end() const noexcept
452 if constexpr (std::is_same_v<Policy, std::size_t>) {
453 return ArrayIterator<T, U>{m_ptr + m_policy};
455 return ArraySentinel<T, Policy>{m_policy};
470 auto make_array_view_size(
auto* ptr, std::size_t size)
472 using T = std::remove_reference_t<decltype(*ptr)>;
473 return ArrayView<T, U, std::size_t>(ptr, size);
482 auto make_array_view_until(
auto* ptr,
auto value)
484 using T = std::remove_reference_t<decltype(*ptr)>;
485 return ArrayView<T, U, SentinelUntilPolicy<T>>(ptr, SentinelUntilPolicy<T>{value});
502 template<
typename T,
typename L>
503 requires std::ranges::range<L> &&
505 std::ranges::empty(r);
513 T bestDistance = std::numeric_limits<T>::max();
515 for (
auto&& cur : list) {
516 auto const distance = std::max(cur, value) - std::min(cur, value);
517 if (distance < bestDistance) {
518 bestDistance = distance;
537 if (this->value ==
value)
561 template<
typename T,
typename L,
typename C>
562 T
guessValue(
const T& value,
const L * list, C endListComparator)
568 T bestDistance = std::numeric_limits<T>::max();
570 for (
const L * ptr = list; !endListComparator(*ptr); ++ptr) {
571 auto const cur = *ptr;
572 auto const distance = std::max(cur, value) - std::min(cur, value);
573 if (distance < bestDistance) {
574 bestDistance = distance;
583 template<
typename T,
typename Container>
586 if (!array || nelements == 0)
588 std::copy_n(array, array + nelements, std::back_inserter(container));
591 template<
typename T,
typename Container,
typename Callable>
592 void array_to_container(
const T* array, std::size_t nelements, Container &container, Callable convert)
594 if (!array || nelements == 0)
598 for (
auto i = 0u; i < nelements; ++i) {
599 container.push_back(convert(array[i]));
603 template<
typename T,
typename Container,
typename Compare>
609 while (!isEnd(value = *array++))
610 container.push_back(value);
613 template<
typename T,
typename Container,
typename Compare,
typename Callable>
619 while (!isEnd(value = *array++))
620 container.push_back(convert(value));
628 void hex_dump(FILE *f,
const uint8_t *buf, std::size_t size);
629 void hex_dump_log(
void *avcl,
int level,
const uint8_t *buf, std::size_t size);
631 #if AVCPP_CXX_STANDARD >= 20
632 void hex_dump(FILE *f, std::span<const uint8_t> buf);
633 void hex_dump_log(
void *avcl,
int level, std::span<const uint8_t> buf);
640 #ifdef __cpp_lib_format
644 template <
typename B>
645 concept has_name_method_with_ec = requires(
const B& type, std::error_code ec) {
646 { type.name(ec) } -> std::convertible_to<std::string_view>;
649 template <
typename B>
650 concept has_name_method_without_ec = requires(
const B& type) {
651 { type.name() } -> std::convertible_to<std::string_view>;
654 template <
typename B>
655 concept has_long_name_method_with_ec = requires(
const B& type, std::error_code ec) {
656 { type.longName(ec) } -> std::convertible_to<std::string_view>;
659 template <
typename B>
660 concept has_long_name_method_without_ec = requires(
const B& type) {
661 { type.longName() } -> std::convertible_to<std::string_view>;
665 template <
class T,
class CharT>
666 requires av::has_name_method_with_ec<T> || av::has_name_method_without_ec<T>
667 struct std::formatter<T, CharT>
669 bool longName =
false;
671 template<
typename ParseContext>
672 constexpr ParseContext::iterator parse(ParseContext& ctx)
674 auto it = ctx.begin();
675 if constexpr (requires { requires av::has_long_name_method_with_ec<T> || av::has_long_name_method_without_ec<T>; }) {
682 if (it != ctx.end() && *it !=
'}')
683 throw std::format_error(
"Invalid format args");
688 template<
typename ParseContext>
689 auto format(
const T& value, ParseContext& ctx)
const
692 if constexpr (requires { requires av::has_long_name_method_with_ec<T>; }) {
693 std::error_code dummy;
694 return std::format_to(ctx.out(),
"{}", value.longName(dummy));
695 }
else if constexpr (requires { requires av::has_long_name_method_without_ec<T>; }) {
696 return std::format_to(ctx.out(),
"{}", value.longName());
699 if constexpr (requires { requires av::has_name_method_with_ec<T>; }) {
700 std::error_code dummy;
701 return std::format_to(ctx.out(),
"{}", value.name(dummy));
703 return std::format_to(ctx.out(),
"{}", value.name());
The ScopeOutAction class - guard-type class that allows points callback that will be called at the sc...
Definition: avutils.h:263
~ScopeOutAction()
Definition: avutils.h:275
ScopeOutAction(Proc &&proc)
Definition: avutils.h:271
ScopeOutAction(const Proc &proc)
Definition: avutils.h:266
Functor to take next element in list/array.
Definition: avutils.h:198
ScopedValue(T &var, const V &outValue)
Ctor.
Definition: avutils.h:205
~ScopedValue()
Definition: avutils.h:224
ScopedValue(T &var, const V &inValue, const V &outValue)
Ctor.
Definition: avutils.h:217
void operator=(const noncopyable &)=delete
noncopyable(const noncopyable &)=delete
Definition: audioresampler.cpp:8
R lexical_cast(const T &v)
Definition: avutils.h:41
string error2string(int error)
C++ verstion of the av_err2str()
Definition: avutils.cpp:211
Timestamp operator-(const Timestamp &left, const Timestamp &right) noexcept
Definition: timestamp.cpp:102
Timestamp operator+(const Timestamp &left, const Timestamp &right) noexcept
Definition: timestamp.cpp:90
std::unique_ptr< T, void(*)(void *)> memdup(const void *p, size_t size)
Definition: avutils.h:161
Timestamp operator*(const Timestamp &left, const Timestamp &right) noexcept
Definition: timestamp.cpp:114
std::unique_ptr< T, void(*)(void *)> malloc(size_t size)
Definition: avutils.h:149
void hex_dump_log(void *avcl, int level, const uint8_t *buf, std::size_t size)
Definition: avutils.cpp:324
void dumpBinaryBuffer(uint8_t *buffer, int buffer_size, int width)
dump_binary_buffer Dump binary buffer to std out in HEX view
Definition: avutils.cpp:77
T guessValue(const T &value, const L *list, C endListComparator)
Select more approptiate value from given value list.
Definition: avutils.h:562
constexpr auto TimeBaseQ
Definition: avutils.h:37
void hex_dump(FILE *f, const uint8_t *buf, std::size_t size)
Definition: avutils.cpp:319
bool operator==(const Dictionary::Entry &lhs, const Dictionary::Entry &rhs)
Definition: dictionary.cpp:382
constexpr auto TimeBase
Definition: avutils.h:36
bool operator!=(const Dictionary::Entry &lhs, const Dictionary::Entry &rhs)
Definition: dictionary.cpp:387
void array_to_container(const T *array, std::size_t nelements, Container &container)
Definition: avutils.h:584
void set_logging_level(int32_t level)
This method can be used to turn up or down FFmpeg's logging level.
Definition: avutils.cpp:26
std::unique_ptr< T, void(*)(void *)> mallocz(size_t size)
Definition: avutils.h:155
constexpr auto NoPts
Definition: avutils.h:35
Definition: averror.h:228
Definition: avutils.h:111
void operator()(void *)
Definition: avutils.h:112
Definition: avutils.h:530
bool operator()(const T &value) const
Definition: avutils.h:535
EqualComparator(const T &value)
Definition: avutils.h:531
const T & value
Definition: avutils.h:543
Definition: avutils.h:122
bool operator()(struct SwsContext *&swsContext)
Definition: avutils.cpp:219
Definition: avutils.h:140
bool operator()(T *ptr)
Definition: avutils.h:142