VideoClipWorkflow.cpp 6.06 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
24
#include "Clip.h"
#include "LightVideoFrame.h"
25
#include "MainWorkflow.h"
26
#include "StackedBuffer.hpp"
27
#include "VideoClipWorkflow.h"
28
#include "VLCMedia.h"
29
#include "WaitCondition.hpp"
30

31
#include <QReadWriteLock>
32

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

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

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
67
68
        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 ) );
        }
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=RV24" );
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
116
    if ( isEndReached() == true )
        return NULL;
117
    if ( preGetOutput() == false )
118
        m_renderWaitCond->wait( m_renderLock );
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
    return buff;
126
127
}

128
129
void
VideoClipWorkflow::lock( VideoClipWorkflow *cw, void **pp_ret, int size )
130
131
{
    Q_UNUSED( size );
132
133
    LightVideoFrame*    lvf = NULL;

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

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

155
    cw->computePtsDiff( pts );
156
    LightVideoFrame     *lvf = cw->m_computedBuffers.last();
157
    (*(lvf))->ptsDiff = cw->m_currentPts - cw->m_previousPts;
158
    cw->commonUnlock();
159
    cw->m_renderWaitCond->wakeAll();
160
    cw->m_renderLock->unlock();
161
162
}

163
164
uint32_t
VideoClipWorkflow::getNbComputedBuffers() const
165
166
167
{
    return m_computedBuffers.count();
}
168

169
170
uint32_t
VideoClipWorkflow::getMaxComputedBuffers() const
171
172
173
{
    return VideoClipWorkflow::nbBuffers;
}
174

175
176
void
VideoClipWorkflow::releaseBuffer( LightVideoFrame *lvf )
177
{
178
179
    QMutexLocker    lock( m_renderLock );

180
181
    m_availableBuffers.enqueue( lvf );
}
182

183
184
void
VideoClipWorkflow::flushComputedBuffers()
185
{
186
    QMutexLocker    lock( m_renderLock );
187
188
189
190

    while ( m_computedBuffers.isEmpty() == false )
        m_availableBuffers.enqueue( m_computedBuffers.dequeue() );
}
191
192
193
194
195
196
197
198
199

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

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