avcpp  2.0
Wrapper for the FFmpeg that simplify usage from C++ projects.
timestamp.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <chrono>
4 
5 #include "avutils.h"
6 #include "rational.h"
7 
8 namespace av {
9 
13 class Timestamp
14 {
15 public:
16  Timestamp() noexcept;
17  Timestamp(int64_t timestamp, const Rational &timebase) noexcept;
18 
22  template<typename Duration, typename = typename Duration::period>
23  Timestamp(const Duration& duration)
24  {
25  using Ratio = typename Duration::period;
26 
27  static_assert(Ratio::num <= INT_MAX, "To prevent precision lost, ratio numerator must be less then INT_MAX");
28  static_assert(Ratio::den <= INT_MAX, "To prevent precision lost, ratio denominator must be less then INT_MAX");
29 
30  m_timestamp = duration.count();
31  m_timebase = Rational(static_cast<int>(Ratio::num),
32  static_cast<int>(Ratio::den));
33  }
34 
35  int64_t timestamp() const noexcept;
36  int64_t timestamp(const Rational &timebase) const noexcept;
37  const Rational& timebase() const noexcept;
38 
39  operator bool() const noexcept;
40  bool isValid() const noexcept;
41  bool isNoPts() const noexcept;
42 
43  operator double() const noexcept;
44  double seconds() const noexcept;
45 
49  template<typename Duration>
50  Duration toDuration() const
51  {
52  using Ratio = typename Duration::period;
53 
54  static_assert(Ratio::num <= INT_MAX, "To prevent precision lost, ratio numerator must be less then INT_MAX");
55  static_assert(Ratio::den <= INT_MAX, "To prevent precision lost, ratio denominator must be less then INT_MAX");
56 
57  Rational dstTimebase(static_cast<int>(Ratio::num),
58  static_cast<int>(Ratio::den));
59  auto ts = m_timebase.rescale(m_timestamp, dstTimebase);
60  return Duration(ts);
61  }
62 
63  Timestamp& operator+=(const Timestamp &other);
64  Timestamp& operator-=(const Timestamp &other);
65  Timestamp& operator*=(const Timestamp &other);
66  Timestamp& operator/=(const Timestamp &other);
67 
68 private:
69  int64_t m_timestamp = av::NoPts;
70  Rational m_timebase = av::TimeBaseQ;
71 };
72 
73 
74 //
75 // Comparation
76 //
77 
78 inline
79 bool operator>(const Timestamp& left, const Timestamp &right) noexcept
80 {
81  return av_compare_ts(left.timestamp(), left.timebase(),
82  right.timestamp(), right.timebase()) > 0;
83 }
84 
85 
86 inline
87 bool operator<(const Timestamp& left, const Timestamp &right) noexcept
88 {
89  return av_compare_ts(left.timestamp(), left.timebase(),
90  right.timestamp(), right.timebase()) < 0;
91 }
92 
93 
94 inline
95 bool operator==(const Timestamp& left, const Timestamp &right) noexcept
96 {
97  return av_compare_ts(left.timestamp(), left.timebase(),
98  right.timestamp(), right.timebase()) == 0;
99 }
100 
101 inline
102 bool operator!=(const Timestamp& left, const Timestamp &right) noexcept
103 {
104  return !operator==(left, right);
105 }
106 
107 inline
108 bool operator>=(const Timestamp& left, const Timestamp& right) noexcept
109 {
110  return av_compare_ts(left.timestamp(), left.timebase(),
111  right.timestamp(), right.timebase()) >= 0;
112 }
113 
114 inline
115 bool operator<=(const Timestamp& left, const Timestamp& right) noexcept
116 {
117  return av_compare_ts(left.timestamp(), left.timebase(),
118  right.timestamp(), right.timebase()) <= 0;
119 }
120 
121 //
122 // Math operations
123 //
124 
125 Timestamp operator+(const Timestamp& left, const Timestamp &right) noexcept;
126 Timestamp operator-(const Timestamp& left, const Timestamp &right) noexcept;
127 Timestamp operator*(const Timestamp& left, const Timestamp &right) noexcept;
128 Timestamp operator/(const Timestamp& left, const Timestamp &right) noexcept;
129 
130 //
131 // Output
132 //
133 inline
134 std::ostream& operator<<(std::ostream &ost, const Timestamp &ts)
135 {
136  if (ts.isNoPts()) {
137  ost << "NO_PTS";
138  } else {
139  ost << ts.timestamp() << '*' << ts.timebase();
140  }
141  return ost;
142 }
143 
144 } // ::av
av::Timestamp::Timestamp
Timestamp(const Duration &duration)
Create AvCpp/FFmpeg compatible timestamp value from the std::chrono::duration/boost::chrono::duration...
Definition: timestamp.h:23
av::Timestamp::isNoPts
bool isNoPts() const noexcept
Definition: timestamp.cpp:39
av::Rational
Definition: rational.h:24
rational.h
av::Timestamp::operator/=
Timestamp & operator/=(const Timestamp &other)
Definition: timestamp.cpp:78
av::Timestamp::operator+=
Timestamp & operator+=(const Timestamp &other)
Definition: timestamp.cpp:57
av::operator*
Timestamp operator*(const Timestamp &left, const Timestamp &right) noexcept
Definition: timestamp.cpp:116
av::Rational::rescale
int64_t rescale(int64_t srcValue, const Rational &dstBase) const noexcept
Definition: rational.cpp:36
av::operator==
bool operator==(const Dictionary::Entry &lhs, const Dictionary::Entry &rhs)
Definition: dictionary.cpp:382
av::Timestamp::Timestamp
Timestamp() noexcept
Definition: timestamp.cpp:9
av::operator+
Timestamp operator+(const Timestamp &left, const Timestamp &right) noexcept
Definition: timestamp.cpp:90
av::Timestamp::toDuration
Duration toDuration() const
Convert to the std::chrono::duration compatible value.
Definition: timestamp.h:50
av::TimeBaseQ
constexpr auto TimeBaseQ
Definition: avutils.h:56
av::NoPts
constexpr auto NoPts
Definition: avutils.h:54
av::Timestamp::seconds
double seconds() const noexcept
Definition: timestamp.cpp:49
avutils.h
av::operator>
bool operator>(const Timestamp &left, const Timestamp &right) noexcept
Definition: timestamp.h:79
av::operator>=
bool operator>=(const Timestamp &left, const Timestamp &right) noexcept
Definition: timestamp.h:108
av::operator<<
std::ostream & operator<<(std::ostream &stream, const Rational &value)
Definition: rational.h:118
av::operator-
Timestamp operator-(const Timestamp &left, const Timestamp &right) noexcept
Definition: timestamp.cpp:104
av::operator/
auto operator/(T num, Rational value) -> std::enable_if_t< std::is_floating_point_v< T >||std::is_integral_v< T >, Rational >
Definition: rational.h:105
av::Timestamp::isValid
bool isValid() const noexcept
Definition: timestamp.cpp:34
av
Definition: audioresampler.cpp:8
av::Timestamp::operator-=
Timestamp & operator-=(const Timestamp &other)
Definition: timestamp.cpp:64
av::Timestamp::operator*=
Timestamp & operator*=(const Timestamp &other)
Definition: timestamp.cpp:71
av::Timestamp
The Timestamp class represents timestamp value and it timebase.
Definition: timestamp.h:13
av::operator<
bool operator<(const Timestamp &left, const Timestamp &right) noexcept
Definition: timestamp.h:87
av::operator!=
bool operator!=(const Dictionary::Entry &lhs, const Dictionary::Entry &rhs)
Definition: dictionary.cpp:387
av::Timestamp::timebase
const Rational & timebase() const noexcept
Definition: timestamp.cpp:29
av::operator<=
bool operator<=(const Timestamp &left, const Timestamp &right) noexcept
Definition: timestamp.h:115
av::Timestamp::timestamp
int64_t timestamp() const noexcept
Definition: timestamp.cpp:19