base audio renderer classes created

This commit is contained in:
itsmattkc
2019-10-16 13:51:56 +11:00
parent fe2b0f2dc2
commit d6483916ac
33 changed files with 896 additions and 116 deletions
@@ -0,0 +1,28 @@
# Olive - Non-Linear Video Editor
# Copyright (C) 2019 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/>.
set(OLIVE_SOURCES
${OLIVE_SOURCES}
node/processor/audiorenderer/audiorenderer.h
node/processor/audiorenderer/audiorenderer.cpp
node/processor/audiorenderer/audiorendererparams.h
node/processor/audiorenderer/audiorendererparams.cpp
node/processor/audiorenderer/audiorendererthread.h
node/processor/audiorenderer/audiorendererthread.cpp
node/processor/audiorenderer/audiorendererthreadbase.h
node/processor/audiorenderer/audiorendererthreadbase.cpp
PARENT_SCOPE
)
@@ -0,0 +1,342 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 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 "audiorenderer.h"
#include <QApplication>
#include <QCryptographicHash>
#include <QDateTime>
#include <QDebug>
#include <QDir>
#include <QtMath>
#include "common/filefunctions.h"
#include "render/pixelservice.h"
AudioRendererProcessor::AudioRendererProcessor() :
started_(false),
caching_(false),
starting_(false)
{
texture_input_ = new NodeInput("tex_in");
texture_input_->add_data_input(NodeInput::kTexture);
AddParameter(texture_input_);
length_input_ = new NodeInput("length_in");
length_input_->add_data_input(NodeInput::kRational);
AddParameter(length_input_);
texture_output_ = new NodeOutput("tex_out");
texture_output_->set_data_type(NodeInput::kTexture);
AddParameter(texture_output_);
}
QString AudioRendererProcessor::Name()
{
return tr("Audio Renderer");
}
QString AudioRendererProcessor::Category()
{
return tr("Processor");
}
QString AudioRendererProcessor::Description()
{
return tr("A multi-threaded PCM audio renderer.");
}
QString AudioRendererProcessor::id()
{
return "org.olivevideoeditor.Olive.rendererneptune";
}
void AudioRendererProcessor::SetCacheName(const QString &s)
{
cache_name_ = s;
cache_time_ = QDateTime::currentMSecsSinceEpoch();
GenerateCacheIDInternal();
}
QVariant AudioRendererProcessor::Value(NodeOutput* output, const rational& time)
{
Q_UNUSED(output)
Q_UNUSED(time)
/*if (output == texture_output_) {
if (!texture_input_->IsConnected()) {
// Nothing is connected - nothing to show or render
return 0;
}
if (cache_id_.isEmpty()) {
qWarning() << "RendererProcessor has no cache ID";
return 0;
}
if (timebase_.isNull()) {
qWarning() << "RendererProcessor has no timebase";
return 0;
}
// Find frame in map
if (time_hash_map_.contains(time)) {
QString fn = CachePathName(time_hash_map_[time]);
if (QFileInfo::exists(fn)) {
auto in = OIIO::ImageInput::open(fn.toStdString());
if (in) {
in->read_image(PixelService::GetPixelFormatInfo(format_).oiio_desc, cache_frame_load_buffer_.data());
in->close();
master_texture_->Upload(cache_frame_load_buffer_.data());
return QVariant::fromValue(master_texture_);
} else {
qWarning() << "OIIO Error:" << OIIO::geterror().c_str();
}
}
}
}*/
return 0;
}
void AudioRendererProcessor::Release()
{
Stop();
}
void AudioRendererProcessor::InvalidateCache(const rational &start_range, const rational &end_range, NodeInput *from)
{
Q_UNUSED(from)
if (timebase_.isNull()) {
return;
}
//ClearCachedValuesInParameters(start_range, end_range);
texture_input_->ClearCachedValue();
length_input_->ClearCachedValue();
// Adjust range to min/max values
rational start_range_adj = qMax(rational(0), start_range);
rational end_range_adj = qMin(length_input()->get_value(0).value<rational>(), end_range);
qDebug() << "Cache invalidated between"
<< start_range_adj.toDouble()
<< "and"
<< end_range_adj.toDouble();
// Snap start_range to timebase
double start_range_dbl = start_range_adj.toDouble();
double start_range_numf = start_range_dbl * static_cast<double>(timebase_.denominator());
int64_t start_range_numround = qFloor(start_range_numf/static_cast<double>(timebase_.numerator())) * timebase_.numerator();
rational true_start_range(start_range_numround, timebase_.denominator());
for (rational r=true_start_range;r<=end_range_adj;r+=timebase_) {
// Try to order the queue from closest to the playhead to furthest
rational last_time = texture_output()->LastRequestedTime();
rational diff = r - last_time;
if (diff < 0) {
// FIXME: Hardcoded number
// If the number is before the playhead, we still prioritize its closeness but not nearly as much (5:1 in this
// example)
diff = qAbs(diff) * 5;
}
bool contains = false;
bool added = false;
QLinkedList<rational>::iterator insert_iterator;
for (QLinkedList<rational>::iterator i = cache_queue_.begin();i != cache_queue_.end();i++) {
rational compare = *i;
if (!added) {
rational compare_diff = compare - last_time;
if (compare_diff > diff) {
insert_iterator = i;
added = true;
}
}
if (compare == r) {
contains = true;
break;
}
}
if (!contains) {
if (added) {
cache_queue_.insert(insert_iterator, r);
} else {
cache_queue_.append(r);
}
}
}
CacheNext();
}
void AudioRendererProcessor::SetTimebase(const rational &timebase)
{
timebase_ = timebase;
timebase_dbl_ = timebase_.toDouble();
}
void AudioRendererProcessor::SetParameters(const int &sample_rate,
const uint64_t &channel_layout,
const olive::SampleFormat &format)
{
// Since we're changing parameters, all the existing threads are invalid and must be removed. They will start again
// next time this Node has to process anything.
Stop();
// Set new parameters
sample_rate_ = sample_rate;
channel_layout_ = channel_layout;
format_ = format;
// Regenerate the cache ID
GenerateCacheIDInternal();
}
void AudioRendererProcessor::Start()
{
if (started_) {
return;
}
int background_thread_count = QThread::idealThreadCount();
threads_.resize(background_thread_count);
for (int i=0;i<threads_.size();i++) {
threads_[i] = std::make_shared<AudioRendererThread>(sample_rate_, channel_layout_, format_);
threads_[i]->StartThread(QThread::LowPriority);
// Ensure this connection is "Queued" so that it always runs in this object's threaded rather than any of the
// other threads
connect(threads_.at(i).get(),
SIGNAL(RequestSibling(NodeDependency)),
this,
SLOT(ThreadRequestSibling(NodeDependency)),
Qt::QueuedConnection);
}
// Connect first thread (master thread) to the callback
connect(threads_.first().get(),
SIGNAL(CachedFrame(RenderTexturePtr, const rational&, const QByteArray&)),
this,
SLOT(ThreadCallback(RenderTexturePtr, const rational&, const QByteArray&)),
Qt::QueuedConnection);
connect(threads_.first().get(),
SIGNAL(FrameSkipped(const rational&, const QByteArray&)),
this,
SLOT(ThreadSkippedFrame(const rational&, const QByteArray&)),
Qt::QueuedConnection);
started_ = true;
}
void AudioRendererProcessor::Stop()
{
if (!started_) {
return;
}
started_ = false;
foreach (AudioRendererThreadPtr process_thread, threads_) {
process_thread->Cancel();
}
threads_.clear();
}
void AudioRendererProcessor::GenerateCacheIDInternal()
{
if (cache_name_.isEmpty() || sample_rate_ == 0 || channel_layout_ == 0) {
return;
}
// Generate an ID that is more or less guaranteed to be unique to this Sequence
QCryptographicHash hash(QCryptographicHash::Sha1);
hash.addData(cache_name_.toUtf8());
hash.addData(QString::number(cache_time_).toUtf8());
hash.addData(QString::number(sample_rate_).toUtf8());
hash.addData(QString::number(channel_layout_).toUtf8());
hash.addData(QString::number(format_).toUtf8());
QByteArray bytes = hash.result();
cache_id_ = bytes.toHex();
}
void AudioRendererProcessor::CacheNext()
{
if (cache_queue_.isEmpty() || !texture_input_->IsConnected() || caching_) {
return;
}
// Make sure cache has started
Start();
rational cache_frame = cache_queue_.takeFirst();
qDebug() << "Caching" << cache_frame.toDouble();
caching_ = true;
}
AudioRendererThreadBase *AudioRendererProcessor::CurrentThread()
{
return dynamic_cast<AudioRendererThreadBase*>(QThread::currentThread());
}
AudioRendererParams *AudioRendererProcessor::CurrentInstance()
{
AudioRendererThreadBase* thread = CurrentThread();
if (thread != nullptr) {
return thread->params();
}
return nullptr;
}
NodeInput *AudioRendererProcessor::texture_input()
{
return texture_input_;
}
NodeInput *AudioRendererProcessor::length_input()
{
return length_input_;
}
NodeOutput *AudioRendererProcessor::texture_output()
{
return texture_output_;
}
@@ -0,0 +1,177 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 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 AUDIORENDERER_H
#define AUDIORENDERER_H
#include <QLinkedList>
#include <QOpenGLTexture>
#include "node/node.h"
#include "render/pixelformat.h"
#include "render/rendermodes.h"
#include "audiorendererthread.h"
/**
* @brief A multithreaded PCM audio renderer
*/
class AudioRendererProcessor : public Node
{
Q_OBJECT
public:
/**
* @brief Renderer Constructor
*
* Constructing a Renderer object will not start any threads/backend on its own. Use Start() to do this and Stop()
* when the Renderer is about to be destroyed.
*/
AudioRendererProcessor();
virtual QString Name() override;
virtual QString Category() override;
virtual QString Description() override;
virtual QString id() override;
void SetCacheName(const QString& s);
virtual void Release() override;
virtual void InvalidateCache(const rational &start_range, const rational &end_range, NodeInput *from = nullptr) override;
void SetTimebase(const rational& timebase);
/**
* @brief Set parameters of the Renderer
*
* The Renderer owns the buffers that are used in the rendering process and this function sets the kind of buffers
* to use. The Renderer must be stopped when calling this function.
*
* @param width
*
* Buffer width
*
* @param height
*
* Buffer height
*
* @param format
*
* Buffer pixel format
*/
void SetParameters(const int& sample_rate,
const uint64_t& channel_layout,
const olive::SampleFormat& format);
/**
* @brief Return whether a frame is currently being cached
*/
bool IsCaching(const QByteArray& hash);
/**
* @brief Check if a frame is currently being cached, and if not reserve it
*/
bool TryCache(const QByteArray& hash);
/**
* @brief Return current instance of a RenderThread (or nullptr if there is none)
*
* This function attempts a dynamic_cast on QThread::currentThread() to RendererThread, which will return nullptr if
* the cast fails (e.g. if this function is called from the main thread rather than a RendererThread).
*/
static AudioRendererThreadBase* CurrentThread();
static AudioRendererParams* CurrentInstance();
NodeInput* texture_input();
NodeInput* length_input();
NodeOutput* texture_output();
protected:
virtual QVariant Value(NodeOutput* output, const rational& time) override;
private:
struct HashTimeMapping {
rational time;
QByteArray hash;
};
/**
* @brief Allocate and start the multithreaded backend
*/
void Start();
/**
* @brief Terminate and deallocate the multithreaded backend
*/
void Stop();
/**
* @brief Internal function for generating the cache ID
*/
void GenerateCacheIDInternal();
/**
* @brief Function called when there are frames in the queue to cache
*
* This function is NOT thread-safe and should only be called in the main thread.
*/
void CacheNext();
bool ShouldPushTexture(const rational &time);
/**
* @brief Internal list of RenderProcessThreads
*/
QVector<AudioRendererThreadPtr> threads_;
/**
* @brief Internal variable that contains whether the Renderer has started or not
*/
bool started_;
NodeInput* texture_input_;
NodeInput* length_input_;
NodeOutput* texture_output_;
rational timebase_;
double timebase_dbl_;
int sample_rate_;
uint64_t channel_layout_;
olive::SampleFormat format_;
QLinkedList<rational> cache_queue_;
QString cache_name_;
qint64 cache_time_;
QString cache_id_;
bool caching_;
bool starting_;
private slots:
};
#endif // AUDIORENDERER_H
@@ -0,0 +1,23 @@
#include "audiorendererparams.h"
AudioRendererParams::AudioRendererParams(const int &sample_rate, const uint64_t &channel_layout, const olive::SampleFormat &format) :
sample_rate_(sample_rate),
channel_layout_(channel_layout),
format_(format)
{
}
const int &AudioRendererParams::sample_rate()
{
return sample_rate_;
}
const uint64_t &AudioRendererParams::channel_layout()
{
return channel_layout_;
}
const olive::SampleFormat &AudioRendererParams::format()
{
return format_;
}
@@ -0,0 +1,28 @@
#ifndef AUDIORENDERERPARAMS_H
#define AUDIORENDERERPARAMS_H
#include <QtGlobal>
#include "audio/sampleformat.h"
class AudioRendererParams
{
public:
AudioRendererParams(const int& sample_rate, const uint64_t& channel_layout, const olive::SampleFormat& format);
const int& sample_rate();
const uint64_t& channel_layout();
const olive::SampleFormat& format();
private:
int sample_rate_;
uint64_t channel_layout_;
olive::SampleFormat format_;
};
#endif // AUDIORENDERERPARAMS_H
@@ -0,0 +1,23 @@
#include "audiorendererthread.h"
AudioRendererThread::AudioRendererThread(const int &sample_rate, const uint64_t &channel_layout, const olive::SampleFormat &format) :
AudioRendererThreadBase(sample_rate, channel_layout, format),
cancelled_(false)
{
}
void AudioRendererThread::Cancel()
{
cancelled_ = true;
mutex_.lock();
wait_cond_.wakeAll();
mutex_.unlock();
wait();
}
void AudioRendererThread::ProcessLoop()
{
}
@@ -0,0 +1,26 @@
#ifndef AUDIORENDERERTHREAD_H
#define AUDIORENDERERTHREAD_H
#include "audiorendererthreadbase.h"
class AudioRendererThread : public AudioRendererThreadBase
{
Q_OBJECT
public:
AudioRendererThread(const int& sample_rate,
const uint64_t& channel_layout,
const olive::SampleFormat& format);
public slots:
virtual void Cancel() override;
protected:
virtual void ProcessLoop() override;
private:
bool cancelled_;
};
using AudioRendererThreadPtr = std::shared_ptr<AudioRendererThread>;
#endif // AUDIORENDERERTHREAD_H
@@ -0,0 +1,69 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 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 "audiorendererthreadbase.h"
#include <QDebug>
AudioRendererThreadBase::AudioRendererThreadBase(const int &sample_rate, const uint64_t &channel_layout, const olive::SampleFormat &format) :
audio_params_(sample_rate, channel_layout, format)
{
}
AudioRendererParams *AudioRendererThreadBase::params()
{
return &audio_params_;
}
void AudioRendererThreadBase::run()
{
// Lock mutex for main loop
mutex_.lock();
// Signal that main thread can continue now
WakeCaller();
// Main loop (use Cancel() to exit it)
ProcessLoop();
// Unlock mutex before exiting
mutex_.unlock();
}
void AudioRendererThreadBase::WakeCaller()
{
// Signal that main thread can continue now
caller_mutex_.lock();
wait_cond_.wakeAll();
caller_mutex_.unlock();
}
void AudioRendererThreadBase::StartThread(QThread::Priority priority)
{
caller_mutex_.lock();
// Start the thread
QThread::start(priority);
// Wait for thread to finish completion
wait_cond_.wait(&caller_mutex_);
caller_mutex_.unlock();
}
@@ -0,0 +1,65 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 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 AUDIORENDERTHREADBASE_H
#define AUDIORENDERTHREADBASE_H
#include <memory>
#include <QMutex>
#include <QThread>
#include <QWaitCondition>
#include "audiorendererparams.h"
#include "node/node.h"
class AudioRendererThreadBase : public QThread
{
Q_OBJECT
public:
AudioRendererThreadBase(const int& sample_rate,
const uint64_t& channel_layout,
const olive::SampleFormat& format);
AudioRendererParams* params();
void StartThread(Priority priority = InheritPriority);
virtual void run() override;
public slots:
virtual void Cancel() = 0;
protected:
virtual void ProcessLoop() = 0;
QWaitCondition wait_cond_;
QMutex mutex_;
QMutex caller_mutex_;
private:
void WakeCaller();
AudioRendererParams audio_params_;
};
#endif // RENDERTHREAD_H