2008-01-09 19:21:31 +02:00
|
|
|
#include "../stdafx.h"
|
2007-08-17 20:42:21 +03:00
|
|
|
#include <iostream>
|
2009-06-20 04:34:26 +03:00
|
|
|
#include "CSndHandler.h"
|
2007-08-08 22:28:56 +03:00
|
|
|
#include "CVideoHandler.h"
|
2009-06-20 04:34:26 +03:00
|
|
|
#include <SDL.h>
|
|
|
|
|
2009-06-20 17:52:20 +03:00
|
|
|
#ifdef _WIN32
|
2008-07-16 21:26:15 +03:00
|
|
|
|
2009-06-22 14:12:40 +03:00
|
|
|
#include "../client/SDL_Extensions.h"
|
2009-06-24 09:56:36 +03:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2009-06-22 14:12:40 +03:00
|
|
|
|
2009-06-23 11:14:49 +03:00
|
|
|
|
2009-06-24 09:56:36 +03:00
|
|
|
void checkForError(bool throwing = true)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
int error = GetLastError();
|
|
|
|
if(!error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
tlog1 << "Error " << error << " encountered!\n";
|
|
|
|
std::string msg;
|
|
|
|
char* pTemp = NULL;
|
|
|
|
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
|
|
|
|
NULL, error, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), (LPSTR)&pTemp, 1, NULL );
|
|
|
|
tlog1 << "Error: " << pTemp << std::endl;
|
|
|
|
msg = pTemp;
|
|
|
|
LocalFree( pTemp );
|
|
|
|
pTemp = NULL;
|
|
|
|
if(throwing)
|
|
|
|
throw msg;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void blitBuffer(char *buffer, int x, int y, int w, int h, SDL_Surface *dst)
|
|
|
|
{
|
|
|
|
const int bpp = dst->format->BytesPerPixel;
|
|
|
|
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;
|
2009-06-20 17:52:20 +03:00
|
|
|
#ifdef _WIN32
|
2007-08-08 22:28:56 +03:00
|
|
|
dll = LoadLibraryA(filename);
|
2009-06-24 09:56:36 +03:00
|
|
|
if(!dll)
|
|
|
|
{
|
|
|
|
tlog1 << "Failed loading " << filename << std::endl;
|
|
|
|
checkForError();
|
|
|
|
}
|
2008-07-16 21:26:15 +03:00
|
|
|
#else
|
|
|
|
dll = dlopen(filename,RTLD_LOCAL | RTLD_LAZY);
|
|
|
|
#endif
|
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;
|
2009-06-20 17:52:20 +03:00
|
|
|
#ifdef _WIN32
|
2009-06-24 09:56:36 +03:00
|
|
|
ret = (void*) GetProcAddress(dll,symbol);
|
|
|
|
if(!ret)
|
|
|
|
{
|
|
|
|
tlog1 << "Failed to find " << symbol << " in " << name << std::endl;
|
|
|
|
checkForError();
|
|
|
|
}
|
2008-07-16 21:26:15 +03:00
|
|
|
#else
|
2009-06-24 09:56:36 +03:00
|
|
|
ret = (void *)dlsym(dll, symbol);
|
2008-07-16 21:26:15 +03:00
|
|
|
#endif
|
2009-06-24 09:56:36 +03:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2009-06-24 09:56:36 +03:00
|
|
|
if(!FreeLibrary(dll))
|
|
|
|
{
|
|
|
|
tlog1 << "Failed to free " << name << std::endl;
|
|
|
|
checkForError();
|
|
|
|
}
|
2009-06-23 11:14:49 +03:00
|
|
|
#else
|
|
|
|
dlclose(dll);
|
|
|
|
#endif
|
|
|
|
}
|
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
|
|
|
{
|
2009-06-23 11:14:49 +03:00
|
|
|
dll = NULL;
|
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");
|
|
|
|
|
|
|
|
hBinkFile = NULL;
|
|
|
|
hBink = NULL;
|
2009-06-23 11:14:49 +03:00
|
|
|
}
|
|
|
|
|
2007-08-08 22:28:56 +03:00
|
|
|
void CBIKHandler::open(std::string name)
|
|
|
|
{
|
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
|
|
|
|
NULL, // Security Descriptor
|
|
|
|
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)
|
|
|
|
{
|
2009-06-24 09:56:36 +03:00
|
|
|
tlog1 << "BIK handler: failed to open " << name << std::endl;
|
2009-06-23 11:14:49 +03:00
|
|
|
checkForError();
|
2009-06-24 09:56:36 +03:00
|
|
|
return;
|
2007-08-17 20:42:21 +03:00
|
|
|
}
|
2007-08-08 22:28:56 +03:00
|
|
|
|
2009-06-24 09:56:36 +03:00
|
|
|
void *waveout = GetProcAddress(dll,"_BinkOpenWaveOut@4");
|
2009-06-23 11:14:49 +03:00
|
|
|
if(waveout)
|
|
|
|
binkSetSoundSystem(waveout,NULL);
|
2007-08-08 22:28:56 +03:00
|
|
|
|
2009-06-23 11:14:49 +03:00
|
|
|
hBink = binkOpen(hBinkFile, 0x8a800000);
|
2009-06-24 09:56:36 +03:00
|
|
|
buffer = new char[hBink->width * hBink->width * 3];
|
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
|
|
|
{
|
|
|
|
int w = hBink->width, h = hBink->height;
|
|
|
|
binkDoFrame(hBink);
|
|
|
|
binkCopyToBuffer(hBink, buffer, w*3, h, 0, 0, 0);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void CBIKHandler::nextFrame()
|
|
|
|
{
|
|
|
|
binkNextFrame(hBink);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBIKHandler::close()
|
|
|
|
{
|
2009-06-24 09:56:36 +03:00
|
|
|
binkClose(hBink);
|
|
|
|
hBink = NULL;
|
|
|
|
CloseHandle(hBinkFile);
|
|
|
|
hBinkFile = NULL;
|
2009-06-23 11:14:49 +03:00
|
|
|
delete [] buffer;
|
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-06-22 14:12:40 +03:00
|
|
|
void CSmackPlayer::nextFrame()
|
|
|
|
{
|
|
|
|
ptrSmackNextFrame(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSmackPlayer::wait()
|
|
|
|
{
|
2009-06-23 11:14:49 +03:00
|
|
|
return ptrSmackWait(data);
|
2009-06-22 14:12:40 +03:00
|
|
|
}
|
|
|
|
|
2009-06-24 09:56:36 +03:00
|
|
|
CSmackPlayer::CSmackPlayer()
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
CSmackPlayer::~CSmackPlayer()
|
|
|
|
{
|
|
|
|
if(data)
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSmackPlayer::close()
|
|
|
|
{
|
|
|
|
ptrSmackClose(data);
|
|
|
|
data = NULL;
|
|
|
|
delete [] buffer;
|
|
|
|
buffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSmackPlayer::open( std::string name )
|
|
|
|
{
|
2009-06-24 15:14:48 +03:00
|
|
|
Uint32 flags[2] = {0xff400, 0xfe400};
|
|
|
|
|
2009-06-24 09:56:36 +03:00
|
|
|
data = ptrSmackOpen( (void*)name.c_str(), flags[1], -1);
|
|
|
|
if (!data)
|
|
|
|
{
|
|
|
|
tlog1 << "Smack cannot open " << name << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = new char[data->width*data->height*2];
|
|
|
|
buf = buffer+data->width*(data->height-1)*2; // adjust pointer position for later use by 'SmackToBuffer'
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSmackPlayer::show( int x, int y, SDL_Surface *dst, bool update)
|
|
|
|
{
|
|
|
|
int w = data->width, h = data->height;
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
int w = data->width, h = data->height;
|
2009-06-24 09:56:36 +03:00
|
|
|
/* Lock the screen for direct access to the pixels */
|
|
|
|
if ( SDL_MUSTLOCK(dst) )
|
|
|
|
{
|
|
|
|
if ( SDL_LockSurface(dst) < 0 )
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw the frame
|
|
|
|
Uint16* addr = (Uint16*) (buffer+w*(h-1)*2-2);
|
|
|
|
for( int j=0; j<h-1; j++) // why -1 ?
|
|
|
|
{
|
|
|
|
for ( int i=w-1; i>=0; i--)
|
|
|
|
{
|
|
|
|
Uint16 pixel = *addr;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
addr--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( SDL_MUSTLOCK(dst) )
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2009-06-22 14:12:40 +03:00
|
|
|
vidh = new CVidHandler(std::string(DATA_DIR "Data" PATHSEPARATOR "VIDEO.VID"));
|
2009-06-24 15:14:48 +03:00
|
|
|
current = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CVideoPlayer::~CVideoPlayer()
|
|
|
|
{
|
|
|
|
delete vidh;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVideoPlayer::open(std::string name)
|
|
|
|
{
|
|
|
|
if(boost::algorithm::ends_with(name, ".BIK"))
|
|
|
|
current = &bikPlayer;
|
|
|
|
else
|
|
|
|
current = &smkPlayer;
|
|
|
|
|
|
|
|
fname = name;
|
|
|
|
first = true;
|
|
|
|
|
|
|
|
//extract video from video.vid so we can play it
|
|
|
|
vidh->extract(name, name);
|
|
|
|
current->open(name);
|
|
|
|
}
|
|
|
|
|
2009-06-24 09:56:36 +03:00
|
|
|
void CVideoPlayer::close()
|
|
|
|
{
|
|
|
|
if(!current)
|
2009-06-22 14:12:40 +03:00
|
|
|
{
|
2009-06-24 09:56:36 +03:00
|
|
|
tlog2 << "Closing no opened player...?" << std::endl;
|
|
|
|
return;
|
2009-06-22 14:12:40 +03:00
|
|
|
}
|
|
|
|
|
2009-06-24 09:56:36 +03:00
|
|
|
current->close();
|
|
|
|
current = NULL;
|
|
|
|
if(!DeleteFileA(fname.c_str()))
|
|
|
|
{
|
|
|
|
tlog1 << "Cannot remove temporarily extracted video file: " << fname;
|
|
|
|
checkForError(false);
|
|
|
|
}
|
|
|
|
fname.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVideoPlayer::nextFrame()
|
|
|
|
{
|
|
|
|
current->nextFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVideoPlayer::show(int x, int y, SDL_Surface *dst, bool update)
|
|
|
|
{
|
|
|
|
current->show(x, y, dst, update);
|
|
|
|
}
|
|
|
|
|
2009-06-24 15:14:48 +03:00
|
|
|
bool CVideoPlayer::wait()
|
|
|
|
{
|
|
|
|
return current->wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
int CVideoPlayer::curFrame() const
|
|
|
|
{
|
|
|
|
return current->curFrame();
|
|
|
|
}
|
|
|
|
|
2009-06-24 09:56:36 +03:00
|
|
|
int CVideoPlayer::frameCount() const
|
2009-06-22 14:12:40 +03:00
|
|
|
{
|
2009-06-24 09:56:36 +03:00
|
|
|
return current->frameCount();
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
open(name);
|
|
|
|
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
|
|
|
{
|
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
|
|
|
|
|
|
|
|
2009-06-24 12:17:33 +03:00
|
|
|
if(!w)
|
|
|
|
{
|
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 )
|
|
|
|
{
|
|
|
|
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
|
|
|
//reads events and throws on key down
|
|
|
|
bool keyDown()
|
|
|
|
{
|
|
|
|
SDL_Event ev;
|
|
|
|
while(SDL_PollEvent(&ev))
|
|
|
|
{
|
|
|
|
if(ev.type == SDL_KEYDOWN || ev.type == SDL_MOUSEBUTTONDOWN)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
2009-06-21 16:44:15 +03:00
|
|
|
#include "../client/SDL_Extensions.h"
|
2009-06-20 04:34:26 +03:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
#include <libswscale/swscale.h>
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *protocol_name = "lod";
|
|
|
|
|
|
|
|
// Open a pseudo file. Name is something like 'lod:0x56432ab6c43df8fe'
|
|
|
|
static int lod_open(URLContext *context, const char *filename, int flags)
|
|
|
|
{
|
|
|
|
CVideoPlayer *video;
|
|
|
|
|
|
|
|
// Retrieve pointer to CVideoPlayer object
|
|
|
|
filename += strlen(protocol_name) + 1;
|
|
|
|
video = (CVideoPlayer *)(uintptr_t)strtoull(filename, NULL, 16);
|
|
|
|
|
|
|
|
// TODO: check flags ?
|
|
|
|
|
|
|
|
context->priv_data = video;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lod_close(URLContext* h)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define a set of functions to read data
|
|
|
|
static int lod_read(URLContext *context, unsigned char *buf, int size)
|
|
|
|
{
|
|
|
|
CVideoPlayer *video = (CVideoPlayer *)context->priv_data;
|
|
|
|
|
|
|
|
amin(size, video->length - video->offset);
|
|
|
|
|
|
|
|
if (size < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// TODO: can we avoid that copy ?
|
|
|
|
memcpy(buf, video->data + video->offset, size);
|
|
|
|
|
|
|
|
video->offset += size;
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static URLProtocol lod_protocol = {
|
|
|
|
protocol_name,
|
|
|
|
lod_open,
|
|
|
|
lod_read,
|
|
|
|
NULL, // no write
|
|
|
|
NULL, // no seek
|
|
|
|
lod_close
|
|
|
|
};
|
|
|
|
|
|
|
|
CVideoPlayer::CVideoPlayer()
|
|
|
|
{
|
|
|
|
format = NULL;
|
|
|
|
frame = NULL;
|
|
|
|
codec = NULL;
|
|
|
|
sws = NULL;
|
|
|
|
overlay = NULL;
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
// Register our protocol 'lod' so we can directly read from mmaped memory
|
|
|
|
av_register_protocol(&lod_protocol);
|
|
|
|
|
|
|
|
vidh = new CVidHandler(std::string(DATA_DIR "Data" PATHSEPARATOR "VIDEO.VID"));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CVideoPlayer::open(std::string fname, int x, int y)
|
|
|
|
{
|
|
|
|
char url[100];
|
|
|
|
|
|
|
|
close();
|
|
|
|
|
|
|
|
data = vidh->extract(fname, length);
|
|
|
|
|
|
|
|
if (data == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
|
|
|
|
// Create our URL name with the 'lod' protocol as a prefix and a
|
|
|
|
// back pointer to our object. Should be 32 and 64 bits compatible.
|
2009-06-20 05:18:27 +03:00
|
|
|
sprintf(url, "%s:0x%016llx", protocol_name, (unsigned long long)(uintptr_t)this);
|
2009-06-20 04:34:26 +03:00
|
|
|
|
|
|
|
if (av_open_input_file(&format, url, NULL, 0, NULL)!=0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Retrieve stream information
|
|
|
|
if (av_find_stream_info(format) < 0)
|
|
|
|
return false;
|
|
|
|
|
2009-06-20 05:18:27 +03:00
|
|
|
#if 0
|
2009-06-20 04:34:26 +03:00
|
|
|
// Dump information about file onto standard error
|
|
|
|
dump_format(format, 0, url, 0);
|
2009-06-20 05:18:27 +03:00
|
|
|
#endif
|
2009-06-20 04:34:26 +03:00
|
|
|
|
|
|
|
// Find the first video stream
|
|
|
|
stream = -1;
|
|
|
|
for(unsigned int i=0; i<format->nb_streams; i++) {
|
|
|
|
if (format->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (codec == NULL) {
|
|
|
|
// Unsupported codec
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open codec
|
|
|
|
if (avcodec_open(codecContext, codec) <0 ) {
|
|
|
|
// Could not open codec
|
|
|
|
codec = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate video frame
|
|
|
|
frame = avcodec_alloc_frame();
|
|
|
|
|
|
|
|
// Allocate a place to put our YUV image on that screen
|
|
|
|
overlay = SDL_CreateYUVOverlay(codecContext->width, codecContext->height,
|
|
|
|
SDL_YV12_OVERLAY, screen);
|
|
|
|
|
|
|
|
// Convert the image into YUV format that SDL uses
|
|
|
|
sws = sws_getContext(codecContext->width, codecContext->height,
|
|
|
|
codecContext->pix_fmt, codecContext->width, codecContext->height,
|
|
|
|
PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
|
|
|
|
if (sws == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
pos.x = x;
|
|
|
|
pos.y = y;
|
|
|
|
pos.w = codecContext->width;
|
|
|
|
pos.h = codecContext->height;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display the next frame. Return false on error/end of file.
|
|
|
|
bool CVideoPlayer::nextFrame()
|
|
|
|
{
|
|
|
|
AVPacket packet;
|
|
|
|
int frameFinished = 0;
|
|
|
|
|
|
|
|
while(!frameFinished && av_read_frame(format, &packet)>=0) {
|
|
|
|
|
|
|
|
// Is this a packet from the video stream?
|
|
|
|
if (packet.stream_index == stream) {
|
|
|
|
// Decode video frame
|
|
|
|
avcodec_decode_video(codecContext, frame, &frameFinished,
|
|
|
|
packet.data, packet.size);
|
|
|
|
|
|
|
|
// Did we get a video frame?
|
|
|
|
if (frameFinished) {
|
|
|
|
SDL_LockYUVOverlay(overlay);
|
|
|
|
|
|
|
|
AVPicture pict;
|
|
|
|
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);
|
|
|
|
|
|
|
|
SDL_DisplayYUVOverlay(overlay, &pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
av_free_packet(&packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return frameFinished != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVideoPlayer::close()
|
|
|
|
{
|
|
|
|
if (sws) {
|
|
|
|
sws_freeContext(sws);
|
|
|
|
sws = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (overlay) {
|
|
|
|
SDL_FreeYUVOverlay(overlay);
|
|
|
|
overlay = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame) {
|
|
|
|
av_free(frame);
|
|
|
|
frame = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (codec) {
|
|
|
|
avcodec_close(codecContext);
|
|
|
|
codec = NULL;
|
|
|
|
codecContext = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format) {
|
|
|
|
av_close_input_file(format);
|
|
|
|
format = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CVideoPlayer::~CVideoPlayer()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|