libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
timecode.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstdint>
12#include <promeki/core/string.h>
13#include <promeki/core/error.h>
14#include <vtc/vtc.h>
15
17
45class Timecode {
46 public:
50
58
60 enum Flags {
61 DropFrame = 0x00000001,
62 FirstField = 0x00000002
63 };
64
70 class Mode {
71 public:
73 Mode() = default;
74
77 Mode(const VtcFormat *format) : _format(format), _valid(format != nullptr) {}
78
84 Mode(uint32_t fps, uint32_t flags) : _valid(true) {
85 if(fps > 0) {
89 }
90 }
91
94 Mode(TimecodeType type) : _valid(true) {
95 switch(type) {
96 case NDF24: _format = &VTC_FORMAT_24; break;
97 case NDF25: _format = &VTC_FORMAT_25; break;
98 case NDF30: _format = &VTC_FORMAT_30_NDF; break;
99 case DF30: _format = &VTC_FORMAT_29_97_DF; break;
100 }
101 }
102
103 bool operator==(const Mode &other) const {
104 if(_format == other._format) return _valid == other._valid;
105 if(_format == nullptr || other._format == nullptr) {
106 // Both valid with no format = equal
107 return _valid == other._valid && _format == other._format;
108 }
109 return _format->tc_fps == other._format->tc_fps &&
111 }
112
113 bool operator!=(const Mode &other) const {
114 return !(*this == other);
115 }
116
118 uint32_t fps() const { return _format ? vtc_format_fps(_format) : 0; }
120 bool isValid() const { return _valid; }
122 bool isDropFrame() const { return _format ? vtc_format_is_drop_frame(_format) : false; }
124 bool hasFormat() const { return _format != nullptr; }
125
127 const VtcFormat *vtcFormat() const { return _format; }
128
129 private:
130 const VtcFormat *_format = nullptr;
131 bool _valid = false;
132 };
133
140 static Timecode fromFrameNumber(const Mode &mode, FrameNumber frameNumber);
141
147 static std::pair<Timecode, Error> fromString(const String &str);
148
150 Timecode() = default;
153 Timecode(const Mode &md) : _mode(md) {}
160 _mode(Mode(0u, 0u)), _hour(h), _min(m), _sec(s), _frame(f) {}
168 _mode(md), _hour(h), _min(m), _sec(s), _frame(f) {}
171 Timecode(const String &str) {
172 auto [tc, err] = fromString(str);
173 if(err.isOk()) *this = tc;
174 }
175
176 bool operator==(const Timecode &other) const {
177 return _mode == other._mode &&
178 _hour == other._hour &&
179 _min == other._min &&
180 _sec == other._sec &&
181 _frame == other._frame;
182 }
183
184 bool operator!=(const Timecode &other) const {
185 return !(*this == other);
186 }
187
188 bool operator>(const Timecode &other) const {
189 return toFrameNumber() > other.toFrameNumber();
190 }
191
192 bool operator<(const Timecode &other) const {
193 return toFrameNumber() < other.toFrameNumber();
194 }
195
196 bool operator>=(const Timecode &other) const {
197 return toFrameNumber() >= other.toFrameNumber();
198 }
199
200 bool operator<=(const Timecode &other) const {
201 return toFrameNumber() <= other.toFrameNumber();
202 }
203
205 bool isValid() const { return _mode.isValid(); }
207 bool isDropFrame() const { return _mode.isDropFrame(); }
209 bool isFirstField() const { return _flags & FirstField; }
211 uint32_t fps() const { return _mode.fps(); }
213 Mode mode() const { return _mode; }
214
219 Timecode ret = *this;
220 ++(*this);
221 return ret;
222 }
227 Timecode ret = *this;
228 --(*this);
229 return ret;
230 }
231
238 _hour = h;
239 _min = m;
240 _sec = s;
241 _frame = f;
242 }
243
245 DigitType hour() const { return _hour; }
247 DigitType min() const { return _min; }
249 DigitType sec() const { return _sec; }
251 DigitType frame() const { return _frame; }
252
254 const VtcFormat *vtcFormat() const { return _mode.vtcFormat(); }
255
257 operator String() const { return toString().first; }
263 std::pair<String, Error> toString(const VtcStringFormat *fmt = &VTC_STR_FMT_SMPTE) const;
268 std::pair<FrameNumber, Error> toFrameNumber() const;
269
270 private:
271 VtcTimecode toVtc() const;
272 void fromVtc(const VtcTimecode &vtc);
273
274 Mode _mode;
275 FlagsType _flags = 0;
276 DigitType _hour = 0;
277 DigitType _min = 0;
278 DigitType _sec = 0;
279 DigitType _frame = 0;
280};
281
282
Dynamic array container wrapping std::vector.
Definition list.h:40
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
Describes the timecode mode (frame rate and drop-frame status).
Definition timecode.h:70
Mode(TimecodeType type)
Constructs a mode from a standard TimecodeType.
Definition timecode.h:94
Mode()=default
Constructs an invalid, default mode.
bool hasFormat() const
Returns true if a VtcFormat pointer is assigned.
Definition timecode.h:124
bool isValid() const
Returns true if this mode has been explicitly set.
Definition timecode.h:120
Mode(const VtcFormat *format)
Constructs a mode from a libvtc format pointer.
Definition timecode.h:77
const VtcFormat * vtcFormat() const
Returns the underlying libvtc format pointer.
Definition timecode.h:127
Mode(uint32_t fps, uint32_t flags)
Constructs a mode from a frame rate and flag set.
Definition timecode.h:84
uint32_t fps() const
Returns the frames-per-second rate, or 0 if no format is set.
Definition timecode.h:118
bool isDropFrame() const
Returns true if this mode uses drop-frame counting.
Definition timecode.h:122
Class for holding and manipulating timecode.
Definition timecode.h:45
Timecode(DigitType h, DigitType m, DigitType s, DigitType f)
Constructs a timecode with explicit digit values and no specific mode.
Definition timecode.h:159
static std::pair< Timecode, Error > fromString(const String &str)
Parses a Timecode from its string representation.
Timecode & operator--()
Pre-decrement: moves the timecode back by one frame.
Flags
Timecode flag bits.
Definition timecode.h:60
@ FirstField
Indicates the first field of an interlaced frame.
Definition timecode.h:62
@ DropFrame
Indicates drop-frame timecode.
Definition timecode.h:61
Timecode operator++(int)
Post-increment: advances the timecode by one frame, returning the previous value.
Definition timecode.h:218
Mode mode() const
Returns the timecode mode.
Definition timecode.h:213
DigitType sec() const
Returns the second digit.
Definition timecode.h:249
Timecode(const String &str)
Constructs a timecode by parsing a string representation.
Definition timecode.h:171
static Timecode fromFrameNumber(const Mode &mode, FrameNumber frameNumber)
Constructs a Timecode from a mode and absolute frame number.
TimecodeType
Standard timecode types.
Definition timecode.h:52
@ NDF25
25 fps non-drop-frame (maps to VTC_FORMAT_25)
Definition timecode.h:54
@ DF30
29.97 fps drop-frame (maps to VTC_FORMAT_29_97_DF)
Definition timecode.h:56
@ NDF24
24 fps non-drop-frame (maps to VTC_FORMAT_24)
Definition timecode.h:53
@ NDF30
30 fps non-drop-frame (maps to VTC_FORMAT_30_NDF)
Definition timecode.h:55
Timecode & operator++()
Pre-increment: advances the timecode by one frame.
Timecode(const Mode &md)
Constructs a timecode with the given mode and zeroed digits.
Definition timecode.h:153
DigitType frame() const
Returns the frame digit.
Definition timecode.h:251
const VtcFormat * vtcFormat() const
Returns the underlying libvtc format pointer from the mode.
Definition timecode.h:254
void set(DigitType h, DigitType m, DigitType s, DigitType f)
Sets the timecode digit fields directly.
Definition timecode.h:237
Timecode operator--(int)
Post-decrement: moves the timecode back by one frame, returning the previous value.
Definition timecode.h:226
DigitType hour() const
Returns the hour digit.
Definition timecode.h:245
bool isFirstField() const
Returns true if this timecode represents the first field of an interlaced frame.
Definition timecode.h:209
std::pair< String, Error > toString(const VtcStringFormat *fmt=&VTC_STR_FMT_SMPTE) const
Converts the timecode to a string.
bool isValid() const
Returns true if the timecode mode is valid.
Definition timecode.h:205
Timecode(const Mode &md, DigitType h, DigitType m, DigitType s, DigitType f)
Constructs a timecode with a mode and explicit digit values.
Definition timecode.h:167
uint32_t fps() const
Returns the frames-per-second rate.
Definition timecode.h:211
bool isDropFrame() const
Returns true if the timecode uses drop-frame counting.
Definition timecode.h:207
DigitType min() const
Returns the minute digit.
Definition timecode.h:247
std::pair< FrameNumber, Error > toFrameNumber() const
Converts the timecode to an absolute frame number.
Timecode()=default
Constructs a default (invalid) timecode.
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19