VideoClipWorkflow.cpp 6.25 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 34
        ClipWorkflow( clip ),
        m_lastRenderedFrame( NULL )
35
{
36
    debugType = 2;
37 38 39 40
}

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

47 48 49 50 51 52 53 54 55 56
void
VideoClipWorkflow::preallocate()
{
    for ( unsigned int i = 0; i < VideoClipWorkflow::nbBuffers; ++i )
    {
        m_availableBuffers.enqueue( new LightVideoFrame( MainWorkflow::getInstance()->getWidth(),
                                                         MainWorkflow::getInstance()->getHeight() ) );
    }
}

57 58
void
VideoClipWorkflow::initVlcOutput()
59 60 61 62 63 64
{
    char        buffer[32];

    m_vlcMedia->addOption( ":no-audio" );
    m_vlcMedia->addOption( ":no-sout-audio" );
    m_vlcMedia->addOption( ":sout=#transcode{}:smem" );
65 66 67
    m_vlcMedia->setVideoDataCtx( this );
    m_vlcMedia->setVideoLockCallback( reinterpret_cast<void*>( getLockCallback() ) );
    m_vlcMedia->setVideoUnlockCallback( reinterpret_cast<void*>( getUnlockCallback() ) );
68
    m_vlcMedia->addOption( ":sout-transcode-vcodec=RV24" );
69 70 71 72
    if ( m_fullSpeedRender == false )
        m_vlcMedia->addOption( ":sout-smem-time-sync" );
    else
        m_vlcMedia->addOption( ":no-sout-smem-time-sync" );
73

74 75
    sprintf( buffer, ":sout-transcode-width=%i",
             MainWorkflow::getInstance()->getWidth() );
76
    m_vlcMedia->addOption( buffer );
77 78
    sprintf( buffer, ":sout-transcode-height=%i",
             MainWorkflow::getInstance()->getHeight() );
79
    m_vlcMedia->addOption( buffer );
80
    sprintf( buffer, ":sout-transcode-fps=%f", (float)Clip::DefaultFPS );
81
    m_vlcMedia->addOption( buffer );
82
    preallocate();
83 84
}

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

91
void*
Hugo Beauzee-Luyssen's avatar
Hugo Beauzee-Luyssen committed
92
VideoClipWorkflow::getUnlockCallback() const
93 94 95 96
{
    return reinterpret_cast<void*>( &VideoClipWorkflow::unlock );
}

97 98
void*
VideoClipWorkflow::getOutput( ClipWorkflow::GetMode mode )
99
{
100
    QMutexLocker    lock( m_renderLock );
101
    QMutexLocker    lock2( m_computedBuffersMutex );
102

103
    if ( preGetOutput() == false )
104 105 106
    {
        if ( m_lastRenderedFrame != NULL )
            return new StackedBuffer( m_lastRenderedFrame, NULL, false );
107
        return NULL;
108
    }
109 110
    if ( isEndReached() == true )
        return NULL;
111
    ::StackedBuffer<LightVideoFrame*>* buff;
112
    if ( mode == ClipWorkflow::Pop )
113
        buff = new StackedBuffer( m_computedBuffers.dequeue(), this, true );
114
    else if ( mode == ClipWorkflow::Get )
115
        buff = new StackedBuffer( m_computedBuffers.head(), NULL, false );
116
    postGetOutput();
117
    m_lastRenderedFrame = buff->get();
118
    return buff;
119 120
}

121 122
void
VideoClipWorkflow::lock( VideoClipWorkflow *cw, void **pp_ret, int size )
123 124
{
    Q_UNUSED( size );
125
    QMutexLocker        lock( cw->m_availableBuffersMutex );
126 127
    LightVideoFrame*    lvf = NULL;

128
    cw->m_renderLock->lock();
129
    cw->m_computedBuffersMutex->lock();
130 131
    if ( cw->m_availableBuffers.isEmpty() == true )
    {
132 133
        lvf = new LightVideoFrame( MainWorkflow::getInstance()->getWidth(),
                                   MainWorkflow::getInstance()->getHeight() );
134 135
    }
    else
136 137
        lvf = cw->m_availableBuffers.dequeue();
    cw->m_computedBuffers.enqueue( lvf );
138
    *pp_ret = (*(lvf))->frame.octets;
139 140
}

141 142 143
void
VideoClipWorkflow::unlock( VideoClipWorkflow *cw, void *buffer, int width,
                           int height, int bpp, int size, qint64 pts )
144 145 146 147 148 149
{
    Q_UNUSED( buffer );
    Q_UNUSED( width );
    Q_UNUSED( height );
    Q_UNUSED( bpp );
    Q_UNUSED( size );
150

151
    cw->computePtsDiff( pts );
152
    LightVideoFrame     *lvf = cw->m_computedBuffers.last();
153
    (*(lvf))->ptsDiff = cw->m_currentPts - cw->m_previousPts;
154
    cw->commonUnlock();
155
    cw->m_renderLock->unlock();
156
    cw->m_computedBuffersMutex->unlock();
157 158
}

159 160
uint32_t
VideoClipWorkflow::getNbComputedBuffers() const
161 162 163
{
    return m_computedBuffers.count();
}
164

165 166
uint32_t
VideoClipWorkflow::getMaxComputedBuffers() const
167 168 169
{
    return VideoClipWorkflow::nbBuffers;
}
170

171 172
void
VideoClipWorkflow::releaseBuffer( LightVideoFrame *lvf )
173 174 175 176
{
    QMutexLocker    lock( m_availableBuffersMutex );
    m_availableBuffers.enqueue( lvf );
}
177

178 179
void
VideoClipWorkflow::flushComputedBuffers()
180 181 182 183 184 185 186
{
    QMutexLocker    lock( m_computedBuffersMutex );
    QMutexLocker    lock2( m_availableBuffersMutex );

    while ( m_computedBuffers.isEmpty() == false )
        m_availableBuffers.enqueue( m_computedBuffers.dequeue() );
}
187 188 189 190 191 192 193 194 195

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

196 197
void
VideoClipWorkflow::StackedBuffer::release()
198
{
199
    if ( m_mustRelease == true && m_poolHandler.isNull() == false )
200 201 202
        m_poolHandler->releaseBuffer( m_buff );
    delete this;
}