place timecode conversion functions into a Timecode class rather than an olive namespace
Code cleanup and improvement.
This commit is contained in:
@@ -28,10 +28,10 @@ QString padded(int64_t arg, int padding) {
|
||||
return QStringLiteral("%1").arg(arg, padding, 10, QChar('0'));
|
||||
}
|
||||
|
||||
QString olive::timestamp_to_timecode(const int64_t ×tamp,
|
||||
const rational& timebase,
|
||||
const TimecodeDisplay& display,
|
||||
bool show_plus_if_positive)
|
||||
QString Timecode::timestamp_to_timecode(const int64_t ×tamp,
|
||||
const rational& timebase,
|
||||
const Display& display,
|
||||
bool show_plus_if_positive)
|
||||
{
|
||||
if (timebase.isNull()) {
|
||||
return QString();
|
||||
@@ -75,7 +75,7 @@ QString olive::timestamp_to_timecode(const int64_t ×tamp,
|
||||
int64_t frames, secs, mins, hours;
|
||||
int64_t f = qAbs(timestamp);
|
||||
|
||||
if (display == kTimecodeDropFrame && timebase.numerator() == 1001) {
|
||||
if (display == kTimecodeDropFrame && TimebaseIsDropFrame(timebase)) {
|
||||
frame_token = ";";
|
||||
|
||||
/**
|
||||
@@ -129,7 +129,7 @@ QString olive::timestamp_to_timecode(const int64_t ×tamp,
|
||||
return QString();
|
||||
}
|
||||
|
||||
int64_t olive::timecode_to_timestamp(const QString &timecode, const rational &timebase, const olive::TimecodeDisplay &display, bool* ok)
|
||||
int64_t Timecode::timecode_to_timestamp(const QString &timecode, const rational &timebase, const Display &display, bool* ok)
|
||||
{
|
||||
double timebase_dbl = timebase.toDouble();
|
||||
|
||||
@@ -182,7 +182,7 @@ int64_t olive::timecode_to_timestamp(const QString &timecode, const rational &ti
|
||||
int64_t sec_count = (hours*3600 + mins*60 + secs);
|
||||
int64_t timestamp = sec_count*rounded_frame_rate + frames;
|
||||
|
||||
if (display == kTimecodeDropFrame && timebase.numerator() == 1001) {
|
||||
if (display == kTimecodeDropFrame && TimebaseIsDropFrame(timebase)) {
|
||||
|
||||
// Number of frames to drop on the minute marks is the nearest integer to 6% of the framerate
|
||||
int64_t dropFrames = qRound64(frame_rate * (2.0/30.0));
|
||||
@@ -231,22 +231,27 @@ err_fatal:
|
||||
return 0;
|
||||
}
|
||||
|
||||
rational olive::timestamp_to_time(const int64_t ×tamp, const rational &timebase)
|
||||
rational Timecode::timestamp_to_time(const int64_t ×tamp, const rational &timebase)
|
||||
{
|
||||
return rational(timestamp) * timebase;
|
||||
}
|
||||
|
||||
int64_t olive::time_to_timestamp(const rational &time, const rational &timebase)
|
||||
bool Timecode::TimebaseIsDropFrame(const rational &timebase)
|
||||
{
|
||||
return (timebase.numerator() == 1001);
|
||||
}
|
||||
|
||||
int64_t Timecode::time_to_timestamp(const rational &time, const rational &timebase)
|
||||
{
|
||||
return time_to_timestamp(time.toDouble(), timebase);
|
||||
}
|
||||
|
||||
int64_t olive::time_to_timestamp(const double &time, const rational &timebase)
|
||||
int64_t Timecode::time_to_timestamp(const double &time, const rational &timebase)
|
||||
{
|
||||
return qRound64(time * timebase.flipped().toDouble());
|
||||
}
|
||||
|
||||
olive::TimecodeDisplay olive::CurrentTimecodeDisplay()
|
||||
Timecode::Display Timecode::CurrentDisplay()
|
||||
{
|
||||
return static_cast<olive::TimecodeDisplay>(Config::Current()["TimecodeDisplay"].toInt());
|
||||
return static_cast<Timecode::Display>(Config::Current()["TimecodeDisplay"].toInt());
|
||||
}
|
||||
|
||||
@@ -25,30 +25,42 @@
|
||||
|
||||
#include "common/rational.h"
|
||||
|
||||
namespace olive {
|
||||
/**
|
||||
* @brief Functions for converting times/timecodes/timestamps
|
||||
*
|
||||
* Olive uses the following terminology through its code:
|
||||
*
|
||||
* `time` - time in seconds presented in a rational form
|
||||
* `timebase` - the base time unit of an audio/video stream in seconds
|
||||
* `timestamp` - an integer representation of a time in timebase units (in many cases is used like a frame number)
|
||||
* `timecode` a user-friendly string representation of a time according to Timecode::Display
|
||||
*/
|
||||
class Timecode {
|
||||
public:
|
||||
enum Display {
|
||||
kTimecodeDropFrame,
|
||||
kTimecodeNonDropFrame,
|
||||
kTimecodeSeconds,
|
||||
kFrames,
|
||||
kMilliseconds
|
||||
};
|
||||
|
||||
static Display CurrentDisplay();
|
||||
|
||||
/**
|
||||
* @brief Convert a timestamp (according to a rational timebase) to a user-friendly string representation
|
||||
*/
|
||||
static QString timestamp_to_timecode(const int64_t ×tamp, const rational& timebase, const Display &display, bool show_plus_if_positive = false);
|
||||
|
||||
static int64_t timecode_to_timestamp(const QString& timecode, const rational& timebase, const Display& display, bool *ok = nullptr);
|
||||
|
||||
static int64_t time_to_timestamp(const rational& time, const rational& timebase);
|
||||
static int64_t time_to_timestamp(const double& time, const rational& timebase);
|
||||
|
||||
static rational timestamp_to_time(const int64_t& timestamp, const rational& timebase);
|
||||
|
||||
static bool TimebaseIsDropFrame(const rational& timebase);
|
||||
|
||||
enum TimecodeDisplay {
|
||||
kTimecodeDropFrame,
|
||||
kTimecodeNonDropFrame,
|
||||
kTimecodeSeconds,
|
||||
kFrames,
|
||||
kMilliseconds
|
||||
};
|
||||
|
||||
TimecodeDisplay CurrentTimecodeDisplay();
|
||||
|
||||
/**
|
||||
* @brief Convert a timestamp (according to a rational timebase) to a user-friendly string representation
|
||||
*/
|
||||
QString timestamp_to_timecode(const int64_t ×tamp, const rational& timebase, const TimecodeDisplay& display, bool show_plus_if_positive = false);
|
||||
|
||||
int64_t timecode_to_timestamp(const QString& timecode, const rational& timebase, const TimecodeDisplay& display, bool *ok = nullptr);
|
||||
|
||||
int64_t time_to_timestamp(const rational& time, const rational& timebase);
|
||||
int64_t time_to_timestamp(const double& time, const rational& timebase);
|
||||
|
||||
rational timestamp_to_time(const int64_t& timestamp, const rational& timebase);
|
||||
|
||||
}
|
||||
|
||||
#endif // TIMECODEFUNCTIONS_H
|
||||
|
||||
Reference in New Issue
Block a user