Commit baf29920 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

EffectsEngine: Refactoring.

Making the WorkflowRenderer effect processing thread safe
parent 56d5057c
......@@ -23,6 +23,7 @@
#include "EffectsEngine.h"
#include "Effect.h"
#include "Types.h"
#include <QDir>
#include <QtDebug>
......@@ -83,3 +84,52 @@ EffectsEngine::browseDirectory( const QString &path )
loadEffect( path + '/' + file );
}
}
void
EffectsEngine::applyEffects( const EffectList &effects, MainWorkflow::OutputBuffers *ret,
qint64 currentFrame )
{
if ( effects.size() == 0 )
return ;
EffectList::const_iterator it = effects.constBegin();
EffectList::const_iterator ite = effects.constEnd();
quint8 *buff1 = NULL;
quint8 *buff2 = NULL;
quint8 *input = ret->video->buffer();
bool firstBuff = true;
while ( it != ite )
{
if ( (*it)->start < currentFrame &&
( (*it)->end < 0 || (*it)->end > currentFrame ) )
{
quint8 **buff;
if ( firstBuff == true )
buff = &buff1;
else
buff = &buff2;
if ( *buff == NULL )
*buff = new quint8[ret->video->size()];
Effect *effect = (*it)->effect;
effect->process( 0.0, (quint32*)input, (quint32*)*buff );
input = *buff;
firstBuff = !firstBuff;
}
++it;
}
if ( buff1 != NULL || buff2 != NULL )
{
//The old input frame will automatically be deleted when setting the new buffer
if ( firstBuff == true )
{
delete[] buff1;
ret->video->setBuffer( buff2 );
}
else
{
delete[] buff2;
ret->video->setBuffer( buff1 );
}
}
}
......@@ -26,6 +26,7 @@
#include "Singleton.hpp"
#include "Effect.h"
#include "MainWorkflow.h"
#include <QObject>
#include <QHash>
......@@ -42,11 +43,15 @@ class EffectsEngine : public QObject, public Singleton<EffectsEngine>
qint64 start;
qint64 end;
};
typedef QList<EffectHelper*> EffectList;
void initAll( quint32 width, quint32 height );
Effect* effect( const QString& name );
bool loadEffect( const QString& fileName );
void browseDirectory( const QString& path );
static void applyEffects( const EffectList& effects,
MainWorkflow::OutputBuffers* ret, qint64 currentFrame );
private:
EffectsEngine();
~EffectsEngine();
......
......@@ -48,6 +48,7 @@ WorkflowRenderer::WorkflowRenderer() :
m_esHandler( NULL ),
m_oldLength( 0 )
{
m_effectsLock = new QReadWriteLock;
}
void WorkflowRenderer::initializeRenderer()
......@@ -82,6 +83,7 @@ WorkflowRenderer::~WorkflowRenderer()
delete m_media;
if ( m_silencedAudioBuffer )
delete m_silencedAudioBuffer;
delete m_effectsLock;
}
void
......@@ -167,7 +169,10 @@ WorkflowRenderer::lockVideo( EsHandler *handler, qint64 *pts, size_t *bufferSize
//this is a bit hackish though... (especially regarding the "no frame computed" detection)
ptsDiff = 1000000 / handler->fps;
}
applyEffects( ret );
{
QReadLocker lock( m_effectsLock );
EffectsEngine::applyEffects( m_effects, ret, m_mainWorkflow->getCurrentFrame() );
}
m_pts = *pts = ptsDiff + m_pts;
*buffer = ret->video->buffer();
*bufferSize = ret->video->size();
......@@ -216,54 +221,6 @@ void WorkflowRenderer::unlock( void*, const char*, size_t, void* )
{
}
void
WorkflowRenderer::applyEffects( MainWorkflow::OutputBuffers* ret )
{
if ( m_effects.size() == 0 )
return ;
QList<EffectsEngine::EffectHelper*>::const_iterator it = m_effects.constBegin();
QList<EffectsEngine::EffectHelper*>::const_iterator ite = m_effects.constEnd();
quint8 *buff1 = NULL;
quint8 *buff2 = NULL;
quint8 *input = ret->video->buffer();
bool firstBuff = true;
while ( it != ite )
{
if ( (*it)->start < m_mainWorkflow->getCurrentFrame() &&
( (*it)->end < 0 || (*it)->end > m_mainWorkflow->getCurrentFrame() ) )
{
quint8 **buff;
if ( firstBuff == true )
buff = &buff1;
else
buff = &buff2;
if ( *buff == NULL )
*buff = new quint8[ret->video->size()];
Effect *effect = (*it)->effect;
effect->process( 0.0, (quint32*)input, (quint32*)*buff );
input = *buff;
firstBuff = !firstBuff;
}
++it;
}
if ( buff1 != NULL || buff2 != NULL )
{
//The old input frame will automatically be deleted when setting the new buffer
if ( firstBuff == true )
{
delete[] buff1;
ret->video->setBuffer( buff2 );
}
else
{
delete[] buff2;
ret->video->setBuffer( buff1 );
}
}
}
void WorkflowRenderer::startPreview()
{
if ( m_mainWorkflow->getLengthFrame() <= 0 )
......@@ -431,6 +388,7 @@ WorkflowRenderer::appendEffect( Effect *effect, qint64 start, qint64 end )
{
effect->setUsed( true );
effect->init( m_width, m_height );
QWriteLocker lock( m_effectsLock );
m_effects.push_back( new EffectsEngine::EffectHelper( effect, start, end ) );
}
......
......@@ -149,8 +149,6 @@ class WorkflowRenderer : public GenericRenderer
*/
virtual void startPreview();
void applyEffects( MainWorkflow::OutputBuffers *ret );
protected:
/**
* \brief Will return a pointer to the function/static method to use
......@@ -283,7 +281,8 @@ class WorkflowRenderer : public GenericRenderer
*/
qint64 m_oldLength;
QList<EffectsEngine::EffectHelper*> m_effects;
QReadWriteLock *m_effectsLock;
EffectsEngine::EffectList m_effects;
static const quint8 VideoCookie = '0';
static const quint8 AudioCookie = '1';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment