44 template<
typename Duration,
45 typename =
typename Duration::period,
46 typename = std::enable_if_t<std::is_integral_v<typename Duration::rep>>>
49 using Ratio =
typename Duration::period;
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");
54 m_timestamp = duration.count();
55 m_timebase =
Rational(
static_cast<int>(Ratio::num),
56 static_cast<int>(Ratio::den));
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)})
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");
88 template<
typename Duration,
89 typename =
typename Duration::period,
90 typename = std::enable_if_t<std::is_floating_point_v<typename Duration::rep>>>
94 using ValueType =
typename Duration::rep;
95 using Ratio =
typename Duration::period;
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");
102 ValueType
const b =
static_cast<ValueType
>(Ratio::num) * m_timebase.getDenominator();
103 ValueType
const c =
static_cast<ValueType
>(Ratio::den) * m_timebase.getNumerator();
105 m_timestamp =
static_cast<int64_t
>(duration.count() * b / c);
135 operator
bool() const noexcept;
154 operator
double() const noexcept;
168 template<typename Duration,
169 typename = typename Duration::period,
170 typename = typename Duration::rep>
181 using ValueType =
typename Duration::rep;
182 using Ratio =
typename Duration::period;
184 static_assert(Ratio::num <= INT_MAX,
"To prevent precision lost, ratio numerator must be less then INT_MAX");
185 static_assert(Ratio::den <= INT_MAX,
"To prevent precision lost, ratio denominator must be less then INT_MAX");
187 if constexpr (std::is_integral_v<ValueType>) {
188 Rational dstTimebase(
static_cast<int>(Ratio::num),
189 static_cast<int>(Ratio::den));
190 auto ts = m_timebase.rescale(m_timestamp, dstTimebase);
193 namespace dt = std::chrono;
195 ValueType
const b = m_timebase.getNumerator() *
static_cast<ValueType
>(Ratio::den);
196 ValueType
const c = m_timebase.getDenominator() *
static_cast<ValueType
>(Ratio::num);
197 ValueType
const ts =
static_cast<ValueType
>(m_timestamp) * b / c;
256 return av_compare_ts(left.timestamp(), left.timebase(),
257 right.timestamp(), right.timebase()) > 0;
272 return av_compare_ts(left.timestamp(), left.timebase(),
273 right.timestamp(), right.timebase()) < 0;
288 return av_compare_ts(left.timestamp(), left.timebase(),
289 right.timestamp(), right.timebase()) == 0;
317 return av_compare_ts(left.timestamp(), left.timebase(),
318 right.timestamp(), right.timebase()) >= 0;
332 return av_compare_ts(left.timestamp(), left.timebase(),
333 right.timestamp(), right.timebase()) <= 0;
401#ifdef __cpp_lib_format
405template <
typename CharT>
406struct std::formatter<
av::Timestamp, CharT>
408 template<
typename ParseContext>
409 constexpr auto parse(ParseContext& ctx)
414 template<
typename ParseContext>
415 auto format(
const av::Timestamp& ts, ParseContext& ctx)
const
418 return std::format_to(ctx.out(),
"NO_PTS");
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
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.
constexpr Duration toDuration() const
Convert to the std::chrono::duration compatible value.
Definition timestamp.h:179
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:270
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:254
bool operator<=(const Timestamp &left, const Timestamp &right) noexcept
Compare two timestamps.
Definition timestamp.h:330
bool operator>=(const Timestamp &left, const Timestamp &right) noexcept
Compare two timestamps.
Definition timestamp.h:315
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:286
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:301
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.