rewrote hashing

This commit is contained in:
itsmattkc
2021-09-20 21:14:15 -07:00
parent 8e195a0a9c
commit 0df7012c9e
15 changed files with 402 additions and 318 deletions
-1
View File
@@ -54,7 +54,6 @@ set(OLIVE_SOURCES
render/renderprocessor.cpp
render/renderprocessor.h
render/shadercode.h
render/stillimagecache.h
render/subtitleparams.cpp
render/subtitleparams.h
render/texture.cpp
+5
View File
@@ -72,6 +72,11 @@ public:
return interpolation_.value(id, Texture::kDefaultInterpolation);
}
const QHash<QString, Texture::Interpolation> &GetInterpolationMap() const
{
return interpolation_;
}
void SetInterpolation(const NodeInput& input, Texture::Interpolation interp)
{
interpolation_.insert(input.input(), interp);
+3 -1
View File
@@ -69,7 +69,9 @@ QVector<PreviewAutoCacher::HashData> PreviewAutoCacher::GenerateHashes(ViewerOut
const rational &time = times.at(i);
// See if hash already exists in disk cache
QByteArray hash = RenderManager::Hash(viewer->GetConnectedTextureOutput(), viewer->GetValueHintForInput(ViewerOutput::kTextureInput), viewer->GetVideoParams(), time);
QByteArray hash = RenderManager::Hash(viewer,
viewer->GetVideoParams(),
time);
// Check memory list since disk checking is slow
bool hash_exists = existing_hashes.contains(hash);
+8 -21
View File
@@ -26,6 +26,7 @@
#include "config/config.h"
#include "core.h"
#include "node/hashtraverser.h"
#include "render/opengl/openglrenderer.h"
#include "render/rendererthreadwrapper.h"
#include "renderprocessor.h"
@@ -53,7 +54,6 @@ RenderManager::RenderManager(QObject *parent) :
context_->Init();
context_->PostInit();
still_cache_ = new StillImageCache();
decoder_cache_ = new DecoderCache();
shader_cache_ = new ShaderCache();
default_shader_ = context_->CreateNativeShader(ShaderCode(QString(), QString()));
@@ -64,7 +64,6 @@ RenderManager::RenderManager(QObject *parent) :
} else {
qCritical() << "Tried to initialize unknown graphics backend";
context_ = nullptr;
still_cache_ = nullptr;
decoder_cache_ = nullptr;
}
}
@@ -76,7 +75,6 @@ RenderManager::~RenderManager()
delete shader_cache_;
delete decoder_cache_;
delete still_cache_;
context_->Destroy();
context_->PostDestroy();
@@ -104,24 +102,13 @@ void RenderManager::ClearOldDecoders()
QByteArray RenderManager::Hash(const Node *n, const Node::ValueHint &output, const VideoParams &params, const rational &time)
{
QCryptographicHash hasher(QCryptographicHash::Sha1);
HashTraverser hasher;
return hasher.GetHash(n, output, params, TimeRange(time, time + params.frame_rate_as_time_base()));
}
// Embed video parameters into this hash
int width = params.effective_width();
int height = params.effective_height();
VideoParams::Format format = params.format();
VideoParams::Interlacing interlacing = params.interlacing();
hasher.addData(reinterpret_cast<const char*>(&width), sizeof(width));
hasher.addData(reinterpret_cast<const char*>(&height), sizeof(height));
hasher.addData(reinterpret_cast<const char*>(&format), sizeof(format));
hasher.addData(reinterpret_cast<const char*>(&interlacing), sizeof(interlacing));
if (n) {
Node::Hash(n, output, hasher, NodeTraverser::GenerateGlobals(params, time), params);
}
return hasher.result();
QByteArray RenderManager::Hash(ViewerOutput *viewer, const VideoParams &params, const rational &time)
{
return Hash(viewer->GetConnectedTextureOutput(), viewer->GetValueHintForInput(ViewerOutput::kTextureInput), params, time);
}
RenderTicketPtr RenderManager::RenderFrame(ViewerOutput *viewer, ColorManager* color_manager,
@@ -236,7 +223,7 @@ RenderTicketPtr RenderManager::SaveFrameToCache(FrameHashCache *cache, FramePtr
void RenderManager::RunTicket(RenderTicketPtr ticket) const
{
RenderProcessor::Process(ticket, context_, still_cache_, decoder_cache_, shader_cache_, default_shader_);
RenderProcessor::Process(ticket, context_, decoder_cache_, shader_cache_, default_shader_);
}
}
+2 -4
View File
@@ -31,7 +31,6 @@
#include "node/traverser.h"
#include "render/renderer.h"
#include "rendercache.h"
#include "stillimagecache.h"
#include "threading/threadpool.h"
namespace olive {
@@ -65,9 +64,10 @@ public:
}
/**
* @brief Generate a unique identifier for a certain node at a certain time
* @brief Generate a unique identifier for a certain node at a cconst Node *n, const Node::ValueHint &outputertain time
*/
static QByteArray Hash(const Node *n, const Node::ValueHint &output, const VideoParams &params, const rational &time);
static QByteArray Hash(ViewerOutput *viewer, const VideoParams &params, const rational &time);
/**
* @brief Asynchronously generate a frame at a given time
@@ -137,8 +137,6 @@ private:
Backend backend_;
StillImageCache* still_cache_;
DecoderCache* decoder_cache_;
ShaderCache* shader_cache_;
+76 -133
View File
@@ -32,10 +32,11 @@
namespace olive {
RenderProcessor::RenderProcessor(RenderTicketPtr ticket, Renderer *render_ctx, StillImageCache* still_image_cache, DecoderCache* decoder_cache, ShaderCache *shader_cache, QVariant default_shader) :
#define super NodeTraverser
RenderProcessor::RenderProcessor(RenderTicketPtr ticket, Renderer *render_ctx, DecoderCache* decoder_cache, ShaderCache *shader_cache, QVariant default_shader) :
ticket_(ticket),
render_ctx_(render_ctx),
still_image_cache_(still_image_cache),
decoder_cache_(decoder_cache),
shader_cache_(shader_cache),
default_shader_(default_shader)
@@ -47,8 +48,7 @@ TexturePtr RenderProcessor::GenerateTexture(const rational &time, const rational
ViewerOutput* viewer = Node::ValueToPtr<ViewerOutput>(ticket_->property("viewer"));
NodeValueTable table;
Node *texture_output = viewer->GetConnectedTextureOutput();
if (texture_output) {
if (Node *texture_output = viewer->GetConnectedTextureOutput()) {
table = GenerateTable(texture_output, viewer->GetValueHintForInput(ViewerOutput::kTextureInput), TimeRange(time, time + frame_length));
}
@@ -169,8 +169,7 @@ void RenderProcessor::Run()
TimeRange time = ticket_->property("time").value<TimeRange>();
NodeValueTable table;
Node *texture_output = viewer->GetConnectedSampleOutput();
if (texture_output) {
if (Node *texture_output = viewer->GetConnectedSampleOutput()) {
table = GenerateTable(texture_output, viewer->GetValueHintForInput(ViewerOutput::kSamplesInput),time);
}
@@ -231,9 +230,9 @@ DecoderPtr RenderProcessor::ResolveDecoderFromInput(const QString& decoder_id, c
return decoder.decoder;
}
void RenderProcessor::Process(RenderTicketPtr ticket, Renderer *render_ctx, StillImageCache *still_image_cache, DecoderCache *decoder_cache, ShaderCache *shader_cache, QVariant default_shader)
void RenderProcessor::Process(RenderTicketPtr ticket, Renderer *render_ctx, DecoderCache *decoder_cache, ShaderCache *shader_cache, QVariant default_shader)
{
RenderProcessor p(ticket, render_ctx, still_image_cache, decoder_cache, shader_cache, default_shader);
RenderProcessor p(ticket, render_ctx, decoder_cache, shader_cache, default_shader);
p.Run();
}
@@ -322,19 +321,17 @@ NodeValueTable RenderProcessor::GenerateBlockTable(const Track *track, const Tim
return merged_table;
} else {
return NodeTraverser::GenerateBlockTable(track, range);
return super::GenerateBlockTable(track, range);
}
}
QVariant RenderProcessor::ProcessVideoFootage(const FootageJob &stream, const rational &input_time)
TexturePtr RenderProcessor::ProcessVideoFootage(const FootageJob &stream, const rational &input_time)
{
if (ticket_->property("type").value<RenderManager::TicketType>() != RenderManager::kTypeVideo) {
// Video cannot contribute to audio, so we do nothing here
return QVariant();
return super::ProcessVideoFootage(stream, input_time);
}
TexturePtr value = nullptr;
// Check the still frame cache. On large frames such as high resolution still images, uploading
// and color managing them for every frame is a waste of time, so we implement a small cache here
// to optimize such a situation
@@ -360,132 +357,78 @@ QVariant RenderProcessor::ProcessVideoFootage(const FootageJob &stream, const ra
Decoder::CodecStream default_codec_stream(stream.filename(), stream_data.stream_index());
StillImageCache::EntryPtr want_entry = std::make_shared<StillImageCache::Entry>(
nullptr,
default_codec_stream,
ColorProcessor::GenerateID(color_manager, using_colorspace, color_manager->GetReferenceColorSpace()),
stream_data.premultiplied_alpha(),
footage_divider,
(stream_data.video_type() == VideoParams::kVideoTypeStill) ? 0 : input_time,
true);
QString decoder_id = stream.decoder();
bool found_existing = false;
DecoderPtr decoder = nullptr;
still_image_cache_->mutex()->lock();
foreach (StillImageCache::EntryPtr e, still_image_cache_->entries()) {
if (StillImageCache::CompareEntryMetadata(want_entry, e)) {
// Found an exact match of the texture we want in the cache. See if it's working or if it's
// ready.
want_entry = e;
found_existing = true;
while (want_entry->working) {
still_image_cache_->wait_cond()->wait(still_image_cache_->mutex());
}
value = want_entry->texture;
break;
}
}
if (value) {
// Found the texture, we can release the cache now
still_image_cache_->mutex()->unlock();
if (stream_data.video_type() == VideoParams::kVideoTypeVideo) {
decoder = ResolveDecoderFromInput(decoder_id, default_codec_stream);
} else {
// Wasn't in still image cache, so we'll have to retrieve it from the decoder
// Since image sequences involve multiple files, we don't engage the decoder cache
decoder = Decoder::CreateFromID(decoder_id);
// Let other processors know we're getting this texture (want_entry's `working` field is
// already set to true in the initializer above)
if (!found_existing) {
still_image_cache_->PushEntry(want_entry);
}
QString frame_filename;
still_image_cache_->mutex()->unlock();
QString decoder_id = stream.decoder();
DecoderPtr decoder = nullptr;
if (stream_data.video_type() == VideoParams::kVideoTypeVideo) {
decoder = ResolveDecoderFromInput(decoder_id, default_codec_stream);
if (stream_data.video_type() == VideoParams::kVideoTypeImageSequence) {
int64_t frame_number = stream_data.get_time_in_timebase_units(input_time);
frame_filename = Decoder::TransformImageSequenceFileName(stream.filename(), frame_number);
} else {
// Since image sequences involve multiple files, we don't engage the decoder cache
decoder = Decoder::CreateFromID(decoder_id);
QString frame_filename;
if (stream_data.video_type() == VideoParams::kVideoTypeImageSequence) {
int64_t frame_number = stream_data.get_time_in_timebase_units(input_time);
frame_filename = Decoder::TransformImageSequenceFileName(stream.filename(), frame_number);
} else {
frame_filename = stream.filename();
}
// Decoder will close automatically since it's a stream_ptr
decoder->Open(Decoder::CodecStream(frame_filename, stream_data.stream_index()));
frame_filename = stream.filename();
}
if (decoder) {
Decoder::RetrieveVideoParams p;
p.divider = footage_divider;
p.src_interlacing = stream_data.interlacing();
p.dst_interlacing = GetCacheVideoParams().interlacing();
// Decoder will close automatically since it's a stream_ptr
decoder->Open(Decoder::CodecStream(frame_filename, stream_data.stream_index()));
}
FramePtr frame = decoder->RetrieveVideo((stream_data.video_type() == VideoParams::kVideoTypeVideo) ? input_time : Decoder::kAnyTimecode, p);
if (decoder) {
Decoder::RetrieveVideoParams p;
p.divider = footage_divider;
p.src_interlacing = stream_data.interlacing();
p.dst_interlacing = GetCacheVideoParams().interlacing();
if (frame) {
// Return a texture from the derived class
TexturePtr unmanaged_texture = render_ctx_->CreateTexture(frame->video_params(),
frame->data(),
frame->linesize_pixels());
FramePtr frame = decoder->RetrieveVideo((stream_data.video_type() == VideoParams::kVideoTypeVideo) ? input_time : Decoder::kAnyTimecode, p);
// We convert to our rendering pixel format, since that will always be float-based which
// is necessary for correct color conversion
VideoParams managed_params = frame->video_params();
managed_params.set_format(render_params.format());
managed_params.set_pixel_aspect_ratio(stream_data.pixel_aspect_ratio());
managed_params.set_interlacing(stream_data.interlacing());
value = render_ctx_->CreateTexture(managed_params);
if (frame) {
// Return a texture from the derived class
TexturePtr unmanaged_texture = render_ctx_->CreateTexture(frame->video_params(),
frame->data(),
frame->linesize_pixels());
ColorProcessorPtr processor = ColorProcessor::Create(color_manager,
using_colorspace,
color_manager->GetReferenceColorSpace());
// We convert to our rendering pixel format, since that will always be float-based which
// is necessary for correct color conversion
VideoParams managed_params = frame->video_params();
managed_params.set_format(render_params.format());
managed_params.set_pixel_aspect_ratio(stream_data.pixel_aspect_ratio());
managed_params.set_interlacing(stream_data.interlacing());
TexturePtr value = render_ctx_->CreateTexture(managed_params);
Renderer::AlphaAssociated alpha_assoc;
if (stream_data.channel_count() != VideoParams::kRGBAChannelCount
|| stream_data.colorspace() == color_manager->GetReferenceColorSpace()) {
alpha_assoc = Renderer::kAlphaNone;
} else if (stream_data.premultiplied_alpha()) {
alpha_assoc = Renderer::kAlphaAssociated;
} else {
alpha_assoc = Renderer::kAlphaUnassociated;
}
ColorProcessorPtr processor = ColorProcessor::Create(color_manager,
using_colorspace,
color_manager->GetReferenceColorSpace());
render_ctx_->BlitColorManaged(processor, unmanaged_texture,
alpha_assoc,
value.get());
still_image_cache_->mutex()->lock();
// Put this into the image cache instead
want_entry->texture = value;
want_entry->working = false;
still_image_cache_->wait_cond()->wakeAll();
still_image_cache_->mutex()->unlock();
Renderer::AlphaAssociated alpha_assoc;
if (stream_data.channel_count() != VideoParams::kRGBAChannelCount
|| stream_data.colorspace() == color_manager->GetReferenceColorSpace()) {
alpha_assoc = Renderer::kAlphaNone;
} else if (stream_data.premultiplied_alpha()) {
alpha_assoc = Renderer::kAlphaAssociated;
} else {
alpha_assoc = Renderer::kAlphaUnassociated;
}
render_ctx_->BlitColorManaged(processor, unmanaged_texture,
alpha_assoc,
value.get());
return value;
}
}
return QVariant::fromValue(value);
return super::ProcessVideoFootage(stream, input_time);
}
QVariant RenderProcessor::ProcessAudioFootage(const FootageJob &stream, const TimeRange &input_time)
SampleBufferPtr RenderProcessor::ProcessAudioFootage(const FootageJob &stream, const TimeRange &input_time)
{
QVariant value;
DecoderPtr decoder = ResolveDecoderFromInput(stream.decoder(), Decoder::CodecStream(stream.filename(), stream.audio_params().stream_index()));
if (decoder) {
@@ -497,16 +440,16 @@ QVariant RenderProcessor::ProcessAudioFootage(const FootageJob &stream, const Ti
static_cast<RenderMode::Mode>(ticket_->property("mode").toInt()));
if (status.status == Decoder::kOK && status.samples) {
value = QVariant::fromValue(status.samples);
return status.samples;
} else if (status.status == Decoder::kWaitingForConform) {
ticket_->setProperty("incomplete", true);
}
}
return value;
return super::ProcessAudioFootage(stream, input_time);
}
QVariant RenderProcessor::ProcessShader(const Node *node, const TimeRange &range, const ShaderJob &job)
TexturePtr RenderProcessor::ProcessShader(const Node *node, const TimeRange &range, const ShaderJob &job)
{
Q_UNUSED(range)
@@ -522,7 +465,7 @@ QVariant RenderProcessor::ProcessShader(const Node *node, const TimeRange &range
if (shader.isNull()) {
// Couldn't find or build the shader required
return QVariant();
return super::ProcessShader(node, range, job);
}
}
@@ -535,13 +478,13 @@ QVariant RenderProcessor::ProcessShader(const Node *node, const TimeRange &range
// Run shader
render_ctx_->BlitToTexture(shader, job, destination.get());
return QVariant::fromValue(destination);
return destination;
}
QVariant RenderProcessor::ProcessSamples(const Node *node, const TimeRange &range, const SampleJob &job)
SampleBufferPtr RenderProcessor::ProcessSamples(const Node *node, const TimeRange &range, const SampleJob &job)
{
if (!job.samples() || !job.samples()->is_allocated()) {
return QVariant();
super::ProcessSamples(node, range, job);
}
SampleBufferPtr output_buffer = SampleBuffer::CreateAllocated(job.samples()->audio_params(), job.samples()->sample_count());
@@ -568,10 +511,10 @@ QVariant RenderProcessor::ProcessSamples(const Node *node, const TimeRange &rang
i);
}
return QVariant::fromValue(output_buffer);
return output_buffer;
}
QVariant RenderProcessor::ProcessFrameGeneration(const Node *node, const GenerateJob &job)
TexturePtr RenderProcessor::ProcessFrameGeneration(const Node *node, const GenerateJob &job)
{
FramePtr frame = Frame::Create();
@@ -586,7 +529,7 @@ QVariant RenderProcessor::ProcessFrameGeneration(const Node *node, const Generat
frame->data(),
frame->linesize_pixels());
return QVariant::fromValue(texture);
return texture;
}
bool RenderProcessor::CanCacheFrames()
@@ -594,24 +537,24 @@ bool RenderProcessor::CanCacheFrames()
return ticket_->property("type").value<RenderManager::TicketType>() == RenderManager::kTypeVideo;
}
QVariant RenderProcessor::GetCachedTexture(const QByteArray& hash)
TexturePtr RenderProcessor::GetCachedTexture(const QByteArray& hash)
{
QString cache_dir = ticket_->property("cache").toString();
if (cache_dir.isEmpty()) {
return QVariant();
return nullptr;
}
FramePtr f = FrameHashCache::LoadCacheFrame(cache_dir, hash);
if (f) {
TexturePtr texture = render_ctx_->CreateTexture(f->video_params(), f->data(), f->linesize_pixels());
return QVariant::fromValue(texture);
return texture;
}
return QVariant();
return nullptr;
}
void RenderProcessor::SaveCachedTexture(const QByteArray &hash, const QVariant &tex_var)
void RenderProcessor::SaveCachedTexture(const QByteArray &hash, TexturePtr tex_var)
{
// FIXME: Temporarily disabled because I don't know how to ensure that the frame saved here is
// not the main frame. If it is, it'll be saved twice which will waste a lot of cycles.
+9 -12
View File
@@ -25,7 +25,6 @@
#include "node/traverser.h"
#include "render/renderer.h"
#include "rendercache.h"
#include "stillimagecache.h"
#include "threading/threadticket.h"
namespace olive {
@@ -33,7 +32,7 @@ namespace olive {
class RenderProcessor : public NodeTraverser
{
public:
static void Process(RenderTicketPtr ticket, Renderer* render_ctx, StillImageCache* still_image_cache, DecoderCache* decoder_cache, ShaderCache* shader_cache, QVariant default_shader);
static void Process(RenderTicketPtr ticket, Renderer* render_ctx, DecoderCache* decoder_cache, ShaderCache* shader_cache, QVariant default_shader);
struct RenderedWaveform {
const ClipBlock* block;
@@ -45,24 +44,24 @@ public:
protected:
virtual NodeValueTable GenerateBlockTable(const Track *track, const TimeRange &range) override;
virtual QVariant ProcessVideoFootage(const FootageJob &stream, const rational &input_time) override;
virtual TexturePtr ProcessVideoFootage(const FootageJob &stream, const rational &input_time) override;
virtual QVariant ProcessAudioFootage(const FootageJob &stream, const TimeRange &input_time) override;
virtual SampleBufferPtr ProcessAudioFootage(const FootageJob &stream, const TimeRange &input_time) override;
virtual QVariant ProcessShader(const Node *node, const TimeRange &range, const ShaderJob& job) override;
virtual TexturePtr ProcessShader(const Node *node, const TimeRange &range, const ShaderJob& job) override;
virtual QVariant ProcessSamples(const Node *node, const TimeRange &range, const SampleJob &job) override;
virtual SampleBufferPtr ProcessSamples(const Node *node, const TimeRange &range, const SampleJob &job) override;
virtual QVariant ProcessFrameGeneration(const Node *node, const GenerateJob& job) override;
virtual TexturePtr ProcessFrameGeneration(const Node *node, const GenerateJob& job) override;
virtual bool CanCacheFrames() override;
virtual QVariant GetCachedTexture(const QByteArray &hash) override;
virtual TexturePtr GetCachedTexture(const QByteArray &hash) override;
virtual void SaveCachedTexture(const QByteArray& hash, const QVariant& texture) override;
virtual void SaveCachedTexture(const QByteArray& hash, TexturePtr texture) override;
private:
RenderProcessor(RenderTicketPtr ticket, Renderer* render_ctx, StillImageCache* still_image_cache, DecoderCache* decoder_cache, ShaderCache* shader_cache, QVariant default_shader);
RenderProcessor(RenderTicketPtr ticket, Renderer* render_ctx, DecoderCache* decoder_cache, ShaderCache* shader_cache, QVariant default_shader);
TexturePtr GenerateTexture(const rational& time, const rational& frame_length);
@@ -76,8 +75,6 @@ private:
Renderer* render_ctx_;
StillImageCache* still_image_cache_;
DecoderCache* decoder_cache_;
ShaderCache* shader_cache_;
-84
View File
@@ -1,84 +0,0 @@
#ifndef STILLIMAGECACHE_H
#define STILLIMAGECACHE_H
#include <QHash>
#include <QWaitCondition>
#include "codec/decoder.h"
#include "common/rational.h"
#include "node/project/footage/footage.h"
#include "render/texture.h"
namespace olive {
class StillImageCache
{
public:
struct Entry {
Entry(TexturePtr t, const Decoder::CodecStream& s, const QString& cs, bool a, int d, const rational& i, bool w)
{
texture = t;
stream = s;
colorspace = cs;
alpha_is_associated = a;
divider = d;
time = i;
working = w;
}
TexturePtr texture;
Decoder::CodecStream stream;
QString colorspace;
bool alpha_is_associated;
int divider;
rational time;
bool working;
};
using EntryPtr = std::shared_ptr<Entry>;
QMutex* mutex()
{
return &mutex_;
}
QWaitCondition* wait_cond()
{
return &wait_cond_;
}
const QVector<EntryPtr>& entries() const
{
return entries_;
}
static bool CompareEntryMetadata(EntryPtr a, EntryPtr b)
{
return (a->stream == b->stream
&& a->colorspace == b->colorspace
&& a->alpha_is_associated == b->alpha_is_associated
&& a->divider == b->divider
&& a->time == b->time);
}
void PushEntry(EntryPtr e)
{
entries_.prepend(e);
if (entries_.size() > 8) {
entries_.removeLast();
}
}
private:
QMutex mutex_;
QWaitCondition wait_cond_;
QVector<EntryPtr> entries_;
};
}
#endif // STILLIMAGECACHE_H