VideoClipWorkflow.cpp 6.2 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
 *
 * 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.
 *****************************************************************************/

23
#include "Clip.h"
24
#include "MainWorkflow.h"
25
#include "StackedBuffer.hpp"
26
#include "VideoClipWorkflow.h"
27
#include "VLCMedia.h"
28
#include "WaitCondition.hpp"
29
#include "Workflow/Types.h"
30

31
#include <QReadWriteLock>
32

33 34
VideoClipWorkflow::VideoClipWorkflow( ClipHelper *ch ) :
        ClipWorkflow( ch ),
35 36
        m_width( 0 ),
        m_height( 0 )
37
{
38 39 40
}

VideoClipWorkflow::~VideoClipWorkflow()
41
{
42
    stop();
43 44 45 46
}

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

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

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

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

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

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

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

110 111
void*
VideoClipWorkflow::getOutput( ClipWorkflow::GetMode mode )
112
{
113
    QMutexLocker    lock( m_renderLock );
114

115
    if ( shouldRender() == false )
116
        return NULL;
117
    if ( getNbComputedBuffers() == 0 )
118
        m_renderWaitCond->wait( m_renderLock );
119 120 121
    //Recheck again, as the WaitCondition may have been awaken when stopping.
    if ( getNbComputedBuffers() == 0 )
        return NULL;
122
    ::StackedBuffer<Workflow::Frame*>* buff;
123
    if ( mode == ClipWorkflow::Pop )
124
        buff = new StackedBuffer( m_computedBuffers.dequeue(), this, true );
125
    else if ( mode == ClipWorkflow::Get )
126
        buff = new StackedBuffer( m_computedBuffers.head(), NULL, false );
127
    postGetOutput();
128
    return buff;
129 130
}

131 132
void
VideoClipWorkflow::lock( VideoClipWorkflow *cw, void **pp_ret, int size )
133 134
{
    Q_UNUSED( size );
135
    Workflow::Frame*    frame = NULL;
136

137
    cw->m_renderLock->lock();
138 139
    if ( cw->m_availableBuffers.isEmpty() == true )
    {
140
        frame = new Workflow::Frame( cw->m_width, cw->m_height );
141 142
    }
    else
143 144 145
        frame = cw->m_availableBuffers.dequeue();
    cw->m_computedBuffers.enqueue( frame );
    *pp_ret = frame->buffer();
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 160
    Workflow::Frame     *frame = cw->m_computedBuffers.last();
    frame->ptsDiff = cw->m_currentPts - cw->m_previousPts;
161
    cw->commonUnlock();
162
    cw->m_renderWaitCond->wakeAll();
163
    cw->m_renderLock->unlock();
164 165
}

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

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

178
void
179
VideoClipWorkflow::releaseBuffer( Workflow::Frame *frame )
180
{
181 182
    QMutexLocker    lock( m_renderLock );

183
    m_availableBuffers.enqueue( frame );
184
}
185

186 187
void
VideoClipWorkflow::flushComputedBuffers()
188
{
189
    QMutexLocker    lock( m_renderLock );
190 191 192 193

    while ( m_computedBuffers.isEmpty() == false )
        m_availableBuffers.enqueue( m_computedBuffers.dequeue() );
}
194

195
VideoClipWorkflow::StackedBuffer::StackedBuffer( Workflow::Frame *frame,
196 197
                                                    VideoClipWorkflow *poolHandler,
                                                    bool mustBeReleased) :
198
    ::StackedBuffer<Workflow::Frame*>( frame, mustBeReleased ),
199 200 201 202
    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;
}