VideoClipWorkflow.cpp 6.27 KB
Newer Older
1 2 3
/*****************************************************************************
 * VideoClipWorkflow.cpp : Clip workflow. Will extract a single frame from a VLCMedia
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
4
 * Copyright (C) 2008-2010 VideoLAN
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Authors: Hugo Beauzee-Luyssen <hugo@vlmc.org>
 *
 * 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 "VideoClipWorkflow.h"
24
#include "MainWorkflow.h"
25
#include "StackedBuffer.hpp"
26
#include "LightVideoFrame.h"
27
#include "Clip.h"
28
#include <QReadWriteLock>
29

30
VideoClipWorkflow::VideoClipWorkflow( Clip *clip ) :
31 32
        ClipWorkflow( clip ),
        m_lastRenderedFrame( NULL )
33
{
34 35
    for ( unsigned int i = 0; i < VideoClipWorkflow::nbBuffers; ++i )
    {
36
        m_availableBuffers.enqueue( new LightVideoFrame( MainWorkflow::getInstance()->getWidth()
37
                                    * MainWorkflow::getInstance()->getHeight()
38 39
                                    * Pixel::NbComposantes ) );
    }
40
    debugType = 2;
41 42 43 44
}

VideoClipWorkflow::~VideoClipWorkflow()
{
45
    while ( m_availableBuffers.isEmpty() == false )
46
        delete m_availableBuffers.dequeue();
47
    while ( m_computedBuffers.isEmpty() == false )
48
        delete m_computedBuffers.dequeue();
49 50
}

51 52
void
VideoClipWorkflow::initVlcOutput()
53 54 55 56 57 58
{
    char        buffer[32];

    m_vlcMedia->addOption( ":no-audio" );
    m_vlcMedia->addOption( ":no-sout-audio" );
    m_vlcMedia->addOption( ":sout=#transcode{}:smem" );
59 60 61
    m_vlcMedia->setVideoDataCtx( this );
    m_vlcMedia->setVideoLockCallback( reinterpret_cast<void*>( getLockCallback() ) );
    m_vlcMedia->setVideoUnlockCallback( reinterpret_cast<void*>( getUnlockCallback() ) );
62
    m_vlcMedia->addOption( ":sout-transcode-vcodec=RV24" );
63 64 65 66
    if ( m_fullSpeedRender == false )
        m_vlcMedia->addOption( ":sout-smem-time-sync" );
    else
        m_vlcMedia->addOption( ":no-sout-smem-time-sync" );
67

68 69
    sprintf( buffer, ":sout-transcode-width=%i",
             MainWorkflow::getInstance()->getWidth() );
70
    m_vlcMedia->addOption( buffer );
71 72
    sprintf( buffer, ":sout-transcode-height=%i",
             MainWorkflow::getInstance()->getHeight() );
73
    m_vlcMedia->addOption( buffer );
74
    sprintf( buffer, ":sout-transcode-fps=%f", (float)Clip::DefaultFPS );
75
    m_vlcMedia->addOption( buffer );
76 77
}

78
void*
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
79
VideoClipWorkflow::getLockCallback() const
80 81 82 83
{
    return reinterpret_cast<void*>(&VideoClipWorkflow::lock);
}

84
void*
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
85
VideoClipWorkflow::getUnlockCallback() const
86 87 88 89
{
    return reinterpret_cast<void*>( &VideoClipWorkflow::unlock );
}

90 91
void*
VideoClipWorkflow::getOutput( ClipWorkflow::GetMode mode )
92
{
93
    QMutexLocker    lock( m_renderLock );
94
    QMutexLocker    lock2( m_computedBuffersMutex );
95

96
    if ( preGetOutput() == false )
97 98 99
    {
        if ( m_lastRenderedFrame != NULL )
            return new StackedBuffer( m_lastRenderedFrame, NULL, false );
100
        return NULL;
101
    }
102 103
    if ( isEndReached() == true )
        return NULL;
104
    ::StackedBuffer<LightVideoFrame*>* buff;
105
    if ( mode == ClipWorkflow::Pop )
106
        buff = new StackedBuffer( m_computedBuffers.dequeue(), this, true );
107
    else if ( mode == ClipWorkflow::Get )
108
        buff = new StackedBuffer( m_computedBuffers.head(), NULL, false );
109
    postGetOutput();
110
    m_lastRenderedFrame = buff->get();
111
    return buff;
112 113
}

114 115
void
VideoClipWorkflow::lock( VideoClipWorkflow *cw, void **pp_ret, int size )
116 117
{
    Q_UNUSED( size );
118
    QMutexLocker        lock( cw->m_availableBuffersMutex );
119 120
    LightVideoFrame*    lvf = NULL;

121
    cw->m_renderLock->lock();
122
    cw->m_computedBuffersMutex->lock();
123 124 125 126 127 128 129
    if ( cw->m_availableBuffers.isEmpty() == true )
    {
        lvf = new LightVideoFrame( MainWorkflow::getInstance()->getWidth()
                                   * MainWorkflow::getInstance()->getHeight()
                                   * Pixel::NbComposantes );
    }
    else
130 131
        lvf = cw->m_availableBuffers.dequeue();
    cw->m_computedBuffers.enqueue( lvf );
132
    *pp_ret = (*(lvf))->frame.octets;
133 134
}

135 136 137
void
VideoClipWorkflow::unlock( VideoClipWorkflow *cw, void *buffer, int width,
                           int height, int bpp, int size, qint64 pts )
138 139 140 141 142 143
{
    Q_UNUSED( buffer );
    Q_UNUSED( width );
    Q_UNUSED( height );
    Q_UNUSED( bpp );
    Q_UNUSED( size );
144

145
    cw->computePtsDiff( pts );
146
    LightVideoFrame     *lvf = cw->m_computedBuffers.last();
147
    (*(lvf))->ptsDiff = cw->m_currentPts - cw->m_previousPts;
148
    cw->commonUnlock();
149
    cw->m_renderLock->unlock();
150
    cw->m_computedBuffersMutex->unlock();
151 152
}

153 154
uint32_t
VideoClipWorkflow::getNbComputedBuffers() const
155 156 157
{
    return m_computedBuffers.count();
}
158

159 160
uint32_t
VideoClipWorkflow::getMaxComputedBuffers() const
161 162 163
{
    return VideoClipWorkflow::nbBuffers;
}
164

165 166
void
VideoClipWorkflow::releaseBuffer( LightVideoFrame *lvf )
167 168 169 170
{
    QMutexLocker    lock( m_availableBuffersMutex );
    m_availableBuffers.enqueue( lvf );
}
171

172 173
void
VideoClipWorkflow::flushComputedBuffers()
174 175 176 177 178 179 180
{
    QMutexLocker    lock( m_computedBuffersMutex );
    QMutexLocker    lock2( m_availableBuffersMutex );

    while ( m_computedBuffers.isEmpty() == false )
        m_availableBuffers.enqueue( m_computedBuffers.dequeue() );
}
181 182 183 184 185 186 187 188 189

VideoClipWorkflow::StackedBuffer::StackedBuffer( LightVideoFrame *lvf,
                                                    VideoClipWorkflow *poolHandler,
                                                    bool mustBeReleased) :
    ::StackedBuffer<LightVideoFrame*>( lvf, mustBeReleased ),
    m_poolHandler( poolHandler )
{
}

190 191
void
VideoClipWorkflow::StackedBuffer::release()
192
{
193
    if ( m_mustRelease == true && m_poolHandler.isNull() == false )
194 195 196
        m_poolHandler->releaseBuffer( m_buff );
    delete this;
}