VideoClipWorkflow.cpp 6.38 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
#include "VLCMedia.h"

30
#include <QReadWriteLock>
31

32
VideoClipWorkflow::VideoClipWorkflow( Clip *clip ) :
33
        ClipWorkflow( clip ),
34 35 36
        m_lastRenderedFrame( NULL ),
        m_width( 0 ),
        m_height( 0 )
37
{
38
    debugType = 2;
39 40 41 42
}

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

49 50 51
void
VideoClipWorkflow::preallocate()
{
52 53 54
    quint32     newWidth = MainWorkflow::getInstance()->getWidth();
    quint32     newHeight = MainWorkflow::getInstance()->getHeight();
    if ( newWidth != m_width || newHeight != m_height )
55
    {
56 57 58 59 60 61 62 63
        m_width = newWidth;
        m_height = newHeight;
        while ( m_availableBuffers.isEmpty() == false )
            delete m_availableBuffers.dequeue();
        for ( unsigned int i = 0; i < VideoClipWorkflow::nbBuffers; ++i )
        {
            m_availableBuffers.enqueue( new LightVideoFrame( newWidth, newHeight ) );
        }
64 65 66
    }
}

67 68
void
VideoClipWorkflow::initVlcOutput()
69 70 71
{
    char        buffer[32];

72
    preallocate();
73 74 75
    m_vlcMedia->addOption( ":no-audio" );
    m_vlcMedia->addOption( ":no-sout-audio" );
    m_vlcMedia->addOption( ":sout=#transcode{}:smem" );
76 77 78
    m_vlcMedia->setVideoDataCtx( this );
    m_vlcMedia->setVideoLockCallback( reinterpret_cast<void*>( getLockCallback() ) );
    m_vlcMedia->setVideoUnlockCallback( reinterpret_cast<void*>( getUnlockCallback() ) );
79
    m_vlcMedia->addOption( ":sout-transcode-vcodec=RV24" );
80 81 82 83
    if ( m_fullSpeedRender == false )
        m_vlcMedia->addOption( ":sout-smem-time-sync" );
    else
        m_vlcMedia->addOption( ":no-sout-smem-time-sync" );
84

85
    sprintf( buffer, ":sout-transcode-width=%i", m_width );
86
    m_vlcMedia->addOption( buffer );
87
    sprintf( buffer, ":sout-transcode-height=%i", m_height );
88
    m_vlcMedia->addOption( buffer );
89
    sprintf( buffer, ":sout-transcode-fps=%f", (float)Clip::DefaultFPS );
90
    m_vlcMedia->addOption( buffer );
91 92
}

93
void*
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
94
VideoClipWorkflow::getLockCallback() const
95 96 97 98
{
    return reinterpret_cast<void*>(&VideoClipWorkflow::lock);
}

99
void*
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
100
VideoClipWorkflow::getUnlockCallback() const
101 102 103 104
{
    return reinterpret_cast<void*>( &VideoClipWorkflow::unlock );
}

105 106
void*
VideoClipWorkflow::getOutput( ClipWorkflow::GetMode mode )
107
{
108
    QMutexLocker    lock( m_renderLock );
109
    QMutexLocker    lock2( m_computedBuffersMutex );
110

111
    if ( preGetOutput() == false )
112 113 114
    {
        if ( m_lastRenderedFrame != NULL )
            return new StackedBuffer( m_lastRenderedFrame, NULL, false );
115
        return NULL;
116
    }
117 118
    if ( isEndReached() == true )
        return NULL;
119
    ::StackedBuffer<LightVideoFrame*>* buff;
120
    if ( mode == ClipWorkflow::Pop )
121
        buff = new StackedBuffer( m_computedBuffers.dequeue(), this, true );
122
    else if ( mode == ClipWorkflow::Get )
123
        buff = new StackedBuffer( m_computedBuffers.head(), NULL, false );
124
    postGetOutput();
125
    m_lastRenderedFrame = buff->get();
126
    return buff;
127 128
}

129 130
void
VideoClipWorkflow::lock( VideoClipWorkflow *cw, void **pp_ret, int size )
131 132
{
    Q_UNUSED( size );
133
    QMutexLocker        lock( cw->m_availableBuffersMutex );
134 135
    LightVideoFrame*    lvf = NULL;

136
    cw->m_renderLock->lock();
137
    cw->m_computedBuffersMutex->lock();
138 139
    if ( cw->m_availableBuffers.isEmpty() == true )
    {
140
        lvf = new LightVideoFrame( cw->m_width, cw->m_height );
141 142
    }
    else
143 144
        lvf = cw->m_availableBuffers.dequeue();
    cw->m_computedBuffers.enqueue( lvf );
145
    *pp_ret = (*(lvf))->frame.octets;
146 147
}

148 149 150
void
VideoClipWorkflow::unlock( VideoClipWorkflow *cw, void *buffer, int width,
                           int height, int bpp, int size, qint64 pts )
151 152 153 154 155 156
{
    Q_UNUSED( buffer );
    Q_UNUSED( width );
    Q_UNUSED( height );
    Q_UNUSED( bpp );
    Q_UNUSED( size );
157

158
    cw->computePtsDiff( pts );
159
    LightVideoFrame     *lvf = cw->m_computedBuffers.last();
160
    (*(lvf))->ptsDiff = cw->m_currentPts - cw->m_previousPts;
161
    cw->commonUnlock();
162
    cw->m_renderLock->unlock();
163
    cw->m_computedBuffersMutex->unlock();
164 165
}

166 167
uint32_t
VideoClipWorkflow::getNbComputedBuffers() const
168 169 170
{
    return m_computedBuffers.count();
}
171

172 173
uint32_t
VideoClipWorkflow::getMaxComputedBuffers() const
174 175 176
{
    return VideoClipWorkflow::nbBuffers;
}
177

178 179
void
VideoClipWorkflow::releaseBuffer( LightVideoFrame *lvf )
180 181 182 183
{
    QMutexLocker    lock( m_availableBuffersMutex );
    m_availableBuffers.enqueue( lvf );
}
184

185 186
void
VideoClipWorkflow::flushComputedBuffers()
187 188 189 190 191 192 193
{
    QMutexLocker    lock( m_computedBuffersMutex );
    QMutexLocker    lock2( m_availableBuffersMutex );

    while ( m_computedBuffers.isEmpty() == false )
        m_availableBuffers.enqueue( m_computedBuffers.dequeue() );
}
194 195 196 197 198 199 200 201 202

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

203 204
void
VideoClipWorkflow::StackedBuffer::release()
205
{
206
    if ( m_mustRelease == true && m_poolHandler.isNull() == false )
207 208 209
        m_poolHandler->releaseBuffer( m_buff );
    delete this;
}