2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include <SDL.h>
|
2007-08-08 22:28:56 +03:00
|
|
|
#include "CVideoHandler.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
|
2013-04-07 14:52:07 +03:00
|
|
|
#include "gui/CGuiHandler.h"
|
|
|
|
#include "gui/SDL_Extensions.h"
|
2011-04-07 20:54:08 +03:00
|
|
|
#include "CPlayerInterface.h"
|
2013-07-28 17:49:50 +03:00
|
|
|
#include "../lib/filesystem/Filesystem.h"
|
2009-06-28 02:33:59 +03:00
|
|
|
|
2012-01-12 18:23:00 +03:00
|
|
|
extern CGuiHandler GH; //global gui handler
|
|
|
|
|
2009-07-27 20:55:56 +03:00
|
|
|
//reads events and returns true on key down
|
2009-06-28 02:33:59 +03:00
|
|
|
static bool keyDown()
|
|
|
|
{
|
|
|
|
SDL_Event ev;
|
|
|
|
while(SDL_PollEvent(&ev))
|
|
|
|
{
|
|
|
|
if(ev.type == SDL_KEYDOWN || ev.type == SDL_MOUSEBUTTONDOWN)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-06-20 04:34:26 +03:00
|
|
|
|
2009-06-20 17:52:20 +03:00
|
|
|
#ifdef _WIN32
|
2008-07-16 21:26:15 +03:00
|
|
|
|
2009-06-24 09:56:36 +03:00
|
|
|
void checkForError(bool throwing = true)
|
|
|
|
{
|
|
|
|
int error = GetLastError();
|
|
|
|
if(!error)
|
|
|
|
return;
|
|
|
|
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Error " << error << " encountered!";
|
2009-06-24 09:56:36 +03:00
|
|
|
std::string msg;
|
2013-06-26 14:18:27 +03:00
|
|
|
char* pTemp = nullptr;
|
2009-06-24 09:56:36 +03:00
|
|
|
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
|
2013-06-26 14:18:27 +03:00
|
|
|
nullptr, error, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), (LPSTR)&pTemp, 1, nullptr );
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Error: " << pTemp;
|
2009-06-24 09:56:36 +03:00
|
|
|
msg = pTemp;
|
|
|
|
LocalFree( pTemp );
|
2013-06-26 14:18:27 +03:00
|
|
|
pTemp = nullptr;
|
2009-06-24 09:56:36 +03:00
|
|
|
if(throwing)
|
2012-08-09 10:40:47 +03:00
|
|
|
throw std::runtime_error(msg);
|
2009-06-24 09:56:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void blitBuffer(char *buffer, int x, int y, int w, int h, SDL_Surface *dst)
|
|
|
|
{
|
2012-09-15 22:16:16 +03:00
|
|
|
const int bpp = dst->format->BytesPerPixel;
|
2009-06-24 09:56:36 +03:00
|
|
|
char *dest;
|
|
|
|
for(int i = h; i > 0; i--)
|
|
|
|
{
|
|
|
|
dest = (char*)dst->pixels + dst->pitch*(y+h-i) + x*dst->format->BytesPerPixel;
|
|
|
|
memcpy(dest, buffer, bpp*w);
|
|
|
|
buffer += bpp*w;
|
|
|
|
}
|
|
|
|
}
|
2009-06-23 11:14:49 +03:00
|
|
|
|
2007-08-08 22:28:56 +03:00
|
|
|
void DLLHandler::Instantiate(const char *filename)
|
|
|
|
{
|
2009-06-23 11:14:49 +03:00
|
|
|
name = filename;
|
2007-08-08 22:28:56 +03:00
|
|
|
dll = LoadLibraryA(filename);
|
2009-06-24 09:56:36 +03:00
|
|
|
if(!dll)
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Failed loading " << filename;
|
2011-08-08 10:12:18 +03:00
|
|
|
checkForError(true);
|
2009-06-24 09:56:36 +03:00
|
|
|
}
|
2007-08-08 22:28:56 +03:00
|
|
|
}
|
2007-08-17 20:42:21 +03:00
|
|
|
|
2009-06-23 11:14:49 +03:00
|
|
|
void *DLLHandler::FindAddress(const char *symbol)
|
2007-08-08 22:28:56 +03:00
|
|
|
{
|
2009-06-24 09:56:36 +03:00
|
|
|
void *ret;
|
2011-09-22 17:21:53 +03:00
|
|
|
|
2011-08-08 10:12:18 +03:00
|
|
|
if(!dll)
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Cannot look for " << symbol << " because DLL hasn't been appropriately loaded!";
|
2013-06-26 14:18:27 +03:00
|
|
|
return nullptr;
|
2011-08-08 10:12:18 +03:00
|
|
|
}
|
2009-06-24 09:56:36 +03:00
|
|
|
ret = (void*) GetProcAddress(dll,symbol);
|
|
|
|
if(!ret)
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Failed to find " << symbol << " in " << name;
|
2009-06-24 09:56:36 +03:00
|
|
|
checkForError();
|
|
|
|
}
|
|
|
|
return ret;
|
2007-08-08 22:28:56 +03:00
|
|
|
}
|
2009-06-23 11:14:49 +03:00
|
|
|
|
2007-08-08 22:28:56 +03:00
|
|
|
DLLHandler::~DLLHandler()
|
|
|
|
{
|
2009-06-23 11:14:49 +03:00
|
|
|
if(dll)
|
|
|
|
{
|
2009-06-24 09:56:36 +03:00
|
|
|
if(!FreeLibrary(dll))
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Failed to free " << name;
|
2009-06-24 09:56:36 +03:00
|
|
|
checkForError();
|
|
|
|
}
|
2009-06-23 11:14:49 +03:00
|
|
|
}
|
2007-08-08 22:28:56 +03:00
|
|
|
}
|
|
|
|
|
2009-06-23 11:14:49 +03:00
|
|
|
DLLHandler::DLLHandler()
|
2007-08-08 22:28:56 +03:00
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
dll = nullptr;
|
2007-08-08 22:28:56 +03:00
|
|
|
}
|
2009-06-23 11:14:49 +03:00
|
|
|
|
|
|
|
CBIKHandler::CBIKHandler()
|
|
|
|
{
|
|
|
|
Instantiate("BINKW32.DLL");
|
2009-06-24 09:56:36 +03:00
|
|
|
//binkGetError = FindAddress("_BinkGetError@0");
|
2009-06-23 11:14:49 +03:00
|
|
|
binkOpen = (BinkOpen)FindAddress("_BinkOpen@8");
|
|
|
|
binkSetSoundSystem = (BinkSetSoundSystem)FindAddress("_BinkSetSoundSystem@8");
|
2009-06-24 15:14:48 +03:00
|
|
|
//getPalette = (BinkGetPalette)FindAddress("_BinkGetPalette@4");
|
|
|
|
binkNextFrame = (BinkNextFrame)FindAddress("_BinkNextFrame@4");
|
2009-06-23 11:14:49 +03:00
|
|
|
binkDoFrame = (BinkDoFrame)FindAddress("_BinkDoFrame@4");
|
|
|
|
binkCopyToBuffer = (BinkCopyToBuffer)FindAddress("_BinkCopyToBuffer@28");
|
|
|
|
binkWait = (BinkWait)FindAddress("_BinkWait@4");
|
2009-06-24 09:56:36 +03:00
|
|
|
binkClose = (BinkClose)FindAddress("_BinkClose@4");
|
|
|
|
|
2011-08-08 10:12:18 +03:00
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
hBinkFile = nullptr;
|
|
|
|
hBink = nullptr;
|
2009-08-01 13:08:16 +03:00
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
buffer = nullptr;
|
2009-08-01 13:08:16 +03:00
|
|
|
bufferSize = 0;
|
2009-06-23 11:14:49 +03:00
|
|
|
}
|
|
|
|
|
2010-07-20 13:46:38 +03:00
|
|
|
bool CBIKHandler::open(std::string name)
|
2007-08-08 22:28:56 +03:00
|
|
|
{
|
2009-06-23 11:14:49 +03:00
|
|
|
hBinkFile = CreateFileA
|
2007-08-17 20:42:21 +03:00
|
|
|
(
|
2009-06-23 11:14:49 +03:00
|
|
|
name.c_str(), // file name
|
2007-08-17 20:42:21 +03:00
|
|
|
GENERIC_READ, // access mode
|
|
|
|
FILE_SHARE_READ, // share mode
|
2013-06-26 14:18:27 +03:00
|
|
|
nullptr, // Security Descriptor
|
2007-08-17 20:42:21 +03:00
|
|
|
OPEN_EXISTING, // how to create
|
|
|
|
FILE_ATTRIBUTE_NORMAL,//FILE_FLAG_SEQUENTIAL_SCAN, // file attributes
|
|
|
|
0 // handle to template file
|
|
|
|
);
|
2009-06-23 11:14:49 +03:00
|
|
|
|
2007-08-17 20:42:21 +03:00
|
|
|
if(hBinkFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "BIK handler: failed to open " << name;
|
2010-07-20 13:46:38 +03:00
|
|
|
goto checkErrorAndClean;
|
2007-08-17 20:42:21 +03:00
|
|
|
}
|
2012-09-15 22:16:16 +03:00
|
|
|
//GCC wants scope of waveout to dont cross labels/swith/goto
|
|
|
|
{
|
|
|
|
void *waveout = GetProcAddress(dll,"_BinkOpenWaveOut@4");
|
|
|
|
if(waveout)
|
2013-06-26 14:18:27 +03:00
|
|
|
binkSetSoundSystem(waveout,nullptr);
|
2007-08-08 22:28:56 +03:00
|
|
|
|
2012-09-15 22:16:16 +03:00
|
|
|
}
|
2007-08-08 22:28:56 +03:00
|
|
|
|
2009-06-23 11:14:49 +03:00
|
|
|
hBink = binkOpen(hBinkFile, 0x8a800000);
|
2010-07-20 13:46:38 +03:00
|
|
|
if(!hBink)
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "bink failed to open " << name;
|
2010-07-20 13:46:38 +03:00
|
|
|
goto checkErrorAndClean;
|
|
|
|
}
|
2009-08-01 13:08:16 +03:00
|
|
|
|
|
|
|
allocBuffer();
|
2010-07-20 13:46:38 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
checkErrorAndClean:
|
|
|
|
CloseHandle(hBinkFile);
|
2013-06-26 14:18:27 +03:00
|
|
|
hBinkFile = nullptr;
|
2012-08-09 10:40:47 +03:00
|
|
|
checkForError();
|
|
|
|
throw std::runtime_error("BIK failed opening video!");
|
2009-06-23 11:14:49 +03:00
|
|
|
}
|
|
|
|
|
2009-06-24 09:56:36 +03:00
|
|
|
void CBIKHandler::show( int x, int y, SDL_Surface *dst, bool update )
|
2009-06-23 11:14:49 +03:00
|
|
|
{
|
2012-09-15 22:16:16 +03:00
|
|
|
const int w = hBink->width,
|
|
|
|
h = hBink->height,
|
2009-08-01 13:08:16 +03:00
|
|
|
Bpp = dst->format->BytesPerPixel;
|
|
|
|
|
|
|
|
int mode = -1;
|
|
|
|
|
|
|
|
//screen color depth might have changed... (eg. because F4)
|
|
|
|
if(bufferSize != w * h * Bpp)
|
|
|
|
{
|
|
|
|
freeBuffer();
|
|
|
|
allocBuffer(Bpp);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(Bpp)
|
|
|
|
{
|
2010-08-08 17:39:41 +03:00
|
|
|
case 2:
|
|
|
|
mode = 3; //565, mode 2 is 555 probably
|
|
|
|
break;
|
2009-08-01 13:08:16 +03:00
|
|
|
case 3:
|
|
|
|
mode = 0;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
mode = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return; //not supported screen depth
|
|
|
|
}
|
|
|
|
|
2009-06-23 11:14:49 +03:00
|
|
|
binkDoFrame(hBink);
|
2009-08-01 13:08:16 +03:00
|
|
|
binkCopyToBuffer(hBink, buffer, w*Bpp, h, 0, 0, mode);
|
2009-06-24 09:56:36 +03:00
|
|
|
blitBuffer(buffer, x, y, w, h, dst);
|
|
|
|
if(update)
|
|
|
|
SDL_UpdateRect(dst, x, y, w, h);
|
2009-06-23 11:14:49 +03:00
|
|
|
}
|
|
|
|
|
2011-08-08 17:20:22 +03:00
|
|
|
bool CBIKHandler::nextFrame()
|
2009-06-23 11:14:49 +03:00
|
|
|
{
|
|
|
|
binkNextFrame(hBink);
|
2011-08-08 17:20:22 +03:00
|
|
|
return true;
|
2009-06-23 11:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CBIKHandler::close()
|
|
|
|
{
|
2009-06-24 09:56:36 +03:00
|
|
|
binkClose(hBink);
|
2013-06-26 14:18:27 +03:00
|
|
|
hBink = nullptr;
|
2009-06-24 09:56:36 +03:00
|
|
|
CloseHandle(hBinkFile);
|
2013-06-26 14:18:27 +03:00
|
|
|
hBinkFile = nullptr;
|
2009-06-23 11:14:49 +03:00
|
|
|
delete [] buffer;
|
2009-08-01 13:08:16 +03:00
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
buffer = nullptr;
|
2009-08-01 13:08:16 +03:00
|
|
|
bufferSize = 0;
|
2007-08-08 22:28:56 +03:00
|
|
|
}
|
2008-07-16 21:26:15 +03:00
|
|
|
|
2009-06-23 11:14:49 +03:00
|
|
|
bool CBIKHandler::wait()
|
2009-06-22 14:12:40 +03:00
|
|
|
{
|
2009-06-23 11:14:49 +03:00
|
|
|
return binkWait(hBink);
|
2009-06-22 14:12:40 +03:00
|
|
|
}
|
|
|
|
|
2009-06-24 09:56:36 +03:00
|
|
|
int CBIKHandler::curFrame() const
|
|
|
|
{
|
|
|
|
return hBink->currentFrame;
|
2009-06-22 14:12:40 +03:00
|
|
|
}
|
|
|
|
|
2009-06-24 09:56:36 +03:00
|
|
|
int CBIKHandler::frameCount() const
|
2009-06-22 14:12:40 +03:00
|
|
|
{
|
2009-06-24 09:56:36 +03:00
|
|
|
return hBink->frameCount;
|
2009-06-22 14:12:40 +03:00
|
|
|
}
|
|
|
|
|
2009-06-24 12:17:33 +03:00
|
|
|
void CBIKHandler::redraw( int x, int y, SDL_Surface *dst, bool update )
|
|
|
|
{
|
|
|
|
int w = hBink->width, h = hBink->height;
|
|
|
|
blitBuffer(buffer, x, y, w, h, dst);
|
|
|
|
if(update)
|
|
|
|
SDL_UpdateRect(dst, x, y, w, h);
|
|
|
|
}
|
|
|
|
|
2009-08-01 13:08:16 +03:00
|
|
|
void CBIKHandler::allocBuffer(int Bpp)
|
|
|
|
{
|
|
|
|
if(!Bpp) Bpp = screen->format->BytesPerPixel;
|
|
|
|
|
|
|
|
bufferSize = hBink->width * hBink->height * Bpp;
|
|
|
|
buffer = new char[bufferSize];
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBIKHandler::freeBuffer()
|
|
|
|
{
|
|
|
|
delete [] buffer;
|
2013-06-26 14:18:27 +03:00
|
|
|
buffer = nullptr;
|
2009-08-01 13:08:16 +03:00
|
|
|
bufferSize = 0;
|
|
|
|
}
|
|
|
|
|
2011-08-08 17:20:22 +03:00
|
|
|
bool CSmackPlayer::nextFrame()
|
2009-06-22 14:12:40 +03:00
|
|
|
{
|
|
|
|
ptrSmackNextFrame(data);
|
2011-08-08 17:20:22 +03:00
|
|
|
return true;
|
2009-06-22 14:12:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CSmackPlayer::wait()
|
|
|
|
{
|
2009-06-23 11:14:49 +03:00
|
|
|
return ptrSmackWait(data);
|
2009-06-22 14:12:40 +03:00
|
|
|
}
|
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
CSmackPlayer::CSmackPlayer() : data(nullptr)
|
2009-06-22 14:12:40 +03:00
|
|
|
{
|
2009-06-23 11:14:49 +03:00
|
|
|
Instantiate("smackw32.dll");
|
|
|
|
ptrSmackNextFrame = (SmackNextFrame)FindAddress("_SmackNextFrame@4");
|
|
|
|
ptrSmackWait = (SmackWait)FindAddress("_SmackWait@4");
|
|
|
|
ptrSmackDoFrame = (SmackDoFrame)FindAddress("_SmackDoFrame@4");
|
|
|
|
ptrSmackToBuffer = (SmackToBuffer)FindAddress("_SmackToBuffer@28");
|
|
|
|
ptrSmackOpen = (SmackOpen)FindAddress("_SmackOpen@12");
|
|
|
|
ptrSmackSoundOnOff = (SmackSoundOnOff)FindAddress("_SmackSoundOnOff@8");
|
2009-06-24 09:56:36 +03:00
|
|
|
ptrSmackClose = (SmackClose)FindAddress("_SmackClose@4");
|
2010-07-28 14:34:26 +03:00
|
|
|
ptrVolumePan = (SmackVolumePan)FindAddress("_SmackVolumePan@16");
|
2009-06-24 09:56:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CSmackPlayer::~CSmackPlayer()
|
|
|
|
{
|
|
|
|
if(data)
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSmackPlayer::close()
|
|
|
|
{
|
|
|
|
ptrSmackClose(data);
|
2013-06-26 14:18:27 +03:00
|
|
|
data = nullptr;
|
2009-06-24 09:56:36 +03:00
|
|
|
}
|
|
|
|
|
2010-07-20 13:46:38 +03:00
|
|
|
bool CSmackPlayer::open( std::string name )
|
2009-06-24 09:56:36 +03:00
|
|
|
{
|
2009-06-24 15:14:48 +03:00
|
|
|
Uint32 flags[2] = {0xff400, 0xfe400};
|
|
|
|
|
2010-07-28 14:34:26 +03:00
|
|
|
data = ptrSmackOpen( (void*)name.c_str(), flags[1], -1);
|
2012-09-15 22:16:16 +03:00
|
|
|
if (!data)
|
2009-06-24 09:56:36 +03:00
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Smack cannot open " << name;
|
2012-08-09 10:40:47 +03:00
|
|
|
checkForError();
|
|
|
|
throw std::runtime_error("SMACK failed opening video");
|
2009-06-24 09:56:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer = new char[data->width*data->height*2];
|
|
|
|
buf = buffer+data->width*(data->height-1)*2; // adjust pointer position for later use by 'SmackToBuffer'
|
2010-07-28 14:34:26 +03:00
|
|
|
|
2010-08-01 17:04:48 +03:00
|
|
|
//ptrVolumePan(data, 0xfe000, 3640 * GDefaultOptions.musicVolume / 11, 0x8000); //set volume
|
2010-07-20 13:46:38 +03:00
|
|
|
return true;
|
2009-06-24 09:56:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSmackPlayer::show( int x, int y, SDL_Surface *dst, bool update)
|
|
|
|
{
|
2012-09-26 16:13:39 +03:00
|
|
|
int w = data->width;
|
2009-06-24 09:56:36 +03:00
|
|
|
int stripe = (-w*2) & (~3);
|
|
|
|
|
|
|
|
//put frame to the buffer
|
|
|
|
ptrSmackToBuffer(data, 0, 0, stripe, w, buf, 0x80000000);
|
|
|
|
ptrSmackDoFrame(data);
|
2009-06-24 12:17:33 +03:00
|
|
|
redraw(x, y, dst, update);
|
|
|
|
}
|
2009-06-24 09:56:36 +03:00
|
|
|
|
2009-06-24 12:17:33 +03:00
|
|
|
int CSmackPlayer::curFrame() const
|
|
|
|
{
|
|
|
|
return data->currentFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CSmackPlayer::frameCount() const
|
|
|
|
{
|
|
|
|
return data->frameCount;
|
|
|
|
}
|
2009-06-24 09:56:36 +03:00
|
|
|
|
2009-06-24 12:17:33 +03:00
|
|
|
void CSmackPlayer::redraw( int x, int y, SDL_Surface *dst, bool update )
|
|
|
|
{
|
2010-08-23 18:16:40 +03:00
|
|
|
int w = std::min<int>(data->width, dst->w - x), h = std::min<int>(data->height, dst->h - y);
|
2009-06-24 09:56:36 +03:00
|
|
|
/* Lock the screen for direct access to the pixels */
|
2012-09-15 22:16:16 +03:00
|
|
|
if ( SDL_MUSTLOCK(dst) )
|
2009-06-24 09:56:36 +03:00
|
|
|
{
|
2012-09-15 22:16:16 +03:00
|
|
|
if ( SDL_LockSurface(dst) < 0 )
|
2009-06-24 09:56:36 +03:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw the frame
|
|
|
|
Uint16* addr = (Uint16*) (buffer+w*(h-1)*2-2);
|
2010-08-08 17:39:41 +03:00
|
|
|
if(dst->format->BytesPerPixel >= 3)
|
2009-06-24 09:56:36 +03:00
|
|
|
{
|
2010-08-08 17:39:41 +03:00
|
|
|
for( int j=0; j<h-1; j++) // why -1 ?
|
2009-06-24 09:56:36 +03:00
|
|
|
{
|
2010-08-08 17:39:41 +03:00
|
|
|
for ( int i=w-1; i>=0; i--)
|
|
|
|
{
|
|
|
|
Uint16 pixel = *addr;
|
2009-06-24 09:56:36 +03:00
|
|
|
|
2010-08-08 17:39:41 +03:00
|
|
|
Uint8 *p = (Uint8 *)dst->pixels + (j+y) * dst->pitch + (i + x) * dst->format->BytesPerPixel;
|
|
|
|
p[2] = ((pixel & 0x7c00) >> 10) * 8;
|
|
|
|
p[1] = ((pixel & 0x3e0) >> 5) * 8;
|
|
|
|
p[0] = ((pixel & 0x1F)) * 8;
|
2009-06-24 09:56:36 +03:00
|
|
|
|
2010-08-08 17:39:41 +03:00
|
|
|
addr--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(dst->format->BytesPerPixel == 2)
|
|
|
|
{
|
|
|
|
for( int j=0; j<h-1; j++) // why -1 ?
|
|
|
|
{
|
|
|
|
for ( int i=w-1; i>=0; i--)
|
|
|
|
{
|
|
|
|
//convert rgb 555 to 565
|
|
|
|
Uint16 pixel = *addr;
|
|
|
|
Uint16 *p = (Uint16 *)((Uint8 *)dst->pixels + (j+y) * dst->pitch + (i + x) * dst->format->BytesPerPixel);
|
2012-09-15 22:16:16 +03:00
|
|
|
*p = (pixel & 0x1F)
|
2010-08-08 17:39:41 +03:00
|
|
|
+ ((pixel & 0x3e0) << 1)
|
|
|
|
+ ((pixel & 0x7c00) << 1);
|
|
|
|
|
|
|
|
addr--;
|
|
|
|
}
|
2009-06-24 09:56:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-15 22:16:16 +03:00
|
|
|
if ( SDL_MUSTLOCK(dst) )
|
2009-06-24 09:56:36 +03:00
|
|
|
{
|
|
|
|
SDL_UnlockSurface(dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(update)
|
|
|
|
SDL_UpdateRect(dst, x, y, w, h);
|
|
|
|
}
|
|
|
|
|
2009-06-24 15:14:48 +03:00
|
|
|
CVideoPlayer::CVideoPlayer()
|
2009-06-23 11:14:49 +03:00
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
current = nullptr;
|
2009-06-24 15:14:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CVideoPlayer::~CVideoPlayer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-07-20 13:46:38 +03:00
|
|
|
bool CVideoPlayer::open(std::string name)
|
2009-06-24 15:14:48 +03:00
|
|
|
{
|
|
|
|
fname = name;
|
|
|
|
first = true;
|
|
|
|
|
2012-08-09 10:40:47 +03:00
|
|
|
try
|
2011-03-19 16:27:51 +02:00
|
|
|
{
|
2012-08-09 10:40:47 +03:00
|
|
|
// Extract video from video.vid so we can play it.
|
|
|
|
// We can handle only videos in form of single file, no archive support yet.
|
|
|
|
{
|
2013-02-05 23:06:13 +03:00
|
|
|
ResourceID videoID = ResourceID("VIDEO/" + name, EResType::VIDEO);
|
2013-07-29 14:38:18 +03:00
|
|
|
auto data = CResourceHandler::get()->load(videoID)->readAll();
|
2013-02-05 23:06:13 +03:00
|
|
|
|
2013-07-29 14:38:18 +03:00
|
|
|
// try to determine video format using magic number from header (3 bytes, SMK or BIK)
|
|
|
|
std::string magic(reinterpret_cast<char*>(data.first.get()), 3);
|
|
|
|
if (magic == "BIK")
|
2013-02-05 23:06:13 +03:00
|
|
|
current = &bikPlayer;
|
2013-07-29 14:38:18 +03:00
|
|
|
else if (magic == "SMK")
|
2013-02-05 23:06:13 +03:00
|
|
|
current = &smkPlayer;
|
2013-07-29 14:38:18 +03:00
|
|
|
else
|
|
|
|
throw std::runtime_error("Unknown video format: " + magic);
|
2012-08-09 10:40:47 +03:00
|
|
|
|
|
|
|
std::ofstream out(name, std::ofstream::binary);
|
|
|
|
out.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
2013-07-29 15:15:50 +03:00
|
|
|
out.write(reinterpret_cast<char*>(data.first.get()), data.second);
|
2012-08-09 10:40:47 +03:00
|
|
|
}
|
2012-09-15 22:16:16 +03:00
|
|
|
|
2012-08-09 10:40:47 +03:00
|
|
|
current->open(name);
|
|
|
|
return true;
|
2011-03-19 16:27:51 +02:00
|
|
|
}
|
2012-08-09 10:40:47 +03:00
|
|
|
catch(std::exception &e)
|
2010-07-20 13:46:38 +03:00
|
|
|
{
|
2012-08-09 10:40:47 +03:00
|
|
|
current = nullptr;
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->warnStream() << "Failed to open video file " << name << ": " << e.what();
|
2010-07-20 13:46:38 +03:00
|
|
|
}
|
|
|
|
|
2012-08-09 10:40:47 +03:00
|
|
|
return false;
|
2009-06-24 15:14:48 +03:00
|
|
|
}
|
|
|
|
|
2009-06-24 09:56:36 +03:00
|
|
|
void CVideoPlayer::close()
|
|
|
|
{
|
|
|
|
if(!current)
|
2009-06-22 14:12:40 +03:00
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->warnStream() << "Closing no opened player...?";
|
2009-06-24 09:56:36 +03:00
|
|
|
return;
|
2009-06-22 14:12:40 +03:00
|
|
|
}
|
|
|
|
|
2009-06-24 09:56:36 +03:00
|
|
|
current->close();
|
2013-06-26 14:18:27 +03:00
|
|
|
current = nullptr;
|
2009-06-24 09:56:36 +03:00
|
|
|
if(!DeleteFileA(fname.c_str()))
|
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Cannot remove temporarily extracted video file: " << fname;
|
2009-06-24 09:56:36 +03:00
|
|
|
checkForError(false);
|
|
|
|
}
|
|
|
|
fname.clear();
|
|
|
|
}
|
|
|
|
|
2011-08-08 17:37:18 +03:00
|
|
|
bool CVideoPlayer::nextFrame()
|
2009-06-24 09:56:36 +03:00
|
|
|
{
|
2010-07-20 13:46:38 +03:00
|
|
|
if(current)
|
2011-08-08 17:37:18 +03:00
|
|
|
{
|
2010-07-20 13:46:38 +03:00
|
|
|
current->nextFrame();
|
2011-08-08 17:37:18 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2009-06-24 09:56:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CVideoPlayer::show(int x, int y, SDL_Surface *dst, bool update)
|
|
|
|
{
|
2010-07-20 13:46:38 +03:00
|
|
|
if(current)
|
|
|
|
current->show(x, y, dst, update);
|
2009-06-24 09:56:36 +03:00
|
|
|
}
|
|
|
|
|
2009-06-24 15:14:48 +03:00
|
|
|
bool CVideoPlayer::wait()
|
|
|
|
{
|
2010-07-20 13:46:38 +03:00
|
|
|
if(current)
|
|
|
|
return current->wait();
|
|
|
|
else
|
|
|
|
return false;
|
2009-06-24 15:14:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int CVideoPlayer::curFrame() const
|
|
|
|
{
|
2010-07-20 13:46:38 +03:00
|
|
|
if(current)
|
|
|
|
return current->curFrame();
|
|
|
|
else
|
|
|
|
return -1;
|
2009-06-24 15:14:48 +03:00
|
|
|
}
|
|
|
|
|
2009-06-24 09:56:36 +03:00
|
|
|
int CVideoPlayer::frameCount() const
|
2009-06-22 14:12:40 +03:00
|
|
|
{
|
2010-07-20 13:46:38 +03:00
|
|
|
if(current)
|
|
|
|
return current->frameCount();
|
|
|
|
else
|
|
|
|
return -1;
|
2009-06-24 09:56:36 +03:00
|
|
|
}
|
2009-06-22 14:12:40 +03:00
|
|
|
|
2009-06-24 09:56:36 +03:00
|
|
|
bool CVideoPlayer::openAndPlayVideo(std::string name, int x, int y, SDL_Surface *dst, bool stopOnKey)
|
|
|
|
{
|
2010-07-20 13:46:38 +03:00
|
|
|
if(!open(name))
|
|
|
|
return false;
|
|
|
|
|
2009-06-24 09:56:36 +03:00
|
|
|
bool ret = playVideo(x, y, dst, stopOnKey);
|
|
|
|
close();
|
|
|
|
return ret;
|
|
|
|
}
|
2009-06-22 14:12:40 +03:00
|
|
|
|
2009-06-24 12:17:33 +03:00
|
|
|
void CVideoPlayer::update( int x, int y, SDL_Surface *dst, bool forceRedraw, bool update )
|
2009-06-24 09:56:36 +03:00
|
|
|
{
|
2010-07-23 15:02:15 +03:00
|
|
|
if(!current)
|
2010-07-20 13:46:38 +03:00
|
|
|
return;
|
|
|
|
|
2009-06-24 12:17:33 +03:00
|
|
|
bool w = false;
|
|
|
|
if(!first)
|
|
|
|
{
|
|
|
|
w = wait(); //check if should keep current frame
|
|
|
|
if(!w)
|
|
|
|
nextFrame();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
2009-06-23 11:14:49 +03:00
|
|
|
|
|
|
|
|
2012-09-15 22:16:16 +03:00
|
|
|
if(!w)
|
2009-06-24 12:17:33 +03:00
|
|
|
{
|
2009-06-24 09:56:36 +03:00
|
|
|
show(x,y,dst,update);
|
2009-06-24 12:17:33 +03:00
|
|
|
}
|
|
|
|
else if (forceRedraw)
|
|
|
|
{
|
|
|
|
redraw(x, y, dst, update);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVideoPlayer::redraw( int x, int y, SDL_Surface *dst, bool update )
|
|
|
|
{
|
2010-07-20 13:46:38 +03:00
|
|
|
if(current)
|
|
|
|
current->redraw(x, y, dst, update);
|
2009-06-24 09:56:36 +03:00
|
|
|
}
|
2009-06-22 14:12:40 +03:00
|
|
|
|
2009-06-24 09:56:36 +03:00
|
|
|
bool CVideoPlayer::playVideo(int x, int y, SDL_Surface *dst, bool stopOnKey)
|
|
|
|
{
|
2010-07-20 13:46:38 +03:00
|
|
|
if(!current)
|
|
|
|
return false;
|
|
|
|
|
2009-06-24 09:56:36 +03:00
|
|
|
int frame = 0;
|
|
|
|
while(frame < frameCount()) //play all frames
|
|
|
|
{
|
|
|
|
if(stopOnKey && keyDown())
|
|
|
|
return false;
|
2009-06-23 11:14:49 +03:00
|
|
|
|
2009-06-24 09:56:36 +03:00
|
|
|
if(!wait())
|
2009-06-23 11:14:49 +03:00
|
|
|
{
|
2009-06-24 09:56:36 +03:00
|
|
|
show(x, y, dst);
|
|
|
|
nextFrame();
|
|
|
|
frame++;
|
2009-06-23 11:14:49 +03:00
|
|
|
}
|
2009-06-24 09:56:36 +03:00
|
|
|
SDL_Delay(20);
|
2009-06-22 14:12:40 +03:00
|
|
|
}
|
2009-06-24 09:56:36 +03:00
|
|
|
|
2009-06-22 14:12:40 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-06-20 04:34:26 +03:00
|
|
|
#else
|
|
|
|
|
2012-08-18 13:29:54 +03:00
|
|
|
#ifndef DISABLE_VIDEO
|
|
|
|
|
2012-08-21 20:37:06 +03:00
|
|
|
// Define a set of functions to read data
|
|
|
|
static int lodRead(void* opaque, uint8_t* buf, int size)
|
|
|
|
{
|
|
|
|
auto video = reinterpret_cast<CVideoPlayer *>(opaque);
|
|
|
|
|
2013-02-06 02:11:48 +03:00
|
|
|
return video->data->read(buf, size);
|
2009-06-20 04:34:26 +03:00
|
|
|
}
|
|
|
|
|
2012-08-21 20:37:06 +03:00
|
|
|
static si64 lodSeek(void * opaque, si64 pos, int whence)
|
|
|
|
{
|
|
|
|
auto video = reinterpret_cast<CVideoPlayer *>(opaque);
|
|
|
|
|
|
|
|
if (whence & AVSEEK_SIZE)
|
2013-02-06 02:11:48 +03:00
|
|
|
return video->data->getSize();
|
2009-06-20 04:34:26 +03:00
|
|
|
|
2013-02-06 02:11:48 +03:00
|
|
|
return video->data->seek(pos);
|
2009-06-20 04:34:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CVideoPlayer::CVideoPlayer()
|
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
format = nullptr;
|
|
|
|
frame = nullptr;
|
|
|
|
codec = nullptr;
|
|
|
|
sws = nullptr;
|
|
|
|
overlay = nullptr;
|
|
|
|
dest = nullptr;
|
2012-08-21 20:37:06 +03:00
|
|
|
context = nullptr;
|
2009-06-20 04:34:26 +03:00
|
|
|
|
|
|
|
// Register codecs. TODO: May be overkill. Should call a
|
|
|
|
// combination of av_register_input_format() /
|
|
|
|
// av_register_output_format() / av_register_protocol() instead.
|
|
|
|
av_register_all();
|
|
|
|
}
|
|
|
|
|
2011-08-08 17:20:22 +03:00
|
|
|
bool CVideoPlayer::open(std::string fname)
|
|
|
|
{
|
|
|
|
return open(fname, true, false);
|
|
|
|
}
|
|
|
|
|
2009-06-28 02:33:59 +03:00
|
|
|
// loop = to loop through the video
|
|
|
|
// useOverlay = directly write to the screen.
|
|
|
|
bool CVideoPlayer::open(std::string fname, bool loop, bool useOverlay)
|
2009-06-20 04:34:26 +03:00
|
|
|
{
|
|
|
|
close();
|
2012-09-15 22:16:16 +03:00
|
|
|
|
2011-04-17 21:02:13 +03:00
|
|
|
this->fname = fname;
|
2009-06-28 02:33:59 +03:00
|
|
|
refreshWait = 3;
|
|
|
|
refreshCount = -1;
|
|
|
|
doLoop = loop;
|
2009-06-20 04:34:26 +03:00
|
|
|
|
2012-08-11 14:34:49 +03:00
|
|
|
ResourceID resource(std::string("Video/") + fname, EResType::VIDEO);
|
2012-08-08 15:17:25 +03:00
|
|
|
|
2013-02-06 02:11:48 +03:00
|
|
|
if (!CResourceHandler::get()->existsResource(resource))
|
2013-02-27 18:46:14 +03:00
|
|
|
{
|
2013-04-09 17:31:36 +03:00
|
|
|
logGlobal->errorStream() << "Error: video " << resource.getName() << " was not found";
|
2012-08-08 15:17:25 +03:00
|
|
|
return false;
|
2013-02-27 18:46:14 +03:00
|
|
|
}
|
2009-06-20 04:34:26 +03:00
|
|
|
|
2013-02-06 02:11:48 +03:00
|
|
|
data = CResourceHandler::get()->load(resource);
|
2012-08-21 20:37:06 +03:00
|
|
|
|
|
|
|
static const int BUFFER_SIZE = 4096;
|
2013-02-06 02:11:48 +03:00
|
|
|
|
|
|
|
unsigned char * buffer = (unsigned char *)av_malloc(BUFFER_SIZE);// will be freed by ffmpeg
|
2013-06-26 14:18:27 +03:00
|
|
|
context = avio_alloc_context( buffer, BUFFER_SIZE, 0, (void *)this, lodRead, nullptr, lodSeek);
|
2012-08-21 20:37:06 +03:00
|
|
|
|
|
|
|
format = avformat_alloc_context();
|
|
|
|
format->pb = context;
|
|
|
|
// filename is not needed - file was already open and stored in this->data;
|
|
|
|
int avfopen = avformat_open_input(&format, "dummyFilename", nullptr, nullptr);
|
|
|
|
|
2012-05-13 18:04:21 +03:00
|
|
|
if (avfopen != 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-06-20 04:34:26 +03:00
|
|
|
// Retrieve stream information
|
2012-05-25 14:49:56 +03:00
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(53, 17, 0)
|
2009-06-20 04:34:26 +03:00
|
|
|
if (av_find_stream_info(format) < 0)
|
2012-05-13 18:04:21 +03:00
|
|
|
#else
|
2013-06-26 14:18:27 +03:00
|
|
|
if (avformat_find_stream_info(format, nullptr) < 0)
|
2012-05-13 18:04:21 +03:00
|
|
|
#endif
|
2009-06-20 04:34:26 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Find the first video stream
|
|
|
|
stream = -1;
|
2012-05-13 18:04:21 +03:00
|
|
|
for(ui32 i=0; i<format->nb_streams; i++)
|
|
|
|
{
|
2011-07-05 22:06:47 +03:00
|
|
|
if (format->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
|
|
|
|
{
|
2009-06-20 04:34:26 +03:00
|
|
|
stream = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream < 0)
|
|
|
|
// No video stream in that file
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Get a pointer to the codec context for the video stream
|
|
|
|
codecContext = format->streams[stream]->codec;
|
|
|
|
|
|
|
|
// Find the decoder for the video stream
|
|
|
|
codec = avcodec_find_decoder(codecContext->codec_id);
|
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
if (codec == nullptr)
|
2012-05-13 18:04:21 +03:00
|
|
|
{
|
2009-06-20 04:34:26 +03:00
|
|
|
// Unsupported codec
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-15 22:16:16 +03:00
|
|
|
|
2009-06-20 04:34:26 +03:00
|
|
|
// Open codec
|
2012-08-10 11:49:18 +03:00
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(53, 6, 0)
|
2012-05-13 18:04:21 +03:00
|
|
|
if ( avcodec_open(codecContext, codec) < 0 )
|
|
|
|
#else
|
2013-06-26 14:18:27 +03:00
|
|
|
if ( avcodec_open2(codecContext, codec, nullptr) < 0 )
|
2012-05-13 18:04:21 +03:00
|
|
|
#endif
|
|
|
|
{
|
2009-06-20 04:34:26 +03:00
|
|
|
// Could not open codec
|
2013-06-26 14:18:27 +03:00
|
|
|
codec = nullptr;
|
2009-06-20 04:34:26 +03:00
|
|
|
return false;
|
|
|
|
}
|
2012-09-15 22:16:16 +03:00
|
|
|
|
2009-06-20 04:34:26 +03:00
|
|
|
// Allocate video frame
|
|
|
|
frame = avcodec_alloc_frame();
|
|
|
|
|
|
|
|
// Allocate a place to put our YUV image on that screen
|
2012-05-13 18:04:21 +03:00
|
|
|
if (useOverlay)
|
|
|
|
{
|
2009-06-28 02:33:59 +03:00
|
|
|
overlay = SDL_CreateYUVOverlay(codecContext->width, codecContext->height,
|
|
|
|
SDL_YV12_OVERLAY, screen);
|
2012-05-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-28 02:33:59 +03:00
|
|
|
dest = CSDL_Ext::newSurface(codecContext->width, codecContext->height);
|
|
|
|
destRect.x = destRect.y = 0;
|
|
|
|
destRect.w = codecContext->width;
|
|
|
|
destRect.h = codecContext->height;
|
|
|
|
}
|
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
if (overlay == nullptr && dest == nullptr)
|
2009-06-28 02:33:59 +03:00
|
|
|
return false;
|
2009-06-20 04:34:26 +03:00
|
|
|
|
|
|
|
// Convert the image into YUV format that SDL uses
|
2012-05-13 18:04:21 +03:00
|
|
|
if (overlay)
|
|
|
|
{
|
2012-09-15 22:16:16 +03:00
|
|
|
sws = sws_getContext(codecContext->width, codecContext->height,
|
|
|
|
codecContext->pix_fmt, codecContext->width, codecContext->height,
|
2013-06-26 14:18:27 +03:00
|
|
|
PIX_FMT_YUV420P, SWS_BICUBIC, nullptr, nullptr, nullptr);
|
2012-05-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-26 16:30:29 +03:00
|
|
|
|
2010-08-26 19:55:42 +03:00
|
|
|
PixelFormat screenFormat = PIX_FMT_NONE;
|
2012-12-03 19:00:17 +03:00
|
|
|
if (screen->format->Bshift > screen->format->Rshift)
|
2010-08-26 19:55:42 +03:00
|
|
|
{
|
2012-12-01 09:30:52 +03:00
|
|
|
// this a BGR surface
|
|
|
|
switch (screen->format->BytesPerPixel)
|
|
|
|
{
|
|
|
|
case 2: screenFormat = PIX_FMT_BGR565; break;
|
|
|
|
case 3: screenFormat = PIX_FMT_BGR24; break;
|
|
|
|
case 4: screenFormat = PIX_FMT_BGR32; break;
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// this a RGB surface
|
|
|
|
switch (screen->format->BytesPerPixel)
|
|
|
|
{
|
|
|
|
case 2: screenFormat = PIX_FMT_RGB565; break;
|
|
|
|
case 3: screenFormat = PIX_FMT_RGB24; break;
|
|
|
|
case 4: screenFormat = PIX_FMT_RGB32; break;
|
|
|
|
default: return false;
|
|
|
|
}
|
2010-08-26 19:55:42 +03:00
|
|
|
}
|
|
|
|
|
2012-09-15 22:16:16 +03:00
|
|
|
sws = sws_getContext(codecContext->width, codecContext->height,
|
|
|
|
codecContext->pix_fmt, codecContext->width, codecContext->height,
|
2013-06-26 14:18:27 +03:00
|
|
|
screenFormat, SWS_BICUBIC, nullptr, nullptr, nullptr);
|
2009-06-28 02:33:59 +03:00
|
|
|
}
|
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
if (sws == nullptr)
|
2009-06-20 04:34:26 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
pos.w = codecContext->width;
|
|
|
|
pos.h = codecContext->height;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-06-28 02:33:59 +03:00
|
|
|
// Read the next frame. Return false on error/end of file.
|
2009-06-20 04:34:26 +03:00
|
|
|
bool CVideoPlayer::nextFrame()
|
|
|
|
{
|
|
|
|
AVPacket packet;
|
|
|
|
int frameFinished = 0;
|
2009-06-28 02:33:59 +03:00
|
|
|
bool gotError = false;
|
2009-06-20 04:34:26 +03:00
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
if (sws == nullptr)
|
2009-06-28 02:33:59 +03:00
|
|
|
return false;
|
2009-06-20 04:34:26 +03:00
|
|
|
|
2012-05-13 18:04:21 +03:00
|
|
|
while(!frameFinished)
|
|
|
|
{
|
2009-06-28 02:33:59 +03:00
|
|
|
int ret = av_read_frame(format, &packet);
|
2012-05-13 18:04:21 +03:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2009-06-28 02:33:59 +03:00
|
|
|
// Error. It's probably an end of file.
|
2012-05-13 18:04:21 +03:00
|
|
|
if (doLoop && !gotError)
|
|
|
|
{
|
2009-06-28 02:33:59 +03:00
|
|
|
// Rewind
|
|
|
|
if (av_seek_frame(format, stream, 0, 0) < 0)
|
|
|
|
break;
|
|
|
|
gotError = true;
|
2012-05-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-28 02:33:59 +03:00
|
|
|
break;
|
|
|
|
}
|
2012-05-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-28 02:33:59 +03:00
|
|
|
// Is this a packet from the video stream?
|
2012-05-13 18:04:21 +03:00
|
|
|
if (packet.stream_index == stream)
|
|
|
|
{
|
2009-06-28 02:33:59 +03:00
|
|
|
// Decode video frame
|
2012-07-03 00:51:48 +03:00
|
|
|
|
2011-03-20 00:27:05 +02:00
|
|
|
avcodec_decode_video2(codecContext, frame, &frameFinished, &packet);
|
2009-06-28 02:33:59 +03:00
|
|
|
|
|
|
|
// Did we get a video frame?
|
2012-05-13 18:04:21 +03:00
|
|
|
if (frameFinished)
|
|
|
|
{
|
2009-06-28 02:33:59 +03:00
|
|
|
AVPicture pict;
|
|
|
|
|
|
|
|
if (overlay) {
|
|
|
|
SDL_LockYUVOverlay(overlay);
|
2012-09-15 22:16:16 +03:00
|
|
|
|
2009-06-28 02:33:59 +03:00
|
|
|
pict.data[0] = overlay->pixels[0];
|
|
|
|
pict.data[1] = overlay->pixels[2];
|
|
|
|
pict.data[2] = overlay->pixels[1];
|
|
|
|
|
|
|
|
pict.linesize[0] = overlay->pitches[0];
|
|
|
|
pict.linesize[1] = overlay->pitches[2];
|
|
|
|
pict.linesize[2] = overlay->pitches[1];
|
|
|
|
|
|
|
|
sws_scale(sws, frame->data, frame->linesize,
|
|
|
|
0, codecContext->height, pict.data, pict.linesize);
|
|
|
|
|
|
|
|
SDL_UnlockYUVOverlay(overlay);
|
2012-05-13 18:04:21 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
pict.data[0] = (ui8 *)dest->pixels;
|
2009-06-28 02:33:59 +03:00
|
|
|
pict.linesize[0] = dest->pitch;
|
|
|
|
|
|
|
|
sws_scale(sws, frame->data, frame->linesize,
|
|
|
|
0, codecContext->height, pict.data, pict.linesize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-20 04:34:26 +03:00
|
|
|
|
2009-06-28 02:33:59 +03:00
|
|
|
av_free_packet(&packet);
|
|
|
|
}
|
|
|
|
}
|
2009-06-20 04:34:26 +03:00
|
|
|
|
2009-06-28 02:33:59 +03:00
|
|
|
return frameFinished != 0;
|
|
|
|
}
|
2009-06-20 04:34:26 +03:00
|
|
|
|
2009-06-28 02:33:59 +03:00
|
|
|
void CVideoPlayer::show( int x, int y, SDL_Surface *dst, bool update )
|
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
if (sws == nullptr)
|
2009-06-28 02:33:59 +03:00
|
|
|
return;
|
2009-06-20 04:34:26 +03:00
|
|
|
|
2009-06-28 02:33:59 +03:00
|
|
|
pos.x = x;
|
|
|
|
pos.y = y;
|
2010-08-23 22:00:14 +03:00
|
|
|
CSDL_Ext::blitSurface(dest, &destRect, dst, &pos);
|
2012-09-15 22:16:16 +03:00
|
|
|
|
2009-06-28 02:33:59 +03:00
|
|
|
if (update)
|
|
|
|
SDL_UpdateRect(dst, pos.x, pos.y, pos.w, pos.h);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVideoPlayer::redraw( int x, int y, SDL_Surface *dst, bool update )
|
|
|
|
{
|
|
|
|
show(x, y, dst, update);
|
|
|
|
}
|
2009-06-20 04:34:26 +03:00
|
|
|
|
2009-06-28 02:33:59 +03:00
|
|
|
void CVideoPlayer::update( int x, int y, SDL_Surface *dst, bool forceRedraw, bool update )
|
|
|
|
{
|
2013-06-26 14:18:27 +03:00
|
|
|
if (sws == nullptr)
|
2009-06-28 02:33:59 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (refreshCount <= 0)
|
|
|
|
{
|
|
|
|
refreshCount = refreshWait;
|
|
|
|
if (nextFrame())
|
|
|
|
show(x,y,dst,update);
|
2012-05-13 18:04:21 +03:00
|
|
|
else
|
|
|
|
{
|
2011-04-17 21:02:13 +03:00
|
|
|
open(fname);
|
2012-09-15 22:16:16 +03:00
|
|
|
nextFrame();
|
|
|
|
|
2011-04-26 16:30:29 +03:00
|
|
|
// The y position is wrong at the first frame.
|
|
|
|
// Note: either the windows player or the linux player is
|
2012-09-15 22:16:16 +03:00
|
|
|
// broken. Compensate here until the bug is found.
|
2011-04-26 16:30:29 +03:00
|
|
|
show(x, y--, dst, update);
|
|
|
|
}
|
2012-09-15 22:16:16 +03:00
|
|
|
}
|
2012-05-13 18:04:21 +03:00
|
|
|
else
|
|
|
|
{
|
2009-06-28 02:33:59 +03:00
|
|
|
redraw(x, y, dst, update);
|
2009-06-20 04:34:26 +03:00
|
|
|
}
|
|
|
|
|
2009-06-28 02:33:59 +03:00
|
|
|
refreshCount --;
|
2009-06-20 04:34:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CVideoPlayer::close()
|
|
|
|
{
|
2012-09-15 22:16:16 +03:00
|
|
|
fname = "";
|
2012-05-13 18:04:21 +03:00
|
|
|
if (sws)
|
|
|
|
{
|
2009-06-20 04:34:26 +03:00
|
|
|
sws_freeContext(sws);
|
2013-06-26 14:18:27 +03:00
|
|
|
sws = nullptr;
|
2009-06-20 04:34:26 +03:00
|
|
|
}
|
|
|
|
|
2012-05-13 18:04:21 +03:00
|
|
|
if (overlay)
|
|
|
|
{
|
2009-06-20 04:34:26 +03:00
|
|
|
SDL_FreeYUVOverlay(overlay);
|
2013-06-26 14:18:27 +03:00
|
|
|
overlay = nullptr;
|
2009-06-20 04:34:26 +03:00
|
|
|
}
|
|
|
|
|
2012-05-13 18:04:21 +03:00
|
|
|
if (dest)
|
|
|
|
{
|
2009-06-28 02:33:59 +03:00
|
|
|
SDL_FreeSurface(dest);
|
2013-06-26 14:18:27 +03:00
|
|
|
dest = nullptr;
|
2009-06-28 02:33:59 +03:00
|
|
|
}
|
|
|
|
|
2012-05-13 18:04:21 +03:00
|
|
|
if (frame)
|
|
|
|
{
|
2009-06-20 04:34:26 +03:00
|
|
|
av_free(frame);
|
2013-06-26 14:18:27 +03:00
|
|
|
frame = nullptr;
|
2009-06-20 04:34:26 +03:00
|
|
|
}
|
|
|
|
|
2012-05-13 18:04:21 +03:00
|
|
|
if (codec)
|
|
|
|
{
|
2009-06-20 04:34:26 +03:00
|
|
|
avcodec_close(codecContext);
|
2013-06-26 14:18:27 +03:00
|
|
|
codec = nullptr;
|
|
|
|
codecContext = nullptr;
|
2009-06-20 04:34:26 +03:00
|
|
|
}
|
|
|
|
|
2012-05-13 18:04:21 +03:00
|
|
|
if (format)
|
|
|
|
{
|
2012-05-25 14:49:56 +03:00
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(53, 17, 0)
|
2009-06-20 04:34:26 +03:00
|
|
|
av_close_input_file(format);
|
2013-06-26 14:18:27 +03:00
|
|
|
format = nullptr;
|
2012-05-13 18:04:21 +03:00
|
|
|
#else
|
|
|
|
avformat_close_input(&format);
|
|
|
|
#endif
|
2009-06-20 04:34:26 +03:00
|
|
|
}
|
2012-08-21 20:37:06 +03:00
|
|
|
|
|
|
|
if (context)
|
|
|
|
{
|
|
|
|
av_free(context);
|
|
|
|
context = nullptr;
|
|
|
|
}
|
2009-06-20 04:34:26 +03:00
|
|
|
}
|
2009-06-28 02:33:59 +03:00
|
|
|
|
|
|
|
// Plays a video. Only works for overlays.
|
|
|
|
bool CVideoPlayer::playVideo(int x, int y, SDL_Surface *dst, bool stopOnKey)
|
|
|
|
{
|
|
|
|
// Note: either the windows player or the linux player is
|
|
|
|
// broken. Compensate here until the bug is found.
|
|
|
|
y--;
|
|
|
|
|
|
|
|
pos.x = x;
|
|
|
|
pos.y = y;
|
|
|
|
|
2012-05-13 18:04:21 +03:00
|
|
|
while(nextFrame())
|
|
|
|
{
|
2012-09-15 22:16:16 +03:00
|
|
|
|
2009-06-28 02:33:59 +03:00
|
|
|
if(stopOnKey && keyDown())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SDL_DisplayYUVOverlay(overlay, &pos);
|
|
|
|
|
|
|
|
// Wait 3 frames
|
2011-04-07 05:16:32 +03:00
|
|
|
GH.mainFPSmng->framerateDelay();
|
|
|
|
GH.mainFPSmng->framerateDelay();
|
|
|
|
GH.mainFPSmng->framerateDelay();
|
2009-06-28 02:33:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CVideoPlayer::openAndPlayVideo(std::string name, int x, int y, SDL_Surface *dst, bool stopOnKey)
|
|
|
|
{
|
|
|
|
open(name, false, true);
|
|
|
|
bool ret = playVideo(x, y, dst, stopOnKey);
|
|
|
|
close();
|
|
|
|
return ret;
|
|
|
|
}
|
2012-09-15 22:16:16 +03:00
|
|
|
|
2009-06-20 04:34:26 +03:00
|
|
|
CVideoPlayer::~CVideoPlayer()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
#endif
|
2012-08-18 13:29:54 +03:00
|
|
|
|
|
|
|
#endif
|