avcpp 3.2.1
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
timestamp.h
Go to the documentation of this file.
1#pragma once
2
9
10#include <chrono>
11
12#include "avutils.h"
13#include "rational.h"
14
15namespace av {
16
24class Timestamp
25{
26public:
27 Timestamp() noexcept;
34 Timestamp(int64_t timestamp, const Rational &timebase) noexcept;
35
44 template<typename Duration,
45 typename = typename Duration::period,
46 typename = std::enable_if_t<std::is_integral_v<typename Duration::rep>>>
47 constexpr Timestamp(const Duration& duration)
48 {
49 using Ratio = typename Duration::period;
50
51 static_assert(Ratio::num <= INT_MAX, "To prevent precision lost, ratio numerator must be less then INT_MAX");
52 static_assert(Ratio::den <= INT_MAX, "To prevent precision lost, ratio denominator must be less then INT_MAX");
53
54 m_timestamp = duration.count();
55 m_timebase = Rational(static_cast<int>(Ratio::num),
56 static_cast<int>(Ratio::den));
57 }
58
67 template<typename Duration,
68 typename PrecisionPeriod,
69 typename = typename Duration::period,
70 typename = std::enable_if_t<std::is_floating_point_v<typename Duration::rep>>>
71 constexpr Timestamp(const Duration& duration, PrecisionPeriod)
72 : Timestamp(duration, Rational{static_cast<int>(PrecisionPeriod::num), static_cast<int>(PrecisionPeriod::den)})
73 {
74 using Ratio = typename Duration::period;
75 static_assert(Ratio::num <= INT_MAX, "To prevent precision lost, ratio numerator must be less then INT_MAX");
76 static_assert(Ratio::den <= INT_MAX, "To prevent precision lost, ratio denominator must be less then INT_MAX");
77 static_assert(PrecisionPeriod::num <= INT_MAX, "To prevent precision lost, ratio numerator must be less then INT_MAX");
78 static_assert(PrecisionPeriod::den <= INT_MAX, "To prevent precision lost, ratio denominator must be less then INT_MAX");
79 }
80
88 template<typename Duration,
89 typename = typename Duration::period,
90 typename = std::enable_if_t<std::is_floating_point_v<typename Duration::rep>>>
91 constexpr Timestamp(const Duration& duration, const Rational& timebase)
92 : m_timebase(timebase)
93 {
94 using ValueType = typename Duration::rep;
95 using Ratio = typename Duration::period;
96
97 static_assert(Ratio::num <= INT_MAX, "To prevent precision lost, ratio numerator must be less then INT_MAX");
98 static_assert(Ratio::den <= INT_MAX, "To prevent precision lost, ratio denominator must be less then INT_MAX");
99
100 // rescale input ticks into integer one
101 // m_timestamp = ts * Raio / m_timebase
102 ValueType const b = static_cast<ValueType>(Ratio::num) * m_timebase.getDenominator();
103 ValueType const c = static_cast<ValueType>(Ratio::den) * m_timebase.getNumerator();
104
105 m_timestamp = static_cast<int64_t>(duration.count() * b / c);
106 }
107
108
114 int64_t timestamp() const noexcept;
122 int64_t timestamp(const Rational &timebase) const noexcept;
128 const Rational& timebase() const noexcept;
129
135 operator bool() const noexcept;
141 bool isValid() const noexcept;
147 bool isNoPts() const noexcept;
148
154 operator double() const noexcept;
160 double seconds() const noexcept;
161
168 template<typename Duration,
169 typename = typename Duration::period,
170 typename = typename Duration::rep>
179#if AVCPP_CXX_STANDARD >= 23
180 constexpr Duration toDuration() const
181#else
182 Duration toDuration() const
183#endif
184 {
185 using ValueType = typename Duration::rep;
186 using Ratio = typename Duration::period;
187
188 static_assert(Ratio::num <= INT_MAX, "To prevent precision lost, ratio numerator must be less then INT_MAX");
189 static_assert(Ratio::den <= INT_MAX, "To prevent precision lost, ratio denominator must be less then INT_MAX");
190
191 if constexpr (std::is_integral_v<ValueType>) {
192 Rational dstTimebase(static_cast<int>(Ratio::num),
193 static_cast<int>(Ratio::den));
194 auto ts = m_timebase.rescale(m_timestamp, dstTimebase);
195 return Duration(ts);
196 } else {
197 namespace dt = std::chrono;
198 // ts = m_timestamp * m_timebase / dstTimebase
199 ValueType const b = m_timebase.getNumerator() * static_cast<ValueType>(Ratio::den);
200 ValueType const c = m_timebase.getDenominator() * static_cast<ValueType>(Ratio::num);
201 ValueType const ts = static_cast<ValueType>(m_timestamp) * b / c;
202 return Duration{ts};
203 }
204 }
205
213 Timestamp& operator+=(const Timestamp &other);
221 Timestamp& operator-=(const Timestamp &other);
229 Timestamp& operator*=(const Timestamp &other);
237 Timestamp& operator/=(const Timestamp &other);
238
239private:
240 int64_t m_timestamp = av::NoPts;
241 Rational m_timebase = av::TimeBaseQ;
242};
243
244
245//
246// Comparation
247//
248
249inline
258bool operator>(const Timestamp& left, const Timestamp &right) noexcept
259{
260 return av_compare_ts(left.timestamp(), left.timebase(),
261 right.timestamp(), right.timebase()) > 0;
262}
263
264
265inline
274bool operator<(const Timestamp& left, const Timestamp &right) noexcept
275{
276 return av_compare_ts(left.timestamp(), left.timebase(),
277 right.timestamp(), right.timebase()) < 0;
278}
279
280
281inline
290bool operator==(const Timestamp& left, const Timestamp &right) noexcept
291{
292 return av_compare_ts(left.timestamp(), left.timebase(),
293 right.timestamp(), right.timebase()) == 0;
294}
295
296inline
305bool operator!=(const Timestamp& left, const Timestamp &right) noexcept
306{
307 return !operator==(left, right);
308}
309
310inline
319bool operator>=(const Timestamp& left, const Timestamp& right) noexcept
320{
321 return av_compare_ts(left.timestamp(), left.timebase(),
322 right.timestamp(), right.timebase()) >= 0;
323}
324
325inline
334bool operator<=(const Timestamp& left, const Timestamp& right) noexcept
335{
336 return av_compare_ts(left.timestamp(), left.timebase(),
337 right.timestamp(), right.timebase()) <= 0;
338}
339
340//
341// Math operations
342//
343
352Timestamp operator+(const Timestamp& left, const Timestamp &right) noexcept;
361Timestamp operator-(const Timestamp& left, const Timestamp &right) noexcept;
370Timestamp operator*(const Timestamp& left, const Timestamp &right) noexcept;
379Timestamp operator/(const Timestamp& left, const Timestamp &right) noexcept;
380
381//
382// Output
383//
384inline
393std::ostream& operator<<(std::ostream &ost, const Timestamp &ts)
394{
395 if (ts.isNoPts()) {
396 ost << "NO_PTS";
397 } else {
398 ost << ts.timestamp() << '*' << ts.timebase();
399 }
400 return ost;
401}
402
403} // ::av
404
405#ifdef __cpp_lib_format
406#include <format>
407
408// std::format
409template <typename CharT>
410struct std::formatter<av::Timestamp, CharT>
411{
412 template<typename ParseContext>
413 constexpr auto parse(ParseContext& ctx)
414 {
415 return ctx.begin();
416 }
417
418 template<typename ParseContext>
419 auto format(const av::Timestamp& ts, ParseContext& ctx) const
420 {
421 if (ts.isNoPts()) {
422 return std::format_to(ctx.out(), "NO_PTS");
423 }
424 return std::format_to(ctx.out(), "{}*{}", ts.timestamp(), ts.timebase());
425 }
426};
427#endif
Common helpers: comparisons, value lookup, non-copyable base, string utilities.
Rational number: a numerator and a denominator.
Definition rational.h:49
Rational() noexcept
Create zero, in the sense of 0/1.
The Timestamp class represents timestamp value and it timebase.
Definition timestamp.h:25
bool isNoPts() const noexcept
Whether the timestamp means "no timestamp".
int64_t timestamp() const noexcept
The raw timestamp value.
constexpr Timestamp(const Duration &duration)
Create AvCpp/FFmpeg compatible timestamp value from the std::chrono::duration/boostchrono::duration.
Definition timestamp.h:47
Duration toDuration() const
Convert to the std::chrono::duration compatible value.
Definition timestamp.h:182
Timestamp(int64_t timestamp, const Rational &timebase) noexcept
Create a timestamp from a value and its time base.
Timestamp & operator+=(const Timestamp &other)
Add another timestamp, aligning the time bases.
bool isValid() const noexcept
Whether the timestamp is usable.
constexpr Timestamp(const Duration &duration, PrecisionPeriod)
Create AvCpp/FFmpeg compatible timestamp value from the floating point std::chrono::duration/boostchr...
Definition timestamp.h:71
double seconds() const noexcept
The timestamp in seconds.
const Rational & timebase() const noexcept
The time base of the timestamp.
Timestamp & operator/=(const Timestamp &other)
Divide by another timestamp.
Timestamp & operator-=(const Timestamp &other)
Subtract another timestamp, aligning the time bases.
Timestamp & operator*=(const Timestamp &other)
Multiply by another timestamp.
constexpr Timestamp(const Duration &duration, const Rational &timebase)
Create AvCpp/FFmpeg compatible timestamp value from the floating point std::chrono::duration/boostchr...
Definition timestamp.h:91
Root namespace of the library.
Definition audioresampler.h:19
bool operator<(const Timestamp &left, const Timestamp &right) noexcept
Compare two timestamps, taking their time bases into account.
Definition timestamp.h:274
Timestamp operator-(const Timestamp &left, const Timestamp &right) noexcept
Subtract two timestamps, aligning the time bases.
Timestamp operator+(const Timestamp &left, const Timestamp &right) noexcept
Add two timestamps, aligning the time bases.
bool operator>(const Timestamp &left, const Timestamp &right) noexcept
Compare two timestamps, taking their time bases into account.
Definition timestamp.h:258
bool operator<=(const Timestamp &left, const Timestamp &right) noexcept
Compare two timestamps.
Definition timestamp.h:334
bool operator>=(const Timestamp &left, const Timestamp &right) noexcept
Compare two timestamps.
Definition timestamp.h:319
Timestamp operator*(const Timestamp &left, const Timestamp &right) noexcept
Multiply two timestamps.
bool operator==(const Timestamp &left, const Timestamp &right) noexcept
Compare two timestamps, taking their time bases into account.
Definition timestamp.h:290
constexpr auto TimeBaseQ
The global time base as a rational value.
Definition avutils.h:63
bool operator!=(const Timestamp &left, const Timestamp &right) noexcept
Compare two timestamps.
Definition timestamp.h:305
std::ostream & operator<<(std::ostream &stream, const Rational &value)
Print the rational as "numerator/denominator".
Definition rational.h:348
auto operator/(T num, Rational value) -> std::enable_if_t< std::is_floating_point_v< T >||std::is_integral_v< T >, Rational >
Divide a number by a rational.
Definition rational.h:317
constexpr auto NoPts
Timestamp value meaning "no timestamp".
Definition avutils.h:55
Rational numbers, time bases and arithmetic on them.