avcpp 3.1.0
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 constexpr Duration toDuration() const
180 {
181 using ValueType = typename Duration::rep;
182 using Ratio = typename Duration::period;
183
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");
186
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);
191 return Duration(ts);
192 } else {
193 namespace dt = std::chrono;
194 // ts = m_timestamp * m_timebase / dstTimebase
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;
198 return Duration{ts};
199 }
200 }
201
209 Timestamp& operator+=(const Timestamp &other);
217 Timestamp& operator-=(const Timestamp &other);
225 Timestamp& operator*=(const Timestamp &other);
233 Timestamp& operator/=(const Timestamp &other);
234
235private:
236 int64_t m_timestamp = av::NoPts;
237 Rational m_timebase = av::TimeBaseQ;
238};
239
240
241//
242// Comparation
243//
244
245inline
254bool operator>(const Timestamp& left, const Timestamp &right) noexcept
255{
256 return av_compare_ts(left.timestamp(), left.timebase(),
257 right.timestamp(), right.timebase()) > 0;
258}
259
260
261inline
270bool operator<(const Timestamp& left, const Timestamp &right) noexcept
271{
272 return av_compare_ts(left.timestamp(), left.timebase(),
273 right.timestamp(), right.timebase()) < 0;
274}
275
276
277inline
286bool operator==(const Timestamp& left, const Timestamp &right) noexcept
287{
288 return av_compare_ts(left.timestamp(), left.timebase(),
289 right.timestamp(), right.timebase()) == 0;
290}
291
292inline
301bool operator!=(const Timestamp& left, const Timestamp &right) noexcept
302{
303 return !operator==(left, right);
304}
305
306inline
315bool operator>=(const Timestamp& left, const Timestamp& right) noexcept
316{
317 return av_compare_ts(left.timestamp(), left.timebase(),
318 right.timestamp(), right.timebase()) >= 0;
319}
320
321inline
330bool operator<=(const Timestamp& left, const Timestamp& right) noexcept
331{
332 return av_compare_ts(left.timestamp(), left.timebase(),
333 right.timestamp(), right.timebase()) <= 0;
334}
335
336//
337// Math operations
338//
339
348Timestamp operator+(const Timestamp& left, const Timestamp &right) noexcept;
357Timestamp operator-(const Timestamp& left, const Timestamp &right) noexcept;
366Timestamp operator*(const Timestamp& left, const Timestamp &right) noexcept;
375Timestamp operator/(const Timestamp& left, const Timestamp &right) noexcept;
376
377//
378// Output
379//
380inline
389std::ostream& operator<<(std::ostream &ost, const Timestamp &ts)
390{
391 if (ts.isNoPts()) {
392 ost << "NO_PTS";
393 } else {
394 ost << ts.timestamp() << '*' << ts.timebase();
395 }
396 return ost;
397}
398
399} // ::av
400
401#ifdef __cpp_lib_format
402#include <format>
403
404// std::format
405template <typename CharT>
406struct std::formatter<av::Timestamp, CharT>
407{
408 template<typename ParseContext>
409 constexpr auto parse(ParseContext& ctx)
410 {
411 return ctx.begin();
412 }
413
414 template<typename ParseContext>
415 auto format(const av::Timestamp& ts, ParseContext& ctx) const
416 {
417 if (ts.isNoPts()) {
418 return std::format_to(ctx.out(), "NO_PTS");
419 }
420 return std::format_to(ctx.out(), "{}*{}", ts.timestamp(), ts.timebase());
421 }
422};
423#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
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.