VideoClipWorkflow.cpp 6.46 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
}

VideoClipWorkflow::~VideoClipWorkflow()
42 43 44 45 46 47
{
    releasePrealocated();
}

void
VideoClipWorkflow::releasePrealocated()
48
{
49
    while ( m_availableBuffers.isEmpty() == false )
50
        delete m_availableBuffers.dequeue();
51
    while ( m_computedBuffers.isEmpty() == false )
52
        delete m_computedBuffers.dequeue();
53 54
}

55 56 57
void
VideoClipWorkflow::preallocate()
{
58 59 60
    quint32     newWidth = MainWorkflow::getInstance()->getWidth();
    quint32     newHeight = MainWorkflow::getInstance()->getHeight();
    if ( newWidth != m_width || newHeight != m_height )
61
    {
62 63 64 65 66 67 68 69
        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 ) );
        }
70 71 72
    }
}

73 74
void
VideoClipWorkflow::initVlcOutput()
75 76 77
{
    char        buffer[32];

78
    preallocate();
79 80 81
    m_vlcMedia->addOption( ":no-audio" );
    m_vlcMedia->addOption( ":no-sout-audio" );
    m_vlcMedia->addOption( ":sout=#transcode{}:smem" );
82 83 84
    m_vlcMedia->setVideoDataCtx( this );
    m_vlcMedia->setVideoLockCallback( reinterpret_cast<void*>( getLockCallback() ) );
    m_vlcMedia->setVideoUnlockCallback( reinterpret_cast<void*>( getUnlockCallback() ) );
85
    m_vlcMedia->addOption( ":sout-transcode-vcodec=RV24" );
86 87 88 89
    if ( m_fullSpeedRender == false )
        m_vlcMedia->addOption( ":sout-smem-time-sync" );
    else
        m_vlcMedia->addOption( ":no-sout-smem-time-sync" );
90

91
    sprintf( buffer, ":sout-transcode-width=%i", m_width );
92
    m_vlcMedia->addOption( buffer );
93
    sprintf( buffer, ":sout-transcode-height=%i", m_height );
94
    m_vlcMedia->addOption( buffer );
95
    sprintf( buffer, ":sout-transcode-fps=%f", (float)Clip::DefaultFPS );
96
    m_vlcMedia->addOption( buffer );
97 98
}

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

105
void*
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
106
VideoClipWorkflow::getUnlockCallback() const
107 108 109 110
{
    return reinterpret_cast<void*>( &VideoClipWorkflow::unlock );
}

111 112
void*
VideoClipWorkflow::getOutput( ClipWorkflow::GetMode mode )
113
{
114
    QMutexLocker    lock( m_renderLock );
115
    QMutexLocker    lock2( m_computedBuffersMutex );
116

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

135 136
void
VideoClipWorkflow::lock( VideoClipWorkflow *cw, void **pp_ret, int size )
137 138
{
    Q_UNUSED( size );
139
    QMutexLocker        lock( cw->m_availableBuffersMutex );
140 141
    LightVideoFrame*    lvf = NULL;

142
    cw->m_renderLock->lock();
143
    cw->m_computedBuffersMutex->lock();
144 145
    if ( cw->m_availableBuffers.isEmpty() == true )
    {
146
        lvf = new LightVideoFrame( cw->m_width, cw->m_height );
147 148
    }
    else
149 150
        lvf = cw->m_availableBuffers.dequeue();
    cw->m_computedBuffers.enqueue( lvf );
151
    *pp_ret = (*(lvf))->frame.octets;
152 153
}

154 155 156
void
VideoClipWorkflow::unlock( VideoClipWorkflow *cw, void *buffer, int width,
                           int height, int bpp, int size, qint64 pts )
157 158 159 160 161 162
{
    Q_UNUSED( buffer );
    Q_UNUSED( width );
    Q_UNUSED( height );
    Q_UNUSED( bpp );
    Q_UNUSED( size );
163

164
    cw->computePtsDiff( pts );
165
    LightVideoFrame     *lvf = cw->m_computedBuffers.last();
166
    (*(lvf))->ptsDiff = cw->m_currentPts - cw->m_previousPts;
167
    cw->commonUnlock();
168
    cw->m_renderLock->unlock();
169
    cw->m_computedBuffersMutex->unlock();
170 171
}

172 173
uint32_t
VideoClipWorkflow::getNbComputedBuffers() const
174 175 176
{
    return m_computedBuffers.count();
}
177

178 179
uint32_t
VideoClipWorkflow::getMaxComputedBuffers() const
180 181 182
{
    return VideoClipWorkflow::nbBuffers;
}
183

184 185
void
VideoClipWorkflow::releaseBuffer( LightVideoFrame *lvf )
186 187 188 189
{
    QMutexLocker    lock( m_availableBuffersMutex );
    m_availableBuffers.enqueue( lvf );
}
190

191 192
void
VideoClipWorkflow::flushComputedBuffers()
193 194 195 196 197 198 199
{
    QMutexLocker    lock( m_computedBuffersMutex );
    QMutexLocker    lock2( m_availableBuffersMutex );

    while ( m_computedBuffers.isEmpty() == false )
        m_availableBuffers.enqueue( m_computedBuffers.dequeue() );
}
200 201 202 203 204 205 206 207 208

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

209 210
void
VideoClipWorkflow::StackedBuffer::release()
211
{
212
    if ( m_mustRelease == true && m_poolHandler.isNull() == false )
213 214 215
        m_poolHandler->releaseBuffer( m_buff );
    delete this;
}