10#include "avcpp/avconfig.h"
25#if AVCPP_CXX_STANDARD >= 20
33#include <libavutil/avstring.h>
34#include <libavutil/mem.h>
39#include <libavformat/avformat.h>
55constexpr auto NoPts =
static_cast<int64_t
>(AV_NOPTS_VALUE);
59constexpr auto TimeBase =
static_cast<int>(AV_TIME_BASE);
63constexpr auto TimeBaseQ = AVRational{1, AV_TIME_BASE};
77template<
typename R,
typename T>
96 noncopyable() =
default;
97 noncopyable(
const noncopyable& ) =
delete;
98 void operator=(
const noncopyable& ) =
delete;
128#define setFFmpegLoggingLevel set_logging_level
137[[deprecated(
"Use av::hex_dump()")]]
153inline std::string_view
wrap2sview(
const char *value)
noexcept
155 return value ? std::string_view(value) : std::string_view();
213#if AVCPP_HAS_AVFORMAT
255#if AVCPP_HAS_AVFILTER
311std::unique_ptr<T, void(*)(
void*)>
malloc(
size_t size)
313 return {
static_cast<T*
>(av_malloc(size)), av_free};
326std::unique_ptr<T, void(*)(
void*)>
mallocz(
size_t size)
328 return {
static_cast<T*
>(av_mallocz(size)), av_free};
342std::unique_ptr<T, void(*)(
void*)>
memdup(
const void *p,
size_t size)
344 return {
static_cast<T*
>(av_memdup(p, size)), av_free};
353inline std::unique_ptr<char, void(*)(
void*)>
strdup(std::string_view value)
356 return {
nullptr, av_free};
358 return {av_strndup(value.data(), value.size()), av_free};
378 AVFilterInOut * operator()(AVFilterInOut * x)
const
402template<
typename T,
typename V = T>
478 template<
typename Proc>
490 template<
typename Proc>
492 : m_proc(
std::forward<Proc>(proc))
502 std::function<void()> m_proc;
508#if AVCPP_CXX_STANDARD >= 20
527 constexpr bool isEnd(T* it)
const noexcept {
549 constexpr bool isEnd(T* it)
const noexcept {
550 return it ==
nullptr || *it ==
value;
558template<
class T,
class Policy>
572 return s.policy.isEnd(it);
576template<
class T,
class U,
bool UseProxy>
585template<
class T,
class U>
598template<
class T,
class U>
611template<
typename T,
typename U>
615 static constexpr bool UseProxy = !std::is_same_v<std::remove_cvref_t<T>, std::remove_cvref_t<U>>;
630 std::random_access_iterator_tag,
631 std::contiguous_iterator_tag>;
633 ArrayIterator() noexcept = default;
639 ArrayIterator(
pointer ptr) noexcept : m_ptr(ptr) {}
658 ArrayIterator&
operator++() noexcept { m_ptr++;
return *
this; }
664 ArrayIterator
operator++(
int)
noexcept { ArrayIterator tmp = *
this; ++(*this);
return tmp; }
671 ArrayIterator&
operator--() noexcept { m_ptr--;
return *
this; }
677 ArrayIterator
operator--(
int)
noexcept { ArrayIterator tmp = *
this; --(*this);
return tmp; }
686 ArrayIterator&
operator+=(
int n)
noexcept { m_ptr+=n;
return *
this; }
694 ArrayIterator&
operator-=(
int n)
noexcept { m_ptr-=n;
return *
this; }
734 friend ArrayIterator
operator+(
const ArrayIterator& a,
int n)
noexcept {
return {a.m_ptr + n}; }
743 friend ArrayIterator
operator+(
int n,
const ArrayIterator& a)
noexcept {
return {a.m_ptr + n}; }
753 friend ArrayIterator
operator-(
const ArrayIterator& a,
int n)
noexcept {
return {a.m_ptr - n}; }
774 friend std::strong_ordering
operator<=>(
const ArrayIterator& a,
const ArrayIterator& b)
noexcept {
775 return a.m_ptr == b.m_ptr ? std::strong_ordering::equal
776 : a.m_ptr < b.m_ptr ? std::strong_ordering::less
777 : std::strong_ordering::greater;
787 friend bool operator== (
const ArrayIterator& a,
const ArrayIterator& b) {
return a.m_ptr == b.m_ptr; };
796 friend bool operator!= (
const ArrayIterator& a,
const ArrayIterator& b) {
return !(a == b); };
811 template<
class Policy>
813 {
return sentinel.
policy.isEnd(it.m_ptr); }
823 template<
class Policy>
825 {
return !sentinel.
policy.isEnd(it.m_ptr); }
835 template<
class Policy>
837 {
return sentinel.
policy.isEnd(it.m_ptr); }
847 template<
class Policy>
849 {
return !sentinel.
policy.isEnd(it.m_ptr); }
860template<
typename T,
typename U,
class Policy>
861class ArrayView :
public std::ranges::view_interface<ArrayView<T, U, Policy>>
864 constexpr ArrayView() =
default;
874 : m_ptr(ptr), m_policy(
std::move(policy))
882 auto constexpr begin() const noexcept
892 auto constexpr end() const noexcept
895 if constexpr (std::is_same_v<Policy, std::size_t>) {
916 using T = std::remove_reference_t<
decltype(*ptr)>;
929 using T = std::remove_reference_t<
decltype(*ptr)>;
946template<
typename T,
typename L>
947 requires std::ranges::range<L> &&
949 std::ranges::empty(r);
957 T bestDistance = (std::numeric_limits<T>::max)();
959 for (
auto&& cur : list) {
960 auto const distance = (std::max)(cur, value) - (std::min)(cur, value);
961 if (distance < bestDistance) {
962 bestDistance = distance;
998 if (this->value ==
value)
1023template<
typename T,
typename L,
typename C>
1030 T bestDistance = (std::numeric_limits<T>::max)();
1032 for (
const L * ptr = list; !endListComparator(*ptr); ++ptr) {
1033 auto const cur = *ptr;
1034 auto const distance = (std::max)(cur, value) - (std::min)(cur, value);
1035 if (distance < bestDistance) {
1036 bestDistance = distance;
1052template<
typename T,
typename Container>
1055 if (!array || nelements == 0)
1057 std::copy_n(array, array + nelements, std::back_inserter(container));
1068template<
typename T,
typename Container,
typename Callable>
1071 if (!array || nelements == 0)
1075 for (
auto i = 0u; i < nelements; ++i) {
1076 container.push_back(convert(array[i]));
1088template<
typename T,
typename Container,
typename Compare>
1094 while (!isEnd(value = *array++))
1095 container.push_back(value);
1106template<
typename T,
typename Container,
typename Compare,
typename Callable>
1112 while (!isEnd(value = *array++))
1113 container.push_back(convert(value));
1128void hex_dump(FILE *f,
const uint8_t *buf, std::size_t size);
1137void hex_dump_log(
void *avcl,
int level,
const uint8_t *buf, std::size_t size);
1139#if AVCPP_CXX_STANDARD >= 20
1161#ifdef __cpp_lib_format
1165template <
typename B>
1166concept has_name_method_with_ec =
requires(
const B& type, std::error_code ec) {
1167 { type.name(ec) } -> std::convertible_to<std::string_view>;
1170template <
typename B>
1171concept has_name_method_without_ec =
requires(
const B& type) {
1172 { type.name() } -> std::convertible_to<std::string_view>;
1175template <
typename B>
1176concept has_long_name_method_with_ec =
requires(
const B& type, std::error_code ec) {
1177 { type.longName(ec) } -> std::convertible_to<std::string_view>;
1180template <
typename B>
1181concept has_long_name_method_without_ec =
requires(
const B& type) {
1182 { type.longName() } -> std::convertible_to<std::string_view>;
1186template <
class T,
class CharT>
1187 requires av::has_name_method_with_ec<T> || av::has_name_method_without_ec<T>
1188struct std::formatter<T, CharT>
1190 bool longName =
false;
1192 template<
typename ParseContext>
1193 constexpr ParseContext::iterator parse(ParseContext& ctx)
1195 auto it = ctx.begin();
1196 if constexpr (
requires {
requires av::has_long_name_method_with_ec<T> || av::has_long_name_method_without_ec<T>; }) {
1197 if (it == ctx.end())
1203 if (it != ctx.end() && *it !=
'}')
1204 throw std::format_error(
"Invalid format args");
1209 template<
typename ParseContext>
1210 auto format(
const T& value, ParseContext& ctx)
const
1213 if constexpr (
requires {
requires av::has_long_name_method_with_ec<T>; }) {
1214 std::error_code dummy;
1215 return std::format_to(ctx.out(),
"{}", value.longName(dummy));
1216 }
else if constexpr (
requires {
requires av::has_long_name_method_without_ec<T>; }) {
1217 return std::format_to(ctx.out(),
"{}", value.longName());
1220 if constexpr (
requires {
requires av::has_name_method_with_ec<T>; }) {
1221 std::error_code dummy;
1222 return std::format_to(ctx.out(),
"{}", value.name(dummy));
1224 return std::format_to(ctx.out(),
"{}", value.name());
Conversions between time units and FFmpeg time bases.
C-array (FFmpeg) wrapper witch can iterate over collection and represents any element via light-weigh...
Definition avutils.h:862
auto constexpr begin() const noexcept
First element.
Definition avutils.h:882
constexpr ArrayView(T *ptr, Policy policy)
Wrap an array with a policy that defines where it ends.
Definition avutils.h:873
auto constexpr end() const noexcept
End of the range.
Definition avutils.h:892
The ScopeOutAction class - guard-type class that allows points callback that will be called at the sc...
Definition avutils.h:469
ScopeOutAction(Proc &&proc)
Run a callback when the guard leaves scope, moving it in.
Definition avutils.h:491
ScopeOutAction(const Proc &proc)
Run a callback when the guard leaves scope.
Definition avutils.h:479
Functor to take next element in list/array.
Definition avutils.h:404
ScopedValue(T &var, const V &outValue)
Ctor.
Definition avutils.h:411
ScopedValue(T &var, const V &inValue, const V &outValue)
Ctor.
Definition avutils.h:423
FFmpeg types re-exported for the public API and small value wrappers.
The AvDeleter struct Unified delete functor for variois FFMPEG/libavformat/libavcodec and so on resou...
Definition avutils.h:187
Root namespace of the library.
Definition audioresampler.h:19
std::unique_ptr< char, void(*)(void *)> strdup(std::string_view value)
Duplicates a string using the FFmpeg allocator.
Definition avutils.h:353
std::unique_ptr< T, void(*)(void *)> malloc(size_t size)
Allocate memory with the FFmpeg allocator.
Definition avutils.h:311
R lexical_cast(const T &v)
Convert a value through its stream representation.
Definition avutils.h:78
std::string_view wrap2sview(const char *value) noexcept
Wraps a nullable C string into a string_view without copying.
Definition avutils.h:153
std::string error2string(int error)
C++ verstion of the av_err2str().
std::unique_ptr< T, void(*)(void *)> mallocz(size_t size)
Allocate zeroed memory with the FFmpeg allocator.
Definition avutils.h:326
void freep(T *&ptr) noexcept
Frees a buffer allocated by the FFmpeg allocator and resets the pointer.
Definition avutils.h:367
auto make_array_view_size(auto *ptr, std::size_t size)
Helper to create array view with well-known size.
Definition avutils.h:914
void hex_dump_log(void *avcl, int level, const uint8_t *buf, std::size_t size)
Write a hexadecimal dump of a buffer through the FFmpeg log system.
void dumpBinaryBuffer(uint8_t *buffer, int buffer_size, int width=16)
dump_binary_buffer Dump binary buffer to std out in HEX view
bool operator==(const Timestamp &left, const Timestamp &right) noexcept
Compare two timestamps, taking their time bases into account.
Definition timestamp.h:290
auto make_array_view_until(auto *ptr, auto value)
Helper to create array view with end marker.
Definition avutils.h:927
constexpr auto TimeBaseQ
The global time base as a rational value.
Definition avutils.h:63
void hex_dump(FILE *f, const uint8_t *buf, std::size_t size)
Write a hexadecimal dump of a buffer to a C stream.
std::unique_ptr< T, void(*)(void *)> memdup(const void *p, size_t size)
Copy a memory block using the FFmpeg allocator.
Definition avutils.h:342
constexpr auto TimeBase
Denominator of the global time base used for container-level timestamps.
Definition avutils.h:59
T guessValue(const T &value, L list)
Select more approptiate value from given value list.
Definition avutils.h:951
void array_to_container(const T *array, std::size_t nelements, Container &container)
Append a fixed number of array elements to a container.
Definition avutils.h:1053
void set_logging_level(int32_t level)
This method can be used to turn up or down FFmpeg's logging level.
constexpr auto NoPts
Timestamp value meaning "no timestamp".
Definition avutils.h:55
Wrapped array iterator.
Definition avutils.h:613
std::ptrdiff_t difference_type
Distance type required by the iterator traits.
Definition avutils.h:618
friend std::strong_ordering operator<=>(const ArrayIterator &a, const ArrayIterator &b) noexcept
Order two iterators.
Definition avutils.h:774
ArrayIterator operator--(int) noexcept
Step back, returning the previous position.
Definition avutils.h:677
friend bool operator==(const ArrayIterator &a, const ArrayIterator &b)
Compare two iterators.
Definition avutils.h:787
friend constexpr bool operator!=(const ArraySentinel< T, Policy > &sentinel, ArrayIterator it)
Compare a sentinel with an iterator.
Definition avutils.h:848
friend difference_type operator-(const ArrayIterator &b, const ArrayIterator &a) noexcept
Distance between two iterators.
Definition avutils.h:763
typename ReferenceSelector< T, U, !UseProxy >::Type reference
Dereference result: a proxy wrapper or a plain reference.
Definition avutils.h:625
ArrayIterator & operator--() noexcept
Step back to the previous element.
Definition avutils.h:671
ArrayIterator & operator+=(int n) noexcept
Advance by several elements.
Definition avutils.h:686
std::remove_reference_t< U > value_type
Type produced by dereferencing.
Definition avutils.h:620
std::remove_reference_t< T > * pointer
Pointer type of the underlying array.
Definition avutils.h:622
friend ArrayIterator operator+(int n, const ArrayIterator &a) noexcept
Advance an iterator.
Definition avutils.h:743
static constexpr bool UseProxy
Whether dereferencing must produce a proxy view instead of a reference.
Definition avutils.h:615
reference operator[](int n) noexcept
Element at an offset from the current position.
Definition avutils.h:704
ArrayIterator & operator++() noexcept
Advance to the next element.
Definition avutils.h:658
friend constexpr bool operator!=(ArrayIterator it, const ArraySentinel< T, Policy > &sentinel)
Compare an iterator with a sentinel.
Definition avutils.h:824
const reference operator[](int n) const noexcept
Element at an offset, read-only.
Definition avutils.h:717
friend constexpr bool operator==(ArrayIterator it, const ArraySentinel< T, Policy > &sentinel)
Compare an iterator with a sentinel.
Definition avutils.h:812
friend bool operator!=(const ArrayIterator &a, const ArrayIterator &b)
Compare two iterators.
Definition avutils.h:796
friend ArrayIterator operator+(const ArrayIterator &a, int n) noexcept
Advance an iterator.
Definition avutils.h:734
std::conditional_t< UseProxy, std::random_access_iterator_tag, std::contiguous_iterator_tag > iterator_category
Iterator category: random access for proxies, contiguous otherwise.
Definition avutils.h:627
ArrayIterator operator++(int) noexcept
Advance, returning the previous position.
Definition avutils.h:664
friend ArrayIterator operator-(const ArrayIterator &a, int n) noexcept
Step an iterator back.
Definition avutils.h:753
friend constexpr bool operator==(const ArraySentinel< T, Policy > &sentinel, ArrayIterator it)
Compare a sentinel with an iterator.
Definition avutils.h:836
reference operator*() const noexcept
Element at the current position.
Definition avutils.h:646
ArrayIterator & operator-=(int n) noexcept
Step back by several elements.
Definition avutils.h:694
Policy based sentinel marker.
Definition avutils.h:560
Policy policy
Policy that decides where the range ends.
Definition avutils.h:562
friend constexpr bool operator==(T *it, const ArraySentinel &s) noexcept
Compare an iterator with the sentinel.
Definition avutils.h:571
Deleter that does nothing.
Definition avutils.h:175
void operator()(void *)
Accept the pointer and do nothing with it.
Definition avutils.h:179
bool operator()(const T &value) const
Compare an element with the stored value.
Definition avutils.h:996
EqualComparator(const T &value)
Remember the value to compare against.
Definition avutils.h:985
const T & value
The value that terminates the list.
Definition avutils.h:1005
U Type
Dereference result when a proxy view is produced.
Definition avutils.h:602
T & Type
Dereference result when no proxy is needed.
Definition avutils.h:589
Sentinel policy that defines well-known range end pointer.
Definition avutils.h:515
std::remove_reference_t< T > value_type
Type of the range elements.
Definition avutils.h:517
value_type * end
Pointer that marks the end of the range.
Definition avutils.h:519
constexpr bool isEnd(T *it) const noexcept
Whether the iterator reached the end pointer.
Definition avutils.h:527
Sentinel policy that defines well-known sequence end marker, like '\0' at the C-string end or special...
Definition avutils.h:537
constexpr bool isEnd(T *it) const noexcept
Whether the iterator reached the terminating value.
Definition avutils.h:549
std::remove_cvref_t< T > value_type
Type of the range elements.
Definition avutils.h:539
value_type value
Value that terminates the sequence, for example '\0' or a null pointer.
Definition avutils.h:541
Free functors for the FFmpeg resource types.
Definition avutils.h:196
bool operator()(struct SwsContext *&swsContext)
Free a swscale context.
Template facade over the per-type deleters of namespace v1.
Definition avutils.h:284
bool operator()(T *ptr)
Free an FFmpeg object of type T.
Definition avutils.h:295