VideoClipWorkflow.cpp 6.24 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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