timecode: allow specific rounding routines
This commit is contained in:
@@ -396,7 +396,7 @@ bool FFmpegEncoder::WriteSubtitle(const SubtitleBlock *sub_block)
|
||||
subtitle.num_rects = 1;
|
||||
subtitle.rects = &rect_array;
|
||||
|
||||
subtitle.pts = Timecode::time_to_timestamp(sub_block->in(), av_get_time_base_q(), true);
|
||||
subtitle.pts = Timecode::time_to_timestamp(sub_block->in(), av_get_time_base_q(), Timecode::kFloor);
|
||||
subtitle.end_display_time = qRound64(sub_block->length().toDouble() * 1000);
|
||||
|
||||
QVector<uint8_t> out_buf(1024 * 1024);
|
||||
|
||||
@@ -239,7 +239,7 @@ rational Timecode::timecode_to_time(const QString &timecode, const rational &tim
|
||||
return timestamp_to_time(timestamp, timebase);
|
||||
}
|
||||
|
||||
rational Timecode::snap_time_to_timebase(const rational &time, const rational &timebase, bool floor)
|
||||
rational Timecode::snap_time_to_timebase(const rational &time, const rational &timebase, Rounding floor)
|
||||
{
|
||||
// Just convert to a timestamp in timebase units and back
|
||||
int64_t timestamp = time_to_timestamp(time, timebase, floor);
|
||||
@@ -275,19 +275,23 @@ QString Timecode::TimeToString(int64_t ms)
|
||||
.arg(ss, 2, 10, QChar('0'));
|
||||
}
|
||||
|
||||
int64_t Timecode::time_to_timestamp(const rational &time, const rational &timebase, bool floor)
|
||||
int64_t Timecode::time_to_timestamp(const rational &time, const rational &timebase, Rounding floor)
|
||||
{
|
||||
return time_to_timestamp(time.toDouble(), timebase, floor);
|
||||
}
|
||||
|
||||
int64_t Timecode::time_to_timestamp(const double &time, const rational &timebase, bool floor)
|
||||
int64_t Timecode::time_to_timestamp(const double &time, const rational &timebase, Rounding floor)
|
||||
{
|
||||
double d = time * timebase.flipped().toDouble();
|
||||
|
||||
if (floor) {
|
||||
return qFloor(d);
|
||||
} else {
|
||||
switch (floor) {
|
||||
case kRound:
|
||||
default:
|
||||
return qRound64(d);
|
||||
case kFloor:
|
||||
return qFloor(d);
|
||||
case kCeil:
|
||||
return qCeil(d);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -47,6 +47,12 @@ public:
|
||||
kMilliseconds
|
||||
};
|
||||
|
||||
enum Rounding {
|
||||
kCeil,
|
||||
kFloor,
|
||||
kRound
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Convert a timestamp (according to a rational timebase) to a user-friendly string representation
|
||||
*/
|
||||
@@ -55,10 +61,10 @@ public:
|
||||
static int64_t timecode_to_timestamp(const QString& timecode, const rational& timebase, const Display& display, bool *ok = nullptr);
|
||||
static rational timecode_to_time(const QString& timecode, const rational& timebase, const Display& display, bool *ok = nullptr);
|
||||
|
||||
static rational snap_time_to_timebase(const rational& time, const rational& timebase, bool floor = false);
|
||||
static rational snap_time_to_timebase(const rational& time, const rational& timebase, Rounding floor = kRound);
|
||||
|
||||
static int64_t time_to_timestamp(const rational& time, const rational& timebase, bool floor = false);
|
||||
static int64_t time_to_timestamp(const double& time, const rational& timebase, bool floor = false);
|
||||
static int64_t time_to_timestamp(const rational& time, const rational& timebase, Rounding floor = kRound);
|
||||
static int64_t time_to_timestamp(const double& time, const rational& timebase, Rounding floor = kRound);
|
||||
|
||||
static int64_t rescale_timestamp(const int64_t& ts, const rational& source, const rational& dest);
|
||||
static int64_t rescale_timestamp_ceil(const int64_t& ts, const rational& source, const rational& dest);
|
||||
|
||||
@@ -151,7 +151,7 @@ QVector<rational> FrameHashCache::GetFrameListFromTimeRange(TimeRangeList range_
|
||||
QVector<rational> times;
|
||||
|
||||
foreach (const TimeRange &range, range_list) {
|
||||
rational frame = Timecode::snap_time_to_timebase(range.in(), timebase, true);
|
||||
rational frame = Timecode::snap_time_to_timebase(range.in(), timebase, Timecode::kCeil);
|
||||
|
||||
while (frame < range.out()) {
|
||||
times.append(frame);
|
||||
|
||||
Reference in New Issue
Block a user