EffectUser.cpp 9.92 KB
Newer Older
1 2 3 4 5
/*****************************************************************************
 * EffectUser.cpp: Handles effects list and application
 *****************************************************************************
 * Copyright (C) 2008-2010 VideoLAN
 *
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
6
 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#include "EffectUser.h"

25
#include "EffectHelper.h"
26 27 28 29 30 31 32 33 34 35 36 37
#include "EffectInstance.h"
#include "Types.h"

#include <QDomElement>
#include <QReadWriteLock>
#include <QtDebug>

EffectUser::EffectUser() :
        m_isRendering( false ),
        m_width( 0 ),
        m_height( 0 )
{
38
    m_effectsLock = new QReadWriteLock();
39 40 41 42
}

EffectUser::~EffectUser()
{
43
    cleanEffects();
44 45 46
    delete m_effectsLock;
}

47
EffectHelper*
48 49
EffectUser::addEffect( Effect *effect, qint64 start /*= 0*/, qint64 end /*= -1*/ )
{
50 51 52 53 54 55 56 57 58
    //Check that effect type is one of the supported ones
    if ( effect->type() == Effect::Filter  || effect->type() == Effect::Mixer2 )
    {
        EffectInstance  *effectInstance = effect->createInstance();
        EffectHelper *ret = new EffectHelper( effectInstance, start, end );
        addEffect( ret );
        return ret;
    }
    return NULL;
59
}
60

61 62 63 64 65
void
EffectUser::addEffect( EffectHelper *effectHelper )
{
    if ( m_isRendering == true )
        effectHelper->effectInstance()->init( m_width, m_height );
66
    QWriteLocker    lock( m_effectsLock );
67 68
    if ( effectHelper->effectInstance()->effect()->type() == Effect::Filter )
        m_filters.push_back( effectHelper );
69
    else
70
        m_mixers.push_back( effectHelper );
71
    effectHelper->setTarget( this );
72
    emit effectAdded( effectHelper, effectHelper->begin() );
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
}

quint32*
EffectUser::applyFilters( const Workflow::Frame* frame,
                             qint64 currentFrame, double time )
{
    QReadLocker     lock( m_effectsLock );

    if ( m_filters.size() == 0 )
        return NULL;
    EffectsEngine::EffectList::const_iterator     it = m_filters.constBegin();
    EffectsEngine::EffectList::const_iterator     ite = m_filters.constEnd();

    quint32         *buff1 = NULL;
    quint32         *buff2 = NULL;
    const quint32   *input = frame->buffer();
    bool            firstBuff = true;

    while ( it != ite )
    {
93 94
        if ( (*it)->begin() < currentFrame &&
             ( (*it)->end() < 0 || (*it)->end() > currentFrame ) )
95 96 97 98 99 100 101 102
        {
            quint32     **buff;
            if ( firstBuff == true )
                buff = &buff1;
            else
                buff = &buff2;
            if ( *buff == NULL )
                *buff = new quint32[frame->nbPixels()];
103
            EffectInstance      *effect = (*it)->effectInstance();
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
            effect->process( time, input, *buff );
            input = *buff;
            firstBuff = !firstBuff;
        }
        ++it;
    }
    if ( buff1 != NULL || buff2 != NULL )
    {
        if ( firstBuff == true )
        {
            delete[] buff1;
            return buff2;
        }
        else
        {
            delete[] buff2;
            return buff1;
        }
    }
    return NULL;
}

void
EffectUser::initFilters()
{
    QReadLocker     lock( m_effectsLock );

    EffectsEngine::EffectList::const_iterator   it = m_filters.begin();
    EffectsEngine::EffectList::const_iterator   ite = m_filters.end();

    while ( it != ite )
    {
136
        (*it)->effectInstance()->init( m_width, m_height );
137 138 139 140 141 142 143 144 145 146 147 148 149 150
        ++it;
    }
}

void
EffectUser::initMixers()
{
    QReadLocker     lock( m_effectsLock );

    EffectsEngine::EffectList::const_iterator   it = m_mixers.begin();
    EffectsEngine::EffectList::const_iterator   ite = m_mixers.end();

    while ( it != ite )
    {
151
        (*it)->effectInstance()->init( m_width, m_height );
152 153 154 155
        ++it;
    }
}

156
EffectHelper*
157 158 159 160 161 162 163 164 165
EffectUser::getMixer( qint64 currentFrame )
{
    QReadLocker     lock( m_effectsLock );

    EffectsEngine::EffectList::const_iterator      it = m_mixers.constBegin();
    EffectsEngine::EffectList::const_iterator      ite = m_mixers.constEnd();

    while ( it != ite )
    {
166
        if ( (*it)->begin() <= currentFrame && currentFrame <= (*it)->end() )
167
        {
168
            Q_ASSERT( (*it)->effectInstance()->effect()->type() == Effect::Mixer2 );
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
            return (*it);
        }
        ++it;
    }
    return NULL;
}

void
EffectUser::loadEffects( const QDomElement &parent )
{
    QDomElement     effects = parent.firstChildElement( "effects" );
    if ( effects.isNull() == true )
        return ;
    QDomElement     effect = effects.firstChildElement( "effect" );
    while ( effect.isNull() == false )
    {
        if ( effect.hasAttribute( "name" ) == true &&
             effect.hasAttribute( "start" ) == true &&
             effect.hasAttribute( "end" ) == true )
        {
            Effect  *e = EffectsEngine::getInstance()->effect( effect.attribute( "name" ) );
            if ( e != NULL )
191 192 193 194 195 196
            {
                EffectHelper    *helper = addEffect( e, effect.attribute( "start" ).toLongLong(),
                                                    effect.attribute( "end" ).toLongLong() );
                if ( helper == NULL )
                    qCritical() << "Can't load effect" << effect.attribute( "name" );
            }
197
            else
198
                qCritical() << "Can't load effect" << effect.attribute( "name" );
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        }
        effect = effect.nextSiblingElement();
    }
}

void
EffectUser::saveFilters( QXmlStreamWriter &project ) const
{
    QReadLocker     lock( m_effectsLock );

    if ( m_filters.size() <= 0 )
        return ;
    project.writeStartElement( "effects" );
    EffectsEngine::EffectList::const_iterator   it = m_filters.begin();
    EffectsEngine::EffectList::const_iterator   ite = m_filters.end();
    while ( it != ite )
    {
        project.writeStartElement( "effect" );
217 218 219
        project.writeAttribute( "name", (*it)->effectInstance()->effect()->name() );
        project.writeAttribute( "start", QString::number( (*it)->begin() ) );
        project.writeAttribute( "end", QString::number( (*it)->end() ) );
220 221 222 223 224 225
        project.writeEndElement();
        ++it;
    }
    project.writeEndElement();
}

226 227 228 229 230 231 232 233 234 235 236
const EffectsEngine::EffectList&
EffectUser::effects( Effect::Type type ) const
{
    if ( type == Effect::Filter )
        return m_filters;
    if ( type != Effect::Mixer2 )
        qCritical() << "Only Filters and Mixer2 are handled. This is going to be nasty !";
    return m_mixers;
}

void
237
EffectUser::removeEffect( Effect::Type type, qint32 idx )
238
{
239 240
    Q_ASSERT( idx >= 0 );

241 242 243 244 245
    QWriteLocker    lock( m_effectsLock );

    if ( type == Effect::Filter )
    {
        if ( idx < m_filters.size() )
246 247
        {
            EffectHelper    *helper = m_filters.takeAt( idx );
248
            helper->setTarget( NULL );
249
            emit    effectRemoved( helper->uuid() );
250
        }
251 252 253 254
    }
    else if ( type == Effect::Mixer2 )
    {
        if ( idx < m_mixers.size() )
255 256
        {
            EffectHelper    *helper = m_mixers.takeAt( idx );
257
            helper->setTarget( NULL );
258
            emit    effectRemoved( helper->uuid() );
259
        }
260 261 262 263 264
    }
    else
        qCritical() << "Unhandled effect type";
}

265 266 267 268 269 270 271 272 273 274 275
void
EffectUser::removeEffect(EffectHelper *helper)
{
    QWriteLocker    lock( m_effectsLock );

    EffectsEngine::EffectList::iterator     it = m_filters.begin();
    EffectsEngine::EffectList::iterator     ite = m_filters.end();
    while ( it != ite )
    {
        if ( (*it)->uuid() == helper->uuid() )
        {
276 277
            EffectHelper    *eh = *it;
            eh->setTarget( NULL );
278
            m_filters.erase( it );
279
            emit effectRemoved( eh->uuid() );
280 281
            return ;
        }
282
        ++it;
283 284 285 286
    }
    qWarning() << "Can't find EffectHelper" << helper->uuid() << "for removal.";
}

287
void
288
EffectUser::swapFilters( qint32 idx, qint32 idx2 )
289
{
290 291 292
    Q_ASSERT( idx >= 0 && idx2 >= 0 );

    if ( idx >= m_filters.size() || idx2 >= m_filters.size() )
293 294 295 296 297 298
        return ;
    QWriteLocker        lock( m_effectsLock );

    m_filters.swap( idx, idx2 );
}

299
qint32
300 301 302 303 304 305 306 307 308 309 310
EffectUser::count( Effect::Type type ) const
{
    QReadLocker     lock( m_effectsLock );

    if ( type == Effect::Filter )
        return m_filters.count();
    if ( type == Effect::Mixer2 )
        return m_mixers.count();
    qCritical() << "Unhandled effect type";
    return 0;
}
311 312 313 314 315 316 317 318 319 320 321 322

void
EffectUser::moveEffect( EffectHelper *helper, qint64 newPos )
{
    QWriteLocker    lock( m_effectsLock );

    foreach ( EffectHelper *eh, m_filters )
    {
        if ( helper->uuid() == eh->uuid() )
        {
            qint64  offset = helper->begin() - newPos;
            helper->setBoundaries( newPos, helper->end() - offset );
323
            emit effectMoved( helper, newPos );
324 325 326
            return ;
        }
    }
327
    qWarning() << this << "Can't find effect" << helper->uuid();
328
}
329 330 331 332 333 334 335 336 337 338 339

void
EffectUser::cleanEffects()
{
    QWriteLocker    lock( m_effectsLock );

    qDeleteAll( m_filters );
    qDeleteAll( m_mixers );
    m_filters.clear();
    m_mixers.clear();
}
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

bool
EffectUser::contains( Effect::Type, const QUuid &uuid ) const
{
    QReadLocker     lock( m_effectsLock );

    EffectsEngine::EffectList::const_iterator     it = m_filters.constBegin();
    EffectsEngine::EffectList::const_iterator     ite = m_filters.constEnd();
    while ( it != ite )
    {
        if ( (*it)->uuid() == uuid )
            return true;
        ++it;
    }
    return false;
}