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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#if defined AVI_CAPTURING
#include "AVIGenerator.h"
#include "Rendering/GlobalRendering.h"
#include "Rendering/GL/myGL.h"
#include "Game/GameVersion.h"
#include "System/Log/ILog.h"
#include "System/Platform/Threading.h"
#include "System/SpringApp.h"
#include "System/SafeUtil.h"
#include <windows.h>
#include <functional>
#include <cassert>
#if defined(_WIN32) && !defined(__MINGW32__)
#pragma message("Adding library: vfw32.lib")
#pragma comment(lib, "vfw32.lib")
#endif
bool CAVIGenerator::initVFW()
{
#if defined(_WIN32) && defined(__MINGW32__)
msvfw32 = LoadLibrary("msvfw32.dll");
avifil32 = LoadLibrary("avifil32.dll");
VideoForWindowsVersion_ptr = (VideoForWindowsVersion_type)GetProcAddress(msvfw32, "VideoForWindowsVersion");
ICCompressorChoose_ptr = (ICCompressorChoose_type)GetProcAddress(msvfw32, "ICCompressorChoose");
ICCompressorFree_ptr = (ICCompressorFree_type)GetProcAddress(msvfw32, "ICCompressorFree");
ICOpen_ptr = (ICOpen_type)GetProcAddress(msvfw32, "ICOpen");
AVIFileInit_ptr = (AVIFileInit_type)GetProcAddress(avifil32, "AVIFileInit");
AVIFileOpenA_ptr = (AVIFileOpenA_type)GetProcAddress(avifil32, "AVIFileOpenA");
AVIFileCreateStreamA_ptr = (AVIFileCreateStreamA_type)GetProcAddress(avifil32, "AVIFileCreateStreamA");
AVIMakeCompressedStream_ptr = (AVIMakeCompressedStream_type)GetProcAddress(avifil32, "AVIMakeCompressedStream");
AVIStreamSetFormat_ptr = (AVIStreamSetFormat_type)GetProcAddress(avifil32, "AVIStreamSetFormat");
AVIStreamRelease_ptr = (AVIStreamRelease_type)GetProcAddress(avifil32, "AVIStreamRelease");
AVIFileRelease_ptr = (AVIFileRelease_type)GetProcAddress(avifil32, "AVIFileRelease");
AVIFileExit_ptr = (AVIFileExit_type)GetProcAddress(avifil32, "AVIFileExit");
AVIStreamWrite_ptr = (AVIStreamWrite_type)GetProcAddress(avifil32, "AVIStreamWrite");
if (nullptr == msvfw32 || nullptr == avifil32) {
errorMsg = "LoadLibrary failed.";
return false;
}
if (!VideoForWindowsVersion_ptr || !ICCompressorChoose_ptr || !ICCompressorFree_ptr || !ICOpen_ptr) {
errorMsg = "initVFW Error.";
return false;
}
if (!AVIFileInit_ptr || !AVIFileOpenA_ptr || !AVIFileCreateStreamA_ptr ||
!AVIMakeCompressedStream_ptr || !AVIStreamSetFormat_ptr || !AVIStreamRelease_ptr ||
!AVIFileRelease_ptr || !AVIFileExit_ptr || !AVIStreamWrite_ptr) {
errorMsg = "initVFW Error.";
return false;
}
#else
VideoForWindowsVersion_ptr = &VideoForWindowsVersion;
AVIFileInit_ptr = &AVIFileInit;
AVIFileOpenA_ptr = &AVIFileOpenA;
AVIFileCreateStreamA_ptr = &AVIFileCreateStreamA;
AVIMakeCompressedStream_ptr = &AVIMakeCompressedStream;
AVIStreamSetFormat_ptr = &AVIStreamSetFormat;
AVIStreamRelease_ptr = &AVIStreamRelease;
AVIFileRelease_ptr = &AVIFileRelease;
AVIFileExit_ptr = &AVIFileExit;
AVIStreamWrite_ptr = &AVIStreamWrite;
ICCompressorChoose_ptr = &ICCompressorChoose;
ICCompressorFree_ptr = &ICCompressorFree;
ICOpen_ptr = &ICOpen;
#endif
return true;
}
CAVIGenerator::CAVIGenerator(const std::string& fileName, int videoSizeX, int videoSizeY, DWORD videoFPS):
fileName(fileName),
videoFPS(videoFPS),
errorMsg("Ok"),
quitAVIgen(false),
AVIThread(0),
readBuf(nullptr),
m_lFrame(0),
m_pAVIFile(nullptr),
m_pStream(nullptr),
m_pStreamCompressed(nullptr)
{
assert(videoSizeX % 4 == 0);
assert(videoSizeY % 4 == 0);
memset(&bitmapInfo, 0, sizeof(BITMAPINFOHEADER));
bitmapInfo.biSize = sizeof(BITMAPINFOHEADER);
bitmapInfo.biWidth = videoSizeX;
bitmapInfo.biHeight = videoSizeY;
bitmapInfo.biPlanes = 1;
bitmapInfo.biBitCount = 24;
bitmapInfo.biSizeImage = videoSizeX * videoSizeY * 3;
bitmapInfo.biCompression = BI_RGB;
quitAVIgen = (!initVFW());
}
CAVIGenerator::~CAVIGenerator()
{
if (AVIThread) {
{
std::lock_guard<spring::mutex> lock(AVIMutex);
quitAVIgen = true;
AVICondition.notify_all();
}
AVIThread->join();
spring::SafeDelete(AVIThread);
}
while (!freeImageBuffers.empty()) {
delete[] freeImageBuffers.front();
freeImageBuffers.pop_front();
}
while (!imageBuffers.empty()) {
delete[] imageBuffers.front();
imageBuffers.pop_front();
}
spring::SafeDelete(readBuf);
ReleaseAVICompressionEngine();
LOG("Finished writing avi file %s", fileName.c_str());
// Just checking that all allocated ressources have been released.
assert(AVIThread == nullptr);
assert(m_pAVIFile == nullptr);
assert(m_pStream == nullptr);
assert(m_pStreamCompressed == nullptr);
assert(freeImageBuffers.empty());
assert(imageBuffers.empty());
assert(readBuf == nullptr);
}
void CAVIGenerator::ReleaseAVICompressionEngine()
{
if (m_pStream && AVIStreamRelease_ptr) {
AVIStreamRelease_ptr(m_pStream);
m_pStream = nullptr;
}
if (m_pStreamCompressed && AVIStreamRelease_ptr) {
AVIStreamRelease_ptr(m_pStreamCompressed);
m_pStreamCompressed = nullptr;
}
if (m_pAVIFile && AVIFileRelease_ptr) {
AVIFileRelease_ptr(m_pAVIFile);
m_pAVIFile = nullptr;
}
if (ICCompressorFree_ptr)
ICCompressorFree_ptr(&cv);
if (AVIFileExit_ptr)
AVIFileExit_ptr();
if (msvfw32)
FreeLibrary(msvfw32);
if (avifil32)
FreeLibrary(avifil32);
}
HRESULT CAVIGenerator::InitAVICompressionEngine()
{
AVISTREAMINFO strHdr; // Information for a single stream
AVICOMPRESSOPTIONS opts;
HRESULT hr;
// Let us make sure we are running on 1.1
DWORD wVer = HIWORD(VideoForWindowsVersion_ptr());
if (wVer < 0x010a) {
// oops, we are too old, blow out of here
errorMsg = "Version of Video for Windows too old. Come on, join the 21th century!";
return S_FALSE;
}
// Initialize AVI engine
AVIFileInit_ptr();
memset(&cv, 0, sizeof(COMPVARS));
cv.cbSize = sizeof(COMPVARS);
cv.dwFlags = ICMF_COMPVARS_VALID;
cv.fccHandler = mmioFOURCC('x', 'v', 'i', 'd'); // default video codec
cv.lQ = ICQUALITY_DEFAULT;
//Set the compression, prompting dialog if necessary
if (!ICCompressorChoose_ptr(nullptr, ICMF_CHOOSE_DATARATE | ICMF_CHOOSE_KEYFRAME, &bitmapInfo, nullptr, &cv, nullptr))
return S_FALSE;
// Fill in the header for the video stream....
memset(&strHdr, 0, sizeof(AVISTREAMINFO));
strHdr.fccType = streamtypeVIDEO; // video stream type
strHdr.fccHandler = cv.fccHandler;
strHdr.dwScale = 1; // should be one for video
strHdr.dwRate = videoFPS; // fps
strHdr.dwSuggestedBufferSize = bitmapInfo.biSizeImage; // Recommended buffer size, in bytes, for the stream.
SetRect(&strHdr.rcFrame, 0, 0, bitmapInfo.biWidth, bitmapInfo.biHeight);
strcpy(strHdr.szName, "Spring video.");
memset(&opts, 0, sizeof(AVICOMPRESSOPTIONS));
opts.fccType = streamtypeVIDEO;
opts.fccHandler = cv.fccHandler;
opts.dwKeyFrameEvery = cv.lKey;
opts.dwQuality = cv.lQ;
opts.dwBytesPerSecond = cv.lDataRate;
opts.dwFlags = ((cv.lDataRate > 0) ? AVICOMPRESSF_DATARATE : 0) | ((cv.lKey > 0) ? AVICOMPRESSF_KEYFRAMES : 0);
opts.lpFormat = nullptr;
opts.cbFormat = 0;
opts.lpParms = cv.lpState;
opts.cbParms = cv.cbState;
opts.dwInterleaveEvery = 0;
//Open the movie file for writing
hr = AVIFileOpenA_ptr(&m_pAVIFile, // Address to contain the new file interface pointer
fileName.c_str(), // Null-terminated string containing the name of the file to open
OF_WRITE | OF_CREATE | OF_SHARE_EXCLUSIVE, // Access mode to use when opening the file.
nullptr); // use handler determined from file extension.
if (hr != AVIERR_OK) {
errorMsg = "AVI Engine failed to initialize. Check filename ";
errorMsg += fileName;
// Translate error code
switch (hr) {
case AVIERR_BADFORMAT: {
errorMsg += "The file couldn't be read, indicating a corrupt file or an unrecognized format.";
} break;
case AVIERR_MEMORY: {
errorMsg += "The file could not be opened because of insufficient memory.";
} break;
case AVIERR_FILEREAD: {
errorMsg += "A disk error occurred while reading the file.";
} break;
case AVIERR_FILEOPEN: {
errorMsg += "A disk error occurred while opening the file.";
} break;
case REGDB_E_CLASSNOTREG: {
errorMsg += "According to the registry, the type of file specified in AVIFileOpen does not have a handler to process it";
} break;
default: {
errorMsg += "Unknown error.";
}
}
return hr;
}
// Create the stream
hr = AVIFileCreateStreamA_ptr(m_pAVIFile, // file pointer
&m_pStream, // returned stream pointer
&strHdr); // stream header
if (hr != AVIERR_OK) {
errorMsg = "AVI Stream creation failed. Check Bitmap info.";
if (hr == AVIERR_READONLY)
errorMsg += " Read only file.";
return hr;
}
// Create a compressed stream using codec options.
if ((hr = AVIMakeCompressedStream_ptr(&m_pStreamCompressed, m_pStream, &opts, nullptr)) != AVIERR_OK) {
errorMsg = "AVI Compressed Stream creation failed.";
switch (hr) {
case AVIERR_NOCOMPRESSOR: {
errorMsg += " A suitable compressor cannot be found.";
} break;
case AVIERR_MEMORY: {
errorMsg += " There is not enough memory to complete the operation.";
} break;
case AVIERR_UNSUPPORTED: {
errorMsg += "Compression is not supported for this type of data. This error might be returned if you try to compress data that is not audio or video.";
} break;
default: {
errorMsg += "Unknown error.";
}
}
return hr;
}
//Sets the format of a stream at the specified position
hr = AVIStreamSetFormat_ptr(m_pStreamCompressed,
0, // position
&bitmapInfo, // stream format
bitmapInfo.biSize + // format size
bitmapInfo.biClrUsed * sizeof(RGBQUAD));
if (hr != AVIERR_OK) {
errorMsg = "AVI Compressed Stream format setting failed.";
return hr;
}
return hr;
}
bool CAVIGenerator::InitEngine()
{
// error in initVFW
if (quitAVIgen)
return false;
for (int i = 0; i < 10; i++) {
freeImageBuffers.push_back(new unsigned char[bitmapInfo.biSizeImage]);
}
HWND mainWindow = FindWindow(nullptr, ("Spring " + SpringVersion::GetFull()).c_str());
if (globalRendering->fullScreen)
ShowWindow(mainWindow, SW_SHOWMINNOACTIVE);
std::unique_lock<spring::mutex> lock(AVIMutex);
AVIThread = new spring::thread(std::bind(&CAVIGenerator::AVIGeneratorThreadProc, this));
AVICondition.wait(lock); // Wait until InitAVICompressionEngine() completes.
if (globalRendering->fullScreen)
ShowWindow(mainWindow, SW_RESTORE);
return !quitAVIgen;
}
HRESULT CAVIGenerator::AddFrame(unsigned char* pixelData)
{
// compress bitmap
HRESULT hr = AVIStreamWrite_ptr(m_pStreamCompressed, // stream pointer
m_lFrame, // time of this frame
1, // number to write
pixelData, // image buffer
bitmapInfo.biSizeImage, // size of this frame
AVIIF_KEYFRAME, // flags....
nullptr,
nullptr);
// updating frame counter
m_lFrame++;
return hr;
}
bool CAVIGenerator::readOpenglPixelDataThreaded()
{
while (true) {
std::unique_lock<spring::mutex> lock(AVIMutex);
if (quitAVIgen)
return false;
if (readBuf != nullptr) {
imageBuffers.push_back(readBuf);
readBuf = nullptr;
AVICondition.notify_all();
}
if (freeImageBuffers.empty()) {
AVICondition.wait(lock);
} else {
readBuf = freeImageBuffers.front();
freeImageBuffers.pop_front();
break;
}
}
glReadPixels(0, 0, bitmapInfo.biWidth, bitmapInfo.biHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, readBuf);
return true;
}
__FORCE_ALIGN_STACK__
void CAVIGenerator::AVIGeneratorThreadProc()
{
Threading::SetThreadName("avi-recorder");
//Run init from the encoding thread because custom controls displayed by codecs
//sends window messages to the thread it started from, thus deadlocking if
//sending to the main thread whilst it is blocking on readOpenglPixelDataThreaded().
quitAVIgen = InitAVICompressionEngine() != AVIERR_OK;
AVICondition.notify_all();
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
unsigned char* localWriteBuf = 0;
bool encoderError = false;
while (true) {
{
std::unique_lock<spring::mutex> lock(AVIMutex);
if (encoderError) {
LOG_L(L_ERROR, "The avi generator terminated unexpectedly!");
quitAVIgen = true;
// Do not let the main thread wait, as the encoder will not
// process and free the remaining content in imageBuffers.
AVICondition.notify_all();
}
if (quitAVIgen)
break;
if (localWriteBuf != 0) {
freeImageBuffers.push_back(localWriteBuf);
localWriteBuf = 0;
AVICondition.notify_all();
}
if (imageBuffers.empty()) {
AVICondition.wait(lock);
continue;
}
localWriteBuf = imageBuffers.front();
imageBuffers.pop_front();
}
if (AddFrame(localWriteBuf))
encoderError = true;
MSG msg;
// Handle all messages the codec sends.
while (PeekMessage(&msg, nullptr, 0, 0, PM_NOREMOVE)) {
if (GetMessage(&msg, nullptr, 0, 0) < 0) {
encoderError = true;
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
delete[] localWriteBuf;
}
#endif // defined AVI_CAPTURING
|