Merge branch 'cache-update'

This commit is contained in:
itsmattkc
2022-09-15 16:10:25 -07:00
77 changed files with 2334 additions and 1597 deletions
+2
View File
@@ -24,6 +24,8 @@ set(OLIVE_SOURCES
render/audioparams.h
render/audioplaybackcache.cpp
render/audioplaybackcache.h
render/audiowaveformcache.cpp
render/audiowaveformcache.h
render/cancelatom.h
render/color.cpp
render/color.h
+8 -1
View File
@@ -106,7 +106,14 @@ qint64 AudioParams::samples_to_bytes(const qint64 &samples) const
{
Q_ASSERT(is_valid());
return samples * channel_count() * bytes_per_sample_per_channel();
return samples_to_bytes_per_channel(samples) * channel_count();
}
qint64 AudioParams::samples_to_bytes_per_channel(const qint64 &samples) const
{
Q_ASSERT(is_valid());
return samples * bytes_per_sample_per_channel();
}
rational AudioParams::samples_to_time(const qint64 &samples) const
+1
View File
@@ -213,6 +213,7 @@ public:
qint64 time_to_samples(const double& time) const;
qint64 time_to_samples(const rational& time) const;
qint64 samples_to_bytes(const qint64& samples) const;
qint64 samples_to_bytes_per_channel(const qint64& samples) const;
rational samples_to_time(const qint64& samples) const;
qint64 bytes_to_samples(const qint64 &bytes) const;
rational bytes_to_time(const qint64 &bytes) const;
+42 -387
View File
@@ -39,8 +39,6 @@ AudioPlaybackCache::AudioPlaybackCache(QObject* parent) :
AudioPlaybackCache::~AudioPlaybackCache()
{
// Segments are volatile, so delete them here
ClearPlaylist();
}
void AudioPlaybackCache::SetParameters(const AudioParams &params)
@@ -50,115 +48,13 @@ void AudioPlaybackCache::SetParameters(const AudioParams &params)
}
params_ = params;
visual_.set_channel_count(params_.channel_count());
// Restart empty file so there's always "something" to play
ClearPlaylist();
emit ParametersChanged();
}
void AudioPlaybackCache::WritePCM(const TimeRange &range, const TimeRangeList &valid_ranges, const SampleBuffer &samples)
{
// Ensure if we have enough segments to write this data, creating more if not
qint64 length_diff = params_.time_to_bytes_per_channel(range.out()) - playlist_.GetLength();
while (length_diff > 0) {
qint64 seg_sz = qMin(kDefaultSegmentSizePerChannel, length_diff);
playlist_.push_back(CreateSegment(seg_sz, playlist_.GetLength()));
length_diff -= seg_sz;
}
// Keep track of validated ranges so we can signal them all at once at the end
TimeRangeList ranges_we_validated;
// Calculate buffer size per channel
qint64 buffer_size_per_channel = samples.sample_count() * params_.bytes_per_sample_per_channel();
// Write each valid range to the segments
foreach (const TimeRange& r, valid_ranges) {
rational this_segment_in = 0;
// Write PCM to playlist
for (auto it=playlist_.begin(); it!=playlist_.end(); it++) {
rational this_segment_out = this_segment_in + params_.bytes_per_channel_to_time((*it).size());
if (r.in() < this_segment_out) {
// We'll write at least something to this segment
bool succeeded = true;
// Calculate how much to write
rational this_write_in_point = qMax(r.in(), this_segment_in);
rational this_write_out_point = qMin(r.out(), this_segment_out);
for (int i=0; i<(*it).channels(); i++) {
QFile seg_file((*it).filename(i));
if (seg_file.open(QFile::ReadWrite)) {
// Calculate what the byte offsets are going to be in this segment file
rational in_point_relative = this_write_in_point - this_segment_in;
qint64 dst_offset = params_.time_to_bytes_per_channel(in_point_relative);
// Calculate where to retrieve data from in the source buffer
qint64 src_offset = params_.time_to_bytes_per_channel(this_write_in_point - range.in());
// Determine how many bytes need to be written
qint64 total_write_length = params_.time_to_bytes_per_channel(this_write_out_point - this_write_in_point);
// Determine how many bytes we actually have in the source buffer
qint64 possible_write_length = qMin(qMax(qint64(0), buffer_size_per_channel - src_offset), total_write_length);
// Seek to our start offset
seg_file.seek(dst_offset);
// If we have source bytes to write, write them here
if (possible_write_length > 0) {
// Assume `samples` is valid if we're here, or else `buffer_size_per_channel` and
// therefore `possible_write_length` will be 0.
seg_file.write(reinterpret_cast<const char*>(samples.data(i)) + src_offset, possible_write_length);
}
if (possible_write_length < total_write_length) {
// Fill remaining space with silence
QByteArray s(total_write_length - possible_write_length, 0x00);
seg_file.write(s);
}
seg_file.close();
} else {
qWarning() << "Failed to write PCM data to" << seg_file.fileName();
succeeded = false;
}
}
if (succeeded) {
ranges_we_validated.insert(TimeRange(this_write_in_point, this_write_out_point));
}
}
if (r.out() <= this_segment_out) {
// We've reached the end of this range, we can break out of the loop here
break;
}
// Each segment is contiguous, so this out will be the next segment's in
this_segment_in = this_segment_out;
}
}
foreach (const TimeRange& v, ranges_we_validated) {
Validate(v);
}
}
void AudioPlaybackCache::WriteWaveform(const TimeRange &range, const TimeRangeList &valid_ranges, const AudioVisualWaveform *waveform)
{
// Write each valid range to the segments
foreach (const TimeRange& r, valid_ranges) {
// Write visual
if (waveform) {
visual_.OverwriteSums(*waveform, r.in(), r.in() - range.in(), r.length());
} else {
visual_.OverwriteSilence(r.in(), r.length());
for (const TimeRange &r : valid_ranges) {
if (WritePartOfSampleBuffer(samples, r.in(), r.in() - range.in(), r.length())) {
Validate(r);
}
}
}
@@ -170,311 +66,70 @@ void AudioPlaybackCache::WriteSilence(const TimeRange &range)
WritePCM(range, {range}, SampleBuffer());
}
AudioPlaybackCache::Segment AudioPlaybackCache::CloneSegment(const AudioPlaybackCache::Segment &s) const
bool AudioPlaybackCache::WritePartOfSampleBuffer(const SampleBuffer &samples, const rational &write_start, const rational &buffer_start, const rational &length)
{
Segment new_seg = s;
qint64 length_in_bytes = params_.time_to_bytes_per_channel(length);
new_seg.set_channels(s.channels());
qint64 start_cache_offset = params_.time_to_bytes_per_channel(write_start);
qint64 end_cache_offset = start_cache_offset + length_in_bytes;
// Copy data to a new file
for (int i=0; i<s.channels(); i++) {
QString new_filename = GenerateSegmentFilename();
QFile::copy(s.filename(i), new_filename);
qint64 start_buffer_offset = params_.time_to_bytes_per_channel(buffer_start);
qint64 end_buffer_offset = std::min(start_buffer_offset + length_in_bytes, params_.samples_to_bytes_per_channel(samples.sample_count()));
new_seg.set_filename(i, new_filename);
}
qint64 current_cache_offset = start_cache_offset;
qint64 current_buffer_offset = start_buffer_offset;
return new_seg;
}
bool success = true;
AudioPlaybackCache::Segment AudioPlaybackCache::CreateSegment(const qint64 &size, const qint64& offset) const
{
Segment s(size);
while (current_cache_offset != end_cache_offset) {
qint64 segment = current_cache_offset / kDefaultSegmentSizePerChannel;
qint64 segment_start = segment * kDefaultSegmentSizePerChannel;
qint64 segment_end = segment_start + kDefaultSegmentSizePerChannel;
s.set_channels(params_.channel_count());
qint64 offset_in_segment = current_cache_offset - segment_start;
qint64 write_len = segment_end - offset_in_segment;
qint64 max_buffer_len = end_buffer_offset - current_buffer_offset;
qint64 zero_len = 0;
for (int i=0; i<params_.channel_count(); i++) {
// Generate random unused filename for this segment
QString fn = GenerateSegmentFilename();
// Set it for this segment/channel
s.set_filename(i, fn);
// Create empty file
QFile f(fn);
if (f.open(QFile::WriteOnly)) {
f.close();
}
}
s.set_offset(offset);
return s;
}
QString AudioPlaybackCache::GenerateSegmentFilename() const
{
QString new_seg_filename;
QDir cache_dir(QDir(GetCacheDirectory()).filePath(GetUuid().toString()));
do {
uint32_t r = QRandomGenerator::global()->generate();
new_seg_filename = cache_dir.filePath(QStringLiteral("%1.pcm").arg(r));
} while (QFileInfo::exists(new_seg_filename));
return new_seg_filename;
}
void AudioPlaybackCache::TrimSegmentIn(AudioPlaybackCache::Segment *s, qint64 new_length)
{
// Read filename
for (int i=0; i<s->channels(); i++) {
QFile f(s->filename(i));
if (f.open(QFile::ReadWrite)) {
// Read segment into memory, according to the size we acknowledge
QByteArray data = f.read(s->size());
// Trim to new length
data = data.right(new_length);
// Seek to start and write
f.seek(0);
// Write trimmed data
f.write(data);
f.close();
}
}
s->set_size(new_length);
}
void AudioPlaybackCache::TrimSegmentOut(AudioPlaybackCache::Segment *s, qint64 new_length)
{
// For efficiency, we don't truncate the file, we just truncate our usage of it
s->set_size(new_length);
}
void AudioPlaybackCache::RemoveSegmentFromArray(int index)
{
const Segment &s = playlist_.at(index);
for (int i=0; i<s.channels(); i++) {
QFile::remove(s.filename(i));
}
playlist_.removeAt(index);
}
void AudioPlaybackCache::ClearPlaylist()
{
foreach (const Segment& s, playlist_) {
for (int i=0; i<s.channels(); i++) {
QFile::remove(s.filename(i));
}
}
playlist_.clear();
}
void AudioPlaybackCache::UpdateOffsetsFrom(int index)
{
qint64 current_offset;
if (index == 0) {
current_offset = 0;
} else {
const Segment& previous = playlist_.at(index - 1);
current_offset = previous.offset() + previous.size();
}
for (int i=index; i<playlist_.size(); i++) {
Segment& s = playlist_[i];
s.set_offset(current_offset);
current_offset += s.size();
}
}
AudioPlaybackCache::PlaybackDevice *AudioPlaybackCache::CreatePlaybackDevice(QObject* parent) const
{
PlaybackDevice *d = new PlaybackDevice(playlist_, params_.bytes_per_sample_per_channel(), parent);
// If we're child of a viewer, set the data limit so audio doesn't play beyond the length
if (ViewerOutput *viewer = dynamic_cast<ViewerOutput*>(this->parent())) {
d->SetDataLimit(params_.time_to_bytes_per_channel(viewer->GetAudioLength()));
}
return d;
}
AudioPlaybackCache::Segment::Segment(qint64 size)
{
size_ = size;
}
AudioPlaybackCache::PlaybackDevice::PlaybackDevice(const AudioPlaybackCache::Playlist &playlist, int sample_sz, QObject *parent) :
QIODevice(parent),
playlist_(playlist),
current_segment_(0),
segment_read_index_(0),
sample_size_(sample_sz),
limit_(INT64_MAX)
{
}
AudioPlaybackCache::PlaybackDevice::~PlaybackDevice()
{
close();
}
bool AudioPlaybackCache::PlaybackDevice::seek(qint64 pos)
{
// Default behavior
QIODevice::seek(pos);
// Find which segment we're in
current_segment_ = playlist_.GetIndexOfPosition(pos);
// Catch failure to find index
if (current_segment_ == -1) {
return false;
}
// Find position in segment
segment_read_index_ = pos - playlist_.at(current_segment_).offset();
return true;
}
qint64 AudioPlaybackCache::PlaybackDevice::readData(char *data, qint64 maxSize)
{
qint64 read_size = 0;
while (read_size < maxSize
&& current_segment_ >= 0
&& current_segment_ < playlist_.size()
&& playlist_.at(current_segment_).offset() + segment_read_index_ < limit_) {
const Segment& cs = playlist_.at(current_segment_);
qint64 current_segment_sz = cs.size();
if (cs.offset() + current_segment_sz > limit_) {
current_segment_sz = limit_ - cs.offset();
if (write_len > max_buffer_len) {
zero_len = write_len - max_buffer_len;
write_len = max_buffer_len;
}
QVector<QFile*> segment_files(cs.channels());
segment_files.fill(nullptr);
for (int channel=0; channel<params_.channel_count(); channel++) {
QString filename = GetSegmentFilename(segment, channel);
bool all_files_opened = true;
// Open all file handles
for (int i=0; i<cs.channels(); i++) {
QFile *f = new QFile(cs.filename(i));
segment_files[i] = f;
if (f->open(QFile::ReadOnly)) {
// Seek to our stored index of this segment
f->seek(segment_read_index_);
} else {
all_files_opened = false;
if (!FileFunctions::DirectoryIsValid(QFileInfo(filename).dir())) {
success = false;
break;
}
}
// If all file handles opened successfully, time to interleave and send them out
if (all_files_opened) {
// Determine how many bytes to read
qint64 this_read_length = qMin((current_segment_sz - segment_read_index_) * cs.channels(), maxSize - read_size);
QFile f(filename);
if (f.open(QFile::ReadWrite)) {
f.seek(offset_in_segment);
f.write(reinterpret_cast<const char*>(samples.data(channel)) + current_buffer_offset, write_len);
qint64 target = read_size + this_read_length;
while (read_size < target) {
for (int i=0; i<cs.channels(); i++) {
QFile *segment_file = segment_files.at(i);
// Read those bytes
segment_file->read(data + read_size, sample_size_);
// Add to the read size
read_size += sample_size_;
if (zero_len > 0) {
QByteArray b(zero_len, 0);
f.write(b.constData());
}
// Add to the read index
segment_read_index_ += sample_size_;
}
// If we've reached the end of this segment, tick the counter over to the next segment
if (segment_read_index_ == current_segment_sz) {
// Jump to the next file
segment_read_index_ = 0;
current_segment_++;
f.close();
} else {
success = false;
}
}
// Close and delete file handles
for (int i=0; i<cs.channels(); i++) {
QFile *f = segment_files.at(i);
if (f) {
if (f->isOpen()) {
f->close();
}
delete f;
}
}
current_cache_offset += write_len;
current_buffer_offset += write_len;
}
if (read_size < maxSize) {
// Zero out remaining data
memset(data + read_size, 0, maxSize - read_size);
}
//return read_size;
return maxSize;
return success;
}
int AudioPlaybackCache::Playlist::GetIndexOfPosition(qint64 pos)
QString AudioPlaybackCache::GetSegmentFilename(qint64 segment_index, int channel)
{
if (this->isEmpty()
|| pos < 0
|| pos >= GetLength()) {
return -1;
}
if (pos < this->first().size()) {
return 0;
}
if (pos > this->last().offset()) {
return this->size() - 1;
}
// Use a binary search to find the segment with the right offset
int low = 0;
int high = this->size() - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
const Segment& mid_segment = this->at(mid);
if (mid_segment.offset() <= pos && mid_segment.offset() + mid_segment.size() > pos) {
return mid;
} else if (mid_segment.offset() < pos) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
qint64 AudioPlaybackCache::Playlist::GetLength() const
{
if (this->isEmpty()) {
return 0;
}
return this->last().offset() + this->last().size();
return GetThisCacheDirectory().filePath(QStringLiteral("%1.%2").arg(QString::number(segment_index), QString::number(channel)));
}
}
+4 -158
View File
@@ -68,171 +68,17 @@ public:
void WritePCM(const TimeRange &range, const TimeRangeList &valid_ranges, const SampleBuffer &samples);
void WriteWaveform(const TimeRange &range, const TimeRangeList &valid_ranges, const AudioVisualWaveform *waveform);
void WriteSilence(const TimeRange &range);
class Segment
{
public:
Segment(qint64 size = 0);
qint64 size() const
{
return size_;
}
void set_size(qint64 sz)
{
size_ = sz;
}
qint64 offset() const
{
return offset_;
}
void set_offset(qint64 o)
{
offset_ = o;
}
int channels() const
{
return filenames_.size();
}
void set_channels(int index)
{
filenames_.resize(index);
}
const QString& filename(int index) const
{
return filenames_.at(index);
}
void set_filename(int index, const QString& filename)
{
filenames_[index] = filename;
}
qint64 end() const
{
return offset_ + size_;
}
private:
QVector<QString> filenames_;
qint64 size_;
qint64 offset_;
};
class Playlist : public QVector<Segment>
{
public:
Playlist() = default;
int GetIndexOfPosition(qint64 pos);
qint64 GetLength() const;
};
class PlaybackDevice : public QIODevice
{
public:
PlaybackDevice(const Playlist& playlist, int sample_sz, QObject* parent = nullptr);
void SetDataLimit(qint64 limit)
{
limit_ = limit;
}
virtual ~PlaybackDevice() override;
virtual bool isSequential() const override
{
return false;
}
virtual bool seek(qint64 pos) override;
virtual qint64 size() const override
{
return playlist_.GetLength();
}
virtual qint64 readData(char *data, qint64 maxSize) override;
virtual qint64 writeData(const char *data, qint64 maxSize) override
{
Q_UNUSED(data)
Q_UNUSED(maxSize)
return -1;
}
private:
Playlist playlist_;
int current_segment_;
qint64 segment_read_index_;
int sample_size_;
qint64 limit_;
};
/**
* @brief Create a QIODevice that can play whatever's in the cache currently
*
* This device will act very much like a QFile, transparently linking together various segments
* into what will appear to be a single contiguous file.
*
* The caller becomes responsible for ownership of the device, though the parent can be set
* automatically as an optional parameter to this function.
*/
PlaybackDevice* CreatePlaybackDevice(QObject *parent = nullptr) const;
const AudioVisualWaveform &visual() const
{
return visual_;
}
signals:
void ParametersChanged();
private:
bool WritePartOfSampleBuffer(const SampleBuffer &samples, const rational &write_start, const rational &buffer_start, const rational &length);
QString GetSegmentFilename(qint64 segment_index, int channel);
static const qint64 kDefaultSegmentSizePerChannel;
Segment CloneSegment(const Segment& s) const;
Segment CreateSegment(const qint64 &size, const qint64 &offset) const;
QString GenerateSegmentFilename() const;
void TrimSegmentIn(Segment* s, qint64 new_length);
void TrimSegmentOut(Segment* s, qint64 new_length);
void RemoveSegmentFromArray(int index);
void ClearPlaylist();
void UpdateOffsetsFrom(int index);
Playlist playlist_;
AudioParams params_;
AudioVisualWaveform visual_;
};
}
+134
View File
@@ -0,0 +1,134 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2022 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "audiowaveformcache.h"
namespace olive {
AudioWaveformCache::AudioWaveformCache(QObject *parent) :
PlaybackCache{parent}
{
}
void AudioWaveformCache::WriteWaveform(const TimeRange &range, const TimeRangeList &valid_ranges, const AudioVisualWaveform *waveform)
{
// Write each valid range to the segments
foreach (const TimeRange& r, valid_ranges) {
#ifdef AVW_USE_LIST
// Write visual
TimeRangeList::util_remove(&waveforms_, r);
if (waveform) {
TimeRangeWithWaveform wv = r;
rational local_start = r.in() - range.in();
if (local_start != 0) {
wv.waveform = waveform->Mid(local_start, r.length());
} else {
wv.waveform = *waveform;
}
waveforms_.append(wv);
}
#else
if (waveform) {
waveforms_.OverwriteSums(*waveform, r.in(), r.in() - range.in(), r.length());
}
#endif
Validate(r);
}
}
void AudioWaveformCache::Draw(QPainter *painter, const QRect &rect, const double &scale, const rational &start_time) const
{
rational end = start_time + rational::fromDouble(rect.width() / scale);
TimeRange draw_range(start_time, end);
#ifdef AVW_USE_LIST
foreach (const TimeRangeWithWaveform &wv, waveforms_) {
if (wv.OverlapsWith(draw_range)) {
rational substart = std::max(wv.in(), draw_range.in());
rational subend = std::min(wv.out(), draw_range.out());
QRect subrect = rect;
subrect.setLeft(subrect.left() + (substart - draw_range.in()).toDouble()*scale);
subrect.setWidth((subend - substart).toDouble()*scale);
rational local_start = substart - wv.in();
AudioVisualWaveform::DrawWaveform(painter, subrect, scale, wv.waveform, local_start);
}
}
#else
AudioVisualWaveform::DrawWaveform(painter, rect, scale, waveforms_, start_time);
#endif
}
AudioVisualWaveform::Sample AudioWaveformCache::GetSummaryFromTime(const rational &start, const rational &length) const
{
#ifdef AVW_USE_LIST
QMap<rational, AudioVisualWaveform::Sample> sample;
TimeRange acquire(start, start+length);
foreach (const TimeRangeWithWaveform &wv, waveforms_) {
if (wv.OverlapsWith(acquire)) {
TimeRange this_range = wv.Intersected(acquire);
auto sum = wv.waveform.GetSummaryFromTime(this_range.in() - wv.in(), this_range.length());
sample.insert(this_range.in(), sum);
}
}
AudioVisualWaveform::Sample result;
for (auto it=sample.cbegin(); it!=sample.cend(); it++) {
result.insert(result.end(), it.value().begin(), it.value().end());
}
return result;
#else
return waveforms_.GetSummaryFromTime(start, length);
#endif
}
rational AudioWaveformCache::length() const
{
#ifdef AVW_USE_LIST
rational len = 0;
foreach (const TimeRangeWithWaveform &wv, waveforms_) {
len = std::max(len, wv.out());
}
return len;
#else
return waveforms_.length();
#endif
}
void AudioWaveformCache::SetPassthrough(PlaybackCache *cache)
{
AudioWaveformCache *c = static_cast<AudioWaveformCache*>(cache);
waveforms_ = c->waveforms_;
for (const TimeRange &r : c->GetValidatedRanges()) {
Validate(r);
}
SetParameters(c->GetParameters());
SetSavingEnabled(c->IsSavingEnabled());
}
}
+97
View File
@@ -0,0 +1,97 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2022 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef AUDIOWAVEFORMCACHE_H
#define AUDIOWAVEFORMCACHE_H
#include "audio/audiovisualwaveform.h"
#include "playbackcache.h"
//#define AVW_USE_LIST
namespace olive {
class AudioWaveformCache : public PlaybackCache
{
Q_OBJECT
public:
AudioWaveformCache(QObject *parent = nullptr);
void WriteWaveform(const TimeRange &range, const TimeRangeList &valid_ranges, const AudioVisualWaveform *waveform);
const AudioParams &GetParameters() const { return params_; }
void SetParameters(const AudioParams &p)
{
params_ = p;
waveforms_.set_channel_count(p.channel_count());
}
void Draw(QPainter* painter, const QRect &rect, const double &scale, const rational &start_time) const;
AudioVisualWaveform::Sample GetSummaryFromTime(const rational &start, const rational &length) const;
rational length() const;
virtual void SetPassthrough(PlaybackCache *cache) override;
private:
#ifdef AVW_USE_LIST
class TimeRangeWithWaveform : public TimeRange
{
public:
TimeRangeWithWaveform() = default;
TimeRangeWithWaveform(const TimeRange &r) :
TimeRange(r)
{
}
void set_in(const rational& in)
{
waveform.TrimIn(in - this->in());
TimeRange::set_in(in);
}
void set_out(const rational& out)
{
waveform.Resize(out - this->in());
TimeRange::set_out(out);
}
void set_range(const rational& in, const rational& out)
{
waveform.TrimRange(in, out-in);
TimeRange::set_range(in, out);
}
AudioVisualWaveform waveform;
};
QVector<TimeRangeWithWaveform> waveforms_;
#else
AudioVisualWaveform waveforms_;
#endif
AudioParams params_;
};
}
#endif // AUDIOWAVEFORMCACHE_H
+1 -3
View File
@@ -314,9 +314,7 @@ bool DiskCacheFolder::DeleteFileInternal(QMap<QString, HashTime>::iterator hash_
// Remove from disk
QFile f(filename);
if (!f.exists()) {
return true;
} else if (f.remove()) {
if (!f.exists() || f.remove()) {
// Remove from internal map
disk_data_.erase(hash_to_delete);
+159 -54
View File
@@ -32,12 +32,11 @@
#include "codec/frame.h"
#include "common/filefunctions.h"
#include "common/oiioutils.h"
#include "render/diskmanager.h"
namespace olive {
const QString FrameHashCache::kCacheFormatExtension = QStringLiteral(".exr");
#define super PlaybackCache
FrameHashCache::FrameHashCache(QObject *parent) :
@@ -65,6 +64,21 @@ void FrameHashCache::ValidateTime(const rational &time)
Validate(TimeRange(time, time + timebase_));
}
QString FrameHashCache::GetValidCacheFilename(const rational &time) const
{
if (IsFrameCached(time)) {
return CachePathName(time);
} else if (!GetPassthroughs().empty()) {
for (const Passthrough &p : GetPassthroughs()) {
if (p.Contains(time)) {
return CachePathName(GetCacheDirectory(), p.cache, time, timebase_);
}
}
}
return QString();
}
bool FrameHashCache::SaveCacheFrame(const int64_t &time, FramePtr frame) const
{
return SaveCacheFrame(GetCacheDirectory(), GetUuid(), time, frame);
@@ -178,15 +192,46 @@ FramePtr FrameHashCache::LoadCacheFrame(const QString &fn)
}
file.setFrameBuffer(framebuffer);
file.readPixels(dw.min.y, dw.max.y);
} catch (const std::exception &e) {
qCritical() << "Failed to read cache frame:" << e.what();
// Not an EXR, maybe it's a JPEG?
QImage img;
// Clear frame to signal that nothing was loaded
frame = nullptr;
if (img.load(fn, "jpg")) {
// Assume this frame is corrupt in some way and delete it
QMetaObject::invokeMethod(DiskManager::instance(), "DeleteSpecificFile", Q_ARG(QString, fn));
// FIXME: Hardcoded
const int div = 1;
const VideoParams::Format image_format = VideoParams::kFormatUnsigned8;
const int channel_count = 4;
const rational par(1, 1);
frame = Frame::Create();
frame->set_video_params(VideoParams(img.width() * div,
img.height() * div,
image_format,
channel_count,
par,
VideoParams::kInterlaceNone,
div));
frame->allocate();
for (int i=0; i<img.height(); i++) {
memcpy(frame->data() + frame->linesize_bytes() * i,
img.bits() + img.bytesPerLine() * i,
frame->width() * frame->video_params().GetBytesPerPixel());
}
} else {
qCritical() << "Failed to read cache frame:" << e.what();
// Clear frame to signal that nothing was loaded
frame = nullptr;
// Assume this frame is corrupt in some way and delete it
QMetaObject::invokeMethod(DiskManager::instance(), "DeleteSpecificFile", Q_ARG(QString, fn));
}
}
}
@@ -194,6 +239,38 @@ FramePtr FrameHashCache::LoadCacheFrame(const QString &fn)
return frame;
}
void FrameHashCache::SetPassthrough(PlaybackCache *cache)
{
super::SetPassthrough(cache);
SetTimebase(static_cast<FrameHashCache*>(cache)->GetTimebase());
}
void FrameHashCache::LoadStateEvent(QDataStream &stream)
{
uint32_t version;
int num, den;
stream >> version;
switch (version) {
case 1:
stream >> num;
stream >> den;
timebase_ = rational(num, den);
break;
}
}
void FrameHashCache::SaveStateEvent(QDataStream &stream)
{
uint32_t version = 1;
stream << version;
stream << timebase_.numerator();
stream << timebase_.denominator();
}
rational FrameHashCache::ToTime(const int64_t &ts) const
{
return Timecode::timestamp_to_time(ts, timebase_);
@@ -239,7 +316,7 @@ QString FrameHashCache::CachePathName(const rational &time) const
QString FrameHashCache::CachePathName(const QString &cache_path, const QUuid &cache_id, const int64_t &time)
{
QString filename = QDir(QDir(cache_path).filePath(cache_id.toString())).filePath(QString::number(time));
QString filename = GetThisCacheDirectory(cache_path, cache_id).filePath(QString::number(time));
// Register that in some way this hash has been accessed
if (DiskManager::instance()) {
@@ -256,63 +333,91 @@ QString FrameHashCache::CachePathName(const QString &cache_path, const QUuid &ca
bool FrameHashCache::SaveCacheFrame(const QString &filename, const FramePtr frame)
{
if (!VideoParams::FormatIsFloat(frame->format())) {
return false;
}
// Ensure directory is created
QDir cache_dir = QFileInfo(filename).dir();
if (!FileFunctions::DirectoryIsValid(cache_dir)) {
return false;
}
// Floating point types are stored in EXR
Imf::PixelType pix_type;
if (VideoParams::FormatIsFloat(frame->format())) {
// Floating point types are stored in EXR
Imf::PixelType pix_type;
if (frame->format() == VideoParams::kFormatFloat16) {
pix_type = Imf::HALF;
} else {
pix_type = Imf::FLOAT;
}
Imf::Header header(frame->width(), frame->height());
header.channels().insert("R", Imf::Channel(pix_type));
header.channels().insert("G", Imf::Channel(pix_type));
header.channels().insert("B", Imf::Channel(pix_type));
if (frame->channel_count() == VideoParams::kRGBAChannelCount) {
header.channels().insert("A", Imf::Channel(pix_type));
}
header.compression() = Imf::DWAA_COMPRESSION;
header.insert("dwaCompressionLevel", Imf::FloatAttribute(200.0f));
header.pixelAspectRatio() = frame->video_params().pixel_aspect_ratio().toDouble();
header.insert("oliveDivider", Imf::IntAttribute(frame->video_params().divider()));
try {
Imf::OutputFile out(filename.toUtf8(), header, 0);
int bpc = VideoParams::GetBytesPerChannel(frame->format());
size_t xs = frame->channel_count() * bpc;
size_t ys = frame->linesize_bytes();
Imf::FrameBuffer framebuffer;
framebuffer.insert("R", Imf::Slice(pix_type, frame->data(), xs, ys));
framebuffer.insert("G", Imf::Slice(pix_type, frame->data() + bpc, xs, ys));
framebuffer.insert("B", Imf::Slice(pix_type, frame->data() + 2*bpc, xs, ys));
if (frame->channel_count() == VideoParams::kRGBAChannelCount) {
framebuffer.insert("A", Imf::Slice(pix_type, frame->data() + 3*bpc, xs, ys));
if (frame->format() == VideoParams::kFormatFloat16) {
pix_type = Imf::HALF;
} else {
pix_type = Imf::FLOAT;
}
out.setFrameBuffer(framebuffer);
out.writePixels(frame->height());
Imf::Header header(frame->width(), frame->height());
header.channels().insert("R", Imf::Channel(pix_type));
header.channels().insert("G", Imf::Channel(pix_type));
header.channels().insert("B", Imf::Channel(pix_type));
if (frame->channel_count() == VideoParams::kRGBAChannelCount) {
header.channels().insert("A", Imf::Channel(pix_type));
}
return true;
} catch (const std::exception &e) {
qCritical() << "Failed to write cache frame:" << e.what();
header.compression() = Imf::DWAA_COMPRESSION;
header.insert("dwaCompressionLevel", Imf::FloatAttribute(200.0f));
header.pixelAspectRatio() = frame->video_params().pixel_aspect_ratio().toDouble();
return false;
header.insert("oliveDivider", Imf::IntAttribute(frame->video_params().divider()));
try {
Imf::OutputFile out(filename.toUtf8(), header, 0);
int bpc = VideoParams::GetBytesPerChannel(frame->format());
size_t xs = frame->channel_count() * bpc;
size_t ys = frame->linesize_bytes();
Imf::FrameBuffer framebuffer;
framebuffer.insert("R", Imf::Slice(pix_type, frame->data(), xs, ys));
framebuffer.insert("G", Imf::Slice(pix_type, frame->data() + bpc, xs, ys));
framebuffer.insert("B", Imf::Slice(pix_type, frame->data() + 2*bpc, xs, ys));
if (frame->channel_count() == VideoParams::kRGBAChannelCount) {
framebuffer.insert("A", Imf::Slice(pix_type, frame->data() + 3*bpc, xs, ys));
}
out.setFrameBuffer(framebuffer);
out.writePixels(frame->height());
return true;
} catch (const std::exception &e) {
qCritical() << "Failed to write cache frame:" << e.what();
return false;
}
} else {
QImage::Format fmt = QImage::Format_Invalid;
switch (frame->format()) {
case VideoParams::kFormatUnsigned8:
if (frame->channel_count() == VideoParams::kRGBAChannelCount){
fmt = QImage::Format_RGBA8888_Premultiplied;
} else if (frame->channel_count() == VideoParams::kRGBChannelCount){
fmt = QImage::Format_RGB888;
}
break;
case VideoParams::kFormatUnsigned16:
if (frame->channel_count() == VideoParams::kRGBAChannelCount){
fmt = QImage::Format_RGBA64_Premultiplied;
}
break;
case VideoParams::kFormatFloat16:
case VideoParams::kFormatFloat32:
case VideoParams::kFormatCount:
case VideoParams::kFormatInvalid:
break;
}
if (fmt == QImage::Format_Invalid) {
return false;
}
QImage img(reinterpret_cast<const uchar*>(frame->data()), frame->width(), frame->height(), frame->linesize_bytes(), fmt);
return img.save(filename, "jpg");
}
}
+18 -10
View File
@@ -48,14 +48,7 @@ public:
return GetValidatedRanges().contains(time);
}
QString GetValidCacheFilename(const rational &time) const
{
if (IsFrameCached(time)) {
return CachePathName(time);
} else {
return QString();
}
}
QString GetValidCacheFilename(const rational &time) const;
static bool SaveCacheFrame(const QString& filename, FramePtr frame);
bool SaveCacheFrame(const int64_t &time, FramePtr frame) const;
@@ -65,6 +58,12 @@ public:
FramePtr LoadCacheFrame(const int64_t &time) const;
static FramePtr LoadCacheFrame(const QString& fn);
virtual void SetPassthrough(PlaybackCache *cache) override;
protected:
virtual void LoadStateEvent(QDataStream &stream) override;
virtual void SaveStateEvent(QDataStream &stream) override;
private:
rational ToTime(const int64_t &ts) const;
int64_t ToTimestamp(const rational &ts, Timecode::Rounding rounding = Timecode::kRound) const;
@@ -80,8 +79,6 @@ private:
rational timebase_;
static const QString kCacheFormatExtension;
private slots:
void HashDeleted(const QString &path, const QString &filename);
@@ -89,6 +86,17 @@ private slots:
};
class ThumbnailCache : public FrameHashCache
{
Q_OBJECT
public:
ThumbnailCache(QObject* parent = nullptr) :
FrameHashCache(parent)
{
SetTimebase(rational(1, 10));
}
};
}
#endif // VIDEORENDERFRAMECACHE_H
+55
View File
@@ -0,0 +1,55 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2022 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef CACHEJOB_H
#define CACHEJOB_H
#include <QString>
#include <QVariant>
namespace olive {
class CacheJob
{
public:
CacheJob() = default;
CacheJob(const QString &filename, const QVariant &fallback = QVariant())
{
filename_ = filename;
}
const QString &GetFilename() const { return filename_; }
void SetFilename(const QString &s) { filename_ = s; }
const QVariant &GetFallback() const { return fallback_; }
void SetFallback(const QVariant &val) { fallback_ = val; }
private:
QString filename_;
QVariant fallback_;
};
}
Q_DECLARE_METATYPE(olive::CacheJob)
#endif // CACHEJOB_H
+176 -6
View File
@@ -27,7 +27,7 @@
namespace olive {
void PlaybackCache::Invalidate(const TimeRange &r, bool signal)
void PlaybackCache::Invalidate(const TimeRange &r)
{
if (r.in() == r.out()) {
qWarning() << "Tried to invalidate zero-length range";
@@ -36,10 +36,16 @@ void PlaybackCache::Invalidate(const TimeRange &r, bool signal)
validated_.remove(r);
if (!passthroughs_.empty()) {
TimeRangeList::util_remove(&passthroughs_, r);
}
InvalidateEvent(r);
if (signal) {
emit Invalidated(r);
emit Invalidated(r);
if (saving_enabled_) {
SaveState();
}
}
@@ -48,6 +54,155 @@ Node *PlaybackCache::parent() const
return dynamic_cast<Node*>(QObject::parent());
}
QDir PlaybackCache::GetThisCacheDirectory() const
{
return GetThisCacheDirectory(GetCacheDirectory(), GetUuid());
}
QDir PlaybackCache::GetThisCacheDirectory(const QString &cache_path, const QUuid &cache_id)
{
return QDir(cache_path).filePath(cache_id.toString());
}
void PlaybackCache::LoadState()
{
QDir cache_dir = GetThisCacheDirectory();
QFile f(cache_dir.filePath(QStringLiteral("state")));
if (f.open(QFile::ReadOnly)) {
QDataStream s(&f);
uint32_t version;
s >> version;
LoadStateEvent(s);
switch (version) {
case 1:
{
int valid_count, pass_count;
validated_.clear();
s >> valid_count;
for (int i=0; i<valid_count; i++) {
int in_num, in_den, out_num, out_den;
s >> in_num;
s >> in_den;
s >> out_num;
s >> out_den;
validated_.insert(TimeRange(rational(in_num, in_den), rational(out_num, out_den)));
}
passthroughs_.clear();
s >> pass_count;
for (int i=0; i<pass_count; i++) {
QUuid id;
int in_num, in_den, out_num, out_den;
s >> in_num;
s >> in_den;
s >> out_num;
s >> out_den;
s >> id;
Passthrough p = TimeRange(rational(in_num, in_den), rational(out_num, out_den));
p.cache = id;
passthroughs_.append(p);
}
break;
}
}
f.close();
}
}
void PlaybackCache::SaveState()
{
QDir cache_dir = GetThisCacheDirectory();
QFile f(cache_dir.filePath(QStringLiteral("state")));
if (validated_.isEmpty() && passthroughs_.isEmpty()) {
if (f.exists()) {
f.remove();
}
} else {
if (FileFunctions::DirectoryIsValid(cache_dir)) {
if (f.open(QFile::WriteOnly)) {
QDataStream s(&f);
uint32_t version = 1;
s << version;
SaveStateEvent(s);
s << validated_.size();
for (const TimeRange &r : validated_) {
s << r.in().numerator();
s << r.in().denominator();
s << r.out().numerator();
s << r.out().denominator();
}
s << passthroughs_.size();
for (const Passthrough &p : passthroughs_) {
s << p.in().numerator();
s << p.in().denominator();
s << p.out().numerator();
s << p.out().denominator();
s << p.cache;
}
f.close();
}
}
}
}
void PlaybackCache::Draw(QPainter *p, const rational &start, double scale, const QRect &rect) const
{
p->fillRect(rect, Qt::red);
foreach (const TimeRange& range, GetValidatedRanges()) {
int range_left = rect.left() + (range.in() - start).toDouble() * scale;
if (range_left >= rect.right()) {
continue;
}
int range_right = rect.left() + (range.out() - start).toDouble() * scale;
if (range_right < rect.left()) {
continue;
}
int adjusted_left = std::max(range_left, rect.left());
int adjusted_right = std::min(range_right, rect.right());
p->fillRect(adjusted_left,
rect.top(),
adjusted_right - adjusted_left,
rect.height(),
Qt::green);
}
}
void PlaybackCache::SetPassthrough(PlaybackCache *cache)
{
for (const TimeRange &r : cache->GetValidatedRanges()) {
Passthrough p = r;
p.cache = cache->GetUuid();
passthroughs_.push_back(p);
}
passthroughs_.append(cache->GetPassthroughs());
if (saving_enabled_) {
SaveState();
}
}
void PlaybackCache::InvalidateAll()
{
Invalidate(TimeRange(0, RATIONAL_MAX));
@@ -60,6 +215,10 @@ void PlaybackCache::Validate(const TimeRange &r, bool signal)
if (signal) {
emit Validated(r);
}
if (saving_enabled_) {
SaveState();
}
}
void PlaybackCache::InvalidateEvent(const TimeRange &)
@@ -73,12 +232,19 @@ Project *PlaybackCache::GetProject() const
PlaybackCache::PlaybackCache(QObject *parent) :
QObject(parent),
enabled_(false)
saving_enabled_(true)
{
uuid_ = QUuid::createUuid();
}
TimeRangeList PlaybackCache::GetInvalidatedRanges(TimeRange intersecting)
void PlaybackCache::SetUuid(const QUuid &u)
{
uuid_ = u;
LoadState();
}
TimeRangeList PlaybackCache::GetInvalidatedRanges(TimeRange intersecting) const
{
TimeRangeList invalidated;
@@ -93,10 +259,14 @@ TimeRangeList PlaybackCache::GetInvalidatedRanges(TimeRange intersecting)
invalidated.remove(range);
}
foreach (const TimeRange &range, passthroughs_) {
invalidated.remove(range);
}
return invalidated;
}
bool PlaybackCache::HasInvalidatedRanges(const TimeRange &intersecting)
bool PlaybackCache::HasInvalidatedRanges(const TimeRange &intersecting) const
{
return !validated_.contains(intersecting);
}
+54 -17
View File
@@ -21,7 +21,10 @@
#ifndef PLAYBACKCACHE_H
#define PLAYBACKCACHE_H
#include <QDir>
#include <QMutex>
#include <QObject>
#include <QPainter>
#include <QUuid>
#include "common/jobtime.h"
@@ -40,37 +43,61 @@ public:
PlaybackCache(QObject* parent = nullptr);
const QUuid &GetUuid() const { return uuid_; }
void SetUuid(const QUuid &u) { uuid_ = u; }
void SetUuid(const QUuid &u);
bool IsEnabled() const { return enabled_; }
void SetEnabled(bool e)
{
if (enabled_ != e) {
enabled_ = e;
emit EnabledChanged(e);
}
}
TimeRangeList GetInvalidatedRanges(TimeRange intersecting);
TimeRangeList GetInvalidatedRanges(const rational &length)
TimeRangeList GetInvalidatedRanges(TimeRange intersecting) const;
TimeRangeList GetInvalidatedRanges(const rational &length) const
{
return GetInvalidatedRanges(TimeRange(0, length));
}
bool HasInvalidatedRanges(const TimeRange &intersecting);
bool HasInvalidatedRanges(const rational &length)
bool HasInvalidatedRanges(const TimeRange &intersecting) const;
bool HasInvalidatedRanges(const rational &length) const
{
return HasInvalidatedRanges(TimeRange(0, length));
}
QString GetCacheDirectory() const;
void Invalidate(const TimeRange& r, bool signal = true);
void Invalidate(const TimeRange& r);
bool HasValidatedRanges() const { return !validated_.isEmpty(); }
const TimeRangeList &GetValidatedRanges() const { return validated_; }
Node *parent() const;
QDir GetThisCacheDirectory() const;
static QDir GetThisCacheDirectory(const QString &cache_path, const QUuid &cache_id);
void LoadState();
void SaveState();
void Draw(QPainter *painter, const rational &start, double scale, const QRect &rect) const;
static int GetCacheIndicatorHeight()
{
return QFontMetrics(QFont()).height()/4;
}
bool IsSavingEnabled() const { return saving_enabled_; }
void SetSavingEnabled(bool e) { saving_enabled_ = e; }
virtual void SetPassthrough(PlaybackCache *cache);
QMutex *mutex() { return &mutex_; }
class Passthrough : public TimeRange
{
public:
Passthrough(const TimeRange &r) :
TimeRange(r)
{}
QUuid cache;
};
const QVector<Passthrough> &GetPassthroughs() const { return passthroughs_; }
public slots:
void InvalidateAll();
@@ -79,13 +106,19 @@ signals:
void Validated(const olive::TimeRange& r);
void EnabledChanged(bool e);
void Request(const olive::TimeRange& r);
void CancelAll();
protected:
void Validate(const TimeRange& r, bool signal = true);
virtual void InvalidateEvent(const TimeRange& range);
virtual void LoadStateEvent(QDataStream &stream){}
virtual void SaveStateEvent(QDataStream &stream){}
Project* GetProject() const;
private:
@@ -93,7 +126,11 @@ private:
QUuid uuid_;
bool enabled_;
bool saving_enabled_;
QMutex mutex_;
QVector<Passthrough> passthroughs_;
};
+331 -288
View File
@@ -26,22 +26,22 @@
#include "codec/conformmanager.h"
#include "node/inputdragger.h"
#include "node/project/project.h"
#include "render/diskmanager.h"
#include "render/renderprocessor.h"
#include "task/customcache/customcachetask.h"
#include "task/taskmanager.h"
#include "widget/slider/base/numericsliderbase.h"
#include "widget/viewer/viewer.h"
namespace olive {
// We may want to make this configurable at some point, so for now this constant is used as a
// placeholder for where that configarable variable would be used.
const bool PreviewAutoCacher::kRealTimeWaveformsEnabled = true;
PreviewAutoCacher::PreviewAutoCacher() :
PreviewAutoCacher::PreviewAutoCacher(QObject *parent) :
QObject(parent),
viewer_node_(nullptr),
use_custom_range_(false),
pause_audio_(false),
single_frame_render_(nullptr)
pause_renders_(false),
single_frame_render_(nullptr),
display_color_processor_(nullptr)
{
// Set defaults
SetPlayhead(0);
@@ -49,7 +49,7 @@ PreviewAutoCacher::PreviewAutoCacher() :
// Wait a certain amount of time before requeuing when we receive an invalidate signal
delayed_requeue_timer_.setInterval(OLIVE_CONFIG("AutoCacheDelay").toInt());
delayed_requeue_timer_.setSingleShot(true);
connect(&delayed_requeue_timer_, &QTimer::timeout, this, &PreviewAutoCacher::RequeueFrames);
connect(&delayed_requeue_timer_, &QTimer::timeout, this, &PreviewAutoCacher::TryRender);
// Catch when a conform is ready
connect(ConformManager::instance(), &ConformManager::ConformReady, this, &PreviewAutoCacher::ConformFinished);
@@ -81,30 +81,55 @@ RenderTicketPtr PreviewAutoCacher::GetSingleFrame(const rational &t, bool dry)
RenderTicketPtr PreviewAutoCacher::GetRangeOfAudio(TimeRange range)
{
return RenderAudio(range, false);
return RenderAudio(copied_viewer_node_->GetConnectedSampleOutput(), range, nullptr);
}
void PreviewAutoCacher::VideoInvalidated(const TimeRange &range)
void PreviewAutoCacher::ClearSingleFrameRenders()
{
// Stop any current render tasks because a) they might be out of date now anyway, and b) we
// want to dedicate all our rendering power to realtime feedback for the user
CancelVideoTasks();
// If auto-cache is enabled and a slider is not being dragged, queue up to hash these frames
if (viewer_node_->video_frame_cache()->IsEnabled() && !NodeInputDragger::IsInputBeingDragged()) {
StartCachingVideoRange(range);
QMap<RenderTicketWatcher*, QVector<RenderTicketPtr> > copy = video_immediate_passthroughs_;
for (auto it=copy.cbegin(); it!=copy.cend(); it++) {
it.key()->Cancel();
if (!it.key()->IsRunning()) {
RenderManager::instance()->RemoveTicket(it.key()->GetTicket());
emit it.key()->GetTicket()->Finished();
}
}
}
void PreviewAutoCacher::AudioInvalidated(const TimeRange &range)
void PreviewAutoCacher::VideoInvalidatedFromCache(const TimeRange &range)
{
// We don't stop rendering audio because currently there's no system of requeuing audio if it's
// cancelled, so some areas may end up unrendered forever
// ClearAudioQueue();
PlaybackCache *cache = static_cast<PlaybackCache*>(sender());
// If we're auto-caching audio or require realtime waveforms, we'll have to render this
if (viewer_node_->audio_playback_cache()->IsEnabled() || kRealTimeWaveformsEnabled) {
StartCachingAudioRange(range);
VideoInvalidatedFromNode(cache, range);
}
void PreviewAutoCacher::AudioInvalidatedFromCache(const TimeRange &range)
{
PlaybackCache *cache = static_cast<PlaybackCache*>(sender());
AudioInvalidatedFromNode(cache, range);
}
void PreviewAutoCacher::CancelForCache()
{
PlaybackCache *cache = static_cast<PlaybackCache*>(sender());
if (dynamic_cast<FrameHashCache*>(cache) || dynamic_cast<ThumbnailCache*>(cache)) {
for (auto it=pending_video_jobs_.begin(); it!=pending_video_jobs_.end(); ) {
if ((*it).cache == cache) {
it = pending_video_jobs_.erase(it);
} else {
it++;
}
}
} else if (dynamic_cast<AudioPlaybackCache*>(cache) || dynamic_cast<AudioWaveformCache*>(cache)) {
for (auto it=pending_audio_jobs_.begin(); it!=pending_audio_jobs_.end(); ) {
if ((*it).cache == cache) {
it = pending_audio_jobs_.erase(it);
} else {
it++;
}
}
}
}
@@ -115,68 +140,45 @@ void PreviewAutoCacher::AudioRendered()
// If the task list doesn't contain this watcher, presumably it was cleared as a result of a
// viewer switch, so we'll completely ignore this watcher
if (audio_tasks_.contains(watcher)) {
if (running_audio_tasks_.removeOne(watcher)) {
// Assume that a "result" is a fully completed image and a non-result is a cancelled ticket
TimeRange range = audio_tasks_.take(watcher);
TimeRange range = watcher->property("time").value<TimeRange>();
Node *node = copy_map_.key(Node::ValueToPtr<Node>(watcher->property("node")));
if (watcher->HasResult()) {
// Remove this task from the list
JobTime watcher_job_time = watcher->property("job").value<JobTime>();
if (watcher->HasResult() && node) {
if (PlaybackCache *cache = Node::ValueToPtr<PlaybackCache>(watcher->property("cache"))) {
AudioCacheData &d = audio_cache_data_[cache];
TimeRangeList valid_ranges = audio_job_tracker_.getCurrentSubRanges(range, watcher_job_time);
JobTime watcher_job_time = watcher->property("job").value<JobTime>();
AudioVisualWaveform waveform = watcher->GetTicket()->property("waveform").value<AudioVisualWaveform>();
TimeRangeList valid_ranges = d.job_tracker.getCurrentSubRanges(range, watcher_job_time);
if (viewer_node_->audio_playback_cache()->IsEnabled()) {
// WritePCM is tolerant to its buffer being null, it will just write silence instead
viewer_node_->audio_playback_cache()->WritePCM(range,
valid_ranges,
watcher->Get().value<SampleBuffer>());
}
AudioVisualWaveform waveform = watcher->GetTicket()->property("waveform").value<AudioVisualWaveform>();
viewer_node_->audio_playback_cache()->WriteWaveform(range, valid_ranges, &waveform);
SampleBuffer buf = watcher->Get().value<SampleBuffer>();
// Detect if this audio was incomplete because it was waiting on a conform to finish
if (watcher->GetTicket()->property("incomplete").toBool()) {
if (last_conform_task_ > watcher_job_time) {
// Requeue now
viewer_node_->audio_playback_cache()->Invalidate(range);
} else {
// Wait for conform
audio_needing_conform_.insert(range);
}
} else{
// Retrieve visual waveforms
QVector<RenderProcessor::RenderedWaveform> waveform_list = watcher->GetTicket()->property("waveforms").value< QVector<RenderProcessor::RenderedWaveform> >();
foreach (const RenderProcessor::RenderedWaveform& waveform_info, waveform_list) {
// Find original track
ClipBlock* block = nullptr;
bool incomplete = watcher->GetTicket()->property("incomplete").toBool();
for (auto it=copy_map_.cbegin(); it!=copy_map_.cend(); it++) {
if (it.value() == waveform_info.block) {
block = static_cast<ClipBlock*>(it.key());
break;
}
if (AudioPlaybackCache *pcm = dynamic_cast<AudioPlaybackCache*>(cache)) {
// WritePCM is tolerant to its buffer being null, it will just write silence instead
pcm->SetParameters(buf.audio_params());
pcm->WritePCM(range,
valid_ranges,
watcher->Get().value<SampleBuffer>());
} else if (AudioWaveformCache *wave = dynamic_cast<AudioWaveformCache*>(cache)) {
wave->SetParameters(buf.audio_params());
if (!incomplete) {
wave->WriteWaveform(range, valid_ranges, &waveform);
}
}
if (block && !valid_ranges.isEmpty()) {
// Generate visual waveform in this background thread
block->waveform().set_channel_count(viewer_node_->GetAudioParams().channel_count());
// Determine which of the waveform ranges we got intersects with the valid ranges
TimeRangeList intersections = valid_ranges.Intersects(waveform_info.range + block->in());
foreach (TimeRange r, intersections) {
// For each range, adjust it relative to the block and write it
r -= block->in();
if (waveform_info.silence) {
block->waveform().OverwriteSilence(r.in(), r.length());
} else {
block->waveform().OverwriteSums(waveform_info.waveform, r.in(), r.in() - waveform_info.range.in(), r.length());
}
}
emit block->PreviewChanged();
if (incomplete) {
if (last_conform_task_ > watcher_job_time) {
// Requeue now
cache->Invalidate(range);
} else {
// Wait for conform
d.needs_conform.insert(range);
}
}
}
@@ -193,6 +195,13 @@ void PreviewAutoCacher::VideoRendered()
{
RenderTicketWatcher* watcher = static_cast<RenderTicketWatcher*>(sender());
const QStringList bad_cache_names = watcher->GetTicket()->property("badcache").toStringList();
if (!bad_cache_names.empty()) {
for (const QString &fn : bad_cache_names) {
DiskManager::instance()->DeleteSpecificFile(fn);
}
}
// Process passthroughs no matter what, if the viewer was switched, the passthrough map would be
// cleared anyway
QVector<RenderTicketPtr> tickets = video_immediate_passthroughs_.take(watcher);
@@ -206,21 +215,21 @@ void PreviewAutoCacher::VideoRendered()
// If the task list doesn't contain this watcher, presumably it was cleared as a result of a
// viewer switch, so we'll completely ignore this watcher
auto it = video_tasks_.find(watcher);
if (it != video_tasks_.end()) {
if (running_video_tasks_.removeOne(watcher)) {
// Assume that a "result" is a fully completed image and a non-result is a cancelled ticket
if (watcher->HasResult()) {
// Download frame in another thread
if (watcher->GetTicket()->property("cached").toBool()) {
if (FrameHashCache *cache = Node::ValueToPtr<FrameHashCache>(watcher->property("cache"))) {
cache->ValidateTime(it.value());
rational time = watcher->property("time").value<rational>();
JobTime job = watcher->property("job").value<JobTime>();
if (video_cache_data_.value(cache).job_tracker.isCurrent(time, job)) {
cache->ValidateTime(time);
}
}
}
}
video_tasks_.erase(it);
// Continue rendering
TryRender();
}
@@ -273,6 +282,12 @@ void PreviewAutoCacher::AddNode(Node *node)
// Add to project
copy->setParent(&copied_project_);
// Disable caches for copy
copy->SetCachesEnabled(false);
// Copy cache UUIDs
copy->CopyCacheUuidsFrom(node);
// Insert into map
InsertIntoCopyMap(node, copy);
@@ -352,54 +367,68 @@ void PreviewAutoCacher::InsertIntoCopyMap(Node *node, Node *copy)
void PreviewAutoCacher::ConnectToNodeCache(Node *node)
{
// TEMP: Retain existing behavior until more work is done
if (node == viewer_node_) {
connect(node->video_frame_cache(),
&PlaybackCache::EnabledChanged,
this,
&PreviewAutoCacher::VideoAutoCacheEnableChanged);
connect(node->video_frame_cache(),
&PlaybackCache::Request,
this,
&PreviewAutoCacher::VideoInvalidatedFromCache);
connect(node->audio_playback_cache(),
&PlaybackCache::EnabledChanged,
this,
&PreviewAutoCacher::AudioAutoCacheEnableChanged);
connect(node->thumbnail_cache(),
&PlaybackCache::Request,
this,
&PreviewAutoCacher::VideoInvalidatedFromCache);
connect(node->video_frame_cache(),
&PlaybackCache::Invalidated,
this,
&PreviewAutoCacher::VideoInvalidated);
connect(node->audio_playback_cache(),
&PlaybackCache::Request,
this,
&PreviewAutoCacher::AudioInvalidatedFromCache);
connect(node->audio_playback_cache(),
&PlaybackCache::Invalidated,
this,
&PreviewAutoCacher::AudioInvalidated);
}
connect(node->waveform_cache(),
&PlaybackCache::Request,
this,
&PreviewAutoCacher::AudioInvalidatedFromCache);
connect(node->video_frame_cache(),
&PlaybackCache::CancelAll,
this,
&PreviewAutoCacher::CancelForCache);
connect(node->audio_playback_cache(),
&PlaybackCache::CancelAll,
this,
&PreviewAutoCacher::CancelForCache);
}
void PreviewAutoCacher::DisconnectFromNodeCache(Node *node)
{
// TEMP: Retain existing behavior until more work is done
if (node == viewer_node_) {
disconnect(node->video_frame_cache(),
&PlaybackCache::EnabledChanged,
this,
&PreviewAutoCacher::VideoAutoCacheEnableChanged);
disconnect(node->video_frame_cache(),
&PlaybackCache::Request,
this,
&PreviewAutoCacher::VideoInvalidatedFromCache);
disconnect(node->audio_playback_cache(),
&PlaybackCache::EnabledChanged,
this,
&PreviewAutoCacher::AudioAutoCacheEnableChanged);
disconnect(node->thumbnail_cache(),
&PlaybackCache::Request,
this,
&PreviewAutoCacher::VideoInvalidatedFromCache);
disconnect(node->video_frame_cache(),
&PlaybackCache::Invalidated,
this,
&PreviewAutoCacher::VideoInvalidated);
disconnect(node->audio_playback_cache(),
&PlaybackCache::Request,
this,
&PreviewAutoCacher::AudioInvalidatedFromCache);
disconnect(node->audio_playback_cache(),
&PlaybackCache::Invalidated,
this,
&PreviewAutoCacher::AudioInvalidated);
}
disconnect(node->waveform_cache(),
&PlaybackCache::Request,
this,
&PreviewAutoCacher::AudioInvalidatedFromCache);
disconnect(node->video_frame_cache(),
&PlaybackCache::CancelAll,
this,
&PreviewAutoCacher::CancelForCache);
disconnect(node->audio_playback_cache(),
&PlaybackCache::CancelAll,
this,
&PreviewAutoCacher::CancelForCache);
}
void PreviewAutoCacher::UpdateGraphChangeValue()
@@ -421,44 +450,64 @@ void PreviewAutoCacher::CancelQueuedSingleFrameRender()
}
}
void PreviewAutoCacher::VideoInvalidatedList(const TimeRangeList &list)
{
foreach (const TimeRange &range, list) {
VideoInvalidated(range);
}
}
void PreviewAutoCacher::AudioInvalidatedList(const TimeRangeList &list)
{
foreach (const TimeRange &range, list) {
AudioInvalidated(range);
}
}
void PreviewAutoCacher::StartCachingRange(const TimeRange &range, TimeRangeList *range_list, RenderJobTracker *tracker)
{
range_list->insert(range);
tracker->insert(range, graph_changed_time_);
}
void PreviewAutoCacher::StartCachingVideoRange(const TimeRange &range)
void PreviewAutoCacher::StartCachingVideoRange(PlaybackCache *cache, const TimeRange &range)
{
StartCachingRange(range, &invalidated_video_, &video_job_tracker_);
RequeueFrames();
Node *node = cache->parent();
rational using_tb;
if (ThumbnailCache *thumbs = dynamic_cast<ThumbnailCache*>(cache)) {
using_tb = thumbs->GetTimebase();
} else {
using_tb = viewer_node_->GetVideoParams().frame_rate_as_time_base();
}
TimeRangeListFrameIterator iterator({range}, using_tb);
pending_video_jobs_.push_back({node, cache, range, iterator});
video_cache_data_[cache].job_tracker.insert(TimeRange(iterator.Snap(range.in()), range.out()), graph_changed_time_);
TryRender();
}
void PreviewAutoCacher::StartCachingAudioRange(const TimeRange &range)
void PreviewAutoCacher::StartCachingAudioRange(PlaybackCache *cache, const TimeRange &range)
{
StartCachingRange(range, &invalidated_audio_, &audio_job_tracker_);
Node *node = cache->parent();
pending_audio_jobs_.push_back({node, cache, range});
audio_cache_data_[cache].job_tracker.insert(range, graph_changed_time_);
TryRender();
}
void PreviewAutoCacher::VideoInvalidatedFromNode(PlaybackCache *cache, const TimeRange &range)
{
// Stop any current render tasks because a) they might be out of date now anyway, and b) we
// want to dedicate all our rendering power to realtime feedback for the user
//CancelVideoTasks(node);
// If auto-cache is enabled and a slider is not being dragged, queue up to hash these frames
if (!NodeInputDragger::IsInputBeingDragged()) {
StartCachingVideoRange(cache, range);
}
}
void PreviewAutoCacher::AudioInvalidatedFromNode(PlaybackCache *cache, const TimeRange &range)
{
// We don't stop rendering audio because currently there's no system of requeuing audio if it's
// cancelled, so some areas may end up unrendered forever
// ClearAudioQueue();
// If we're auto-caching audio or require realtime waveforms, we'll have to render this
StartCachingAudioRange(cache, range);
}
void PreviewAutoCacher::SetPlayhead(const rational &playhead)
{
cache_range_ = TimeRange(playhead - OLIVE_CONFIG("DiskCacheBehind").value<rational>(),
playhead + OLIVE_CONFIG("DiskCacheAhead").value<rational>());
RequeueFrames();
TryRender();
}
template<typename T>
@@ -466,30 +515,37 @@ void CancelTasks(const T &task_list, bool and_wait)
{
for (auto it=task_list.cbegin(); it!=task_list.cend(); it++) {
// Signal that the ticket should not be finished
it.key()->Cancel();
(*it)->Cancel();
}
if (and_wait) {
// Wait for each ticket to finish
for (auto it=task_list.cbegin(); it!=task_list.cend(); it++) {
it.key()->WaitForFinished();
(*it)->WaitForFinished();
}
}
}
void PreviewAutoCacher::CancelVideoTasks(bool and_wait_for_them_to_finish)
{
CancelTasks(video_tasks_, and_wait_for_them_to_finish);
CancelTasks(running_video_tasks_, and_wait_for_them_to_finish);
}
void PreviewAutoCacher::CancelAudioTasks(bool and_wait_for_them_to_finish)
{
CancelTasks(audio_tasks_, and_wait_for_them_to_finish);
CancelTasks(running_audio_tasks_, and_wait_for_them_to_finish);
}
void PreviewAutoCacher::SetAudioPaused(bool e)
bool PreviewAutoCacher::IsRenderingCustomRange() const
{
pause_audio_ = e;
/*const VideoCacheData &d = video_cache_data_.value(viewer_node_);
return d.iterator.IsCustomRange() && d.iterator.HasNext();*/
return false;
}
void PreviewAutoCacher::SetRendersPaused(bool e)
{
pause_renders_ = e;
if (!e) {
TryRender();
}
@@ -533,12 +589,14 @@ void PreviewAutoCacher::ValueHintChanged(const NodeInput &input)
void PreviewAutoCacher::TryRender()
{
delayed_requeue_timer_.stop();
if (!graph_update_queue_.isEmpty()) {
// Check if we have jobs running in other threads that shouldn't be interrupted right now
// NOTE: We don't check for downloads because, while they run in another thread, they don't
// require any access to the graph and therefore don't risk race conditions.
if (!audio_tasks_.isEmpty()
|| !video_tasks_.isEmpty()) {
if (!running_audio_tasks_.isEmpty()
|| !running_video_tasks_.isEmpty()) {
return;
}
@@ -546,173 +604,164 @@ void PreviewAutoCacher::TryRender()
ProcessUpdateQueue();
}
// Check for newly invalidated video and hash it
if (!invalidated_video_.isEmpty()) {
if (!copied_viewer_node_->GetConnectedTextureOutput()) {
queued_frame_iterator_.reset();
} else if (queued_frame_iterator_.HasNext()) {
queued_frame_iterator_.insert(invalidated_video_);
} else {
queued_frame_iterator_ = TimeRangeListFrameIterator(invalidated_video_, viewer_node_->GetVideoParams().frame_rate_as_time_base());
}
invalidated_video_.clear();
}
if (!invalidated_audio_.isEmpty()) {
// Add newly invalidated audio to iterator
audio_iterator_.insert(invalidated_audio_);
invalidated_audio_.clear();
}
if (single_frame_render_) {
// Make an explicit copy of the render ticket here - it seems that on some systems it can be set
// to NULL before we're done with it...
RenderTicketPtr t = single_frame_render_;
single_frame_render_ = nullptr;
RenderTicketWatcher *watcher = RenderFrame(t->property("time").value<rational>(),
// Check if already caching this
RenderTicketWatcher *watcher = RenderFrame(copied_viewer_node_->GetConnectedTextureOutput(),
t->property("time").value<rational>(),
nullptr,
t->property("dry").toBool());
video_immediate_passthroughs_[watcher].append(t);
}
// Completely arbitrary number. I don't know what's optimal for this yet.
const int max_tasks = 4;
if (!pause_renders_) {
// Completely arbitrary number. I don't know what's optimal for this yet.
const int max_tasks = 4;
// Handle video tasks
rational t;
while (video_tasks_.size() < max_tasks && queued_frame_iterator_.GetNext(&t)) {
RenderTicketWatcher* render_task = video_tasks_.key(t);
// Handle video tasks
while (!pending_video_jobs_.empty()) {
VideoJob &d = pending_video_jobs_.front();
// We want this hash, if we're not already rendering, start render now
if (!render_task) {
// Don't render any hash more than once
RenderFrame(t, viewer_node_->video_frame_cache(), false);
if (Node *copy = copy_map_.value(d.node)) {
// Queue next frames
rational t;
while (running_video_tasks_.size() < max_tasks && d.iterator.GetNext(&t)) {
RenderFrame(copy, t, d.cache, false);
emit SignalCacheProxyTaskProgress(double(d.iterator.frame_index()) / double(d.iterator.size()));
if (!d.iterator.HasNext()) {
emit StopCacheProxyTasks();
}
}
} else {
qCritical() << "Failed to find node copy for video job";
}
if (d.iterator.HasNext()) {
break;
} else {
pending_video_jobs_.pop_front();
}
}
emit SignalCacheProxyTaskProgress(double(queued_frame_iterator_.frame_index()) / double(queued_frame_iterator_.size()));
// Handle audio tasks
while (!pending_audio_jobs_.empty() && running_audio_tasks_.size() < max_tasks) {
AudioJob &d = pending_audio_jobs_.front();
if (!queued_frame_iterator_.HasNext()) {
emit StopCacheProxyTasks();
bool pop = true;
// Start job
if (Node *copy = copy_map_.value(d.node)) {
TimeRange &queued_range = d.range;
TimeRange use_range = queued_range;
if (dynamic_cast<AudioWaveformCache*>(d.cache)) {
rational new_out = std::min(use_range.in() + AudioVisualWaveform::kMinimumSampleRate.flipped(), use_range.out());
if (new_out != use_range.out()) {
use_range.set_out(new_out);
queued_range.set_in(new_out);
pop = false;
}
}
RenderAudio(copy, use_range, d.cache);
} else {
qCritical() << "Failed to find node copy for audio job";
}
if (pop) {
pending_audio_jobs_.pop_front();
}
}
}
// Handle audio tasks
while (!audio_iterator_.isEmpty() && audio_tasks_.size() < max_tasks && !pause_audio_) {
// Copy first range in list
TimeRange r = audio_iterator_.first();
// Limit to the minimum sample rate supported by AudioVisualWaveform - we use this value so that
// whatever chunk we render can be summed down to the smallest mipmap whole
r.set_out(qMin(r.out(), r.in() + AudioVisualWaveform::kMinimumSampleRate.flipped()));
// Start job
RenderAudio(r, true);
audio_iterator_.remove(r);
}
}
RenderTicketWatcher* PreviewAutoCacher::RenderFrame(Node *node, const rational& time, FrameHashCache *cache, bool dry)
RenderTicketWatcher* PreviewAutoCacher::RenderFrame(Node *node, const rational& time, PlaybackCache *cache, bool dry)
{
RenderTicketWatcher* watcher = new RenderTicketWatcher();
watcher->setProperty("job", QVariant::fromValue(last_update_time_));
watcher->setProperty("cache", Node::PtrToValue(cache));
watcher->setProperty("time", QVariant::fromValue(time));
connect(watcher, &RenderTicketWatcher::Finished, this, &PreviewAutoCacher::VideoRendered);
video_tasks_.insert(watcher, time);
watcher->SetTicket(RenderManager::instance()->RenderFrame(node,
copied_viewer_node_->GetVideoParams(),
copied_viewer_node_->GetAudioParams(),
copied_color_manager_,
time,
RenderMode::kOffline,
cache,
dry ? RenderManager::kNull : RenderManager::kTexture));
running_video_tasks_.append(watcher);
RenderManager::RenderVideoParams rvp(node,
copied_viewer_node_->GetVideoParams(),
copied_viewer_node_->GetAudioParams(),
time,
copied_color_manager_,
RenderMode::kOffline);
if (FrameHashCache *frame_cache = dynamic_cast<FrameHashCache *>(cache)) {
if (ThumbnailCache *wave_cache = dynamic_cast<ThumbnailCache *>(cache)) {
rvp.video_params.set_divider(VideoParams::GetDividerForTargetResolution(rvp.video_params.width(), rvp.video_params.height(), 160, 120));
rvp.force_color_output = display_color_processor_;
rvp.force_format = VideoParams::kFormatUnsigned8;
} else {
frame_cache->SetTimebase(viewer_node_->GetVideoParams().frame_rate_as_time_base());
}
rvp.AddCache(frame_cache);
}
rvp.return_type = dry ? RenderManager::kNull : RenderManager::kTexture;
// Allow using cached images for this render job
rvp.use_cache = true;
watcher->SetTicket(RenderManager::instance()->RenderFrame(rvp));
return watcher;
}
RenderTicketPtr PreviewAutoCacher::RenderAudio(Node *node, const TimeRange &r, bool generate_waveforms)
RenderTicketPtr PreviewAutoCacher::RenderAudio(Node *node, const TimeRange &r, PlaybackCache *cache)
{
RenderTicketWatcher* watcher = new RenderTicketWatcher();
watcher->setProperty("job", QVariant::fromValue(last_update_time_));
watcher->setProperty("node", Node::PtrToValue(node));
watcher->setProperty("cache", Node::PtrToValue(cache));
watcher->setProperty("time", QVariant::fromValue(r));
connect(watcher, &RenderTicketWatcher::Finished, this, &PreviewAutoCacher::AudioRendered);
audio_tasks_.insert(watcher, r);
running_audio_tasks_.append(watcher);
RenderTicketPtr ticket = RenderManager::instance()->RenderAudio(node, r, copied_viewer_node_->GetAudioParams(), RenderMode::kOffline, generate_waveforms);
RenderManager::RenderAudioParams rap(node,
r,
copied_viewer_node_->GetAudioParams(),
RenderMode::kOffline);
rap.generate_waveforms = dynamic_cast<AudioWaveformCache*>(cache);
rap.clamp = false;
RenderTicketPtr ticket = RenderManager::instance()->RenderAudio(rap);
watcher->SetTicket(ticket);
return ticket;
}
void PreviewAutoCacher::RequeueFrames()
{
delayed_requeue_timer_.stop();
if (viewer_node_
&& (viewer_node_->video_frame_cache()->IsEnabled() || use_custom_range_)
&& viewer_node_->video_frame_cache()->HasInvalidatedRanges(viewer_node_->GetVideoLength())
&& !IsRenderingCustomRange()) {
TimeRange using_range = use_custom_range_ ? custom_autocache_range_ : cache_range_;
TimeRangeList invalidated = viewer_node_->video_frame_cache()->GetInvalidatedRanges(using_range);
queued_frame_iterator_ = TimeRangeListFrameIterator(invalidated, viewer_node_->video_frame_cache()->GetTimebase());
queued_frame_iterator_.SetCustomRange(use_custom_range_);
emit StopCacheProxyTasks();
if (use_custom_range_) {
CustomCacheTask *cct = new CustomCacheTask(viewer_node_->GetLabelOrName());
connect(this, &PreviewAutoCacher::StopCacheProxyTasks, cct, &CustomCacheTask::Finish);
connect(this, &PreviewAutoCacher::SignalCacheProxyTaskProgress, cct, &CustomCacheTask::ProgressChanged);
connect(cct, &CustomCacheTask::Cancelled, this, &PreviewAutoCacher::CacheProxyTaskCancelled);
TaskManager::instance()->AddTask(cct);
}
use_custom_range_ = false;
TryRender();
}
}
void PreviewAutoCacher::ConformFinished()
{
// Got an audio conform, requeue all the audio currently needing a conform
last_conform_task_.Acquire();
if (!audio_needing_conform_.isEmpty()) {
// This list should be empty if there was a viewer switch
foreach (const TimeRange &range, audio_needing_conform_) {
viewer_node_->audio_playback_cache()->Invalidate(range);
for (auto it=audio_cache_data_.begin(); it!=audio_cache_data_.end(); it++) {
foreach (const TimeRange &range, it.value().needs_conform) {
it.key()->Request(range);
}
audio_needing_conform_.clear();
}
}
void PreviewAutoCacher::VideoAutoCacheEnableChanged(bool e)
{
if (e) {
VideoInvalidatedList(viewer_node_->video_frame_cache()->GetInvalidatedRanges(viewer_node_->GetVideoLength()));
} else {
CancelVideoTasks();
queued_frame_iterator_.reset();
}
}
void PreviewAutoCacher::AudioAutoCacheEnableChanged(bool e)
{
if (e) {
AudioInvalidatedList(viewer_node_->audio_playback_cache()->GetInvalidatedRanges(viewer_node_->GetAudioLength()));
} else {
CancelAudioTasks();
audio_iterator_.clear();
it.value().needs_conform.clear();
}
}
void PreviewAutoCacher::CacheProxyTaskCancelled()
{
queued_frame_iterator_.reset();
RequeueFrames();
pending_video_jobs_.clear();
TryRender();
}
void PreviewAutoCacher::ForceCacheRange(const TimeRange &range)
@@ -721,7 +770,7 @@ void PreviewAutoCacher::ForceCacheRange(const TimeRange &range)
custom_autocache_range_ = range;
// Re-hash these frames and start rendering
StartCachingVideoRange(range);
StartCachingVideoRange(viewer_node_->video_frame_cache(), range);
}
void PreviewAutoCacher::SetViewerNode(ViewerOutput *viewer_node)
@@ -738,36 +787,25 @@ void PreviewAutoCacher::SetViewerNode(ViewerOutput *viewer_node)
delayed_requeue_timer_.stop();
// Handle video rendering tasks
if (!video_tasks_.isEmpty()) {
if (!running_video_tasks_.isEmpty()) {
// Cancel any video tasks and wait for them to finish
CancelVideoTasks(true);
video_tasks_.clear();
running_video_tasks_.clear();
}
// Handle audio rendering tasks
if (!audio_tasks_.isEmpty()) {
if (!running_audio_tasks_.isEmpty()) {
// Cancel any audio tasks and wait for them to finish
CancelAudioTasks(true);
audio_tasks_.clear();
running_audio_tasks_.clear();
}
// Clear iterators
queued_frame_iterator_.reset();
audio_iterator_.clear();
// Clear any invalidated ranges
invalidated_video_.clear();
invalidated_audio_.clear();
// Clear any single frame render that might be queued
CancelQueuedSingleFrameRender();
// Not interested in video passthroughs anymore
video_immediate_passthroughs_.clear();
// Not interested in audio conforming anymore
audio_needing_conform_.clear();
// Disconnect from all node cache's
for (auto it=copy_map_.cbegin(); it!=copy_map_.cend(); it++) {
DisconnectFromNodeCache(it.key());
@@ -779,8 +817,10 @@ void PreviewAutoCacher::SetViewerNode(ViewerOutput *viewer_node)
copy_map_.clear();
copied_viewer_node_ = nullptr;
graph_update_queue_.clear();
video_job_tracker_.clear();
audio_job_tracker_.clear();
// Ensure all cache data is cleared
video_cache_data_.clear();
audio_cache_data_.clear();
// Disconnect signals for future node additions/deletions
NodeGraph* graph = viewer_node_->parent();
@@ -799,6 +839,8 @@ void PreviewAutoCacher::SetViewerNode(ViewerOutput *viewer_node)
// Copy graph
NodeGraph* graph = viewer_node_->parent();
SetRendersPaused(true);
// Add all nodes
for (int i=0; i<copied_project_.nodes().size(); i++) {
InsertIntoCopyMap(graph->nodes().at(i), copied_project_.nodes().at(i));
@@ -806,6 +848,9 @@ void PreviewAutoCacher::SetViewerNode(ViewerOutput *viewer_node)
for (int i=copied_project_.nodes().size(); i<graph->nodes().size(); i++) {
AddNode(graph->nodes().at(i));
}
for (int i=0; i<graph->nodes().size(); i++) {
graph->nodes().at(i)->ConnectedToPreviewEvent();
}
// Find copied viewer node
copied_viewer_node_ = static_cast<ViewerOutput*>(copy_map_.value(viewer_node_));
@@ -830,9 +875,7 @@ void PreviewAutoCacher::SetViewerNode(ViewerOutput *viewer_node)
connect(graph, &NodeGraph::ValueChanged, this, &PreviewAutoCacher::ValueChanged, Qt::DirectConnection);
connect(graph, &NodeGraph::InputValueHintChanged, this, &PreviewAutoCacher::ValueHintChanged, Qt::DirectConnection);
// Copy invalidated ranges and start rendering if necessary
VideoInvalidatedList(viewer_node_->video_frame_cache()->GetInvalidatedRanges(viewer_node_->GetVideoLength()));
AudioInvalidatedList(viewer_node_->audio_playback_cache()->GetInvalidatedRanges(viewer_node_->GetAudioLength()));
SetRendersPaused(false);
}
}
+54 -42
View File
@@ -45,7 +45,7 @@ class PreviewAutoCacher : public QObject
{
Q_OBJECT
public:
PreviewAutoCacher();
PreviewAutoCacher(QObject *parent = nullptr);
virtual ~PreviewAutoCacher() override;
@@ -53,6 +53,8 @@ public:
RenderTicketPtr GetRangeOfAudio(TimeRange range);
void ClearSingleFrameRenders();
/**
* @brief Set the viewer node to auto-cache
*/
@@ -83,12 +85,15 @@ public:
void CancelVideoTasks(bool and_wait_for_them_to_finish = false);
void CancelAudioTasks(bool and_wait_for_them_to_finish = false);
bool IsRenderingCustomRange() const
{
return queued_frame_iterator_.IsCustomRange() && queued_frame_iterator_.HasNext();
}
bool IsRenderingCustomRange() const;
void SetAudioPaused(bool e);
void SetRendersPaused(bool e);
public slots:
void SetDisplayColorProcessor(ColorProcessorPtr processor)
{
display_color_processor_ = processor;
}
signals:
void StopCacheProxyTasks();
@@ -98,17 +103,9 @@ signals:
private:
void TryRender();
RenderTicketWatcher *RenderFrame(Node *node, const rational &time, FrameHashCache *cache, bool dry);
RenderTicketWatcher *RenderFrame(const rational &time, FrameHashCache *cache, bool dry)
{
return RenderFrame(copied_viewer_node_->GetConnectedTextureOutput(), time, cache, dry);
}
RenderTicketWatcher *RenderFrame(Node *node, const rational &time, PlaybackCache *cache, bool dry);
RenderTicketPtr RenderAudio(Node *node, const TimeRange &range, bool generate_waveforms);
RenderTicketPtr RenderAudio(const TimeRange &range, bool generate_waveforms)
{
return RenderAudio(copied_viewer_node_->GetConnectedSampleOutput(), range, generate_waveforms);
}
RenderTicketPtr RenderAudio(Node *node, const TimeRange &range, PlaybackCache *cache);
/**
* @brief Process all changes to internal NodeGraph copy
@@ -135,12 +132,12 @@ private:
void CancelQueuedSingleFrameRender();
void VideoInvalidatedList(const TimeRangeList &list);
void AudioInvalidatedList(const TimeRangeList &list);
void StartCachingRange(const TimeRange &range, TimeRangeList *range_list, RenderJobTracker *tracker);
void StartCachingVideoRange(const TimeRange &range);
void StartCachingAudioRange(const TimeRange &range);
void StartCachingVideoRange(PlaybackCache *cache, const TimeRange &range);
void StartCachingAudioRange(PlaybackCache *cache, const TimeRange &range);
void VideoInvalidatedFromNode(PlaybackCache *cache, const olive::TimeRange &range);
void AudioInvalidatedFromNode(PlaybackCache *cache, const olive::TimeRange &range);
class QueuedJob {
public:
@@ -175,15 +172,9 @@ private:
bool use_custom_range_;
TimeRange custom_autocache_range_;
TimeRangeList invalidated_video_;
TimeRangeList invalidated_audio_;
bool pause_audio_;
bool pause_renders_;
RenderTicketPtr single_frame_render_;
QMap<RenderTicketWatcher*, TimeRange> audio_tasks_;
QMap<RenderTicketWatcher*, rational> video_tasks_;
QMap<RenderTicketWatcher*, QVector<RenderTicketPtr> > video_immediate_passthroughs_;
JobTime graph_changed_time_;
@@ -191,28 +182,53 @@ private:
QTimer delayed_requeue_timer_;
TimeRangeList audio_needing_conform_;
JobTime last_conform_task_;
RenderJobTracker video_job_tracker_;
RenderJobTracker audio_job_tracker_;
QVector<RenderTicketWatcher*> running_video_tasks_;
QVector<RenderTicketWatcher*> running_audio_tasks_;
TimeRangeListFrameIterator queued_frame_iterator_;
TimeRangeList audio_iterator_;
struct VideoJob {
Node *node;
PlaybackCache *cache;
TimeRange range;
TimeRangeListFrameIterator iterator;
};
static const bool kRealTimeWaveformsEnabled;
struct VideoCacheData {
RenderJobTracker job_tracker;
};
struct AudioJob {
Node *node;
PlaybackCache *cache;
TimeRange range;
};
struct AudioCacheData {
RenderJobTracker job_tracker;
TimeRangeList needs_conform;
};
std::list<VideoJob> pending_video_jobs_;
std::list<AudioJob> pending_audio_jobs_;
QHash<PlaybackCache*, VideoCacheData> video_cache_data_;
QHash<PlaybackCache*, AudioCacheData> audio_cache_data_;
ColorProcessorPtr display_color_processor_;
private slots:
/**
* @brief Handler for when the NodeGraph reports a video change over a certain time range
*/
void VideoInvalidated(const olive::TimeRange &range);
void VideoInvalidatedFromCache(const olive::TimeRange &range);
/**
* @brief Handler for when the NodeGraph reports a audio change over a certain time range
*/
void AudioInvalidated(const olive::TimeRange &range);
void AudioInvalidatedFromCache(const olive::TimeRange &range);
void CancelForCache();
/**
* @brief Handler for when the RenderManager has returned rendered audio
@@ -239,14 +255,10 @@ private slots:
/**
* @brief Generic function called whenever the frames to render need to be (re)queued
*/
void RequeueFrames();
//void RequeueFrames();
void ConformFinished();
void VideoAutoCacheEnableChanged(bool e);
void AudioAutoCacheEnableChanged(bool e);
void CacheProxyTaskCancelled();
};
+2
View File
@@ -121,6 +121,8 @@ TexturePtr Renderer::InterlaceTexture(TexturePtr top, TexturePtr bottom, const V
QVariant Renderer::GetDefaultShader()
{
QMutexLocker locker(&color_cache_mutex_);
if (default_shader_.isNull()) {
default_shader_ = CreateNativeShader(ShaderCode(QString(), QString()));
}
+51 -73
View File
@@ -52,13 +52,10 @@ RenderManager::RenderManager(QObject *parent) :
}
if (context_) {
video_thread_ = new RenderThread(context_, decoder_cache_, shader_cache_, this);
dry_run_thread_ = new RenderThread(nullptr, decoder_cache_, shader_cache_, this);
audio_thread_ = new RenderThread(nullptr, decoder_cache_, shader_cache_, this);
video_thread_->start(QThread::IdlePriority);
dry_run_thread_->start(QThread::IdlePriority);
audio_thread_->start(QThread::IdlePriority);
video_thread_ = CreateThread(context_);
dry_run_thread_ = CreateThread();
audio_thread_ = CreateThread();
waveform_thread_ = CreateThread();
}
decoder_clear_timer_ = new QTimer(this);
@@ -73,72 +70,49 @@ RenderManager::~RenderManager()
delete shader_cache_;
delete decoder_cache_;
video_thread_->quit();
video_thread_->wait();
dry_run_thread_->quit();
dry_run_thread_->wait();
for (RenderThread *rt : render_threads_) {
rt->quit();
rt->wait();
}
context_->PostDestroy();
delete context_;
audio_thread_->quit();
audio_thread_->wait();
}
}
RenderTicketPtr RenderManager::RenderFrame(Node *node, const VideoParams &vparam, const AudioParams &param,
ColorManager* color_manager, const rational& time, RenderMode::Mode mode,
FrameHashCache* cache, ReturnType return_type)
RenderThread *RenderManager::CreateThread(Renderer *renderer)
{
return RenderFrame(node,
color_manager,
time,
mode,
vparam,
param,
QSize(0, 0),
QMatrix4x4(),
VideoParams::kFormatInvalid,
0,
nullptr,
cache,
return_type);
auto t = new RenderThread(renderer, decoder_cache_, shader_cache_, this);
render_threads_.push_back(t);
t->start(QThread::IdlePriority);
return t;
}
RenderTicketPtr RenderManager::RenderFrame(Node *node, ColorManager* color_manager,
const rational& time, RenderMode::Mode mode,
const VideoParams &video_params, const AudioParams &audio_params,
const QSize& force_size,
const QMatrix4x4& force_matrix, VideoParams::Format force_format,
int force_channel_count,
ColorProcessorPtr force_color_output,
FrameHashCache* cache, ReturnType return_type)
RenderTicketPtr RenderManager::RenderFrame(const RenderVideoParams &params)
{
// Create ticket
RenderTicketPtr ticket = std::make_shared<RenderTicket>();
ticket->setProperty("node", Node::PtrToValue(node));
ticket->setProperty("time", QVariant::fromValue(time));
ticket->setProperty("size", force_size);
ticket->setProperty("matrix", force_matrix);
ticket->setProperty("format", force_format);
ticket->setProperty("channelcount", force_channel_count);
ticket->setProperty("mode", mode);
ticket->setProperty("node", Node::PtrToValue(params.node));
ticket->setProperty("time", QVariant::fromValue(params.time));
ticket->setProperty("size", params.force_size);
ticket->setProperty("matrix", params.force_matrix);
ticket->setProperty("format", params.force_format);
ticket->setProperty("usecache", params.use_cache);
ticket->setProperty("channelcount", params.force_channel_count);
ticket->setProperty("mode", params.mode);
ticket->setProperty("type", kTypeVideo);
ticket->setProperty("colormanager", Node::PtrToValue(color_manager));
ticket->setProperty("coloroutput", QVariant::fromValue(force_color_output));
ticket->setProperty("vparam", QVariant::fromValue(video_params));
ticket->setProperty("aparam", QVariant::fromValue(audio_params));
ticket->setProperty("return", return_type);
ticket->setProperty("colormanager", Node::PtrToValue(params.color_manager));
ticket->setProperty("coloroutput", QVariant::fromValue(params.force_color_output));
Q_ASSERT(params.video_params.is_valid());
ticket->setProperty("vparam", QVariant::fromValue(params.video_params));
ticket->setProperty("aparam", QVariant::fromValue(params.audio_params));
ticket->setProperty("return", params.return_type);
ticket->setProperty("cache", params.cache_dir);
ticket->setProperty("cachetimebase", QVariant::fromValue(params.cache_timebase));
ticket->setProperty("cacheid", QVariant::fromValue(params.cache_id));
if (cache) {
ticket->setProperty("cache", cache->GetCacheDirectory());
ticket->setProperty("cachetimebase", QVariant::fromValue(cache->GetTimebase()));
ticket->setProperty("cacheuuid", QVariant::fromValue(cache->GetUuid()));
}
if (return_type == ReturnType::kNull) {
if (params.return_type == ReturnType::kNull) {
dry_run_thread_->AddTicket(ticket);
} else {
video_thread_->AddTicket(ticket);
@@ -147,34 +121,37 @@ RenderTicketPtr RenderManager::RenderFrame(Node *node, ColorManager* color_manag
return ticket;
}
RenderTicketPtr RenderManager::RenderAudio(Node *node, const TimeRange &r, const AudioParams &params, RenderMode::Mode mode, bool generate_waveforms)
RenderTicketPtr RenderManager::RenderAudio(const RenderAudioParams &params)
{
// Create ticket
RenderTicketPtr ticket = std::make_shared<RenderTicket>();
ticket->setProperty("node", Node::PtrToValue(node));
ticket->setProperty("time", QVariant::fromValue(r));
ticket->setProperty("node", Node::PtrToValue(params.node));
ticket->setProperty("time", QVariant::fromValue(params.range));
ticket->setProperty("type", kTypeAudio);
ticket->setProperty("mode", mode);
ticket->setProperty("enablewaveforms", generate_waveforms);
ticket->setProperty("aparam", QVariant::fromValue(params));
ticket->setProperty("enablewaveforms", params.generate_waveforms);
ticket->setProperty("clamp", params.clamp);
ticket->setProperty("aparam", QVariant::fromValue(params.audio_params));
ticket->setProperty("mode", params.mode);
audio_thread_->AddTicket(ticket);
if (params.generate_waveforms) {
waveform_thread_->AddTicket(ticket);
} else {
audio_thread_->AddTicket(ticket);
}
return ticket;
}
bool RenderManager::RemoveTicket(RenderTicketPtr ticket)
{
if (video_thread_->RemoveTicket(ticket)) {
return true;
} else if (audio_thread_->RemoveTicket(ticket)) {
return true;
} else if (dry_run_thread_->RemoveTicket(ticket)) {
return true;
} else {
return false;
for (RenderThread *rt : render_threads_) {
if (rt->RemoveTicket(ticket)) {
return true;
}
}
return false;
}
void RenderManager::SetAggressiveGarbageCollection(bool enabled)
@@ -222,6 +199,7 @@ RenderThread::RenderThread(Renderer *renderer, DecoderCache *decoder_cache, Shad
void RenderThread::AddTicket(RenderTicketPtr ticket)
{
QMutexLocker locker(&mutex_);
ticket->moveToThread(this);
queue_.push_back(ticket);
wait_.wakeOne();
}
+71 -12
View File
@@ -101,6 +101,51 @@ public:
kNull
};
struct RenderVideoParams {
RenderVideoParams(Node *n, const VideoParams &vparam, const AudioParams &aparam, const rational &t,
ColorManager *colorman, RenderMode::Mode m)
{
node = n;
video_params = vparam;
audio_params = aparam;
time = t;
color_manager = colorman;
use_cache = false;
return_type = kFrame;
force_format = VideoParams::kFormatInvalid;
force_color_output = nullptr;
force_size = QSize(0, 0);
force_channel_count = 0;
mode = m;
}
void AddCache(FrameHashCache *cache)
{
cache_dir = cache->GetCacheDirectory();
cache_timebase = cache->GetTimebase();
cache_id = cache->GetUuid().toString();
}
Node *node;
VideoParams video_params;
AudioParams audio_params;
rational time;
ColorManager *color_manager;
bool use_cache;
ReturnType return_type;
RenderMode::Mode mode;
QString cache_dir;
rational cache_timebase;
QString cache_id;
QSize force_size;
int force_channel_count;
QMatrix4x4 force_matrix;
VideoParams::Format force_format;
ColorProcessorPtr force_color_output;
};
static const rational kDryRunInterval;
/**
@@ -111,17 +156,26 @@ public:
*
* This function is thread-safe.
*/
RenderTicketPtr RenderFrame(Node *node, const VideoParams &vparam, const AudioParams &param, ColorManager* color_manager,
const rational& time, RenderMode::Mode mode,
FrameHashCache* cache = nullptr, ReturnType return_type = kFrame);
RenderTicketPtr RenderFrame(Node *node, ColorManager* color_manager,
const rational& time, RenderMode::Mode mode,
const VideoParams& video_params, const AudioParams& audio_params,
const QSize& force_size,
const QMatrix4x4& force_matrix, VideoParams::Format force_format,
int force_channel_count,
ColorProcessorPtr force_color_output,
FrameHashCache* cache = nullptr, ReturnType return_type = kFrame);
RenderTicketPtr RenderFrame(const RenderVideoParams &params);
struct RenderAudioParams {
RenderAudioParams(Node *n, const TimeRange &time, const AudioParams &aparam, RenderMode::Mode m)
{
node = n;
range = time;
audio_params = aparam;
generate_waveforms = false;
clamp = true;
mode = m;
}
Node *node;
TimeRange range;
AudioParams audio_params;
bool generate_waveforms;
bool clamp;
RenderMode::Mode mode;
};
/**
* @brief Asynchronously generate a chunk of audio
@@ -130,7 +184,7 @@ public:
*
* This function is thread-safe.
*/
RenderTicketPtr RenderAudio(Node *viewer, const TimeRange& r, const AudioParams& params, RenderMode::Mode mode, bool generate_waveforms);
RenderTicketPtr RenderAudio(const RenderAudioParams &params);
bool RemoveTicket(RenderTicketPtr ticket);
@@ -154,6 +208,8 @@ private:
virtual ~RenderManager() override;
RenderThread *CreateThread(Renderer *renderer = nullptr);
static RenderManager* instance_;
Renderer* context_;
@@ -174,6 +230,9 @@ private:
RenderThread *video_thread_;
RenderThread *dry_run_thread_;
RenderThread *audio_thread_;
RenderThread *waveform_thread_;
std::list<RenderThread *> render_threads_;
private slots:
void ClearOldDecoders();
+60 -56
View File
@@ -95,32 +95,32 @@ FramePtr RenderProcessor::GenerateFrame(TexturePtr texture, const rational& time
ColorProcessorPtr output_color_transform = ticket_->property("coloroutput").value<ColorProcessorPtr>();
const VideoParams& tex_params = texture->params();
if (output_color_transform) {
TexturePtr transform_tex = render_ctx_->CreateTexture(tex_params);
ColorTransformJob job;
job.SetColorProcessor(output_color_transform);
job.SetInputTexture(texture);
job.SetInputAlphaAssociation(OLIVE_CONFIG("ReassocLinToNonLin").toBool() ? kAlphaAssociated : kAlphaNone);
render_ctx_->BlitColorManaged(job, transform_tex.get());
texture = transform_tex;
}
if (tex_params.effective_width() != frame_params.effective_width()
|| tex_params.effective_height() != frame_params.effective_height()
|| tex_params.format() != frame_params.format()
|| output_color_transform) {
|| tex_params.format() != frame_params.format()) {
TexturePtr blit_tex = render_ctx_->CreateTexture(frame_params);
QMatrix4x4 matrix = ticket_->property("matrix").value<QMatrix4x4>();
if (output_color_transform) {
// Yes color transform, blit color managed
ColorTransformJob job;
// No color transform, just blit
ShaderJob job;
job.Insert(QStringLiteral("ove_maintex"), NodeValue(NodeValue::kTexture, QVariant::fromValue(texture)));
job.Insert(QStringLiteral("ove_mvpmat"), NodeValue(NodeValue::kMatrix, matrix));
job.SetColorProcessor(output_color_transform);
job.SetInputTexture(texture);
job.SetInputAlphaAssociation(OLIVE_CONFIG("ReassocLinToNonLin").toBool() ? kAlphaAssociated : kAlphaNone);
job.SetTransformMatrix(matrix);
render_ctx_->BlitColorManaged(job, blit_tex.get());
} else {
// No color transform, just blit
ShaderJob job;
job.Insert(QStringLiteral("ove_maintex"), NodeValue(NodeValue::kTexture, QVariant::fromValue(texture)));
job.Insert(QStringLiteral("ove_mvpmat"), NodeValue(NodeValue::kMatrix, matrix));
render_ctx_->BlitToTexture(render_ctx_->GetDefaultShader(), job, blit_tex.get());
}
render_ctx_->BlitToTexture(render_ctx_->GetDefaultShader(), job, blit_tex.get());
// Replace texture that we're going to download in the next step
texture = blit_tex;
@@ -144,6 +144,11 @@ void RenderProcessor::Run()
SetCacheVideoParams(ticket_->property("vparam").value<VideoParams>());
SetCacheAudioParams(ticket_->property("aparam").value<AudioParams>());
if (IsCancelled()) {
ticket_->Finish();
return;
}
switch (type) {
case RenderManager::kTypeVideo:
{
@@ -176,10 +181,9 @@ void RenderProcessor::Run()
// is actually "complete
ticket_->Finish();
} else {
RenderManager::ReturnType return_type = RenderManager::ReturnType(ticket_->property("return").toInt());
FramePtr frame;
QString cache = ticket_->property("cache").toString();
RenderManager::ReturnType return_type = RenderManager::ReturnType(ticket_->property("return").toInt());
if (return_type == RenderManager::kFrame || !cache.isEmpty()) {
// Convert to CPU frame
@@ -188,7 +192,7 @@ void RenderProcessor::Run()
// Save to cache if requested
if (!cache.isEmpty()) {
rational timebase = ticket_->property("cachetimebase").value<rational>();
QUuid uuid = ticket_->property("cacheuuid").value<QUuid>();
QUuid uuid = ticket_->property("cacheid").value<QUuid>();
bool cache_result = FrameHashCache::SaveCacheFrame(cache, uuid, time, timebase, frame);
ticket_->setProperty("cached", cache_result);
}
@@ -226,9 +230,11 @@ void RenderProcessor::Run()
SampleBuffer samples = sample_val.toSamples();
if (samples.is_allocated()) {
samples.clamp();
if (ticket_->property("clamp").toBool() && !IsCancelled()) {
samples.clamp();
}
if (ticket_->property("enablewaveforms").toBool()) {
if (ticket_->property("enablewaveforms").toBool() && !IsCancelled()) {
AudioVisualWaveform vis;
vis.set_channel_count(samples.audio_params().channel_count());
vis.OverwriteSamples(samples, samples.audio_params().sample_rate());
@@ -262,14 +268,18 @@ DecoderPtr RenderProcessor::ResolveDecoderFromInput(const QString& decoder_id, c
qint64 file_last_modified = QFileInfo(stream.filename()).lastModified().toMSecsSinceEpoch();
if (!decoder.decoder || decoder.last_modified != file_last_modified) {
DecoderPtr dec = nullptr;
if (decoder.decoder && decoder.last_modified == file_last_modified) {
dec = decoder.decoder;
} else {
// No decoder
decoder.decoder = Decoder::CreateFromID(decoder_id);
decoder.decoder = dec = Decoder::CreateFromID(decoder_id);
decoder.last_modified = file_last_modified;
decoder_cache_->insert(stream, decoder);
locker.unlock();
if (!decoder.decoder->Open(stream)) {
if (!dec->Open(stream)) {
qWarning() << "Failed to open decoder for" << stream.filename()
<< "::" << stream.stream();
return nullptr;
@@ -281,7 +291,7 @@ DecoderPtr RenderProcessor::ResolveDecoderFromInput(const QString& decoder_id, c
}
}
return decoder.decoder;
return dec;
}
void RenderProcessor::Process(RenderTicketPtr ticket, Renderer *render_ctx, DecoderCache *decoder_cache, ShaderCache *shader_cache)
@@ -310,8 +320,8 @@ NodeValueTable RenderProcessor::GenerateBlockTable(const Track *track, const Tim
TimeRange range_for_block(qMax(b->in(), range.in()),
qMin(b->out(), range.out()));
int destination_offset = audio_params.time_to_samples(range_for_block.in() - range.in());
int max_dest_sz = audio_params.time_to_samples(range_for_block.length());
qint64 destination_offset = audio_params.time_to_samples(range_for_block.in() - range.in());
qint64 max_dest_sz = audio_params.time_to_samples(range_for_block.length());
// Destination buffer
NodeValueTable table = GenerateTable(b, Track::TransformRangeForBlock(b, range_for_block));
@@ -375,7 +385,7 @@ NodeValueTable RenderProcessor::GenerateBlockTable(const Track *track, const Tim
}
}
int copy_length = qMin(max_dest_sz, samples_from_this_block.sample_count());
qint64 copy_length = qMin(max_dest_sz, qint64(samples_from_this_block.sample_count()));
// Copy samples into destination buffer
for (int i=0; i<samples_from_this_block.audio_params().channel_count(); i++) {
@@ -384,26 +394,6 @@ NodeValueTable RenderProcessor::GenerateBlockTable(const Track *track, const Tim
NodeValueTable::Merge({merged_table, table});
}
// Create block waveforms if requested
if (ticket_->property("enablewaveforms").toBool() && clip_cast) {
// Format information for use in the main thread
RenderedWaveform waveform_info;
waveform_info.block = clip_cast;
waveform_info.range = range_for_block - b->in();
if (!(waveform_info.silence = !samples_from_this_block.is_allocated())) {
// Generate a visual waveform from the samples acquired from this block
AudioVisualWaveform visual_waveform;
visual_waveform.set_channel_count(audio_params.channel_count());
visual_waveform.OverwriteSamples(samples_from_this_block, audio_params.sample_rate());
waveform_info.waveform = visual_waveform;
}
QVector<RenderedWaveform> waveform_list = ticket_->property("waveforms").value< QVector<RenderedWaveform> >();
waveform_list.append(waveform_info);
ticket_->setProperty("waveforms", QVariant::fromValue(waveform_list));
}
}
}
@@ -485,7 +475,7 @@ void RenderProcessor::ProcessVideoFootage(TexturePtr destination, const FootageJ
unmanaged_texture = decoder->RetrieveVideo(p);
if (unmanaged_texture) {
if (!IsCancelled() && unmanaged_texture) {
// We convert to our rendering pixel format, since that will always be float-based which
// is necessary for correct color conversion
ColorProcessorPtr processor = ColorProcessor::Create(color_manager,
@@ -576,9 +566,10 @@ void RenderProcessor::ProcessSamples(SampleBuffer &destination, const Node *node
// Update all non-sample and non-footage inputs
for (auto j=job.GetValues().constBegin(); j!=job.GetValues().constEnd(); j++) {
NodeValueTable value = ProcessInput(node, j.key(), TimeRange(this_sample_time, this_sample_time));
TimeRange r = TimeRange(this_sample_time, this_sample_time);
NodeValueTable value = ProcessInput(node, j.key(), r);
value_db.insert(j.key(), GenerateRowValue(node, j.key(), &value));
value_db.insert(j.key(), GenerateRowValue(node, j.key(), &value, r));
}
node->ProcessSamples(value_db,
@@ -613,9 +604,22 @@ void RenderProcessor::ProcessFrameGeneration(TexturePtr destination, const Node
destination->Upload(frame->data(), frame->linesize_pixels());
}
bool RenderProcessor::CanCacheFrames()
TexturePtr RenderProcessor::ProcessVideoCacheJob(const CacheJob &val)
{
return ticket_->property("type").value<RenderManager::TicketType>() == RenderManager::kTypeVideo;
FramePtr frame = FrameHashCache::LoadCacheFrame(val.GetFilename());
if (frame) {
TexturePtr tex = CreateTexture(frame->video_params());
if (tex) {
tex->Upload(frame->data(), frame->linesize_pixels());
return tex;
}
} else {
QStringList s = ticket_->property("badcache").toStringList();
s.append(val.GetFilename());
ticket_->setProperty("badcache", s);
}
return nullptr;
}
TexturePtr RenderProcessor::CreateTexture(const VideoParams &p)
+1 -1
View File
@@ -56,7 +56,7 @@ protected:
virtual void ProcessFrameGeneration(TexturePtr destination, const Node *node, const GenerateJob& job) override;
virtual bool CanCacheFrames() override;
virtual TexturePtr ProcessVideoCacheJob(const CacheJob &val) override;
virtual TexturePtr CreateTexture(const VideoParams &p) override;
+15
View File
@@ -243,6 +243,21 @@ QString VideoParams::GetFormatName(VideoParams::Format format)
return QCoreApplication::translate("VideoParams", "Unknown (0x%1)").arg(format, 0, 16);
}
int VideoParams::GetDividerForTargetResolution(int src_width, int src_height, int dst_width, int dst_height)
{
int divider = 0;
int test_width, test_height;
do {
divider++;
test_width = VideoParams::GetScaledDimension(src_width, divider);
test_height = VideoParams::GetScaledDimension(src_height, divider);
} while (test_width > dst_width || test_height > dst_height);
return divider;
}
void VideoParams::calculate_effective_size()
{
effective_width_ = GetScaledDimension(width(), divider_);
+2
View File
@@ -247,6 +247,8 @@ public:
static QString GetFormatName(Format format);
static int GetDividerForTargetResolution(int src_width, int src_height, int dst_width, int dst_height);
static const int kInternalChannelCount;
static const rational kPixelAspectSquare;