diff --git a/app/codec/ffmpeg/ffmpegencoder.cpp b/app/codec/ffmpeg/ffmpegencoder.cpp index c1b54c5aa..a2926f15d 100644 --- a/app/codec/ffmpeg/ffmpegencoder.cpp +++ b/app/codec/ffmpeg/ffmpegencoder.cpp @@ -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 out_buf(1024 * 1024); diff --git a/app/common/timecodefunctions.cpp b/app/common/timecodefunctions.cpp index 0501d5bb4..a90b9e023 100644 --- a/app/common/timecodefunctions.cpp +++ b/app/common/timecodefunctions.cpp @@ -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); } } diff --git a/app/common/timecodefunctions.h b/app/common/timecodefunctions.h index dea645b1c..7252f44db 100644 --- a/app/common/timecodefunctions.h +++ b/app/common/timecodefunctions.h @@ -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); diff --git a/app/render/framehashcache.cpp b/app/render/framehashcache.cpp index e78578488..21b087086 100644 --- a/app/render/framehashcache.cpp +++ b/app/render/framehashcache.cpp @@ -151,7 +151,7 @@ QVector FrameHashCache::GetFrameListFromTimeRange(TimeRangeList range_ QVector 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);