summaryrefslogtreecommitdiff
path: root/src/player/VideoWriter.cpp
blob: e91db380afc43d98768fd8e2f84b4575dfc2c926 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library 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
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#include "VideoWriter.h"
#include "OffscreenCanvas.h"
#include "Player.h"
#include "SDLDisplayEngine.h"

#include "../graphics/FBO.h"
#include "../graphics/GPURGB2YUVFilter.h"
#include "../graphics/Filterfill.h"
#include "../base/StringHelper.h"

#include <boost/bind.hpp>

#include <fcntl.h>
#include <stdio.h>

using namespace std;
using namespace boost;

namespace avg {

VideoWriter::VideoWriter(CanvasPtr pCanvas, const string& sOutFileName, int frameRate,
        int qMin, int qMax, bool bSyncToPlayback)
    : m_pCanvas(pCanvas),
      m_sOutFileName(sOutFileName),
      m_FrameRate(frameRate),
      m_QMin(qMin),
      m_QMax(qMax),
      m_bHasValidData(false),
      m_bSyncToPlayback(bSyncToPlayback),
      m_bPaused(false),
      m_PauseTime(0),
      m_bStopped(false),
      m_CurFrame(0),
      m_StartTime(-1),
      m_bFramePending(false)
{
    if (!pCanvas) {
        throw Exception(AVG_ERR_INVALID_ARGS, "VideoWriter needs a canvas to write to.");
    }
    if (GLContext::getCurrent()->isGLES()) {
        throw Exception(AVG_ERR_UNSUPPORTED, "VideoWriter not supported under GLES.");
    }
#ifdef WIN32
    int fd = _open(m_sOutFileName.c_str(), O_RDWR | O_CREAT, _S_IREAD | _S_IWRITE);
#elif defined linux
    int fd = open64(m_sOutFileName.c_str(), O_RDWR | O_CREAT, S_IRWXU);
#else
    int fd = open(m_sOutFileName.c_str(), O_RDWR | O_CREAT, S_IRWXU);
#endif
    if (fd == -1) {
        throw Exception(AVG_ERR_VIDEO_INIT_FAILED, 
                string("Could not open output file '") + m_sOutFileName + "'. Reason: " +
                strerror(errno));
    }
#ifdef WIN32
    _close(fd);
#else
    close(fd);
#endif
    remove(m_sOutFileName.c_str());
    CanvasPtr pMainCanvas = Player::get()->getMainCanvas();
    if (pMainCanvas == m_pCanvas) {
        m_FrameSize = Player::get()->getDisplayEngine()->getWindowSize();
    } else {
        m_FrameSize = m_pCanvas->getSize();
        m_pFBO = dynamic_pointer_cast<OffscreenCanvas>(m_pCanvas)->getFBO();
        if (GLContext::getMain()->useGPUYUVConversion()) {
            m_pFilter = GPURGB2YUVFilterPtr(new GPURGB2YUVFilter(m_FrameSize));
        }
    }
    VideoWriterThread writer(m_CmdQueue, m_sOutFileName, m_FrameSize, m_FrameRate, 
            qMin, qMax);
    m_pThread = new boost::thread(writer);
    m_pCanvas->registerPlaybackEndListener(this);
    m_pCanvas->registerFrameEndListener(this);
}

VideoWriter::~VideoWriter()
{
    stop();
    if (m_pThread) {
        m_pThread->join();
        delete m_pThread;
    }
}

void VideoWriter::stop()
{
    if (!m_bStopped) {
        getFrameFromPBO();
        if (!m_bHasValidData) {
            writeDummyFrame();
        }

        m_bStopped = true;
        m_CmdQueue.pushCmd(boost::bind(&VideoWriterThread::stop, _1));
        
        m_pCanvas->unregisterFrameEndListener(this);
        m_pCanvas->unregisterPlaybackEndListener(this);
    }
}

void VideoWriter::pause()
{
    if (m_bPaused) {
        throw Exception(AVG_ERR_UNSUPPORTED, "VideoWriter::pause() called when paused.");
    }
    if (m_bStopped) {
        throw Exception(AVG_ERR_UNSUPPORTED, "VideoWriter::pause() called when stopped.");
    }
    m_bPaused = true;
    m_PauseStartTime = Player::get()->getFrameTime();
}

void VideoWriter::play()
{
    if (!m_bPaused) {
        throw Exception(AVG_ERR_UNSUPPORTED, 
                "VideoWriter::play() called when not paused.");
    }
    m_bPaused = false;
    m_PauseTime += (Player::get()->getFrameTime() - m_PauseStartTime);
}

std::string VideoWriter::getFileName() const
{
    return m_sOutFileName;
}

int VideoWriter::getFramerate() const
{
    return m_FrameRate;
}

int VideoWriter::getQMin() const
{
    return m_QMin;
}

int VideoWriter::getQMax() const
{
    return m_QMax;
}

void VideoWriter::onFrameEnd()
{
    // The VideoWriter handles OffscreenCanvas and MainCanvas differently:
    // For MainCanvas, it simply does a screenshot onFrameEnd and sends that to the 
    // VideoWriterThread immediately.
    // For OffscreenCanvas, an asynchronous PBO readback is started in onFrameEnd.
    // In the next frame's onFrameEnd, the data is read into a bitmap and sent to 
    // the VideoWriterThread.
    if (m_pFBO) {
        // Read last frame's bitmap.
        getFrameFromPBO();
    }
    if (m_StartTime == -1) {
        m_StartTime = Player::get()->getFrameTime();
    }
    if (!m_bPaused) {
        if (m_bSyncToPlayback) {
            getFrameFromFBO();
        } else {
            long long movieTime = Player::get()->getFrameTime() - m_StartTime
                    - m_PauseTime;
            float timePerFrame = 1000.f/m_FrameRate;
            int wantedFrame = int(movieTime/timePerFrame+0.1);
            if (wantedFrame > m_CurFrame) {
                getFrameFromFBO();
                if (wantedFrame > m_CurFrame + 1) {
                    m_CurFrame = wantedFrame - 1;
                }
            }
        }
    }
    
    if (!m_pFBO) {
        getFrameFromPBO();
    }
}

void VideoWriter::getFrameFromFBO()
{
    if (m_pFBO) {
        if (m_pFilter) {
            m_pFilter->apply(m_pFBO->getTex());
            FBOPtr pYUVFBO = m_pFilter->getFBO();
            pYUVFBO->moveToPBO();
        } else {
            m_pFBO->moveToPBO();
        }
        m_bFramePending = true;
    } else {
        BitmapPtr pBmp = Player::get()->getDisplayEngine()->screenshot(GL_BACK);
        sendFrameToEncoder(pBmp);
    }
}

void VideoWriter::getFrameFromPBO()
{
    if (m_bFramePending) {
        BitmapPtr pBmp;
        if (m_pFilter) {
            pBmp = m_pFilter->getFBO()->getImageFromPBO();
        } else {
            pBmp = m_pFBO->getImageFromPBO();
        }
        sendFrameToEncoder(pBmp);
        m_bFramePending = false;
    }
}

void VideoWriter::sendFrameToEncoder(BitmapPtr pBitmap)
{
    m_CurFrame++;
    m_bHasValidData = true;
    if (m_pFilter) {
        m_CmdQueue.pushCmd(boost::bind(&VideoWriterThread::encodeYUVFrame, _1, pBitmap));
    } else {
        m_CmdQueue.pushCmd(boost::bind(&VideoWriterThread::encodeFrame, _1, pBitmap));
    }
}

void VideoWriter::onPlaybackEnd()
{
    stop();
    m_pThread->join();
    delete m_pThread;
    m_pThread = 0;
}

void VideoWriter::writeDummyFrame()
{
    BitmapPtr pBmp = BitmapPtr(new Bitmap(m_FrameSize, B8G8R8X8));
    FilterFill<Pixel32>(Pixel32(0,0,0,255)).applyInPlace(pBmp);
    sendFrameToEncoder(pBmp);
}

}