2019-11-20 16:21:32 +00:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
/*
|
|
|
|
Copyright (C) 2010-2019 EDuke32 developers and contributors
|
|
|
|
Copyright (C) 2019 sirlemonhead, Nuke.YKT
|
|
|
|
This file is part of PCExhumed.
|
|
|
|
PCExhumed is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License version 2
|
|
|
|
as published by the Free Software Foundation.
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
//-------------------------------------------------------------------------
|
2019-11-22 23:11:37 +00:00
|
|
|
#include "ns.h"
|
2019-08-26 03:59:14 +00:00
|
|
|
#include "engine.h"
|
|
|
|
#include "exhumed.h"
|
|
|
|
#include "names.h"
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2020-01-02 18:56:57 +00:00
|
|
|
#include "c_bind.h"
|
2019-12-05 21:18:34 +00:00
|
|
|
#include "sound.h"
|
2019-12-31 18:25:49 +00:00
|
|
|
#include "v_2ddrawer.h"
|
2020-04-12 05:46:53 +00:00
|
|
|
#include "animtexture.h"
|
2020-08-20 15:52:56 +00:00
|
|
|
#include "s_music.h"
|
2020-08-20 19:05:14 +00:00
|
|
|
#include "screenjob.h"
|
|
|
|
#include "v_draw.h"
|
2019-08-26 03:59:14 +00:00
|
|
|
|
2019-11-22 23:11:37 +00:00
|
|
|
BEGIN_PS_NS
|
|
|
|
|
2019-08-26 03:59:14 +00:00
|
|
|
|
2020-08-20 16:35:52 +00:00
|
|
|
class LMFPlayer
|
|
|
|
{
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
kFramePalette = 0,
|
|
|
|
kFrameSound,
|
|
|
|
kFrameImage,
|
|
|
|
kFrameDone
|
|
|
|
};
|
2019-08-26 03:59:14 +00:00
|
|
|
|
2020-08-20 16:35:52 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
kSampleRate = 22050,
|
|
|
|
kSampleSize = 2205,
|
|
|
|
numAudioBlocks = 20
|
|
|
|
};
|
2019-08-26 03:59:14 +00:00
|
|
|
|
2020-08-20 16:35:52 +00:00
|
|
|
struct AudioData
|
|
|
|
{
|
|
|
|
int16_t samples[kSampleSize * numAudioBlocks]; // must be a multiple of the stream buffer size
|
|
|
|
int nWrite;
|
|
|
|
int nRead;
|
|
|
|
};
|
2019-08-26 03:59:14 +00:00
|
|
|
|
2020-08-20 16:35:52 +00:00
|
|
|
uint8_t palette[768];
|
|
|
|
uint8_t CurFrame[64000];
|
2019-08-26 03:59:14 +00:00
|
|
|
|
2020-08-20 16:35:52 +00:00
|
|
|
SoundStream* stream = nullptr;
|
|
|
|
AudioData audio{};
|
2020-08-20 19:05:14 +00:00
|
|
|
AnimTextures animtex;
|
2020-08-20 15:52:56 +00:00
|
|
|
|
2020-08-20 16:35:52 +00:00
|
|
|
int nFrame = 0;
|
2020-08-20 15:52:56 +00:00
|
|
|
|
2020-08-20 16:35:52 +00:00
|
|
|
public:
|
|
|
|
int ReadFrame(FileReader& fp)
|
|
|
|
{
|
|
|
|
nFrame++;
|
2020-08-20 15:52:56 +00:00
|
|
|
|
2020-08-20 16:35:52 +00:00
|
|
|
uint8_t nType;
|
|
|
|
uint8_t var_1C;
|
|
|
|
int nSize;
|
|
|
|
uint16_t yOffset;
|
|
|
|
uint8_t xOffset;
|
|
|
|
uint8_t nPixels;
|
2019-10-12 21:09:55 +00:00
|
|
|
|
2020-08-20 16:35:52 +00:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (fp.Read(&nType, sizeof(nType)) == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2020-08-20 16:35:52 +00:00
|
|
|
fp.Read(&nSize, sizeof(nSize));
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2020-08-20 16:35:52 +00:00
|
|
|
nType--;
|
|
|
|
if (nType > 3)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2020-08-20 16:35:52 +00:00
|
|
|
switch (nType)
|
|
|
|
{
|
2019-08-31 07:47:15 +00:00
|
|
|
case kFramePalette:
|
|
|
|
{
|
2020-04-12 05:46:53 +00:00
|
|
|
fp.Read(palette, 768);
|
2019-11-24 12:59:36 +00:00
|
|
|
fp.Read(&var_1C, sizeof(var_1C));
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2020-08-20 16:35:52 +00:00
|
|
|
for (unsigned i = 0; i < 768; i++)
|
2020-04-12 05:46:53 +00:00
|
|
|
palette[i] <<= 2;
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2020-08-20 16:35:52 +00:00
|
|
|
memset(CurFrame, 0, 4); //sizeof(CurFrame));
|
2019-08-31 07:47:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case kFrameSound:
|
|
|
|
{
|
2020-08-20 15:52:56 +00:00
|
|
|
auto sfxx = soundEngine->GetSounds();
|
|
|
|
auto buffer = fp.Read(nSize);
|
2020-08-20 16:35:52 +00:00
|
|
|
assert(buffer.Size() == kSampleSize);
|
|
|
|
auto wbuffer = audio.samples + audio.nWrite * kSampleSize;
|
2020-08-20 15:52:56 +00:00
|
|
|
for (int i = 0; i < 2205; i++)
|
2019-12-05 21:18:34 +00:00
|
|
|
{
|
2020-08-20 15:52:56 +00:00
|
|
|
wbuffer[i] = (buffer[i] - 128) << 8;
|
2019-12-05 21:18:34 +00:00
|
|
|
}
|
2020-08-20 15:52:56 +00:00
|
|
|
audio.nWrite++;
|
|
|
|
if (audio.nWrite >= numAudioBlocks) audio.nWrite = 0;
|
2019-08-31 07:47:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case kFrameImage:
|
|
|
|
{
|
2020-08-20 16:35:52 +00:00
|
|
|
if (nSize == 0)
|
|
|
|
{
|
2019-08-31 07:47:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-20 16:35:52 +00:00
|
|
|
uint8_t* pFrame = CurFrame;
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2019-11-24 12:59:36 +00:00
|
|
|
int nRead = fp.Read(&yOffset, sizeof(yOffset));
|
2019-08-31 07:47:15 +00:00
|
|
|
nSize -= nRead;
|
|
|
|
|
|
|
|
pFrame += yOffset * 200; // row position
|
|
|
|
|
|
|
|
while (nSize > 0)
|
|
|
|
{
|
2019-11-24 12:59:36 +00:00
|
|
|
fp.Read(&xOffset, sizeof(xOffset));
|
|
|
|
fp.Read(&nPixels, sizeof(nPixels));
|
2019-08-31 07:47:15 +00:00
|
|
|
nSize -= 2;
|
|
|
|
|
|
|
|
pFrame += xOffset;
|
|
|
|
|
|
|
|
if (nPixels)
|
|
|
|
{
|
2019-11-24 12:59:36 +00:00
|
|
|
int nRead = fp.Read(pFrame, nPixels);
|
2019-08-31 07:47:15 +00:00
|
|
|
pFrame += nRead;
|
|
|
|
nSize -= nRead;
|
|
|
|
}
|
|
|
|
}
|
2020-08-20 19:05:14 +00:00
|
|
|
animtex.SetFrame(palette, CurFrame);
|
2019-08-31 07:47:15 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kFrameDone:
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-20 16:35:52 +00:00
|
|
|
}
|
2019-08-31 07:47:15 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-05 21:18:34 +00:00
|
|
|
|
|
|
|
|
2020-08-20 16:35:52 +00:00
|
|
|
static bool StreamCallbackFunc(SoundStream* stream, void* buff, int len, void* userdata)
|
|
|
|
{
|
|
|
|
LMFPlayer* pId = (LMFPlayer*)userdata;
|
|
|
|
memcpy(buff, &pId->audio.samples[pId->audio.nRead], len);
|
|
|
|
pId->audio.nRead += len / 2;
|
|
|
|
if (pId->audio.nRead >= countof(pId->audio.samples)) pId->audio.nRead = 0;
|
|
|
|
return true;
|
2019-12-05 21:18:34 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 16:35:52 +00:00
|
|
|
void Open(FileReader& fp)
|
|
|
|
{
|
2020-08-20 15:52:56 +00:00
|
|
|
|
2020-08-20 16:35:52 +00:00
|
|
|
uint8_t header[32];
|
|
|
|
fp.Read(header, sizeof(header));
|
|
|
|
audio.nWrite = 5; // play 5 blocks (i.e. half a second) of silence to get ahead of the stream. For this video it isn't necessary to sync it perfectly.
|
2020-08-20 15:52:56 +00:00
|
|
|
|
2020-08-20 16:35:52 +00:00
|
|
|
// start audio playback
|
|
|
|
stream = S_CreateCustomStream(kSampleSize * 2, kSampleRate, 1, StreamCallbackFunc, this); // size must be doubled here or dropouts can be heard.
|
2020-08-20 19:05:14 +00:00
|
|
|
animtex.SetSize(AnimTexture::Paletted, 200, 320);
|
2020-08-20 16:35:52 +00:00
|
|
|
}
|
2020-02-29 11:55:12 +00:00
|
|
|
|
2020-08-20 19:05:14 +00:00
|
|
|
void Close()
|
2020-08-20 16:35:52 +00:00
|
|
|
{
|
2020-08-20 19:05:14 +00:00
|
|
|
S_StopCustomStream(stream);
|
2020-08-20 16:35:52 +00:00
|
|
|
}
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2020-08-20 19:05:14 +00:00
|
|
|
AnimTextures& animTex()
|
2020-08-20 16:35:52 +00:00
|
|
|
{
|
2020-08-20 19:05:14 +00:00
|
|
|
return animtex;
|
2020-08-20 16:35:52 +00:00
|
|
|
}
|
|
|
|
};
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-04-25 18:02:40 +00:00
|
|
|
#if 0
|
2020-08-20 19:05:14 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2021-04-16 18:08:20 +00:00
|
|
|
class DLmfPlayer : public DSkippableScreenJob
|
2020-08-20 19:05:14 +00:00
|
|
|
{
|
|
|
|
LMFPlayer decoder;
|
2020-08-20 15:52:56 +00:00
|
|
|
double angle = 1536;
|
|
|
|
double z = 0;
|
2020-08-20 19:05:14 +00:00
|
|
|
uint64_t nextclock = 0, lastclock = 0;
|
|
|
|
FileReader fp;
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2020-08-20 19:05:14 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
DLmfPlayer(FileReader& fr)
|
|
|
|
{
|
|
|
|
decoder.Open(fr);
|
|
|
|
lastclock = 0;
|
|
|
|
nextclock = 0;
|
|
|
|
fp = std::move(fr);
|
2021-04-16 18:08:20 +00:00
|
|
|
pausable = false;
|
2020-08-20 19:05:14 +00:00
|
|
|
}
|
2019-10-12 21:09:55 +00:00
|
|
|
|
2020-08-20 19:05:14 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
2020-08-20 15:52:56 +00:00
|
|
|
|
2021-04-16 18:08:20 +00:00
|
|
|
void Draw(double smoothratio) override
|
2019-08-31 07:47:15 +00:00
|
|
|
{
|
2021-04-16 18:08:20 +00:00
|
|
|
uint64_t clock = (ticks + smoothratio) * 1'000'000'000. / GameTicRate;
|
2020-08-20 19:05:14 +00:00
|
|
|
if (clock >= nextclock)
|
2019-08-31 07:47:15 +00:00
|
|
|
{
|
2020-08-20 19:05:14 +00:00
|
|
|
nextclock += 100'000'000;
|
|
|
|
if (decoder.ReadFrame(fp) == 0)
|
|
|
|
{
|
2021-04-16 18:08:20 +00:00
|
|
|
state = finished;
|
|
|
|
return;
|
2019-08-31 07:47:15 +00:00
|
|
|
}
|
2020-08-20 19:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double duration = (clock - lastclock) * double(120. / 8'000'000'000);
|
|
|
|
if (z < 65536) { // Zoom - normal zoom is 65536.
|
|
|
|
z += 2048 * duration;
|
|
|
|
}
|
|
|
|
if (z > 65536) z = 65536;
|
|
|
|
if (angle != 0) {
|
|
|
|
angle += 16. * duration;
|
|
|
|
if (angle >= 2048) {
|
|
|
|
angle = 0;
|
2019-08-31 07:47:15 +00:00
|
|
|
}
|
2020-08-20 19:05:14 +00:00
|
|
|
}
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2020-08-20 19:05:14 +00:00
|
|
|
{
|
|
|
|
twod->ClearScreen();
|
2020-08-24 18:25:53 +00:00
|
|
|
DrawTexture(twod, decoder.animTex().GetFrame(), 160, 100, DTA_FullscreenScale, FSMode_Fit320x200,
|
2021-03-31 23:42:22 +00:00
|
|
|
DTA_CenterOffset, true, DTA_FlipY, true, DTA_ScaleX, z / 65536., DTA_ScaleY, z / 65536., DTA_Rotate, (-angle - 512) * BAngToDegree, TAG_DONE);
|
2020-08-20 19:05:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lastclock = clock;
|
|
|
|
}
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2020-08-20 19:05:14 +00:00
|
|
|
void OnDestroy() override
|
|
|
|
{
|
|
|
|
decoder.Close();
|
|
|
|
fp.Close();
|
|
|
|
}
|
|
|
|
};
|
2020-08-20 15:52:56 +00:00
|
|
|
|
|
|
|
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2020-08-20 19:05:14 +00:00
|
|
|
DScreenJob* PlayMovie(const char* fileName)
|
|
|
|
{
|
|
|
|
// clear keys
|
|
|
|
|
|
|
|
auto fp = fileSystem.OpenFileReader(fileName);
|
|
|
|
if (!fp.isOpen())
|
|
|
|
{
|
2021-04-15 22:11:02 +00:00
|
|
|
return Create<DBlackScreen>(1);
|
2020-08-20 19:05:14 +00:00
|
|
|
}
|
|
|
|
char buffer[4];
|
|
|
|
fp.Read(buffer, 4);
|
|
|
|
if (memcmp(buffer, "LMF ", 4))
|
|
|
|
{
|
|
|
|
fp.Close();
|
|
|
|
// Allpw replacement with more modern formats.
|
|
|
|
return PlayVideo(fileName);
|
|
|
|
}
|
|
|
|
fp.Seek(0, FileReader::SeekSet);
|
|
|
|
return Create<DLmfPlayer>(fp);
|
2019-08-26 03:59:14 +00:00
|
|
|
}
|
2021-04-25 18:02:40 +00:00
|
|
|
#endif
|
2020-07-23 14:35:54 +00:00
|
|
|
|
2019-11-22 23:11:37 +00:00
|
|
|
END_PS_NS
|