VideoClipWorkflow.cpp 5.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*****************************************************************************
 * VideoClipWorkflow.cpp : Clip workflow. Will extract a single frame from a VLCMedia
 *****************************************************************************
 * Copyright (C) 2008-2009 the VLMC team
 *
 * 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 30
VideoClipWorkflow::VideoClipWorkflow( Clip *clip ) :
        ClipWorkflow( clip )
31
{
32 33
    for ( unsigned int i = 0; i < VideoClipWorkflow::nbBuffers; ++i )
    {
34
        m_availableBuffers.enqueue( new LightVideoFrame( MainWorkflow::getInstance()->getWidth()
35
                                    * MainWorkflow::getInstance()->getHeight()
36 37
                                    * Pixel::NbComposantes ) );
    }
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
void
VideoClipWorkflow::initVlcOutput()
51 52 53 54 55 56
{
    char        buffer[32];

    m_vlcMedia->addOption( ":no-audio" );
    m_vlcMedia->addOption( ":no-sout-audio" );
    m_vlcMedia->addOption( ":sout=#transcode{}:smem" );
57 58 59
    m_vlcMedia->setVideoDataCtx( this );
    m_vlcMedia->setVideoLockCallback( reinterpret_cast<void*>( getLockCallback() ) );
    m_vlcMedia->setVideoUnlockCallback( reinterpret_cast<void*>( getUnlockCallback() ) );
60 61
    m_vlcMedia->addOption( ":sout-transcode-vcodec=RV24" );
    m_vlcMedia->addOption( ":sout-transcode-acodec=s16l" );
62
    m_vlcMedia->addOption( ":sout-smem-time-sync" );
63

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

74 75
void*
VideoClipWorkflow::getLockCallback()
76 77 78 79
{
    return reinterpret_cast<void*>(&VideoClipWorkflow::lock);
}

80 81
void*
VideoClipWorkflow::getUnlockCallback()
82 83 84 85
{
    return reinterpret_cast<void*>( &VideoClipWorkflow::unlock );
}

86 87
void*
VideoClipWorkflow::getOutput( ClipWorkflow::GetMode mode )
88
{
89
    QMutexLocker    lock( m_renderLock );
90
    QMutexLocker    lock2( m_computedBuffersMutex );
91

92 93
    if ( preGetOutput() == false )
        return NULL;
94 95
    if ( isEndReached() == true )
        return NULL;
96
    ::StackedBuffer<LightVideoFrame*>* buff;
97
    if ( mode == ClipWorkflow::Pop )
98
        buff = new StackedBuffer( m_computedBuffers.dequeue(), this, true );
99
    else if ( mode == ClipWorkflow::Get )
100
        buff = new StackedBuffer( m_computedBuffers.head(), NULL, false );
101
    postGetOutput();
102
    return buff;
103 104
}

105 106
void
VideoClipWorkflow::lock( VideoClipWorkflow *cw, void **pp_ret, int size )
107 108
{
    Q_UNUSED( size );
109
    QMutexLocker        lock( cw->m_availableBuffersMutex );
110 111
    LightVideoFrame*    lvf = NULL;

112
    cw->m_renderLock->lock();
113
    cw->m_computedBuffersMutex->lock();
114 115 116 117 118 119 120
    if ( cw->m_availableBuffers.isEmpty() == true )
    {
        lvf = new LightVideoFrame( MainWorkflow::getInstance()->getWidth()
                                   * MainWorkflow::getInstance()->getHeight()
                                   * Pixel::NbComposantes );
    }
    else
121 122
        lvf = cw->m_availableBuffers.dequeue();
    cw->m_computedBuffers.enqueue( lvf );
123
    *pp_ret = (*(lvf))->frame.octets;
124 125
}

126 127 128
void
VideoClipWorkflow::unlock( VideoClipWorkflow *cw, void *buffer, int width,
                           int height, int bpp, int size, qint64 pts )
129 130 131 132 133 134
{
    Q_UNUSED( buffer );
    Q_UNUSED( width );
    Q_UNUSED( height );
    Q_UNUSED( bpp );
    Q_UNUSED( size );
135

136
    cw->computePtsDiff( pts );
137
    LightVideoFrame     *lvf = cw->m_computedBuffers.last();
138
    (*(lvf))->ptsDiff = cw->m_currentPts - cw->m_previousPts;
139
    cw->commonUnlock();
140
    cw->m_renderLock->unlock();
141
    cw->m_computedBuffersMutex->unlock();
142 143
}

144 145
uint32_t
VideoClipWorkflow::getNbComputedBuffers() const
146 147 148
{
    return m_computedBuffers.count();
}
149

150 151
uint32_t
VideoClipWorkflow::getMaxComputedBuffers() const
152 153 154
{
    return VideoClipWorkflow::nbBuffers;
}
155

156 157
void
VideoClipWorkflow::releaseBuffer( LightVideoFrame *lvf )
158 159 160 161
{
    QMutexLocker    lock( m_availableBuffersMutex );
    m_availableBuffers.enqueue( lvf );
}
162

163 164
void
VideoClipWorkflow::flushComputedBuffers()
165 166 167 168 169 170 171
{
    QMutexLocker    lock( m_computedBuffersMutex );
    QMutexLocker    lock2( m_availableBuffersMutex );

    while ( m_computedBuffers.isEmpty() == false )
        m_availableBuffers.enqueue( m_computedBuffers.dequeue() );
}
172 173 174 175 176 177 178 179 180

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

181 182
void
VideoClipWorkflow::StackedBuffer::release()
183
{
184
    if ( m_mustRelease == true && m_poolHandler.isNull() == false )
185 186 187
        m_poolHandler->releaseBuffer( m_buff );
    delete this;
}