nodes: rewrote and simplified length signaling system
This commit is contained in:
@@ -166,14 +166,13 @@ void AudioPlaybackCache::WriteSilence(const TimeRange &range)
|
||||
|
||||
void AudioPlaybackCache::ShiftEvent(const rational &from_in_time, const rational &to_in_time)
|
||||
{
|
||||
if (from_in_time == to_in_time || from_in_time >= GetLength()) {
|
||||
// Nothing to be done
|
||||
return;
|
||||
}
|
||||
|
||||
qint64 to = params_.time_to_bytes_per_channel(to_in_time);
|
||||
qint64 from = params_.time_to_bytes_per_channel(from_in_time);
|
||||
|
||||
if (from >= playlist_.GetLength()) {
|
||||
return;
|
||||
}
|
||||
|
||||
int to_seg_index = playlist_.GetIndexOfPosition(to);
|
||||
int from_seg_index = playlist_.GetIndexOfPosition(from);
|
||||
|
||||
@@ -265,31 +264,6 @@ void AudioPlaybackCache::ShiftEvent(const rational &from_in_time, const rational
|
||||
}
|
||||
}
|
||||
|
||||
void AudioPlaybackCache::LengthChangedEvent(const rational& old, const rational& newlen)
|
||||
{
|
||||
Q_UNUSED(old)
|
||||
|
||||
if (!params_.is_valid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
qint64 new_len_in_bytes = params_.time_to_bytes_per_channel(newlen);
|
||||
|
||||
while (new_len_in_bytes < playlist_.GetLength()) {
|
||||
Segment& last_seg = playlist_.back();
|
||||
|
||||
if (playlist_.GetLength() - last_seg.size() < new_len_in_bytes) {
|
||||
// Truncate this segment rather than removing it
|
||||
qint64 diff = playlist_.GetLength() - new_len_in_bytes;
|
||||
|
||||
TrimSegmentOut(&last_seg, last_seg.size() - diff);
|
||||
} else {
|
||||
// Remove last segment
|
||||
RemoveSegmentFromArray(playlist_.size() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AudioPlaybackCache::Segment AudioPlaybackCache::CloneSegment(const AudioPlaybackCache::Segment &s) const
|
||||
{
|
||||
Segment new_seg = s;
|
||||
|
||||
@@ -202,8 +202,6 @@ signals:
|
||||
protected:
|
||||
virtual void ShiftEvent(const rational& from, const rational& to) override;
|
||||
|
||||
virtual void LengthChangedEvent(const rational& old, const rational& newlen) override;
|
||||
|
||||
private:
|
||||
static const qint64 kDefaultSegmentSizePerChannel;
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ void FrameHashCache::SetTimebase(const rational &tb)
|
||||
|
||||
void FrameHashCache::ValidateFramesWithHash(const QByteArray &hash)
|
||||
{
|
||||
const TimeRangeList& invalidated_ranges = GetInvalidatedRanges();
|
||||
auto invalidated_ranges = GetInvalidatedRanges(ToTime(GetMapSize()));
|
||||
|
||||
for (int64_t i=0; i<GetMapSize(); i++) {
|
||||
if (time_hash_map_[i] == hash) {
|
||||
@@ -241,17 +241,6 @@ FramePtr FrameHashCache::LoadCacheFrame(const QString &fn)
|
||||
return frame;
|
||||
}
|
||||
|
||||
void FrameHashCache::LengthChangedEvent(const rational &old, const rational &newlen)
|
||||
{
|
||||
if (newlen < old) {
|
||||
// Determine length in frames by ceil-ing the time
|
||||
int64_t new_ts_length = ToTimestamp(newlen, Timecode::kCeil);
|
||||
|
||||
// Resize vector to this length, which will discard all frames after it
|
||||
time_hash_map_.resize(new_ts_length);
|
||||
}
|
||||
}
|
||||
|
||||
struct HashTimePair {
|
||||
rational time;
|
||||
QByteArray hash;
|
||||
|
||||
@@ -50,8 +50,6 @@ public:
|
||||
|
||||
void ValidateFramesWithHash(const QByteArray& hash);
|
||||
|
||||
QMap<rational, QByteArray> time_hash_map();
|
||||
|
||||
/**
|
||||
* @brief Return the path of the cached image at this time
|
||||
*/
|
||||
@@ -70,8 +68,6 @@ public:
|
||||
void SetHash(const olive::rational &time, const QByteArray& hash, bool frame_exists);
|
||||
|
||||
protected:
|
||||
virtual void LengthChangedEvent(const rational& old, const rational& newlen) override;
|
||||
|
||||
virtual void ShiftEvent(const rational& from, const rational& to) override;
|
||||
|
||||
virtual void InvalidateEvent(const TimeRange& range) override;
|
||||
|
||||
@@ -27,58 +27,25 @@
|
||||
|
||||
namespace olive {
|
||||
|
||||
void PlaybackCache::Invalidate(const TimeRange &r)
|
||||
void PlaybackCache::Invalidate(const TimeRange &r, bool signal)
|
||||
{
|
||||
if (r.in() == r.out()) {
|
||||
qWarning() << "Tried to invalidate zero-length range";
|
||||
return;
|
||||
}
|
||||
|
||||
invalidated_.insert(r);
|
||||
validated_.remove(r);
|
||||
|
||||
InvalidateEvent(r);
|
||||
|
||||
emit Invalidated(r);
|
||||
if (signal) {
|
||||
emit Invalidated(r);
|
||||
}
|
||||
}
|
||||
|
||||
void PlaybackCache::InvalidateAll()
|
||||
{
|
||||
if (length_.isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Invalidate(TimeRange(0, length_));
|
||||
}
|
||||
|
||||
void PlaybackCache::SetLength(const rational &r)
|
||||
{
|
||||
if (length_ == r) {
|
||||
// Same length - do nothing
|
||||
return;
|
||||
}
|
||||
|
||||
LengthChangedEvent(length_, r);
|
||||
|
||||
TimeRange range_diff(length_, r);
|
||||
|
||||
if (r.isNull()) {
|
||||
invalidated_.clear();
|
||||
} else if (r > length_) {
|
||||
// If new length is greater, simply extend the invalidated range for now
|
||||
invalidated_.insert(range_diff);
|
||||
} else {
|
||||
// If new length is smaller, removed hashes
|
||||
invalidated_.remove(range_diff);
|
||||
}
|
||||
|
||||
rational old_length = length_;
|
||||
length_ = r;
|
||||
|
||||
if (r > old_length) {
|
||||
emit Invalidated(range_diff);
|
||||
} else {
|
||||
emit Validated(range_diff);
|
||||
}
|
||||
Invalidate(TimeRange(0, RATIONAL_MAX));
|
||||
}
|
||||
|
||||
void PlaybackCache::Shift(rational from, rational to)
|
||||
@@ -87,54 +54,33 @@ void PlaybackCache::Shift(rational from, rational to)
|
||||
return;
|
||||
}
|
||||
|
||||
if (from > length_) {
|
||||
if (to > from) {
|
||||
// No-op
|
||||
return;
|
||||
} else if (to >= length_) {
|
||||
// No-op
|
||||
return;
|
||||
} else {
|
||||
from = length_;
|
||||
}
|
||||
}
|
||||
|
||||
// An region between `from` and `to` will be inserted or spliced out
|
||||
TimeRangeList ranges_to_shift = invalidated_.Intersects(TimeRange(from, RATIONAL_MAX));
|
||||
TimeRangeList ranges_to_shift = validated_.Intersects(TimeRange(from, RATIONAL_MAX));
|
||||
|
||||
// Remove everything from the minimum point
|
||||
TimeRange remove_range = TimeRange(qMin(from, to), RATIONAL_MAX);
|
||||
Validate(remove_range);
|
||||
Invalidate(remove_range, false);
|
||||
|
||||
// Shift invalidated ranges
|
||||
// (`diff` is POSITIVE when moving forward -> and NEGATIVE when moving backward <-)
|
||||
rational diff = to - from;
|
||||
foreach (const TimeRange& r, ranges_to_shift) {
|
||||
Invalidate(r + diff);
|
||||
Validate(r + diff, false);
|
||||
}
|
||||
|
||||
ShiftEvent(from, to);
|
||||
|
||||
length_ += diff;
|
||||
|
||||
if (diff > 0) {
|
||||
// If shifting forward, add this section to the invalidated region
|
||||
Invalidate(TimeRange(from, to));
|
||||
}
|
||||
|
||||
// Emit signals
|
||||
emit Shifted(from, to);
|
||||
}
|
||||
|
||||
void PlaybackCache::Validate(const TimeRange &r)
|
||||
void PlaybackCache::Validate(const TimeRange &r, bool signal)
|
||||
{
|
||||
invalidated_.remove(r);
|
||||
validated_.insert(r);
|
||||
|
||||
emit Validated(r);
|
||||
}
|
||||
|
||||
void PlaybackCache::LengthChangedEvent(const rational &, const rational &)
|
||||
{
|
||||
if (signal) {
|
||||
emit Validated(r);
|
||||
}
|
||||
}
|
||||
|
||||
void PlaybackCache::InvalidateEvent(const TimeRange &)
|
||||
@@ -156,6 +102,29 @@ Project *PlaybackCache::GetProject() const
|
||||
return viewer->project();
|
||||
}
|
||||
|
||||
TimeRangeList PlaybackCache::GetInvalidatedRanges(TimeRange intersecting)
|
||||
{
|
||||
TimeRangeList invalidated;
|
||||
|
||||
// Prevent TimeRange from being below 0, some other behavior in Olive relies on this behavior
|
||||
// and it seemed reasonable to have safety code in here
|
||||
intersecting.set_out(qMax(rational(0), intersecting.out()));
|
||||
intersecting.set_in(qMax(rational(0), intersecting.in()));
|
||||
|
||||
invalidated.insert(intersecting);
|
||||
|
||||
foreach (const TimeRange &range, validated_) {
|
||||
invalidated.remove(range);
|
||||
}
|
||||
|
||||
return invalidated;
|
||||
}
|
||||
|
||||
bool PlaybackCache::HasInvalidatedRanges(const TimeRange &intersecting)
|
||||
{
|
||||
return !validated_.contains(intersecting);
|
||||
}
|
||||
|
||||
QString PlaybackCache::GetCacheDirectory() const
|
||||
{
|
||||
Project* project = GetProject();
|
||||
@@ -167,4 +136,9 @@ QString PlaybackCache::GetCacheDirectory() const
|
||||
}
|
||||
}
|
||||
|
||||
ViewerOutput *PlaybackCache::viewer_parent() const
|
||||
{
|
||||
return dynamic_cast<ViewerOutput*>(parent());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+14
-28
@@ -30,46 +30,38 @@
|
||||
namespace olive {
|
||||
|
||||
class Project;
|
||||
class ViewerOutput;
|
||||
|
||||
class PlaybackCache : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
PlaybackCache(QObject* parent = nullptr) :
|
||||
QObject(parent),
|
||||
length_(0)
|
||||
QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
const rational& GetLength()
|
||||
TimeRangeList GetInvalidatedRanges(TimeRange intersecting);
|
||||
TimeRangeList GetInvalidatedRanges(const rational &length)
|
||||
{
|
||||
return length_;
|
||||
return GetInvalidatedRanges(TimeRange(0, length));
|
||||
}
|
||||
|
||||
bool IsFullyValidated()
|
||||
bool HasInvalidatedRanges(const TimeRange &intersecting);
|
||||
bool HasInvalidatedRanges(const rational &length)
|
||||
{
|
||||
return invalidated_.isEmpty();
|
||||
}
|
||||
|
||||
const TimeRangeList& GetInvalidatedRanges()
|
||||
{
|
||||
return invalidated_;
|
||||
}
|
||||
|
||||
bool HasInvalidatedRanges()
|
||||
{
|
||||
return !invalidated_.isEmpty();
|
||||
return HasInvalidatedRanges(TimeRange(0, length));
|
||||
}
|
||||
|
||||
QString GetCacheDirectory() const;
|
||||
|
||||
ViewerOutput *viewer_parent() const;
|
||||
|
||||
void Invalidate(const TimeRange& r, bool signal = true);
|
||||
|
||||
public slots:
|
||||
void Invalidate(const TimeRange& r);
|
||||
|
||||
void InvalidateAll();
|
||||
|
||||
void SetLength(const rational& r);
|
||||
|
||||
void Shift(rational from, rational to);
|
||||
|
||||
signals:
|
||||
@@ -79,12 +71,8 @@ signals:
|
||||
|
||||
void Shifted(const olive::rational& from, const olive::rational& to);
|
||||
|
||||
void LengthChanged(const olive::rational& r);
|
||||
|
||||
protected:
|
||||
void Validate(const TimeRange& r);
|
||||
|
||||
virtual void LengthChangedEvent(const rational& old, const rational& newlen);
|
||||
void Validate(const TimeRange& r, bool signal = true);
|
||||
|
||||
virtual void InvalidateEvent(const TimeRange& range);
|
||||
|
||||
@@ -93,9 +81,7 @@ protected:
|
||||
Project* GetProject() const;
|
||||
|
||||
private:
|
||||
TimeRangeList invalidated_;
|
||||
|
||||
rational length_;
|
||||
TimeRangeList validated_;
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user